Skip to content

Commit 265ef4a

Browse files
committed
test(soundness): pin #836 wasm intrinsic-name shadowing as open (tracked)
The #829 thread reported two more divergences; both reproduce on main. - #836: on core wasm, a user fn named like a name-intercepted intrinsic (e.g. `string_sub`) is silently replaced by the intrinsic, so the program prints a heap pointer where interp and Bun-ESM print the user's value. A silent wrong value, so it is entered in docs/SOUNDNESS.adoc as `open (tracked)` with an xfail pin, test_wasm_intrinsic_shadow_xfail (fixture wasm_intrinsic_name_shadowed.affine). Mutation-checked: with the `string_sub` intercept disabled the pin reports XPASS. Not fixed here: some intercepts deliberately catch stdlib externs and need a per-arm audit. - #837: an intrinsic used as a value (`len`) emits a bare name on Bun-ESM, a run-time ReferenceError (wasm refuses at compile time). Both are added to the backend-divergence audit. Ledger resealed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J4QvDjPsKtu8zU9b32Cv1P
1 parent 9b4c696 commit 265ef4a

5 files changed

Lines changed: 142 additions & 5 deletions

File tree

‎docs/SOUNDNESS.adoc‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,20 @@ write into the literal region. Layout invariant:
258258
`test_wasm_heap_literals_intact_after_alloc` — run under Bun's WASI;
259259
`test_wasm_heap_exhaustion_traps`); `lib/codegen.ml`
260260
`memory_layout_of_datas` / `check_memory_layout`
261+
262+
| #836
263+
| On core wasm, a user function named like one of the 20 name-intercepted
264+
intrinsics (`string_sub`, `trim`, `int_to_string`, …) is silently replaced by
265+
the intrinsic: `gen_expr` matches the call by spelling before resolution, so
266+
`fn string_sub(…) -> Int { 7 }` called from `main` prints a heap pointer
267+
(interpreter and Bun-ESM print `7`). Some arms deliberately intercept stdlib
268+
`extern fn`s, so the fix needs a per-arm audit (resolution-based tagging is
269+
the recommended fix; see #836).
270+
| `open (tracked)`
271+
| `test_wasm_intrinsic_shadow_xfail` (xfail: asserts the wasm `main` calls the
272+
user's `string_sub`; currently fails-as-expected; flips to XPASS when fixed —
273+
mutation-checked by disabling the `string_sub` intercept);
274+
`test/e2e/fixtures/wasm_intrinsic_name_shadowed.affine`
261275
|===
262276

263277
== Still open — read before claiming "sound"
@@ -269,14 +283,18 @@ row in the table above, restated for the hurried reader — not a separate list:
269283
* *Interpreter non-tail resume* (#555 / #623, `residual (pinned)`) — the
270284
`5`-not-`105` shape. Silent on the interpreter path; pinned by
271285
`test_resume_nontail_xfail`.
286+
* *Wasm intrinsic-name shadowing* (#836, `open (tracked)`) — a user function
287+
named like a name-intercepted wasm intrinsic is silently replaced by it. Silent
288+
on the core-wasm path; pinned by `test_wasm_intrinsic_shadow_xfail`.
272289
(Stub backends dropping `return` (#555-stub / #624) is now `loud-fail` — Lean and
273290
Why3 reject an early `return` instead of silently dropping it — so it is no longer
274291
a "still open" residual. Treat all non-reference backends as experimental anyway,
275292
see `docs/CAPABILITY-MATRIX.adoc`.)
276293

277294
If you are deciding whether AffineScript is "sound enough" for a use, the
278-
interpreter non-tail resume row above is the one remaining residual, and the
279-
metatheory caveat below applies to it.
295+
interpreter non-tail resume row and the wasm intrinsic-shadowing row (#836)
296+
above are the remaining silent residuals, and the metatheory caveat below
297+
applies to them.
280298

281299
=== Pinned-residual discipline
282300

‎docs/reports/AUDIT-2026-12-01-backend-divergences.adoc‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
:toc: macro
55
:icons: font
66

7-
This audit follows the #830 fix. The Unicode thread (#829) found several ways
7+
This audit follows the #830 fix. Findings 5 and 6 were added after the #829 thread reported them. The Unicode thread (#829) found several ways
88
the backends disagree. The owner asked whether any of the others needs the
99
same at-source treatment as the wasm heap/literal overlap.
1010

@@ -59,6 +59,16 @@ plausible-wrong result.
5959
| Loud divergence (the reference interpreter)
6060
| No
6161
| #833
62+
63+
| A user fn named like a wasm-intercepted intrinsic is silently replaced
64+
| *Soundness hole*: silent wrong value
65+
| No
66+
| #836 (pinned `open (tracked)` in this PR)
67+
68+
| An intrinsic used as a value (e.g. `len`) emits a bare name on Bun-ESM
69+
| Loud divergence, at the wrong phase
70+
| No
71+
| #837
6272
|===
6373

6474
== 1. Core-wasm heap overlaps string literals (#830)
@@ -198,6 +208,55 @@ Outside the box::
198208
Move the CLI's interpreter role onto `solo_cesk`. That would also unblock
199209
#623.
200210

211+
== 5. Wasm replaces a user function named like an intrinsic (#836)
212+
213+
Reported by the #829 thread after the first version of this audit, and
214+
reproduced independently.
215+
216+
Evidence::
217+
On `main`, `fn string_sub(s: String, a: Int, b: Int) -> Int { 7 }` called from
218+
`main` prints `7` on the interpreter and on Bun-ESM, but `1024` on core wasm
219+
(`1040` with #830). That number is the heap address of the intrinsic's
220+
substring. `wasm-tools print` shows the user's function compiled as func 1,
221+
which `main` never calls. Cause: `gen_expr` in `lib/codegen.ml` intercepts 20
222+
names by spelling (`ExprVar id when id.name = "…"`) before checking whether
223+
the name resolves to a user declaration.
224+
225+
Classification::
226+
*Soundness hole*: a silent wrong value on a supported path. This PR pins it
227+
in `docs/SOUNDNESS.adoc` as `open (tracked)` with an xfail pin,
228+
`test_wasm_intrinsic_shadow_xfail`. With the `string_sub` intercept removed,
229+
the pin flips to XPASS, which confirms it detects the hole. It is not fixed
230+
here: some arms deliberately intercept stdlib `extern fn`s, so each of the 20
231+
arms needs auditing.
232+
233+
Recommended fix (*most elegant and correct*)::
234+
Tag builtin calls at resolution and match on the tag in codegen, so a user
235+
declaration always shadows the builtin.
236+
237+
Most dependable::
238+
Add a per-arm `not (is_user_defined …)` guard, with one run test per name.
239+
240+
Stop-gap::
241+
Refuse a user top-level declaration that reuses an intercepted name on wasm.
242+
243+
== 6. Intrinsics used as values on Bun-ESM (#837)
244+
245+
Evidence::
246+
`apply(len, "abc")` prints `3` on the interpreter. On Bun-ESM it fails with
247+
`ReferenceError: len is not defined` at run time. Core wasm refuses it at
248+
compile time (`UnboundVariable "len"`).
249+
250+
Classification::
251+
Loud divergence, at the wrong phase. This is the same class as #832.
252+
253+
Recommended fix (*most elegant and correct*)::
254+
Eta-expand an intrinsic in value position in a shared pre-codegen pass, so
255+
it becomes first-class on every backend.
256+
257+
Most dependable::
258+
Refuse it at codegen on Bun-ESM, as wasm already does.
259+
201260
== Reaching beyond this repo
202261

203262
* #834 tracks rebuilding core-wasm artefacts built before #830 in other
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: MPL-2.0
2+
// #836: a user fn named like a wasm-intercepted intrinsic must be called.
3+
// Interp and Bun-ESM print 7; core wasm emits the intrinsic instead and
4+
// prints a heap pointer.
5+
fn string_sub(s: String, a: Int, b: Int) -> Int { 7 }
6+
7+
fn main() -> () {
8+
println(int_to_string(string_sub("hello", 0, 2)));
9+
}

‎test/xfail/test_xfail_pins.ml‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,53 @@ let test_resume_nontail_xfail () =
7676
raise
7777
(Xfail ("non-tail resume returned " ^ Value.show_value other ^ ", not 105"))
7878

79+
(* ---- xfail pin: #836 — wasm ignores a user fn named like an intrinsic -------
80+
The fixture declares its own `string_sub` (returns 7) and calls it from
81+
`main`. Desired: the wasm `main` calls the user's function. Today the
82+
name-matched intercept in `Codegen.gen_expr` lowers the call to the
83+
built-in substring op instead, and the program prints a heap pointer. *)
84+
let test_wasm_intrinsic_shadow_xfail () =
85+
let prog = parse (fixture "wasm_intrinsic_name_shadowed.affine") in
86+
let _ = resolve_symbols prog in
87+
let m =
88+
match Codegen.generate_module prog with
89+
| Ok m -> m
90+
| Error e -> failwith ("codegen: " ^ Codegen.show_codegen_error e)
91+
in
92+
let nimports =
93+
List.length
94+
(List.filter
95+
(fun (i : Wasm.import) ->
96+
match i.i_desc with Wasm.ImportFunc _ -> true | _ -> false)
97+
m.imports)
98+
in
99+
(* The user's `string_sub` is the fixture's first function declaration. *)
100+
let user_fn = nimports in
101+
let main_idx =
102+
match
103+
List.find_map
104+
(fun (e : Wasm.export) ->
105+
match e.e_desc with
106+
| Wasm.ExportFunc i when e.e_name = "main" -> Some i
107+
| _ -> None)
108+
m.exports
109+
with
110+
| Some i -> i
111+
| None -> failwith "no exported `main`"
112+
in
113+
let body = (List.nth m.funcs (main_idx - nimports)).f_body in
114+
let rec calls = function
115+
| Wasm.Call i -> i = user_fn
116+
| Wasm.Block (_, b) | Wasm.Loop (_, b) -> List.exists calls b
117+
| Wasm.If (_, a, b) -> List.exists calls a || List.exists calls b
118+
| _ -> false
119+
in
120+
if List.exists calls body then () (* desired met -> XPASS: the hole is fixed *)
121+
else
122+
raise
123+
(Xfail "wasm `main` does not call the user's `string_sub`; the intrinsic \
124+
is emitted instead")
125+
79126
(* ---- fence: #555-stub / #624 — Lean backend fails loud on `return` ---------
80127
Fixed: `codegen_lean` now returns Error on an early `return` (the
81128
fn_body_contains_return fence) instead of silently lowering to `:= ()`. This
@@ -94,7 +141,9 @@ let test_stub_backend_return_fenced () =
94141

95142
let pins =
96143
[ ("test_resume_nontail_xfail",
97-
"#555/#623 interp non-tail resume -> 5 not 105", test_resume_nontail_xfail) ]
144+
"#555/#623 interp non-tail resume -> 5 not 105", test_resume_nontail_xfail);
145+
("test_wasm_intrinsic_shadow_xfail",
146+
"#836 wasm replaces a user fn named like an intrinsic", test_wasm_intrinsic_shadow_xfail) ]
98147

99148
let fences =
100149
[ ("test_stub_backend_return_fenced",

‎tools/soundness-anchors.sha256‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
# test's function-body hash; SUITE = whole-file hash.
66
09f299eaf7e9a0d912db441f346438e31ea7157f6f730ab1b6d78ef6d22a76a1 test:test_801_planted_collision_refused
77
0d3623cf36a67860106d64bb32d30633978bb426d4ddd77ecc2bf6e48e1b7263 fixture:test/e2e/fixtures/borrow_callee_returned_borrow_uam.affine
8+
2978221834a9e4fb0f1de5579c1d3652c421f7eb3b91f34effb7c87fd3e20f20 test:test_wasm_intrinsic_shadow_xfail
89
2dfe36a21983a7dfb7ffa1157f7ddec9865a531a92682976b8fd677e4825e78d test:test_801_impl_methods_refused
910
45eb0f3aa5f643afbd003248998dd5a046688418647b491e0f2d4bb55fcbd0f2 test:test_wasm_heap_exhaustion_traps
1011
546939eaa3c64c82edd23da0a3ba7f74bcf7378cfe1e426132dd1a9f8fd46050 test:test_801_transitive_public_and_const
12+
5b5629f16432a3ebcfd3a29e998a9533815a9b009839d7dbf620434f346dee64 test:test_resume_nontail_xfail
1113
5b84a4f71aa65f9aa9383e31c0cb624291c4f9d0de7cca4a0ea6a76fe85aea35 fixture:test/e2e/fixtures/handle_resume_nontail.affine
1214
61a824a9bbd1243c27aab2687e7ef0da1afdfd208cd26187908797f16adad163 test:test_wasm_heap_repro_runs
1315
68f0f4ad2462f146b87f491b7456aff9a56b77397344d22c62f4f4ff5ca6e3ae fixture:test/e2e/fixtures/assume_rejected.affine
@@ -17,8 +19,8 @@
1719
7f416f186a15511ec5c64d2b96bfffbdf9e7c8c0879951d56c80e46129e2ea48 test:test_stub_backend_return_fenced
1820
80541ee00580b4dccee4657cbd31d11a1ddaa18cc49813b48f686c9509f314dc suite:test/test_borrow_polonius.ml
1921
832d4c8b64176706535334a5dbbb16345f42b75ac9b1a93f0a5dc371d84b5a0d test:test_801_same_name_in_two_modules
20-
874a5531cd8bc402adf4b66277b21b15d921efe06a7128ea6583bcd6e55781ba test:test_resume_nontail_xfail
2122
94b346ae2d57068c6096411126b2e4dc9cc6da83816056339079cf3811629deb test:test_wasm_heap_literals_intact_after_alloc
23+
a4f226e96317cc96d27dce061e2b3b8090a5d07dc3e7eb73ffcd15d061bc8f7e fixture:test/e2e/fixtures/wasm_intrinsic_name_shadowed.affine
2224
ac1da10c3206605304f11e869f6aa07acba57f6f8248bb2dfacca19984f86c31 test:test_coherence_duplicate_rejected
2325
b76c7824a9b1570d66752359a28d75ebdfaedb5de5ad68c343a0a2c26ad4221c fixture:test/e2e/fixtures/async_sync_fallback.affine
2426
c743b86cfc50512e5867ddd792b4b4273b5bfb6f40205b4a07019031d7e7b102 fixture:test/e2e/fixtures/handle_resume_multishot.affine

0 commit comments

Comments
 (0)