parse_fuzzer.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 <cstddef>
  5. #include <cstring>
  6. #include "llvm/ADT/StringRef.h"
  7. #include "testing/fuzzing/libfuzzer.h"
  8. #include "toolchain/base/shared_value_stores.h"
  9. #include "toolchain/diagnostics/null_diagnostics.h"
  10. #include "toolchain/lex/lex.h"
  11. #include "toolchain/parse/parse.h"
  12. namespace Carbon::Testing {
  13. // NOLINTNEXTLINE: Match the documented fuzzer entry point declaration style.
  14. extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data,
  15. std::size_t size) {
  16. // Ignore large inputs.
  17. // TODO: See tokenized_buffer_fuzzer.cpp.
  18. if (size > 100000) {
  19. return 0;
  20. }
  21. static constexpr llvm::StringLiteral TestFileName = "test.carbon";
  22. llvm::vfs::InMemoryFileSystem fs;
  23. llvm::StringRef data_ref(reinterpret_cast<const char*>(data), size);
  24. CARBON_CHECK(fs.addFile(
  25. TestFileName, /*ModificationTime=*/0,
  26. llvm::MemoryBuffer::getMemBuffer(data_ref, /*BufferName=*/TestFileName,
  27. /*RequiresNullTerminator=*/false)));
  28. auto source =
  29. SourceBuffer::MakeFromFile(fs, TestFileName, NullDiagnosticConsumer());
  30. // Lex the input.
  31. SharedValueStores value_stores;
  32. auto tokens = Lex::Lex(value_stores, *source, NullDiagnosticConsumer());
  33. if (tokens.has_errors()) {
  34. return 0;
  35. }
  36. // Now parse it into a tree. Note that parsing will (when asserts are enabled)
  37. // walk the entire tree to verify it so we don't have to do that here.
  38. Parse::Parse(tokens, NullDiagnosticConsumer(), /*vlog_stream=*/nullptr);
  39. return 0;
  40. }
  41. } // namespace Carbon::Testing