1 //===--- CallAndMessageChecker.cpp ------------------------------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines CallAndMessageChecker, a builtin checker that checks for various
11 // errors of call and objc message expressions.
13 //===----------------------------------------------------------------------===//
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"
25 using namespace clang;
29 class CallAndMessageChecker
30 : public Checker< check::PreStmt<CallExpr>, check::PreObjCMessage,
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;
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;
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);
54 static void emitBadCall(BugType *BT, CheckerContext &C, const Expr *BadE);
55 void emitNilReceiverBug(CheckerContext &C, const ObjCMethodCall &msg,
56 ExplodedNode *N) const;
58 void HandleNilReceiver(CheckerContext &C,
59 ProgramStateRef state,
60 const ObjCMethodCall &msg) const;
62 static void LazyInit_BT(const char *desc, OwningPtr<BugType> &BT) {
64 BT.reset(new BuiltinBug(desc));
67 } // end anonymous namespace
69 void CallAndMessageChecker::emitBadCall(BugType *BT, CheckerContext &C,
71 ExplodedNode *N = C.generateSink();
75 BugReport *R = new BugReport(*BT, BT->getName(), N);
77 R->addRange(BadE->getSourceRange());
78 bugreporter::trackNullOrUndefValue(N, BadE, *R);
83 static 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()) {
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";
95 if (Msg.isSetter() && IsFirstArgument)
96 return "Argument for subscript setter is an uninitialized value";
97 return "Subscript index is an uninitialized value";
99 llvm_unreachable("Unknown message kind.");
102 return "Block call argument is an uninitialized value";
104 return "Function call argument is an uninitialized value";
108 bool CallAndMessageChecker::PreVisitProcessArg(CheckerContext &C,
109 SVal V, SourceRange argRange,
111 bool IsFirstArgument,
112 bool checkUninitFields,
113 const CallEvent &Call,
114 OwningPtr<BugType> &BT) {
116 if (ExplodedNode *N = C.generateSink()) {
117 LazyInit_BT("Uninitialized argument value", BT);
119 // Generate a report for this bug.
120 StringRef Desc = describeUninitializedArgumentInCall(Call,
122 BugReport *R = new BugReport(*BT, Desc, N);
123 R->addRange(argRange);
125 bugreporter::trackNullOrUndefValue(N, argEx, *R);
131 if (!checkUninitFields)
134 if (const nonloc::LazyCompoundVal *LV =
135 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
137 class FindUninitializedField {
139 SmallVector<const FieldDecl *, 10> FieldChain;
141 StoreManager &StoreMgr;
142 MemRegionManager &MrMgr;
145 FindUninitializedField(StoreManager &storeMgr,
146 MemRegionManager &mrMgr, Store s)
147 : StoreMgr(storeMgr), MrMgr(mrMgr), store(s) {}
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);
159 if (T->getAsStructureType()) {
164 const SVal &V = StoreMgr.getBinding(store, loc::MemRegionVal(FR));
168 FieldChain.pop_back();
176 const LazyCompoundValData *D = LV->getCVData();
177 FindUninitializedField F(C.getState()->getStateManager().getStoreManager(),
178 C.getSValBuilder().getRegionManager(),
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";
188 if (F.FieldChain.size() == 1)
189 os << " (e.g., field: '" << *F.FieldChain[0] << "')";
191 os << " (e.g., via the field chain: '";
193 for (SmallVectorImpl<const FieldDecl *>::iterator
194 DI = F.FieldChain.begin(), DE = F.FieldChain.end(); DI!=DE;++DI){
204 // Generate a report for this bug.
205 BugReport *R = new BugReport(*BT, os.str(), N);
206 R->addRange(argRange);
208 // FIXME: enhance track back for uninitialized value for arbitrary
219 void CallAndMessageChecker::checkPreStmt(const CallExpr *CE,
220 CheckerContext &C) const{
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);
229 BT_call_undef.reset(new BuiltinBug("Called function pointer is an "
230 "uninitalized pointer value"));
231 emitBadCall(BT_call_undef.get(), C, Callee);
235 ProgramStateRef StNonNull, StNull;
236 llvm::tie(StNonNull, StNull) = State->assume(cast<DefinedOrUnknownSVal>(L));
238 if (StNull && !StNonNull) {
241 new BuiltinBug("Called function pointer is null (null dereference)"));
242 emitBadCall(BT_call_null.get(), C, Callee);
245 C.addTransition(StNonNull);
248 void CallAndMessageChecker::checkPreCall(const CallEvent &Call,
249 CheckerContext &C) const {
250 ProgramStateRef State = C.getState();
252 // If this is a call to a C++ method, check if the callee is null or
254 if (const CXXInstanceCall *CC = dyn_cast<CXXInstanceCall>(&Call)) {
255 SVal V = CC->getCXXThisVal();
257 if (!BT_cxx_call_undef)
258 BT_cxx_call_undef.reset(new BuiltinBug("Called C++ object pointer is "
260 emitBadCall(BT_cxx_call_undef.get(), C, CC->getCXXThisExpr());
264 ProgramStateRef StNonNull, StNull;
265 llvm::tie(StNonNull, StNull) = State->assume(cast<DefinedOrUnknownSVal>(V));
267 if (StNull && !StNonNull) {
268 if (!BT_cxx_call_null)
269 BT_cxx_call_null.reset(new BuiltinBug("Called C++ object pointer "
271 emitBadCall(BT_cxx_call_null.get(), C, CC->getCXXThisExpr());
278 // Don't check for uninitialized field values in arguments if the
279 // caller has a body that is available and we have the chance to inline it.
280 // This is a hack, but is a reasonable compromise betweens sometimes warning
281 // and sometimes not depending on if we decide to inline a function.
282 const Decl *D = Call.getDecl();
283 const bool checkUninitFields =
284 !(C.getAnalysisManager().shouldInlineCall() && (D && D->getBody()));
286 OwningPtr<BugType> *BT;
287 if (isa<ObjCMethodCall>(Call))
292 for (unsigned i = 0, e = Call.getNumArgs(); i != e; ++i)
293 if (PreVisitProcessArg(C, Call.getArgSVal(i), Call.getArgSourceRange(i),
294 Call.getArgExpr(i), /*IsFirstArgument=*/i == 0,
295 checkUninitFields, Call, *BT))
298 // If we make it here, record our assumptions about the callee.
299 C.addTransition(State);
302 void CallAndMessageChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
303 CheckerContext &C) const {
304 SVal recVal = msg.getReceiverSVal();
305 if (recVal.isUndef()) {
306 if (ExplodedNode *N = C.generateSink()) {
308 switch (msg.getMessageKind()) {
311 BT_msg_undef.reset(new BuiltinBug("Receiver in message expression "
312 "is an uninitialized value"));
313 BT = BT_msg_undef.get();
315 case OCM_PropertyAccess:
316 if (!BT_objc_prop_undef)
317 BT_objc_prop_undef.reset(new BuiltinBug("Property access on an "
318 "uninitialized object "
320 BT = BT_objc_prop_undef.get();
323 if (!BT_objc_subscript_undef)
324 BT_objc_subscript_undef.reset(new BuiltinBug("Subscript access on an "
325 "uninitialized object "
327 BT = BT_objc_subscript_undef.get();
330 assert(BT && "Unknown message kind.");
332 BugReport *R = new BugReport(*BT, BT->getName(), N);
333 const ObjCMessageExpr *ME = msg.getOriginExpr();
334 R->addRange(ME->getReceiverRange());
336 // FIXME: getTrackNullOrUndefValueVisitor can't handle "super" yet.
337 if (const Expr *ReceiverE = ME->getInstanceReceiver())
338 bugreporter::trackNullOrUndefValue(N, ReceiverE, *R);
343 // Bifurcate the state into nil and non-nil ones.
344 DefinedOrUnknownSVal receiverVal = cast<DefinedOrUnknownSVal>(recVal);
346 ProgramStateRef state = C.getState();
347 ProgramStateRef notNilState, nilState;
348 llvm::tie(notNilState, nilState) = state->assume(receiverVal);
350 // Handle receiver must be nil.
351 if (nilState && !notNilState) {
352 HandleNilReceiver(C, state, msg);
358 void CallAndMessageChecker::emitNilReceiverBug(CheckerContext &C,
359 const ObjCMethodCall &msg,
360 ExplodedNode *N) const {
364 new BuiltinBug("Receiver in message expression is "
365 "'nil' and returns a garbage value"));
367 const ObjCMessageExpr *ME = msg.getOriginExpr();
369 SmallString<200> buf;
370 llvm::raw_svector_ostream os(buf);
371 os << "The receiver of message '" << ME->getSelector().getAsString()
372 << "' is nil and returns a value of type '";
373 msg.getResultType().print(os, C.getLangOpts());
374 os << "' that will be garbage";
376 BugReport *report = new BugReport(*BT_msg_ret, os.str(), N);
377 report->addRange(ME->getReceiverRange());
378 // FIXME: This won't track "self" in messages to super.
379 if (const Expr *receiver = ME->getInstanceReceiver()) {
380 bugreporter::trackNullOrUndefValue(N, receiver, *report);
382 C.emitReport(report);
385 static bool supportsNilWithFloatRet(const llvm::Triple &triple) {
386 return (triple.getVendor() == llvm::Triple::Apple &&
387 (triple.getOS() == llvm::Triple::IOS ||
388 !triple.isMacOSXVersionLT(10,5)));
391 void CallAndMessageChecker::HandleNilReceiver(CheckerContext &C,
392 ProgramStateRef state,
393 const ObjCMethodCall &Msg) const {
394 ASTContext &Ctx = C.getASTContext();
396 // Check the return type of the message expression. A message to nil will
397 // return different values depending on the return type and the architecture.
398 QualType RetTy = Msg.getResultType();
399 CanQualType CanRetTy = Ctx.getCanonicalType(RetTy);
400 const LocationContext *LCtx = C.getLocationContext();
402 if (CanRetTy->isStructureOrClassType()) {
403 // Structure returns are safe since the compiler zeroes them out.
404 SVal V = C.getSValBuilder().makeZeroVal(RetTy);
405 C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V));
409 // Other cases: check if sizeof(return type) > sizeof(void*)
410 if (CanRetTy != Ctx.VoidTy && C.getLocationContext()->getParentMap()
411 .isConsumedExpr(Msg.getOriginExpr())) {
412 // Compute: sizeof(void *) and sizeof(return type)
413 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
414 const uint64_t returnTypeSize = Ctx.getTypeSize(CanRetTy);
416 if (voidPtrSize < returnTypeSize &&
417 !(supportsNilWithFloatRet(Ctx.getTargetInfo().getTriple()) &&
418 (Ctx.FloatTy == CanRetTy ||
419 Ctx.DoubleTy == CanRetTy ||
420 Ctx.LongDoubleTy == CanRetTy ||
421 Ctx.LongLongTy == CanRetTy ||
422 Ctx.UnsignedLongLongTy == CanRetTy))) {
423 if (ExplodedNode *N = C.generateSink(state))
424 emitNilReceiverBug(C, Msg, N);
428 // Handle the safe cases where the return value is 0 if the
431 // FIXME: For now take the conservative approach that we only
432 // return null values if we *know* that the receiver is nil.
433 // This is because we can have surprises like:
435 // ... = [[NSScreens screens] objectAtIndex:0];
437 // What can happen is that [... screens] could return nil, but
438 // it most likely isn't nil. We should assume the semantics
439 // of this case unless we have *a lot* more knowledge.
441 SVal V = C.getSValBuilder().makeZeroVal(RetTy);
442 C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V));
446 C.addTransition(state);
449 void ento::registerCallAndMessageChecker(CheckerManager &mgr) {
450 mgr.registerChecker<CallAndMessageChecker>();