summaryrefslogtreecommitdiffstats
path: root/clang/tools/clang-fuzzer/proto-to-cxx
diff options
context:
space:
mode:
Diffstat (limited to 'clang/tools/clang-fuzzer/proto-to-cxx')
-rw-r--r--clang/tools/clang-fuzzer/proto-to-cxx/CMakeLists.txt11
-rw-r--r--clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp148
-rw-r--r--clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx_main.cpp32
-rw-r--r--clang/tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.h4
4 files changed, 194 insertions, 1 deletions
diff --git a/clang/tools/clang-fuzzer/proto-to-cxx/CMakeLists.txt b/clang/tools/clang-fuzzer/proto-to-cxx/CMakeLists.txt
index 9337092fa4f..339959b81af 100644
--- a/clang/tools/clang-fuzzer/proto-to-cxx/CMakeLists.txt
+++ b/clang/tools/clang-fuzzer/proto-to-cxx/CMakeLists.txt
@@ -2,12 +2,21 @@ 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 proto_to_cxx.cpp proto_to_cxx_main.cpp)
+set(LLVM_OPTIONAL_SOURCES proto_to_cxx.cpp proto_to_cxx_main.cpp
+ loop_proto_to_cxx.cpp loop_proto_to_cxx_main.cpp)
add_clang_library(clangProtoToCXX proto_to_cxx.cpp
DEPENDS clangCXXProto
LINK_LIBS clangCXXProto ${PROTOBUF_LIBRARIES}
)
+add_clang_library(clangLoopProtoToCXX loop_proto_to_cxx.cpp
+ DEPENDS clangCXXLoopProto
+ LINK_LIBS clangCXXLoopProto ${PROTOBUF_LIBRARIES}
+ )
+
add_clang_executable(clang-proto-to-cxx proto_to_cxx_main.cpp)
+add_clang_executable(clang-loop-proto-to-cxx loop_proto_to_cxx_main.cpp)
+
target_link_libraries(clang-proto-to-cxx PRIVATE clangProtoToCXX)
+target_link_libraries(clang-loop-proto-to-cxx PRIVATE clangLoopProtoToCXX)
diff --git a/clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp b/clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
new file mode 100644
index 00000000000..a0007fb6f9e
--- /dev/null
+++ b/clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
@@ -0,0 +1,148 @@
+//==-- loop_proto_to_cxx.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 C++. Extends
+// proto_to_cxx.cpp by wrapping all the generated C++ code in a single for
+// loop. Also coutputs a different function signature that includes a
+// size_t parameter for the loop to use.
+//
+//===----------------------------------------------------------------------===//
+
+#include "cxx_loop_proto.pb.h"
+#include "proto_to_cxx.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::ostream &operator<<(std::ostream &os, const BinaryOp &x);
+std::ostream &operator<<(std::ostream &os, const StatementSeq &x);
+
+// Proto to C++.
+std::ostream &operator<<(std::ostream &os, const Const &x) {
+ return os << "(" << x.val() << ")";
+}
+std::ostream &operator<<(std::ostream &os, const VarRef &x) {
+ if (x.is_loop_var()) {
+ return os << "a[loop_ctr]";
+ } else {
+ return os << "a[" << static_cast<uint32_t>(x.varnum()) << " % s]";
+ }
+}
+std::ostream &operator<<(std::ostream &os, const Lvalue &x) {
+ return os << x.varref();
+}
+std::ostream &operator<<(std::ostream &os, const Rvalue &x) {
+ if (x.has_varref())
+ return os << x.varref();
+ if (x.has_cons())
+ return os << x.cons();
+ if (x.has_binop())
+ return os << x.binop();
+ return os << "1";
+}
+std::ostream &operator<<(std::ostream &os, const BinaryOp &x) {
+ os << "(" << x.left();
+ switch (x.op()) {
+ case BinaryOp::PLUS:
+ os << "+";
+ break;
+ case BinaryOp::MINUS:
+ os << "-";
+ break;
+ case BinaryOp::MUL:
+ os << "*";
+ break;
+ case BinaryOp::DIV:
+ os << "/";
+ break;
+ case BinaryOp::MOD:
+ os << "%";
+ break;
+ case BinaryOp::XOR:
+ os << "^";
+ break;
+ case BinaryOp::AND:
+ os << "&";
+ break;
+ case BinaryOp::OR:
+ os << "|";
+ break;
+ case BinaryOp::EQ:
+ os << "==";
+ break;
+ case BinaryOp::NE:
+ os << "!=";
+ break;
+ case BinaryOp::LE:
+ os << "<=";
+ break;
+ case BinaryOp::GE:
+ os << ">=";
+ break;
+ case BinaryOp::LT:
+ os << "<";
+ break;
+ case BinaryOp::GT:
+ os << ">";
+ break;
+ }
+ return os << x.right() << ")";
+}
+std::ostream &operator<<(std::ostream &os, const AssignmentStatement &x) {
+ return os << x.lvalue() << "=" << x.rvalue();
+}
+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 While &x) {
+ return os << "while (" << x.cond() << "){\n" << x.body() << "}\n";
+}
+std::ostream &operator<<(std::ostream &os, const Statement &x) {
+ if (x.has_assignment())
+ return os << x.assignment() << ";\n";
+ if (x.has_ifelse())
+ return os << x.ifelse();
+ if (x.has_while_loop())
+ return os << x.while_loop();
+ return os << "(void)0;\n";
+}
+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 << "void foo(int *a, size_t s) {\n"
+ << "for (int loop_ctr = 0; loop_ctr < s; loop_ctr++){\n"
+ << x.statements() << "}\n}\n";
+}
+
+// ---------------------------------
+
+std::string LoopFunctionToString(const LoopFunction &input) {
+ std::ostringstream os;
+ os << input;
+ return os.str();
+}
+std::string LoopProtoToCxx(const uint8_t *data, size_t size) {
+ LoopFunction message;
+ if (!message.ParsePartialFromArray(data, size))
+ return "#error invalid proto, may not be binary encoded\n";
+ return LoopFunctionToString(message);
+}
+
+} // namespace clang_fuzzer
diff --git a/clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx_main.cpp b/clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx_main.cpp
new file mode 100644
index 00000000000..e4b1414c63b
--- /dev/null
+++ b/clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx_main.cpp
@@ -0,0 +1,32 @@
+//==-- loop_proto_to_cxx_main.cpp - Driver for 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 a simple driver to print a C++ 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"
+
+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(
+ reinterpret_cast<const uint8_t *>(str.data()), str.size());
+ }
+}
diff --git a/clang/tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.h b/clang/tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.h
index 1985e91ba2c..8d2e2e6f008 100644
--- a/clang/tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.h
+++ b/clang/tools/clang-fuzzer/proto-to-cxx/proto_to_cxx.h
@@ -17,6 +17,10 @@
namespace clang_fuzzer {
class Function;
+class LoopFunction;
+
std::string FunctionToString(const Function &input);
std::string ProtoToCxx(const uint8_t *data, size_t size);
+std::string LoopFunctionToString(const LoopFunction &input);
+std::string LoopProtoToCxx(const uint8_t *data, size_t size);
}
OpenPOWER on IntegriCloud