[cfe-commits] r76833 - in /cfe/trunk: include/clang/Analysis/LocalCheckers.h include/clang/Frontend/Analyses.def lib/Analysis/CMakeLists.txt lib/Analysis/CheckSecuritySyntaxOnly.cpp lib/Frontend/AnalysisConsumer.cpp

Ted Kremenek kremenek at apple.com
Wed Jul 22 18:07:20 PDT 2009


Author: kremenek
Date: Wed Jul 22 20:07:19 2009
New Revision: 76833

URL: http://llvm.org/viewvc/llvm-project?rev=76833&view=rev
Log:
Add initial implementation of checking for uses of floating point as a loop counter.

Added:
    cfe/trunk/lib/Analysis/CheckSecuritySyntaxOnly.cpp
Modified:
    cfe/trunk/include/clang/Analysis/LocalCheckers.h
    cfe/trunk/include/clang/Frontend/Analyses.def
    cfe/trunk/lib/Analysis/CMakeLists.txt
    cfe/trunk/lib/Frontend/AnalysisConsumer.cpp

Modified: cfe/trunk/include/clang/Analysis/LocalCheckers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/LocalCheckers.h?rev=76833&r1=76832&r2=76833&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/LocalCheckers.h (original)
+++ cfe/trunk/include/clang/Analysis/LocalCheckers.h Wed Jul 22 20:07:19 2009
@@ -48,6 +48,9 @@
   
 void RegisterAppleChecks(GRExprEngine& Eng);
   
+void CheckSecuritySyntaxOnly(Decl *D, BugReporter &BR);
+
+  
 } // end namespace clang
 
 #endif

Modified: cfe/trunk/include/clang/Frontend/Analyses.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/Analyses.def?rev=76833&r1=76832&r2=76833&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/Analyses.def (original)
+++ cfe/trunk/include/clang/Frontend/Analyses.def Wed Jul 22 20:07:19 2009
@@ -24,6 +24,10 @@
 ANALYSIS(DisplayLiveVariables, "dump-live-variables",
          "Print results of live variable analysis", Code)
 
+ANALYSIS(SecuritySyntacticChecks, "warn-security-syntactic",
+         "Perform quick security checks that require no data flow",
+         Code)
+
 ANALYSIS(WarnDeadStores, "warn-dead-stores",
          "Warn about stores to dead variables", Code)
 

Modified: cfe/trunk/lib/Analysis/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CMakeLists.txt?rev=76833&r1=76832&r2=76833&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CMakeLists.txt (original)
+++ cfe/trunk/lib/Analysis/CMakeLists.txt Wed Jul 22 20:07:19 2009
@@ -15,6 +15,7 @@
   CheckObjCDealloc.cpp
   CheckObjCInstMethSignature.cpp
   CheckObjCUnusedIVars.cpp
+  CheckSecuritySyntaxOnly.cpp
   Environment.cpp
   ExplodedGraph.cpp
   GRBlockCounter.cpp

Added: cfe/trunk/lib/Analysis/CheckSecuritySyntaxOnly.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CheckSecuritySyntaxOnly.cpp?rev=76833&view=auto

