blob: c85521066711b8689134ad78e55bb975eb3b5f57 [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 Kyrtzidisec8605f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000021
22using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000023using namespace ento;
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000024
25namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000026class CastToStructChecker : public Checker< check::PreStmt<CastExpr> > {
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000027 mutable llvm::OwningPtr<BuiltinBug> BT;
28
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000029public:
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000030 void checkPreStmt(const CastExpr *CE, CheckerContext &C) const;
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000031};
32}
33
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000034void CastToStructChecker::checkPreStmt(const CastExpr *CE,
35 CheckerContext &C) const {
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000036 const Expr *E = CE->getSubExpr();
37 ASTContext &Ctx = C.getASTContext();
38 QualType OrigTy = Ctx.getCanonicalType(E->getType());
39 QualType ToTy = Ctx.getCanonicalType(CE->getType());
40
John McCallf4c73712011-01-19 06:33:43 +000041 const PointerType *OrigPTy = dyn_cast<PointerType>(OrigTy.getTypePtr());
42 const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr());
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000043
44 if (!ToPTy || !OrigPTy)
45 return;
46
47 QualType OrigPointeeTy = OrigPTy->getPointeeType();
48 QualType ToPointeeTy = ToPTy->getPointeeType();
49
Douglas Gregorfb87b892010-04-26 21:31:17 +000050 if (!ToPointeeTy->isStructureOrClassType())
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000051 return;
52
53 // We allow cast from void*.
54 if (OrigPointeeTy->isVoidType())
55 return;
56
57 // Now the cast-to-type is struct pointer, the original type is not void*.
58 if (!OrigPointeeTy->isRecordType()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +000059 if (ExplodedNode *N = C.generateNode()) {
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000060 if (!BT)
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000061 BT.reset(new BuiltinBug("Cast from non-struct type to struct type",
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000062 "Casting a non-structure type to a structure type "
63 "and accessing a field can lead to memory access "
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000064 "errors or data corruption."));
Anna Zakse172e8b2011-08-17 23:00:25 +000065 BugReport *R = new BugReport(*BT,BT->getDescription(), N);
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000066 R->addRange(CE->getSourceRange());
67 C.EmitReport(R);
68 }
69 }
70}
71
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000072void ento::registerCastToStructChecker(CheckerManager &mgr) {
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000073 mgr.registerChecker<CastToStructChecker>();
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000074}