@@ -853,9 +853,7 @@ TEST(TestRemoveUnregisterToken) {
853853
854854 finalization_registry->RemoveUnregisterToken (
855855 JSReceiver::cast (*token2), isolate,
856- [undefined](WeakCell matched_cell) {
857- matched_cell.set_unregister_token (*undefined);
858- },
856+ JSFinalizationRegistry::kKeepMatchedCellsInRegistry ,
859857 [](HeapObject, ObjectSlot, Object) {});
860858
861859 // Both weak_cell2a and weak_cell2b remain on the weak cell chains.
@@ -1025,5 +1023,52 @@ TEST(UnregisterTokenHeapVerifier) {
10251023 EmptyMessageQueues (isolate);
10261024}
10271025
1026+ TEST (UnregisteredAndUnclearedCellHeapVerifier) {
1027+ if (!FLAG_incremental_marking) return ;
1028+ ManualGCScope manual_gc_scope;
1029+ #ifdef VERIFY_HEAP
1030+ FLAG_verify_heap = true ;
1031+ #endif
1032+
1033+ CcTest::InitializeVM ();
1034+ v8::Isolate* isolate = CcTest::isolate ();
1035+ Heap* heap = CcTest::heap ();
1036+ v8::HandleScope outer_scope (isolate);
1037+
1038+ {
1039+ // Make a new FinalizationRegistry and register an object with a token.
1040+ v8::HandleScope scope (isolate);
1041+ CompileRun (
1042+ " var token = {}; "
1043+ " var registry = new FinalizationRegistry(function () {}); "
1044+ " registry.register({}, undefined, token);" );
1045+ }
1046+
1047+ // Start incremental marking to activate the marking barrier.
1048+ heap::SimulateIncrementalMarking (heap, false );
1049+
1050+ {
1051+ // Make a WeakCell list with length >1, then unregister with the token to
1052+ // the WeakCell from the registry. The linked list manipulation keeps the
1053+ // unregistered WeakCell alive (i.e. not put into cleared_cells) due to the
1054+ // marking barrier from incremental marking. Then make the original token
1055+ // collectible.
1056+ v8::HandleScope scope (isolate);
1057+ CompileRun (
1058+ " registry.register({}); "
1059+ " registry.unregister(token); "
1060+ " token = 0;" );
1061+ }
1062+
1063+ // Trigger GC.
1064+ CcTest::CollectAllGarbage ();
1065+ CcTest::CollectAllGarbage ();
1066+
1067+ // Pump message loop to run the finalizer task, then the incremental marking
1068+ // task. The verifier will verify that live WeakCells don't point to dead
1069+ // unregister tokens.
1070+ EmptyMessageQueues (isolate);
1071+ }
1072+
10281073} // namespace internal
10291074} // namespace v8
0 commit comments