Skip to content
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

PHP.wasm: fix stack overflow in wasm_set_request_body #993

Merged
merged 4 commits into from
Feb 2, 2024

Conversation

adamziel
Copy link
Collaborator

@adamziel adamziel commented Feb 2, 2024

Before this PR, requests with large body trigger a stack overflow error. For example, issue #816 describes how clicking a "Use template" button calls wasm_set_request_body with a body string that's 153KB large which is too much for the current WASM build.

This PR fixes the issue by allocating the request body on the heap, not on the stack.

Implementation details

BasePHP.setRequestBody allocates enough heap memory to store the body string. Then, it converts the body string into bytes and writes it into heap. Once the request is finished, BasePHP.run() frees that memory.

The conversion to bytes is done via the stringToUTF8 function provided by Emscripten. This is just as it was before this PR. This method isn't perfect and will break for some inputs. The setRequestBody method should just accept a UInt8Array instead of a string, but that's outside of scope of this PR.

Testing instructions

Confirm the CI checks are green. This PR ships with a series of checks to confirm the crash is fixed.

For manual testing, navigate to https://beemovie.fandom.com/wiki/Bee_Movie/Transcript and Select All, then Copy the selection.

Then go to http://localhost:5400/website-server/?php=8.3&wp=6.4&storage=none&url=/wp-admin/post-new.php and paste the content of the clipboard directly into the editor. The javascript will take a moment to digest the pasted content into blocks. The editor should end up creating very many blocks. Once thats done, click publish, and the post should save without crashing.

Supersedes #870

Before this PR, requests with large body trigger a stack overflow error.
For example, issue #816 describes how clicking a "Use template" button
calls wasm_set_request_body with a body string that's 153KB large which
is too much for the current WASM build.

This PR fixes the issue by allocating the request body on the heap, not
on the stack.

 ## Implementation details

`BasePHP.setRequestBody` converts the body string into bytes using
the `stringToUTF8` function provided by Emscripten. This is just as
it was before this PR. This method isn't perfect and will break for
some inputs. The `setRequestBody` method should just accept a `UInt8Array`
instead of a string, but that's outside of scope of this PR.

 ## Testing instructions

Confirm the CI checks are green. This PR ships with a series of checks
to confirm the crash is fixed.

For manual testing, navigate to https://beemovie.fandom.com/wiki/Bee_Movie/Transcript and Select All, then Copy the selection.

Then go to http://localhost:5400/website-server/?php=8.3&wp=6.4&storage=none&url=/wp-admin/post-new.php
and paste the content of the clipboard directly into the editor. The javascript will take a moment to digest
the pasted content into blocks. The editor should end up creating very many blocks. Once thats done, click
publish, and the post should save without crashing.
@adamziel adamziel added [Type] Bug An existing feature does not function as intended [Feature] PHP.wasm labels Feb 2, 2024
@adamziel
Copy link
Collaborator Author

adamziel commented Feb 2, 2024

The failing test is flaky – it timed out. All the important checks worked. Let's merge and address the flaky tests separately.

@adamziel adamziel merged commit adabb8c into trunk Feb 2, 2024
4 of 5 checks passed
@adamziel adamziel deleted the request-body-memory-leak branch February 2, 2024 14:09
@adamziel adamziel mentioned this pull request Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] PHP.wasm [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant