From c147d90dbfcc4f9fd494215b7e5740e497d818c1 Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Sat, 3 Oct 2015 19:38:56 -0700 Subject: [PATCH 1/5] Fixed bug #70601 (Segfault in gc_remove_from_buffer()) --- NEWS | 3 +++ ext/opcache/Optimizer/pass1_5.c | 4 +++- ext/opcache/tests/bug70601.phpt | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 ext/opcache/tests/bug70601.phpt diff --git a/NEWS b/NEWS index 7f5c7a40a6..4adadb2083 100644 --- a/NEWS +++ b/NEWS @@ -14,6 +14,9 @@ PHP NEWS (Andrey) . Fixed bug #70572 segfault in mysqlnd_connect. (Andrey, Remi) +- Opcache: + . Fixed bug #70601 (Segfault in gc_remove_from_buffer()). (Laruence) + 01 Oct 2015, PHP 5.6.14 - Core: diff --git a/ext/opcache/Optimizer/pass1_5.c b/ext/opcache/Optimizer/pass1_5.c index 4ed3dd4c85..1302e4b182 100644 --- a/ext/opcache/Optimizer/pass1_5.c +++ b/ext/opcache/Optimizer/pass1_5.c @@ -314,9 +314,11 @@ if (ZEND_OPTIMIZER_PASS_1 & OPTIMIZATION_LEVEL) { ZEND_IS_CONSTANT_TYPE(Z_TYPE(t))) { break; } - } else { + } else if (Z_TYPE_PP(c) <= IS_BOOL || Z_TYPE_PP(c) == IS_STRING) { t = **c; zval_copy_ctor(&t); + } else { + break; } if (ZEND_OP1_TYPE(opline) == IS_CONST) { diff --git a/ext/opcache/tests/bug70601.phpt b/ext/opcache/tests/bug70601.phpt new file mode 100644 index 0000000000..55b1d7e4c2 --- /dev/null +++ b/ext/opcache/tests/bug70601.phpt @@ -0,0 +1,23 @@ +--TEST-- +Bug #70601 (Segfault in gc_remove_from_buffer()) +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.protect_memory=1 +--SKIPIF-- + +--FILE-- + "" ]; +class a { + const REPLACEMENTS_PASS_1 = [ "\x01" => "" ]; + public function __construct() { + echo strtr("Ok", REPLACEMENTS_PASS_1); + echo strtr("ay", self::REPLACEMENTS_PASS_1); + } +} + +new a(); +?> +--EXPECT-- +Okay From 925412ee1c123c55e2b5badf6daf193e6c1bdd5b Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Sat, 3 Oct 2015 19:50:38 -0700 Subject: [PATCH 2/5] Do not edit the zval cause it might be in shared memory --- ext/spl/spl_array.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ext/spl/spl_array.c b/ext/spl/spl_array.c index 2c65210b98..6ebbb7c068 100644 --- a/ext/spl/spl_array.c +++ b/ext/spl/spl_array.c @@ -303,6 +303,8 @@ static zval **spl_array_get_dimension_ptr_ptr(int check_inherited, zval *object, { spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC); zval **retval; + char *key; + uint len; long index; HashTable *ht = spl_array_get_hash_table(intern, 0 TSRMLS_CC); @@ -315,12 +317,12 @@ static zval **spl_array_get_dimension_ptr_ptr(int check_inherited, zval *object, return &EG(error_zval_ptr);; } - switch(Z_TYPE_P(offset)) { - case IS_NULL: - Z_STRVAL_P(offset) = ""; - Z_STRLEN_P(offset) = 0; + switch (Z_TYPE_P(offset)) { case IS_STRING: - if (zend_symtable_find(ht, Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1, (void **) &retval) == FAILURE) { + key = Z_STRVAL_P(offset); + len = Z_STRLEN_P(offset) + 1; +string_offest: + if (zend_symtable_find(ht, key, len, (void **) &retval) == FAILURE) { switch (type) { case BP_VAR_R: zend_error(E_NOTICE, "Undefined index: %s", Z_STRVAL_P(offset)); @@ -333,11 +335,15 @@ static zval **spl_array_get_dimension_ptr_ptr(int check_inherited, zval *object, case BP_VAR_W: { zval *value; ALLOC_INIT_ZVAL(value); - zend_symtable_update(ht, Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1, (void**)&value, sizeof(void*), (void **)&retval); + zend_symtable_update(ht, key, len, (void**)&value, sizeof(void*), (void **)&retval); } } } return retval; + case IS_NULL: + key = ""; + len = 1; + goto string_offest; case IS_RESOURCE: zend_error(E_STRICT, "Resource ID#%ld used as offset, casting to integer (%ld)", Z_LVAL_P(offset), Z_LVAL_P(offset)); case IS_DOUBLE: From 4744eec753786e18b28887463bdcb3f1e91efb60 Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Sat, 3 Oct 2015 20:08:21 -0700 Subject: [PATCH 3/5] Fixed bug #70631 (Another Segfault in gc_remove_from_buffer()) --- NEWS | 1 + ext/opcache/tests/bug70631.phpt | 20 ++++++++++++++++++++ ext/reflection/php_reflection.c | 30 +++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 ext/opcache/tests/bug70631.phpt diff --git a/NEWS b/NEWS index 4adadb2083..79ca9877be 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,7 @@ PHP NEWS . Fixed bug #70572 segfault in mysqlnd_connect. (Andrey, Remi) - Opcache: + . Fixed bug #70631 (Another Segfault in gc_remove_from_buffer()). (Laruence) . Fixed bug #70601 (Segfault in gc_remove_from_buffer()). (Laruence) 01 Oct 2015, PHP 5.6.14 diff --git a/ext/opcache/tests/bug70631.phpt b/ext/opcache/tests/bug70631.phpt new file mode 100644 index 0000000000..1089329fe2 --- /dev/null +++ b/ext/opcache/tests/bug70631.phpt @@ -0,0 +1,20 @@ +--TEST-- +Bug #70631 (Another Segfault in gc_remove_from_buffer()) +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.protect_memory=1 +--SKIPIF-- + +--FILE-- +getParameters(); +$value = $params[0]->getDefaultValue(); +echo $value[0]; +?> +--EXPECT-- +Okay diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index eff8bdc65b..3bf49eafc7 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -242,6 +242,25 @@ static void _default_lookup_entry(zval *object, char *name, int name_len, zval * /* }}} */ #endif +static void reflection_zval_deep_copy(zval **p) +{ + zval *value; + + ALLOC_ZVAL(value); + *value = **p; + if (Z_TYPE_P(value) == IS_ARRAY) { + HashTable *ht; + + ALLOC_HASHTABLE(ht); + zend_hash_init(ht, zend_hash_num_elements(Z_ARRVAL_P(value)), NULL, ZVAL_PTR_DTOR, 0); + zend_hash_copy(ht, Z_ARRVAL_P(value), (copy_ctor_func_t) reflection_zval_deep_copy, NULL, sizeof(zval *)); + Z_ARRVAL_P(value) = ht; + } else { + zval_copy_ctor(value); + } + INIT_PZVAL(value); + *p = value; +} static void reflection_register_implement(zend_class_entry *class_entry, zend_class_entry *interface_entry TSRMLS_DC) /* {{{ */ { zend_uint num_interfaces = ++class_entry->num_interfaces; @@ -2606,7 +2625,16 @@ ZEND_METHOD(reflection_parameter, getDefaultValue) *return_value = *precv->op2.zv; INIT_PZVAL(return_value); if (!IS_CONSTANT_TYPE(Z_TYPE_P(return_value))) { - zval_copy_ctor(return_value); + if (Z_TYPE_P(return_value) != IS_ARRAY) { + zval_copy_ctor(return_value); + } else { + HashTable *ht; + + ALLOC_HASHTABLE(ht); + zend_hash_init(ht, zend_hash_num_elements(Z_ARRVAL_P(return_value)), NULL, ZVAL_PTR_DTOR, 0); + zend_hash_copy(ht, Z_ARRVAL_P(return_value), (copy_ctor_func_t) reflection_zval_deep_copy, NULL, sizeof(zval *)); + Z_ARRVAL_P(return_value) = ht; + } } old_scope = EG(scope); EG(scope) = param->fptr->common.scope; From 6876112c89e6dc4434e64a78084ca3bb118c98bd Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Sat, 3 Oct 2015 20:14:55 -0700 Subject: [PATCH 4/5] Fixed the third one of (segfault in gc_remove_from_buffer()) This one maybe only used in debug mode, so no bug report and no test script provided --- ext/reflection/php_reflection.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 3bf49eafc7..e1a2c57668 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -750,14 +750,18 @@ static void _parameter_string(string *str, zend_function *fptr, struct _zend_arg zend_class_entry *old_scope; string_write(str, " = ", sizeof(" = ")-1); - ALLOC_ZVAL(zv); - *zv = *precv->op2.zv; - zval_copy_ctor(zv); - INIT_PZVAL(zv); - old_scope = EG(scope); - EG(scope) = fptr->common.scope; - zval_update_constant_ex(&zv, 1, NULL TSRMLS_CC); - EG(scope) = old_scope; + if (IS_CONSTANT_TYPE(Z_TYPE_P(precv->op2.zv))) { + ALLOC_ZVAL(zv); + *zv = *precv->op2.zv; + zval_copy_ctor(zv); + INIT_PZVAL(zv); + old_scope = EG(scope); + EG(scope) = fptr->common.scope; + zval_update_constant_ex(&zv, 1, NULL TSRMLS_CC); + EG(scope) = old_scope; + } else { + zv = precv->op2.zv; + } if (Z_TYPE_P(zv) == IS_BOOL) { if (Z_LVAL_P(zv)) { string_write(str, "true", sizeof("true")-1); @@ -782,7 +786,9 @@ static void _parameter_string(string *str, zend_function *fptr, struct _zend_arg zval_dtor(&zv_copy); } } - zval_ptr_dtor(&zv); + if (zv != precv->op2.zv) { + zval_ptr_dtor(&zv); + } } } string_write(str, " ]", sizeof(" ]")-1); From 2fb8bb1157958465a8b12c1a41015b907787c17a Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Sat, 3 Oct 2015 20:33:24 -0700 Subject: [PATCH 5/5] Fixed bug #70632 (Third one of segfault in gc_remove_from_buffer) --- NEWS | 2 ++ Zend/zend_compile.c | 6 +----- ext/opcache/tests/bug70632.phpt | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 ext/opcache/tests/bug70632.phpt diff --git a/NEWS b/NEWS index 79ca9877be..d8d0354f01 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,8 @@ PHP NEWS . Fixed bug #70572 segfault in mysqlnd_connect. (Andrey, Remi) - Opcache: + . Fixed bug #70632 (Third one of segfault in gc_remove_from_buffer). + (Laruence) . Fixed bug #70631 (Another Segfault in gc_remove_from_buffer()). (Laruence) . Fixed bug #70601 (Segfault in gc_remove_from_buffer()). (Laruence) diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 0bfb770f3f..5ff1b042c2 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -3472,10 +3472,7 @@ static char * zend_get_function_declaration(zend_function *fptr TSRMLS_DC) /* {{ if (precv && precv->opcode == ZEND_RECV_INIT && precv->op2_type != IS_UNUSED) { zval *zv, zv_copy; int use_copy; - ALLOC_ZVAL(zv); - *zv = *precv->op2.zv; - zval_copy_ctor(zv); - INIT_PZVAL(zv); + zv = precv->op2.zv; if ((Z_TYPE_P(zv) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT) { REALLOC_BUF_IF_EXCEED(buf, offset, length, Z_STRLEN_P(zv)); memcpy(offset, Z_STRVAL_P(zv), Z_STRLEN_P(zv)); @@ -3517,7 +3514,6 @@ static char * zend_get_function_declaration(zend_function *fptr TSRMLS_DC) /* {{ zval_dtor(&zv_copy); } } - zval_ptr_dtor(&zv); } } else { memcpy(offset, "NULL", 4); diff --git a/ext/opcache/tests/bug70632.phpt b/ext/opcache/tests/bug70632.phpt new file mode 100644 index 0000000000..4cdb5d84f9 --- /dev/null +++ b/ext/opcache/tests/bug70632.phpt @@ -0,0 +1,21 @@ +--TEST-- +Bug #70632 (Third one of segfault in gc_remove_from_buffer) +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.protect_memory=1 +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +Strict Standards: Declaration of B::test() should be compatible with A::test($a = Array) in %sbug70632.php(%d) : eval()'d code on line %d