Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regression test for broken non commutative multiplication (#9175) #9218

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 21 additions & 19 deletions Zend/tests/operator_unsupported_types.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,20 @@ Unsupported operand types: string - string
Warning: A non-numeric value encountered
Unsupported operand types: string - string
Unsupported operand types: array * array
Unsupported operand types: stdClass * array
Unsupported operand types: resource * array
Unsupported operand types: array * stdClass
Unsupported operand types: array * resource
Unsupported operand types: array * string
Unsupported operand types: stdClass * array
Unsupported operand types: stdClass * stdClass
Unsupported operand types: stdClass * resource
Unsupported operand types: stdClass * string
Unsupported operand types: resource * array
Unsupported operand types: stdClass * resource
Unsupported operand types: resource * stdClass
Unsupported operand types: resource * resource
Unsupported operand types: resource * string
Unsupported operand types: string * array
Unsupported operand types: stdClass * string
Unsupported operand types: resource * string
Unsupported operand types: string * stdClass
Unsupported operand types: string * resource
Unsupported operand types: string * string
Unsupported operand types: array * null
Unsupported operand types: null * array
Expand All @@ -310,33 +310,35 @@ Unsupported operand types: array * string
Warning: A non-numeric value encountered
Unsupported operand types: string * array
Unsupported operand types: stdClass * null
Unsupported operand types: stdClass * null
Unsupported operand types: stdClass * bool
Unsupported operand types: stdClass * bool
Unsupported operand types: null * stdClass
Unsupported operand types: stdClass * bool
Unsupported operand types: bool * stdClass
Unsupported operand types: stdClass * bool
Unsupported operand types: bool * stdClass
Unsupported operand types: stdClass * int
Unsupported operand types: stdClass * int
Unsupported operand types: stdClass * float
Unsupported operand types: int * stdClass
Unsupported operand types: stdClass * float
Unsupported operand types: float * stdClass
Unsupported operand types: stdClass * string
Unsupported operand types: string * stdClass
Unsupported operand types: stdClass * string
Unsupported operand types: stdClass * string
Unsupported operand types: stdClass * string
Unsupported operand types: resource * null
Warning: A non-numeric value encountered
Unsupported operand types: string * stdClass
Unsupported operand types: resource * null
Unsupported operand types: null * resource
Unsupported operand types: resource * bool
Unsupported operand types: bool * resource
Unsupported operand types: resource * bool
Unsupported operand types: resource * bool
Unsupported operand types: resource * bool
Unsupported operand types: resource * int
Unsupported operand types: bool * resource
Unsupported operand types: resource * int
Unsupported operand types: int * resource
Unsupported operand types: resource * float
Unsupported operand types: resource * float
Unsupported operand types: resource * string
Unsupported operand types: resource * string
Unsupported operand types: float * resource
Unsupported operand types: resource * string
Unsupported operand types: string * resource
Unsupported operand types: resource * string
Warning: A non-numeric value encountered
Unsupported operand types: string * resource
Unsupported operand types: string * null
Unsupported operand types: null * string
Unsupported operand types: string * bool
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ ZEND_VM_HELPER(zend_mul_helper, ANY, ANY, zval *op_1, zval *op_2)
ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}

