diff options
Diffstat (limited to 'Zend/zend_execute.c')
| -rw-r--r-- | Zend/zend_execute.c | 975 |
1 files changed, 353 insertions, 622 deletions
diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 42822785cb..d180a83601 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -109,16 +109,16 @@ typedef int (ZEND_FASTCALL *incdec_t)(zval *); -#define get_zval_ptr(op_type, node, should_free, type) _get_zval_ptr(op_type, node, should_free, type EXECUTE_DATA_CC OPLINE_CC) -#define get_zval_ptr_deref(op_type, node, should_free, type) _get_zval_ptr_deref(op_type, node, should_free, type EXECUTE_DATA_CC OPLINE_CC) -#define get_zval_ptr_undef(op_type, node, should_free, type) _get_zval_ptr_undef(op_type, node, should_free, type EXECUTE_DATA_CC OPLINE_CC) -#define get_op_data_zval_ptr_r(op_type, node, should_free) _get_op_data_zval_ptr_r(op_type, node, should_free EXECUTE_DATA_CC OPLINE_CC) -#define get_op_data_zval_ptr_deref_r(op_type, node, should_free) _get_op_data_zval_ptr_deref_r(op_type, node, should_free EXECUTE_DATA_CC OPLINE_CC) -#define get_zval_ptr_ptr(op_type, node, should_free, type) _get_zval_ptr_ptr(op_type, node, should_free, type EXECUTE_DATA_CC) -#define get_zval_ptr_ptr_undef(op_type, node, should_free, type) _get_zval_ptr_ptr(op_type, node, should_free, type EXECUTE_DATA_CC) -#define get_obj_zval_ptr(op_type, node, should_free, type) _get_obj_zval_ptr(op_type, node, should_free, type EXECUTE_DATA_CC OPLINE_CC) -#define get_obj_zval_ptr_undef(op_type, node, should_free, type) _get_obj_zval_ptr_undef(op_type, node, should_free, type EXECUTE_DATA_CC OPLINE_CC) -#define get_obj_zval_ptr_ptr(op_type, node, should_free, type) _get_obj_zval_ptr_ptr(op_type, node, should_free, type EXECUTE_DATA_CC) +#define get_zval_ptr(op_type, node, type) _get_zval_ptr(op_type, node, type EXECUTE_DATA_CC OPLINE_CC) +#define get_zval_ptr_deref(op_type, node, type) _get_zval_ptr_deref(op_type, node, type EXECUTE_DATA_CC OPLINE_CC) +#define get_zval_ptr_undef(op_type, node, type) _get_zval_ptr_undef(op_type, node, type EXECUTE_DATA_CC OPLINE_CC) +#define get_op_data_zval_ptr_r(op_type, node) _get_op_data_zval_ptr_r(op_type, node EXECUTE_DATA_CC OPLINE_CC) +#define get_op_data_zval_ptr_deref_r(op_type, node) _get_op_data_zval_ptr_deref_r(op_type, node EXECUTE_DATA_CC OPLINE_CC) +#define get_zval_ptr_ptr(op_type, node, type) _get_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC) +#define get_zval_ptr_ptr_undef(op_type, node, type) _get_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC) +#define get_obj_zval_ptr(op_type, node, type) _get_obj_zval_ptr(op_type, node, type EXECUTE_DATA_CC OPLINE_CC) +#define get_obj_zval_ptr_undef(op_type, node, type) _get_obj_zval_ptr_undef(op_type, node, type EXECUTE_DATA_CC OPLINE_CC) +#define get_obj_zval_ptr_ptr(op_type, node, type) _get_obj_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC) #define RETURN_VALUE_USED(opline) ((opline)->result_type != IS_UNUSED) @@ -141,13 +141,12 @@ ZEND_API const zend_internal_function zend_pass_function = { {NULL,NULL,NULL,NULL} /* reserved */ }; -#define FREE_VAR_PTR_AND_EXTRACT_RESULT_IF_NECESSARY(free_op, result) do { \ - zval *__container_to_free = (free_op); \ - if (UNEXPECTED(__container_to_free) \ - && EXPECTED(Z_REFCOUNTED_P(__container_to_free))) { \ +#define FREE_VAR_PTR_AND_EXTRACT_RESULT_IF_NECESSARY(free_var) do { \ + zval *__container_to_free = EX_VAR(free_var); \ + if (UNEXPECTED(Z_REFCOUNTED_P(__container_to_free))) { \ zend_refcounted *__ref = Z_COUNTED_P(__container_to_free); \ if (UNEXPECTED(!GC_DELREF(__ref))) { \ - zval *__zv = (result); \ + zval *__zv = EX_VAR(opline->result.var); \ if (EXPECTED(Z_TYPE_P(__zv) == IS_INDIRECT)) { \ ZVAL_COPY(__zv, Z_INDIRECT_P(__zv)); \ } \ @@ -156,20 +155,16 @@ ZEND_API const zend_internal_function zend_pass_function = { } \ } while (0) -#define FREE_OP(should_free) \ - if (should_free) { \ - zval_ptr_dtor_nogc(should_free); \ - } - -#define FREE_UNFETCHED_OP(type, var) \ +#define FREE_OP(type, var) \ if ((type) & (IS_TMP_VAR|IS_VAR)) { \ zval_ptr_dtor_nogc(EX_VAR(var)); \ } -#define FREE_OP_VAR_PTR(should_free) \ - if (should_free) { \ - zval_ptr_dtor_nogc(should_free); \ - } +#define FREE_UNFETCHED_OP(type, var) \ + FREE_OP(type, var) + +#define FREE_OP_VAR_PTR(type, var) \ + FREE_OP(type, var) #define CV_DEF_OF(i) (EX(func)->op_array.vars[i]) @@ -241,29 +236,26 @@ ZEND_API zval* zend_get_compiled_variable_value(const zend_execute_data *execute return EX_VAR(var); } -static zend_always_inline zval *_get_zval_ptr_tmp(uint32_t var, zend_free_op *should_free EXECUTE_DATA_DC) +static zend_always_inline zval *_get_zval_ptr_tmp(uint32_t var EXECUTE_DATA_DC) { zval *ret = EX_VAR(var); - *should_free = ret; ZEND_ASSERT(Z_TYPE_P(ret) != IS_REFERENCE); return ret; } -static zend_always_inline zval *_get_zval_ptr_var(uint32_t var, zend_free_op *should_free EXECUTE_DATA_DC) +static zend_always_inline zval *_get_zval_ptr_var(uint32_t var EXECUTE_DATA_DC) { zval *ret = EX_VAR(var); - *should_free = ret; return ret; } -static zend_always_inline zval *_get_zval_ptr_var_deref(uint32_t var, zend_free_op *should_free EXECUTE_DATA_DC) +static zend_always_inline zval *_get_zval_ptr_var_deref(uint32_t var EXECUTE_DATA_DC) { zval *ret = EX_VAR(var); - *should_free = ret; ZVAL_DEREF(ret); return ret; } @@ -390,17 +382,16 @@ static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_W(uint32_t var EXECUTE_D return ret; } -static zend_always_inline zval *_get_zval_ptr(int op_type, znode_op node, zend_free_op *should_free, int type EXECUTE_DATA_DC OPLINE_DC) +static zend_always_inline zval *_get_zval_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC) { if (op_type & (IS_TMP_VAR|IS_VAR)) { if (!ZEND_DEBUG || op_type == IS_VAR) { - return _get_zval_ptr_var(node.var, should_free EXECUTE_DATA_CC); + return _get_zval_ptr_var(node.var EXECUTE_DATA_CC); } else { ZEND_ASSERT(op_type == IS_TMP_VAR); - return _get_zval_ptr_tmp(node.var, should_free EXECUTE_DATA_CC); + return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC); } } else { - *should_free = NULL; if (op_type == IS_CONST) { return RT_CONSTANT(opline, node); } else if (op_type == IS_CV) { @@ -411,17 +402,16 @@ static zend_always_inline zval *_get_zval_ptr(int op_type, znode_op node, zend_f } } -static zend_always_inline zval *_get_op_data_zval_ptr_r(int op_type, znode_op node, zend_free_op *should_free EXECUTE_DATA_DC OPLINE_DC) +static zend_always_inline zval *_get_op_data_zval_ptr_r(int op_type, znode_op node EXECUTE_DATA_DC OPLINE_DC) { if (op_type & (IS_TMP_VAR|IS_VAR)) { if (!ZEND_DEBUG || op_type == IS_VAR) { - return _get_zval_ptr_var(node.var, should_free EXECUTE_DATA_CC); + return _get_zval_ptr_var(node.var EXECUTE_DATA_CC); } else { ZEND_ASSERT(op_type == IS_TMP_VAR); - return _get_zval_ptr_tmp(node.var, should_free EXECUTE_DATA_CC); + return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC); } } else { - *should_free = NULL; if (op_type == IS_CONST) { return RT_CONSTANT(opline + 1, node); } else if (op_type == IS_CV) { @@ -432,17 +422,16 @@ static zend_always_inline zval *_get_op_data_zval_ptr_r(int op_type, znode_op no } } -static zend_always_inline ZEND_ATTRIBUTE_UNUSED zval *_get_zval_ptr_deref(int op_type, znode_op node, zend_free_op *should_free, int type EXECUTE_DATA_DC OPLINE_DC) +static zend_always_inline ZEND_ATTRIBUTE_UNUSED zval *_get_zval_ptr_deref(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC) { if (op_type & (IS_TMP_VAR|IS_VAR)) { if (op_type == IS_TMP_VAR) { - return _get_zval_ptr_tmp(node.var, should_free EXECUTE_DATA_CC); + return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC); } else { ZEND_ASSERT(op_type == IS_VAR); - return _get_zval_ptr_var_deref(node.var, should_free EXECUTE_DATA_CC); + return _get_zval_ptr_var_deref(node.var EXECUTE_DATA_CC); } } else { - *should_free = NULL; if (op_type == IS_CONST) { return RT_CONSTANT(opline, node); } else if (op_type == IS_CV) { @@ -453,17 +442,16 @@ static zend_always_inline ZEND_ATTRIBUTE_UNUSED zval *_get_zval_ptr_deref(int op } } -static zend_always_inline ZEND_ATTRIBUTE_UNUSED zval *_get_op_data_zval_ptr_deref_r(int op_type, znode_op node, zend_free_op *should_free EXECUTE_DATA_DC OPLINE_DC) +static zend_always_inline ZEND_ATTRIBUTE_UNUSED zval *_get_op_data_zval_ptr_deref_r(int op_type, znode_op node EXECUTE_DATA_DC OPLINE_DC) { if (op_type & (IS_TMP_VAR|IS_VAR)) { if (op_type == IS_TMP_VAR) { - return _get_zval_ptr_tmp(node.var, should_free EXECUTE_DATA_CC); + return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC); } else { ZEND_ASSERT(op_type == IS_VAR); - return _get_zval_ptr_var_deref(node.var, should_free EXECUTE_DATA_CC); + return _get_zval_ptr_var_deref(node.var EXECUTE_DATA_CC); } } else { - *should_free = NULL; if (op_type == IS_CONST) { return RT_CONSTANT(opline + 1, node); } else if (op_type == IS_CV) { @@ -474,17 +462,16 @@ static zend_always_inline ZEND_ATTRIBUTE_UNUSED zval *_get_op_data_zval_ptr_dere } } -static zend_always_inline zval *_get_zval_ptr_undef(int op_type, znode_op node, zend_free_op *should_free, int type EXECUTE_DATA_DC OPLINE_DC) +static zend_always_inline zval *_get_zval_ptr_undef(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC) { if (op_type & (IS_TMP_VAR|IS_VAR)) { if (!ZEND_DEBUG || op_type == IS_VAR) { - return _get_zval_ptr_var(node.var, should_free EXECUTE_DATA_CC); + return _get_zval_ptr_var(node.var EXECUTE_DATA_CC); } else { ZEND_ASSERT(op_type == IS_TMP_VAR); - return _get_zval_ptr_tmp(node.var, should_free EXECUTE_DATA_CC); + return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC); } } else { - *should_free = NULL; if (op_type == IS_CONST) { return RT_CONSTANT(opline, node); } else if (op_type == IS_CV) { @@ -495,55 +482,48 @@ static zend_always_inline zval *_get_zval_ptr_undef(int op_type, znode_op node, } } -static zend_always_inline zval *_get_zval_ptr_ptr_var(uint32_t var, zend_free_op *should_free EXECUTE_DATA_DC) +static zend_always_inline zval *_get_zval_ptr_ptr_var(uint32_t var EXECUTE_DATA_DC) { zval *ret = EX_VAR(var); if (EXPECTED(Z_TYPE_P(ret) == IS_INDIRECT)) { - *should_free = NULL; ret = Z_INDIRECT_P(ret); - } else { - *should_free = ret; } return ret; } -static inline zval *_get_zval_ptr_ptr(int op_type, znode_op node, zend_free_op *should_free, int type EXECUTE_DATA_DC) +static inline zval *_get_zval_ptr_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC) { if (op_type == IS_CV) { - *should_free = NULL; return _get_zval_ptr_cv(node.var, type EXECUTE_DATA_CC); } else /* if (op_type == IS_VAR) */ { ZEND_ASSERT(op_type == IS_VAR); - return _get_zval_ptr_ptr_var(node.var, should_free EXECUTE_DATA_CC); + return _get_zval_ptr_ptr_var(node.var EXECUTE_DATA_CC); } } -static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr(int op_type, znode_op op, zend_free_op *should_free, int type EXECUTE_DATA_DC OPLINE_DC) +static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr(int op_type, znode_op op, int type EXECUTE_DATA_DC OPLINE_DC) { if (op_type == IS_UNUSED) { - *should_free = NULL; return &EX(This); } - return get_zval_ptr(op_type, op, should_free, type); + return get_zval_ptr(op_type, op, type); } -static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_undef(int op_type, znode_op op, zend_free_op *should_free, int type EXECUTE_DATA_DC OPLINE_DC) +static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_undef(int op_type, znode_op op, int type EXECUTE_DATA_DC OPLINE_DC) { if (op_type == IS_UNUSED) { - *should_free = NULL; return &EX(This); } - return get_zval_ptr_undef(op_type, op, should_free, type); + return get_zval_ptr_undef(op_type, op, type); } -static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_ptr(int op_type, znode_op node, zend_free_op *should_free, int type EXECUTE_DATA_DC) +static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC) { if (op_type == IS_UNUSED) { - *should_free = NULL; return &EX(This); } - return get_zval_ptr_ptr(op_type, node, should_free, type); + return get_zval_ptr_ptr(op_type, node, type); } static inline void zend_assign_to_variable_reference(zval *variable_ptr, zval *value_ptr) @@ -602,39 +582,26 @@ static zend_never_inline ZEND_COLD int zend_wrong_assign_to_variable_reference(z return 1; } -static void zend_format_type(zend_type type, const char **part1, const char **part2) { - *part1 = ZEND_TYPE_ALLOW_NULL(type) ? "?" : ""; - if (ZEND_TYPE_IS_CLASS(type)) { - if (ZEND_TYPE_IS_CE(type)) { - *part2 = ZSTR_VAL(ZEND_TYPE_CE(type)->name); - } else { - *part2 = ZSTR_VAL(ZEND_TYPE_NAME(type)); - } - } else { - *part2 = zend_get_type_by_const(ZEND_TYPE_CODE(type)); - } -} - static zend_never_inline ZEND_COLD void zend_throw_auto_init_in_prop_error(zend_property_info *prop, const char *type) { - const char *prop_type1, *prop_type2; - zend_format_type(prop->type, &prop_type1, &prop_type2); + zend_string *type_str = zend_type_to_string(prop->type); zend_type_error( - "Cannot auto-initialize an %s inside property %s::$%s of type %s%s", + "Cannot auto-initialize an %s inside property %s::$%s of type %s", type, ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name), - prop_type1, prop_type2 + ZSTR_VAL(type_str) ); + zend_string_release(type_str); } static zend_never_inline ZEND_COLD void zend_throw_auto_init_in_ref_error(zend_property_info *prop, const char *type) { - const char *prop_type1, *prop_type2; - zend_format_type(prop->type, &prop_type1, &prop_type2); + zend_string *type_str = zend_type_to_string(prop->type); zend_type_error( - "Cannot auto-initialize an %s inside a reference held by property %s::$%s of type %s%s", + "Cannot auto-initialize an %s inside a reference held by property %s::$%s of type %s", type, ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name), - prop_type1, prop_type2 + ZSTR_VAL(type_str) ); + zend_string_release(type_str); } static zend_never_inline ZEND_COLD void zend_throw_access_uninit_prop_by_ref_error( @@ -645,70 +612,36 @@ static zend_never_inline ZEND_COLD void zend_throw_access_uninit_prop_by_ref_err zend_get_unmangled_property_name(prop->name)); } -static zend_never_inline zend_bool zend_verify_ref_stdClass_assignable(zend_reference *ref); static zend_never_inline zend_bool zend_verify_ref_array_assignable(zend_reference *ref); /* this should modify object only if it's empty */ -static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL make_real_object(zval *object, zval *property OPLINE_DC EXECUTE_DATA_DC) -{ - zend_object *obj; - zval *ref = NULL; - if (Z_ISREF_P(object)) { - ref = object; - object = Z_REFVAL_P(object); - } - - if (UNEXPECTED(Z_TYPE_P(object) > IS_FALSE && - (Z_TYPE_P(object) != IS_STRING || Z_STRLEN_P(object) != 0))) { - if (opline->op1_type != IS_VAR || EXPECTED(!Z_ISERROR_P(object))) { - zend_string *tmp_property_name; - zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name); - - if (opline->opcode == ZEND_PRE_INC_OBJ - || opline->opcode == ZEND_PRE_DEC_OBJ - || opline->opcode == ZEND_POST_INC_OBJ - || opline->opcode == ZEND_POST_DEC_OBJ) { - zend_error(E_WARNING, "Attempt to increment/decrement property '%s' of non-object", ZSTR_VAL(property_name)); - } else if (opline->opcode == ZEND_FETCH_OBJ_W - || opline->opcode == ZEND_FETCH_OBJ_RW - || opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG - || opline->opcode == ZEND_ASSIGN_OBJ_REF) { - zend_error(E_WARNING, "Attempt to modify property '%s' of non-object", ZSTR_VAL(property_name)); - } else { - zend_error(E_WARNING, "Attempt to assign property '%s' of non-object", ZSTR_VAL(property_name)); - } - zend_tmp_string_release(tmp_property_name); - } - if (UNEXPECTED(RETURN_VALUE_USED(opline))) { - ZVAL_NULL(EX_VAR(opline->result.var)); - } - return NULL; - } +static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_throw_non_object_error(zval *object, zval *property OPLINE_DC EXECUTE_DATA_DC) +{ + zend_string *tmp_property_name; + zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name); - if (ref && ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(ref))) { - if (UNEXPECTED(!zend_verify_ref_stdClass_assignable(Z_REF_P(ref)))) { - if (RETURN_VALUE_USED(opline)) { - ZVAL_UNDEF(EX_VAR(opline->result.var)); - } - return NULL; - } + if (opline->opcode == ZEND_PRE_INC_OBJ + || opline->opcode == ZEND_PRE_DEC_OBJ + || opline->opcode == ZEND_POST_INC_OBJ + || opline->opcode == ZEND_POST_DEC_OBJ) { + zend_throw_error(NULL, + "Attempt to increment/decrement property '%s' of non-object", + ZSTR_VAL(property_name)); + } else if (opline->opcode == ZEND_FETCH_OBJ_W + || opline->opcode == ZEND_FETCH_OBJ_RW + || opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG + || opline->opcode == ZEND_ASSIGN_OBJ_REF) { + zend_throw_error(NULL, + "Attempt to modify property '%s' of non-object", ZSTR_VAL(property_name)); + } else { + zend_throw_error(NULL, + "Attempt to assign property '%s' of non-object", ZSTR_VAL(property_name)); } + zend_tmp_string_release(tmp_property_name); - zval_ptr_dtor_nogc(object); - object_init(object); - Z_ADDREF_P(object); - obj = Z_OBJ_P(object); - zend_error(E_WARNING, "Creating default object from empty value"); - if (GC_REFCOUNT(obj) == 1) { - /* the enclosing container was deleted, obj is unreferenced */ - OBJ_RELEASE(obj); - if (UNEXPECTED(RETURN_VALUE_USED(opline))) { - ZVAL_NULL(EX_VAR(opline->result.var)); - } - return NULL; + if (UNEXPECTED(RETURN_VALUE_USED(opline))) { + ZVAL_NULL(EX_VAR(opline->result.var)); } - Z_DELREF_P(object); - return object; } static ZEND_COLD void zend_verify_type_error_common( @@ -744,22 +677,25 @@ static ZEND_COLD void zend_verify_type_error_common( *need_kind = ZSTR_VAL(ZEND_TYPE_NAME(arg_info->type)); } } else { - switch (ZEND_TYPE_CODE(arg_info->type)) { - case IS_OBJECT: + zend_type type = ZEND_TYPE_WITHOUT_NULL(arg_info->type); + switch (ZEND_TYPE_MASK(type)) { + case MAY_BE_OBJECT: *need_msg = "be an "; *need_kind = "object"; break; - case IS_CALLABLE: + case MAY_BE_CALLABLE: *need_msg = "be callable"; *need_kind = ""; break; - case IS_ITERABLE: + case MAY_BE_ITERABLE: *need_msg = "be iterable"; *need_kind = ""; break; default: + /* TODO: The zend_type_to_string() result is guaranteed interned here. + * It would be beter to switch all this code to use zend_string though. */ *need_msg = "be of the type "; - *need_kind = zend_get_type_by_const(ZEND_TYPE_CODE(arg_info->type)); + *need_kind = ZSTR_VAL(zend_type_to_string(type)); break; } } @@ -784,7 +720,7 @@ static ZEND_COLD void zend_verify_type_error_common( } } -static ZEND_COLD void zend_verify_arg_error( +ZEND_API ZEND_COLD void zend_verify_arg_error( const zend_function *zf, const zend_arg_info *arg_info, int arg_num, const zend_class_entry *ce, zval *value) { @@ -813,108 +749,129 @@ static ZEND_COLD void zend_verify_arg_error( } } -static int is_null_constant(zend_class_entry *scope, zval *default_value) +static zend_bool zend_verify_weak_scalar_type_hint(uint32_t type_mask, zval *arg) { - if (Z_TYPE_P(default_value) == IS_CONSTANT_AST) { - zval constant; + if (type_mask & (MAY_BE_TRUE|MAY_BE_FALSE)) { + zend_bool dest; - ZVAL_COPY(&constant, default_value); - if (UNEXPECTED(zval_update_constant_ex(&constant, scope) != SUCCESS)) { + if (!zend_parse_arg_bool_weak(arg, &dest)) { return 0; } - if (Z_TYPE(constant) == IS_NULL) { - return 1; + zval_ptr_dtor(arg); + ZVAL_BOOL(arg, dest); + return 1; + } + if (type_mask & MAY_BE_LONG) { + zend_long dest; + + if (!zend_parse_arg_long_weak(arg, &dest)) { + return 0; + } + zval_ptr_dtor(arg); + ZVAL_LONG(arg, dest); + return 1; + } + if (type_mask & MAY_BE_DOUBLE) { + double dest; + + if (!zend_parse_arg_double_weak(arg, &dest)) { + return 0; } - zval_ptr_dtor_nogc(&constant); + zval_ptr_dtor(arg); + ZVAL_DOUBLE(arg, dest); + return 1; + } + if (type_mask & MAY_BE_STRING) { + zend_string *dest; + + /* on success "arg" is converted to IS_STRING */ + return zend_parse_arg_str_weak(arg, &dest); } return 0; } -static zend_bool zend_verify_weak_scalar_type_hint(zend_uchar type_hint, zval *arg) -{ - switch (type_hint) { - case _IS_BOOL: { - zend_bool dest; - - if (!zend_parse_arg_bool_weak(arg, &dest)) { - return 0; +#if ZEND_DEBUG +/* Used to sanity-check internal arginfo types without performing any actual type conversions. */ +static zend_bool zend_verify_weak_scalar_type_hint_no_sideeffect(uint32_t type_mask, zval *arg) +{ + if (type_mask & (MAY_BE_TRUE|MAY_BE_FALSE)) { + zend_bool dest; + return zend_parse_arg_bool_weak(arg, &dest); + } + if (type_mask & MAY_BE_LONG) { + zend_long dest; + if (Z_TYPE_P(arg) == IS_STRING) { + /* Handle this case separately to avoid the "non well-formed" warning */ + double dval; + zend_uchar type = is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, &dval, 1); + if (type == IS_LONG) { + return 1; } - zval_ptr_dtor(arg); - ZVAL_BOOL(arg, dest); - return 1; - } - case IS_LONG: { - zend_long dest; + if (type == IS_DOUBLE) { + return !zend_isnan(dval) && ZEND_DOUBLE_FITS_LONG(dval); - if (!zend_parse_arg_long_weak(arg, &dest)) { - return 0; } - zval_ptr_dtor(arg); - ZVAL_LONG(arg, dest); - return 1; - } - case IS_DOUBLE: { - double dest; - - if (!zend_parse_arg_double_weak(arg, &dest)) { - return 0; - } - zval_ptr_dtor(arg); - ZVAL_DOUBLE(arg, dest); - return 1; + return 0; } - case IS_STRING: { - zend_string *dest; - - /* on success "arg" is converted to IS_STRING */ - return zend_parse_arg_str_weak(arg, &dest); + return zend_parse_arg_long_weak(arg, &dest); + } + if (type_mask & MAY_BE_DOUBLE) { + double dest; + if (Z_TYPE_P(arg) == IS_STRING) { + /* Handle this case separately to avoid the "non well-formed" warning */ + return is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 1) != 0; } - default: - return 0; + return zend_parse_arg_double_weak(arg, &dest); } + if (type_mask & MAY_BE_STRING) { + /* We don't call cast_object here, because this check must be side-effect free. As this + * is only used for a sanity check of arginfo/zpp consistency, it's okay if we accept + * more than actually allowed here. */ + return Z_TYPE_P(arg) < IS_STRING || Z_TYPE_P(arg) == IS_OBJECT; + } + return 0; } +#endif -static zend_bool zend_verify_scalar_type_hint(zend_uchar type_hint, zval *arg, zend_bool strict) +ZEND_API zend_bool zend_verify_scalar_type_hint(uint32_t type_mask, zval *arg, zend_bool strict, zend_bool is_internal_arg) { if (UNEXPECTED(strict)) { /* SSTH Exception: IS_LONG may be accepted as IS_DOUBLE (converted) */ - if (type_hint != IS_DOUBLE || Z_TYPE_P(arg) != IS_LONG) { + if (!(type_mask & MAY_BE_DOUBLE) || Z_TYPE_P(arg) != IS_LONG) { return 0; } } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL)) { /* NULL may be accepted only by nullable hints (this is already checked) */ + if (is_internal_arg && (type_mask & (MAY_BE_TRUE|MAY_BE_FALSE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING))) { + /* As an exception, null is allowed for scalar types in weak mode. */ + return 1; + } return 0; } - return zend_verify_weak_scalar_type_hint(type_hint, arg); +#if ZEND_DEBUG + if (is_internal_arg) { + return zend_verify_weak_scalar_type_hint_no_sideeffect(type_mask, arg); + } +#endif + return zend_verify_weak_scalar_type_hint(type_mask, arg); } ZEND_COLD zend_never_inline void zend_verify_property_type_error(zend_property_info *info, zval *property) { - const char *prop_type1, *prop_type2; + zend_string *type_str; /* we _may_ land here in case reading already errored and runtime cache thus has not been updated (i.e. it contains a valid but unrelated info) */ if (EG(exception)) { return; } - // TODO Switch to a more standard error message? - zend_format_type(info->type, &prop_type1, &prop_type2); - (void) prop_type1; - if (ZEND_TYPE_IS_CLASS(info->type)) { - zend_type_error("Typed property %s::$%s must be an instance of %s%s, %s used", - ZSTR_VAL(info->ce->name), - zend_get_unmangled_property_name(info->name), - prop_type2, - ZEND_TYPE_ALLOW_NULL(info->type) ? " or null" : "", - Z_TYPE_P(property) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(property)->name) : zend_get_type_by_const(Z_TYPE_P(property))); - } else { - zend_type_error("Typed property %s::$%s must be %s%s, %s used", - ZSTR_VAL(info->ce->name), - zend_get_unmangled_property_name(info->name), - prop_type2, - ZEND_TYPE_ALLOW_NULL(info->type) ? " or null" : "", - Z_TYPE_P(property) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(property)->name) : zend_get_type_by_const(Z_TYPE_P(property))); - } + type_str = zend_type_to_string(info->type); + zend_type_error("Cannot assign %s to property %s::$%s of type %s", + Z_TYPE_P(property) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(property)->name) : zend_get_type_by_const(Z_TYPE_P(property)), + ZSTR_VAL(info->ce->name), + zend_get_unmangled_property_name(info->name), + ZSTR_VAL(type_str)); + zend_string_release(type_str); } static zend_bool zend_resolve_class_type(zend_type *type, zend_class_entry *self_ce) { @@ -962,17 +919,13 @@ static zend_always_inline zend_bool i_zend_check_property_type(zend_property_inf return instanceof_function(Z_OBJCE_P(property), ZEND_TYPE_CE(info->type)); } - ZEND_ASSERT(ZEND_TYPE_CODE(info->type) != IS_CALLABLE); - if (EXPECTED(ZEND_TYPE_CODE(info->type) == Z_TYPE_P(property))) { + ZEND_ASSERT(!(ZEND_TYPE_MASK(info->type) & MAY_BE_CALLABLE)); + if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(info->type, Z_TYPE_P(property)))) { return 1; - } else if (EXPECTED(Z_TYPE_P(property) == IS_NULL)) { - return ZEND_TYPE_ALLOW_NULL(info->type); - } else if (ZEND_TYPE_CODE(info->type) == _IS_BOOL && EXPECTED(Z_TYPE_P(property) == IS_FALSE || Z_TYPE_P(property) == IS_TRUE)) { - return 1; - } else if (ZEND_TYPE_CODE(info->type) == IS_ITERABLE) { + } else if (ZEND_TYPE_MASK(info->type) & MAY_BE_ITERABLE) { return zend_is_iterable(property); } else { - return zend_verify_scalar_type_hint(ZEND_TYPE_CODE(info->type), property, strict); + return zend_verify_scalar_type_hint(ZEND_TYPE_MASK(info->type), property, strict, 0); } } @@ -1009,10 +962,11 @@ static zend_never_inline zval* zend_assign_to_typed_prop(zend_property_info *inf static zend_always_inline zend_bool zend_check_type( zend_type type, zval *arg, zend_class_entry **ce, void **cache_slot, - zval *default_value, zend_class_entry *scope, - zend_bool is_return_type) + zend_class_entry *scope, + zend_bool is_return_type, zend_bool is_internal) { zend_reference *ref = NULL; + uint32_t type_mask; if (!ZEND_TYPE_IS_SET(type)) { return 1; @@ -1029,64 +983,41 @@ static zend_always_inline zend_bool zend_check_type( } else { *ce = zend_fetch_class(ZEND_TYPE_NAME(type), (ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_NO_AUTOLOAD)); if (UNEXPECTED(!*ce)) { - return Z_TYPE_P(arg) == IS_NULL && (ZEND_TYPE_ALLOW_NULL(type) || (default_value && is_null_constant(scope, default_value))); + return Z_TYPE_P(arg) == IS_NULL && ZEND_TYPE_ALLOW_NULL(type); } *cache_slot = (void *) *ce; } if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) { return instanceof_function(Z_OBJCE_P(arg), *ce); } - return Z_TYPE_P(arg) == IS_NULL && (ZEND_TYPE_ALLOW_NULL(type) || (default_value && is_null_constant(scope, default_value))); - } else if (EXPECTED(ZEND_TYPE_CODE(type) == Z_TYPE_P(arg))) { - return 1; - } - - if (Z_TYPE_P(arg) == IS_NULL && (ZEND_TYPE_ALLOW_NULL(type) || (default_value && is_null_constant(scope, default_value)))) { - /* Null passed to nullable type */ + return Z_TYPE_P(arg) == IS_NULL && ZEND_TYPE_ALLOW_NULL(type); + } else if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(arg)))) { return 1; } - if (ZEND_TYPE_CODE(type) == IS_CALLABLE) { + type_mask = ZEND_TYPE_MASK(type); + if (type_mask & MAY_BE_CALLABLE) { return zend_is_callable(arg, IS_CALLABLE_CHECK_SILENT, NULL); - } else if (ZEND_TYPE_CODE(type) == IS_ITERABLE) { + } else if (type_mask & MAY_BE_ITERABLE) { return zend_is_iterable(arg); - } else if (ZEND_TYPE_CODE(type) == _IS_BOOL && - EXPECTED(Z_TYPE_P(arg) == IS_FALSE || Z_TYPE_P(arg) == IS_TRUE)) { - return 1; } else if (ref && ZEND_REF_HAS_TYPE_SOURCES(ref)) { return 0; /* we cannot have conversions for typed refs */ + } else if (is_internal && is_return_type) { + /* For internal returns, the type has to match exactly, because we're not + * going to check it for non-debug builds, and there will be no chance to + * apply coercions. */ + return 0; } else { - return zend_verify_scalar_type_hint(ZEND_TYPE_CODE(type), arg, - is_return_type ? ZEND_RET_USES_STRICT_TYPES() : ZEND_ARG_USES_STRICT_TYPES()); + return zend_verify_scalar_type_hint(type_mask, arg, + is_return_type ? ZEND_RET_USES_STRICT_TYPES() : ZEND_ARG_USES_STRICT_TYPES(), + is_internal); } /* Special handling for IS_VOID is not necessary (for return types), * because this case is already checked at compile-time. */ } -static zend_always_inline int zend_verify_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, zval *default_value, void **cache_slot) -{ - zend_arg_info *cur_arg_info; - zend_class_entry *ce; - - if (EXPECTED(arg_num <= zf->common.num_args)) { - cur_arg_info = &zf->common.arg_info[arg_num-1]; - } else if (UNEXPECTED(zf->common.fn_flags & ZEND_ACC_VARIADIC)) { - cur_arg_info = &zf->common.arg_info[zf->common.num_args]; - } else { - return 1; - } - - ce = NULL; - if (UNEXPECTED(!zend_check_type(cur_arg_info->type, arg, &ce, cache_slot, default_value, zf->common.scope, 0))) { - zend_verify_arg_error(zf, cur_arg_info, arg_num, ce, arg); - return 0; - } - - return 1; -} - -static zend_always_inline int zend_verify_recv_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, zval *default_value, void **cache_slot) +static zend_always_inline int zend_verify_recv_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, void **cache_slot) { zend_arg_info *cur_arg_info = &zf->common.arg_info[arg_num-1]; zend_class_entry *ce; @@ -1095,7 +1026,7 @@ static zend_always_inline int zend_verify_recv_arg_type(zend_function *zf, uint3 cur_arg_info = &zf->common.arg_info[arg_num-1]; ce = NULL; - if (UNEXPECTED(!zend_check_type(cur_arg_info->type, arg, &ce, cache_slot, default_value, zf->common.scope, 0))) { + if (UNEXPECTED(!zend_check_type(cur_arg_info->type, arg, &ce, cache_slot, zf->common.scope, 0, 0))) { zend_verify_arg_error(zf, cur_arg_info, arg_num, ce, arg); return 0; } @@ -1103,7 +1034,7 @@ static zend_always_inline int zend_verify_recv_arg_type(zend_function *zf, uint3 return 1; } -static zend_always_inline int zend_verify_variadic_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, zval *default_value, void **cache_slot) +static zend_always_inline int zend_verify_variadic_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, void **cache_slot) { zend_arg_info *cur_arg_info; zend_class_entry *ce; @@ -1113,7 +1044,7 @@ static zend_always_inline int zend_verify_variadic_arg_type(zend_function *zf, u cur_arg_info = &zf->common.arg_info[zf->common.num_args]; ce = NULL; - if (UNEXPECTED(!zend_check_type(cur_arg_info->type, arg, &ce, cache_slot, default_value, zf->common.scope, 0))) { + if (UNEXPECTED(!zend_check_type(cur_arg_info->type, arg, &ce, cache_slot, zf->common.scope, 0, 0))) { zend_verify_arg_error(zf, cur_arg_info, arg_num, ce, arg); return 0; } @@ -1121,20 +1052,29 @@ static zend_always_inline int zend_verify_variadic_arg_type(zend_function *zf, u return 1; } -static zend_never_inline int zend_verify_internal_arg_types(zend_function *fbc, zend_execute_data *call) +static zend_never_inline ZEND_ATTRIBUTE_UNUSED int zend_verify_internal_arg_types(zend_function *fbc, zend_execute_data *call) { uint32_t i; uint32_t num_args = ZEND_CALL_NUM_ARGS(call); - zval *p = ZEND_CALL_ARG(call, 1); - void *dummy_cache_slot; + zval *arg = ZEND_CALL_ARG(call, 1); for (i = 0; i < num_args; ++i) { - dummy_cache_slot = NULL; - if (UNEXPECTED(!zend_verify_arg_type(fbc, i + 1, p, NULL, &dummy_cache_slot))) { - EG(current_execute_data) = call->prev_execute_data; + zend_arg_info *cur_arg_info; + zend_class_entry *ce = NULL; + void *dummy_cache_slot = NULL; + + if (EXPECTED(i < fbc->common.num_args)) { + cur_arg_info = &fbc->common.arg_info[i]; + } else if (UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_VARIADIC)) { + cur_arg_info = &fbc->common.arg_info[fbc->common.num_args]; + } else { + break; + } + + if (UNEXPECTED(!zend_check_type(cur_arg_info->type, arg, &ce, &dummy_cache_slot, fbc->common.scope, 0, /* is_internal */ 1))) { return 0; } - p++; + arg++; } return 1; } @@ -1219,7 +1159,7 @@ static int zend_verify_internal_return_type(zend_function *zf, zval *ret) zend_class_entry *ce = NULL; void *dummy_cache_slot = NULL; - if (ZEND_TYPE_CODE(ret_info->type) == IS_VOID) { + if (ZEND_TYPE_IS_MASK(ret_info->type) && (ZEND_TYPE_MASK(ret_info->type) & MAY_BE_VOID)) { if (UNEXPECTED(Z_TYPE_P(ret) != IS_NULL)) { zend_verify_void_return_error(zf, zend_zval_type_name(ret), ""); return 0; @@ -1227,7 +1167,7 @@ static int zend_verify_internal_return_type(zend_function *zf, zval *ret) return 1; } - if (UNEXPECTED(!zend_check_type(ret_info->type, ret, &ce, &dummy_cache_slot, NULL, NULL, 1))) { + if (UNEXPECTED(!zend_check_type(ret_info->type, ret, &ce, &dummy_cache_slot, NULL, 1, /* is_internal */ 1))) { zend_verify_internal_return_error(zf, ce, ret); return 0; } @@ -1241,7 +1181,7 @@ static zend_always_inline void zend_verify_return_type(zend_function *zf, zval * zend_arg_info *ret_info = zf->common.arg_info - 1; zend_class_entry *ce = NULL; - if (UNEXPECTED(!zend_check_type(ret_info->type, ret, &ce, cache_slot, NULL, NULL, 1))) { + if (UNEXPECTED(!zend_check_type(ret_info->type, ret, &ce, cache_slot, NULL, 1, 0))) { zend_verify_return_error(zf, ce, ret); } } @@ -1250,7 +1190,9 @@ static ZEND_COLD int zend_verify_missing_return_type(const zend_function *zf, vo { zend_arg_info *ret_info = zf->common.arg_info - 1; - if (ZEND_TYPE_IS_SET(ret_info->type) && UNEXPECTED(ZEND_TYPE_CODE(ret_info->type) != IS_VOID)) { + if (ZEND_TYPE_IS_SET(ret_info->type) + && (!ZEND_TYPE_IS_MASK(ret_info->type) + || !(ZEND_TYPE_MASK(ret_info->type) & MAY_BE_VOID))) { zend_class_entry *ce = NULL; if (ZEND_TYPE_IS_CLASS(ret_info->type)) { if (EXPECTED(*cache_slot)) { @@ -1275,12 +1217,12 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_object_as_array(v static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_offset(void) { - zend_error(E_WARNING, "Illegal offset type"); + zend_type_error("Illegal offset type"); } static zend_never_inline void zend_assign_to_object_dim(zval *object, zval *dim, zval *value OPLINE_DC EXECUTE_DATA_DC) { - Z_OBJ_HT_P(object)->write_dimension(object, dim, value); + Z_OBJ_HT_P(object)->write_dimension(Z_OBJ_P(object), dim, value); if (UNEXPECTED(RETURN_VALUE_USED(opline))) { ZVAL_COPY(EX_VAR(opline->result.var), value); @@ -1311,25 +1253,15 @@ static zend_always_inline int zend_binary_op(zval *ret, zval *op1, zval *op2 OPL static zend_never_inline void zend_binary_assign_op_obj_dim(zval *object, zval *property OPLINE_DC EXECUTE_DATA_DC) { - zend_free_op free_op_data1; zval *value; zval *z; zval rv, res; - value = get_op_data_zval_ptr_r((opline+1)->op1_type, (opline+1)->op1, &free_op_data1); - if ((z = Z_OBJ_HT_P(object)->read_dimension(object, property, BP_VAR_R, &rv)) != NULL) { + value = get_op_data_zval_ptr_r((opline+1)->op1_type, (opline+1)->op1); + if ((z = Z_OBJ_HT_P(object)->read_dimension(Z_OBJ_P(object), property, BP_VAR_R, &rv)) != NULL) { - if (Z_TYPE_P(z) == IS_OBJECT && Z_OBJ_HT_P(z)->get) { - zval rv2; - zval *value = Z_OBJ_HT_P(z)->get(z, &rv2); - - if (z == &rv) { - zval_ptr_dtor(&rv); - } - ZVAL_COPY_VALUE(z, value); - } if (zend_binary_op(&res, z, value OPLINE_CC) == SUCCESS) { - Z_OBJ_HT_P(object)->write_dimension(object, property, &res); + Z_OBJ_HT_P(object)->write_dimension(Z_OBJ_P(object), property, &res); } if (z == &rv) { zval_ptr_dtor(&rv); @@ -1344,7 +1276,7 @@ static zend_never_inline void zend_binary_assign_op_obj_dim(zval *object, zval * ZVAL_NULL(EX_VAR(opline->result.var)); } } - FREE_OP(free_op_data1); + FREE_OP((opline+1)->op1_type, (opline+1)->op1.var); } static zend_never_inline void zend_binary_assign_op_typed_ref(zend_reference *ref, zval *value OPLINE_DC EXECUTE_DATA_DC) @@ -1536,12 +1468,6 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_deprecated_function(c ZSTR_VAL(fbc->common.function_name)); } -static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_abstract_method(const zend_function *fbc) -{ - zend_throw_error(NULL, "Cannot call abstract method %s::%s()", - ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name)); -} - static zend_never_inline void zend_assign_to_string_offset(zval *str, zval *dim, zval *value OPLINE_DC EXECUTE_DATA_DC) { zend_uchar c; @@ -1616,7 +1542,7 @@ static zend_property_info *zend_get_prop_not_accepting_double(zend_reference *re { zend_property_info *prop; ZEND_REF_FOREACH_TYPE_SOURCES(ref, prop) { - if (ZEND_TYPE_CODE(prop->type) != IS_DOUBLE) { + if (!ZEND_TYPE_IS_MASK(prop->type) || !(ZEND_TYPE_MASK(prop->type) & MAY_BE_DOUBLE)) { return prop; } } ZEND_REF_FOREACH_TYPE_SOURCES_END(); @@ -1647,19 +1573,20 @@ static ZEND_COLD zend_long zend_throw_incdec_ref_error(zend_reference *ref OPLIN } static ZEND_COLD zend_long zend_throw_incdec_prop_error(zend_property_info *prop OPLINE_DC) { - const char *prop_type1, *prop_type2; - zend_format_type(prop->type, &prop_type1, &prop_type2); + zend_string *type_str = zend_type_to_string(prop->type); if (ZEND_IS_INCREMENT(opline->opcode)) { - zend_type_error("Cannot increment property %s::$%s of type %s%s past its maximal value", + zend_type_error("Cannot increment property %s::$%s of type %s past its maximal value", ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name), - prop_type1, prop_type2); + ZSTR_VAL(type_str)); + zend_string_release(type_str); return ZEND_LONG_MAX; } else { - zend_type_error("Cannot decrement property %s::$%s of type %s%s past its minimal value", + zend_type_error("Cannot decrement property %s::$%s of type %s past its minimal value", ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name), - prop_type1, prop_type2); + ZSTR_VAL(type_str)); + zend_string_release(type_str); return ZEND_LONG_MIN; } } @@ -1794,30 +1721,20 @@ static void zend_post_incdec_property_zval(zval *prop, zend_property_info *prop_ } } -static zend_never_inline void zend_post_incdec_overloaded_property(zval *object, zval *property, void **cache_slot OPLINE_DC EXECUTE_DATA_DC) +static zend_never_inline void zend_post_incdec_overloaded_property(zend_object *object, zend_string *name, void **cache_slot OPLINE_DC EXECUTE_DATA_DC) { - zval rv, obj; + zval rv; zval *z; zval z_copy; - ZVAL_OBJ(&obj, Z_OBJ_P(object)); - Z_ADDREF(obj); - z = Z_OBJ_HT(obj)->read_property(&obj, property, BP_VAR_R, cache_slot, &rv); + GC_ADDREF(object); + z =object->handlers->read_property(object, name, BP_VAR_R, cache_slot, &rv); if (UNEXPECTED(EG(exception))) { - OBJ_RELEASE(Z_OBJ(obj)); + OBJ_RELEASE(object); ZVAL_UNDEF(EX_VAR(opline->result.var)); return; } - if (UNEXPECTED(Z_TYPE_P(z) == IS_OBJECT) && Z_OBJ_HT_P(z)->get) { - zval rv2; - zval *value = Z_OBJ_HT_P(z)->get(z, &rv2); - if (z == &rv) { - zval_ptr_dtor(&rv); - } - ZVAL_COPY_VALUE(z, value); - } - ZVAL_COPY_DEREF(&z_copy, z); ZVAL_COPY(EX_VAR(opline->result.var), &z_copy); if (ZEND_IS_INCREMENT(opline->opcode)) { @@ -1825,38 +1742,28 @@ static zend_never_inline void zend_post_incdec_overloaded_property(zval *object, } else { decrement_function(&z_copy); } - Z_OBJ_HT(obj)->write_property(&obj, property, &z_copy, cache_slot); - OBJ_RELEASE(Z_OBJ(obj)); + object->handlers->write_property(object, name, &z_copy, cache_slot); + OBJ_RELEASE(object); zval_ptr_dtor(&z_copy); zval_ptr_dtor(z); } -static zend_never_inline void zend_pre_incdec_overloaded_property(zval *object, zval *property, void **cache_slot OPLINE_DC EXECUTE_DATA_DC) +static zend_never_inline void zend_pre_incdec_overloaded_property(zend_object *object, zend_string *name, void **cache_slot OPLINE_DC EXECUTE_DATA_DC) { zval rv; - zval *z, obj; + zval *z; zval z_copy; - ZVAL_OBJ(&obj, Z_OBJ_P(object)); - Z_ADDREF(obj); - z = Z_OBJ_HT(obj)->read_property(&obj, property, BP_VAR_R, cache_slot, &rv); + GC_ADDREF(object); + z = object->handlers->read_property(object, name, BP_VAR_R, cache_slot, &rv); if (UNEXPECTED(EG(exception))) { - OBJ_RELEASE(Z_OBJ(obj)); + OBJ_RELEASE(object); if (UNEXPECTED(RETURN_VALUE_USED(opline))) { ZVAL_NULL(EX_VAR(opline->result.var)); } return; } - if (UNEXPECTED(Z_TYPE_P(z) == IS_OBJECT) && Z_OBJ_HT_P(z)->get) { - zval rv2; - zval *value = Z_OBJ_HT_P(z)->get(z, &rv2); - - if (z == &rv) { - zval_ptr_dtor(&rv); - } - ZVAL_COPY_VALUE(z, value); - } ZVAL_COPY_DEREF(&z_copy, z); if (ZEND_IS_INCREMENT(opline->opcode)) { increment_function(&z_copy); @@ -1866,45 +1773,35 @@ static zend_never_inline void zend_pre_incdec_overloaded_property(zval *object, if (UNEXPECTED(RETURN_VALUE_USED(opline))) { ZVAL_COPY(EX_VAR(opline->result.var), &z_copy); } - Z_OBJ_HT(obj)->write_property(&obj, property, &z_copy, cache_slot); - OBJ_RELEASE(Z_OBJ(obj)); + object->handlers->write_property(object, name, &z_copy, cache_slot); + OBJ_RELEASE(object); zval_ptr_dtor(&z_copy); zval_ptr_dtor(z); } -static zend_never_inline void zend_assign_op_overloaded_property(zval *object, zval *property, void **cache_slot, zval *value OPLINE_DC EXECUTE_DATA_DC) +static zend_never_inline void zend_assign_op_overloaded_property(zend_object *object, zend_string *name, void **cache_slot, zval *value OPLINE_DC EXECUTE_DATA_DC) { zval *z; - zval rv, obj, res; + zval rv, res; - ZVAL_OBJ(&obj, Z_OBJ_P(object)); - Z_ADDREF(obj); - z = Z_OBJ_HT(obj)->read_property(&obj, property, BP_VAR_R, cache_slot, &rv); + GC_ADDREF(object); + z = object->handlers->read_property(object, name, BP_VAR_R, cache_slot, &rv); if (UNEXPECTED(EG(exception))) { - OBJ_RELEASE(Z_OBJ(obj)); + OBJ_RELEASE(object); if (UNEXPECTED(RETURN_VALUE_USED(opline))) { ZVAL_UNDEF(EX_VAR(opline->result.var)); } return; } - if (Z_TYPE_P(z) == IS_OBJECT && Z_OBJ_HT_P(z)->get) { - zval rv2; - zval *value = Z_OBJ_HT_P(z)->get(z, &rv2); - - if (z == &rv) { - zval_ptr_dtor(&rv); - } - ZVAL_COPY_VALUE(z, value); - } if (zend_binary_op(&res, z, value OPLINE_CC) == SUCCESS) { - Z_OBJ_HT(obj)->write_property(&obj, property, &res, cache_slot); + object->handlers->write_property(object, name, &res, cache_slot); } if (UNEXPECTED(RETURN_VALUE_USED(opline))) { ZVAL_COPY(EX_VAR(opline->result.var), &res); } zval_ptr_dtor(z); zval_ptr_dtor(&res); - OBJ_RELEASE(Z_OBJ(obj)); + OBJ_RELEASE(object); } /* Utility Functions for Extensions */ @@ -1970,16 +1867,10 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_invalid_method_call(z static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_non_static_method_call(const zend_function *fbc) { - if (fbc->common.fn_flags & ZEND_ACC_ALLOW_STATIC) { - zend_error(E_DEPRECATED, - "Non-static method %s::%s() should not be called statically", - ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name)); - } else { - zend_throw_error( - zend_ce_error, - "Non-static method %s::%s() cannot be called statically", - ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name)); - } + zend_throw_error( + zend_ce_error, + "Non-static method %s::%s() cannot be called statically", + ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name)); } static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_param_must_be_ref(const zend_function *func, uint32_t arg_num) @@ -1993,12 +1884,12 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_param_must_be_ref(con static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_scalar_as_array(void) { - zend_error(E_WARNING, "Cannot use a scalar value as an array"); + zend_throw_error(NULL, "Cannot use a scalar value as an array"); } static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void) { - zend_error(E_WARNING, "Cannot add element to the array as the next element is already occupied"); + zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied"); } static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_resource_as_offset(const zval *dim) @@ -2013,8 +1904,6 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_new_element_for_s static ZEND_COLD void zend_binary_assign_op_dim_slow(zval *container, zval *dim OPLINE_DC EXECUTE_DATA_DC) { - zend_free_op free_op_data1; - if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) { if (opline->op2_type == IS_UNUSED) { zend_use_new_element_for_string(); @@ -2022,11 +1911,10 @@ static ZEND_COLD void zend_binary_assign_op_dim_slow(zval *container, zval *dim zend_check_string_offset(dim, BP_VAR_RW EXECUTE_DATA_CC); zend_wrong_string_offset(EXECUTE_DATA_C); } - } else if (EXPECTED(!Z_ISERROR_P(container))) { + } else { zend_use_scalar_as_array(); } - get_op_data_zval_ptr_r((opline+1)->op1_type, (opline+1)->op1, &free_op_data1); - FREE_OP(free_op_data1); + FREE_OP((opline+1)->op1_type, (opline+1)->op1.var); } static zend_never_inline zend_uchar slow_index_convert(const zval *dim, zend_value *value EXECUTE_DATA_DC) @@ -2188,13 +2076,13 @@ fetch_from_array: retval = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval)); if (UNEXPECTED(retval == NULL)) { zend_cannot_add_element(); - ZVAL_ERROR(result); + ZVAL_UNDEF(result); return; } } else { retval = zend_fetch_dimension_address_inner(Z_ARRVAL_P(container), dim, dim_type, type EXECUTE_DATA_CC); if (UNEXPECTED(!retval)) { - ZVAL_ERROR(result); + ZVAL_UNDEF(result); return; } } @@ -2209,7 +2097,7 @@ fetch_from_array: if (type != BP_VAR_UNSET) { if (ZEND_REF_HAS_TYPE_SOURCES(ref)) { if (UNEXPECTED(!zend_verify_ref_array_assignable(ref))) { - ZVAL_ERROR(result); + ZVAL_UNDEF(result); return; } } @@ -2227,7 +2115,7 @@ fetch_from_array: zend_check_string_offset(dim, type EXECUTE_DATA_CC); zend_wrong_string_offset(EXECUTE_DATA_C); } - ZVAL_ERROR(result); + ZVAL_UNDEF(result); } else if (EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) { if (ZEND_CONST_COND(dim_type == IS_CV, dim != NULL) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) { dim = ZVAL_UNDEFINED_OP2(); @@ -2235,7 +2123,7 @@ fetch_from_array: if (dim_type == IS_CONST && Z_EXTRA_P(dim) == ZEND_EXTRA_VALUE) { dim++; } - retval = Z_OBJ_HT_P(container)->read_dimension(container, dim, type, result); + retval = Z_OBJ_HT_P(container)->read_dimension(Z_OBJ_P(container), dim, type, result); if (UNEXPECTED(retval == &EG(uninitialized_zval))) { zend_class_entry *ce = Z_OBJCE_P(container); @@ -2259,7 +2147,8 @@ fetch_from_array: ZVAL_INDIRECT(result, retval); } } else { - ZVAL_ERROR(result); + ZEND_ASSERT(EG(exception) && "read_dimension() returned NULL without exception"); + ZVAL_UNDEF(result); } } else { if (EXPECTED(Z_TYPE_P(container) <= IS_FALSE)) { @@ -2277,15 +2166,13 @@ return_null: } ZVAL_NULL(result); } - } else if (EXPECTED(Z_ISERROR_P(container))) { - ZVAL_ERROR(result); } else { if (type == BP_VAR_UNSET) { - zend_error(E_WARNING, "Cannot unset offset in a non-array variable"); - ZVAL_NULL(result); + zend_throw_error(NULL, "Cannot unset offset in a non-array variable"); + ZVAL_UNDEF(result); } else { zend_use_scalar_as_array(); - ZVAL_ERROR(result); + ZVAL_UNDEF(result); } } } @@ -2390,7 +2277,7 @@ try_string_offset: if (dim_type == IS_CONST && Z_EXTRA_P(dim) == ZEND_EXTRA_VALUE) { dim++; } - retval = Z_OBJ_HT_P(container)->read_dimension(container, dim, type, result); + retval = Z_OBJ_HT_P(container)->read_dimension(Z_OBJ_P(container), dim, type, result); ZEND_ASSERT(result != NULL); if (retval) { @@ -2470,7 +2357,7 @@ str_idx: ZVAL_UNDEFINED_OP2(); goto str_idx; } else { - zend_error(E_WARNING, "Illegal offset type in isset or empty"); + zend_type_error("Illegal offset type in isset or empty"); return NULL; } } @@ -2482,7 +2369,7 @@ static zend_never_inline int ZEND_FASTCALL zend_isset_dim_slow(zval *container, } if (/*OP1_TYPE != IS_CONST &&*/ EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) { - return Z_OBJ_HT_P(container)->has_dimension(container, offset, 0); + return Z_OBJ_HT_P(container)->has_dimension(Z_OBJ_P(container), offset, 0); } else if (EXPECTED(Z_TYPE_P(container) == IS_STRING)) { /* string offsets */ zend_long lval; @@ -2521,7 +2408,7 @@ static zend_never_inline int ZEND_FASTCALL zend_isempty_dim_slow(zval *container } if (/*OP1_TYPE != IS_CONST &&*/ EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) { - return !Z_OBJ_HT_P(container)->has_dimension(container, offset, 1); + return !Z_OBJ_HT_P(container)->has_dimension(Z_OBJ_P(container), offset, 1); } else if (EXPECTED(Z_TYPE_P(container) == IS_STRING)) { /* string offsets */ zend_long lval; @@ -2603,7 +2490,7 @@ static zend_never_inline uint32_t ZEND_FASTCALL zend_array_key_exists_slow(zval if (UNEXPECTED(Z_TYPE_INFO_P(subject) == IS_UNDEF)) { ZVAL_UNDEFINED_OP2(); } - zend_internal_type_error(EX_USES_STRICT_TYPES(), "array_key_exists() expects parameter 2 to be array, %s given", zend_get_type_by_const(Z_TYPE_P(subject))); + zend_type_error("array_key_exists() expects parameter 2 to be array, %s given", zend_get_type_by_const(Z_TYPE_P(subject))); return IS_NULL; } } @@ -2613,33 +2500,11 @@ static zend_always_inline zend_bool promotes_to_array(zval *val) { || (Z_ISREF_P(val) && Z_TYPE_P(Z_REFVAL_P(val)) <= IS_FALSE); } -static zend_always_inline zend_bool promotes_to_object(zval *val) { - ZVAL_DEREF(val); - return Z_TYPE_P(val) <= IS_FALSE - || (Z_TYPE_P(val) == IS_STRING && Z_STRLEN_P(val) == 0); -} - static zend_always_inline zend_bool check_type_array_assignable(zend_type type) { - if (!type) { - return 1; - } - return ZEND_TYPE_IS_CODE(type) - && (ZEND_TYPE_CODE(type) == IS_ARRAY || ZEND_TYPE_CODE(type) == IS_ITERABLE); -} - -static zend_always_inline zend_bool check_type_stdClass_assignable(zend_type type) { - if (!type) { + if (!ZEND_TYPE_IS_SET(type)) { return 1; } - if (ZEND_TYPE_IS_CLASS(type)) { - if (ZEND_TYPE_IS_CE(type)) { - return ZEND_TYPE_CE(type) == zend_standard_class_def; - } else { - return zend_string_equals_literal_ci(ZEND_TYPE_NAME(type), "stdclass"); - } - } else { - return ZEND_TYPE_CODE(type) == IS_OBJECT; - } + return ZEND_TYPE_IS_MASK(type) && (ZEND_TYPE_MASK(type) & (MAY_BE_ITERABLE|MAY_BE_ARRAY)); } /* Checks whether an array can be assigned to the reference. Returns conflicting property if @@ -2656,20 +2521,6 @@ static zend_never_inline zend_bool zend_verify_ref_array_assignable(zend_referen return 1; } -/* Checks whether an stdClass can be assigned to the reference. Returns conflicting property if - * assignment is not possible, NULL otherwise. */ -static zend_never_inline zend_bool zend_verify_ref_stdClass_assignable(zend_reference *ref) { - zend_property_info *prop; - ZEND_ASSERT(ZEND_REF_HAS_TYPE_SOURCES(ref)); - ZEND_REF_FOREACH_TYPE_SOURCES(ref, prop) { - if (!check_type_stdClass_assignable(prop->type)) { - zend_throw_auto_init_in_ref_error(prop, "stdClass"); - return 0; - } - } ZEND_REF_FOREACH_TYPE_SOURCES_END(); - return 1; -} - static zend_property_info *zend_object_fetch_property_type_info( zend_object *obj, zval *slot) { @@ -2705,21 +2556,6 @@ static zend_never_inline zend_bool zend_handle_fetch_obj_flags( } } break; - case ZEND_FETCH_OBJ_WRITE: - if (promotes_to_object(ptr)) { - if (!prop_info) { - prop_info = zend_object_fetch_property_type_info(obj, ptr); - if (!prop_info) { - break; - } - } - if (!check_type_stdClass_assignable(prop_info->type)) { - zend_throw_auto_init_in_prop_error(prop_info, "stdClass"); - if (result) ZVAL_ERROR(result); - return 0; - } - } - break; case ZEND_FETCH_REF: if (Z_TYPE_P(ptr) != IS_REFERENCE) { if (!prop_info) { @@ -2749,8 +2585,10 @@ static zend_never_inline zend_bool zend_handle_fetch_obj_flags( static zend_always_inline void zend_fetch_property_address(zval *result, zval *container, uint32_t container_op_type, zval *prop_ptr, uint32_t prop_op_type, void **cache_slot, int type, uint32_t flags, zend_bool init_undef OPLINE_DC EXECUTE_DATA_DC) { zval *ptr; + zend_object *zobj; + zend_string *name, *tmp_name; - if (container_op_type != IS_UNUSED && UNEXPECTED(Z_TYPE_P(container) != IS_OBJECT)) { + if (container_op_type != IS_UNUSED && UNEXPECTED(Z_TYPE_P(container) != IS_OBJECT)) { do { if (Z_ISREF_P(container) && Z_TYPE_P(Z_REFVAL_P(container)) == IS_OBJECT) { container = Z_REFVAL_P(container); @@ -2768,17 +2606,16 @@ static zend_always_inline void zend_fetch_property_address(zval *result, zval *c return; } - container = make_real_object(container, prop_ptr OPLINE_CC EXECUTE_DATA_CC); - if (UNEXPECTED(!container)) { - ZVAL_ERROR(result); - return; - } + zend_throw_non_object_error(container, prop_ptr OPLINE_CC EXECUTE_DATA_CC); + ZVAL_ERROR(result); + return; } while (0); } + + zobj = Z_OBJ_P(container); if (prop_op_type == IS_CONST && - EXPECTED(Z_OBJCE_P(container) == CACHED_PTR_EX(cache_slot))) { + EXPECTED(zobj->ce == CACHED_PTR_EX(cache_slot))) { uintptr_t prop_offset = (uintptr_t)CACHED_PTR_EX(cache_slot + 1); - zend_object *zobj = Z_OBJ_P(container); if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) { ptr = OBJ_PROP(zobj, prop_offset); @@ -2806,9 +2643,15 @@ static zend_always_inline void zend_fetch_property_address(zval *result, zval *c } } } - ptr = Z_OBJ_HT_P(container)->get_property_ptr_ptr(container, prop_ptr, type, cache_slot); + + if (prop_op_type == IS_CONST) { + name = Z_STR_P(prop_ptr); + } else { + name = zval_get_tmp_string(prop_ptr, &tmp_name); + } + ptr = zobj->handlers->get_property_ptr_ptr(zobj, name, type, cache_slot); if (NULL == ptr) { - ptr = Z_OBJ_HT_P(container)->read_property(container, prop_ptr, type, cache_slot, result); + ptr = zobj->handlers->read_property(zobj, name, type, cache_slot, result); if (ptr == result) { if (UNEXPECTED(Z_ISREF_P(ptr) && Z_REFCOUNT_P(ptr) == 1)) { ZVAL_UNREF(ptr); @@ -2851,13 +2694,12 @@ static zend_always_inline void zend_assign_to_property_reference(zval *container variable_ptr = Z_INDIRECT_P(variable_ptr); } + /* variable_ptr may be ERROR if fetching the property threw an exception. */ if (UNEXPECTED(Z_ISERROR_P(variable_ptr))) { variable_ptr = &EG(uninitialized_zval); } else if (UNEXPECTED(Z_TYPE(variable) != IS_INDIRECT)) { zend_throw_error(NULL, "Cannot assign by reference to overloaded object"); variable_ptr = &EG(uninitialized_zval); - } else if (/*OP_DATA_TYPE == IS_VAR &&*/ UNEXPECTED(Z_ISERROR_P(value_ptr))) { - variable_ptr = &EG(uninitialized_zval); } else if (/*OP_DATA_TYPE == IS_VAR &&*/ (opline->extended_value & ZEND_RETURNS_FUNCTION) && UNEXPECTED(!Z_ISREF_P(value_ptr))) { @@ -2913,7 +2755,6 @@ static zend_never_inline void zend_assign_to_property_reference_var_var(zval *co } static zend_never_inline int zend_fetch_static_property_address_ex(zval **retval, zend_property_info **prop_info, uint32_t cache_slot, int fetch_type OPLINE_DC EXECUTE_DATA_DC) { - zend_free_op free_op1; zend_string *name, *tmp_name; zend_class_entry *ce; zend_property_info *property_info; @@ -2955,7 +2796,7 @@ static zend_never_inline int zend_fetch_static_property_address_ex(zval **retval if (EXPECTED(op1_type == IS_CONST)) { name = Z_STR_P(RT_CONSTANT(opline, opline->op1)); } else { - zval *varname = get_zval_ptr_undef(opline->op1_type, opline->op1, &free_op1, BP_VAR_R); + zval *varname = get_zval_ptr_undef(opline->op1_type, opline->op1, BP_VAR_R); if (EXPECTED(Z_TYPE_P(varname) == IS_STRING)) { name = Z_STR_P(varname); tmp_name = NULL; @@ -2972,9 +2813,7 @@ static zend_never_inline int zend_fetch_static_property_address_ex(zval **retval if (UNEXPECTED(op1_type != IS_CONST)) { zend_tmp_string_release(tmp_name); - if (op1_type != IS_CV) { - zval_ptr_dtor_nogc(free_op1); - } + FREE_OP(op1_type, opline->op1.var); } if (UNEXPECTED(*retval == NULL)) { @@ -3001,7 +2840,8 @@ static zend_always_inline int zend_fetch_static_property_address(zval **retval, property_info = CACHED_PTR(cache_slot + sizeof(void *) * 2); if ((fetch_type == BP_VAR_R || fetch_type == BP_VAR_RW) - && UNEXPECTED(Z_TYPE_P(*retval) == IS_UNDEF) && UNEXPECTED(property_info->type != 0)) { + && UNEXPECTED(Z_TYPE_P(*retval) == IS_UNDEF) + && UNEXPECTED(ZEND_TYPE_IS_SET(property_info->type))) { zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization", ZSTR_VAL(property_info->ce->name), zend_get_unmangled_property_name(property_info->name)); @@ -3014,7 +2854,7 @@ static zend_always_inline int zend_fetch_static_property_address(zval **retval, } } - if (flags && property_info->type) { + if (flags && ZEND_TYPE_IS_SET(property_info->type)) { zend_handle_fetch_obj_flags(NULL, *retval, NULL, property_info, flags); } @@ -3026,58 +2866,59 @@ static zend_always_inline int zend_fetch_static_property_address(zval **retval, } ZEND_API ZEND_COLD void zend_throw_ref_type_error_type(zend_property_info *prop1, zend_property_info *prop2, zval *zv) { - const char *prop1_type1, *prop1_type2, *prop2_type1, *prop2_type2; - zend_format_type(prop1->type, &prop1_type1, &prop1_type2); - zend_format_type(prop2->type, &prop2_type1, &prop2_type2); - zend_type_error("Reference with value of type %s held by property %s::$%s of type %s%s is not compatible with property %s::$%s of type %s%s", + zend_string *type1_str = zend_type_to_string(prop1->type); + zend_string *type2_str = zend_type_to_string(prop2->type); + zend_type_error("Reference with value of type %s held by property %s::$%s of type %s is not compatible with property %s::$%s of type %s", Z_TYPE_P(zv) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(zv)->name) : zend_get_type_by_const(Z_TYPE_P(zv)), ZSTR_VAL(prop1->ce->name), zend_get_unmangled_property_name(prop1->name), - prop1_type1, prop1_type2, + ZSTR_VAL(type1_str), ZSTR_VAL(prop2->ce->name), zend_get_unmangled_property_name(prop2->name), - prop2_type1, prop2_type2 + ZSTR_VAL(type2_str) ); + zend_string_release(type1_str); + zend_string_release(type2_str); } ZEND_API ZEND_COLD void zend_throw_ref_type_error_zval(zend_property_info *prop, zval *zv) { - const char *prop_type1, *prop_type2; - zend_format_type(prop->type, &prop_type1, &prop_type2); - zend_type_error("Cannot assign %s to reference held by property %s::$%s of type %s%s", + zend_string *type_str = zend_type_to_string(prop->type); + zend_type_error("Cannot assign %s to reference held by property %s::$%s of type %s", Z_TYPE_P(zv) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(zv)->name) : zend_get_type_by_const(Z_TYPE_P(zv)), ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name), - prop_type1, prop_type2 + ZSTR_VAL(type_str) ); + zend_string_release(type_str); } ZEND_API ZEND_COLD void zend_throw_conflicting_coercion_error(zend_property_info *prop1, zend_property_info *prop2, zval *zv) { - const char *prop1_type1, *prop1_type2, *prop2_type1, *prop2_type2; - zend_format_type(prop1->type, &prop1_type1, &prop1_type2); - zend_format_type(prop2->type, &prop2_type1, &prop2_type2); - zend_type_error("Cannot assign %s to reference held by property %s::$%s of type %s%s and property %s::$%s of type %s%s, as this would result in an inconsistent type conversion", + zend_string *type1_str = zend_type_to_string(prop1->type); + zend_string *type2_str = zend_type_to_string(prop2->type); + zend_type_error("Cannot assign %s to reference held by property %s::$%s of type %s and property %s::$%s of type %s, as this would result in an inconsistent type conversion", Z_TYPE_P(zv) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(zv)->name) : zend_get_type_by_const(Z_TYPE_P(zv)), ZSTR_VAL(prop1->ce->name), zend_get_unmangled_property_name(prop1->name), - prop1_type1, prop1_type2, + ZSTR_VAL(type1_str), ZSTR_VAL(prop2->ce->name), zend_get_unmangled_property_name(prop2->name), - prop2_type1, prop2_type2 + ZSTR_VAL(type2_str) ); + zend_string_release(type1_str); + zend_string_release(type2_str); } /* 1: valid, 0: invalid, -1: may be valid after type coercion */ static zend_always_inline int i_zend_verify_type_assignable_zval( zend_type *type_ptr, zend_class_entry *self_ce, zval *zv, zend_bool strict) { zend_type type = *type_ptr; - zend_uchar type_code; + uint32_t type_mask; zend_uchar zv_type = Z_TYPE_P(zv); - if (ZEND_TYPE_ALLOW_NULL(type) && zv_type == IS_NULL) { - return 1; - } - if (ZEND_TYPE_IS_CLASS(type)) { + if (ZEND_TYPE_ALLOW_NULL(type) && zv_type == IS_NULL) { + return 1; + } if (!ZEND_TYPE_IS_CE(type)) { if (!zend_resolve_class_type(type_ptr, self_ce)) { return 0; @@ -3087,26 +2928,25 @@ static zend_always_inline int i_zend_verify_type_assignable_zval( return zv_type == IS_OBJECT && instanceof_function(Z_OBJCE_P(zv), ZEND_TYPE_CE(type)); } - type_code = ZEND_TYPE_CODE(type); - if (type_code == zv_type || - (type_code == _IS_BOOL && (zv_type == IS_FALSE || zv_type == IS_TRUE))) { + if (ZEND_TYPE_CONTAINS_CODE(type, zv_type)) { return 1; } - if (type_code == IS_ITERABLE) { + type_mask = ZEND_TYPE_MASK(type); + if (type_mask & MAY_BE_ITERABLE) { return zend_is_iterable(zv); } /* SSTH Exception: IS_LONG may be accepted as IS_DOUBLE (converted) */ if (strict) { - if (type_code == IS_DOUBLE && zv_type == IS_LONG) { + if ((type_mask & MAY_BE_DOUBLE) && zv_type == IS_LONG) { return -1; } return 0; } /* No weak conversions for arrays and objects */ - if (type_code == IS_ARRAY || type_code == IS_OBJECT) { + if (type_mask & (MAY_BE_ARRAY|MAY_BE_OBJECT)) { return 0; } @@ -3128,7 +2968,7 @@ ZEND_API zend_bool ZEND_FASTCALL zend_verify_ref_assignable_zval(zend_reference * must be the same (modulo nullability). To handle this, remember the first type we see and * compare against it when coercion becomes necessary. */ zend_property_info *seen_prop = NULL; - zend_uchar seen_type; + uint32_t seen_type_mask; zend_bool needs_coercion = 0; ZEND_ASSERT(Z_TYPE_P(zv) != IS_REFERENCE); @@ -3145,14 +2985,16 @@ ZEND_API zend_bool ZEND_FASTCALL zend_verify_ref_assignable_zval(zend_reference if (!seen_prop) { seen_prop = prop; - seen_type = ZEND_TYPE_IS_CLASS(prop->type) ? IS_OBJECT : ZEND_TYPE_CODE(prop->type); - } else if (needs_coercion && seen_type != ZEND_TYPE_CODE(prop->type)) { + seen_type_mask = ZEND_TYPE_IS_CLASS(prop->type) + ? MAY_BE_OBJECT : ZEND_TYPE_MASK(ZEND_TYPE_WITHOUT_NULL(prop->type)); + } else if (needs_coercion + && seen_type_mask != ZEND_TYPE_MASK(ZEND_TYPE_WITHOUT_NULL(prop->type))) { zend_throw_conflicting_coercion_error(seen_prop, prop, zv); return 0; } } ZEND_REF_FOREACH_TYPE_SOURCES_END(); - if (UNEXPECTED(needs_coercion && !zend_verify_weak_scalar_type_hint(seen_type, zv))) { + if (UNEXPECTED(needs_coercion && !zend_verify_weak_scalar_type_hint(seen_type_mask, zv))) { zend_throw_ref_type_error_zval(seen_prop, zv); return 0; } @@ -3213,12 +3055,13 @@ ZEND_API zend_bool ZEND_FASTCALL zend_verify_prop_assignable_by_ref(zend_propert if (result < 0) { zend_property_info *ref_prop = ZEND_REF_FIRST_SOURCE(Z_REF_P(orig_val)); - if (ZEND_TYPE_CODE(prop_info->type) != ZEND_TYPE_CODE(ref_prop->type)) { + if (ZEND_TYPE_MASK(ZEND_TYPE_WITHOUT_NULL(prop_info->type)) + != ZEND_TYPE_MASK(ZEND_TYPE_WITHOUT_NULL(ref_prop->type))) { /* Invalid due to conflicting coercion */ zend_throw_ref_type_error_type(ref_prop, prop_info, val); return 0; } - if (zend_verify_weak_scalar_type_hint(ZEND_TYPE_CODE(prop_info->type), val)) { + if (zend_verify_weak_scalar_type_hint(ZEND_TYPE_MASK(prop_info->type), val)) { return 1; } } @@ -3908,7 +3751,8 @@ static void cleanup_live_vars(zend_execute_data *execute_data, uint32_t op_num, } } else if (kind == ZEND_LIVE_SILENCE) { /* restore previous error_reporting value */ - if (!EG(error_reporting) && Z_LVAL_P(var) != 0) { + if (E_HAS_ONLY_FATAL_ERRORS(EG(error_reporting)) + && !E_HAS_ONLY_FATAL_ERRORS(Z_LVAL_P(var))) { EG(error_reporting) = Z_LVAL_P(var); } } @@ -3984,9 +3828,7 @@ static zend_never_inline zend_execute_data *zend_init_dynamic_call_string(zend_s if (UNEXPECTED(!(fbc->common.fn_flags & ZEND_ACC_STATIC))) { zend_non_static_method_call(fbc); - if (UNEXPECTED(EG(exception) != NULL)) { - return NULL; - } + return NULL; } if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) { init_func_run_time_cache(&fbc->op_array); @@ -4017,7 +3859,7 @@ static zend_never_inline zend_execute_data *zend_init_dynamic_call_string(zend_s } /* }}} */ -static zend_never_inline zend_execute_data *zend_init_dynamic_call_object(zval *function, uint32_t num_args) /* {{{ */ +static zend_never_inline zend_execute_data *zend_init_dynamic_call_object(zend_object *function, uint32_t num_args) /* {{{ */ { zend_function *fbc; void *object_or_called_scope; @@ -4025,8 +3867,8 @@ static zend_never_inline zend_execute_data *zend_init_dynamic_call_object(zval * zend_object *object; uint32_t call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC; - if (EXPECTED(Z_OBJ_HANDLER_P(function, get_closure)) && - EXPECTED(Z_OBJ_HANDLER_P(function, get_closure)(function, &called_scope, &fbc, &object) == SUCCESS)) { + if (EXPECTED(function->handlers->get_closure) && + EXPECTED(function->handlers->get_closure(function, &called_scope, &fbc, &object, 0) == SUCCESS)) { object_or_called_scope = called_scope; if (fbc->common.fn_flags & ZEND_ACC_CLOSURE) { @@ -4108,9 +3950,7 @@ static zend_never_inline zend_execute_data *zend_init_dynamic_call_array(zend_ar } if (!(fbc->common.fn_flags & ZEND_ACC_STATIC)) { zend_non_static_method_call(fbc); - if (UNEXPECTED(EG(exception) != NULL)) { - return NULL; - } + return NULL; } object_or_called_scope = called_scope; } else { @@ -4240,43 +4080,6 @@ already_compiled: } /* }}} */ -static ZEND_COLD int ZEND_FASTCALL zend_do_fcall_overloaded(zend_execute_data *call, zval *ret) /* {{{ */ -{ - zend_function *fbc = call->func; - zend_object *object; - - /* Not sure what should be done here if it's a static method */ - if (UNEXPECTED(Z_TYPE(call->This) != IS_OBJECT)) { - zend_vm_stack_free_args(call); - if (fbc->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY) { - zend_string_release_ex(fbc->common.function_name, 0); - } - efree(fbc); - zend_vm_stack_free_call_frame(call); - - zend_throw_error(NULL, "Cannot call overloaded function for non-object"); - return 0; - } - - object = Z_OBJ(call->This); - - ZVAL_NULL(ret); - - EG(current_execute_data) = call; - object->handlers->call_method(fbc->common.function_name, object, call, ret); - EG(current_execute_data) = call->prev_execute_data; - - zend_vm_stack_free_args(call); - - if (fbc->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY) { - zend_string_release_ex(fbc->common.function_name, 0); - } - efree(fbc); - - return 1; -} -/* }}} */ - static zend_never_inline zend_bool ZEND_FASTCALL zend_fe_reset_iterator(zval *array_ptr, int by_ref OPLINE_DC EXECUTE_DATA_DC) /* {{{ */ { zend_class_entry *ce = Z_OBJCE_P(array_ptr); @@ -4324,96 +4127,30 @@ static zend_always_inline int _zend_quick_get_constant( const zval *key, uint32_t flags, int check_defined_only OPLINE_DC EXECUTE_DATA_DC) /* {{{ */ { zval *zv; - const zval *orig_key = key; zend_constant *c = NULL; + /* null/true/false are resolved during compilation, so don't check for them here. */ zv = zend_hash_find_ex(EG(zend_constants), Z_STR_P(key), 1); if (zv) { c = (zend_constant*)Z_PTR_P(zv); - } else { + } else if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) { key++; zv = zend_hash_find_ex(EG(zend_constants), Z_STR_P(key), 1); - if (zv && (ZEND_CONSTANT_FLAGS((zend_constant*)Z_PTR_P(zv)) & CONST_CS) == 0) { + if (zv) { c = (zend_constant*)Z_PTR_P(zv); - } else { - if ((flags & (IS_CONSTANT_IN_NAMESPACE|IS_CONSTANT_UNQUALIFIED)) == (IS_CONSTANT_IN_NAMESPACE|IS_CONSTANT_UNQUALIFIED)) { - key++; - zv = zend_hash_find_ex(EG(zend_constants), Z_STR_P(key), 1); - if (zv) { - c = (zend_constant*)Z_PTR_P(zv); - } else { - key++; - zv = zend_hash_find_ex(EG(zend_constants), Z_STR_P(key), 1); - if (zv && (ZEND_CONSTANT_FLAGS((zend_constant*)Z_PTR_P(zv)) & CONST_CS) == 0) { - c = (zend_constant*)Z_PTR_P(zv); - } - } - } } } if (!c) { if (!check_defined_only) { - if ((opline->op1.num & IS_CONSTANT_UNQUALIFIED) != 0) { - char *actual = (char *)zend_memrchr(Z_STRVAL_P(RT_CONSTANT(opline, opline->op2)), '\\', Z_STRLEN_P(RT_CONSTANT(opline, opline->op2))); - if (!actual) { - ZVAL_STR_COPY(EX_VAR(opline->result.var), Z_STR_P(RT_CONSTANT(opline, opline->op2))); - } else { - actual++; - ZVAL_STRINGL(EX_VAR(opline->result.var), - actual, Z_STRLEN_P(RT_CONSTANT(opline, opline->op2)) - (actual - Z_STRVAL_P(RT_CONSTANT(opline, opline->op2)))); - } - /* non-qualified constant - allow text substitution */ - zend_error(E_WARNING, "Use of undefined constant %s - assumed '%s' (this will throw an Error in a future version of PHP)", - Z_STRVAL_P(EX_VAR(opline->result.var)), Z_STRVAL_P(EX_VAR(opline->result.var))); - } else { - zend_throw_error(NULL, "Undefined constant '%s'", Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))); - ZVAL_UNDEF(EX_VAR(opline->result.var)); - } + zend_throw_error(NULL, "Undefined constant '%s'", Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))); + ZVAL_UNDEF(EX_VAR(opline->result.var)); } return FAILURE; } if (!check_defined_only) { ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value); - if (!(ZEND_CONSTANT_FLAGS(c) & (CONST_CS|CONST_CT_SUBST))) { - const char *ns_sep; - size_t shortname_offset; - size_t shortname_len; - zend_bool is_deprecated; - - if (flags & IS_CONSTANT_UNQUALIFIED) { - const zval *access_key; - - if (!(flags & IS_CONSTANT_IN_NAMESPACE)) { - access_key = orig_key - 1; - } else { - if (key < orig_key + 2) { - goto check_short_name; - } else { - access_key = orig_key + 2; - } - } - is_deprecated = !zend_string_equals(c->name, Z_STR_P(access_key)); - } else { -check_short_name: - ns_sep = zend_memrchr(ZSTR_VAL(c->name), '\\', ZSTR_LEN(c->name)); - ZEND_ASSERT(ns_sep); - /* Namespaces are always case-insensitive. Only compare shortname. */ - shortname_offset = ns_sep - ZSTR_VAL(c->name) + 1; - shortname_len = ZSTR_LEN(c->name) - shortname_offset; - - is_deprecated = memcmp(ZSTR_VAL(c->name) + shortname_offset, Z_STRVAL_P(orig_key - 1) + shortname_offset, shortname_len) != 0; - } - - if (is_deprecated) { - zend_error(E_DEPRECATED, - "Case-insensitive constants are deprecated. " - "The correct casing for this constant is \"%s\"", - ZSTR_VAL(c->name)); - return SUCCESS; - } - } } CACHE_PTR(opline->extended_value, c); @@ -4613,27 +4350,21 @@ ZEND_API user_opcode_handler_t zend_get_user_opcode_handler(zend_uchar opcode) return zend_user_opcode_handlers[opcode]; } -ZEND_API zval *zend_get_zval_ptr(const zend_op *opline, int op_type, const znode_op *node, const zend_execute_data *execute_data, zend_free_op *should_free, int type) +ZEND_API zval *zend_get_zval_ptr(const zend_op *opline, int op_type, const znode_op *node, const zend_execute_data *execute_data, int type) { zval *ret; switch (op_type) { case IS_CONST: ret = RT_CONSTANT(opline, *node); - *should_free = NULL; break; case IS_TMP_VAR: case IS_VAR: - ret = EX_VAR(node->var); - *should_free = ret; - break; case IS_CV: ret = EX_VAR(node->var); - *should_free = NULL; break; default: ret = NULL; - *should_free = ret; break; } return ret; |
