diff --git a/Zend/tests/bug70805.phpt b/Zend/tests/bug70805.phpt new file mode 100644 index 0000000000..256f52eed5 --- /dev/null +++ b/Zend/tests/bug70805.phpt @@ -0,0 +1,46 @@ +--TEST-- +Bug #70805 (Segmentation faults whilst running Drupal 8 test suite) +--FILE-- +b = new B; +$a->b->a = $a; + +$i = 0; + +$c = new A; +$array = array($c); //This is used to leave a room for $GLOBALS["a"] +unset($c); + +while ($i++ < 9997) { + $t = []; + $t[] = &$t; + unset($t); +} +$t = [new C]; +$t[] = &$t; +unset($t); // This is used to trigger C::__destruct while doing gc_colloct_roots + +$e = $a; +unset($a); // This one can not be putted into roots buf because it's full, thus gc_colloct_roots will be called, + // but C::__destructor which is called in gc_colloct_roots will put $a into buf + // which will make $a be putted into gc roots buf twice +var_dump(gc_collect_cycles()); +?> +--EXPECT-- +int(0) diff --git a/Zend/tests/bug70805_1.phpt b/Zend/tests/bug70805_1.phpt new file mode 100644 index 0000000000..0225b4ce82 --- /dev/null +++ b/Zend/tests/bug70805_1.phpt @@ -0,0 +1,43 @@ +--TEST-- +Bug #70805 (Segmentation faults whilst running Drupal 8 test suite) (Crash) +--FILE-- +b = new B; +$a->b->a = $a; + +$i = 0; + +$c = new A; +$array = array($c); +unset($c); + +while ($i++ < 9997) { + $t = []; + $t[] = &$t; + unset($t); +} +$t = [new C]; +$t[] = &$t; +unset($t); +unset($a); + +var_dump(gc_collect_cycles()); +?> +--EXPECT-- +int(2) diff --git a/Zend/tests/bug70805_2.phpt b/Zend/tests/bug70805_2.phpt new file mode 100644 index 0000000000..a9b11684f5 --- /dev/null +++ b/Zend/tests/bug70805_2.phpt @@ -0,0 +1,37 @@ +--TEST-- +Bug #70805 (Segmentation faults whilst running Drupal 8 test suite) (Memleak) +--FILE-- +b = new B; +$a->b->a = $a; + +$i = 0; + +while ($i++ < 9998) { + $t = []; + $t[] = &$t; + unset($t); +} +$t = [new C]; +$t[] = &$t; +unset($t); + +unset($a); +var_dump(gc_collect_cycles()); +--EXPECT-- +int(2) diff --git a/Zend/tests/bug70873.phpt b/Zend/tests/bug70873.phpt new file mode 100644 index 0000000000..3edb7eac43 --- /dev/null +++ b/Zend/tests/bug70873.phpt @@ -0,0 +1,34 @@ +--TEST-- +Bug #70873 (Regression on private static properties access) +--FILE-- +bar(); + +$b = new C; +$b->bar(); +?> +--EXPECTF-- +Fatal error: Uncaught Error: Cannot access property B::$x in %sbug70873.php:%d +Stack trace: +#0 %sbug70873.php(%d): B->bar() +#1 {main} + thrown in %sbug70873.php on line %d diff --git a/Zend/tests/varSyntax/static_prop_on_expr_class.phpt b/Zend/tests/varSyntax/static_prop_on_expr_class.phpt new file mode 100644 index 0000000000..d17b6cdb0b --- /dev/null +++ b/Zend/tests/varSyntax/static_prop_on_expr_class.phpt @@ -0,0 +1,13 @@ +--TEST-- +Accessing a static property on a statically evaluable class expression +--FILE-- + +--EXPECT-- +int(42) diff --git a/Zend/tests/varSyntax/static_prop_on_expr_class_with_backslash.phpt b/Zend/tests/varSyntax/static_prop_on_expr_class_with_backslash.phpt new file mode 100644 index 0000000000..c78610401f --- /dev/null +++ b/Zend/tests/varSyntax/static_prop_on_expr_class_with_backslash.phpt @@ -0,0 +1,13 @@ +--TEST-- +Static property on constexpr class with leading backslash +--FILE-- + +--EXPECT-- +int(42) diff --git a/Zend/tests/varSyntax/static_prop_on_int_expr_class.phpt b/Zend/tests/varSyntax/static_prop_on_int_expr_class.phpt new file mode 100644 index 0000000000..c7c44d5694 --- /dev/null +++ b/Zend/tests/varSyntax/static_prop_on_int_expr_class.phpt @@ -0,0 +1,11 @@ +--TEST-- +Static property access on constexpr class evaluating to integer +--FILE-- + +--EXPECTF-- +Fatal error: Uncaught Error: Class name must be a valid object or a string in %s:%d +Stack trace: +#0 {main} + thrown in %s on line %d diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 06cda75363..c9d4de43a4 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -1463,9 +1463,9 @@ static void *zend_mm_realloc_heap(zend_mm_heap *heap, void *ptr, size_t size, si size = real_size; #endif #ifdef ZEND_WIN32 - /* On Windows we don't have ability to extend huge block in-place. - * We allocate them with 2MB size granularuty, to avoid many - * reallocatioons whenthey when they are extended by small peaces + /* On Windows we don't have ability to extend huge blocks in-place. + * We allocate them with 2MB size granularity, to avoid many + * reallocations when they are extended by small pieces */ new_size = ZEND_MM_ALIGNED_SIZE_EX(size, MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE)); #else @@ -1731,9 +1731,9 @@ static void zend_mm_change_huge_block_size(zend_mm_heap *heap, void *ptr, size_t static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) { #ifdef ZEND_WIN32 - /* On Windows we don't have ability to extend huge block in-place. - * We allocate them with 2MB size granularuty, to avoid many - * reallocatioons whenthey when they are extended by small peaces + /* On Windows we don't have ability to extend huge blocks in-place. + * We allocate them with 2MB size granularity, to avoid many + * reallocations when they are extended by small pieces */ size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE)); #else diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c index 8ffdcaa0bc..6faa57cd99 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -2481,7 +2481,7 @@ ZEND_FUNCTION(debug_print_backtrace) ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit) /* {{{ */ { - zend_execute_data *call, *ptr, *skip; + zend_execute_data *ptr, *skip, *call = NULL; zend_object *object; int lineno, frameno = 0; zend_function *func; @@ -2490,8 +2490,12 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int zend_string *include_filename = NULL; zval stack_frame; - call = NULL; - ptr = EG(current_execute_data); + array_init(return_value); + + if (!(ptr = EG(current_execute_data))) { + return; + } + if (!ptr->func || !ZEND_USER_CODE(ptr->func->common.type)) { call = ptr; ptr = ptr->prev_execute_data; @@ -2509,13 +2513,11 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int ptr = ptr->prev_execute_data; } } + if (!call) { + call = ptr; + ptr = ptr->prev_execute_data; + } } - if (!call) { - call = ptr; - ptr = ptr->prev_execute_data; - } - - array_init(return_value); while (ptr && (limit == 0 || frameno < limit)) { frameno++; diff --git a/Zend/zend_gc.c b/Zend/zend_gc.c index e223a49906..74c534844d 100644 --- a/Zend/zend_gc.c +++ b/Zend/zend_gc.c @@ -242,6 +242,13 @@ ZEND_API void ZEND_FASTCALL gc_possible_root(zend_refcounted *ref) GC_REFCOUNT(ref)++; gc_collect_cycles(); GC_REFCOUNT(ref)--; + if (UNEXPECTED(GC_REFCOUNT(ref)) == 0) { + zval_dtor_func_for_ptr(ref); + return; + } + if (UNEXPECTED(GC_INFO(ref))) { + return; + } newRoot = GC_G(unused); if (!newRoot) { #if ZEND_GC_DEBUG diff --git a/Zend/zend_hash.c b/Zend/zend_hash.c index 8a8ef2948e..52868cf4a6 100644 --- a/Zend/zend_hash.c +++ b/Zend/zend_hash.c @@ -1049,9 +1049,13 @@ ZEND_API int ZEND_FASTCALL zend_hash_del_ind(HashTable *ht, zend_string *key) return FAILURE; } else { if (ht->pDestructor) { - ht->pDestructor(data); + zval tmp; + ZVAL_COPY_VALUE(&tmp, data); + ZVAL_UNDEF(data); + ht->pDestructor(&tmp); + } else { + ZVAL_UNDEF(data); } - ZVAL_UNDEF(data); } } else { _zend_hash_del_el_ex(ht, idx, p, prev); diff --git a/Zend/zend_object_handlers.c b/Zend/zend_object_handlers.c index 7c4b6a679a..4cd8a4e7c2 100644 --- a/Zend/zend_object_handlers.c +++ b/Zend/zend_object_handlers.c @@ -275,10 +275,10 @@ static zend_always_inline int zend_verify_property_access(zend_property_info *pr return 1; } else if (property_info->flags & ZEND_ACC_PRIVATE) { return (ce == EG(scope) || property_info->ce == EG(scope)); - } else { - ZEND_ASSERT(property_info->flags & ZEND_ACC_PROTECTED); + } else if (property_info->flags & ZEND_ACC_PROTECTED) { return zend_check_protected(property_info->ce, EG(scope)); } + return 0; } /* }}} */ diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c index 14dafe353b..3f4f29020e 100644 --- a/Zend/zend_operators.c +++ b/Zend/zend_operators.c @@ -2277,7 +2277,7 @@ try_again: zval *val; val = Z_OBJ_HANDLER_P(op1, get)(op1, &rv); - Z_ADDREF_P(val); + Z_TRY_ADDREF_P(val); increment_function(val); Z_OBJ_HANDLER_P(op1, set)(op1, val); zval_ptr_dtor(val); @@ -2345,7 +2345,7 @@ try_again: zval *val; val = Z_OBJ_HANDLER_P(op1, get)(op1, &rv); - Z_ADDREF_P(val); + Z_TRY_ADDREF_P(val); decrement_function(val); Z_OBJ_HANDLER_P(op1, set)(op1, val); zval_ptr_dtor(val); diff --git a/Zend/zend_virtual_cwd.c b/Zend/zend_virtual_cwd.c index 4206d4d2e8..fedd0a22ad 100644 --- a/Zend/zend_virtual_cwd.c +++ b/Zend/zend_virtual_cwd.c @@ -459,6 +459,9 @@ CWD_API void virtual_cwd_startup(void) /* {{{ */ } } #else +#ifdef ZEND_WIN32 + ZeroMemory(&cwd, sizeof(cwd)); +#endif result = getcwd(cwd, sizeof(cwd)); #endif if (!result) { @@ -1563,6 +1566,20 @@ CWD_API int virtual_chmod(const char *filename, mode_t mode) /* {{{ */ return -1; } +#ifdef ZEND_WIN32 + { + mode_t _tmp = mode; + + mode = 0; + + if (_tmp & _S_IREAD) { + mode |= _S_IREAD; + } + if (_tmp & _S_IWRITE) { + mode |= _S_IWRITE; + } + } +#endif ret = chmod(new_state.cwd, mode); CWD_STATE_FREE_ERR(&new_state); diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index 561d79354b..5cee5bf695 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -5605,8 +5605,14 @@ ZEND_VM_HANDLER(74, ZEND_UNSET_VAR, CONST|TMPVAR|CV, UNUSED, VAR_FETCH) ZVAL_UNDEF(var); zval_dtor_func_for_ptr(garbage); } else { - GC_ZVAL_CHECK_POSSIBLE_ROOT(var); - ZVAL_UNDEF(var); + zval *z = var; + ZVAL_DEREF(z); + if (Z_COLLECTABLE_P(z) && UNEXPECTED(!Z_GC_INFO_P(z))) { + ZVAL_UNDEF(var); + gc_possible_root(Z_COUNTED_P(z)); + } else { + ZVAL_UNDEF(var); + } } } else { ZVAL_UNDEF(var); diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 34f5fbe90e..813879f0a1 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -7737,8 +7737,14 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_VAR_SPEC_CONST_UNUSED_HA ZVAL_UNDEF(var); zval_dtor_func_for_ptr(garbage); } else { - GC_ZVAL_CHECK_POSSIBLE_ROOT(var); - ZVAL_UNDEF(var); + zval *z = var; + ZVAL_DEREF(z); + if (Z_COLLECTABLE_P(z) && UNEXPECTED(!Z_GC_INFO_P(z))) { + ZVAL_UNDEF(var); + gc_possible_root(Z_COUNTED_P(z)); + } else { + ZVAL_UNDEF(var); + } } } else { ZVAL_UNDEF(var); @@ -35008,8 +35014,14 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_VAR_SPEC_CV_UNUSED_HANDL ZVAL_UNDEF(var); zval_dtor_func_for_ptr(garbage); } else { - GC_ZVAL_CHECK_POSSIBLE_ROOT(var); - ZVAL_UNDEF(var); + zval *z = var; + ZVAL_DEREF(z); + if (Z_COLLECTABLE_P(z) && UNEXPECTED(!Z_GC_INFO_P(z))) { + ZVAL_UNDEF(var); + gc_possible_root(Z_COUNTED_P(z)); + } else { + ZVAL_UNDEF(var); + } } } else { ZVAL_UNDEF(var); @@ -43180,8 +43192,14 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_VAR_SPEC_TMPVAR_UNUSED_H ZVAL_UNDEF(var); zval_dtor_func_for_ptr(garbage); } else { - GC_ZVAL_CHECK_POSSIBLE_ROOT(var); - ZVAL_UNDEF(var); + zval *z = var; + ZVAL_DEREF(z); + if (Z_COLLECTABLE_P(z) && UNEXPECTED(!Z_GC_INFO_P(z))) { + ZVAL_UNDEF(var); + gc_possible_root(Z_COUNTED_P(z)); + } else { + ZVAL_UNDEF(var); + } } } else { ZVAL_UNDEF(var); diff --git a/ext/mbstring/mb_gpc.c b/ext/mbstring/mb_gpc.c index db6b5c47cd..0607894447 100644 --- a/ext/mbstring/mb_gpc.c +++ b/ext/mbstring/mb_gpc.c @@ -377,8 +377,10 @@ SAPI_POST_HANDLER_FUNC(php_mb_post_handler) php_stream_rewind(SG(request_info).request_body); post_data_str = php_stream_copy_to_mem(SG(request_info).request_body, PHP_STREAM_COPY_ALL, 0); - detected = _php_mb_encoding_handler_ex(&info, arg, ZSTR_VAL(post_data_str)); - zend_string_release(post_data_str); + detected = _php_mb_encoding_handler_ex(&info, arg, post_data_str ? ZSTR_VAL(post_data_str) : NULL); + if (post_data_str) { + zend_string_release(post_data_str); + } MBSTRG(http_input_identify) = detected; if (detected) { diff --git a/ext/oci8/oci8_statement.c b/ext/oci8/oci8_statement.c index a23ed2bbb1..4ec7d2964a 100644 --- a/ext/oci8/oci8_statement.c +++ b/ext/oci8/oci8_statement.c @@ -1149,8 +1149,13 @@ int php_oci_bind_by_name(php_oci_statement *statement, char *name, size_t name_l return 1; } convert_to_long(var); +#if defined(OCI_MAJOR_VERSION) && OCI_MAJOR_VERSION > 10 + bind_data = (ub8 *)&Z_LVAL_P(var); + value_sz = sizeof(ub8); +#else bind_data = (ub4 *)&Z_LVAL_P(var); value_sz = sizeof(ub4); +#endif mode = OCI_DEFAULT; break; diff --git a/ext/oci8/tests/bug68298.phpt b/ext/oci8/tests/bug68298.phpt new file mode 100644 index 0000000000..223faa5cad --- /dev/null +++ b/ext/oci8/tests/bug68298.phpt @@ -0,0 +1,51 @@ +--TEST-- +Bug #68298 (OCI int overflow) +--SKIPIF-- + +--FILE-- + +===DONE=== + +--EXPECTF-- +array(1) { + ["COL1"]=> + array(2) { + [0]=> + string(19) "1152921504606846975" + [1]=> + string(20) "-1152921504606846975" + } +} +===DONE=== diff --git a/ext/opcache/Optimizer/block_pass.c b/ext/opcache/Optimizer/block_pass.c index 9a740a8739..eeb263a434 100644 --- a/ext/opcache/Optimizer/block_pass.c +++ b/ext/opcache/Optimizer/block_pass.c @@ -643,13 +643,15 @@ static void zend_optimize_block(zend_code_block *block, zend_op_array *op_array, (opline->opcode != ZEND_FE_RESET_R || opline->opcode != ZEND_FE_RESET_RW) && opline->opcode != ZEND_FREE ) { - zend_op *src = VAR_SOURCE(opline->op1); + znode_op op1 = opline->op1; + zend_op *src = VAR_SOURCE(op1); zval c = ZEND_OP1_LITERAL(src); - VAR_UNSET(opline->op1); zval_copy_ctor(&c); - zend_optimizer_update_op1_const(op_array, opline, &c); - literal_dtor(&ZEND_OP1_LITERAL(src)); - MAKE_NOP(src); + if (zend_optimizer_update_op1_const(op_array, opline, &c)) { + VAR_SOURCE(op1) = NULL; + literal_dtor(&ZEND_OP1_LITERAL(src)); + MAKE_NOP(src); + } } /* T = QM_ASSIGN(C), F(T) => NOP, F(C) */ @@ -657,13 +659,15 @@ static void zend_optimize_block(zend_code_block *block, zend_op_array *op_array, VAR_SOURCE(opline->op2) && VAR_SOURCE(opline->op2)->opcode == ZEND_QM_ASSIGN && ZEND_OP1_TYPE(VAR_SOURCE(opline->op2)) == IS_CONST) { - zend_op *src = VAR_SOURCE(opline->op2); + znode_op op2 = opline->op2; + zend_op *src = VAR_SOURCE(op2); zval c = ZEND_OP1_LITERAL(src); - VAR_UNSET(opline->op2); zval_copy_ctor(&c); - zend_optimizer_update_op2_const(op_array, opline, &c); - literal_dtor(&ZEND_OP1_LITERAL(src)); - MAKE_NOP(src); + if (zend_optimizer_update_op2_const(op_array, opline, &c)) { + VAR_SOURCE(op2) = NULL; + literal_dtor(&ZEND_OP1_LITERAL(src)); + MAKE_NOP(src); + } } /* T = CAST(X, String), ECHO(T) => NOP, ECHO(X) */ diff --git a/ext/opcache/Optimizer/zend_optimizer.c b/ext/opcache/Optimizer/zend_optimizer.c index b4bc971286..70cf4ccfe1 100644 --- a/ext/opcache/Optimizer/zend_optimizer.c +++ b/ext/opcache/Optimizer/zend_optimizer.c @@ -110,6 +110,13 @@ int zend_optimizer_add_literal(zend_op_array *op_array, zval *zv) return i; } +static inline int zend_optimizer_add_literal_string(zend_op_array *op_array, zend_string *str) { + zval zv; + ZVAL_STR(&zv, str); + zend_string_hash_val(str); + return zend_optimizer_add_literal(op_array, &zv); +} + int zend_optimizer_is_disabled_func(const char *name, size_t len) { zend_function *fbc = (zend_function *)zend_hash_str_find_ptr(EG(function_table), name, len); @@ -117,182 +124,227 @@ int zend_optimizer_is_disabled_func(const char *name, size_t len) { fbc->internal_function.handler == ZEND_FN(display_disabled_function)); } -void zend_optimizer_update_op1_const(zend_op_array *op_array, - zend_op *opline, - zval *val) -{ - if (opline->opcode == ZEND_FREE) { - MAKE_NOP(opline); +static inline void drop_leading_backslash(zval *val) { + if (Z_STRVAL_P(val)[0] == '\\') { + zend_string *str = zend_string_init(Z_STRVAL_P(val) + 1, Z_STRLEN_P(val) - 1, 0); zval_dtor(val); - } else { - ZEND_OP1_TYPE(opline) = IS_CONST; - if (Z_TYPE_P(val) == IS_STRING) { - switch (opline->opcode) { - case ZEND_INIT_STATIC_METHOD_CALL: - case ZEND_CATCH: - case ZEND_FETCH_CONSTANT: - case ZEND_FETCH_CLASS_CONSTANT: - case ZEND_DEFINED: - case ZEND_NEW: - opline->op1.constant = zend_optimizer_add_literal(op_array, val); - zend_string_hash_val(Z_STR(ZEND_OP1_LITERAL(opline))); - Z_CACHE_SLOT(op_array->literals[opline->op1.constant]) = op_array->cache_size; - op_array->cache_size += sizeof(void*); - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - zend_optimizer_add_literal(op_array, val); - zend_string_hash_val(Z_STR(op_array->literals[opline->op1.constant+1])); - break; - default: - opline->op1.constant = zend_optimizer_add_literal(op_array, val); - zend_string_hash_val(Z_STR(ZEND_OP1_LITERAL(opline))); - break; - } - } else { - if (opline->opcode == ZEND_CONCAT || - opline->opcode == ZEND_FAST_CONCAT) { - convert_to_string(val); - } - opline->op1.constant = zend_optimizer_add_literal(op_array, val); - } + ZVAL_STR(val, str); } } -void zend_optimizer_update_op2_const(zend_op_array *op_array, - zend_op *opline, - zval *val) -{ - ZEND_OP2_TYPE(opline) = IS_CONST; - if (opline->opcode == ZEND_INIT_FCALL) { - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - opline->op2.constant = zend_optimizer_add_literal(op_array, val); - zend_string_hash_val(Z_STR(ZEND_OP2_LITERAL(opline))); - Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = op_array->cache_size; - op_array->cache_size += sizeof(void*); - return; - } else if (opline->opcode == ZEND_ROPE_INIT || - opline->opcode == ZEND_ROPE_ADD || - opline->opcode == ZEND_ROPE_END || - opline->opcode == ZEND_CONCAT || - opline->opcode == ZEND_FAST_CONCAT) { - convert_to_string(val); - } - opline->op2.constant = zend_optimizer_add_literal(op_array, val); - if (Z_TYPE_P(val) == IS_STRING) { - zend_string_hash_val(Z_STR(ZEND_OP2_LITERAL(opline))); - switch (opline->opcode) { - case ZEND_FETCH_CLASS: - case ZEND_INIT_FCALL_BY_NAME: - /*case ZEND_INIT_NS_FCALL_BY_NAME:*/ - case ZEND_ADD_INTERFACE: - case ZEND_ADD_TRAIT: - case ZEND_INSTANCEOF: - case ZEND_FETCH_STATIC_PROP_R: - case ZEND_FETCH_STATIC_PROP_W: - case ZEND_FETCH_STATIC_PROP_RW: - case ZEND_FETCH_STATIC_PROP_IS: - case ZEND_FETCH_STATIC_PROP_UNSET: - case ZEND_FETCH_STATIC_PROP_FUNC_ARG: - case ZEND_UNSET_STATIC_PROP: - case ZEND_ISSET_ISEMPTY_STATIC_PROP: - Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = op_array->cache_size; - op_array->cache_size += sizeof(void*); - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - zend_optimizer_add_literal(op_array, val); - zend_string_hash_val(Z_STR(op_array->literals[opline->op2.constant+1])); - break; - case ZEND_INIT_DYNAMIC_CALL: - opline->opcode = ZEND_INIT_FCALL_BY_NAME; - Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = op_array->cache_size; - op_array->cache_size += sizeof(void*); - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - zend_optimizer_add_literal(op_array, val); - zend_string_hash_val(Z_STR(op_array->literals[opline->op2.constant+1])); - break; - case ZEND_INIT_METHOD_CALL: - case ZEND_INIT_STATIC_METHOD_CALL: - zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); - zend_optimizer_add_literal(op_array, val); - zend_string_hash_val(Z_STR(op_array->literals[opline->op2.constant+1])); - /* break missing intentionally */ - /*case ZEND_FETCH_CLASS_CONSTANT:*/ - case ZEND_ASSIGN_OBJ: - case ZEND_FETCH_OBJ_R: - case ZEND_FETCH_OBJ_W: - case ZEND_FETCH_OBJ_RW: - case ZEND_FETCH_OBJ_IS: - case ZEND_FETCH_OBJ_UNSET: - case ZEND_FETCH_OBJ_FUNC_ARG: - case ZEND_UNSET_OBJ: - case ZEND_PRE_INC_OBJ: - case ZEND_PRE_DEC_OBJ: - case ZEND_POST_INC_OBJ: - case ZEND_POST_DEC_OBJ: - case ZEND_ISSET_ISEMPTY_PROP_OBJ: - Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = op_array->cache_size; - op_array->cache_size += 2 * sizeof(void*); - break; - case ZEND_ASSIGN_ADD: - case ZEND_ASSIGN_SUB: - case ZEND_ASSIGN_MUL: - case ZEND_ASSIGN_DIV: - case ZEND_ASSIGN_POW: - case ZEND_ASSIGN_MOD: - case ZEND_ASSIGN_SL: - case ZEND_ASSIGN_SR: - case ZEND_ASSIGN_CONCAT: - case ZEND_ASSIGN_BW_OR: - case ZEND_ASSIGN_BW_AND: - case ZEND_ASSIGN_BW_XOR: - if (opline->extended_value == ZEND_ASSIGN_OBJ) { - Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = op_array->cache_size; - op_array->cache_size += 2 * sizeof(void*); - } - break; - case ZEND_OP_DATA: - if ((opline-1)->opcode == ZEND_ASSIGN_DIM || - ((opline-1)->extended_value == ZEND_ASSIGN_DIM && - ((opline-1)->opcode == ZEND_ASSIGN_ADD || - (opline-1)->opcode == ZEND_ASSIGN_SUB || - (opline-1)->opcode == ZEND_ASSIGN_MUL || - (opline-1)->opcode == ZEND_ASSIGN_DIV || - (opline-1)->opcode == ZEND_ASSIGN_POW || - (opline-1)->opcode == ZEND_ASSIGN_MOD || - (opline-1)->opcode == ZEND_ASSIGN_SL || - (opline-1)->opcode == ZEND_ASSIGN_SR || - (opline-1)->opcode == ZEND_ASSIGN_CONCAT || - (opline-1)->opcode == ZEND_ASSIGN_BW_OR || - (opline-1)->opcode == ZEND_ASSIGN_BW_AND || - (opline-1)->opcode == ZEND_ASSIGN_BW_XOR))) { - goto check_numeric; - } - break; - case ZEND_ISSET_ISEMPTY_DIM_OBJ: - case ZEND_ADD_ARRAY_ELEMENT: - case ZEND_INIT_ARRAY: - case ZEND_ASSIGN_DIM: - case ZEND_UNSET_DIM: - case ZEND_FETCH_DIM_R: - case ZEND_FETCH_DIM_W: - case ZEND_FETCH_DIM_RW: - case ZEND_FETCH_DIM_IS: - case ZEND_FETCH_DIM_FUNC_ARG: - case ZEND_FETCH_DIM_UNSET: - case ZEND_FETCH_LIST: -check_numeric: - { - zend_ulong index; +static inline void alloc_cache_slots_op2(zend_op_array *op_array, zend_op *opline, uint32_t num) { + Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = op_array->cache_size; + op_array->cache_size += num * sizeof(void *); +} - if (ZEND_HANDLE_NUMERIC(Z_STR_P(val), index)) { - zval_dtor(val); - ZVAL_LONG(val, index); - op_array->literals[opline->op2.constant] = *val; - } - } - break; - default: - break; - } +#define REQUIRES_STRING(val) do { \ + if (Z_TYPE_P(val) != IS_STRING) { \ + zval_dtor(val); \ + return 0; \ + } \ +} while (0) + +#define TO_STRING_NOWARN(val) do { \ + if (Z_TYPE_P(val) >= IS_ARRAY) { \ + zval_dtor(val); \ + return 0; \ + } \ + convert_to_string(val); \ +} while (0) + +int zend_optimizer_update_op1_const(zend_op_array *op_array, + zend_op *opline, + zval *val) +{ + switch (opline->opcode) { + case ZEND_FREE: + MAKE_NOP(opline); + zval_dtor(val); + break; + case ZEND_INIT_STATIC_METHOD_CALL: + case ZEND_CATCH: + case ZEND_FETCH_CONSTANT: + case ZEND_FETCH_CLASS_CONSTANT: + case ZEND_DEFINED: + case ZEND_NEW: + REQUIRES_STRING(val); + ZEND_OP1_TYPE(opline) = IS_CONST; + drop_leading_backslash(val); + opline->op1.constant = zend_optimizer_add_literal(op_array, val); + zend_string_hash_val(Z_STR(ZEND_OP1_LITERAL(opline))); + Z_CACHE_SLOT(op_array->literals[opline->op1.constant]) = op_array->cache_size; + op_array->cache_size += sizeof(void*); + zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val))); + break; + case ZEND_CONCAT: + case ZEND_FAST_CONCAT: + TO_STRING_NOWARN(val); + /* break missing intentionally */ + default: + ZEND_OP1_TYPE(opline) = IS_CONST; + opline->op1.constant = zend_optimizer_add_literal(op_array, val); + if (Z_TYPE_P(val) == IS_STRING) { + zend_string_hash_val(Z_STR(ZEND_OP1_LITERAL(opline))); + } + break; } + + return 1; +} + +int zend_optimizer_update_op2_const(zend_op_array *op_array, + zend_op *opline, + zval *val) +{ + switch (opline->opcode) { + case ZEND_ASSIGN_REF: + zval_dtor(val); + return 0; + case ZEND_FETCH_CLASS: + case ZEND_INIT_FCALL_BY_NAME: + /*case ZEND_INIT_NS_FCALL_BY_NAME:*/ + case ZEND_ADD_INTERFACE: + case ZEND_ADD_TRAIT: + case ZEND_INSTANCEOF: + case ZEND_FETCH_STATIC_PROP_R: + case ZEND_FETCH_STATIC_PROP_W: + case ZEND_FETCH_STATIC_PROP_RW: + case ZEND_FETCH_STATIC_PROP_IS: + case ZEND_FETCH_STATIC_PROP_UNSET: + case ZEND_FETCH_STATIC_PROP_FUNC_ARG: + case ZEND_UNSET_STATIC_PROP: + case ZEND_ISSET_ISEMPTY_STATIC_PROP: + REQUIRES_STRING(val); + drop_leading_backslash(val); + opline->op2.constant = zend_optimizer_add_literal(op_array, val); + zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val))); + alloc_cache_slots_op2(op_array, opline, 1); + break; + case ZEND_INIT_FCALL: + REQUIRES_STRING(val); + zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val)); + opline->op2.constant = zend_optimizer_add_literal(op_array, val); + alloc_cache_slots_op2(op_array, opline, 1); + break; + case ZEND_INIT_DYNAMIC_CALL: + if (Z_TYPE_P(val) == IS_STRING) { + if (zend_memrchr(Z_STRVAL_P(val), ':', Z_STRLEN_P(val))) { + zval_dtor(val); + return 0; + } + + opline->opcode = ZEND_INIT_FCALL_BY_NAME; + drop_leading_backslash(val); + opline->op2.constant = zend_optimizer_add_literal(op_array, val); + zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val))); + alloc_cache_slots_op2(op_array, opline, 1); + } else { + opline->op2.constant = zend_optimizer_add_literal(op_array, val); + } + break; + case ZEND_INIT_METHOD_CALL: + case ZEND_INIT_STATIC_METHOD_CALL: + REQUIRES_STRING(val); + opline->op2.constant = zend_optimizer_add_literal(op_array, val); + zend_optimizer_add_literal_string(op_array, zend_string_tolower(Z_STR_P(val))); + alloc_cache_slots_op2(op_array, opline, 2); + break; + /*case ZEND_FETCH_CLASS_CONSTANT:*/ + case ZEND_ASSIGN_OBJ: + case ZEND_FETCH_OBJ_R: + case ZEND_FETCH_OBJ_W: + case ZEND_FETCH_OBJ_RW: + case ZEND_FETCH_OBJ_IS: + case ZEND_FETCH_OBJ_UNSET: + case ZEND_FETCH_OBJ_FUNC_ARG: + case ZEND_UNSET_OBJ: + case ZEND_PRE_INC_OBJ: + case ZEND_PRE_DEC_OBJ: + case ZEND_POST_INC_OBJ: + case ZEND_POST_DEC_OBJ: + case ZEND_ISSET_ISEMPTY_PROP_OBJ: + TO_STRING_NOWARN(val); + opline->op2.constant = zend_optimizer_add_literal(op_array, val); + alloc_cache_slots_op2(op_array, opline, 2); + break; + case ZEND_ASSIGN_ADD: + case ZEND_ASSIGN_SUB: + case ZEND_ASSIGN_MUL: + case ZEND_ASSIGN_DIV: + case ZEND_ASSIGN_POW: + case ZEND_ASSIGN_MOD: + case ZEND_ASSIGN_SL: + case ZEND_ASSIGN_SR: + case ZEND_ASSIGN_CONCAT: + case ZEND_ASSIGN_BW_OR: + case ZEND_ASSIGN_BW_AND: + case ZEND_ASSIGN_BW_XOR: + if (opline->extended_value == ZEND_ASSIGN_OBJ) { + TO_STRING_NOWARN(val); + opline->op2.constant = zend_optimizer_add_literal(op_array, val); + alloc_cache_slots_op2(op_array, opline, 2); + } else { + opline->op2.constant = zend_optimizer_add_literal(op_array, val); + } + break; + case ZEND_OP_DATA: + if ((opline-1)->opcode != ZEND_ASSIGN_DIM && + ((opline-1)->extended_value != ZEND_ASSIGN_DIM || + ((opline-1)->opcode != ZEND_ASSIGN_ADD && + (opline-1)->opcode != ZEND_ASSIGN_SUB && + (opline-1)->opcode != ZEND_ASSIGN_MUL && + (opline-1)->opcode != ZEND_ASSIGN_DIV && + (opline-1)->opcode != ZEND_ASSIGN_POW && + (opline-1)->opcode != ZEND_ASSIGN_MOD && + (opline-1)->opcode != ZEND_ASSIGN_SL && + (opline-1)->opcode != ZEND_ASSIGN_SR && + (opline-1)->opcode != ZEND_ASSIGN_CONCAT && + (opline-1)->opcode != ZEND_ASSIGN_BW_OR && + (opline-1)->opcode != ZEND_ASSIGN_BW_AND && + (opline-1)->opcode != ZEND_ASSIGN_BW_XOR)) + ) { + opline->op2.constant = zend_optimizer_add_literal(op_array, val); + break; + } + /* break missing intentionally */ + case ZEND_ISSET_ISEMPTY_DIM_OBJ: + case ZEND_ADD_ARRAY_ELEMENT: + case ZEND_INIT_ARRAY: + case ZEND_ASSIGN_DIM: + case ZEND_UNSET_DIM: + case ZEND_FETCH_DIM_R: + case ZEND_FETCH_DIM_W: + case ZEND_FETCH_DIM_RW: + case ZEND_FETCH_DIM_IS: + case ZEND_FETCH_DIM_FUNC_ARG: + case ZEND_FETCH_DIM_UNSET: + case ZEND_FETCH_LIST: + if (Z_TYPE_P(val) == IS_STRING) { + zend_ulong index; + if (ZEND_HANDLE_NUMERIC(Z_STR_P(val), index)) { + zval_dtor(val); + ZVAL_LONG(val, index); + } + } + opline->op2.constant = zend_optimizer_add_literal(op_array, val); + break; + case ZEND_ROPE_INIT: + case ZEND_ROPE_ADD: + case ZEND_ROPE_END: + case ZEND_CONCAT: + case ZEND_FAST_CONCAT: + TO_STRING_NOWARN(val); + /* break missing intentionally */ + default: + opline->op2.constant = zend_optimizer_add_literal(op_array, val); + break; + } + + ZEND_OP2_TYPE(opline) = IS_CONST; + if (Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_STRING) { + zend_string_hash_val(Z_STR(ZEND_OP2_LITERAL(opline))); + } + return 1; } int zend_optimizer_replace_by_const(zend_op_array *op_array, @@ -313,6 +365,8 @@ int zend_optimizer_replace_by_const(zend_op_array *op_array, case ZEND_FETCH_DIM_UNSET: case ZEND_ASSIGN_DIM: case ZEND_SEPARATE: + case ZEND_RETURN_BY_REF: + zval_dtor(val); return 0; case ZEND_SEND_VAR: opline->extended_value = 0; @@ -335,6 +389,9 @@ int zend_optimizer_replace_by_const(zend_op_array *op_array, opline->opcode = ZEND_SEND_VAL; } break; + case ZEND_SEND_USER: + opline->opcode = ZEND_SEND_VAL_EX; + break; /* In most cases IS_TMP_VAR operand may be used only once. * The operands are usually destroyed by the opcode handler. * ZEND_CASE is an exception, that keeps operand unchanged, @@ -400,21 +457,12 @@ int zend_optimizer_replace_by_const(zend_op_array *op_array, default: break; } - zend_optimizer_update_op1_const(op_array, opline, val); - break; + return zend_optimizer_update_op1_const(op_array, opline, val); } if (ZEND_OP2_TYPE(opline) == type && ZEND_OP2(opline).var == var) { - switch (opline->opcode) { - case ZEND_ASSIGN_REF: - zval_dtor(val); - return 0; - default: - break; - } - zend_optimizer_update_op2_const(op_array, opline, val); - break; + return zend_optimizer_update_op2_const(op_array, opline, val); } opline++; } diff --git a/ext/opcache/Optimizer/zend_optimizer_internal.h b/ext/opcache/Optimizer/zend_optimizer_internal.h index ca452670d7..90df3cfadc 100644 --- a/ext/opcache/Optimizer/zend_optimizer_internal.h +++ b/ext/opcache/Optimizer/zend_optimizer_internal.h @@ -118,12 +118,12 @@ int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int void zend_optimizer_collect_constant(zend_optimizer_ctx *ctx, zval *name, zval* value); int zend_optimizer_get_collected_constant(HashTable *constants, zval *name, zval* value); int zend_optimizer_lookup_cv(zend_op_array *op_array, zend_string* name); -void zend_optimizer_update_op1_const(zend_op_array *op_array, - zend_op *opline, - zval *val); -void zend_optimizer_update_op2_const(zend_op_array *op_array, - zend_op *opline, - zval *val); +int zend_optimizer_update_op1_const(zend_op_array *op_array, + zend_op *opline, + zval *val); +int zend_optimizer_update_op2_const(zend_op_array *op_array, + zend_op *opline, + zval *val); int zend_optimizer_replace_by_const(zend_op_array *op_array, zend_op *opline, zend_uchar type, diff --git a/ext/opcache/zend_file_cache.c b/ext/opcache/zend_file_cache.c index a2a5343deb..7c615e927b 100644 --- a/ext/opcache/zend_file_cache.c +++ b/ext/opcache/zend_file_cache.c @@ -20,6 +20,7 @@ #include "zend_virtual_cwd.h" #include "zend_compile.h" #include "zend_vm.h" +#include "zend_interfaces.h" #include "php.h" @@ -35,6 +36,10 @@ #include #include +#if HAVE_UNISTD_H +#include +#endif + #ifdef HAVE_SYS_UIO_H # include #endif @@ -801,10 +806,12 @@ int zend_file_cache_script_store(zend_persistent_script *script, int in_shm) static void zend_file_cache_unserialize_hash(HashTable *ht, zend_persistent_script *script, void *buf, - unserialize_callback_t func) + unserialize_callback_t func, + dtor_func_t dtor) { Bucket *p, *end; + ht->pDestructor = dtor; if (!(ht->u.flags & HASH_FLAG_INITIALIZED)) { HT_SET_DATA_ADDR(ht, &uninitialized_bucket); return; @@ -869,7 +876,8 @@ static void zend_file_cache_unserialize_zval(zval *zv, UNSERIALIZE_PTR(Z_ARR_P(zv)); ht = Z_ARR_P(zv); - zend_file_cache_unserialize_hash(ht, script, buf, zend_file_cache_unserialize_zval); + zend_file_cache_unserialize_hash(ht, + script, buf, zend_file_cache_unserialize_zval, ZVAL_PTR_DTOR); } break; case IS_REFERENCE: @@ -904,7 +912,8 @@ static void zend_file_cache_unserialize_op_array(zend_op_array *op_arr UNSERIALIZE_PTR(op_array->static_variables); ht = op_array->static_variables; - zend_file_cache_unserialize_hash(ht, script, buf, zend_file_cache_unserialize_zval); + zend_file_cache_unserialize_hash(ht, + script, buf, zend_file_cache_unserialize_zval, ZVAL_PTR_DTOR); } if (op_array->literals && !IS_UNSERIALIZED(op_array->literals)) { @@ -1056,7 +1065,8 @@ static void zend_file_cache_unserialize_class(zval *zv, ce = Z_PTR_P(zv); UNSERIALIZE_STR(ce->name); - zend_file_cache_unserialize_hash(&ce->function_table, script, buf, zend_file_cache_unserialize_func); + zend_file_cache_unserialize_hash(&ce->function_table, + script, buf, zend_file_cache_unserialize_func, ZEND_FUNCTION_DTOR); if (ce->default_properties_table) { zval *p, *end; @@ -1079,10 +1089,12 @@ static void zend_file_cache_unserialize_class(zval *zv, p++; } } - zend_file_cache_unserialize_hash(&ce->constants_table, script, buf, zend_file_cache_unserialize_zval); + zend_file_cache_unserialize_hash(&ce->constants_table, + script, buf, zend_file_cache_unserialize_zval, NULL); UNSERIALIZE_STR(ZEND_CE_FILENAME(ce)); UNSERIALIZE_STR(ZEND_CE_DOC_COMMENT(ce)); - zend_file_cache_unserialize_hash(&ce->properties_info, script, buf, zend_file_cache_unserialize_prop_info); + zend_file_cache_unserialize_hash(&ce->properties_info, + script, buf, zend_file_cache_unserialize_prop_info, ZVAL_PTR_DTOR); if (ce->trait_aliases) { zend_trait_alias **p, *q; @@ -1168,6 +1180,11 @@ static void zend_file_cache_unserialize_class(zval *zv, UNSERIALIZE_PTR(ce->__tostring); UNSERIALIZE_PTR(ce->__callstatic); UNSERIALIZE_PTR(ce->__debugInfo); + + if (UNEXPECTED((ce->ce_flags & ZEND_ACC_ANON_CLASS))) { + ce->serialize = zend_class_serialize_deny; + ce->unserialize = zend_class_unserialize_deny; + } } static void zend_file_cache_unserialize(zend_persistent_script *script, @@ -1177,8 +1194,10 @@ static void zend_file_cache_unserialize(zend_persistent_script *script, UNSERIALIZE_STR(script->full_path); - zend_file_cache_unserialize_hash(&script->class_table, script, buf, zend_file_cache_unserialize_class); - zend_file_cache_unserialize_hash(&script->function_table, script, buf, zend_file_cache_unserialize_func); + zend_file_cache_unserialize_hash(&script->class_table, + script, buf, zend_file_cache_unserialize_class, ZEND_CLASS_DTOR); + zend_file_cache_unserialize_hash(&script->function_table, + script, buf, zend_file_cache_unserialize_func, ZEND_FUNCTION_DTOR); zend_file_cache_unserialize_op_array(&script->main_op_array, script, buf); UNSERIALIZE_PTR(script->arena_mem); diff --git a/ext/pdo/pdo_sql_parser.c b/ext/pdo/pdo_sql_parser.c index 155ca95c1c..22e5fcf35b 100644 --- a/ext/pdo/pdo_sql_parser.c +++ b/ext/pdo/pdo_sql_parser.c @@ -575,6 +575,9 @@ safe: zend_string *buf; buf = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0); + if (!buf) { + buf = ZSTR_EMPTY_ALLOC(); + } if (!stmt->dbh->methods->quoter(stmt->dbh, ZSTR_VAL(buf), ZSTR_LEN(buf), &plc->quoted, &plc->qlen, param->param_type)) { /* bork */ diff --git a/ext/pdo/pdo_sql_parser.re b/ext/pdo/pdo_sql_parser.re index 9922e03690..629f3079c2 100644 --- a/ext/pdo/pdo_sql_parser.re +++ b/ext/pdo/pdo_sql_parser.re @@ -217,6 +217,9 @@ safe: zend_string *buf; buf = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0); + if (!buf) { + buf = ZSTR_EMPTY_ALLOC(); + } if (!stmt->dbh->methods->quoter(stmt->dbh, ZSTR_VAL(buf), ZSTR_LEN(buf), &plc->quoted, &plc->qlen, param->param_type)) { /* bork */ diff --git a/ext/pdo_firebird/firebird_statement.c b/ext/pdo_firebird/firebird_statement.c index 1e0151dc9d..5445368bc9 100644 --- a/ext/pdo_firebird/firebird_statement.c +++ b/ext/pdo_firebird/firebird_statement.c @@ -523,12 +523,13 @@ static int firebird_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_dat } if (Z_TYPE_P(parameter) == IS_RESOURCE) { - php_stream *stm; + php_stream *stm = NULL; php_stream_from_zval_no_verify(stm, parameter); if (stm) { + zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0); zval_ptr_dtor(parameter); - ZVAL_STR(parameter, php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0)); + ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC()); } else { pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource"); return 0; diff --git a/ext/pdo_mysql/mysql_statement.c b/ext/pdo_mysql/mysql_statement.c index f55843072f..2c7e39bbbd 100644 --- a/ext/pdo_mysql/mysql_statement.c +++ b/ext/pdo_mysql/mysql_statement.c @@ -523,10 +523,12 @@ static int pdo_mysql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_da parameter = Z_REFVAL(param->parameter); } if (Z_TYPE_P(parameter) == IS_RESOURCE) { - php_stream *stm; + php_stream *stm = NULL; php_stream_from_zval_no_verify(stm, parameter); if (stm) { - ZVAL_STR(parameter, php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0)); + zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0); + zval_ptr_dtor(parameter); + ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC()); } else { pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource"); return 0; diff --git a/ext/pdo_mysql/tests/bug70862.phpt b/ext/pdo_mysql/tests/bug70862.phpt new file mode 100644 index 0000000000..4a3a618854 --- /dev/null +++ b/ext/pdo_mysql/tests/bug70862.phpt @@ -0,0 +1,46 @@ +--TEST-- +MySQL Prepared Statements and BLOBs +--SKIPIF-- + +--FILE-- +exec('DROP TABLE IF EXISTS test'); + $db->exec(sprintf('CREATE TABLE test(id INT, label BLOB)')); + + $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0); + $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true); + + class HelloWrapper { + public function stream_open() { return true; } + public function stream_eof() { return true; } + public function stream_read() { return NULL; } + public function stream_stat() { return array(); } + } + stream_wrapper_register("hello", "HelloWrapper"); + + $f = fopen("hello://there", "r"); + + $stmt = $db->prepare('INSERT INTO test(id, label) VALUES (1, :para)'); + $stmt->bindParam(":para", $f, PDO::PARAM_LOB); + $stmt->execute(); + + var_dump($f); + + print "done!"; +?> +--CLEAN-- +exec('DROP TABLE IF EXISTS test'); +?> +--EXPECTF-- +string(0) "" +done! diff --git a/ext/pdo_pgsql/tests/bug70861.phpt b/ext/pdo_pgsql/tests/bug70861.phpt new file mode 100644 index 0000000000..7edebba1e6 --- /dev/null +++ b/ext/pdo_pgsql/tests/bug70861.phpt @@ -0,0 +1,46 @@ +--TEST-- +Bug #70861 Segmentation fault in pdo_parse_params() during Drupal 8 test suite +--SKIPIF-- + +--FILE-- +setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true); + +try { + @$db->query("SET bytea_output = 'escape'"); +} catch (Exception $e) { +} + +$db->query('DROP TABLE IF EXISTS test_blob_crash CASCADE'); +$db->query('CREATE TABLE test_blob_crash (id SERIAL NOT NULL, blob1 BYTEA)'); + +class HelloWrapper { + public function stream_open() { return true; } + public function stream_eof() { return true; } + public function stream_read() { return NULL; } + public function stream_stat() { return array(); } +} +stream_wrapper_register("hello", "HelloWrapper"); + +$f = fopen("hello://there", "r"); + +$stmt = $db->prepare("INSERT INTO test_one_blob (blob1) VALUES (:foo)", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL)); + +$stmt->bindparam(':foo', $f, PDO::PARAM_LOB); +$stmt->execute(); + +fclose($f); + +?> ++++DONE+++ +--EXPECTF-- +%a ++++DONE+++ diff --git a/ext/pdo_sqlite/sqlite_statement.c b/ext/pdo_sqlite/sqlite_statement.c index 9b74b24212..efe84c11cd 100644 --- a/ext/pdo_sqlite/sqlite_statement.c +++ b/ext/pdo_sqlite/sqlite_statement.c @@ -138,11 +138,12 @@ static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_d parameter = ¶m->parameter; } if (Z_TYPE_P(parameter) == IS_RESOURCE) { - php_stream *stm; + php_stream *stm = NULL; php_stream_from_zval_no_verify(stm, parameter); if (stm) { + zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0); zval_ptr_dtor(parameter); - ZVAL_STR(parameter, php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0)); + ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC()); } else { pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource"); return 0; diff --git a/ext/pdo_sqlite/tests/bug70862.phpt b/ext/pdo_sqlite/tests/bug70862.phpt new file mode 100644 index 0000000000..3df1e4f2bd --- /dev/null +++ b/ext/pdo_sqlite/tests/bug70862.phpt @@ -0,0 +1,36 @@ +--TEST-- +PDO_sqlite: Testing sqliteCreateCollation() +--SKIPIF-- + +--FILE-- +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + +$db->exec('CREATE TABLE test(field BLOB)'); + +$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0); +$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true); + +class HelloWrapper { + public function stream_open() { return true; } + public function stream_eof() { return true; } + public function stream_read() { return NULL; } + public function stream_stat() { return array(); } +} +stream_wrapper_register("hello", "HelloWrapper"); + +$f = fopen("hello://there", "r"); + +$stmt = $db->prepare('INSERT INTO test(field) VALUES (:para)'); +$stmt->bindParam(":para", $f, PDO::PARAM_LOB); +$stmt->execute(); + +var_dump($f); + +?> ++++DONE+++ +--EXPECTF-- +string(0) "" ++++DONE+++ diff --git a/ext/reflection/tests/ReflectionClass_CannotClone_basic.phpt b/ext/reflection/tests/ReflectionClass_CannotClone_basic.phpt index 900ce21d45..bbe5fdbaec 100644 --- a/ext/reflection/tests/ReflectionClass_CannotClone_basic.phpt +++ b/ext/reflection/tests/ReflectionClass_CannotClone_basic.phpt @@ -5,7 +5,7 @@ Stefan Koopmanschap TestFest PHP|Tek --SKIPIF-- --FILE-- basedir); efree(data); - *mod_data = NULL; + PS_SET_MOD_DATA(NULL); return SUCCESS; } diff --git a/ext/session/session.c b/ext/session/session.c index 570173a838..dd8651883d 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -2077,6 +2077,11 @@ static PHP_FUNCTION(session_regenerate_id) php_error_docref(NULL, E_RECOVERABLE_ERROR, "Failed to create session ID: %s (path: %s)", PS(mod)->s_name, PS(save_path)); RETURN_FALSE; } + if (PS(mod)->s_open(&PS(mod_data), PS(save_path), PS(session_name)) == FAILURE) { + PS(session_status) = php_session_none; + php_error_docref(NULL, E_RECOVERABLE_ERROR, "Failed to create session ID: %s (path: %s)", PS(mod)->s_name, PS(save_path)); + RETURN_FALSE; + } if (PS(use_strict_mode) && PS(mod)->s_validate_sid && PS(mod)->s_validate_sid(&PS(mod_data), PS(id)) == FAILURE) { zend_string_release(PS(id)); @@ -2087,11 +2092,6 @@ static PHP_FUNCTION(session_regenerate_id) RETURN_FALSE; } } - if (PS(mod)->s_open(&PS(mod_data), PS(save_path), PS(session_name)) == FAILURE) { - PS(session_status) = php_session_none; - php_error_docref(NULL, E_RECOVERABLE_ERROR, "Failed to create session ID: %s (path: %s)", PS(mod)->s_name, PS(save_path)); - RETURN_FALSE; - } /* Read is required to make new session data at this point. */ if (PS(mod)->s_read(&PS(mod_data), PS(id), &data, PS(gc_maxlifetime)) == FAILURE) { PS(session_status) = php_session_none; diff --git a/ext/session/tests/bug70876.phpt b/ext/session/tests/bug70876.phpt new file mode 100644 index 0000000000..33e0323b9f --- /dev/null +++ b/ext/session/tests/bug70876.phpt @@ -0,0 +1,17 @@ +--TEST-- +Bug #70876 Segmentation fault when regenerating session id with strict mode +--SKIPIF-- + +--INI-- +html_errors=0 +session.save_handler=files +--FILE-- + +--EXPECT-- +ok \ No newline at end of file diff --git a/ext/soap/php_sdl.c b/ext/soap/php_sdl.c index 84166780a6..6efd2b06c0 100644 --- a/ext/soap/php_sdl.c +++ b/ext/soap/php_sdl.c @@ -112,7 +112,7 @@ encodePtr get_encoder(sdlPtr sdl, const char *ns, const char *type) { encodePtr enc = NULL; char *nscat; - int ns_len = strlen(ns); + int ns_len = ns ? strlen(ns) : 0; int type_len = strlen(type); int len = ns_len + type_len + 1; diff --git a/ext/soap/tests/bug70875.phpt b/ext/soap/tests/bug70875.phpt new file mode 100644 index 0000000000..6f10462bfc --- /dev/null +++ b/ext/soap/tests/bug70875.phpt @@ -0,0 +1,50 @@ +--TEST-- +SOAP Bug #70875 - Segmentation fault if wsdl has no targetNamespace attribute +--SKIPIF-- + +--INI-- +soap.wsdl_cache_enabled=0 +--FILE-- + 1, + 'classmap' => [ + 'TestService' => 'TestService', + 'TestServiceRQ' => 'TestServiceRQ', + 'RqHeader' => 'RqHeader', + ], +]); + + +class TestService +{ + public $TestServiceRQ; +} + +class TestServiceRQ +{ + public $RqHeader; +} + +class RqHeader +{ +} + +$r = new TestService(); +$r->TestServiceRQ = new TestServiceRQ(); +$r->TestServiceRQ->RqHeader = new RqHeader(); + +$c->testService($r); + +?> +--EXPECT-- +no SIGSEGV diff --git a/ext/soap/tests/bug70875.wsdl b/ext/soap/tests/bug70875.wsdl new file mode 100644 index 0000000000..f874b9ae6c --- /dev/null +++ b/ext/soap/tests/bug70875.wsdl @@ -0,0 +1,156 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ext/sockets/sockets.c b/ext/sockets/sockets.c index 4385093fe9..39bfd2bf49 100644 --- a/ext/sockets/sockets.c +++ b/ext/sockets/sockets.c @@ -716,6 +716,10 @@ static PHP_MINIT_FUNCTION(sockets) REGISTER_LONG_CONSTANT("IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP, CONST_CS | CONST_PERSISTENT); #endif +#ifdef IPV6_V6ONLY + REGISTER_LONG_CONSTANT("IPV6_V6ONLY", IPV6_V6ONLY, CONST_CS | CONST_PERSISTENT); +#endif + #ifndef WIN32 # include "unix_socket_constants.h" #else diff --git a/ext/spl/spl_engine.c b/ext/spl/spl_engine.c index c9960d69ef..b3d1a3e9f5 100644 --- a/ext/spl/spl_engine.c +++ b/ext/spl/spl_engine.c @@ -42,6 +42,7 @@ PHPAPI zend_long spl_offset_convert_to_long(zval *offset) /* {{{ */ { zend_ulong idx; +try_again: switch (Z_TYPE_P(offset)) { case IS_STRING: if (ZEND_HANDLE_NUMERIC(Z_STR_P(offset), idx)) { @@ -56,6 +57,9 @@ PHPAPI zend_long spl_offset_convert_to_long(zval *offset) /* {{{ */ return 0; case IS_TRUE: return 1; + case IS_REFERENCE: + offset = Z_REFVAL_P(offset); + goto try_again; case IS_RESOURCE: return Z_RES_HANDLE_P(offset); } diff --git a/ext/spl/spl_iterators.c b/ext/spl/spl_iterators.c index cd0c624b50..b29fc5298b 100644 --- a/ext/spl/spl_iterators.c +++ b/ext/spl/spl_iterators.c @@ -2059,6 +2059,11 @@ SPL_METHOD(RegexIterator, accept) { case REGIT_MODE_MAX: /* won't happen but makes compiler happy */ case REGIT_MODE_MATCH: +#ifdef PCRE_EXTRA_MARK + if (intern->u.regex.pce->extra) { + intern->u.regex.pce->extra->flags &= ~PCRE_EXTRA_MARK; + } +#endif count = pcre_exec(intern->u.regex.pce->re, intern->u.regex.pce->extra, ZSTR_VAL(subject), ZSTR_LEN(subject), 0, 0, NULL, 0); RETVAL_BOOL(count >= 0); break; diff --git a/ext/spl/tests/bug70852.phpt b/ext/spl/tests/bug70852.phpt new file mode 100644 index 0000000000..2b23ce4e09 --- /dev/null +++ b/ext/spl/tests/bug70852.phpt @@ -0,0 +1,17 @@ +--TEST-- +Bug #70852 Segfault getting NULL offset of an ArrayObject +--FILE-- + +===DONE=== +--EXPECTF-- +Notice: Undefined index: in %s on line %d +NULL + +Notice: Undefined index: in %s on line %d +NULL +===DONE=== diff --git a/ext/spl/tests/bug70853.phpt b/ext/spl/tests/bug70853.phpt new file mode 100644 index 0000000000..99530ece40 --- /dev/null +++ b/ext/spl/tests/bug70853.phpt @@ -0,0 +1,15 @@ +--TEST-- +Bug #70853 (SplFixedArray throws exception when using ref variable as index) +--FILE-- + +--EXPECT-- +ok diff --git a/ext/spl/tests/bug70868.phpt b/ext/spl/tests/bug70868.phpt new file mode 100644 index 0000000000..d3dd76354a --- /dev/null +++ b/ext/spl/tests/bug70868.phpt @@ -0,0 +1,32 @@ +--TEST-- +Bug #70868, with PCRE JIT +--INI-- +pcre.jit=1 +--SKIPIF-- + +--FILE-- + +--EXPECTF-- + +string(6) "A.phpt" +string(6) "B.phpt" +string(6) "C.phpt" +Done diff --git a/ext/sqlite3/sqlite3.c b/ext/sqlite3/sqlite3.c index 12340f991e..4e3cc28eda 100644 --- a/ext/sqlite3/sqlite3.c +++ b/ext/sqlite3/sqlite3.c @@ -1527,7 +1527,7 @@ PHP_METHOD(sqlite3stmt, execute) case SQLITE_BLOB: { php_stream *stream = NULL; - zend_string *buffer; + zend_string *buffer = NULL; if (Z_TYPE_P(parameter) == IS_RESOURCE) { php_stream_from_zval_no_verify(stream, parameter); if (stream == NULL) { @@ -1540,10 +1540,11 @@ PHP_METHOD(sqlite3stmt, execute) buffer = Z_STR_P(parameter); } - sqlite3_bind_blob(stmt_obj->stmt, param->param_number, ZSTR_VAL(buffer), ZSTR_LEN(buffer), SQLITE_TRANSIENT); - - if (stream) { + if (buffer) { + sqlite3_bind_blob(stmt_obj->stmt, param->param_number, ZSTR_VAL(buffer), ZSTR_LEN(buffer), SQLITE_TRANSIENT); zend_string_release(buffer); + } else { + sqlite3_bind_null(stmt_obj->stmt, param->param_number); } break; } diff --git a/ext/sqlite3/tests/sqlite3_blob_bind_resource.phpt b/ext/sqlite3/tests/sqlite3_blob_bind_resource.phpt new file mode 100644 index 0000000000..108956b87b --- /dev/null +++ b/ext/sqlite3/tests/sqlite3_blob_bind_resource.phpt @@ -0,0 +1,39 @@ +--TEST-- +SQLite3::execute() with a resource bound for blob param +--SKIPIF-- + +--FILE-- +exec('CREATE TABLE test (id STRING, data BLOB)')); +$insert_stmt = $db->prepare("INSERT INTO test (id, data) VALUES (1, ?)"); + + +class HelloWrapper { + public function stream_open() { return true; } + public function stream_eof() { return true; } + public function stream_read() { return NULL; } + public function stream_stat() { return array(); } +} +stream_wrapper_register("hello", "HelloWrapper"); + +$f = fopen("hello://there", "r"); + +var_dump($insert_stmt->bindParam(1, $f, SQLITE3_BLOB)); +var_dump($insert_stmt->execute()); + +var_dump($insert_stmt->close()); +fclose($f); + +?> ++++DONE+++ +--EXPECTF-- +bool(true) +bool(true) +object(SQLite3Result)#%d (%d) { +} +bool(true) ++++DONE+++ diff --git a/ext/standard/image.c b/ext/standard/image.c index 378423917e..7f100d68f9 100644 --- a/ext/standard/image.c +++ b/ext/standard/image.c @@ -217,11 +217,16 @@ static struct gfxinfo *php_handle_swc(php_stream * stream) if (uncompress(b, &len, a, sizeof(a)) != Z_OK) { /* failed to decompress the file, will try reading the rest of the file */ - if (php_stream_seek(stream, 8, SEEK_SET)) + if (php_stream_seek(stream, 8, SEEK_SET)) { return NULL; + } bufz = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0); + if (!bufz) { + return NULL; + } + /* * zlib::uncompress() wants to know the output data length * if none was given as a parameter diff --git a/main/network.c b/main/network.c index c08194bcd6..cb112464d1 100644 --- a/main/network.c +++ b/main/network.c @@ -472,6 +472,12 @@ php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned po #ifdef SO_REUSEADDR setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&sockoptval, sizeof(sockoptval)); #endif +#ifdef IPV6_V6ONLY + if (sockopts & STREAM_SOCKOP_IPV6_V6ONLY) { + int ipv6_val = !!(sockopts & STREAM_SOCKOP_IPV6_V6ONLY_ENABLED); + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&ipv6_val, sizeof(sockoptval)); + } +#endif #ifdef SO_REUSEPORT if (sockopts & STREAM_SOCKOP_SO_REUSEPORT) { setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (char*)&sockoptval, sizeof(sockoptval)); diff --git a/main/php_network.h b/main/php_network.h index d17530208a..a0b87797b6 100644 --- a/main/php_network.h +++ b/main/php_network.h @@ -106,9 +106,11 @@ typedef int php_socket_t; # define SOCK_RECV_ERR -1 #endif -#define STREAM_SOCKOP_NONE 1 << 0 -#define STREAM_SOCKOP_SO_REUSEPORT 1 << 1 -#define STREAM_SOCKOP_SO_BROADCAST 1 << 2 +#define STREAM_SOCKOP_NONE (1 << 0) +#define STREAM_SOCKOP_SO_REUSEPORT (1 << 1) +#define STREAM_SOCKOP_SO_BROADCAST (1 << 2) +#define STREAM_SOCKOP_IPV6_V6ONLY (1 << 3) +#define STREAM_SOCKOP_IPV6_V6ONLY_ENABLED (1 << 4) /* uncomment this to debug poll(2) emulation on systems that have poll(2) */ diff --git a/main/streams/streams.c b/main/streams/streams.c index 3ad44b07a9..d0f8a44b98 100644 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -84,6 +84,7 @@ fprintf(stderr, "forget_persistent: %s:%p\n", stream->ops->label, stream); if (stream->ctx) { zend_list_delete(stream->ctx); + stream->ctx = NULL; } return 0; diff --git a/main/streams/xp_socket.c b/main/streams/xp_socket.c index e88bb91208..be4edd76e9 100644 --- a/main/streams/xp_socket.c +++ b/main/streams/xp_socket.c @@ -635,6 +635,16 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t * return -1; } +#ifdef IPV6_V6ONLY + if (PHP_STREAM_CONTEXT(stream) + && (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "ipv6_v6only")) != NULL + && Z_TYPE_P(tmpzval) != IS_NULL + ) { + sockopts |= STREAM_SOCKOP_IPV6_V6ONLY; + sockopts |= STREAM_SOCKOP_IPV6_V6ONLY_ENABLED * zend_is_true(tmpzval); + } +#endif + #ifdef SO_REUSEPORT if (PHP_STREAM_CONTEXT(stream) && (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "so_reuseport")) != NULL diff --git a/php.ini-production b/php.ini-production index 368794f700..cc3ffa096b 100644 --- a/php.ini-production +++ b/php.ini-production @@ -1842,7 +1842,7 @@ ldap.max_links = -1 ; Enables or disables copying of PHP code (text segment) into HUGE PAGES. ; This should improve performance, but requires appropriate OS configuration. -opcache.huge_code_pages=1 +;opcache.huge_code_pages=1 [curl] ; A default value for the CURLOPT_CAINFO option. This is required to be an diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c index 6293470472..63f7dfca6b 100644 --- a/sapi/cgi/cgi_main.c +++ b/sapi/cgi/cgi_main.c @@ -31,7 +31,6 @@ #include "SAPI.h" #include -#include "php.h" #ifdef PHP_WIN32 # include "win32/time.h" diff --git a/sapi/cli/tests/006.phpt b/sapi/cli/tests/006.phpt index 1170ea217a..b24da56660 100644 --- a/sapi/cli/tests/006.phpt +++ b/sapi/cli/tests/006.phpt @@ -25,7 +25,7 @@ string(44) "Exception: Extension unknown does not exist " string(37) "Exception: Extension does not exist " -string(%d) "Extension [ extension #%d pcre version %d.%d.%d%a ] { +string(%d) "Extension [ extension #%d pcre version %s ] { - INI { Entry [ pcre.backtrack_limit ] diff --git a/sapi/fpm/tests/008.phpt b/sapi/fpm/tests/008.phpt index 60f1ea6ebd..732a717aad 100644 --- a/sapi/fpm/tests/008.phpt +++ b/sapi/fpm/tests/008.phpt @@ -1,7 +1,22 @@ --TEST-- FPM: Test multi pool (dynamic + ondemand + static) (bug #68423) --SKIPIF-- - + --FILE-- + --FILE-- \ No newline at end of file +?> diff --git a/sapi/fpm/tests/021-uds-acl.phpt b/sapi/fpm/tests/021-uds-acl.phpt index f39c526418..f5da086702 100644 --- a/sapi/fpm/tests/021-uds-acl.phpt +++ b/sapi/fpm/tests/021-uds-acl.phpt @@ -4,9 +4,16 @@ FPM: Test Unix Domain Socket with Posix ACL ---XFAIL-- -Mark as XFAIL because --with-fpm-acl is not enabled in default build --FILE-- &1', $output, $code); + if ($code) { + $msg = preg_replace("/\[.+?\]/", "", $output[0]); + return false; + } + return true; +} +/* }}} */ + function run_fpm_till($needle, $config, $max = 10) /* {{{ */ { $i = 0; @@ -76,7 +88,7 @@ function run_fpm_till($needle, $config, $max = 10) /* {{{ */ } /* }}} */ -function fpm_display_log($tail, $n=1, $ignore='systemd') { +function fpm_display_log($tail, $n=1, $ignore='systemd') { /* {{{ */ while ($n) { $a = fgets($tail); if (empty($ignore) || !strpos($a, $ignore)) { @@ -84,9 +96,9 @@ function fpm_display_log($tail, $n=1, $ignore='systemd') { $n--; } } -} +} /* }}} */ -function run_request($host, $port, $uri='/ping', $query='') { +function run_request($host, $port, $uri='/ping', $query='') { /* {{{ */ require_once 'fcgi.inc'; $client = new Adoy\FastCGI\Client($host, $port); $params = array( @@ -109,3 +121,4 @@ function run_request($host, $port, $uri='/ping', $query='') { ); return $client->request($params, false)."\n"; } +/* }}} */