Skip to content

Commit b33a850

Browse files
ulanCommit Bot
authored andcommitted
[heap]: Make addition of detached contexts robust for GC
The (age, context) pair has to be added atomically in to the weak array of detached contexts. Otherwise, GC may happen after insertion of age and observe inconsistent state. Bug: chromium:1016703 Change-Id: Icb20bed4359904b2d976986a236558542e314bbf Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1895573 Commit-Queue: Ulan Degenbaev <[email protected]> Reviewed-by: Toon Verwaest <[email protected]> Cr-Commit-Position: refs/heads/master@{#64820}
1 parent 465c97f commit b33a850

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

src/execution/isolate.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4273,9 +4273,8 @@ void Isolate::AddDetachedContext(Handle<Context> context) {
42734273
HandleScope scope(this);
42744274
Handle<WeakArrayList> detached_contexts = factory()->detached_contexts();
42754275
detached_contexts = WeakArrayList::AddToEnd(
4276-
this, detached_contexts, MaybeObjectHandle(Smi::kZero, this));
4277-
detached_contexts = WeakArrayList::AddToEnd(this, detached_contexts,
4278-
MaybeObjectHandle::Weak(context));
4276+
this, detached_contexts, MaybeObjectHandle(Smi::kZero, this),
4277+
MaybeObjectHandle::Weak(context));
42794278
heap()->set_detached_contexts(*detached_contexts);
42804279
}
42814280

src/objects/fixed-array.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,12 @@ class WeakArrayList : public HeapObject {
338338
Isolate* isolate, Handle<WeakArrayList> array,
339339
const MaybeObjectHandle& value);
340340

341+
// A version that adds to elements. This ensures that the elements are
342+
// inserted atomically w.r.t GC.
343+
V8_EXPORT_PRIVATE static Handle<WeakArrayList> AddToEnd(
344+
Isolate* isolate, Handle<WeakArrayList> array,
345+
const MaybeObjectHandle& value1, const MaybeObjectHandle& value2);
346+
341347
inline MaybeObject Get(int index) const;
342348
inline MaybeObject Get(Isolate* isolate, int index) const;
343349

src/objects/objects.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3955,6 +3955,20 @@ Handle<WeakArrayList> WeakArrayList::AddToEnd(Isolate* isolate,
39553955
return array;
39563956
}
39573957

3958+
Handle<WeakArrayList> WeakArrayList::AddToEnd(Isolate* isolate,
3959+
Handle<WeakArrayList> array,
3960+
const MaybeObjectHandle& value1,
3961+
const MaybeObjectHandle& value2) {
3962+
int length = array->length();
3963+
array = EnsureSpace(isolate, array, length + 2);
3964+
// Reload length; GC might have removed elements from the array.
3965+
length = array->length();
3966+
array->Set(length, *value1);
3967+
array->Set(length + 1, *value2);
3968+
array->set_length(length + 2);
3969+
return array;
3970+
}
3971+
39583972
bool WeakArrayList::IsFull() { return length() == capacity(); }
39593973

39603974
// static
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2019 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// Flags: --expose-gc
6+
7+
let realms = [];
8+
for (let i = 0; i < 4; i++) {
9+
realms.push(Realm.createAllowCrossRealmAccess());
10+
}
11+
12+
for (let i = 0; i < 4; i++) {
13+
Realm.detachGlobal(realms[i]);
14+
gc();
15+
}

0 commit comments

Comments
 (0)