mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 22:50:59 +01:00
tests: Add fuzzing harness for AdditionOverflow(...)
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
FUZZ_TARGETS = \
|
FUZZ_TARGETS = \
|
||||||
|
test/fuzz/addition_overflow \
|
||||||
test/fuzz/addr_info_deserialize \
|
test/fuzz/addr_info_deserialize \
|
||||||
test/fuzz/addrdb \
|
test/fuzz/addrdb \
|
||||||
test/fuzz/address_deserialize \
|
test/fuzz/address_deserialize \
|
||||||
@@ -280,6 +281,12 @@ endif
|
|||||||
|
|
||||||
if ENABLE_FUZZ
|
if ENABLE_FUZZ
|
||||||
|
|
||||||
|
test_fuzz_addition_overflow_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
|
||||||
|
test_fuzz_addition_overflow_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
||||||
|
test_fuzz_addition_overflow_LDADD = $(FUZZ_SUITE_LD_COMMON)
|
||||||
|
test_fuzz_addition_overflow_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
|
||||||
|
test_fuzz_addition_overflow_SOURCES = test/fuzz/addition_overflow.cpp
|
||||||
|
|
||||||
test_fuzz_addr_info_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DADDR_INFO_DESERIALIZE=1
|
test_fuzz_addr_info_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DADDR_INFO_DESERIALIZE=1
|
||||||
test_fuzz_addr_info_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
test_fuzz_addr_info_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
||||||
test_fuzz_addr_info_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
|
test_fuzz_addr_info_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
|
||||||
|
|||||||
55
src/test/fuzz/addition_overflow.cpp
Normal file
55
src/test/fuzz/addition_overflow.cpp
Normal file
@@ -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);
|
||||||
|
}
|
||||||
@@ -120,4 +120,15 @@ NODISCARD bool MultiplicationOverflow(const T i, const T j) noexcept
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
NODISCARD bool AdditionOverflow(const T i, const T j) noexcept
|
||||||
|
{
|
||||||
|
static_assert(std::is_integral<T>::value, "Integral required.");
|
||||||
|
if (std::numeric_limits<T>::is_signed) {
|
||||||
|
return (i > 0 && j > std::numeric_limits<T>::max() - i) ||
|
||||||
|
(i < 0 && j < std::numeric_limits<T>::min() - i);
|
||||||
|
}
|
||||||
|
return std::numeric_limits<T>::max() - i < j;
|
||||||
|
}
|
||||||
|
|
||||||
#endif // BITCOIN_TEST_FUZZ_UTIL_H
|
#endif // BITCOIN_TEST_FUZZ_UTIL_H
|
||||||
|
|||||||
Reference in New Issue
Block a user