Definition
An instruction budget, also called fuel, is a counter that limits how many operations a guest program may execute in one run. A guest program is code that a host runs inside a virtual machine §2.3 (VM), a program that executes another program’s instructions. The host sets the counter before each run. A counting rule states which operations cost units and how many. The VM takes each charged operation’s cost from the counter and stops the run, with a budget-exhausted result, when too few units remain.
The idea
A host runs a guest program on every event, for example to decide whether a received packet may pass. Memory checks keep the program’s loads and stores inside its permitted regions, but they do nothing against the one-instruction eBPF §2.1 (extended Berkeley Packet Filter) program goto -1, a jump to itself, which touches no memory and never ends. The thread that runs the VM is stuck, and every later event waits behind it: a denial of service §1.28. Deciding in advance whether any given program ends is impossible in general; this is the halting problem §19.2.2. A host can accept only programs whose termination a checker proves before they run, which requires programs written in a restricted way; the eBPF verifier §3.3 in Linux works like this. Or it can count at run time and stop any run that has used up its allowance, which works for any bytecode §2.4. That count is the instruction budget.
The name comes from a vehicle that stops when its tank is empty, wherever it happens to be. Like the vehicle, the run stops at an arbitrary point, so the program delivers no result. Unlike a vehicle, which burns fuel all the time, a VM takes units only for the operations its counting rule charges: anything uncharged runs for free, and an uncharged path that can repeat defeats the budget. The WebAssembly §19.5.6 runtime Wasmtime also calls the idea fuel; the Ethereum blockchain calls it gas and prices each kind of operation separately.
How it works
One run proceeds in three steps.
Set. Before the run, the host sets the counter to the budget , a whole number such as 10,000. Normally every run starts with the full budget.
Check, then charge. Before each charged operation, the VM compares the counter with the operation’s cost. If too few units remain, the run stops; otherwise the VM subtracts the cost and performs the operation. The worked example shows why the check must come first.
Stop. A stopped run ends with its own status, budget exhausted, distinct from a normal return; the registers at that moment are not a result. The host’s fault-to-verdict mapping §2.34 turns the status into a decision, for example rejecting the event.
The counting rule decides what the budget guarantees. Three rules are common.
One unit per executed instruction. At most instructions run. The rule must state whether a wide instruction, such as the eBPF 64-bit immediate load, which occupies two 8-byte slots, costs one unit or two.
One unit per taken backward jump and per call, tail calls included. A backward jump is one whose target is at or before its own position. Within one function, an instruction runs again only after such a jump or after a new call into the function, so charging these two operations stops every endless run and needs fewer checks than the first rule. This rule bounds the number of instructions only through the length of the program, as the section Stated precisely shows.
A weighted cost per kind of operation, as with gas, so that slow operations cost more units.
Three kinds of operation need an explicit decision, because each can make the budget guarantee nothing.
Branches. Every loop passes through a jump back. If taken jumps are free,
goto -1runs forever without spending a unit.Helper calls. A helper function §2.19 is a host function that the guest calls by number. The call instruction is charged, typically one unit, but the helper’s own work runs as native code outside the VM’s instruction loop and is not measured: one unit can stand for copying 4 bytes or 4,096. That work needs its own bound, stated in the helper contract §2.21. A helper that runs a guest function repeatedly, as the Linux helper
bpf_loopdoes, is a loop that the instruction loop does not see; in a runtime with a budget, the guest instructions it runs must be charged to the same counter.Guest subcalls. A subcall is a call from one function of the guest program to another function of the same program; eBPF calls these BPF-to-BPF calls. The called function must spend from its caller’s counter: if a subcall reset the counter to , a loop whose body calls a function would refill the counter on every pass and never stop. A tail call, which replaces the running program by another without returning, must likewise carry the remaining units over. The depth of nested subcalls needs a separate limit, because the guest’s stack has a fixed size.
A just-in-time (JIT §2.14) compiler translates bytecode into the processor’s own machine code before it runs. The translated code must contain the same checks and charges as the interpreter §2.13, or the guarantee holds only while the interpreter runs the program.
Worked example
Take this three-instruction eBPF program. An eBPF jump offset counts from the instruction after the jump, so goto -2 at position 2 jumps to position .
0: r0 = 0
1: r0 += 1
2: goto -2
The budget is units, and the rule charges one unit per executed instruction, checking before it subtracts.
| step | position | fuel before | what happens | fuel after | r0 |
|---|---|---|---|---|---|
| 1 | 0 | 5 | set r0 to 0 |
4 | 0 |
| 2 | 1 | 4 | r0 becomes |
3 | 1 |
| 3 | 2 | 3 | jump to position 1 | 2 | 1 |
| 4 | 1 | 2 | r0 becomes |
1 | 2 |
| 5 | 2 | 1 | jump to position 1 | 0 | 2 |
| 6 | 1 | 0 | stop: budget exhausted | 0 | 2 |
Five instructions ran, which equals . The run did not return: the 2 in r0 is an interrupted calculation, not an answer, and the host must not read it as one.
Now change the rule so that it charges arithmetic but not jumps, and run the one-instruction program 0: goto -1, which jumps to position , itself. No instruction it executes is charged, so the counter stays at 5 and the run never stops.
Under the rule that charges only taken backward jumps, the three-instruction program runs longer on the same budget. Position 0 runs once and sets r0 to 0. Each pass through positions 1 and 2 then adds 1 to r0 and spends one unit at the jump, so five passes spend all 5 units and leave 5 in r0. Position 1 runs once more and makes r0 equal to 6, and the next jump finds the counter at 0 and stops the run. That is instructions, within the bound stated below, where is the length of the program.
The order of check and charge matters. Suppose a VM keeps the counter as a 32-bit unsigned integer, charges 3 units for a call, and subtracts first, stopping only if the counter is then 0. A call that arrives with 2 units left computes . Unsigned arithmetic wraps around, so the result is , not . That is not 0, so the run continues with about 4.3 billion units: at 50 nanoseconds per instruction, nanoseconds, about 215 seconds. A VM that checks first never subtracts more than the counter holds.
Stated precisely
Let be the budget, a whole number of units set before the run, and let be the total cost charged so far in the run. As long as nothing adds units during the run, the counter holds . A charge happens only when at least its cost remains, so at every moment. With a cost of 1 for every charged operation, a run therefore performs at most charged operations.
Under the rule that charges every taken backward jump, a program of instructions without calls executes at most instructions. Between two charged jumps the program counter §2.9, the position of the next instruction, only moves forward, so at most instructions run in that stretch. At most charged jumps happen, and they divide the run into at most such stretches.
The guarantee is that every run stops after charged work of at most units. It rests on five assumptions.
Coverage. Every way of running an instruction again passes a charge: a taken backward jump, a subcall, a tail call, and a helper that calls back into guest code.
No refill. Nothing adds units during the run: no subcall, tail call or helper resets the counter.
Check before charge. The counter never goes below 0 and never wraps around.
Uncharged work ends. Every helper returns. The budget counts calls, not what happens inside them.
Every engine. The interpreter and every compiled form of the program apply the same rule.
Limits and common mistakes
A budget bounds steps, not time. One charged helper call can take any amount of time, and the time per instruction depends on the processor and its clock. The Termination versus time bound §2.33 entry lists what a time bound needs in addition.
A budget limits one run, not the total load. A budget of 10,000 instructions per run at 1,000 runs per second still allows instructions per second. Limiting the total needs rate limiting §9.38: a cap on the runs or units allowed per unit of time.
Stopping a run does not undo it. Writes to a BPF map §2.18, a table that outlives the run, and other helper effects made before the counter ran out remain. If a decision must be all or nothing, the guest’s effects must be held back and applied only after a normal return.
A budget that is too small rejects correct programs. The host must choose from the longest run a legitimate program needs, and must decide what an exhausted run means for the event: allowing the event is failing open, rejecting it is failing closed.
An instruction budget is not the limit of a static checker. The eBPF verifier in Linux rejects a program whose analysis would examine more than 1 million instructions; that limits the work of checking a program when it is loaded, not the work of running it. Nor is a budget a CPU time §18.1.5 quota, which an operating system applies to a whole process.
Questions answered
Does fuel include branches, helper calls, and guest subcalls?
Fuel includes exactly the operations that the virtual machine’s counting rule charges, so the answer comes from that rule; for the budget to guarantee that every run stops, the rule must charge all three from one shared counter. Fuel, also called an instruction budget, is a counter that a virtual machine (VM) reduces as a guest program runs; the run stops when too few units remain.
Branches: every taken jump back must be charged, because every loop passes through one. If jumps are free, the eBPF (extended Berkeley Packet Filter) instruction
goto -1, a jump to itself, runs forever without spending fuel.Helper calls: the call instruction is charged, typically one unit, but fuel does not measure the host function’s own work, which runs as native code and needs its own bound in the helper’s contract. A helper that runs guest code repeatedly must charge those guest instructions to the same counter.
Guest subcalls: a call to another function of the same program is charged, and the called function spends from the caller’s counter. A fresh budget per call would let a loop that calls a function refill the counter on every pass and never stop; a tail call, which replaces the running program by another, must carry the count over in the same way.
Whether a particular runtime does all this can be settled only by reading every path in its code that runs guest instructions, including the paths for calls and callbacks §14.22.
Related entries: Termination versus time bound (§2.33), Ranking function (§3.25), Helper contract (§2.21), Fault-to-verdict mapping (§2.34), Resource confinement (§2.40), Rate limiting (§9.38).