Skip to content

wasm: a user fn named like an intrinsic (string_sub, …) is silently replaced by the intrinsic — wrong value #836

Description

@hyperpolymath

Found by the #829 thread. Reproduced independently on main ee211d3 and on #835's branch.

Evidence

fn string_sub(s: String, a: Int, b: Int) -> Int { 7 }
fn main() -> () { println(int_to_string(string_sub("hello", 0, 2))); }
Backend Output
interp (eval) 7
Bun-ESM 7
core wasm 1024 on main (1040 with #835)

The wasm number is a heap pointer: the address of the intrinsic's substring.

wasm-tools print shows the user's function compiled as func 1. main never calls it.

Classification

Soundness-class hole: a silent wrong value. A well-typed program prints a plausible number and nothing fails.

Cause

gen_expr in lib/codegen.ml intercepts calls by name, as in ExprVar id when id.name = "string_sub". It does this before it checks whether the name resolves to a user declaration. Twenty names are intercepted this way: char_to_int clock_now_ms file_close file_fd_write file_open file_read float_to_string int_to_string net_accept net_recv net_send net_shutdown print println string_char_code_at string_find string_from_char_code string_slice string_sub trim.

#829 fixed only its three new intrinsics.

Options

Ranked by elegance and correctness first, then by dependability.

  1. (Recommended: most elegant and correct) Intercept on resolution, not on spelling. The resolver should tag a call as a builtin only when the name resolves to the builtin or stdlib-extern symbol. Codegen should match on that tag, so a user declaration always shadows. Some arms deliberately intercept stdlib extern fn declarations, for example the net and file externs. Those arms must keep matching the extern symbol, so each of the 20 arms needs auditing to confirm which interceptions are intended.
  2. (Most dependable, smaller) At the top of each arm, guard: not (is_user_defined ctx id.name). The user's function then wins. One test per name, compiled to wasm and run, asserts the user's result.
  3. (Fail-loud stop-gap) Refuse, at compile time, a user top-level declaration that reuses an intercepted name on the wasm backend.

Pinned in docs/SOUNDNESS.adoc as open (tracked) by an xfail pin (PR #835). Audit: docs/reports/AUDIT-2026-12-01-backend-divergences.adoc.


Generated by Claude Code

Activity

  1. whatproblem-ai commented on Dec 1, 2026

    @whatproblem-ai

    Problem: On the core wasm backend, a user function whose name matches one of 20 intercepted intrinsics (e.g. string_sub) is never called. gen_expr in lib/codegen.ml matches calls by name before resolving them, so the intrinsic runs in its place. The program silently returns a heap pointer (1024 on ee211d3), while interp and Bun-ESM return 7.

    Expected: A user declaration shadows the intrinsic on every backend. wasm output should match interp and Bun-ESM (7).

    Missing: which of the 20 arms are meant to intercept stdlib extern fn symbols (file/net) and must keep doing so


    Clarity: 95%. Clear enough to work on.

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

  2. coderabbitai commented on Dec 1, 2026

    @coderabbitai
    Contributor
    🔗 Related PRs

    #829 - feat(strings): backend-independent UTF-8 octet count and code-point view [open]


    📝 Issue Planner

    Check the box below or use the @coderabbitai plan command to generate an implementation plan and prompts that you can use with your favorite coding assistant.

    • Create Plan

    🧪 Issue enrichment is currently in open beta.

    You can configure auto-planning by selecting labels in the issue_enrichment configuration.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord!

  3. added
    architecture
    bindings
    bug
    priority:p1
    scope:repo
    on Dec 1, 2026
  4. added 3 commits that reference this issue on Dec 1, 2026
  5. added 5 commits that reference this issue 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

    architecturebindingsbugpriority:p1scope:repo

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions