Section 2.36

Guest persistent state

Definition

Guest persistent state is data that a guest program, code a host runs inside a virtual machine §2.3 (VM), deliberately keeps from one run to the next, so that a later decision can depend on earlier events. The VM resets its registers and memory before each run, so the host holds this data outside the VM, usually in maps: key-value tables the host creates and owns, which the guest reaches only through host functions or through addresses the host hands it. Here persistent means outliving a run, not necessarily a restart.

The idea

A rate limiter §9.38 must allow at most 5 messages per second from each sender. A run that sees only the current message cannot decide: it must know how many messages the same sender has already sent in this second. That count has to live somewhere that outlasts the run, and the VM’s own registers and stack are reset before every run. So the host keeps a map, and each run reads the sender’s record, updates it and writes it back.

A notebook kept at a front desk is a fair picture. Each visitor, a run, may read and write certain pages, but only pages that the clerk, the host, opens for that visitor; the clerk decides which pages exist, how many there may be and when they are thrown away. Three things make the notebook useful and safe: someone owns it, every writer and reader follows one format, and its lifetime is known. The picture stops at the format. A reader of a notebook sees words, while a map holds bare bytes, and the same bytes read with a different format give a different meaning without any visible sign.

How it works

The host decides five things about every retained object and states them in a contract.

  1. Owner and creation. The host creates the map, typically when it loads the program, with a fixed key size, value size and maximum number of entries, and gives the program a reference to it. The guest cannot create or free maps. In eBPF §2.1 (extended Berkeley Packet Filter) systems these are BPF maps §2.18.

  2. Access. The guest reaches the map through helper functions §2.19, host functions it calls by number, such as lookup, update and delete, and in some runtimes also directly at the address of a stored value. In Linux the lookup helper returns the address of the value itself, and the program may then read and write the value in place with ordinary load and store instructions; the eBPF verifier §3.3 keeps each such access inside the value’s bytes. Linux also keeps a program’s global variables in maps that hold one value each. While loading the program, the kernel writes the address of each such variable, a position inside its map’s value, into every wide immediate instruction §2.10 (the 16-byte instruction that loads a 64-bit constant) that refers to the variable; the program then reaches its global variables with ordinary loads and stores, without calling any helper. The contract states which programs may read and which may write each map, and by which of these routes.

  3. Format. A schema states the layout and meaning of every stored field, its offset, width, byte order §4.21, unit and allowed range, together with a version number.

  4. Concurrency §9.1. If two runs can execute at the same time, for example on two processors, a read-modify-write of one record (read a count, add 1, write it back) can lose an update. The contract names the remedy: an atomic operation §9.8, which completes as one indivisible step; a lock; or a separate copy of the record per processor, combined when read. Linux offers all three for eBPF maps.

  5. Lifetime and reclamation. The contract states when the object ends and when each entry ends. In Linux a map lives as long as something refers to it: a loaded program, a file descriptor §15.21 held by a process, or a pin, a name in a special file system; reference counting §9.22 frees it when the last reference goes. Entries need their own rule, such as deletion by the program, expiry by age, or eviction of the least recently used entry when the map is full.

Persistence has three scopes, and the contract must say which apply.

A program update is where most mistakes happen, because the new program may read the same bytes with a different meaning. Three safe choices exist: keep the schema unchanged; perform a schema migration §8.36, a defined transformation of every stored record from the old format to the new one, before the new program runs; or start with empty state and accept the loss of history. A version number stored with the data lets a reader detect a mismatch, but only if the reader checks it and refuses or converts data whose version it does not expect.

Worked example

Take the rate limiter, with a record per sender of two 32-bit fields: window_start, the time in milliseconds (ms) at which the sender’s current one-second window began, and count, the number of messages accepted in that window. The rule is: if a message arrives 1,000 ms or more after window_start, start a new window at the arrival time with count set to 1 and allow; otherwise, if count is below 5, add 1 and allow; otherwise reject.

The rejection at 10,500 ms depended on five earlier runs. A host that cleared the record before every run would allow every message.

Now a new program version stores window_start in microseconds instead of milliseconds, to measure shorter windows. A stored record from the old version holds 10,000, meaning 10,000 ms. Read by the new program without migration, it means 10,000 microseconds, which is 10 ms. The window appears to have started 9,990 ms earlier than it did, so the new program treats it as long expired and lets the sender start a fresh window at once. The correct migration multiplies by 1,000: 10,000×1,000=10,000,00010{,}000 \times 1{,}000 = 10{,}000{,}000 microseconds. The migration must also check the range. A 32-bit unsigned field holds at most 232−1=4,294,967,2952^{32} - 1 = 4{,}294{,}967{,}295, which in microseconds is 4,294,967,295/1,000,000≈4,2954{,}294{,}967{,}295 / 1{,}000{,}000 \approx 4{,}295 seconds, about 71.6 minutes. A stored time later than 4,294,967 ms cannot be converted into a 32-bit microsecond field, so the new schema must widen the field to 64 bits.

Capacity belongs to the contract too. With a 4-byte sender address as key and the 8-byte record as value, 1,000 senders need 1,000×(4+8)=12,0001{,}000 \times (4 + 8) = 12{,}000 bytes of keys and values, plus the map’s own bookkeeping for each entry. If every new sender creates an entry, an attacker who sends from many addresses can fill the map. The contract must say what happens then: refuse new senders, evict the least recently used entry, or reject the message.

Stated precisely

For each retained object the contract is a list of facts that hold at all times.

The central invariant is that every reader interprets each record with the schema of the version that wrote it, or with a schema into which a migration has converted it. A program update preserves the invariant only if it keeps the schema, migrates every record before the new program reads any, or discards the old records.

Limits and common mistakes

Retained state is not durable state. A map in RAM is gone after a reset. Counts that must survive a restart must be written to storage that keeps its content without power, with a rule for which copy wins after a crash. If recovery accepts an older stored value, such as a smaller message counter, an attacker who can force a crash may undo part of the history.

A shared map is a channel. Two programs that can write and read the same map can pass data to each other, and a guest that can read another program’s map learns what that program stored. Access rights belong in the contract.

An address into a map is valid only while the entry exists. In a runtime that hands the guest the address of a value, a guest that keeps the address after the entry is deleted, by another run or by the host, reads or writes freed memory: a use-after-free §4.6.

A program without maps can still see state. Through helper results such as the time, a guest observes changing host state, so removing maps does not by itself make runs independent of history.

Testing gets harder. A run’s outcome depends on earlier runs, so a test must set up or record that history; testing runs in isolation misses bugs that need a particular sequence of events.

Questions answered

What defines the lifetime and ownership of persistent state?

The lifetime and ownership of guest persistent state are defined by an explicit contract that the host sets, not by the guest program and not by the stored bytes. Guest persistent state is data that a guest program, code a host runs inside a virtual machine (VM), keeps from one run to the next, usually in maps, the key-value tables the host owns. The contract answers these questions.

A lookup that succeeds at one moment answers none of these questions.

Related entries: BPF map (§2.18), Deterministic invocation state (§2.35), Security invariant (§1.23), Persistent state (§8.1), Schema migration (§8.36), Atomic operation (§9.8), Reference counting (§9.22), Use-after-free (§4.6).