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.
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
- Plugin pushes base
-1and runsi32.store offset=1(4 bytes wide). - Convert base to unsigned 32-bit:
(-1) >>> 0 = 4,294,967,295. - Add offset in widened arithmetic (NO 32-bit wrap!):
start = 4,294,967,295 + 1 = 4,294,967,296,end = 4,294,967,300. - Compare
end <= 65536→ false! Traps immediately with0bytes modified.
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.
4 · Make a prediction
5 · Repair the program
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.mjsOptional 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.
Edit the bytecode
Change the numbered inputs or instructions and run the checker again. Each run starts with a fresh page. This editor accepts a JSON object containing the decoded instructions.