Home | History | Annotate | Download | only in Checkers
      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 (Optional<nonloc::LazyCompoundVal> LV =
    138           V.getAs<nonloc::LazyCompoundVal>()) {
    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) =
    240       State->assume(L.castAs<DefinedOrUnknownSVal>());
    241 
    242   if (StNull && !StNonNull) {
    243     if (!BT_call_null)
    244       BT_call_null.reset(
    245         new BuiltinBug("Called function pointer is null (null dereference)"));
    246     emitBadCall(BT_call_null.get(), C, Callee);
    247   }
    248 
    249   C.addTransition(StNonNull);
    250 }
    251 
    252 void CallAndMessageChecker::checkPreCall(const CallEvent &Call,
    253                                          CheckerContext &C) const {
    254   ProgramStateRef State = C.getState();
    255 
    256   // If this is a call to a C++ method, check if the callee is null or
    257   // undefined.
    258   if (const CXXInstanceCall *CC = dyn_cast<CXXInstanceCall>(&Call)) {
    259     SVal V = CC->getCXXThisVal();
    260     if (V.isUndef()) {
    261       if (!BT_cxx_call_undef)
    262         BT_cxx_call_undef.reset(new BuiltinBug("Called C++ object pointer is "
    263                                                "uninitialized"));
    264       emitBadCall(BT_cxx_call_undef.get(), C, CC->getCXXThisExpr());
    265       return;
    266     }
    267 
    268     ProgramStateRef StNonNull, StNull;
    269     llvm::tie(StNonNull, StNull) =
    270         State->assume(V.castAs<DefinedOrUnknownSVal>());
    271 
    272     if (StNull && !StNonNull) {
    273       if (!BT_cxx_call_null)
    274         BT_cxx_call_null.reset(new BuiltinBug("Called C++ object pointer "
    275                                               "is null"));
    276       emitBadCall(BT_cxx_call_null.get(), C, CC->getCXXThisExpr());
    277       return;
    278     }
    279 
    280     State = StNonNull;
    281   }
    282 
    283   // Don't check for uninitialized field values in arguments if the
    284   // caller has a body that is available and we have the chance to inline it.
    285   // This is a hack, but is a reasonable compromise betweens sometimes warning
    286   // and sometimes not depending on if we decide to inline a function.
    287   const Decl *D = Call.getDecl();
    288   const bool checkUninitFields =
    289     !(C.getAnalysisManager().shouldInlineCall() && (D && D->getBody()));
    290 
    291   OwningPtr<BugType> *BT;
    292   if (isa<ObjCMethodCall>(Call))
    293     BT = &BT_msg_arg;
    294   else
    295     BT = &BT_call_arg;
    296 
    297   for (unsigned i = 0, e = Call.getNumArgs(); i != e; ++i)
    298     if (PreVisitProcessArg(C, Call.getArgSVal(i), Call.getArgSourceRange(i),
    299                            Call.getArgExpr(i), /*IsFirstArgument=*/i == 0,
    300                            checkUninitFields, Call, *BT))
    301       return;
    302 
    303   // If we make it here, record our assumptions about the callee.
    304   C.addTransition(State);
    305 }
    306 
    307 void CallAndMessageChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
    308                                                 CheckerContext &C) const {
    309   SVal recVal = msg.getReceiverSVal();
    310   if (recVal.isUndef()) {
    311     if (ExplodedNode *N = C.generateSink()) {
    312       BugType *BT = 0;
    313       switch (msg.getMessageKind()) {
    314       case OCM_Message:
    315         if (!BT_msg_undef)
    316           BT_msg_undef.reset(new BuiltinBug("Receiver in message expression "
    317                                             "is an uninitialized value"));
    318         BT = BT_msg_undef.get();
    319         break;
    320       case OCM_PropertyAccess:
    321         if (!BT_objc_prop_undef)
    322           BT_objc_prop_undef.reset(new BuiltinBug("Property access on an "
    323                                                   "uninitialized object "
    324                                                   "pointer"));
    325         BT = BT_objc_prop_undef.get();
    326         break;
    327       case OCM_Subscript:
    328         if (!BT_objc_subscript_undef)
    329           BT_objc_subscript_undef.reset(new BuiltinBug("Subscript access on an "
    330                                                        "uninitialized object "
    331                                                        "pointer"));
    332         BT = BT_objc_subscript_undef.get();
    333         break;
    334       }
    335       assert(BT && "Unknown message kind.");
    336 
    337       BugReport *R = new BugReport(*BT, BT->getName(), N);
    338       const ObjCMessageExpr *ME = msg.getOriginExpr();
    339       R->addRange(ME->getReceiverRange());
    340 
    341       // FIXME: getTrackNullOrUndefValueVisitor can't handle "super" yet.
    342       if (const Expr *ReceiverE = ME->getInstanceReceiver())
    343         bugreporter::trackNullOrUndefValue(N, ReceiverE, *R);
    344       C.emitReport(R);
    345     }
    346     return;
    347   } else {
    348     // Bifurcate the state into nil and non-nil ones.
    349     DefinedOrUnknownSVal receiverVal = recVal.castAs<DefinedOrUnknownSVal>();
    350 
    351     ProgramStateRef state = C.getState();
    352     ProgramStateRef notNilState, nilState;
    353     llvm::tie(notNilState, nilState) = state->assume(receiverVal);
    354 
    355     // Handle receiver must be nil.
    356     if (nilState && !notNilState) {
    357       HandleNilReceiver(C, state, msg);
    358       return;
    359     }
    360   }
    361 }
    362 
    363 void CallAndMessageChecker::emitNilReceiverBug(CheckerContext &C,
    364                                                const ObjCMethodCall &msg,
    365                                                ExplodedNode *N) const {
    366 
    367   if (!BT_msg_ret)
    368     BT_msg_ret.reset(
    369       new BuiltinBug("Receiver in message expression is "
    370                      "'nil' and returns a garbage value"));
    371 
    372   const ObjCMessageExpr *ME = msg.getOriginExpr();
    373 
    374   SmallString<200> buf;
    375   llvm::raw_svector_ostream os(buf);
    376   os << "The receiver of message '" << ME->getSelector().getAsString()
    377      << "' is nil and returns a value of type '";
    378   msg.getResultType().print(os, C.getLangOpts());
    379   os << "' that will be garbage";
    380 
    381   BugReport *report = new BugReport(*BT_msg_ret, os.str(), N);
    382   report->addRange(ME->getReceiverRange());
    383   // FIXME: This won't track "self" in messages to super.
    384   if (const Expr *receiver = ME->getInstanceReceiver()) {
    385     bugreporter::trackNullOrUndefValue(N, receiver, *report);
    386   }
    387   C.emitReport(report);
    388 }
    389 
    390 static bool supportsNilWithFloatRet(const llvm::Triple &triple) {
    391   return (triple.getVendor() == llvm::Triple::Apple &&
    392           (triple.getOS() == llvm::Triple::IOS ||
    393            !triple.isMacOSXVersionLT(10,5)));
    394 }
    395 
    396 void CallAndMessageChecker::HandleNilReceiver(CheckerContext &C,
    397                                               ProgramStateRef state,
    398                                               const ObjCMethodCall &Msg) const {
    399   ASTContext &Ctx = C.getASTContext();
    400 
    401   // Check the return type of the message expression.  A message to nil will
    402   // return different values depending on the return type and the architecture.
    403   QualType RetTy = Msg.getResultType();
    404   CanQualType CanRetTy = Ctx.getCanonicalType(RetTy);
    405   const LocationContext *LCtx = C.getLocationContext();
    406 
    407   if (CanRetTy->isStructureOrClassType()) {
    408     // Structure returns are safe since the compiler zeroes them out.
    409     SVal V = C.getSValBuilder().makeZeroVal(RetTy);
    410     C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V));
    411     return;
    412   }
    413 
    414   // Other cases: check if sizeof(return type) > sizeof(void*)
    415   if (CanRetTy != Ctx.VoidTy && C.getLocationContext()->getParentMap()
    416                                   .isConsumedExpr(Msg.getOriginExpr())) {
    417     // Compute: sizeof(void *) and sizeof(return type)
    418     const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
    419     const uint64_t returnTypeSize = Ctx.getTypeSize(CanRetTy);
    420 
    421     if (voidPtrSize < returnTypeSize &&
    422         !(supportsNilWithFloatRet(Ctx.getTargetInfo().getTriple()) &&
    423           (Ctx.FloatTy == CanRetTy ||
    424            Ctx.DoubleTy == CanRetTy ||
    425            Ctx.LongDoubleTy == CanRetTy ||
    426            Ctx.LongLongTy == CanRetTy ||
    427            Ctx.UnsignedLongLongTy == CanRetTy))) {
    428       if (ExplodedNode *N = C.generateSink(state))
    429         emitNilReceiverBug(C, Msg, N);
    430       return;
    431     }
    432 
    433     // Handle the safe cases where the return value is 0 if the
    434     // receiver is nil.
    435     //
    436     // FIXME: For now take the conservative approach that we only
    437     // return null values if we *know* that the receiver is nil.
    438     // This is because we can have surprises like:
    439     //
    440     //   ... = [[NSScreens screens] objectAtIndex:0];
    441     //
    442     // What can happen is that [... screens] could return nil, but
    443     // it most likely isn't nil.  We should assume the semantics
    444     // of this case unless we have *a lot* more knowledge.
    445     //
    446     SVal V = C.getSValBuilder().makeZeroVal(RetTy);
    447     C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V));
    448     return;
    449   }
    450 
    451   C.addTransition(state);
    452 }
    453 
    454 void ento::registerCallAndMessageChecker(CheckerManager &mgr) {
    455   mgr.registerChecker<CallAndMessageChecker>();
    456 }
    457