Section 16.10

Z3

Definition

Z3 is an SMT solver §3.28, short for satisfiability modulo theories solver: a program that decides whether a logical formula can be made true when its symbols have fixed mathematical meanings, such as those of integer arithmetic or of fixed-width machine arithmetic. Given a formula, Z3 answers sat together with a model, values that make the formula true; unsat, meaning that no such values exist; or unknown, meaning that it reached no conclusion. Microsoft Research created Z3 and publishes it as open source. The name is not an abbreviation.

The idea

A programmer wants to know whether any input can make the comparison x + 1 > x false. Testing tries some inputs. A solver considers all of them at once: write the assumptions about the input, what the code computes and the bad outcome as one formula, and ask whether all of it can be true together. If the answer is unsat, no input reaches the bad outcome. If it is sat, the model is a concrete input that does, a counterexample to run and inspect. Tools for symbolic execution §3.27 and bounded model checking §3.29 build such formulas from code automatically.

The three answers resemble court verdicts. Sat is a conviction with the evidence attached; unsat is a proven alibi; unknown is a hung jury, which is not an acquittal. The picture stops at retrial: rerunning the query with more time or other settings can turn unknown into sat or unsat, but a correct solver never turns sat into unsat.

How it works

A Z3 query has four steps.

  1. Declare variables with sorts, Z3’s word for types. Int is a mathematical integer with no size limit. A BitVec of width ww is a ww-bit machine word whose arithmetic wraps around modulo 2w2^w, as the bit-vector theory §15.15 defines. Booleans, real numbers and arrays §14.5 are also available.

  2. Assert constraints, the formulas that must all hold, into a solver object.

  3. Check. Z3 combines a SAT (Boolean satisfiability) engine, which searches over true-or-false choices, with theory solvers, each of which decides facts in one theory, such as linear arithmetic. Bit-vector constraints are often translated into Boolean form, one variable per bit.

  4. Read the result. After sat, model() returns the values. After unsat, Z3 can report an unsat core, a subset of the tracked assertions that is already contradictory. After unknown, reason_unknown() returns a short text naming the cause.

To prove that a property PP holds for every input that satisfies assumptions AA, ask whether AA and the negation of PP can hold together. Unsat means that no input satisfies AA and violates PP, so PP holds. Z3’s Python §12.23 function prove performs exactly this check. Z3 reads formulas written in SMT-LIB (Satisfiability Modulo Theories Library), the standard text format of SMT solvers, and offers interfaces for C, C++, Python (the package z3-solver), Java and other languages.

Unknown has three common causes:

For decidable theories without quantifiers, such as fixed-width bit-vectors, Z3 always finishes in principle, but it can need more time than any practical limit.

Worked example

First, with integers: x = Int("x"), and the constraints 0≤x≤30 \le x \le 3 and x+1≤xx + 1 \le x. Subtracting xx from both sides of the second constraint gives 1≤01 \le 0, which is false for every integer, so Z3 answers unsat. By hand, the four allowed values give 0+1=1>00 + 1 = 1 > 0, 1+1=2>11 + 1 = 2 > 1, 2+1=3>22 + 1 = 3 > 2 and 3+1=4>33 + 1 = 4 > 3; none satisfies x+1≤xx + 1 \le x.

Second, with a 2-bit word: x = BitVec("x", 2) and the constraint ULE(x + 1, x), where ULE is unsigned less-than-or-equal. A 2-bit word holds the values 0 to 3, and addition wraps modulo 22=42^2 = 4, which is unsigned wraparound §4.16: 0+1=10 + 1 = 1, 1+1=21 + 1 = 2, 2+1=32 + 1 = 3, and 3+1=43 + 1 = 4, which is 4mod4=04 \bmod 4 = 0. Only x=3x = 3 satisfies the constraint, since 0≤30 \le 3. Z3 answers sat with the model x=3x = 3.

