blob: acd2373effe939cd63a287686bc7ab7899b05b3b [file] [log] [blame]
Ted Kremenek076d84e2009-07-23 01:07:19 +00001//==- CheckSecuritySyntaxOnly.cpp - Basic security checks --------*- 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 file defines a set of flow-insensitive security checks.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/PathSensitive/BugReporter.h"
15#include "clang/Analysis/LocalCheckers.h"
16#include "clang/AST/StmtVisitor.h"
17#include "llvm/Support/Compiler.h"
Ted Kremenekf08a6c52009-07-23 21:34:35 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenek076d84e2009-07-23 01:07:19 +000019
20using namespace clang;
21
22namespace {
23class VISIBILITY_HIDDEN WalkAST : public StmtVisitor<WalkAST> {
24 BugReporter &BR;
25public:
26 WalkAST(BugReporter &br) : BR(br) {}
27
28 // Statement visitor methods.
Ted Kremenek076d84e2009-07-23 01:07:19 +000029 void VisitForStmt(ForStmt *S);
Ted Kremenekf08a6c52009-07-23 21:34:35 +000030 void VisitStmt(Stmt *S) { VisitChildren(S); }
Ted Kremenek076d84e2009-07-23 01:07:19 +000031
32 void VisitChildren(Stmt *S);
Ted Kremenek076d84e2009-07-23 01:07:19 +000033
34 // Checker-specific methods.
Ted Kremenekf08a6c52009-07-23 21:34:35 +000035 void CheckLoopConditionForFloat(const ForStmt *FS);
Ted Kremenek076d84e2009-07-23 01:07:19 +000036};
37} // end anonymous namespace
38
39//===----------------------------------------------------------------------===//
40// AST walking.
41//===----------------------------------------------------------------------===//
42
43void WalkAST::VisitChildren(Stmt *S) {
44 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
45 if (Stmt *child = *I)
46 Visit(child);
47}
48
Ted Kremenekf08a6c52009-07-23 21:34:35 +000049void WalkAST::VisitForStmt(ForStmt *FS) {
50 CheckLoopConditionForFloat(FS);
Ted Kremenek076d84e2009-07-23 01:07:19 +000051
Ted Kremenekf08a6c52009-07-23 21:34:35 +000052 // Recurse and check children.
53 VisitChildren(FS);
Ted Kremenek076d84e2009-07-23 01:07:19 +000054}
55
56//===----------------------------------------------------------------------===//
Ted Kremenekf08a6c52009-07-23 21:34:35 +000057// Check: floating poing variable used as loop counter.
Ted Kremenek076d84e2009-07-23 01:07:19 +000058//===----------------------------------------------------------------------===//
59
Ted Kremenekf08a6c52009-07-23 21:34:35 +000060static const DeclRefExpr*
61GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
62 expr = expr->IgnoreParenCasts();
63
64 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
65 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
66 B->getOpcode() == BinaryOperator::Comma))
67 return NULL;
68
69 if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
70 return lhs;
71
72 if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
73 return rhs;
74
75 return NULL;
Ted Kremenek076d84e2009-07-23 01:07:19 +000076 }
Ted Kremenekf08a6c52009-07-23 21:34:35 +000077
78 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
79 const NamedDecl *ND = DR->getDecl();
80 return ND == x || ND == y ? DR : NULL;
81 }
82
83 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
84 return U->isIncrementDecrementOp()
85 ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
86
Ted Kremenek076d84e2009-07-23 01:07:19 +000087 return NULL;
88}
89
Ted Kremenekf08a6c52009-07-23 21:34:35 +000090/// CheckLoopConditionForFloat - This check looks for 'for' statements that
91/// use a floating point variable as a loop counter.
92/// CERT: FLP30-C, FLP30-CPP.
93///
94void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
95 // Does the loop have a condition?
96 const Expr *condition = FS->getCond();
97
98 if (!condition)
99 return;
100
101 // Does the loop have an increment?
102 const Expr *increment = FS->getInc();
103
104 if (!increment)
105 return;
106
107 // Strip away '()' and casts.
108 condition = condition->IgnoreParenCasts();
109 increment = increment->IgnoreParenCasts();
110
111 // Is the loop condition a comparison?
112 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
113
114 if (!B)
115 return;
116
117 // The actual error condition.
118 if (!((B->isRelationalOp() || B->isEqualityOp()) &&
119 ((B->getLHS()->getType()->isFloatingType() ||
120 B->getRHS()->getType()->isFloatingType()))))
121 return;
122
123 // Are we comparing variables?
124 const DeclRefExpr *drLHS = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens());
125 const DeclRefExpr *drRHS = dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParens());
126
127 if (!drLHS && !drRHS)
128 return;
129
130 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
131 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
132
133 if (!vdLHS && !vdRHS)
134 return;
135
136 // Does either variable appear in increment?
137 const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
138
139 if (!drInc)
140 return;
141
142 // Emit the error. First figure out which DeclRefExpr in the condition
143 // referenced the compared variable.
144 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
145
146 llvm::SmallVector<SourceRange, 2> ranges;
147 std::string sbuf;
148 llvm::raw_string_ostream os(sbuf);
149
150 os << "Variable '" << drCond->getDecl()->getNameAsCString()
151 << "' with floating point type '" << drCond->getType().getAsString()
152 << "' should not be used as a loop counter";
153
154 ranges.push_back(drCond->getSourceRange());
155 ranges.push_back(drInc->getSourceRange());
156
157 const char *bugType = "Floating point variable used as loop counter";
158 BR.EmitBasicReport(bugType, "Security", os.str().c_str(),
159 FS->getLocStart(), ranges.data(), ranges.size());
Ted Kremenek076d84e2009-07-23 01:07:19 +0000160}
161
162//===----------------------------------------------------------------------===//
163// Entry point for check.
164//===----------------------------------------------------------------------===//
165
166void clang::CheckSecuritySyntaxOnly(Decl *D, BugReporter &BR) {
167 WalkAST walker(BR);
168 walker.Visit(D->getBody());
169}