diff --git a/src/config/CMakeLists.txt b/src/config/CMakeLists.txt --- a/src/config/CMakeLists.txt +++ b/src/config/CMakeLists.txt @@ -55,15 +55,15 @@ check_include_files("sys/select.h" HAVE_SYS_SELECT_H) check_include_files("sys/prctl.h" HAVE_SYS_PRCTL_H) -# Bitmanip intrinsics -function(check_builtin_exist SYMBOL VARIABLE) +# Built-in compiler intrinsics +function(check_builtin_exist_with_code SYMBOL VARIABLE CODE) set( SOURCE_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckBuiltinExists.c" ) set( CMAKE_CONFIGURABLE_FILE_CONTENT - "int main(int argc, char** argv) { (void)argv; return ${SYMBOL}(argc); }\n" + "int main(int argc, char** argv) { (void)argv; return ${CODE}; }\n" ) configure_file( "${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in" @@ -101,10 +101,26 @@ endif() endfunction() +function(check_builtin_exist SYMBOL VARIABLE) + check_builtin_exist_with_code( + ${SYMBOL} + ${VARIABLE} + "${SYMBOL}(argc)" + ) +endfunction() + +# Bitmanip intrinsics check_builtin_exist(__builtin_clz HAVE_DECL___BUILTIN_CLZ) check_builtin_exist(__builtin_clzl HAVE_DECL___BUILTIN_CLZL) check_builtin_exist(__builtin_clzll HAVE_DECL___BUILTIN_CLZLL) check_builtin_exist(__builtin_popcount HAVE_DECL___BUILTIN_POPCOUNT) +# Overflow math +check_builtin_exist_with_code( + __builtin_saddll_overflow HAVE_DECL___BUILTIN_SADDLL_OVERFLOW + "__builtin_saddll_overflow(argc, argc, (long long int*)&argc)") +check_builtin_exist_with_code( + __builtin_ssubll_overflow HAVE_DECL___BUILTIN_SSUBLL_OVERFLOW + "__builtin_ssubll_overflow(argc, argc, (long long int*)&argc)") # Memory management capabilities check_symbol_exists(M_ARENA_MAX "malloc.h" HAVE_MALLOPT_ARENA_MAX) diff --git a/src/config/bitcoin-config.h.cmake.in b/src/config/bitcoin-config.h.cmake.in --- a/src/config/bitcoin-config.h.cmake.in +++ b/src/config/bitcoin-config.h.cmake.in @@ -41,6 +41,8 @@ #cmakedefine HAVE_DECL___BUILTIN_CLZL 1 #cmakedefine HAVE_DECL___BUILTIN_CLZLL 1 #cmakedefine HAVE_DECL___BUILTIN_POPCOUNT 1 +#cmakedefine HAVE_DECL___BUILTIN_SADDLL_OVERFLOW 1 +#cmakedefine HAVE_DECL___BUILTIN_SSUBLL_OVERFLOW 1 #cmakedefine HAVE_MALLOPT_ARENA_MAX 1 #cmakedefine HAVE_MALLOC_INFO 1 diff --git a/src/script/intmath.h b/src/script/intmath.h --- a/src/script/intmath.h +++ b/src/script/intmath.h @@ -7,6 +7,11 @@ #include #include +static_assert(std::numeric_limits::max() == + std::numeric_limits::max()); +static_assert(std::numeric_limits::min() == + std::numeric_limits::min()); + /** * Computes a + b and stores it in result. If the sum overflows or results in an * invalid 63+sign-bit integer, return true, otherwise false. Overflow checks @@ -25,6 +30,23 @@ return result == std::numeric_limits::min(); } +/** + * Computes a + b and stores it in result. If the sum overflows or results in an + * invalid 63+sign-bit integer, return true, otherwise false. + * Uses built-in overflow functions if available, and emulated overflow math + * otherwise. + */ +static bool AddInt63Overflow(int64_t a, int64_t b, int64_t &result) { +#if HAVE_DECL___BUILTIN_SADDLL_OVERFLOW + if (__builtin_saddll_overflow(a, b, (long long int *)&result)) { + return true; + } + return result == std::numeric_limits::min(); +#else + return AddInt63OverflowEmulated(a, b, result); +#endif +} + /** * Computes a - b and stores it in result. If the difference overflows or * results in an invalid 63+sign-bit integer, return true, otherwise false. @@ -43,4 +65,21 @@ return result == std::numeric_limits::min(); } +/** + * Computes a - b and stores it in result. If the sum difference or results in + * an invalid 63+sign-bit integer, return true, otherwise false. + * Uses built-in overflow functions if available, and emulated overflow math + * otherwise. + */ +static bool SubInt63Overflow(int64_t a, int64_t b, int64_t &result) { +#if HAVE_DECL___BUILTIN_SSUBLL_OVERFLOW + if (__builtin_ssubll_overflow(a, b, (long long int *)&result)) { + return true; + } + return result == std::numeric_limits::min(); +#else + return SubInt63OverflowEmulated(a, b, result); +#endif +} + #endif // BITCOIN_SCRIPT_INTMATH_H diff --git a/src/test/intmath_tests.cpp b/src/test/intmath_tests.cpp --- a/src/test/intmath_tests.cpp +++ b/src/test/intmath_tests.cpp @@ -110,6 +110,34 @@ BOOST_CHECK_EQUAL(result, a - b); } } + { + // Test AddInt63Overflow + int64_t result; + bool expect_overflow = !IsInScriptBounds(a + b); + bool had_overflow = AddInt63Overflow(a_64, b_64, result); + if (expect_overflow) { + BOOST_CHECK_MESSAGE( + had_overflow, strprintf("%d + %d didn't overflow", a_64, b_64)); + } else { + BOOST_CHECK_MESSAGE(!had_overflow, + strprintf("%d + %d overflowed", a_64, b_64)); + BOOST_CHECK_EQUAL(result, a + b); + } + } + { + // Test SubInt63Overflow + int64_t result; + bool expect_overflow = !IsInScriptBounds(a - b); + bool had_overflow = SubInt63Overflow(a_64, b_64, result); + if (expect_overflow) { + BOOST_CHECK_MESSAGE( + had_overflow, strprintf("%d - %d didn't overflow", a_64, b_64)); + } else { + BOOST_CHECK_MESSAGE(!had_overflow, + strprintf("%d - %d overflowed", a_64, b_64)); + BOOST_CHECK_EQUAL(result, a - b); + } + } } /** Generate random number in [-2^63; 2^63]. */