From: Nikita Popov <nikic@php.net>
Date: Thu, 10 Nov 2016 20:36:46 +0000 (+0100)
Subject: Fcall optimization: Avoid FETCH_DIM_R with UNUSED op2
X-Git-Tag: php-7.0.14RC1~63
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bb3d0c0e17a614f2bcb2a257f8146affced86341;p=php

Fcall optimization: Avoid FETCH_DIM_R with UNUSED op2
---

diff --git a/ext/opcache/Optimizer/optimize_func_calls.c b/ext/opcache/Optimizer/optimize_func_calls.c
index 5804a5fb0e..ccac5b9fbc 100644
--- a/ext/opcache/Optimizer/optimize_func_calls.c
+++ b/ext/opcache/Optimizer/optimize_func_calls.c
@@ -112,6 +112,13 @@ void optimize_func_calls(zend_op_array *op_array, zend_optimizer_ctx *ctx)
 						opline->extended_value &= ZEND_FETCH_TYPE_MASK;
 						opline->opcode -= 9;
 					} else {
+						if (opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
+								&& opline->op2_type == IS_UNUSED) {
+							/* FETCH_DIM_FUNC_ARG supports UNUSED op2, while FETCH_DIM_R does not.
+							 * Performing the replacement would create an invalid opcode. */
+							break;
+						}
+
 						opline->extended_value &= ZEND_FETCH_TYPE_MASK;
 						opline->opcode -= 12;
 					}
diff --git a/ext/opcache/tests/optimize_func_calls_001.phpt b/ext/opcache/tests/optimize_func_calls_001.phpt
new file mode 100644
index 0000000000..5745b3fad0
--- /dev/null
+++ b/ext/opcache/tests/optimize_func_calls_001.phpt
@@ -0,0 +1,15 @@
+--TEST--
+Don't create FETCH_DIM_R with UNUSED op2
+--FILE--
+<?php
+
+// Order matters
+test($arr[]);
+function test($arg) {}
+
+?>
+--EXPECTF--
+Fatal error: Uncaught Error: Cannot use [] for reading in %s:%d
+Stack trace:
+#0 {main}
+  thrown in %s on line %d