Skip to content

Crash in js_map_finalizer when a WeakMap value is the symbol key of the next entry #1835

Closed

Description

@igorsantosgrueiro

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

  1. 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');
  1. Run qjs repro.js.
  2. 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 set calls, 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 by JS_FreeAtomStruct (quickjs.c:3682), called from js_map_finalizer (quickjs.c:53889).

Possible cause

My reading of quickjs.c (line numbers at 876442b):

  1. js_map_finalizer (lines 53870-53895) walks the records with list_for_each_safe, which saves the next element before it handles the current one.
  2. For each record it frees mr->value. Objects are deferred, but a symbol is freed at once: JS_FreeAtomStruct calls reset_weak_ref (lines 63369-63454), which unlinks and frees the record keyed by that symbol.
  3. If that record is the saved next element, the loop goes on with a freed record.

Same family as #1352.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions