liveBook · Manning

Linear memory

As explained in Server-Side WebAssembly

Linear memory is a contiguous, growable byte array that provides the addressable memory space of a WebAssembly program.

Linear memory is a Wasm program’s contiguous, growable array of bytes. Programs read and write it with explicit byte offsets, rather than through built-in high-level structures. That direct model provides precise control, but it also makes layout, allocation, and access the developer’s responsibility. section 2.1 section 2.6

A string can be represented with two integers: a pointer to its first byte and its byte length. The bytes themselves are stored in linear memory using an encoding such as ASCII or UTF-8. More complex data structures are built from this byte storage plus Wasm’s integer and floating-point primitives. section 2.1 section 2.6

Figure 2.2 Representing a string with Wasm’s linear memory and basic types
A screenshot of a computer

AI-generated content may be incorrect.

The diagram makes the pointer-and-length representation concrete: integers locate and size a sequence of encoded bytes in linear memory. section 2.1

Constructing a greeting in place

A host writes the bytes for "Dan" into guest linear memory, then invokes greet with that location and length. greet calculates space for "Hello, Dan", creates a mutable output slice beginning at byte offset 0, and copies the prefix bytes followed by the input bytes. The host then reads the completed greeting from the same memory space. The old input remains at its original location, illustrating that copying data does not erase its previous bytes. section 2.2 section 2.3.3 section 2.3.5

When working directly with linear memory, keep these constraints in view:

  • Treat offsets and lengths as part of the guest-host contract; the host invokes greet with two i32 values. section 2.3.3
  • Convert pointers and sizes carefully when using Rust memory operations; raw slice access requires unsafe code because incorrect access can cause safety problems. section 2.3.3
  • Prefer richer interface abstractions when manual primitive management becomes intricate or error-prone; WIT supplies language-agnostic types and interfaces. section 2.6
Initial Memory: 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Memory After Host Writes 'Dan': 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 97, 110]
Memory After Host Calls `greet`: 
[72, 101, 108, 108, 111, 44, 32, 68, 97, 110, 0, 0, 0, 68, 97, 110]
Hello, Dan 

The memory dump shows zeroed initial memory, the encoded input bytes, and the final prefix-plus-input bytes after greet runs. section 2.3.5

How Server-Side WebAssembly covers it

How deep it goes
Defines byte-offset access and works through pointer-length strings, Rust raw slices, and a memory trace; defers richer types to WIT.
Examples
Uses a Hello, Dan greeting, displaying host-written bytes and guest-copied prefix and input segments.
What it assumes
Introduces Wasm memory from byte arrays and primitive integers, while requiring Rust unsafe operations for the implementation.
What you can do after
Build it from scratch by passing a pointer and length, copying encoded bytes in guest memory, and reading the result from a host.
How it connects
Connects linear memory to Wasm primitive types, pointers, strings, guest-host architecture, Rust, Wasmtime, and WIT.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Server-Side WebAssembly. The section links point to the parts of the book it draws on.