executable_semantics_fuzzer.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 <google/protobuf/text_format.h>
  5. #include <libprotobuf_mutator/src/libfuzzer/libfuzzer_macro.h>
  6. #include "common/fuzzing/carbon.pb.h"
  7. #include "executable_semantics/fuzzing/fuzzer_util.h"
  8. #include "executable_semantics/interpreter/exec_program.h"
  9. #include "executable_semantics/syntax/parse.h"
  10. #include "executable_semantics/syntax/prelude.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. namespace Carbon {
  13. // Parses and executes a fuzzer-generated program.
  14. void ParseAndExecute(const Fuzzing::CompilationUnit& compilation_unit) {
  15. const std::string source = ProtoToCarbonWithMain(compilation_unit);
  16. Arena arena;
  17. ErrorOr<AST> ast = ParseFromString(&arena, "Fuzzer.carbon", source,
  18. /*trace=*/false);
  19. if (!ast.ok()) {
  20. llvm::errs() << "Parsing failed: " << ast.error().message() << "\n";
  21. return;
  22. }
  23. AddPrelude("executable_semantics/data/prelude.carbon", &arena,
  24. &ast->declarations);
  25. const ErrorOr<int> result = ExecProgram(&arena, *ast, /*trace=*/false);
  26. if (!result.ok()) {
  27. llvm::errs() << "Execution failed: " << result.error().message() << "\n";
  28. return;
  29. }
  30. llvm::outs() << "Executed OK: " << *result << "\n";
  31. }
  32. } // namespace Carbon
  33. DEFINE_TEXT_PROTO_FUZZER(const Carbon::Fuzzing::Carbon& input) {
  34. Carbon::ParseAndExecute(input.compilation_unit());
  35. }