1 //===--- CallAndMessageChecker.cpp ------------------------------*- 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 CallAndMessageChecker, a builtin checker that checks for various 11 // errors of call and objc message expressions. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "ClangSACheckers.h" 16 #include "clang/StaticAnalyzer/Core/Checker.h" 17 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 18 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 19 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 20 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 21 #include "clang/AST/ParentMap.h" 22 #include "clang/Basic/TargetInfo.h" 23 #include "llvm/ADT/SmallString.h" 24 25 using namespace clang; 26 using namespace ento; 27 28 namespace { 29 class CallAndMessageChecker 30 : public Checker< check::PreStmt<CallExpr>, check::PreObjCMessage, 31 check::PreCall > { 32 mutable OwningPtr<BugType> BT_call_null; 33 mutable OwningPtr<BugType> BT_call_undef; 34 mutable OwningPtr<BugType> BT_cxx_call_null; 35 mutable OwningPtr<BugType> BT_cxx_call_undef; 36 mutable OwningPtr<BugType> BT_call_arg; 37 mutable OwningPtr<BugType> BT_msg_undef; 38 mutable OwningPtr<BugType> BT_objc_prop_undef; 39 mutable OwningPtr<BugType> BT_objc_subscript_undef; 40 mutable OwningPtr<BugType> BT_msg_arg; 41 mutable OwningPtr<BugType> BT_msg_ret; 42 public: 43 44 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; 45 void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const; 46 void checkPreCall(const CallEvent &Call, CheckerContext &C) const; 47 48 private: 49 static bool PreVisitProcessArg(CheckerContext &C, SVal V, 50 SourceRange argRange, const Expr *argEx, 51 bool IsFirstArgument, bool checkUninitFields, 52 const CallEvent &Call, OwningPtr<BugType> &BT); 53 54 static void emitBadCall(BugType *BT, CheckerContext &C, const Expr *BadE); 55 void emitNilReceiverBug(CheckerContext &C, const ObjCMethodCall &msg, 56 ExplodedNode *N) const; 57 58 void HandleNilReceiver(CheckerContext &C, 59 ProgramStateRef state, 60 const ObjCMethodCall &msg) const; 61 62 static void LazyInit_BT(const char *desc, OwningPtr<BugType> &BT) { 63 if (!BT) 64 BT.reset(new BuiltinBug(desc)); 65 } 66 }; 67 } // end anonymous namespace 68 69 void CallAndMessageChecker::emitBadCall(BugType *BT, CheckerContext &C, 70 const Expr *BadE) { 71 ExplodedNode *N = C.generateSink(); 72 if (!N) 73 return; 74 75 BugReport *R = new BugReport(*BT, BT->getName(), N); 76 if (BadE) { 77 R->addRange(BadE->getSourceRange()); 78 bugreporter::addTrackNullOrUndefValueVisitor(N, BadE, R); 79 } 80 C.EmitReport(R); 81 } 82 83 StringRef describeUninitializedArgumentInCall(const CallEvent &Call, 84 bool IsFirstArgument) { 85 switch (Call.getKind()) { 86 case CE_ObjCMessage: { 87 const ObjCMethodCall &Msg = cast<ObjCMethodCall>(Call); 88 switch (Msg.getMessageKind()) { 89 case OCM_Message: 90 return "Argument in message expression is an uninitialized value"; 91 case OCM_PropertyAccess: 92 assert(Msg.isSetter() && "Getters have no args"); 93 return "Argument for property setter is an uninitialized value"; 94 case OCM_Subscript: 95 if (Msg.isSetter() && IsFirstArgument) 96 return "Argument for subscript setter is an uninitialized value"; 97 return "Subscript index is an uninitialized value"; 98 } 99 llvm_unreachable("Unknown message kind."); 100 } 101 case CE_Block: 102 return "Block call argument is an uninitialized value"; 103 default: 104 return "Function call argument is an uninitialized value"; 105 } 106 } 107 108 bool CallAndMessageChecker::PreVisitProcessArg(CheckerContext &C, 109 SVal V, SourceRange argRange, 110 const Expr *argEx, 111 bool IsFirstArgument, 112 bool checkUninitFields, 113 const CallEvent &Call, 114 OwningPtr<BugType> &BT) { 115 if (V.isUndef()) { 116 if (ExplodedNode *N = C.generateSink()) { 117 LazyInit_BT("Uninitialized argument value", BT); 118 119 // Generate a report for this bug. 120 StringRef Desc = describeUninitializedArgumentInCall(Call, 121 IsFirstArgument); 122 BugReport *R = new BugReport(*BT, Desc, N); 123 R->addRange(argRange); 124 if (argEx) 125 bugreporter::addTrackNullOrUndefValueVisitor(N, argEx, R); 126 C.EmitReport(R); 127 } 128 return true; 129 } 130 131 if (!checkUninitFields) 132 return false; 133 134 if (const nonloc::LazyCompoundVal *LV = 135 dyn_cast<nonloc::LazyCompoundVal>(&V)) { 136 137 class FindUninitializedField { 138 public: 139 SmallVector<const FieldDecl *, 10> FieldChain; 140 private: 141 StoreManager &StoreMgr; 142 MemRegionManager &MrMgr; 143 Store store; 144 public: 145 FindUninitializedField(StoreManager &storeMgr, 146 MemRegionManager &mrMgr, Store s) 147 : StoreMgr(storeMgr), MrMgr(mrMgr), store(s) {} 148 149 bool Find(const TypedValueRegion *R) { 150 QualType T = R->getValueType(); 151 if (const RecordType *RT = T->getAsStructureType()) { 152 const RecordDecl *RD = RT->getDecl()->getDefinition(); 153 assert(RD && "Referred record has no definition"); 154 for (RecordDecl::field_iterator I = 155 RD->field_begin(), E = RD->field_end(); I!=E; ++I) { 156 const FieldRegion *FR = MrMgr.getFieldRegion(*I, R); 157 FieldChain.push_back(*I); 158 T = I->getType(); 159 if (T->getAsStructureType()) { 160 if (Find(FR)) 161 return true; 162 } 163 else { 164 const SVal &V = StoreMgr.getBinding(store, loc::MemRegionVal(FR)); 165 if (V.isUndef()) 166 return true; 167 } 168 FieldChain.pop_back(); 169 } 170 } 171 172 return false; 173 } 174 }; 175 176 const LazyCompoundValData *D = LV->getCVData(); 177 FindUninitializedField F(C.getState()->getStateManager().getStoreManager(), 178 C.getSValBuilder().getRegionManager(), 179 D->getStore()); 180 181 if (F.Find(D->getRegion())) { 182 if (ExplodedNode *N = C.generateSink()) { 183 LazyInit_BT("Uninitialized argument value", BT); 184 SmallString<512> Str; 185 llvm::raw_svector_ostream os(Str); 186 os << "Passed-by-value struct argument contains uninitialized data"; 187 188 if (F.FieldChain.size() == 1) 189 os << " (e.g., field: '" << *F.FieldChain[0] << "')"; 190 else { 191 os << " (e.g., via the field chain: '"; 192 bool first = true; 193 for (SmallVectorImpl<const FieldDecl *>::iterator 194 DI = F.FieldChain.begin(), DE = F.FieldChain.end(); DI!=DE;++DI){ 195 if (first) 196 first = false; 197 else 198 os << '.'; 199 os << **DI; 200 } 201 os << "')"; 202 } 203 204 // Generate a report for this bug. 205 BugReport *R = new BugReport(*BT, os.str(), N); 206 R->addRange(argRange); 207 208 // FIXME: enhance track back for uninitialized value for arbitrary 209 // memregions 210 C.EmitReport(R); 211 } 212 return true; 213 } 214 } 215 216 return false; 217 } 218 219 void CallAndMessageChecker::checkPreStmt(const CallExpr *CE, 220 CheckerContext &C) const{ 221 222 const Expr *Callee = CE->getCallee()->IgnoreParens(); 223 ProgramStateRef State = C.getState(); 224 const LocationContext *LCtx = C.getLocationContext(); 225 SVal L = State->getSVal(Callee, LCtx); 226 227 if (L.isUndef()) { 228 if (!BT_call_undef) 229 BT_call_undef.reset(new BuiltinBug("Called function pointer is an " 230 "uninitalized pointer value")); 231 emitBadCall(BT_call_undef.get(), C, Callee); 232 return; 233 } 234 235 if (L.isZeroConstant()) { 236 if (!BT_call_null) 237 BT_call_null.reset( 238 new BuiltinBug("Called function pointer is null (null dereference)")); 239 emitBadCall(BT_call_null.get(), C, Callee); 240 } 241 } 242 243 void CallAndMessageChecker::checkPreCall(const CallEvent &Call, 244 CheckerContext &C) const { 245 // If this is a call to a C++ method, check if the callee is null or 246 // undefined. 247 if (const CXXInstanceCall *CC = dyn_cast<CXXInstanceCall>(&Call)) { 248 SVal V = CC->getCXXThisVal(); 249 if (V.isUndef()) { 250 if (!BT_cxx_call_undef) 251 BT_cxx_call_undef.reset(new BuiltinBug("Called C++ object pointer is " 252 "uninitialized")); 253 emitBadCall(BT_cxx_call_undef.get(), C, CC->getCXXThisExpr()); 254 return; 255 } 256 if (V.isZeroConstant()) { 257 if (!BT_cxx_call_null) 258 BT_cxx_call_null.reset(new BuiltinBug("Called C++ object pointer " 259 "is null")); 260 emitBadCall(BT_cxx_call_null.get(), C, CC->getCXXThisExpr()); 261 return; 262 } 263 } 264 265 // Don't check for uninitialized field values in arguments if the 266 // caller has a body that is available and we have the chance to inline it. 267 // This is a hack, but is a reasonable compromise betweens sometimes warning 268 // and sometimes not depending on if we decide to inline a function. 269 const Decl *D = Call.getDecl(); 270 const bool checkUninitFields = 271 !(C.getAnalysisManager().shouldInlineCall() && (D && D->getBody())); 272 273 OwningPtr<BugType> *BT; 274 if (isa<ObjCMethodCall>(Call)) 275 BT = &BT_msg_arg; 276 else 277 BT = &BT_call_arg; 278 279 for (unsigned i = 0, e = Call.getNumArgs(); i != e; ++i) 280 if (PreVisitProcessArg(C, Call.getArgSVal(i), Call.getArgSourceRange(i), 281 Call.getArgExpr(i), /*IsFirstArgument=*/i == 0, 282 checkUninitFields, Call, *BT)) 283 return; 284 } 285 286 void CallAndMessageChecker::checkPreObjCMessage(const ObjCMethodCall &msg, 287 CheckerContext &C) const { 288 SVal recVal = msg.getReceiverSVal(); 289 if (recVal.isUndef()) { 290 if (ExplodedNode *N = C.generateSink()) { 291 BugType *BT = 0; 292 switch (msg.getMessageKind()) { 293 case OCM_Message: 294 if (!BT_msg_undef) 295 BT_msg_undef.reset(new BuiltinBug("Receiver in message expression " 296 "is an uninitialized value")); 297 BT = BT_msg_undef.get(); 298 break; 299 case OCM_PropertyAccess: 300 if (!BT_objc_prop_undef) 301 BT_objc_prop_undef.reset(new BuiltinBug("Property access on an " 302 "uninitialized object " 303 "pointer")); 304 BT = BT_objc_prop_undef.get(); 305 break; 306 case OCM_Subscript: 307 if (!BT_objc_subscript_undef) 308 BT_objc_subscript_undef.reset(new BuiltinBug("Subscript access on an " 309 "uninitialized object " 310 "pointer")); 311 BT = BT_objc_subscript_undef.get(); 312 break; 313 } 314 assert(BT && "Unknown message kind."); 315 316 BugReport *R = new BugReport(*BT, BT->getName(), N); 317 const ObjCMessageExpr *ME = msg.getOriginExpr(); 318 R->addRange(ME->getReceiverRange()); 319 320 // FIXME: getTrackNullOrUndefValueVisitor can't handle "super" yet. 321 if (const Expr *ReceiverE = ME->getInstanceReceiver()) 322 bugreporter::addTrackNullOrUndefValueVisitor(N, ReceiverE, R); 323 C.EmitReport(R); 324 } 325 return; 326 } else { 327 // Bifurcate the state into nil and non-nil ones. 328 DefinedOrUnknownSVal receiverVal = cast<DefinedOrUnknownSVal>(recVal); 329 330 ProgramStateRef state = C.getState(); 331 ProgramStateRef notNilState, nilState; 332 llvm::tie(notNilState, nilState) = state->assume(receiverVal); 333 334 // Handle receiver must be nil. 335 if (nilState && !notNilState) { 336 HandleNilReceiver(C, state, msg); 337 return; 338 } 339 } 340 } 341 342 void CallAndMessageChecker::emitNilReceiverBug(CheckerContext &C, 343 const ObjCMethodCall &msg, 344 ExplodedNode *N) const { 345 346 if (!BT_msg_ret) 347 BT_msg_ret.reset( 348 new BuiltinBug("Receiver in message expression is " 349 "'nil' and returns a garbage value")); 350 351 const ObjCMessageExpr *ME = msg.getOriginExpr(); 352 353 SmallString<200> buf; 354 llvm::raw_svector_ostream os(buf); 355 os << "The receiver of message '" << ME->getSelector().getAsString() 356 << "' is nil and returns a value of type '"; 357 msg.getResultType().print(os, C.getLangOpts()); 358 os << "' that will be garbage"; 359 360 BugReport *report = new BugReport(*BT_msg_ret, os.str(), N); 361 report->addRange(ME->getReceiverRange()); 362 // FIXME: This won't track "self" in messages to super. 363 if (const Expr *receiver = ME->getInstanceReceiver()) { 364 bugreporter::addTrackNullOrUndefValueVisitor(N, receiver, report); 365 } 366 C.EmitReport(report); 367 } 368 369 static bool supportsNilWithFloatRet(const llvm::Triple &triple) { 370 return (triple.getVendor() == llvm::Triple::Apple && 371 (triple.getOS() == llvm::Triple::IOS || 372 !triple.isMacOSXVersionLT(10,5))); 373 } 374 375 void CallAndMessageChecker::HandleNilReceiver(CheckerContext &C, 376 ProgramStateRef state, 377 const ObjCMethodCall &Msg) const { 378 ASTContext &Ctx = C.getASTContext(); 379 380 // Check the return type of the message expression. A message to nil will 381 // return different values depending on the return type and the architecture. 382 QualType RetTy = Msg.getResultType(); 383 CanQualType CanRetTy = Ctx.getCanonicalType(RetTy); 384 const LocationContext *LCtx = C.getLocationContext(); 385 386 if (CanRetTy->isStructureOrClassType()) { 387 // Structure returns are safe since the compiler zeroes them out. 388 SVal V = C.getSValBuilder().makeZeroVal(RetTy); 389 C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V)); 390 return; 391 } 392 393 // Other cases: check if sizeof(return type) > sizeof(void*) 394 if (CanRetTy != Ctx.VoidTy && C.getLocationContext()->getParentMap() 395 .isConsumedExpr(Msg.getOriginExpr())) { 396 // Compute: sizeof(void *) and sizeof(return type) 397 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy); 398 const uint64_t returnTypeSize = Ctx.getTypeSize(CanRetTy); 399 400 if (voidPtrSize < returnTypeSize && 401 !(supportsNilWithFloatRet(Ctx.getTargetInfo().getTriple()) && 402 (Ctx.FloatTy == CanRetTy || 403 Ctx.DoubleTy == CanRetTy || 404 Ctx.LongDoubleTy == CanRetTy || 405 Ctx.LongLongTy == CanRetTy || 406 Ctx.UnsignedLongLongTy == CanRetTy))) { 407 if (ExplodedNode *N = C.generateSink(state)) 408 emitNilReceiverBug(C, Msg, N); 409 return; 410 } 411 412 // Handle the safe cases where the return value is 0 if the 413 // receiver is nil. 414 // 415 // FIXME: For now take the conservative approach that we only 416 // return null values if we *know* that the receiver is nil. 417 // This is because we can have surprises like: 418 // 419 // ... = [[NSScreens screens] objectAtIndex:0]; 420 // 421 // What can happen is that [... screens] could return nil, but 422 // it most likely isn't nil. We should assume the semantics 423 // of this case unless we have *a lot* more knowledge. 424 // 425 SVal V = C.getSValBuilder().makeZeroVal(RetTy); 426 C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V)); 427 return; 428 } 429 430 C.addTransition(state); 431 } 432 433 void ento::registerCallAndMessageChecker(CheckerManager &mgr) { 434 mgr.registerChecker<CallAndMessageChecker>(); 435 } 436