Section 2.35

Deterministic invocation state

Definition

Deterministic invocation state is the property that every run of a guest program starts from the same defined state, whatever earlier runs did. A guest program is code that a host runs inside a virtual machine §2.3 (VM); one run is an invocation. This state covers the registers, stack, scratch memory §2.24, instruction budget §2.32, the position of the first instruction, and the host’s status and result storage. A run’s outcome then depends only on its declared inputs: the event’s data, state kept on purpose between runs, and host function results.

The idea

A host keeps one VM object and runs the same eBPF §2.1 (extended Berkeley Packet Filter) filter program on every received message. The program counts suspicious bytes in register R6 and rejects a message that has more than 2. It never sets R6 to 0 itself, because this runtime promised that every register other than the input register R1 and the stack register R10 starts at 0. Message A contains 3 suspicious bytes and is rejected, which leaves 3 in R6. If the runtime does not clear R6 before the next run, message B, with 1 suspicious byte, ends with 3+1=43 + 1 = 4 and is rejected too. A fresh VM would have counted 1 and allowed B. The same program and the same message gave different answers because of something the message did not contain.

The remedy is a contract about what a run can observe at its start, kept by the host on every run: each run behaves like the first run on a freshly built VM with the same inputs. Fresh does not require every byte to be zero; a runtime may instead track which registers and bytes the current run has written and refuse reads of the others. What matters is that nothing an earlier run wrote is readable by accident. State that must survive between runs, such as a counter in a BPF map §2.18, a key-value table the host owns, is a separate, deliberate input, described in the Guest persistent state §2.36 entry.

How it works

Define the starting state once, as a list, and have the host establish every item before every run.

  1. Position. The program counter §2.9, the position of the next instruction, is set to the program’s entry point.

  2. Registers. The input registers receive this run’s inputs: in eBPF, R1 holds the address of the guest context §2.23, the block of memory that describes the event, and R10 holds the address of the stack. Every other register is either set to a fixed value, such as 0, or marked unreadable until written. The eBPF verifier §3.3 in Linux takes the second approach: it rejects, when the program is loaded, any program that can read a register before writing it.

  3. Stack and scratch memory. The guest’s stack and its scratch memory, a temporary area lent for one run, are cleared, or their unwritten bytes are made unreadable.

  4. Budget. The instruction budget is set to its full amount, not to whatever an earlier run left.

  5. Status and result. The host’s status and result storage are set to no result yet, so that a failed run cannot leave an earlier run’s answer in place.

After the run, the host turns the status and the value into a decision through its fault-to-verdict mapping §2.34 and releases whatever it lent to the run, such as references to buffers whose lifetime ends with the event. Only then may the VM object serve the next run, which begins again at item 1.

The outcome is then a function of the declared inputs: the program, the event’s context, the contents of every map the program reads, and every value returned by a helper function §2.19, a host function the guest may call. Some helpers return changing values, such as the time or random numbers. With them, two runs on the same event can legitimately differ, and the claim becomes: the same program, context, map contents and helper results give the same outcome.

Worked example

Take a runtime that promises, before every run, a cleared 4-byte scratch area, a budget of 100 units and result storage set to no result yet. Run A writes the value 9 into scratch byte 3, uses all 100 units and returns allow, stored as the result 1. Consider the next run, B, under three hosts, each reusing one VM object.

The declared inputs decide what deterministic means. A rule that alerts when a temperature reading is above 30 gives the same answer, no alert, for the reading 28 on every run, because 28 is not greater than 30. Add a second clause that alerts for any reading taken between 02:00 and 04:00, which needs a clock helper, and the time becomes an input: the reading 28 gives no alert at 01:59 and an alert at 02:00. Both runs are deterministic functions of their full inputs; they differ only because the clock is one of those inputs.

Stated precisely

Write the starting state of a run as σ0=(p,R,K,W,b,r)\sigma_0 = (p, R, K, W, b, r): the program counter pp, the register values RR, the stack contents KK, the scratch contents WW, the budget bb and the result storage rr. The host keeps the contract when every run starts with σ0=Init(x)\sigma_0 = \mathrm{Init}(x), where xx is this run’s context and Init\mathrm{Init} is a fixed function that reads nothing an earlier run wrote. Let the outcome of a run be its status, its returned value, its writes to maps and the helper calls it made. Then two runs of the same program with the same context xx, the same map contents mm at the start and the same sequence of helper results hh have the same outcome.

The statement depends on four assumptions.

  1. Complete initialization: Init\mathrm{Init} covers every location the guest can read before writing it. Where a location is marked unreadable instead of cleared, every path that reads guest state enforces the mark.

  2. A deterministic VM: the interpreter §2.13 itself has no undefined behavior §4.12 and reads no uninitialized host memory. If the guest can read the numeric value of an address it receives, such as the stack address in R10, that address is the same on every run or counts as an input.

  3. No concurrent writers: nothing else changes the maps the run reads while the run is in progress, or the inputs are taken to be the values the run actually read.

  4. Complete inputs: every helper result that can differ between runs is counted among the inputs.

Cached VM instances

A cached VM is a VM object that the host prepares once and reuses for many runs, instead of building a new one for every event. Preparation includes checking the bytecode §2.4, by structural bytecode admission §2.26 and any verifier; decoding it into a form the interpreter runs quickly, or compiling it into machine code; allocating its stack and scratch memory; and resolving its references to maps and helpers. A cached VM moves all of that off the path that every event takes: each event pays only for setting the starting state, running the guest and mapping the result. For a short program the preparation can cost far more than the run itself.

A long-lived instance adds three risks.

Limits and common mistakes

Clearing the VM’s own state does not clear state the host keeps elsewhere. Maps, buffers inside helpers and counters inside helpers survive, by design or by mistake; each must be listed as either kept or reset.

Deterministic does not mean correct or secret. A program that gives the same wrong answer every time is deterministic. If leftover bytes came from a run on behalf of another user or another program, reading them also leaks that run’s data.

Resetting too much is also a bug. State the policy needs between runs, such as a rate counter, must be kept; a reset that clears it silently changes the policy.

Determinism of each run does not by itself make a sequence of runs reproducible. Replaying recorded events reproduces the outcomes only if the replay also restores the map contents and supplies the same helper results in the same order, which is what deterministic replay §10.37 does.

Questions answered

Which persistent state §8.1 is intentionally retained outside the VM?

Only state that the design deliberately lists as surviving from one run of the guest program to the next is retained, and the host keeps it outside the virtual machine’s per-run state. A guest program is code that a host runs inside a virtual machine (VM); its per-run state is the registers, the stack, the scratch memory, the instruction budget and the result storage, all reset before every run. In a runtime of the eBPF (extended Berkeley Packet Filter) kind, the deliberately retained state is typically the following.

For each retained object the design must state its owner, which runs may read and write it, when it is created and cleared, and how concurrent runs share it. Nothing outside that list may carry over, above all not leftover register, stack, scratch, budget or result values; if it does, that is a bug, not retained state.

Related entries: Scratch memory (§2.24), Guest persistent state (§2.36), Concrete state (§3.5), Fault-to-verdict mapping (§2.34), Instruction budget (fuel) (§2.32), Reentrancy (§9.27), Deterministic replay (§10.37).