Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame] | 1 | //===--- CallAndMessageChecker.cpp ------------------------------*- C++ -*--==// |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 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 | // |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame] | 10 | // This defines CallAndMessageChecker, a builtin checker that checks for various |
| 11 | // errors of call and objc message expressions. |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | f81330c | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 15 | #include "clang/Basic/TargetInfo.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 16 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
| 17 | #include "clang/Checker/PathSensitive/BugReporter.h" |
Zhongxing Xu | 2055eff | 2009-11-24 07:06:39 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 19 | #include "GRExprEngineInternalChecks.h" |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 23 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 24 | class CallAndMessageChecker |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame] | 25 | : public CheckerVisitor<CallAndMessageChecker> { |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 26 | BugType *BT_call_null; |
| 27 | BugType *BT_call_undef; |
| 28 | BugType *BT_call_arg; |
| 29 | BugType *BT_msg_undef; |
| 30 | BugType *BT_msg_arg; |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 31 | BugType *BT_msg_ret; |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 32 | public: |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame] | 33 | CallAndMessageChecker() : |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 34 | BT_call_null(0), BT_call_undef(0), BT_call_arg(0), |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 35 | BT_msg_undef(0), BT_msg_arg(0), BT_msg_ret(0) {} |
Zhongxing Xu | 2055eff | 2009-11-24 07:06:39 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 37 | static void *getTag() { |
| 38 | static int x = 0; |
| 39 | return &x; |
| 40 | } |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 42 | void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE); |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 43 | void PreVisitObjCMessageExpr(CheckerContext &C, const ObjCMessageExpr *ME); |
Zhongxing Xu | a46e4d9 | 2009-12-02 05:49:12 +0000 | [diff] [blame] | 44 | bool EvalNilReceiver(CheckerContext &C, const ObjCMessageExpr *ME); |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 46 | private: |
| 47 | void EmitBadCall(BugType *BT, CheckerContext &C, const CallExpr *CE); |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 48 | void EmitNilReceiverBug(CheckerContext &C, const ObjCMessageExpr *ME, |
| 49 | ExplodedNode *N); |
| 50 | |
| 51 | void HandleNilReceiver(CheckerContext &C, const GRState *state, |
| 52 | const ObjCMessageExpr *ME); |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 53 | }; |
| 54 | } // end anonymous namespace |
| 55 | |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame] | 56 | void clang::RegisterCallAndMessageChecker(GRExprEngine &Eng) { |
| 57 | Eng.registerCheck(new CallAndMessageChecker()); |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame] | 60 | void CallAndMessageChecker::EmitBadCall(BugType *BT, CheckerContext &C, |
| 61 | const CallExpr *CE) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 62 | ExplodedNode *N = C.GenerateSink(); |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 63 | if (!N) |
| 64 | return; |
| 65 | |
| 66 | EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N); |
| 67 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 68 | bugreporter::GetCalleeExpr(N)); |
| 69 | C.EmitReport(R); |
| 70 | } |
| 71 | |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame] | 72 | void CallAndMessageChecker::PreVisitCallExpr(CheckerContext &C, |
| 73 | const CallExpr *CE){ |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 74 | |
| 75 | const Expr *Callee = CE->getCallee()->IgnoreParens(); |
| 76 | SVal L = C.getState()->getSVal(Callee); |
| 77 | |
| 78 | if (L.isUndef()) { |
| 79 | if (!BT_call_undef) |
| 80 | BT_call_undef = |
| 81 | new BuiltinBug("Called function pointer is an undefined pointer value"); |
| 82 | EmitBadCall(BT_call_undef, C, CE); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | if (isa<loc::ConcreteInt>(L)) { |
| 87 | if (!BT_call_null) |
| 88 | BT_call_null = |
| 89 | new BuiltinBug("Called function pointer is null (null dereference)"); |
| 90 | EmitBadCall(BT_call_null, C, CE); |
| 91 | } |
| 92 | |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 93 | for (CallExpr::const_arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 94 | I != E; ++I) { |
| 95 | if (C.getState()->getSVal(*I).isUndef()) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 96 | if (ExplodedNode *N = C.GenerateSink()) { |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 97 | if (!BT_call_arg) |
| 98 | BT_call_arg = new BuiltinBug("Pass-by-value argument in function call" |
| 99 | " is undefined"); |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 100 | // Generate a report for this bug. |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 101 | EnhancedBugReport *R = new EnhancedBugReport(*BT_call_arg, |
| 102 | BT_call_arg->getName(), N); |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 103 | R->addRange((*I)->getSourceRange()); |
| 104 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, *I); |
| 105 | C.EmitReport(R); |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame] | 112 | void CallAndMessageChecker::PreVisitObjCMessageExpr(CheckerContext &C, |
| 113 | const ObjCMessageExpr *ME) { |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 115 | const GRState *state = C.getState(); |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 116 | |
| 117 | if (const Expr *receiver = ME->getReceiver()) |
| 118 | if (state->getSVal(receiver).isUndef()) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 119 | if (ExplodedNode *N = C.GenerateSink()) { |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 120 | if (!BT_msg_undef) |
| 121 | BT_msg_undef = |
| 122 | new BuiltinBug("Receiver in message expression is a garbage value"); |
| 123 | EnhancedBugReport *R = |
| 124 | new EnhancedBugReport(*BT_msg_undef, BT_msg_undef->getName(), N); |
| 125 | R->addRange(receiver->getSourceRange()); |
| 126 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 127 | receiver); |
| 128 | C.EmitReport(R); |
| 129 | } |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | // Check for any arguments that are uninitialized/undefined. |
Zhongxing Xu | 2055eff | 2009-11-24 07:06:39 +0000 | [diff] [blame] | 134 | for (ObjCMessageExpr::const_arg_iterator I = ME->arg_begin(), |
| 135 | E = ME->arg_end(); I != E; ++I) { |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 136 | if (state->getSVal(*I).isUndef()) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 137 | if (ExplodedNode *N = C.GenerateSink()) { |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 138 | if (!BT_msg_arg) |
| 139 | BT_msg_arg = |
| 140 | new BuiltinBug("Pass-by-value argument in message expression" |
| 141 | " is undefined"); |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 142 | // Generate a report for this bug. |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 143 | EnhancedBugReport *R = new EnhancedBugReport(*BT_msg_arg, |
| 144 | BT_msg_arg->getName(), N); |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 145 | R->addRange((*I)->getSourceRange()); |
| 146 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, *I); |
| 147 | C.EmitReport(R); |
| 148 | return; |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
Zhongxing Xu | a46e4d9 | 2009-12-02 05:49:12 +0000 | [diff] [blame] | 152 | } |
Zhongxing Xu | 2055eff | 2009-11-24 07:06:39 +0000 | [diff] [blame] | 153 | |
Zhongxing Xu | a46e4d9 | 2009-12-02 05:49:12 +0000 | [diff] [blame] | 154 | bool CallAndMessageChecker::EvalNilReceiver(CheckerContext &C, |
| 155 | const ObjCMessageExpr *ME) { |
| 156 | HandleNilReceiver(C, C.getState(), ME); |
| 157 | return true; // Nil receiver is not handled elsewhere. |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void CallAndMessageChecker::EmitNilReceiverBug(CheckerContext &C, |
| 161 | const ObjCMessageExpr *ME, |
| 162 | ExplodedNode *N) { |
| 163 | |
| 164 | if (!BT_msg_ret) |
| 165 | BT_msg_ret = |
| 166 | new BuiltinBug("Receiver in message expression is " |
| 167 | "'nil' and returns a garbage value"); |
| 168 | |
| 169 | llvm::SmallString<200> buf; |
| 170 | llvm::raw_svector_ostream os(buf); |
| 171 | os << "The receiver of message '" << ME->getSelector().getAsString() |
| 172 | << "' is nil and returns a value of type '" |
| 173 | << ME->getType().getAsString() << "' that will be garbage"; |
| 174 | |
| 175 | EnhancedBugReport *report = new EnhancedBugReport(*BT_msg_ret, os.str(), N); |
| 176 | const Expr *receiver = ME->getReceiver(); |
| 177 | report->addRange(receiver->getSourceRange()); |
| 178 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 179 | receiver); |
| 180 | C.EmitReport(report); |
| 181 | } |
| 182 | |
Ted Kremenek | f81330c | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 183 | static bool SupportsNilWithFloatRet(const llvm::Triple &triple) { |
| 184 | return triple.getVendor() == llvm::Triple::Apple && |
| 185 | triple.getDarwinMajorNumber() >= 9; |
| 186 | } |
| 187 | |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 188 | void CallAndMessageChecker::HandleNilReceiver(CheckerContext &C, |
| 189 | const GRState *state, |
| 190 | const ObjCMessageExpr *ME) { |
| 191 | |
| 192 | // Check the return type of the message expression. A message to nil will |
| 193 | // return different values depending on the return type and the architecture. |
| 194 | QualType RetTy = ME->getType(); |
Ted Kremenek | f81330c | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 195 | |
| 196 | ASTContext &Ctx = C.getASTContext(); |
| 197 | CanQualType CanRetTy = Ctx.getCanonicalType(RetTy); |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | f81330c | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 199 | if (CanRetTy->isStructureType()) { |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 200 | // FIXME: At some point we shouldn't rely on isConsumedExpr(), but instead |
| 201 | // have the "use of undefined value" be smarter about where the |
| 202 | // undefined value came from. |
| 203 | if (C.getPredecessor()->getParentMap().isConsumedExpr(ME)) { |
| 204 | if (ExplodedNode* N = C.GenerateSink(state)) |
| 205 | EmitNilReceiverBug(C, ME, N); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // The result is not consumed by a surrounding expression. Just propagate |
| 210 | // the current state. |
| 211 | C.addTransition(state); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | // Other cases: check if the return type is smaller than void*. |
Ted Kremenek | f81330c | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 216 | if (CanRetTy != Ctx.VoidTy && |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 217 | C.getPredecessor()->getParentMap().isConsumedExpr(ME)) { |
| 218 | // Compute: sizeof(void *) and sizeof(return type) |
Ted Kremenek | f81330c | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 219 | const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy); |
| 220 | const uint64_t returnTypeSize = Ctx.getTypeSize(CanRetTy); |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 221 | |
Ted Kremenek | f81330c | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 222 | if (voidPtrSize < returnTypeSize && |
| 223 | !(SupportsNilWithFloatRet(Ctx.Target.getTriple()) && |
| 224 | (Ctx.FloatTy == CanRetTy || |
| 225 | Ctx.DoubleTy == CanRetTy || |
| 226 | Ctx.LongDoubleTy == CanRetTy || |
| 227 | Ctx.LongLongTy == CanRetTy))) { |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 228 | if (ExplodedNode* N = C.GenerateSink(state)) |
| 229 | EmitNilReceiverBug(C, ME, N); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | // Handle the safe cases where the return value is 0 if the |
| 234 | // receiver is nil. |
| 235 | // |
| 236 | // FIXME: For now take the conservative approach that we only |
| 237 | // return null values if we *know* that the receiver is nil. |
| 238 | // This is because we can have surprises like: |
| 239 | // |
| 240 | // ... = [[NSScreens screens] objectAtIndex:0]; |
| 241 | // |
| 242 | // What can happen is that [... screens] could return nil, but |
| 243 | // it most likely isn't nil. We should assume the semantics |
| 244 | // of this case unless we have *a lot* more knowledge. |
| 245 | // |
| 246 | SVal V = C.getValueManager().makeZeroVal(ME->getType()); |
| 247 | C.GenerateNode(state->BindExpr(ME, V)); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | C.addTransition(state); |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 252 | } |