Section 2.37

Guest-host ABI

Full name: Guest-host application binary interface.

Definition

A guest-host ABI (application binary interface) is the agreement, at the level of bits and bytes, between a guest program and the host that runs it. A guest program is code compiled separately and run inside the host’s virtual machine §2.3 (VM). The agreement fixes the instruction set, the registers that carry inputs and results, the byte layout and meaning of the input data, the number and calling rules of each host function, the meaning of each return value, and how a program learns which version and features the host offers.

The idea

A guest program was compiled to read a message length as the 4 bytes at offset 4, counted from the start of the input block that the host passes to it. After a firmware update, the host writes a flags field at offset 4 and the length at offset 8. Every instruction still runs correctly and every memory access stays in bounds, yet the guest decides on the wrong number. Only an agreement about where a value lives has failed, and no bounds check can see that kind of failure.

The guest and the host are built at different times, often by different people, and they meet only as bytes: instructions in the program, values in registers, fields in memory. An API is a contract about source code: names, types and functions as written. An ABI §12.47 is what those names become after compilation: offsets, widths, byte orders §4.21, register numbers and numeric identifiers. Sharing a header file, a source file of declarations, does not settle the binary form, because the compiler and the target processor decide the layout. The native application binary interface of a platform plays the same role between compiled functions on one processor; the guest-host ABI plays it between bytecode §2.4 and the runtime that executes it.

How it works

The agreement has six parts, shown here for eBPF §2.1 (extended Berkeley Packet Filter), the most widely used bytecode of this kind.

  1. Instruction set: the encoding and meaning of every instruction, which RFC 9669 standardizes for eBPF [160].

  2. Register roles: the eBPF calling convention §2.6. At entry R1 holds the address of the input and R10 the address of the stack; at a call to a host function R1 to R5 carry the arguments and R0 receives the result; R0 also carries the program’s return value.

  3. Context layout: for each field of the guest context §2.23, the block of memory that describes the event, its offset, width, byte order, signedness, unit, allowed range, whether the guest may write it, and how long it stays valid.

  4. Helpers: for each helper function §2.19, a host function the guest calls by number, its number, the type and meaning of each argument, and the meaning of its result and error values.

  5. Maps and verdicts: how the guest names a map, a key-value table the host owns, and which return values mean which decision.

  6. Version and features: how the guest and its loader learn what this host provides.

The context layout is where most breaks happen, because a structure declared in C is not a fixed format. A C compiler places each field at an offset that satisfies its alignment §4.22, on most platforms a multiple of the field’s own size, and inserts padding bytes to get there. The width of a pointer depends on the target: 4 bytes on a 32-bit processor, 8 on a 64-bit one. So inserting a field moves every later field, and changing the pointer width moves every field after a pointer and changes the size of the structure. Linux avoids both problems. The structures that eBPF programs see change only by adding fields at their end. Their pointer fields are either declared as 32-bit numbers, as the fields data and data_end that point to the start and end of a packet are, or declared with a macro, __bpf_md_ptr, that reserves 8 bytes with 8-byte alignment on every processor; either way the guest-visible offsets do not depend on the pointer width. The eBPF verifier §3.3 in Linux then rewrites each guest access to such a structure into an access to the kernel’s real internal structure, whose layout may change freely, and turns a load of data into a load of the full pointer.

Because guest and host change separately, the ABI needs a way to detect a mismatch.

The safest check is in the loader: the program declares the version and features it requires, and the loader refuses it before it runs if the host lacks any. A program can also test the feature bits while it runs, reading them from the context or through a helper, and take a fallback path when a map it needs is absent. That is safe only if every use of an absent feature fails safely: a reference to a missing map must be rejected at load time or make the helper return an error, and must never reach memory. Linux supports both styles: its loader library probes the kernel for helpers and map types, and a program can test with bpf_core_field_exists whether a field exists in the running kernel.

A hook is a point in the host’s code where the host runs a guest program, such as on receipt of a packet or before a connection is accepted. An ABI is hook-agnostic when every hook uses the same contract: one context format that carries an event number and the event’s data in a self-describing form, one set of verdict values, and one helper table. Adding a hook then needs only new host code that fills the common context and calls the VM; guest programs and their compilers see no new interface, and a program can handle the new event as soon as it knows the event’s number. Three kinds of change still need new host software: a new hook location, because the host must call the VM from there; a new helper, because the guest can do only what host functions offer; and a new kind of action, because the host must implement whatever a new verdict value is meant to do.

Worked example

Version 1 of a context has two 4-byte unsigned fields, both little-endian: kind at offset 0 and length at offset 4, 8 bytes in total. A guest compiled for version 1 rejects any message longer than 256 bytes by loading 4 bytes at offset 4. Version 2 inserts a 4-byte flags field after kind, which moves length to offset 8 and makes the context 12 bytes long. Run the old guest on a version 2 context with kind = 2, flags = 1 and length = 300. The guest loads offset 4 and gets 1, the flags value. Since 1≤2561 \le 256, it allows a 300-byte message that its rule should reject, and no access left the 12-byte context. Appending flags at offset 8 instead would have kept length at offset 4 and left the old guest correct.

