FuzzerCLI.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //===-- FuzzerCLI.cpp -----------------------------------------------------===//
  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. #include "llvm/FuzzMutate/FuzzerCLI.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/ADT/Triple.h"
  11. #include "llvm/Bitcode/BitcodeReader.h"
  12. #include "llvm/Bitcode/BitcodeWriter.h"
  13. #include "llvm/IR/LLVMContext.h"
  14. #include "llvm/Support/CommandLine.h"
  15. #include "llvm/Support/Compiler.h"
  16. #include "llvm/Support/Error.h"
  17. #include "llvm/Support/MemoryBuffer.h"
  18. #include "llvm/Support/SourceMgr.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include "llvm/IR/Verifier.h"
  21. using namespace llvm;
  22. void llvm::parseFuzzerCLOpts(int ArgC, char *ArgV[]) {
  23. std::vector<const char *> CLArgs;
  24. CLArgs.push_back(ArgV[0]);
  25. int I = 1;
  26. while (I < ArgC)
  27. if (StringRef(ArgV[I++]).equals("-ignore_remaining_args=1"))
  28. break;
  29. while (I < ArgC)
  30. CLArgs.push_back(ArgV[I++]);
  31. cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data());
  32. }
  33. void llvm::handleExecNameEncodedBEOpts(StringRef ExecName) {
  34. std::vector<std::string> Args{std::string(ExecName)};
  35. auto NameAndArgs = ExecName.split("--");
  36. if (NameAndArgs.second.empty())
  37. return;
  38. SmallVector<StringRef, 4> Opts;
  39. NameAndArgs.second.split(Opts, '-');
  40. for (StringRef Opt : Opts) {
  41. if (Opt.equals("gisel")) {
  42. Args.push_back("-global-isel");
  43. // For now we default GlobalISel to -O0
  44. Args.push_back("-O0");
  45. } else if (Opt.startswith("O")) {
  46. Args.push_back("-" + Opt.str());
  47. } else if (Triple(Opt).getArch()) {
  48. Args.push_back("-mtriple=" + Opt.str());
  49. } else {
  50. errs() << ExecName << ": Unknown option: " << Opt << ".\n";
  51. exit(1);
  52. }
  53. }
  54. errs() << NameAndArgs.first << ": Injected args:";
  55. for (int I = 1, E = Args.size(); I < E; ++I)
  56. errs() << " " << Args[I];
  57. errs() << "\n";
  58. std::vector<const char *> CLArgs;
  59. CLArgs.reserve(Args.size());
  60. for (std::string &S : Args)
  61. CLArgs.push_back(S.c_str());
  62. cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data());
  63. }
  64. void llvm::handleExecNameEncodedOptimizerOpts(StringRef ExecName) {
  65. // TODO: Refactor parts common with the 'handleExecNameEncodedBEOpts'
  66. std::vector<std::string> Args{std::string(ExecName)};
  67. auto NameAndArgs = ExecName.split("--");
  68. if (NameAndArgs.second.empty())
  69. return;
  70. SmallVector<StringRef, 4> Opts;
  71. NameAndArgs.second.split(Opts, '-');
  72. for (StringRef Opt : Opts) {
  73. if (Opt == "instcombine") {
  74. Args.push_back("-passes=instcombine");
  75. } else if (Opt == "earlycse") {
  76. Args.push_back("-passes=early-cse");
  77. } else if (Opt == "simplifycfg") {
  78. Args.push_back("-passes=simplifycfg");
  79. } else if (Opt == "gvn") {
  80. Args.push_back("-passes=gvn");
  81. } else if (Opt == "sccp") {
  82. Args.push_back("-passes=sccp");
  83. } else if (Opt == "loop_predication") {
  84. Args.push_back("-passes=loop-predication");
  85. } else if (Opt == "guard_widening") {
  86. Args.push_back("-passes=guard-widening");
  87. } else if (Opt == "loop_rotate") {
  88. Args.push_back("-passes=loop(rotate)");
  89. } else if (Opt == "loop_unswitch") {
  90. Args.push_back("-passes=loop(simple-loop-unswitch)");
  91. } else if (Opt == "loop_unroll") {
  92. Args.push_back("-passes=unroll");
  93. } else if (Opt == "loop_vectorize") {
  94. Args.push_back("-passes=loop-vectorize");
  95. } else if (Opt == "licm") {
  96. Args.push_back("-passes=licm");
  97. } else if (Opt == "indvars") {
  98. Args.push_back("-passes=indvars");
  99. } else if (Opt == "strength_reduce") {
  100. Args.push_back("-passes=loop-reduce");
  101. } else if (Opt == "irce") {
  102. Args.push_back("-passes=irce");
  103. } else if (Triple(Opt).getArch()) {
  104. Args.push_back("-mtriple=" + Opt.str());
  105. } else {
  106. errs() << ExecName << ": Unknown option: " << Opt << ".\n";
  107. exit(1);
  108. }
  109. }
  110. errs() << NameAndArgs.first << ": Injected args:";
  111. for (int I = 1, E = Args.size(); I < E; ++I)
  112. errs() << " " << Args[I];
  113. errs() << "\n";
  114. std::vector<const char *> CLArgs;
  115. CLArgs.reserve(Args.size());
  116. for (std::string &S : Args)
  117. CLArgs.push_back(S.c_str());
  118. cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data());
  119. }
  120. int llvm::runFuzzerOnInputs(int ArgC, char *ArgV[], FuzzerTestFun TestOne,
  121. FuzzerInitFun Init) {
  122. errs() << "*** This tool was not linked to libFuzzer.\n"
  123. << "*** No fuzzing will be performed.\n";
  124. if (int RC = Init(&ArgC, &ArgV)) {
  125. errs() << "Initialization failed\n";
  126. return RC;
  127. }
  128. for (int I = 1; I < ArgC; ++I) {
  129. StringRef Arg(ArgV[I]);
  130. if (Arg.startswith("-")) {
  131. if (Arg.equals("-ignore_remaining_args=1"))
  132. break;
  133. continue;
  134. }
  135. auto BufOrErr = MemoryBuffer::getFile(Arg, /*IsText=*/false,
  136. /*RequiresNullTerminator=*/false);
  137. if (std::error_code EC = BufOrErr.getError()) {
  138. errs() << "Error reading file: " << Arg << ": " << EC.message() << "\n";
  139. return 1;
  140. }
  141. std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
  142. errs() << "Running: " << Arg << " (" << Buf->getBufferSize() << " bytes)\n";
  143. TestOne(reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
  144. Buf->getBufferSize());
  145. }
  146. return 0;
  147. }
  148. std::unique_ptr<Module> llvm::parseModule(
  149. const uint8_t *Data, size_t Size, LLVMContext &Context) {
  150. if (Size <= 1)
  151. // We get bogus data given an empty corpus - just create a new module.
  152. return std::make_unique<Module>("M", Context);
  153. auto Buffer = MemoryBuffer::getMemBuffer(
  154. StringRef(reinterpret_cast<const char *>(Data), Size), "Fuzzer input",
  155. /*RequiresNullTerminator=*/false);
  156. SMDiagnostic Err;
  157. auto M = parseBitcodeFile(Buffer->getMemBufferRef(), Context);
  158. if (Error E = M.takeError()) {
  159. errs() << toString(std::move(E)) << "\n";
  160. return nullptr;
  161. }
  162. return std::move(M.get());
  163. }
  164. size_t llvm::writeModule(const Module &M, uint8_t *Dest, size_t MaxSize) {
  165. std::string Buf;
  166. {
  167. raw_string_ostream OS(Buf);
  168. WriteBitcodeToFile(M, OS);
  169. }
  170. if (Buf.size() > MaxSize)
  171. return 0;
  172. memcpy(Dest, Buf.data(), Buf.size());
  173. return Buf.size();
  174. }
  175. std::unique_ptr<Module> llvm::parseAndVerify(const uint8_t *Data, size_t Size,
  176. LLVMContext &Context) {
  177. auto M = parseModule(Data, Size, Context);
  178. if (!M || verifyModule(*M, &errs()))
  179. return nullptr;
  180. return M;
  181. }