139 points · by ingve · 7y ago · 64 comments
> A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written in another.
"Future Work - Right now, this only supports an EXTREMELY limited subset of PHP. There is no support for dynamic anything. Arrays aren't supported. Neither Object properties nor methods are supported. And the only builtin functions that are supported are var_dump and strlen."
https://github.com/ircmaxell/php-compiler/blob/master/README...
He basically has avoided that road thus far :)
I think you're right though. Things like eval() and ${$deref-me-at-runtime} aren't going to be easily compiled.
Detailed discussion here: https://paulbiggar.com/research/#sac-2009
For example in PHP:
$a = $_GET['greeting'] ?? 'hello';
eval('echo $a;'); // => hello, or an error or anything else if there is a query parameter called greeting.
$c = [ $a => eval('return "hi";') ];
extract($c);
echo $hello; // => hi, or an error or anything else if there is a query parameter called greeting.
While in CL: (let ((a "hello"))
(eval 'a))
; => debugger invoked, the variable A is unbound.
A CL compiler can make a lot of assumptions about the local environment, and only has to account for real dynamicity in the global environment. For PHP it's basically the other way around. Since most optimizations seem to be relatively localized, that might be a real issue. But then, of course, a good compiler could fall back to unreasonable assembly wherever such dynamic features are used. CL-USER 29 > (let ((a "hello"))
(declare (special a))
(eval 'a))
"hello"$i = "j": $$i = 3;
WTF?
int a = 1; int *b = &a; *b = 2;
If this can be compiled to efficient assembly, I don't think that kind of variable binding presents much more of a challenge.This is a compiler. The difference between distributing the output of this and a compiled C or C++ library is basically that this emits machine code that no sane C or C++ compiler would emit today. This does not prevent me from figuring out how the parts you want kept secret work, it only spreads bugs out over more code.
My objection is twofold: first, on practical grounds, this makes producing good bug reports harder. That's because there's more code to prove isn't the true source of the problematic behavior.
The second is aesthetic. If your goal is to keep your secrets, this does not accomplish it.
Apart from that, it seems to be following a similar path, but is a little bit earlier down it. Could probably look at the code we generated and copy some of the solutions.
PHC was also designed to support eval/dynamic features by embedding the PHP interpreter and optimizing around it. Not sure that this implementation is going down that path, which has upsides and downsides, the upside being that eval can be handled. Would be interested to see what he ends up doing.
When I looked into it, it wasn't so much eval as dynamic includes, often used for localization:
$lang = "en";
include("l18n/$lang.php");