1 //===-- ExampleClangProtoFuzzer.cpp - Fuzz Clang --------------------------===//
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 /// \file
11 /// \brief This file implements a function that runs Clang on a single
12 ///  input and uses libprotobuf-mutator to find new inputs. This function is
13 ///  then linked into the Fuzzer library.
14 ///
15 //===----------------------------------------------------------------------===//
16 
17 #include "cxx_proto.pb.h"
18 #include "handle-cxx/handle_cxx.h"
19 #include "proto-to-cxx/proto_to_cxx.h"
20 
21 #include "src/libfuzzer/libfuzzer_macro.h"
22 
23 #include <cstring>
24 
25 using namespace clang_fuzzer;
26 
27 static std::vector<const char *> CLArgs;
28 
29 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
30   CLArgs.push_back("-O2");
31   for (int I = 1; I < *argc; I++) {
32     if (strcmp((*argv)[I], "-ignore_remaining_args=1") == 0) {
33       for (I++; I < *argc; I++)
34         CLArgs.push_back((*argv)[I]);
35       break;
36     }
37   }
38   return 0;
39 }
40 
41 DEFINE_BINARY_PROTO_FUZZER(const Function& input) {
42   auto S = FunctionToString(input);
43   HandleCXX(S, CLArgs);
44 }
45