Skip to content

Commit

Permalink
Fix shutdown errors (#1104)
Browse files Browse the repository at this point in the history
Fixes #1078

## What is this PR doing?

If fixes an Asyncify error and memory leak when running PHP shutdown
functions.

## What problem is it solving?

It adds full support for PHP shutdown functions and prevents crashes.

## How is the problem addressed?

By adding Asyncify support for `wasm_sapi_request_shutdown` and
preventing `free` from running if memory wasn't allocated.

## Testing Instructions

- Checkout this branch
- Run dev env `npm run dev`
- [Open this
URL](http://localhost:5400/website-server/?php=8.0&wp=latest&storage=none&networking=yes#{%20%22landingPage%22:%20%22/wp-admin/%22,%20%22features%22:%20{%20%22networking%22:%20true%20},%20%22steps%22:%20[%20{%20%22step%22:%20%22login%22%20},%20{%20%22step%22:%20%22writeFile%22,%20%22path%22:%20%22/wordpress/wp-content/mu-plugins/rewrite.php%22,%20%22data%22:%20%22%3C?php%20add_action(%20'shutdown',%20function()%20{%20post_message_to_js('test');%20}%20);%22%20}%20]%20})
and confirm that WP loads without Asyncify errors and and memory leaks
(check the browser console)
  • Loading branch information
bgrgicak authored Mar 13, 2024
1 parent 1a1c241 commit 30466b1
Show file tree
Hide file tree
Showing 59 changed files with 404 additions and 343 deletions.
2 changes: 2 additions & 0 deletions packages/php-wasm/compile/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ RUN echo -n ' -s ASYNCIFY=1 -s ASYNCIFY_IGNORE_INDIRECT=1 ' >> /root/.emcc-php-w
"get_http_body",\
"wasm_php_exec",\
"wasm_sapi_handle_request",\
"wasm_sapi_request_shutdown",\
"_call_user_function_ex",\
"_call_user_function_impl",\
"_mysqlnd_run_command",\
Expand Down Expand Up @@ -786,6 +787,7 @@ RUN set -euxo pipefail; \
"_wasm_add_ENV_entry", \n\
"_wasm_read", \n\
"_wasm_sapi_handle_request", \n\
"_wasm_sapi_request_shutdown", \n\
"_wasm_set_content_length", \n\
"_wasm_set_content_type", \n\
"_wasm_set_cookies", \n\
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/compile/php/php_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ PHP_FUNCTION(post_message_to_js)

char *response;
size_t response_len = js_module_onMessage(data, &response);
if (response != NULL)
if (response != -1)
{
zend_string *return_string = zend_string_init(response, response_len, 0);
free(response);
Expand Down
6 changes: 3 additions & 3 deletions packages/php-wasm/compile/php/phpwasm-emscripten-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const LibraryExample = {
// stdout, stderr, and headers information are written for the JavaScript
// code to read later on.
FS.mkdir("/internal");

PHPWASM.EventEmitter = ENVIRONMENT_IS_NODE
? require('events').EventEmitter
: class EventEmitter {
Expand Down Expand Up @@ -548,7 +548,7 @@ const LibraryExample = {
return Asyncify.handleSleep((wakeUp) => {
const poll = function () {
if (PHPWASM.child_proc_by_pid[pid]?.exited) {
HEAPU32[exitCodePtr >> 2] =
HEAPU32[exitCodePtr >> 2] =
PHPWASM.child_proc_by_pid[pid].exitCode;
wakeUp(pid);
} else {
Expand Down Expand Up @@ -950,7 +950,7 @@ const LibraryExample = {
// separates JS context from the PHP context so we
// don't let PHP crash here.
console.error(e);
wakeUp(0);
wakeUp(-1);
});
});
}
Expand Down
Binary file modified packages/php-wasm/node/public/7_0_33/php_7_0.wasm
Binary file not shown.
Binary file modified packages/php-wasm/node/public/7_1_30/php_7_1.wasm
Binary file not shown.
Binary file modified packages/php-wasm/node/public/7_2_34/php_7_2.wasm
Binary file not shown.
Binary file modified packages/php-wasm/node/public/7_3_33/php_7_3.wasm
Binary file not shown.
Binary file modified packages/php-wasm/node/public/7_4_33/php_7_4.wasm
Binary file not shown.
Binary file modified packages/php-wasm/node/public/8_0_30/php_8_0.wasm
Binary file not shown.
Binary file modified packages/php-wasm/node/public/8_1_23/php_8_1.wasm
Binary file not shown.
Binary file modified packages/php-wasm/node/public/8_2_10/php_8_2.wasm
Binary file not shown.
Binary file modified packages/php-wasm/node/public/8_3_0/php_8_3.wasm
Binary file not shown.
70 changes: 37 additions & 33 deletions packages/php-wasm/node/public/php_7_0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const dependencyFilename = __dirname + '/7_0_33/php_7_0.wasm';
export { dependencyFilename };
export const dependenciesTotalSize = 11390946;
export const dependenciesTotalSize = 11391244;
export function init(RuntimeName, PHPLoader) {
/**
* Overrides Emscripten's default ExitStatus object which gets
Expand Down Expand Up @@ -5741,7 +5741,7 @@ function _js_module_onMessage(data, bufPtr) {
wakeUp(responseSize);
})).catch((e => {
console.error(e);
wakeUp(0);
wakeUp(-1);
}));
}));
}
Expand Down Expand Up @@ -7194,28 +7194,32 @@ var _wasm_set_request_port = Module["_wasm_set_request_port"] = function() {
return (_wasm_set_request_port = Module["_wasm_set_request_port"] = Module["asm"]["Db"]).apply(null, arguments);
};

var _wasm_sapi_request_shutdown = Module["_wasm_sapi_request_shutdown"] = function() {
return (_wasm_sapi_request_shutdown = Module["_wasm_sapi_request_shutdown"] = Module["asm"]["Eb"]).apply(null, arguments);
};

var _wasm_sapi_handle_request = Module["_wasm_sapi_handle_request"] = function() {
return (_wasm_sapi_handle_request = Module["_wasm_sapi_handle_request"] = Module["asm"]["Eb"]).apply(null, arguments);
return (_wasm_sapi_handle_request = Module["_wasm_sapi_handle_request"] = Module["asm"]["Fb"]).apply(null, arguments);
};

var _php_wasm_init = Module["_php_wasm_init"] = function() {
return (_php_wasm_init = Module["_php_wasm_init"] = Module["asm"]["Fb"]).apply(null, arguments);
return (_php_wasm_init = Module["_php_wasm_init"] = Module["asm"]["Gb"]).apply(null, arguments);
};

var ___funcs_on_exit = function() {
return (___funcs_on_exit = Module["asm"]["Gb"]).apply(null, arguments);
return (___funcs_on_exit = Module["asm"]["Hb"]).apply(null, arguments);
};

var _emscripten_builtin_memalign = function() {
return (_emscripten_builtin_memalign = Module["asm"]["Hb"]).apply(null, arguments);
return (_emscripten_builtin_memalign = Module["asm"]["Ib"]).apply(null, arguments);
};

var __emscripten_timeout = function() {
return (__emscripten_timeout = Module["asm"]["Ib"]).apply(null, arguments);
return (__emscripten_timeout = Module["asm"]["Jb"]).apply(null, arguments);
};

var _setThrew = function() {
return (_setThrew = Module["asm"]["Jb"]).apply(null, arguments);
return (_setThrew = Module["asm"]["Kb"]).apply(null, arguments);
};

var _emscripten_stack_set_limits = function() {
Expand All @@ -7231,103 +7235,103 @@ var _emscripten_stack_get_end = function() {
};

var stackSave = function() {
return (stackSave = Module["asm"]["Kb"]).apply(null, arguments);
return (stackSave = Module["asm"]["Lb"]).apply(null, arguments);
};

var stackRestore = function() {
return (stackRestore = Module["asm"]["Lb"]).apply(null, arguments);
return (stackRestore = Module["asm"]["Mb"]).apply(null, arguments);
};

var stackAlloc = function() {
return (stackAlloc = Module["asm"]["Mb"]).apply(null, arguments);
return (stackAlloc = Module["asm"]["Nb"]).apply(null, arguments);
};

var dynCall_viii = Module["dynCall_viii"] = function() {
return (dynCall_viii = Module["dynCall_viii"] = Module["asm"]["Nb"]).apply(null, arguments);
return (dynCall_viii = Module["dynCall_viii"] = Module["asm"]["Ob"]).apply(null, arguments);
};

var dynCall_vii = Module["dynCall_vii"] = function() {
return (dynCall_vii = Module["dynCall_vii"] = Module["asm"]["Ob"]).apply(null, arguments);
return (dynCall_vii = Module["dynCall_vii"] = Module["asm"]["Pb"]).apply(null, arguments);
};

var dynCall_vi = Module["dynCall_vi"] = function() {
return (dynCall_vi = Module["dynCall_vi"] = Module["asm"]["Pb"]).apply(null, arguments);
return (dynCall_vi = Module["dynCall_vi"] = Module["asm"]["Qb"]).apply(null, arguments);
};

var dynCall_iii = Module["dynCall_iii"] = function() {
return (dynCall_iii = Module["dynCall_iii"] = Module["asm"]["Qb"]).apply(null, arguments);
return (dynCall_iii = Module["dynCall_iii"] = Module["asm"]["Rb"]).apply(null, arguments);
};

var dynCall_ii = Module["dynCall_ii"] = function() {
return (dynCall_ii = Module["dynCall_ii"] = Module["asm"]["Rb"]).apply(null, arguments);
return (dynCall_ii = Module["dynCall_ii"] = Module["asm"]["Sb"]).apply(null, arguments);
};

var dynCall_v = Module["dynCall_v"] = function() {
return (dynCall_v = Module["dynCall_v"] = Module["asm"]["Sb"]).apply(null, arguments);
return (dynCall_v = Module["dynCall_v"] = Module["asm"]["Tb"]).apply(null, arguments);
};

var dynCall_iiii = Module["dynCall_iiii"] = function() {
return (dynCall_iiii = Module["dynCall_iiii"] = Module["asm"]["Tb"]).apply(null, arguments);
return (dynCall_iiii = Module["dynCall_iiii"] = Module["asm"]["Ub"]).apply(null, arguments);
};

var dynCall_viiiii = Module["dynCall_viiiii"] = function() {
return (dynCall_viiiii = Module["dynCall_viiiii"] = Module["asm"]["Ub"]).apply(null, arguments);
return (dynCall_viiiii = Module["dynCall_viiiii"] = Module["asm"]["Vb"]).apply(null, arguments);
};

var dynCall_iiiii = Module["dynCall_iiiii"] = function() {
return (dynCall_iiiii = Module["dynCall_iiiii"] = Module["asm"]["Vb"]).apply(null, arguments);
return (dynCall_iiiii = Module["dynCall_iiiii"] = Module["asm"]["Wb"]).apply(null, arguments);
};

var dynCall_iiiiiii = Module["dynCall_iiiiiii"] = function() {
return (dynCall_iiiiiii = Module["dynCall_iiiiiii"] = Module["asm"]["Wb"]).apply(null, arguments);
return (dynCall_iiiiiii = Module["dynCall_iiiiiii"] = Module["asm"]["Xb"]).apply(null, arguments);
};

var dynCall_iiiiii = Module["dynCall_iiiiii"] = function() {
return (dynCall_iiiiii = Module["dynCall_iiiiii"] = Module["asm"]["Xb"]).apply(null, arguments);
return (dynCall_iiiiii = Module["dynCall_iiiiii"] = Module["asm"]["Yb"]).apply(null, arguments);
};

var dynCall_i = Module["dynCall_i"] = function() {
return (dynCall_i = Module["dynCall_i"] = Module["asm"]["Yb"]).apply(null, arguments);
return (dynCall_i = Module["dynCall_i"] = Module["asm"]["Zb"]).apply(null, arguments);
};

var dynCall_viiii = Module["dynCall_viiii"] = function() {
return (dynCall_viiii = Module["dynCall_viiii"] = Module["asm"]["Zb"]).apply(null, arguments);
return (dynCall_viiii = Module["dynCall_viiii"] = Module["asm"]["_b"]).apply(null, arguments);
};

var dynCall_iiiiiiiii = Module["dynCall_iiiiiiiii"] = function() {
return (dynCall_iiiiiiiii = Module["dynCall_iiiiiiiii"] = Module["asm"]["_b"]).apply(null, arguments);
return (dynCall_iiiiiiiii = Module["dynCall_iiiiiiiii"] = Module["asm"]["$b"]).apply(null, arguments);
};

var dynCall_viiiiiiiii = Module["dynCall_viiiiiiiii"] = function() {
return (dynCall_viiiiiiiii = Module["dynCall_viiiiiiiii"] = Module["asm"]["$b"]).apply(null, arguments);
return (dynCall_viiiiiiiii = Module["dynCall_viiiiiiiii"] = Module["asm"]["ac"]).apply(null, arguments);
};

var dynCall_viiiiii = Module["dynCall_viiiiii"] = function() {
return (dynCall_viiiiii = Module["dynCall_viiiiii"] = Module["asm"]["ac"]).apply(null, arguments);
return (dynCall_viiiiii = Module["dynCall_viiiiii"] = Module["asm"]["bc"]).apply(null, arguments);
};

var dynCall_iiiiiiii = Module["dynCall_iiiiiiii"] = function() {
return (dynCall_iiiiiiii = Module["dynCall_iiiiiiii"] = Module["asm"]["bc"]).apply(null, arguments);
return (dynCall_iiiiiiii = Module["dynCall_iiiiiiii"] = Module["asm"]["cc"]).apply(null, arguments);
};

var dynCall_iiiiiiiiii = Module["dynCall_iiiiiiiiii"] = function() {
return (dynCall_iiiiiiiiii = Module["dynCall_iiiiiiiiii"] = Module["asm"]["cc"]).apply(null, arguments);
return (dynCall_iiiiiiiiii = Module["dynCall_iiiiiiiiii"] = Module["asm"]["dc"]).apply(null, arguments);
};

var _asyncify_start_unwind = function() {
return (_asyncify_start_unwind = Module["asm"]["dc"]).apply(null, arguments);
return (_asyncify_start_unwind = Module["asm"]["ec"]).apply(null, arguments);
};

var _asyncify_stop_unwind = function() {
return (_asyncify_stop_unwind = Module["asm"]["ec"]).apply(null, arguments);
return (_asyncify_stop_unwind = Module["asm"]["fc"]).apply(null, arguments);
};

var _asyncify_start_rewind = function() {
return (_asyncify_start_rewind = Module["asm"]["fc"]).apply(null, arguments);
return (_asyncify_start_rewind = Module["asm"]["gc"]).apply(null, arguments);
};

var _asyncify_stop_rewind = function() {
return (_asyncify_stop_rewind = Module["asm"]["gc"]).apply(null, arguments);
return (_asyncify_stop_rewind = Module["asm"]["hc"]).apply(null, arguments);
};

function invoke_viii(index, a1, a2, a3) {
Expand Down
Loading

0 comments on commit 30466b1

Please sign in to comment.