Section 3.28

SMT solver

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 α\alpha and β\beta satisfy 2β=α2\beta = \alpha and α>β+10\alpha > \beta + 10. A bounds checker asks whether some index ii with 0≤i<n0 \le i < n makes a 4-byte read at byte offset 4i+84i + 8 run past a buffer of 4n4n 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, x≤3x \le 3 and x≥5x \ge 5 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 x+1>xx + 1 > x is true for every mathematical integer, but false for the 32-bit unsigned machine integer x=4,294,967,295x = 4{,}294{,}967{,}295, where x+1x + 1 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:

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 TT:

  1. Replace every basic theory fact, such as x+1=0x + 1 = 0, by a yes-or-no variable. The result is a pure Boolean formula.

  2. The SAT engine finds a true-or-false assignment to these variables that satisfies the Boolean structure. If none exists, the answer is unsat.

  3. The theory solvers check whether the facts chosen true, and the negations of those chosen false, are consistent together.

  4. 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 ww-bit variable becomes ww Boolean variables, and each operation becomes a Boolean circuit, as in hardware. An adder needs a number of gates proportional to ww, a multiplier a number proportional to w2w^2, 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, x+1≤xx + 1 \le x, 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 x+1≤xx + 1 \le x would mean 1≤01 \le 0. 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 232−1=4,294,967,2952^{32} - 1 = 4{,}294{,}967{,}295. Check: 4,294,967,295+1=4,294,967,296=2324{,}294{,}967{,}295 + 1 = 4{,}294{,}967{,}296 = 2^{32}, and 232mod232=02^{32} \bmod 2^{32} = 0, and 0≤4,294,967,2950 \le 4{,}294{,}967{,}295. 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 −231-2^{31}, finds x=231−1=2,147,483,647x = 2^{31} - 1 = 2{,}147{,}483{,}647, whose increment wraps to −231-2^{31}. 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 TT fixes the meaning of a set of symbols, for example ++ and ≤\le over the integers. A formula φ\varphi with variables x1,…,xnx_1, \dots, x_n is satisfiable modulo TT if some assignment of values to x1,…,xnx_1, \dots, x_n, with every symbol of TT read in its fixed meaning, makes φ\varphi true; that assignment is a model. A formula ψ\psi is valid modulo TT, true for all values, exactly when ¬ψ\lnot\psi is unsatisfiable modulo TT, where ¬\lnot means “not”.

The solver’s contract:

Bit-vector meaning: a pattern of ww bits bw−1…b0b_{w-1} \dots b_0 denotes the unsigned value u=∑k=0w−1bk2ku = \sum_{k=0}^{w-1} b_k 2^k, between 0 and 2w−12^w - 1, and the signed two’s-complement value u−bw−12wu - b_{w-1} 2^w, between −2w−1-2^{w-1} and 2w−1−12^{w-1} - 1. Adding two patterns with unsigned values u1u_1 and u2u_2 gives the pattern of (u1+u2)mod2w(u_1 + u_2) \bmod 2^w. For w=8w = 8, the pattern 11111111 has u=255u = 255 and signed value 255−256=−1255 - 256 = -1, 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 2w2^w for a width of ww 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].