Third, the same word with Python’s <= operator, which Z3’s Python interface defines on bit-vectors as a signed comparison §2.12. In two’s complement, the usual signed reading, the top bit of a 2-bit word counts −2-2 instead of +2+2, so the patterns 00, 01, 10, 11 mean 0, 1, −2-2, −1-1. Adding 1 to each gives 1, −2-2, −1-1, 0. Only x=1x = 1 satisfies x+1≤xx + 1 \le x, since −2≤1-2 \le 1, so Z3 answers sat with the model x=1x = 1. One question has three answers under three encodings: the solver is consistent, and the encodings differ.

Fourth, a query that reaches its time limit returns unknown, and reason_unknown() names the limit. That says nothing about the formula: with a longer limit, the same query may return sat, unsat or unknown again.

Stated precisely

Let FF be a formula and TT a theory, a fixed meaning for the symbols of FF. FF is satisfiable in TT if some assignment of values to its free variables, interpreted by TT, makes FF true. Z3’s three answers mean:

A safety question about code is posed as Q=A∧C∧¬PQ = A \wedge C \wedge \neg P, where AA states the assumptions about inputs, CC encodes what the code computes, PP is the property, ∧\wedge means “and” and ¬\neg means “not”. If QQ is unsat, every assignment that satisfies A∧CA \wedge C satisfies PP. That conclusion carries over to the real program under three conditions.

  1. Faithful encoding: CC describes every behavior of the code, with the real widths, signedness, wraparound, memory effects and side effects.

  2. Satisfiable premises: A∧CA \wedge C alone is sat. If it is unsat, QQ is unsat for every PP, and the proof is vacuous.

  3. Correct solver: Z3 has no bug that affects this query.

Limits and common mistakes

Unknown is not unsat, and a timeout §9.34 is one kind of unknown. A tool that reports “no counterexample found” after a timeout claims a proof it does not have; the honest report is “not proven”.

In Z3’s Python interface, <, <=, > and >= on bit-vectors compare as signed numbers, / divides as signed numbers, and >> shifts in copies of the sign bit. The unsigned forms are ULT, ULE, UGT, UGE, UDiv and LShR. Mixing them up proves facts about a different program.

Int never overflows. A proof about Int says nothing about a C variable of type uint32_t, which wraps around at 2322^{32}.

A model of an over-approximation §19.2.5 can be spurious. If CC allows more behavior than the code has, a sat answer must be confirmed by running the code on the model’s values.

Answers near a time limit are not reproducible: machine speed, load, Z3 version, random seed and option settings change which queries finish. Record the version, the options and the limit with each result.

Fuzzing §10.9 campaigns have found soundness §3.7 bugs in Z3, cases of a wrong sat or unsat answer. A high-assurance result is cross-checked with a second solver, or Z3 produces a proof of unsat that a separate tool checks.

Questions answered

What does unknown or a timeout mean compared with unsatisfiable?

Unsatisfiable (unsat) is a conclusion: Z3 has established that no assignment of values makes the formula true under the chosen theories. Unknown is the absence of a conclusion: Z3 stopped without finding a satisfying assignment and without ruling one out. A timeout is one cause of unknown, the time limit expiring. Other causes are an interruption by the caller, a theory for which Z3’s methods can give up, such as integer arithmetic with multiplication of variables, and quantifiers.

For proofs the difference is decisive. A property is proven only when the query “assumptions and code and not property” is unsat. After unknown or a timeout, the formula may be satisfiable, which means a counterexample may exist, or it may be unsatisfiable; nothing is known. Report the property as not proven, then retry with a longer limit, with a simpler or split formula, or with another solver. Even an unsat answer proves the property only for the encoded formula, with the widths, signedness and behaviors that the encoding contains.

Related entries: SMT solver (§3.28), Bounded model checking (§3.29), Soundness (§3.7), Undefined versus unsupported semantics (§3.35), Bit-vector logic (§15.15), Unsigned modular arithmetic (§4.16), Signed versus unsigned comparison (§2.12), Symbolic execution (§3.27). Further reading: [59, 181].