The ABI is the byte-level contract between a compiled module and the runtime
that runs it. It lives in one crate, crates/atoll-abi, which depends on the
memory model and on nothing of the compiler: the compiler states each layout
there, and the runtime, an embedder and a shipped program read it from there
rather than restating it. A field renamed in Rust is harmless; a size, an
offset, a tag or a meaning changed is a change to the contract.
The layouts below are #[repr(C)] structs, and the offsets are what
core::mem::offset_of! answers for them. Each struct carries compile-time
assertions of the properties its readers rely on, so a field moved without
moving its readers fails to build. Where this page and those assertions
disagree, the assertions are right.
| Module | What it states |
|---|---|
rt |
the identities and the two structures both sides read at fixed offsets: FrameHeader and InstanceContext |
context, memory |
where the context, the heap and the reserved areas sit in linear memory |
allocator |
the block format every value is laid out against |
runtime, host |
the functions a module imports from the runtime and from its hosts |
module, module_file |
the exports a host binds a module by, and the file it travels in |
host_artifact, value_shape |
the record a Rust host implements its @host declarations against |
Pointers
Atoll has one pointer width, 64 bits, on every target (AtollPtr,
crate::ptr). An address is a byte offset into the module’s linear memory:
the guest sees that memory based at zero, so the offset is the address there,
and the host adds its own base to it. A native pointer stored by either side
means nothing to the other. The target’s address width decides how a memory
operation is emitted, not how wide a stored pointer is.
Address zero is null. The first 4 KiB of memory belong to nothing
(context::GUARD_BYTES), so a field read or an element read off a null base
lands in the guard rather than in somebody’s data.
Identities and tags
FunctionId is a u32 naming a resume point, not a function: a suspending
Atoll function is emitted as one wasm body entered at a different block per
resume point, and each point has its own id. Ids are dense from zero over the
module’s guest continuations, so the dispatcher’s br_table covers exactly
them. They are local to one build.
How a suspended call ended is one of two tags:
| Value | Constant | Meaning |
|---|---|---|
0 |
TAG_OK |
the call ran; its result is already written through the caller’s out-pointer |
1 |
TAG_CANCELLED |
the call was cancelled and produced no result |
An error is not a tag. A call that fails returns a Result through the
out-pointer like any other value, and a timeout is a call that returns.
NOT_FINISHED (-1) is not a tag either: it is what a continuation answers
when it parked, negative so one i32.lt_s separates not finished from both
tags.
A suspending @host import answers neither. It has the declaration’s own
signature, as a direct import does, and a call that did not answer says so
through the instance context’s park word: the guest reads the word after
the call, and when it is set hands atoll_parked the frame to resume and the
address the answer goes to, then returns NOT_FINISHED to the dispatcher.
Continuations
Every continuation takes the same two parameters, (frame, tag): the frame’s
address and how the call it was waiting on ended (CONTINUATION_PARAMS). The
user function’s arguments live in its frame, and its result goes through the
frame’s out-pointer at whatever width its type has, so a resume carries
nothing else. The scheduler resumes a frame through the module’s
atoll_dispatch export (module::DISPATCH_EXPORT), which takes a
FunctionId and a frame.
Frame header
Every frame the scheduler dispatches into starts with a FrameHeader, 24
bytes with eight-byte alignment. Each field is about this frame:
| Offset | Field | Type | Meaning |
|---|---|---|---|
0 |
caller_frame |
AtollPtr |
the frame to resume when this one finishes; 0 (NO_CALLER) when finishing this frame finishes the task |
8 |
out_pointer |
AtollPtr |
where this frame’s result is written; 0 for a result with no bytes |
16 |
entry |
FunctionId |
where this frame enters next |
20 |
flags |
FrameFlags (u32) |
the frame’s provenance |
Whoever makes a frame runnable writes entry: a caller writes its callee’s
when it sets the frame up, and a suspending call writes its own resume point
into its own frame before it hands off. The scheduler only reads it, so
enqueue_ready, enqueue_resume and atoll_spawn take a frame and nothing
about functions. The caller owns the result’s slot and hands its address in
out_pointer, except for a spawned task, whose slot the scheduler provides.
| Bit | Constant | Meaning |
|---|---|---|
1 |
FRAME_SCHEDULER_OWNS |
the scheduler gives this frame’s block back when the frame is finished; clear for a frame carved out of its caller’s |
2 |
FRAME_CALLER_ON_STACK |
the caller reached this frame with an ordinary call and is on the wasm stack below it, so finishing is a return; cleared when the frame parks |
A non-suspending function’s frame carries no header: the scheduler never sees
it, and it is cut from the running task’s frame region rather than allocated
(see stack_top below).
Instance context
One InstanceContext per module instance holds what is ambient to it: which
heap, and which task. It sits at context::ADDRESS, just past the guard, and
compiled code loads its fields at that address plus a constant offset, with
no parameter or global to keep in step. 312 bytes, eight-byte alignment:
| Offset | Field | Type | Meaning |
|---|---|---|---|
0 |
allocator |
AtollPtr |
the instance’s segalloc header, which does not move as the heap grows |
8 |
active_task |
u64 |
the scheduler’s handle for the running task, or NO_TASK (u64::MAX) where none runs |
16 |
preempt |
u64 |
nonzero when the host asks the running dispatch to stop at its next poll |
24 |
stack_top |
AtollPtr |
where the running task’s next non-suspending frame goes |
32 |
stack_limit |
AtollPtr |
one past the end of that task’s frame region |
40 |
raises |
u64 |
how many error raises the instance has made, ever |
48 |
raise_ring |
16 × Raise |
the last RAISE_RING raise sites, indexed by raises % 16 |
304 |
park |
u64 |
nonzero when the suspending @host call just made did not answer, or the running task was cancelled since its last one; written by the host only |
A Raise is 16 bytes: site, the raise’s file in the high 32 bits and its
byte offset in the low 32, then task, the active_task it was raised in.
stack_top and stack_limit are the running task’s, saved and restored by
whoever publishes active_task, which is the one moment a task changes. A
task with no region has both at zero, so every frame fails the bound and is
allocated instead: a frame that does not fit is a slower frame, not a stack
overflow.
Memory below the heap
The context is followed, in order, by the heap’s control block
(context::HEAP_ADDRESS) and its class table, then a sink for stores an
optimization made unconditional (STORE_SINK), then the area a preemption
poll saves live values to (POLL_SAVE, 512 bytes). The addresses are
constants in context, so emitted code reaches each with a constant offset
and nothing is passed.
The module file
A compiled module is one ordinary wasm file. What a host needs beside the
code — frame sizes, argument offsets, the FunctionId space, the constant
pool’s region, which entry points start a task — travels inside it, in a
custom section named atoll.module: an eight-byte magic, an eight-byte
producer word, then the record, bincode-encoded from its type. A consumer
that does not know the section ignores it, so wasmtime and other wasm
tools read the file unchanged.
The magic is compared before the record is decoded, because bincode is not
self-describing: a record whose shape changed would decode into something
else, and the something else is an offset a host reads memory at. A test
pins the encoding, so a shape change does not build until the magic changes
with it. crates/atoll-abi/docs/module-file.md has the format and what
each magic changed.
Changing the contract
A change to a layout here changes both sides at once, because both read it
from atoll-abi:
- change the struct or constant in
atoll-abi, and the assertions beside it; - change the emitter’s stores and loads and the runtime’s reads in the same change, which the shared constants make one edit rather than two;
- where the change alters what a module file carries, change its magic;
- update this page.
A field added to a #[repr(C)] structure moves every field after it, and a
field reordered moves the fields between; both are contract changes, whatever
the Rust names say.