standalone_fuzz_target_runner.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // This runner does not do any fuzzing, but allows us to run the fuzz target
  4. // on the test corpus or on a single file,
  5. // e.g. the one that comes from a bug report.
  6. #include <cassert>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <vector>
  10. // Forward declare the "fuzz target" interface.
  11. // We deliberately keep this interface simple and header-free.
  12. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
  13. // It reads all files passed as parameters and feeds their contents
  14. // one by one into the fuzz target (LLVMFuzzerTestOneInput).
  15. int main(int argc, char **argv) {
  16. for (int i = 1; i < argc; i++) {
  17. std::ifstream in(argv[i]);
  18. in.seekg(0, in.end);
  19. size_t length = static_cast<size_t>(in.tellg());
  20. in.seekg (0, in.beg);
  21. std::cout << "Reading " << length << " bytes from " << argv[i] << std::endl;
  22. // Allocate exactly length bytes so that we reliably catch buffer overflows.
  23. std::vector<char> bytes(length);
  24. in.read(bytes.data(), static_cast<std::streamsize>(bytes.size()));
  25. LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t *>(bytes.data()),
  26. bytes.size());
  27. std::cout << "Execution successful" << std::endl;
  28. }
  29. std::cout << "Execution finished" << std::endl;
  30. return 0;
  31. }