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 { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 22 | class CastToStructChecker |
Zhongxing Xu | 4f3dc69 | 2009-11-09 08:07:38 +0000 | [diff] [blame] | 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()) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 62 | if (ExplodedNode *N = C.GenerateNode()) { |
Zhongxing Xu | 4f3dc69 | 2009-11-09 08:07:38 +0000 | [diff] [blame] | 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."); |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 68 | RangedBugReport *R = new RangedBugReport(*BT,BT->getDescription(), N); |
Zhongxing Xu | 4f3dc69 | 2009-11-09 08:07:38 +0000 | [diff] [blame] | 69 | R->addRange(CE->getSourceRange()); |
| 70 | C.EmitReport(R); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void clang::RegisterCastToStructChecker(GRExprEngine &Eng) { |
| 76 | Eng.registerCheck(new CastToStructChecker()); |
| 77 | } |