emmettneyman created this revision.
emmettneyman added reviewers: vitalybuka, kcc, morehouse.
Herald added subscribers: cfe-commits, mgorny.

Refactored LLVMFuzzerInitialize function into its own file.

  Copied and renamed files in preparation for new loop-proto-fuzzer.


Repository:
  rC Clang

https://reviews.llvm.org/D47665

Files:
  tools/clang-fuzzer/CMakeLists.txt
  tools/clang-fuzzer/ExampleClangProtoFuzzer.cpp
  tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.cpp
  tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.h
  tools/clang-fuzzer/proto-to-cxx/proto_to_cxx_main.cpp

Index: tools/clang-fuzzer/proto-to-cxx/proto_to_cxx_main.cpp
===================================================================
--- tools/clang-fuzzer/proto-to-cxx/proto_to_cxx_main.cpp
+++ tools/clang-fuzzer/proto-to-cxx/proto_to_cxx_main.cpp
@@ -23,8 +23,9 @@
     std::string str((std::istreambuf_iterator<char>(in)),
                     std::istreambuf_iterator<char>());
     std::cout << "// " << argv[i] << std::endl;
-    std::cout << clang_fuzzer::ProtoToCxx(
-        reinterpret_cast<const uint8_t *>(str.data()), str.size());
+    //std::cout << clang_fuzzer::ProtoToCxx(
+    //    reinterpret_cast<const uint8_t *>(str.data()), str.size());
+    std::cout << clang_fuzzer::ProtoStringToCxx(str);
   }
 }
 
Index: tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.h
===================================================================
--- tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.h
+++ tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.h
@@ -19,4 +19,5 @@
 class Function;
 std::string FunctionToString(const Function &input);
 std::string ProtoToCxx(const uint8_t *data, size_t size);
+std::string ProtoStringToCxx(const std::string& data);
 }
Index: tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.cpp
===================================================================
--- tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.cpp
+++ tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.cpp
@@ -14,6 +14,10 @@
 #include "proto_to_cxx.h"
 #include "cxx_proto.pb.h"
 
+// The following is needed to convert protos in human-readable form
+#include <google/protobuf/text_format.h>
+
+
 #include <ostream>
 #include <sstream>
 
@@ -95,7 +99,13 @@
 std::string ProtoToCxx(const uint8_t *data, size_t size) {
   Function message;
   if (!message.ParsePartialFromArray(data, size))
-    return "#error invalid proto\n";
+    return "#error invalid proto, may not be binary encoded\n";
+  return FunctionToString(message);
+}
+std::string ProtoStringToCxx(const std::string& data) {
+  Function message;
+  if (!google::protobuf::TextFormat::ParseFromString(data, &message))
+    return "#error invalid proto, may not be string encoded\n";
   return FunctionToString(message);
 }
 
Index: tools/clang-fuzzer/ExampleClangProtoFuzzer.cpp
===================================================================
--- tools/clang-fuzzer/ExampleClangProtoFuzzer.cpp
+++ tools/clang-fuzzer/ExampleClangProtoFuzzer.cpp
@@ -18,12 +18,15 @@
 #include "handle-cxx/handle_cxx.h"
 #include "proto-to-cxx/proto_to_cxx.h"
 
+#include "FuzzerInitialize.h"
+
 #include "src/libfuzzer/libfuzzer_macro.h"
 
 #include <cstring>
 
 using namespace clang_fuzzer;
 
+/*
 static std::vector<const char *> CLArgs;
 
 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
@@ -37,8 +40,9 @@
   }
   return 0;
 }
+*/
 
-DEFINE_BINARY_PROTO_FUZZER(const Function& input) {
+DEFINE_PROTO_FUZZER(const Function& input) {
   auto S = FunctionToString(input);
   HandleCXX(S, CLArgs);
 }
Index: tools/clang-fuzzer/CMakeLists.txt
===================================================================
--- tools/clang-fuzzer/CMakeLists.txt
+++ tools/clang-fuzzer/CMakeLists.txt
@@ -14,6 +14,7 @@
   ClangFuzzer.cpp
   DummyClangFuzzer.cpp
   ExampleClangProtoFuzzer.cpp
+  FuzzerInitialize.cpp
   )
 
 if(CLANG_ENABLE_PROTO_FUZZER)
@@ -44,6 +45,7 @@
   add_clang_executable(clang-proto-fuzzer
     ${DUMMY_MAIN}
     ExampleClangProtoFuzzer.cpp
+    FuzzerInitialize.cpp
     )
 
   target_link_libraries(clang-proto-fuzzer
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D47665: Refactored c... Emmett Neyman via Phabricator via cfe-commits

Reply via email to