core/fault
skip to the library navigationShared: available on every enabled target.
Deterministic allocation-failure injection over any allocator, with cumulative counters.
over wraps a copy of a provider's handle in an injector over the caller's space. permit replaces the budget without resetting evidence.
The budget counts delegated attempts, including attempts the inner provider refuses. So wrapping an arena counts both injected failures and the arena's own.
- Free calls delegate to the parent.
- An allocation, a resize and a growth in place are each an attempt: each spends the budget, and once it is spent the injector refuses without asking the parent.
- A refused growth, the injector's or the parent's, leaves the block as it was, and the container moves it instead. So a container tested through an injector takes the in-place path its provider offers.
successescounts the blocks a delegated allocation or resize gave. A growth in place gives none and is counted bygrowths.
Items
Functions 11
Also on this page
public injector: type (inner_provider: type) = injector_value(inner_provider)
An allocator wrapper that injects failures deterministically and keeps cumulative counters. It is a handle: copies share one budget. It holds a copy of its inner provider's handle and delegates to it the allocations, resizes and in-place growths the budget permits, and every free.
public space: type = counters
Caller-owned state of an injector: its budget and cumulative counters. Declare it zeroed and give it to over, which fills it; it must outlive the injector handle.
public attempts: (inner_provider: type,
memory: injector(inner_provider)) -> (count: usize)
Return all attempts, including injected and inner failures.
public delegated: (inner_provider: type,
memory: injector(inner_provider)) -> (count: usize)
Return the attempts passed through to the inner provider.
public frees: (inner_provider: type,
memory: injector(inner_provider)) -> (count: usize)
Return the number of delegated free calls.
public growths: (inner_provider: type,
memory: injector(inner_provider)) -> (count: usize)
Return the number of blocks the inner provider grew where they are.
public injected_failures: (inner_provider: type,
memory: injector(inner_provider))
-> (count: usize)
Return attempts rejected by the wrapper before calling the inner provider.
public inner_failures: (inner_provider: type,
memory: injector(inner_provider)) -> (count: usize)
Return allocation failures reported by the inner provider.
public live: (inner_provider: type,
memory: injector(inner_provider)) -> (count: usize)
Return successful allocations minus free calls, bounded below by zero. Exact under the allocator contract; malformed or duplicate frees are not validated here.
public over: (inner_provider: type is mem.allocator, inout state: space,
inner: inner_provider, allowed_attempts: usize)
-> (handle: injector(inner_provider) from state, inner)
Fill state for an injector over inner with a budget of allowed_attempts allocation, resize and in-place growth calls that may reach it, and return its handle. An inner failure consumes one of them too; an injected failure never calls the inner provider and counts only in attempts and injected_failures.
public permit: (inner_provider: type, memory: injector(inner_provider),
allowed_attempts: usize) -> none
Replace the future delegation budget without resetting the counters, so failed and successful calls stay distinguishable across retries.
public remaining: (inner_provider: type,
memory: injector(inner_provider)) -> (count: usize)
Return how many attempts may still reach the inner provider.
public successes: (inner_provider: type,
memory: injector(inner_provider)) -> (count: usize)
Return the number of blocks a delegated allocation or resize gave; a growth in place gives none and is counted by growths.
Example program
This complete program is one of the compiler's runtime tests. View source.
The program, 29 lines
import core/arena import core/fault import core/mem exercise: () -> (ok: bool) ! mem.out_of_memory = ok = false mut backing: [32]u8 = zeroed mut arena_space: arena.space = zeroed memory := arena.over_unchecked(arena_space, addr backing[0], 32) mut space: fault.space = zeroed counted := fault.over(space, memory, usize(1)) address := try mem.allocate(counted, usize(4), usize(1)) address.val = 42 return when address.val <> 42 _ = mem.allocate(counted, usize(4), usize(1)) else (problem) _ = problem ok = fault.successes(counted) == 1 and fault.injected_failures(counted) == 1 mem.release(counted, address, usize(4)) return end end exercise public main: () -> (code: i32) = code = 1 ok := exercise() else false if ok then code = 42 end if end main