com php-src: Fix SSA integrity violation for type inference in dead code: Zend/tests/dead_array_type_inference.phpt ext/opcache
/Optimizer/zend_inference.c
| From: | Nikita Popov | Date: | Fri, 30 Oct 2020 14:52:10 +0000 |
| Subject: | com php-src: Fix SSA integrity violation for type inference in dead code: Zend/tests/dead_array_type_inference.phpt ext/opcache /Optimizer/zend_inference.c |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-113997@lists.php.net to get a copy of this message | ||
Commit: 83738281eb9f2577eda1c76a6acfe644d6fcc6c1
Author: Nikita Popov <nikita.ppv@gmail.com> Fri, 30 Oct 2020 15:48:03 +0100
Parents: 0427dcb91300f993b80c3d77f348d5e1ffcb0d16
Branches: PHP-7.4 PHP-8.0 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=83738281eb9f2577eda1c76a6acfe644d6fcc6c1
Log:
Fix SSA integrity violation for type inference in dead code
The foreach body can never be executed and thus may contain empty
types. We should still uphold our SSA integrity invariants in that
case.
Changed paths:
A Zend/tests/dead_array_type_inference.phpt
M ext/opcache/Optimizer/zend_inference.c
Diff:
diff --git a/Zend/tests/dead_array_type_inference.phpt b/Zend/tests/dead_array_type_inference.phpt
new file mode 100644
index 00000000000..51f9b05816e
--- /dev/null
+++ b/Zend/tests/dead_array_type_inference.phpt
@@ -0,0 +1,18 @@
+--TEST--
+Make sure type inference upholds invariants for dead arrays
+--FILE--
+<?php
+
+function test() {
+ foreach ($a as $v) {
+ $b[] = $v;
+ }
+}
+
+test();
+
+?>
+--EXPECTF--
+Notice: Undefined variable: a in %s on line %d
+
+Warning: Invalid argument supplied for foreach() in %s on line %d
diff --git a/ext/opcache/Optimizer/zend_inference.c b/ext/opcache/Optimizer/zend_inference.c
index fa494794cb2..368f68108dc 100644
--- a/ext/opcache/Optimizer/zend_inference.c
+++ b/ext/opcache/Optimizer/zend_inference.c
@@ -2106,24 +2106,28 @@ static uint32_t assign_dim_result_type(
tmp |= MAY_BE_RC1 | MAY_BE_RCN;
}
if (tmp & MAY_BE_ARRAY) {
- if (value_type & MAY_BE_UNDEF) {
- tmp |= MAY_BE_ARRAY_OF_NULL;
- }
- if (dim_op_type == IS_UNUSED) {
- tmp |= MAY_BE_ARRAY_KEY_LONG;
- } else {
- if (dim_type & (MAY_BE_LONG|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_RESOURCE|MAY_BE_DOUBLE)) {
- tmp |= MAY_BE_ARRAY_KEY_LONG;
+ /* Only add key type if we have a value type. We want to maintain the invariant that a
+ * key type exists iff a value type exists even in dead code that may use empty types. */
+ if (value_type & (MAY_BE_ANY|MAY_BE_UNDEF)) {
+ if (value_type & MAY_BE_UNDEF) {
+ tmp |= MAY_BE_ARRAY_OF_NULL;
}
- if (dim_type & MAY_BE_STRING) {
- tmp |= MAY_BE_ARRAY_KEY_STRING;
- if (dim_op_type != IS_CONST) {
- // FIXME: numeric string
+ if (dim_op_type == IS_UNUSED) {
+ tmp |= MAY_BE_ARRAY_KEY_LONG;
+ } else {
+ if (dim_type & (MAY_BE_LONG|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_RESOURCE|MAY_BE_DOUBLE)) {
tmp |= MAY_BE_ARRAY_KEY_LONG;
}
- }
- if (dim_type & (MAY_BE_UNDEF|MAY_BE_NULL)) {
- tmp |= MAY_BE_ARRAY_KEY_STRING;
+ if (dim_type & MAY_BE_STRING) {
+ tmp |= MAY_BE_ARRAY_KEY_STRING;
+ if (dim_op_type != IS_CONST) {
+ // FIXME: numeric string
+ tmp |= MAY_BE_ARRAY_KEY_LONG;
+ }
+ }
+ if (dim_type & (MAY_BE_UNDEF|MAY_BE_NULL)) {
+ tmp |= MAY_BE_ARRAY_KEY_STRING;
+ }
}
}
/* Only add value type if we have a key type.. It might be that the key type is illegal