FuzzerCLI.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //===-- FuzzerCLI.h - Common logic for CLIs of fuzzers ----------*- C++ -*-===//
  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. // Common logic needed to implement LLVM's fuzz targets' CLIs - including LLVM
  10. // concepts like cl::opt and libFuzzer concepts like -ignore_remaining_args=1.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_FUZZMUTATE_FUZZERCLI_H
  14. #define LLVM_FUZZMUTATE_FUZZERCLI_H
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/Support/DataTypes.h"
  17. namespace llvm {
  18. class StringRef;
  19. /// Parse cl::opts from a fuzz target commandline.
  20. ///
  21. /// This handles all arguments after -ignore_remaining_args=1 as cl::opts.
  22. void parseFuzzerCLOpts(int ArgC, char *ArgV[]);
  23. /// Handle backend options that are encoded in the executable name.
  24. ///
  25. /// Parses some common backend options out of a specially crafted executable
  26. /// name (argv[0]). For example, a name like llvm-foo-fuzzer--aarch64-gisel
  27. /// might set up an AArch64 triple and the Global ISel selector. This should be
  28. /// called *before* parseFuzzerCLOpts if calling both.
  29. ///
  30. /// This is meant to be used for environments like OSS-Fuzz that aren't capable
  31. /// of passing in command line arguments in the normal way.
  32. void handleExecNameEncodedBEOpts(StringRef ExecName);
  33. /// Handle optimizer options which are encoded in the executable name.
  34. /// Same semantics as in 'handleExecNameEncodedBEOpts'.
  35. void handleExecNameEncodedOptimizerOpts(StringRef ExecName);
  36. using FuzzerTestFun = int (*)(const uint8_t *Data, size_t Size);
  37. using FuzzerInitFun = int (*)(int *argc, char ***argv);
  38. /// Runs a fuzz target on the inputs specified on the command line.
  39. ///
  40. /// Useful for testing fuzz targets without linking to libFuzzer. Finds inputs
  41. /// in the argument list in a libFuzzer compatible way.
  42. int runFuzzerOnInputs(int ArgC, char *ArgV[], FuzzerTestFun TestOne,
  43. FuzzerInitFun Init = [](int *, char ***) { return 0; });
  44. /// Fuzzer friendly interface for the llvm bitcode parser.
  45. ///
  46. /// \param Data Bitcode we are going to parse
  47. /// \param Size Size of the 'Data' in bytes
  48. /// \return New module or nullptr in case of error
  49. std::unique_ptr<Module> parseModule(const uint8_t *Data, size_t Size,
  50. LLVMContext &Context);
  51. /// Fuzzer friendly interface for the llvm bitcode printer.
  52. ///
  53. /// \param M Module to print
  54. /// \param Dest Location to store serialized module
  55. /// \param MaxSize Size of the destination buffer
  56. /// \return Number of bytes that were written. When module size exceeds MaxSize
  57. /// returns 0 and leaves Dest unchanged.
  58. size_t writeModule(const Module &M, uint8_t *Dest, size_t MaxSize);
  59. /// Try to parse module and verify it. May output verification errors to the
  60. /// errs().
  61. /// \return New module or nullptr in case of error.
  62. std::unique_ptr<Module> parseAndVerify(const uint8_t *Data, size_t Size,
  63. LLVMContext &Context);
  64. } // end llvm namespace
  65. #endif // LLVM_FUZZMUTATE_FUZZERCLI_H