Section 2.25

VM stack

Full name: Virtual machine stack.

Definition

The VM stack (virtual machine stack) is the memory region in which a guest program keeps its local variables and the data of its function calls; the guest is the program that a virtual machine §2.3 (VM), a computer defined in software, runs. Each call adds a frame, an area for that call’s data, and each return removes the newest frame. The VM stack is part of the guest’s memory, distinct from the native stack: the call stack of the host thread on which the VM’s own code runs.

The idea

When a guest function calls another, the caller needs its local values and its place in the code back when the callee returns, and the callee needs fresh room of its own. A stack supplies both: the newest frame is always the one in use, and a return discards it. The VM must also limit the stack, because guest code is untrusted and could otherwise call deeper and deeper until memory runs out.

Two stacks are involved, and they must be counted separately. The guest’s stack is memory that the VM hands out and checks. The native stack belongs to the host thread that runs the interpreter §2.13, the program that executes guest instructions one at a time. It holds the interpreter’s own variables and the frames of every helper function §2.19 the guest calls. On a small device a thread’s native stack may be only a few KiB, and running past its end can silently overwrite neighboring memory. A guest that respects its own stack limit can still exhaust the native stack, depending on how the VM implements guest calls.

A pile of canteen trays is the usual picture: each call adds a tray on top, and each return removes the top tray. Here there are two piles, the guest’s and the host’s, and the question is whether each guest tray also adds a tray to the host’s pile. The picture stops at the limit: a pile that grows too high falls over where everyone can see it, while a stack that runs past its end overwrites memory silently unless something checks.

How it works

eBPF §2.1 (extended Berkeley Packet Filter) shows a typical design.

  1. The frame pointer. Register R10 holds the address just above the top byte of the current frame, and the guest cannot write R10. The stack grows downward, so the guest addresses its local variables as R10 minus an offset: a 4-byte value at R10 −- 4 occupies the 4 bytes just below that address.

  2. Access checks. Every stack access must lie inside the current frame. Linux allows offsets from −512-512 up to, but not including, 0, which gives a frame of at most 512 bytes. The Linux eBPF verifier §3.3, which checks programs before they run, also refuses a read of stack bytes that the program has not yet written.

  3. A call. For a call to another function of the same program, the VM must preserve three things for the caller: the return position, which is the number of the instruction after the call; the registers R6 to R9, which the eBPF calling convention §2.6 requires a callee to leave unchanged; and the caller’s R10. It then points R10 at the top of a new frame and jumps to the callee.

  4. A return. When the callee executes its exit instruction, the VM restores R6 to R9 and R10 and continues at the saved return position. The callee’s frame is discarded.

  5. Limits. The Linux eBPF verifier rejects any program whose functions can call themselves, directly or through other functions. It allows at most 8 frames in one chain of calls, the program’s main function included, and limits the combined stack of all frames in one chain to 512 bytes. The guest’s total stack use is therefore known before the program runs.

A VM can implement guest calls in three ways, and they differ in native stack use.

In all three designs, a helper function that the guest calls runs on the native stack. Where the guest stack itself lives is a separate choice: an interpreter that declares it as a local array of its run function puts it on the native stack too.

Worked example

Let each guest frame hold 16 bytes of local variables, and let a recursive interpreter use 128 bytes of native stack for each guest call in progress. These sizes are invented for the example. With 3 guest calls in progress, their 3 frames hold 3×16=483 \times 16 = 48 bytes and the interpreter’s recursion holds 3×128=3843 \times 128 = 384 bytes of native stack, 8 times as much. With 1,000 guest calls in progress, their frames need 1,000×16=16,0001{,}000 \times 16 = 16{,}000 bytes and the native stack 1,000×128=128,0001{,}000 \times 128 = 128{,}000 bytes. Every local access could lie inside its own 16-byte frame and the run would still exhaust memory; only a limit on depth prevents that.

Now fit the recursive design into a thread whose native stack is 4,096 bytes (4 KiB), of which the host code and the interpreter’s first call already use 1,024 bytes. Each nested guest call takes 128 bytes more, so at most (4,096−1,024)/128=3,072/128=24(4{,}096 - 1{,}024) / 128 = 3{,}072 / 128 = 24 nested calls fit. That fills the stack exactly and leaves nothing for a helper function or an interrupt handler §6.12, so the safe limit is lower than 24.

An iterative design moves the cost instead of removing it. If each call record holds a return position of 8 bytes, R6 to R9 at 4×8=324 \times 8 = 32 bytes, and the saved R10 at 8 bytes, a record takes 8+32+8=488 + 32 + 8 = 48 bytes. An array for 8 nested frames takes 8×48=3848 \times 48 = 384 bytes, and the 8 frames’ locals at 16 bytes each take 8×16=1288 \times 16 = 128 bytes, so 384+128=512384 + 128 = 512 bytes in all. That is a fixed amount the host can reserve once, whatever the guest does.

Stated precisely

