19 #include "llvm/ADT/FoldingSet.h" 21 using namespace clang;
34 : ZeroSymbol(S), BlockID(B), SFC(SFC) {}
39 return BlockID == X.BlockID && SFC == X.SFC && ZeroSymbol == X.ZeroSymbol;
43 if (BlockID != X.BlockID)
44 return BlockID < X.BlockID;
47 return ZeroSymbol < X.ZeroSymbol;
50 void Profile(llvm::FoldingSetNodeID &
ID)
const {
51 ID.AddInteger(BlockID);
53 ID.AddPointer(ZeroSymbol);
65 : ZeroSymbol(ZeroSymbol), SFC(SFC), Satisfied(
false) {}
67 void Profile(llvm::FoldingSetNodeID &ID)
const override {
73 BugReporterContext &BRC,
74 PathSensitiveBugReport &BR)
override;
77 class TestAfterDivZeroChecker
78 :
public Checker<check::PreStmt<BinaryOperator>, check::BranchCondition,
80 mutable std::unique_ptr<BuiltinBug> DivZeroBug;
81 void reportBug(SVal Val, CheckerContext &C)
const;
84 void checkPreStmt(
const BinaryOperator *B, CheckerContext &C)
const;
85 void checkBranchCondition(
const Stmt *Condition, CheckerContext &C)
const;
86 void checkEndFunction(
const ReturnStmt *RS, CheckerContext &C)
const;
87 void setDivZeroMap(SVal Var, CheckerContext &C)
const;
88 bool hasDivZeroMap(SVal Var,
const CheckerContext &C)
const;
89 bool isZero(SVal S, CheckerContext &C)
const;
96 DivisionBRVisitor::VisitNode(
const ExplodedNode *Succ, BugReporterContext &BRC,
97 PathSensitiveBugReport &BR) {
101 const Expr *E =
nullptr;
106 if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
107 Op == BO_RemAssign) {
115 SVal S = Succ->getSVal(E);
116 if (ZeroSymbol == S.getAsSymbol() && SFC == Succ->
getStackFrame()) {
121 PathDiagnosticLocation L =
124 if (!L.isValid() || !L.asLocation().isValid())
127 return std::make_shared<PathDiagnosticEventPiece>(
128 L,
"Division with compared value made here");
134 bool TestAfterDivZeroChecker::isZero(SVal S, CheckerContext &C)
const {
140 ConstraintManager &CM = C.getConstraintManager();
141 return !CM.assume(C.getState(), *DSV,
true);
144 void TestAfterDivZeroChecker::setDivZeroMap(SVal Var, CheckerContext &C)
const {
151 State->add<DivZeroMap>(ZeroState(SR, C.getBlockID(), C.getStackFrame()));
152 C.addTransition(State);
155 bool TestAfterDivZeroChecker::hasDivZeroMap(SVal Var,
156 const CheckerContext &C)
const {
161 ZeroState ZS(SR, C.getBlockID(), C.getStackFrame());
162 return C.getState()->contains<DivZeroMap>(ZS);
165 void TestAfterDivZeroChecker::reportBug(SVal Val, CheckerContext &C)
const {
166 if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
168 DivZeroBug.reset(
new BuiltinBug(
this,
"Division by zero"));
170 auto R = std::make_unique<PathSensitiveBugReport>(
171 *DivZeroBug,
"Value being compared against zero has already been used " 175 R->addVisitor(std::make_unique<DivisionBRVisitor>(Val.getAsSymbol(),
177 C.emitReport(std::move(R));
181 void TestAfterDivZeroChecker::checkEndFunction(
const ReturnStmt *,
182 CheckerContext &C)
const {
185 DivZeroMapTy DivZeroes = State->get<DivZeroMap>();
186 if (DivZeroes.isEmpty())
189 DivZeroMapTy::Factory &F = State->get_context<DivZeroMap>();
190 for (llvm::ImmutableSet<ZeroState>::iterator I = DivZeroes.begin(),
194 if (ZS.getStackFrameContext() == C.getStackFrame())
195 DivZeroes = F.remove(DivZeroes, ZS);
197 C.addTransition(State->set<DivZeroMap>(DivZeroes));
200 void TestAfterDivZeroChecker::checkPreStmt(
const BinaryOperator *B,
201 CheckerContext &C)
const {
203 if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
204 Op == BO_RemAssign) {
205 SVal S = C.getSVal(B->
getRHS());
212 void TestAfterDivZeroChecker::checkBranchCondition(
const Stmt *Condition,
213 CheckerContext &C)
const {
214 if (
const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
223 if (!IntLiteral || IntLiteral->
getValue() != 0)
227 if (hasDivZeroMap(Val, C))
230 }
else if (
const UnaryOperator *U = dyn_cast<UnaryOperator>(Condition)) {
231 if (U->getOpcode() == UO_LNot) {
234 dyn_cast<ImplicitCastExpr>(U->getSubExpr()))
235 Val = C.getSVal(I->getSubExpr());
237 if (hasDivZeroMap(Val, C))
240 Val = C.getSVal(U->getSubExpr());
241 if (hasDivZeroMap(Val, C))
246 dyn_cast<ImplicitCastExpr>(Condition)) {
247 SVal Val = C.getSVal(IE->getSubExpr());
249 if (hasDivZeroMap(Val, C))
252 SVal Val = C.getSVal(Condition);
254 if (hasDivZeroMap(Val, C))
260 void ento::registerTestAfterDivZeroChecker(CheckerManager &mgr) {
261 mgr.registerChecker<TestAfterDivZeroChecker>();
264 bool ento::shouldRegisterTestAfterDivZeroChecker(
const LangOptions &LO) {
bool operator==(CanQual< T > x, CanQual< U > y)
Stmt - This represents one statement.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
std::shared_ptr< PathDiagnosticPiece > PathDiagnosticPieceRef
A builtin binary operation expression such as "x + y" or "x <= y".
static PathDiagnosticLocation create(const Decl *D, const SourceManager &SM)
Create a location corresponding to the given declaration.
This represents one expression.
ReturnStmt - This represents a return, optionally of an expression: return; return 4;...
UnaryOperator - This represents the unary-expression's (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
#define REGISTER_SET_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable set of type NameTy, suitable for placement into the ProgramState.
Dataflow Directional Tag Classes.
llvm::APInt getValue() const
const StackFrameContext * getStackFrame() const
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
static bool isComparisonOp(Opcode Opc)