tokenized_buffer_fuzzer.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 "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. namespace Carbon::Testing {
  12. // NOLINTNEXTLINE: Match the documented fuzzer entry point declaration style.
  13. extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data, 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::MakeFromFile(fs, TestFileName, Diagnostics::NullConsumer());
  32. SharedValueStores value_stores;
  33. Lex::LexOptions options;
  34. options.consumer = &Diagnostics::NullConsumer();
  35. auto buffer = Lex::Lex(value_stores, *source, options);
  36. if (buffer.has_errors()) {
  37. return 0;
  38. }
  39. // Walk the lexed and tokenized buffer to ensure it isn't corrupt in some way.
  40. //
  41. // TODO: We should enhance this to do more sanity checks on the resulting
  42. // token stream.
  43. for (Lex::TokenIndex token : buffer.tokens()) {
  44. int line_number = buffer.GetLineNumber(token);
  45. CARBON_CHECK(line_number > 0, "Invalid line number!");
  46. CARBON_CHECK(line_number < INT_MAX, "Invalid line number!");
  47. int column_number = buffer.GetColumnNumber(token);
  48. CARBON_CHECK(column_number > 0, "Invalid line number!");
  49. CARBON_CHECK(column_number < INT_MAX, "Invalid line number!");
  50. }
  51. return 0;
  52. }
  53. } // namespace Carbon::Testing