vjardin / bcgen Goto Github PK
View Code? Open in Web Editor NEWA bytecode generator (compiler) for PHP7
A bytecode generator (compiler) for PHP7
BCgen ===== BCgen is derived from the Zend OPcache source code. It provides a single function to compile a PHP script into binary bytecode file. BCgen is similiar to bcompiler, but not a direct replacement. Compatibility ------------- Only PHP-7.2 is supported for now. There are currently no plans to support earlier PHP versions as well. Supporting different PHP versions with their different internal data structures is really a hassle. So if you distribute your BCGen compiled PHP files to protect your code, make sure that the user uses the correct PHP version that matches the BCgen version. In times of docker et al it should not be a problem to achieve this. Quick Install ------------- - Compile $PHP_DIR/bin/phpize ./configure \ --with-php-config=$PHP_DIR/bin/php-config make - Install make install # this will copy bcgen.so into PHP extension directory - Edit php.ini zend_extension=/...full path.../bcgen.so NOTE: In case you are going to use BCgen together with Xdebug or Zend Debugger, be sure that the debugger is loaded after BCgen. "php -v" must show the debugger after BCgen. - Restart PHP Configuration Directives ------------------------ bcgen.enable (default "1") BCgen On/Off switch. When set to Off, bytecode files cannot be read bcgen.save_comments (default "1") If disabled, all PHPDoc comments are dropped from the code to reduce the size of the optimized code. Disabling "Doc Comments" may break some existing applications and frameworks (e.g. Doctrine, ZF2, PHPUnit) bcgen.optimization_level (default "0xffffffff") A bitmask, where each bit enables or disables the appropriate BCgen passes bcgen.dups_fix (default "0") Enable this hack as a workaround for "Cannot redeclare class" errors. bcgen.file_consistency_checks (default "0") Check the file checksum on each request. The default value of "0" means that the check is disabled. Because calculating the checksum impairs performance, this directive should be enabled only as part of a debugging process. bcgen.error_log BCgen error_log file name. Empty string assumes "stderr". bcgen.log_verbosity_level (default "1") All BCgen errors go to the Web server log. By default, only fatal errors (level 0) or errors (level 1) are logged. You can also enable warnings (level 2), info messages (level 3) or debug messages (level 4).
No changes in 3 years, 7.2 will be unsupported soon: https://www.php.net/supported-versions.php, Will there be a PHP 7.4> version?
Any sign of a release: https://github.com/vjardin/bcgen/releases Linux or Windows(dll)?
Work in progress https://pecl.php.net/package/bcgen
/install/bc # uname -a Linux 4.12.14-lp151.28.48-default #1 SMP Fri Apr 17 05:38:36 UTC 2020 (18849d1) x86_64 x86_64 x86_64 GNU/Linux /install/bc # php -v PHP 7.2.5 (cli) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies ~/install/bc # phpize Configuring for: PHP Api Version: 20170718 Zend Module Api No: 20170718 Zend Extension Api No: 320170718 /install/bc # ./configure configure: loading site script /usr/share/site/x86_64-unknown-linux-gnu checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for a sed that does not truncate output... /bin/sed checking for cc... cc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether cc accepts -g... yes checking for cc option to accept ISO C89... none needed checking how to run the C preprocessor... cc -E checking for icc... no checking for suncc... no checking whether cc understands -c and -o together... yes checking for system library directory... lib checking if compiler supports -R... no checking if compiler supports -Wl,-rpath,... yes checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking target system type... x86_64-unknown-linux-gnu checking for PHP prefix... /usr checking for PHP includes... -I/usr/include/php7 -I/usr/include/php7/main -I/usr/include/php7/TSRM -I/usr/include/php7/Zend -I/usr/include/php7/ext -I/usr/include/php7/ext/date/lib checking for PHP extension directory... /usr/lib64/php7/extensions checking for PHP installed headers prefix... /usr/include/php7 checking if debug is enabled... no checking if zts is enabled... no checking for re2c... re2c checking for re2c version... 1.0.3 (ok) checking for gawk... gawk checking whether to enable Zend bcgen support... yes, shared checking whether to enable file based caching... yes checking whether to enable copying PHP CODE pages into HUGE PAGES... yes checking for ANSI C header files... yes checking for sys/types.h... yes checking for sys/stat.h... yes checking for stdlib.h... yes checking for string.h... yes checking for memory.h... yes checking for strings.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for unistd.h... yes checking for unistd.h... (cached) yes checking sys/uio.h usability... yes checking sys/uio.h presence... yes checking for sys/uio.h... yes checking how to print strings... printf checking for a sed that does not truncate output... (cached) /bin/sed checking for fgrep... /bin/grep -F checking for ld used by cc... /usr/x86_64-suse-linux/bin/ld checking if the linker (/usr/x86_64-suse-linux/bin/ld) is GNU ld... yes checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B checking the name lister (/usr/bin/nm -B) interface... BSD nm checking whether ln -s works... yes checking the maximum length of command line arguments... 1572864 checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop checking for /usr/x86_64-suse-linux/bin/ld option to reload object files... -r checking for objdump... objdump checking how to recognize dependent libraries... pass_all checking for dlltool... no checking how to associate runtime and link libraries... printf %s\n checking for ar... ar checking for archiver @FILE support... @ checking for strip... strip checking for ranlib... ranlib checking for gawk... (cached) gawk checking command to parse /usr/bin/nm -B output from cc object... ok checking for sysroot... no checking for a working dd... /bin/dd checking how to truncate binary pipes... /bin/dd bs=4096 count=1 checking for mt... mt checking if mt is a manifest tool... no checking for dlfcn.h... yes checking for objdir... .libs checking if cc supports -fno-rtti -fno-exceptions... no checking for cc option to produce PIC... -fPIC -DPIC checking if cc PIC flag -fPIC -DPIC works... yes checking if cc static flag -static works... no checking if cc supports -c -o file.o... yes checking if cc supports -c -o file.o... (cached) yes checking whether the cc linker (/usr/x86_64-suse-linux/bin/ld -m elf_x86_64) supports shared libraries... yes checking whether -lc should be explicitly linked in... no checking dynamic linker characteristics... GNU/Linux ld.so checking how to hardcode library paths into programs... immediate checking whether stripping libraries is possible... yes checking if libtool supports shared libraries... yes checking whether to build shared libraries... yes checking whether to build static libraries... no configure: creating ./config.status config.status: creating config.h config.status: executing libtool commands `/root/install/bc # make /root/install/bc/libtool --mode=compile cc -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -I. -I/root/install/bc -DPHP_ATOM_INC -I/root/install/bc/include -I/root/install/bc/main -I/root/install/bc -I/usr/include/php7 -I/usr/include/php7/main -I/usr/include/php7/TSRM -I/usr/include/php7/Zend -I/usr/include/php7/ext -I/usr/include/php7/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /root/install/bc/ZendAccelerator.c -o ZendAccelerator.lo libtool: compile: cc -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -I. -I/root/install/bc -DPHP_ATOM_INC -I/root/install/bc/include -I/root/install/bc/main -I/root/install/bc -I/usr/include/php7 -I/usr/include/php7/main -I/usr/include/php7/TSRM -I/usr/include/php7/Zend -I/usr/include/php7/ext -I/usr/include/php7/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /root/install/bc/ZendAccelerator.c -fPIC -DPIC -o .libs/ZendAccelerator.o /root/install/bc/ZendAccelerator.c: In function 'cache_script_in_file_cache': /root/install/bc/ZendAccelerator.c:167:29: error: 'ZEND_COMPILE_WITH_FILE_CACHE' undeclared (first use in this function); did you mean 'ZEND_EX_USE_RUN_TIME_CACHE'? CG(compiler_options) |= ZEND_COMPILE_WITH_FILE_CACHE; ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ ZEND_EX_USE_RUN_TIME_CACHE /root/install/bc/ZendAccelerator.c:167:29: note: each undeclared identifier is reported only once for each function it appears in /root/install/bc/ZendAccelerator.c: In function 'bcgen_compile_file': /root/install/bc/ZendAccelerator.c:279:33: error: 'ZEND_COMPILE_WITH_FILE_CACHE' undeclared (first use in this function); did you mean 'ZEND_EX_USE_RUN_TIME_CACHE'? CG(compiler_options) |= ZEND_COMPILE_WITH_FILE_CACHE; ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ ZEND_EX_USE_RUN_TIME_CACHE /root/install/bc/ZendAccelerator.c:310:31: error: 'ZEND_ACC_EARLY_BINDING' undeclared (first use in this function); did you mean 'ZEND_ACC_VARIADIC'? (op_array->fn_flags & ZEND_ACC_EARLY_BINDING) ? ^~~~~~~~~~~~~~~~~~~~~~ ZEND_ACC_VARIADIC /root/install/bc/ZendAccelerator.c:311:13: warning: implicit declaration of function 'zend_build_delayed_early_binding_list'; did you mean 'zend_do_delayed_early_binding'? [-Wimplicit-function-declaration] zend_build_delayed_early_binding_list(op_array) : ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ zend_do_delayed_early_binding /root/install/bc/ZendAccelerator.c: In function 'accel_startup': /root/install/bc/ZendAccelerator.c:704:28: error: 'zend_post_startup_cb' undeclared (first use in this function); did you mean 'zend_post_startup'? orig_post_startup_cb = zend_post_startup_cb; ^~~~~~~~~~~~~~~~~~~~ zend_post_startup /root/install/bc/ZendAccelerator.c: In function 'accel_post_startup': /root/install/bc/ZendAccelerator.c:744:40: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] accelerator_orig_zend_resolve_path = zend_resolve_path; ^ /root/install/bc/ZendAccelerator.c:745:23: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] zend_resolve_path = persistent_zend_resolve_path; ^ make: *** [Makefile:193: ZendAccelerator.lo] Error 1 `
What is wrong?
Thank You!
I am trying to get bcgen to work with PHAR files. I have added the compiled PHP files to the PHAR file (using their PHP extension within the PHAR file)
The stub of the PHP archive is:
#!/usr/bin/env php
<?php
Phar::mapPhar( 'app.phar' );
set_include_path( 'phar://app.phar' . PATH_SEPARATOR.';.');
require('phar://' . __FILE__ . '/app.php');
__HALT_COMPILER();
?>`
The file app.php is in the root of the PHAR archive and is the version compiled by bcgen.
When I run the phar (on the commandline), all I get is some gibberish output (to STDOUT) and the process exits with exitcode 130 ?
My understanding is that bcgen extension will inspect the file which is included/required (ie. app.php) and then - if it is bytecode - then simply execute the bytecode - If it is not, then it will parse the included file normally ?
However it seems that what actually happens, is that the header of the PHAR file itself is checked and since that is obviously not a BCGEN header, processing is aborted.
If we could skip this check when running from a PHAR archive (from the commandline or when the PHAR is included within some other PHP file) and in this scenario only "react" to include/require/autoloading statements (ie. check if a file that is about to be included is bytecode or not - if it is, then run it - If it is not then compile it first and then run the resulting bytecode), then - in my humble opinion - we would have a way of adding minimal protection to PHAR files - useful when redistributing PHARs (since such a PHAR archive would then simply contain a number of files that are bytecode - except for the stub of course...) and that would - I think - make PHAR files even cooler and provide a most sought-after feature.
Please let me know what you think.
Thanks for your efforts for providing bcgen.
Hi there
It is not possible to execute bcgen encoded files on PHP-CLI where shebang is a must.
I've tried this: http://prntscr.com/npvgbc
Also I tried adding opening and closing tags, that also failed.
I would be appreciated to receive any help regarding this matter.
Thanks
$ cat s/testinfo.php
<?php
phpinfo();
$ cat compile.php | grep testinfo
bcgen_compile_file("s/testinfo.php", "t/testinfo.php");
$ cd t/ # testinfo.php is compiled here
$ php testinfo.php | head
phpinfo()
PHP Version => 7.2.7-0ubuntu0.18.04.2
System => Linux pluton 4.4.0-17134-Microsoft #137-Microsoft Thu Jun 14 18:46:00 PST 2018 x86_64
Build Date => Jul 4 2018 16:55:24
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/php/7.2/cli
Loaded Configuration File => /etc/php/7.2/cli/php.ini
Scan this dir for additional .ini files => /etc/php/7.2/cli/conf.d
$ cd t/
$ php -S 127.0.0.1:8080
PHP 7.2.7-0ubuntu0.18.04.2 Development Server started at Thu Aug 2 17:49:52 2018
Listening on http://127.0.0.1:8080
Document root is /home/vjardin/Documents/compile/t
Press Ctrl-C to quit.
zend_mm_heap corrupted
Hi there.
I see all usable PHP versions listed but not 5.6?
Any plans to support PHP 7.3?
What is the assumption to use the files compiled by bcgen_compile_file() ?
-> should they keep the same names?
Same question, about the files included (required), how should they be named?
I feel that we cannot rename any files. So assuming that all the plain php code is into a folder orig/.php ; in order to avoid overwriting them, we need to compile them into a target folder target/.php without renaming them (for instance no *.phb renaming).
Please, can you confirm?
Where is the documentation about extension php functions.
I have create on difference Server to Docker container with PHP7. On server 1 create php bytecode and copy all to Server 2 Docker Container. The Ngnix on Server 2 write to Log: nginx errors “recv() failed (*: Connection reset by peer) while reading response header from upstream”
When i create new php bytecode on Server 2 it works.
Server 1:
PHP Version 7.2.15-1+020190209065123.16+stretch1.gbp3ad8c0
Linux ba379afcdad3 3.19.0-74-generic #82~14.04.1-Ubuntu SMP Fri Oct 21 15:43:47 UTC 2016 x86_64
Server 2:
PHP Version 7.2.15-1+020190209065123.16+stretch1.gbp3ad8c0
Linux afccfcb416d6 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1+deb9u1 (2018-05-07) x86_64
How i/to can fix this problem/check difference version? Is there workaround? I want create on my server bytecode and share to customers.
Can you help me?
Hello,
[root@localhost /]# cd /tmp/bcgen-php72
[root@localhost bcgen-php72]# phpize ./configure \ --with-php-config=/bin/php-config make install
Configuring for:
PHP Api Version: 20170718
Zend Module Api No: 20170718
Zend Extension Api No: 320170718
[root@localhost bcgen-php72]# find / -name "bcgen.so"
[root@localhost bcgen-php72]#
After that command bcgen.so in php extension dir is missing..
extension_dir = /usr/lib64/php/modules
CentOS Linux release 8.1.1911 (Core)
PHP 7.2.11
What I'am doing wrong?
sorry i never use bcompiler or bcgen, any page or site how to use this ? thanks ..
Hi !
Since PHP 8.0 will be released soon, I just wondered if we have "someone" to work on making bcgen PHP8.0 compatible ?
Please, can you provide builds with bcgen dll for Windows?
/usr/src/bcgen/zend_persist.c: 在函数‘zend_persist_op_array_ex’中:
/usr/src/bcgen/zend_persist.c:502: 错误:‘zend_arg_info’没有名为‘type’的成员
/usr/src/bcgen/zend_persist.c:503: 错误:‘zend_arg_info’没有名为‘type’的成员
/usr/src/bcgen/zend_persist.c:504: 错误:‘zend_arg_info’没有名为‘type’的成员
/usr/src/bcgen/zend_persist.c:507: 错误:‘zend_arg_info’没有名为‘type’的成员
make: *** [zend_persist.lo] 错误 1
make, make test and make install all worked perfectly. I can even compile and test my own compiled files with php and get the right output.
However, when I tried to enable this in my php.ini for php7.2-fpm and I only get:
fpm_stdio_child_said(), line 190: [pool www] child 15726 said into stderr: "zend_mm_heap corrupted"
Any tips of things I can try?
BCgen | Up and Running
Optimization | Enabled
Startup | OK
Directive | Local Value | Master Value
bcgen.dups_fix | On | On
bcgen.enable | On | On
bcgen.error_log | /var/log/nginx/error.log | /var/log/nginx/error.log
bcgen.file_consistency_checks | 0 | 0
bcgen.huge_code_pages | Off | Off
bcgen.log_verbosity_level | 4 | 4
bcgen.opt_debug_level | 0 | 0
bcgen.optimization_level | 0x7FFFBFFF | 0x7FFFBFFF
bcgen.save_comments | 0 | 0
Magic Constants like __DIR__
and __FILE__
keep value of compiling path.
Is there a way to get the runtime value of __FILE__
??
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.