|
| 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 |
0 commit comments