fuzzer_util.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/FileSystem.h"
  11. #include "llvm/Support/Path.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. #include "tools/cpp/runfiles/runfiles.h"
  14. namespace Carbon {
  15. // Appended to fuzzer-generated Carbon source when the source is missing
  16. // `Main()` definition, to prevent early error return in semantic analysis.
  17. static constexpr char EmptyMain[] = R"(
  18. fn Main() -> i32 {
  19. return 0;
  20. }
  21. )";
  22. auto Internal::GetRunfilesFile(const std::string& file)
  23. -> ErrorOr<std::string> {
  24. using bazel::tools::cpp::runfiles::Runfiles;
  25. std::string error;
  26. // `Runfiles::Create()` fails if passed an empty `argv0`.
  27. std::unique_ptr<Runfiles> runfiles(Runfiles::Create(
  28. /*argv0=*/llvm::sys::fs::getMainExecutable(nullptr, nullptr), &error));
  29. if (runfiles == nullptr) {
  30. return Error(error);
  31. }
  32. std::string full_path = runfiles->Rlocation(file);
  33. if (!llvm::sys::fs::exists(full_path)) {
  34. return ErrorBuilder() << full_path << " doesn't exist";
  35. }
  36. return full_path;
  37. }
  38. auto ProtoToCarbonWithMain(const Fuzzing::CompilationUnit& compilation_unit)
  39. -> std::string {
  40. const bool has_main = std::any_of(
  41. compilation_unit.declarations().begin(),
  42. compilation_unit.declarations().end(),
  43. [](const Fuzzing::Declaration& decl) {
  44. return decl.kind_case() == Fuzzing::Declaration::kFunction &&
  45. decl.function().name() == "Main";
  46. });
  47. return Carbon::ProtoToCarbon(compilation_unit) + (has_main ? "" : EmptyMain);
  48. }
  49. void ParseAndExecute(const Fuzzing::CompilationUnit& compilation_unit) {
  50. const std::string source = ProtoToCarbonWithMain(compilation_unit);
  51. Arena arena;
  52. ErrorOr<AST> ast = ParseFromString(&arena, "Fuzzer.carbon", source,
  53. /*parser_debug=*/false);
  54. if (!ast.ok()) {
  55. llvm::errs() << "Parsing failed: " << ast.error().message() << "\n";
  56. return;
  57. }
  58. const ErrorOr<std::string> prelude_path =
  59. Internal::GetRunfilesFile("carbon/explorer/data/prelude.carbon");
  60. CARBON_CHECK(prelude_path.ok()) << prelude_path.error().message();
  61. AddPrelude(*prelude_path, &arena, &ast->declarations);
  62. const ErrorOr<int> result =
  63. ExecProgram(&arena, *ast, /*trace_stream=*/std::nullopt);
  64. if (!result.ok()) {
  65. llvm::errs() << "Execution failed: " << result.error().message() << "\n";
  66. return;
  67. }
  68. llvm::outs() << "Executed OK: " << *result << "\n";
  69. }
  70. } // namespace Carbon