Skip to content

Check memory64 addr+offset overflow in wasm2c store/atomic/simd ops - #2790

Merged
sbc100 merged 2 commits into
WebAssembly:mainfrom
aizu-m:memory64-store-offset-overflow
Jul 8, 2026
Merged

sbc100 merged 2 commits into
WebAssembly:mainfrom
aizu-m:memory64-store-offset-overflow

Conversation

@aizu-m

@aizu-m aizu-m commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Comparing wasm2c output either side of 4bedf7c ("check address + offset addition for memory64"):

// load  (fixed in 4bedf7c)
i64_load(&M0, (u64)checked_add_u64(var_j0, 16u));
// store (still unchecked)
i64_store(&M0, (u64)(var_j0) + 16, var_j1);

That commit only converted CWriter::Write(const LoadExpr&). For an i64 memory the store still adds the static offset with a wrapping +, and so do the SIMD lane/splat/zero and atomic emitters.

How I hit it: memory64 module, i64.store offset=16 at address 0xFFFFFFFFFFFFFFF0. The sum overflows u64 to 0, the generated RANGE_CHECK sees an in-range address, and the write lands at guest address 0. Per the spec that access should trap; the load form already does after 4bedf7c.

Applied the same checked_add_u64 guard to StoreExpr, SimdLoadLaneExpr, SimdStoreLaneExpr, LoadSplatExpr, LoadZeroExpr, AtomicLoadExpr, AtomicStoreExpr, AtomicRmwExpr and AtomicRmwCmpxchgExpr for is_64 memories. The 32-bit output is byte for byte unchanged. The wasm2c spec tests (memory64, threads, simd) still pass.

Comment thread src/c-writer.cc Resolved
@sbc100
sbc100 requested a review from shravanrn July 7, 2026 16:16

@shravanrn shravanrn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I have the same comment as @sbc100 : it would be helpful if you could pull this into a helper function that can be invoked in load/store, simd load/store, atomic load/store to simplify the code.

@aizu-m

aizu-m commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Pushed the helper. One note on output: the old emitters disagreed on the literal suffix for the 32-bit path (loads wrote + 16u, most stores + 16). The helper normalises to u everywhere, so 32-bit store output changes cosmetically. No checked-in test expectations contain that pattern, so nothing needed rebasing.

Re-ran the checks after the refactor: wasm2c tests 265/265, unit tests 134/134, clang-format clean. The memory64 repro (store at 0xFFFFFFFFFFFFFFF0 with offset=16) still traps instead of wrapping to guest address 0.

@sbc100

sbc100 commented Jul 8, 2026

Copy link
Copy Markdown
Member

@shravanrn can you take another look?

@shravanrn shravanrn left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aizu-m Thanks for addressing this! Looks good to me.

@shravanrn
shravanrn force-pushed the memory64-store-offset-overflow branch from a33bd27 to 71a8219 Compare July 8, 2026 20:11
@sbc100
sbc100 enabled auto-merge (squash) July 8, 2026 20:12
@sbc100
sbc100 disabled auto-merge July 8, 2026 23:56
@sbc100
sbc100 merged commit fb04713 into WebAssembly:main Jul 8, 2026
17 checks passed
@aizu-m

aizu-m commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews and the merge. The helper ended up tidier than the original anyway.

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.

3 participants