check_fuzzer.cpp 1.5 KB

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