Section 2.32

Instruction budget (fuel)

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.

  1. Set. Before the run, the host sets the counter to the budget BB, a whole number such as 10,000. Normally every run starts with the full budget.

  2. 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.

  3. 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.

Three kinds of operation need an explicit decision, because each can make the budget guarantee nothing.

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 2+1−2=12 + 1 - 2 = 1.

0: r0 = 0
1: r0 += 1
2: goto -2

The budget is B=5B = 5 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 0+10 + 1 3 1
3 2 3 jump to position 1 2 1
4 1 2 r0 becomes 1+11 + 1 1 2
5 2 1 jump to position 1 0 2
6 1 0 stop: budget exhausted 0 2

Five instructions ran, which equals BB. 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 0+1−1=00 + 1 - 1 = 0, 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 1+5×2+1=121 + 5 \times 2 + 1 = 12 instructions, within the bound (B+1)×N=(5+1)×3=18(B + 1) \times N = (5 + 1) \times 3 = 18 stated below, where N=3N = 3 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 2−32 - 3. Unsigned arithmetic wraps around, so the result is 232−1=4,294,967,2952^{32} - 1 = 4{,}294{,}967{,}295, not −1-1. That is not 0, so the run continues with about 4.3 billion units: at 50 nanoseconds per instruction, 4,294,967,295×504{,}294{,}967{,}295 \times 50 nanoseconds, about 215 seconds. A VM that checks first never subtracts more than the counter holds.

Stated precisely

Let BB be the budget, a whole number of units set before the run, and let kk be the total cost charged so far in the run. As long as nothing adds units during the run, the counter holds B−kB - k. A charge happens only when at least its cost remains, so k≤Bk \le B at every moment. With a cost of 1 for every charged operation, a run therefore performs at most BB charged operations.

Under the rule that charges every taken backward jump, a program of NN instructions without calls executes at most (B+1)×N(B + 1) \times N instructions. Between two charged jumps the program counter §2.9, the position of the next instruction, only moves forward, so at most NN instructions run in that stretch. At most BB charged jumps happen, and they divide the run into at most B+1B + 1 such stretches.

The guarantee is that every run stops after charged work of at most BB units. It rests on five assumptions.

  1. 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.

  2. No refill. Nothing adds units during the run: no subcall, tail call or helper resets the counter.

  3. Check before charge. The counter never goes below 0 and never wraps around.

  4. Uncharged work ends. Every helper returns. The budget counts calls, not what happens inside them.

  5. 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 10,000×1,000=10,000,00010{,}000 \times 1{,}000 = 10{,}000{,}000 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 BB 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.

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).