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 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.
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 % 3to 2, while eBPF requires signed modulo that truncates toward zero, which gives .Choose inputs at the edges. For each width , 32 or 64 bits, take 0, 1, (the largest signed value), (the most negative signed value, read as unsigned) and (all bits set, which is 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.
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.
Compare the whole state. After each case, compare all eleven registers
r0tor10, the memory, the next instruction and the fault status, not only the result register. A 1-byte store must change exactly one byte.Run on the build that ships. Where the C code has undefined behavior, another processor or other optimization settings can give another answer.
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.
Truncation and extension §4.19. 32-bit arithmetic zeroes the upper half of the destination; ordinary loads of 1, 2 or 4 bytes fill the rest of the register with zeros, while the sign-extending loads, whose mode field is
MEMSX, copy the top bit of the loaded value into every higher bit: a loaded byte0x80becomes0x0000000000000080after an ordinary load and0xFFFFFFFFFFFFFF80after a sign-extending one; the sign-extending moveMOVSXcopies the top bit of the narrow value upward, and its 32-bit form then zeroes the upper half.Shifts. The specification keeps only the low 6 bits of a 64-bit shift count (mask
0x3F) and the low 5 bits of a 32-bit one (mask0x1F). Test counts 0, 1, , , and counts with high bits set, for every shift, including the arithmetic right shiftARSH, which fills from the left with copies of the sign bit.Division and modulo. Division by zero sets the destination to 0. Modulo by zero leaves a 64-bit destination unchanged and, in the 32-bit form, zeroes its upper half. Signed division of the most negative value by gives the most negative value, and the matching signed modulo gives 0.
Signed branches. Choose pairs on which signed and unsigned order disagree, such as and 1, and pairs of equal values, which separate from . For the 32-bit jump class
JMP32, choose registers whose lower halves compare differently from the full 64-bit values.
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 . An interpreter that evaluates the C expression x << n with a 32-bit x and 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 = (0x8000000000000000) and the immediate , the true quotient of the 64-bit signed division SDIV is , which does not fit, because the largest signed 64-bit value is ; the specification sets the result to . 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 = and the immediate 3, the signed modulo SMOD first truncates to and gives .
Signed branch. With r1 = 0xFFFFFFFFFFFFFFFF and r2 = 1, the unsigned jump-if-greater JGT is taken, because . The signed jump-if-greater JSGT is not taken, because the same bits read as signed are , and is false. With r1 = 0x00000001FFFFFFFF and the immediate 0, the 64-bit JSGT is taken, since . The 32-bit JSGT of class JMP32 compares only the lower half 0xFFFFFFFF, which is , so it is not taken. An interpreter that compares full registers for JMP32 takes the wrong branch here.
Stated precisely
Let a guest state 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 , the next state . The interpreter keeps its own representation of the state, for example a C array §14.5 of registers. Let , the abstraction function, be the guest state that represents, and let be the interpreter’s representation after it executes . The interpreter is faithful for when 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 ; a proof covers every . Exhaustive testing §10.33 is out of reach even for one 32-bit operation with two operands: its input pairs take seconds, about 585 years, at 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 cases per operation and source form, placed where results change.
The equation, and every test of it, rests on three assumptions.
Determinism. Each instruction’s result depends only on the state. Helper functions §2.19, clocks and random sources are not instructions: their outputs are fed identically to both sides, and their behavior is checked against their own contracts.
A fixed specification. RFC 9669 sorts instructions into conformance groups, such as
base32,base64anddivmul64. A fidelity claim names the groups and the version it covers, and the interpreter rejects instructions outside them.An independent oracle. The reference model was derived from the specification text, not from the interpreter’s code.
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 , and negative operands of signed modulo, which truncates toward zero. For signed branches, use pairs such as 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].