HHVM executes code in an unreachable closure (in Laravel)
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::groupin 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
Same result on nightly:
$ hhvm --version HipHop VM 3.1.0-dev+2014.04.04 (rel) Compiler: heads/master-0-ge76e160f99ec5bfb895c1ba0bab355a8bf486c53 Repo schema: 5dd782c3d5318cf9f301cf7f7efae86bda0d7f99
Confirmed same for 3.0.1-rel
I'm guessing it's related to the use of ReflectionClass in Laravel's ControllerInspector::getRoutable()
Thanks for opening this issue. Feel free (you or anyone else reading this) to get this working; we'd appreciate it.
cc @LiraNuna:did you fix this in D1258207?
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.
@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.
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:

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
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.
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.
This has been fixed in the nightly and will go out with the release of 3.1 on thursday.