Repository navigation
Description
- What you did:
bend tests/run/nat_add_infix.bend -o n.c, thenclang -std=c11 -O1 -fsanitize=address n.c -lpthread -lm -o n && ./n - What happened:
bend: memory fault (machine stack overflow?), exit 1 (expected6n) - bend --version: bend 2.0.37 (checkout 014ccd0)
- uname -sm: Darwin arm64
- clang --version: Apple clang version 21.0.0 (clang-2100.3.34.2)
Summary
A native program built from the emitted C with -fsanitize=address at
-O1 or higher crashes with bend: memory fault (machine stack overflow?). This happens for most programs, even tiny ones, and also with
--threads 1. The same C file runs correctly at -O0 with ASan, and
without ASan at any optimization level. UBSan, built together with ASan,
first reports applying non-zero offset 256 to null pointer in
root_done. That report is a symptom of the same bug, not a separate
problem: the heap pointer H is never null.
The cause is a codegen hazard in how the runtime enters its
preserve_none segments, not ASan itself. ASan makes corpus_eval
realign its stack frame. On arm64 the frame then uses x19 as its base
pointer, and a preserve_none callee may clobber x19.
Repro
Revision: 014ccd0. The program is the
in-tree test tests/run/nat_add_infix.bend:
import Base
law main.out:
Nat
def main.out():
(1n + 2n + 3n : Nat)
law main:
IO(Unit)
def main():
IO.print(String.append(Nat.show(main.out()), "n"))
bun bend2/main.ts tests/run/nat_add_infix.bend -o /tmp/n.c
clang -std=c11 -O1 -fsanitize=address /tmp/n.c -lpthread -lm -o /tmp/n
/tmp/n # bend: memory fault (machine stack overflow?), exit 1
clang -std=c11 -O0 -fsanitize=address /tmp/n.c -lpthread -lm -o /tmp/n0
/tmp/n0 # 6n
clang -std=c11 -O1 /tmp/n.c -lpthread -lm -o /tmp/n1
/tmp/n1 # 6ntests/run/binary_search.bend faults the same way, also with
--threads 1. With the runtime's SIGSEGV handler removed, ASan reports
SEGV on unknown address 0x...cb ... in work_loop, inlined into
corpus_eval.
Environment: Apple clang version 21.0.0 (clang-2100.3.34.2), macOS 27.0.1
(26A434), arm64.
Root cause
All line numbers below are in bend2/comp.ts at 014ccd0.
-
The segments are
WL_FNfunctions, which arepreserve_none
(#define WL_FN static PRESERVE(preserve_none) ..., line 3369; and
PRESERVE, line 3339). They musttail into each other (lines
3372-3373). -
work_loop(line 4308) is the one function with the C calling
convention that calls into them (return WL_FID_ENTER(WL_ALL);, line
4321). It isstaticand gets inlined intocorpus_eval(line 5270,
OUTLINE, which ispreserve_most, line 3343). -
Under ASan,
corpus_evalrealigns its stack to 32 bytes for its
instrumented locals. AArch64 then keeps a base pointer inx19. In the
disassembly of the-O1 -g -fsanitize=addressbuild:_corpus_eval: ... and sp, x9, #0xffffffffffffffe0 mov x19, sp ; base pointer ... bl _WL_FID_ENTER ; preserve_none callee ldr x12, [x19, #0x68] ; frame read through x19 after the call _WL_FID_ENTER: ... mov x19, x22 ; the callee overwrites x19 -
preserve_nonemakesx19a scratch register for the callee, and
clang does not save the base pointer around the call. After
WL_FID_ENTERreturns,corpus_evalreads its frame through a garbage
x19: the value it loads forHis 0, which UBSan reports in
root_done, and the next access faults. -
Without ASan the frame needs no realignment and no base pointer, so the
bug stays latent. Any future change that gives a frame on this path a
base pointer (a VLA,alloca, over-aligned locals) would expose it in
release builds too.
The cause is not:
- A null-based heap pointer: the corpus is mapped at a 2^45 hint and
His valid. With the fix below, UBSan withpointer-overflowon is
clean. - The fixed-hint corpus mapping colliding with ASan's shadow memory:
corpus_atmaps withoutMAP_FIXEDand accepts only an honored hint,
so it cannot overlap the shadow. Under ASan the corpus still lands at
2^45.
Suggested fix
Keep work_loop out of line. It then becomes a small C-convention
trampoline: its own frame needs no base pointer, it saves x19 itself,
and its callers keep their frames intact.
-static Term work_loop(Env e, DEV Term* sp, Term t, u32 seq) {
+FAR Term work_loop(Env e, DEV Term* sp, Term t, u32 seq) {FAR is static __attribute__((noinline)). On this Mac the change costs
nothing measurable: the CPU lane (--gpu off) gives a median of 7 runs of
1.29 s before and 1.27 s after for bench/runtime/queens, and 0.823 s
both before and after for symreg, with identical output. With it,
nat_add_infix and binary_search pass under -fsanitize=address,undefined
at -O1 and -O3, with UBSan's pointer-overflow on, and under TSan.
A related clang/LLVM bug is likely worth reporting too: a preserve_none
call from a frame that uses a base pointer does not preserve the base
pointer on AArch64.
Activity
rofrol commentedon Oct 15, 2026
The release v2.0.35 (79df8d9) has the same hazard.
tests/run/nat_add_infix.bendbuilt with-fsanitize=addressprints6nat-O0and faults withbend: memory fault (machine stack overflow?)at-O1and-O3. Withwork_loopmadeFARit prints6nat both levels.Whether a build faults depends on the inliner. In a larger program, clang kept
work_loopout of line. Its prologue then saved x19, and 20 ASan runs at-O3passed. Forcingwork_loopalways_inlinein the same generated C made it fault at once. So passing ASan runs do not show that a build is free of the hazard.This scan of a built binary lists the functions at risk: those that keep a base pointer in x19 and call a segment directly.
It names
_corpus_evalin every faulting binary. It names nothing in the passing ASan build, theFARbuilds, or release-O3builds, including one withwork_loopforced inline. Release builds are therefore not affected today, but a frame on this path that needs realignment would expose them.pool_rowsalso entersWL_FID_ENTERdirectly from a C-convention frame, through the inlinedwork_loop. That frame needs no realignment today, andFARcovers it too.