Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 1 | //=== CastSizeChecker.cpp ---------------------------------------*- 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 | // |
| 10 | // CastSizeChecker checks when casting a malloc'ed symbolic region to type T, |
| 11 | // whether the size of the symbolic region is a multiple of the size of T. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "clang/AST/CharUnits.h" |
Argyrios Kyrtzidis | 8d602a8 | 2010-12-22 18:51:49 +0000 | [diff] [blame] | 15 | #include "clang/GR/BugReporter/BugType.h" |
| 16 | #include "clang/GR/PathSensitive/CheckerVisitor.h" |
Argyrios Kyrtzidis | 1696f50 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 17 | #include "ExprEngineInternalChecks.h" |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame^] | 20 | using namespace ento; |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | class CastSizeChecker : public CheckerVisitor<CastSizeChecker> { |
| 24 | BuiltinBug *BT; |
| 25 | public: |
| 26 | CastSizeChecker() : BT(0) {} |
| 27 | static void *getTag(); |
| 28 | void PreVisitCastExpr(CheckerContext &C, const CastExpr *B); |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | void *CastSizeChecker::getTag() { |
| 33 | static int x; |
| 34 | return &x; |
| 35 | } |
| 36 | |
| 37 | void CastSizeChecker::PreVisitCastExpr(CheckerContext &C, const CastExpr *CE) { |
| 38 | const Expr *E = CE->getSubExpr(); |
| 39 | ASTContext &Ctx = C.getASTContext(); |
| 40 | QualType ToTy = Ctx.getCanonicalType(CE->getType()); |
| 41 | PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr()); |
| 42 | |
| 43 | if (!ToPTy) |
| 44 | return; |
| 45 | |
| 46 | QualType ToPointeeTy = ToPTy->getPointeeType(); |
| 47 | |
Ted Kremenek | 026cb58 | 2010-09-01 20:35:38 +0000 | [diff] [blame] | 48 | // Only perform the check if 'ToPointeeTy' is a complete type. |
| 49 | if (ToPointeeTy->isIncompleteType()) |
| 50 | return; |
| 51 | |
Jordy Rose | 674bd55 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 52 | const GRState *state = C.getState(); |
| 53 | const MemRegion *R = state->getSVal(E).getAsRegion(); |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 54 | if (R == 0) |
| 55 | return; |
| 56 | |
| 57 | const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R); |
| 58 | if (SR == 0) |
| 59 | return; |
| 60 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 61 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 62 | SVal extent = SR->getExtent(svalBuilder); |
| 63 | const llvm::APSInt *extentInt = svalBuilder.getKnownValue(state, extent); |
| 64 | if (!extentInt) |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 65 | return; |
| 66 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 67 | CharUnits regionSize = CharUnits::fromQuantity(extentInt->getSExtValue()); |
| 68 | CharUnits typeSize = C.getASTContext().getTypeSizeInChars(ToPointeeTy); |
| 69 | |
Jordy Rose | 674bd55 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 70 | // Ignore void, and a few other un-sizeable types. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 71 | if (typeSize.isZero()) |
Jordy Rose | 2dd9b02 | 2010-06-20 04:30:57 +0000 | [diff] [blame] | 72 | return; |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 73 | |
| 74 | if (regionSize % typeSize != 0) { |
Ted Kremenek | 750b7ac | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 75 | if (ExplodedNode *errorNode = C.generateSink()) { |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 76 | if (!BT) |
| 77 | BT = new BuiltinBug("Cast region with wrong size.", |
| 78 | "Cast a region whose size is not a multiple of the" |
| 79 | " destination type size."); |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 80 | RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), |
| 81 | errorNode); |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 82 | R->addRange(CE->getSourceRange()); |
| 83 | C.EmitReport(R); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame^] | 89 | void ento::RegisterCastSizeChecker(ExprEngine &Eng) { |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 90 | Eng.registerCheck(new CastSizeChecker()); |
| 91 | } |