Skip to content

fix(runtime): keep the host work-loop entry out of line - #1581

Merged
Lorenzobattistela merged 1 commit into
bendlang:mainfrom
vicmcorrea:fix/host-work-loop-asan
Oct 15, 2026
Merged

Lorenzobattistela merged 1 commit into
bendlang:mainfrom
vicmcorrea:fix/host-work-loop-asan

Conversation

@vicmcorrea

Copy link
Copy Markdown
Contributor

Problem and fix

Generated C can crash on arm64 when built with optimization and AddressSanitizer, even with one thread. For example, nat_add_infix faults instead of printing 6n. Inlining the C entry into a caller with a realigned stack frame lets a call into a preserve_none segment clobber the caller's x19 base pointer.

Add one FAR declaration for work_loop inside the existing #if !DEVICE block. It keeps the host entry out of line through FAR's existing noinline attribute. The shared definition and calling convention stay as they are.

Fixes #1574.

Blast radius

Only bend2/comp.ts changes: one host-only declaration affecting evaluator and pool-worker entry. No new helper, macro, allocation, or public API change. The device dispatcher is unchanged; baseline/candidate CUDA and Metal preprocessing tokens match. This avoids the broader GPU outlining change discussed in #1433.

Regression evidence

Fresh base 25233fb runs reproduce the nat_add_infix failure at O1/O3 with ASan+UBSan on Apple Clang 17.0.0 and LLVM 23.1.3. The corrected generated C passes 24 sanitizer runs across nat_add_infix, binary_search, and fork_leaf_result_loop, both compilers, O1/O3, and threads 1/4 with GPU off. Candidate O0 ASan and O3 unsanitized Nat controls also pass across both compilers and thread counts. Disassembly confirms that the outlined wrapper saves and restores x19 around WL_FID_ENTER.

A separate diagnostic build confirms work_loop entry on actual non-main pool workers with both compilers and the expected fork result 4000. Diagnostic hooks are not part of the patch or the sanitizer regression binaries.

Repeatable manual regression using an existing fixture:

bun bend2/main.ts tests/run/nat_add_infix.bend -o /tmp/bend-1574.c
clang -std=c11 -O1 -fsanitize=address,undefined /tmp/bend-1574.c -lpthread -lm -o /tmp/bend-1574
/tmp/bend-1574 --threads 1 --gpu off

Expected output: 6n. The ordinary gate does not enable ASan, so a duplicate Bend fixture would pass on the broken baseline too.

Checks

  • Local repo gate: 54/54, with CI-pinned ttok 0.3; comp.ts is 63,495/64,000 tokens.
  • Nine check/interpreter/emitted-JS controls pass across three existing fixtures. Their emitted JS is byte-identical to base.
  • git diff --check: passed.

CPU timing used an Apple M1 Pro, Apple Clang 17.0.0, cc -std=c11 -O3, GPU off, and threads 1/4. Each cell has one warm run per variant and five alternating base/fix pairs. All 96 release runs (16 warm, 80 measured) produced the expected output with exit 0 and empty stderr.

Workload / threads Base median [min–max], s Fix median [min–max], s Fix/base medians
queens / 1 9.382 [9.205–12.591] 9.401 [9.189–12.905] 1.002
queens / 4 2.348 [2.262–3.742] 2.372 [2.308–3.260] 1.010
symreg / 1 6.553 [6.008–6.792] 6.319 [6.237–6.554] 0.964
symreg / 4 1.643 [1.640–1.774] 1.671 [1.612–1.786] 1.017
deep fork / 1 0.939 [0.922–0.999] 0.952 [0.949–1.039] 1.013
deep fork / 4 0.273 [0.249–0.304] 0.274 [0.258–0.309] 1.002
repeated shallow fork / 1 2.900 [2.825–3.345] 2.343 [2.300–2.569] 0.808
repeated shallow fork / 4 12.830 [12.588–13.425] 12.359 [12.000–14.186] 0.963

The scratch fork workloads reuse fork_leaf_result_loop: a depth-22 tree with 16 Result turns per leaf, and 1,000,000 repeated depth-2 calls with 16 turns per leaf. Separate diagnostics count 767 host entries for the deep tree and about 12 million for the repeated shallow case; timed binaries have no diagnostic hooks. The shared desktop had material background activity, and timings include visible outliers. These are local measurements, not a performance-neutrality claim or the cluster perf gate.

Validation limits

Local checks used macOS 15.8.1 arm64 with Bun 1.3.11. The reporter's exact Apple Clang 21, Linux, Windows, TSan, and the cluster gates were not run. GPU preprocessing equality is not GPU compilation/execution: Metal preprocessing used an empty header shim because the Metal compiler is unavailable. Other than manual sanitizer coverage, no test gate was added or changed. Existing sanitizer-build sprintf deprecation warnings remain.

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 15, 2026
@Lorenzobattistela
Lorenzobattistela merged commit b869cca into bendlang:main Oct 15, 2026
1 check passed
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.

C runtime faults under AddressSanitizer at -O1 and above: the base pointer x19 is clobbered by a preserve_none callee (arm64)

2 participants