Full name: Satisfiability modulo theories solver.
Definition
An SMT solver, short for satisfiability modulo theories solver, is a program that decides whether a logical formula is satisfiable, that is, whether some values of its variables make it true, when the symbols in the formula have the fixed meanings of background theories such as integer arithmetic, fixed-width bit-vectors or arrays §14.5. It answers sat together with a model, values that make the formula true; unsat, meaning that no such values exist; or unknown. A SAT (Boolean satisfiability) solver handles only true-or-false variables; an SMT solver adds the theories.
The idea
Many questions in program analysis have the form “do values exist such that …?”. Symbolic execution §3.27 asks whether some inputs and satisfy and . A bounds checker asks whether some index with makes a 4-byte read at byte offset run past a buffer of bytes. A verifier proves that a property holds for all values by asking whether its negation can be satisfied: “no” is a proof, and “yes” comes with a counterexample.
An SMT solver splits such a question between two kinds of reasoning. A SAT engine handles the logical structure: which of the basic facts, combined with “and”, “or” and “not”, to make true. Theory solvers handle the meaning of the basic facts: whether, say, and can hold together over the integers. The SAT engine proposes, the theory solvers check, and every rejection teaches the SAT engine a new rule. “Modulo theories” means “with respect to the theories”.
The theories decide what the formula means. The text is true for every mathematical integer, but false for the 32-bit unsigned machine integer , where wraps around to 0. The solver answers about the formula it is given, never about the program behind it.
How it works
The input is a list of declarations and assertions, usually in SMT-LIB (Satisfiability Modulo Theories Library), the standard text format that most solvers read. Each variable has a sort, the solver’s word for a type. Common theories:
Linear integer and real arithmetic: addition, subtraction, multiplication by constants, comparisons. Integers are unbounded and never wrap.
Nonlinear arithmetic: products of variables. Over the integers it is undecidable §19.2.1 in general, so a solver may answer unknown.
Fixed-width bit-vectors: values are patterns of bits; addition, subtraction and multiplication wrap modulo ; signed and unsigned comparisons §2.12 are separate operations (
bvslt,bvult); shifts and bitwise operations §4.50 are included.Arrays: reads element of array , and is the array with element replaced by ; together they model memory.
Uninterpreted functions: a function symbol about which the solver knows only that equal arguments give equal results, which models a computation whose details are unknown.
Many solvers combine the two kinds of reasoning in a scheme called DPLL(T), after the Davis–Putnam–Logemann–Loveland (DPLL) search procedure for SAT, combined with a theory :
Replace every basic theory fact, such as , by a yes-or-no variable. The result is a pure Boolean formula.
The SAT engine finds a true-or-false assignment to these variables that satisfies the Boolean structure. If none exists, the answer is unsat.
The theory solvers check whether the facts chosen true, and the negations of those chosen false, are consistent together.
If they are, the answer is sat, and the theory solvers supply the values of a model. If not, the theory solver names a small inconsistent set of facts, a clause (an “or” of facts and negated facts) forbidding that combination is added to the formula, and the procedure returns to step 2.
The SAT engine itself learns in the same way from its own dead ends, a technique called conflict-driven clause learning.
Bit-vector formulas are often solved by bit-blasting: each -bit variable becomes Boolean variables, and each operation becomes a Boolean circuit, as in hardware. An adder needs a number of gates proportional to , a multiplier a number proportional to , which is one reason why multiplications of 64-bit values make formulas slow. Widely used solvers include Z3 §16.10 from Microsoft Research and cvc5.
Worked example
Question: is x + 1 > x true for every 32-bit unsigned x? Ask whether its negation, , can be satisfied. With mathematical integers restricted to the range of the type, in SMT-LIB:
(declare-const x Int)
(assert (and (<= 0 x) (<= x 4294967295)))
(assert (<= (+ x 1) x))
(check-sat)
The answer is unsat, since would mean . The integer model concludes that the comparison is always true.
With 32-bit bit-vectors:
(declare-const x (_ BitVec 32))
(assert (bvule (bvadd x #x00000001) x))
(check-sat)
(get-model)
Here bvadd is wrapping addition, bvule is unsigned less-than-or-equal, and #x00000001 is the 32-bit pattern for 1 in hexadecimal. The answer is sat, with the model x = #xffffffff, which is . Check: , and , and . So x + 1 > x is false for this x. The bit-vector model finds the case that the integer model ruled out, and for the C type unsigned int on a machine with 32-bit int it is the correct model.
For the signed type int the answer changes again. A 32-bit two’s-complement bit-vector model, in which the top bit counts as , finds , whose increment wraps to . In C, however, signed overflow §4.15 is undefined behavior §4.12: the program has no defined result, and compilers may compile x + 1 > x as always true. Neither wrapping bit-vectors nor unbounded integers describe C here. A faithful encoding adds a separate query that asks whether the addition can overflow, and reports a satisfiable answer as an error.
Stated precisely
A theory fixes the meaning of a set of symbols, for example and over the integers. A formula with variables is satisfiable modulo if some assignment of values to , with every symbol of read in its fixed meaning, makes true; that assignment is a model. A formula is valid modulo , true for all values, exactly when is unsatisfiable modulo , where means “not”.
The solver’s contract:
sat: the returned model makes true. Anyone can check this by evaluating at the model.
unsat: no model exists. This answer is trusted, unless the solver emits a proof that an independent checker accepts.
unknown: no conclusion, because of a time or memory limit or an incomplete method, for example for nonlinear integer arithmetic or for formulas with quantifiers (“for all”, “there exists”).
Bit-vector meaning: a pattern of bits denotes the unsigned value , between 0 and , and the signed two’s-complement value , between and . Adding two patterns with unsigned values and gives the pattern of . For , the pattern 11111111 has and signed value , so it is greater than 00000001 as an unsigned value but less than it as a signed value. Quantifier-free formulas in each of the theories listed above, except nonlinear integer arithmetic, are decidable. Nonlinear integer arithmetic is not: no algorithm §14.1 decides whether a polynomial equation has integer solutions (Matiyasevich, 1970) [267].
Limits and common mistakes
The solver answers about the formula, not the program. An unsat answer for a formula that models 32-bit values as unbounded integers says nothing about overflow, as the example shows.
Unknown is not unsat. A timeout §9.34 must never count as “the property holds”.
Model the width at which the language computes. In C, two 8-bit unsigned values are first converted to int by integer promotion §4.18, so with x = 255 the expression x + 1 is 256, not 0; the value wraps only when it is stored back into an 8-bit variable.
Theories define corner cases in their own way. In SMT-LIB a left shift by the full width or more gives 0, and unsigned division by 0 gives the pattern of all ones. In C both are undefined behavior, and a 32-bit shift instruction on x86 processors uses only the low 5 bits of the count, so shifting 1 left by 32 yields 1. The encoder must state which behavior it means.
Performance is hard to predict. A small change in the formula, the solver version or its random seed can move the solving time from milliseconds to hours, and wide multiplications and divisions over bit-vectors are often slow.
Questions answered
Should machine integers be modeled as mathematical integers or bit-vectors?
Model them as bit-vectors of the width at which the program computes, unless it has been proved that no operation leaves the type’s range. A machine integer is a fixed-width bit pattern: its arithmetic wraps modulo for a width of bits, and its signed and unsigned readings compare differently. Mathematical integers never wrap, so they silently assume that no overflow occurs. With them a solver proves x + 1 > x for every x, while a 32-bit unsigned x = 4294967295 gives x + 1 = 0. Bitwise operations, shifts, conversions between widths and memory viewed as bytes need bit-vectors in any case.
Mathematical integers are correct where a separate check has shown that every operation stays inside its type’s range; then both models agree, and integer reasoning is often faster for wide linear arithmetic. The language’s rules matter as much as the hardware’s: C unsigned arithmetic wraps, which bit-vectors match, but C signed overflow is undefined behavior, which neither model captures, so a faithful encoding first proves that no signed overflow occurs. A solver cannot correct a wrong choice afterwards, because it answers about the formula it was given.
Related entries: Symbolic execution (§3.27), Bounded model checking (§3.29), Signed versus unsigned comparison (§2.12), Bit-vector logic (§15.15), Unsigned modular arithmetic (§4.16), Signed integer overflow (§4.15), Integer promotion and conversion (§4.18). Further reading: [59, 181, 267].