Free to LearnSystems fieldbooksCode workshop
Fieldbooks/WebAssembly/Lesson 05
Lesson 05 of 08 · Course index

Lesson 05 / Memory & calls

Will the address wrap?

Build the guard for a write near the edge of an address. Linear memory is one row of numbered bytes. A page contains 65,536 bytes, also called 64 KiB. For an address, the runner reads the 32 bits of −1 as the unsigned number 4,294,967,295. This write adds one byte of offset and needs four bytes.

+ Figure 05
Adding one to 4,294,967,295 produces 4,294,967,296.
Count on the full number line

1 · The big idea

In C or C++, a bad pointer can read secrets or corrupt neighboring code. WebAssembly prevents this with linear memory: a single isolated byte array starting at index 0. One standard memory page is 65,536 bytes (64 KiB), covering addresses 0 through 65535.

A store instruction like i32.store offset=1 takes a 32-bit base address from the stack and writes 4 bytes starting at base + offset. What if a malicious plugin passes base = -1?

WebAssembly treats base as an unsigned 32-bit integer (-1 >>> 0 = 4,294,967,295), then widens the arithmetic to 64-bit precision before adding offset and width. That stops 4,294,967,295 + 1 from wrapping around 32 bits back to byte 0!

2 · Worked step-by-step example

Step-by-step worked trace for lesson 5Widened Unsigned Address Math Defeats 32-Bit WraparoundPage 0: bytes 0..65535Valid end <= 65536(-1 >>> 0) + 1 = 4,294,967,296Does NOT wrap to 0 -> TRAPBase 65529 + offset 2 = 65531..65534 (fits inside 65536).
Worked Example: Widened Addition vs. 32-Bit Wraparound
  1. Plugin pushes base -1 and runs i32.store offset=1 (4 bytes wide).
  2. Convert base to unsigned 32-bit: (-1) >>> 0 = 4,294,967,295.
  3. Add offset in widened arithmetic (NO 32-bit wrap!): start = 4,294,967,295 + 1 = 4,294,967,296, end = 4,294,967,300.
  4. Compare end <= 65536 → false! Traps immediately with 0 bytes modified.
Result: Widening before adding offset guarantees huge addresses never wrap around into valid page 0.

Read the case

Page 0 covers 0 through 65535

Base 4294967295 + offset 1 = start 4294967296

Start + width 4 = end 4294967300, first byte beyond the write

3 · Bytecode stepper (explore before quizzing)

Convert the base to unsigned once. Add offset and width without narrowing the sum to 32 bits. Trap when end exceeds the memory’s byte length. A valid alignment hint can be larger than the actual address alignment: an ordinary unaligned store still works when every byte fits.

Bytecode is the list of machine instructions below. Step through Check types and Run values before answering the prediction below.

Instruction step
Ready0 / 3

Start with an empty stack. Read the instructions in order.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=1 align=4

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const1 / 3

Push the type i32.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=1 align=4

Operand stack top at right

i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const2 / 3

Push the type i32.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=1 align=4

Operand stack top at right

i32i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.store3 / 3

Check the address and value types. The actual address is checked during execution.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=1 align=4

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

Types accepted
Instruction step
Ready0 / 3

Start with an empty stack and one page of zero bytes.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=1 align=4

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.const1 / 3

Push the value -1.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=1 align=4

Operand stack top at right

i32 -1

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.const2 / 3

Push the value 42.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=1 align=4

Operand stack top at right

i32 -1i32 42

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.store3 / 3

Memory access out of bounds: [4294967296, 4294967300) exceeds 65536 bytes.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=1 align=4

Operand stack top at right

i32 -1i32 42

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Base 4294967295 + offset 1 = 4294967296. Four bytes need [4294967296, 4294967300). The end exceeds 65536. No bytes move.

Run trapped

4 · Make a prediction

Does adding the offset send this write back to byte zero?

Compare your prediction. The runner widens the arithmetic before adding the offset. Inspect the attempted start and end at the trap.

Prediction confirmed. The start is 4294967296. Comparing the full end with 65536 rejects the write before it touches memory.

Compare your prediction. Alignment gives a spacing hint. It changes neither the address nor the width of the write.

5 · Repair the program

Place the four bytes at 65531 through 65534. Keep the ordinary four-byte store.
Inspect this edit. Removing the offset leaves an unsigned start of 4294967295. Four bytes there still exceed one page.
Types acceptedRun trappedCalls 0Writes 0
Step through this edit
Instruction step
Ready0 / 3

Start with an empty stack. Read the instructions in order.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=0 align=1

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const1 / 3

Push the type i32.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=0 align=1

Operand stack top at right

i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const2 / 3

Push the type i32.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=0 align=1

Operand stack top at right

i32i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.store3 / 3

Check the address and value types. The actual address is checked during execution.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=0 align=1

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

Types accepted
Instruction step
Ready0 / 3

Start with an empty stack and one page of zero bytes.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=0 align=1

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.const1 / 3

Push the value -1.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=0 align=1

Operand stack top at right

i32 -1

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.const2 / 3

Push the value 42.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=0 align=1

Operand stack top at right

i32 -1i32 42

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.store3 / 3

Memory access out of bounds: [4294967295, 4294967299) exceeds 65536 bytes.

Bytecode instruction address

  1. 00i32.const -1
  2. 01i32.const 42
  3. 02i32.store offset=0 align=1

Operand stack top at right

i32 -1i32 42

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Base 4294967295 + offset 0 = 4294967295. Four bytes need [4294967295, 4294967299). The end exceeds 65536. No bytes move.

