Zhongxing Xu | 4f3dc69 | 2009-11-09 08:07:38 +0000 | [diff] [blame] | 1 | //=== CastToStructChecker.cpp - Fixed address usage 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 | // |
| 10 | // This files defines CastToStructChecker, a builtin checker that checks for |
| 11 | // assignment of a fixed address to a pointer. |
| 12 | // This check corresponds to CWE-588. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
| 17 | #include "GRExprEngineInternalChecks.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | namespace { |
| 22 | class VISIBILITY_HIDDEN CastToStructChecker |
| 23 | : public CheckerVisitor<CastToStructChecker> { |
| 24 | BuiltinBug *BT; |
| 25 | public: |
| 26 | CastToStructChecker() : BT(0) {} |
| 27 | static void *getTag(); |
| 28 | void PreVisitCastExpr(CheckerContext &C, const CastExpr *B); |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | void *CastToStructChecker::getTag() { |
| 33 | static int x; |
| 34 | return &x; |
| 35 | } |
| 36 | |
| 37 | void CastToStructChecker::PreVisitCastExpr(CheckerContext &C, |
| 38 | const CastExpr *CE) { |
| 39 | const Expr *E = CE->getSubExpr(); |
| 40 | ASTContext &Ctx = C.getASTContext(); |
| 41 | QualType OrigTy = Ctx.getCanonicalType(E->getType()); |
| 42 | QualType ToTy = Ctx.getCanonicalType(CE->getType()); |
| 43 | |
| 44 | PointerType *OrigPTy = dyn_cast<PointerType>(OrigTy.getTypePtr()); |
| 45 | PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr()); |
| 46 | |
| 47 | if (!ToPTy || !OrigPTy) |
| 48 | return; |
| 49 | |
| 50 | QualType OrigPointeeTy = OrigPTy->getPointeeType(); |
| 51 | QualType ToPointeeTy = ToPTy->getPointeeType(); |
| 52 | |
Ted Kremenek | de7d800 | 2009-11-11 06:43:42 +0000 | [diff] [blame^] | 53 | if (!ToPointeeTy->isStructureType()) |
Zhongxing Xu | 4f3dc69 | 2009-11-09 08:07:38 +0000 | [diff] [blame] | 54 | return; |
| 55 | |
| 56 | // We allow cast from void*. |
| 57 | if (OrigPointeeTy->isVoidType()) |
| 58 | return; |
| 59 | |
| 60 | // Now the cast-to-type is struct pointer, the original type is not void*. |
| 61 | if (!OrigPointeeTy->isRecordType()) { |
| 62 | if (ExplodedNode *N = C.GenerateNode(CE)) { |
| 63 | if (!BT) |
| 64 | BT = new BuiltinBug("Cast from non-struct type to struct type", |
| 65 | "Casting a non-structure type to a structure type " |
| 66 | "and accessing a field can lead to memory access " |
| 67 | "errors or data corruption."); |
| 68 | RangedBugReport *R = new RangedBugReport(*BT,BT->getDescription().c_str(), |
| 69 | N); |
| 70 | R->addRange(CE->getSourceRange()); |
| 71 | C.EmitReport(R); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void clang::RegisterCastToStructChecker(GRExprEngine &Eng) { |
| 77 | Eng.registerCheck(new CastToStructChecker()); |
| 78 | } |