2009-11-24 12:45:44 +08:00
|
|
|
//===--- CallAndMessageChecker.cpp ------------------------------*- C++ -*--==//
|
2009-11-03 14:46:03 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-11-24 12:45:44 +08:00
|
|
|
// This defines CallAndMessageChecker, a builtin checker that checks for various
|
|
|
|
// errors of call and objc message expressions.
|
2009-11-03 14:46:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-28 09:28:13 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-28 09:28:13 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2012-07-27 05:39:41 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2011-02-28 09:28:13 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2010-03-28 05:19:47 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2009-11-25 06:48:18 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2009-11-03 14:46:03 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-03 14:46:03 +08:00
|
|
|
|
2009-11-11 13:50:44 +08:00
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class CallAndMessageChecker
|
2012-07-03 03:28:21 +08:00
|
|
|
: public Checker< check::PreStmt<CallExpr>, check::PreObjCMessage,
|
|
|
|
check::PreCall > {
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<BugType> BT_call_null;
|
|
|
|
mutable OwningPtr<BugType> BT_call_undef;
|
2012-07-26 08:22:32 +08:00
|
|
|
mutable OwningPtr<BugType> BT_cxx_call_null;
|
|
|
|
mutable OwningPtr<BugType> BT_cxx_call_undef;
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<BugType> BT_call_arg;
|
|
|
|
mutable OwningPtr<BugType> BT_msg_undef;
|
2012-02-19 04:53:30 +08:00
|
|
|
mutable OwningPtr<BugType> BT_objc_prop_undef;
|
2012-07-19 05:59:51 +08:00
|
|
|
mutable OwningPtr<BugType> BT_objc_subscript_undef;
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<BugType> BT_msg_arg;
|
|
|
|
mutable OwningPtr<BugType> BT_msg_ret;
|
2009-11-11 13:50:44 +08:00
|
|
|
public:
|
2009-11-25 05:41:28 +08:00
|
|
|
|
2011-02-28 09:28:13 +08:00
|
|
|
void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
|
2012-07-03 03:28:04 +08:00
|
|
|
void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
|
2012-07-03 03:28:21 +08:00
|
|
|
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
|
2009-11-25 05:41:28 +08:00
|
|
|
|
2009-11-21 09:25:37 +08:00
|
|
|
private:
|
2012-07-03 03:28:21 +08:00
|
|
|
static bool PreVisitProcessArg(CheckerContext &C, SVal V,
|
|
|
|
SourceRange argRange, const Expr *argEx,
|
2012-07-19 05:59:51 +08:00
|
|
|
bool IsFirstArgument, bool checkUninitFields,
|
|
|
|
const CallEvent &Call, OwningPtr<BugType> &BT);
|
2010-03-18 11:22:29 +08:00
|
|
|
|
2012-08-04 07:08:49 +08:00
|
|
|
static void emitBadCall(BugType *BT, CheckerContext &C, const Expr *BadE);
|
2012-07-03 03:28:04 +08:00
|
|
|
void emitNilReceiverBug(CheckerContext &C, const ObjCMethodCall &msg,
|
2011-02-28 09:28:13 +08:00
|
|
|
ExplodedNode *N) const;
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
void HandleNilReceiver(CheckerContext &C,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state,
|
2012-07-03 03:28:04 +08:00
|
|
|
const ObjCMethodCall &msg) const;
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
static void LazyInit_BT(const char *desc, OwningPtr<BugType> &BT) {
|
2010-03-18 11:22:29 +08:00
|
|
|
if (!BT)
|
2011-02-28 09:28:13 +08:00
|
|
|
BT.reset(new BuiltinBug(desc));
|
2010-03-18 10:17:27 +08:00
|
|
|
}
|
2009-11-11 13:50:44 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2012-08-04 07:08:49 +08:00
|
|
|
void CallAndMessageChecker::emitBadCall(BugType *BT, CheckerContext &C,
|
|
|
|
const Expr *BadE) {
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink();
|
2009-11-21 09:25:37 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *R = new BugReport(*BT, BT->getName(), N);
|
2012-08-04 07:08:49 +08:00
|
|
|
if (BadE) {
|
|
|
|
R->addRange(BadE->getSourceRange());
|
2012-08-04 07:09:01 +08:00
|
|
|
bugreporter::addTrackNullOrUndefValueVisitor(N, BadE, R);
|
2012-08-04 07:08:49 +08:00
|
|
|
}
|
2009-11-21 09:25:37 +08:00
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
|
2012-07-19 05:59:51 +08:00
|
|
|
StringRef describeUninitializedArgumentInCall(const CallEvent &Call,
|
|
|
|
bool IsFirstArgument) {
|
|
|
|
switch (Call.getKind()) {
|
|
|
|
case CE_ObjCMessage: {
|
|
|
|
const ObjCMethodCall &Msg = cast<ObjCMethodCall>(Call);
|
|
|
|
switch (Msg.getMessageKind()) {
|
|
|
|
case OCM_Message:
|
|
|
|
return "Argument in message expression is an uninitialized value";
|
|
|
|
case OCM_PropertyAccess:
|
|
|
|
assert(Msg.isSetter() && "Getters have no args");
|
|
|
|
return "Argument for property setter is an uninitialized value";
|
|
|
|
case OCM_Subscript:
|
|
|
|
if (Msg.isSetter() && IsFirstArgument)
|
|
|
|
return "Argument for subscript setter is an uninitialized value";
|
|
|
|
return "Subscript index is an uninitialized value";
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown message kind.");
|
|
|
|
}
|
|
|
|
case CE_Block:
|
|
|
|
return "Block call argument is an uninitialized value";
|
|
|
|
default:
|
|
|
|
return "Function call argument is an uninitialized value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-18 11:22:29 +08:00
|
|
|
bool CallAndMessageChecker::PreVisitProcessArg(CheckerContext &C,
|
2011-01-25 08:03:53 +08:00
|
|
|
SVal V, SourceRange argRange,
|
|
|
|
const Expr *argEx,
|
2012-07-19 05:59:51 +08:00
|
|
|
bool IsFirstArgument,
|
|
|
|
bool checkUninitFields,
|
|
|
|
const CallEvent &Call,
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<BugType> &BT) {
|
2010-03-18 11:22:29 +08:00
|
|
|
if (V.isUndef()) {
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2012-07-03 03:28:21 +08:00
|
|
|
LazyInit_BT("Uninitialized argument value", BT);
|
2010-03-18 11:22:29 +08:00
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2012-07-19 05:59:51 +08:00
|
|
|
StringRef Desc = describeUninitializedArgumentInCall(Call,
|
|
|
|
IsFirstArgument);
|
|
|
|
BugReport *R = new BugReport(*BT, Desc, N);
|
2011-01-25 08:03:53 +08:00
|
|
|
R->addRange(argRange);
|
|
|
|
if (argEx)
|
2012-08-04 07:09:01 +08:00
|
|
|
bugreporter::addTrackNullOrUndefValueVisitor(N, argEx, R);
|
2010-03-18 11:22:29 +08:00
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-06 07:57:14 +08:00
|
|
|
if (!checkUninitFields)
|
|
|
|
return false;
|
|
|
|
|
2010-03-18 11:22:29 +08:00
|
|
|
if (const nonloc::LazyCompoundVal *LV =
|
|
|
|
dyn_cast<nonloc::LazyCompoundVal>(&V)) {
|
|
|
|
|
|
|
|
class FindUninitializedField {
|
|
|
|
public:
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<const FieldDecl *, 10> FieldChain;
|
2010-03-18 11:22:29 +08:00
|
|
|
private:
|
|
|
|
StoreManager &StoreMgr;
|
|
|
|
MemRegionManager &MrMgr;
|
|
|
|
Store store;
|
|
|
|
public:
|
2012-06-07 01:32:50 +08:00
|
|
|
FindUninitializedField(StoreManager &storeMgr,
|
2010-03-18 11:22:29 +08:00
|
|
|
MemRegionManager &mrMgr, Store s)
|
2012-06-07 01:32:50 +08:00
|
|
|
: StoreMgr(storeMgr), MrMgr(mrMgr), store(s) {}
|
2010-03-18 11:22:29 +08:00
|
|
|
|
2011-08-13 04:02:48 +08:00
|
|
|
bool Find(const TypedValueRegion *R) {
|
2010-08-11 14:10:55 +08:00
|
|
|
QualType T = R->getValueType();
|
2010-03-18 11:22:29 +08:00
|
|
|
if (const RecordType *RT = T->getAsStructureType()) {
|
|
|
|
const RecordDecl *RD = RT->getDecl()->getDefinition();
|
|
|
|
assert(RD && "Referred record has no definition");
|
|
|
|
for (RecordDecl::field_iterator I =
|
|
|
|
RD->field_begin(), E = RD->field_end(); I!=E; ++I) {
|
2012-06-07 04:45:41 +08:00
|
|
|
const FieldRegion *FR = MrMgr.getFieldRegion(*I, R);
|
|
|
|
FieldChain.push_back(*I);
|
2012-04-30 10:36:29 +08:00
|
|
|
T = I->getType();
|
2010-03-18 11:22:29 +08:00
|
|
|
if (T->getAsStructureType()) {
|
|
|
|
if (Find(FR))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
2012-01-12 10:22:40 +08:00
|
|
|
const SVal &V = StoreMgr.getBinding(store, loc::MemRegionVal(FR));
|
2010-03-18 11:22:29 +08:00
|
|
|
if (V.isUndef())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
FieldChain.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const LazyCompoundValData *D = LV->getCVData();
|
2012-06-07 01:32:50 +08:00
|
|
|
FindUninitializedField F(C.getState()->getStateManager().getStoreManager(),
|
2010-12-02 15:49:45 +08:00
|
|
|
C.getSValBuilder().getRegionManager(),
|
2010-03-18 11:22:29 +08:00
|
|
|
D->getStore());
|
|
|
|
|
|
|
|
if (F.Find(D->getRegion())) {
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2012-07-19 05:59:51 +08:00
|
|
|
LazyInit_BT("Uninitialized argument value", BT);
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<512> Str;
|
2010-03-18 11:22:29 +08:00
|
|
|
llvm::raw_svector_ostream os(Str);
|
|
|
|
os << "Passed-by-value struct argument contains uninitialized data";
|
|
|
|
|
|
|
|
if (F.FieldChain.size() == 1)
|
2011-10-15 02:45:37 +08:00
|
|
|
os << " (e.g., field: '" << *F.FieldChain[0] << "')";
|
2010-03-18 11:22:29 +08:00
|
|
|
else {
|
|
|
|
os << " (e.g., via the field chain: '";
|
|
|
|
bool first = true;
|
2011-07-23 18:55:15 +08:00
|
|
|
for (SmallVectorImpl<const FieldDecl *>::iterator
|
2010-03-18 11:22:29 +08:00
|
|
|
DI = F.FieldChain.begin(), DE = F.FieldChain.end(); DI!=DE;++DI){
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
os << '.';
|
2011-10-15 02:45:37 +08:00
|
|
|
os << **DI;
|
2010-03-18 11:22:29 +08:00
|
|
|
}
|
|
|
|
os << "')";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *R = new BugReport(*BT, os.str(), N);
|
2011-01-25 08:03:53 +08:00
|
|
|
R->addRange(argRange);
|
2010-03-18 11:22:29 +08:00
|
|
|
|
|
|
|
// FIXME: enhance track back for uninitialized value for arbitrary
|
|
|
|
// memregions
|
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:28:13 +08:00
|
|
|
void CallAndMessageChecker::checkPreStmt(const CallExpr *CE,
|
|
|
|
CheckerContext &C) const{
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2009-11-21 09:25:37 +08:00
|
|
|
const Expr *Callee = CE->getCallee()->IgnoreParens();
|
2012-07-03 03:27:46 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
2012-07-03 03:27:46 +08:00
|
|
|
SVal L = State->getSVal(Callee, LCtx);
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2009-11-21 09:25:37 +08:00
|
|
|
if (L.isUndef()) {
|
|
|
|
if (!BT_call_undef)
|
2011-02-28 09:28:13 +08:00
|
|
|
BT_call_undef.reset(new BuiltinBug("Called function pointer is an "
|
|
|
|
"uninitalized pointer value"));
|
2012-08-04 07:08:49 +08:00
|
|
|
emitBadCall(BT_call_undef.get(), C, Callee);
|
2009-11-21 09:25:37 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2012-07-03 03:27:46 +08:00
|
|
|
if (L.isZeroConstant()) {
|
2009-11-21 09:25:37 +08:00
|
|
|
if (!BT_call_null)
|
2011-02-28 09:28:13 +08:00
|
|
|
BT_call_null.reset(
|
|
|
|
new BuiltinBug("Called function pointer is null (null dereference)"));
|
2012-08-04 07:08:49 +08:00
|
|
|
emitBadCall(BT_call_null.get(), C, Callee);
|
2010-03-18 10:17:27 +08:00
|
|
|
}
|
2012-07-03 03:28:21 +08:00
|
|
|
}
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2012-07-03 03:28:21 +08:00
|
|
|
void CallAndMessageChecker::checkPreCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C) const {
|
2012-07-26 08:22:32 +08:00
|
|
|
// If this is a call to a C++ method, check if the callee is null or
|
|
|
|
// undefined.
|
2012-08-04 07:08:49 +08:00
|
|
|
if (const CXXInstanceCall *CC = dyn_cast<CXXInstanceCall>(&Call)) {
|
2012-07-26 08:22:32 +08:00
|
|
|
SVal V = CC->getCXXThisVal();
|
|
|
|
if (V.isUndef()) {
|
|
|
|
if (!BT_cxx_call_undef)
|
|
|
|
BT_cxx_call_undef.reset(new BuiltinBug("Called C++ object pointer is "
|
|
|
|
"uninitialized"));
|
2012-08-04 07:08:49 +08:00
|
|
|
emitBadCall(BT_cxx_call_undef.get(), C, CC->getCXXThisExpr());
|
2012-07-26 08:22:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (V.isZeroConstant()) {
|
|
|
|
if (!BT_cxx_call_null)
|
|
|
|
BT_cxx_call_null.reset(new BuiltinBug("Called C++ object pointer "
|
|
|
|
"is null"));
|
2012-08-04 07:08:49 +08:00
|
|
|
emitBadCall(BT_cxx_call_null.get(), C, CC->getCXXThisExpr());
|
2012-07-26 08:22:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:21 +08:00
|
|
|
// Don't check for uninitialized field values in arguments if the
|
|
|
|
// caller has a body that is available and we have the chance to inline it.
|
|
|
|
// This is a hack, but is a reasonable compromise betweens sometimes warning
|
|
|
|
// and sometimes not depending on if we decide to inline a function.
|
|
|
|
const Decl *D = Call.getDecl();
|
|
|
|
const bool checkUninitFields =
|
2012-07-26 08:22:32 +08:00
|
|
|
!(C.getAnalysisManager().shouldInlineCall() && (D && D->getBody()));
|
2012-07-03 03:28:21 +08:00
|
|
|
|
|
|
|
OwningPtr<BugType> *BT;
|
2012-07-19 05:59:51 +08:00
|
|
|
if (isa<ObjCMethodCall>(Call))
|
2012-07-03 03:28:21 +08:00
|
|
|
BT = &BT_msg_arg;
|
2012-07-19 05:59:51 +08:00
|
|
|
else
|
2012-07-03 03:28:21 +08:00
|
|
|
BT = &BT_call_arg;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Call.getNumArgs(); i != e; ++i)
|
2012-07-19 05:59:51 +08:00
|
|
|
if (PreVisitProcessArg(C, Call.getArgSVal(i), Call.getArgSourceRange(i),
|
|
|
|
Call.getArgExpr(i), /*IsFirstArgument=*/i == 0,
|
|
|
|
checkUninitFields, Call, *BT))
|
2012-07-03 03:28:21 +08:00
|
|
|
return;
|
2009-11-21 08:49:41 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
void CallAndMessageChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
|
2011-02-28 09:28:13 +08:00
|
|
|
CheckerContext &C) const {
|
2012-07-03 03:28:04 +08:00
|
|
|
SVal recVal = msg.getReceiverSVal();
|
|
|
|
if (recVal.isUndef()) {
|
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
|
|
|
BugType *BT = 0;
|
2012-07-19 05:59:51 +08:00
|
|
|
switch (msg.getMessageKind()) {
|
|
|
|
case OCM_Message:
|
2012-07-03 03:28:04 +08:00
|
|
|
if (!BT_msg_undef)
|
|
|
|
BT_msg_undef.reset(new BuiltinBug("Receiver in message expression "
|
|
|
|
"is an uninitialized value"));
|
|
|
|
BT = BT_msg_undef.get();
|
2012-07-19 05:59:51 +08:00
|
|
|
break;
|
|
|
|
case OCM_PropertyAccess:
|
|
|
|
if (!BT_objc_prop_undef)
|
|
|
|
BT_objc_prop_undef.reset(new BuiltinBug("Property access on an "
|
|
|
|
"uninitialized object "
|
|
|
|
"pointer"));
|
|
|
|
BT = BT_objc_prop_undef.get();
|
|
|
|
break;
|
|
|
|
case OCM_Subscript:
|
|
|
|
if (!BT_objc_subscript_undef)
|
|
|
|
BT_objc_subscript_undef.reset(new BuiltinBug("Subscript access on an "
|
|
|
|
"uninitialized object "
|
|
|
|
"pointer"));
|
|
|
|
BT = BT_objc_subscript_undef.get();
|
|
|
|
break;
|
2012-07-03 03:28:04 +08:00
|
|
|
}
|
2012-07-19 05:59:51 +08:00
|
|
|
assert(BT && "Unknown message kind.");
|
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
BugReport *R = new BugReport(*BT, BT->getName(), N);
|
2012-07-19 05:59:51 +08:00
|
|
|
const ObjCMessageExpr *ME = msg.getOriginExpr();
|
|
|
|
R->addRange(ME->getReceiverRange());
|
2009-11-21 08:49:41 +08:00
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
// FIXME: getTrackNullOrUndefValueVisitor can't handle "super" yet.
|
2012-07-19 05:59:51 +08:00
|
|
|
if (const Expr *ReceiverE = ME->getInstanceReceiver())
|
2012-08-04 07:09:01 +08:00
|
|
|
bugreporter::addTrackNullOrUndefValueVisitor(N, ReceiverE, R);
|
2012-07-03 03:28:04 +08:00
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Bifurcate the state into nil and non-nil ones.
|
|
|
|
DefinedOrUnknownSVal receiverVal = cast<DefinedOrUnknownSVal>(recVal);
|
|
|
|
|
|
|
|
ProgramStateRef state = C.getState();
|
|
|
|
ProgramStateRef notNilState, nilState;
|
|
|
|
llvm::tie(notNilState, nilState) = state->assume(receiverVal);
|
|
|
|
|
|
|
|
// Handle receiver must be nil.
|
|
|
|
if (nilState && !notNilState) {
|
|
|
|
HandleNilReceiver(C, state, msg);
|
2009-11-21 09:25:37 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-02-28 09:28:13 +08:00
|
|
|
}
|
2009-12-02 13:49:12 +08:00
|
|
|
}
|
2009-11-24 15:06:39 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
void CallAndMessageChecker::emitNilReceiverBug(CheckerContext &C,
|
2012-07-03 03:28:04 +08:00
|
|
|
const ObjCMethodCall &msg,
|
2011-02-28 09:28:13 +08:00
|
|
|
ExplodedNode *N) const {
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2009-11-25 05:41:28 +08:00
|
|
|
if (!BT_msg_ret)
|
2011-02-28 09:28:13 +08:00
|
|
|
BT_msg_ret.reset(
|
2009-11-25 05:41:28 +08:00
|
|
|
new BuiltinBug("Receiver in message expression is "
|
2011-02-28 09:28:13 +08:00
|
|
|
"'nil' and returns a garbage value"));
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2012-07-19 05:59:51 +08:00
|
|
|
const ObjCMessageExpr *ME = msg.getOriginExpr();
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<200> buf;
|
2009-11-25 05:41:28 +08:00
|
|
|
llvm::raw_svector_ostream os(buf);
|
2012-07-19 05:59:51 +08:00
|
|
|
os << "The receiver of message '" << ME->getSelector().getAsString()
|
2012-07-03 03:28:04 +08:00
|
|
|
<< "' is nil and returns a value of type '";
|
|
|
|
msg.getResultType().print(os, C.getLangOpts());
|
|
|
|
os << "' that will be garbage";
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *report = new BugReport(*BT_msg_ret, os.str(), N);
|
2012-07-19 05:59:51 +08:00
|
|
|
report->addRange(ME->getReceiverRange());
|
2012-07-03 03:28:04 +08:00
|
|
|
// FIXME: This won't track "self" in messages to super.
|
2012-07-19 05:59:51 +08:00
|
|
|
if (const Expr *receiver = ME->getInstanceReceiver()) {
|
2012-08-04 07:09:01 +08:00
|
|
|
bugreporter::addTrackNullOrUndefValueVisitor(N, receiver, report);
|
Overhaul the AST representation of Objective-C message send
expressions, to improve source-location information, clarify the
actual receiver of the message, and pave the way for proper C++
support. The ObjCMessageExpr node represents four different kinds of
message sends in a single AST node:
1) Send to a object instance described by an expression (e.g., [x method:5])
2) Send to a class described by the class name (e.g., [NSString method:5])
3) Send to a superclass class (e.g, [super method:5] in class method)
4) Send to a superclass instance (e.g., [super method:5] in instance method)
Previously these four cases where tangled together. Now, they have
more distinct representations. Specific changes:
1) Unchanged; the object instance is represented by an Expr*.
2) Previously stored the ObjCInterfaceDecl* referring to the class
receiving the message. Now stores a TypeSourceInfo* so that we know
how the class was spelled. This both maintains typedef information
and opens the door for more complicated C++ types (e.g., dependent
types). There was an alternative, unused representation of these
sends by naming the class via an IdentifierInfo *. In practice, we
either had an ObjCInterfaceDecl *, from which we would get the
IdentifierInfo *, or we fell into the case below...
3) Previously represented by a class message whose IdentifierInfo *
referred to "super". Sema and CodeGen would use isStr("super") to
determine if they had a send to super. Now represented as a
"class super" send, where we have both the location of the "super"
keyword and the ObjCInterfaceDecl* of the superclass we're
targetting (statically).
4) Previously represented by an instance message whose receiver is a
an ObjCSuperExpr, which Sema and CodeGen would check for via
isa<ObjCSuperExpr>(). Now represented as an "instance super" send,
where we have both the location of the "super" keyword and the
ObjCInterfaceDecl* of the superclass we're targetting
(statically). Note that ObjCSuperExpr only has one remaining use in
the AST, which is for "super.prop" references.
The new representation of ObjCMessageExpr is 2 pointers smaller than
the old one, since it combines more storage. It also eliminates a leak
when we loaded message-send expressions from a precompiled header. The
representation also feels much cleaner to me; comments welcome!
This patch attempts to maintain the same semantics we previously had
with Objective-C message sends. In several places, there are massive
changes that boil down to simply replacing a nested-if structure such
as:
if (message has a receiver expression) {
// instance message
if (isa<ObjCSuperExpr>(...)) {
// send to super
} else {
// send to an object
}
} else {
// class message
if (name->isStr("super")) {
// class send to super
} else {
// send to class
}
}
with a switch
switch (E->getReceiverKind()) {
case ObjCMessageExpr::SuperInstance: ...
case ObjCMessageExpr::Instance: ...
case ObjCMessageExpr::SuperClass: ...
case ObjCMessageExpr::Class:...
}
There are quite a few places (particularly in the checkers) where
send-to-super is effectively ignored. I've placed FIXMEs in most of
them, and attempted to address send-to-super in a reasonable way. This
could use some review.
llvm-svn: 101972
2010-04-21 08:45:42 +08:00
|
|
|
}
|
2010-03-18 10:17:27 +08:00
|
|
|
C.EmitReport(report);
|
2009-11-25 05:41:28 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
static bool supportsNilWithFloatRet(const llvm::Triple &triple) {
|
2012-02-01 07:52:54 +08:00
|
|
|
return (triple.getVendor() == llvm::Triple::Apple &&
|
|
|
|
(triple.getOS() == llvm::Triple::IOS ||
|
|
|
|
!triple.isMacOSXVersionLT(10,5)));
|
2009-11-25 06:48:18 +08:00
|
|
|
}
|
|
|
|
|
2009-11-25 05:41:28 +08:00
|
|
|
void CallAndMessageChecker::HandleNilReceiver(CheckerContext &C,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state,
|
2012-07-03 03:28:04 +08:00
|
|
|
const ObjCMethodCall &Msg) const {
|
2011-01-25 08:03:53 +08:00
|
|
|
ASTContext &Ctx = C.getASTContext();
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2009-11-25 05:41:28 +08:00
|
|
|
// Check the return type of the message expression. A message to nil will
|
|
|
|
// return different values depending on the return type and the architecture.
|
2012-07-03 03:28:04 +08:00
|
|
|
QualType RetTy = Msg.getResultType();
|
2009-11-25 06:48:18 +08:00
|
|
|
CanQualType CanRetTy = Ctx.getCanonicalType(RetTy);
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
2009-11-25 05:41:28 +08:00
|
|
|
|
2010-04-27 05:31:17 +08:00
|
|
|
if (CanRetTy->isStructureOrClassType()) {
|
2011-10-29 03:05:10 +08:00
|
|
|
// Structure returns are safe since the compiler zeroes them out.
|
2012-07-03 03:28:04 +08:00
|
|
|
SVal V = C.getSValBuilder().makeZeroVal(RetTy);
|
|
|
|
C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V));
|
2009-11-25 05:41:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-29 03:05:10 +08:00
|
|
|
// Other cases: check if sizeof(return type) > sizeof(void*)
|
2011-11-02 06:41:01 +08:00
|
|
|
if (CanRetTy != Ctx.VoidTy && C.getLocationContext()->getParentMap()
|
2012-07-03 03:28:04 +08:00
|
|
|
.isConsumedExpr(Msg.getOriginExpr())) {
|
2009-11-25 05:41:28 +08:00
|
|
|
// Compute: sizeof(void *) and sizeof(return type)
|
2010-03-18 10:17:27 +08:00
|
|
|
const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
|
2009-11-25 06:48:18 +08:00
|
|
|
const uint64_t returnTypeSize = Ctx.getTypeSize(CanRetTy);
|
2009-11-25 05:41:28 +08:00
|
|
|
|
2009-11-25 06:48:18 +08:00
|
|
|
if (voidPtrSize < returnTypeSize &&
|
2011-09-02 08:18:52 +08:00
|
|
|
!(supportsNilWithFloatRet(Ctx.getTargetInfo().getTriple()) &&
|
2009-11-25 06:48:18 +08:00
|
|
|
(Ctx.FloatTy == CanRetTy ||
|
|
|
|
Ctx.DoubleTy == CanRetTy ||
|
|
|
|
Ctx.LongDoubleTy == CanRetTy ||
|
2010-09-30 08:37:10 +08:00
|
|
|
Ctx.LongLongTy == CanRetTy ||
|
|
|
|
Ctx.UnsignedLongLongTy == CanRetTy))) {
|
2011-08-13 07:37:29 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink(state))
|
2012-07-03 03:28:04 +08:00
|
|
|
emitNilReceiverBug(C, Msg, N);
|
2009-11-25 05:41:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the safe cases where the return value is 0 if the
|
|
|
|
// receiver is nil.
|
|
|
|
//
|
|
|
|
// FIXME: For now take the conservative approach that we only
|
|
|
|
// return null values if we *know* that the receiver is nil.
|
|
|
|
// This is because we can have surprises like:
|
|
|
|
//
|
|
|
|
// ... = [[NSScreens screens] objectAtIndex:0];
|
|
|
|
//
|
|
|
|
// What can happen is that [... screens] could return nil, but
|
|
|
|
// it most likely isn't nil. We should assume the semantics
|
|
|
|
// of this case unless we have *a lot* more knowledge.
|
|
|
|
//
|
2012-07-03 03:28:04 +08:00
|
|
|
SVal V = C.getSValBuilder().makeZeroVal(RetTy);
|
|
|
|
C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V));
|
2009-11-25 05:41:28 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state);
|
2009-11-03 14:46:03 +08:00
|
|
|
}
|
2011-02-28 09:28:13 +08:00
|
|
|
|
|
|
|
void ento::registerCallAndMessageChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<CallAndMessageChecker>();
|
|
|
|
}
|