1 //===-- fuzzer_initialize.cpp - Fuzz Clang --------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements two functions: one that returns the command line
11 /// arguments for a given call to the fuzz target and one that initializes
12 /// the fuzzer with the correct command line arguments.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "fuzzer_initialize.h"
17 
18 #include "llvm/InitializePasses.h"
19 #include "llvm/PassRegistry.h"
20 #include "llvm/Support/TargetSelect.h"
21 #include <cstring>
22 
23 using namespace clang_fuzzer;
24 using namespace llvm;
25 
26 
27 namespace clang_fuzzer {
28 
29 static std::vector<const char *> CLArgs;
30 
GetCLArgs()31 const std::vector<const char *>& GetCLArgs() {
32   return CLArgs;
33 }
34 
35 }
36 
LLVMFuzzerInitialize(int * argc,char *** argv)37 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
38   InitializeAllTargets();
39   InitializeAllTargetMCs();
40   InitializeAllAsmPrinters();
41   InitializeAllAsmParsers();
42 
43   PassRegistry &Registry = *PassRegistry::getPassRegistry();
44   initializeCore(Registry);
45   initializeScalarOpts(Registry);
46   initializeVectorization(Registry);
47   initializeIPO(Registry);
48   initializeAnalysis(Registry);
49   initializeTransformUtils(Registry);
50   initializeInstCombine(Registry);
51   initializeAggressiveInstCombine(Registry);
52   initializeInstrumentation(Registry);
53   initializeTarget(Registry);
54 
55   CLArgs.push_back("-O2");
56   for (int I = 1; I < *argc; I++) {
57     if (strcmp((*argv)[I], "-ignore_remaining_args=1") == 0) {
58       for (I++; I < *argc; I++)
59         CLArgs.push_back((*argv)[I]);
60       break;
61     }
62   }
63   return 0;
64 }
65