| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| 14 | #include "clang/AST/CharUnits.h" |
| 15 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 16 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 17 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 18 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | using namespace ento; |
| 22 | |
| 23 | namespace { |
| 24 | class CastSizeChecker : public Checker< check::PreStmt<CastExpr> > { |
| 25 | mutable std::unique_ptr<BuiltinBug> BT; |
| 26 | |
| 27 | public: |
| 28 | void checkPreStmt(const CastExpr *CE, CheckerContext &C) const; |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | static bool evenFlexibleArraySize(ASTContext &Ctx, CharUnits RegionSize, |
| 49 | CharUnits TypeSize, QualType ToPointeeTy) { |
| 50 | const RecordType *RT = ToPointeeTy->getAs<RecordType>(); |
| 51 | if (!RT) |
| 52 | return false; |
| 53 | |
| 54 | const RecordDecl *RD = RT->getDecl(); |
| 55 | RecordDecl::field_iterator Iter(RD->field_begin()); |
| 56 | RecordDecl::field_iterator End(RD->field_end()); |
| 57 | const FieldDecl *Last = nullptr; |
| 58 | for (; Iter != End; ++Iter) |
| 59 | Last = *Iter; |
| 60 | (0) . __assert_fail ("Last && \"empty structs should already be handled\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Checkers/CastSizeChecker.cpp", 60, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(Last && "empty structs should already be handled"); |
| 61 | |
| 62 | const Type *ElemType = Last->getType()->getArrayElementTypeNoTypeQual(); |
| 63 | CharUnits FlexSize; |
| 64 | if (const ConstantArrayType *ArrayTy = |
| 65 | Ctx.getAsConstantArrayType(Last->getType())) { |
| 66 | FlexSize = Ctx.getTypeSizeInChars(ElemType); |
| 67 | if (ArrayTy->getSize() == 1 && TypeSize > FlexSize) |
| 68 | TypeSize -= FlexSize; |
| 69 | else if (ArrayTy->getSize() != 0) |
| 70 | return false; |
| 71 | } else if (RD->hasFlexibleArrayMember()) { |
| 72 | FlexSize = Ctx.getTypeSizeInChars(ElemType); |
| 73 | } else { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | if (FlexSize.isZero()) |
| 78 | return false; |
| 79 | |
| 80 | CharUnits Left = RegionSize - TypeSize; |
| 81 | if (Left.isNegative()) |
| 82 | return false; |
| 83 | |
| 84 | return Left % FlexSize == 0; |
| 85 | } |
| 86 | |
| 87 | void CastSizeChecker::checkPreStmt(const CastExpr *CE,CheckerContext &C) const { |
| 88 | const Expr *E = CE->getSubExpr(); |
| 89 | ASTContext &Ctx = C.getASTContext(); |
| 90 | QualType ToTy = Ctx.getCanonicalType(CE->getType()); |
| 91 | const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr()); |
| 92 | |
| 93 | if (!ToPTy) |
| 94 | return; |
| 95 | |
| 96 | QualType ToPointeeTy = ToPTy->getPointeeType(); |
| 97 | |
| 98 | |
| 99 | if (ToPointeeTy->isIncompleteType()) |
| 100 | return; |
| 101 | |
| 102 | ProgramStateRef state = C.getState(); |
| 103 | const MemRegion *R = C.getSVal(E).getAsRegion(); |
| 104 | if (!R) |
| 105 | return; |
| 106 | |
| 107 | const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R); |
| 108 | if (!SR) |
| 109 | return; |
| 110 | |
| 111 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 112 | SVal extent = SR->getExtent(svalBuilder); |
| 113 | const llvm::APSInt *extentInt = svalBuilder.getKnownValue(state, extent); |
| 114 | if (!extentInt) |
| 115 | return; |
| 116 | |
| 117 | CharUnits regionSize = CharUnits::fromQuantity(extentInt->getSExtValue()); |
| 118 | CharUnits typeSize = C.getASTContext().getTypeSizeInChars(ToPointeeTy); |
| 119 | |
| 120 | |
| 121 | if (typeSize.isZero()) |
| 122 | return; |
| 123 | |
| 124 | if (regionSize % typeSize == 0) |
| 125 | return; |
| 126 | |
| 127 | if (evenFlexibleArraySize(Ctx, regionSize, typeSize, ToPointeeTy)) |
| 128 | return; |
| 129 | |
| 130 | if (ExplodedNode *errorNode = C.generateErrorNode()) { |
| 131 | if (!BT) |
| 132 | BT.reset(new BuiltinBug(this, "Cast region with wrong size.", |
| 133 | "Cast a region whose size is not a multiple" |
| 134 | " of the destination type size.")); |
| 135 | auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), errorNode); |
| 136 | R->addRange(CE->getSourceRange()); |
| 137 | C.emitReport(std::move(R)); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void ento::registerCastSizeChecker(CheckerManager &mgr) { |
| 142 | mgr.registerChecker<CastSizeChecker>(); |
| 143 | } |
| 144 | |
| 145 | bool ento::shouldRegisterCastSizeChecker(const LangOptions &LO) { |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | return !LO.CPlusPlus; |
| 151 | } |
| 152 | |