Skip to content

Commit 4c3852b

Browse files
saladqcopybara-github
authored andcommitted
#Centipede: Refactor helper functions for running coverage tests.
PiperOrigin-RevId: 599935613
1 parent e8298b3 commit 4c3852b

File tree

4 files changed

+152
-51
lines changed

4 files changed

+152
-51
lines changed

centipede/BUILD

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,24 @@ cc_library(
11421142
],
11431143
)
11441144

1145+
cc_library(
1146+
name = "test_coverage_util",
1147+
testonly = True,
1148+
srcs = ["test_coverage_util.cc"],
1149+
hdrs = ["test_coverage_util.h"],
1150+
deps = [
1151+
":centipede_callbacks",
1152+
":corpus",
1153+
":defs",
1154+
":environment",
1155+
":feature",
1156+
":mutation_input",
1157+
":runner_result",
1158+
":util",
1159+
"@com_google_absl//absl/log:check",
1160+
],
1161+
)
1162+
11451163
sh_library(
11461164
name = "test_fuzzing_util_sh",
11471165
srcs = ["test_fuzzing_util.sh"],
@@ -1680,20 +1698,15 @@ cc_test(
16801698
],
16811699
deps = [
16821700
":binary_info",
1683-
":centipede_callbacks",
16841701
":control_flow",
16851702
":coverage",
1686-
":defs",
16871703
":environment",
16881704
":feature",
1689-
":mutation_input",
16901705
":pc_info",
1691-
":runner_result",
16921706
":symbol_table",
1707+
":test_coverage_util",
16931708
":test_util",
1694-
":util",
16951709
"@com_google_absl//absl/container:flat_hash_set",
1696-
"@com_google_absl//absl/log:check",
16971710
"@com_google_absl//absl/strings",
16981711
"@com_google_googletest//:gtest_main",
16991712
],

centipede/coverage_test.cc

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,15 @@
3131
#include "gmock/gmock.h"
3232
#include "gtest/gtest.h"
3333
#include "absl/container/flat_hash_set.h"
34-
#include "absl/log/check.h"
3534
#include "absl/strings/str_cat.h"
3635
#include "./centipede/binary_info.h"
37-
#include "./centipede/centipede_callbacks.h"
3836
#include "./centipede/control_flow.h"
39-
#include "./centipede/defs.h"
4037
#include "./centipede/environment.h"
4138
#include "./centipede/feature.h"
42-
#include "./centipede/mutation_input.h"
4339
#include "./centipede/pc_info.h"
44-
#include "./centipede/runner_result.h"
4540
#include "./centipede/symbol_table.h"
41+
#include "./centipede/test_coverage_util.h"
4642
#include "./centipede/test_util.h"
47-
#include "./centipede/util.h"
4843

4944
namespace centipede {
5045
namespace {
@@ -192,45 +187,6 @@ static std::string GetThreadedTargetPath() {
192187
return GetDataDependencyFilepath("centipede/testing/threaded_fuzz_target");
193188
}
194189

195-
// A simple CentipedeCallbacks derivative for this test.
196-
class TestCallbacks : public CentipedeCallbacks {
197-
public:
198-
explicit TestCallbacks(const Environment &env) : CentipedeCallbacks(env) {}
199-
bool Execute(std::string_view binary, const std::vector<ByteArray> &inputs,
200-
BatchResult &batch_result) override {
201-
int result =
202-
ExecuteCentipedeSancovBinaryWithShmem(binary, inputs, batch_result);
203-
CHECK_EQ(EXIT_SUCCESS, result);
204-
return true;
205-
}
206-
void Mutate(const std::vector<MutationInputRef> &inputs, size_t num_mutants,
207-
std::vector<ByteArray> &mutants) override {}
208-
};
209-
210-
// Runs all `inputs`, returns FeatureVec for every input.
211-
// `env` defines what target is executed and with what flags.
212-
static std::vector<FeatureVec> RunInputsAndCollectCoverage(
213-
const Environment &env, const std::vector<std::string> &inputs) {
214-
TestCallbacks CBs(env);
215-
std::filesystem::create_directories(TemporaryLocalDirPath());
216-
217-
// Repackage string inputs into ByteArray inputs.
218-
std::vector<ByteArray> byte_array_inputs;
219-
for (auto &string_input : inputs) {
220-
byte_array_inputs.emplace_back(string_input.begin(), string_input.end());
221-
}
222-
BatchResult batch_result;
223-
// Run.
224-
CBs.Execute(env.binary, byte_array_inputs, batch_result);
225-
226-
// Repackage execution results into a vector of FeatureVec.
227-
std::vector<FeatureVec> res;
228-
for (const auto &er : batch_result.results()) {
229-
res.push_back(er.features());
230-
}
231-
return res;
232-
}
233-
234190
// Tests coverage collection on test_fuzz_target
235191
// using two inputs that trigger different code paths.
236192
TEST(Coverage, CoverageFeatures) {

centipede/test_coverage_util.cc

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2022 The Centipede Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "./centipede/test_coverage_util.h"
16+
17+
#include <filesystem> // NOLINT
18+
#include <string>
19+
#include <vector>
20+
21+
#include "absl/log/check.h"
22+
#include "./centipede/corpus.h"
23+
#include "./centipede/defs.h"
24+
#include "./centipede/environment.h"
25+
#include "./centipede/feature.h"
26+
#include "./centipede/runner_result.h"
27+
#include "./centipede/util.h"
28+
29+
namespace centipede {
30+
31+
std::vector<CorpusRecord> RunInputsAndCollectCorpusRecords(
32+
const Environment &env, const std::vector<std::string> &inputs) {
33+
TestCallbacks CBs(env);
34+
std::filesystem::create_directories(TemporaryLocalDirPath());
35+
36+
// Repackage string inputs into ByteArray inputs.
37+
std::vector<ByteArray> byte_array_inputs;
38+
byte_array_inputs.reserve(inputs.size());
39+
for (auto &string_input : inputs) {
40+
byte_array_inputs.emplace_back(string_input.begin(), string_input.end());
41+
}
42+
BatchResult batch_result;
43+
// Run.
44+
CBs.Execute(env.binary, byte_array_inputs, batch_result);
45+
46+
// Repackage execution results into a vector of CorpusRecords.
47+
std::vector<CorpusRecord> corpus_records;
48+
std::vector<ExecutionResult> &execution_results = batch_result.results();
49+
CHECK_EQ(byte_array_inputs.size(), execution_results.size());
50+
51+
corpus_records.reserve(byte_array_inputs.size());
52+
for (int i = 0; i < byte_array_inputs.size(); ++i) {
53+
corpus_records.push_back({.data = byte_array_inputs[i],
54+
.features = execution_results[i].features()});
55+
}
56+
return corpus_records;
57+
}
58+
59+
std::vector<FeatureVec> RunInputsAndCollectCoverage(
60+
const Environment &env, const std::vector<std::string> &inputs) {
61+
std::vector<CorpusRecord> corpus_records =
62+
RunInputsAndCollectCorpusRecords(env, inputs);
63+
64+
// Repackage corpus records into a vector of FeatureVec.
65+
std::vector<FeatureVec> res;
66+
res.reserve(corpus_records.size());
67+
for (const auto &record : corpus_records) {
68+
res.push_back(record.features);
69+
}
70+
return res;
71+
}
72+
73+
} // namespace centipede

centipede/test_coverage_util.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2022 The Centipede Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef FUZZTEST_CENTIPEDE_TEST_COVERAGE_UTIL_H_
16+
#define FUZZTEST_CENTIPEDE_TEST_COVERAGE_UTIL_H_
17+
18+
#include <cstddef>
19+
#include <cstdlib>
20+
#include <string>
21+
#include <string_view>
22+
#include <vector>
23+
24+
#include "absl/log/check.h"
25+
#include "./centipede/centipede_callbacks.h"
26+
#include "./centipede/corpus.h"
27+
#include "./centipede/defs.h"
28+
#include "./centipede/environment.h"
29+
#include "./centipede/feature.h"
30+
#include "./centipede/mutation_input.h"
31+
#include "./centipede/runner_result.h"
32+
namespace centipede {
33+
// Runs all `inputs`, returns FeatureVec for every input.
34+
// `env` defines what target is executed and with what flags.
35+
std::vector<CorpusRecord> RunInputsAndCollectCorpusRecords(
36+
const Environment &env, const std::vector<std::string> &inputs);
37+
38+
// Runs all `inputs`, returns a CorpusRecord for every input.
39+
// `env` defines what target is executed and with what flags.
40+
std::vector<FeatureVec> RunInputsAndCollectCoverage(
41+
const Environment &env, const std::vector<std::string> &inputs);
42+
43+
// A simple CentipedeCallbacks derivative.
44+
class TestCallbacks : public CentipedeCallbacks {
45+
public:
46+
explicit TestCallbacks(const Environment &env) : CentipedeCallbacks(env) {}
47+
bool Execute(std::string_view binary, const std::vector<ByteArray> &inputs,
48+
BatchResult &batch_result) override {
49+
int result =
50+
ExecuteCentipedeSancovBinaryWithShmem(binary, inputs, batch_result);
51+
CHECK_EQ(EXIT_SUCCESS, result);
52+
return true;
53+
}
54+
void Mutate(const std::vector<MutationInputRef> &inputs, size_t num_mutants,
55+
std::vector<ByteArray> &mutants) override {}
56+
};
57+
} // namespace centipede
58+
59+
#endif // FUZZTEST_CENTIPEDE_TEST_COVERAGE_UTIL_H_

0 commit comments

Comments
 (0)