Skip to content

Conversation

nielsdos
Copy link
Member

There were a couple of issues with the phar stub. It seems that in the past people did not realise they had to change shortarc.php and run makestub.php to generate the stubfile and instead they modified stub.h manually. This meant that there were a couple of mistakes in the stub which are fixed in this patch. In particular:

  • The title tag was not closed
  • The length of the stub was wrong in stub.h
  • This PR syncs previous changes to stub.h back to shortarc.php and vice versa.
  • Adds a note such that hopefully no mistakes against updating the stubs are made in the future (hopefully).
  • The makestub.php script was actually broken because the expected signature of the stub got changed in b874f1a

There were a couple of issues with the phar stub. It seems that in the
past people did not realise they had to change shortarc.php and run
makestub.php to generate the stubfile and instead they modified stub.h
manually. This meant that there were a couple of mistakes in the stub
which are fixed in this patch. In particular:

* The title tag was not closed
* The length of the stub was wrong in stub.h
* This PR syncs previous changes to stub.h back to shortarc.php
  and vice versa.
* Adds a note such that hopefully no mistakes against updating the stubs
  are made in the future (hopefully).
* The makestub.php script was actually broken because the expected
  signature of the stub got changed in b874f1a
@mvorisek
Copy link
Contributor

It seems that in the past people did not realise they had to change shortarc.php and run makestub.php to generate the stubfile and instead they modified stub.h manually.

assert that in https://github.com/php/php-src/blob/master/.github/actions/verify-generated-files/action.yml with CI

Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks sensible but I don't know anything about phar tbh.

One question, is the change in file permission in makestub.php intended?

@nielsdos
Copy link
Member Author

This looks sensible but I don't know anything about phar tbh.

I don't know a lot about phar too tbh, so best to let someone with more knowledge about phar double check I guess... 😅

One question, is the change in file permission in makestub.php intended?

Yes. This is needed so we can conveniently run it from the command line and from the CI script (see my second commit) :)

@medabkari
Copy link

I don't know a lot about phar too tbh, so best to let someone with more knowledge about phar double check I guess... 😅

This PR has been stale for almost a year. I remember Bishop was the maintainer of ext/phar, so if someone could ping him for a quick review, that would be helpful.

Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks okay after a rebase to 8.2

ext/phar/stub.h Outdated
static const char newstub3_1[] = "ction tmpdir()\n{\nif (strpos(PHP_OS, 'WIN') !== false) {\nif ($var = getenv('TMP') ? getenv('TMP') : getenv('TEMP')) {\nreturn $var;\n}\nif (is_dir('/temp') || mkdir('/temp')) {\nreturn realpath('/temp');\n}\nreturn false;\n}\nif ($var = getenv('TMPDIR')) {\nreturn $var;\n}\nreturn realpath('/tmp');\n}\n\nstatic function _unpack($m)\n{\n$info = unpack('V', substr($m, 0, 4));\n\n$l = unpack('V', substr($m, 10, 4));\n$m = substr($m, 14 + $l[1]);\n$s = unpack('V', substr($m, 0, 4));\n$o = 0;\n$start = 4 + $s[1];\n$ret['c'] = 0;\n\nfor ($i = 0; $i < $info[1]; $i++) {\n\n$len = unpack('V', substr($m, $start, 4));\n$start += 4;\n\n$savepath = substr($m, $start, $len[1]);\n$start += $len[1];\n\n\n\n$ret['m'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($m, $start, 24)));\n$ret['m'][$savepath][3] = sprintf('%u', $ret['m'][$savepath][3]\n& 0xffffffff);\n$ret['m'][$savepath][7] = $o;\n$o += $ret['m'][$savepath][2];\n$start += 24 + $ret['m'][$savepath][5];\n$ret['c'] |= $ret['m'][$savepath][4] & self::MASK;\n}\nreturn $ret;\n}\n\nstatic function extractFile($path, $entry, $fp)\n{\n$data = '';\n$c = $entry[2];\n\nwhile ($c) {\nif ($c < 8192) {\n$data .= @fread($fp, $c);\n$c = 0;\n} else {\n$c -= 8192;\n$data .= @fread($fp, 8192);\n}\n}\n\nif ($entry[4] & self::GZ) {\n$data = gzinflate($data);\n} elseif ($entry[4] & self::BZ2) {\n$data = bzdecompress($data);\n}\n\nif (strlen($data) != $entry[0]) {\ndie(\"Invalid internal .phar file (size error \" . strlen($data) . \" != \" .\n$entry[0] . \")\");\n}\n\nif ($entry[3] != sprintf(\"%u\", crc32($data) & 0xffffffff)) {\ndie(\"Invalid internal .phar file (checksum error)\");\n}\n\nreturn $data;\n}\n\nstatic function _removeTmpFiles($temp, $origdir)\n{\nchdir($temp);\n\nforeach (glob('*') as $f) {\nif (file_exists($f)) {\nis_dir($f) ? @rmdir($f) : @unlink($f);\nif (file_exists($f) && is_dir($f)) {\nself::_removeTmpFiles($f, getcwd());\n}\n}\n}\n\n@rmdir($temp);\nclearstatcache();\nchdir($origdir);\n}\n}\n\nExtract_Phar::go();\n__HALT_COMPILER(); ?>";

