tokenized_buffer_fuzzer.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 "common/check.h"
  6. #include "llvm/ADT/StringRef.h"
  7. #include "toolchain/base/value_store.h"
  8. #include "toolchain/diagnostics/null_diagnostics.h"
  9. #include "toolchain/lex/tokenized_buffer.h"
  10. namespace Carbon::Testing {
  11. // NOLINTNEXTLINE: Match the documented fuzzer entry point declaration style.
  12. extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data,
  13. std::size_t size) {
  14. // Ignore large inputs.
  15. // TODO: Investigate replacement with an error limit. Content with errors on
  16. // escaped quotes (`\"` repeated) have O(M * N) behavior for M errors in a
  17. // file length N, so either that will need to also be fixed or M will need to
  18. // shrink for large (1MB+) inputs.
  19. // This also affects parse/parse_fuzzer.cpp.
  20. if (size > 100000) {
  21. return 0;
  22. }
  23. static constexpr llvm::StringLiteral TestFileName = "test.carbon";
  24. llvm::vfs::InMemoryFileSystem fs;
  25. llvm::StringRef data_ref(reinterpret_cast<const char*>(data), size);
  26. CARBON_CHECK(fs.addFile(
  27. TestFileName, /*ModificationTime=*/0,
  28. llvm::MemoryBuffer::getMemBuffer(data_ref, /*BufferName=*/TestFileName,
  29. /*RequiresNullTerminator=*/false)));
  30. auto source =
  31. SourceBuffer::CreateFromFile(fs, TestFileName, NullDiagnosticConsumer());
  32. SharedValueStores value_stores;
  33. auto buffer = Lex::TokenizedBuffer::Lex(value_stores, *source,
  34. NullDiagnosticConsumer());
  35. if (buffer.has_errors()) {
  36. return 0;
  37. }
  38. // Walk the lexed and tokenized buffer to ensure it isn't corrupt in some way.
  39. //
  40. // TODO: We should enhance this to do more sanity checks on the resulting
  41. // token stream.
  42. for (Lex::Token token : buffer.tokens()) {
  43. int line_number = buffer.GetLineNumber(token);
  44. CARBON_CHECK(line_number > 0) << "Invalid line number!";
  45. CARBON_CHECK(line_number < INT_MAX) << "Invalid line number!";
  46. int column_number = buffer.GetColumnNumber(token);
  47. CARBON_CHECK(column_number > 0) << "Invalid line number!";
  48. CARBON_CHECK(column_number < INT_MAX) << "Invalid line number!";
  49. }
  50. return 0;
  51. }
  52. } // namespace Carbon::Testing