Skip to content

Commit

Permalink
#Centipede: Refactor helper functions for running coverage tests.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 599935613
  • Loading branch information
saladq authored and copybara-github committed Jan 19, 2024
1 parent e8298b3 commit 4c3852b
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 51 deletions.
25 changes: 19 additions & 6 deletions centipede/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,24 @@ cc_library(
],
)

cc_library(
name = "test_coverage_util",
testonly = True,
srcs = ["test_coverage_util.cc"],
hdrs = ["test_coverage_util.h"],
deps = [
":centipede_callbacks",
":corpus",
":defs",
":environment",
":feature",
":mutation_input",
":runner_result",
":util",
"@com_google_absl//absl/log:check",
],
)

sh_library(
name = "test_fuzzing_util_sh",
srcs = ["test_fuzzing_util.sh"],
Expand Down Expand Up @@ -1680,20 +1698,15 @@ cc_test(
],
deps = [
":binary_info",
":centipede_callbacks",
":control_flow",
":coverage",
":defs",
":environment",
":feature",
":mutation_input",
":pc_info",
":runner_result",
":symbol_table",
":test_coverage_util",
":test_util",
":util",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
],
Expand Down
46 changes: 1 addition & 45 deletions centipede/coverage_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,15 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/flat_hash_set.h"
#include "absl/log/check.h"
#include "absl/strings/str_cat.h"
#include "./centipede/binary_info.h"
#include "./centipede/centipede_callbacks.h"
#include "./centipede/control_flow.h"
#include "./centipede/defs.h"
#include "./centipede/environment.h"
#include "./centipede/feature.h"
#include "./centipede/mutation_input.h"
#include "./centipede/pc_info.h"
#include "./centipede/runner_result.h"
#include "./centipede/symbol_table.h"
#include "./centipede/test_coverage_util.h"
#include "./centipede/test_util.h"
#include "./centipede/util.h"

namespace centipede {
namespace {
Expand Down Expand Up @@ -192,45 +187,6 @@ static std::string GetThreadedTargetPath() {
return GetDataDependencyFilepath("centipede/testing/threaded_fuzz_target");
}

// A simple CentipedeCallbacks derivative for this test.
class TestCallbacks : public CentipedeCallbacks {
public:
explicit TestCallbacks(const Environment &env) : CentipedeCallbacks(env) {}
bool Execute(std::string_view binary, const std::vector<ByteArray> &inputs,
BatchResult &batch_result) override {
int result =
ExecuteCentipedeSancovBinaryWithShmem(binary, inputs, batch_result);
CHECK_EQ(EXIT_SUCCESS, result);
return true;
}
void Mutate(const std::vector<MutationInputRef> &inputs, size_t num_mutants,
std::vector<ByteArray> &mutants) override {}
};

// Runs all `inputs`, returns FeatureVec for every input.
// `env` defines what target is executed and with what flags.
static std::vector<FeatureVec> RunInputsAndCollectCoverage(
const Environment &env, const std::vector<std::string> &inputs) {
TestCallbacks CBs(env);
std::filesystem::create_directories(TemporaryLocalDirPath());

// Repackage string inputs into ByteArray inputs.
std::vector<ByteArray> byte_array_inputs;
for (auto &string_input : inputs) {
byte_array_inputs.emplace_back(string_input.begin(), string_input.end());
}
BatchResult batch_result;
// Run.
CBs.Execute(env.binary, byte_array_inputs, batch_result);

// Repackage execution results into a vector of FeatureVec.
std::vector<FeatureVec> res;
for (const auto &er : batch_result.results()) {
res.push_back(er.features());
}
return res;
}

// Tests coverage collection on test_fuzz_target
// using two inputs that trigger different code paths.
TEST(Coverage, CoverageFeatures) {
Expand Down
73 changes: 73 additions & 0 deletions centipede/test_coverage_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2022 The Centipede Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "./centipede/test_coverage_util.h"

#include <filesystem> // NOLINT
#include <string>
#include <vector>

#include "absl/log/check.h"
#include "./centipede/corpus.h"
#include "./centipede/defs.h"
#include "./centipede/environment.h"
#include "./centipede/feature.h"
#include "./centipede/runner_result.h"
#include "./centipede/util.h"

namespace centipede {

std::vector<CorpusRecord> RunInputsAndCollectCorpusRecords(
const Environment &env, const std::vector<std::string> &inputs) {
TestCallbacks CBs(env);
std::filesystem::create_directories(TemporaryLocalDirPath());

// Repackage string inputs into ByteArray inputs.
std::vector<ByteArray> byte_array_inputs;
byte_array_inputs.reserve(inputs.size());
for (auto &string_input : inputs) {
byte_array_inputs.emplace_back(string_input.begin(), string_input.end());
}
BatchResult batch_result;
// Run.
CBs.Execute(env.binary, byte_array_inputs, batch_result);

// Repackage execution results into a vector of CorpusRecords.
std::vector<CorpusRecord> corpus_records;
std::vector<ExecutionResult> &execution_results = batch_result.results();
CHECK_EQ(byte_array_inputs.size(), execution_results.size());

corpus_records.reserve(byte_array_inputs.size());
for (int i = 0; i < byte_array_inputs.size(); ++i) {
corpus_records.push_back({.data = byte_array_inputs[i],
.features = execution_results[i].features()});
}
return corpus_records;
}

std::vector<FeatureVec> RunInputsAndCollectCoverage(
const Environment &env, const std::vector<std::string> &inputs) {
std::vector<CorpusRecord> corpus_records =
RunInputsAndCollectCorpusRecords(env, inputs);

// Repackage corpus records into a vector of FeatureVec.
std::vector<FeatureVec> res;
res.reserve(corpus_records.size());
for (const auto &record : corpus_records) {
res.push_back(record.features);
}
return res;
}

} // namespace centipede
59 changes: 59 additions & 0 deletions centipede/test_coverage_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2022 The Centipede Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef FUZZTEST_CENTIPEDE_TEST_COVERAGE_UTIL_H_
#define FUZZTEST_CENTIPEDE_TEST_COVERAGE_UTIL_H_

#include <cstddef>
#include <cstdlib>
#include <string>
#include <string_view>
#include <vector>

#include "absl/log/check.h"
#include "./centipede/centipede_callbacks.h"
#include "./centipede/corpus.h"
#include "./centipede/defs.h"
#include "./centipede/environment.h"
#include "./centipede/feature.h"
#include "./centipede/mutation_input.h"
#include "./centipede/runner_result.h"
namespace centipede {
// Runs all `inputs`, returns FeatureVec for every input.
// `env` defines what target is executed and with what flags.
std::vector<CorpusRecord> RunInputsAndCollectCorpusRecords(
const Environment &env, const std::vector<std::string> &inputs);

// Runs all `inputs`, returns a CorpusRecord for every input.
// `env` defines what target is executed and with what flags.
std::vector<FeatureVec> RunInputsAndCollectCoverage(
const Environment &env, const std::vector<std::string> &inputs);

// A simple CentipedeCallbacks derivative.
class TestCallbacks : public CentipedeCallbacks {
public:
explicit TestCallbacks(const Environment &env) : CentipedeCallbacks(env) {}
bool Execute(std::string_view binary, const std::vector<ByteArray> &inputs,
BatchResult &batch_result) override {
int result =
ExecuteCentipedeSancovBinaryWithShmem(binary, inputs, batch_result);
CHECK_EQ(EXIT_SUCCESS, result);
return true;
}
void Mutate(const std::vector<MutationInputRef> &inputs, size_t num_mutants,
std::vector<ByteArray> &mutants) override {}
};
} // namespace centipede

#endif // FUZZTEST_CENTIPEDE_TEST_COVERAGE_UTIL_H_

0 comments on commit 4c3852b

Please sign in to comment.