blob: 8ec226abf6a9c07d3ded7040f22913011a73fc02 [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
Zhongxing Xu36897912010-01-20 07:57:45 +000011// cast from non-struct pointer to struct pointer.
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000012// This check corresponds to CWE-588.
13//
14//===----------------------------------------------------------------------===//
15
Argyrios Kyrtzidis23ade502011-02-15 22:55:20 +000016#include "ClangSACheckers.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerVisitor.h"
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000020
21using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000022using namespace ento;
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000023
24namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000025class CastToStructChecker
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000026 : public CheckerVisitor<CastToStructChecker> {
27 BuiltinBug *BT;
28public:
29 CastToStructChecker() : BT(0) {}
30 static void *getTag();
31 void PreVisitCastExpr(CheckerContext &C, const CastExpr *B);
32};
33}
34
35void *CastToStructChecker::getTag() {
36 static int x;
37 return &x;
38}
39
40void CastToStructChecker::PreVisitCastExpr(CheckerContext &C,
41 const CastExpr *CE) {
42 const Expr *E = CE->getSubExpr();
43 ASTContext &Ctx = C.getASTContext();
44 QualType OrigTy = Ctx.getCanonicalType(E->getType());
45 QualType ToTy = Ctx.getCanonicalType(CE->getType());
46
John McCallf4c73712011-01-19 06:33:43 +000047 const PointerType *OrigPTy = dyn_cast<PointerType>(OrigTy.getTypePtr());
48 const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr());
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000049
50 if (!ToPTy || !OrigPTy)
51 return;
52
53 QualType OrigPointeeTy = OrigPTy->getPointeeType();
54 QualType ToPointeeTy = ToPTy->getPointeeType();
55
Douglas Gregorfb87b892010-04-26 21:31:17 +000056 if (!ToPointeeTy->isStructureOrClassType())
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000057 return;
58
59 // We allow cast from void*.
60 if (OrigPointeeTy->isVoidType())
61 return;
62
63 // Now the cast-to-type is struct pointer, the original type is not void*.
64 if (!OrigPointeeTy->isRecordType()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +000065 if (ExplodedNode *N = C.generateNode()) {
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000066 if (!BT)
67 BT = new BuiltinBug("Cast from non-struct type to struct type",
68 "Casting a non-structure type to a structure type "
69 "and accessing a field can lead to memory access "
70 "errors or data corruption.");
Benjamin Kramerd02e2322009-11-14 12:08:24 +000071 RangedBugReport *R = new RangedBugReport(*BT,BT->getDescription(), N);
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000072 R->addRange(CE->getSourceRange());
73 C.EmitReport(R);
74 }
75 }
76}
77
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000078static void RegisterCastToStructChecker(ExprEngine &Eng) {
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000079 Eng.registerCheck(new CastToStructChecker());
80}
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000081
82void ento::registerCastToStructChecker(CheckerManager &mgr) {
83 mgr.addCheckerRegisterFunction(RegisterCastToStructChecker);
84}