Section 2.38

Interpreter semantic fidelity

Definition

Interpreter semantic fidelity is the property that an interpreter §2.13, a program that executes another program’s instructions one at a time, changes state exactly as the instruction set specification says, for every accepted instruction and every starting state. State means the registers, the memory, the position of the next instruction and whether execution has stopped with a fault. Fidelity is judged against the written specification, such as RFC 9669 for eBPF §2.1 (extended Berkeley Packet Filter) bytecode §2.4, not against another implementation. Tests give evidence of it; only a proof covers every input.

The idea

A 32-bit add instruction in eBPF adds the lower halves of two 64-bit registers, keeps the lower 32 bits of the sum and sets the upper 32 bits of the destination register to zero. Take a register holding 0x00000000FFFFFFFF and add 1. The specification gives 0. An interpreter that performs one 64-bit addition gives 0x0000000100000000, which is 4,294,967,296. A later comparison with zero now goes the other way. Worse, a verifier, a program that proves bytecode safe before it runs, may have concluded from the specification that the register holds 0 and accepted a memory access that uses it as an offset without a run-time check. The interpreter then uses an offset 4 GiB larger than the one that was proved safe. A proof about the specified machine says nothing about a machine that computes differently.

Machine arithmetic differs from school arithmetic only at a few edges: a result too wide for its register, a value read as signed or as unsigned, a shift by the full width or more, and division by zero. Interpreters go wrong at exactly these edges, and C, the language they are usually written in, makes that easy. In C, shifting by at least the width of the type, overflowing a signed integer, dividing by zero and dividing the most negative 64-bit value by −1-1 are undefined behavior §4.12: the language promises nothing, so the compiled interpreter may do anything. The eBPF specification defines a result for each of these cases, so a faithful interpreter writes out every guest rule explicitly instead of relying on what a C operator happens to do.

How it works

Testing for fidelity takes six steps for every instruction.

  1. Write a reference model. The test oracle §10.5, the part of a test that decides the right answer, is a second, deliberately simple implementation written directly from the specification text. In Python §12.23, whose integers are unbounded, every result is cut to its width explicitly, for example with & 0xFFFFFFFF. Each rule is checked against the text, because the model’s own operators can be wrong too: Python evaluates -13 % 3 to 2, while eBPF requires signed modulo that truncates toward zero, which gives −1-1.

  2. Choose inputs at the edges. For each width ww, 32 or 64 bits, take 0, 1, 2w−1−12^{w-1}-1 (the largest signed value), 2w−12^{w-1} (the most negative signed value, read as unsigned) and 2w−12^{w}-1 (all bits set, which is −1-1 when read as signed). For a 32-bit instruction, also take inputs whose upper 32 bits are not zero: the instruction must ignore them and must clear them in its result.

  3. Cover both source forms. Most eBPF instructions take their second operand either from a register or from a 32-bit immediate, a constant stored in the instruction itself. A 64-bit instruction first sign-extends the immediate to 64 bits, so the two forms exercise different code.

  4. Compare the whole state. After each case, compare all eleven registers r0 to r10, the memory, the next instruction and the fault status, not only the result register. A 1-byte store must change exactly one byte.

  5. Run on the build that ships. Where the C code has undefined behavior, another processor or other optimization settings can give another answer.

  6. Add breadth. Random inputs, from property-based testing §10.7 or fuzzing §10.9, and differential testing §10.6 against an independent implementation find cases the edge list missed. Every disagreement is settled by the specification, never by majority vote.

The edges differ by instruction class.

Worked example

Truncation. Start with r1 = 0x00000001FFFFFFFF and execute a 32-bit add of the immediate 1. The lower half 0xFFFFFFFF plus 1 is 0x100000000; its lower 32 bits are 0, and the upper half is cleared, so the specified result is r1 = 0. An interpreter that adds in 64 bits and forgets to truncate gives 0x0000000200000000. An interpreter that truncates the sum but keeps the old upper half gives 0x0000000100000000. The second bug is invisible with the input 0x00000000FFFFFFFF, whose upper half is already zero; that is why the edge list includes inputs with a nonzero upper half.

Shift. Start with r1 = 1 and r2 = 33 and execute a 32-bit left shift of r1 by r2. The specification masks the count: 33 is binary 100001, and keeping the low 5 bits leaves 00001, which is 1. The result is 1×21=21 \times 2^{1} = 2. An interpreter that evaluates the C expression x << n with a 32-bit x and n=33n = 33 has undefined behavior. When the compiler simply emits the processor’s shift instruction, the answer depends on the processor: the x86 shift instruction uses the low 5 bits of the count and gives 2, while the register shift of an Arm Cortex-M processor uses the low 8 bits, so a count of 33 shifts every bit out and gives 0. Tests run only on an x86 workstation pass, and the build for the microcontroller §6.1 is wrong.

