You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
src: detach cppgc wrappers from their Realm before it is freed - #65778
A vm.Script (or other cppgc-managed wrapper) that is garbage-collected shortly before its Environment is freed but destroyed afterwards writes into the freed Realm from ~CppgcMixin(); a Worker that compiles a few scripts, gets a GC from memory pressure and calls process.exit() is enough, and ASAN reports a heap-use-after-free write in ~ContextifyScript. Realm cleanup reached wrappers only through weak persistents, which V8 clears when it finds a wrapper dead, long before lazy sweeping runs the destructor, so those wrappers kept their Realm pointer.
The Realm pointer now lives in the off-heap list node that the wrapper owns and frees in its destructor, and Realm cleanup clears it on every node (finalizing only wrappers that are still alive), so a collected wrapper's destructor finds no Realm instead of a freed one. The purge flag, its GC epilogue callback and PurgeEmpty() are removed, which also fixes the list nodes of wrappers alive at FreeEnvironment() leaking.
Tests: CppgcTest.VmScriptCollectedBeforeFreeEnvironmentSweptAfter (use-after-free under ASAN before), CleanIsNotCalledWithFreedRealm, WrappersAliveAtFreeEnvironmentDoNotLeak in test_cppgc.cc.
A
vm.Script(or other cppgc-managed wrapper) that is garbage-collected shortly before its Environment is freed but destroyed afterwards writes into the freed Realm from~CppgcMixin(); a Worker that compiles a few scripts, gets a GC from memory pressure and callsprocess.exit()is enough, and ASAN reports a heap-use-after-free write in~ContextifyScript. Realm cleanup reached wrappers only through weak persistents, which V8 clears when it finds a wrapper dead, long before lazy sweeping runs the destructor, so those wrappers kept their Realm pointer.The Realm pointer now lives in the off-heap list node that the wrapper owns and frees in its destructor, and Realm cleanup clears it on every node (finalizing only wrappers that are still alive), so a collected wrapper's destructor finds no Realm instead of a freed one. The purge flag, its GC epilogue callback and
PurgeEmpty()are removed, which also fixes the list nodes of wrappers alive atFreeEnvironment()leaking.Tests:
CppgcTest.VmScriptCollectedBeforeFreeEnvironmentSweptAfter(use-after-free under ASAN before),CleanIsNotCalledWithFreedRealm,WrappersAliveAtFreeEnvironmentDoNotLeakin test_cppgc.cc.Refs: #56534
Disclosure: the code, tests and this description were written by Claude Code, directed and reviewed by @codebytere.