Section 2.15

Ahead-of-time compilation (AOT)

Definition

Ahead-of-time compilation (AOT) is the translation of a program into native code, the instructions of one kind of processor, before the program reaches or starts on the machine that runs it. The result is a native artifact: a file that holds the machine code and a description of what the code assumes, such as the processor and the interfaces it calls. The target machine loads and runs the artifact without translating anything. Just-in-time compilation (JIT) §2.14, the alternative, translates on the target after the program has arrived.

The idea

On a microcontroller §6.1 with 64 KiB of RAM, a compiler, its working data and a buffer for the code it produces compete with the application for scarce memory, and translating the same program at every start costs time and energy each time. A workstation can translate once and send the result. Every C program built on a computer and copied to a device is compiled ahead of time; the term is used mostly in contrast to JIT, for code that would otherwise run on a virtual machine §2.3. The Android Runtime (ART) can compile an app’s bytecode §2.4 into native code on the phone, for example while the phone is idle, so that later runs need no translation, and the WebAssembly §19.5.6 Micro Runtime (WAMR) provides a compiler, wamrc, that turns WebAssembly into a native file for a named processor.

Shipping native code moves trust. A device that receives bytecode can check it, with a verifier before the first run or with an interpreter §2.13 that checks each memory access. A device that receives only native code can check very little. The bytes make sense only for one processor, one set of calling rules and one layout of every structure the code shares with the host, and the bytes themselves do not show that they translate the approved program faithfully. The device can confirm that only by also holding the approved program and running a translation validator §3.33, a checker that compares one translation with its source program. An AOT artifact is therefore safe to run only if it is bound to the target it was built for and to the program it claims to translate, and if either the device validates the translation in this way or the artifact is also bound to a compiler that the device’s owner trusts.

How it works

  1. Describe the target. Fix the instruction set architecture §4.38 (ISA), the instructions and encodings the processor understands, including optional extensions such as a floating-point unit. Fix the ABI §12.47 (application binary interface), the rules by which separately compiled code passes arguments and results, uses registers and lays out data. Fix the layout of every structure shared with the host. Fix the helper interface: the numbers and signatures of the host functions the program may call.

  2. Translate on the build machine. This is cross-compilation §12.1: compiling on one kind of machine for another.

  3. Leave references for the loader. The build machine does not know where the device keeps its host functions. Either the code calls them through a table whose layout is part of the helper interface, or the artifact carries relocation records §12.50, notes that tell the device’s loader which bytes to patch with which address.

  4. Describe the artifact. A header records the ISA and its extensions, the ABI variant, the versions of the shared layouts and of the helper interface, a cryptographic hash §5.1 of the input program (a short fingerprint computed from its bytes; for a collision-resistant §5.5 hash, nobody can find two different programs with the same fingerprint), and the compiler’s name and version.

  5. Authenticate. The build service computes a digital signature §5.14 over the code and the header together.

  6. Admit on the device. The loader verifies the signature, compares every header field with what the device provides, rejects any mismatch, applies the relocations, places the code in memory that may execute, and only then calls it.

Common formats carry such bindings. An ELF §12.6 (Executable and Linking Format) file names its processor in the field e_machine, for example 40 for 32-bit Arm, 62 for x86-64 and 183 for 64-bit Arm, and records ABI variants in e_flags. A Linux kernel module records the kernel version and main configuration options it was built for, and the kernel refuses a module whose record does not match. ART records in each compiled file the ISA and a checksum §4.32 of the bytecode it was compiled from, and does not use a compiled file whose checksum no longer matches the bytecode.

Worked example

Checking the target. A device’s firmware runs on a 32-bit Arm processor with a floating-point unit and uses the soft-float variant of the Procedure Call Standard for the Arm Architecture (AAPCS), in which the first single-precision floating-point argument travels in the integer register r0. In the hard-float variant it travels in the floating-point register s0. An ELF artifact arrives with e_machine = 40 and e_flags = 0x05000400.

  1. Processor: 40 means 32-bit Arm, which matches.

  2. ABI version: the bitwise AND of 0x05000400 with the mask 0xFF000000 is 0x05000000, which means version 5 of the Arm embedded ABI (EABI), the version the firmware also uses.

  3. Floating-point variant: the bitwise AND of 0x05000400 with the mask 0x00000600 is 0x00000400, the hard-float flag. The firmware needs 0x00000200, the soft-float flag, so the loader rejects the artifact.

Had it been accepted, firmware passing the temperature 21.5 would have put its bit pattern 0x41AC0000 in r0, while the artifact read s0, which holds whatever an earlier calculation left there. Every instruction would have executed, and the result would have been wrong.

