driver_fuzzer.cpp 2.6 KB

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