Repository navigation
Conversation
The emitter of a receiver that is not a native emitter can have this._events as its only reference. The conversion of the event name runs user code, which can drop that reference and collect. The method then used a freed emitter.
|
Status Reproduced on main 7ca0042 and on bun 1.4.4 (linux x64) with the script in the description: an emitter that is reachable only through an accessor
With this change the answers are The tests also pass on a debug build of main. A debug build keeps the pointer to the emitter in a stack slot for the whole call, so the defect shows only in an optimized build. CI. Build 138221 finished red from 28 test files: 27 in |
|
No actionable comments were generated in the recent review. Walkthrough
ChangesNative EventEmitter lifetime
Suggested reviewers: Merge Risk:
|
There was a problem hiding this comment.
LGTM — a two-line keep-alive in the one shared trampoline, with a subprocess regression test that pins the exact output.
What was reviewed:
- Confirmed
jsEventEmitterCastFastis the only native site that resolvesthis._events, and all 15 prototype host functions inJSEventEmitter.cppgo throughIDLOperation<JSEventEmitter>::call, so rootingthisObjectthere coversremoveListener/offand the other methods not in the test matrix. - Checked the method bodies: each keeps only
castedThis->wrapped()acrosstoPropertyKey/listener conversion, so rooting the wrapper cell (whose destructor releases the impl) is sufficient;EnsureStillAliveScopeis the same idiom these bindings already use for arguments. - Test:
bunExe/bunEnv,await using, stdout/stderr/exit drained concurrently, exacttoEqualon the combined result; the known limitation that debug lanes pass with or without the fix is acceptable since release lanes fail on the unfixed build.
Extended reasoning...
The diff adds JSC::EnsureStillAliveScope keepEmitterAlive(thisObject) in src/jsc/bindings/webcore/JSEventEmitterCustom.h before the operation call, plus a five-case test.concurrent.each in test/js/node/events/event-emitter.test.ts that spawns bun -e and asserts the emitter survives Bun.gc(true) triggered from the event-name toString. It touches no security-sensitive surface (no auth, crypto, or input parsing); it is a GC-rooting fix for a native EventEmitter wrapper that was reachable only via a user getter. Approve was decided by the change being tiny, placed at the single owning layer that every method shares, using an established JSC idiom, with no CODEOWNER on either file, no third-party objections, and the bug hunt exiting on dry_streak.
Problem
processaccept any object asthis. For athisthat is not a native emitter, they use the native emitter inthis._events. Nothing holds that emitter during the call (JSEventEmitterCustom.h:23).listenerCount,listeners,rawListeners,emitandremoveAllListeners(name)then use a freed emitter.AddressSanitizer: heap-use-after-freeinWebCore::EventEmitter::listenerCount. A release build gives wrong answers, andremoveAllListeners(name)ends inpanic(main thread): abort() called.Fix
IDLOperation<JSEventEmitter>::callholds the emitter withJSC::EnsureStillAliveScopeuntil the method returns. All 15 methods go through that function.test/js/node/events/event-emitter.test.ts. They fail on release and release ASAN builds of main.this._eventsonly from the methods that assign #42575: 1 concern raised, 1 addressed (Notes).Background
process. The firston()call on another receiver builds an emitter and stores it asreceiver._events.EnsureStillAliveScopekeeps a cell where the collector looks for roots, until the scope ends.Downsides
processitself.process.listenerCount("x")123 to 126,process.emit("x", 1)224 to 228,process.on("x", f)1,791 to 1,796.Notes
Repro. Run on main 7ca0042 and on bun 1.4.4, which has the same emitter sources. The emitter is reachable only through an accessor, and the calls run in a later task, so that no frame of the script holds it.
listenerCountemitremoveAllListeners(name)0false, the listener does not runpanic(main thread): abort() called, exit code 134Malloc=1heap-use-after-free, READ of size 8heap-use-after-freeheap-use-after-free, WRITE of size 11true, the listener runsthisThe ASAN report for an emitter that a plain property holds (
process.on.call(o, "x", f), thenprocess.listenerCount.call(o, { toString() { delete o._events; Bun.gc(true); return "x" } })):Why the debug build does not show it. A method body keeps only
castedThis->wrapped()across the conversion. An optimized build drops the pointer to the wrapper cell, the collector frees the cell, and its destructor releases the emitter. A debug build keeps every local in a stack slot for the whole call, so the collector still finds the cell. The 5 new tests pass on a debug build of main (5 of 5) and fail on a release build of main (0 of 5) and on a release ASAN build of main (0 of 5). With this change they pass on the three builds.Cost per call. Instructions inside the host function, from its first instruction to its return, on release builds of main and of this branch. The count comes from
ptrace(PTRACE_SINGLESTEP), becauseperfandvalgrindare not available on the machine. Each row is calls 40 to 49 of a loop, and the count is the same for the 10 calls.process.listenerCount("x")process.emit("x", 1), no listenerprocess.emit("x", 1), one listenerprocess.on("x", f)listenerCount.call(o, "x"),ohas an emitter in_eventsemit("x", 1)onObject.create(process), one listenersize bun-profilegives 140,315,585 bytes of text before and after.Relation to other changes.
this._eventsonly from the methods that assign #42575 carried together with behavior changes. The self-review of Native EventEmitter methods: hold the emitter during the call, and writethis._eventsonly from the methods that assign #42575 asked for it as a change of its own, and found that the test there did not fail when the keep-alive was removed from that branch. The tests here use an accessor for_eventsand fail on every release build of main._maxListenersand followevents.defaultMaxListeners#41599, process: filter listenerCount(event, fn) and run events.errorMonitor listeners #41830, process: run a once() listener one time under a re-entrant emit #47120, process: keep a listener that is removed during an emit until the emit returns #47121, process: return the once() wrapper from rawListeners() #47122, AOT: front end, test runs and tests for the rewritten compiler #47942, fix(process): propagate synchronous emit listener exceptions #45021 and process: emit 'beforeExit' and 'exit' through a replaced process.emit #32228, which edit the same files.this._eventsonly from the methods that assign #42575).Suites run.
test/js/node/events/event-emitter.test.tson the debug build (102 pass, also withBUN_JSC_validateExceptionChecks=1), the release build (102 pass) and the release ASAN build (102 pass). On the debug build also 28 files oftest/js/node/test/parallel/(test-event-emitter-*.js,test-process-emit*.js).