Definition
A virtual machine (VM) is a computer defined by a specification and carried out by software instead of hardware. The specification lists the machine’s storage locations and states how each instruction changes them. A program written for the machine is called the guest. The software that carries out the guest’s instructions on a real processor is called the host. This entry covers small machines that run one short program at a time, not system virtual machines, which imitate a whole computer so that an operating system can run on it.
The idea
A host program, for example the software of a network device, wants to run a small program that someone else supplies: a rule that inspects each incoming message and returns a decision. Running it as native machine code, meaning instructions of the real processor, has two drawbacks. Native code is tied to one kind of processor, and it can use every instruction of that processor on every memory address the host can reach.
A virtual machine removes both drawbacks with one move: the guest is written for a machine that the host invents. The invented machine has only the instructions the host chose to give it, and its memory is only the memory the host chose to show it. Any processor can run the guest, because the host’s own code does the work.
A board game is a fair picture. The rules define the board, the pieces and the legal moves, and the players carry out the moves with their hands. A guest instruction is a move, the state of the machine is the position on the board, and the host is the hand that moves the pieces. The picture stops being accurate at one point. Nothing physical keeps the pieces on the board. Every guest action is in the end performed by host code on host memory, so the guest is confined only as well as the host code confines it.
How it works
A virtual machine has two parts: its state, and the engine that advances the state. The example is eBPF §2.1 (extended Berkeley Packet Filter), a widely used machine of this kind. Its state consists of the following.
Registers: numbered storage locations for the values being worked on. eBPF has eleven, each 64 bits wide.
The program counter §2.9: the position of the next instruction.
Memory the guest may use: the input of this run, called the guest context §2.23; a VM stack §2.25 for temporary values; and storage that outlives a run, called guest persistent state §2.36, if the host provides any.
Limits, if the host sets them, such as an instruction budget §2.32: a count of the instructions the guest may still execute.
The simplest engine is an interpreter §2.13. It repeats three steps.
Fetch the instruction at the program counter.
Decode it: split its bits into the operation and the operands, which are the values and locations that the operation works on.
Execute it: change the registers or the memory exactly as the specification says, and move the program counter to the next instruction or to the target of a jump.
The loop ends when the guest executes its exit instruction, or when the engine has to stop the guest, for example at a forbidden memory access or when a limit is used up. Such a forced stop is called a fault. The other kind of engine, a JIT §2.14 (just-in-time) compiler, translates the guest once into instructions of the real processor and runs the translation. Both engines must produce the result that the specification defines.
Memory needs one more step. When the guest asks for “byte 5 of my input”, the host must turn the request into a read of real memory. Suppose the input is 16 bytes long. Offset 5 lies inside it. Offset 20 does not, although real memory continues past the end of the input. Either a verifier, a checker that examines the guest before the run, proves that the guest can never ask for offset 20, or a runtime bounds check §2.27 refuses the request when it is made.
One run of a guest, from its first instruction to its exit, is called an invocation. A host normally loads a guest once and invokes it many times, for example once for every incoming message.
Worked example
A host uses one virtual machine to run guests for two customers, A and B. The machine keeps its registers in an array §14.5, has a stack of 64 bytes, and gives every invocation a budget of 1,000 instructions.
Invocation 1 runs the guest of customer A. The guest places A’s account number, 7041, in register R6, writes it to the stack as well, and exits after 12 instructions. Afterwards the register array holds 7041 in R6, the stack bytes hold 7041, and the budget counter stands at .
Invocation 2 runs the guest of customer B, which consists of two instructions: r0 = r6 and exit. The guest of B never wrote R6. If the host reuses the register array as it is, the guest returns 7041, and B has learned A’s account number. There are two repairs.
Reset. The host sets every register to zero before each invocation. The guest of B returns 0.
Isolate. The host refuses to load any guest that reads a register before writing it. The guest of B never runs.
The stack needs the same treatment. A guest that reads stack bytes it did not write sees the 7041 that A left there, unless the stack is cleared or the read is refused.
The budget counter shows a different harm. Suppose the host forgets to restore it. Each invocation uses 12 instructions, so after 83 invocations instructions are used and remain. Invocation 84 is stopped after 4 instructions although its guest did nothing wrong.
Real runtimes show both repairs, and also the gap. The Linux kernel isolates: its eBPF verifier §3.3 refuses every program that reads a register before writing it. The kernel documentation states the same rule for stack bytes, but current kernel source lifts the stack rule for programs loaded with the Linux privilege CAP_PERFMON. uBPF §2.16 (userspace eBPF), a library that runs eBPF programs inside an application, resets only part of the state. Each call of its function ubpf_exec restarts the instruction count from the configured limit and sets the three registers that describe the input and the stack: R1, R2 and R10. By default the uBPF interpreter does not clear the other registers, and it clears the stack only in a build for the Windows kernel. The registers, and outside that build the stack, are local variables of host functions and begin each call with whatever the host last left in that memory, so the guest of customer B returns such a leftover value. An optional check, switched off by default, tracks which registers and stack bytes the guest has written. With that check switched on, uBPF stops the guest of customer B with an error.
Stated precisely
The state of a virtual machine is everything that can influence what the guest does next. For eBPF, write the state as , where is the program counter, the values of the registers, the content of the memory the guest can reach, and the remaining instruction budget if the host sets one. Each instruction is a rule that maps one state either to the next state or to a fault.
An invocation is isolated when its result and its effects depend on three things only: the program, the input of this invocation, and storage that the host has declared to be kept between invocations for this guest. Every other piece of state that an earlier invocation could have changed must, before the next invocation starts, be either reset to a fixed starting value or made unreachable for the guest.
State that must be fresh for each invocation:
the program counter, which starts at the first instruction;
all registers, except the ones the host sets on purpose, such as the register that points to the input;
the stack and any other temporary memory;
the instruction budget and the depth of nested function calls;
the recorded result and fault status;
everything that a helper function §2.19 (a host function the guest may call) handed out during the run, such as pointers into host memory, locks and open files.
State that is kept on purpose, such as a table of counters that the guest updates, needs three stated facts: who owns it, which guests may read and write it, and when it is deleted.
Limits and common mistakes
A virtual machine is not a hardware barrier. The interpreter or JIT compiler, the helper functions and every check are ordinary native code of the host. A flaw in any of them is a flaw in the host, and the processor does not know that an address came from a guest.
Clearing the registers and the stack is not the whole job. State also hides outside the machine’s own data. A helper function that returns a pointer to a buffer it filled during an earlier invocation leaks that buffer, however clean the registers are.
A virtual machine does not limit running time by itself. A guest with a backward jump can loop for ever unless a verifier or an instruction budget stops it.
Resetting the state of the machine does not remove side channels §1.29, which are unintended ways of learning information from an observable effect such as running time. The time an invocation takes can depend on what earlier invocations left in the caches of the processor.
Questions answered
What state must be reset or isolated between VM invocations?
Every piece of state that one invocation can change and a later invocation can observe must be reset or isolated, unless the host has declared that it is kept on purpose. An invocation is one run of a guest program in a VM (virtual machine), from the first instruction to the exit, and the host is the software that runs the machine. For a small machine that runs one program per invocation the list is: the program counter; the registers, apart from those the host sets as input; the stack and other temporary memory; the instruction budget and the call depth; the result and fault status; and whatever helper functions (host functions the guest may call) handed out during the run, such as pointers into host memory, locks and open files.
For each item the host chooses one of two methods. Reset gives the item a fixed starting value, for example by filling the stack with zeros. Isolate leaves the old value in place and makes it unreachable, for example by refusing every program that reads a register or a stack byte before writing it. State that is kept on purpose, such as a table of counters, is the exception. It needs an owner, a rule for who may read and write it, and a lifetime.
The test is this: after any sequence of earlier invocations by other parties, an invocation must behave exactly as it would on a newly created machine that holds the same kept state.
Related entries: Interpreter (§2.13), Just-in-time compilation (JIT) (§2.14), Runtime bounds check (§2.27), Software fault isolation (SFI) (§2.41), Deterministic invocation state (§2.35), Guest persistent state (§2.36), VM stack (§2.25), Instruction budget (fuel) (§2.32), Guest context (§2.23). Further reading: [1, 2, 3, 163].