Linear Memory
Numbers in locals and globals only get you so far. Arrays, strings, structs, images: anything bigger lives in linear memory, a single contiguous array of bytes that the module can read and write by address.
address: 0 1 2 3 4 5 6 ... 65535
┌────┬────┬────┬────┬────┬────┬────┬───┬────┐
bytes: │ 00 │ 00 │ 00 │ 00 │ 00 │ 00 │ 00 │ … │ 00 │ one page = 64 KiB
└────┴────┴────┴────┴────┴────┴────┴───┴────┘Declaring memory
(memory 1) ;; initial size 1 page (64 KiB), no maximum
(memory 2 10) ;; 2 pages now, may grow to at most 10
(memory (export "memory") 1)Sizes are counted in pages of 65,536 bytes. A fresh memory is filled with zeros. In classic WebAssembly a
module has at most one memory (the multi-memory feature lifts this), and addresses are i32 values
(the memory64 feature adds 64-bit addresses).
Store and load
i32.store takes an address and a value; i32.load takes an address and pushes the value found there.
(module
(import "console" "log" (func $log (param i32)))
(memory 1)
(func (export "main")
(i32.store (i32.const 0) (i32.const 1234)) ;; bytes 0..3
(i32.store (i32.const 4) (i32.const 5678)) ;; bytes 4..7
(call $log (i32.load (i32.const 0)))
(call $log (i32.load (i32.const 4)))
(call $log (i32.load (i32.const 8))))) ;; never written: zero1234
5678
0
An i32 occupies 4 bytes, so consecutive integers are 4 addresses apart. The same goes for f32; i64 and
f64 take 8 bytes.
Byte order: little-endian
Multi-byte values are stored little-endian: the least significant byte comes first, at the lowest address. You
can observe it by storing an i32 and loading single bytes with i32.load8_u:
(module
(import "console" "log" (func $log (param i32)))
(memory 1)
(func (export "main")
(i32.store (i32.const 0) (i32.const 0x11223344))
(call $log (i32.load8_u (i32.const 0))) ;; 0x44 = 68
(call $log (i32.load8_u (i32.const 1))) ;; 0x33 = 51
(call $log (i32.load8_u (i32.const 2))) ;; 0x22 = 34
(call $log (i32.load8_u (i32.const 3))))) ;; 0x11 = 1768
51
34
17
This is fixed by the spec, on every CPU, so a module behaves identically on x86, ARM and RISC-V.
Arrays are just addresses
There is no array type. An array of i32 is a base address plus index × 4. Let's fill an array with squares and
sum it:
(module
(import "console" "log" (func $log (param i32)))
(memory 1)
(global $base i32 (i32.const 256)) ;; where the array starts
;; address of element i
(func $addr (param $i i32) (result i32)
(i32.add (global.get $base) (i32.shl (local.get $i) (i32.const 2)))) ;; base + i*4
(func (export "main")
(local $i i32) (local $sum i32)
;; arr[i] = i * i, for i in 0..9
(loop $fill
(i32.store (call $addr (local.get $i)) (i32.mul (local.get $i) (local.get $i)))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br_if $fill (i32.lt_u (local.get $i) (i32.const 10))))
;; sum the array
(local.set $i (i32.const 0))
(loop $add
(local.set $sum (i32.add (local.get $sum) (i32.load (call $addr (local.get $i)))))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br_if $add (i32.lt_u (local.get $i) (i32.const 10))))
(call $log (i32.load (call $addr (i32.const 7))))
(call $log (local.get $sum))))49
285
i32.shl by 2 multiplies by 4, the usual way compilers scale an index.
Bounds checking
Every access is checked against the current memory size. Reading the very last valid i32 works, but one byte
further traps:
(module
(import "console" "log" (func $log (param i32)))
(memory 1)
(func (export "main")
(i32.store (i32.const 65532) (i32.const 99)) ;; bytes 65532..65535: the last 4
(call $log (i32.load (i32.const 65532)))
(call $log (i32.load (i32.const 65533))))) ;; would need byte 6553699
RuntimeError: memory access out of bounds
Note: Address 0 is a perfectly valid address in WebAssembly: there is no "null pointer trap" like on native systems. C compilers usually leave the first bytes unused so that a null pointer bug at least reads harmless zeros, but it won't crash.
Memory size
memory.size returns the current size in pages:
(module
(import "console" "log" (func $log (param i32)))
(memory 3)
(func (export "main")
(call $log (memory.size))
(call $log (i32.mul (memory.size) (i32.const 65536))))) ;; in bytes3
196608
Memory can also grow at runtime; that has its own lesson, Memory Growth.
Sharing memory with the host
The host sees the exported memory as an ArrayBuffer (instance.exports.memory.buffer) and can wrap it in typed
arrays such as Uint8Array or Int32Array. Both sides see the same bytes, which is how data moves between
JavaScript and WebAssembly without copying. The lesson Passing Strings & Arrays shows it in action.
Test yourself
How big is one WebAssembly memory page?
- 4 KiB
- 64 KiB
- 1 MiB
- It depends on the CPU
After (i32.store (i32.const 0) (i32.const 0x0A0B0C0D)), what byte is at address 0?
- 0x0A
- 0x0B
- 0x0C
- 0x0D
Where is element 5 of an i32 array that starts at address 1000?
- 1005
- 1020
- 1025
- 5000