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 |
Zhongxing Xu | 3689791 | 2010-01-20 07:57:45 +0000 | [diff] [blame] | 11 | // cast from non-struct pointer to struct pointer. |
Zhongxing Xu | 4f3dc69 | 2009-11-09 08:07:38 +0000 | [diff] [blame] | 12 | // This check corresponds to CWE-588. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 16 | #include "clang/Checker/BugReporter/BugType.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 17 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
Zhongxing Xu | 4f3dc69 | 2009-11-09 08:07:38 +0000 | [diff] [blame] | 18 | #include "GRExprEngineInternalChecks.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 23 | class CastToStructChecker |
Zhongxing Xu | 4f3dc69 | 2009-11-09 08:07:38 +0000 | [diff] [blame] | 24 | : public CheckerVisitor<CastToStructChecker> { |
| 25 | BuiltinBug *BT; |
| 26 | public: |
| 27 | CastToStructChecker() : BT(0) {} |
| 28 | static void *getTag(); |
| 29 | void PreVisitCastExpr(CheckerContext &C, const CastExpr *B); |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | void *CastToStructChecker::getTag() { |
| 34 | static int x; |
| 35 | return &x; |
| 36 | } |
| 37 | |
| 38 | void CastToStructChecker::PreVisitCastExpr(CheckerContext &C, |
| 39 | const CastExpr *CE) { |
| 40 | const Expr *E = CE->getSubExpr(); |
| 41 | ASTContext &Ctx = C.getASTContext(); |
| 42 | QualType OrigTy = Ctx.getCanonicalType(E->getType()); |
| 43 | QualType ToTy = Ctx.getCanonicalType(CE->getType()); |
| 44 | |
| 45 | PointerType *OrigPTy = dyn_cast<PointerType>(OrigTy.getTypePtr()); |
| 46 | PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr()); |
| 47 | |
| 48 | if (!ToPTy || !OrigPTy) |
| 49 | return; |
| 50 | |
| 51 | QualType OrigPointeeTy = OrigPTy->getPointeeType(); |
| 52 | QualType ToPointeeTy = ToPTy->getPointeeType(); |
| 53 | |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 54 | if (!ToPointeeTy->isStructureOrClassType()) |
Zhongxing Xu | 4f3dc69 | 2009-11-09 08:07:38 +0000 | [diff] [blame] | 55 | return; |
| 56 | |
| 57 | // We allow cast from void*. |
| 58 | if (OrigPointeeTy->isVoidType()) |
| 59 | return; |
| 60 | |
| 61 | // Now the cast-to-type is struct pointer, the original type is not void*. |
| 62 | if (!OrigPointeeTy->isRecordType()) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 63 | if (ExplodedNode *N = C.generateNode()) { |
Zhongxing Xu | 4f3dc69 | 2009-11-09 08:07:38 +0000 | [diff] [blame] | 64 | if (!BT) |
| 65 | BT = new BuiltinBug("Cast from non-struct type to struct type", |
| 66 | "Casting a non-structure type to a structure type " |
| 67 | "and accessing a field can lead to memory access " |
| 68 | "errors or data corruption."); |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 69 | RangedBugReport *R = new RangedBugReport(*BT,BT->getDescription(), N); |
Zhongxing Xu | 4f3dc69 | 2009-11-09 08:07:38 +0000 | [diff] [blame] | 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 | } |