Skip to content

interpreter: evaluate const-expr data/elem segment offsets at instantiation - #150

Merged
mfornet merged 1 commit into
mainfrom
claude/issue-101-pr-372e1e
Jul 15, 2026
Merged

interpreter: evaluate const-expr data/elem segment offsets at instantiation#150
mfornet merged 1 commit into
mainfrom
claude/issue-101-pr-372e1e

Conversation

@mfornet

@mfornet mfornet commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Fixes #101.

A data or element segment whose offset is a global.get (or extended-const arithmetic) const-expr was placed at offset 0: the decoder folded non-literal offset expressions to a 0 placeholder, so the offset was never evaluated at instantiation.

Change

  • Wasm.DataSegment / Wasm.ElementSegment gain an offsetExpr : Program := [] field, mirroring GlobalDecl.initExpr. The WAT decoder keeps the parsed const-expr program (wrapped (offset …) and bare abbreviated forms) instead of collapsing it to 0; offset := some 0 remains as the active-segment marker.
  • Module.initialStore skips segments with a pending offsetExpr — their offset depends on the store (imported globals), so they cannot be written there.
  • New Module.runActiveSegments (Semantics.lean) evaluates each pending offsetExpr against the current store and writes the segment at the computed offset — data bytes into the right (multi-)memory, funcref/GC-item elements into the right table slot. It runs after Module.runConstGlobals, so offsets see the evaluated and imported globals; runConstElems skips these segments to avoid double-writing.
  • Wired into both instantiation pipelines: the testsuite driver (Testsuite/Exec.lean) and the runner (Runner.lean).
  • New example Examples/SegmentOffsetExpr.lean is the regression test: the issue's exact scenario (data at global.get $o = 4, elem at global.get $t = 2) proven end-to-end via native_decide, including that byte 0 stays zero (segment moved, not duplicated).

Testsuite report

testsuite_report.txt regenerated (all three packages lake build cleanly):

  • 8 × module decode_errorpass (data.wast 178/183/188/195, elem.wast 128/135/178/182)
  • 4 × assert_return failpass (elem.wast 1065/1076/1087/1109 — call_indirect through a global-offset elem segment)
  • 11 × assert_invalid passskipped: not rejected: ill-typed offset const-exprs (e.g. (offset (nop) …), two-value offsets) were previously rejected only because the decoder couldn't parse non-literal offsets at all; they now decode and join the existing not-validated skip bucket (data.wast already carried 25 such skips).

🤖 Generated with Claude Code

…iation

An active data or element segment whose offset is a non-literal constant
expression (`global.get`, extended-const arithmetic) was decoded with a
`0` placeholder, so the segment landed at offset 0 instead of the
computed offset.

Keep the parsed const-expr program in a new `offsetExpr` field on
`DataSegment` / `ElementSegment` (mirroring `GlobalDecl.initExpr`).
`Module.initialStore` defers such segments, and a new instantiation pass
`Module.runActiveSegments` — run after `Module.runConstGlobals`, so the
offsets see the evaluated (and imported) globals — evaluates each
`offsetExpr` and writes the segment at the computed offset. Wired into
the testsuite driver and the runner.

Testsuite: data.wast/elem.wast lose all 8 offset-related decode errors
and 4 downstream assert_return failures; 11 assert_invalid commands that
were rejected only because the decoder choked on non-literal offsets now
decode and move to the standard not-rejected skip bucket.

Closes #101

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Verifier report preview: https://talos-verifier-report-pr-150.vercel.app

(This URL is stable for this PR — it always points to the latest build of 9869340.)

@mfornet
mfornet merged commit a01d01c into main Jul 15, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

data/elem segment offsets from a global.get land at 0

1 participant