|
| 1 | +// SPDX-License-Identifier: CC-BY-SA-4.0 |
| 2 | +// SPDX-FileCopyrightText: 2026 hyperpolymath |
| 3 | += Backend Divergence Audit (2026-12-01) |
| 4 | +:toc: macro |
| 5 | +:icons: font |
| 6 | + |
| 7 | +This audit follows the #830 fix. The Unicode thread (#829) found several ways |
| 8 | +the backends disagree. The owner asked whether any of the others needs the |
| 9 | +same at-source treatment as the wasm heap/literal overlap. |
| 10 | + |
| 11 | +Each finding below was reproduced on `main` `ee211d3` with the built compiler. |
| 12 | +For each one, the audit records the evidence, a classification and a |
| 13 | +recommended fix. The PR that adds this audit implements only #830. The other |
| 14 | +findings are filed as issues. Those that change language semantics are marked |
| 15 | +for the owner's review. |
| 16 | + |
| 17 | +Soundness-hole status is recorded in link:../SOUNDNESS.adoc[SOUNDNESS.adoc], and |
| 18 | +that ledger is the authority for it. This report is a dated input to it. |
| 19 | + |
| 20 | +toc::[] |
| 21 | + |
| 22 | +== Classification used |
| 23 | + |
| 24 | +Soundness hole:: |
| 25 | +A well-typed program silently gets a wrong value or corrupts state on a |
| 26 | +supported path. |
| 27 | + |
| 28 | +Silent divergence:: |
| 29 | +Backends give different observable results for the same well-typed program, |
| 30 | +and nothing fails. |
| 31 | + |
| 32 | +Loud divergence:: |
| 33 | +A backend refuses or crashes on a well-typed program that another backend |
| 34 | +runs. This breaks progress for that backend, but it never gives a |
| 35 | +plausible-wrong result. |
| 36 | + |
| 37 | +== Summary |
| 38 | + |
| 39 | +[cols="3,2,2,2",options="header"] |
| 40 | +|=== |
| 41 | +| Finding | Class | Changes semantics? | Tracking |
| 42 | + |
| 43 | +| Core-wasm heap overlaps string literals |
| 44 | +| *Soundness hole*: silent memory corruption |
| 45 | +| No |
| 46 | +| #830 (fixed in this PR); estate rebuilds #834 |
| 47 | + |
| 48 | +| String indices are octets on interp/wasm but UTF-16 units on Bun-ESM |
| 49 | +| Silent divergence |
| 50 | +| *Yes*: owner decision |
| 51 | +| #831 |
| 52 | + |
| 53 | +| Plain-JS backend has no string builtins (run-time `ReferenceError`) |
| 54 | +| Loud divergence, at the wrong phase |
| 55 | +| No |
| 56 | +| #832 |
| 57 | + |
| 58 | +| `eval` rejects self-recursion, forward and mutual references, and `let mut` |
| 59 | +| Loud divergence (the reference interpreter) |
| 60 | +| No |
| 61 | +| #833 |
| 62 | +|=== |
| 63 | + |
| 64 | +== 1. Core-wasm heap overlaps string literals (#830) |
| 65 | + |
| 66 | +Evidence:: |
| 67 | +Sixty `println("label_number_N" ++ " ok")` calls, compiled to wasm and run |
| 68 | +with `bun run`, printed 21 correct lines, then `label_number_22abe`, then |
| 69 | +`RuntimeError: Out of bounds memory access`. `affinescript eval` printed all |
| 70 | +60 lines. Cause: the heap global was initialised to a fixed `1024`, and |
| 71 | +literals were laid out from a fixed `2048`. |
| 72 | + |
| 73 | +Classification:: |
| 74 | +Soundness hole: silent corruption of static data. The trap comes only after |
| 75 | +the corrupted line has been printed. |
| 76 | + |
| 77 | +Fix (implemented):: |
| 78 | +The heap base is computed after code generation as the 8-aligned end of the |
| 79 | +last data segment, and the memory is sized to cover it. The module is |
| 80 | +refused if the heap base is below the end of static data. See |
| 81 | +link:../WASM-MEMORY-LAYOUT.adoc[WASM-MEMORY-LAYOUT.adoc]. |
| 82 | ++ |
| 83 | +Alternatives considered: |
| 84 | ++ |
| 85 | +* Bounds-checked allocation with `memory.grow`. This is needed eventually, |
| 86 | +but on its own it would not move the heap off the literals. |
| 87 | +* A separate memory for literals (multi-memory). This is not yet supported |
| 88 | +by every target host. |
| 89 | +* Literals in a WasmGC `array i8`. That is only possible on the GC backend. |
| 90 | ++ |
| 91 | +Computing the layout from the emitted segments was chosen because it makes |
| 92 | +the invariant structural and checks it in one place. |
| 93 | + |
| 94 | +== 2. String index units differ by backend (#831) |
| 95 | + |
| 96 | +Evidence:: |
| 97 | ++ |
| 98 | +[cols="3,1,1,1",options="header"] |
| 99 | +|=== |
| 100 | +| Probe | interp | core wasm | Bun-ESM |
| 101 | +| `len("é")` | 2 | 2 | 1 |
| 102 | +| `len("🦜")` | 4 | 4 | 2 |
| 103 | +| `string_char_code_at("🦜", 0)` | 240 | 240 | 55358 |
| 104 | +|=== |
| 105 | ++ |
| 106 | +`lib/codegen_deno.ml:1240` lowers `len` to JS `.length`. `string_get` and |
| 107 | +`string_sub` (`:1244-1245`) use JS indices in the same way. |
| 108 | + |
| 109 | +Classification:: |
| 110 | +Silent divergence (#560 class). It is not a type-soundness hole, but it |
| 111 | +produces the same kind of wrong answer: the same program prints different |
| 112 | +numbers on different backends. |
| 113 | + |
| 114 | +Recommended fix (*most elegant and correct*):: |
| 115 | +Use octet offsets everywhere, with boundary checks. `len` counts UTF-8 octets |
| 116 | +on every backend. `string_get` and `string_sub` fail loudly when an offset is |
| 117 | +not on a character boundary. Code that works with characters moves to the |
| 118 | +#829 code-point API. |
| 119 | + |
| 120 | +Most dependable, type-led:: |
| 121 | +Make offsets an opaque type (`Utf8Offset`) that only `find`, iteration and |
| 122 | +`utf8_byte_length` can produce. Mixing units then becomes a type error. This |
| 123 | +pairs with the recommended fix, and it is the one with a proof story: a |
| 124 | +phantom-unit argument is a short preservation lemma. |
| 125 | + |
| 126 | +Outside the box:: |
| 127 | +Represent `String` on Bun as a `Uint8Array` of UTF-8. All backends would then |
| 128 | +share one representation. The cost is transcoding at every JS host boundary. |
| 129 | + |
| 130 | +Stop-gap:: |
| 131 | +Refuse `len`, `string_get` and `string_sub` on Bun-ESM, and point to the #829 |
| 132 | +API. This fails loudly, but it breaks existing Bun code. |
| 133 | + |
| 134 | +Scope:: |
| 135 | +This reaches beyond the repo. Estate Bun-ESM code, such as the DEED printer, |
| 136 | +counts in UTF-16 units today. The owner needs to decide. |
| 137 | + |
| 138 | +== 3. Plain-JS backend has no string builtins (#832) |
| 139 | + |
| 140 | +Evidence:: |
| 141 | +`affinescript compile idx.affine -o idx.js` succeeds. Running the result |
| 142 | +with `node idx.js` fails with `ReferenceError: int_to_string is not defined`. |
| 143 | +`lib/js_codegen.ml:171` emits a free `ExprVar` as a bare name, and nothing |
| 144 | +checks it against what the backend provides. |
| 145 | + |
| 146 | +Classification:: |
| 147 | +Loud divergence, but at run time instead of compile time. |
| 148 | + |
| 149 | +Recommended fix (*most elegant and correct*):: |
| 150 | +Add a closed-world check at codegen. Each text backend declares which |
| 151 | +builtins it can lower. Any free identifier that is neither bound by the |
| 152 | +program nor in that table makes codegen return `Error`. This removes the |
| 153 | +whole bug class, not only on JS. |
| 154 | + |
| 155 | +Most dependable:: |
| 156 | +Share the Bun-ESM builtin lowering table with `js_codegen`, so there is one |
| 157 | +source of truth. |
| 158 | + |
| 159 | +Outside the box:: |
| 160 | +Retire the plain-JS backend, as was done for Deno-ESM. |
| 161 | + |
| 162 | +== 4. `affinescript eval` diverges from `compile` (#833) |
| 163 | + |
| 164 | +Evidence:: |
| 165 | +Self-recursion fails with `Runtime error: Unbound variable: g`. A forward |
| 166 | +reference fails with `Unbound variable: helper`, reported as a *type* error, |
| 167 | +and so does mutual recursion. `let mut` with `while` fails with |
| 168 | +`Type mismatch: Expected mutable reference`. The interpreter's `println` |
| 169 | +prints `"2"` with the quotes. Bun-ESM and wasm run all of these correctly. |
| 170 | + |
| 171 | +Root causes:: |
| 172 | +* `lib/interp.ml:1151`: a top-level closure captures the environment from |
| 173 | +before its own binding exists (there is no letrec). |
| 174 | +* `bin/main.ml:315` and `:363`: `eval` type-checks with its own fold of |
| 175 | +`check_decl`. That fold skips the forward pass in |
| 176 | +`Typecheck.check_program` (`lib/typecheck.ml:2656`). |
| 177 | +* `lib/interp.ml:507`: `StmtLet` ignores `mut`, so the cell that `assign` |
| 178 | +needs is never created. |
| 179 | + |
| 180 | +Classification:: |
| 181 | +Loud divergence. The reference interpreter refuses well-typed programs, and |
| 182 | +the `eval` frontend accepts less than `compile` does. No silent wrong value |
| 183 | +was found. A same-name shadowing probe is refused by the flattener (E0206). |
| 184 | + |
| 185 | +Recommended fix (*most elegant and correct*):: |
| 186 | +* Make `eval` call `check_program`, so every command shares one frontend |
| 187 | +entry. |
| 188 | +* Make `eval_program` two-phase, with a recursive top-level environment. |
| 189 | +* Make `let mut` bind a `VMut` cell, make reads dereference it, and make |
| 190 | +assignment resolve to that cell. |
| 191 | +* Make `println` print the raw string. |
| 192 | + |
| 193 | +Most dependable:: |
| 194 | +Add a differential `eval`-versus-Bun stdout test over the codegen fixture |
| 195 | +corpus. It catches the class, not just these instances. |
| 196 | + |
| 197 | +Outside the box:: |
| 198 | +Move the CLI's interpreter role onto `solo_cesk`. That would also unblock |
| 199 | +#623. |
| 200 | + |
| 201 | +== Reaching beyond this repo |
| 202 | + |
| 203 | +* #834 tracks rebuilding core-wasm artefacts built before #830 in other |
| 204 | +estate repos. In this repo, `editors/vscode/out/extension.cjs` is confirmed |
| 205 | +affected and also stale. |
| 206 | +* The decision on #831 changes what Bun-ESM consumers across the estate |
| 207 | +observe. |
| 208 | + |
| 209 | +== Possible proof work |
| 210 | + |
| 211 | +The #830 invariant (`heap_base >= data_end`, the heap pointer only |
| 212 | +increases, and every allocation is at or above the heap pointer) is a |
| 213 | +two-line lemma over the bump allocator. It belongs next to the existing K-1 |
| 214 | +codegen-preservation work in `formal/`, as a frame property: _no store |
| 215 | +issued by heap allocation aliases a data segment_. It is not claimed here. |
| 216 | +The implementation fix is test-anchored, not proved. |
0 commit comments