tokenized_buffer_fuzzer.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/lex.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::Lex(value_stores, *source, NullDiagnosticConsumer());
  34. if (buffer.has_errors()) {
  35. return 0;
  36. }
  37. // Walk the lexed and tokenized buffer to ensure it isn't corrupt in some way.
  38. //
  39. // TODO: We should enhance this to do more sanity checks on the resulting
  40. // token stream.
  41. for (Lex::Token token : buffer.tokens()) {
  42. int line_number = buffer.GetLineNumber(token);
  43. CARBON_CHECK(line_number > 0) << "Invalid line number!";
  44. CARBON_CHECK(line_number < INT_MAX) << "Invalid line number!";
  45. int column_number = buffer.GetColumnNumber(token);
  46. CARBON_CHECK(column_number > 0) << "Invalid line number!";
  47. CARBON_CHECK(column_number < INT_MAX) << "Invalid line number!";
  48. }
  49. return 0;
  50. }
  51. } // namespace Carbon::Testing