emmettneyman updated this revision to Diff 151801.
emmettneyman added a comment.

- removed unnecessary includes and linked libs


Repository:
  rC Clang

https://reviews.llvm.org/D48106

Files:
  tools/clang-fuzzer/CMakeLists.txt
  tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
  tools/clang-fuzzer/cxx_loop_proto.proto
  tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  tools/clang-fuzzer/handle-llvm/\
  tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  tools/clang-fuzzer/handle-llvm/handle_llvm.h
  tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx_main.cpp
  tools/clang-fuzzer/proto-to-llvm/CMakeLists.txt
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp

Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
===================================================================
--- tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
@@ -1,32 +1,31 @@
-//==-- loop_proto_to_cxx_main.cpp - Driver for protobuf-C++ conversion -----==//
+//==-- loop_proto_to_llvm_main.cpp - Driver for protobuf-LLVM conversion----==//
 //
 //                     The LLVM Compiler Infrastructure
 //
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
-// Implements a simple driver to print a C++ program from a protobuf with loops.
+// Implements a simple driver to print a LLVM program from a protobuf with loops
 //
 //===----------------------------------------------------------------------===//
 
-// This is a copy and will be updated later to introduce changes
 
 #include <fstream>
 #include <iostream>
 #include <streambuf>
 #include <string>
 
