1 //===- ConstraintManager.cpp - Constraints on symbolic values. ------------===//
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 defined the interface to manage constraints on symbolic values.
11 //===----------------------------------------------------------------------===//
13 #include "clang/StaticAnalyzer/Core/PathSensitive/ConstraintManager.h"
14 #include "clang/AST/Type.h"
15 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
16 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
19 #include "llvm/ADT/ScopeExit.h"
21 using namespace clang
;
24 ConstraintManager::~ConstraintManager() = default;
26 static DefinedSVal
getLocFromSymbol(const ProgramStateRef
&State
,
29 State
->getStateManager().getRegionManager().getSymbolicRegion(Sym
);
30 return loc::MemRegionVal(R
);
33 ConditionTruthVal
ConstraintManager::checkNull(ProgramStateRef State
,
35 QualType Ty
= Sym
->getType();
36 DefinedSVal V
= Loc::isLocType(Ty
) ? getLocFromSymbol(State
, Sym
)
37 : nonloc::SymbolVal(Sym
);
38 const ProgramStatePair
&P
= assumeDual(State
, V
);
39 if (P
.first
&& !P
.second
)
40 return ConditionTruthVal(false);
41 if (!P
.first
&& P
.second
)
42 return ConditionTruthVal(true);
46 template <typename AssumeFunction
>
47 ConstraintManager::ProgramStatePair
48 ConstraintManager::assumeDualImpl(ProgramStateRef
&State
,
49 AssumeFunction
&Assume
) {
50 if (LLVM_UNLIKELY(State
->isPosteriorlyOverconstrained()))
51 return {State
, State
};
53 // Assume functions might recurse (see `reAssume` or `tryRearrange`). During
54 // the recursion the State might not change anymore, that means we reached a
56 // We avoid infinite recursion of assume calls by checking already visited
57 // States on the stack of assume function calls.
58 const ProgramState
*RawSt
= State
.get();
59 if (LLVM_UNLIKELY(AssumeStack
.contains(RawSt
)))
60 return {State
, State
};
61 AssumeStack
.push(RawSt
);
62 auto AssumeStackBuilder
=
63 llvm::make_scope_exit([this]() { AssumeStack
.pop(); });
65 ProgramStateRef StTrue
= Assume(true);
68 ProgramStateRef StFalse
= Assume(false);
69 if (LLVM_UNLIKELY(!StFalse
)) { // both infeasible
70 ProgramStateRef StInfeasible
= State
->cloneAsPosteriorlyOverconstrained();
71 assert(StInfeasible
->isPosteriorlyOverconstrained());
72 // Checkers might rely on the API contract that both returned states
73 // cannot be null. Thus, we return StInfeasible for both branches because
74 // it might happen that a Checker uncoditionally uses one of them if the
75 // other is a nullptr. This may also happen with the non-dual and
76 // adjacent `assume(true)` and `assume(false)` calls. By implementing
77 // assume in therms of assumeDual, we can keep our API contract there as
79 return ProgramStatePair(StInfeasible
, StInfeasible
);
81 return ProgramStatePair(nullptr, StFalse
);
84 ProgramStateRef StFalse
= Assume(false);
86 return ProgramStatePair(StTrue
, nullptr);
89 return ProgramStatePair(StTrue
, StFalse
);
92 ConstraintManager::ProgramStatePair
93 ConstraintManager::assumeDual(ProgramStateRef State
, DefinedSVal Cond
) {
94 auto AssumeFun
= [&, Cond
](bool Assumption
) {
95 return assumeInternal(State
, Cond
, Assumption
);
97 return assumeDualImpl(State
, AssumeFun
);
100 ConstraintManager::ProgramStatePair
101 ConstraintManager::assumeInclusiveRangeDual(ProgramStateRef State
, NonLoc Value
,
102 const llvm::APSInt
&From
,
103 const llvm::APSInt
&To
) {
104 auto AssumeFun
= [&](bool Assumption
) {
105 return assumeInclusiveRangeInternal(State
, Value
, From
, To
, Assumption
);
107 return assumeDualImpl(State
, AssumeFun
);
110 ProgramStateRef
ConstraintManager::assume(ProgramStateRef State
,
111 DefinedSVal Cond
, bool Assumption
) {
112 ConstraintManager::ProgramStatePair R
= assumeDual(State
, Cond
);
113 return Assumption
? R
.first
: R
.second
;
117 ConstraintManager::assumeInclusiveRange(ProgramStateRef State
, NonLoc Value
,
118 const llvm::APSInt
&From
,
119 const llvm::APSInt
&To
, bool InBound
) {
120 ConstraintManager::ProgramStatePair R
=
121 assumeInclusiveRangeDual(State
, Value
, From
, To
);
122 return InBound
? R
.first
: R
.second
;