Definition
Resource confinement is the set of limits that bound how much of each shared resource an untrusted guest program, code the host runs on someone else’s behalf, can consume or make the host consume for it: processor time, memory inside and outside the guest, persistent storage, input and output, energy, and shared objects such as locks. Each limit has a unit, an account it is charged to, a time window and a defined action when it is reached. It protects the host’s availability §1.10, which memory safety alone does not.
The idea
A sandboxed filter program runs once for every received packet. It is memory-safe: it never touches memory outside its 512-byte stack. It can still call a hashing helper function §2.19, a host routine that guest programs may call, as often as its instruction limit allows; insert entries into a shared table until the table is full; write a log record to flash memory §6.6 on every run; or send a radio message on every run. None of this allocates one byte of guest memory. The work happens in host code that runs on the guest’s behalf, and the host pays in processor time, RAM, flash wear §8.17 and battery charge.
A hotel room is a fair picture. Locking the guest into the room is memory safety; it does not stop the guest from running every tap all day. Resource confinement puts a meter on every pipe that leaves the room and closes the pipe when the meter reaches its limit. The picture stops being accurate in one respect: a hotel bills afterwards, but a host must refuse before the resource is spent, because spent processor time and worn flash cannot be returned.
How it works
List every resource the guest can reach. Directly: instructions and stack. Through each helper: the processor time the helper uses, entries in maps (key-value tables the host keeps for guest programs), buffers, timers, writes to storage and messages sent. Indirectly: actions that cause further runs of a guest, and log output that another part of the system must store or send.
Fix five things for each resource: the unit (instructions, cycles, bytes, entries, erase cycles, messages); the account charged (this guest, its owner, or a shared pool); a limit per invocation, meaning per run of the guest; a limit per time window, meaning a rate; and the action at the limit (the helper returns an error, the run ends with a fault, or the request is dropped).
Charge before consuming, where the consumption happens. The check sits inside the helper or allocator that spends the resource, and every path to the resource passes it, the principle called complete mediation §1.16.
Charge helper work to the same budget as instructions. An instruction budget (fuel) §2.32 counts guest instructions, but one helper call is one instruction that may do thousands of cycles of work. Each call is charged in proportion to that work, for example per byte processed.
Bound how often the guest runs. A per-invocation limit says nothing about invocations per second. A token bucket §19.4.1 bounds that rate: a counter gains tokens at a fixed rate up to a maximum, each run spends one token, and a run that finds no token is refused or deferred.
Release on every exit. What a run holds is returned when the run faults or is canceled, not only when it succeeds, so failed runs cannot pile up held state.
These limits are caps: they forbid consuming more. A cap promises no service at all, to the guest or to anyone else; a promise of service is a reservation, and the resource cap versus resource reservation §13.22 entry treats the difference.
Worked example
A microcontroller §6.1 runs at 64 MHz (megahertz), that is 64,000,000 cycles per second, and its interpreter §2.13 needs at most 50 cycles per guest instruction. The instruction budget is 4,000 instructions per invocation, so the instructions of one run cost at most cycles. Dividing by 64,000,000 cycles per second gives seconds, or 3.125 milliseconds.
Helper work. A helper hashes up to 1,024 bytes at 25 cycles per byte, cycles per call. Setting up its arguments and calling it takes 4 guest instructions, so one run can make calls. The helper work is then cycles, 0.4 s, and the run costs cycles, 129 times the instruction-only figure. The repair charges the helper to the budget at one unit per 50 cycles of work, which is one unit per 2 hashed bytes. A 1,024-byte hash then costs 512 units, and a whole call costs units for cycles. Every unit now stands for at most 50 cycles, so a run again costs at most cycles.
Rate. Packets arrive at up to 200 per second, and each starts one run: ms of processor time per second, 62.5% of the processor, although every single run keeps to its budget. A token bucket that gains 20 tokens per second and holds at most 5 admits at most runs in any one second, which is ms, about 7.8% of the processor.
Storage. A logging helper appends a 32-byte record to a flash log of 4 pages of 4,096 bytes, bytes in all. Assume an endurance rating of 10,000 erase cycles per page. At 200 runs per second the guest writes bytes per second, fills the log every s and erases each page once per fill. At that rate each page reaches its rated cycle count after s, about 7.1 hours. Individual pages can continue working beyond that rating.
Memory. A shared map with room for 1,000 entries, each an 8-byte key, a 64-byte value and an assumed 16 bytes of bookkeeping, holds bytes, about 34% of a 256 KiB (262,144-byte) RAM. One guest that fills the map leaves no entry for any other guest. In none of these cases did the guest allocate guest memory.
Stated precisely
Per invocation. Let be the budget in units. Each operation , an instruction or a helper call, is charged units before it runs. Let be the number of cycles that one unit stands for, and let be the worst-case number of cycles of operation ; require . If the runtime refuses every operation whose charge exceeds the remaining budget, the operations of one run cost at most
Over time. If a token bucket with a rate of runs per second and a depth of tokens admits the runs, at most runs start in any interval of seconds, and those runs cost at most cycles. With , , , and , the bound is cycles, 78.125 ms at 64 MHz.
Held state. The bytes retained at any moment are at most the sum, over the guest’s quotas, of each entry limit times the bytes per entry, bookkeeping included.
These bounds hold under five assumptions: every path that consumes the resource charges it; each charge is at least the worst-case cost; the charge is made before the cost is spent; every exit path releases what the run held; and the guest cannot reset its counters or raise its own limits.
Limits and common mistakes
A per-invocation budget is not a rate limit: the rate example above stays within budget on every run and still takes 62.5% of the processor.
Counting instructions is not measuring time. Helpers, interrupts §6.11 and memory waits add time that an instruction count does not see; the termination versus time bound §2.33 entry treats this.
A cap gives no progress guarantee. A guest within every cap can still miss its deadline because other work held the processor.
Refusing costs something too. Parsing and authenticating a request that is then refused takes processor time, so rejected requests need their own rate limit, applied before the expensive checks.
A shared pool with one overall limit lets one guest starve the others; per-guest quotas prevent that.
Holding is consuming. A helper that takes a lock or waits for an event lets a guest delay host threads while using almost no processor time. Helpers called by guests should not wait, or their hold time must be bounded.
One guest action can trigger further events and further runs, which multiplies consumption beyond what one run’s budget suggests.
Questions answered
Which resources can a guest consume without allocating guest memory?
A guest program, code a host runs inside a sandbox §2.30 on someone else’s behalf, can consume almost every host resource without allocating guest memory, because the consumption happens in host code that runs for it.
Processor time: its own instructions up to its budget, and the work inside helper functions it calls, which can be thousands of cycles per call.
Host memory: entries it inserts into shared tables (maps), and buffers, queued messages and timers that helpers create for it.
Persistent storage: both space and the limited erase cycles of flash memory, through logging or storage helpers.
Input and output: radio or network messages, the bandwidth they occupy and the energy they use.
Shared objects: locks held through helpers, slots in fixed-size tables, identifiers and handles.
Further runs of itself or of other guests, triggered by events it causes.
Each needs its own limit, with a unit, an account and a rate, charged before the resource is spent. A per-invocation instruction budget bounds none of them on its own: 200 runs per second that each stay within budget can still take 62.5% of a processor, and one 32-byte log record per run can wear out a 16 KiB flash log in about 7 hours.
Related entries: Instruction budget (fuel) (§2.32), Termination versus time bound (§2.33), Denial of service (DoS) (§1.28), Resource cap versus resource reservation (§13.22), Rate limiting (§9.38), Token bucket: rate, burst, and the arrival envelope (§19.4.1), Helper contract (§2.21), Sandbox (§2.30).