Section 2.7

Instruction encoding

Definition

Instruction encoding is the rule that turns each instruction of a machine into a pattern of bits, and the bits back into the instruction. The rule divides the pattern into fields. One field selects the operation. The others hold the operands, such as register numbers, a jump distance or a constant. Reading the fields back out is called decoding. For the instruction set eBPF §2.1 (extended Berkeley Packet Filter), the specification RFC 9669 defines two encodings: a basic encoding of 64 bits and a wide encoding of 128 bits.

The idea

A stored program is a row of bytes. Nothing in the bytes marks where one instruction ends and the next begins, or which bytes are an operation and which are a number. The encoding supplies those answers. Every tool that touches the program, from the assembler that writes it to the checker that approves it and the interpreter §2.13 that runs it, must apply the encoding in the same way. If the checker and the interpreter split the bytes differently, the checker has approved a program that the interpreter never runs.

eBPF, an instruction set for small programs that run inside a host program, makes the splitting easy by using slots of equal size. A program is cut into slots of 8 bytes, so slot number kk begins at byte 8k8k, counting slots and bytes from 0. Compare a form with printed boxes to a letter in free text: in the form, each answer is found at a fixed place. There is one exception. A 64-bit constant does not fit into a slot that has only 32 bits for a constant, so the instruction that loads such a constant takes two slots. Its second slot holds data, not an instruction. That exception is the reason for the question whether a jump may land on a second slot.

How it works

RFC 9669 lays out the 8 bytes of a basic instruction as four fields.

bytes field content
0 opcode the number that selects the operation
1 regs two register numbers of 4 bits each
2 to 3 offset a signed 16-bit number
4 to 7 imm a signed 32-bit constant, called the immediate

A register is one of the machine’s numbered storage locations. The two register numbers name the destination register, which receives the result, and the source register. The offset is a jump distance, or a distance that is added to a memory address. Signed means that the number can be negative. Three rules complete the layout.

The wide encoding adds a second slot directly after the first. In the second slot the opcode §2.8, both register numbers and the offset are zero, and the last 4 bytes hold a second 32-bit constant called next_imm. In RFC 9669 only the instructions with opcode 0x18 use the wide encoding. They load a 64-bit value into a register. RFC 9669 names them 64-bit immediate instructions §2.10, and because they use the wide encoding, such an instruction is also called a wide immediate instruction. In the plain form of that instruction, imm supplies the low 32 bits of the value and next_imm the high 32 bits.

A jump states its distance in slots, counted from the slot after the jump. When a jump in slot jj with distance dd is taken, the program counter §2.9, which is the position of the next instruction, becomes slot j+1+dj + 1 + d. A wide instruction counts as two slots.

A decoder finds the instruction boundaries by walking through the program from slot 0. At each instruction it reads the opcode. If the opcode is 0x18, the instruction owns the next slot as well, and the walk continues two slots later. Otherwise the walk continues one slot later.

Worked example

The following program has 32 bytes, 4 slots and 3 instructions. The bytes are in hexadecimal for a little-endian machine.

slot bytes meaning
0 b7 00 00 00 00 00 00 00 r0 = 0
1 18 01 00 00 88 77 66 55 r1 = 0x1122334455667788, first half
2 00 00 00 00 44 33 22 11 second half
3 95 00 00 00 00 00 00 00 exit

Slot 1, field by field: 18 is the opcode for loading a 64-bit value. 01 is the register byte, with destination register 1 in the low 4 bits and 0 in the high 4 bits. 00 00 is the unused offset. 88 77 66 55 is imm, the number 0x55667788 with its lowest byte first. In slot 2 the first 4 bytes are zero, and the last 4 bytes are next_imm, the number 0x11223344. The loaded value is 𝟶𝚡𝟷𝟷𝟸𝟸𝟹𝟹𝟺𝟺×232+𝟶𝚡𝟻𝟻𝟼𝟼𝟽𝟽𝟾𝟾=𝟶𝚡𝟷𝟷𝟸𝟸𝟹𝟹𝟺𝟺𝟻𝟻𝟼𝟼𝟽𝟽𝟾𝟾\texttt{0x11223344} \times 2^{32} + \texttt{0x55667788} = \texttt{0x1122334455667788}.

