Zhongxing Xu | 259d464 | 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 | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 10 | // This defines VLASizeChecker, a builtin check in GRExprEngine that |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 11 | // performs checks for declaration of VLA of undefined or zero size. |
Jordy Rose | cf781e5 | 2010-07-06 23:33:54 +0000 | [diff] [blame] | 12 | // In addition, VLASizeChecker is responsible for defining the extent |
| 13 | // of the MemRegion that represents a VLA. |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Ted Kremenek | 795c611 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 17 | #include "GRExprEngineInternalChecks.h" |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 18 | #include "clang/AST/CharUnits.h" |
Argyrios Kyrtzidis | 8d602a8 | 2010-12-22 18:51:49 +0000 | [diff] [blame] | 19 | #include "clang/GR/BugReporter/BugType.h" |
| 20 | #include "clang/GR/PathSensitive/CheckerVisitor.h" |
| 21 | #include "clang/GR/PathSensitive/GRExprEngine.h" |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
Argyrios Kyrtzidis | ca08fba | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 24 | using namespace GR; |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | 795c611 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 26 | namespace { |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 27 | class VLASizeChecker : public CheckerVisitor<VLASizeChecker> { |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 28 | BugType *BT_zero; |
| 29 | BugType *BT_undef; |
Ted Kremenek | 795c611 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 30 | |
| 31 | public: |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 32 | VLASizeChecker() : BT_zero(0), BT_undef(0) {} |
| 33 | static void *getTag() { static int tag = 0; return &tag; } |
| 34 | void PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS); |
Ted Kremenek | 795c611 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 35 | }; |
| 36 | } // end anonymous namespace |
| 37 | |
Argyrios Kyrtzidis | ca08fba | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 38 | void GR::RegisterVLASizeChecker(GRExprEngine &Eng) { |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 39 | Eng.registerCheck(new VLASizeChecker()); |
Ted Kremenek | 795c611 | 2009-11-06 21:51:50 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 42 | void VLASizeChecker::PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS) { |
| 43 | if (!DS->isSingleDecl()) |
| 44 | return; |
| 45 | |
| 46 | const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl()); |
| 47 | if (!VD) |
| 48 | return; |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 49 | |
| 50 | ASTContext &Ctx = C.getASTContext(); |
| 51 | const VariableArrayType *VLA = Ctx.getAsVariableArrayType(VD->getType()); |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 52 | if (!VLA) |
| 53 | return; |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 55 | // FIXME: Handle multi-dimensional VLAs. |
| 56 | const Expr* SE = VLA->getSizeExpr(); |
| 57 | const GRState *state = C.getState(); |
Ted Kremenek | 57f0989 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 58 | SVal sizeV = state->getSVal(SE); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 60 | if (sizeV.isUndef()) { |
| 61 | // Generate an error node. |
Ted Kremenek | 750b7ac | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 62 | ExplodedNode *N = C.generateSink(); |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 63 | if (!N) |
| 64 | return; |
| 65 | |
| 66 | if (!BT_undef) |
| 67 | BT_undef = new BuiltinBug("Declared variable-length array (VLA) uses a " |
| 68 | "garbage value as its size"); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 70 | EnhancedBugReport *report = |
Benjamin Kramer | f4c511b | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 71 | new EnhancedBugReport(*BT_undef, BT_undef->getName(), N); |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 72 | report->addRange(SE->getSourceRange()); |
| 73 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE); |
| 74 | C.EmitReport(report); |
| 75 | return; |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 76 | } |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 77 | |
| 78 | // See if the size value is known. It can't be undefined because we would have |
| 79 | // warned about that already. |
| 80 | if (sizeV.isUnknown()) |
| 81 | return; |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 82 | |
| 83 | // Check if the size is zero. |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 84 | DefinedSVal sizeD = cast<DefinedSVal>(sizeV); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 86 | const GRState *stateNotZero, *stateZero; |
Ted Kremenek | c5bea1e | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 87 | llvm::tie(stateNotZero, stateZero) = state->assume(sizeD); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 89 | if (stateZero && !stateNotZero) { |
Ted Kremenek | 750b7ac | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 90 | ExplodedNode* N = C.generateSink(stateZero); |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 91 | if (!BT_zero) |
| 92 | BT_zero = new BuiltinBug("Declared variable-length array (VLA) has zero " |
| 93 | "size"); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 95 | EnhancedBugReport *report = |
Benjamin Kramer | f4c511b | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 96 | new EnhancedBugReport(*BT_zero, BT_zero->getName(), N); |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 97 | report->addRange(SE->getSourceRange()); |
| 98 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE); |
| 99 | C.EmitReport(report); |
| 100 | return; |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 101 | } |
Ted Kremenek | ae3361d | 2009-11-07 03:56:57 +0000 | [diff] [blame] | 102 | |
| 103 | // From this point on, assume that the size is not zero. |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 104 | state = stateNotZero; |
| 105 | |
Jordy Rose | cf781e5 | 2010-07-06 23:33:54 +0000 | [diff] [blame] | 106 | // VLASizeChecker is responsible for defining the extent of the array being |
| 107 | // declared. We do this by multiplying the array length by the element size, |
| 108 | // then matching that with the array region's extent symbol. |
| 109 | |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 110 | // Convert the array length to size_t. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 111 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 112 | QualType SizeTy = Ctx.getSizeType(); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 113 | NonLoc ArrayLength = cast<NonLoc>(svalBuilder.evalCast(sizeD, SizeTy, |
| 114 | SE->getType())); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 115 | |
| 116 | // Get the element size. |
| 117 | CharUnits EleSize = Ctx.getTypeSizeInChars(VLA->getElementType()); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 118 | SVal EleSizeVal = svalBuilder.makeIntVal(EleSize.getQuantity(), SizeTy); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 119 | |
| 120 | // Multiply the array length by the element size. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 121 | SVal ArraySizeVal = svalBuilder.evalBinOpNN(state, BO_Mul, ArrayLength, |
| 122 | cast<NonLoc>(EleSizeVal), SizeTy); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | c5bea1e | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 124 | // Finally, assume that the array's extent matches the given size. |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 125 | const LocationContext *LC = C.getPredecessor()->getLocationContext(); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 126 | DefinedOrUnknownSVal Extent = |
| 127 | state->getRegion(VD, LC)->getExtent(svalBuilder); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 128 | DefinedOrUnknownSVal ArraySize = cast<DefinedOrUnknownSVal>(ArraySizeVal); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 129 | DefinedOrUnknownSVal sizeIsKnown = |
| 130 | svalBuilder.evalEQ(state, Extent, ArraySize); |
| 131 | state = state->assume(sizeIsKnown, true); |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 132 | |
Zhongxing Xu | 5b488b1 | 2010-07-06 07:08:47 +0000 | [diff] [blame] | 133 | // Assume should not fail at this point. |
| 134 | assert(state); |
| 135 | |
Jordy Rose | e6b999b | 2010-07-05 00:50:15 +0000 | [diff] [blame] | 136 | // Remember our assumptions! |
| 137 | C.addTransition(state); |
Zhongxing Xu | 259d464 | 2009-11-04 01:43:07 +0000 | [diff] [blame] | 138 | } |