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