blob: fa7841356efbdf0eca4a2538f8b7cce51f259479 [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"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.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> > {
Ahmed Charlesb8984322014-03-07 20:03:18 +000027 mutable std::unique_ptr<BuiltinBug> BT;
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000028
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()) {
Devin Coughline39bd402015-09-16 22:03:05 +000059 if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
Zhongxing Xuf06c6842009-11-09 08:07:38 +000060 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000061 BT.reset(
62 new BuiltinBug(this, "Cast from non-struct type to struct type",
63 "Casting a non-structure type to a structure type "
64 "and accessing a field can lead to memory access "
65 "errors or data corruption."));
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000066 auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
Zhongxing Xuf06c6842009-11-09 08:07:38 +000067 R->addRange(CE->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000068 C.emitReport(std::move(R));
Zhongxing Xuf06c6842009-11-09 08:07:38 +000069 }
70 }
71}
72
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000073void ento::registerCastToStructChecker(CheckerManager &mgr) {
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000074 mgr.registerChecker<CastToStructChecker>();
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000075}