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 | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 17 | #include "GRExprEngineInternalChecks.h" |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 21 | namespace { |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame^] | 22 | class VISIBILITY_HIDDEN CallAndMessageChecker |
| 23 | : public CheckerVisitor<CallAndMessageChecker> { |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 24 | BugType *BT_call_null; |
| 25 | BugType *BT_call_undef; |
| 26 | BugType *BT_call_arg; |
| 27 | BugType *BT_msg_undef; |
| 28 | BugType *BT_msg_arg; |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 29 | public: |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame^] | 30 | CallAndMessageChecker() : |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 31 | BT_call_null(0), BT_call_undef(0), BT_call_arg(0), |
| 32 | BT_msg_undef(0), BT_msg_arg(0) {} |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 33 | static void *getTag() { |
| 34 | static int x = 0; |
| 35 | return &x; |
| 36 | } |
| 37 | void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE); |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 38 | void PreVisitObjCMessageExpr(CheckerContext &C, const ObjCMessageExpr *ME); |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 39 | private: |
| 40 | void EmitBadCall(BugType *BT, CheckerContext &C, const CallExpr *CE); |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 41 | }; |
| 42 | } // end anonymous namespace |
| 43 | |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame^] | 44 | void clang::RegisterCallAndMessageChecker(GRExprEngine &Eng) { |
| 45 | Eng.registerCheck(new CallAndMessageChecker()); |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame^] | 48 | void CallAndMessageChecker::EmitBadCall(BugType *BT, CheckerContext &C, |
| 49 | const CallExpr *CE) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 50 | ExplodedNode *N = C.GenerateSink(); |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 51 | if (!N) |
| 52 | return; |
| 53 | |
| 54 | EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N); |
| 55 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 56 | bugreporter::GetCalleeExpr(N)); |
| 57 | C.EmitReport(R); |
| 58 | } |
| 59 | |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame^] | 60 | void CallAndMessageChecker::PreVisitCallExpr(CheckerContext &C, |
| 61 | const CallExpr *CE){ |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 62 | |
| 63 | const Expr *Callee = CE->getCallee()->IgnoreParens(); |
| 64 | SVal L = C.getState()->getSVal(Callee); |
| 65 | |
| 66 | if (L.isUndef()) { |
| 67 | if (!BT_call_undef) |
| 68 | BT_call_undef = |
| 69 | new BuiltinBug("Called function pointer is an undefined pointer value"); |
| 70 | EmitBadCall(BT_call_undef, C, CE); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | if (isa<loc::ConcreteInt>(L)) { |
| 75 | if (!BT_call_null) |
| 76 | BT_call_null = |
| 77 | new BuiltinBug("Called function pointer is null (null dereference)"); |
| 78 | EmitBadCall(BT_call_null, C, CE); |
| 79 | } |
| 80 | |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 81 | for (CallExpr::const_arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 82 | I != E; ++I) { |
| 83 | if (C.getState()->getSVal(*I).isUndef()) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 84 | if (ExplodedNode *N = C.GenerateSink()) { |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 85 | if (!BT_call_arg) |
| 86 | BT_call_arg = new BuiltinBug("Pass-by-value argument in function call" |
| 87 | " is undefined"); |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 88 | // Generate a report for this bug. |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 89 | EnhancedBugReport *R = new EnhancedBugReport(*BT_call_arg, |
| 90 | BT_call_arg->getName(), N); |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 91 | R->addRange((*I)->getSourceRange()); |
| 92 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, *I); |
| 93 | C.EmitReport(R); |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame^] | 100 | void CallAndMessageChecker::PreVisitObjCMessageExpr(CheckerContext &C, |
| 101 | const ObjCMessageExpr *ME) { |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 103 | const GRState *state = C.getState(); |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 104 | |
| 105 | if (const Expr *receiver = ME->getReceiver()) |
| 106 | if (state->getSVal(receiver).isUndef()) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 107 | if (ExplodedNode *N = C.GenerateSink()) { |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 108 | if (!BT_msg_undef) |
| 109 | BT_msg_undef = |
| 110 | new BuiltinBug("Receiver in message expression is a garbage value"); |
| 111 | EnhancedBugReport *R = |
| 112 | new EnhancedBugReport(*BT_msg_undef, BT_msg_undef->getName(), N); |
| 113 | R->addRange(receiver->getSourceRange()); |
| 114 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 115 | receiver); |
| 116 | C.EmitReport(R); |
| 117 | } |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | // Check for any arguments that are uninitialized/undefined. |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 122 | for (ObjCMessageExpr::const_arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 123 | I != E; ++I) { |
| 124 | if (state->getSVal(*I).isUndef()) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 125 | if (ExplodedNode *N = C.GenerateSink()) { |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 126 | if (!BT_msg_arg) |
| 127 | BT_msg_arg = |
| 128 | new BuiltinBug("Pass-by-value argument in message expression" |
| 129 | " is undefined"); |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 130 | // Generate a report for this bug. |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 131 | EnhancedBugReport *R = new EnhancedBugReport(*BT_msg_arg, |
| 132 | BT_msg_arg->getName(), N); |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 133 | R->addRange((*I)->getSourceRange()); |
| 134 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, *I); |
| 135 | C.EmitReport(R); |
| 136 | return; |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |