LCOV - code coverage report
Current view: top level - src/test/fuzz - addition_overflow.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 2 35 5.7 %
Date: 2024-01-03 14:57:27 Functions: 1 12 8.3 %
Branches: 2 220 0.9 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2020-2022 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                 :            : #include <test/fuzz/FuzzedDataProvider.h>
       6                 :            : #include <test/fuzz/fuzz.h>
       7                 :            : #include <test/fuzz/util.h>
       8                 :            : #include <util/overflow.h>
       9                 :            : 
      10                 :            : #include <cstdint>
      11                 :            : #include <string>
      12                 :            : #include <vector>
      13                 :            : 
      14                 :            : #if defined(__has_builtin)
      15                 :            : #if __has_builtin(__builtin_add_overflow)
      16                 :            : #define HAVE_BUILTIN_ADD_OVERFLOW
      17                 :            : #endif
      18                 :            : #elif defined(__GNUC__)
      19                 :            : #define HAVE_BUILTIN_ADD_OVERFLOW
      20                 :            : #endif
      21                 :            : 
      22                 :            : namespace {
      23                 :            : template <typename T>
      24                 :          0 : void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider)
      25                 :            : {
      26                 :          0 :     const T i = fuzzed_data_provider.ConsumeIntegral<T>();
      27                 :          0 :     const T j = fuzzed_data_provider.ConsumeIntegral<T>();
      28                 :          0 :     const bool is_addition_overflow_custom = AdditionOverflow(i, j);
      29                 :          0 :     const auto maybe_add{CheckedAdd(i, j)};
      30                 :          2 :     const auto sat_add{SaturatingAdd(i, j)};
      31 [ #  # ][ #  # ]:          0 :     assert(is_addition_overflow_custom == !maybe_add.has_value());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      32 [ #  # ][ #  # ]:          0 :     assert(is_addition_overflow_custom == AdditionOverflow(j, i));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      33 [ #  # ][ #  # ]:          0 :     assert(maybe_add == CheckedAdd(j, i));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      34 [ #  # ][ #  # ]:          0 :     assert(sat_add == SaturatingAdd(j, i));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      35                 :            : #if defined(HAVE_BUILTIN_ADD_OVERFLOW)
      36                 :            :     T result_builtin;
      37                 :          0 :     const bool is_addition_overflow_builtin = __builtin_add_overflow(i, j, &result_builtin);
      38 [ #  # ][ #  # ]:          0 :     assert(is_addition_overflow_custom == is_addition_overflow_builtin);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      39 [ #  # ][ #  # ]:          0 :     if (!is_addition_overflow_custom) {
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      40 [ #  # ][ #  # ]:          0 :         assert(i + j == result_builtin);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      41                 :          0 :     }
      42                 :            : #endif
      43 [ #  # ][ #  # ]:          0 :     if (is_addition_overflow_custom) {
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      44 [ #  # ][ #  # ]:          0 :         assert(sat_add == std::numeric_limits<T>::min() || sat_add == std::numeric_limits<T>::max());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      45                 :          0 :     } else {
      46                 :          0 :         const auto add{i + j};
      47 [ #  # ][ #  # ]:          0 :         assert(add == maybe_add.value());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      48 [ #  # ][ #  # ]:          0 :         assert(add == sat_add);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      49                 :            :     }
      50                 :          0 : }
      51                 :            : } // namespace
      52                 :            : 
      53 [ +  - ][ +  - ]:          6 : FUZZ_TARGET(addition_overflow)
      54                 :            : {
      55                 :          0 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      56                 :          0 :     TestAdditionOverflow<int64_t>(fuzzed_data_provider);
      57                 :          0 :     TestAdditionOverflow<uint64_t>(fuzzed_data_provider);
      58                 :          0 :     TestAdditionOverflow<int32_t>(fuzzed_data_provider);
      59                 :          0 :     TestAdditionOverflow<uint32_t>(fuzzed_data_provider);
      60                 :          0 :     TestAdditionOverflow<int16_t>(fuzzed_data_provider);
      61                 :          0 :     TestAdditionOverflow<uint16_t>(fuzzed_data_provider);
      62                 :          0 :     TestAdditionOverflow<char>(fuzzed_data_provider);
      63                 :          0 :     TestAdditionOverflow<unsigned char>(fuzzed_data_provider);
      64                 :          0 :     TestAdditionOverflow<signed char>(fuzzed_data_provider);
      65                 :          0 : }

Generated by: LCOV version 1.14