Question
How can a sound verifier reject a program that is actually safe?
Short answer
Sound means only that every program the verifier accepts is safe, not that every safe program is accepted. A verifier does not run the program. It reasons about a simplified description of the states the program could reach, where a state is one combination of values of the program’s variables. That description may also contain states that can never occur. If an impossible state breaks a safety rule, the verifier cannot tell it from a real one and rejects the program. A rejection means “not proved safe”, not “proved unsafe”.
Terms in the question
Verifier: a program that examines another program before it runs, and accepts it only after showing that it obeys fixed safety rules on every possible input.
Safe: obeying those rules in every real execution. A typical rule is that the program never reads or writes memory outside the regions it was given.
Actually safe: safe in fact, whether or not anyone can prove it.
Sound: the verifier never accepts an unsafe program. Write for the set of accepted programs and for the set of safe programs. Soundness §3.7 says : every member of is a member of .
Complete §3.8: the other direction, . Every safe program is accepted. A verifier that is sound but not complete rejects some safe programs. Such a rejection is a false alarm §1.22.
Full answer
Soundness forbids only one of two mistakes. A verifier can accept an unsafe program, which is dangerous, or reject a safe program, which is an inconvenience. A sound verifier never makes the first mistake. Nothing in the definition limits the second.
An exact verifier cannot exist for a general language. Turing proved in a paper of 1936 that no algorithm §14.1 can decide, for an arbitrary program, whether the program ever prints a given symbol. Turing wrote of machines where this text says algorithm and program. The same holds for safety. A program in a language with unlimited loops and memory can be changed so that it makes a forbidden memory access exactly where it would have printed the symbol, and nowhere else. An algorithm that decided safety for the changed program would answer Turing’s question, so no such algorithm exists. For such a language, a verifier that always finishes and never accepts an unsafe program must therefore reject some safe programs.
A restricted language does not escape in practice. A program limited to a bounded number of steps and bounded memory has finitely many executions, so an exact check exists in principle. But a program that reads two 64-bit inputs already has , about , combinations of inputs. Trying them all is out of reach. Exact methods that do not try every input exist, but none is known that is fast for every program. Deciding safety exactly is at least as hard as deciding whether some choice of true and false for the variables of a logical formula makes the formula true, and no method is known that is fast for every formula.
So the verifier approximates. It replaces the exact set of reachable states §3.21 by a summary that is cheap to compute, such as “ is between 0 and 10”. Soundness requires the summary to cover every state the program can really reach. The summary may cover more. The theory of such summaries is called abstract interpretation §3.4.
The extra states cause the rejections. The verifier tests the safety rule against every state in the summary. If a state in the summary breaks the rule, the verifier cannot know whether the state is real or was added by the approximation. To stay sound it must reject.
A summary can lose facts in several ways. It can forget how two variables are related. It can merge the states of different paths into one. It may be unable to express the fact that makes a loop safe. A verifier may also limit its own work and reject a program when the limit is reached. The verifier that the Linux kernel applies to programs in eBPF §2.1 (extended Berkeley Packet Filter, now used as a plain name) is an example. eBPF programs are programs that the kernel accepts from outside the kernel and runs. The eBPF verifier §3.3 rejects a program once it has examined more than 1 million instructions along the program’s paths.
Worked example
A program receives a number , and an earlier test has limited to the whole numbers 0 to 10. The program copies into , computes , and reads entry of a table that has 4 entries, numbered 0 to 3. In every real execution equals , so . All 11 possible executions read entry 0. The program is safe.
A verifier that keeps one range for each variable, a method called interval analysis §3.9, proceeds as follows.
After the test, lies in the range from 0 to 10, written .
After the copy, lies in . The summary has no place to record that equals .
After the subtraction, the smallest possible result is the smallest minus the largest , which is . The largest possible result is . So lies in .
At the table read, the rule requires . The range contains 17 values outside 0 to 3. The verifier rejects the program.
The summary describes pairs of and . Only 11 of the pairs are real. Of the 121 pairs, 83 give an index outside 0 to 3, and each of the 83 is one of the 110 impossible pairs, such as with . The verifier accepted nothing unsafe, so it stayed sound. It failed to prove a true fact.
A verifier whose summaries can also record relations between variables keeps after the copy, derives , and accepts the program. A family of summaries that can record such relations is called a relational abstract domain §15.12. It costs more time and memory to compute than separate ranges.
Follow-up questions and limits
Is a rejection evidence of a bug? Not by itself. Three outcomes must be kept apart. Accepted means proved safe. Rejected with a concrete input that breaks a rule means proved unsafe. Rejected because the analysis could not decide means unknown.
How does an author get a safe program accepted? By making the missing fact visible to the verifier. In the example, a test that stops the program unless , placed before the table read, gives the verifier the range it needs, although the test never fails at run time.
Sound with respect to what? With respect to the verifier’s own model of how each instruction behaves. If the software that runs the program, or a function that the program calls, behaves differently from the model, an accepted program can still break a rule.
Is rejecting when unsure a defect? It is a deliberate choice, called conservative refusal §3.36. The alternative, accepting when unsure, gives up soundness. The price is that some useful programs are turned away and must be rewritten.
Fullest treatment: Abstract interpretation (§3.4).
Related entries: Completeness (§3.8), Conservative refusal (§3.36), Relational abstract domain (§15.12), Soundness (§3.7), Interval analysis (§3.9), eBPF verifier (§3.3), Halting problem, Rice’s theorem, and restricted verifiers (§19.2.2). Further reading: [2, 68, 158].