Reland "[heap] Allow shared references in WeakMap"

This is a reland of a183895687

Now that https://crrev.com/c/3485678 landed and fixed the deadlock
in the linked bug, we can reland this CL without changes.

Original change's description:
> [heap] Allow shared references in WeakMap
>
> Shared references can also be stored in WeakMaps and during marking we
> need to be able to deal with such references. In a client GC shared
> objects are treated as live, so we don't need to update or check mark
> bits for such objects.
>
> Bug: v8:11708
> Change-Id: I0dbf797472c4779f462750dab63cc9b012aad091
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3447365
> Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
> Commit-Queue: Dominik Inführ <dinfuehr@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#79153}

Bug: v8:11708, v8:12642
Change-Id: I5945a16255647c897a1df834267137bf73b6207f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3485679
Auto-Submit: Dominik Inführ <dinfuehr@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79267}
This commit is contained in:
Dominik Inführ 2022-02-17 11:40:07 +01:00 committed by V8 LUCI CQ
parent 1bf1aa4c1b
commit 7768e9347b
3 changed files with 34 additions and 4 deletions

View File

@ -2917,12 +2917,16 @@ void MarkCompactCollector::ClearWeakCollections() {
if (FLAG_verify_heap) {
Object value = table.ValueAt(i);
if (value.IsHeapObject()) {
CHECK_IMPLIES(non_atomic_marking_state()->IsBlackOrGrey(key),
non_atomic_marking_state()->IsBlackOrGrey(
HeapObject::cast(value)));
HeapObject heap_object = HeapObject::cast(value);
CHECK_IMPLIES(
(!is_shared_heap_ && key.InSharedHeap()) ||
non_atomic_marking_state()->IsBlackOrGrey(key),
(!is_shared_heap_ && heap_object.InSharedHeap()) ||
non_atomic_marking_state()->IsBlackOrGrey(heap_object));
}
}
#endif
if (!is_shared_heap_ && key.InSharedHeap()) continue;
if (!non_atomic_marking_state()->IsBlackOrGrey(key)) {
table.RemoveEntry(i);
}

View File

@ -355,7 +355,8 @@ int MarkingVisitorBase<ConcreteVisitor, MarkingState>::VisitEphemeronHashTable(
ObjectSlot value_slot =
table.RawFieldOfElementAt(EphemeronHashTable::EntryToValueIndex(i));
if (concrete_visitor()->marking_state()->IsBlackOrGrey(key)) {
if ((!is_shared_heap_ && key.InSharedHeap()) ||
concrete_visitor()->marking_state()->IsBlackOrGrey(key)) {
VisitPointer(table, value_slot);
} else {
Object value_obj = table.ValueAt(i);
@ -366,6 +367,8 @@ int MarkingVisitorBase<ConcreteVisitor, MarkingState>::VisitEphemeronHashTable(
concrete_visitor()->RecordSlot(table, value_slot, value);
AddWeakReferenceForReferenceSummarizer(table, value);
if (!is_shared_heap_ && value.InSharedHeap()) continue;
// Revisit ephemerons with both key and value unreachable at end
// of concurrent marking cycle.
if (concrete_visitor()->marking_state()->IsWhite(value)) {

View File

@ -0,0 +1,23 @@
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Flags: --shared-string-table --allow-natives-syntax --stress-compaction --expose-gc
const val1 = "some value";
assertTrue(%IsSharedString(val1));
const wm = new WeakMap();
const key1 = {};
wm.set(key1, val1);
assertTrue(wm.get(key1) == val1);
assertTrue(%IsSharedString(wm.get(key1)));
gc();
assertTrue(wm.get(key1) == val1);
assertTrue(%IsSharedString(wm.get(key1)));
%SharedGC();
assertTrue(wm.get(key1) == val1);
assertTrue(%IsSharedString(wm.get(key1)));