From 98d95cd188d1c72ffd5e263a5e0664475a8a22f0 Mon Sep 17 00:00:00 2001 From: pk19604014 <95385881+pk19604014@users.noreply.github.com> Date: Thu, 25 Aug 2022 12:06:45 -0400 Subject: [PATCH] Cleanup: since explorer_fuzzer is now a standard cc_fuzz_test, and runs on all files in the corpus, there's no need for fuzzer_util_test to do the same manually Before: ``` //explorer/fuzzing:fuzzer_util_test PASSED in 18.5s ``` After: ``` //explorer/fuzzing:fuzzer_util_test PASSED in 0.3s ``` --- explorer/fuzzing/BUILD | 2 + explorer/fuzzing/explorer_fuzzer.cpp | 9 ++++- explorer/fuzzing/fuzzer_util.cpp | 28 ++++++-------- explorer/fuzzing/fuzzer_util.h | 4 +- explorer/fuzzing/fuzzer_util_test.cpp | 54 +++++++++++++-------------- 5 files changed, 51 insertions(+), 46 deletions(-) diff --git a/explorer/fuzzing/BUILD b/explorer/fuzzing/BUILD index e7021c2b3b26..cc17905a79d8 100644 --- a/explorer/fuzzing/BUILD +++ b/explorer/fuzzing/BUILD @@ -90,6 +90,7 @@ cc_test( ], deps = [ ":fuzzer_util", + "//common:gtest_main", "@com_google_googletest//:gtest", "@com_google_protobuf//:protobuf_headers", "@llvm-project//llvm:Support", @@ -125,6 +126,7 @@ cc_fuzz_test( corpus = glob(["fuzzer_corpus/*"]), deps = [ ":fuzzer_util", + "//common:error", "@com_google_libprotobuf_mutator//:libprotobuf_mutator", "@llvm-project//llvm:Support", ], diff --git a/explorer/fuzzing/explorer_fuzzer.cpp b/explorer/fuzzing/explorer_fuzzer.cpp index dca4ed0dc552..406849990199 100644 --- a/explorer/fuzzing/explorer_fuzzer.cpp +++ b/explorer/fuzzing/explorer_fuzzer.cpp @@ -4,8 +4,15 @@ #include +#include "common/error.h" #include "explorer/fuzzing/fuzzer_util.h" +#include "llvm/Support/raw_ostream.h" DEFINE_TEXT_PROTO_FUZZER(const Carbon::Fuzzing::Carbon& input) { - Carbon::ParseAndExecute(input.compilation_unit()); + const auto result = Carbon::ParseAndExecute(input.compilation_unit()); + if (result.ok()) { + llvm::outs() << "Executed OK: " << *result << "\n"; + } else { + llvm::errs() << "Execution failed: " << result.error() << "\n"; + } } diff --git a/explorer/fuzzing/fuzzer_util.cpp b/explorer/fuzzing/fuzzer_util.cpp index caa66dd6e91b..64a13e2a3703 100644 --- a/explorer/fuzzing/fuzzer_util.cpp +++ b/explorer/fuzzing/fuzzer_util.cpp @@ -7,13 +7,13 @@ #include #include "common/check.h" +#include "common/error.h" #include "common/fuzzing/proto_to_carbon.h" #include "explorer/interpreter/exec_program.h" #include "explorer/syntax/parse.h" #include "explorer/syntax/prelude.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" -#include "llvm/Support/raw_ostream.h" #include "tools/cpp/runfiles/runfiles.h" namespace Carbon { @@ -69,27 +69,21 @@ auto ProtoToCarbonWithMain(const Fuzzing::CompilationUnit& compilation_unit) return Carbon::ProtoToCarbon(compilation_unit) + (has_main ? "" : EmptyMain); } -void ParseAndExecute(const Fuzzing::CompilationUnit& compilation_unit) { +auto ParseAndExecute(const Fuzzing::CompilationUnit& compilation_unit) + -> ErrorOr { const std::string source = ProtoToCarbonWithMain(compilation_unit); Arena arena; - ErrorOr ast = ParseFromString(&arena, "Fuzzer.carbon", source, - /*parser_debug=*/false); - if (!ast.ok()) { - llvm::errs() << "Parsing failed: " << ast.error().message() << "\n"; - return; - } + CARBON_ASSIGN_OR_RETURN(AST ast, + ParseFromString(&arena, "Fuzzer.carbon", source, + /*parser_debug=*/false)); const ErrorOr prelude_path = Internal::GetRunfilesFile("carbon/explorer/data/prelude.carbon"); - CARBON_CHECK(prelude_path.ok()) << prelude_path.error().message(); - AddPrelude(*prelude_path, &arena, &ast->declarations); - const ErrorOr result = - ExecProgram(&arena, *ast, /*trace_stream=*/std::nullopt); - if (!result.ok()) { - llvm::errs() << "Execution failed: " << result.error().message() << "\n"; - return; - } - llvm::outs() << "Executed OK: " << *result << "\n"; + // Can't do anything without a prelude, so it's a fatal error. + CARBON_CHECK(prelude_path.ok()) << prelude_path.error(); + + AddPrelude(*prelude_path, &arena, &ast.declarations); + return ExecProgram(&arena, ast, /*trace_stream=*/std::nullopt); } } // namespace Carbon diff --git a/explorer/fuzzing/fuzzer_util.h b/explorer/fuzzing/fuzzer_util.h index 6d32d612c53f..235ad699b4c0 100644 --- a/explorer/fuzzing/fuzzer_util.h +++ b/explorer/fuzzing/fuzzer_util.h @@ -21,7 +21,9 @@ auto ProtoToCarbonWithMain(const Fuzzing::CompilationUnit& compilation_unit) -> std::string; // Parses and executes a fuzzer-generated program. -void ParseAndExecute(const Fuzzing::CompilationUnit& compilation_unit); +// Returns program result if execution was successful. +auto ParseAndExecute(const Fuzzing::CompilationUnit& compilation_unit) + -> ErrorOr; namespace Internal { diff --git a/explorer/fuzzing/fuzzer_util_test.cpp b/explorer/fuzzing/fuzzer_util_test.cpp index ddee7c555cd8..1e4b40ee2fa5 100644 --- a/explorer/fuzzing/fuzzer_util_test.cpp +++ b/explorer/fuzzing/fuzzer_util_test.cpp @@ -15,26 +15,33 @@ namespace Carbon::Testing { namespace { -static std::vector* carbon_files = nullptr; - -// A workaround for https://github.com/carbon-language/carbon-lang/issues/1208. -TEST(FuzzerUtilTest, RunFuzzerOnCorpus) { - int parsed_file_count = 0; - for (const llvm::StringRef f : *carbon_files) { - llvm::outs() << "Processing " << f << "\n"; - std::ifstream file(f.str(), std::ios::in); - ASSERT_TRUE(file.is_open()); - std::stringstream contents; - contents << file.rdbuf(); - // Parsing errors are ignored to make the fuzzer inputs less brittle as the - // explorer code changes. This also matches standard fuzzer behavior. - if (auto carbon_proto = ParseCarbonTextProto(contents.str()); - carbon_proto.ok()) { - ParseAndExecute(carbon_proto->compilation_unit()); - ++parsed_file_count; - } - } - EXPECT_GT(parsed_file_count, 0); +TEST(FuzzerUtilTest, ParseAndExecute) { + const ErrorOr carbon_proto = ParseCarbonTextProto(R"( + compilation_unit { + package_statement { package_name: "P" } + is_api: true + declarations { + function { + name: "Main" + param_pattern {} + return_term { + kind: Expression + type { int_type_literal {} } + } + body { + statements { + return_expression_statement { + expression { int_literal { value: 0 } } + } + } + } + } + } + })"); + ASSERT_TRUE(carbon_proto.ok()); + const ErrorOr result = ParseAndExecute(carbon_proto->compilation_unit()); + ASSERT_TRUE(result.ok()) << "Execution failed: " << result.error(); + EXPECT_EQ(*result, 0); } TEST(FuzzerUtilTest, GetRunfilesFile) { @@ -64,10 +71,3 @@ TEST(FuzzerUtilTest, ParseCarbonTextProtoWithUnknownField) { } // namespace } // namespace Carbon::Testing - -auto main(int argc, char** argv) -> int { - ::testing::InitGoogleTest(&argc, argv); - Carbon::Testing::carbon_files = - new std::vector(&argv[1], &argv[argc]); - return RUN_ALL_TESTS(); -}