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
```
This commit is contained in:
pk19604014
2022-08-25 09:06:45 -07:00
committed by GitHub
parent 25d824ef15
commit 98d95cd188
5 changed files with 51 additions and 46 deletions
+2
View File
@@ -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",
],
+8 -1
View File
@@ -4,8 +4,15 @@
#include <libprotobuf_mutator/src/libfuzzer/libfuzzer_macro.h>
#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";
}
}
+11 -17
View File
@@ -7,13 +7,13 @@
#include <google/protobuf/text_format.h>
#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<int> {
const std::string source = ProtoToCarbonWithMain(compilation_unit);
Arena arena;
ErrorOr<AST> 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<std::string> 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<int> 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
+3 -1
View File
@@ -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<int>;
namespace Internal {
+27 -27
View File
@@ -15,26 +15,33 @@
namespace Carbon::Testing {
namespace {
static std::vector<llvm::StringRef>* 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<Fuzzing::Carbon> 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<int> 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<llvm::StringRef>(&argv[1], &argv[argc]);
return RUN_ALL_TESTS();
}