Checking a shared layout. In version 1 of a host’s input structure, a 4-byte event number sits at offset 0 and a 4-byte payload length at offset 4. Version 2 inserts a 4-byte timestamp at offset 4 and moves the length to offset 8. An artifact compiled for version 1 reads offset 4 and takes the timestamp, say 1,700,000 milliseconds, as the length of a 64-byte payload; reading that many bytes runs 1,700,000−64=1,699,9361{,}700{,}000 - 64 = 1{,}699{,}936 bytes past the payload, and no interpreter stands in the way. A header field stating input layout 1 lets the loader refuse the artifact on a device that provides layout 2.

Stated precisely

Let an artifact be a triple (C,M,σ)(C, M, \sigma): code CC, header MM and a signature σ\sigma over CC and MM together. Let DD describe the device: its ISA and extensions, its ABI, its layout versions and its helper interface version. Let AA be the set of cryptographic hashes of the guest programs that the device’s owner has approved. The loader accepts only if σ\sigma verifies under the build service’s public key, every requirement in MM is met by DD, either by equal values or by a documented compatibility rule, and the program hash recorded in MM lies in AA.

If the signing key stays secret, the signature scheme cannot be forged and the hash function is collision-resistant, acceptance shows that the build service produced CC and MM for this kind of device and for an approved program PP. It does not show that CC behaves like PP. That needs compiler correctness §3.40: every observable behavior of the code produced is a behavior that the source program allows. CompCert, a C compiler verified with the Rocq (formerly Coq) proof assistant, proves this property, which its manual calls semantic preservation, for its translation from C to assembly code. The proof starts after parsing and ends before the assembler and linker §12.4 turn assembly code into machine code, so those steps are not covered; and for a source program with undefined behavior §4.12 it promises nothing beyond the point where that behavior occurs. Without such a proof, correspondence rests on checking each translation separately, which is called translation validation, or on trust in the compiler.

Limits and common mistakes

A signature proves origin, not correctness: it shows who produced the bytes, not that the translation is faithful. A faithful translation, in turn, does not show that its author was entitled to install it.

Native code bypasses every check an interpreter would make. Memory confinement must be compiled into the code, as in software fault isolation §2.41 (SFI), enforced by hardware such as a memory protection unit §6.8 (MPU), or follow from a proof about a checked bytecode program together with a trusted compiler or a validated translation.

A processor name is not enough. One processor can run code built for two incompatible ABIs, as the floating-point example shows, and an artifact can use an optional instruction that a smaller member of the same processor family lacks, where the instruction raises a fault.

Version numbers bind only as well as they are maintained. A different number does not prove incompatibility if a documented rule keeps the old behavior, and an equal number proves nothing if a layout changed without a new number.

AOT does not promise a smaller update. When eBPF §2.1 (extended Berkeley Packet Filter) bytecode is compiled for 32-bit Arm, each 8-byte eBPF instruction becomes Thumb-2 §4.40 instructions of 2 or 4 bytes, but one 64-bit eBPF operation can need several of them. A fleet with several kinds of processor also needs one artifact for each.

Questions answered

How are native artifacts bound to the architecture and trusted compiler?

A native artifact, the machine code that ahead-of-time compilation produces, is bound by a header that states what the code assumes, a signature that covers the code and the header together, and a device loader that checks both before running anything. The header names the instruction set and its optional extensions, the application binary interface (the calling and data-layout rules), the version of every shared structure and host-function interface, a cryptographic hash of the input program, and the compiler and its version. The loader verifies the signature with the build service’s public key, compares each header field with what the device provides, and rejects any mismatch. In an ELF file, for instance, e_machine names the processor and e_flags records Arm’s floating-point calling variant.

The binding to a trusted compiler is indirect: only a build service that runs the approved compiler version holds the signing key. The signature therefore shows who produced the artifact, not that the translation is right. Correct translation must come from a verified compiler such as CompCert, from checking each translation separately (translation validation), or from explicit trust in the compiler. A reproducible build §13.8, which yields bit-identical output from the same input and compiler version, lets a second party rebuild the artifact and compare.

Related entries: Translation validation (§3.33), Compiler correctness (§3.40), Guest-host ABI (§2.37), Just-in-time compilation (JIT) (§2.14), Native application binary interface (ABI) (§12.47), ELF (§12.6), Cross-compilation (§12.1), Reproducible build (§13.8), Bytecode authenticity versus safety (§2.43). Further reading: [72, 82].