Definition
A dispatch table is an array §14.5 that maps a small integer, called the selector, to the code that handles it. The selector is typically an opcode §2.8, the number that names an instruction, or the number of a helper function §2.19. Dispatch means reading the entry at the selector and transferring control to it, by calling a function pointer or jumping to a code address. When the selector comes from untrusted input, the lookup must refuse out-of-range selectors before reading the table, and must define what happens for slots without a handler.
The idea
An interpreter §2.13 runs a loop: fetch the next instruction, find the code for its opcode, run that code. Finding the code with a chain of comparisons, first against opcode 0, then 1, and so on, costs up to one comparison per defined opcode for every instruction executed. A table indexed by the opcode costs one memory read and one indirect jump, a jump to an address held in data, whatever the opcode is. A C compiler turns a dense switch statement into such a table on its own. The same pattern selects a helper function from the number in a call instruction.
The table turns a number into a code address, and that is its risk. If an attacker chooses the number and nothing checks it, the attacker chooses which word of memory the processor treats as a code address. The result can be an immediate crash or a call into code the attacker picked. A dispatch table is safe only when every possible selector value has a defined outcome: either a call to a valid handler that this program may use, or a refusal.
How it works
Read the selector at its declared width and sign. In eBPF §2.1 (extended Berkeley Packet Filter) the opcode is an 8-bit unsigned field, so it takes the values 0 to 255. A helper number sits in the 32-bit immediate field of the call instruction, which the instruction format declares as a signed integer.
Check the range before reading the table. A table of slots has the valid indices 0 to , so the condition for selector is . Check the same value, at the same width, that will be used as the index. A table with one slot for every value the selector can take, such as 256 slots for an 8-bit unsigned opcode, needs no range check.
Handle holes. A hole is a slot inside the range that has no handler. One design stores a null pointer §4.26, a pointer value that refers to nothing, and tests for it before every call. The other fills every hole with a refusal handler that only reports an invalid instruction, so every slot is safe to call and the fast path needs no test. The Linux kernel’s eBPF interpreter uses the second design: its 256-slot table starts with every slot pointing at code that reports an unknown opcode and stops, and only the defined opcodes are then filled in.
Check permission. A populated slot means that the handler exists, not that this program may call it. A helper allowlist §2.20 decides that separately.
Call through one function type. All handlers share one function type: the same parameter types and the same return type. In C, calling a function through a pointer of a different type is undefined behavior §4.12, so a cast that makes the table compile does not make the call valid.
Keep the table constant. A table declared
constcan be placed in read-only memory, such as flash memory §6.6 on a microcontroller §6.1, so a memory-corruption bug elsewhere cannot overwrite a handler address. A table that changes at run time, for example when handlers are registered, needs a rule that keeps a handler in place while a call to it runs.
A loader can refuse a program that names a hole or an out-of-range helper once, before the program runs, which removes the check from every call. That is sound only if the table cannot change after loading.
Worked example
A 32-bit microcontroller has 4-byte pointers. A helper table T of slots starts at address 0x20000100 and occupies bytes, up to 0x20000113. This example reserves helper number 0 to mean no helper, so slot 0 is empty; slots 1, 2 and 4 hold handlers; slot 3 is a hole. Slot lies at address .
: holds, the slot at
0x20000110holds a handler, and the call proceeds if the program may use helper 4., where a header defines
HELPER_MAX = 5as the number of helpers: is false, so the call is refused. A check written as lets 5 through, and the program reads0x20000100plus 20, which is0x20000114, the first word after the table, and calls whatever address that word holds.: the range check passes, but the slot is a hole. If holes are null pointers, the call is refused only if the code tests for null. What a call through a null pointer does depends on the processor and its memory map: it may stop the program at once or run whatever lies at address 0. Neither is a defined refusal.
, read as a signed 32-bit value: a check of only passes, and the program reads
0x20000100minus 4, which is0x200000FC, the word before the table.
Converting to an unsigned 32-bit value gives , written 0xFFFFFFFF. Without a check the conversion does not help: the byte offset is 0x3FFFFFFFC, a 32-bit address calculation keeps only 0xFFFFFFFC, and 0x20000100 plus 0xFFFFFFFC wraps around to 0x200000FC, the same word as before. With the check, the single unsigned comparison §2.12 fails and the call is refused.
Stated precisely
Let the table have slots, let be the set of slots that hold a handler, and let be the set of handlers the current program may call. Read the selector as an ordinary integer, using the width and sign that its field declares. The dispatch contract is and in every other case take the defined refusal path, which reports a fault and ends the call. The slot is read only after has been established.
One unsigned comparison checks both bounds. In two’s complement, the usual encoding of signed integers, a negative -bit value has the same bits as the unsigned number . Let be that unsigned reading: if and if . If , then holds exactly when . For the two tests are the same test. For , the smallest possible value is , so , and the comparison fails as it should. For this covers every table of up to slots.
Limits and common mistakes
Range is not presence. The check alone lets every hole through.
A count constant such as HELPER_MAX is the first invalid index, not the last valid one; comparing with instead of reads one slot past the end.
A refusal handler must refuse. A default slot that sends unknown numbers to a general-purpose or privileged handler turns a wrong number into a granted power.
On a processor that executes speculatively, running instructions ahead before an earlier comparison is resolved, a correct check can still be bypassed for a moment: the processor reads for an out-of-range and leaves a trace in its cache. This is Spectre variant 1, bounds check bypass. Where it matters, the index is also clamped with branch-free arithmetic; Linux provides array_index_nospec for this.
A writable table of function pointers is a classic attack target: overwriting one entry redirects every later call through it.
If a handler can be unloaded between reading its slot and calling it, the call runs code that no longer exists, a use-after-free §4.6 of code.
A switch statement handles range and holes through its default branch, which must refuse. A computed jump written by hand, through a compiler extension, needs the explicit checks above.
Questions answered
What happens for holes, negative values, and the maximum identifier?
In a correct dispatch table all three take the same defined refusal path: the call is refused and a fault is reported, without reading memory outside the table and without calling anything. A dispatch table maps a number, such as an opcode or a helper function number, to the code that handles it, and a table of slots has the valid indices 0 to .
A hole is a slot inside that range with no handler. The lookup either tests for an empty slot before calling, or fills every hole with a handler that only reports an invalid instruction, so that a hole never becomes a call through a null pointer.
A negative value fails the lower bound before the table is read. In C, converting the selector to an unsigned number before comparing it with rejects negative values in the same comparison, because a 32-bit becomes , which is far above .
The maximum identifier needs care with its meaning. The largest valid index, , must work if its slot is populated. A count constant equal to , often named
MAX, is the first invalid value: the check must be , and a check written as reads one word past the table and calls whatever address it holds.
A test suite exercises each of these values, together with a populated slot that the program is not allowed to call.
Related entries: Helper allowlist (§2.20), Opcode (§2.8), Interpreter (§2.13), Null pointer (§4.26), Native application binary interface (ABI) (§12.47).