-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#Centipede: Refactor helper functions for running coverage tests.
PiperOrigin-RevId: 599935613
- Loading branch information
1 parent
e8298b3
commit 4c3852b
Showing
4 changed files
with
152 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |