Section 3.5

Concrete state

Definition

A concrete state is a complete snapshot of a running program at one moment: the exact value of everything that, in a chosen model of execution, determines what the program can do next. For a simple machine it consists of the position of the next instruction, the content of every register and the content of every memory byte. “Concrete” is the opposite of “abstract”: static analysis §3.1 computes with abstract states, summaries that each stand for a set of concrete states. The model, not the program, fixes the components.

The idea

Every question about what a program can do is a question about the states it passes through. “Can this load read outside its buffer?” asks whether some reachable state §3.21 has the load as its next instruction and an address outside the buffer. To answer, the state must hold everything that the answer depends on. “The program is at instruction 5” is not enough: what instruction 5 does depends on register values, on memory, and on whether the buffer it reads still exists.

A chess position is a good picture. The placement of the pieces, plus whose turn it is, determines nearly every legal next move, and anyone can continue a game from a recorded position. The picture also shows the trap. Whether a player may still castle depends on whether the king or that rook has moved earlier, which the placement does not show, so a complete chess position also records the castling rights. A program model must likewise record every hidden fact that the next step depends on.

How it works

A model of execution fixes three things.

  1. The components of a state. Typically:

    • the program counter §2.9, the position of the next instruction;

    • the registers;

    • memory, as a map from addresses to bytes;

    • which memory belongs to live objects, and with what permissions (read, write);

    • the call stack, the active function calls and the positions to return to;

    • counters that the runtime keeps, such as fuel §2.32, the number of instructions the program may still execute;

    • the environment: input buffers, persistent state §8.1 kept in storage, clocks, device registers, and anything another thread or device can change.

  2. The initial states, in which a run may begin.

  3. The step relation, which gives, for each state, the set of possible next states. If every state has exactly one next state, the model is deterministic. If an input, the result of a called function or a thread schedule can differ, a state has one next state per possibility, and the model is nondeterministic.

A run is a sequence of states, each a possible next state of the one before. The reachable states are all states that some run reaches. Dynamic analysis §3.2 observes runs. Static analysis computes summaries of the reachable states.

Worked example

A small machine is about to execute instruction 5, which loads one byte from an input object, at the offset held in register R1, into register R0. The state before the step:

The offset 2 lies in 0 to 3, so the step is allowed. The next state has program counter 6, R0 =30= 30 (the byte at position 2), R1 =2= 2, fuel 3−1=23 - 1 = 2, and the same input object.

Change one component. The input object has been freed, and its memory now holds another object with the bytes 7, 7, 7, 7. The program counter, the registers and the fuel are unchanged. A model that records which objects are live sees a read from a freed object and moves to an error state. A model that records only addresses and bytes predicts R0 =7= 7 and a normal step. That second model cannot see the use-after-free §4.6, because the component that tells the two situations apart is missing from its states.

Instruction 6 calls a helper function §2.19, a routine that the runtime provides, which can return 0 or 1. From the one state before the call, the model has two possible next states, one with R0 =0= 0 and one with R0 =1= 1. Each run takes one of them.

Even a tiny machine has many states. With 8 instruction positions, two 8-bit registers and 4 bytes of memory, there are 8×216×232=23+16+32=2518 \times 2^{16} \times 2^{32} = 2^{3 + 16 + 32} = 2^{51}, about 2.3×10152.3 \times 10^{15}, states. This count is why static analysis works with summaries.

Stated precisely

Write a concrete state as s=(p,R,M,L,f,E)s = (p, R, M, L, f, E). Here pp is the program counter, RR maps each register to its value, MM maps each address to a byte, LL is the set of address ranges of live objects with their permissions, ff is the remaining fuel, and EE is the state of the environment. A model is a set II of initial states and a step relation →\to, where s→s′s \to s' means that s′s' is a possible next state of ss. A state s′s' is reachable when there are states s0,s1,…,sk=s′s_0, s_1, \dots, s_k = s' with s0∈Is_0 \in I and si−1→sis_{i-1} \to s_i for each ii from 1 to kk.

The choice of components is adequate for a property PP when two conditions hold.

  1. The set of possible next states of every state is determined by the state alone: every outside influence is either recorded in EE or listed in the step relation as a choice that can go any way the real environment can.

  2. Whether a step violates PP can be decided from the states before and after it.

If a component that influences the next step is left out, two different real situations become one model state, and a proof about the model can miss a behavior that the real system has.

Limits and common mistakes

Concrete means concrete relative to the model. A model for checking array §14.5 bounds needs no voltages or cache contents. A model for proving that timing reveals no secret, the absence of a timing side channel §1.29, must include the cache, because cache contents change how long a load takes.

Leaving a component out is safe only when no claimed property depends on it, or when the model lets the component take every value it can really take, as an arbitrary input. Fixing the component to one assumed value, such as “the clock never wraps” or “allocation never fails”, is a hidden assumption, and a proof that relies on it fails when the assumption does.

Some properties depend on history, for example “a counter value is never accepted twice”. The state must then record the history needed, as the castling rights do in chess.

Concurrency §9.1 changes the step relation, not only the state. An interrupt handler §6.12, another core, or a direct memory access §4.36 (DMA) transfer, in which a device writes memory without the processor, can change memory between any two instructions of the program.

Questions answered

Which environmental state is omitted from your semantics?

The answer is a list: every influence on a program’s next step that the model’s concrete state, its snapshot of the running program, does not record and that the model does not treat as an arbitrary input. Making that list is the point of the question. Common omissions are:

For each item, do one of three things: add it to the state, model it as an arbitrary input limited by a stated contract, or argue why the property being proved cannot depend on it. A state that omits an influence without such an argument is not complete for that property.

Related entries: Virtual machine (VM) (§2.3), Abstract interpretation (§3.4), Reachability (§3.21), Program counter (PC) (§2.9), Instruction budget (fuel) (§2.32), Use-after-free (§4.6).