fuzzer_util.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <google/protobuf/text_format.h>
  6. #include "common/check.h"
  7. #include "common/error.h"
  8. #include "common/fuzzing/proto_to_carbon.h"
  9. #include "explorer/interpreter/exec_program.h"
  10. #include "explorer/interpreter/trace_stream.h"
  11. #include "explorer/syntax/parse.h"
  12. #include "explorer/syntax/prelude.h"
  13. #include "llvm/Support/FileSystem.h"
  14. #include "llvm/Support/Path.h"
  15. #include "tools/cpp/runfiles/runfiles.h"
  16. namespace Carbon {
  17. // Appended to fuzzer-generated Carbon source when the source is missing
  18. // `Main()` definition, to prevent early error return in semantic analysis.
  19. static constexpr char EmptyMain[] = R"(
  20. fn Main() -> i32 {
  21. return 0;
  22. }
  23. )";
  24. auto Internal::GetRunfilesFile(const std::string& file)
  25. -> ErrorOr<std::string> {
  26. using bazel::tools::cpp::runfiles::Runfiles;
  27. std::string error;
  28. // `Runfiles::Create()` fails if passed an empty `argv0`.
  29. std::unique_ptr<Runfiles> runfiles(Runfiles::Create(
  30. /*argv0=*/llvm::sys::fs::getMainExecutable(nullptr, nullptr), &error));
  31. if (runfiles == nullptr) {
  32. return Error(error);
  33. }
  34. std::string full_path = runfiles->Rlocation(file);
  35. if (!llvm::sys::fs::exists(full_path)) {
  36. return ErrorBuilder() << full_path << " doesn't exist";
  37. }
  38. return full_path;
  39. }
  40. auto ParseCarbonTextProto(const std::string& contents, bool allow_unknown)
  41. -> ErrorOr<Fuzzing::Carbon> {
  42. google::protobuf::TextFormat::Parser parser;
  43. if (allow_unknown) {
  44. parser.AllowUnknownField(true);
  45. parser.AllowUnknownExtension(true);
  46. }
  47. Fuzzing::Carbon carbon_proto;
  48. if (!parser.ParseFromString(contents, &carbon_proto)) {
  49. return ErrorBuilder() << "Couldn't parse Carbon text proto";
  50. }
  51. return carbon_proto;
  52. }
  53. auto ProtoToCarbonWithMain(const Fuzzing::CompilationUnit& compilation_unit)
  54. -> std::string {
  55. const bool has_main = std::any_of(
  56. compilation_unit.declarations().begin(),
  57. compilation_unit.declarations().end(),
  58. [](const Fuzzing::Declaration& decl) {
  59. return decl.kind_case() == Fuzzing::Declaration::kFunction &&
  60. decl.function().name().name() == "Main";
  61. });
  62. return Carbon::ProtoToCarbon(compilation_unit) + (has_main ? "" : EmptyMain);
  63. }
  64. auto ParseAndExecute(const Fuzzing::CompilationUnit& compilation_unit)
  65. -> ErrorOr<int> {
  66. const std::string source = ProtoToCarbonWithMain(compilation_unit);
  67. Arena arena;
  68. CARBON_ASSIGN_OR_RETURN(AST ast,
  69. ParseFromString(&arena, "Fuzzer.carbon", source,
  70. /*parser_debug=*/false));
  71. const ErrorOr<std::string> prelude_path =
  72. Internal::GetRunfilesFile("carbon/explorer/data/prelude.carbon");
  73. // Can't do anything without a prelude, so it's a fatal error.
  74. CARBON_CHECK(prelude_path.ok()) << prelude_path.error();
  75. AddPrelude(*prelude_path, &arena, &ast.declarations,
  76. &ast.num_prelude_declarations);
  77. TraceStream trace_stream;
  78. CARBON_ASSIGN_OR_RETURN(ast, AnalyzeProgram(&arena, ast, &trace_stream));
  79. return ExecProgram(&arena, ast, &trace_stream);
  80. }
  81. } // namespace Carbon