From 9fe30a1428277141b7b51569823b41c1e5f6d3b4 Mon Sep 17 00:00:00 2001 From: Go Kudo Date: Wed, 20 Jul 2022 19:50:43 +0900 Subject: [PATCH 1/2] random: suppress UBSan This is equivalent to the behavior of the original PHP API, and similar events occur in PHP 8.1 mt_rand() in PHPT test cases. > mt_rand(PHP_INT_MIN, PHP_INT_MAX); --- ext/random/random.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ext/random/random.c b/ext/random/random.c index f8a94e65ee34e..e081965d4bbe6 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -61,6 +61,14 @@ #if __has_feature(memory_sanitizer) # include +# define MSAN_UNPOISON(ptr, size) __msan_unpoison((ptr), (size)); +#else +# define MSAN_UNPOISON(ptr, size) +#endif +#if __has_feature(undefined_behavior_sanitizer) || ZEND_GCC_VERSION >= 4900 +# define UBSAN_SUPPRESS_SIGNED_INTEGER_OVERFLOW __attribute__((no_sanitize("signed-integer-overflow"))) +#else +# define UBSAN_SUPPRESS_SIGNED_INTEGER_OVERFLOW #endif #include "random_arginfo.h" @@ -317,7 +325,7 @@ PHPAPI zend_object *php_random_engine_common_clone_object(zend_object *object) } /* {{{ php_random_range */ -PHPAPI zend_long php_random_range(const php_random_algo *algo, php_random_status *status, zend_long min, zend_long max) +UBSAN_SUPPRESS_SIGNED_INTEGER_OVERFLOW PHPAPI zend_long php_random_range(const php_random_algo *algo, php_random_status *status, zend_long min, zend_long max) { zend_ulong umax = max - min; @@ -539,10 +547,8 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) } } -# if __has_feature(memory_sanitizer) /* MSan does not instrument manual syscall invocations. */ - __msan_unpoison(bytes + read_bytes, n); -# endif + MSAN_UNPOISON(bytes + read_bytes, n); read_bytes += (size_t) n; } # endif From 9eb0ae830f50e8b5a400fbea90c14e77154a50fb Mon Sep 17 00:00:00 2001 From: Go Kudo Date: Wed, 20 Jul 2022 20:09:04 +0900 Subject: [PATCH 2/2] fix: no_sanitize attribute requires GCC 8 or higher --- ext/random/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/random/random.c b/ext/random/random.c index e081965d4bbe6..796b84b4a0ab9 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -65,7 +65,7 @@ #else # define MSAN_UNPOISON(ptr, size) #endif -#if __has_feature(undefined_behavior_sanitizer) || ZEND_GCC_VERSION >= 4900 +#if __has_feature(undefined_behavior_sanitizer) || ZEND_GCC_VERSION >= 8000 # define UBSAN_SUPPRESS_SIGNED_INTEGER_OVERFLOW __attribute__((no_sanitize("signed-integer-overflow"))) #else # define UBSAN_SUPPRESS_SIGNED_INTEGER_OVERFLOW