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

fix(api): Check if pointer is aligned before reading #5449

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

xdoardo
Copy link
Collaborator

@xdoardo xdoardo commented Mar 3, 2025

As per title. Fixes #5444

@xdoardo xdoardo requested a review from syrusakbary as a code owner March 3, 2025 16:53
@xdoardo xdoardo linked an issue Mar 3, 2025 that may be closed by this pull request
@xdoardo xdoardo requested a review from Arshia001 March 3, 2025 16:53
Copy link

promptless bot commented Mar 3, 2025

✅ No documentation updates required.

Comment on lines +109 to +113
let buf_ptr = out.as_mut_ptr() as *mut u8;
if !buf_ptr.is_aligned() {
return Err(MemoryAccessError::UnalignedPointer);
}
let buf = unsafe { slice::from_raw_parts_mut(buf_ptr, mem::size_of::<T>()) };
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't .is_aligned() always going to return true due to this?

MaybeUninit is guaranteed to have the same size, alignment, and ABI as T:

Copy link
Member

Choose a reason for hiding this comment

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

Depends... These guarantees only hold as long as we don't have unsafe code, and we do have a lot of unsafe code near the syscall boundaries. Still, @xdoardo would know better.

Copy link
Member

Choose a reason for hiding this comment

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

But also, since we're casting to *mut u8... Let's add a test?

Comment on lines +109 to +113
let buf_ptr = out.as_mut_ptr() as *mut u8;
if !buf_ptr.is_aligned() {
return Err(MemoryAccessError::UnalignedPointer);
}
let buf = unsafe { slice::from_raw_parts_mut(buf_ptr, mem::size_of::<T>()) };
Copy link
Member

Choose a reason for hiding this comment

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

But also, since we're casting to *mut u8... Let's add a test?

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.

Check WasmPtr alignment before reading from it
4 participants