8 ms·
A PHP Compiler, a.k.a. the FFI Rabbit Hole
- thrusong 8y agoLooks really cool, but it's a little above my skill level. Does this mean you could protect your PHP source code if you wanted to compile and then distribute it?
- captn3m0 8y agoYes, the compile script outputs code via LLVM as a binary. So yeah, you could do it.
- debatem1 8y agoPlease don't do this. It's both ineffective and pisses off those of us who try to produce good bug reports when your obfuscated software misbehaves.
- thrusong 8y agoI understand why this is generally not recommended but there are situations where someone like you wouldn't have to submit a bug report, so chill man.
- debatem1 8y agoI do security for a living. Generally speaking, code that A) is obfuscated, B) is written in PHP, and C) I'm told won't have bugs... is exactly the code I'm probably going to need to file bugs against.
- munk-a 8y agoThis is just a general issue with third party code - C/C++ libraries have always suffered from this weakness. Still, I've worked in places where if we were unable to distribute our library closed source the business wing wouldn't let us distribute it at all - everyone is welcome to their thoughts on copy-left software, but a thing that exists wouldn't have existed if we could only release it open-source (and yes, we even explained how licensing worked to the higher ups)
- debatem1 8y agoI'm confused. 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.
- Piskvorrr 8y agoNo, such behavior does not make technical sense, any more than bits can be coloured. Yet... https://ansuz.sooke.bc.ca/entry/23 https://ansuz.sooke.bc.ca/entry/23
- maxxxxx 8y agoWhen you think about how dynamic PHP is wouldn't you pretty much end up with the source code and a PHP Interpreter baked together into a binary?
- fiddlerwoaroof 8y agoCommon Lisp is more dynamic than PHP yet is still compiled to reasonable assembly.
- jorams 8y agoCommon Lisp is much more dynamic in some ways, but much less dynamic in others. In PHP you can't redeclare functions or classes, while in Common Lisp you can. On the other hand, in PHP you can manipulate the current scope and make variables appear out of thin air, but you can't do stuff like that in Common Lisp. 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.
- deleted 8y ago[deleted]
- fiddlerwoaroof 8y agoYou can't manipulate the lexical environment using the standardized language, but sbcl and other lisps provide libraries for programatic manipulation of the lexical environment.
- ufo 8y agoDoes anyone know how this compares to PHC? It was an ahead-of-time compiler for PHP. https://github.com/pbiggar/phc https://github.com/pbiggar/phc
- pbiggar 8y agophc is dead, so there's that :) 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.
- Twirrim 8y agoHow heavily does eval get used in PHP?
- pbiggar 8y agoMore than you'd think. I did some analysis (in 2008 mind you, long time ago, PHP4/PHP5). I think I found that half the packages on sourceforge (sourceforge!) had a use of a dynamic construct. When I looked into it, it wasn't so much eval as dynamic includes, often used for localization: $lang = "en"; include("l18n/$lang.php");
- beberlei 8y agoIt is not used alot in modern code, the only use case that I think is reasonably ok nowadays from and used that way is template engines or config caches in development mode, that usually compile template code to generated PHP files, but eval() immediately during development for simplicity.
- sedeki 8y agoI appreciate the clear explainations of AOT, VM, etc. Someone tried to explain AOT to me a while back, but I didn’t understand anything.
- coderintherye 8y agoThis is amazing work and great job documenting! I had no idea about the Foreign Function Interface coming to PHP 7.4, that's exciting.
- dingdingdang 8y agoVery impressive work, will comment again if I manage a test compile of some minor project! :)
- jtd514 8y agohttps://en.wikipedia.org/wiki/HipHop_for_PHP https://en.wikipedia.org/wiki/HipHop_for_PHP
- simcop2387 8y agoHipHop turned into HHVM, which as of Feb no longer supports PHP going forward. https://hhvm.com/blog/2019/02/11/hhvm-4.0.0.html https://hhvm.com/blog/2019/02/11/hhvm-4.0.0.html
- fernly 8y agoIronically about the only acronym he does NOT try to explain is FFI, the reason for the project. > 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. https://en.wikipedia.org/wiki/Foreign_function_interface https://en.wikipedia.org/wiki/Foreign_function_interface
- simplyinfinity 8y agoAnother cool (and more functional) compiler is Peachpie. You can compile WordPress and run it on. Net vm. From the reports it's even few times faster than "native" php :) https://www.peachpie.io https://www.peachpie.io
- xfer 8y agoWonder why meta-compilation is not more popular? There are already some frameworks available like RPython for quite some time, Truffle is relatively new.