Skip to content

[random] Suppress UBSan #9060

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

Closed
wants to merge 2 commits into from
Closed

Conversation

zeriyoshi
Copy link
Contributor

Undefined behavior is similarly detected at the time of mt_rand() in PHP 8.1.
This is detrimental to testing and should be suppressed in the code.

mt_rand(PHP_INT_MIN, PHP_INT_MAX);

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);
@zeriyoshi zeriyoshi changed the title random: Suppress UBSan [random] Suppress UBSan Jul 20, 2022
@zeriyoshi
Copy link
Contributor Author

@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not avoid this undefined behavior by using unsigned integers and only then casting them to signed? I think all we might need is:

return (zend_long) (rand_range64(algo, status, umax) + min);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related, php_random_int does umax = (zend_ulong) max - (zend_ulong) min;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iluuu1994 @guilliamxavier
I had BC break concerns about this, but apparently my fears were unfounded. I will correct it to the appropriate form. Thanks!

$ docker run --rm -it arm64v8/php:8.1-cli -r 'mt_srand(1234); echo mt_rand(PHP_INT_MIN, PHP_INT_MAX) . PHP_EOL;'
-5690461752414248237
$ docker run --rm -it amd64/php:8.1-cli -r 'mt_srand(1234); echo mt_rand(PHP_INT_MIN, PHP_INT_MAX) . PHP_EOL;'
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
-5690461752414248237

@@ -61,6 +61,14 @@

#if __has_feature(memory_sanitizer)
# include <sanitizer/msan_interface.h>
# define MSAN_UNPOISON(ptr, size) __msan_unpoison((ptr), (size));
Copy link
Contributor

@guilliamxavier guilliamxavier Jul 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing semicolon to remove? (but why touch this part anyway?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because we initially used a similar macro branch. Revert back to the original.

@zeriyoshi
Copy link
Contributor Author

Having found the original solution to be inadequate, close and remediate this PR.
Thank you!

@zeriyoshi zeriyoshi closed this Jul 20, 2022
@zeriyoshi zeriyoshi deleted the upstream/random_ubsan branch July 20, 2022 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants