Skip to content

Commit

Permalink
deps: V8: cherry-pick 4c29cf1b7885
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
targos committed Apr 12, 2022
1 parent a052c03 commit 004137e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

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

##### V8 defaults for Node.js #####

Expand Down
3 changes: 3 additions & 0 deletions deps/v8/src/heap/heap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4283,6 +4283,9 @@ void Heap::AutomaticallyRestoreInitialHeapLimit(double threshold_percent) {

bool Heap::InvokeNearHeapLimitCallback() {
if (near_heap_limit_callbacks_.size() > 0) {
AllowGarbageCollection allow_gc;
TRACE_GC(tracer(), GCTracer::Scope::HEAP_EXTERNAL_NEAR_HEAP_LIMIT);
VMState<EXTERNAL> callback_state(isolate());
HandleScope scope(isolate());
v8::NearHeapLimitCallback callback =
near_heap_limit_callbacks_.back().first;
Expand Down
1 change: 1 addition & 0 deletions deps/v8/src/init/heap-symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@
F(HEAP_EPILOGUE_REDUCE_NEW_SPACE) \
F(HEAP_EPILOGUE_SAFEPOINT) \
F(HEAP_EXTERNAL_EPILOGUE) \
F(HEAP_EXTERNAL_NEAR_HEAP_LIMIT) \
F(HEAP_EXTERNAL_PROLOGUE) \
F(HEAP_EXTERNAL_WEAK_GLOBAL_HANDLES) \
F(HEAP_PROLOGUE) \
Expand Down
56 changes: 56 additions & 0 deletions deps/v8/test/cctest/heap/test-heap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,62 @@ UNINITIALIZED_TEST(Regress10843) {
isolate->Dispose();
}

size_t near_heap_limit_invocation_count = 0;
size_t InvokeGCNearHeapLimitCallback(void* data, size_t current_heap_limit,
size_t initial_heap_limit) {
near_heap_limit_invocation_count++;
if (near_heap_limit_invocation_count > 1) {
// We are already in a GC triggered in this callback, raise the limit
// to avoid an OOM.
return current_heap_limit * 5;
}

DCHECK_EQ(near_heap_limit_invocation_count, 1);
// Operations that may cause GC (e.g. taking heap snapshots) in the
// near heap limit callback should not hit the AllowGarbageCollection
// assertion.
static_cast<v8::Isolate*>(data)->GetHeapProfiler()->TakeHeapSnapshot();
return current_heap_limit * 5;
}

UNINITIALIZED_TEST(Regress12777) {
v8::Isolate::CreateParams create_params;
create_params.constraints.set_max_old_generation_size_in_bytes(10 * i::MB);
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);

isolate->AddNearHeapLimitCallback(InvokeGCNearHeapLimitCallback, isolate);

{
v8::Isolate::Scope isolate_scope(isolate);

Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate);
// Allocate data to trigger the NearHeapLimitCallback.
HandleScope scope(i_isolate);
int length = 2 * i::MB / i::kTaggedSize;
std::vector<Handle<FixedArray>> arrays;
for (int i = 0; i < 5; i++) {
arrays.push_back(i_isolate->factory()->NewFixedArray(length));
}
CcTest::CollectAllGarbage(i_isolate);
for (int i = 0; i < 5; i++) {
arrays.push_back(i_isolate->factory()->NewFixedArray(length));
}
CcTest::CollectAllGarbage(i_isolate);
for (int i = 0; i < 5; i++) {
arrays.push_back(i_isolate->factory()->NewFixedArray(length));
}

// The work done above should trigger the heap limit callback at least
// twice to prove that the callback can raise the limit in the second
// or later calls to avoid an OOM.
CHECK_GE(near_heap_limit_invocation_count, 2);
}

isolate->GetHeapProfiler()->DeleteAllHeapSnapshots();
isolate->Dispose();
}

#ifndef V8_LITE_MODE

TEST(TestOptimizeAfterBytecodeFlushingCandidate) {
Expand Down

0 comments on commit 004137e

Please sign in to comment.