Repository navigation
Closed
Description
Summary
QuickJS-ng crashes when a WeakMap is freed and the value of one entry is a symbol that is also the key of the next entry. The crash happens in js_map_finalizer: the process dies with SIGSEGV, and an ASAN build reports a heap-use-after-free there. A program that only fills a WeakMap should not crash the runtime.
Environment
- QuickJS-ng 0.17.0, Homebrew build, macOS arm64 (the runs and the crash report below).
- master at 876442b, built by hand with
cc, not with the cmake files: one plain build with asserts enabled, and one ASAN build.
Steps to reproduce
- Save this program as
repro.js:
const m = new WeakMap();
const a = Symbol('a'), b = Symbol('b');
m.set(a, b);
m.set(b, a);
print('done');- Run
qjs repro.js. - Look at the output and at how the process ends.
Expected result
The program prints done and exits normally.
Actual result
The program prints done, then the process dies with SIGSEGV. The macOS crash report has EXC_BAD_ACCESS at 0x0 in delete_map_weak_ref, called from js_map_finalizer.
Additional observations
- One symbol is enough, and the exit path is not needed. This program crashes at the marked line:
var o = {};
function f() {
var m = new WeakMap(), b = Symbol('b');
m.set(o, b);
m.set(b, 1);
b = undefined;
m = undefined; // crashes here
}
f();- Objects instead of symbols do not crash.
- With two symbols inside a function, it does not crash either if an unrelated entry is inserted between the two
setcalls, or if the map is freed while the symbols are still referenced. - Both programs also fail on master (876442b). The plain build with asserts enabled stops with:
Assertion failed: (wr != NULL), function delete_map_weak_ref, file quickjs.c, line 53553
- The ASAN build reports heap-use-after-free: a read in
js_map_finalizer(quickjs.c:53886) of memory freed byJS_FreeAtomStruct(quickjs.c:3682), called fromjs_map_finalizer(quickjs.c:53889).
Possible cause
My reading of quickjs.c (line numbers at 876442b):
js_map_finalizer(lines 53870-53895) walks the records withlist_for_each_safe, which saves the next element before it handles the current one.- For each record it frees
mr->value. Objects are deferred, but a symbol is freed at once:JS_FreeAtomStructcallsreset_weak_ref(lines 63369-63454), which unlinks and frees the record keyed by that symbol. - If that record is the saved next element, the loop goes on with a freed record.
Same family as #1352.
Reacted by Saúl Ibarra Corretgé
Metadata
Metadata
Assignees
Labels
No labels
Activity
Fix UAF when freeing WeakMap with symbol values (#1844)