Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 722786031
  • Loading branch information
lszekeres authored and copybara-github committed Feb 3, 2025
1 parent 1752ee0 commit b44c412
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 144 deletions.
31 changes: 19 additions & 12 deletions centipede/execution_metadata_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@ using ::testing::IsEmpty;
using ::testing::UnorderedElementsAreArray;

TEST(ExecutionMetadata, ForEachCmpEntryEnumeratesEntriesInRawBytes) {
ExecutionMetadata metadata{.cmp_data = {
2, // size
1, 2, // a
3, 4, // b
0, // zero-sized entry
3, // size
5, 6, 7, // a
8, 9, 10, // b
}};
ExecutionMetadata metadata;
metadata.cmp_data = {
2, // size
1,
2, // a
3,
4, // b
0, // zero-sized entry
3, // size
5, 6,
7, // a
8, 9,
10, // b
};
std::vector<std::pair<ByteSpan, ByteSpan>> enumeration_result;
EXPECT_TRUE(metadata.ForEachCmpEntry(
[&](ByteSpan a, ByteSpan b) { enumeration_result.emplace_back(a, b); }));
Expand All @@ -54,15 +59,17 @@ TEST(ExecutionMetadata, ForEachCmpEntryEnumeratesEntriesInRawBytes) {

TEST(ExecutionMetadata, ForEachCmpEntryHandlesEmptyCmpData) {
auto noop_callback = [](ByteSpan, ByteSpan) {};
EXPECT_TRUE(ExecutionMetadata{.cmp_data = {}}.ForEachCmpEntry(noop_callback));
EXPECT_TRUE(ExecutionMetadata{}.ForEachCmpEntry(noop_callback));
}

TEST(ExecutionMetadata,
ForEachCmpEntryReturnsFalseOnCmpDataWithNotEnoughBytes) {
auto noop_callback = [](ByteSpan, ByteSpan) {};
const auto bad_metadata_1 = ExecutionMetadata{.cmp_data = {3, 1, 2, 3}};
auto bad_metadata_1 = ExecutionMetadata{};
bad_metadata_1.cmp_data = {3, 1, 2, 3};
EXPECT_FALSE(bad_metadata_1.ForEachCmpEntry(noop_callback));
const auto bad_metadata_2 = ExecutionMetadata{.cmp_data = {3, 1, 2, 3, 4, 5}};
auto bad_metadata_2 = ExecutionMetadata{};
bad_metadata_2.cmp_data = {3, 1, 2, 3, 4, 5};
EXPECT_FALSE(bad_metadata_2.ForEachCmpEntry(noop_callback));
}

Expand Down
17 changes: 8 additions & 9 deletions centipede/fuzztest_mutator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -47,11 +48,9 @@ struct FuzzTestMutator::MutationMetadata {
class FuzzTestMutator::MutatorDomain : public MutatorDomainBase {
public:
MutatorDomain()
: MutatorDomainBase(fuzztest::VectorOf(fuzztest::Arbitrary<uint8_t>())) {
}
: MutatorDomainBase(fuzztest::VectorOf(fuzztest::Arbitrary<uint8_t>())) {}

~MutatorDomain() {
}
~MutatorDomain() {}
};

FuzzTestMutator::FuzzTestMutator(const Knobs &knobs, uint64_t seed)
Expand Down Expand Up @@ -99,9 +98,9 @@ void FuzzTestMutator::CrossOver(ByteArray &data, const ByteArray &other) {
}
}

void FuzzTestMutator::MutateMany(const std::vector<MutationInputRef>& inputs,
void FuzzTestMutator::MutateMany(const std::vector<MutationInputRef> &inputs,
size_t num_mutants,
std::vector<ByteArray>& mutants) {
std::vector<ByteArray> &mutants) {
if (inputs.empty()) abort();
// TODO(xinhaoyuan): Consider metadata in other inputs instead of always the
// first one.
Expand All @@ -119,14 +118,14 @@ void FuzzTestMutator::MutateMany(const std::vector<MutationInputRef>& inputs,
CrossOver(mutant, other_input);
} else {
domain_->Mutate(mutant, prng_,
{.cmp_tables = &mutation_metadata_->cmp_tables},
{/*cmp_tables=*/&mutation_metadata_->cmp_tables},
/*only_shrink=*/false);
}
mutants.push_back(std::move(mutant));
}
}

void FuzzTestMutator::SetMetadata(const ExecutionMetadata& metadata) {
void FuzzTestMutator::SetMetadata(const ExecutionMetadata &metadata) {
metadata.ForEachCmpEntry([this](ByteSpan a, ByteSpan b) {
size_t size = a.size();
if (size < kMinCmpEntrySize) return;
Expand All @@ -145,7 +144,7 @@ bool FuzzTestMutator::set_max_len(size_t max_len) {
}

void FuzzTestMutator::AddToDictionary(
const std::vector<ByteArray>& dict_entries) {
const std::vector<ByteArray> &dict_entries) {
domain_->WithDictionary(dict_entries);
}

Expand Down
239 changes: 116 additions & 123 deletions centipede/fuzztest_mutator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TEST(FuzzTestMutator, DifferentRngSeedsLeadToDifferentMutantSequences) {
std::vector<ByteArray> res[2];
for (size_t i = 0; i < 2; i++) {
ByteArray data = {0};
std::vector<MutationInputRef> mutation_inputs = {{.data = data}};
std::vector<MutationInputRef> mutation_inputs = {{data}};
std::vector<ByteArray> mutants;
constexpr size_t kMutantSequenceLength = 100;
for (size_t iter = 0; iter < kMutantSequenceLength; iter++) {
Expand All @@ -68,11 +68,11 @@ TEST(FuzzTestMutator, MutateManyWorksWithInputsLargerThanMaxLen) {

mutator.MutateMany(
{
{.data = {0, 1, 2, 3, 4, 5, 6, 7}},
{.data = {0}},
{.data = {0, 1}},
{.data = {0, 1, 2}},
{.data = {0, 1, 2, 3}},
{/*data=*/{0, 1, 2, 3, 4, 5, 6, 7}},
{/*data=*/{0}},
{/*data=*/{0, 1}},
{/*data=*/{0, 1, 2}},
{/*data=*/{0, 1, 2, 3}},
},
kNumMutantsToGenerate, mutants);

Expand All @@ -88,8 +88,8 @@ TEST(FuzzTestMutator, CrossOverInsertsDataFromOtherInputs) {

mutator.MutateMany(
{
{.data = {0, 1, 2, 3}},
{.data = {4, 5, 6, 7}},
{/*data=*/{0, 1, 2, 3}},
{/*data=*/{4, 5, 6, 7}},
},
kNumMutantsToGenerate, mutants);

Expand Down Expand Up @@ -121,8 +121,8 @@ TEST(FuzzTestMutator, CrossOverOverwritesDataFromOtherInputs) {

mutator.MutateMany(
{
{.data = {0, 1, 2, 3, 4, 5, 6, 7}},
{.data = {100, 101, 102, 103}},
{/*data=*/{0, 1, 2, 3, 4, 5, 6, 7}},
{/*data=*/{100, 101, 102, 103}},
},
kNumMutantsToGenerate, mutants);

Expand Down Expand Up @@ -182,9 +182,10 @@ TEST_P(MutationStepTest, GeneratesExpectedMutantsAndAvoidsUnexpectedMutants) {
absl::flat_hash_set<ByteArray> unmatched_expected_mutants =
GetParam().expected_mutants;
const auto& unexpected_mutants = GetParam().unexpected_mutants;
ExecutionMetadata metadata = {.cmp_data = GetParam().cmp_data};
ExecutionMetadata metadata;
metadata.cmp_data = GetParam().cmp_data;
const std::vector<MutationInputRef> inputs = {
{.data = GetParam().seed_input, .metadata = &metadata}};
{/*data=*/GetParam().seed_input, /*metadata=*/&metadata}};
std::vector<ByteArray> mutants;
for (size_t i = 0; i < GetParam().max_num_iterations; i++) {
mutator.MutateMany(inputs, 1, mutants);
Expand All @@ -200,124 +201,116 @@ TEST_P(MutationStepTest, GeneratesExpectedMutantsAndAvoidsUnexpectedMutants) {
EXPECT_TRUE(unmatched_expected_mutants.empty());
}

INSTANTIATE_TEST_SUITE_P(InsertByteUpToMaxLen, MutationStepTest,
Values(MutationStepTestParameter{
.seed_input = {0, 1, 2},
.expected_mutants =
{
{0, 1, 2, 3},
{0, 3, 1, 2},
{3, 0, 1, 2},
},
.unexpected_mutants =
{
{0, 1, 2, 3, 4},
{0, 3, 4, 1, 2},
{3, 4, 0, 1, 2},
},
.max_len = 4,
}));
INSTANTIATE_TEST_SUITE_P(InsertByteUpToMaxLen, MutationStepTest, Values([]() {
MutationStepTestParameter params;
params.seed_input = {0, 1, 2};
params.expected_mutants = {
{0, 1, 2, 3},
{0, 3, 1, 2},
{3, 0, 1, 2},
};
params.unexpected_mutants = {
{0, 1, 2, 3, 4},
{0, 3, 4, 1, 2},
{3, 4, 0, 1, 2},
};
params.max_len = 4;
return params;
}()));

INSTANTIATE_TEST_SUITE_P(OverwriteFromDictionary, MutationStepTest,
Values(MutationStepTestParameter{
.seed_input = {1, 2, 3, 4, 5},
.expected_mutants =
{
{1, 2, 7, 8, 9},
{1, 7, 8, 9, 5},
{7, 8, 9, 4, 5},
{1, 2, 3, 0, 6},
{1, 2, 0, 6, 5},
{1, 0, 6, 4, 5},
{0, 6, 3, 4, 5},
{42, 2, 3, 4, 5},
{1, 42, 3, 4, 5},
{1, 2, 42, 4, 5},
{1, 2, 3, 42, 5},
{1, 2, 3, 4, 42},
},
.dictionary =
{
{7, 8, 9},
{0, 6},
{42},
},
}));
Values([]() {
MutationStepTestParameter params;
params.seed_input = {1, 2, 3, 4, 5};
params.expected_mutants = {
{1, 2, 7, 8, 9}, {1, 7, 8, 9, 5},
{7, 8, 9, 4, 5}, {1, 2, 3, 0, 6},
{1, 2, 0, 6, 5}, {1, 0, 6, 4, 5},
{0, 6, 3, 4, 5}, {42, 2, 3, 4, 5},
{1, 42, 3, 4, 5}, {1, 2, 42, 4, 5},
{1, 2, 3, 42, 5}, {1, 2, 3, 4, 42},
};
params.dictionary = {
{7, 8, 9},
{0, 6},
{42},
};
return params;
}()));

INSTANTIATE_TEST_SUITE_P(
OverwriteFromCmpDictionary, MutationStepTest,
Values(MutationStepTestParameter{
.seed_input = {1, 2, 40, 50, 60},
.expected_mutants =
{
{3, 4, 40, 50, 60},
{1, 2, 10, 20, 30},
},
.cmp_data = {/*size*/ 2, /*lhs*/ 1, 2, /*rhs*/ 3, 4, /*size*/ 3,
/*lhs*/ 10, 20, 30, /*rhs*/ 40, 50, 60},
}));
INSTANTIATE_TEST_SUITE_P(OverwriteFromCmpDictionary, MutationStepTest,
Values([]() {
MutationStepTestParameter params;
params.seed_input = {1, 2, 40, 50, 60};
params.expected_mutants = {
{3, 4, 40, 50, 60},
{1, 2, 10, 20, 30},
};
params.cmp_data = {2, // size
1, 2, // lhs
3, 4, // rhs
3, // size
10, 20, 30, // lhs
40, 50, 60}; // rhs
return params;
}()));

INSTANTIATE_TEST_SUITE_P(InsertFromDictionary, MutationStepTest,
Values(MutationStepTestParameter{
.seed_input = {1, 2, 3},
.expected_mutants =
{
{1, 2, 3, 4, 5},
{1, 2, 4, 5, 3},
{1, 4, 5, 2, 3},
{4, 5, 1, 2, 3},
{1, 2, 3, 6, 7, 8},
{1, 2, 6, 7, 8, 3},
{1, 6, 7, 8, 2, 3},
{6, 7, 8, 1, 2, 3},
},
.dictionary =
{
{4, 5},
{6, 7, 8},
},
}));
INSTANTIATE_TEST_SUITE_P(InsertFromDictionary, MutationStepTest, Values([]() {
MutationStepTestParameter params;
params.seed_input = {1, 2, 3};
params.expected_mutants = {
{1, 2, 3, 4, 5}, {1, 2, 4, 5, 3},
{1, 4, 5, 2, 3}, {4, 5, 1, 2, 3},
{1, 2, 3, 6, 7, 8}, {1, 2, 6, 7, 8, 3},
{1, 6, 7, 8, 2, 3}, {6, 7, 8, 1, 2, 3},
};
params.dictionary = {
{4, 5},
{6, 7, 8},
};
return params;
}()));

INSTANTIATE_TEST_SUITE_P(InsertFromCmpDictionary, MutationStepTest,
Values(MutationStepTestParameter{
.seed_input = {1, 2, 3},
.expected_mutants =
{
{1, 2, 3, 4, 5},
{1, 2, 4, 5, 3},
{1, 4, 5, 2, 3},
{4, 5, 1, 2, 3},
{1, 2, 3, 6, 7, 8},
{1, 2, 6, 7, 8, 3},
{1, 6, 7, 8, 2, 3},
{6, 7, 8, 1, 2, 3},
},
.cmp_data = {/*size*/ 2, /*lhs*/ 4, 5, /*rhs*/ 4,
5, /*size*/ 3,
/*lhs*/ 6, 7, 8, /*rhs*/ 6, 7, 8},
}));
Values([]() {
MutationStepTestParameter params;
params.seed_input = {1, 2, 3};
params.expected_mutants = {
{1, 2, 3, 4, 5}, {1, 2, 4, 5, 3},
{1, 4, 5, 2, 3}, {4, 5, 1, 2, 3},
{1, 2, 3, 6, 7, 8}, {1, 2, 6, 7, 8, 3},
{1, 6, 7, 8, 2, 3}, {6, 7, 8, 1, 2, 3},
};
params.cmp_data = {2, // size
4, 5, // lhs
4, 5, // rhs
3, // size
6, 7, 8, // lhs
6, 7, 8}; // rhs
return params;
}()));

INSTANTIATE_TEST_SUITE_P(
SkipsLongCmpEntry, MutationStepTest,
Values(MutationStepTestParameter{
.seed_input = {0},
.expected_mutants =
{
{0, 1, 2, 3, 4},
},
.unexpected_mutants =
{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
},
.cmp_data = {/*size*/ 20, /*lhs*/ 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20,
/*rhs*/ 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20,
/*size*/ 4, /*lhs*/ 1, 2, 3, 4, /*rhs*/ 1, 2,
3, 4}}));
INSTANTIATE_TEST_SUITE_P(SkipsLongCmpEntry, MutationStepTest, Values([]() {
MutationStepTestParameter params;
params.seed_input = {0};
params.expected_mutants = {
{0, 1, 2, 3, 4},
};
params.unexpected_mutants = {
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
};
params.cmp_data = {
20, // size
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, // lhs
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, // rhs
4, // size
1, 2, 3, 4, // lhs
1, 2, 3, 4}; // rhs
return params;
}()));

} // namespace

Expand Down

0 comments on commit b44c412

Please sign in to comment.