From e113bb35e76c657705063181af758f529c7ed77e Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 20 Sep 2023 14:36:32 +0200 Subject: [PATCH 1/2] Move JIT check to BigInteger --- phpseclib/Math/BigInteger.php | 11 +++++++++++ phpseclib/bootstrap.php | 12 +----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index c6609e4d..99690817 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -29,6 +29,7 @@ namespace phpseclib3\Math; use phpseclib3\Exception\BadConfigurationException; use phpseclib3\Math\BigInteger\Engines\Engine; +use UnexpectedValueException; /** * Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 @@ -135,6 +136,16 @@ class BigInteger implements \JsonSerializable private static function initialize_static_variables() { if (!isset(self::$mainEngine)) { + // see https://github.com/php/php-src/issues/11917 + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && function_exists('opcache_get_status') && !defined('PHPSECLIB_ALLOW_JIT')) { + $status = opcache_get_status(); + if ($status && isset($status['jit']) && $status['jit']['enabled'] && $status['jit']['on']) { + throw new UnexpectedValueException( + 'JIT on Windows is not currently supported' + ); + } + } + $engines = [ ['GMP', ['DefaultEngine']], ['PHP64', ['OpenSSL']], diff --git a/phpseclib/bootstrap.php b/phpseclib/bootstrap.php index b794d549..c84c60dc 100644 --- a/phpseclib/bootstrap.php +++ b/phpseclib/bootstrap.php @@ -19,14 +19,4 @@ if (extension_loaded('mbstring')) { 'is not supported by phpseclib.' ); } -} - -// see https://github.com/php/php-src/issues/11917 -if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && function_exists('opcache_get_status') && !defined('PHPSECLIB_ALLOW_JIT')) { - $status = opcache_get_status(); - if ($status && isset($status['jit']) && $status['jit']['enabled'] && $status['jit']['on']) { - throw new UnexpectedValueException( - 'JIT on Windows is not currently supported' - ); - } -} +} \ No newline at end of file From 7b9ab171ce89f5d91d76e6a624e8bedb47c6b8e6 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 22 Sep 2023 10:07:03 +0200 Subject: [PATCH 2/2] Refactor --- phpseclib/Math/BigInteger.php | 15 ++++----------- phpseclib/Math/BigInteger/Engines/PHP.php | 14 ++++++++++++++ phpseclib/Math/BigInteger/Engines/PHP32.php | 2 +- phpseclib/Math/BigInteger/Engines/PHP64.php | 2 +- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 99690817..3f4dc2ed 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -136,16 +136,6 @@ class BigInteger implements \JsonSerializable private static function initialize_static_variables() { if (!isset(self::$mainEngine)) { - // see https://github.com/php/php-src/issues/11917 - if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && function_exists('opcache_get_status') && !defined('PHPSECLIB_ALLOW_JIT')) { - $status = opcache_get_status(); - if ($status && isset($status['jit']) && $status['jit']['enabled'] && $status['jit']['on']) { - throw new UnexpectedValueException( - 'JIT on Windows is not currently supported' - ); - } - } - $engines = [ ['GMP', ['DefaultEngine']], ['PHP64', ['OpenSSL']], @@ -154,13 +144,16 @@ class BigInteger implements \JsonSerializable ['PHP64', ['DefaultEngine']], ['PHP32', ['DefaultEngine']] ]; + foreach ($engines as $engine) { try { self::setEngine($engine[0], $engine[1]); - break; + return; } catch (\Exception $e) { } } + + throw new UnexpectedValueException('No valid BigInteger found. This is only possible when JIT is enabled on Windows and neither the GMP or BCMath extensions are available so either disable JIT or install GMP / BCMath'); } } diff --git a/phpseclib/Math/BigInteger/Engines/PHP.php b/phpseclib/Math/BigInteger/Engines/PHP.php index ab9bdc99..4c30da56 100644 --- a/phpseclib/Math/BigInteger/Engines/PHP.php +++ b/phpseclib/Math/BigInteger/Engines/PHP.php @@ -1326,4 +1326,18 @@ abstract class PHP extends Engine return array_reverse($vals); } + + /** + * @return bool + */ + protected static function testJITOnWindows() + { + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && function_exists('opcache_get_status') && !defined('PHPSECLIB_ALLOW_JIT')) { + $status = opcache_get_status(); + if ($status && isset($status['jit']) && $status['jit']['enabled'] && $status['jit']['on']) { + return true; + } + } + return false; + } } diff --git a/phpseclib/Math/BigInteger/Engines/PHP32.php b/phpseclib/Math/BigInteger/Engines/PHP32.php index 18f78cdb..3a775e7d 100644 --- a/phpseclib/Math/BigInteger/Engines/PHP32.php +++ b/phpseclib/Math/BigInteger/Engines/PHP32.php @@ -102,7 +102,7 @@ class PHP32 extends PHP */ public static function isValidEngine() { - return PHP_INT_SIZE >= 4; + return PHP_INT_SIZE >= 4 && !self::testJITOnWindows(); } /** diff --git a/phpseclib/Math/BigInteger/Engines/PHP64.php b/phpseclib/Math/BigInteger/Engines/PHP64.php index ca11c08d..70a2e173 100644 --- a/phpseclib/Math/BigInteger/Engines/PHP64.php +++ b/phpseclib/Math/BigInteger/Engines/PHP64.php @@ -103,7 +103,7 @@ class PHP64 extends PHP */ public static function isValidEngine() { - return PHP_INT_SIZE >= 8; + return PHP_INT_SIZE >= 8 && !self::testJITOnWindows(); } /**