php-src icon indicating copy to clipboard operation
php-src copied to clipboard

First Class Callable Syntax issue with OpCache

Open L1Q opened this issue 4 years ago • 10 comments

From manual page: https://php.net/manual/en/functions.first_class_callable_syntax.php


Is it possible to have this feature disabled? The docs never mention it, but I encountered a server running PHP 8.1.2 (according to echo phpinfo())that does not recognize functionname(...) syntax and throws "ArgumentCountError".

Is there a setting I can check in phpinfo() to know for sure this is intended? E.g. the website hosting provider disabled the feature or is using a build of PHP without it?

Most importantly for the docs, any info about how to figure this out before doing a major refactor and finding out the thing would not run in production would be appreciated.

L1Q avatar Feb 22 '22 21:02 L1Q

It cannot be disabled.

If you're having problems with some code then please post the code.

damianwadley avatar Feb 22 '22 21:02 damianwadley

Thanks for quick response, here is my MRE:

<?php

namespace Test;

function greeter(string $name) {
  echo "Hello, ${name}!";
}

$mycallable = greeter(...);

$mycallable(PHP_VERSION);

As expected this returns Hello, 8.1.2! on my local machine. And here is what I get on the server: image

This is the exact same error I get when running following code on my local machine:

<?php

namespace Test;

function greeter(string $name) {
  echo "Hello, ${name}!";
}

$mycallable = greeter();

$mycallable(PHP_VERSION);

L1Q avatar Feb 22 '22 22:02 L1Q

You do not have the ... in the definition of $mycallable in greeter() in your second example.

Girgias avatar Feb 22 '22 22:02 Girgias

@Girgias exactly! That's what makes me think the server version is not working properly. not providing ... means calling the function immediately. Interestingly enough, when providing full namespace when creating the closure, it runs on the server. This code works fine on both machines:

<?php

namespace Test;

function greeter(string $name) {
  echo "Hello, ${name}!";
}

$mycallable = \Test\greeter(...);

$mycallable(PHP_VERSION);


My actual project code still fails on the server even with the namespace provided, looking into it more, perhaps I can make another reproducible.

L1Q avatar Feb 22 '22 22:02 L1Q

Here, this fails again with ArgumentCountError:

<?php

namespace Test;

$mycallable = \Test\greeter(...);

function greeter(string $name) {
  echo "Hello, ${name}!";
}

$mycallable(PHP_VERSION);

L1Q avatar Feb 22 '22 22:02 L1Q

This runs just fine on https://3v4l.org/vS7IB are you running with JIT or Opcache?

Girgias avatar Feb 22 '22 22:02 Girgias

are you running with JIT or Opcache?

YES! Server is running with opcache.enable

L1Q avatar Feb 22 '22 22:02 L1Q

For now, I know that on the server, the functions have to be defined earlier in the file before they are converted into closures, and with full namespace. Both conditions are important.

L1Q avatar Feb 22 '22 22:02 L1Q

It is indeed OpCache the issue I managed to reproduce the issue with the first snippet you provided.

The fact the call is made inside a namespace is important, as outside of it the Optimizer works fine.

PHPT file:

--TEST--
GitHub bug 8140: First Class Callable Syntax issue with OpCache
--EXTENSIONS--
opcache
--INI--
opcache.enable_cli=1
--FILE--
<?php

namespace Test;

function greeter(string $name) {
  echo "Hello, ${name}!";
}

$mycallable = greeter(...);

$mycallable('I should work');

?>
--EXPECT--
Hello, I should work!

Girgias avatar Feb 22 '22 23:02 Girgias

Using opcache.opt_debug_level=0x20000 it seems in the namespace case a DO_UCALL is generated instead of a CALLABLE_CONVERT which seems to break down the optimizer.

In global namespace:

$_main:
     ; (lines=7, args=0, vars=1, tmps=1)
     ; (after optimizer)
     ; /home/girgias/Dev/php-src/ext/opcache/tests/gh8140.php:1-14
0000 INIT_FCALL 0 144 string("greeter")
0001 T1 = CALLABLE_CONVERT
0002 ASSIGN CV0($mycallable) T1
0003 INIT_DYNAMIC_CALL 1 CV0($mycallable)
0004 SEND_VAL_EX string("I should work") 1
0005 DO_FCALL
0006 RETURN int(1)

greeter:
     ; (lines=6, args=1, vars=1, tmps=3)
     ; (after optimizer)
     ; /home/girgias/Dev/php-src/ext/opcache/tests/gh8140.php:5-7
0000 CV0($name) = RECV 1
0001 T2 = ROPE_INIT 3 string("Hello, ")
0002 T2 = ROPE_ADD 1 T2 CV0($name)
0003 T1 = ROPE_END 2 T2 string("!")
0004 ECHO T1
0005 RETURN null
LIVE RANGES:
     2: 0001 - 0003 (rope)

In namespace:

$_main:
     ; (lines=7, args=0, vars=1, tmps=1)
     ; (after optimizer)
     ; /home/girgias/Dev/php-src/ext/opcache/tests/gh8140.php:1-14
0000 INIT_FCALL 0 144 string("test\greeter")
0001 T1 = DO_UCALL
0002 ASSIGN CV0($mycallable) T1
0003 INIT_DYNAMIC_CALL 1 CV0($mycallable)
0004 SEND_VAL_EX string("I should work") 1
0005 DO_FCALL
0006 RETURN int(1)

Test\greeter:
     ; (lines=6, args=1, vars=1, tmps=3)
     ; (after optimizer)
     ; /home/girgias/Dev/php-src/ext/opcache/tests/gh8140.php:5-7
0000 CV0($name) = RECV 1
0001 T2 = ROPE_INIT 3 string("Hello, ")
0002 T2 = ROPE_ADD 1 T2 CV0($name)
0003 T1 = ROPE_END 2 T2 string("!")
0004 ECHO T1
0005 RETURN null
LIVE RANGES:
     2: 0001 - 0003 (rope)

Girgias avatar Feb 22 '22 23:02 Girgias