Definition
An interpreter is a program that runs another program by reading its instructions one at a time and carrying out each one, without first translating the program into the processor’s machine code. The interpreter runs on the real processor, called the host. The program it runs, called the guest, is written for another machine, often a virtual machine §2.3: a computer defined in software. The interpreter keeps the guest’s registers, program counter §2.9 and memory as ordinary data in host memory. A compiler, by contrast, translates the guest before it runs.
The idea
A program for a virtual machine, such as an eBPF §2.1 (extended Berkeley Packet Filter) program, is bytecode §2.4: numeric instructions that no hardware processor understands. A compiler translates it into processor instructions once, either just before the run, which is called just-in-time compilation §2.14 (JIT), or earlier, which is called ahead-of-time compilation §2.15. An interpreter instead works out the meaning of each instruction when it reaches it. It is small, portable and quick to start, needs no memory that is both writable and executable, and passes every guest instruction through one loop, a natural place for checks. The price is speed: each guest instruction costs several host instructions, and a simple interpreter decodes each instruction again on every pass through a loop.
How it works
The guest’s state lives in host variables: an array §14.5 of registers (eBPF has 11 of 64 bits each), the program counter, the memory regions that the guest may use and, if the host sets one, an instruction budget §2.32, the number of instructions the guest may still execute. The interpreter’s loop repeats six steps.
Charge: stop the guest if no budget is left; otherwise subtract 1.
Fetch: load the 8 bytes at the program start plus 8 times the program counter.
Decode: extract with shifts and masks the opcode §2.8, which names the operation, the register numbers, the offset and the 32-bit constant, called the immediate.
Dispatch: jump to the code for this opcode, called its handler, through an indirect jump, one whose address is read from a table: a dispatch table §2.39, or the jump table that a C compiler often builds for a
switchstatement.Execute: load the guest registers the handler needs from the host array, compute by the guest’s rules for width and wrap-around, and store the result.
Advance: add 1 to the program counter, 2 after a wide instruction, or set it to the target of a taken jump.
Some handlers do more. A memory access first checks that every byte it touches lies in a region the guest may use, unless a verifier, a checker that examines the program before it runs, has proved this. A call looks up a helper function §2.19, a host function that the guest may call, in a table of allowed helpers, passes registers R1 to R5 and puts the result in R0. An exit returns R0 as the guest’s result. A failed check, an exhausted budget or an opcode without a handler stops the guest with a fault, and the interpreter returns an error result distinct from every normal one.
Faster interpreters change the dispatch. In threaded code, each handler ends with its own indirect jump to the next handler, which gives the processor’s branch predictor more context than one shared jump; some C compilers support this through an extension called computed goto.
Worked example
A three-instruction eBPF program, with its bytes in hexadecimal for a little-endian §4.21 machine, which stores the lowest byte of a number first:
| slot | bytes | meaning |
|---|---|---|
| 0 | b7 00 00 00 02 00 00 00 |
r0 = 2 |
| 1 | 07 00 00 00 03 00 00 00 |
r0 += 3 |
| 2 | 95 00 00 00 00 00 00 00 |
exit |
Start with R0 equal to 0, program counter 0 and budget 3. Step 1: budget 2; opcode 0xb7 selects the handler that moves a constant; R0 becomes 2 and the program counter 1. Step 2: budget 1; opcode 0x07 selects the handler that adds a constant; R0 becomes and the program counter 2. Step 3: budget 0; opcode 0x95 selects the exit handler, which returns 5.
For step 2 alone, a straightforward interpreter written in C executes roughly these host instructions, though the exact count depends on the compiler and the processor: 2 to test and decrement the budget; 2 to compute the address of slot 1 and load the opcode 0x07; 2 to load the handler address from the table and jump to it; 2 to extract the destination register number 0 and the immediate 3, sign-extended to 64 bits; 3 to load R0, add 3 and store 5; and 2 to increment the program counter and jump back to the top of the loop. That is host instructions for one guest addition. A JIT compiler that keeps R0 in a processor register emits a single add.
A memory access adds a check. R1 holds the address of a 16-byte input with bytes 0 to 15, and the guest asks for 4 bytes at offset 14, which would need bytes 14 to 17. The interpreter tests offset length size, here , finds it false and stops the guest with a fault before any host load. This overflow-safe range check §4.17 avoids the sum offset size, which could wrap around for a huge offset; the subtraction is safe because the size, 4, is at most the length, 16.
Stated precisely
An interpreter is correct when three properties hold for every accepted program and every input.
Fidelity: the guest states it produces (registers, program counter and guest memory after each instruction) and its result at the exit are those that the specification of the instruction set defines, for eBPF RFC 9669.
Confinement: every host memory access made for the guest lies in a region the guest may use, and every host function called for it is allowed.
Clean faults: an instruction that would break a check is stopped before its effect, with an error result distinct from every normal result.
All three assume that the interpreter’s own code, which runs as trusted host code, is correct, and that nothing else changes the program or the guest’s memory during the run.
Cost is a sum, not a count. Let be the number of guest instructions executed, the host work for instruction number , and the work of starting and finishing the run, all in one unit. Then An instruction budget guarantees , but it bounds only if every is at most a known maximum ; then . A helper function that can wait without limit has no such maximum.
Limits and common mistakes
An interpreter is not safe by construction: its code is trusted host code. The register fields of an eBPF instruction are 4 bits wide and can name registers 0 to 15, but eBPF has 11. An interpreter that indexes its 11-entry register array with an unchecked number reads or writes past the array, so the loader must refuse numbers above 10 or the interpreter must check them. A check skipped because a verifier proved it unnecessary is only as good as that proof.
One guest instruction is not a fixed amount of time. Its cost depends on the operation, on the processor’s cache, on whether the processor predicted the dispatch jump correctly and, for a call, on the helper function. Counting guest instructions does not measure time.
A JIT compiler is faster, but it writes processor instructions while the system runs, so a bug in it can produce arbitrary native code. An interpreter avoids that risk, at the cost of speed.
Questions answered
What additional host operations are performed for one guest instruction?
Besides the operation itself, the host runs the interpreter loop around it. The host is the real processor that runs the interpreter; the guest is the program that the interpreter carries out, one instruction at a time. For one guest instruction a typical interpreter checks and charges the instruction budget, if there is one; computes the instruction’s address from the program counter and loads it (fetch); extracts the opcode, register numbers, offset and constant (decode); jumps to the opcode’s handler through a table (dispatch); loads guest registers from a host array and stores the result back; checks a memory access against the permitted regions, or a call against the allowed helper functions; advances the program counter and jumps back to the top of the loop; and stops the guest with an error result when a check fails.
In a straightforward interpreter written in C, a guest addition takes about a dozen host instructions where native code needs one. The exact number depends on the interpreter, the compiler and the processor, so it must be measured on the real implementation.
Related entries: Instruction budget (fuel) (§2.32), Interpreter semantic fidelity (§2.38), Virtual machine (VM) (§2.3), Just-in-time compilation (JIT) (§2.14), Dispatch table (§2.39), Program counter (PC) (§2.9). Further reading: [160].