Skip to content

Conversation

@peterhuene
Copy link
Member

This PR fixes the Rust bindings generated for lifting and lowering of lists
to account for lists of zero-length.

It is undefined behavior to call std::alloc::alloc with a zero-sized layout.

Additionally, as the behavior of the canonical_abi_realloc implemented in
wit-bindgen is to return a non-null dummy pointer for a zero-sized alloc, the
bindings were calling std::alloc::dealloc with a pointer that was not
allocated with std::alloc::alloc (also undefined behavior).

To fix this, the bindings now account for zero-length lists and skip allocation
and deallocation where appropriate.

This commit fixes the Rust bindings generated for lifting and lowering of lists
to account for lists of zero-length.

It is undefined behavior to call `std::alloc::alloc` with a zero-sized layout.

Additionally, as the behavior of the `canonical_abi_realloc` implemented in
`wit-bindgen` is to return a non-null dummy pointer for a zero-sized alloc, the
bindings were calling `std::alloc::dealloc` with a pointer that was not
allocated with `std::alloc::alloc` (also undefined behavior).

To fix this, the bindings now account for zero-length lists and skip allocation
and deallocation where appropriate.
@peterhuene peterhuene requested a review from alexcrichton August 3, 2022 19:37
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add some tests exercising this as well which try to transfer empty lists or nonempty lists of zero-sized types? (also perhaps empty strings?)

@peterhuene peterhuene merged commit f7f95ba into bytecodealliance:main Aug 4, 2022
@peterhuene peterhuene deleted the fix-zero-sized-lists branch August 4, 2022 00:17
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