Run trapped
Inspect this edit. 65531 plus 2 starts at 65533. Its fourth byte falls outside.
Types acceptedRun trappedCalls 0Writes 0
Step through this edit
Instruction step
Ready0 / 3

Start with an empty stack. Read the instructions in order.

Bytecode instruction address

  1. 00i32.const 65531
  2. 01i32.const 42
  3. 02i32.store offset=2 align=1

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const1 / 3

Push the type i32.

Bytecode instruction address

  1. 00i32.const 65531
  2. 01i32.const 42
  3. 02i32.store offset=2 align=1

Operand stack top at right

i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const2 / 3

Push the type i32.

Bytecode instruction address

  1. 00i32.const 65531
  2. 01i32.const 42
  3. 02i32.store offset=2 align=1

Operand stack top at right

i32i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.store3 / 3

Check the address and value types. The actual address is checked during execution.

Bytecode instruction address

  1. 00i32.const 65531
  2. 01i32.const 42
  3. 02i32.store offset=2 align=1

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

Types accepted
Instruction step
Ready0 / 3

Start with an empty stack and one page of zero bytes.

Bytecode instruction address

  1. 00i32.const 65531
  2. 01i32.const 42
  3. 02i32.store offset=2 align=1

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.const1 / 3

Push the value 65531.

Bytecode instruction address

  1. 00i32.const 65531
  2. 01i32.const 42
  3. 02i32.store offset=2 align=1

Operand stack top at right

i32 65531

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.const2 / 3

Push the value 42.

Bytecode instruction address

  1. 00i32.const 65531
  2. 01i32.const 42
  3. 02i32.store offset=2 align=1

Operand stack top at right

i32 65531i32 42

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.store3 / 3

Memory access out of bounds: [65533, 65537) exceeds 65536 bytes.

Bytecode instruction address

  1. 00i32.const 65531
  2. 01i32.const 42
  3. 02i32.store offset=2 align=1

Operand stack top at right

i32 65531i32 42

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Base 65531 + offset 2 = 65533. Four bytes need [65533, 65537). The end exceeds 65536. No bytes move.

Run trapped
Repair confirmed. 65529 plus 2 starts at 65531. It is unaligned, and all four bytes still fit. Find 42 at 65531.
Types acceptedRun finishedCalls 0Writes 1
Step through this edit
Instruction step
Ready0 / 3

Start with an empty stack. Read the instructions in order.

Bytecode instruction address

  1. 00i32.const 65529
  2. 01i32.const 42
  3. 02i32.store offset=2 align=4

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const1 / 3

Push the type i32.

Bytecode instruction address

  1. 00i32.const 65529
  2. 01i32.const 42
  3. 02i32.store offset=2 align=4

Operand stack top at right

i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.const2 / 3

Push the type i32.

Bytecode instruction address

  1. 00i32.const 65529
  2. 01i32.const 42
  3. 02i32.store offset=2 align=4

Operand stack top at right

i32i32

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

i32.store3 / 3

Check the address and value types. The actual address is checked during execution.

Bytecode instruction address

  1. 00i32.const 65529
  2. 01i32.const 42
  3. 02i32.store offset=2 align=4

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
–65528
–65529
–65530
–65531
–65532
–65533
–65534
–65535
×65536+

Type checking does not read or write bytes.

Types accepted
Instruction step
Ready0 / 3

Start with an empty stack and one page of zero bytes.

Bytecode instruction address

  1. 00i32.const 65529
  2. 01i32.const 42
  3. 02i32.store offset=2 align=4

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.const1 / 3

Push the value 65529.

Bytecode instruction address

  1. 00i32.const 65529
  2. 01i32.const 42
  3. 02i32.store offset=2 align=4

Operand stack top at right

i32 65529

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.const2 / 3

Push the value 42.

Bytecode instruction address

  1. 00i32.const 65529
  2. 01i32.const 42
  3. 02i32.store offset=2 align=4

Operand stack top at right

i32 65529i32 42

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
0065531
0065532
0065533
0065534
0065535
×65536+

Each pair is one byte in base sixteen. The striped guard region marks addresses outside the page.

i32.store3 / 3

The entire range fits. Store four bytes, least significant byte first.

Bytecode instruction address

  1. 00i32.const 65529
  2. 01i32.const 42
  3. 02i32.store offset=2 align=4

Operand stack top at right

[ ] Empty

Control label stack innermost at right

function h=0

Each label keeps its entry height (h = stack height when the block opened).

Linear memory

Tail of page 0
0065528
0065529
0065530
2A65531
0065532
0065533
0065534
0065535
×65536+

Base 65529 + offset 2 = 65531. Four bytes need [65531, 65535). The whole range fits.

Run finished

Code workshop

Implement fits32 in my-rules.mjs. Read the base as unsigned, then add the offset without wrapping and call your range check. The first command makes your own copy of the starter if needed. Edit the named function, then run the second command.

cp -n workshop/starter.mjs my-rules.mjs
node workshop/check.mjs 5 ./my-rules.mjs
Optional worked example in a separate file

Extend fits with the unsigned base and the offset. JavaScript’s >>> 0 reads a number as an unsigned 32-bit integer.

function fits32(base, offset, width, size) {
  const start = (base >>> 0) + offset
  return fits(start, width, size)
}
console.log(fits32(-1, 1, 4, 65536))

Expect false. If you apply >>> 0 after adding the offset, the large start becomes zero. Keep the sum wide enough to compare its full value.

Key Takeaway & Next Up

Linear-memory bounds checks widen unsigned base + offset + width before comparing against page size, defeating integer-wrap escapes.

Next: Next, we verify indirect function calls through a plugin’s host call table.