Skip to content

Create the callback key whenever code reads it - #23

Open
Qard wants to merge 1 commit into
mainfrom
fix/callback-key-startup
Open

Qard wants to merge 1 commit into
mainfrom
fix/callback-key-startup

Conversation

@Qard

@Qard Qard commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes a latent AArch64 image-startup bug. Under PR #17 it surfaced as the macOS gate failure node:zlib sync codecs ... (copying), exit 70.

Bug

  • AArch64 startup created the callback pthread key (pthread_key_create) only when the program had callback thunks.
  • Every may-callback C call wrapper reads that key whether or not the program has a callback: it calls callbackState(), which calls callbackKey().
  • So a program whose only may-callback calls take no callback ran without the key. pthread_setspecific failed, and the call threw RangeError: callback state registration failed.
  • x86-64 startup already creates the key whenever any callback-key access is emitted.

On main, node:zlib only worked because unused allocator closures in the program kept a callback thunk alive. PR #17 makes unused function-new removable. That dropped those closures, and deflateSync("") then threw the RangeError.

Fix

packages/codegen/src/aarch64/startup.ts: create the key when the program has thunks or when any module imports tint:callback-key, matching x86-64.

How it was found

  • Bisected Typed code generation faster than Node #17 to its P6 commit. Splitting P6 showed the dead-closure removal, not the environment forwarding, triggers it.
  • The six removed closures have no IR uses, and function-new only allocates and checks the environment descriptor.
  • An instrumented fixture located the throw inside deflateSync(""). Its error-message classification matched callback state registration failed.

Validation

  • New fixture in tools/native-ffi.test.ts: abs declared as MayCallback<i32>, in a program with no callbacks. On main without the fix it exits 70 under LLDB; with the fix it returns 42.
  • All 12 callback and zlib native tests in tools/native-ffi.test.ts pass under LLDB on this branch, both GC variants:
    • zlib codecs
    • qsort callbacks
    • registered zlib allocator callbacks
    • callback contract and violation traps
  • Native-subset check: "checked". Codegen tests 511/511. Stage-1 bootstrap passes.

🤖 Generated with Claude Code


See it in action
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

AArch64 image startup created the callback pthread key only when the
program had callback thunks. Every may-callback C call publishes the
thread's callback state under that key (callbackState reads it through
callbackKey()), with or without a callback in the program, so a program
whose only may-callback calls take no callback ran with an uncreated key:
pthread_setspecific failed and the call threw "callback state
registration failed". The key is now created when the program has thunks
or any module imports tint:callback-key, as x86-64 startup already does.

On main the zlib codecs only worked because unused allocator closures in
the program kept a callback thunk alive; removing dead closures (PR #17)
exposed the bug as a RangeError from deflateSync.

tools/native-ffi.test.ts: a may-callback call (abs as MayCallback<i32>)
in a program without callbacks; it exits 70 without this change and 42
with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant