tokenized_buffer_fuzzer.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 <cstdint>
  5. #include <cstring>
  6. #include "common/check.h"
  7. #include "llvm/ADT/StringRef.h"
  8. #include "toolchain/diagnostics/diagnostic_emitter.h"
  9. #include "toolchain/diagnostics/null_diagnostics.h"
  10. #include "toolchain/lexer/tokenized_buffer.h"
  11. namespace Carbon::Testing {
  12. // NOLINTNEXTLINE: Match the documented fuzzer entry point declaration style.
  13. extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data,
  14. std::size_t size) {
  15. auto source = SourceBuffer::CreateFromText(
  16. llvm::StringRef(reinterpret_cast<const char*>(data), size));
  17. auto buffer = TokenizedBuffer::Lex(source, NullDiagnosticConsumer());
  18. if (buffer.HasErrors()) {
  19. return 0;
  20. }
  21. // Walk the lexed and tokenized buffer to ensure it isn't corrupt in some way.
  22. //
  23. // TODO: We should enhance this to do more sanity checks on the resulting
  24. // token stream.
  25. for (TokenizedBuffer::Token token : buffer.Tokens()) {
  26. int line_number = buffer.GetLineNumber(token);
  27. CHECK(line_number > 0) << "Invalid line number!";
  28. CHECK(line_number < INT_MAX) << "Invalid line number!";
  29. int column_number = buffer.GetColumnNumber(token);
  30. CHECK(column_number > 0) << "Invalid line number!";
  31. CHECK(column_number < INT_MAX) << "Invalid line number!";
  32. }
  33. return 0;
  34. }
  35. } // namespace Carbon::Testing