wasm_global (wasm v0.16.0)

View Source

Mutable globals held outside an instance's state: cells, which two instances can share, and slots, which hold what nothing else can see.

Read this when you link modules that share a global. Module A exports a mutable global, module B imports it, and a global.set in either has to be visible to the other. A value copied into each instance's state cannot do that: the two would silently diverge, which is the same defect imported tables had before 0.2.0.

Why not every global

A cell is an ETS row, and reading one measured 17.7 ns against 1.9 ns for the element/2 read of an inline value. global.get is hot in real compiler output, where the shadow stack pointer is a mutable global read on nearly every function entry, so paying that on every global would be a poor trade for a feature only dynamic linking uses.

So a global becomes a cell only when it is mutable and either imported or exported, which is exactly when sharing is observable. That is decided from the module alone, and wasm_instance resolves it into the instruction when the IR is built, so an ordinary global.get still reads a tuple element.

A global nothing else can see

A mutable i32 or i64 global that is neither imported nor exported, such as a Rust or C guest's stack pointer, is held in a slot: one word of the instance's counter array, #inst.version, written in place. Both tiers read and write the word itself: the instruction carries its index (global_get_slot and global_set_slot). get/1 and set/2 reach it through the handle in #mut.globals, for a snapshot, a restore and a constant expression.

It used to be a value in the instance's mutable state, #mut{}, which a call threads through execution and stores when it ends. A trap does not undo what the computation did, and that state goes with the stack the trap unwinds, so every global.set also wrote the new state to the process dictionary, where a trap, a host function and a nested call could find it: a new state term and a dictionary write for each one, and a stack pointer is set on entry to and exit from most functions. A slot is written where everything reads it, so a trap, a host function that calls back in, a call into another instance and the end of the call all find the last write, and nothing is copied.

Only integers: atomics holds nothing else. A float, vector or reference global that nothing else can see stays a value in #mut{}. A slot holds an i32 as the interpreter does, between -2^31 and 2^31 - 1.

Lifetime

A cell is held by whoever can reach it: the instance that created it, and every instance that imported it. It goes when the last of them lets go, so an exported global outlives the instance that created it. Nothing for you to release.

It was not so. The cell was tied to the creating process alone, so an exported global vanished the moment that process exited and the importer's global.get found nothing.

Summary

Types

An opaque, shareable handle to one mutable global.

One word of an instance's counter array, holding a global's value.

Functions

Add a holder, for an instance importing this global.

Whether an entry of #mut.globals holds a handle, a cell or a slot, rather than the global's value.

Whether a term is a global cell.

Create a cell, saying who holds it.

Remove a holder. The cell goes when the last one lets go.

This cell's registry identity.

A slot: word Ix of Ref, the instance's counter array. It needs no holder and nothing to release, because the array goes with the instance.

The value an entry of #mut.globals stands for: read through a cell or a slot, or the entry itself.

Types

global()

-nominal global() :: {wasm_global, reference()}.

An opaque, shareable handle to one mutable global.

slot()

-nominal slot() :: {wasm_slot, atomics:atomics_ref(), pos_integer()}.

One word of an instance's counter array, holding a global's value.

Functions

acquire/3

-spec acquire(global(), wasm_keeper:token(), pid() | none) -> ok | {error, gone | keeper_unavailable}.

Add a holder, for an instance importing this global.

get/1

-spec get(global() | slot()) -> term().

held/1

-spec held(term()) -> boolean().

Whether an entry of #mut.globals holds a handle, a cell or a slot, rather than the global's value.

is_global/1

-spec is_global(term()) -> boolean().

Whether a term is a global cell.

This doubles as the mutability check across an import boundary: an export hands over a cell for a mutable global and a bare value for an immutable one, so a module that declared the wrong mutability is refused rather than linked.

new(Value)

-spec new(term()) -> global().

new(Value, Opts)

-spec new(term(), map()) -> global().

Create a cell, saying who holds it.

holder is a {Token, Owner} pair naming the registry entry to create and the process whose death removes it. An instance passes its own token, so the cell lives as long as anything that imported it.

release/2

-spec release(global(), wasm_keeper:token()) -> ok.

Remove a holder. The cell goes when the last one lets go.

resource/1

-spec resource(global()) -> wasm_keeper:resource().

This cell's registry identity.

set/2

-spec set(global() | slot(), term()) -> ok.

slot(Ref, Ix)

-spec slot(atomics:atomics_ref(), pos_integer()) -> slot().

A slot: word Ix of Ref, the instance's counter array. It needs no holder and nothing to release, because the array goes with the instance.

value(G)

-spec value(term()) -> term().

The value an entry of #mut.globals stands for: read through a cell or a slot, or the entry itself.