Next, a context with a pointer and a length, declared in C as struct { void *buf; uint32_t len; }, where uint32_t is a 32-bit unsigned integer. On a 32-bit host, buf occupies offsets 0 to 3 and len offsets 4 to 7, 8 bytes in total. On a 64-bit host, buf occupies offsets 0 to 7 and len offsets 8 to 11, and 4 padding bytes round the size up to 16, a multiple of the pointer’s 8-byte alignment. Run a guest built for the 32-bit layout on a little-endian 64-bit host where buf is 0x00007ffd12345678. The guest loads 4 bytes at offset 4 as the length. Little-endian order stores the low 4 bytes of the pointer first, at offsets 0 to 3, so offsets 4 to 7 hold its high half, 0x00007ffd, which is 32,765. The guest believes the buffer is 32,765 bytes long.

A third break appears when the host’s addresses are narrower than the guest’s 64-bit registers. A host on a 32-bit processor that receives an address from the guest, for example as a helper argument, and keeps only the low 32 bits turns 0x0000000100001000 into 0x00001000, a different address that may look valid. The host must check that the upper 32 bits are zero before narrowing, and the ABI should say so.

Finally, a feature check. A host publishes the feature bits 1011 in binary: bits 0, 1 and 3 are set, counting from the rightmost bit as bit 0, so features 0, 1 and 3 are present. A program needs features 1 and 2, the mask 0110. The missing features are the required bits that the host does not provide: the required mask AND the inverse of the provided mask, 0110 AND 0100, which is 0100. The result is not zero, so feature 2 is missing. The loader refuses the program before it runs, or the program takes its fallback path for feature 2.

Stated precisely

Describe the ABI as lists of facts, and call a guest and a host compatible when the facts the guest was built with equal the facts the host provides.

With versions written as pairs (major, minor), a host that provides (Mh,mh)(M_h, m_h) accepts a program built for (Mg,mg)(M_g, m_g) when Mg=MhM_g = M_h and mg≤mhm_g \le m_h. With feature bits, let FhF_h be the mask the host provides and FgF_g the mask the program requires; the program’s needs are met when FgAND(NOTFh)=0F_g \mathbin{\mathrm{AND}} (\mathrm{NOT}\ F_h) = 0, where NOT\mathrm{NOT} inverts every bit. Both rules are sound only if the host keeps its promise about minor versions: a minor change only adds, and never moves, removes or redefines an existing item.

Limits and common mistakes

A matching header file does not prove a matching ABI. Compiler options §12.3 such as structure packing, a different target processor, or a different version of the header can change offsets and sizes while every name stays the same.

Equal widths and offsets do not prove equal meaning. A field that changes from milliseconds to microseconds keeps its width and offset but changes every decision that depends on it by a factor of 1,000: the value 100 meant 100 milliseconds and now means 0.1 milliseconds.

A correct layout does not protect a stale pointer §4.1. A field that holds the address of a buffer is correct only while the buffer exists. The ABI must say how long each address stays valid, and the guest must not keep it beyond that.

Binary compatibility is not authorization §1.7 or correctness. A guest that matches the ABI exactly can still be malicious or wrong; the ABI only ensures that both sides read the same bits the same way.

The contexts and helpers of Linux are one ABI, not the only one. Other hosts, including eBPF runtimes on small devices, define their own context layouts, helper numbers and verdict values. RFC 9669 standardizes the instruction set, not these.

Guessing hides a mismatch. A loader that runs a program built for a newer minor version, in the hope that it does not use the new parts, turns a detectable mismatch into a silent one.

Questions answered

What breaks when a struct field is inserted or pointer width changes?

Inserting a field into a structure, or changing the width of a pointer, moves the byte offsets of the fields after it and changes the size of the structure, so a guest program compiled for the old layout reads and writes the wrong bytes. The guest is code that a host runs inside a virtual machine (VM). It finds each field of the host’s data by a byte offset fixed in its instructions when it was compiled, and that offset is part of the guest-host ABI (application binary interface), the binary agreement between the two sides. What breaks:

The remedies are to append new fields at the end instead of inserting them; to make the guest-visible layout independent of pointer width, for example with an 8-byte slot for every pointer on every processor, as Linux does for its newer pointer fields; to translate between the layout the guest sees and the host’s internal layout; and to carry a version number or feature bits, so that a mismatched program is refused instead of silently misread.

Related entries: Struct padding and packing (§4.27), Serialization (§4.28), eBPF register convention (§2.6), Native application binary interface (ABI) (§12.47), Guest context (§2.23), Endianness (§4.21), Alignment (§4.22), Bit operations: truth tables, masks, shifts, and rotations (§4.50). Further reading: [160].