blob: 3210b0a4bb61e4e343f7e458b66f614b63c8736a [file] [log] [blame]
Zhongxing Xuf06c6842009-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 Xueb76a852010-01-20 07:57:45 +000011// cast from non-struct pointer to struct pointer.
Zhongxing Xuf06c6842009-11-09 08:07:38 +000012// This check corresponds to CWE-588.
13//
14//===----------------------------------------------------------------------===//
15
Argyrios Kyrtzidisa9215282011-02-15 22:55:20 +000016#include "ClangSACheckers.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Zhongxing Xuf06c6842009-11-09 08:07:38 +000021
22using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000023using namespace ento;
Zhongxing Xuf06c6842009-11-09 08:07:38 +000024
25namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000026class CastToStructChecker : public Checker< check::PreStmt<CastExpr> > {
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000027 mutable llvm::OwningPtr<BuiltinBug> BT;
28
Zhongxing Xuf06c6842009-11-09 08:07:38 +000029public:
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000030 void checkPreStmt(const CastExpr *CE, CheckerContext &C) const;
Zhongxing Xuf06c6842009-11-09 08:07:38 +000031};
32}
33
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000034void CastToStructChecker::checkPreStmt(const CastExpr *CE,
35 CheckerContext &C) const {
Zhongxing Xuf06c6842009-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 McCall424cec92011-01-19 06:33:43 +000041 const PointerType *OrigPTy = dyn_cast<PointerType>(OrigTy.getTypePtr());
42 const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr());
Zhongxing Xuf06c6842009-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 Gregor8385a062010-04-26 21:31:17 +000050 if (!ToPointeeTy->isStructureOrClassType())
Zhongxing Xuf06c6842009-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 Kremenek750b7ac2010-12-20 21:19:09 +000059 if (ExplodedNode *N = C.generateNode()) {
Zhongxing Xuf06c6842009-11-09 08:07:38 +000060 if (!BT)
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000061 BT.reset(new BuiltinBug("Cast from non-struct type to struct type",
Zhongxing Xuf06c6842009-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 Kyrtzidisdff865d2011-02-23 01:05:36 +000064 "errors or data corruption."));
Benjamin Kramerf4c511b2009-11-14 12:08:24 +000065 RangedBugReport *R = new RangedBugReport(*BT,BT->getDescription(), N);
Zhongxing Xuf06c6842009-11-09 08:07:38 +000066 R->addRange(CE->getSourceRange());
67 C.EmitReport(R);
68 }
69 }
70}
71
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000072void ento::registerCastToStructChecker(CheckerManager &mgr) {
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000073 mgr.registerChecker<CastToStructChecker>();
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000074}