fuzzer_util.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 "executable_semantics/fuzzing/fuzzer_util.h"
  5. #include "common/check.h"
  6. #include "common/fuzzing/proto_to_carbon.h"
  7. #include "executable_semantics/interpreter/exec_program.h"
  8. #include "executable_semantics/syntax/parse.h"
  9. #include "executable_semantics/syntax/prelude.h"
  10. #include "llvm/Support/raw_ostream.h"
  11. namespace Carbon {
  12. // Appended to fuzzer-generated Carbon source when the source is missing
  13. // `Main()` definition, to prevent early error return in semantic analysis.
  14. static constexpr char EmptyMain[] = R"(
  15. fn Main() -> i32 {
  16. return 0;
  17. }
  18. )";
  19. auto ProtoToCarbonWithMain(const Fuzzing::CompilationUnit& compilation_unit)
  20. -> std::string {
  21. const bool has_main = std::any_of(
  22. compilation_unit.declarations().begin(),
  23. compilation_unit.declarations().end(),
  24. [](const Fuzzing::Declaration& decl) {
  25. return decl.kind_case() == Fuzzing::Declaration::kFunction &&
  26. decl.function().name() == "Main";
  27. });
  28. return Carbon::ProtoToCarbon(compilation_unit) + (has_main ? "" : EmptyMain);
  29. }
  30. void ParseAndExecute(const Fuzzing::CompilationUnit& compilation_unit) {
  31. const std::string source = ProtoToCarbonWithMain(compilation_unit);
  32. Arena arena;
  33. ErrorOr<AST> ast = ParseFromString(&arena, "Fuzzer.carbon", source,
  34. /*parser_debug=*/false);
  35. if (!ast.ok()) {
  36. llvm::errs() << "Parsing failed: " << ast.error().message() << "\n";
  37. return;
  38. }
  39. AddPrelude("executable_semantics/data/prelude.carbon", &arena,
  40. &ast->declarations);
  41. const ErrorOr<int> result =
  42. ExecProgram(&arena, *ast, /*trace_stream=*/std::nullopt);
  43. if (!result.ok()) {
  44. llvm::errs() << "Execution failed: " << result.error().message() << "\n";
  45. return;
  46. }
  47. llvm::outs() << "Executed OK: " << *result << "\n";
  48. }
  49. } // namespace Carbon