Bitcoin Core  22.99.0
P2P Digital Currency
addition_overflow.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
6 #include <test/fuzz/fuzz.h>
7 #include <test/fuzz/util.h>
8 
9 #include <cstdint>
10 #include <string>
11 #include <vector>
12 
13 #if defined(__has_builtin)
14 #if __has_builtin(__builtin_add_overflow)
15 #define HAVE_BUILTIN_ADD_OVERFLOW
16 #endif
17 #elif defined(__GNUC__)
18 #define HAVE_BUILTIN_ADD_OVERFLOW
19 #endif
20 
21 namespace {
22 template <typename T>
23 void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider)
24 {
25  const T i = fuzzed_data_provider.ConsumeIntegral<T>();
26  const T j = fuzzed_data_provider.ConsumeIntegral<T>();
27  const bool is_addition_overflow_custom = AdditionOverflow(i, j);
28 #if defined(HAVE_BUILTIN_ADD_OVERFLOW)
29  T result_builtin;
30  const bool is_addition_overflow_builtin = __builtin_add_overflow(i, j, &result_builtin);
31  assert(is_addition_overflow_custom == is_addition_overflow_builtin);
32  if (!is_addition_overflow_custom) {
33  assert(i + j == result_builtin);
34  }
35 #else
36  if (!is_addition_overflow_custom) {
37  (void)(i + j);
38  }
39 #endif
40 }
41 } // namespace
42 
43 FUZZ_TARGET(addition_overflow)
44 {
45  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
46  TestAdditionOverflow<int64_t>(fuzzed_data_provider);
47  TestAdditionOverflow<uint64_t>(fuzzed_data_provider);
48  TestAdditionOverflow<int32_t>(fuzzed_data_provider);
49  TestAdditionOverflow<uint32_t>(fuzzed_data_provider);
50  TestAdditionOverflow<int16_t>(fuzzed_data_provider);
51  TestAdditionOverflow<uint16_t>(fuzzed_data_provider);
52  TestAdditionOverflow<char>(fuzzed_data_provider);
53  TestAdditionOverflow<unsigned char>(fuzzed_data_provider);
54  TestAdditionOverflow<signed char>(fuzzed_data_provider);
55 }
assert
assert(!tx.IsCoinBase())
util.h
AdditionOverflow
bool AdditionOverflow(const T i, const T j) noexcept
Definition: util.h:195
FUZZ_TARGET
FUZZ_TARGET(addition_overflow)
Definition: addition_overflow.cpp:43
FuzzedDataProvider.h
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
T
#define T(expected, seed, data)