Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GCC/Clang builtins for overflows checking #4447

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions librz/include/rz_types_overflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@
#define RZ_TYPES_OVERFLOW_H

#include <rz_types.h>
#include <rz_userconf.h>

// TODO: Use CLANG/GCC builtins if available: __builtin_mul_overflow
// Using compiler builtins when available

// ADD
// if ((x > 0) && (a > INT_MAX - x)) /* `a + x` would overflow */;
// if ((x < 0) && (a < INT_MIN - x)) /* `a + x` would underflow */;
#if HAVE___BUILTIN_ADD_OVERFLOW
#define SZT_ADD_OVFCHK(x, y) __builtin_add_overflow_p (x, y, (__typeof__ ((x) + (y))) 0)
#define SZT_ADD_OVFCHK(x, y) __builtin_add_overflow_p (x, y, (__typeof__ ((x) + (y))) 0)
#define SSZT_ADD_OVFCHK(a, x) __builtin_add_overflow_p (a, x, (__typeof__ ((a) + (x))) 0)
#define UT64_ADD_OVFCHK(x, y) __builtin_add_overflow_p (x, y, (__typeof__ ((x) + (y))) 0)
#define ST64_ADD_OVFCHK(a, x) __builtin_add_overflow_p (a, x, (__typeof__ ((a) + (x))) 0)
#define UT32_ADD_OVFCHK(x, y) __builtin_add_overflow_p (x, y, (__typeof__ ((x) + (y))) 0)
#define ST32_ADD_OVFCHK(a, x) __builtin_add_overflow_p (a, x, (__typeof__ ((a) + (x))) 0)
#define UT16_ADD_OVFCHK(x, y) __builtin_add_overflow_p (x, y, (__typeof__ ((x) + (y))) 0)
#define ST16_ADD_OVFCHK(a, b) __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
#define UT8_ADD_OVFCHK(x, y) __builtin_add_overflow_p (x, y, (__typeof__ ((x) + (y))) 0)
#define ST8_ADD_OVFCHK(a, x) __builtin_add_overflow_p (a, x, (__typeof__ ((a) + (x))) 0)
#else
#define SZT_ADD_OVFCHK(x, y) ((SIZE_MAX - (x)) < (y))
#define SSZT_ADD_OVFCHK(a, x) ((((x) > 0) && ((a) > SSIZE_MAX - (x))) || (((x) < 0) && (a) < SSIZE_MIN - (x)))
#define UT64_ADD_OVFCHK(x, y) ((UT64_MAX - (x)) < (y))
Expand All @@ -18,10 +30,21 @@
#define ST16_ADD_OVFCHK(a, b) ((((b) > 0) && ((a) > ST16_MAX - (b))) || (((b) < 0) && ((a) < ST16_MIN - (b))))
#define UT8_ADD_OVFCHK(x, y) ((UT8_MAX - (x)) < (y))
#define ST8_ADD_OVFCHK(a, x) ((((x) > 0) && ((a) > ST8_MAX - (x))) || ((x) < 0 && (a) < ST8_MIN - (x)))
#endif

// SUB
// if ((x < 0) && (a > INT_MAX + x)) /* `a - x` would overflow */;
// if ((x > 0) && (a < INT_MIN + x)) /* `a - x` would underflow */;
#if HAVE___BUILTIN_SUB_OVERFLOW
#define SZT_SUB_OVFCHK(a, b) __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
#define SSZT_SUB_OVFCHK(a, b) __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
#define UT64_SUB_OVFCHK(a, b) __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
#define ST64_SUB_OVFCHK(a, b) __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
#define UT32_SUB_OVFCHK(a, b) __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
#define ST32_SUB_OVFCHK(a, b) __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
#define UT16_SUB_OVFCHK(a, b) __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
#define ST16_SUB_OVFCHK(a, b) __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
#define UT8_SUB_OVFCHK(a, b) __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
#define ST8_SUB_OVFCHK(a, b) __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
#else
#define SZT_SUB_OVFCHK(a, b) SZT_ADD_OVFCHK(a, -(b))
#define SSZT_SUB_OVFCHK(a, b) SSZT_ADD_OVFCHK(a, -(b))
#define UT64_SUB_OVFCHK(a, b) UT64_ADD_OVFCHK(a, -(b))
Expand All @@ -32,13 +55,21 @@
#define ST16_SUB_OVFCHK(a, b) ST16_ADD_OVFCHK(a, -(b))
#define UT8_SUB_OVFCHK(a, b) UT8_ADD_OVFCHK(a, -(b))
#define ST8_SUB_OVFCHK(a, b) ST8_ADD_OVFCHK(a, -(b))
#endif

// MUL
#define UNSIGNED_MUL_OVERFLOW_CHECK(overflow_name, type_base, type_min, type_max) \
static inline bool overflow_name(type_base a, type_base b) { \
return (a > 0 && b > 0 && a > type_max / b); \
}


#if HAVE___BUILTIN_MUL_OVERFLOW
#define SIGNED_MUL_OVERFLOW_CHECK(overflow_name, type_base, type_min, type_max) \
static inline bool overflow_name(type_base a, type_base b) { \
return __builtin_mul_overflow_p(a, b, (__typeof__ ((a) * (b))) 0); \
}
#else
#define SIGNED_MUL_OVERFLOW_CHECK(overflow_name, type_base, type_min, type_max) \
static inline bool overflow_name(type_base a, type_base b) { \
if (a > 0) { \
Expand All @@ -52,11 +83,13 @@
} \
return a && b < type_max / a; \
}
#endif

#define SIGNED_DIV_OVERFLOW_CHECK(overflow_name, type_base, type_mid, type_max) \
static inline bool overflow_name(type_base a, type_base b) { \
return (!b || (a == type_mid && b == type_max)); \
}

#define UNSIGNED_DIV_OVERFLOW_CHECK(overflow_name, type_base, type_min, type_max) \
static inline bool overflow_name(type_base a, type_base b) { \
(void)a; \
Expand Down
3 changes: 3 additions & 0 deletions librz/include/rz_userconf.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
#define HAVE___BUILTIN_BSWAP32 @HAVE___BUILTIN_BSWAP32@
#define HAVE___BUILTIN_BSWAP64 @HAVE___BUILTIN_BSWAP64@
#define HAVE___BUILTIN_CLZLL @HAVE___BUILTIN_CLZLL@
#define HAVE___BUILTIN_ADD_OVERFLOW @HAVE___BUILTIN_ADD_OVERFLOW_P@
#define HAVE___BUILTIN_SUB_OVERFLOW @HAVE___BUILTIN_SUB_OVERFLOW_P@
#define HAVE___BUILTIN_MUL_OVERFLOW @HAVE___BUILTIN_MUL_OVERFLOW_P@
#define HAVE_POSIX_MEMALIGN @HAVE_POSIX_MEMALIGN@
#define HAVE__ALIGNED_MALLOC @HAVE__ALIGNED_MALLOC@

Expand Down
3 changes: 3 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ foreach it : ccs
['__builtin_bswap32', '', []],
['__builtin_bswap64', '', []],
['__builtin_clzll', '', []],
['__builtin_add_overflow_p', '', []],
['__builtin_sub_overflow_p', '', []],
['__builtin_mul_overflow_p', '', []],
['posix_memalign', '#include <stdlib.h>', []],
['_aligned_malloc', '#include <malloc.h>', []],
]
Expand Down
Loading