forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge bitcoin#18565: Add fuzzing harnesses for classes/functions in c…
…heckqueue.h and cuckoocache.h. Add fuzzing coverage.
- Loading branch information
Showing
7 changed files
with
216 additions
and
2 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,55 @@ | ||
// Copyright (c) 2020 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <test/fuzz/fuzz.h> | ||
#include <test/fuzz/util.h> | ||
|
||
#include <cstdint> | ||
#include <string> | ||
#include <vector> | ||
|
||
#if defined(__has_builtin) | ||
#if __has_builtin(__builtin_add_overflow) | ||
#define HAVE_BUILTIN_ADD_OVERFLOW | ||
#endif | ||
#elif defined(__GNUC__) && (__GNUC__ >= 5) | ||
#define HAVE_BUILTIN_ADD_OVERFLOW | ||
#endif | ||
|
||
namespace { | ||
template <typename T> | ||
void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider) | ||
{ | ||
const T i = fuzzed_data_provider.ConsumeIntegral<T>(); | ||
const T j = fuzzed_data_provider.ConsumeIntegral<T>(); | ||
const bool is_addition_overflow_custom = AdditionOverflow(i, j); | ||
#if defined(HAVE_BUILTIN_ADD_OVERFLOW) | ||
T result_builtin; | ||
const bool is_addition_overflow_builtin = __builtin_add_overflow(i, j, &result_builtin); | ||
assert(is_addition_overflow_custom == is_addition_overflow_builtin); | ||
if (!is_addition_overflow_custom) { | ||
assert(i + j == result_builtin); | ||
} | ||
#else | ||
if (!is_addition_overflow_custom) { | ||
(void)(i + j); | ||
} | ||
#endif | ||
} | ||
} // namespace | ||
|
||
void test_one_input(const std::vector<uint8_t>& buffer) | ||
{ | ||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); | ||
TestAdditionOverflow<int64_t>(fuzzed_data_provider); | ||
TestAdditionOverflow<uint64_t>(fuzzed_data_provider); | ||
TestAdditionOverflow<int32_t>(fuzzed_data_provider); | ||
TestAdditionOverflow<uint32_t>(fuzzed_data_provider); | ||
TestAdditionOverflow<int16_t>(fuzzed_data_provider); | ||
TestAdditionOverflow<uint16_t>(fuzzed_data_provider); | ||
TestAdditionOverflow<char>(fuzzed_data_provider); | ||
TestAdditionOverflow<unsigned char>(fuzzed_data_provider); | ||
TestAdditionOverflow<signed char>(fuzzed_data_provider); | ||
} |
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,65 @@ | ||
// Copyright (c) 2020 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <checkqueue.h> | ||
#include <optional.h> | ||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <test/fuzz/fuzz.h> | ||
#include <test/fuzz/util.h> | ||
|
||
#include <cstdint> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace { | ||
struct DumbCheck { | ||
const bool result = false; | ||
|
||
DumbCheck() = default; | ||
|
||
explicit DumbCheck(const bool _result) : result(_result) | ||
{ | ||
} | ||
|
||
bool operator()() const | ||
{ | ||
return result; | ||
} | ||
|
||
void swap(DumbCheck& x) | ||
{ | ||
} | ||
}; | ||
} // namespace | ||
|
||
void test_one_input(const std::vector<uint8_t>& buffer) | ||
{ | ||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); | ||
|
||
const unsigned int batch_size = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 1024); | ||
CCheckQueue<DumbCheck> check_queue_1{batch_size}; | ||
CCheckQueue<DumbCheck> check_queue_2{batch_size}; | ||
std::vector<DumbCheck> checks_1; | ||
std::vector<DumbCheck> checks_2; | ||
const int size = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 1024); | ||
for (int i = 0; i < size; ++i) { | ||
const bool result = fuzzed_data_provider.ConsumeBool(); | ||
checks_1.emplace_back(result); | ||
checks_2.emplace_back(result); | ||
} | ||
if (fuzzed_data_provider.ConsumeBool()) { | ||
check_queue_1.Add(checks_1); | ||
} | ||
if (fuzzed_data_provider.ConsumeBool()) { | ||
(void)check_queue_1.Wait(); | ||
} | ||
|
||
CCheckQueueControl<DumbCheck> check_queue_control{&check_queue_2}; | ||
if (fuzzed_data_provider.ConsumeBool()) { | ||
check_queue_control.Add(checks_2); | ||
} | ||
if (fuzzed_data_provider.ConsumeBool()) { | ||
(void)check_queue_control.Wait(); | ||
} | ||
} |
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,49 @@ | ||
// Copyright (c) 2020 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <cuckoocache.h> | ||
#include <optional.h> | ||
#include <script/sigcache.h> | ||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <test/fuzz/fuzz.h> | ||
#include <test/fuzz/util.h> | ||
#include <test/util/setup_common.h> | ||
|
||
#include <cstdint> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace { | ||
FuzzedDataProvider* fuzzed_data_provider_ptr = nullptr; | ||
|
||
struct RandomHasher { | ||
template <uint8_t> | ||
uint32_t operator()(const bool& /* unused */) const | ||
{ | ||
assert(fuzzed_data_provider_ptr != nullptr); | ||
return fuzzed_data_provider_ptr->ConsumeIntegral<uint32_t>(); | ||
} | ||
}; | ||
} // namespace | ||
|
||
void test_one_input(const std::vector<uint8_t>& buffer) | ||
{ | ||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); | ||
fuzzed_data_provider_ptr = &fuzzed_data_provider; | ||
CuckooCache::cache<bool, RandomHasher> cuckoo_cache{}; | ||
if (fuzzed_data_provider.ConsumeBool()) { | ||
const size_t megabytes = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 16); | ||
cuckoo_cache.setup_bytes(megabytes << 20); | ||
} else { | ||
cuckoo_cache.setup(fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, 4096)); | ||
} | ||
while (fuzzed_data_provider.ConsumeBool()) { | ||
if (fuzzed_data_provider.ConsumeBool()) { | ||
cuckoo_cache.insert(fuzzed_data_provider.ConsumeBool()); | ||
} else { | ||
cuckoo_cache.contains(fuzzed_data_provider.ConsumeBool(), fuzzed_data_provider.ConsumeBool()); | ||
} | ||
} | ||
fuzzed_data_provider_ptr = nullptr; | ||
} |
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