==============================================================================
--- cfe/trunk/lib/Analysis/CheckSecuritySyntaxOnly.cpp (added)
+++ cfe/trunk/lib/Analysis/CheckSecuritySyntaxOnly.cpp Wed Jul 22 20:07:19 2009
@@ -0,0 +1,122 @@
+//==- CheckSecuritySyntaxOnly.cpp - Basic security checks --------*- C++ -*-==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines a set of flow-insensitive security checks.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Analysis/PathSensitive/BugReporter.h"
+#include "clang/Analysis/LocalCheckers.h"
+#include "clang/AST/StmtVisitor.h"
+#include "llvm/Support/Compiler.h"
+
+using namespace clang;
+
+namespace {
+class VISIBILITY_HIDDEN WalkAST : public StmtVisitor<WalkAST> {
+  BugReporter &BR;
+public:
+  WalkAST(BugReporter &br) : BR(br) {}
+  
+  // Statement visitor methods.
+  void VisitDoStmt(DoStmt *S);
+  void VisitWhileStmt(WhileStmt *S);
+  void VisitForStmt(ForStmt *S);
+
+  void VisitChildren(Stmt *S);
+  void VisitStmt(Stmt *S) { VisitChildren(S); }
+  
+  // Checker-specific methods.
+  void CheckLoopConditionForFloat(Stmt *Loop, Expr *Condition); 
+};
+} // end anonymous namespace
+
+//===----------------------------------------------------------------------===//
+// AST walking.
+//===----------------------------------------------------------------------===//
+
+void WalkAST::VisitChildren(Stmt *S) {
+  for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
+    if (Stmt *child = *I)
+      Visit(child);
+}
+
+void WalkAST::VisitDoStmt(DoStmt *S) {
+  CheckLoopConditionForFloat(S, S->getCond());
+  VisitChildren(S);
+}
+
+void WalkAST::VisitForStmt(ForStmt *S) {
+  if (Expr *Cond = S->getCond())  
+    CheckLoopConditionForFloat(S, Cond);  
+
+  VisitChildren(S);
+}
+
+void WalkAST::VisitWhileStmt(WhileStmt *S) {
+  CheckLoopConditionForFloat(S, S->getCond());
+  VisitChildren(S);
+}
+
+//===----------------------------------------------------------------------===//
+// Checking logic.
+//===----------------------------------------------------------------------===//
+
+static Expr* IsFloatCondition(Expr *Condition) {  
+  while (Condition) {
+    Condition = Condition->IgnoreParenCasts();
+
+    if (Condition->getType()->isFloatingType())
+      return Condition;
+
+    switch (Condition->getStmtClass()) {
+      case Stmt::BinaryOperatorClass: {
+        BinaryOperator *B = cast<BinaryOperator>(Condition);
+
+        Expr *LHS = B->getLHS();
+        if (LHS->getType()->isFloatingType())
+          return LHS;
+
+        Expr *RHS = B->getRHS();
+        if (RHS->getType()->isFloatingType())
+          return RHS;
+
+        return NULL;
+      }
+      case Stmt::UnaryOperatorClass: {
+        UnaryOperator *U = cast<UnaryOperator>(Condition);
+        if (U->isArithmeticOp()) {
+          Condition = U->getSubExpr();
+          continue;
+        }
+        return NULL;
+      }
+      default:
+        break;
+    }
+  }
+  return NULL;
+}
+
+void WalkAST::CheckLoopConditionForFloat(Stmt *Loop, Expr *Condition) {
+  if ((Condition = IsFloatCondition(Condition))) {
+    const char *bugType = "Floating point value used in loop condition";
+    SourceRange R = Condition->getSourceRange();    
+    BR.EmitBasicReport(bugType, "Security", bugType, Loop->getLocStart(),&R, 1);
+  }
+}
+
+//===----------------------------------------------------------------------===//
+// Entry point for check.
+//===----------------------------------------------------------------------===//
+
+void clang::CheckSecuritySyntaxOnly(Decl *D, BugReporter &BR) {  
+  WalkAST walker(BR);
+  walker.Visit(D->getBody());  
+}

Modified: cfe/trunk/lib/Frontend/AnalysisConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/AnalysisConsumer.cpp?rev=76833&r1=76832&r2=76833&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/AnalysisConsumer.cpp (original)
+++ cfe/trunk/lib/Frontend/AnalysisConsumer.cpp Wed Jul 22 20:07:19 2009
@@ -492,6 +492,11 @@
   }
 }
 
+static void ActionSecuritySyntacticChecks(AnalysisManager &mgr) {
+  BugReporter BR(mgr);  
+  CheckSecuritySyntaxOnly(mgr.getCodeDecl(), BR);
+}
+
 static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
   if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
     return;





More information about the cfe-commits mailing list