Skip to content

Commit 6d4d6ef

Browse files
committed
Optimizer: array keys whose conversion may throw
Float keys emit a deprecation and resource keys a warning, which an error handler can turn into an exception. Share one key mask in zend_may_throw_ex() so ASSIGN_DIM, INIT_ARRAY, ADD_ARRAY_ELEMENT, FETCH_DIM_W/FETCH_LIST_W, ARRAY_KEY_EXISTS, FETCH_DIM_IS and ISSET_ISEMPTY_DIM_OBJ agree.
1 parent afd1569 commit 6d4d6ef

12 files changed

Lines changed: 322 additions & 7 deletions

‎Zend/Optimizer/zend_inference.c‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4926,6 +4926,9 @@ ZEND_API zend_result zend_ssa_inference(zend_arena **arena, const zend_op_array
49264926
}
49274927
/* }}} */
49284928

4929+
/* Key types whose conversion to an array offset may emit a diagnostic */
4930+
#define MAY_BE_THROWING_KEY (MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)
4931+
49294932
ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_op_array *op_array, const zend_ssa *ssa, uint32_t t1, uint32_t t2)
49304933
{
49314934
if (opline->op1_type == IS_CV) {
@@ -5205,7 +5208,7 @@ ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op
52055208
return 1;
52065209
}
52075210
return (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_TRUE|MAY_BE_FALSE|MAY_BE_STRING|MAY_BE_LONG|MAY_BE_DOUBLE)) || opline->op2_type == IS_UNUSED ||
5208-
(t2 & (MAY_BE_UNDEF|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5211+
(t2 & (MAY_BE_UNDEF|MAY_BE_THROWING_KEY));
52095212
case ZEND_ASSIGN_OBJ:
52105213
if (t1 & (MAY_BE_ANY-MAY_BE_OBJECT)) {
52115214
return 1;
@@ -5257,9 +5260,9 @@ ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op
52575260
case ZEND_ROPE_END:
52585261
return t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT);
52595262
case ZEND_INIT_ARRAY:
5260-
return (opline->op2_type != IS_UNUSED) && (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5263+
return (opline->op2_type != IS_UNUSED) && (t2 & MAY_BE_THROWING_KEY);
52615264
case ZEND_ADD_ARRAY_ELEMENT:
5262-
return (opline->op2_type == IS_UNUSED) || (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5265+
return (opline->op2_type == IS_UNUSED) || (t2 & MAY_BE_THROWING_KEY);
52635266
case ZEND_STRLEN:
52645267
return (t1 & MAY_BE_ANY) != MAY_BE_STRING;
52655268
case ZEND_COUNT:
@@ -5286,9 +5289,9 @@ ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op
52865289
case ZEND_FETCH_IS:
52875290
return (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT));
52885291
case ZEND_ISSET_ISEMPTY_DIM_OBJ:
5289-
return (t1 & MAY_BE_OBJECT) || (t2 & (MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT));
5292+
return (t1 & MAY_BE_OBJECT) || (t2 & MAY_BE_THROWING_KEY);
52905293
case ZEND_FETCH_DIM_IS:
5291-
return (t1 & MAY_BE_OBJECT) || (t2 & (MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5294+
return (t1 & MAY_BE_OBJECT) || (t2 & MAY_BE_THROWING_KEY);
52925295
case ZEND_CAST:
52935296
switch (opline->extended_value) {
52945297
case IS_LONG:
@@ -5309,7 +5312,7 @@ ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op
53095312
if ((t2 & MAY_BE_ANY) != MAY_BE_ARRAY) {
53105313
return 1;
53115314
}
5312-
if ((t1 & (MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
5315+
if (t1 & MAY_BE_THROWING_KEY) {
53135316
return 1;
53145317
}
53155318
return 0;
@@ -5334,7 +5337,7 @@ ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op
53345337
if (t1 & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
53355338
return 1;
53365339
}
5337-
if (t2 & (MAY_BE_RESOURCE|MAY_BE_ARRAY|MAY_BE_OBJECT)) {
5340+
if (t2 & MAY_BE_THROWING_KEY) {
53385341
return 1;
53395342
}
53405343
if (opline->op2_type == IS_UNUSED) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
JIT ADD_ARRAY_ELEMENT: float key 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 test(float $k) {
17+
var_dump([0, $k => 1]);
18+
}
19+
20+
try {
21+
test(1.5);
22+
} catch (Exception $e) {
23+
echo $e::class, ": ", $e->getMessage(), "\n";
24+
}
25+
?>
26+
--EXPECT--
27+
Exception: Implicit conversion from float 1.5 to int loses precision
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
JIT ARRAY_KEY_EXISTS: float key 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 test(float $k) {
17+
var_dump(array_key_exists($k, []));
18+
}
19+
20+
try {
21+
test(1.5);
22+
} catch (Exception $e) {
23+
echo $e::class, ": ", $e->getMessage(), "\n";
24+
}
25+
?>
26+
--EXPECT--
27+
Exception: Implicit conversion from float 1.5 to int loses precision
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
JIT ASSIGN_DIM: float key 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 test(float $k) {
17+
$a = [];
18+
var_dump($a[$k] = 1);
19+
}
20+
21+
try {
22+
test(1.5);
23+
} catch (Exception $e) {
24+
echo $e::class, ": ", $e->getMessage(), "\n";
25+
}
26+
?>
27+
--EXPECT--
28+
Exception: Implicit conversion from float 1.5 to int loses precision
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
JIT FETCH_DIM_W: float key 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+
class C {
17+
function __destruct() {
18+
echo "destruct\n";
19+
}
20+
}
21+
22+
function g($x, &$y) {}
23+
24+
function test(float $k) {
25+
$c = new C;
26+
g($c, $a[$k]);
27+
}
28+
29+
try {
30+
test(1.5);
31+
} catch (Exception $e) {
32+
echo $e::class, ": ", $e->getMessage(), "\n";
33+
}
34+
?>
35+
--EXPECT--
36+
destruct
37+
Exception: Implicit conversion from float 1.5 to int loses precision
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
JIT INIT_ARRAY: float key 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 test(float $k) {
17+
var_dump([$k => 1]);
18+
}
19+
20+
try {
21+
test(1.5);
22+
} catch (Exception $e) {
23+
echo $e::class, ": ", $e->getMessage(), "\n";
24+
}
25+
?>
26+
--EXPECT--
27+
Exception: Implicit conversion from float 1.5 to int loses precision
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
JIT ISSET_ISEMPTY_DIM_OBJ: resource key 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 test() {
17+
$k = fopen("php://memory", "r");
18+
$a = [];
19+
var_dump(isset($a[$k]));
20+
}
21+
22+
try {
23+
test();
24+
} catch (Exception $e) {
25+
echo $e::class, ": ", $e->getMessage(), "\n";
26+
}
27+
?>
28+
--EXPECTF--
29+
Exception: Resource ID#%d used as offset, casting to integer (%d)

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
DCE must not remove ASSIGN_DIM with a float key
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(float $k) {
17+
$a[$k] = 1;
18+
}
19+
20+
try {
21+
test(1.5);
22+
} catch (Exception $e) {
23+
echo $e::class, ": ", $e->getMessage(), "\n";
24+
}
25+
?>
26+
--EXPECT--
27+
Exception: Implicit conversion from float 1.5 to int loses precision

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
DCE must not remove INIT_ARRAY with a float key
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(float $k) {
17+
$a = [$k => 1];
18+
}
19+
20+
try {
21+
test(1.5);
22+
} catch (Exception $e) {
23+
echo $e::class, ": ", $e->getMessage(), "\n";
24+
}
25+
?>
26+
--EXPECT--
27+
Exception: Implicit conversion from float 1.5 to int loses precision

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
DCE must not remove ADD_ARRAY_ELEMENT with a float key
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(float $k) {
17+
$a = [0, $k => 1];
18+
}
19+
20+
try {
21+
test(1.5);
22+
} catch (Exception $e) {
23+
echo $e::class, ": ", $e->getMessage(), "\n";
24+
}
25+
?>
26+
--EXPECT--
27+
Exception: Implicit conversion from float 1.5 to int loses precision

0 commit comments

Comments
 (0)