-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
cast_ptr_alignment is acceptable in a narrow set of contexts #2881
Comments
Example from something I'm working on:
which is obviously fine. It seems appropriate to at least mention this in the 'Known problems' section of cast_ptr_alignment. |
I've only ever seen
and also
|
This also breaks in the context of allocation functions - |
Malloc returns pointers that aligned to size of the allocation (rounding upto the next highest power of 2) or sufficiently large enough max alignment (often 16 bytes). Most allocators have a minimum alignment as well. Rust's default allocator jemalloc has a minimum alignment of 8 bytes by default. |
@Cocalus oh, also, to be clear, the only functions in rust which can deal with unaligned pointers are those that end in |
If that's true then the documentation is wrong, or the name should be fixed. From https://doc.rust-lang.org/beta/std/ptr/fn.copy_nonoverlapping.html
Memcpy just move bytes from one place to another. And is generally the standards correct way to realign data in C. I'm actively using it in unsafe code to fix the alignment, of a few types. So If that's wrong what is the correct way of copying a bunch of bytes to a particular alignment. |
@Cocalus note: https://doc.rust-lang.org/nightly/std/intrinsics/fn.copy_nonoverlapping.html#safety The correct way is to either 1) use |
I guess this has been a learning experience, so this is likely a good warning. I'd like to do the following:
are people okay with this? |
Thanks, the alignment requirement has been added to the nightly docs since I last checked. I think just casting both sides to *u8 and doing the sizeof calculation by hand should work for my needs. Adding the note that there are some functions that support unaligned is a definite improvement. If clippy can't white list the functions directly, then listing some/all of them should be helpful. I wonder if the pointer types could be extended to have a unaligned type. Then there could be "safe" casts that verify alignment. For my needs I still want the alignment check in general, since it's easy to mess up in unsafe code. I've been adding allow(cast_ptr_alignment) to the individual calls that I hand verify. Since I don't allow clippy warnings the change from error to warning doesn't really affect my workflow. I'm fine leaving it as an error with a description of the rare exceptions, since it seems far more likely to misused. But I don't know what clippy's standards are for that. |
+1 for recognizing the specific pattern of |
cast_ptr_alignment: Mention legal use under known problems Refs #2881. changelog: Mention know problems for cast_ptr_alignment
A note about the |
Same here. I've discovered it because I had a real bug (yay!), but the warning didn't go away when I used |
Clippy (rightful) complained that we read from a potential unaligned pointer, which would be undefined behaviour. The problem is: Clippy continues to complain about this, even if we use `read_unaligned()` here. This seems to be a clippy bug, see rust-lang/rust-clippy#2881
Clippy (rightful) complained that we read from a potential unaligned pointer, which would be undefined behaviour. The problem is: Clippy continues to complain about this, even if we use `read_unaligned()` here. This seems to be a clippy bug, see rust-lang/rust-clippy#2881
Clippy (rightful) complained that we read from a potential unaligned pointer, which would be undefined behaviour. The problem is: Clippy continues to complain about this, even if we use `read_unaligned()` here. This seems to be a clippy bug, see rust-lang/rust-clippy#2881
Clippy may not be able to see when this is acceptable but if so there are several functions that can handle unaligned ptrs. If the unaligned pointer is only used as an input to those then it ideally wouldn't be flagged.
The obvious
std::ptr::read_unaligned
std::ptr::write_unaligned
Anything with memcpy or memmove semantics
std::ptr::copy
std::ptr::copy_nonoverlapping
I'm unsure about std::ptr::swap and std::ptr::swap_nonoverlapping
And in
std::arch::x86_64
std::arch::x86
storeu
loadu
lddqu
There might be some others.
The text was updated successfully, but these errors were encountered: