Skip to content

Commit 85f3230

Browse files
committed
v8: avoid truncating cppgc heap statistics
Convert cppgc size_t counters directly to JavaScript Numbers instead of narrowing them to uint32_t. This prevents v8.getCppHeapStatistics() values from wrapping when a cppgc heap statistic exceeds 4 GiB. Signed-off-by: Archkon <180910180+Archkon@users.noreply.github.com>
1 parent a609b17 commit 85f3230

1 file changed

Lines changed: 18 additions & 23 deletions

File tree

src/node_v8.cc

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,10 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
463463
const cppgc::HeapStatistics::ObjectStatsEntry& object_stats =
464464
page_stats.object_statistics[k];
465465
MaybeLocal<Value> object_stats_values[] = {
466-
Uint32::NewFromUnsigned(
467-
isolate, static_cast<uint32_t>(object_stats.allocated_bytes)),
468-
Uint32::NewFromUnsigned(
469-
isolate, static_cast<uint32_t>(object_stats.object_count))};
466+
Number::New(isolate,
467+
static_cast<double>(object_stats.allocated_bytes)),
468+
Number::New(isolate,
469+
static_cast<double>(object_stats.object_count))};
470470
Local<Object> object_stats_object;
471471
if (!NewDictionaryInstanceNullProto(
472472
context, object_stats_template, object_stats_values)
@@ -478,12 +478,11 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
478478

479479
// Set page statistics
480480
MaybeLocal<Value> page_stats_values[] = {
481-
Uint32::NewFromUnsigned(
482-
isolate, static_cast<uint32_t>(page_stats.committed_size_bytes)),
483-
Uint32::NewFromUnsigned(
484-
isolate, static_cast<uint32_t>(page_stats.resident_size_bytes)),
485-
Uint32::NewFromUnsigned(
486-
isolate, static_cast<uint32_t>(page_stats.used_size_bytes)),
481+
Number::New(isolate,
482+
static_cast<double>(page_stats.committed_size_bytes)),
483+
Number::New(isolate,
484+
static_cast<double>(page_stats.resident_size_bytes)),
485+
Number::New(isolate, static_cast<double>(page_stats.used_size_bytes)),
487486
Array::New(isolate,
488487
object_statistics_array.data(),
489488
object_statistics_array.size())};
@@ -521,15 +520,14 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
521520
}
522521
MaybeLocal<Value> space_stats_values[] = {
523522
name_value,
524-
Uint32::NewFromUnsigned(
523+
Number::New(
525524
isolate,
526-
static_cast<uint32_t>(stats.space_stats[i].committed_size_bytes)),
527-
Uint32::NewFromUnsigned(
525+
static_cast<double>(stats.space_stats[i].committed_size_bytes)),
526+
Number::New(
528527
isolate,
529-
static_cast<uint32_t>(stats.space_stats[i].resident_size_bytes)),
530-
Uint32::NewFromUnsigned(
531-
isolate,
532-
static_cast<uint32_t>(stats.space_stats[i].used_size_bytes)),
528+
static_cast<double>(stats.space_stats[i].resident_size_bytes)),
529+
Number::New(isolate,
530+
static_cast<double>(stats.space_stats[i].used_size_bytes)),
533531
Array::New(isolate,
534532
page_statistics_array.data(),
535533
page_statistics_array.size()),
@@ -550,12 +548,9 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
550548
return MaybeLocal<Object>();
551549
}
552550
MaybeLocal<Value> heap_statistics_values[] = {
553-
Uint32::NewFromUnsigned(
554-
isolate, static_cast<uint32_t>(stats.committed_size_bytes)),
555-
Uint32::NewFromUnsigned(isolate,
556-
static_cast<uint32_t>(stats.resident_size_bytes)),
557-
Uint32::NewFromUnsigned(isolate,
558-
static_cast<uint32_t>(stats.used_size_bytes)),
551+
Number::New(isolate, static_cast<double>(stats.committed_size_bytes)),
552+
Number::New(isolate, static_cast<double>(stats.resident_size_bytes)),
553+
Number::New(isolate, static_cast<double>(stats.used_size_bytes)),
559554
Array::New(isolate,
560555
space_statistics_array.data(),
561556
space_statistics_array.size()),

0 commit comments

Comments
 (0)