Definition
Just-in-time compilation (JIT) is a way of running a program written for a virtual machine §2.3. On the machine that runs the program, after the program has been loaded, a component called a JIT compiler translates the program’s instructions into native code, the instructions of the real processor, and the processor runs that code. An interpreter §2.13 never translates; it carries out one virtual-machine instruction at a time. Ahead-of-time compilation §2.15 translates before the program reaches the machine. The native code must do exactly what the virtual machine’s specification says the program does.
The idea
A program for a virtual machine arrives as bytecode §2.4: numbered instructions that no hardware processor runs directly. Call that program the guest, and call the real processor and the software that runs the guest the host. An interpreter fetches and decodes every guest instruction each time it reaches it and then jumps to the host code for that operation, so one guest instruction costs several host instructions. A packet filter, a small program that decides what happens to each network packet, repeats that work millions of times. A JIT compiler decodes once. It writes, for each guest instruction, the host instructions that perform it, and every later run is a plain function call into that native code.
The price is trust. Load-time checks, such as a verifier that inspects bytecode before accepting it, examine the guest instructions. The processor, however, runs the bytes that the JIT compiler wrote, and nothing checks those bytes while they run. A mistranslated instruction, or a dropped check that the safety argument counted on, makes the native code do something the bytecode never asked for, with the full privileges of the host. The JIT compiler therefore belongs to the trusted computing base §1.4: the set of components that must be correct for the system to be secure.
How it works
The steps below follow the JIT compilers in the Linux kernel for eBPF §2.1 (extended Berkeley Packet Filter), a virtual machine with eleven 64-bit registers, r0 to r10. Linux translates each program once, when it is loaded.
Admit. The eBPF verifier §3.3, the kernel’s load-time checker, accepts the program first. The compiler relies on facts the verifier established, such as that every jump lands inside the program and that every constant amount of a 64-bit shift lies between 0 and 63.
Assign registers. Each eBPF register gets a fixed place in the host. An x86-64 processor has sixteen 64-bit registers, so each eBPF register gets one of its own. On 32-bit Arm, each 64-bit eBPF register needs two 32-bit host registers, one per half, or a slot in memory.
Emit code. For each guest instruction the compiler writes host instructions with the same effect, often several: a 64-bit addition on 32-bit Arm adds the lower halves and then adds the upper halves with the carry. A prologue sets up a stack frame, the block of stack memory for one function call, and saves the registers that the host’s calling rules oblige a function to preserve; an epilogue restores them and returns
r0as the result.Fix jump distances. A guest jump counts instructions and a native jump counts bytes. On x86-64 a nearby target allows a shorter jump encoding, so code length and jump distances depend on each other; the Linux x86-64 compiler repeats the translation until the length stops changing.
Translate calls. A call to a helper function §2.19, a host function that the program may invoke, becomes a native call that follows the host’s calling rules.
Publish. The compiler writes the code, then makes the memory read-only and executable before the first run, so the code is never writable and executable at the same time. Where the instruction cache does not track writes to memory, the processor must also be made to fetch the new bytes (see the executable-code publication §19.5.3 entry).
Run. Each run is an ordinary function call into the native code.
Where untrusted users may load programs, the compiler also blinds constants. Without blinding, a constant chosen by an attacker appears unchanged in executable memory, and its bytes, decoded from a different starting position, can form machine instructions; an attacker who exploits a separate bug to divert execution into the middle of the code then runs them. This attack is called JIT spraying. Blinding stores each constant combined with a fresh random value by exclusive or (XOR), written , and adds one instruction that applies the XOR again at run time, since . Linux enables blinding with the setting net.core.bpf_jit_harden.
Worked example
When translation pays. With invented costs, interpreting one run takes 10 microseconds, running the native code takes 4 microseconds, and translating takes 120 microseconds once. For runs, interpretation costs microseconds and JIT costs . They are equal when , that is, when and ; both then cost 200 microseconds. At 21 runs the totals are 210 and 204 microseconds, and at 1,000 runs 10,000 and 4,120. A program that runs only a few times is cheaper to interpret.
A translation bug. The Linux eBPF compiler for 32-bit Arm kept each 64-bit register in two 32-bit host registers. Let r1 hold 0x0000000500000007: upper half 5, lower half 7, value . The guest instruction shifts r1 right by the constant 0, which by the eBPF specification changes nothing. The compiler emitted Arm shift instructions with the amount 0, but a 32-bit Arm right-shift instruction encoded with the amount 0 shifts by 32, which empties the register. The native code set r1 to 0 instead of 21,474,836,487. No check at run time could notice, because every check had been applied to the bytecode, and no test that avoids a shift by exactly 0 reveals the bug.
Stated precisely
Let be a guest program that passed the load-time checks and its native code. Let an input consist of the program’s input bytes, the contents of the memory it may use, and the values that helper functions will return. Let be the observable behavior that the specification gives on : the final value of r0, every helper call with its arguments, in order, and every write to memory outside the program’s own stack. Let be the same record for the native code. The compiler is correct when, for every such and , and the native code touches no memory except memory that may access and its own stack frame. Where the specification leaves a choice open, equality weakens to refinement: every behavior of the native code must be one that the specification allows.
Four assumptions carry the statement: every fact the compiler takes from the checker really holds for ; the processor behaves as its documented instruction set says; every helper keeps its contract; and nothing modifies the native code after publication.
Verifying a JIT compiler: Jitterbug
Luke Nelson, Jacob Van Geffen, Emina Torlak and Xi Wang (2020) built Jitterbug, a framework for specifying and verifying the eBPF JIT compilers of the Linux kernel [284]. Its correctness specification is stepwise. The prologue must produce a host state that corresponds to the initial eBPF state. The native code emitted for each eBPF instruction must take any host state that corresponds to the eBPF state before the instruction to one that corresponds to the eBPF state after it. The epilogue must return the correct result. The paper proves that these conditions imply the correctness of whole translated programs, so an automated tool can check one instruction at a time.
With Jitterbug the authors built and verified a new eBPF JIT compiler for 32-bit RISC-V, an open processor instruction set whose name refers to reduced instruction set computer (RISC) designs. They also found and fixed 16 previously unknown bugs in five JIT compilers deployed in Linux; the shift-by-zero bug above is one of them. Their class of bug, a JIT compiler producing erroneous machine instructions, cannot arise in a runtime that only interprets: the interpreter’s machine code is compiled once from its source and is the same for every guest program, so no register assignment, jump distance or encoding is computed per program. An interpreter can still implement an instruction wrongly, the subject of the Interpreter semantic fidelity §2.38 entry, and it is only as correct as the compiler that built it.
Limits and common mistakes
A safe interpreter does not make a safe JIT compiler. Each code path must match the specification separately, and a runtime’s safety features may cover only one path: in the uBPF §2.16 (userspace eBPF) library, the instruction limit and the memory bounds checks apply to the interpreter only.
JIT needs memory that is first written and then executed. On a microcontroller §6.1, RAM is scarce, and the firmware may forbid execution from RAM altogether, for example with a memory protection unit §6.8 (MPU), hardware that enforces access rules on regions of memory.
Neither path is safer in every respect. A JIT compiler enlarges the trusted computing base and invites JIT spraying. An interpreter is code an attacker can borrow: a 2018 proof-of-concept attack of the kind called Spectre variant 2 [252], which misleads branch prediction so that the processor speculatively runs code the attacker picked, used the Linux eBPF interpreter [253]. Linux then added the build option CONFIG_BPF_JIT_ALWAYS_ON, which leaves the interpreter out.
Just in time does not mean at the instant an instruction is reached. Linux translates a whole eBPF program when it is loaded, while a Java virtual machine such as HotSpot compiles a method only after interpreting it often enough.
Questions answered
Which checks must remain true after translation and optimization?
Every property that the safety argument established for the bytecode must still hold for the native code, because the processor runs only the native code. A just-in-time (JIT) compiler and its optimizations may change how a result is computed, never what is computed. In particular:
Arithmetic: each operation keeps the width, sign rules and edge cases of the specification, such as clearing the upper 32 bits after a 32-bit operation.
Control flow: every native jump lands at the start of the native code for the intended guest instruction.
Memory: every run-time bounds check that the argument relies on is emitted, runs before the access, covers its full width, and cannot be bypassed by any native path.
Calls: only permitted helper functions are called, with their arguments where the host expects them.
Limits and faults: an instruction budget §2.32 the argument relies on is still counted, and every fault ends the run with the outcome the specification gives.
Code integrity §1.9: the native code is never writable while it is executable.
A check that covers only the start of an access is the typical mistake. Take an 8-byte input with valid offsets 0 to 7 and a 4-byte load at offset 5. The load needs bytes 5 to 8, and , so it must be refused. Native code that tests only accepts it and reads byte 8, outside the input. An optimization may delete a check only where an invariant proves that the check passes for that exact access on every path that reaches it.
Related entries: Refinement (§3.32), Translation validation (§3.33), Software fault isolation (SFI) (§2.41), Compiler correctness (§3.40), Interpreter (§2.13), Ahead-of-time compilation (AOT) (§2.15), Interpreter semantic fidelity (§2.38), Trusted computing base (TCB) (§1.4). Further reading: [1, 72, 252, 253, 284].