Skip to content

Commit 73b3ef6

Browse files
committed
JIT: Fix FETCH_DIM_IS ignoring leading-numeric string offsets
For $str["1x"] ?? $default, the VM warns "Illegal string offset" and uses the leading number, while the JIT helper returned null silently. zend_may_throw_ex() did not count this warning either, so DCE removed the inner fetch of an unused nested isset()/empty() and the JIT skipped saving the call chain around it.
1 parent a93d24c commit 73b3ef6

6 files changed

Lines changed: 167 additions & 7 deletions

File tree

‎Zend/Optimizer/zend_inference.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5299,7 +5299,9 @@ ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op
52995299
case ZEND_ISSET_ISEMPTY_DIM_OBJ:
53005300
return (t1 & MAY_BE_OBJECT) || (t2 & MAY_BE_THROWING_KEY);
53015301
case ZEND_FETCH_DIM_IS:
5302-
return (t1 & MAY_BE_OBJECT) || (t2 & MAY_BE_THROWING_KEY);
5302+
return (t1 & MAY_BE_OBJECT) || (t2 & MAY_BE_THROWING_KEY)
5303+
/* "Illegal string offset" warning for leading-numeric string offsets */
5304+
|| ((t1 & MAY_BE_STRING) && (t2 & MAY_BE_STRING));
53035305
case ZEND_CAST:
53045306
switch (opline->extended_value) {
53055307
case IS_LONG:

‎ext/opcache/jit/zend_jit_helpers.c‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,17 +1112,34 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_str_is_helper(zend_string *str, zva
11121112
switch (Z_TYPE_P(dim)) {
11131113
/* case IS_LONG: */
11141114
case IS_STRING:
1115-
if (IS_LONG == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), NULL, NULL, false)) {
1116-
break;
1115+
{
1116+
bool trailing_data = false;
1117+
/* Allow errors to warn on leading-numeric strings, like the VM */
1118+
if (IS_LONG != is_numeric_string_ex(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset,
1119+
NULL, /* allow errors */ true, NULL, &trailing_data)) {
1120+
ZVAL_NULL(result);
1121+
return;
11171122
}
1118-
ZVAL_NULL(result);
1119-
return;
1123+
if (UNEXPECTED(trailing_data)) {
1124+
if (!(GC_FLAGS(str) & IS_STR_INTERNED)) {
1125+
GC_ADDREF(str);
1126+
}
1127+
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
1128+
if (!(GC_FLAGS(str) & IS_STR_INTERNED) && UNEXPECTED(GC_DELREF(str) == 0)) {
1129+
zend_string_efree(str);
1130+
ZVAL_NULL(result);
1131+
return;
1132+
}
1133+
}
1134+
break;
1135+
}
11201136
case IS_UNDEF:
11211137
zend_jit_undefined_op_helper(EG(current_execute_data)->opline->op2.var);
11221138
case IS_DOUBLE:
11231139
case IS_NULL:
11241140
case IS_FALSE:
11251141
case IS_TRUE:
1142+
offset = zval_get_long_func(dim, /* is_strict */ false);
11261143
break;
11271144
case IS_REFERENCE:
11281145
dim = Z_REFVAL_P(dim);
@@ -1134,8 +1151,6 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_str_is_helper(zend_string *str, zva
11341151
ZVAL_NULL(result);
11351152
return;
11361153
}
1137-
1138-
offset = zval_get_long_func(dim, /* is_strict */ false);
11391154
} else {
11401155
offset = Z_LVAL_P(dim);
11411156
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
JIT FETCH_DIM_IS: string offsets given as strings behave like the VM
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.jit=function
7+
opcache.file_update_protection=0
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
function test(string $s, string $k) {
13+
return $s[$k] ?? "default";
14+
}
15+
16+
foreach (["1", "-1", "1 ", " 1", "1x", "-1x", "9x", "x", "", "1.5", "1.5x", "99999999999999999999"] as $k) {
17+
echo var_export($k, true), ": ";
18+
var_dump(test("abc", $k));
19+
}
20+
?>
21+
--EXPECTF--
22+
'1': string(1) "b"
23+
'-1': string(1) "c"
24+
'1 ': string(1) "b"
25+
' 1': string(1) "b"
26+
'1x':
27+
Warning: Illegal string offset "1x" in %s on line %d
28+
string(1) "b"
29+
'-1x':
30+
Warning: Illegal string offset "-1x" in %s on line %d
31+
string(1) "c"
32+
'9x':
33+
Warning: Illegal string offset "9x" in %s on line %d
34+
string(7) "default"
35+
'x': string(7) "default"
36+
'': string(7) "default"
37+
'1.5': string(7) "default"
38+
'1.5x': string(7) "default"
39+
'99999999999999999999': string(7) "default"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
JIT FETCH_DIM_IS: leading-numeric string offset conversion may throw
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.jit=function
7+
opcache.file_update_protection=0
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
set_error_handler(function ($errno, $errstr) {
13+
throw new Exception($errstr);
14+
});
15+
16+
function make(int $n) {
17+
return str_repeat("abc", $n);
18+
}
19+
20+
function consume($v) {
21+
echo "unreachable\n";
22+
}
23+
24+
function test(string $s) {
25+
consume($s["1x"] ?? "default");
26+
}
27+
28+
try {
29+
test(make(2));
30+
} catch (Exception $e) {
31+
echo $e::class, ": ", $e->getMessage(), "\n";
32+
}
33+
?>
34+
--EXPECT--
35+
Exception: Illegal string offset "1x"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
JIT FETCH_DIM_IS: leading-numeric string offset may throw between call arguments
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.jit=function
7+
opcache.file_update_protection=0
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
set_error_handler(function ($errno, $errstr) {
13+
throw new Exception($errstr);
14+
});
15+
16+
function make(int $n) {
17+
return str_repeat("abc", $n);
18+
}
19+
20+
function consume($a, $b) {
21+
echo "unreachable\n";
22+
}
23+
24+
function test(string $s, string $t) {
25+
consume($t, isset($s["1x"][0]));
26+
}
27+
28+
try {
29+
test(make(2), make(3));
30+
} catch (Exception $e) {
31+
echo $e::class, ": ", $e->getMessage(), "\n";
32+
}
33+
?>
34+
--EXPECT--
35+
Exception: Illegal string offset "1x"

‎ext/opcache/tests/opt/dce_023.phpt‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
DCE must not remove FETCH_DIM_IS of a nested isset()/empty() with a leading-numeric string offset
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.optimization_level=-1
7+
opcache.file_update_protection=0
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
set_error_handler(function ($errno, $errstr) {
13+
throw new Exception($errstr);
14+
});
15+
16+
function test_isset(string $s) {
17+
$x = isset($s["1x"][0]);
18+
}
19+
20+
function test_empty(string $s) {
21+
$x = empty($s["1x"][0]);
22+
}
23+
24+
foreach (['test_isset', 'test_empty'] as $f) {
25+
try {
26+
$f("abc");
27+
} catch (Exception $e) {
28+
echo $e::class, ": ", $e->getMessage(), "\n";
29+
}
30+
}
31+
?>
32+
--EXPECT--
33+
Exception: Illegal string offset "1x"
34+
Exception: Illegal string offset "1x"

0 commit comments

Comments
 (0)