fuzzverter.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // An utility for converting between fuzzer protos and Carbon sources.
  5. //
  6. // For example, to convert a crashing input in text proto to carbon source:
  7. // `fuzzverter --mode=proto_to_carbon --input file.textproto`
  8. //
  9. // To generate a new text proto from carbon source for seeding the corpus:
  10. // `fuzzverter --mode=carbon_to_proto --input file.carbon`
  11. #include <google/protobuf/text_format.h>
  12. #include <cstdlib>
  13. #include <fstream>
  14. #include <ios>
  15. #include <sstream>
  16. #include "common/error.h"
  17. #include "common/fuzzing/carbon.pb.h"
  18. #include "explorer/common/error_builders.h"
  19. #include "explorer/fuzzing/ast_to_proto.h"
  20. #include "explorer/fuzzing/fuzzer_util.h"
  21. #include "explorer/syntax/parse.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Support/InitLLVM.h"
  24. namespace Carbon {
  25. namespace cl = llvm::cl;
  26. // Reads a file and returns its contents as a string.
  27. static auto ReadFile(std::string_view file_name) -> ErrorOr<std::string> {
  28. std::ifstream file(file_name, std::ios::in);
  29. if (!file.is_open()) {
  30. return ErrorBuilder() << "Could not open " << file_name << " for reading";
  31. }
  32. std::stringstream ss;
  33. ss << file.rdbuf();
  34. return ss.str();
  35. }
  36. // Writes string `s` to `file_name`.
  37. static auto WriteFile(std::string_view s, std::string_view file_name)
  38. -> ErrorOr<Success> {
  39. std::ofstream file(file_name, std::ios::out);
  40. if (!file.is_open()) {
  41. return ErrorBuilder() << "Could not open " << file_name << " for writing";
  42. }
  43. file << s;
  44. return Success();
  45. }
  46. // Converts text proto to Carbon source.
  47. static auto TextProtoToCarbon(std::string_view input_file_name,
  48. std::string_view output_file_name)
  49. -> ErrorOr<Success> {
  50. CARBON_ASSIGN_OR_RETURN(const std::string input_contents,
  51. ReadFile(input_file_name));
  52. CARBON_ASSIGN_OR_RETURN(const Fuzzing::Carbon carbon_proto,
  53. ParseCarbonTextProto(input_contents));
  54. const std::string carbon_source =
  55. ProtoToCarbonWithMain(carbon_proto.compilation_unit());
  56. return WriteFile(carbon_source, output_file_name);
  57. }
  58. // Converts Carbon source to text proto.
  59. static auto CarbonToTextProto(std::string_view input_file_name,
  60. std::string_view output_file_name)
  61. -> ErrorOr<Success> {
  62. Carbon::Arena arena;
  63. const ErrorOr<AST> ast = Carbon::Parse(&arena, input_file_name,
  64. /*trace=*/false);
  65. if (!ast.ok()) {
  66. return ErrorBuilder() << "Parsing failed: " << ast.error().message();
  67. }
  68. Fuzzing::Carbon carbon_proto;
  69. *carbon_proto.mutable_compilation_unit() = AstToProto(*ast);
  70. std::string proto_string;
  71. google::protobuf::TextFormat::Printer p;
  72. if (!p.PrintToString(carbon_proto, &proto_string)) {
  73. return Error("Failed to convert to text proto");
  74. }
  75. return WriteFile(proto_string, output_file_name);
  76. }
  77. // Command line options for defining input/output format.
  78. enum class ConversionMode { TextProtoToCarbon, CarbonToTextProto };
  79. auto Main(int argc, char* argv[]) -> ErrorOr<Success> {
  80. llvm::InitLLVM init_llvm(argc, argv);
  81. cl::opt<ConversionMode> mode(
  82. "mode", cl::desc("Conversion mode"),
  83. cl::values(
  84. clEnumValN(ConversionMode::TextProtoToCarbon, "proto_to_carbon",
  85. "Convert text proto to Carbon source"),
  86. clEnumValN(ConversionMode::CarbonToTextProto, "carbon_to_proto",
  87. "Convert Carbon source to text proto")),
  88. cl::Required);
  89. cl::opt<std::string> input_file_name("input", cl::desc("<input file>"),
  90. cl::init("/dev/stdin"));
  91. cl::opt<std::string> output_file_name("output", cl::desc("<output file>"),
  92. cl::init("/dev/stdout"));
  93. cl::ParseCommandLineOptions(argc, argv);
  94. switch (mode) {
  95. case ConversionMode::TextProtoToCarbon:
  96. return TextProtoToCarbon(input_file_name, output_file_name);
  97. case ConversionMode::CarbonToTextProto:
  98. return CarbonToTextProto(input_file_name, output_file_name);
  99. }
  100. }
  101. } // namespace Carbon
  102. auto main(int argc, char* argv[]) -> int {
  103. if (const auto result = Carbon::Main(argc, argv); !result.ok()) {
  104. llvm::errs() << result.error().message() << "\n";
  105. return EXIT_FAILURE;
  106. }
  107. return EXIT_SUCCESS;
  108. }