ZEND_VM_COLD_CONSTCONST_HANDLER(3, ZEND_MUL, CONST|TMPVARCV, CONST|TMPVARCV, SPEC(COMMUTATIVE))
ZEND_VM_COLD_CONSTCONST_HANDLER(3, ZEND_MUL, CONST|TMPVARCV, CONST|TMPVARCV)
{
USE_OPLINE
zval *op1, *op2, *result;
Expand Down Expand Up @@ -7591,7 +7591,7 @@ ZEND_VM_HOT_NOCONST_HANDLER(198, ZEND_JMP_NULL, CONST|TMP|VAR|CV, JMP_ADDR)
uint32_t short_circuiting_type = opline->extended_value & ZEND_SHORT_CIRCUITING_CHAIN_MASK;
if (EXPECTED(short_circuiting_type == ZEND_SHORT_CIRCUITING_CHAIN_EXPR)) {
ZVAL_NULL(result);
if (OP1_TYPE == IS_CV
if (OP1_TYPE == IS_CV
&& UNEXPECTED(Z_TYPE_P(val) == IS_UNDEF)
&& (opline->extended_value & ZEND_JMP_NULL_BP_VAR_IS) == 0
) {
Expand Down
71 changes: 61 additions & 10 deletions Zend/zend_vm_execute.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Zend/zend_vm_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
_(48, ZEND_SUB_SPEC_TMPVARCV_TMPVARCV) \
_(50, ZEND_SUB_SPEC_TMPVARCV_TMPVARCV) \
_(51, ZEND_MUL_SPEC_CONST_CONST) \
_(52, ZEND_MUL_SPEC_CONST_TMPVARCV) \
_(53, ZEND_MUL_SPEC_CONST_TMPVARCV) \
_(55, ZEND_MUL_SPEC_CONST_TMPVARCV) \
_(56, ZEND_MUL_SPEC_TMPVARCV_CONST) \
_(57, ZEND_MUL_SPEC_TMPVARCV_TMPVARCV) \
_(58, ZEND_MUL_SPEC_TMPVARCV_TMPVARCV) \
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_vm_opcodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ static uint32_t zend_vm_opcodes_flags[203] = {
0x00000000,
0x00000b0b,
0x00000b0b,
0x80000b0b,
0x00000b0b,
0x00000707,
0x00000b0b,
0x00000b0b,
Expand Down
73 changes: 73 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,73 @@ static ZEND_METHOD(ZendTestForbidDynamicCall, callStatic)
zend_forbid_dynamic_call();
}

// TODO HERE
/* ncm refers to non commutative multiplication */
static zend_class_entry *ncm_ce;
static zend_object_handlers ncm_object_handlers;

static zend_object* ncm_object_create_ex(zend_class_entry* ce, zend_long l) {
zend_object *obj = zend_objects_new(ce);
object_properties_init(obj, ce);
obj->handlers = &ncm_object_handlers;
ZVAL_LONG(OBJ_PROP_NUM(obj, 0), l);
return obj;
}
static zend_object *ncm_object_create(zend_class_entry *ce) /* {{{ */
{
return ncm_object_create_ex(ce, 0);
}
/* }}} */

static inline void ncm_create(zval *target, zend_long l) /* {{{ */
{
ZVAL_OBJ(target, ncm_object_create_ex(ncm_ce, l));
}

#define IS_NCM(zval) \
(Z_TYPE_P(zval) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zval), ncm_ce))

static void ncm_subtraction(zval *result, zval *op1, zval *op2)
{
zend_long val_1;
zend_long val_2;
if (IS_NCM(op1)) {
val_1 = Z_LVAL_P(OBJ_PROP_NUM(Z_OBJ_P(op1), 0));
} else {
val_1 = zval_get_long(op1);
}
if (IS_NCM(op2)) {
val_2 = Z_LVAL_P(OBJ_PROP_NUM(Z_OBJ_P(op2), 0));
} else {
val_2 = zval_get_long(op2);
}

ncm_create(result, val_1 - val_2);
}

static zend_result ncm_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op2)
{
switch (opcode) {
case ZEND_MUL:
ncm_subtraction(result, op1, op2);
if (UNEXPECTED(EG(exception))) { return FAILURE; }
return SUCCESS;
default:
return FAILURE;
}
}

PHP_METHOD(NonCommutativeMultiplication, __construct)
{
zend_long l;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(l)
ZEND_PARSE_PARAMETERS_END();

ZVAL_LONG(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), l);
}

PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("zend_test.replace_zend_execute_ex", "0", PHP_INI_SYSTEM, OnUpdateBool, replace_zend_execute_ex, zend_zend_test_globals, zend_test_globals)
STD_PHP_INI_BOOLEAN("zend_test.register_passes", "0", PHP_INI_SYSTEM, OnUpdateBool, register_passes, zend_zend_test_globals, zend_test_globals)
Expand Down Expand Up @@ -713,6 +780,12 @@ PHP_MINIT_FUNCTION(zend_test)
zend_test_string_enum = register_class_ZendTestStringEnum();
zend_test_int_enum = register_class_ZendTestIntEnum();

/* Non commutative multiplication class */
ncm_ce = register_class_NonCommutativeMultiplication();
ncm_ce->create_object = ncm_object_create;
memcpy(&ncm_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
ncm_object_handlers.do_operation = ncm_do_operation;

zend_register_functions(NULL, ext_function_legacy, NULL, EG(current_module)->type);

// Loading via dl() not supported with the observer API
Expand Down
6 changes: 6 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ enum ZendTestIntEnum: int {
case Baz = -1;
}

final class NonCommutativeMultiplication {
private int $val;

public function __construct(int $val) {}
}

function zend_test_array_return(): array {}

function zend_test_nullable_array_return(): ?array {}
Expand Down