parse_fuzzer.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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, size_t size) {
  15. // Ignore large inputs.
  16. // TODO: See tokenized_buffer_fuzzer.cpp.
  17. if (size > 100000) {
  18. return 0;
  19. }
  20. static constexpr llvm::StringLiteral TestFileName = "test.carbon";
  21. llvm::vfs::InMemoryFileSystem fs;
  22. llvm::StringRef data_ref(reinterpret_cast<const char*>(data), size);
  23. CARBON_CHECK(fs.addFile(
  24. TestFileName, /*ModificationTime=*/0,
  25. llvm::MemoryBuffer::getMemBuffer(data_ref, /*BufferName=*/TestFileName,
  26. /*RequiresNullTerminator=*/false)));
  27. auto source =
  28. SourceBuffer::MakeFromFile(fs, TestFileName, Diagnostics::NullConsumer());
  29. // Lex the input.
  30. SharedValueStores value_stores;
  31. auto tokens = Lex::Lex(value_stores, *source, Diagnostics::NullConsumer());
  32. if (tokens.has_errors()) {
  33. return 0;
  34. }
  35. // Now parse it into a tree. Note that parsing will (when asserts are enabled)
  36. // walk the entire tree to verify it so we don't have to do that here.
  37. Parse::Parse(tokens, Diagnostics::NullConsumer(), /*vlog_stream=*/nullptr);
  38. return 0;
  39. }
  40. } // namespace Carbon::Testing