Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .machine_readable/descriptiles/STATE.deed
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,14 @@ landed = [
"docs: bindings/stdlib/alib roadmap Tracking sections de-staled to live umbrellas #446/#412/#413"
]
umbrella-446 = "stays open — STEP 1-3 done, STEP 4/5 long-running"

[[session-delta]]
# Appended 2026-12-01 — core-wasm heap/literal overlap fix (#830).
# Mirror entry only; soundness status is docs/SOUNDNESS.adoc (row #830).
date = "2026-12-01"
issue = "#830"
landed = [
"codegen: heap base computed after codegen as align8(end of last data segment); memory sized to cover it; module refused if heap base < data end (docs/WASM-MEMORY-LAYOUT.adoc)",
"tests: test/test_wasm_heap_layout.ml (layout check, static layout, #829 repro + 40 KiB allocation + exhaustion under Bun WASI)",
"audit: docs/reports/AUDIT-2026-12-01-backend-divergences.adoc; follow-ups #831 (string index units, owner decision), #832 (js builtins), #833 (eval), #834 (estate rebuilds)"
]
22 changes: 21 additions & 1 deletion docs/SOUNDNESS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Freshness stamp — a real main-ancestor commit (survives squash-merge); advance
// it (and --reseal) in any change that touches a soundness path. See "Keeping
// this honest" and tools/check-soundness-ledger.sh (property 4).
:ground-truth-sha: 4248912
:ground-truth-sha: 0cf73b2
:ground-truth-date: 2026-12-01
:stamp-verified-by: tools/check-soundness-ledger.sh
:toc: macro
Expand Down Expand Up @@ -291,6 +291,26 @@ write, so a user `int_to_string` / `string_slice` does not capture them.
core wasm; every wasm case fails on `4248912`. Also: an `extern fn` in scope
keeps the lowering; the lowerings still run; `show`/`slice` are not captured;
a local and an imported definition win)

| #830
| Core-wasm heap allocations can no longer overwrite string literals. The
heap-pointer global used to start at a fixed 1024 while literals were laid
out from a fixed 2048, so a program that allocated more than ~1 KiB
silently corrupted its own literals (#829's 60-line repro printed 21 lines,
then `label_number_22abe`, then trapped). The heap base is now computed
after code generation as the 8-aligned end of the last data segment, the
initial memory covers it, and `generate_module` refuses (compile-time
error) any module whose heap base lies below the end of static data. There
is no `memory.grow`; exhausting the heap is an out-of-bounds trap, not a
write into the literal region. Layout invariant:
`docs/WASM-MEMORY-LAYOUT.adoc`.
| `fixed`
| `test/test_wasm_heap_layout.ml` (`test_wasm_heap_base_below_data_end_refused`
— the gate refuses the old layout; `test_wasm_heap_after_literals_static`;
`test_wasm_heap_repro_runs` and
`test_wasm_heap_literals_intact_after_alloc` — run under Bun's WASI;
`test_wasm_heap_exhaustion_traps`); `lib/codegen.ml`
`memory_layout_of_datas` / `check_memory_layout`
|===

== Still open — read before claiming "sound"
Expand Down
109 changes: 109 additions & 0 deletions docs/WASM-MEMORY-LAYOUT.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-License-Identifier: CC-BY-SA-4.0
// SPDX-FileCopyrightText: 2026 hyperpolymath
= Core-Wasm Linear-Memory Layout
:toc: macro
:icons: font

This page describes how the core-wasm backend (`lib/codegen.ml`) lays out
the single linear memory of every module it emits. It also states the
invariant that the layout must keep. The status of the hole this invariant
closes (#830) is recorded in link:SOUNDNESS.adoc[SOUNDNESS.adoc], and that
ledger is the authority for it.

toc::[]

== Regions

[cols="2,2,5",options="header"]
|===
| Region | Range | Contents

| Runtime scratch
| `[0, scratch_end)` = `[0, 1024)`
| Fixed-address words that `lib/wasi_runtime.ml` writes: the WASI iovec,
the `nwritten` cell and the int-to-string buffer.

| Static data
| `[static_data_base, data_end)`, with `static_data_base = 1024`
| Every active data segment. Today these are string literals, each stored as
`[len: i32][utf8 bytes]` and laid out one after another by `gen_literal`.

| Heap
| `[heap_base, …)`
| The bump allocator behind the heap-pointer global: strings built at run
time, arrays, tuples, records, closures, `float_to_string` buffers and
clock scratch.
|===

`heap_base = align8(max(scratch_end, data_end))`. The initial memory size is
`max(1, ceil(heap_base / 65536))` pages.

== Invariant

*No heap allocation overlaps a static data segment.*

The heap pointer starts at `heap_base`. Allocation only ever increases it,
so every heap address is at least `data_end`.

The constants and functions that keep this invariant are in `lib/codegen.ml`,
under _Linear-memory layout (core-wasm)_:

`memory_layout_of_datas`::
Computes `data_end`, `heap_base` and the page count from the data segments
the module actually carries. It returns an error if two segments overlap or
if a segment lies in the scratch region.

`generate_module`::
Calls `memory_layout_of_datas` after code generation has finished, so every
literal has already been laid out. It then patches the heap-pointer global
to `heap_base` and sizes the memory.

`check_memory_layout`::
Runs on the finished module. It refuses the module with a compile-time
error if the heap global starts below `data_end`, or if the initial memory
does not hold the static data.

An error from either check is an internal compiler error. It is reported
loudly. The compiler never emits a module that would corrupt itself.

== Memory growth

The backend never emits `memory.grow`. If a program allocates past the end
of the module's pages, the access is a wasm out-of-bounds trap. That is a
loud failure, and it never writes into the static region.

If a growth path is added, it must grow memory in place (as `memory.grow`
does) and must not move or reuse `[static_data_base, heap_base)`. When that
happens, `test_wasm_heap_exhaustion_traps` should be changed to expect the
full output.

== History

Before #830 the heap global was initialised to a fixed 1024, and literals
were laid out from a fixed 2048. Any program that allocated more than about
1 KiB overwrote its own literals. In #829's reproduction, 60 concatenated
`println` calls printed 21 correct lines, then `label_number_22abe`, then
trapped.

== Tests

All of these are in `test/test_wasm_heap_layout.ml`, which runs as part of
`dune runtest`. The run tests SKIP when `bun` is not installed.

* `test_wasm_heap_base_below_data_end_refused`: the check refuses the old
layout, overlapping segments, and a segment in the scratch region.
* `test_wasm_heap_after_literals_static`: compiled programs start the heap at
`heap_base`, and their memory covers the static data.
* `test_wasm_heap_repro_runs`: #829's reproduction prints all 60 lines.
* `test_wasm_heap_literals_intact_after_alloc`: after about 40 KiB of
allocation, every literal is still intact.
* `test_wasm_heap_exhaustion_traps`: allocating past the page traps loudly.

== Built artefacts

Wasm built by an older compiler still has the overlapping layout, and
recompiling it fixes that. To check a module, read its heap global and its
data segments with `wasm-tools print`. If the module has a mutable i32
global initialised to `1024` and data segments starting at `2048`, it was
built before the fix. This includes `editors/vscode/out/extension.cjs`,
which embeds such a module. The estate tracking note is #834.
Loading
Loading