check_fuzzer.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 "llvm/ADT/StringRef.h"
  5. #include "llvm/Support/MemoryBuffer.h"
  6. #include "llvm/Support/VirtualFileSystem.h"
  7. #include "testing/fuzzing/libfuzzer.h"
  8. #include "toolchain/driver/driver.h"
  9. namespace Carbon::Testing {
  10. // NOLINTNEXTLINE: Match the documented fuzzer entry point declaration style.
  11. extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data,
  12. std::size_t size) {
  13. // Ignore large inputs.
  14. // TODO: See tokenized_buffer_fuzzer.cpp.
  15. if (size > 100000) {
  16. return 0;
  17. }
  18. static constexpr llvm::StringLiteral TestFileName = "test.carbon";
  19. llvm::vfs::InMemoryFileSystem fs;
  20. llvm::StringRef data_ref(reinterpret_cast<const char*>(data), size);
  21. CARBON_CHECK(fs.addFile(
  22. TestFileName, /*ModificationTime=*/0,
  23. llvm::MemoryBuffer::getMemBuffer(data_ref, /*BufferName=*/TestFileName,
  24. /*RequiresNullTerminator=*/false)));
  25. // TODO: We should try to thread the executable path into here.
  26. const auto install_paths = InstallPaths::Make("");
  27. llvm::raw_null_ostream null_ostream;
  28. Driver driver(fs, &install_paths, null_ostream, null_ostream);
  29. // TODO: Get checking to a point where it can handle invalid parse trees
  30. // without crashing.
  31. if (!driver.RunCommand({"compile", "--phase=parse", TestFileName}).success) {
  32. return 0;
  33. }
  34. driver.RunCommand({"compile", "--phase=check", TestFileName});
  35. return 0;
  36. }
  37. } // namespace Carbon::Testing