1 //===--- CallAndMessageChecker.cpp ------------------------------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines CallAndMessageChecker, a builtin checker that checks for various
11 // errors of call and objc message expressions.
13 //===----------------------------------------------------------------------===//
15 #include "GRExprEngineInternalChecks.h"
16 #include "clang/AST/ParentMap.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/GR/BugReporter/BugType.h"
19 #include "clang/GR/PathSensitive/CheckerVisitor.h"
21 using namespace clang
;
25 class CallAndMessageChecker
26 : public CheckerVisitor
<CallAndMessageChecker
> {
27 BugType
*BT_call_null
;
28 BugType
*BT_call_undef
;
30 BugType
*BT_msg_undef
;
34 CallAndMessageChecker() :
35 BT_call_null(0), BT_call_undef(0), BT_call_arg(0),
36 BT_msg_undef(0), BT_msg_arg(0), BT_msg_ret(0) {}
38 static void *getTag() {
43 void PreVisitCallExpr(CheckerContext
&C
, const CallExpr
*CE
);
44 void PreVisitObjCMessageExpr(CheckerContext
&C
, const ObjCMessageExpr
*ME
);
45 bool evalNilReceiver(CheckerContext
&C
, const ObjCMessageExpr
*ME
);
48 bool PreVisitProcessArg(CheckerContext
&C
, const Expr
*Ex
,
49 const char *BT_desc
, BugType
*&BT
);
51 void EmitBadCall(BugType
*BT
, CheckerContext
&C
, const CallExpr
*CE
);
52 void emitNilReceiverBug(CheckerContext
&C
, const ObjCMessageExpr
*ME
,
55 void HandleNilReceiver(CheckerContext
&C
, const GRState
*state
,
56 const ObjCMessageExpr
*ME
);
58 void LazyInit_BT(const char *desc
, BugType
*&BT
) {
60 BT
= new BuiltinBug(desc
);
63 } // end anonymous namespace
65 void GR::RegisterCallAndMessageChecker(GRExprEngine
&Eng
) {
66 Eng
.registerCheck(new CallAndMessageChecker());
69 void CallAndMessageChecker::EmitBadCall(BugType
*BT
, CheckerContext
&C
,
71 ExplodedNode
*N
= C
.generateSink();
75 EnhancedBugReport
*R
= new EnhancedBugReport(*BT
, BT
->getName(), N
);
76 R
->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue
,
77 bugreporter::GetCalleeExpr(N
));
81 bool CallAndMessageChecker::PreVisitProcessArg(CheckerContext
&C
,
86 const SVal
&V
= C
.getState()->getSVal(Ex
);
89 if (ExplodedNode
*N
= C
.generateSink()) {
90 LazyInit_BT(BT_desc
, BT
);
92 // Generate a report for this bug.
93 EnhancedBugReport
*R
= new EnhancedBugReport(*BT
, BT
->getName(), N
);
94 R
->addRange(Ex
->getSourceRange());
95 R
->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue
, Ex
);
101 if (const nonloc::LazyCompoundVal
*LV
=
102 dyn_cast
<nonloc::LazyCompoundVal
>(&V
)) {
104 class FindUninitializedField
{
106 llvm::SmallVector
<const FieldDecl
*, 10> FieldChain
;
109 StoreManager
&StoreMgr
;
110 MemRegionManager
&MrMgr
;
113 FindUninitializedField(ASTContext
&c
, StoreManager
&storeMgr
,
114 MemRegionManager
&mrMgr
, Store s
)
115 : C(c
), StoreMgr(storeMgr
), MrMgr(mrMgr
), store(s
) {}
117 bool Find(const TypedRegion
*R
) {
118 QualType T
= R
->getValueType();
119 if (const RecordType
*RT
= T
->getAsStructureType()) {
120 const RecordDecl
*RD
= RT
->getDecl()->getDefinition();
121 assert(RD
&& "Referred record has no definition");
122 for (RecordDecl::field_iterator I
=
123 RD
->field_begin(), E
= RD
->field_end(); I
!=E
; ++I
) {
124 const FieldRegion
*FR
= MrMgr
.getFieldRegion(*I
, R
);
125 FieldChain
.push_back(*I
);
127 if (T
->getAsStructureType()) {
132 const SVal
&V
= StoreMgr
.Retrieve(store
, loc::MemRegionVal(FR
));
136 FieldChain
.pop_back();
144 const LazyCompoundValData
*D
= LV
->getCVData();
145 FindUninitializedField
F(C
.getASTContext(),
146 C
.getState()->getStateManager().getStoreManager(),
147 C
.getSValBuilder().getRegionManager(),
150 if (F
.Find(D
->getRegion())) {
151 if (ExplodedNode
*N
= C
.generateSink()) {
152 LazyInit_BT(BT_desc
, BT
);
153 llvm::SmallString
<512> Str
;
154 llvm::raw_svector_ostream
os(Str
);
155 os
<< "Passed-by-value struct argument contains uninitialized data";
157 if (F
.FieldChain
.size() == 1)
158 os
<< " (e.g., field: '" << F
.FieldChain
[0] << "')";
160 os
<< " (e.g., via the field chain: '";
162 for (llvm::SmallVectorImpl
<const FieldDecl
*>::iterator
163 DI
= F
.FieldChain
.begin(), DE
= F
.FieldChain
.end(); DI
!=DE
;++DI
){
173 // Generate a report for this bug.
174 EnhancedBugReport
*R
= new EnhancedBugReport(*BT
, os
.str(), N
);
175 R
->addRange(Ex
->getSourceRange());
177 // FIXME: enhance track back for uninitialized value for arbitrary
188 void CallAndMessageChecker::PreVisitCallExpr(CheckerContext
&C
,
191 const Expr
*Callee
= CE
->getCallee()->IgnoreParens();
192 SVal L
= C
.getState()->getSVal(Callee
);
197 new BuiltinBug("Called function pointer is an uninitalized pointer value");
198 EmitBadCall(BT_call_undef
, C
, CE
);
202 if (isa
<loc::ConcreteInt
>(L
)) {
205 new BuiltinBug("Called function pointer is null (null dereference)");
206 EmitBadCall(BT_call_null
, C
, CE
);
209 for (CallExpr::const_arg_iterator I
= CE
->arg_begin(), E
= CE
->arg_end();
211 if (PreVisitProcessArg(C
, *I
,
212 "Function call argument is an uninitialized value",
217 void CallAndMessageChecker::PreVisitObjCMessageExpr(CheckerContext
&C
,
218 const ObjCMessageExpr
*ME
) {
220 const GRState
*state
= C
.getState();
222 // FIXME: Handle 'super'?
223 if (const Expr
*receiver
= ME
->getInstanceReceiver())
224 if (state
->getSVal(receiver
).isUndef()) {
225 if (ExplodedNode
*N
= C
.generateSink()) {
228 new BuiltinBug("Receiver in message expression is an uninitialized value");
229 EnhancedBugReport
*R
=
230 new EnhancedBugReport(*BT_msg_undef
, BT_msg_undef
->getName(), N
);
231 R
->addRange(receiver
->getSourceRange());
232 R
->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue
,
239 // Check for any arguments that are uninitialized/undefined.
240 for (ObjCMessageExpr::const_arg_iterator I
= ME
->arg_begin(),
241 E
= ME
->arg_end(); I
!= E
; ++I
)
242 if (PreVisitProcessArg(C
, *I
,
243 "Argument in message expression "
244 "is an uninitialized value", BT_msg_arg
))
248 bool CallAndMessageChecker::evalNilReceiver(CheckerContext
&C
,
249 const ObjCMessageExpr
*ME
) {
250 HandleNilReceiver(C
, C
.getState(), ME
);
251 return true; // Nil receiver is not handled elsewhere.
254 void CallAndMessageChecker::emitNilReceiverBug(CheckerContext
&C
,
255 const ObjCMessageExpr
*ME
,
260 new BuiltinBug("Receiver in message expression is "
261 "'nil' and returns a garbage value");
263 llvm::SmallString
<200> buf
;
264 llvm::raw_svector_ostream
os(buf
);
265 os
<< "The receiver of message '" << ME
->getSelector().getAsString()
266 << "' is nil and returns a value of type '"
267 << ME
->getType().getAsString() << "' that will be garbage";
269 EnhancedBugReport
*report
= new EnhancedBugReport(*BT_msg_ret
, os
.str(), N
);
270 if (const Expr
*receiver
= ME
->getInstanceReceiver()) {
271 report
->addRange(receiver
->getSourceRange());
272 report
->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue
,
275 C
.EmitReport(report
);
278 static bool supportsNilWithFloatRet(const llvm::Triple
&triple
) {
279 return triple
.getVendor() == llvm::Triple::Apple
&&
280 (triple
.getDarwinMajorNumber() >= 9 ||
281 triple
.getArch() == llvm::Triple::arm
||
282 triple
.getArch() == llvm::Triple::thumb
);
285 void CallAndMessageChecker::HandleNilReceiver(CheckerContext
&C
,
286 const GRState
*state
,
287 const ObjCMessageExpr
*ME
) {
289 // Check the return type of the message expression. A message to nil will
290 // return different values depending on the return type and the architecture.
291 QualType RetTy
= ME
->getType();
293 ASTContext
&Ctx
= C
.getASTContext();
294 CanQualType CanRetTy
= Ctx
.getCanonicalType(RetTy
);
296 if (CanRetTy
->isStructureOrClassType()) {
297 // FIXME: At some point we shouldn't rely on isConsumedExpr(), but instead
298 // have the "use of undefined value" be smarter about where the
299 // undefined value came from.
300 if (C
.getPredecessor()->getParentMap().isConsumedExpr(ME
)) {
301 if (ExplodedNode
* N
= C
.generateSink(state
))
302 emitNilReceiverBug(C
, ME
, N
);
306 // The result is not consumed by a surrounding expression. Just propagate
307 // the current state.
308 C
.addTransition(state
);
312 // Other cases: check if the return type is smaller than void*.
313 if (CanRetTy
!= Ctx
.VoidTy
&&
314 C
.getPredecessor()->getParentMap().isConsumedExpr(ME
)) {
315 // Compute: sizeof(void *) and sizeof(return type)
316 const uint64_t voidPtrSize
= Ctx
.getTypeSize(Ctx
.VoidPtrTy
);
317 const uint64_t returnTypeSize
= Ctx
.getTypeSize(CanRetTy
);
319 if (voidPtrSize
< returnTypeSize
&&
320 !(supportsNilWithFloatRet(Ctx
.Target
.getTriple()) &&
321 (Ctx
.FloatTy
== CanRetTy
||
322 Ctx
.DoubleTy
== CanRetTy
||
323 Ctx
.LongDoubleTy
== CanRetTy
||
324 Ctx
.LongLongTy
== CanRetTy
||
325 Ctx
.UnsignedLongLongTy
== CanRetTy
))) {
326 if (ExplodedNode
* N
= C
.generateSink(state
))
327 emitNilReceiverBug(C
, ME
, N
);
331 // Handle the safe cases where the return value is 0 if the
334 // FIXME: For now take the conservative approach that we only
335 // return null values if we *know* that the receiver is nil.
336 // This is because we can have surprises like:
338 // ... = [[NSScreens screens] objectAtIndex:0];
340 // What can happen is that [... screens] could return nil, but
341 // it most likely isn't nil. We should assume the semantics
342 // of this case unless we have *a lot* more knowledge.
344 SVal V
= C
.getSValBuilder().makeZeroVal(ME
->getType());
345 C
.generateNode(state
->BindExpr(ME
, V
));
349 C
.addTransition(state
);