Basic

One flat memory, two owners

The module has one array of bytes and the host owns the object that holds it. Two owners and one array is the source of the only class of bug that WASM has.

What you will see

One line. A run stamped with its memory size is a run that can be reproduced.

About 1 min of reading

Try It

Show a detached view

1

Write the memory size

printf 'pages 256 bytes 16777216\n' > /tmp/m.txt; awk '{print $2/1048576}' /tmp/m.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
16. Sixteen megabytes is the size of the flat array at start.
2

Grow and re-read

printf 'pages 300\n' > /tmp/m2.txt; awk '{print $1*65536}' /tmp/m2.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
19660800. The number changed, so the view must be re-read after the call.
3

Stamp the size used

printf 'memory_bytes 19660800 date=2026-10-10\n' > /tmp/s.txt; cat /tmp/s.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line. A run stamped with its memory size is a run that can be reproduced.

Data Flow

A growth to a stale view
1 The host holds a view of memory
↓
2 The module grows memory
↓
3 The old view is detached
↓
4 The host reads the new memory and re-reads the view
Why this works
Why the memory is stamped

The size of the memory is a number that changes. A run without the size is a run that cannot be reproduced.

The one rule that prevents the bug

Never hold a view across a call that can grow. Re-read the view after the call, every time.

Examples

The pointer is an index

There is no address space. A pointer is a position in one array, and the array can move.

Growth invalidates views

When the array grows, the host view is detached. A view held across a growth is a view of old memory.

The allocator lives in the module

The host cannot allocate inside the module. The module must own its own allocator, and that allocator is exported.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Compiling to a module
Previous Lesson
Compiling to a module
Next Lesson
Recursion inside async
Recursion inside async →