2020-04-07 16:31:43 +00:00
|
|
|
// 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>
|
2021-11-02 09:48:10 +01:00
|
|
|
#include <util/overflow.h>
|
2020-04-07 16:31:43 +00:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#if defined(__has_builtin)
|
|
|
|
#if __has_builtin(__builtin_add_overflow)
|
|
|
|
#define HAVE_BUILTIN_ADD_OVERFLOW
|
|
|
|
#endif
|
2020-11-25 14:25:56 +02:00
|
|
|
#elif defined(__GNUC__)
|
2020-04-07 16:31:43 +00:00
|
|
|
#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);
|
2022-02-01 13:43:42 +01:00
|
|
|
const auto maybe_add{CheckedAdd(i, j)};
|
|
|
|
const auto sat_add{SaturatingAdd(i, j)};
|
|
|
|
assert(is_addition_overflow_custom == !maybe_add.has_value());
|
|
|
|
assert(is_addition_overflow_custom == AdditionOverflow(j, i));
|
|
|
|
assert(maybe_add == CheckedAdd(j, i));
|
|
|
|
assert(sat_add == SaturatingAdd(j, i));
|
2020-04-07 16:31:43 +00:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
#endif
|
2022-02-01 13:43:42 +01:00
|
|
|
if (is_addition_overflow_custom) {
|
|
|
|
assert(sat_add == std::numeric_limits<T>::min() || sat_add == std::numeric_limits<T>::max());
|
|
|
|
} else {
|
|
|
|
const auto add{i + j};
|
|
|
|
assert(add == maybe_add.value());
|
|
|
|
assert(add == sat_add);
|
|
|
|
}
|
2020-04-07 16:31:43 +00:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2020-12-03 16:42:49 +01:00
|
|
|
FUZZ_TARGET(addition_overflow)
|
2020-04-07 16:31:43 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|