Skip to content

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

Closed

Description

@rofrol
  • What you did: bend tests/run/nat_add_infix.bend -o n.c, then clang -std=c11 -O1 -fsanitize=address n.c -lpthread -lm -o n && ./n
  • What happened: bend: memory fault (machine stack overflow?), exit 1 (expected 6n)
  • 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                 # 6n

tests/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_FN functions, which are preserve_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 is static and gets inlined into corpus_eval (line 5270,
    OUTLINE, which is preserve_most, line 3343).

  • Under ASan, corpus_eval realigns its stack to 32 bytes for its
    instrumented locals. AArch64 then keeps a base pointer in x19. In the
    disassembly of the -O1 -g -fsanitize=address build:

    _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_none makes x19 a scratch register for the callee, and
    clang does not save the base pointer around the call. After
    WL_FID_ENTER returns, corpus_eval reads its frame through a garbage
    x19: the value it loads for H is 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
    H is valid. With the fix below, UBSan with pointer-overflow on is
    clean.
  • The fixed-hint corpus mapping colliding with ASan's shadow memory:
    corpus_at maps without MAP_FIXED and 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

  1. rofrol commented on Oct 15, 2026

    @rofrol
    Author

    The release v2.0.35 (79df8d9) has the same hazard. tests/run/nat_add_infix.bend built with -fsanitize=address prints 6n at -O0 and faults with bend: memory fault (machine stack overflow?) at -O1 and -O3. With work_loop made FAR it prints 6n at both levels.

    Whether a build faults depends on the inliner. In a larger program, clang kept work_loop out of line. Its prologue then saved x19, and 20 ASan runs at -O3 passed. Forcing work_loop always_inline in 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.

    otool -tvV ./prog | awk '/^_[A-Za-z0-9_]+:$/{f=$1} /mov\tx19, sp/{bp[f]=1} /\tbl\t_WL_/{if(bp[f]) h[f]=1} END{for(k in h) print k}'

    It names _corpus_eval in every faulting binary. It names nothing in the passing ASan build, the FAR builds, or release -O3 builds, including one with work_loop forced inline. Release builds are therefore not affected today, but a frame on this path that needs realignment would expose them. pool_rows also enters WL_FID_ENTER directly from a C-convention frame, through the inlined work_loop. That frame needs no realignment today, and FAR covers it too.

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