Definition
Fault injection is a testing technique that deliberately causes chosen faults while a system runs, such as a failed memory allocation, a storage write error, a lost or corrupted message, a reset or a power cut, and checks that the system then does what its requirements say: report the error, keep its data consistent, release what it holds, or recover. Which faults to inject, and where, when and how often, comes from a fault model §1.30: a written list of the faults that the system is claimed to tolerate.
The idea
Error-handling code is the code that normal tests run least. On a test machine malloc almost never returns a null pointer §4.26, storage writes almost never fail, and the power never drops in the middle of a write. Yet that code decides what a device does when something goes wrong in the field. A function that leaks a buffer whenever its third allocation fails passes every normal test.
Fault injection makes the rare event happen on demand, at a chosen place and moment. A fire drill is a fair picture: nobody waits for a fire to test the exits. The picture also shows the limit: a drill tests the staged fire, and a real fire may block a different exit. A campaign likewise says nothing about faults it did not inject.
The taxonomy of Avižienis, Laprie, Randell and Landwehr (2004) [492] separates three words. A fault is the cause, such as a failed allocation. An error is the wrong internal state that the fault produces, such as a buffer that is still allocated but no longer referenced. A failure is the event in which the service that users see departs from correct service. Fault injection supplies the fault and checks that the error does not become a failure.
How it works
Write the fault model. For each fault, state its kind (error return, timeout §9.34, corrupted value, lost, repeated or reordered message, reset, power loss), its location (call site, component or storage write), its trigger (the -th call, a moment, or a state such as ‘while a flash memory §6.6 page is being written’), its persistence (transient, happening once, or permanent, continuing from then on) and its multiplicity (one fault per run, or a second fault while the first is being handled).
Inject. In software, replace the dependency with a test double §10.35, a stand-in that fails on command, such as an allocator that fails its -th call; the Linux kernel has a fault-injection facility of this kind. A network proxy drops, delays or corrupts messages. In hardware, a controlled switch cuts the supply at a chosen moment, or a pin drives the reset line, which is called reset injection §8.37.
Define the test oracle §10.5, the rule that decides whether the response was correct: the caller received the error, a stated invariant still holds, every resource taken by the failed operation was released, and stored data is the old version or the new one, never a mixture.
Sweep. Count the calls to each dependency that can fail during a fault-free run. Rerun with the first call failing, then the second, and so on, until a run completes without reaching the call chosen to fail. Repeat the sweep for each persistence.
Record the scenarios run, those reachable but not run, and those the model excludes.
The database library SQLite publishes such a sweep for memory allocation. A test allocator fails the first allocation, then the second, and so on, until the operation completes without meeting the failure. Each test runs twice: once with the allocator failing a single time and then working again, and once with it failing on every call after the first failure. Tests of input and output errors follow the same pattern.
Worked example
An operation open_session makes three allocations in order: , a 64-byte session object; , a 256-byte receive buffer; , a 128-byte cryptographic context. On any failure it must return an error and free everything allocated so far. The fault model allows one transient allocation failure per run. The operation returns at the first failure, so the sweep has runs:
: fails, and nothing needs freeing.
: fails, and must be freed.
: fails, and and must be freed.
: there is no fourth allocation, so the normal path runs and the sweep ends.
The oracle compares the number of live allocations before and after each failed call; the difference must be 0.
Run 3 finds a defect: the error path frees but forgets , which leaves a difference of one 64-byte allocation, and nothing crashes. A 32 KiB heap holds bytes, so it is exhausted after such failures, ignoring the allocator’s own overhead. Normal tests never make fail, so only the injected fault reveals this memory leak §4.8.
Change one assumption and the count changes. If the code attempted all three allocations before checking any result, each site could succeed or fail independently: combinations. With the early return, the outcome at a site after a failure never matters, so the 8 combinations collapse into the 4 runs above. If the code retried a failed allocation once, the retry would absorb a transient fault, and only a permanent fault would reach the error path.
An error return is not a power cut. An injected error returns control to code that can clean up. A power cut stops the processor in the middle of an operation, loses the contents of RAM, may leave a flash memory page half written, and hands control to the boot code. That case needs power-loss injection §8.38, which tests crash consistency §8.4.
Stated precisely
Let a scenario be a tuple of fault kind , location , trigger , persistence and multiplicity . The fault model is the set of scenarios that the claim covers. Let be the reachable scenarios, those that the code’s control flow can actually produce, and the scenarios that the campaign ran. The campaign supports exactly this claim: for every scenario in , the oracle held. A complete report gives out of , lists the scenarios in as infeasible with the reason, and makes no claim about any scenario outside .
For one operation with call sites that can fail, reached in a fixed order and returning at the first failure, single transient faults give runs, the fault-free run included. If every site is always reached and the sites fail independently, . Three assumptions carry the claim from injected faults to real ones.
Fidelity: an injected fault leaves the system in the state that the real fault would leave. An error returned before a write is not a write torn by power loss.
Determinism: repeated runs make the same sequence of calls that can fail, so that ‘the -th call’ names the same site every time.
Observability: the oracle detects every violation it is meant to detect, such as a leak or a broken invariant.
Limits and common mistakes
A campaign proves nothing about faults outside its model. A system tested against single faults may fail on a second fault during recovery, for example a write error inside the cleanup that handles an allocation failure.
Fidelity is the usual weak point. A stub that returns an error code does not reproduce a dependency that reports success and stores garbage, a write that completes halfway, or a reply that arrives after the caller gave up.
The injector disturbs the system. Wrappers, probes and logging change timing and memory layout, so a defect that depends on timing may hide during injection.
If the order of calls varies between runs, for example because several threads allocate at the same time, ‘the -th call’ no longer names a fixed site, and scenarios must be named by site instead.
A Byzantine fault §1.31, in which a component may behave in any way at all, including lying consistently, lies outside any model of error returns and needs a different fault model and different defenses.
In hardware security, fault injection also names an attack: an attacker disturbs a chip’s supply voltage, clock or electromagnetic surroundings so that it skips or corrupts an instruction, such as the comparison in a signature check. Robustness against that attack needs its own fault model.
Questions answered
Which failure locations and combinations are part of the model?
Only the failure locations and combinations that the fault model lists explicitly are part of the model; nothing belongs to it by default, and a test campaign supports claims only about the listed cases that it actually ran. A fault model is the written list of faults that a system is claimed to tolerate. To build the list:
Name every location where a fault can occur: each call site of each dependency that can fail (allocation, storage write, message receipt), each persistent write, and each point in a multi-step operation at which a reset or a power cut can occur.
For each location, name the fault kinds, the trigger (which call or which moment) and the persistence (once, or from then on).
State the combinations: single faults only, a second fault while the first is handled, or every pair.
Remove the combinations that control flow makes impossible, and say why.
For example, an operation with 3 allocations that returns at the first failure has 4 reachable runs under single transient faults: one per allocation plus the fault-free run. If all 3 allocations were always attempted, the independent combinations would number . The report should state the reachable cases, the cases run and the cases excluded, and it should keep apart an error return, a reset and a physical power interruption, because each leaves a different state behind.
Related entries: Reset injection (§8.37), Power-loss injection (§8.38), Test oracle (§10.5), Model-based testing (§10.34), Fault model (§1.30), Crash consistency (§8.4), Memory leak (§4.8), Byzantine failure (§1.31), Mock versus stub versus fake (§10.35). Further reading: [492].