fuzzer_util.cpp 3.0 KB

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