Understanding Formal Verification Through Truth Tables
I find truth tables to be a particularly nice way of developing an intuition for formal verification without getting into the mechanics of it.
In this post, we’ll use a simple Python function to explore how ordinary program behaviour can be expressed as logical propositions and how those propositions can help us reason about correctness.
Modelling program behaviour
Consider a fairly ordinary piece of Python code that retrieves a user from a database, with a cache in front of it.
def get_user(id, nocache):
if not nocache:
user = cache.get(id)
if user is not None:
return user
return database.get(id)
Suppose we want to verify that whenever nocache is true, the return value comes from a call to database.get().
Let’s begin by defining a few boolean variables to represent different aspects of our program’s execution:
N: the value of nocache.C: whether the cache is called.H: whether the cache lookup is a hit, returning a non-None value.D: whether the database is called.R: whether the return value comes from the cache.
To understand why we’re doing this, let’s start with just the first two lines of our function and examine the corresponding truth table:
if not nocache:
user = cache.get(id)
| Nnocache | Ccache call | Possible execution? | Why |
|---|---|---|---|
| F | F | F | The cache should have been called |
| F | T | T | |
| T | F | T | |
| T | T | F | The cache should have been bypassed |
Only two of these assignments are consistent with our implementation. We can capture this relationship using the following expression:
C ↔ ¬N – cache.get() is invoked if and only if nocache is false.
We can capture the remaining relationships in the same way:
R ↔ (C ∧ H) – A value is returned from the cache if and only if the cache is consulted and the lookup returns a non-None value.
D ↔ ¬R – The database is consulted if and only if we haven’t returned a cached value.
Combining these expressions, we obtain a formula that determines whether a given assignment is consistent with our program’s behaviour:
P = (C ↔ ¬N) ∧ (R ↔ (C ∧ H)) ∧ (D ↔ ¬R)
Of the 32 possible assignments of these five variables, only four satisfy P:
| Nnocache | Ccache call | Hhit | Rfrom cache | Ddb call | Pprogram |
|---|---|---|---|---|---|
| F | T | F | F | T | T |
| F | T | T | T | F | T |
| T | F | F | F | T | T |
| T | F | T | F | T | T |
Every other assignment describes something our program cannot do, such as consulting the cache when nocache is true.
We now have a logical representation of our program’s possible executions, at a level of abstraction suited to the properties we wish to verify.
Verifying a property
Recall the property we want to verify:
Whenever the cache is bypassed, the returned value must originate from the database.
Since D indicates that the database is consulted, we can express our requirement as:
N → D – If nocache is true, the database must be consulted.
Rather than trying to establish that the requirement holds for every possible execution, let’s look for an execution that violates it.
The negation of our requirement is:
¬(N → D)
We are interested in whether there exists an assignment that satisfies both the program and the violation of our requirement:
P ∧ ¬(N → D)
Let’s add a column for ¬(N → D) to our truth table:
| Nnocache | Ccache call | Hhit | Rfrom cache | Ddb call | Pprogram | ¬(N → D) |
|---|---|---|---|---|---|---|
| F | T | F | F | T | T | F |
| F | T | T | T | F | T | F |
| T | F | F | F | T | T | F |
| T | F | T | F | T | T | F |
¬(N → D) is false in every row where P holds, so P ∧ ¬(N → D) is unsatisfiable.
We have established that our requirement holds for every execution represented by our model.
More generally, given a formula P representing a program and a formula Q representing a requirement, verification amounts to checking whether P ∧ ¬Q is satisfiable.
If it is not, then P → Q is valid – every assignment that satisfies our program also satisfies our requirement.
Introducing a bug
Let’s see if this can catch a bug.
Suppose someone changes the implementation while refactoring the function.
def get_user(id, nocache):
user = cache.get(id)
if user is not None:
return user
return database.get(id)
The nocache parameter is no longer used.
Our mathematical representation changes accordingly:
P = C ∧ (R ↔ (C ∧ H)) ∧ (D ↔ ¬R)
Our truth table now looks like this:
| Nnocache | Ccache call | Hhit | Rfrom cache | Ddb call | Pprogram | ¬(N → D) |
|---|---|---|---|---|---|---|
| F | T | F | F | T | T | F |
| F | T | T | T | F | T | F |
| T | T | F | F | T | T | F |
| T | T | T | T | F | T | T |
Asking the same question as before:
P ∧ ¬(N → D)
We now have a satisfying assignment, the highlighted row above, which is a counterexample to our requirement.
Where SAT solvers come in
So far, we’ve been constructing truth tables and manually searching for assignments that satisfy our expressions.
This is essentially the boolean satisfiability problem, commonly referred to as SAT.
Given a boolean formula, a SAT solver determines whether there exists an assignment of variables that makes the formula true.
The reason we need a solver becomes apparent as the number of variables grows.
| Boolean variables | Possible assignments |
|---|---|
| 5 | 32 |
| 10 | 1,024 |
| 20 | 1,048,576 |
| 30 | 1,073,741,824 |
| 50 | 1,125,899,906,842,624 |
Going beyond boolean variables
There is no particular reason to restrict our mathematical representation to booleans either.
Consider a function that calculates the delay between retry attempts.
def retry_delay(attempt: int) -> int:
delay = attempt * 10
if delay > 60:
return 60
return delay
Suppose we want to verify that the returned delay never exceeds 60 seconds.
We could introduce boolean variables for the branch conditions, but doing so would discard information about the actual numerical values and the arithmetic relationships between them.
Instead, we introduce integer variables:
A: the attempt number.D: the calculated delay.R: the returned delay.
The implementation can be expressed as:
P = (D = 10 * A) ∧ ((D > 60) → (R = 60)) ∧ ((D ≤ 60) → (R = D))
We can now ask questions about it:
P ∧ (R > 60)– Can the returned delay exceed 60 seconds?P ∧ (R < 0)– Can the returned delay be negative?P ∧ (A ≥ 0) ∧ (R < 0)– If negative attempt numbers aren’t valid inputs, can the returned delay still be negative?
Our variables now range over infinitely many values, so a truth table is out of the question. SMT (Satisfiability Modulo Theories) solvers extend satisfiability solving to mathematical theories such as integer arithmetic, real arithmetic, arrays, strings and bit vectors.
Here are the same three questions in SMT-LIB 2, the standard input format for SMT solvers:
(declare-const A Int) ; attempt number
(declare-const D Int) ; calculated delay
(declare-const R Int) ; returned delay
; P = (D = 10 * A) ∧ ((D > 60) → (R = 60)) ∧ ((D ≤ 60) → (R = D))
(define-fun P () Bool
(and (= D (* 10 A))
(=> (> D 60) (= R 60))
(=> (<= D 60) (= R D))))
; Can the returned delay exceed 60? P ∧ (R > 60)
(push 1)
(assert P)
(assert (> R 60))
(check-sat) ; unsat: the delay never exceeds 60
(pop 1)
; Can the returned delay be negative? P ∧ (R < 0)
(push 1)
(assert P)
(assert (< R 0))
(check-sat) ; sat: a negative delay is possible
(get-model) ; shows the assignment, e.g. A = -1
(pop 1)
; With the precondition: P ∧ (A ≥ 0) ∧ (R < 0)
(push 1)
(assert P)
(assert (>= A 0))
(assert (< R 0))
(check-sat) ; unsat: not with non-negative attempts
(pop 1)
Try it yourself in Z3.
Closing thoughts
In this post, we modelled function calls, where a return value comes from and numerical bounds. The same approach extends to the order in which operations happen, how often they happen, or the state of a resource: whether every acquired lock is released, whether every database mutation is preceded by authorization, whether sensitive data ever reaches a logger, or whether our code can satisfy the preconditions of a CVE in one of our dependencies.
However, being able to express a property is not the same as being able to prove it. Loops, recursion, concurrency, external systems and unbounded state make both the translation and the verification considerably harder, and some properties of arbitrary programs are undecidable.
We also derived every model by hand. A practical tool would need to automate that translation, rely on supplied specifications, or combine the two. Either way, the encoding matters as much as the solver: if the model leaves out a possible execution, we can end up proving something that isn’t true of the code.