Skip to content

harden(js): lock call/apply/bind and wrapped intrinsics against own-property steering (#494) - #499

Merged
x-ror merged 2 commits into
masterfrom
harden/lock-captured-invoke
Nov 17, 2026
Merged

x-ror merged 2 commits into
masterfrom
harden/lock-captured-invoke

Conversation

@x-ror

@x-ror x-ror commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Closes #494.

What changed

  • lockIntrinsics (internal/primordials.js) now makes Function.prototype.call/apply/bind and every method a primordials wrapper holds non-extensible. The wrapper factories (caller0..3, callerN, so uncurryThis too) record their method with track(fn), so a newly wrapped method is covered without a hand list. A factory called after the lock (the loader's pristineBuffer) locks its method at once.
  • The wrappers keep fn.call. events.js keeps apply.call(listener, …), and its // primordials-ok: invoke markers now give the reason: the receiver is the locked Function.prototype.apply.
  • CLAUDE.md §5 states what the lock now protects and what it still leaves open.

Before this PR, Function.prototype.apply.call = evil hijacked EventEmitter.emit, and Array.prototype.push.call = evil hijacked the callerN wrappers (URLSearchParams threw). Node is unaffected by both.

Deviation (§1, approved)

Measured on node 26.13. Lava now gives the same answers as node --frozen-intrinsics for call/apply/bind and the wrapped set:

  • isExtensible is false;
  • assigning an own call throws in both modes;
  • assigning a new key is a no-op in sloppy mode and throws in strict mode;
  • defineProperty with a new key throws, and so does setPrototypeOf.

Unlike that flag, these functions are not frozen: name stays configurable. Default node allows all of the above. Intrinsics no wrapper holds (e.g. Array.prototype.at) behave exactly as in node.

Pinned by main.function_call_lock, rows ext, keys, own-, new-key-, setproto, wrapped-ext, push-own/fresh-sloppy, at-own, emit-hijack, push-hijack.

Rejected alternative

Routing calls through call.call(fn, t, …) is slower than Reflect.apply, because JSC inlines only fn.call. Measured on Linux arm64:

  • caller1 alone: about 1 ns vs about 55 ns per call;
  • end to end: new URL 2.64x, URLSearchParams.get 3.81x.

Reuse verdict

No native code. The fix uses Object.preventExtensions and the existing lockIntrinsics.

Tests

  • Red on master: wrapped-ext=true; push-own-sloppy=assigned; push-hijack=THREW:TypeError; emit-hijack= (the listener never ran).
  • 3 recorded mutations: drop the wrapped-set loop; make the lock-on-wrap path return early; track skips apply. Linux docker, clean clone, make test-mutation CHANGED=1: 50/50 died as recorded.
  • The two finds for 72-events-parity are retargeted at the new marker text.

Gates

  • darwin, pass: fmt, check, build, test-odin 25/25, test-odin-serial 25/25, check-js (fmt, graph, primordials with no count raised, globalreplace), check-md.
  • darwin, failed (none caused by this PR): scripttests has 8 failures, all the --changed tests from open tooling: run-mutations --changed tests fail on a clean checkout; check-md scans .claude/worktrees #420.
  • Linux docker: test-lava and nohostfn, 136 cases each, compared node vs master vs branch. The branch differs from master in 0 cases. The one failure, 56-native-function-arity, fails on both master and the branch because of the scratch environment.

Bench

Linux arm64, JSC 2.52.6, master→branch, medians of 11 interleaved launches:

case ratio
emit, 1 listener 0.991
emit, 0 listeners 1.012
new URL 0.998
URLSearchParams.get 1.027
URLSearchParams parse 1.007
decode(5B) 0.989
toString('hex') 0.987

Startup (lava eval 0, 31 interleaved runs per arm, 3 sessions): 0.992 / 1.001 / 0.999.

Still open (filed separately)

  • structured_clone.js toString.call and buffer.js u8Fill.call: intrinsics captured outside primordials.
  • Live prototype .call reads.

🤖 Generated with Claude Code

x-ror and others added 2 commits November 17, 2026 13:30
…roperty steering (#494)

Function.prototype.call/apply/bind were locked on Function.prototype (#450),
but the held functions themselves still took an own `call` or a new
prototype: `Function.prototype.apply.call = evil` hijacked every
`apply.call(listener, ...)` in events.js, and `Array.prototype.push.call =
evil` hijacked the callerN `fn.call(t, ...)` wrappers. node runs neither.

lockIntrinsics() now makes call/apply/bind, and exactly the set of methods
a primordials wrapper closes over, non-extensible. The set is recorded by
the wrapper factories themselves, so a newly wrapped method is covered; a
factory call after the lock (the loader's pristine-Buffer uncurryThis)
locks on the spot. The wrappers keep `fn.call`: `call.call(fn, ...)` was
measured slower than Reflect.apply on Linux JSC (new URL 2.64x).

Deviation: the locked functions answer as under node --frozen-intrinsics
(isExtensible false, own props and setPrototypeOf throw) but are not
frozen. Pinned by cmd/lava/function_call_lock_test.odin.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s not

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@x-ror
x-ror merged commit e14b53d into master Nov 17, 2026
8 checks passed
@x-ror
x-ror deleted the harden/lock-captured-invoke branch November 18, 2026 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

harden(runtime): own call/apply on captured intrinsics still steers builtins after the #485 lock

1 participant