UserSuppliedFuzzerTest.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Simple test for a fuzzer.
  2. // The fuzzer must find the string "Hi!" preceded by a magic value.
  3. // Uses UserSuppliedFuzzer which ensures that the magic is present.
  4. #include <cstdint>
  5. #include <cassert>
  6. #include <cstdlib>
  7. #include <cstddef>
  8. #include <cstring>
  9. #include <iostream>
  10. #include "FuzzerInterface.h"
  11. static const uint64_t kMagic = 8860221463604ULL;
  12. class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
  13. public:
  14. void TargetFunction(const uint8_t *Data, size_t Size) {
  15. if (Size <= 10) return;
  16. if (memcmp(Data, &kMagic, sizeof(kMagic))) return;
  17. // It's hard to get here w/o advanced fuzzing techniques (e.g. cmp tracing).
  18. // So, we simply 'fix' the data in the custom mutator.
  19. if (Data[8] == 'H') {
  20. if (Data[9] == 'i') {
  21. if (Data[10] == '!') {
  22. std::cout << "BINGO; Found the target, exiting\n";
  23. exit(1);
  24. }
  25. }
  26. }
  27. }
  28. // Custom mutator.
  29. virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
  30. assert(MaxSize > sizeof(kMagic));
  31. if (Size < sizeof(kMagic))
  32. Size = sizeof(kMagic);
  33. // "Fix" the data, then mutate.
  34. memcpy(Data, &kMagic, std::min(MaxSize, sizeof(kMagic)));
  35. return BasicMutate(Data + sizeof(kMagic), Size - sizeof(kMagic),
  36. MaxSize - sizeof(kMagic));
  37. }
  38. // No need to redefine CrossOver() here.
  39. };
  40. int main(int argc, char **argv) {
  41. MyFuzzer F;
  42. fuzzer::FuzzerDriver(argc, argv, F);
  43. }