-#include "proto_to_cxx.h"
+#include "loop_proto_to_llvm.h"
 
 int main(int argc, char **argv) {
   for (int i = 1; i < argc; i++) {
     std::fstream in(argv[i]);
     std::string str((std::istreambuf_iterator<char>(in)),
                     std::istreambuf_iterator<char>());
-    std::cout << "// " << argv[i] << std::endl;
-    std::cout << clang_fuzzer::LoopProtoToCxx(
+    std::cout << ";; " << argv[i] << std::endl;
+    std::cout << clang_fuzzer::LoopProtoToLLVM(
         reinterpret_cast<const uint8_t *>(str.data()), str.size());
   }
 }
Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
===================================================================
--- /dev/null
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
@@ -0,0 +1,23 @@
+//==-- loop_proto_to_llvm.h - Protobuf-C++ conversion ----------------------------==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Defines functions for converting between protobufs and LLVM IR.
+//
+//===----------------------------------------------------------------------===//
+
+#include <cstdint>
+#include <cstddef>
+#include <string>
+
+namespace clang_fuzzer {
+class LoopFunction;
+
+std::string LoopFunctionToLLVMString(const LoopFunction &input);
+std::string LoopProtoToLLVM(const uint8_t *data, size_t size);
+}
Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===================================================================
--- /dev/null
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -0,0 +1,159 @@
+//==-- loop_proto_to_llvm.cpp - Protobuf-C++ conversion
+//---------------------==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Implements functions for converting between protobufs and LLVM IR.
+//
+//
+//===----------------------------------------------------------------------===//
+
+#include "loop_proto_to_llvm.h"
+#include "cxx_loop_proto.pb.h"
+
+// The following is needed to convert protos in human-readable form
+#include <google/protobuf/text_format.h>
+
+#include <ostream>
+#include <sstream>
+
+namespace clang_fuzzer {
+
+// Forward decls
+std::string BinopToString(std::ostream &os, const BinaryOp &x);
+std::string StateSeqToString(std::ostream &os, const StatementSeq &x);
+
+// Counter variable to generate new LLVM IR variable names and wrapper function
+std::string get_var() {
+  static int ctr = 0;
+  return "%var" + std::to_string(ctr++);
+}
+
+// Proto to LLVM.
+
+std::string ConstToString(const Const &x) {
+  return std::to_string(x.val());
+}
+std::string VarRefToString(std::ostream &os, const VarRef &x) {
+  std::string arr;
+  switch(x.arr()) {
+  case VarRef::ARR_A:
+    arr = "%a";
+    break;
+  case VarRef::ARR_B:
+    arr = "%b";
+    break;
+  case VarRef::ARR_C:
+    arr = "%c";
+    break;
+  }
+  std::string ptr_var = get_var();
+  std::string insn = ptr_var + " = getelementptr i32, i32* " + arr + ", i64 %ct\n";
+  os << insn;
+  return ptr_var;
+}
+std::string RvalueToString(std::ostream &os, const Rvalue &x) {
+  if(x.has_cons())
+    return ConstToString(x.cons());
+  if(x.has_binop())
+    return BinopToString(os, x.binop());
+  if(x.has_varref()) {
+    std::string var_ref = VarRefToString(os, x.varref());
+    std::string val_var = get_var();
+    std::string insns = val_var + " = load i32, i32* " + var_ref + "\n";
+    os << insns;
+    return val_var;
+  }
+}
+std::string BinopToString(std::ostream &os, const BinaryOp &x) {
+  std::string left = RvalueToString(os, x.left());
+  std::string right = RvalueToString(os, x.right());
+  std::string op;
+  switch (x.op()) {
+  case BinaryOp::PLUS:
+    op = "add";
+    break;
+  case BinaryOp::MINUS:
+    op = "sub";
+    break;
+  case BinaryOp::MUL:
+    op = "mul";
+    break;
+  case BinaryOp::XOR:
+    op = "xor";
+    break;
+  case BinaryOp::AND:
+    op = "and";
+    break;
+  case BinaryOp::OR:
+    op = "or";
+    break;
+  // Support for Boolean operators will be added later
+  case BinaryOp::EQ:
+  case BinaryOp::NE:
+  case BinaryOp::LE:
+  case BinaryOp::GE:
+  case BinaryOp::LT:
+  case BinaryOp::GT:
+    op = "add";
+    break;
+  }
+  std::string val_var = get_var();
+  std::string insns = val_var + " = " + op + " i32 " + left
+                    + ", " + right + "\n";
+  os << insns;
+  return val_var;
+}
+std::ostream &operator<<(std::ostream &os, const AssignmentStatement &x) {
+  std::string ref = VarRefToString(os, x.varref());
+  std::string rv = RvalueToString(os, x.rvalue());
+  std::string insns = "store i32 " + rv + ", i32* " + ref + "\n";
+  return os << insns;
+}
+std::ostream &operator<<(std::ostream &os, const Statement &x) {
+  return os << x.assignment();
+}
+std::ostream &operator<<(std::ostream &os, const StatementSeq &x) {
+  for (auto &st : x.statements()) {
+    os << st;
+  }
+  return os;
+}
+std::ostream &operator<<(std::ostream &os, const LoopFunction &x) {
+  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+            << "%i = alloca i64\n"
+            << "store i64 0, i64* %i\n"
+            << "br label %loop\n\n"
+            << "loop:\n"
+            << "%ct = load i64, i64* %i\n"
+            << "%comp = icmp eq i64 %ct, %s\n"
+            << "br i1 %comp, label %endloop, label %body\n\n"
+            << "body:\n"
+            << x.statements()
+            << "%z = add i64 1, %ct\n"
+            << "store i64 %z, i64* %i\n"
+            << "br label %loop\n\n"
+            << "endloop:\n"
+            << "ret void\n}\n";
+}
+
+// ---------------------------------
+
+std::string LoopFunctionToLLVMString(const LoopFunction &input) {
+  std::ostringstream os;
+  os << input;
+  return os.str();
+}
+std::string LoopProtoToLLVM(const uint8_t *data, size_t size) {
+  LoopFunction message;
+  if (!message.ParsePartialFromArray(data, size))
+    return "#error invalid proto\n";
+  return LoopFunctionToLLVMString(message);
+}
+
+} // namespace clang_fuzzer
Index: tools/clang-fuzzer/proto-to-llvm/CMakeLists.txt
===================================================================
--- /dev/null
+++ tools/clang-fuzzer/proto-to-llvm/CMakeLists.txt
@@ -0,0 +1,14 @@
+set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD})
+set(CMAKE_CXX_FLAGS ${CXX_FLAGS_NOFUZZ})
+
+# Needed by LLVM's CMake checks because this file defines multiple targets.
+set(LLVM_OPTIONAL_SOURCES loop_proto_to_llvm.cpp loop_proto_to_llvm_main.cpp)
+
+add_clang_library(clangLoopProtoToLLVM loop_proto_to_llvm.cpp
+                  DEPENDS clangCXXLoopProto
+                  LINK_LIBS clangCXXLoopProto ${PROTOBUF_LIBRARIES}
+                  )
+
+add_clang_executable(clang-loop-proto-to-llvm loop_proto_to_llvm_main.cpp)
+
+target_link_libraries(clang-loop-proto-to-llvm PRIVATE clangLoopProtoToLLVM)
Index: tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx_main.cpp
===================================================================
--- tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx_main.cpp
+++ tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx_main.cpp
@@ -11,7 +11,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-// This is a copy and will be updated later to introduce changes
 
 #include <fstream>
 #include <iostream>
