string_literal_fuzzer.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/diagnostics/null_diagnostics.h"
  9. #include "toolchain/lex/string_literal.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. auto token = Lex::StringLiteral::Lex(
  15. llvm::StringRef(reinterpret_cast<const char*>(data), size));
  16. if (!token) {
  17. // Lexically not a string literal.
  18. return 0;
  19. }
  20. if (!token->is_terminated()) {
  21. // Found errors while parsing.
  22. return 0;
  23. }
  24. fprintf(stderr, "valid: %d\n", token->is_terminated());
  25. fprintf(stderr, "size: %lu\n", token->text().size());
  26. fprintf(stderr, "text: %s\n", token->text().str().c_str());
  27. // Check multiline flag was computed correctly.
  28. CARBON_CHECK(token->is_multi_line() == token->text().contains('\n'));
  29. llvm::BumpPtrAllocator allocator;
  30. volatile auto value =
  31. token->ComputeValue(allocator, NullDiagnosticEmitter<const char*>());
  32. (void)value;
  33. return 0;
  34. }
  35. } // namespace Carbon::Testing