fuzzverter.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. Fuzzing::Carbon carbon_proto;
  53. if (!google::protobuf::TextFormat::ParseFromString(input_contents,
  54. &carbon_proto)) {
  55. return Error("Could not parse text proto");
  56. }
  57. const std::string carbon_source =
  58. ProtoToCarbonWithMain(carbon_proto.compilation_unit());
  59. return WriteFile(carbon_source, output_file_name);
  60. }
  61. // Converts Carbon source to text proto.
  62. static auto CarbonToTextProto(std::string_view input_file_name,
  63. std::string_view output_file_name)
  64. -> ErrorOr<Success> {
  65. Carbon::Arena arena;
  66. const ErrorOr<AST> ast = Carbon::Parse(&arena, input_file_name,
  67. /*trace=*/false);
  68. if (!ast.ok()) {
  69. return ErrorBuilder() << "Parsing failed: " << ast.error().message();
  70. }
  71. Fuzzing::Carbon carbon_proto;
  72. *carbon_proto.mutable_compilation_unit() = AstToProto(*ast);
  73. std::string proto_string;
  74. google::protobuf::TextFormat::Printer p;
  75. if (!p.PrintToString(carbon_proto, &proto_string)) {
  76. return Error("Failed to convert to text proto");
  77. }
  78. return WriteFile(proto_string, output_file_name);
  79. }
  80. // Command line options for defining input/output format.
  81. enum class ConversionMode { TextProtoToCarbon, CarbonToTextProto };
  82. auto Main(int argc, char* argv[]) -> ErrorOr<Success> {
  83. llvm::InitLLVM init_llvm(argc, argv);
  84. cl::opt<ConversionMode> mode(
  85. "mode", cl::desc("Conversion mode"),
  86. cl::values(
  87. clEnumValN(ConversionMode::TextProtoToCarbon, "proto_to_carbon",
  88. "Convert text proto to Carbon source"),
  89. clEnumValN(ConversionMode::CarbonToTextProto, "carbon_to_proto",
  90. "Convert Carbon source to text proto")),
  91. cl::Required);
  92. cl::opt<std::string> input_file_name("input", cl::desc("<input file>"),
  93. cl::init("/dev/stdin"));
  94. cl::opt<std::string> output_file_name("output", cl::desc("<output file>"),
  95. cl::init("/dev/stdout"));
  96. cl::ParseCommandLineOptions(argc, argv);
  97. switch (mode) {
  98. case ConversionMode::TextProtoToCarbon:
  99. return TextProtoToCarbon(input_file_name, output_file_name);
  100. case ConversionMode::CarbonToTextProto:
  101. return CarbonToTextProto(input_file_name, output_file_name);
  102. }
  103. }
  104. } // namespace Carbon
  105. auto main(int argc, char* argv[]) -> int {
  106. if (const auto result = Carbon::Main(argc, argv); !result.ok()) {
  107. llvm::errs() << result.error().message() << "\n";
  108. return EXIT_FAILURE;
  109. }
  110. return EXIT_SUCCESS;
  111. }