blob: bda8ff47bf85a98b31297e63814623f6ed0b75e8 [file] [log] [blame]
Zhongxing Xu4f3dc692009-11-09 08:07:38 +00001//=== 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
19using namespace clang;
20
21namespace {
22class VISIBILITY_HIDDEN CastToStructChecker
23 : public CheckerVisitor<CastToStructChecker> {
24 BuiltinBug *BT;
25public:
26 CastToStructChecker() : BT(0) {}
27 static void *getTag();
28 void PreVisitCastExpr(CheckerContext &C, const CastExpr *B);
29};
30}
31
32void *CastToStructChecker::getTag() {
33 static int x;
34 return &x;
35}
36
37void 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 Kremenekde7d8002009-11-11 06:43:42 +000053 if (!ToPointeeTy->isStructureType())
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000054 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
76void clang::RegisterCastToStructChecker(GRExprEngine &Eng) {
77 Eng.registerCheck(new CastToStructChecker());
78}