summaryrefslogtreecommitdiffhomepage
path: root/oss-fuzz/mruby_proto_fuzzer.cpp
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2019-05-17 21:30:20 +0900
committerGitHub <[email protected]>2019-05-17 21:30:20 +0900
commit4572d42d07c70409f1ded293ccb4b03862b2bdf4 (patch)
tree461072458cc39eec63d7abdbf9a6573e7c28bd7b /oss-fuzz/mruby_proto_fuzzer.cpp
parenteecac4f18bbd06907d0bed1ec572f69be547e33f (diff)
parent1f3ece9631d3b52911ff7b5fff88fa8fccbbc3f9 (diff)
downloadmruby-4572d42d07c70409f1ded293ccb4b03862b2bdf4.tar.gz
mruby-4572d42d07c70409f1ded293ccb4b03862b2bdf4.zip
Merge pull request #4444 from bshastry/mruby-proto
proto fuzzer: Add source files necessary to compile proto fuzzer
Diffstat (limited to 'oss-fuzz/mruby_proto_fuzzer.cpp')
-rw-r--r--oss-fuzz/mruby_proto_fuzzer.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/oss-fuzz/mruby_proto_fuzzer.cpp b/oss-fuzz/mruby_proto_fuzzer.cpp
new file mode 100644
index 000000000..2999c5470
--- /dev/null
+++ b/oss-fuzz/mruby_proto_fuzzer.cpp
@@ -0,0 +1,44 @@
+#include <string>
+#include <iostream>
+#include <fstream>
+
+#include <mruby.h>
+#include <mruby/compile.h>
+
+#include "libprotobuf-mutator/src/libfuzzer/libfuzzer_macro.h"
+#include "ruby.pb.h"
+#include "proto_to_ruby.h"
+
+using namespace ruby_fuzzer;
+using namespace std;
+
+int FuzzRB(const uint8_t *Data, size_t size) {
+ mrb_value v;
+ mrb_state *mrb = mrb_open();
+ if (!mrb)
+ return 0;
+
+ char *code = (char *)malloc(size+1);
+ if (!code)
+ return 0;
+ memcpy(code, Data, size);
+ code[size] = '\0';
+
+ if (const char *dump_path = getenv("PROTO_FUZZER_DUMP_PATH")) {
+ // With libFuzzer binary run this to generate an RB file x.rb:
+ // PROTO_FUZZER_DUMP_PATH=x.rb ./a.out proto-input
+ std::ofstream of(dump_path);
+ of.write(code, size);
+ }
+ v = mrb_load_string(mrb, code);
+ mrb_close(mrb);
+
+ free(code);
+ return 0;
+}
+
+DEFINE_PROTO_FUZZER(const Function &function) {
+ protoConverter converter;
+ auto s = converter.FunctionToString(function);
+ (void)FuzzRB((const uint8_t*)s.data(), s.size());
+}