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