hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

HHVM executes code in an unreachable closure (in Laravel)

Open dave1010 opened this issue 12 years ago • 11 comments

Somehow HHVM is executing code in a closure when it shouldn't be. PHP doesn't execute the code.

I haven't been able to reduce the test case to not include Laravel yet, so there's a chance Laravel is doing some very strange things with reflection.

Test case: https://gist.github.com/dave1010/9975662

This only seems to happen when:

  • there's a nested Route::group in Laravel
  • a file is required, which has a closure with 2 closures inside

$ hhvm --version HipHop VM 3.0.1 (rel) Compiler: tags/HHVM-3.0.1-0-g97c0ac06000e060376fdac4a7970e954e77900d6 Repo schema: a1146d49c5ba0d6db903beb3a4ed8a3766fef182

dave1010 avatar Apr 04 '14 14:04 dave1010

Same result on nightly:

$ hhvm --version HipHop VM 3.1.0-dev+2014.04.04 (rel) Compiler: heads/master-0-ge76e160f99ec5bfb895c1ba0bab355a8bf486c53 Repo schema: 5dd782c3d5318cf9f301cf7f7efae86bda0d7f99

dave1010 avatar Apr 04 '14 15:04 dave1010

Confirmed same for 3.0.1-rel

nowak-ninja avatar Apr 04 '14 15:04 nowak-ninja

I'm guessing it's related to the use of ReflectionClass in Laravel's ControllerInspector::getRoutable()

dave1010 avatar Apr 04 '14 15:04 dave1010

Thanks for opening this issue. Feel free (you or anyone else reading this) to get this working; we'd appreciate it.

alexmalyshev avatar Apr 04 '14 20:04 alexmalyshev

cc @LiraNuna:did you fix this in D1258207?

fredemmott avatar Apr 04 '14 21:04 fredemmott

If this is the same issue as #1353, then yes. This was fixed already. If you can confirm the issue came from ReflectionMethod::getClosure we can merge this with #1353.

LiraNuna avatar Apr 04 '14 21:04 LiraNuna

@dave1010

I've recently came across the same problem in which the wrong closure is being passed to Route::group()

Looking at the stack trace I can't see getRoutable() being called.

LavaToaster avatar Apr 06 '14 02:04 LavaToaster

I'm having the same issue where call_user_func($callable, $parameter) calls an unrelated function. The same code works in PHP. This is not Laravel.

If I add before the Closure that gets called for mistake, another Closure, like so: screenshot 2014-04-28 02 48 09

the tiny Closure will run, even though it couldn't be referenced whatsoever. It will also get as parameters the parameters of the Closure that should have been called.

I tried for a couple hours to reproduce the issue, but to no avail. Notice that I am nesting a few levels, like 4th Closure within a Closure.

Nightly:
hhvm --version
HipHop VM 3.1.0-dev+2014.04.27 (rel)
Compiler: heads/master-0-g991e92dc79342cf6d28738e599f0b769524b29e8
Repo schema: 5f8ba775524b64db3ec5f27a5c4019a3c7f68362

Edit: this seems to be related to #1889

woxxy avatar Apr 28 '14 00:04 woxxy

Thanks to #1889 there's a way to reproduce. Remixing @drobertduke's work by removing the variable to assign the Closure to:

<?php
require 'closure1.php';
require 'closure2.php';
$foo();
$bar();
<?php
$foo = function () {
    var_dump('foo outer');
    function() { var_dump('foo inner'); }; // not giving it to a variable!
};
<?php
$bar = function() {
    var_dump('bar outer');
    $f = function() { var_dump('bar inner'); };
    $f();
};

Result:

$ hhvm load_closures.php
string(9) "foo outer"
string(9) "bar outer"
string(9) "foo inner" 

foo inner should be unreachable.

Also, if you var_dump($f) on both files, you get:

object(Closure$)#3 (0) {
}
object(Closure$)#3 (0) {
}

Which have the same empty "signature" after the $. I don't know if it matters, but I've seen that different Closures have different signature.

woxxy avatar Apr 28 '14 01:04 woxxy

As I checked the parser.c code https://github.com/facebook/hhvm/blob/10b2a6a3a8075b676ea167ea49e6bba5b8cca68e/hphp/parser/parser.cpp#L35 I noticed I could use a class to edit the signature of the Closure. In fact, wrapping the closure in something like:

<?php
class Foobar {
    public static function run() { function() { test(); }; }
}

Foobar::run()

will keep it working.

Sadly I don't know C++ well enough to fix this myself. I suppose adding the filename to the Closure signature would work, or the funcName should be something like Closure$#23428943.

woxxy avatar Apr 28 '14 02:04 woxxy

This has been fixed in the nightly and will go out with the release of 3.1 on thursday.

ptarjan avatar May 19 '14 07:05 ptarjan