From ec80b781dbd1978653b2e20d6b99ed61e65e646f Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 20 Jun 2020 16:45:01 +0000 Subject: [PATCH 1/3] Haiku opcache jit build update --- ext/opcache/jit/zend_elf.c | 17 +++++++++++++++++ ext/opcache/jit/zend_jit_perf_dump.c | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/ext/opcache/jit/zend_elf.c b/ext/opcache/jit/zend_elf.c index f8a1325db213f..2618f5a488d2d 100644 --- a/ext/opcache/jit/zend_elf.c +++ b/ext/opcache/jit/zend_elf.c @@ -21,6 +21,8 @@ #include #if defined(__FreeBSD__) #include +#elif defined(__HAIKU__) +#include #endif #include #include @@ -64,6 +66,21 @@ void zend_elf_load_symbols(void) #elif defined(__sun) const char *path = getexecname(); int fd = open(path, O_RDONLY); +#elif defined(__HAIKU__) + image_info ii; + int32_t ic = 0; + + while (get_next_image_info(0, &ic, &ii) == B_OK) { + if (ii.type == B_APP_IMAGE) { + break; + } + } + + if (ii.type != B_APP_IMAGE) { + return; + } + + int fd = open(ii.name, O_RDONLY); #else // To complete eventually for other ELF platforms. // Otherwise APPLE is Mach-O diff --git a/ext/opcache/jit/zend_jit_perf_dump.c b/ext/opcache/jit/zend_jit_perf_dump.c index e61c49289535e..94feba1f07d8c 100644 --- a/ext/opcache/jit/zend_jit_perf_dump.c +++ b/ext/opcache/jit/zend_jit_perf_dump.c @@ -35,6 +35,8 @@ #elif defined(__sun) // avoiding thread.h inclusion as it conflicts with vtunes types. extern unsigned int thr_self(void); +#elif defined(__HAIKU__) +#include #endif #include "zend_elf.h" @@ -133,6 +135,21 @@ static void zend_jit_perf_jitdump_open(void) #elif defined(__sun) const char *path = getexecname(); fd = open(path, O_RDONLY); +#elif defined(__HAIKU__) + image_info ii; + int32_t ic = 0; + + while (get_next_image_info(0, &ic, &ii) == B_OK) { + if (ii.type == B_APP_IMAGE) { + break; + } + } + + if (ii.type != B_APP_IMAGE) { + return; + } + + fd = open(ii.name, O_RDONLY); #else fd = -1; #endif From 2c97b401c61ad09ea0bb0045314b410a97e1d4ac Mon Sep 17 00:00:00 2001 From: Vladyslav Startsev <17382248+vladyslavstartsev@users.noreply.github.com> Date: Sat, 20 Jun 2020 18:26:51 +0300 Subject: [PATCH 2/3] make bcpowmod stricter by not returning false, instead throw exception Closes GH-5747 --- ext/bcmath/bcmath.c | 14 ++++++++++---- ext/bcmath/bcmath.stub.php | 2 +- ext/bcmath/bcmath_arginfo.h | 2 +- ext/bcmath/libbcmath/src/raisemod.c | 2 +- ext/bcmath/tests/bcpowmod_negative_exponent.phpt | 16 ++++++++++++++++ ...od_error4.phpt => bcpowmod_zero_modulus.phpt} | 12 +++++++++--- ext/opcache/Optimizer/zend_func_info.c | 2 +- 7 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 ext/bcmath/tests/bcpowmod_negative_exponent.phpt rename ext/bcmath/tests/{bcpowmod_error4.phpt => bcpowmod_zero_modulus.phpt} (50%) diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index 1b91fba72fe74..f1e298b5cc9b0 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -404,10 +404,16 @@ PHP_FUNCTION(bcpowmod) php_str2num(&second, ZSTR_VAL(right)); php_str2num(&mod, ZSTR_VAL(modulus)); - if (bc_raisemod(first, second, mod, &result, scale) != -1) { - RETVAL_STR(bc_num2str_ex(result, scale)); - } else { - RETVAL_FALSE; + switch (bc_raisemod(first, second, mod, &result, scale)) { + case 0: + RETVAL_STR(bc_num2str_ex(result, scale)); + break; + case -1: + zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero"); + break; + case -2: + zend_argument_value_error(2, "must be greater than 0"); + break; } bc_free_num(&first); diff --git a/ext/bcmath/bcmath.stub.php b/ext/bcmath/bcmath.stub.php index 45e75376f0991..97232b0ed7121 100644 --- a/ext/bcmath/bcmath.stub.php +++ b/ext/bcmath/bcmath.stub.php @@ -12,7 +12,7 @@ function bcdiv(string $dividend, string $divisor, ?int $scale = null): string {} function bcmod(string $dividend, string $divisor, ?int $scale = null): string {} -function bcpowmod(string $base, string $exponent, string $modulus, ?int $scale = null): string|false {} +function bcpowmod(string $base, string $exponent, string $modulus, ?int $scale = null): string {} function bcpow(string $base, string $exponent, ?int $scale = null): string {} diff --git a/ext/bcmath/bcmath_arginfo.h b/ext/bcmath/bcmath_arginfo.h index 75f864dc9d36f..46fc5ff9bd3e2 100644 --- a/ext/bcmath/bcmath_arginfo.h +++ b/ext/bcmath/bcmath_arginfo.h @@ -18,7 +18,7 @@ ZEND_END_ARG_INFO() #define arginfo_bcmod arginfo_bcdiv -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_bcpowmod, 0, 3, MAY_BE_STRING|MAY_BE_FALSE) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcpowmod, 0, 3, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, base, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, exponent, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, modulus, IS_STRING, 0) diff --git a/ext/bcmath/libbcmath/src/raisemod.c b/ext/bcmath/libbcmath/src/raisemod.c index 286590394095f..46dfd7a8a863a 100644 --- a/ext/bcmath/libbcmath/src/raisemod.c +++ b/ext/bcmath/libbcmath/src/raisemod.c @@ -67,7 +67,7 @@ bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale) /* Check for correct numbers. */ if (bc_is_zero(mod)) return -1; - if (bc_is_neg(expo)) return -1; + if (bc_is_neg(expo)) return -2; /* Set initial values. */ power = bc_copy_num (base); diff --git a/ext/bcmath/tests/bcpowmod_negative_exponent.phpt b/ext/bcmath/tests/bcpowmod_negative_exponent.phpt new file mode 100644 index 0000000000000..a72b39548b72b --- /dev/null +++ b/ext/bcmath/tests/bcpowmod_negative_exponent.phpt @@ -0,0 +1,16 @@ +--TEST-- +bc_raisemod's expo can't be negative +--CREDITS-- +Gabriel Caruso (carusogabriel34@gmail.com) +--SKIPIF-- + +--FILE-- +getMessage() . \PHP_EOL; +} +?> +--EXPECT-- +bcpowmod(): Argument #2 ($exponent) must be greater than 0 diff --git a/ext/bcmath/tests/bcpowmod_error4.phpt b/ext/bcmath/tests/bcpowmod_zero_modulus.phpt similarity index 50% rename from ext/bcmath/tests/bcpowmod_error4.phpt rename to ext/bcmath/tests/bcpowmod_zero_modulus.phpt index 7c2ba5012b9f5..0b810969c6a79 100644 --- a/ext/bcmath/tests/bcpowmod_error4.phpt +++ b/ext/bcmath/tests/bcpowmod_zero_modulus.phpt @@ -1,10 +1,16 @@ --TEST-- -bc_raisemod's mod can't be zero and expo can't be negative +bc_raisemod's mod can't be zero --CREDITS-- Gabriel Caruso (carusogabriel34@gmail.com) --SKIPIF-- --FILE-- - +getMessage(), PHP_EOL; +} +?> --EXPECT-- -bool(false) +Modulo by zero diff --git a/ext/opcache/Optimizer/zend_func_info.c b/ext/opcache/Optimizer/zend_func_info.c index ff985fc63fae6..1b9e800d7d4cc 100644 --- a/ext/opcache/Optimizer/zend_func_info.c +++ b/ext/opcache/Optimizer/zend_func_info.c @@ -789,7 +789,7 @@ static const func_info_t func_infos[] = { F1("bcmul", MAY_BE_STRING), F1("bcdiv", MAY_BE_STRING), F1("bcmod", MAY_BE_STRING), - F1("bcpowmod", MAY_BE_FALSE | MAY_BE_STRING), + F1("bcpowmod", MAY_BE_STRING), F1("bcpow", MAY_BE_STRING), F1("bcsqrt", MAY_BE_STRING), From efad1372a532037d54978cc61589178076c9172f Mon Sep 17 00:00:00 2001 From: Eddie Kohler Date: Sun, 21 Jun 2020 19:54:39 -0400 Subject: [PATCH 3/3] Correct implementation of joaat hash. Before this commit, the result produced by a joaat hash depended on how the input data was chunked. A hash produced by multiple `hash_update` operations was incorrect. For example, this code, which should produce three identical lines: var_dump(hash("joaat", "abcd")); $hash = hash_init("joaat"); hash_update($hash, "ab"); hash_update($hash, "cd"); var_dump(hash_final($hash)); $hash = hash_init("joaat"); hash_update($hash, "abc"); hash_update($hash, "d"); var_dump(hash_final($hash)); instead produced: string(8) "cd8b6206" string(8) "e590d137" string(8) "2d59d087" This is because the finalization step, involving shift operations and adds, was applied on every chunk, rather than once at the end as is required by the hash definition. After this commit, the code above produces: string(8) "cd8b6206" string(8) "cd8b6206" string(8) "cd8b6206" as expected. Some tests encoded the wrong behavior and were corrected. Closes GH-5749 --- ext/hash/hash_joaat.c | 15 ++++++++------- ext/hash/tests/hash-clone.phpt | 2 +- ext/hash/tests/hash_copy_001.phpt | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/ext/hash/hash_joaat.c b/ext/hash/hash_joaat.c index d6311e81dec6c..10c3ca2748f5d 100644 --- a/ext/hash/hash_joaat.c +++ b/ext/hash/hash_joaat.c @@ -44,17 +44,22 @@ PHP_HASH_API void PHP_JOAATUpdate(PHP_JOAAT_CTX *context, const unsigned char *i PHP_HASH_API void PHP_JOAATFinal(unsigned char digest[4], PHP_JOAAT_CTX * context) { + uint32_t hval = context->state; + hval += (hval << 3); + hval ^= (hval >> 11); + hval += (hval << 15); + #ifdef WORDS_BIGENDIAN - memcpy(digest, &context->state, 4); + memcpy(digest, &hval, 4); #else int i = 0; - unsigned char *c = (unsigned char *) &context->state; + unsigned char *c = (unsigned char *) &hval; for (i = 0; i < 4; i++) { digest[i] = c[3 - i]; } #endif - context->state = 0; + context->state = 0; } /* @@ -79,9 +84,5 @@ joaat_buf(void *buf, size_t len, uint32_t hval) hval ^= (hval >> 6); } - hval += (hval << 3); - hval ^= (hval >> 11); - hval += (hval << 15); - return hval; } diff --git a/ext/hash/tests/hash-clone.phpt b/ext/hash/tests/hash-clone.phpt index 0ef0df4449963..57567c0bc5014 100644 --- a/ext/hash/tests/hash-clone.phpt +++ b/ext/hash/tests/hash-clone.phpt @@ -301,7 +301,7 @@ string(16) "bebc746a33b6ab62" string(16) "893899e4415a920f" string(5) "joaat" string(8) "aaebf370" -string(8) "513479b4" +string(8) "836fb0e5" string(10) "haval128,3" string(32) "86362472c8895e68e223ef8b3711d8d9" string(32) "ebeeeb05c18af1e53d2d127b561d5e0d" diff --git a/ext/hash/tests/hash_copy_001.phpt b/ext/hash/tests/hash_copy_001.phpt index 27993b61b054a..271326178d523 100644 --- a/ext/hash/tests/hash_copy_001.phpt +++ b/ext/hash/tests/hash_copy_001.phpt @@ -301,7 +301,7 @@ string(16) "bebc746a33b6ab62" string(16) "893899e4415a920f" string(5) "joaat" string(8) "aaebf370" -string(8) "513479b4" +string(8) "836fb0e5" string(10) "haval128,3" string(32) "86362472c8895e68e223ef8b3711d8d9" string(32) "ebeeeb05c18af1e53d2d127b561d5e0d"