Index: tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
===================================================================
--- tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
+++ tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
@@ -67,12 +67,6 @@
   case BinaryOp::MUL:
     os << "*";
     break;
-  case BinaryOp::DIV:
-    os << "/";
-    break;
-  case BinaryOp::MOD:
-    os << "%";
-    break;
   case BinaryOp::XOR:
     os << "^";
     break;
@@ -106,11 +100,6 @@
 std::ostream &operator<<(std::ostream &os, const AssignmentStatement &x) {
   return os << x.varref() << "=" << x.rvalue() << ";\n";
 }
-std::ostream &operator<<(std::ostream &os, const IfElse &x) {
-  return os << "if (" << x.cond() << "){\n"
-            << x.if_body() << "} else { \n"
-            << x.else_body() << "}\n";
-}
 std::ostream &operator<<(std::ostream &os, const Statement &x) {
   return os << x.assignment();
 }
Index: tools/clang-fuzzer/handle-llvm/handle_llvm.h
===================================================================
--- /dev/null
+++ tools/clang-fuzzer/handle-llvm/handle_llvm.h
@@ -0,0 +1,25 @@
+//==-- handle_llvm.h - Helper function for Clang fuzzers -------------------==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Defines HandleLLVM for use by the Clang fuzzers.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_CLANG_FUZZER_HANDLE_LLVM_HANDLELLVM_H
+#define LLVM_CLANG_TOOLS_CLANG_FUZZER_HANDLE_LLVM_HANDLELLVM_H
+
+#include <string>
+#include <vector>
+
+namespace clang_fuzzer {
+void HandleLLVM(const std::string &S,
+               const std::vector<const char *> &ExtraArgs);
+} // namespace clang_fuzzer
+
+#endif
Index: tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===================================================================
--- /dev/null
+++ tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -0,0 +1,136 @@
+//==-- handle_llvm.cpp - Helper function for Clang fuzzers -----------------==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
+// compile an LLVM IR file to X86_64 assembly.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/PassRegistry.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/ADT/Triple.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Support/TargetRegistry.h"
+#include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/CommandFlags.inc"
+#include "llvm/IR/Verifier.h"
+#include "llvm/IRReader/IRReader.h"
+#include "llvm/Analysis/TargetLibraryInfo.h"
+
+#include "handle_llvm.h"
+
+
+using namespace llvm;
+
+void InitializeEverything() {
+
+  InitializeAllTargets();
+  InitializeAllTargetMCs();
+  InitializeAllAsmPrinters();
+  InitializeAllAsmParsers();
+
+  PassRegistry *Registry = PassRegistry::getPassRegistry();
+  initializeCore(*Registry);
+  initializeCodeGen(*Registry);
+  initializeLoopStrengthReducePass(*Registry);
+  initializeLowerIntrinsicsPass(*Registry);
+  initializeEntryExitInstrumenterPass(*Registry);
+  initializePostInlineEntryExitInstrumenterPass(*Registry);
+  initializeUnreachableBlockElimLegacyPassPass(*Registry);
+  initializeConstantHoistingLegacyPassPass(*Registry);
+  initializeScalarOpts(*Registry);
+  initializeVectorization(*Registry);
+  initializeScalarizeMaskedMemIntrinPass(*Registry);
+  initializeExpandReductionsPass(*Registry);
+  initializeScavengerTestPass(*Registry);
+
+}
+
+void clang_fuzzer::HandleLLVM(const std::string &S,
+                              const std::vector<const char *> &ExtraArgs) {
+
+  // Create new buffer from LLVM IR string S
+  StringRef *IRString = new StringRef(S);
+  StringRef *ID = new StringRef("IR");
+  MemoryBufferRef *ir = new MemoryBufferRef(*IRString, *ID);
+
+  // Create a new Context and call initializing functions
+  LLVMContext Context;
+  InitializeEverything();
+
+  // Create a new Module and Triple
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M;
+  Triple TheTriple;
+  TheTriple.setTriple(sys::getDefaultTargetTriple());
+
+  // Set the Module to include the the IR code to be compiled
+  M = parseIR(*ir, Err, Context, false);
+
+  // Create a new Target
+  std::string Error;
+  const Target *TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(),
+                                                         Error);
+  std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr();
+
+  // Find the optimization level from the command line args
+  // TODO: find a cleaner way to do this
+  CodeGenOpt::Level OLvl = CodeGenOpt::Default;
+  for (auto &A : ExtraArgs) {
+    if (A[0] == '-' && A[1] == 'O') {
+      switch(A[2]) {  
+	case '0': OLvl = CodeGenOpt::None; break;
+        case '1': OLvl = CodeGenOpt::Less; break;
+        case '2': OLvl = CodeGenOpt::Default; break;
+        case '3': OLvl = CodeGenOpt::Aggressive; break;
+      }
+    }
+  }
+
+  TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+
+  // Create a new Machine
+  std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
+      TheTriple.getTriple(), CPUStr, FeaturesStr, Options, getRelocModel(),
+      getCodeModel(), OLvl));
+
+  // Create a new PassManager
+  legacy::PassManager PM;
+  TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
+  PM.add(new TargetLibraryInfoWrapperPass(TLII));
+  M->setDataLayout(Target->createDataLayout());
+ 
+  // Make sure the Module has no errors
+  if (verifyModule(*M, &errs())) {
+    errs() << "error: input module is broken!\n";
+    return;
+  } 
+
+  setFunctionAttributes(CPUStr, FeaturesStr, *M);
+  
+  raw_null_ostream OS;
+  LLVMTargetMachine &LLVMTM = static_cast<LLVMTargetMachine&>(*Target);
+  MachineModuleInfo *MMI = new MachineModuleInfo(&LLVMTM);
+  Target->addPassesToEmitFile(PM, OS, nullptr, TargetMachine::CGFT_ObjectFile,
+                              false, MMI);
+  PM.run(*M);
+
+  delete ID;
+  delete IRString;
+  delete ir;
+
+  return;
+}
+
Index: tools/clang-fuzzer/handle-llvm/\
===================================================================
--- /dev/null
+++ tools/clang-fuzzer/handle-llvm/\
@@ -0,0 +1,140 @@
+//==-- handle_llvm.cpp - Helper function for Clang fuzzers -----------------==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
+// compile an LLVM IR file to X86_64 assembly.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/PassRegistry.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/ADT/Triple.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Support/TargetRegistry.h"
+#include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/CommandFlags.inc"
+#include "llvm/IR/Verifier.h"
+#include "llvm/IRReader/IRReader.h"
+#include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include <system_error>
+#include <iostream>
+
+#include "handle_llvm.h"
+
+
+using namespace llvm;
+
+void InitializeEverything() {
+
+  InitializeAllTargets();
+  InitializeAllTargetMCs();
+  InitializeAllAsmPrinters();
+  InitializeAllAsmParsers();
+
+  PassRegistry *Registry = PassRegistry::getPassRegistry();
+  initializeCore(*Registry);
+  initializeCodeGen(*Registry);
+  initializeLoopStrengthReducePass(*Registry);
+  initializeLowerIntrinsicsPass(*Registry);
+  initializeEntryExitInstrumenterPass(*Registry);
+  initializePostInlineEntryExitInstrumenterPass(*Registry);
+  initializeUnreachableBlockElimLegacyPassPass(*Registry);
+  initializeConstantHoistingLegacyPassPass(*Registry);
+  initializeScalarOpts(*Registry);
+  initializeVectorization(*Registry);
+  initializeScalarizeMaskedMemIntrinPass(*Registry);
+  initializeExpandReductionsPass(*Registry);
+  initializeScavengerTestPass(*Registry);
+
+}
+
+void clang_fuzzer::HandleLLVM(const std::string &S,
+                              const std::vector<const char *> &ExtraArgs) {
+
+  // Create new buffer from LLVM IR string S
+  StringRef *IRString = new StringRef(S);
+  StringRef *ID = new StringRef("IR");
+  MemoryBufferRef *ir = new MemoryBufferRef(*IRString, *ID);
+
+  // Create a new Context and call initializing functions
+  LLVMContext Context;
+  InitializeEverything();
+
+  // Create a new Module and Triple
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M;
+  Triple TheTriple;
+  TheTriple.setTriple(sys::getDefaultTargetTriple());
+
+  // Set the Module to include the the IR code to be compiled
+  M = parseIR(*ir, Err, Context, false);
+
+  // Create a new Target
+  std::string Error;
+  const Target *TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(),
+                                                         Error);
+  std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr();
+
+  // Find the optimization level from the command line args
+  // TODO: find a cleaner way to do this
+  CodeGenOpt::Level OLvl = CodeGenOpt::Default;
+  for (auto &A : ExtraArgs) {
+    if (A[0] == '-' && A[1] == 'O') {
+      switch(A[2]) {  
+	case '0': OLvl = CodeGenOpt::None; break;
+        case '1': OLvl = CodeGenOpt::Less; break;
+        case '2': OLvl = CodeGenOpt::Default; break;
+        case '3': OLvl = CodeGenOpt::Aggressive; break;
+      }
+    }
+  }
+
+  TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+
+  // Create a new Machine
+  std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
+      TheTriple.getTriple(), CPUStr, FeaturesStr, Options, getRelocModel(),
+      getCodeModel(), OLvl));
+
+  legacy::PassManager PM;
+  TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
+  PM.add(new TargetLibraryInfoWrapperPass(TLII));
+  M->setDataLayout(Target->createDataLayout());
+ 
+  if (verifyModule(*M, &errs())) {
+    errs() << "error: input module is broken!\n";
+    return;
+  } 
+
+  setFunctionAttributes(CPUStr, FeaturesStr, *M);
+  
+  raw_null_ostream OS;
+
+  LLVMTargetMachine &LLVMTM = static_cast<LLVMTargetMachine&>(*Target);
+  MachineModuleInfo *MMI = new MachineModuleInfo(&LLVMTM);
+  Target->addPassesToEmitFile(PM, OS, nullptr, TargetMachine::CGFT_ObjectFile,
+                              false, MMI);
+
+  PM.run(*M);
+
+  delete ID;
+  delete IRString;
+  delete ir;
+
+  return;
+}
+
Index: tools/clang-fuzzer/handle-llvm/CMakeLists.txt
===================================================================
--- /dev/null
+++ tools/clang-fuzzer/handle-llvm/CMakeLists.txt
@@ -0,0 +1,5 @@
+set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} Support)
+
+add_clang_library(clangHandleLLVM
+  handle_llvm.cpp
+  )
Index: tools/clang-fuzzer/cxx_loop_proto.proto
===================================================================
--- tools/clang-fuzzer/cxx_loop_proto.proto
+++ tools/clang-fuzzer/cxx_loop_proto.proto
@@ -37,17 +37,15 @@
     PLUS = 0;
     MINUS = 1;
     MUL = 2;
