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.
Declare variables with sorts, Z3’s word for types.
Intis a mathematical integer with no size limit. ABitVecof width is a -bit machine word whose arithmetic wraps around modulo , as the bit-vector theory §15.15 defines. Booleans, real numbers and arrays §14.5 are also available.Assert constraints, the formulas that must all hold, into a solver object.
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.
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 holds for every input that satisfies assumptions , ask whether and the negation of can hold together. Unsat means that no input satisfies and violates , so 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:
a resource limit, such as a time limit set with
s.set("timeout", 10000), in milliseconds, or an interruption by the caller;an undecidable §19.2.1 theory, one for which no algorithm §14.1 can decide every formula, such as integer arithmetic with multiplication of variables, where Z3 uses methods that can give up (see the entry on decidability);
quantifiers, the phrases “for all” and “there exists” inside a formula, which Z3 handles with methods that can give up.
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 and . Subtracting from both sides of the second constraint gives , which is false for every integer, so Z3 answers unsat. By hand, the four allowed values give , , and ; none satisfies .
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 , which is unsigned wraparound §4.16: , , , and , which is . Only satisfies the constraint, since . Z3 answers sat with the model .
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 instead of , so the patterns 00, 01, 10, 11 mean 0, 1, , . Adding 1 to each gives 1, , , 0. Only satisfies , since , so Z3 answers sat with the model . 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 be a formula and a theory, a fixed meaning for the symbols of . is satisfiable in if some assignment of values to its free variables, interpreted by , makes true. Z3’s three answers mean:
sat: Z3 found such an assignment, a model ; anyone can check it by evaluating under ;
unsat: no assignment makes true;
unknown: no claim either way.
A safety question about code is posed as , where states the assumptions about inputs, encodes what the code computes, is the property, means “and” and means “not”. If is unsat, every assignment that satisfies satisfies . That conclusion carries over to the real program under three conditions.
Faithful encoding: describes every behavior of the code, with the real widths, signedness, wraparound, memory effects and side effects.
Satisfiable premises: alone is sat. If it is unsat, is unsat for every , and the proof is vacuous.
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 .
A model of an over-approximation §19.2.5 can be spurious. If 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].