2
0

FuzzerInterface.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //===- FuzzerInterface.h - Interface header for the Fuzzer ------*- C++ -* ===//
  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. // Define the interface between the Fuzzer and the library being tested.
  10. //===----------------------------------------------------------------------===//
  11. // WARNING: keep the interface free of STL or any other header-based C++ lib,
  12. // to avoid bad interactions between the code used in the fuzzer and
  13. // the code used in the target function.
  14. #ifndef LLVM_FUZZER_INTERFACE_H
  15. #define LLVM_FUZZER_INTERFACE_H
  16. #include <cstddef>
  17. #include <cstdint>
  18. namespace fuzzer {
  19. typedef void (*UserCallback)(const uint8_t *Data, size_t Size);
  20. /** Simple C-like interface with a single user-supplied callback.
  21. Usage:
  22. #\code
  23. #include "FuzzerInterface.h"
  24. void LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  25. DoStuffWithData(Data, Size);
  26. }
  27. // Implement your own main() or use the one from FuzzerMain.cpp.
  28. int main(int argc, char **argv) {
  29. InitializeMeIfNeeded();
  30. return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput);
  31. }
  32. #\endcode
  33. */
  34. int FuzzerDriver(int argc, char **argv, UserCallback Callback);
  35. /** An abstract class that allows to use user-supplied mutators with libFuzzer.
  36. Usage:
  37. #\code
  38. #include "FuzzerInterface.h"
  39. class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
  40. public:
  41. // Must define the target function.
  42. void TargetFunction(...) { ... }
  43. // Optionally define the mutator.
  44. size_t Mutate(...) { ... }
  45. // Optionally define the CrossOver method.
  46. size_t CrossOver(...) { ... }
  47. };
  48. int main(int argc, char **argv) {
  49. MyFuzzer F;
  50. fuzzer::FuzzerDriver(argc, argv, F);
  51. }
  52. #\endcode
  53. */
  54. class UserSuppliedFuzzer {
  55. public:
  56. /// Executes the target function on 'Size' bytes of 'Data'.
  57. virtual void TargetFunction(const uint8_t *Data, size_t Size) = 0;
  58. /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
  59. /// returns the new size of the data, which should be positive.
  60. virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
  61. return BasicMutate(Data, Size, MaxSize);
  62. }
  63. /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
  64. /// returns the number of bytes written, which should be positive.
  65. virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
  66. const uint8_t *Data2, size_t Size2,
  67. uint8_t *Out, size_t MaxOutSize) {
  68. return BasicCrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize);
  69. }
  70. virtual ~UserSuppliedFuzzer() {}
  71. protected:
  72. /// These can be called internally by Mutate and CrossOver.
  73. size_t BasicMutate(uint8_t *Data, size_t Size, size_t MaxSize);
  74. size_t BasicCrossOver(const uint8_t *Data1, size_t Size1,
  75. const uint8_t *Data2, size_t Size2,
  76. uint8_t *Out, size_t MaxOutSize);
  77. };
  78. /// Runs the fuzzing with the UserSuppliedFuzzer.
  79. int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
  80. } // namespace fuzzer
  81. #endif // LLVM_FUZZER_INTERFACE_H