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"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-09-02 16:02:59 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h"
|
2011-02-28 09:28:13 +08:00
|
|
|
#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"
|
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
|
2011-03-01 09:16:21 +08:00
|
|
|
: public Checker< check::PreStmt<CallExpr>, check::PreObjCMessage > {
|
2011-02-28 09:28:13 +08:00
|
|
|
mutable llvm::OwningPtr<BugType> BT_call_null;
|
|
|
|
mutable llvm::OwningPtr<BugType> BT_call_undef;
|
|
|
|
mutable llvm::OwningPtr<BugType> BT_call_arg;
|
|
|
|
mutable llvm::OwningPtr<BugType> BT_msg_undef;
|
|
|
|
mutable llvm::OwningPtr<BugType> BT_msg_arg;
|
|
|
|
mutable llvm::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;
|
|
|
|
void checkPreObjCMessage(ObjCMessage msg, CheckerContext &C) const;
|
2009-11-25 05:41:28 +08:00
|
|
|
|
2009-11-21 09:25:37 +08:00
|
|
|
private:
|
2011-02-28 09:28:13 +08:00
|
|
|
static void PreVisitProcessArgs(CheckerContext &C,CallOrObjCMessage callOrMsg,
|
|
|
|
const char *BT_desc, llvm::OwningPtr<BugType> &BT);
|
|
|
|
static bool PreVisitProcessArg(CheckerContext &C, SVal V,SourceRange argRange,
|
|
|
|
const Expr *argEx, const char *BT_desc, llvm::OwningPtr<BugType> &BT);
|
2010-03-18 11:22:29 +08:00
|
|
|
|
2011-02-28 09:28:13 +08:00
|
|
|
static void EmitBadCall(BugType *BT, CheckerContext &C, const CallExpr *CE);
|
2011-01-25 08:03:53 +08:00
|
|
|
void emitNilReceiverBug(CheckerContext &C, const ObjCMessage &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,
|
|
|
|
const ProgramState *state,
|
2011-02-28 09:28:13 +08:00
|
|
|
ObjCMessage msg) const;
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2011-02-28 09:28:13 +08:00
|
|
|
static void LazyInit_BT(const char *desc, llvm::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
|
|
|
|
|
2009-11-24 12:45:44 +08:00
|
|
|
void CallAndMessageChecker::EmitBadCall(BugType *BT, CheckerContext &C,
|
|
|
|
const CallExpr *CE) {
|
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);
|
2011-08-20 06:33:38 +08:00
|
|
|
R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N,
|
|
|
|
bugreporter::GetCalleeExpr(N)));
|
2009-11-21 09:25:37 +08:00
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
|
2011-01-25 08:03:53 +08:00
|
|
|
void CallAndMessageChecker::PreVisitProcessArgs(CheckerContext &C,
|
|
|
|
CallOrObjCMessage callOrMsg,
|
|
|
|
const char *BT_desc,
|
2011-02-28 09:28:13 +08:00
|
|
|
llvm::OwningPtr<BugType> &BT) {
|
2011-01-25 08:03:53 +08:00
|
|
|
for (unsigned i = 0, e = callOrMsg.getNumArgs(); i != e; ++i)
|
|
|
|
if (PreVisitProcessArg(C, callOrMsg.getArgSVal(i),
|
|
|
|
callOrMsg.getArgSourceRange(i), callOrMsg.getArg(i),
|
|
|
|
BT_desc, BT))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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,
|
2010-03-18 11:22:29 +08:00
|
|
|
const char *BT_desc,
|
2011-02-28 09:28:13 +08:00
|
|
|
llvm::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()) {
|
2010-03-18 11:22:29 +08:00
|
|
|
LazyInit_BT(BT_desc, BT);
|
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *R = new BugReport(*BT, BT->getName(), N);
|
2011-01-25 08:03:53 +08:00
|
|
|
R->addRange(argRange);
|
|
|
|
if (argEx)
|
2011-08-20 06:33:38 +08:00
|
|
|
R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, argEx));
|
2010-03-18 11:22:29 +08:00
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
ASTContext &C;
|
|
|
|
StoreManager &StoreMgr;
|
|
|
|
MemRegionManager &MrMgr;
|
|
|
|
Store store;
|
|
|
|
public:
|
|
|
|
FindUninitializedField(ASTContext &c, StoreManager &storeMgr,
|
|
|
|
MemRegionManager &mrMgr, Store s)
|
|
|
|
: C(c), StoreMgr(storeMgr), MrMgr(mrMgr), store(s) {}
|
|
|
|
|
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) {
|
|
|
|
const FieldRegion *FR = MrMgr.getFieldRegion(*I, R);
|
|
|
|
FieldChain.push_back(*I);
|
|
|
|
T = (*I)->getType();
|
|
|
|
if (T->getAsStructureType()) {
|
|
|
|
if (Find(FR))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const SVal &V = StoreMgr.Retrieve(store, loc::MemRegionVal(FR));
|
|
|
|
if (V.isUndef())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
FieldChain.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const LazyCompoundValData *D = LV->getCVData();
|
|
|
|
FindUninitializedField F(C.getASTContext(),
|
|
|
|
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()) {
|
2010-03-18 11:22:29 +08:00
|
|
|
LazyInit_BT(BT_desc, BT);
|
|
|
|
llvm::SmallString<512> Str;
|
|
|
|
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();
|
2010-02-09 00:18:51 +08:00
|
|
|
SVal L = C.getState()->getSVal(Callee);
|
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"));
|
|
|
|
EmitBadCall(BT_call_undef.get(), C, CE);
|
2009-11-21 09:25:37 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-03-18 10:17:27 +08:00
|
|
|
|
2009-11-21 09:25:37 +08:00
|
|
|
if (isa<loc::ConcreteInt>(L)) {
|
|
|
|
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)"));
|
|
|
|
EmitBadCall(BT_call_null.get(), C, CE);
|
2010-03-18 10:17:27 +08:00
|
|
|
}
|
|
|
|
|
2011-01-25 08:03:53 +08:00
|
|
|
PreVisitProcessArgs(C, CallOrObjCMessage(CE, C.getState()),
|
|
|
|
"Function call argument is an uninitialized value",
|
|
|
|
BT_call_arg);
|
2009-11-21 08:49:41 +08:00
|
|
|
}
|
|
|
|
|
2011-02-28 09:28:13 +08:00
|
|
|
void CallAndMessageChecker::checkPreObjCMessage(ObjCMessage msg,
|
|
|
|
CheckerContext &C) const {
|
2009-11-21 08:49:41 +08:00
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = C.getState();
|
2009-11-21 09:25:37 +08:00
|
|
|
|
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
|
|
|
// FIXME: Handle 'super'?
|
2011-02-28 09:28:13 +08:00
|
|
|
if (const Expr *receiver = msg.getInstanceReceiver()) {
|
|
|
|
SVal recVal = state->getSVal(receiver);
|
|
|
|
if (recVal.isUndef()) {
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2009-11-21 09:25:37 +08:00
|
|
|
if (!BT_msg_undef)
|
2011-02-28 09:28:13 +08:00
|
|
|
BT_msg_undef.reset(new BuiltinBug("Receiver in message expression is "
|
|
|
|
"an uninitialized value"));
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *R =
|
|
|
|
new BugReport(*BT_msg_undef, BT_msg_undef->getName(), N);
|
2009-11-21 09:25:37 +08:00
|
|
|
R->addRange(receiver->getSourceRange());
|
2011-08-20 06:33:38 +08:00
|
|
|
R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N,
|
|
|
|
receiver));
|
2009-11-21 09:25:37 +08:00
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
return;
|
2011-02-28 09:28:13 +08:00
|
|
|
} else {
|
|
|
|
// Bifurcate the state into nil and non-nil ones.
|
|
|
|
DefinedOrUnknownSVal receiverVal = cast<DefinedOrUnknownSVal>(recVal);
|
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *notNilState, *nilState;
|
2011-02-28 09:28:13 +08:00
|
|
|
llvm::tie(notNilState, nilState) = state->assume(receiverVal);
|
|
|
|
|
|
|
|
// Handle receiver must be nil.
|
|
|
|
if (nilState && !notNilState) {
|
|
|
|
HandleNilReceiver(C, state, msg);
|
|
|
|
return;
|
|
|
|
}
|
2009-11-21 09:25:37 +08:00
|
|
|
}
|
2011-02-28 09:28:13 +08:00
|
|
|
}
|
2009-11-21 09:25:37 +08:00
|
|
|
|
2011-01-25 08:04:03 +08:00
|
|
|
const char *bugDesc = msg.isPropertySetter() ?
|
|
|
|
"Argument for property setter is an uninitialized value"
|
|
|
|
: "Argument in message expression is an uninitialized value";
|
2009-11-21 09:25:37 +08:00
|
|
|
// Check for any arguments that are uninitialized/undefined.
|
2011-01-25 08:04:03 +08:00
|
|
|
PreVisitProcessArgs(C, CallOrObjCMessage(msg, state), bugDesc, BT_msg_arg);
|
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,
|
2011-01-25 08:03:53 +08:00
|
|
|
const ObjCMessage &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
|
|
|
|
2009-11-25 05:41:28 +08:00
|
|
|
llvm::SmallString<200> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
2011-01-25 08:03:53 +08:00
|
|
|
os << "The receiver of message '" << msg.getSelector().getAsString()
|
2009-11-25 05:41:28 +08:00
|
|
|
<< "' is nil and returns a value of type '"
|
2011-01-25 08:03:53 +08:00
|
|
|
<< msg.getType(C.getASTContext()).getAsString() << "' 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);
|
2011-01-25 08:03:53 +08:00
|
|
|
if (const Expr *receiver = msg.getInstanceReceiver()) {
|
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
|
|
|
report->addRange(receiver->getSourceRange());
|
2011-08-20 06:33:38 +08:00
|
|
|
report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N,
|
|
|
|
receiver));
|
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) {
|
2009-11-25 06:48:18 +08:00
|
|
|
return triple.getVendor() == llvm::Triple::Apple &&
|
2011-07-02 08:20:22 +08:00
|
|
|
(!triple.isMacOSXVersionLT(10,5) ||
|
2010-09-30 08:37:10 +08:00
|
|
|
triple.getArch() == llvm::Triple::arm ||
|
|
|
|
triple.getArch() == llvm::Triple::thumb);
|
2009-11-25 06:48:18 +08:00
|
|
|
}
|
|
|
|
|
2009-11-25 05:41:28 +08:00
|
|
|
void CallAndMessageChecker::HandleNilReceiver(CheckerContext &C,
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state,
|
2011-02-28 09:28:13 +08:00
|
|
|
ObjCMessage 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.
|
2011-01-25 08:03:53 +08:00
|
|
|
QualType RetTy = msg.getType(Ctx);
|
2009-11-25 06:48:18 +08:00
|
|
|
CanQualType CanRetTy = Ctx.getCanonicalType(RetTy);
|
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.
|
|
|
|
SVal V = C.getSValBuilder().makeZeroVal(msg.getType(Ctx));
|
|
|
|
C.addTransition(state->BindExpr(msg.getOriginExpr(), 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()
|
|
|
|
.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))
|
2011-01-25 08:03:53 +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.
|
|
|
|
//
|
2011-01-25 08:03:53 +08:00
|
|
|
SVal V = C.getSValBuilder().makeZeroVal(msg.getType(Ctx));
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state->BindExpr(msg.getOriginExpr(), 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>();
|
|
|
|
}
|