blob: a3663421a0826a137a5309fa8194668ae60defaa [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 {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000022class CastToStructChecker
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000023 : 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()) {
Ted Kremenek19d67b52009-11-23 22:22:01 +000062 if (ExplodedNode *N = C.GenerateNode()) {
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000063 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 Kramerd02e2322009-11-14 12:08:24 +000068 RangedBugReport *R = new RangedBugReport(*BT,BT->getDescription(), N);
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000069 R->addRange(CE->getSourceRange());
70 C.EmitReport(R);
71 }
72 }
73}
74
75void clang::RegisterCastToStructChecker(GRExprEngine &Eng) {
76 Eng.registerCheck(new CastToStructChecker());
77}