FFI in PHAR files

php.internals

Vinicius Dias

2 years ago
I was playing around with some libraries using FFI and I wanted to share a .phar with the result, but to my surprise, it didn't work. Apparently we are not able to load shared libraries using FFI from within .phar files. Is that the expected behavior or is it a bug in the FFI extension? I have setup a dummy repo so the error could be easily reproduced: https://github.com/CViniciusSDias/ffi-phar-problem I am sorry if this is not the list to send this type of problem. I will gladly move the thread to the right one if someone points it out. Thanks, folks. Vinicius Dias, Zend Certified Engineer, iMasters PHP Certified Professional

Bishop Bettini

2 years ago
On Fri, Sep 8, 2023 at 2:33 PM Vinicius Dias <carlosv775@gmail.com> wrote:
> I was playing around with some libraries using FFI and I wanted to > share a .phar with the result, but to my surprise, it didn't work. > > Apparently we are not able to load shared libraries using FFI from > within .phar files. > Is that the expected behavior or is it a bug in the FFI extension? > > I have setup a dummy repo so the error could be easily reproduced: > https://github.com/CViniciusSDias/ffi-phar-problem > > I am sorry if this is not the list to send this type of problem. I > will gladly move the thread to the right one if someone points it out. > https://www.php.net/unsub.php
Erstwhile PHAR extension maintainer here. What an interesting question! The short answer is no, FFI does not support loading dynamic libraries contained within a PHAR archive. The longer answer is that FFI was not written to be able to support VFS locations, including the phar:// scheme. In this case, PHP starts and invokes the code. The code invokes FFI[1] with a phar:// path, which then tries to perform a dlopen()[2], which fails because dlopen has no idea how to resolve PHAR paths: dlopen() expects a path that the OS can resolve[3]. I suppose it'd be possible to improve FFI to call the PHP VFS layer to resolve a path, which would handle the phar:// scheme and other schemes. But, I would be worried about potential other downstream impacts - esp. security implications - as this is a novel (to me at least) scenario. bishop [1]: https://github.com/php/php-src/blob/50ca24251d97dbf78b0c1165dac7c1a19ff1c87a/ext/ffi/ffi.c#L2968C3-L2968C3 [2]: https://github.com/php/php-src/blob/50ca24251d97dbf78b0c1165dac7c1a19ff1c87a/Zend/zend_portability.h#L156 [3]:https://man7.org/linux/man-pages/man3/dlopen.3.html

Vinicius Dias

2 years ago
Ah, that makes total sense. I was worried I was doing something very wrong. haha Thank you very much for the detailed clarification. This doesn't seem to be a "critical" feature, but I wonder if the documentation shouldn't mention something about only "regular files" being supported. Vinicius Dias, Zend Certified Engineer, iMasters PHP Certified Professional Vinicius Dias, Zend Certified Engineer, iMasters PHP Certified Professional Em sex., 8 de set. de 2023 às 16:55, Bishop Bettini <bishop@php.net> escreveu:

Vinicius Dias

2 years ago
> > I suppose it'd be possible to improve FFI to call the PHP VFS layer to resolve a path, which would handle the phar:// scheme and other schemes. But, I would be worried about potential other downstream impacts - esp. security implications - as this is a novel (to me at least) scenario.
I just realized I never explained the reason for me to wanna use this feature. My bad. I have a CLI project that uses FFI and it would be awesome if I could share it using the micro sfx API[1]. If FFI was supported inside PHARs, we could even create Desktop applications using tools such as php-tkui[2] and make them available via the aforementioned SAPI. Anyway, I just wanted to explain the motive behind my original question. :-D [1]: https://github.com/crazywhalecc/static-php-cli/blob/main/README.md#use-micro [2]: https://github.com/skoro/php-tkui

Robert Landers

2 years ago
On Tue, Dec 19, 2023 at 3:35 AM Vinicius Dias <carlosv775@gmail.com> wrote:
> > > > I suppose it'd be possible to improve FFI to call the PHP VFS layer to resolve a path, which would handle the phar:// scheme and other schemes. But, I would be worried about potential other downstream impacts - esp. security implications - as this is a novel (to me at least) scenario. > > I just realized I never explained the reason for me to wanna use this > feature. My bad. > > I have a CLI project that uses FFI and it would be awesome if I could > share it using the micro sfx API[1]. > > If FFI was supported inside PHARs, we could even create Desktop > applications using tools such as php-tkui[2] and make them available > via the aforementioned SAPI. > > Anyway, I just wanted to explain the motive behind my original question. :-D > > [1]: https://github.com/crazywhalecc/static-php-cli/blob/main/README.md#use-micro > [2]: https://github.com/skoro/php-tkui > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >
Hello,
> I have a CLI project that uses FFI and it would be awesome if I could > share it using the micro sfx API[1].
If you choose not to use a phar, but instead, just loose PHP files, it extracts the sources to a random `/tmp` directory when executing. So, FFI and other things should "just work" without any shenanigans. Robert Landers Software Engineer Utrecht NL

Vinicius Dias

2 years ago
> If you choose not to use a phar, but instead, just loose PHP files, it > extracts the sources to a random `/tmp` directory when executing. So, > FFI and other things should "just work" without any shenanigans. > > Robert Landers > Software Engineer > Utrecht NL
Thank you for the suggestion. The micro.sfx SAPI doesn't support adding multiple files, afaik, so what you mean is concating micro.sfx to my "entrypoint" and providing the whole source instead of just an executable?