Zhongxing Xu | 05a2338 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 1 | //=== VLASizeChecker.cpp - Undefined dereference checker --------*- C++ -*-===// |
| 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 | // |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 10 | // This defines VLASizeChecker, a builtin check in GRExprEngine that |
Zhongxing Xu | 05a2338 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 11 | // performs checks for declaration of VLA of undefined or zero size. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | 84b3595 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 15 | #include "GRExprEngineInternalChecks.h" |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
Zhongxing Xu | 05a2338 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
| 18 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
Ted Kremenek | 84b3595 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 22 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 23 | class VLASizeChecker : public CheckerVisitor<VLASizeChecker> { |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 24 | BugType *BT_zero; |
| 25 | BugType *BT_undef; |
Ted Kremenek | 84b3595 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 26 | |
| 27 | public: |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 28 | VLASizeChecker() : BT_zero(0), BT_undef(0) {} |
| 29 | static void *getTag() { static int tag = 0; return &tag; } |
| 30 | void PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS); |
Ted Kremenek | 84b3595 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 31 | }; |
| 32 | } // end anonymous namespace |
| 33 | |
| 34 | void clang::RegisterVLASizeChecker(GRExprEngine &Eng) { |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 35 | Eng.registerCheck(new VLASizeChecker()); |
Ted Kremenek | 84b3595 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 38 | void VLASizeChecker::PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS) { |
| 39 | if (!DS->isSingleDecl()) |
| 40 | return; |
| 41 | |
| 42 | const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl()); |
| 43 | if (!VD) |
| 44 | return; |
| 45 | |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 46 | const VariableArrayType *VLA |
| 47 | = C.getASTContext().getAsVariableArrayType(VD->getType()); |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 48 | if (!VLA) |
| 49 | return; |
Zhongxing Xu | 05a2338 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 51 | // FIXME: Handle multi-dimensional VLAs. |
| 52 | const Expr* SE = VLA->getSizeExpr(); |
| 53 | const GRState *state = C.getState(); |
| 54 | SVal sizeV = state->getSVal(SE); |
Zhongxing Xu | 05a2338 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 56 | if (sizeV.isUndef()) { |
| 57 | // Generate an error node. |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 58 | ExplodedNode *N = C.GenerateSink(); |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 59 | if (!N) |
| 60 | return; |
| 61 | |
| 62 | if (!BT_undef) |
| 63 | BT_undef = new BuiltinBug("Declared variable-length array (VLA) uses a " |
| 64 | "garbage value as its size"); |
Zhongxing Xu | 05a2338 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 66 | EnhancedBugReport *report = |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 67 | new EnhancedBugReport(*BT_undef, BT_undef->getName(), N); |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 68 | report->addRange(SE->getSourceRange()); |
| 69 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE); |
| 70 | C.EmitReport(report); |
| 71 | return; |
Zhongxing Xu | 05a2338 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 72 | } |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 73 | |
| 74 | // Check if the size is zero. |
| 75 | DefinedOrUnknownSVal sizeD = cast<DefinedOrUnknownSVal>(sizeV); |
Zhongxing Xu | 05a2338 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 77 | const GRState *stateNotZero, *stateZero; |
| 78 | llvm::tie(stateNotZero, stateZero) = state->Assume(sizeD); |
Zhongxing Xu | 05a2338 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 80 | if (stateZero && !stateNotZero) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 81 | ExplodedNode* N = C.GenerateSink(stateZero); |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 82 | if (!BT_zero) |
| 83 | BT_zero = new BuiltinBug("Declared variable-length array (VLA) has zero " |
| 84 | "size"); |
Zhongxing Xu | 05a2338 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 86 | EnhancedBugReport *report = |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 87 | new EnhancedBugReport(*BT_zero, BT_zero->getName(), N); |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 88 | report->addRange(SE->getSourceRange()); |
| 89 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE); |
| 90 | C.EmitReport(report); |
| 91 | return; |
Zhongxing Xu | 05a2338 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 92 | } |
Ted Kremenek | ae16233 | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 93 | |
| 94 | // From this point on, assume that the size is not zero. |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 95 | C.addTransition(stateNotZero); |
Zhongxing Xu | 05a2338 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 96 | } |