Definition
Bytecode is a program stored as a sequence of numeric instructions for a virtual machine §2.3, a computer defined in software, and not for a physical processor. A compiler or an assembler produces bytecode from text that a person wrote. An interpreter §2.13 then carries the bytecode out on the real processor, or a second compiler first translates it into that processor’s instructions. Bytecode is compact and regular, so software can inspect a whole program before running it. The bytes have a meaning only together with the specification of the virtual machine.
The idea
A maker of devices wants its customers to add small rules to a device after it has been sold. Shipping a rule as machine code would need a separate file for every type of processor, and the device could not easily tell what the code would do. Shipping a rule as source text would force every device to carry a full compiler. Bytecode sits between the two. The hard translation work is already done, so the device needs only a small engine. And the instructions belong to an invented machine, so one file serves every device that has an engine for that machine.
Sheet music is a fair picture. It is a compact, standard notation that any trained player can perform on their own instrument, and it can be read through before a note is played. The picture stops being accurate at one point: music leaves room for interpretation, and bytecode must leave none. Two engines that read the same bytes differently are running two different programs. For that reason it matters what must be known besides the bytes.
How it works
Bytecode is the middle station of a pipeline. The example here is the bytecode of eBPF §2.1 (extended Berkeley Packet Filter), a virtual machine for small programs that run inside a host program. Its instructions are 8 bytes each, apart from one kind that takes 16.
Source. A person writes the program in the programming language C, or in assembly, a notation with one line of text for each instruction.
Translate. A compiler turns C into instructions, and an assembler turns assembly into instructions. The Clang §12.17 compiler, for example, writes eBPF instructions into an object file in ELF §12.6 (Executable and Linking Format). Such a file holds the instruction bytes in named sections, together with tables that describe them: a symbol table §12.8 of names, and relocation records §12.50, which mark the places where a number is still missing from an instruction because it was not known at translation time.
Load. A loader reads the file, finds the instructions, fills in the missing numbers and hands the finished instruction sequence to the runtime, the software that will execute it.
Check. If the runtime has a verifier, which is a checker that decides whether a program is safe to run, the verifier examines the finished instructions.
Execute. An interpreter carries out the instructions one by one, or a JIT §2.14 (just-in-time) compiler first translates them into instructions of the real processor.
The loader already decodes the ELF header to locate the code section. Decoding an instruction is a different task: it splits the instruction bytes into fields according to the instruction encoding §2.7. Several stages may do that: the verifier decodes in order to analyze, the interpreter in order to execute, the JIT compiler in order to translate. All of them must split the bytes in exactly the same way. They must also work on the same bytes: instructions that are changed after the check are no longer the instructions that were checked.
The check looks at instructions, not at their history. A program that a person wrote directly in assembly arrives at the loader as the same kind of bytes as a program that came from a C compiler, and it passes through the same check.
Worked example
Take these 16 bytes, written in hexadecimal:
b7 00 00 00 2a 00 00 00 95 00 00 00 00 00 00 00
Three facts make them readable: they are eBPF instructions, the byte order §4.21 is little-endian (the lowest byte of a number is stored first), and the program starts at the first byte. The bytes are then two instructions. The first, b7 00 00 00 2a 00 00 00, puts the number 42, which is 2a in hexadecimal, into register R0, one of the machine’s numbered storage locations. The second, 95 00 00 00 00 00 00 00, is exit. The program returns 42.
Change one of those facts and the bytes change their meaning.
Which instruction set. On a little-endian machine, the 8 bytes
15 00 00 01 06 08 00 00are an instruction of classic BPF §2.2, the older packet-filter format: “if the accumulator equals0x806, continue, otherwise skip 1 instruction”, where the accumulator is the main storage location of that machine. Read as eBPF, the same 8 bytes say “if register R0 equals0x806, jump forward 256 instructions”.Which byte order. RFC 9669, the specification of the eBPF instruction set, encodes “add
0x11223344to register R1” as07 01 00 00 44 33 22 11on a little-endian machine and as07 10 00 00 11 22 33 44on a big-endian machine, which stores the highest byte of a number first. Read with the big-endian rule, the first form names register R0 and the constant0x44332211.Which host. The bytes
85 00 00 00 07 00 00 00mean “call helper function §2.19 number 7”. A helper function is a function of the host that a program may call. On Linux, number 7 returns a pseudo-random number. Another host is free to give number 7 to a different function.
Stated precisely
A sequence of bytes is unambiguous bytecode only together with the following five facts.
The instruction set and its subset. For eBPF this means RFC 9669 and the conformance groups that the program uses. A conformance group is a named set of instructions that a runtime supports either completely or not at all.
The byte order of every field that is longer than one byte. In eBPF the byte order also decides which half of the register byte names the destination register.
The extent of the code: where the instruction bytes begin, how many there are, and at which instruction execution starts.
The open references: the relocation records and the symbol table, if the instructions still contain missing numbers.
The interface to the host: what the registers hold when the program starts, what each helper function number means, and what the returned value means.
An ELF object file carries facts 2, 3 and 4 itself, and it names the instruction set of fact 1. Its header states the byte order and the machine type, where the number 247 stands for BPF, and its tables describe the sections, the symbols and the relocation records. Fact 5 is not recorded in the instruction bytes. The author of the program and the host have to agree on it separately.
Limits and common mistakes
Bytecode is a format, not a guarantee. A program in bytecode can be as harmful as any other program. Safety comes from what the runtime checks and enforces.
Bytecode is portable only between hosts that agree on all five facts. Two hosts with the same instruction set and different helper functions cannot exchange programs.
A check of the bytecode says nothing about the stations before and after it. The compiler may have translated the source wrongly, so that a program that is safe to run still makes the wrong decision. A faulty JIT compiler may execute something other than the checked instructions.
The name does not mean that an instruction is one byte long. An eBPF instruction is 8 bytes long, or 16 for one kind, and its first byte, the opcode §2.8, selects the operation.
Writing bytecode by hand gives no extra power, and it gives no exemption from a check that the host applies to every program.
Questions answered
What metadata is needed to interpret bytecode unambiguously?
Five facts are needed besides the bytes themselves. Bytecode is a program stored as numeric instructions for a virtual machine, a computer defined in software. First, the instruction set, and the subset of it that the program uses; the same bytes mean different things in different instruction sets. Second, the byte order of every field that is longer than one byte. Third, the extent of the code: where the instructions begin, how many there are, and where execution starts. Fourth, if some numbers inside the instructions were left open at translation time, the tables that say how to fill them in; an object file calls them relocation records and a symbol table. Fifth, the interface to the host: what the registers, the storage locations of the machine, hold at the start, what each helper function number means (a helper function is a host function the program may call), and what the returned value means.
An object file format such as ELF (Executable and Linking Format), which stores the instruction bytes together with tables that describe them, records the byte order, the machine type and the layout of the code. The interface to the host is not recorded in the instructions and must be agreed separately. If one of the five facts is missing or wrong, a decoder still produces instructions from the bytes, but they can be the wrong instructions.
Why does handwritten assembly not skip a mandatory bytecode verifier?
Handwritten assembly does not skip the verifier, because the verifier examines the final instruction bytes, and handwritten assembly ends up as the same kind of bytes as a program compiled from the language C. A verifier is a checker that decides whether a program is safe to run. Assembly is a notation with one line of text for each instruction, and an assembler encodes each line as one instruction. The loader, the part of the host that receives programs, gets those instructions on the same path as any others, and a host that requires verification runs the verifier on everything that arrives on that path. The verifier has no knowledge of the tool that produced the bytes. Handwritten code that makes a forbidden memory access is rejected exactly as compiled code would be. The conclusion changes only if the host itself offers a way to load programs without the check, and that is a property of the host, not of assembly.
Related entries: Instruction encoding (§2.7), Opcode (§2.8), Guest-host ABI (§2.37), eBPF verifier (§3.3), ELF (§12.6), Virtual machine (VM) (§2.3), eBPF (§2.1), Bytecode authenticity versus safety (§2.43), Program loading: from an ELF file to a running native process (§12.49). Further reading: [1, 2, 160, 161].