1 //== NullDerefChecker.cpp - Null dereference 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 NullDerefChecker, a builtin check in ExprEngine that performs 11 // checks for null pointers at loads and stores. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "ClangSACheckers.h" 16 #include "clang/AST/ExprObjC.h" 17 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 18 #include "clang/StaticAnalyzer/Core/Checker.h" 19 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 20 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 21 #include "llvm/ADT/SmallString.h" 22 #include "llvm/Support/raw_ostream.h" 23 24 using namespace clang; 25 using namespace ento; 26 27 namespace { 28 class DereferenceChecker 29 : public Checker< check::Location, 30 check::Bind, 31 EventDispatcher<ImplicitNullDerefEvent> > { 32 mutable std::unique_ptr<BuiltinBug> BT_null; 33 mutable std::unique_ptr<BuiltinBug> BT_undef; 34 35 void reportBug(ProgramStateRef State, const Stmt *S, CheckerContext &C, 36 bool IsBind = false) const; 37 38 public: 39 void checkLocation(SVal location, bool isLoad, const Stmt* S, 40 CheckerContext &C) const; 41 void checkBind(SVal L, SVal V, const Stmt *S, CheckerContext &C) const; 42 43 static void AddDerefSource(raw_ostream &os, 44 SmallVectorImpl<SourceRange> &Ranges, 45 const Expr *Ex, const ProgramState *state, 46 const LocationContext *LCtx, 47 bool loadedFrom = false); 48 }; 49 } // end anonymous namespace 50 51 void 52 DereferenceChecker::AddDerefSource(raw_ostream &os, 53 SmallVectorImpl<SourceRange> &Ranges, 54 const Expr *Ex, 55 const ProgramState *state, 56 const LocationContext *LCtx, 57 bool loadedFrom) { 58 Ex = Ex->IgnoreParenLValueCasts(); 59 switch (Ex->getStmtClass()) { 60 default: 61 break; 62 case Stmt::DeclRefExprClass: { 63 const DeclRefExpr *DR = cast<DeclRefExpr>(Ex); 64 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { 65 os << " (" << (loadedFrom ? "loaded from" : "from") 66 << " variable '" << VD->getName() << "')"; 67 Ranges.push_back(DR->getSourceRange()); 68 } 69 break; 70 } 71 case Stmt::MemberExprClass: { 72 const MemberExpr *ME = cast<MemberExpr>(Ex); 73 os << " (" << (loadedFrom ? "loaded from" : "via") 74 << " field '" << ME->getMemberNameInfo() << "')"; 75 SourceLocation L = ME->getMemberLoc(); 76 Ranges.push_back(SourceRange(L, L)); 77 break; 78 } 79 case Stmt::ObjCIvarRefExprClass: { 80 const ObjCIvarRefExpr *IV = cast<ObjCIvarRefExpr>(Ex); 81 os << " (" << (loadedFrom ? "loaded from" : "via") 82 << " ivar '" << IV->getDecl()->getName() << "')"; 83 SourceLocation L = IV->getLocation(); 84 Ranges.push_back(SourceRange(L, L)); 85 break; 86 } 87 } 88 } 89 90 void DereferenceChecker::reportBug(ProgramStateRef State, const Stmt *S, 91 CheckerContext &C, bool IsBind) const { 92 // Generate an error node. 93 ExplodedNode *N = C.generateSink(State); 94 if (!N) 95 return; 96 97 // We know that 'location' cannot be non-null. This is what 98 // we call an "explicit" null dereference. 99 if (!BT_null) 100 BT_null.reset(new BuiltinBug(this, "Dereference of null pointer")); 101 102 SmallString<100> buf; 103 llvm::raw_svector_ostream os(buf); 104 105 SmallVector<SourceRange, 2> Ranges; 106 107 // Walk through lvalue casts to get the original expression 108 // that syntactically caused the load. 109 if (const Expr *expr = dyn_cast<Expr>(S)) 110 S = expr->IgnoreParenLValueCasts(); 111 112 if (IsBind) { 113 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) { 114 if (BO->isAssignmentOp()) 115 S = BO->getRHS(); 116 } else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) { 117 assert(DS->isSingleDecl() && "We process decls one by one"); 118 if (const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl())) 119 if (const Expr *Init = VD->getAnyInitializer()) 120 S = Init; 121 } 122 } 123 124 switch (S->getStmtClass()) { 125 case Stmt::ArraySubscriptExprClass: { 126 os << "Array access"; 127 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(S); 128 AddDerefSource(os, Ranges, AE->getBase()->IgnoreParenCasts(), 129 State.get(), N->getLocationContext()); 130 os << " results in a null pointer dereference"; 131 break; 132 } 133 case Stmt::UnaryOperatorClass: { 134 os << "Dereference of null pointer"; 135 const UnaryOperator *U = cast<UnaryOperator>(S); 136 AddDerefSource(os, Ranges, U->getSubExpr()->IgnoreParens(), 137 State.get(), N->getLocationContext(), true); 138 break; 139 } 140 case Stmt::MemberExprClass: { 141 const MemberExpr *M = cast<MemberExpr>(S); 142 if (M->isArrow() || bugreporter::isDeclRefExprToReference(M->getBase())) { 143 os << "Access to field '" << M->getMemberNameInfo() 144 << "' results in a dereference of a null pointer"; 145 AddDerefSource(os, Ranges, M->getBase()->IgnoreParenCasts(), 146 State.get(), N->getLocationContext(), true); 147 } 148 break; 149 } 150 case Stmt::ObjCIvarRefExprClass: { 151 const ObjCIvarRefExpr *IV = cast<ObjCIvarRefExpr>(S); 152 os << "Access to instance variable '" << *IV->getDecl() 153 << "' results in a dereference of a null pointer"; 154 AddDerefSource(os, Ranges, IV->getBase()->IgnoreParenCasts(), 155 State.get(), N->getLocationContext(), true); 156 break; 157 } 158 default: 159 break; 160 } 161 162 auto report = llvm::make_unique<BugReport>( 163 *BT_null, buf.empty() ? BT_null->getDescription() : StringRef(buf), N); 164 165 bugreporter::trackNullOrUndefValue(N, bugreporter::getDerefExpr(S), *report); 166 167 for (SmallVectorImpl<SourceRange>::iterator 168 I = Ranges.begin(), E = Ranges.end(); I!=E; ++I) 169 report->addRange(*I); 170 171 C.emitReport(std::move(report)); 172 } 173 174 void DereferenceChecker::checkLocation(SVal l, bool isLoad, const Stmt* S, 175 CheckerContext &C) const { 176 // Check for dereference of an undefined value. 177 if (l.isUndef()) { 178 if (ExplodedNode *N = C.generateSink()) { 179 if (!BT_undef) 180 BT_undef.reset( 181 new BuiltinBug(this, "Dereference of undefined pointer value")); 182 183 auto report = 184 llvm::make_unique<BugReport>(*BT_undef, BT_undef->getDescription(), N); 185 bugreporter::trackNullOrUndefValue(N, bugreporter::getDerefExpr(S), 186 *report); 187 C.emitReport(std::move(report)); 188 } 189 return; 190 } 191 192 DefinedOrUnknownSVal location = l.castAs<DefinedOrUnknownSVal>(); 193 194 // Check for null dereferences. 195 if (!location.getAs<Loc>()) 196 return; 197 198 ProgramStateRef state = C.getState(); 199 200 ProgramStateRef notNullState, nullState; 201 std::tie(notNullState, nullState) = state->assume(location); 202 203 // The explicit NULL case. 204 if (nullState) { 205 if (!notNullState) { 206 reportBug(nullState, S, C); 207 return; 208 } 209 210 // Otherwise, we have the case where the location could either be 211 // null or not-null. Record the error node as an "implicit" null 212 // dereference. 213 if (ExplodedNode *N = C.generateSink(nullState)) { 214 ImplicitNullDerefEvent event = { l, isLoad, N, &C.getBugReporter() }; 215 dispatchEvent(event); 216 } 217 } 218 219 // From this point forward, we know that the location is not null. 220 C.addTransition(notNullState); 221 } 222 223 void DereferenceChecker::checkBind(SVal L, SVal V, const Stmt *S, 224 CheckerContext &C) const { 225 // If we're binding to a reference, check if the value is known to be null. 226 if (V.isUndef()) 227 return; 228 229 const MemRegion *MR = L.getAsRegion(); 230 const TypedValueRegion *TVR = dyn_cast_or_null<TypedValueRegion>(MR); 231 if (!TVR) 232 return; 233 234 if (!TVR->getValueType()->isReferenceType()) 235 return; 236 237 ProgramStateRef State = C.getState(); 238 239 ProgramStateRef StNonNull, StNull; 240 std::tie(StNonNull, StNull) = State->assume(V.castAs<DefinedOrUnknownSVal>()); 241 242 if (StNull) { 243 if (!StNonNull) { 244 reportBug(StNull, S, C, /*isBind=*/true); 245 return; 246 } 247 248 // At this point the value could be either null or non-null. 249 // Record this as an "implicit" null dereference. 250 if (ExplodedNode *N = C.generateSink(StNull)) { 251 ImplicitNullDerefEvent event = { V, /*isLoad=*/true, N, 252 &C.getBugReporter() }; 253 dispatchEvent(event); 254 } 255 } 256 257 // Unlike a regular null dereference, initializing a reference with a 258 // dereferenced null pointer does not actually cause a runtime exception in 259 // Clang's implementation of references. 260 // 261 // int &r = *p; // safe?? 262 // if (p != NULL) return; // uh-oh 263 // r = 5; // trap here 264 // 265 // The standard says this is invalid as soon as we try to create a "null 266 // reference" (there is no such thing), but turning this into an assumption 267 // that 'p' is never null will not match our actual runtime behavior. 268 // So we do not record this assumption, allowing us to warn on the last line 269 // of this example. 270 // 271 // We do need to add a transition because we may have generated a sink for 272 // the "implicit" null dereference. 273 C.addTransition(State, this); 274 } 275 276 void ento::registerDereferenceChecker(CheckerManager &mgr) { 277 mgr.registerChecker<DereferenceChecker>(); 278 } 279