Skip to content

wasm: heap overlaps string literals — programs allocating >1 KiB corrupt their own literals #830

Done

Description

@hyperpolymath

Summary

Core-wasm memory corruption on main (ee211d3). The heap-pointer global started at a fixed address, 1024. String literals were laid out from a fixed address, 2048. Any program that bump-allocates more than about 1 KiB overwrites its own literals.

Found by the Unicode thread (#829, finding 2).

Reproduction (on main ee211d3)

Sixty println("label_number_N" ++ " ok"); calls in main, compiled with affinescript compile repro.affine -o repro.wasm and run with bun run repro.wasm:

label_number_1 ok
…
label_number_21 ok
label_number_22abe
RuntimeError: Out of bounds memory access

The interpreter (affinescript eval) prints all 60 lines.

This is silent corruption, not a loud failure: line 22 is a plausible-wrong value printed before anything traps. A program that allocates less, or writes over a literal it never prints, corrupts data without trapping.

Scope

  • Affected: the core-wasm backend (lib/codegen.ml), including the Node-CJS / --vscode-extension wrapper, which embeds the same module.
  • Not affected: WasmGC, Bun-ESM, plain JS, the interpreter, and the TEA bridge modules (they have no data segments).
  • There is no memory.grow path. Exhausting the single page traps loudly.

Fix

The fix is in the linked PR.


Generated by Claude Code

Activity

  1. whatproblem-ai commented on Dec 1, 2026

    @whatproblem-ai

    The pinned commit (ee211d3), the 60-println repro and the side-by-side with affinescript eval make this straightforward to verify.

    On the core-wasm backend (lib/codegen.ml), the heap pointer starts at address 1024 and string literals are laid out from 2048. Any program that bump-allocates more than about 1 KiB writes over its own literals. The failure is usually silent: line 22 prints a wrong but plausible value before anything traps. The Node-CJS / --vscode-extension wrapper is affected too, because it embeds the same module. WasmGC, Bun-ESM, JS, the interpreter and the TEA bridge are not affected.

    Nothing is missing for a fixer. A regression test that allocates past the old 1 KiB boundary and checks literal integrity would keep the fix from being undone.

    flowchart LR
        A[📊 Heap starts at 1024] --> B[🔄 Bump alloc grows upward]
        B --> C[⚠️ Passes 2048 after about 1 KiB]
        D[💾 String literals at 2048] --> C
        C --> E[🔴 Literals silently overwritten]
    
    Loading

    🏆 Clarity Score: 95% - Very well written! ✨

    ⌨️ Commands · 💬 Have feedback? Tell us · 🎯 New here? Add WhatProblem to your repo · 🔒 Privacy

  2. added
    architecture
    bug
    priority:p1
    scope:repo
    on Dec 1, 2026
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

    architecturebugpriority:p1scope:repo

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions