fuzzer_util.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "explorer/fuzzing/fuzzer_util.h"
  5. #include "common/check.h"
  6. #include "common/fuzzing/proto_to_carbon.h"
  7. #include "explorer/interpreter/exec_program.h"
  8. #include "explorer/syntax/parse.h"
  9. #include "explorer/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("explorer/data/prelude.carbon", &arena, &ast->declarations);
  40. const ErrorOr<int> result =
  41. ExecProgram(&arena, *ast, /*trace_stream=*/std::nullopt);
  42. if (!result.ok()) {
  43. llvm::errs() << "Execution failed: " << result.error().message() << "\n";
  44. return;
  45. }
  46. llvm::outs() << "Executed OK: " << *result << "\n";
  47. }
  48. } // namespace Carbon