-    DIV = 3;
-    MOD = 4;
-    XOR = 5;
-    AND = 6;
-    OR = 7;
-    EQ = 8;
-    NE = 9;
-    LE = 10;
-    GE = 11;
-    LT = 12;
-    GT = 13;
+    XOR = 3;
+    AND = 4;
+    OR = 5;
+    EQ = 6;
+    NE = 7;
+    LE = 8;
+    GE = 9;
+    LT = 10;
+    GT = 11;
   };
   required Op op = 1;
   required Rvalue left = 2;
@@ -67,12 +65,6 @@
   required Rvalue rvalue = 2;
 }
 
-message IfElse {
-  required Rvalue cond = 1;
-  required StatementSeq if_body = 2;
-  required StatementSeq else_body = 3;
-}
-
 message Statement {
   required AssignmentStatement assignment = 1;
 }
Index: tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
===================================================================
--- /dev/null
+++ tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
@@ -0,0 +1,28 @@
+//===-- ExampleClangLLVMProtoFuzzer.cpp - Fuzz Clang ----------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+///  This file implements a function that compiles a single LLVM IR string as
+///  input and uses libprotobuf-mutator to find new inputs. This function is
+///  then linked into the Fuzzer library.
+///
+//===----------------------------------------------------------------------===//
+
+#include "cxx_loop_proto.pb.h"
+#include "fuzzer-initialize/fuzzer_initialize.h"
+#include "handle-llvm/handle_llvm.h"
+#include "proto-to-llvm/loop_proto_to_llvm.h"
+#include "src/libfuzzer/libfuzzer_macro.h"
+
+using namespace clang_fuzzer;
+
+DEFINE_BINARY_PROTO_FUZZER(const LoopFunction &input) {
+  auto S = LoopFunctionToLLVMString(input);
+  HandleLLVM(S, GetCLArgs());
+}
Index: tools/clang-fuzzer/CMakeLists.txt
===================================================================
--- tools/clang-fuzzer/CMakeLists.txt
+++ tools/clang-fuzzer/CMakeLists.txt
@@ -15,6 +15,7 @@
   DummyClangFuzzer.cpp
   ExampleClangProtoFuzzer.cpp
   ExampleClangLoopProtoFuzzer.cpp
