Skip to content

[PHP-WASM] Route PHP popen through wasm wrappers - #3951

Merged
bgrgicak merged 2 commits into
trunkfrom
codex/smtp-popen-mail-routing
Jul 8, 2026
Merged

[PHP-WASM] Route PHP popen through wasm wrappers#3951
bgrgicak merged 2 commits into
trunkfrom
codex/smtp-popen-mail-routing

Conversation

@bgrgicak

@bgrgicak bgrgicak commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Motivation for the change, related issues

The previous popen/pclose implementation used single global variables (wasm_popen_last_pid and wasm_pclose_ret) to track the spawned process PID and exit code. This meant concurrent writable popen() calls would clobber each other's state, producing wrong exit codes or closing the wrong process.

Implementation details

Linker-level wrapping replaces macro patching

This PR uses --wrap=popen and --wrap=pclose linker flags to redirect all popen()/pclose() calls in the compiled C code to __wrap_popen/__wrap_pclose, which delegate to wasm_popen/wasm_pclose. For pclose, handles not created by wasm_popen fall through to the real libc pclose.

Per-fd PID tracking instead of globals

Replaces wasm_popen_last_pid and wasm_pclose_ret with an fd-to-pid mapping stored in the existing JS PHPWASM.processTable. Three new Emscripten library functions manage the mapping:

  • js_popen_set_pid_for_fd(fd, pid) — called by wasm_popen after spawning a process
  • js_popen_get_pid_for_fd(fd) — called by wasm_pclose to find which process to wait on
  • js_popen_clear_pid_for_fd(fd) — called by wasm_pclose before closing the file

This allows multiple popen handles to be open simultaneously without state corruption.

Emscripten library updates

Adds __wrap_popen, __wrap_pclose, and zif_pclose to the Asyncify import list and JSPI export list so the new wrapper functions can suspend correctly.

Testing Instructions

  • CI
"php_init_config",\
"zend_register_constant",\
"zend_register_ini_entries_ex",\
"php_mail",\

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This change is unrelated to the popen changes. We need to add support for PHP Mail to Asyncify for the SMTP project and by adding it here we are avoiding a recompile.

@bgrgicak
bgrgicak marked this pull request as ready for review July 8, 2026 08:14
@bgrgicak
bgrgicak requested review from a team, ashfame and Copilot July 8, 2026 08:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@bgrgicak

bgrgicak commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator Author

These changes were already approved in #3486 and I plan to merge it when CI passes tests.

@bgrgicak
bgrgicak merged commit fa6f03d into trunk Jul 8, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment