FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

BUG: Avoid UB in safe_[add,sub,mul] helpers (#31396) by charris · Pull Request #31401 · numpy/numpy · GitHub

/ numpy Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .build  (1) .h  (1) .in  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 2 additions & 0 deletions numpy/_core/config.h.in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#mesondefine HAVE___BUILTIN_BSWAP64
#mesondefine HAVE___BUILTIN_EXPECT
#mesondefine HAVE___BUILTIN_MUL_OVERFLOW
#mesondefine HAVE___BUILTIN_ADD_OVERFLOW
#mesondefine HAVE___BUILTIN_SUB_OVERFLOW
#mesondefine HAVE___BUILTIN_PREFETCH

#mesondefine HAVE_ATTRIBUTE_OPTIMIZE_UNROLL_LOOPS
Expand Down
2 changes: 2 additions & 0 deletions numpy/_core/meson.build
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ optional_intrinsics = [
['__builtin_expect', '5, 0', [], []],
# Test `long long` for arm+clang 13 (gh-22811, but we use all versions):
['__builtin_mul_overflow', '(long long)5, 5, (int*)5', [], []],
['__builtin_add_overflow', '(long long)5, 5, (int*)5', [], []],
['__builtin_sub_overflow', '(long long)5, 5, (int*)5', [], []],
['__builtin_prefetch', '(float*)0, 0, 3', [], []],
]
foreach intrin: optional_intrinsics
Expand Down
51 changes: 48 additions & 3 deletions numpy/_core/src/common/npy_extint128.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,97 @@ typedef struct {
} npy_extint128_t;


/*
* Integer add/sub/mul with overflow checking.
*
* On overflow, *overflow_flag is set to 1 and the return value is
* unspecified (callers must not use it). The arithmetic itself is
* never performed on values that would overflow signed `npy_int64`,
* so these helpers are free of the signed-overflow undefined behavior
* that an unguarded `a + b` / `a - b` / `a * b` would have.
*
* `__builtin_{add,sub,mul}_overflow` are probed independently by the
* meson build (see `numpy/_core/meson.build`); fall back to a
* branch-and-skip implementation otherwise.
*/

/* Integer addition with overflow checking */
static inline npy_int64
safe_add(npy_int64 a, npy_int64 b, char *overflow_flag)
{
#ifdef HAVE___BUILTIN_ADD_OVERFLOW
npy_int64 result;
if (__builtin_add_overflow(a, b, &result)) {
*overflow_flag = 1;
}
return result;
#else
if (a > 0 && b > NPY_MAX_INT64 - a) {
*overflow_flag = 1;
return 0;
}
else if (a < 0 && b < NPY_MIN_INT64 - a) {
if (a < 0 && b < NPY_MIN_INT64 - a) {
*overflow_flag = 1;
return 0;
}
return a + b;
#endif
}


/* Integer subtraction with overflow checking */
static inline npy_int64
safe_sub(npy_int64 a, npy_int64 b, char *overflow_flag)
{
#ifdef HAVE___BUILTIN_SUB_OVERFLOW
npy_int64 result;
if (__builtin_sub_overflow(a, b, &result)) {
*overflow_flag = 1;
}
return result;
#else
if (a >= 0 && b < a - NPY_MAX_INT64) {
*overflow_flag = 1;
return 0;
}
else if (a < 0 && b > a - NPY_MIN_INT64) {
if (a < 0 && b > a - NPY_MIN_INT64) {
*overflow_flag = 1;
return 0;
}
return a - b;
#endif
}


/* Integer multiplication with overflow checking */
static inline npy_int64
safe_mul(npy_int64 a, npy_int64 b, char *overflow_flag)
{
#ifdef HAVE___BUILTIN_MUL_OVERFLOW
npy_int64 result;
if (__builtin_mul_overflow(a, b, &result)) {
*overflow_flag = 1;
}
return result;
#else
if (a > 0) {
if (b > NPY_MAX_INT64 / a || b < NPY_MIN_INT64 / a) {
*overflow_flag = 1;
return 0;
}
}
else if (a < 0) {
if (b > 0 && a < NPY_MIN_INT64 / b) {
*overflow_flag = 1;
return 0;
}
else if (b < 0 && a < NPY_MAX_INT64 / b) {
if (b < 0 && a < NPY_MAX_INT64 / b) {
*overflow_flag = 1;
return 0;
}
}
return a * b;
#endif
}


Expand Down
Loading

Back | FazBrowse Home | New Git URL