The walk gives the instruction boundaries. Slot 0 starts an instruction. Slot 1 starts an instruction that also owns slot 2. Slot 3 starts an instruction. The set of starting slots is {0,1,3}\{0, 1, 3\}.

Now suppose that slot 0 held a jump with distance dd. Its target is slot 0+1+d0 + 1 + d.

Slot 2 lies inside the program, so a check that only compares the target with the length of the program accepts d=1d = 1. The check has to ask whether the target is a starting slot.

Stated precisely

Let a program consist of NN slots of 64 bits, numbered 0 to N−1N - 1. Define the set SS of starting slots by the following walk. Begin with i=0i = 0. While i<Ni < N: put ii into SS; if the opcode in slot ii is 0x18, require that i+1<Ni + 1 < N and that the opcode, register and offset fields of slot i+1i + 1 are zero, and continue with i+2i + 2; otherwise continue with i+1i + 1.

The program is well formed only if no requirement of the walk fails. A jump in slot jj with distance dd is valid only if its target t=j+1+dt = j + 1 + d is a member of SS. The condition 0≤t<N0 \le t < N is necessary and is not sufficient.

RFC 9669 states the consequence for a jump with distance 1: it skips the next instruction if that instruction is a basic one, and it “results in undefined behavior if the next instruction is a 128-bit wide instruction”. The specification gives the second slot no meaning as an instruction. Its list of opcodes names the value 0x00 only as an additional immediate value.

Limits and common mistakes

A correct encoding proves nothing about behavior. A program in which every instruction decodes cleanly can still read forbidden memory or loop for ever. Checking the encoding and the jump targets before execution, which is called structural bytecode admission §2.26, is the first check, not the last.

Bytes, slots and instructions are three different counts. The example has 32 bytes, 4 slots and 3 instructions. Jump distances count slots, so a jump over a wide instruction needs a distance of 2.

The byte order is not written in the bytes. The same program encoded for a little-endian machine and for a big-endian machine gives two different byte sequences, and the register byte differs as well.

The words “64-bit instruction” describe the size of the encoding. They say nothing about the size of the numbers that the instruction computes with.

Recognizing the opcode is not enough to accept an instruction. The unused fields must be zero, and some opcodes have several variants that are told apart by another field.

Questions answered

Can a branch target the second half of a wide instruction?

No valid program can contain such a branch, because the second half of a wide instruction is data, not an instruction, and execution cannot begin there. A program for the instruction set eBPF (extended Berkeley Packet Filter) is a row of 8-byte slots. The instruction that loads a 64-bit constant, opcode 0x18, occupies two slots, and its second slot holds only the upper half of the constant. RFC 9669, the specification of the instruction set, states the case of a jump that skips only the first slot of such an instruction and so lands on the second slot: the jump “results in undefined behavior”.

A runtime, the software that executes the program, therefore has to reject such a program before running it. The check must test more than “the target lies inside the program”, because the second slot does lie inside the program. The check must decode the whole program, record the slots at which instructions start, and accept a jump only if its target is one of those slots. The Linux verifier, the checker that examines a program before the kernel runs it, refuses such a program with the message jump into the middle of ldimm64 insn. uBPF §2.16 (userspace eBPF), a runtime for ordinary applications, refuses it with the message jump to middle of lddw. Both ldimm64 and lddw are names for the instruction that loads a 64-bit constant.

Related entries: Program counter (PC) (§2.9), Structural bytecode admission (§2.26), Opcode (§2.8), Wide immediate instruction (§2.10), Endianness (§4.21). Further reading: [1, 3, 160].