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 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 8b08906 | 2011-02-24 21:42:49 +0000 | [diff] [blame] | 14 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 8b08906 | 2011-02-24 21:42:49 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 17 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | f8cbac4 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 8b08906 | 2011-02-24 21:42:49 +0000 | [diff] [blame] | 19 | #include "clang/AST/CharUnits.h" |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 22 | using namespace ento; |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 25 | class CastSizeChecker : public Checker< check::PreStmt<CastExpr> > { |
Argyrios Kyrtzidis | 8b08906 | 2011-02-24 21:42:49 +0000 | [diff] [blame] | 26 | mutable llvm::OwningPtr<BuiltinBug> BT; |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 27 | public: |
Argyrios Kyrtzidis | 8b08906 | 2011-02-24 21:42:49 +0000 | [diff] [blame] | 28 | void checkPreStmt(const CastExpr *CE, CheckerContext &C) const; |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 29 | }; |
| 30 | } |
| 31 | |
Argyrios Kyrtzidis | 8b08906 | 2011-02-24 21:42:49 +0000 | [diff] [blame] | 32 | void CastSizeChecker::checkPreStmt(const CastExpr *CE,CheckerContext &C) const { |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 33 | const Expr *E = CE->getSubExpr(); |
| 34 | ASTContext &Ctx = C.getASTContext(); |
| 35 | QualType ToTy = Ctx.getCanonicalType(CE->getType()); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 36 | const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr()); |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 37 | |
| 38 | if (!ToPTy) |
| 39 | return; |
| 40 | |
| 41 | QualType ToPointeeTy = ToPTy->getPointeeType(); |
| 42 | |
Ted Kremenek | 026cb58 | 2010-09-01 20:35:38 +0000 | [diff] [blame] | 43 | // Only perform the check if 'ToPointeeTy' is a complete type. |
| 44 | if (ToPointeeTy->isIncompleteType()) |
| 45 | return; |
| 46 | |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 47 | const ProgramState *state = C.getState(); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 48 | const MemRegion *R = state->getSVal(E, C.getLocationContext()).getAsRegion(); |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 49 | if (R == 0) |
| 50 | return; |
| 51 | |
| 52 | const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R); |
| 53 | if (SR == 0) |
| 54 | return; |
| 55 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 56 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 57 | SVal extent = SR->getExtent(svalBuilder); |
| 58 | const llvm::APSInt *extentInt = svalBuilder.getKnownValue(state, extent); |
| 59 | if (!extentInt) |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 60 | return; |
| 61 | |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 62 | CharUnits regionSize = CharUnits::fromQuantity(extentInt->getSExtValue()); |
| 63 | CharUnits typeSize = C.getASTContext().getTypeSizeInChars(ToPointeeTy); |
| 64 | |
Jordy Rose | 674bd55 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 65 | // Ignore void, and a few other un-sizeable types. |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 66 | if (typeSize.isZero()) |
Jordy Rose | 2dd9b02 | 2010-06-20 04:30:57 +0000 | [diff] [blame] | 67 | return; |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 68 | |
| 69 | if (regionSize % typeSize != 0) { |
Ted Kremenek | 750b7ac | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 70 | if (ExplodedNode *errorNode = C.generateSink()) { |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 71 | if (!BT) |
Argyrios Kyrtzidis | 8b08906 | 2011-02-24 21:42:49 +0000 | [diff] [blame] | 72 | BT.reset(new BuiltinBug("Cast region with wrong size.", |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 73 | "Cast a region whose size is not a multiple of the" |
Argyrios Kyrtzidis | 8b08906 | 2011-02-24 21:42:49 +0000 | [diff] [blame] | 74 | " destination type size.")); |
Anna Zaks | 3a6bdf8 | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 75 | BugReport *R = new BugReport(*BT, BT->getDescription(), |
Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 76 | errorNode); |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 77 | R->addRange(CE->getSourceRange()); |
| 78 | C.EmitReport(R); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
Argyrios Kyrtzidis | 8b08906 | 2011-02-24 21:42:49 +0000 | [diff] [blame] | 84 | void ento::registerCastSizeChecker(CheckerManager &mgr) { |
| 85 | mgr.registerChecker<CastSizeChecker>(); |
Zhongxing Xu | 658dd8b | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 86 | } |