Skip to content

Commit 004137e

Browse files
committed
deps: V8: cherry-pick 4c29cf1b7885
Original commit message: [heap] fix invocation of NearHeapLimitCallback This patch makes sure that NearHeapLimitCallback can invoke operations that trigger garbage collections. In addition this adds code to make the tracers aware of NearHeapLimitCallback. Bug: v8:12777 Change-Id: I959a23a3e0224ba536cb18b14933813e56fc5292 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3575468 Reviewed-by: Michael Lippautz <mlippautz@chromium.org> Commit-Queue: Joyee Cheung <joyee@igalia.com> Cr-Commit-Position: refs/heads/main@{#79934} Refs: v8/v8@4c29cf1 PR-URL: #42657 Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
1 parent a052c03 commit 004137e

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
# Reset this number to 0 on major V8 upgrades.
3838
# Increment by one for each non-official patch applied to deps/v8.
39-
'v8_embedder_string': '-node.10',
39+
'v8_embedder_string': '-node.11',
4040

4141
##### V8 defaults for Node.js #####
4242

deps/v8/src/heap/heap.cc

+3
Original file line numberDiff line numberDiff line change
@@ -4283,6 +4283,9 @@ void Heap::AutomaticallyRestoreInitialHeapLimit(double threshold_percent) {
42834283

42844284
bool Heap::InvokeNearHeapLimitCallback() {
42854285
if (near_heap_limit_callbacks_.size() > 0) {
4286+
AllowGarbageCollection allow_gc;
4287+
TRACE_GC(tracer(), GCTracer::Scope::HEAP_EXTERNAL_NEAR_HEAP_LIMIT);
4288+
VMState<EXTERNAL> callback_state(isolate());
42864289
HandleScope scope(isolate());
42874290
v8::NearHeapLimitCallback callback =
42884291
near_heap_limit_callbacks_.back().first;

deps/v8/src/init/heap-symbols.h

+1
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@
527527
F(HEAP_EPILOGUE_REDUCE_NEW_SPACE) \
528528
F(HEAP_EPILOGUE_SAFEPOINT) \
529529
F(HEAP_EXTERNAL_EPILOGUE) \
530+
F(HEAP_EXTERNAL_NEAR_HEAP_LIMIT) \
530531
F(HEAP_EXTERNAL_PROLOGUE) \
531532
F(HEAP_EXTERNAL_WEAK_GLOBAL_HANDLES) \
532533
F(HEAP_PROLOGUE) \

deps/v8/test/cctest/heap/test-heap.cc

+56
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,62 @@ UNINITIALIZED_TEST(Regress10843) {
12711271
isolate->Dispose();
12721272
}
12731273

1274+
size_t near_heap_limit_invocation_count = 0;
1275+
size_t InvokeGCNearHeapLimitCallback(void* data, size_t current_heap_limit,
1276+
size_t initial_heap_limit) {
1277+
near_heap_limit_invocation_count++;
1278+
if (near_heap_limit_invocation_count > 1) {
1279+
// We are already in a GC triggered in this callback, raise the limit
1280+
// to avoid an OOM.
1281+
return current_heap_limit * 5;
1282+
}
1283+
1284+
DCHECK_EQ(near_heap_limit_invocation_count, 1);
1285+
// Operations that may cause GC (e.g. taking heap snapshots) in the
1286+
// near heap limit callback should not hit the AllowGarbageCollection
1287+
// assertion.
1288+
static_cast<v8::Isolate*>(data)->GetHeapProfiler()->TakeHeapSnapshot();
1289+
return current_heap_limit * 5;
1290+
}
1291+
1292+
UNINITIALIZED_TEST(Regress12777) {
1293+
v8::Isolate::CreateParams create_params;
1294+
create_params.constraints.set_max_old_generation_size_in_bytes(10 * i::MB);
1295+
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
1296+
v8::Isolate* isolate = v8::Isolate::New(create_params);
1297+
1298+
isolate->AddNearHeapLimitCallback(InvokeGCNearHeapLimitCallback, isolate);
1299+
1300+
{
1301+
v8::Isolate::Scope isolate_scope(isolate);
1302+
1303+
Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate);
1304+
// Allocate data to trigger the NearHeapLimitCallback.
1305+
HandleScope scope(i_isolate);
1306+
int length = 2 * i::MB / i::kTaggedSize;
1307+
std::vector<Handle<FixedArray>> arrays;
1308+
for (int i = 0; i < 5; i++) {
1309+
arrays.push_back(i_isolate->factory()->NewFixedArray(length));
1310+
}
1311+
CcTest::CollectAllGarbage(i_isolate);
1312+
for (int i = 0; i < 5; i++) {
1313+
arrays.push_back(i_isolate->factory()->NewFixedArray(length));
1314+
}
1315+
CcTest::CollectAllGarbage(i_isolate);
1316+
for (int i = 0; i < 5; i++) {
1317+
arrays.push_back(i_isolate->factory()->NewFixedArray(length));
1318+
}
1319+
1320+
// The work done above should trigger the heap limit callback at least
1321+
// twice to prove that the callback can raise the limit in the second
1322+
// or later calls to avoid an OOM.
1323+
CHECK_GE(near_heap_limit_invocation_count, 2);
1324+
}
1325+
1326+
isolate->GetHeapProfiler()->DeleteAllHeapSnapshots();
1327+
isolate->Dispose();
1328+
}
1329+
12741330
#ifndef V8_LITE_MODE
12751331

12761332
TEST(TestOptimizeAfterBytecodeFlushingCandidate) {

0 commit comments

Comments
 (0)