From 76e1e1b803cbb50059dfac069d623b97995cf9e1 Mon Sep 17 00:00:00 2001 From: zeriyoshi Date: Wed, 20 Jul 2022 00:59:29 +0900 Subject: [PATCH 1/7] random: split Randomizer::getInt() without argument to Randomizer::nextInt() Since argument overloading is not safe for reflection, the method needed to be split appropriately. --- ext/random/random.stub.php | 4 ++- ext/random/random_arginfo.h | 8 +++-- ext/random/randomizer.c | 36 +++++++++++-------- ext/random/tests/03_randomizer/basic.phpt | 12 +++++++ .../tests/03_randomizer/compatibility_mt.phpt | 4 +-- .../03_randomizer/compatibility_user.phpt | 12 +++---- 6 files changed, 51 insertions(+), 25 deletions(-) diff --git a/ext/random/random.stub.php b/ext/random/random.stub.php index fd0bfe8780635..827c209325b01 100644 --- a/ext/random/random.stub.php +++ b/ext/random/random.stub.php @@ -131,7 +131,9 @@ final class Randomizer public function __construct(?Engine $engine = null) {} - public function getInt(int $min = UNKNOWN, int $max = UNKNOWN): int {} + public function nextInt(): int {} + + public function getInt(int $min, int $max): int {} public function getBytes(int $length): string {} diff --git a/ext/random/random_arginfo.h b/ext/random/random_arginfo.h index f24c496746384..3442bb24262f0 100644 --- a/ext/random/random_arginfo.h +++ b/ext/random/random_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: c072111a2483473d47d7d77d7d80aea84c663c7a */ + * Stub hash: 9727755f39598fe1fbc16b501063cd3c30279ff9 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_lcg_value, 0, 0, IS_DOUBLE, 0) ZEND_END_ARG_INFO() @@ -91,7 +91,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Random_Randomizer___construct, 0, 0, 0) ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, engine, Random\\Engine, 1, "null") ZEND_END_ARG_INFO() -#define arginfo_class_Random_Randomizer_getInt arginfo_rand +#define arginfo_class_Random_Randomizer_nextInt arginfo_mt_getrandmax + +#define arginfo_class_Random_Randomizer_getInt arginfo_random_int #define arginfo_class_Random_Randomizer_getBytes arginfo_random_bytes @@ -131,6 +133,7 @@ ZEND_METHOD(Random_Engine_Xoshiro256StarStar, __construct); ZEND_METHOD(Random_Engine_Xoshiro256StarStar, jump); ZEND_METHOD(Random_Engine_Xoshiro256StarStar, jumpLong); ZEND_METHOD(Random_Randomizer, __construct); +ZEND_METHOD(Random_Randomizer, nextInt); ZEND_METHOD(Random_Randomizer, getInt); ZEND_METHOD(Random_Randomizer, getBytes); ZEND_METHOD(Random_Randomizer, shuffleArray); @@ -206,6 +209,7 @@ static const zend_function_entry class_Random_CryptoSafeEngine_methods[] = { static const zend_function_entry class_Random_Randomizer_methods[] = { ZEND_ME(Random_Randomizer, __construct, arginfo_class_Random_Randomizer___construct, ZEND_ACC_PUBLIC) + ZEND_ME(Random_Randomizer, nextInt, arginfo_class_Random_Randomizer_nextInt, ZEND_ACC_PUBLIC) ZEND_ME(Random_Randomizer, getInt, arginfo_class_Random_Randomizer_getInt, ZEND_ACC_PUBLIC) ZEND_ME(Random_Randomizer, getBytes, arginfo_class_Random_Randomizer_getBytes, ZEND_ACC_PUBLIC) ZEND_ME(Random_Randomizer, shuffleArray, arginfo_class_Random_Randomizer_shuffleArray, ZEND_ACC_PUBLIC) diff --git a/ext/random/randomizer.c b/ext/random/randomizer.c index 2de21419a1a82..5e4cb7b1e333e 100644 --- a/ext/random/randomizer.c +++ b/ext/random/randomizer.c @@ -91,26 +91,34 @@ PHP_METHOD(Random_Randomizer, __construct) } /* }}} */ +/* {{{ Generate random number */ +PHP_METHOD(Random_Randomizer, nextInt) +{ + php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS); + uint64_t result; + + ZEND_PARSE_PARAMETERS_NONE(); + + result = randomizer->algo->generate(randomizer->status); + if (randomizer->status->last_generated_size > sizeof(zend_long)) { + zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0); + RETURN_THROWS(); + } + if (EG(exception)) { + zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0); + RETURN_THROWS(); + } + + RETURN_LONG((zend_long) (result >> 1)); +} +/* }}} */ + /* {{{ Generate random number in range */ PHP_METHOD(Random_Randomizer, getInt) { php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS); uint64_t result; zend_long min, max; - int argc = ZEND_NUM_ARGS(); - - if (argc == 0) { - result = randomizer->algo->generate(randomizer->status); - if (randomizer->status->last_generated_size > sizeof(zend_long)) { - zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0); - RETURN_THROWS(); - } - if (EG(exception)) { - zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0); - RETURN_THROWS(); - } - RETURN_LONG((zend_long) (result >> 1)); - } ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_LONG(min) diff --git a/ext/random/tests/03_randomizer/basic.phpt b/ext/random/tests/03_randomizer/basic.phpt index 4e4034a1fb916..13454ca65b485 100644 --- a/ext/random/tests/03_randomizer/basic.phpt +++ b/ext/random/tests/03_randomizer/basic.phpt @@ -27,6 +27,18 @@ $engines[] = new UserEngine(); foreach ($engines as $engine) { $randomizer = new Random\Randomizer($engine); + // nextInt + for ($i = 0; $i < 1000; $i++) { + try { + $randomizer->nextInt(); + } catch (\RuntimeException $e) { + if ($e->getMessage() !== 'Generated value exceeds size of int') { + die($engine::class . ': nextInt: failure: {$e->getMesasge()}'); + throw $e; + } + } + } + // getInt for ($i = 0; $i < 1000; $i++) { $result = $randomizer->getInt(-50, 50); diff --git a/ext/random/tests/03_randomizer/compatibility_mt.phpt b/ext/random/tests/03_randomizer/compatibility_mt.phpt index 89c67d556e0a6..9740a494d6fe6 100644 --- a/ext/random/tests/03_randomizer/compatibility_mt.phpt +++ b/ext/random/tests/03_randomizer/compatibility_mt.phpt @@ -6,7 +6,7 @@ Random: Randomizer: Compatibility: Mt19937 $randomizer = new \Random\Randomizer(new \Random\Engine\Mt19937(1234, \MT_RAND_PHP)); \mt_srand(1234, \MT_RAND_PHP); for ($i = 0; $i < 1000; $i++) { - if ($randomizer->getInt() !== \mt_rand()) { + if ($randomizer->nextInt() !== \mt_rand()) { die('failure'); } } @@ -14,7 +14,7 @@ for ($i = 0; $i < 1000; $i++) { $randomizer = new \Random\Randomizer(new \Random\Engine\Mt19937(1234, \MT_RAND_MT19937)); \mt_srand(1234, \MT_RAND_MT19937); for ($i = 0; $i < 1000; $i++) { - if ($randomizer->getInt() !== \mt_rand()) { + if ($randomizer->nextInt() !== \mt_rand()) { die('failure'); } } diff --git a/ext/random/tests/03_randomizer/compatibility_user.phpt b/ext/random/tests/03_randomizer/compatibility_user.phpt index 2a903a52a0462..42f2428d38e4c 100644 --- a/ext/random/tests/03_randomizer/compatibility_user.phpt +++ b/ext/random/tests/03_randomizer/compatibility_user.phpt @@ -15,8 +15,8 @@ $user_randomizer = new \Random\Randomizer(new class () implements \Random\Engine } }); for ($i = 0; $i < 1000; $i++) { - $native = $native_randomizer->getInt(); - $user = $user_randomizer->getInt(); + $native = $native_randomizer->nextInt(); + $user = $user_randomizer->nextInt(); if ($native !== $user) { die("failure Mt19937 i: {$i} native: {$native} user: {$user}"); } @@ -36,8 +36,8 @@ try { }); for ($i = 0; $i < 1000; $i++) { - $native = $native_randomizer->getInt(); - $user = $user_randomizer->getInt(); + $native = $native_randomizer->nextInt(); + $user = $user_randomizer->nextInt(); if ($native !== $user) { die("failure PcgOneseq128XslRr64 i: {$i} native: {$native} user: {$user}"); } @@ -65,8 +65,8 @@ try { }); for ($i = 0; $i < 1000; $i++) { - $native = $native_randomizer->getInt(); - $user = $user_randomizer->getInt(); + $native = $native_randomizer->nextInt(); + $user = $user_randomizer->nextInt(); if ($native !== $user) { die("failure Xoshiro256StarStar i: {$i} native: {$native} user: {$user}"); } From da679e1e1ac18bb6a1e13b4f3530ae8782de431a Mon Sep 17 00:00:00 2001 From: Go Kudo Date: Wed, 20 Jul 2022 14:34:20 +0900 Subject: [PATCH 2/7] ci: kick From c645fc9c61f06833e8ec338ba96a0cb5a001a857 Mon Sep 17 00:00:00 2001 From: zeriyoshi Date: Wed, 20 Jul 2022 22:46:47 +0900 Subject: [PATCH 3/7] random: test: fix test --- ext/random/tests/03_randomizer/basic.phpt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ext/random/tests/03_randomizer/basic.phpt b/ext/random/tests/03_randomizer/basic.phpt index 13454ca65b485..077b9f5b780db 100644 --- a/ext/random/tests/03_randomizer/basic.phpt +++ b/ext/random/tests/03_randomizer/basic.phpt @@ -33,8 +33,7 @@ foreach ($engines as $engine) { $randomizer->nextInt(); } catch (\RuntimeException $e) { if ($e->getMessage() !== 'Generated value exceeds size of int') { - die($engine::class . ': nextInt: failure: {$e->getMesasge()}'); - throw $e; + die($engine::class . ": nextInt: failure: {$e->getMessage()}"); } } } @@ -51,7 +50,7 @@ foreach ($engines as $engine) { for ($i = 0; $i < 1000; $i++) { $length = \random_int(1, 1024); if (\strlen($randomizer->getBytes($length)) !== $length) { - die($engine::class . ': getBytes: failure.'); + die($engine::class . ': getBytes: failure'); } } @@ -65,14 +64,14 @@ foreach ($engines as $engine) { } } - die($engine::class . ': shuffleArray: failure.'); + die($engine::class . ': shuffleArray: failure'); })(); // shuffleBytes $string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'; $shuffled_string = $randomizer->shuffleBytes($string); if ($string === $shuffled_string) { - die($engine::class . ': shuffleBytes: failure.'); + die($engine::class . ': shuffleBytes: failure'); } } From af433484f97cb93882e402016887e07520083931 Mon Sep 17 00:00:00 2001 From: Go Kudo Date: Thu, 21 Jul 2022 12:45:59 +0900 Subject: [PATCH 4/7] random: test: remove useless if --- ext/random/tests/03_randomizer/compatibility_user.phpt | 3 --- 1 file changed, 3 deletions(-) diff --git a/ext/random/tests/03_randomizer/compatibility_user.phpt b/ext/random/tests/03_randomizer/compatibility_user.phpt index 42f2428d38e4c..afcc0bde1b83d 100644 --- a/ext/random/tests/03_randomizer/compatibility_user.phpt +++ b/ext/random/tests/03_randomizer/compatibility_user.phpt @@ -43,9 +43,6 @@ try { } } } catch (\RuntimeException $e) { - if (\PHP_INT_SIZE >= 8) { - throw $e; - } if ($e->getMessage() !== 'Generated value exceeds size of int') { throw $e; } From ce29b6227dfaba4aba35ec244721900135442eaf Mon Sep 17 00:00:00 2001 From: zeriyoshi Date: Thu, 21 Jul 2022 23:20:59 +0900 Subject: [PATCH 5/7] random: test: remove unnecessary if --- ext/random/tests/03_randomizer/compatibility_user.phpt | 3 --- 1 file changed, 3 deletions(-) diff --git a/ext/random/tests/03_randomizer/compatibility_user.phpt b/ext/random/tests/03_randomizer/compatibility_user.phpt index afcc0bde1b83d..8336e7d569bd0 100644 --- a/ext/random/tests/03_randomizer/compatibility_user.phpt +++ b/ext/random/tests/03_randomizer/compatibility_user.phpt @@ -69,9 +69,6 @@ try { } } } catch (\RuntimeException $e) { - if (\PHP_INT_SIZE >= 8) { - throw $e; - } if ($e->getMessage() !== 'Generated value exceeds size of int') { throw $e; } From 908cb0b063eba6560019fb7c9d6b079427810845 Mon Sep 17 00:00:00 2001 From: Go Kudo Date: Sun, 24 Jul 2022 00:01:11 +0900 Subject: [PATCH 6/7] Update ext/random/randomizer.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tim Düsterhus --- ext/random/randomizer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/random/randomizer.c b/ext/random/randomizer.c index 5e4cb7b1e333e..2672477feccb8 100644 --- a/ext/random/randomizer.c +++ b/ext/random/randomizer.c @@ -91,7 +91,7 @@ PHP_METHOD(Random_Randomizer, __construct) } /* }}} */ -/* {{{ Generate random number */ +/* {{{ Generate positive random number */ PHP_METHOD(Random_Randomizer, nextInt) { php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS); From ad6fd76a87e90c5d227b547a5c73d493bc863a7c Mon Sep 17 00:00:00 2001 From: zeriyoshi Date: Sat, 30 Jul 2022 21:03:39 +0900 Subject: [PATCH 7/7] update NEWS --- NEWS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS b/NEWS index 53dd5bda83ed1..cd8a49874664b 100644 --- a/NEWS +++ b/NEWS @@ -37,6 +37,8 @@ PHP NEWS call twice) (zeriyoshi) . Change Mt19937 to throw a ValueError instead of InvalidArgumentException for invalid $mode. (timwolla) + . Splitting Random\Randomizer::getInt() (without arguments) to + Random\Randomizer::nextInt(). (zeriyoshi) - Sockets: . Added SOL_FILTER socket option for Solaris. (David Carlier)