Line data Source code
1 : // Copyright (c) 2020-2021 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 : 5 : #if defined(HAVE_CONFIG_H) 6 : #include <config/bitcoin-config.h> 7 : #endif 8 : 9 : #include <test/fuzz/FuzzedDataProvider.h> 10 : #include <test/fuzz/fuzz.h> 11 : #include <test/fuzz/util.h> 12 : 13 : #include <cstdint> 14 : #include <string> 15 : #include <vector> 16 : 17 : namespace { 18 : template <typename T> 19 0 : void TestMultiplicationOverflow(FuzzedDataProvider& fuzzed_data_provider) 20 : { 21 0 : const T i = fuzzed_data_provider.ConsumeIntegral<T>(); 22 0 : const T j = fuzzed_data_provider.ConsumeIntegral<T>(); 23 0 : const bool is_multiplication_overflow_custom = MultiplicationOverflow(i, j); 24 : #if defined(HAVE_BUILTIN_MUL_OVERFLOW) 25 2 : T result_builtin; 26 0 : const bool is_multiplication_overflow_builtin = __builtin_mul_overflow(i, j, &result_builtin); 27 0 : assert(is_multiplication_overflow_custom == is_multiplication_overflow_builtin); 28 0 : if (!is_multiplication_overflow_custom) { 29 0 : assert(i * j == result_builtin); 30 0 : } 31 : #else 32 : if (!is_multiplication_overflow_custom) { 33 : (void)(i * j); 34 : } 35 : #endif 36 0 : } 37 : } // namespace 38 : 39 4 : FUZZ_TARGET(multiplication_overflow) 40 : { 41 0 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); 42 0 : TestMultiplicationOverflow<int64_t>(fuzzed_data_provider); 43 0 : TestMultiplicationOverflow<uint64_t>(fuzzed_data_provider); 44 0 : TestMultiplicationOverflow<int32_t>(fuzzed_data_provider); 45 0 : TestMultiplicationOverflow<uint32_t>(fuzzed_data_provider); 46 0 : TestMultiplicationOverflow<int16_t>(fuzzed_data_provider); 47 0 : TestMultiplicationOverflow<uint16_t>(fuzzed_data_provider); 48 0 : TestMultiplicationOverflow<char>(fuzzed_data_provider); 49 0 : TestMultiplicationOverflow<unsigned char>(fuzzed_data_provider); 50 0 : TestMultiplicationOverflow<signed char>(fuzzed_data_provider); 51 0 : }