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

Try to repro memory out of bounds errors in CI #1198

Conversation

brandonpayton
Copy link
Member

This is a test PR to hopefully reproduce memory access out of bounds errors with #1189.

So far, @adamziel has only encountered errors in CI (see #1194), but there was one error report in WP.org Slack:
https://wordpress.slack.com/archives/C06Q5DCKZ3L/p1712232849960669

Adds custom PHP memory storage handlers to stop PHP from
attempting to partial unmap memory via the `munmap()` function.
Emscripten allocators do not support using `munmap()` to partially unmap
memory. They fail for partial unmaps, and when attempts fail, memory is
leaked because PHP thinks the memory is free while Emscripten libs
consider it allocated.

The custom memory storage handlers take over responsibility for
allocating and freeing aligned memory chunks. The handlers use
`posix_memalign()` and `free()` instead of `mmap()` and `munmap()`, and
this appears to resolve the problem.

The handlers are added as part of a PHP extension, which is not perfect
because allocation can happen before the extension is initialized. But
the amount of allocation before the workaround is applied should be
quite small.

## Testing Instructions

Before applying this patch:
Run [this test
script](https://gist.github.com/brandonpayton/934d96d76b96557e94ad6983aaffa09f)
and verify that it OOMs.

After applying this patch:
Rerun the test script and verify that it completes after 1000
interations.

## Remaining work

- [x] Compare performance with php-wasm builds prior to this patch and
reconsider if 20% slower.
- [x] Find out whether it is sufficient to add the workaround during the
PHP module init phase rather than during each request init
- [x] Address minor TODO comments in wasm_memory_storage.c
- [x] See if we can run the test script as part of tests under
`packages/php-wasm/node/src/test`
@brandonpayton brandonpayton added [Type] Bug An existing feature does not function as intended [Priority] High [Feature] PHP.wasm labels Apr 4, 2024
@brandonpayton brandonpayton self-assigned this Apr 4, 2024
@brandonpayton
Copy link
Member Author

Hmm... I think testing CI may require a branch in this repo. Creating a duplicate PR to test that theory...

@brandonpayton
Copy link
Member Author

Hmm... I think testing CI may require a branch in this repo. Creating a duplicate PR to test that theory...

It looks like that is the case. Closing this PR which is based on a branch in my personal fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] PHP.wasm [Priority] High [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant