Skip to content

Optimize memory bounds checks on narrow targets - #13

Merged
espmaniac merged 1 commit into
mainfrom
agent/optimize-memory-bounds
Aug 12, 2026
Merged

Optimize memory bounds checks on narrow targets#13
espmaniac merged 1 commit into
mainfrom
agent/optimize-memory-bounds

Conversation

@espmaniac

Copy link
Copy Markdown
Member

Summary

  • keep the existing 64-bit effective-address check on native 64-bit targets
  • use an overflow-safe 32-bit memory32 check on narrower targets
  • apply the optimized address type and bounds check to all load/store handlers

Why

WebAssembly memory32 base addresses and instruction offsets are unsigned 32-bit values. The previous implementation promoted every effective address to u64, which makes narrow targets such as AVR emit software 64-bit addition and comparison helpers for memory operations.

The new narrow-target path detects 32-bit addition wraparound before validating the access size. It is selected by native pointer width rather than by any platform-specific AVR condition.

Impact

ATmega1284P build with float, fuel, and snapshots explicitly enabled:

  • flash: 109,972 -> 105,706 bytes (-4,266 bytes / -3.88%)
  • static RAM: 5,188 -> 5,188 bytes
  • 57 load/store handlers: 12,752 -> 8,460 bytes
  • software 64-bit helper calls in memory handlers: 132 -> 0

The x86-64 build keeps the previous code path and its measured binary size is unchanged.

Validation

  • built the full AVR configuration with fuel and snapshots enabled
  • checked valid last-byte loads/stores, zero-length memory, end-crossing accesses, and effective-address wraparound
  • compared the narrow check with the original 64-bit reference across 1,000,000 generated inputs
  • ran native fuel tests in default, no-float, and no-bounds-check configurations
  • ran an AddressSanitizer/UndefinedBehaviorSanitizer boundary check

@espmaniac
espmaniac marked this pull request as ready for review August 12, 2026 10:08
@espmaniac
espmaniac merged commit 470fd90 into main Aug 12, 2026
100 checks passed
@espmaniac
espmaniac deleted the agent/optimize-memory-bounds branch August 12, 2026 10:08
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