blob: 36b729e219596bd8a95f8bdbeaa09de3a70acf7e [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 Kremenek69576e92009-07-23 21:44:18 +000058// Originally: <rdar://problem/6336718>
59// Implements: CERT security coding advisory FLP-30.
Ted Kremenek076d84e2009-07-23 01:07:19 +000060//===----------------------------------------------------------------------===//
61
Ted Kremenekf08a6c52009-07-23 21:34:35 +000062static const DeclRefExpr*
63GetIncrementedVar(const Expr *expr, const VarDecl *x, const VarDecl *y) {
64 expr = expr->IgnoreParenCasts();
65
66 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(expr)) {
67 if (!(B->isAssignmentOp() || B->isCompoundAssignmentOp() ||
68 B->getOpcode() == BinaryOperator::Comma))
69 return NULL;
70
71 if (const DeclRefExpr *lhs = GetIncrementedVar(B->getLHS(), x, y))
72 return lhs;
73
74 if (const DeclRefExpr *rhs = GetIncrementedVar(B->getRHS(), x, y))
75 return rhs;
76
77 return NULL;
Ted Kremenek076d84e2009-07-23 01:07:19 +000078 }
Ted Kremenekf08a6c52009-07-23 21:34:35 +000079
80 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(expr)) {
81 const NamedDecl *ND = DR->getDecl();
82 return ND == x || ND == y ? DR : NULL;
83 }
84
85 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(expr))
86 return U->isIncrementDecrementOp()
87 ? GetIncrementedVar(U->getSubExpr(), x, y) : NULL;
88
Ted Kremenek076d84e2009-07-23 01:07:19 +000089 return NULL;
90}
91
Ted Kremenekf08a6c52009-07-23 21:34:35 +000092/// CheckLoopConditionForFloat - This check looks for 'for' statements that
93/// use a floating point variable as a loop counter.
94/// CERT: FLP30-C, FLP30-CPP.
95///
96void WalkAST::CheckLoopConditionForFloat(const ForStmt *FS) {
97 // Does the loop have a condition?
98 const Expr *condition = FS->getCond();
99
100 if (!condition)
101 return;
102
103 // Does the loop have an increment?
104 const Expr *increment = FS->getInc();
105
106 if (!increment)
107 return;
108
109 // Strip away '()' and casts.
110 condition = condition->IgnoreParenCasts();
111 increment = increment->IgnoreParenCasts();
112
113 // Is the loop condition a comparison?
114 const BinaryOperator *B = dyn_cast<BinaryOperator>(condition);
115
116 if (!B)
117 return;
118
119 // The actual error condition.
120 if (!((B->isRelationalOp() || B->isEqualityOp()) &&
121 ((B->getLHS()->getType()->isFloatingType() ||
122 B->getRHS()->getType()->isFloatingType()))))
123 return;
124
125 // Are we comparing variables?
126 const DeclRefExpr *drLHS = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens());
127 const DeclRefExpr *drRHS = dyn_cast<DeclRefExpr>(B->getRHS()->IgnoreParens());
128
129 if (!drLHS && !drRHS)
130 return;
131
132 const VarDecl *vdLHS = drLHS ? dyn_cast<VarDecl>(drLHS->getDecl()) : NULL;
133 const VarDecl *vdRHS = drRHS ? dyn_cast<VarDecl>(drRHS->getDecl()) : NULL;
134
135 if (!vdLHS && !vdRHS)
136 return;
137
138 // Does either variable appear in increment?
139 const DeclRefExpr *drInc = GetIncrementedVar(increment, vdLHS, vdRHS);
140
141 if (!drInc)
142 return;
143
144 // Emit the error. First figure out which DeclRefExpr in the condition
145 // referenced the compared variable.
146 const DeclRefExpr *drCond = vdLHS == drInc->getDecl() ? drLHS : drRHS;
147
148 llvm::SmallVector<SourceRange, 2> ranges;
149 std::string sbuf;
150 llvm::raw_string_ostream os(sbuf);
151
152 os << "Variable '" << drCond->getDecl()->getNameAsCString()
153 << "' with floating point type '" << drCond->getType().getAsString()
154 << "' should not be used as a loop counter";
155
156 ranges.push_back(drCond->getSourceRange());
157 ranges.push_back(drInc->getSourceRange());
158
159 const char *bugType = "Floating point variable used as loop counter";
160 BR.EmitBasicReport(bugType, "Security", os.str().c_str(),
161 FS->getLocStart(), ranges.data(), ranges.size());
Ted Kremenek076d84e2009-07-23 01:07:19 +0000162}
163
164//===----------------------------------------------------------------------===//
165// Entry point for check.
166//===----------------------------------------------------------------------===//
167
168void clang::CheckSecuritySyntaxOnly(Decl *D, BugReporter &BR) {
169 WalkAST walker(BR);
170 walker.Visit(D->getBody());
171}