20 #include "llvm/ADT/FoldingSet.h" 22 using namespace clang;
35 : ZeroSymbol(S), BlockID(B), SFC(SFC) {}
40 return BlockID == X.BlockID && SFC == X.SFC && ZeroSymbol == X.ZeroSymbol;
44 if (BlockID != X.BlockID)
45 return BlockID < X.BlockID;
48 return ZeroSymbol < X.ZeroSymbol;
51 void Profile(llvm::FoldingSetNodeID &
ID)
const {
52 ID.AddInteger(BlockID);
54 ID.AddPointer(ZeroSymbol);
66 : ZeroSymbol(ZeroSymbol), SFC(SFC), Satisfied(
false) {}
68 void Profile(llvm::FoldingSetNodeID &ID)
const override {
73 std::shared_ptr<PathDiagnosticPiece> VisitNode(
const ExplodedNode *Succ,
74 BugReporterContext &BRC,
75 BugReport &BR)
override;
78 class TestAfterDivZeroChecker
79 :
public Checker<check::PreStmt<BinaryOperator>, check::BranchCondition,
81 mutable std::unique_ptr<BuiltinBug> DivZeroBug;
82 void reportBug(SVal Val, CheckerContext &C)
const;
85 void checkPreStmt(
const BinaryOperator *B, CheckerContext &C)
const;
86 void checkBranchCondition(
const Stmt *Condition, CheckerContext &C)
const;
87 void checkEndFunction(
const ReturnStmt *RS, CheckerContext &C)
const;
88 void setDivZeroMap(SVal Var, CheckerContext &C)
const;
89 bool hasDivZeroMap(SVal Var,
const CheckerContext &C)
const;
90 bool isZero(SVal S, CheckerContext &C)
const;
96 std::shared_ptr<PathDiagnosticPiece>
97 DivisionBRVisitor::VisitNode(
const ExplodedNode *Succ,
98 BugReporterContext &BRC, BugReport &BR) {
102 const Expr *E =
nullptr;
107 if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
108 Op == BO_RemAssign) {
116 SVal S = Succ->getSVal(E);
117 if (ZeroSymbol == S.getAsSymbol() && SFC == Succ->
getStackFrame()) {
122 PathDiagnosticLocation L =
125 if (!L.isValid() || !L.asLocation().isValid())
128 return std::make_shared<PathDiagnosticEventPiece>(
129 L,
"Division with compared value made here");
141 ConstraintManager &CM = C.getConstraintManager();
142 return !CM.assume(C.getState(), *DSV,
true);
145 void TestAfterDivZeroChecker::setDivZeroMap(SVal Var, CheckerContext &C)
const {
152 State->add<DivZeroMap>(ZeroState(SR, C.getBlockID(), C.getStackFrame()));
153 C.addTransition(State);
156 bool TestAfterDivZeroChecker::hasDivZeroMap(SVal Var,
157 const CheckerContext &C)
const {
162 ZeroState ZS(SR, C.getBlockID(), C.getStackFrame());
163 return C.getState()->contains<DivZeroMap>(ZS);
166 void TestAfterDivZeroChecker::reportBug(SVal Val, CheckerContext &C)
const {
167 if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
169 DivZeroBug.reset(
new BuiltinBug(
this,
"Division by zero"));
171 auto R = llvm::make_unique<BugReport>(
172 *DivZeroBug,
"Value being compared against zero has already been used " 176 R->addVisitor(llvm::make_unique<DivisionBRVisitor>(Val.getAsSymbol(),
178 C.emitReport(std::move(R));
182 void TestAfterDivZeroChecker::checkEndFunction(
const ReturnStmt *,
183 CheckerContext &C)
const {
186 DivZeroMapTy DivZeroes = State->get<DivZeroMap>();
187 if (DivZeroes.isEmpty())
190 DivZeroMapTy::Factory &F = State->get_context<DivZeroMap>();
191 for (llvm::ImmutableSet<ZeroState>::iterator I = DivZeroes.begin(),
195 if (ZS.getStackFrameContext() == C.getStackFrame())
196 DivZeroes = F.remove(DivZeroes, ZS);
198 C.addTransition(State->set<DivZeroMap>(DivZeroes));
201 void TestAfterDivZeroChecker::checkPreStmt(
const BinaryOperator *B,
202 CheckerContext &C)
const {
204 if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
205 Op == BO_RemAssign) {
206 SVal S = C.getSVal(B->
getRHS());
213 void TestAfterDivZeroChecker::checkBranchCondition(
const Stmt *Condition,
214 CheckerContext &C)
const {
215 if (
const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
224 if (!IntLiteral || IntLiteral->
getValue() != 0)
228 if (hasDivZeroMap(Val, C))
231 }
else if (
const UnaryOperator *U = dyn_cast<UnaryOperator>(Condition)) {
232 if (U->getOpcode() == UO_LNot) {
235 dyn_cast<ImplicitCastExpr>(U->getSubExpr()))
236 Val = C.getSVal(I->getSubExpr());
238 if (hasDivZeroMap(Val, C))
241 Val = C.getSVal(U->getSubExpr());
242 if (hasDivZeroMap(Val, C))
247 dyn_cast<ImplicitCastExpr>(Condition)) {
248 SVal Val = C.getSVal(IE->getSubExpr());
250 if (hasDivZeroMap(Val, C))
253 SVal Val = C.getSVal(Condition);
255 if (hasDivZeroMap(Val, C))
261 void ento::registerTestAfterDivZeroChecker(CheckerManager &mgr) {
262 mgr.registerChecker<TestAfterDivZeroChecker>();
bool operator==(CanQual< T > x, CanQual< U > y)
const SymExpr * SymbolRef
Stmt - This represents one statement.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
bool isZero(ProgramStateRef State, const NonLoc &Val)
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)