Section 2.9

Program counter (PC)

Definition

The program counter (PC) is the part of a machine’s state that holds the position of the next instruction to execute. An ordinary instruction moves it to the following instruction, a jump or a call sets it to another position, and an exit instruction ends the run or returns from a call. In a processor the PC is a register that holds a memory address. In an interpreter §2.13, software that carries out a program’s instructions one at a time, the PC is often an integer that numbers the instructions.

The idea

After each instruction a machine must know where to continue, and the PC holds that answer. Take an interpreter that keeps the PC as a slot number and runs programs for eBPF §2.1 (extended Berkeley Packet Filter), an instruction set whose programs are rows of 8-byte slots. In a program of 5 slots the valid numbers are 0 to 4. After an ordinary instruction in slot 4 the next PC is 5, the length of the program, where no instruction is stored; an interpreter that fetches there runs whatever 8 bytes follow the program.

So a valid current position does not make the next one valid. Arithmetic adds a second trap: a PC kept in an integer of fixed size can overflow and wrap around to a small number that looks valid.

How it works

Let NN be the number of slots, numbered 0 to N−1N - 1, and pp the current PC. For every instruction an interpreter fetches the instruction at slot pp, computes the next PC by the rules below, and checks the next PC before the next fetch, unless a check of the whole program before the run has ruled out every bad value. A fetch is safe only at an instruction start, a slot at which an instruction begins. Every eBPF instruction fills 1 slot, except the wide immediate instruction §2.10, opcode §2.8 0x18, which fills 2; its second slot holds data.

The next PC depends on the instruction at slot pp.

RFC 9669, the specification of eBPF, stores dd in a signed 16-bit field, or in a signed 32-bit field for one form of the unconditional jump and for the call. With a 32-bit distance, the next PC t=p+1+dt = p + 1 + d can reach (N−1)+1+(231−1)=N+231−1(N - 1) + 1 + (2^{31} - 1) = N + 2^{31} - 1, beyond 231−12^{31} - 1, the largest signed 32-bit value. So tt must be computed in a wider type, such as a signed 64-bit integer, and tested at both ends: 0≤t0 \le t and t<Nt < N.

RFC 9669 has no jump to an address computed at run time. A loader, the code that accepts a program before it runs, can therefore list every possible next PC in advance. The instructions, linked to their possible next PCs, form the control-flow graph §3.13 of the program. The loader’s rule is that every possible next PC is an instruction start. It follows that the last instruction must be an exit or an unconditional jump.

Worked example

Take a program of N=5N = 5 slots, each holding one single-slot instruction:

slot instruction
0 r0 = 0
1 if r1 > 10 goto +2
2 r0 = 1
3 exit
4 r0 += 5

The jump in slot 1 has distance d=2d = 2, so its target is 1+1+2=41 + 1 + 2 = 4, not 3: the distance counts from slot 2.

If register r1 holds more than 10, the run visits slots 0, 1 and 4. Slot 4 is not an exit, so the next PC is 4+1=5=N4 + 1 = 5 = N, and an unchecked interpreter fetches bytes 40 to 47, past the 40-byte program. With r1 at 10 or less the run takes slots 0 to 3 and exits, so tests with small inputs never show the fault; the loader must refuse the program. With d=−4d = -4 instead, the target is 1+1−4=−21 + 1 - 4 = -2: a signed comparison §2.12 t<Nt < N alone accepts it, because −2<5-2 < 5, and only the test 0≤t0 \le t rejects it.

Now let the interpreter keep the PC in an unsigned 16-bit variable, which holds 0 to 65,535, and let slot 1 hold a 32-bit unconditional jump with d=65,534d = 65{,}534. The true target is 1+1+65,534=65,5361 + 1 + 65{,}534 = 65{,}536. The variable keeps only the remainder after division by 216=65,5362^{16} = 65{,}536, which is 0, so a check on the stored value sees 0<50 < 5 and accepts a jump to slot 0. The check was right about the number it saw, but that number is not the encoded target. In a signed 64-bit integer the target stays 65,536 and fails t<5t < 5.

Stated precisely

Let SS be the set of instruction starts, found by decoding the program in order from slot 0; the instruction encoding §2.7 entry gives this walk. For the instruction at a slot ii in SS, let ℓ(i)\ell(i) be its length in slots and dd its stored distance. Its successors, the possible next PCs, are: none for an exit; i+1+di + 1 + d for an unconditional jump; i+1i + 1 and i+1+di + 1 + d for a conditional jump; i+1+di + 1 + d, and i+1i + 1 as the return position, for a call to a function inside the program; and i+ℓ(i)i + \ell(i) for any other instruction.

Condition: every successor tt of every instruction in SS, computed as an exact integer, is in SS; this implies 0≤t<N0 \le t < N. Guarantee: if the condition holds and N≥1N \ge 1, every value of the PC during a run is in SS, so every fetch reads a whole instruction inside the program; this follows by induction over the steps, starting from slot 0. The guarantee assumes that the program bytes do not change between check and run, that the interpreter computes successors by the same formulas without wrap-around, and that each exit from a function returns to the position saved by the latest unfinished call.

Without the condition, the instruction at slot ii sets the next PC to NN in exactly two cases: it is the last instruction, i+ℓ(i)=Ni + \ell(i) = N, and is neither an exit nor an unconditional jump; or it is a jump or a call with i+1+d=Ni + 1 + d = N.

Limits and common mistakes

A valid PC does not make the run end. With d=−2d = -2 in slot 1 the target 1+1−2=01 + 1 - 2 = 0 is a valid instruction start, yet with r1 above 10 the program loops for ever. Ending the run needs another rule, such as an instruction budget §2.32, a limit on the number of instructions executed.

A range check is not an instruction-start check. The second slot of a wide immediate instruction lies between 0 and N−1N - 1, yet no instruction starts there. An interpreter that tests only t<Nt < N before each fetch leaves both this case and negative targets open.

Reference points differ between machines. eBPF counts jumps in 8-byte slots from the slot after the jump; an x86-64 processor counts a PC-relative address from the next instruction’s address; an Arm processor in the 32-bit A32 state reads the PC as the current instruction’s address plus 8. Mixing conventions gives wrong targets.

Questions answered

Can the next PC overflow or equal the end after a non-exit instruction?

Yes, both can happen unless the loader or the interpreter rules them out. The program counter (PC) holds the position of the next instruction. In a program of NN instruction slots, numbered 0 to N−1N - 1, an instruction in the last slot that is neither an exit nor an unconditional jump sets the next PC to NN, one past the end, where no instruction is stored; so does a jump whose target, its own slot plus 1 plus its distance, equals NN. The remedy is a check before the run that every possible next PC is an instruction start; it forces the last instruction to be an exit or an unconditional jump. The Linux eBPF verifier §3.3, the Linux kernel’s checker for eBPF programs, refuses a last instruction of any other kind.

The next PC can also overflow. Stored in an integer too narrow for the sum, it wraps around: in an unsigned 16-bit variable a true target of 65,536 becomes 0, which a range check accepts. The safe procedure computes the target tt in a signed 64-bit integer, requires 0≤t<N0 \le t < N, and requires that tt is an instruction start, not the second slot of a wide instruction.

Related entries: Instruction encoding (§2.7), Control-flow graph (CFG) (§3.13), Wide immediate instruction (§2.10), Signed versus unsigned comparison (§2.12), Instruction budget (fuel) (§2.32). Further reading: [160].