Zhongxing Xu | 9e56d23 | 2009-10-31 10:02:37 +0000 | [diff] [blame] | 1 | //== DivZeroChecker.cpp - Division by zero checker --------------*- C++ -*--==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This defines DivZeroChecker, a builtin check in GRExprEngine that performs |
| 11 | // checks for division by zeros. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | 36df58a | 2009-11-06 20:47:51 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
| 16 | #include "GRExprEngineInternalChecks.h" |
Zhongxing Xu | 9e56d23 | 2009-10-31 10:02:37 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | |
Ted Kremenek | 36df58a | 2009-11-06 20:47:51 +0000 | [diff] [blame] | 20 | namespace { |
| 21 | class VISIBILITY_HIDDEN DivZeroChecker : public CheckerVisitor<DivZeroChecker> { |
| 22 | BuiltinBug *BT; |
| 23 | public: |
| 24 | DivZeroChecker() : BT(0) {} |
| 25 | static void *getTag(); |
| 26 | void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B); |
| 27 | }; |
| 28 | } // end anonymous namespace |
| 29 | |
| 30 | void clang::RegisterDivZeroChecker(GRExprEngine &Eng) { |
| 31 | Eng.registerCheck(new DivZeroChecker()); |
| 32 | } |
| 33 | |
Zhongxing Xu | 9e56d23 | 2009-10-31 10:02:37 +0000 | [diff] [blame] | 34 | void *DivZeroChecker::getTag() { |
| 35 | static int x; |
| 36 | return &x; |
| 37 | } |
| 38 | |
| 39 | void DivZeroChecker::PreVisitBinaryOperator(CheckerContext &C, |
| 40 | const BinaryOperator *B) { |
| 41 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 42 | if (Op != BinaryOperator::Div && |
| 43 | Op != BinaryOperator::Rem && |
| 44 | Op != BinaryOperator::DivAssign && |
| 45 | Op != BinaryOperator::RemAssign) |
| 46 | return; |
| 47 | |
| 48 | if (!B->getRHS()->getType()->isIntegerType() || |
| 49 | !B->getRHS()->getType()->isScalarType()) |
| 50 | return; |
| 51 | |
| 52 | SVal Denom = C.getState()->getSVal(B->getRHS()); |
| 53 | const DefinedSVal *DV = dyn_cast<DefinedSVal>(&Denom); |
| 54 | |
| 55 | // Divide-by-undefined handled in the generic checking for uses of |
| 56 | // undefined values. |
| 57 | if (!DV) |
| 58 | return; |
| 59 | |
| 60 | // Check for divide by zero. |
| 61 | ConstraintManager &CM = C.getConstraintManager(); |
| 62 | const GRState *stateNotZero, *stateZero; |
| 63 | llvm::tie(stateNotZero, stateZero) = CM.AssumeDual(C.getState(), *DV); |
| 64 | |
| 65 | if (stateZero && !stateNotZero) { |
| 66 | if (ExplodedNode *N = C.GenerateNode(B, stateZero, true)) { |
| 67 | if (!BT) |
Ted Kremenek | 2c791bd | 2009-11-06 00:44:32 +0000 | [diff] [blame] | 68 | BT = new BuiltinBug("Division by zero"); |
Zhongxing Xu | 9e56d23 | 2009-10-31 10:02:37 +0000 | [diff] [blame] | 69 | |
| 70 | EnhancedBugReport *R = |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 71 | new EnhancedBugReport(*BT, BT->getDescription(), N); |
Zhongxing Xu | 9e56d23 | 2009-10-31 10:02:37 +0000 | [diff] [blame] | 72 | |
| 73 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 74 | bugreporter::GetDenomExpr(N)); |
| 75 | |
| 76 | C.EmitReport(R); |
| 77 | } |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // If we get here, then the denom should not be zero. We abandon the implicit |
| 82 | // zero denom case for now. |
| 83 | if (stateNotZero != C.getState()) |
| 84 | C.addTransition(C.GenerateNode(B, stateNotZero)); |
| 85 | } |