clang API Documentation
00001 //== DivZeroChecker.cpp - Division by zero checker --------------*- C++ -*--==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This defines DivZeroChecker, a builtin check in ExprEngine that performs 00011 // checks for division by zeros. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "ClangSACheckers.h" 00016 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 00017 #include "clang/StaticAnalyzer/Core/Checker.h" 00018 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 00019 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 00020 00021 using namespace clang; 00022 using namespace ento; 00023 00024 namespace { 00025 class DivZeroChecker : public Checker< check::PreStmt<BinaryOperator> > { 00026 mutable std::unique_ptr<BuiltinBug> BT; 00027 void reportBug(const char *Msg, 00028 ProgramStateRef StateZero, 00029 CheckerContext &C) const ; 00030 public: 00031 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const; 00032 }; 00033 } // end anonymous namespace 00034 00035 void DivZeroChecker::reportBug(const char *Msg, 00036 ProgramStateRef StateZero, 00037 CheckerContext &C) const { 00038 if (ExplodedNode *N = C.generateSink(StateZero)) { 00039 if (!BT) 00040 BT.reset(new BuiltinBug(this, "Division by zero")); 00041 00042 BugReport *R = new BugReport(*BT, Msg, N); 00043 bugreporter::trackNullOrUndefValue(N, bugreporter::GetDenomExpr(N), *R); 00044 C.emitReport(R); 00045 } 00046 } 00047 00048 void DivZeroChecker::checkPreStmt(const BinaryOperator *B, 00049 CheckerContext &C) const { 00050 BinaryOperator::Opcode Op = B->getOpcode(); 00051 if (Op != BO_Div && 00052 Op != BO_Rem && 00053 Op != BO_DivAssign && 00054 Op != BO_RemAssign) 00055 return; 00056 00057 if (!B->getRHS()->getType()->isScalarType()) 00058 return; 00059 00060 SVal Denom = C.getState()->getSVal(B->getRHS(), C.getLocationContext()); 00061 Optional<DefinedSVal> DV = Denom.getAs<DefinedSVal>(); 00062 00063 // Divide-by-undefined handled in the generic checking for uses of 00064 // undefined values. 00065 if (!DV) 00066 return; 00067 00068 // Check for divide by zero. 00069 ConstraintManager &CM = C.getConstraintManager(); 00070 ProgramStateRef stateNotZero, stateZero; 00071 std::tie(stateNotZero, stateZero) = CM.assumeDual(C.getState(), *DV); 00072 00073 if (!stateNotZero) { 00074 assert(stateZero); 00075 reportBug("Division by zero", stateZero, C); 00076 return; 00077 } 00078 00079 bool TaintedD = C.getState()->isTainted(*DV); 00080 if ((stateNotZero && stateZero && TaintedD)) { 00081 reportBug("Division by a tainted value, possibly zero", stateZero, C); 00082 return; 00083 } 00084 00085 // If we get here, then the denom should not be zero. We abandon the implicit 00086 // zero denom case for now. 00087 C.addTransition(stateNotZero); 00088 } 00089 00090 void ento::registerDivZeroChecker(CheckerManager &mgr) { 00091 mgr.registerChecker<DivZeroChecker>(); 00092 }