php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79714 Extension bcgen + PHAR archives appear not to be able to work together
Submitted: 2020-06-18 20:09 UTC Modified: 2020-06-25 12:17 UTC
From: henry dot wood dot dk at gmail dot com Assigned:
Status: Open Package: PHAR related
PHP Version: 7.2.31 OS: Ubuntu 18.04
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-06-18 20:09 UTC] henry dot wood dot dk at gmail dot com
Description:
------------
I am trying to build a PHAR archive which contains both files compiled by bcgen (See: https://github.com/vjardin/bcgen) and normal PHP files

I would then expect the PHAR file to be runnable via the command line, regardless   which type of file is referenced (either a bcgen compiled file or a normal php file) as this is what is explained in the README of bcgen.

NOTE: Also note that this INI setting should be set:


bcgen.enable (default "1")
	BCgen On/Off switch. When set to Off, bytecode files cannot be read

Test script:
---------------
<?php
// CHECK FOR BCGEN
if (! extension_loaded('Zend BCgen')) {
        echo "ERROR: Needs Zend BCgen extension".PHP_EOL;
        die(-1);
}

// COMPILE test.phb FROM test.php
// CONTENTS OF test.php: <?php echo "HELLO FROM BCGEN...".PHP_EOL;
file_put_contents('test.php', '<?php echo "HELLO FROM BCGEN...".PHP_EOL;');
bcgen_compile_file('test.php', 'b.phb');

// MAKE test2.php
file_put_contents('test2.php', '<?php echo "HELLO FROM NORMAL PHP...".PHP_EOL;');

// Make PHAR
$phar = new Phar( 'simple.phar' );
$phar->addFile('test.phb', 'b.phb');
$phar->addFile('test2.php', 'c.php');
$phar['a.php'] = '<?php echo "Hola Mundo\n"; include("b.phb"); include("c.php")?>';
$pharfile = 'simple.phar';
$startfile = 'a.php';
$stub = <<< "EOT"
#!/usr/bin/env php
<?php
Phar::mapPhar( '$pharfile' );
set_include_path( 'phar://' . __FILE__ . PATH_SEPARATOR . get_include_path() );
require( '$startfile' );
__HALT_COMPILER();
EOT;
$phar->setStub( $stub );
echo "DONE ...".PHP_EOL;

Expected result:
----------------
After running the test script to build the PHAR archive, I would expect running the simple.phar file from command line to output:

Expected output:
Hola Mundo
HELLO FROM BCGEN...
HELLO FROM NORMAL PHP...

Actual result:
--------------
Actual output:
Hola Mundo
/tmp/test.php▒▒▒▒▒▒▒▒▒`▒f/▒,▒HELLO FROM BCGEN...
i▒▒▒▒>
HELLO FROM NORMAL PHP...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-18 20:11 UTC] henry dot wood dot dk at gmail dot com
Possible conclusion:

Any compiled file *WITHIN* the phar archive 'simple.phar' is simply read as-is and not parsed/evaluated/executed - however, the BCGEN header appears to be stripped from the output.
Any normal PHP file *WITHIN* the phar archive 'simple.phar' is read, parsed, evaluted and executed as expected.
 [2020-06-18 20:21 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2020-06-18 20:21 UTC] nikic@php.net
Do you have any specific reason to believe this issue is on the phar side rather than the bcgen side? I would recommend reporting the issue with bcgen first.

As the project is compatible only with PHP 7.2 and PHP 7.2 is no longer actively supported, we will not be able to fix anything anyway (though if the issue is indeed on the phar side, future versions may be fixed -- that's a big if though).
 [2020-06-18 20:30 UTC] henry dot wood dot dk at gmail dot com
I have previously reported what I think is the same bug to bcgen - Please see: https://github.com/vjardin/bcgen/issues/10

Also running the simple.phar file on the command line, yields an exit code of 130.
 [2020-06-18 21:00 UTC] henry dot wood dot dk at gmail dot com
As BCGEN is based on opcache code, I would - ideally - very much like for someone in the PHP Team to reach out to the BCGEN authors (either the opcache code authors and/or the PHAR authors) and help the authors of BCGEN as it is my feeling that the authors of BCGEN have admitted to not knowing enough about opcache code and/or PHAR extension in order to solve the problem on their own.

The goal would then - of course - be to benefit the BCGEN extension in such a way that it will become compatible with PHP version 7.2+.

Ultimately, I would like to achieve a situation where PHARs can be used to distribute applications in a way/format which is not so easily reverse engineered.
This would indeed benefit the entire PHP community in my humble opinion.
I think it is about time that it becomes possible to distribute PHARs in a secure manner.
 [2020-06-18 21:44 UTC] henry dot wood dot dk at gmail dot com
No, I don't have a specific reason to believe that the problem is with the PHAR extension. However, it should be obvious by now that the combination of BCGEN + PHAR does not work as expected.

As previously stated, I have already submitted a bugreport to the authors of BCGEN, but as also previously stated, I appeared to be in need of help if this problem is to be resolved.
 [2020-06-25 12:17 UTC] cmb@php.net
-Status: Feedback +Status: Open
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Jul 05 00:01:26 2020 UTC