Section 2.24

Scratch memory

Definition

Scratch memory is a fixed-size memory region that a host lends to a guest program for temporary data during one run, such as a record being assembled or an intermediate result too large for registers. A guest program is code that a host runs inside a virtual machine §2.3 (VM). The guest reads and writes the region freely within its bounds; nothing in it is meant to outlive the run. Two rules define it beyond its size: whether it is cleared between runs, and who else can reach its bytes.

The idea

A guest needs 64 bytes of workspace on every run. Allocating 64 fresh bytes for each run costs time and can fail, so hosts usually reserve the region once and lend it out again and again. Reuse brings the first problem: the region still holds the previous run’s bytes. Suppose run A, working for one user, writes a secret value into byte 3. Run B, working for another user, reads byte 3 before writing it and learns A’s secret. B’s read was inside the region, so a bounds check allowed it. A bounds check answers where a guest may read; it does not answer whose data is there.

The second problem is sharing. If one region serves every run, and a second run can start while the first is still working, for example from an interrupt §6.11 or on another processor, the two runs overwrite each other’s data, again without a single out-of-bounds access.

A whiteboard in a shared meeting room is a fair picture. The next meeting sees the last one’s notes unless someone wipes the board, and two meetings in the room at once spoil each other’s notes. The picture stops at visibility: notes left on a whiteboard are plain to see, while old bytes in memory look like any other bytes.

How it works

A design for scratch memory makes three decisions.

  1. Where it lives. A local array §14.5 of the host function that runs the guest lives on the native stack, the host thread’s call stack. It costs stack space, which is scarce on a small device, and it starts with whatever earlier function calls left there. A static array, allocated once for the whole program, costs no stack but is one region shared by every run. A region allocated from the heap for each run is private to that run but costs time and can fail.

  2. How freshness is guaranteed. Either the host clears the region, writing zero to every byte before the run starts, or it tracks which bytes the current run has written and refuses any read of a byte not yet written in this run. The Linux eBPF §2.1 (extended Berkeley Packet Filter) verifier applies the second rule to a program’s stack before the program runs: a program may read stack memory only after writing it. Under tracking, old bytes remain in memory but no path can read them. Tracking must cover the full width of each access: a 4-byte read needs all 4 bytes written, not only the first. If data must survive from one run to the next on purpose, it is not scratch memory but guest persistent state §2.36, with its own contract.

  3. Who can reach it. List every party that holds an address inside the region: the guest; every helper function §2.19 the guest passes a pointer to; the host code that set the region up; a second run of the guest started by another thread, by an interrupt handler §6.12 or on another processor; and any party that kept a pointer from an earlier run. Only the run that currently owns the region, and the helpers and host code acting for that run, may use it.

Two arrangements give a run exclusive use. One is a separate region for each place where a run can execute: each thread, each processor, each interrupt level. Linux eBPF programs often use a per-CPU array map as a large scratch area. Each processor gets its own copy, so runs on different processors do not share it, but a run interrupted by another run on the same processor still shares it. The other arrangement is a lock or flag that prevents a second run from starting while the region is in use; the second run then waits or is refused.

Worked example

Take a 4-byte scratch region, positions 0 to 3, under the tracking rule. At the start of run A the set of written positions is empty. The guest writes 9 at position 1 and 8 at position 2, so the written set becomes {1,2}\{1, 2\}. A 2-byte read at position 1 needs positions 1 and 2, both written: allowed, and it returns 9 and 8. A 2-byte read at position 2 needs positions 2 and 3. Position 3 lies inside the region but has not been written in this run: refused.

Run B starts with an empty written set again. Positions 1 and 2 still physically hold 9 and 8, but a read of position 1 is refused until B writes it. Under the clearing rule the host would instead write 4 zero bytes before B starts, and every read would return 0 until B writes. Clearing costs work in proportion to the size: a 512-byte region needs 512 single-byte stores, or 512/4=128512 / 4 = 128 stores of 4 bytes each, before every run.

Now let one static 4-byte region serve two runs. Run A, on a thread, writes 1, 2, 3 and 4. An interrupt arrives and starts run B, which clears the region to 0, 0, 0, 0, writes 7 at position 0 and returns. Run A resumes, reads position 0 expecting 1, and gets 7. No access was out of bounds and the clearing rule was followed. The error is that two live runs shared one region.

Stated precisely

Let the region have LL positions, numbered 0 to L−1L - 1, and let IrI_r be the set of positions that run rr has written so far; IrI_r is empty when rr starts. A read of width ww at position oo in run rr is allowed only if w≤L,o≤L−w,and{o,o+1,…,o+w−1}⊆Ir,w \le L, \quad o \le L - w, \quad \text{and} \quad \{o, o + 1, \dots, o + w - 1\} \subseteq I_r, where ⊆\subseteq means that every element of the left set is in the right set. A successful write of width ww at oo adds o,o+1,…,o+w−1o, o + 1, \dots, o + w - 1 to IrI_r. The clearing rule is the special case in which the host writes all LL positions before rr starts, so that IrI_r holds every position from the start.

Exclusive use means two further conditions: if two runs overlap in time, they use different regions; and no address inside the region obtained during run rr is used after rr ends. These guarantees rest on two named assumptions: every path to the region, whether guest instructions, helpers or host code, goes through the same checks, and IrI_r is reset, or the region cleared, before each run.

Limits and common mistakes

Static storage §4.11 changes how long the memory exists, not who may see it. A static array survives between runs, and so does its content.

Clearing without exclusive use is itself a hazard: clearing the region for run B while run A is still using it destroys A’s data.

Clearing a local array at the end of a run, to remove a secret, can be undone by the compiler. A store to a local array that nothing reads afterwards is a dead store, and an optimizing compiler may delete it. Functions such as explicit_bzero, or memset_s from the optional Annex K of the C11 standard, are defined so that the store is kept. The secret zeroization §5.37 entry treats this problem in full.

Tracking must count every path and record exactly the positions written. A write that fails halfway but is recorded as complete lets a later read see bytes this run never wrote. A helper that reads scratch without checking the written set can return such bytes directly. A helper that writes into scratch without updating the written set causes the opposite error: a later read of those bytes is refused, although this run wrote them.

A large scratch array on a small thread’s stack can cause a stack overflow §4.10, growth of the stack past its end. Moving the array to static storage removes that risk but makes the region shared.

Questions answered

Is scratch cleared between invocations, and who can alias it?

Scratch memory is cleared between invocations only if the host clears it or enforces an equivalent rule; reused memory otherwise still holds the previous invocation’s bytes. Scratch memory is a fixed region that a host lends to a guest program, code running in a virtual machine, for temporary data. The host has two sound choices: write zeros to the whole region before each invocation, or track which bytes the current invocation has written and refuse reads of the others, as the Linux eBPF (extended Berkeley Packet Filter) verifier does for a program’s stack. Data that is meant to persist belongs in guest persistent state, not in scratch.

To alias the region means to hold a second address of the same bytes. The parties that can do so are the guest itself; every helper function given a pointer into the region; the host code that set it up; another invocation running at the same time, on another thread, in an interrupt handler or on another processor, if the region is shared; and any party that kept a pointer from an earlier invocation. A static region is shared by all invocations. The host must therefore give each concurrent invocation its own region, or stop a second invocation from starting while the first is running, and must forbid keeping pointers into the region after an invocation ends.

Related entries: Spatial memory safety (§2.28), Deterministic invocation state (§2.35), Guest persistent state (§2.36), Secret zeroization (§5.37), Reentrancy (§9.27), Static, stack, and heap storage (§4.11), VM stack (§2.25). Further reading: [2, 78].