Skip to content

[ php-wasm ] Terminate the non-CLI additional functions list - #4524

Merged
mho22 merged 1 commit into
trunkfrom
fix-php-wasm-function-entry-sentinel
Oct 27, 2026
Merged

mho22 merged 1 commit into
trunkfrom
fix-php-wasm-function-entry-sentinel

Conversation

@mho22

@mho22 mho22 commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

A small one, and honestly a low-value one: this only matters if you build PHP.wasm with WITH_CLI_SAPI=no, which no committed binary does. But that option is offered by build.js, and right now it produces a PHP that can crash at startup. So it is broken, even if nobody uses it.

What is wrong

php_wasm.c has two versions of additional_functions[]. The CLI version ends with the usual {NULL, NULL, NULL} entry. The non-CLI version, compiled when WITH_CLI_SAPI=no, does not. zend_register_functions() reads entries until it finds a NULL name, so without that last entry it keeps reading the memory after the array.

How I found it

While I tried a wasm64 build of PHP 8.5 to answer the question in #4499, I used a reduced build with WITH_CLI_SAPI=no. It crashed at startup in strlen() inside zend_register_functions(). With this line added, the same build starts and runs Zend/bench.php. I did not check what wasm32 does without the fix: it depends on what sits in memory after the array.

What this changes

Nothing for current users. Every committed binary uses WITH_CLI_SAPI=yes, so no binary is rebuilt here. It only makes WITH_CLI_SAPI=no usable for whoever needs it.

@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Caution

Review failed

The pull request is closed.

Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@mho22

mho22 commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator Author

Rebuilding the whole set of php.wasm binaries won't help here, since they already use WITH_CLI_SAPI == 1. I checked with a PHP 8.5 build from trunk and one from this branch: the only difference in php_8_5.wasm is the build date string.

@mho22
mho22 marked this pull request as ready for review October 27, 2026 13:11
@mho22
mho22 merged commit cef108e into trunk Oct 27, 2026
63 checks passed
@mho22
mho22 deleted the fix-php-wasm-function-entry-sentinel branch October 27, 2026 13:12
mho22 added a commit that referenced this pull request Nov 5, 2026
…ing reads on slow pipes (#3511)

## Motivation for the change, related issues

`js_fd_read` in `php_wasm.c` has a hardcoded 5-second polling timeout
that causes blocking `fread()` calls to silently return empty data when
the pipe takes longer than 5 seconds to produce output.

This breaks any use case where PHP reads from a slow process via
`proc_open`.

The timeout was introduced in #931 as a safety net against hanging
reads, then carried over into `php_wasm.c` in #1867.

## Why the timeout is no longer necessary

#931 was the first time `fread()` on a blocking pipe polled for data
instead of failing with `EWOULDBLOCK`. At that point a child process
that died or never started could leave the pipe open, and the read would
poll forever with no visible feedback. PHP's own `max_execution_time`
does not help here: it is `0` in CLI mode, and the timer does not fire
while the read awaits in JavaScript. The 5-second cap made that hang
visible, notably when running the PHPUnit test suite.

Every situation the cap guarded against now ends the read on its own:

- When the child process exits, the `exit` handler closes the child's
end of the pipes, and the `pipe.refcnt < 2` check returns.
- When the child process emits `error` without `exit`, the `error`
handler closes the child's end of the pipes.
- When the spawn handler never signals `spawn`, `js_open_process` stops
waiting after 5 seconds and closes the child's end of the pipes.
- When Node cannot start the process at all, the child has no pid.
`proc_open()` returns `false` with the spawn error, so there is no pipe
to read.
- When PHP closes the stream, the `FS.isClosed` check returns.
- When the pipe is non-blocking, `js_fd_read` returns `EWOULDBLOCK`
immediately without polling.

The one remaining case is a child process that is alive, never writes,
and never exits. Native PHP blocks forever on that read too, so this
matches the POSIX behaviour. The read awaits in JavaScript, so the event
loop keeps running and the host can still kill the child, which closes
the pipe and unblocks the read. A read that silently gives up after 5
seconds is worse than a read that blocks, because the caller cannot tell
truncated data from a completed read.

## Implementation details

- Remove the retry counter and `maxRetries` check from the `js_fd_read`
polling loop.
- Move the cleanup of the child's pipe ends from the `exit` handler into
`closeChildPipes()`. It runs on `exit`, on `error`, and when the spawn
wait fails.
- Return the negative errno from `js_open_process`, and set `errno` from
it in `proc_open.c`. Before, `js_open_process` called
`___errno_location(code)`. That export takes no argument, and as a JSPI
export it returns a Promise, so `errno` stayed `0` and `proc_open()`
warned `proc_open() failed - Success`. It now warns `proc_open() failed
- No such file or directory` for a missing `cwd`.
- Copy `config.guess` and `config.sub` from the base image's automake in
`Dockerfile-5-2` instead of downloading them from savannah, so the PHP
5.2 build does not depend on that server.
- Update the PHP 8.5 wasm path in the `php-wasm/web` README from
`8_5_10` to `8_5_12`.
- Rebuild every PHP.wasm binary on top of trunk: PHP 5.2 and 7.4 to 8.5,
for web and Node, in JSPI and Asyncify. The rebuild also ships the
`php_wasm.c` fix from #4524, which trunk merged without a rebuild.

## Breaking change

A blocking `fread()` on a pipe from a child process that never writes
and never exits now waits until the child exits or the host closes the
pipe, instead of returning empty data after 5 seconds.

## Testing Instructions (or ideally a Blueprint)

CI

The `proc_open()` tests in `php-part-1.spec.ts` cover each exit
condition:

- `blocking fread waits for slow pipe output`: the child writes after 6
seconds, and `fread()` receives the data.
- `blocking fread returns when the spawn handler never signals spawn`:
`fread()` returns empty after the 5-second spawn wait.
- `blocking fread returns when the process errors without exiting`:
`fread()` returns empty after the `error` event.
- `reports the spawn error when the process never starts`: `proc_open()`
returns `false` and warns `No such file or directory`.

`Handle process spawn timeout gracefully` is removed. Its spawn handler,
built with `createSpawnHandler()`, emits `spawn` as soon as its callback
returns a promise, so the test covered a live child that never writes.
That case now blocks like native PHP.

### Verification against the WordPress PHPUnit suite

Since #931 was built to run PHPUnit, I ran the WordPress core test suite
from `wordpress-develop` through the Playground CLI `php` subcommand
(PHP 8.3, WordPress nightly) on both trunk and this branch. The results
are identical: `Tests_Option_Option` passes 85 tests with 3 skipped, and
the `Tests_Functions_*` classes run 1028 tests with the same 42 failures
on both builds. Those failures come from environment differences and
from PHPUnit process isolation, not from this change. In the same CLI, a
`proc_open` child that writes after 7 seconds is read correctly on this
branch, while trunk returns an empty string after 5 seconds.

---------

Co-authored-by: Brandon Payton <brandon@happycode.net>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.

1 participant