Let dd be the number of guest frames active at one moment and DD its allowed maximum, and let fif_i be the size in bytes of the ii-th active frame. The guest stack use is G=f1+f2+…+fdG = f_1 + f_2 + \dots + f_d, and the VM guarantees G≤GmaxG \le G_{\max}, the size of the guest stack region. Linux eBPF has D=8D = 8 and Gmax=512G_{\max} = 512 bytes.

For the native stack, let N0N_0 be the most bytes in use while only the guest’s first frame is active, counting the host code and the interpreter’s first call; let hh be the most native bytes that any nested guest call adds, with h=0h = 0 for an iterative interpreter; let HH be the most native stack that helper functions and interrupts §6.11 can occupy at one moment on top of the host code and the interpreter’s frames, counting together everything they have on this stack at that moment: the frames of a running helper function, the frames of every interrupt handler in progress, nested handlers included, and any registers the processor saved on this stack when an interrupt arrived; and let SnativeS_{\mathrm{native}} be the size of the thread’s native stack. The native stack cannot overflow if N0+(D−1)h+H≤Snative.N_0 + (D - 1)\,h + H \le S_{\mathrm{native}}. The factor is D−1D - 1 because the first frame is already counted in N0N_0. Taking HH as the largest stack use of any single helper function or interrupt handler is not enough, because an interrupt can arrive while a helper function runs: if an interrupt handler that uses 400 bytes can interrupt a helper function that uses 400 bytes, HH is at least 400+400=800400 + 400 = 800 bytes, not 400.

If no guest function can call itself, directly or through others, the depth limit DD can be checked before the run: the deepest chain of calls is the longest path through the record of which function calls which, and its length is known in advance. If a guest function can call itself, that record gives no bound, and a VM that allows such recursion must bound the depth in another way. It can prove a bound before the run for the particular program, for example with a ranking function §3.25: a whole number, computed from each call’s arguments, that every recursive call lowers by at least 1 and that never falls below 0, so that a chain of recursive calls that starts at the value mm holds at most m+1m + 1 frames. Such a proof exists only for some programs. The alternative, which works for every program, is to count the depth at run time and stop the guest with a fault when a call would exceed DD.

Limits and common mistakes

A bounded guest stack does not bound the native stack. The two are separate accounts, and the native one also pays for helper functions and for the interpreter’s own frames.

A stack overflow §4.10, a stack running past its end because of too many or too large frames, differs from a buffer overflow §4.4 on the stack, a write past the end of one array that happens to lie on the stack. A check that keeps accesses inside the frame prevents the second; only a limit on depth and frame size prevents the first.

Saved control data must be out of the guest’s reach. If return positions were stored in memory that the guest can write, one guest write could redirect execution when the callee returns, the classic stack-smashing attack. Keep call records in host memory and let the guest address only its local variables.

A guest stack placed on the native stack, as a local array of the interpreter, starts with whatever earlier host calls left there. A guest that may read before writing can then read host data. The rule that a stack byte may be read only after it has been written closes this leak.

Interrupts add native stack use that the guest’s limits do not count. Unless the processor switches to a separate interrupt stack, an interrupt handler pushes its frame onto the stack of the thread it interrupted. Even such a switch does not keep the thread’s stack untouched: a Cortex-M processor first saves 32 bytes of registers on the interrupted thread’s stack, 104 bytes when it also saves floating-point state, plus up to 4 bytes of alignment §4.22, and only then moves to its separate stack for handlers.

The figures 512 bytes and 8 frames are Linux’s choices, not general rules. Other VMs set other limits, and one VM’s interpreter and JIT compiler can use native stack very differently.

Questions answered

Does guest recursion consume native stack frames?

It depends on how the virtual machine (VM) implements guest calls: a recursive interpreter or a just-in-time (JIT) compiler uses native stack for every guest call in progress, while an iterative interpreter uses no extra native frames and keeps explicit call records in host memory instead. Guest recursion means a guest function that calls itself, directly or through other functions. The native stack is the call stack of the host thread that runs the VM.

In a recursive interpreter, each guest call makes the interpreter call its own run function, so native stack use grows with depth: at 128 native bytes per call, 24 nested calls take 24×128=3,07224 \times 128 = 3{,}072 bytes. JIT-compiled guest calls are native call instructions. An iterative interpreter keeps its native depth constant but stores one record per active call, so its memory use still grows with depth. Every design needs a depth bound. Linux eBPF (extended Berkeley Packet Filter) rejects recursion outright and allows at most 8 frames and 512 bytes of guest stack in one chain of calls; a VM that permits recursion must bound depth another way: by proving a bound for the particular program before it runs, which is possible only for some programs, or by counting depth at run time and stopping the guest with a fault at the limit. Helper functions run on the native stack in every design.

Related entries: Stack overflow (§4.10), Resource confinement (§2.40), eBPF register convention (§2.6), Static, stack, and heap storage (§4.11), Buffer overflow (§4.4), Interpreter (§2.13), Scratch memory (§2.24). Further reading: [1, 2].