driver_fuzzer.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include <cstdint>
  5. #include <cstring>
  6. #include <numeric>
  7. #include <string>
  8. #include "llvm/ADT/SmallVector.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/Support/raw_ostream.h"
  11. #include "testing/util/test_raw_ostream.h"
  12. #include "toolchain/driver/driver.h"
  13. namespace Carbon::Testing {
  14. static auto Read(const unsigned char*& data, size_t& size, int& output)
  15. -> bool {
  16. if (size < sizeof(output)) {
  17. return false;
  18. }
  19. std::memcpy(&output, data, sizeof(output));
  20. size -= sizeof(output);
  21. data += sizeof(output);
  22. return true;
  23. }
  24. extern "C" auto LLVMFuzzerTestOneInput(const unsigned char* data, size_t size)
  25. -> int {
  26. // First use the data to compute the number of arguments. Note that for
  27. // scaling reasons we don't allow 2^31 arguments, even empty ones. Simply
  28. // creating the vector of those won't work. We limit this to 2^20 arguments
  29. // total.
  30. int num_args;
  31. if (!Read(data, size, num_args) || num_args < 0 || num_args > (1 << 20)) {
  32. return 0;
  33. }
  34. // Now use the data to compute the length of each argument. We don't want to
  35. // exhaust all memory, so bound the search space to using 2^17 bytes of
  36. // memory for the argument text itself.
  37. size_t arg_length_sum = 0;
  38. llvm::SmallVector<int, 16> arg_lengths(num_args);
  39. for (int& arg_length : arg_lengths) {
  40. if (!Read(data, size, arg_length) || arg_length < 0) {
  41. return 0;
  42. }
  43. arg_length_sum += arg_length;
  44. if (arg_length_sum > (1 << 17)) {
  45. return 0;
  46. }
  47. }
  48. // Ensure we have enough data for all the arguments.
  49. if (size < arg_length_sum) {
  50. return 0;
  51. }
  52. // Lastly, read the contents of each argument out of the data.
  53. llvm::SmallVector<llvm::StringRef, 16> args;
  54. args.reserve(num_args);
  55. for (int arg_length : arg_lengths) {
  56. args.push_back(
  57. llvm::StringRef(reinterpret_cast<const char*>(data), arg_length));
  58. data += arg_length;
  59. size -= arg_length;
  60. }
  61. llvm::vfs::InMemoryFileSystem fs;
  62. TestRawOstream error_stream;
  63. llvm::raw_null_ostream dest;
  64. Driver d(fs, dest, error_stream);
  65. if (!d.RunFullCommand(args)) {
  66. if (error_stream.TakeStr().find("ERROR:") == std::string::npos) {
  67. llvm::errs() << "No error message on a failure!\n";
  68. return 1;
  69. }
  70. }
  71. return 0;
  72. }
  73. } // namespace Carbon::Testing