Division. With r1 = 7 and r2 = 0, the 64-bit unsigned division DIV sets r1 to 0, and the 64-bit modulo MOD leaves r1 = 7. With r1 = −263-2^{63} (0x8000000000000000) and the immediate −1-1, the true quotient of the 64-bit signed division SDIV is 2632^{63}, which does not fit, because the largest signed 64-bit value is 263−12^{63}-1; the specification sets the result to −263-2^{63}. The C expression INT64_MIN / -1 is undefined, and on an x86-64 processor the division instruction raises a divide error that stops the interpreter’s process. With r1 = −13-13 and the immediate 3, the signed modulo SMOD first truncates −13/3≈−4.33-13 / 3 \approx -4.33 to −4-4 and gives −13−3×(−4)=−1-13 - 3 \times (-4) = -1.

Signed branch. With r1 = 0xFFFFFFFFFFFFFFFF and r2 = 1, the unsigned jump-if-greater JGT is taken, because 18,446,744,073,709,551,615>118{,}446{,}744{,}073{,}709{,}551{,}615 > 1. The signed jump-if-greater JSGT is not taken, because the same bits read as signed are −1-1, and −1>1-1 > 1 is false. With r1 = 0x00000001FFFFFFFF and the immediate 0, the 64-bit JSGT is taken, since 8,589,934,591>08{,}589{,}934{,}591 > 0. The 32-bit JSGT of class JMP32 compares only the lower half 0xFFFFFFFF, which is −1-1, so it is not taken. An interpreter that compares full registers for JMP32 takes the wrong branch here.

Stated precisely

Let a guest state ss consist of the eleven 64-bit registers, the memory the program can reach, the index of the next instruction and a fault flag. The specification defines, for each instruction ii, the next state speci(s)\mathrm{spec}_i(s). The interpreter keeps its own representation cc of the state, for example a C array §14.5 of registers. Let α(c)\alpha(c), the abstraction function, be the guest state that cc represents, and let impli(c)\mathrm{impl}_i(c) be the interpreter’s representation after it executes ii. The interpreter is faithful for ii when α(impli(c))=speci(α(c))for every representation c the interpreter can reach.\alpha(\mathrm{impl}_i(c)) = \mathrm{spec}_i(\alpha(c)) \quad \text{for every representation } c \text{ the interpreter can reach.} Faithfulness for every instruction gives, by induction on the number of steps, faithfulness for every complete run. This step-by-step correspondence is a form of refinement §3.32.

A test evaluates both sides at one chosen cc; a proof covers every cc. Exhaustive testing §10.33 is out of reach even for one 32-bit operation with two operands: its 232×232=264≈1.84×10192^{32} \times 2^{32} = 2^{64} \approx 1.84 \times 10^{19} input pairs take 1.84×10101.84 \times 10^{10} seconds, about 585 years, at 10910^{9} tests per second. The edge list instead gives 8 values per 32-bit operand, the five edge values and three with a nonzero upper half, so 8×8=648 \times 8 = 64 cases per operation and source form, placed where results change.

The equation, and every test of it, rests on three assumptions.

Limits and common mistakes

Agreement is not correctness, and passing tests is not proof. Two implementations written from the same misreading agree on every test, a common-mode error §19.2.13, and an untested input, such as an instruction whose source and destination are the same register, can still fail.

Undefined behavior can be latent: the interpreter works with today’s compiler and breaks under tomorrow’s optimizer. UndefinedBehaviorSanitizer §10.23, a run-time checker that compilers build into a program on request, reports out-of-range shifts, signed overflow §4.15 and division by zero as they happen, even when the result happens to be right.

For encodings the specification does not define, fidelity means that the loader or the interpreter refuses them, not that it does something plausible.

Fidelity is not safety. A faithful interpreter executes an out-of-bounds load exactly as written; preventing that load is the job of a verifier or a runtime bounds check §2.27.

Questions answered

How do you test truncation, shifts, division, and signed branches across widths?

Test them against a reference model written directly from the instruction set specification, with inputs chosen at the edges where width and sign change the result, and compare the whole machine state after each instruction. For truncation, run every 32-bit instruction on inputs whose upper 32 bits are not zero and check that the result’s upper half is cleared; check that a 1-, 2- or 4-byte load or store touches exactly that many bytes, and load a value whose top bit is set, such as the byte 0x80, to check that an ordinary load fills the rest of the register with zeros and a sign-extending load fills it with copies of the top bit. For shifts, use counts 0, 1, width minus 1, width, width plus 1 and counts with high bits set; eBPF (extended Berkeley Packet Filter) masks the count to its low 5 bits for 32-bit shifts and its low 6 bits for 64-bit shifts. For division and modulo, include a divisor of zero, the most negative value divided by −1-1, and negative operands of signed modulo, which truncates toward zero. For signed branches, use pairs such as −1-1 and 1, on which signed and unsigned order disagree, pairs of equal values, and 32-bit jumps whose lower halves compare differently from the full registers.

Run each case with the second operand taken from a register and from an immediate constant, run the tests on the build that ships, because an interpreter written in C can inherit the processor’s behavior where C leaves a result undefined, and settle every disagreement by reading the specification.

Related entries: Differential semantics (§3.34), Refinement (§3.32), Undefined behavior (UB) (§4.12), ALU32 versus ALU64 (§2.11), Signed versus unsigned comparison (§2.12), Truncation and extension (§4.19), Shift semantics (§4.20), Differential testing (§10.6), Semantic oracle independence and common-mode errors (§19.2.13). Further reading: [1, 160].