+  ExampleClangLLVMProtoFuzzer.cpp
   )
 
 if(CLANG_ENABLE_PROTO_FUZZER)
@@ -49,6 +50,9 @@
 
   # Build the protobuf->C++ translation library and driver.
   add_clang_subdirectory(proto-to-cxx)
+
+  # Build the protobuf->LLVM IR translation library and driver.
+  add_clang_subdirectory(proto-to-llvm)
   
   # Build the fuzzer initialization library.
   add_clang_subdirectory(fuzzer-initialize)
@@ -65,6 +69,12 @@
     ExampleClangLoopProtoFuzzer.cpp
     )
 
+  # Build the lllvm protobuf fuzzer
+  add_clang_executable(clang-llvm-proto-fuzzer
+    ${DUMMY_MAIN}
+    ExampleClangLLVMProtoFuzzer.cpp
+    )
+
   set(COMMON_PROTO_FUZZ_LIBRARIES
     ${ProtobufMutator_LIBRARIES}
     ${PROTOBUF_LIBRARIES}
@@ -85,9 +95,21 @@
     clangCXXLoopProto
     clangLoopProtoToCXX
     )
+  target_link_libraries(clang-llvm-proto-fuzzer
+    PRIVATE
+    ${ProtobufMutator_LIBRARIES}
+    ${PROTOBUF_LIBRARIES}
+    ${LLVM_LIB_FUZZING_ENGINE}
+    clangFuzzerInitialize
+    clangHandleLLVM
+    clangCXXLoopProto
+    clangLoopProtoToLLVM
+    )
+
 endif()
 
 add_clang_subdirectory(handle-cxx)
+add_clang_subdirectory(handle-llvm)
 
 add_clang_executable(clang-fuzzer
   EXCLUDE_FROM_ALL
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to