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

Allow alignments that eat the remaining allocation #9356

Merged

Conversation

elliottt
Copy link
Member

@elliottt elliottt commented Oct 1, 2024

The preview1 adapter has an implementation of cabi_realloc that facilitates calling component imports that return dynamically allocated results, like lists and strings. However, when returning a zero-length value to a buffer that's been allocated with zero bytes available, a trap will occur. This is because the alignment check requires that there is space left over after alignment has been added to the base pointer, which in the case of an empty buffer, will not be possible.

This PR fixes the issue by relaxing the alignment check to require that the amount required to align the base does not exceed the length of the buffer, but may consume the entire allocation.

co-authored-by: Dan Gohman <dev@sunfishcode.online>
@elliottt elliottt requested a review from a team as a code owner October 1, 2024 21:10
@elliottt elliottt requested review from fitzgen and alexcrichton and removed request for a team October 1, 2024 21:10
@alexcrichton
Copy link
Member

Would it be possible to add a test for this as well? Or is that out-of-scope in the WASI APIs we have bound?

@elliottt elliottt force-pushed the trevor/null-allocation-adapter branch from 2a98e20 to 58a57b6 Compare October 1, 2024 23:48
@elliottt
Copy link
Member Author

elliottt commented Oct 1, 2024

Would it be possible to add a test for this as well? Or is that out-of-scope in the WASI APIs we have bound?

The test added fails when the change to the align_to function is reverted, triggering the original issue in the adapter.

@elliottt elliottt added this pull request to the merge queue Oct 1, 2024
Merged via the queue into bytecodealliance:main with commit 4712420 Oct 2, 2024
39 checks passed
@elliottt elliottt deleted the trevor/null-allocation-adapter branch October 2, 2024 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants