Skip to content

Commit 1949a26

Browse files
committed
Remove more null arithmetic UB
Introduce an EX_NUM_TO_VAR macro to mirror EX_VAR_TO_NUM and replace usages of the ZEND_CALL_VAR_NUM(NULL) pattern.
1 parent 3ae04b0 commit 1949a26

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

Zend/zend_compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ struct _zend_execute_data {
578578
#define EX_VAR_NUM(n) ZEND_CALL_VAR_NUM(execute_data, n)
579579

580580
#define EX_VAR_TO_NUM(n) ((uint32_t)((n) / sizeof(zval) - ZEND_CALL_FRAME_SLOT))
581+
#define EX_NUM_TO_VAR(n) ((uint32_t)((n + ZEND_CALL_FRAME_SLOT) * sizeof(zval)))
581582

582583
#define ZEND_OPLINE_TO_OFFSET(opline, target) \
583584
((char*)(target) - (char*)(opline))

Zend/zend_opcode.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,15 +1035,15 @@ ZEND_API int pass_two(zend_op_array *op_array)
10351035
if (opline->op1_type == IS_CONST) {
10361036
ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op1);
10371037
} else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
1038-
opline->op1.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op1.var);
1038+
opline->op1.var = EX_NUM_TO_VAR(op_array->last_var + opline->op1.var);
10391039
}
10401040
if (opline->op2_type == IS_CONST) {
10411041
ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op2);
10421042
} else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
1043-
opline->op2.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op2.var);
1043+
opline->op2.var = EX_NUM_TO_VAR(op_array->last_var + opline->op2.var);
10441044
}
10451045
if (opline->result_type & (IS_VAR|IS_TMP_VAR)) {
1046-
opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->result.var);
1046+
opline->result.var = EX_NUM_TO_VAR(op_array->last_var + opline->result.var);
10471047
}
10481048
ZEND_VM_SET_OPCODE_HANDLER(opline);
10491049
opline++;

ext/opcache/Optimizer/zend_optimizer_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#define ZEND_OP2_JMP_ADDR(opline) OP_JMP_ADDR(opline, (opline)->op2)
3232

3333
#define VAR_NUM(v) EX_VAR_TO_NUM(v)
34-
#define NUM_VAR(v) ((uint32_t)(zend_uintptr_t)ZEND_CALL_VAR_NUM(0, v))
34+
#define NUM_VAR(v) EX_NUM_TO_VAR(v)
3535

3636
#define INV_COND(op) ((op) == ZEND_JMPZ ? ZEND_JMPNZ : ZEND_JMPZ)
3737
#define INV_EX_COND(op) ((op) == ZEND_JMPZ_EX ? ZEND_JMPNZ : ZEND_JMPZ)

ext/opcache/jit/zend_jit_x86.dasc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,7 +2718,7 @@ static int zend_jit_load_reg(dasm_State **Dst, zend_jit_addr src, zend_jit_addr
27182718
static int zend_jit_store_var(dasm_State **Dst, uint32_t info, int var, zend_reg reg)
27192719
{
27202720
zend_jit_addr src = ZEND_ADDR_REG(reg);
2721-
zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (uint32_t)(uintptr_t)ZEND_CALL_VAR_NUM(NULL, var));
2721+
zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
27222722

27232723
return zend_jit_spill_store(Dst, src, dst, info, 1);
27242724
}
@@ -2751,7 +2751,7 @@ static int zend_jit_store_var_if_necessary_ex(dasm_State **Dst, int var, zend_ji
27512751

27522752
static int zend_jit_load_var(dasm_State **Dst, uint32_t info, int var, zend_reg reg)
27532753
{
2754-
zend_jit_addr src = ZEND_ADDR_MEM_ZVAL(ZREG_FP, (uint32_t)(uintptr_t)ZEND_CALL_VAR_NUM(NULL, var));
2754+
zend_jit_addr src = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var));
27552755
zend_jit_addr dst = ZEND_ADDR_REG(reg);
27562756

27572757
return zend_jit_load_reg(Dst, src, dst, info);
@@ -6971,7 +6971,7 @@ static int zend_jit_do_fcall(dasm_State **Dst, const zend_op *opline, const zend
69716971

69726972
if (func) {
69736973
for (i = call_info->num_args; i < func->op_array.last_var; i++) {
6974-
uint32_t n = (uint32_t)(uintptr_t)ZEND_CALL_VAR_NUM(NULL, i);
6974+
uint32_t n = EX_NUM_TO_VAR(i);
69756975
| SET_Z_TYPE_INFO RX + n, IS_UNDEF
69766976
}
69776977
}
@@ -7189,7 +7189,7 @@ static int zend_jit_do_fcall(dasm_State **Dst, const zend_op *opline, const zend
71897189
| // zend_vm_stack_free_args(call);
71907190
if (func) {
71917191
for (i = 0; i < call_info->num_args; i++ ) {
7192-
uint32_t offset = (uint32_t)(uintptr_t)ZEND_CALL_VAR_NUM(NULL, i);
7192+
uint32_t offset = EX_NUM_TO_VAR(i);
71937193
| ZVAL_PTR_DTOR ZEND_ADDR_MEM_ZVAL(ZREG_RX, offset), MAY_BE_ANY|MAY_BE_RC1|MAY_BE_RCN, 0, 1, 0, opline
71947194
}
71957195
} else {
@@ -7909,8 +7909,7 @@ static int zend_jit_free_compiled_variables(dasm_State **Dst, const zend_op *opl
79097909
const zend_op *opline = op_array->opcodes + ssa->cfg.blocks[j].start + ssa->cfg.blocks[j].len - 1;
79107910

79117911
if (opline->opcode == ZEND_RETURN) {
7912-
if (opline->op1_type == IS_CV &&
7913-
opline->op1.var == (uint32_t)(uintptr_t)(ZEND_CALL_VAR_NUM(NULL, i))) {
7912+
if (opline->op1_type == IS_CV && opline->op1.var == EX_NUM_TO_VAR(i)) {
79147913
info |= MAY_BE_RCN;
79157914
break;
79167915
}
@@ -7921,7 +7920,7 @@ static int zend_jit_free_compiled_variables(dasm_State **Dst, const zend_op *opl
79217920
#endif
79227921

79237922
if (info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
7924-
uint32_t offset = (uint32_t)(uintptr_t)ZEND_CALL_VAR_NUM(NULL, i);
7923+
uint32_t offset = EX_NUM_TO_VAR(i);
79257924
| ZVAL_PTR_DTOR ZEND_ADDR_MEM_ZVAL(ZREG_FP, offset), info, 1, 1, 0, opline
79267925
}
79277926
}

0 commit comments

Comments
 (0)