1 //== SMTConstraintManager.h -------------------------------------*- C++ -*--==//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines a SMT generic API, which will be the base class for
10 // every SMT solver specific class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SMTCONSTRAINTMANAGER_H
15 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SMTCONSTRAINTMANAGER_H
17 #include "clang/Basic/JsonSupport.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/SMTConv.h"
21 typedef llvm::ImmutableSet<
22 std::pair<clang::ento::SymbolRef, const llvm::SMTExpr *>>
24 REGISTER_TRAIT_WITH_PROGRAMSTATE(ConstraintSMT, ConstraintSMTType)
29 class SMTConstraintManager : public clang::ento::SimpleConstraintManager {
30 mutable llvm::SMTSolverRef Solver = llvm::CreateZ3Solver();
33 SMTConstraintManager(clang::ento::SubEngine *SE, clang::ento::SValBuilder &SB)
34 : SimpleConstraintManager(SE, SB) {}
35 virtual ~SMTConstraintManager() = default;
37 //===------------------------------------------------------------------===//
38 // Implementation for interface from SimpleConstraintManager.
39 //===------------------------------------------------------------------===//
41 ProgramStateRef assumeSym(ProgramStateRef State, SymbolRef Sym,
42 bool Assumption) override {
43 ASTContext &Ctx = getBasicVals().getContext();
48 llvm::SMTExprRef Exp =
49 SMTConv::getExpr(Solver, Ctx, Sym, &RetTy, &hasComparison);
51 // Create zero comparison for implicit boolean cast, with reversed
53 if (!hasComparison && !RetTy->isBooleanType())
56 SMTConv::getZeroExpr(Solver, Ctx, Exp, RetTy, !Assumption));
58 return assumeExpr(State, Sym, Assumption ? Exp : Solver->mkNot(Exp));
61 ProgramStateRef assumeSymInclusiveRange(ProgramStateRef State, SymbolRef Sym,
62 const llvm::APSInt &From,
63 const llvm::APSInt &To,
64 bool InRange) override {
65 ASTContext &Ctx = getBasicVals().getContext();
67 State, Sym, SMTConv::getRangeExpr(Solver, Ctx, Sym, From, To, InRange));
70 ProgramStateRef assumeSymUnsupported(ProgramStateRef State, SymbolRef Sym,
71 bool Assumption) override {
72 // Skip anything that is unsupported
76 //===------------------------------------------------------------------===//
77 // Implementation for interface from ConstraintManager.
78 //===------------------------------------------------------------------===//
80 ConditionTruthVal checkNull(ProgramStateRef State, SymbolRef Sym) override {
81 ASTContext &Ctx = getBasicVals().getContext();
84 // The expression may be casted, so we cannot call getZ3DataExpr() directly
85 llvm::SMTExprRef VarExp = SMTConv::getExpr(Solver, Ctx, Sym, &RetTy);
86 llvm::SMTExprRef Exp =
87 SMTConv::getZeroExpr(Solver, Ctx, VarExp, RetTy, /*Assumption=*/true);
89 // Negate the constraint
90 llvm::SMTExprRef NotExp =
91 SMTConv::getZeroExpr(Solver, Ctx, VarExp, RetTy, /*Assumption=*/false);
93 ConditionTruthVal isSat = checkModel(State, Sym, Exp);
94 ConditionTruthVal isNotSat = checkModel(State, Sym, NotExp);
96 // Zero is the only possible solution
97 if (isSat.isConstrainedTrue() && isNotSat.isConstrainedFalse())
100 // Zero is not a solution
101 if (isSat.isConstrainedFalse() && isNotSat.isConstrainedTrue())
104 // Zero may be a solution
105 return ConditionTruthVal();
108 const llvm::APSInt *getSymVal(ProgramStateRef State,
109 SymbolRef Sym) const override {
110 BasicValueFactory &BVF = getBasicVals();
111 ASTContext &Ctx = BVF.getContext();
113 if (const SymbolData *SD = dyn_cast<SymbolData>(Sym)) {
114 QualType Ty = Sym->getType();
115 assert(!Ty->isRealFloatingType());
116 llvm::APSInt Value(Ctx.getTypeSize(Ty),
117 !Ty->isSignedIntegerOrEnumerationType());
119 // TODO: this should call checkModel so we can use the cache, however,
120 // this method tries to get the interpretation (the actual value) from
121 // the solver, which is currently not cached.
123 llvm::SMTExprRef Exp =
124 SMTConv::fromData(Solver, SD->getSymbolID(), Ty, Ctx.getTypeSize(Ty));
127 addStateConstraints(State);
129 // Constraints are unsatisfiable
130 Optional<bool> isSat = Solver->check();
131 if (!isSat.hasValue() || !isSat.getValue())
134 // Model does not assign interpretation
135 if (!Solver->getInterpretation(Exp, Value))
138 // A value has been obtained, check if it is the only value
139 llvm::SMTExprRef NotExp = SMTConv::fromBinOp(
141 Ty->isBooleanType() ? Solver->mkBoolean(Value.getBoolValue())
142 : Solver->mkBitvector(Value, Value.getBitWidth()),
145 Solver->addConstraint(NotExp);
147 Optional<bool> isNotSat = Solver->check();
148 if (!isSat.hasValue() || isNotSat.getValue())
151 // This is the only solution, store it
152 return &BVF.getValue(Value);
155 if (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym)) {
156 SymbolRef CastSym = SC->getOperand();
157 QualType CastTy = SC->getType();
158 // Skip the void type
159 if (CastTy->isVoidType())
162 const llvm::APSInt *Value;
163 if (!(Value = getSymVal(State, CastSym)))
165 return &BVF.Convert(SC->getType(), *Value);
168 if (const BinarySymExpr *BSE = dyn_cast<BinarySymExpr>(Sym)) {
169 const llvm::APSInt *LHS, *RHS;
170 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(BSE)) {
171 LHS = getSymVal(State, SIE->getLHS());
172 RHS = &SIE->getRHS();
173 } else if (const IntSymExpr *ISE = dyn_cast<IntSymExpr>(BSE)) {
174 LHS = &ISE->getLHS();
175 RHS = getSymVal(State, ISE->getRHS());
176 } else if (const SymSymExpr *SSM = dyn_cast<SymSymExpr>(BSE)) {
177 // Early termination to avoid expensive call
178 LHS = getSymVal(State, SSM->getLHS());
179 RHS = LHS ? getSymVal(State, SSM->getRHS()) : nullptr;
181 llvm_unreachable("Unsupported binary expression to get symbol value!");
187 llvm::APSInt ConvertedLHS, ConvertedRHS;
189 std::tie(ConvertedLHS, LTy) = SMTConv::fixAPSInt(Ctx, *LHS);
190 std::tie(ConvertedRHS, RTy) = SMTConv::fixAPSInt(Ctx, *RHS);
191 SMTConv::doIntTypeConversion<llvm::APSInt, &SMTConv::castAPSInt>(
192 Solver, Ctx, ConvertedLHS, LTy, ConvertedRHS, RTy);
193 return BVF.evalAPSInt(BSE->getOpcode(), ConvertedLHS, ConvertedRHS);
196 llvm_unreachable("Unsupported expression to get symbol value!");
199 ProgramStateRef removeDeadBindings(ProgramStateRef State,
200 SymbolReaper &SymReaper) override {
201 auto CZ = State->get<ConstraintSMT>();
202 auto &CZFactory = State->get_context<ConstraintSMT>();
204 for (auto I = CZ.begin(), E = CZ.end(); I != E; ++I) {
205 if (SymReaper.isDead(I->first))
206 CZ = CZFactory.remove(CZ, *I);
209 return State->set<ConstraintSMT>(CZ);
212 void printJson(raw_ostream &Out, ProgramStateRef State, const char *NL = "\n",
213 unsigned int Space = 0, bool IsDot = false) const override {
214 ConstraintSMTType Constraints = State->get<ConstraintSMT>();
216 Indent(Out, Space, IsDot) << "\"constraints\": ";
217 if (Constraints.isEmpty()) {
218 Out << "null," << NL;
224 for (ConstraintSMTType::iterator I = Constraints.begin();
225 I != Constraints.end(); ++I) {
226 Indent(Out, Space, IsDot)
227 << "{ \"symbol\": \"" << I->first << "\", \"range\": \"";
228 I->second->print(Out);
231 if (std::next(I) != Constraints.end())
237 Indent(Out, Space, IsDot) << "],";
240 bool haveEqualConstraints(ProgramStateRef S1,
241 ProgramStateRef S2) const override {
242 return S1->get<ConstraintSMT>() == S2->get<ConstraintSMT>();
245 bool canReasonAbout(SVal X) const override {
246 const TargetInfo &TI = getBasicVals().getContext().getTargetInfo();
248 Optional<nonloc::SymbolVal> SymVal = X.getAs<nonloc::SymbolVal>();
252 const SymExpr *Sym = SymVal->getSymbol();
253 QualType Ty = Sym->getType();
255 // Complex types are not modeled
256 if (Ty->isComplexType() || Ty->isComplexIntegerType())
259 // Non-IEEE 754 floating-point types are not modeled
260 if ((Ty->isSpecificBuiltinType(BuiltinType::LongDouble) &&
261 (&TI.getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended() ||
262 &TI.getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble())))
265 if (Ty->isRealFloatingType())
266 return Solver->isFPSupported();
268 if (isa<SymbolData>(Sym))
271 SValBuilder &SVB = getSValBuilder();
273 if (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym))
274 return canReasonAbout(SVB.makeSymbolVal(SC->getOperand()));
276 if (const BinarySymExpr *BSE = dyn_cast<BinarySymExpr>(Sym)) {
277 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(BSE))
278 return canReasonAbout(SVB.makeSymbolVal(SIE->getLHS()));
280 if (const IntSymExpr *ISE = dyn_cast<IntSymExpr>(BSE))
281 return canReasonAbout(SVB.makeSymbolVal(ISE->getRHS()));
283 if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(BSE))
284 return canReasonAbout(SVB.makeSymbolVal(SSE->getLHS())) &&
285 canReasonAbout(SVB.makeSymbolVal(SSE->getRHS()));
288 llvm_unreachable("Unsupported expression to reason about!");
291 /// Dumps SMT formula
292 LLVM_DUMP_METHOD void dump() const { Solver->dump(); }
295 // Check whether a new model is satisfiable, and update the program state.
296 virtual ProgramStateRef assumeExpr(ProgramStateRef State, SymbolRef Sym,
297 const llvm::SMTExprRef &Exp) {
298 // Check the model, avoid simplifying AST to save time
299 if (checkModel(State, Sym, Exp).isConstrainedTrue())
300 return State->add<ConstraintSMT>(std::make_pair(Sym, Exp));
305 /// Given a program state, construct the logical conjunction and add it to
307 virtual void addStateConstraints(ProgramStateRef State) const {
308 // TODO: Don't add all the constraints, only the relevant ones
309 auto CZ = State->get<ConstraintSMT>();
310 auto I = CZ.begin(), IE = CZ.end();
312 // Construct the logical AND of all the constraints
314 std::vector<llvm::SMTExprRef> ASTs;
316 llvm::SMTExprRef Constraint = I++->second;
318 Constraint = Solver->mkAnd(Constraint, I++->second);
321 Solver->addConstraint(Constraint);
325 // Generate and check a Z3 model, using the given constraint.
326 ConditionTruthVal checkModel(ProgramStateRef State, SymbolRef Sym,
327 const llvm::SMTExprRef &Exp) const {
328 ProgramStateRef NewState =
329 State->add<ConstraintSMT>(std::make_pair(Sym, Exp));
331 llvm::FoldingSetNodeID ID;
332 NewState->get<ConstraintSMT>().Profile(ID);
334 unsigned hash = ID.ComputeHash();
335 auto I = Cached.find(hash);
336 if (I != Cached.end())
340 addStateConstraints(NewState);
342 Optional<bool> res = Solver->check();
344 Cached[hash] = ConditionTruthVal();
346 Cached[hash] = ConditionTruthVal(res.getValue());
351 // Cache the result of an SMT query (true, false, unknown). The key is the
352 // hash of the constraints in a state
353 mutable llvm::DenseMap<unsigned, ConditionTruthVal> Cached;
354 }; // end class SMTConstraintManager