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