Definition
A wide immediate instruction is an instruction encoded in two slots to make room for a wider operand. Its opcode §2.8 selects this size even when the value is small. In eBPF §2.1 (extended Berkeley Packet Filter) an instruction normally fills one 8-byte slot with room for a 32-bit immediate. The only wide instructions have opcode 0x18 and load a 64-bit value into a register. In the form that loads a constant, the first slot holds its low 32 bits and the second slot, which is data and not an instruction, its high 32 bits.
Where it fits
Main entry: Instruction encoding (§2.7).
The instruction encoding entry lays out both slots and decodes a full example; this term adds the status of the second slot. RFC 9669, the specification of eBPF, requires the first 4 bytes of the second slot, where an ordinary instruction keeps its opcode, register numbers and offset, to be zero. The last 4 bytes of the second slot hold a second constant, called next_imm, which in the form that loads a constant is the high half of the value. Read as an instruction, the second slot therefore has opcode 0x00, which RFC 9669 lists only as the additional immediate value of a wide instruction. Tools name the instruction lddw (load double word) or ldimm64.
A second slot is orphaned in three ways: missing, when opcode 0x18 stands in the last slot, so the second slot would come from whatever bytes follow the program; unowned, when the second-slot pattern stands where an instruction starts; and entered, when a jump or a call lands on a genuine second slot. RFC 9669 states that a jump which skips only the first slot of a wide instruction “results in undefined behavior”.
Limits and common mistakes
The zero bytes are a second line of defense. Without them, a second slot could begin with any opcode, for example 0x95, exit: a listing of the program would show only a load of a constant, while a jump into the second slot would run the hidden exit. With the zeros, even a faulty check of jump targets cannot run a hidden instruction.
Questions answered
How should a loader reject an orphaned second slot?
A loader should decode the program once from its first slot, mark each slot as an instruction start or as the second slot of a wide immediate instruction, and refuse the whole program if any of four rules fails. In eBPF (extended Berkeley Packet Filter) that instruction has opcode 0x18 and fills two 8-byte slots. In the constant-load form the two slots together encode a 64-bit constant. Other forms use the immediate fields to identify a map or other platform data. A second slot is orphaned when it is missing, when it stands where an instruction should start, or when a jump or call reaches it directly. Let be the number of slots.
Every instruction start holds an opcode that the runtime, the software that runs the program, implements. A second-slot pattern there has opcode
0x00, which is no instruction, and is refused.A slot that holds
0x18has a following slot: .The first 4 bytes of slot are zero; the loader marks slot as a second slot and continues at slot .
Every jump or call target, the jump’s slot plus 1 plus its distance, is an instruction start, so a target on a second slot is refused although it lies inside the program.
The loader refuses the program; it never repairs the program or skips the slot. The interpreter §2.13, and any compiler that translates the program, must use the same marking of slots.
Related entries: Instruction encoding (§2.7), Opcode (§2.8), Program counter (PC) (§2.9), Structural bytecode admission (§2.26). Further reading: [1, 160].