1 //==-- proto_to_cxx.cpp - Protobuf-C++ conversion --------------------------==// 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 // Implements functions for converting between protobufs and C++. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "proto_to_cxx.h" 15 #include "cxx_proto.pb.h" 16 17 #include <ostream> 18 #include <sstream> 19 20 namespace clang_fuzzer { 21 22 // Forward decls. 23 std::ostream &operator<<(std::ostream &os, const BinaryOp &x); 24 std::ostream &operator<<(std::ostream &os, const StatementSeq &x); 25 26 // Proto to C++. 27 std::ostream &operator<<(std::ostream &os, const Const &x) { 28 return os << "(" << x.val() << ")"; 29 } 30 std::ostream &operator<<(std::ostream &os, const VarRef &x) { 31 return os << "a[" << (static_cast<uint32_t>(x.varnum()) % 100) << "]"; 32 } 33 std::ostream &operator<<(std::ostream &os, const Lvalue &x) { 34 return os << x.varref(); 35 } 36 std::ostream &operator<<(std::ostream &os, const Rvalue &x) { 37 if (x.has_varref()) return os << x.varref(); 38 if (x.has_cons()) return os << x.cons(); 39 if (x.has_binop()) return os << x.binop(); 40 return os << "1"; 41 } 42 std::ostream &operator<<(std::ostream &os, const BinaryOp &x) { 43 os << "(" << x.left(); 44 switch (x.op()) { 45 case BinaryOp::PLUS: os << "+"; break; 46 case BinaryOp::MINUS: os << "-"; break; 47 case BinaryOp::MUL: os << "*"; break; 48 case BinaryOp::DIV: os << "/"; break; 49 case BinaryOp::MOD: os << "%"; break; 50 case BinaryOp::XOR: os << "^"; break; 51 case BinaryOp::AND: os << "&"; break; 52 case BinaryOp::OR: os << "|"; break; 53 case BinaryOp::EQ: os << "=="; break; 54 case BinaryOp::NE: os << "!="; break; 55 case BinaryOp::LE: os << "<="; break; 56 case BinaryOp::GE: os << ">="; break; 57 case BinaryOp::LT: os << "<"; break; 58 case BinaryOp::GT: os << ">"; break; 59 } 60 return os << x.right() << ")"; 61 } 62 std::ostream &operator<<(std::ostream &os, const AssignmentStatement &x) { 63 return os << x.lvalue() << "=" << x.rvalue() << ";\n"; 64 } 65 std::ostream &operator<<(std::ostream &os, const IfElse &x) { 66 return os << "if (" << x.cond() << "){\n" 67 << x.if_body() << "} else { \n" 68 << x.else_body() << "}\n"; 69 } 70 std::ostream &operator<<(std::ostream &os, const While &x) { 71 return os << "while (" << x.cond() << "){\n" << x.body() << "}\n"; 72 } 73 std::ostream &operator<<(std::ostream &os, const Statement &x) { 74 if (x.has_assignment()) return os << x.assignment(); 75 if (x.has_ifelse()) return os << x.ifelse(); 76 if (x.has_while_loop()) return os << x.while_loop(); 77 return os << "(void)0;\n"; 78 } 79 std::ostream &operator<<(std::ostream &os, const StatementSeq &x) { 80 for (auto &st : x.statements()) os << st; 81 return os; 82 } 83 std::ostream &operator<<(std::ostream &os, const Function &x) { 84 return os << "void foo(int *a) {\n" << x.statements() << "}\n"; 85 } 86 87 // --------------------------------- 88 89 std::string FunctionToString(const Function &input) { 90 std::ostringstream os; 91 os << input; 92 return os.str(); 93 94 } 95 std::string ProtoToCxx(const uint8_t *data, size_t size) { 96 Function message; 97 if (!message.ParsePartialFromArray(data, size)) 98 return "#error invalid proto\n"; 99 return FunctionToString(message); 100 } 101 102 } // namespace clang_fuzzer 103