static const int newstub_len = 6623;
static const int newstub_len = 6625;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static const int newstub_len = 6625;
static const size_t newstub_len = 6625;

For master maybe?

*/
static inline void phar_get_stub(const char *index_php, const char *web, size_t *len, char **stub, const int name_len, const int web_len)
static inline zend_string *phar_get_stub(const char *index_php, const char *web, const int name_len, const int web_len)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static inline zend_string *phar_get_stub(const char *index_php, const char *web, const int name_len, const int web_len)
static inline zend_string *phar_get_stub(const char *index_php, const char *web, size_t name_len, size_t web_len)

It could make sense to also rearrange the parameter order in master alongside this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that this should change to size_t as that's what is used in phar_create_default_stub.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note that this will also require format change in strpprintf.

@nielsdos nielsdos mentioned this pull request Aug 30, 2025
@krakjoe krakjoe changed the base branch from PHP-8.1 to master September 2, 2025 04:53
@krakjoe krakjoe requested a review from TimWolla as a code owner September 2, 2025 04:53
@krakjoe
Copy link
Member

krakjoe commented Sep 2, 2025

I switched the base branch to master and fixed conflicts so we can run CI ... the alternative simpler approach that was taken in #19643 was failing CI, so I figure we might as well revisit this more complete approach.

Copy link
Member

@bukka bukka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From reviewing the logic of this interesting templating solution, it looks correct to me.

Just one minor suggestion for the comment and that size_t part probably makes sense too.

static inline zend_string *phar_get_stub(const char *index_php, const char *web, const int name_len, const int web_len)
{
/* Do NOT modify this file directly!
* Modify shortarc.php instead and use makestub.php to generate this file. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should probably note that shortarc.php is just for the PHP content and makestub.php for the C code. Maybe something like:

Instead modify shortarc.php to change PHP code or makestub.php to change C code and then use makestub.php to generate this file. 

*/
static inline void phar_get_stub(const char *index_php, const char *web, size_t *len, char **stub, const int name_len, const int web_len)
static inline zend_string *phar_get_stub(const char *index_php, const char *web, const int name_len, const int web_len)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that this should change to size_t as that's what is used in phar_create_default_stub.

*/
static inline void phar_get_stub(const char *index_php, const char *web, size_t *len, char **stub, const int name_len, const int web_len)
static inline zend_string *phar_get_stub(const char *index_php, const char *web, const int name_len, const int web_len)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note that this will also require format change in strpprintf.

@nielsdos
Copy link
Member Author

nielsdos commented Oct 1, 2025

I double checked this with a word-diff and it seems fine. Waiting for CI now before merging.

@nielsdos nielsdos merged commit b32a2a5 into php:master Oct 1, 2025
8 of 9 checks passed
StephenWall pushed a commit to StephenWall/php-src that referenced this pull request Oct 6, 2025
* Fixes to phar stub

There were a couple of issues with the phar stub. It seems that in the
past people did not realise they had to change shortarc.php and run
makestub.php to generate the stubfile and instead they modified stub.h
manually. This meant that there were a couple of mistakes in the stub
which are fixed in this patch. In particular:

* The title tag was not closed
* The length of the stub was wrong in stub.h
* This PR syncs previous changes to stub.h back to shortarc.php
  and vice versa.
* Adds a note such that hopefully no mistakes against updating the stubs
  are made in the future (hopefully).
* The makestub.php script was actually broken because the expected
  signature of the stub got changed in b874f1a

* Verify that the phar stub is up-to-date in CI

* Update stub again after master changes

* size_t changes

* Update test after stub changes (canonical casts)

---------

Co-authored-by: Joe Watkins <[email protected]>
StephenWall pushed a commit to StephenWall/php-src that referenced this pull request Oct 6, 2025
* Fixes to phar stub

There were a couple of issues with the phar stub. It seems that in the
past people did not realise they had to change shortarc.php and run
makestub.php to generate the stubfile and instead they modified stub.h
manually. This meant that there were a couple of mistakes in the stub
which are fixed in this patch. In particular:

* The title tag was not closed
* The length of the stub was wrong in stub.h
* This PR syncs previous changes to stub.h back to shortarc.php
  and vice versa.
* Adds a note such that hopefully no mistakes against updating the stubs
  are made in the future (hopefully).
* The makestub.php script was actually broken because the expected
  signature of the stub got changed in b874f1a

* Verify that the phar stub is up-to-date in CI

* Update stub again after master changes

* size_t changes

* Update test after stub changes (canonical casts)

---------

Co-authored-by: Joe Watkins <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants