Skip to content

fix: multi-memory WASI import lowering and adapter type fixes#20

Merged
avrabe merged 3 commits into
mainfrom
fix/multi-memory-wasi-lowering
Mar 9, 2026
Merged

fix: multi-memory WASI import lowering and adapter type fixes#20
avrabe merged 3 commits into
mainfrom
fix/multi-memory-wasi-lowering

Conversation

@avrabe
Copy link
Copy Markdown
Contributor

@avrabe avrabe commented Mar 9, 2026

Summary

  • Parser/Adapter: Fix type index mapping for Enum/Primitive/Flags types and retptr layout for variants with f64/i64 payloads. Introduces ReturnAreaSlot for canonical ABI-correct byte-level retptr copy instead of fixed 4-byte i32 slots.
  • STPA: Extend safety analysis with CTRL-WRAPPER controller, hazards H-8/H-9/H-10/H-3.5/H-4.5, 11 new UCAs, 4 loss scenarios, safety requirements SR-21–SR-26, and updated traceability matrix.
  • Multi-memory WASI lowering: Fix component_wrap.rs hardcoding CanonicalOption::Memory(0) for all WASI imports. In multi-memory mode, each component's WASI calls now use the correct memory and cabi_realloc. Merger tracks per-import metadata; wrapper uses per-component Memory(N)/Realloc(N) in canon lower.

All 8 wit-bindgen fixture runtime tests pass, including flavorful (promoted from graceful degradation to hard assertion).

Test plan

  • All 155 tests pass (cargo test --package meld-core)
  • All 8 wit-bindgen runtime fixtures pass (numbers, strings, lists, records, variants, options, many-arguments, flavorful)
  • All 8 component validation tests pass
  • cargo clippy --all-targets clean
  • Multi-memory isolation tests pass
  • Shared-memory mode unchanged (dedup key uses None component dimension)

🤖 Generated with Claude Code

avrabe and others added 3 commits March 9, 2026 19:16
Parser: handle Enum/Primitive/Flags in type definition conversion so
pointer_pair_positions are computed correctly for functions using type
aliases (e.g. list-typedef = string). Add ReturnAreaSlot struct that
computes byte_offset/byte_size from canonical ABI layout instead of
using flat i32 sizes, fixing retptr copy for variant types with
f64/i64 payloads.

Resolver: thread return_area_slots through AdapterRequirements.

Adapter: rewrite Phase 3/4/5 retptr handling to iterate
return_area_slots with correctly-sized load/store operations.
Phase 1b/5b conditional copy uses discriminant checks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add hazards H-8 (P2 wrapping), H-9 (conditional pointer), H-10
(import dedup type safety), H-3.5 (start function remap), H-4.5
(retptr variant layout). Add CTRL-WRAPPER controller with UCAs
UCA-W-1 through UCA-W-3. Add 11 new UCAs, 4 loss scenarios, 13
controller constraints, and safety requirements SR-21 through SR-26.
Update traceability matrix with verification status and gaps GAP-4
through GAP-6.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Root cause: component_wrap.rs hardcoded CanonicalOption::Memory(0) for
all WASI imports. In multi-memory mode, code from component N uses
memory N but the lowered WASI function wrote to memory 0, causing
"pointer out of bounds" traps.

Merger: add import_memory_indices and import_realloc_indices metadata.
Use component-aware dedup key (module, field, Option<comp_idx>) so
multi-memory mode preserves separate import slots per component.
Suffix field names with $N for duplicate imports. Export per-component
cabi_realloc$N.

Wrapper: multi-memory stubs module with all memories. Convert ALL
memories to imports from meld:shim. Per-import Memory(N)/Realloc(N)
in canon lower using merger metadata. Strip $N suffixes in import
resolution. Fix core memory index calculation (per-kind index spaces,
not global).

Promote flavorful runtime test from graceful degradation to hard
assertion — all 8 wit-bindgen fixture runtime tests now pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@avrabe avrabe merged commit 067349a into main Mar 9, 2026
3 checks passed
@avrabe avrabe deleted the fix/multi-memory-wasi-lowering branch March 9, 2026 18:21
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.

1 participant