Repository navigation
fix: use-after-free of a freed Realm by cppgc wrapper destructors at exit - #53560
Conversation
|
nodejs/node#65778 exposes a valid memory safety issue but that also voids the behavior of clean when the wrapper is unreachable and realm shutdown happens, today there are no custom implementation of clean. What if we have something like instead of a clean override on the mixin, it provides a cleanup object. This guarantees a cleanup hook when realm shutdown or wrapper destruction whichever happens first, the cleanup object however cannot or should not access an on heap objects. class RealmCleanupState {
public:
virtual ~RealmCleanupState() = default;
void RunOnce(Realm& realm) noexcept {
if (status_ == Completed)
return;
CHECK(status_ == Pending);
status_ = Running;
Clean(realm);
status_ = Completed;
}
protected:
// Contract:
// - No wrapper pointer, GC objects, or V8 handles.
// - No JavaScript, GC triggering operations, or nested event loops.
// - Must not destroy this state, its node, or the Realm reentrantly.
// - Required Realm state must still be available.
virtual void Clean(Realm& realm) = 0;
private:
enum Status { Pending, Running, Completed };
Status status_ = Pending;
}; |
…exit Cherry-picks nodejs/node#65778 (landed upstream as nodejs/node@c909c63be3). Realm::RunCleanup() detaches the cppgc wrappers it tracks (vm.Script, vm contexts) through weak persistents, which the GC clears as soon as it finds a wrapper unreachable. With lazy/concurrent sweeping the wrapper's destructor can run much later, so one collected shortly before FreeEnvironment() and swept after it kept its Realm pointer, and ~CppgcMixin() wrote into the freed Realm. In Electron this showed up as an ASan heap-use-after-free in ~ContextifyScript during JavascriptEnvironment teardown at process exit. The Realm pointer now lives in the off-heap list node, which cleanup clears for every entry, so a late destructor sees no Realm instead of a freed one.
88b23fd to
1f99699
Compare
|
Release Notes Persisted
|
|
I have automatically backported this PR to "44-x-y", please check out #54266 |
|
I have automatically backported this PR to "45-x-y", please check out #54267 |
|
I have automatically backported this PR to "43-x-y", please check out #54268 |
Description of Change
Cherry-picks nodejs/node#65778 (landed upstream as nodejs/node@c909c63, not yet in a v24.x release) as a Node patch. The plan is to drop the patch once it rolls in with a Node release.
Realm::RunCleanup()detaches the cppgc-managed wrappers it tracks (vm.Script, vm contexts) throughcppgc::WeakPersistents. The GC clears those in the atomic pause that finds a wrapper dead, but with lazy/concurrent sweeping the wrapper's destructor can run much later. A wrapper collected shortly beforeFreeEnvironment()and swept after it was skipped by cleanup, kept itsRealm*, and~CppgcMixin()then wrote into the freed Realm.node::EnvironmentinPostMainMessageLoopRunand then deletes the isolate, whose teardown finishes any pending cppgc sweep, so this surfaced as an ASanheap-use-after-freein~ContextifyScriptat exit on thelinux-x64-asanshard (run, and still recurring, e.g. 45-x-y) after all tests had passed.CppgcWrapperListNode, which the wrapper owns; cleanup clears it for every entry, live or already collected, so a late destructor sees no Realm rather than a freed one. The purge flag, its GC epilogue callback andPurgeEmpty()go away, which also stops the list nodes of wrappers still alive atFreeEnvironment()from leaking. Upstream's cctests are included in the patch.cppgc_helpers-inl.hfrom upstream's embedder-data-tag change, which our Node does not have.Verified locally on an ASan build with instrumentation: before, every run left hundreds of collected-but-unswept
vm.Scriptwrappers holding a staleRealm*pastRunCleanup(); after, none do.Checklist
npm testpassesRelease Notes
Notes: Fixed a possible crash during process exit when
vm.Scriptorvmcontexts created in the main process had been garbage collected shortly before quitting.