From b44c412aa80f57d5c7dce291e655de7234beea13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Szekeres?= Date: Mon, 3 Feb 2025 13:55:29 -0800 Subject: [PATCH] No public description PiperOrigin-RevId: 722786031 --- centipede/execution_metadata_test.cc | 31 ++-- centipede/fuzztest_mutator.cc | 17 +- centipede/fuzztest_mutator_test.cc | 239 +++++++++++++-------------- 3 files changed, 143 insertions(+), 144 deletions(-) diff --git a/centipede/execution_metadata_test.cc b/centipede/execution_metadata_test.cc index c7d7edd4..0aa78b22 100644 --- a/centipede/execution_metadata_test.cc +++ b/centipede/execution_metadata_test.cc @@ -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> enumeration_result; EXPECT_TRUE(metadata.ForEachCmpEntry( [&](ByteSpan a, ByteSpan b) { enumeration_result.emplace_back(a, b); })); @@ -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)); } diff --git a/centipede/fuzztest_mutator.cc b/centipede/fuzztest_mutator.cc index 3ea290d5..388fa56e 100644 --- a/centipede/fuzztest_mutator.cc +++ b/centipede/fuzztest_mutator.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -47,11 +48,9 @@ struct FuzzTestMutator::MutationMetadata { class FuzzTestMutator::MutatorDomain : public MutatorDomainBase { public: MutatorDomain() - : MutatorDomainBase(fuzztest::VectorOf(fuzztest::Arbitrary())) { - } + : MutatorDomainBase(fuzztest::VectorOf(fuzztest::Arbitrary())) {} - ~MutatorDomain() { - } + ~MutatorDomain() {} }; FuzzTestMutator::FuzzTestMutator(const Knobs &knobs, uint64_t seed) @@ -99,9 +98,9 @@ void FuzzTestMutator::CrossOver(ByteArray &data, const ByteArray &other) { } } -void FuzzTestMutator::MutateMany(const std::vector& inputs, +void FuzzTestMutator::MutateMany(const std::vector &inputs, size_t num_mutants, - std::vector& mutants) { + std::vector &mutants) { if (inputs.empty()) abort(); // TODO(xinhaoyuan): Consider metadata in other inputs instead of always the // first one. @@ -119,14 +118,14 @@ void FuzzTestMutator::MutateMany(const std::vector& 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; @@ -145,7 +144,7 @@ bool FuzzTestMutator::set_max_len(size_t max_len) { } void FuzzTestMutator::AddToDictionary( - const std::vector& dict_entries) { + const std::vector &dict_entries) { domain_->WithDictionary(dict_entries); } diff --git a/centipede/fuzztest_mutator_test.cc b/centipede/fuzztest_mutator_test.cc index 79862c31..1361afa1 100644 --- a/centipede/fuzztest_mutator_test.cc +++ b/centipede/fuzztest_mutator_test.cc @@ -46,7 +46,7 @@ TEST(FuzzTestMutator, DifferentRngSeedsLeadToDifferentMutantSequences) { std::vector res[2]; for (size_t i = 0; i < 2; i++) { ByteArray data = {0}; - std::vector mutation_inputs = {{.data = data}}; + std::vector mutation_inputs = {{data}}; std::vector mutants; constexpr size_t kMutantSequenceLength = 100; for (size_t iter = 0; iter < kMutantSequenceLength; iter++) { @@ -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); @@ -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); @@ -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); @@ -182,9 +182,10 @@ TEST_P(MutationStepTest, GeneratesExpectedMutantsAndAvoidsUnexpectedMutants) { absl::flat_hash_set 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 inputs = { - {.data = GetParam().seed_input, .metadata = &metadata}}; + {/*data=*/GetParam().seed_input, /*metadata=*/&metadata}}; std::vector mutants; for (size_t i = 0; i < GetParam().max_num_iterations; i++) { mutator.MutateMany(inputs, 1, mutants); @@ -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