Skip to content

fix(wasm): prevent u32 arithmetic overflow in memory region checks#1305

Open
AftAb-25 wants to merge 1 commit intomofa-org:mainfrom
AftAb-25:fix/wasm-memory-overflow
Open

fix(wasm): prevent u32 arithmetic overflow in memory region checks#1305
AftAb-25 wants to merge 1 commit intomofa-org:mainfrom
AftAb-25:fix/wasm-memory-overflow

Conversation

@AftAb-25
Copy link
Contributor

Fixes #1303

Three sites in wasm_runtime/memory.rs used unchecked u32 arithmetic that overflowed when memory regions were positioned near the end of the 32-bit address space:

  • MemoryRegion::contains: rewrote start + size comparison using subtraction (addr - start < size) to avoid the addition that wraps.

  • WasmMemory::size: replaced pages * PAGE_SIZE with pages.saturating_mul(PAGE_SIZE) to prevent wrapping when the simulated page count is extreme.

  • MemoryAllocator::return_block: replaced + with saturating_add in both coalescing branches to prevent overflow when merging large adjacent free blocks.

Three unit tests are added to confirm each site no longer panics or produces incorrect results at the u32 boundary.

📋 Summary

This PR prevents arithmetic overflow bugs in the WASM memory management module. When memory regions hit the bounds of a 32-bit address space, unchecked addition/multiplication wrapped around to incorrect smaller numbers. This fixes the bounds-checking math to ensure memory safety isn't compromised at the edges.

🔗 Related Issues

Closes #1303


🧠 Context

Because Rust defaults to wrapping arithmetic in release mode and panicking in debug mode, operations near u32::MAX would result in either runtime crashes or incorrect security checks (e.g. low addresses matching incorrectly inside a wrapped high-address bounding box).


🛠️ Changes

  • Rewrote MemoryRegion::contains inequality boundary check without addition.
  • Added saturating_mul to WasmMemory::size and saturating_add to MemoryAllocator::return_block.
  • Added new bounding-specific unit test assertions ensuring no panics occur near u32::MAX.

🧪 How you Tested

  1. Added programmatic isolation tests in memory.rs that explicitly allocate boundary-adjacent regions and simulate crossing the 4GB boundary.
  2. Verified unit tests run without failing (cargo test -p mofa-plugins)
  3. cargo clippy run locally to ensure adherence to syntax constraints.

📸 Screenshots / Logs (if applicable)


⚠️ Breaking Changes

  • No breaking changes
  • Breaking change (describe below)

If breaking:


🧹 Checklist

Code Quality

  • Code follows Rust idioms and project conventions
  • cargo fmt run
  • cargo clippy passes without warnings

Testing

  • Tests added/updated
  • cargo test passes locally without any error

Documentation

  • Public APIs documented
  • README / docs updated (if needed)

PR Hygiene

  • PR is small and focused (one logical change)
  • Branch is up to date with main
  • No unrelated commits
  • Commit messages explain why, not only what

Fixes mofa-org#1303

Three sites in wasm_runtime/memory.rs used unchecked u32 arithmetic
that overflowed when memory regions were positioned near the end of the
32-bit address space:

- MemoryRegion::contains: rewrote start + size comparison using
  subtraction (�ddr - start < size) to avoid the addition that wraps.

- WasmMemory::size: replaced pages * PAGE_SIZE with
  pages.saturating_mul(PAGE_SIZE) to prevent wrapping when the simulated
  page count is extreme.

- MemoryAllocator::return_block: replaced + with saturating_add
  in both coalescing branches to prevent overflow when merging large
  adjacent free blocks.

Three unit tests are added to confirm each site no longer panics or
produces incorrect results at the u32 boundary.
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.

bug(wasm): Arithmetic overflow in MemoryRegion::contains bounds checking

1 participant