FuzzerCLI.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- FuzzerCLI.h - Common logic for CLIs of fuzzers ----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Common logic needed to implement LLVM's fuzz targets' CLIs - including LLVM
  15. // concepts like cl::opt and libFuzzer concepts like -ignore_remaining_args=1.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_FUZZMUTATE_FUZZERCLI_H
  19. #define LLVM_FUZZMUTATE_FUZZERCLI_H
  20. #include "llvm/Support/DataTypes.h"
  21. #include <stddef.h>
  22. namespace llvm {
  23. class StringRef;
  24. /// Parse cl::opts from a fuzz target commandline.
  25. ///
  26. /// This handles all arguments after -ignore_remaining_args=1 as cl::opts.
  27. void parseFuzzerCLOpts(int ArgC, char *ArgV[]);
  28. /// Handle backend options that are encoded in the executable name.
  29. ///
  30. /// Parses some common backend options out of a specially crafted executable
  31. /// name (argv[0]). For example, a name like llvm-foo-fuzzer--aarch64-gisel
  32. /// might set up an AArch64 triple and the Global ISel selector. This should be
  33. /// called *before* parseFuzzerCLOpts if calling both.
  34. ///
  35. /// This is meant to be used for environments like OSS-Fuzz that aren't capable
  36. /// of passing in command line arguments in the normal way.
  37. void handleExecNameEncodedBEOpts(StringRef ExecName);
  38. /// Handle optimizer options which are encoded in the executable name.
  39. /// Same semantics as in 'handleExecNameEncodedBEOpts'.
  40. void handleExecNameEncodedOptimizerOpts(StringRef ExecName);
  41. using FuzzerTestFun = int (*)(const uint8_t *Data, size_t Size);
  42. using FuzzerInitFun = int (*)(int *argc, char ***argv);
  43. /// Runs a fuzz target on the inputs specified on the command line.
  44. ///
  45. /// Useful for testing fuzz targets without linking to libFuzzer. Finds inputs
  46. /// in the argument list in a libFuzzer compatible way.
  47. int runFuzzerOnInputs(
  48. int ArgC, char *ArgV[], FuzzerTestFun TestOne,
  49. FuzzerInitFun Init = [](int *, char ***) { return 0; });
  50. } // namespace llvm
  51. #endif // LLVM_FUZZMUTATE_FUZZERCLI_H
  52. #ifdef __GNUC__
  53. #pragma GCC diagnostic pop
  54. #endif