From 10a262616c60cd23fa648ca80143c138a6740d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Fri, 1 Dec 2023 13:21:16 +0100 Subject: [PATCH] Reconcile some trunk changes --- packages/php-wasm/compile/Dockerfile | 66 ++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/packages/php-wasm/compile/Dockerfile b/packages/php-wasm/compile/Dockerfile index e4caab9758..cbb826a920 100644 --- a/packages/php-wasm/compile/Dockerfile +++ b/packages/php-wasm/compile/Dockerfile @@ -103,7 +103,7 @@ FROM emscripten AS emscripten-libzip ARG PHP_VERSION COPY --from=emscripten-libz /root/lib /root/lib-libz RUN /root/copy-lib.sh lib-libz -RUN if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "3" ]] || [ "${PHP_VERSION:0:1}" -le "5" ]; then \ +RUN if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "3" ]]; then \ export LIBZIP_VERSION=1.2.0; \ else \ export LIBZIP_VERSION=1.9.2; \ @@ -274,10 +274,7 @@ COPY --from=emscripten-libzip /root/lib /root/lib-libzip RUN if [ "$WITH_LIBZIP" = "yes" ]; then \ /root/copy-lib.sh lib-libz; \ /root/copy-lib.sh lib-libzip && \ - if [ "${PHP_VERSION:0:1}" -le "5" ]; then \ - /root/replace.sh 's/ZEND_MODULE_GLOBALS_CTOR_N/(void (*)(void *))ZEND_MODULE_GLOBALS_CTOR_N/g' /root/php-src/ext/zlib/zlib.c; \ - fi;\ - if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "3" ]] || [ "${PHP_VERSION:0:1}" -le "5" ]; then \ + if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "3" ]]; then \ apt install -y zlib1g zlib1g-dev; \ # https://php-legacy-docs.zend.com/manual/php5/en/zlib.installation echo -n ' --with-zlib --with-zlib-dir=/root/lib --enable-zip --with-libzip=/root/lib ' >> /root/.php-configure-flags-bundled-extensions; \ @@ -323,7 +320,7 @@ RUN if [ "$WITH_LIBXML" = "yes" ]; \ # In the regular cc it's just a warning, but in the emscripten's emcc that's an error: perl -pi.bak -e 's/char xmlInitParser/void xmlInitParser/g' /root/php-src/configure; \ # On PHP < 7.1.0, the dom_iterators.c file implicitly converts *char to const *char causing emcc error - if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "0" ]] || [ "${PHP_VERSION:0:1}" -le "5" ]; then \ + if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "0" ]]; then \ /root/replace.sh 's/xmlHashScan\(ht, itemHashScanner, iter\);/xmlHashScan(ht, (xmlHashScanner)itemHashScanner, iter);/g' /root/php-src/ext/dom/dom_iterators.c; \ fi; \ else \ @@ -369,7 +366,7 @@ RUN if [ "$WITH_ICONV" = "yes" ]; \ # PHP <= 7.3 requires Bison 2.7 # PHP >= 7.4 and Bison 3.0 COPY --from=emscripten-bison-2-7 /usr/local/bison /root/linked-bison-27 -RUN if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "3" ]] || [ "${PHP_VERSION:0:1}" -le "5" ]; then \ +RUN if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "3" ]]; then \ mv /root/linked-bison-27 /usr/local/bison && \ ln -s /usr/local/bison/bin/bison /usr/bin/bison && \ ln -s /usr/local/bison/bin/yacc /usr/bin/yacc; \ @@ -497,7 +494,7 @@ RUN /root/replace.sh 's/define HAVE_UNISTD_H 1/define HAVE_UNISTD_H 0/g' /root/p # PHP <= 7.3 is not very good at detecting the presence of the POSIX readdir_r function # so we need to force it to be enabled. -RUN if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "3" ]] || [ "${PHP_VERSION:0:1}" -le "5" ]; then \ +RUN if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "3" ]]; then \ echo '#define HAVE_POSIX_READDIR_R 1' >> /root/php-src/main/php_config.h; \ fi; @@ -508,12 +505,28 @@ RUN /root/replace.sh 's/static int php_cli_server_poller_poll/extern int wasm_se # Provide a custom implementation of the php_select() function. RUN /root/replace.sh 's/return php_select\(/return wasm_select(/g' /root/php-src/sapi/cli/php_cli_server.c +# Provide a custom implementation of the php_exec() function that handles spawning +# the process inside exec(), passthru(), system(), etc. +# We effectively remove the php_exec() implementation from the build by renaming it +# to an unused identifier "php_exec_old", and then we mark php_exec as extern. +RUN /root/replace.sh 's/PHPAPI int php_exec(.+)$/PHPAPI extern int php_exec\1; int php_exec_old\1/g' /root/php-src/ext/standard/exec.c + +# Provide a custom implementation of the VCWD_POPEN() function that handles spawning +# the process inside PHP_FUNCTION(popen). +RUN /root/replace.sh 's/VCWD_POPEN\(/wasm_popen(/g' /root/php-src/ext/standard/file.c +RUN /root/replace.sh 's/PHP_FUNCTION\(popen\)/extern FILE *wasm_popen(const char *cmd, const char *mode);PHP_FUNCTION(popen)/g' /root/php-src/ext/standard/file.c + # Provide a custom implementation of the shutdown() function. RUN perl -pi.bak -e $'s/(\s+)shutdown\(/$1 wasm_shutdown(/g' /root/php-src/sapi/cli/php_cli_server.c RUN perl -pi.bak -e $'s/(\s+)closesocket\(/$1 wasm_close(/g' /root/php-src/sapi/cli/php_cli_server.c RUN echo 'extern int wasm_shutdown(int fd, int how);' >> /root/php-src/main/php_config.h; RUN echo 'extern int wasm_close(int fd);' >> /root/php-src/main/php_config.h; +# Don't ship PHP_FUNCTION(proc_open) with the PHP build +# so that we can ship a patched version with php_wasm.c +RUN echo '' > /root/php-src/ext/standard/proc_open.h; +RUN echo '' > /root/php-src/ext/standard/proc_open.c; + RUN source /root/emsdk/emsdk_env.sh && \ # We're compiling PHP as emscripten's side module... EMCC_FLAGS=" -sSIDE_MODULE -Dsetsockopt=wasm_setsockopt -Dpopen=wasm_popen -Dpclose=wasm_pclose " \ @@ -525,6 +538,16 @@ RUN cp -v /root/php-src/.libs/libphp*.la /root/lib/libphp.la RUN cp -v /root/php-src/.libs/libphp*.a /root/lib/libphp.a COPY ./build-assets/php_wasm.c /root/ +COPY ./build-assets/proc_open* /root/ + +RUN if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "3" ]]; then \ + cp /root/proc_open7.0.c /root/proc_open.c; \ + cp /root/proc_open7.0.h /root/proc_open.h; \ + else \ + cp /root/proc_open7.4.c /root/proc_open.c; \ + cp /root/proc_open7.4.h /root/proc_open.h; \ + fi + ARG WITH_SOURCEMAPS RUN set -euxo pipefail; \ @@ -605,7 +628,10 @@ RUN if [ "$WITH_WS_NETWORKING_PROXY" = "yes" ]; \ "invoke_viiiiii",\n\ "invoke_viiiiiii",\n\ "invoke_viiiiiiiii",\n\ +"js_open_process",\n\ +"js_popen_to_file",\n\ "wasm_poll_socket",\n\ +"js_module_onMessage",\n\ "wasm_shutdown"]'; \ echo -n " -s ASYNCIFY_IMPORTS=$ASYNCIFY_IMPORTS " | tr -d "\n" >> /root/.emcc-php-wasm-flags; \ export ASYNCIFY_ONLY_UNPREFIXED=$'"dynCall_dd",\ @@ -639,7 +665,14 @@ RUN if [ "$WITH_WS_NETWORKING_PROXY" = "yes" ]; \ "dynCall_viiiii",\ "dynCall_viiiiiii",\ "dynCall_viiiiiiii",'; \ - export ASYNCIFY_ONLY=$'"zif_array_filter",\ + export ASYNCIFY_ONLY=$'"zif_array_filter",\"__fwritex",\ +"zif_sleep",\ +"zif_stream_get_contents",\ +"php_stdiop_read",\ +"fwrite",\ +"zif_fwrite",\ +"php_stdiop_write",\ +"zif_array_filter",\ "dlopen",\ "zend_load_extension",\ "zend_call_known_instance_method_with_2_params",\ @@ -697,6 +730,8 @@ RUN if [ "$WITH_WS_NETWORKING_PROXY" = "yes" ]; \ "ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_TMPVAR_CONST_HANDLER",\ "ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_TMPVAR_HANDLER",\ "cli",\ +"wasm_sleep",\ +"wasm_php_exec",\ "wasm_sapi_handle_request",\ "_call_user_function_ex",\ "_call_user_function_impl",\ @@ -782,6 +817,7 @@ RUN if [ "$WITH_WS_NETWORKING_PROXY" = "yes" ]; \ "php_cli_server_do_event_for_each_fd_callback",\ "php_cli_server_poller_poll",\ "php_cli_server_recv_event_read_request",\ +"php_exec",\ "php_execute_script",\ "php_fsockopen_stream",\ "php_getimagesize_from_any",\ @@ -880,6 +916,7 @@ RUN if [ "$WITH_WS_NETWORKING_PROXY" = "yes" ]; \ "zif_fclose",\ "zif_feof",\ "zif_file_get_contents",\ +"zif_post_message_to_js",\ "zif_fopen",\ "zif_fread",\ "zif_fsockopen",\ @@ -904,6 +941,15 @@ RUN if [ "$WITH_WS_NETWORKING_PROXY" = "yes" ]; \ "zif_mysqli_stmt_execute",\ "zif_mysqli_stmt_fetch",\ "zif_preg_replace_callback",\ +"zif_popen",\ +"php_exec_ex",\ +"wasm_popen",\ +"zif_wasm_popen",\ +"zif_system",\ +"zif_exec",\ +"zif_passthru",\ +"zif_shell_exec",\ +"zif_proc_open",\ "zif_stream_socket_client",\ "zim_PDOStatement_execute",\ "zim_PDO___construct",\ @@ -959,6 +1005,8 @@ RUN set -euxo pipefail; \ "_phpwasm_destroy_uploaded_files_hash", \n\ "_phpwasm_init_uploaded_files_hash", \n\ "_phpwasm_register_uploaded_file", \n\ +"_emscripten_sleep", \n\ +"_wasm_sleep", \n\ "_wasm_set_phpini_path", \n\ "_wasm_set_phpini_entries", \n\ "_wasm_add_SERVER_entry", \n\