Repository navigation
Conversation
|
Thanks for working on this. I tested a backport of this PR onto quickjs-ng 0.15.1 in rquickjs 0.12.2, and this PR on the 0.16.2-derived engine in rquickjs 0.14.0. Both fix a cyclic top-level-await module use-after-free, once the embedding also releases its owned compile/read module values. The minimal teardown cases below produced this before/after result under AddressSanitizer on both versions:
A queued native job whose context owner was dropped before GC also stopped reporting use-after-free with this PR. Minimal Rust reproductionUse rquickjs with the fn release_pending_module(source: &str) {
let runtime = rquickjs::Runtime::new().unwrap();
let context = rquickjs::Context::full(&runtime).unwrap();
context.with(|ctx| {
let (module, promise) = rquickjs::Module::declare(ctx.clone(), "pending.mjs", source)
.unwrap()
.eval()
.unwrap();
ctx.globals().set("evaluation", promise).unwrap();
// Accessing an uninitialized export is expected to fail while TLA is pending.
assert!(module.get::<_, i32>("result").is_err());
});
while runtime.execute_pending_job().unwrap() {}
drop(context);
drop(runtime);
}
#[test]
fn pending_module_teardown() {
release_pending_module("await new Promise(() => {}); export const result = 1;");
release_pending_module(
"const p = new Promise(() => {}); p.self = p; await p; export const result = 1;",
);
release_pending_module(
"await (() => { const p = new Promise(() => {}); p.self = p; return p; })(); \
export const result = 1;",
);
}ASan settings for the original before/after experiment: nightly Rust, Embedding integration detail
Advisory: partly-built module on
|
Queued jobs keep their arguments alive, but currently do not retain their realm. If the last external context reference is released while a job is pending, executing that job can access a freed context. Top-level-await state can likewise keep a module reachable after its context's loaded-module list is destroyed, causing a use-after-free during runtime teardown.
This ports and adapts bellard/quickjs@458c34d:
This is the focused lifetime item still listed in #1166.
Validation:
make ctest cxxtestwith both NaN-boxing modes