Definition
A basic block is a sequence of consecutive instructions that execution can enter only at the first instruction and leave only after the last one. No instruction after the first is the target of a jump, and only the last instruction can send control elsewhere. Once the first instruction runs, the others therefore run in order, unless an instruction faults. A basic block is normally as long as possible: it ends before the next jump target or at the next jump. Basic blocks are the nodes of most control-flow graphs §3.13.
Where it fits
Main entry: Control-flow graph (CFG) (§3.13).
A control-flow graph (CFG) records the possible transfers between basic blocks; the control-flow graph entry treats the graph and its edges in full. To split code into basic blocks, mark the leaders, the instructions that begin a block: the first instruction, every target of a jump, and every instruction directly after a jump or a return. Each block runs from one leader up to the next. The last instruction of a block, its terminator, decides the outgoing edges: a jump, a conditional jump, a return, or an ordinary instruction that falls through to the next block.
Blocks make analysis cheaper. An analysis treats a whole block as one step, whose effect is the effect of its instructions in sequence, and it needs to combine facts only at the start of a block, where several edges can arrive. Compilers use blocks the same way, for example to reorder instructions within a block.
Calls and faults are handled in two ways. Many compilers keep a call inside a block, because execution normally continues after it; an analysis that follows the called function, or an exception the call can raise, ends the block at the call. Instructions that can fault normally stay inside a block, with the fault treated as an extra edge out of the block. The rule that all instructions run once the first one runs therefore holds only when no fault occurs.
The leader rule assumes that the instruction boundaries are already known. For machine code, a decoder must first find where each instruction starts, and the instruction set decides which starting points are legal.
Questions answered
Can execution legally enter the middle of a decoded instruction sequence?
Execution can enter the middle of a sequence of whole instructions, but whether it can enter the middle of a single instruction depends on the instruction set and on any checker’s rules.
At an instruction boundary: yes. Any instruction can be the target of a jump. If a jump targets an instruction in the middle of a straight-line sequence, that instruction starts a new basic block, and the sequence is split there. A correct block builder therefore finds every jump target before it forms blocks. A jump whose target is computed at run time can make many instructions possible targets.
Inside one instruction: it depends. eBPF §2.1 (extended Berkeley Packet Filter), an instruction set whose programs an operating-system kernel checks before running them, has a wide immediate instruction §2.10 that fills two 8-byte slots. The second slot holds the upper half of a 64-bit constant, not an instruction, and the Linux eBPF verifier §3.3 rejects any program with a jump to that slot. Processors with fixed-length instructions, such as 64-bit Arm with its 4-byte instructions, raise a fault on an attempt to execute from an address that is not a multiple of 4. On x86, whose instructions are 1 to 15 bytes long, a jump may land on any byte, and the processor decodes the bytes from that point as different instructions. Attackers use this to find unintended instruction sequences, and it is why a CFG for x86 machine code must be built from the actual jump targets, not from one reading of the bytes from the start.
The instruction set decides which entry points are legal. A basic block only records them: calling something a block cannot make the inside of an instruction a legal place to start.
Related entries: Instruction encoding (§2.7), Wide immediate instruction (§2.10), eBPF verifier (§3.3).