Skip to content

Support proc_open(), exec(), passthru(), system() #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 24, 2023
Merged

Conversation

adamziel
Copy link
Collaborator

@adamziel adamziel commented Jun 26, 2023

Description

Adds support for proc_open(), popen($cmd, "w"), exec(), passthru(), system() via a custom spawn handler defined in JavaScript via php.setSpawnHandler().

proc_open()

This PR patches PHP to replace C calls to proc_open() with a custom implementation provided with a new proc_open7.0.c/proc_open7.4.c file. The original proc_open() relies on POSIX fork() function not available in WebAssembly, so which is why this PR defers the command execution to the setSpawnHandler() callback provided via JavaScript.

exec(), passthru(), system()

This PR provides a custom php_exec implementation (via the wasm_php_exec C function). php_exec is the function powering PHP functions like exec(), passthru(), system().

popen()

The existing popen() implementation is rewired to call the setSpawnHandler() callback. Also, the support for the popen($cmd, "w") mode was added.

Other notes

This PR removes support for PHP 5.6 as supporting these functions was challenging there AND also WordPress dropped the support for PHP 5.6.

Closes #594
Closes #710

@dmsnell
Copy link
Member

dmsnell commented Jul 17, 2023

this seems quite usable, though maybe we need to figure out how to make the difference between browser and Node environments less implicit. if running in Node certain things will work that crash in the browser. I wonder if we can stub the functions in the web version to do something benign.

obviously we can't spawn arbitrary processes because we only have PHP and SQLite built. what is PHP spawning? is this only important when running the WordPress test suites?

@adamziel
Copy link
Collaborator Author

adamziel commented Oct 2, 2023

this seems quite usable, though maybe we need to figure out how to make the difference between browser and Node environments less implicit. if running in Node certain things will work that crash in the browser. I wonder if we can stub the functions in the web version to do something benign.

Good call! I added this:

		if (!ENVIRONMENT_IS_NODE) {
			return 1; // proc_open() is not supported in the browser yet.
		}

obviously we can't spawn arbitrary processes because we only have PHP and SQLite built. what is PHP spawning? is this only important when running the WordPress test suites?

Mostly it's spawning php, as in phpunit test runner spawns another process. A follow-up PR would be good to ensure proc_open('php' runs php-wasm as the system installation may be a different PHP version or missing entirely.

Spawning another WebAssembly PHP instance could just work on the web. We could even open up wider proc_open support in the browser by exposing a callback like registerShellCommand() that people could use to shim busybox and other utilities as needed. Let's talk about it once a need arises.

@adamziel adamziel marked this pull request as ready for review October 2, 2023 17:07
@adamziel adamziel changed the title Quick&dirty attempt to shim proc_open with child_process proc_open() support Oct 9, 2023
@adamziel adamziel changed the title proc_open() support Support proc_open(), exec(), passthru(), system() Oct 23, 2023
@adamziel adamziel requested a review from a team as a code owner October 23, 2023 11:55
Provides a custom implementation of:

* php_exec – under the name wasm_php_exec
* proc_open

Both allow the C API to call a JavaScript callback set
via setSpawnHandler().
@adamziel adamziel merged commit 9bceb4a into trunk Oct 24, 2023
@adamziel adamziel deleted the popen_for_php_7_4 branch October 24, 2023 20:03
adamziel added a commit that referenced this pull request Jan 8, 2024
Continues the work from
#710 and
#596. Rewires the
PHP `VCWD_POPEN` macro used internally to spawn a new process to
Playground-provided implementation called `wasm_popen`. This enables
handling the `shell_exec` calls using the same `createSpawnHandler()`
JavaScript callback as all the other process-opening functions like
`proc_open`, `system`, or `exec`

## Testing instructions

This PR ships a unit test for `shell_exec()` so you just need to confirm
that test passes.
@adamziel adamziel mentioned this pull request Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support system(), exec(), passthrough() functions PHP: Support proc_open
2 participants