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

Alignment Woes and False Positives #1449

Closed
cmazakas opened this issue Jun 13, 2020 · 5 comments
Closed

Alignment Woes and False Positives #1449

cmazakas opened this issue Jun 13, 2020 · 5 comments
Labels
C-support Category: Not necessarily a bug, but someone asking for support

Comments

@cmazakas
Copy link

Miri seems to have significant trouble when operating with raw memory and users constructing objects in place.

Simply consider some slightly modified code from the stdlib documentation:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c29624c1b97ae8f0730d5f41816f3143

Miri also seems to needlessly complain about accessing 1 byte aligned data as a type with higher alignment. To me, this is the entire point of what I'm doing.

Is there an idiomatic way of conveying to Miri that I know what I'm doing and it doesn't need to complain (aside from just disabling alignment checking altogether)?

@RalfJung RalfJung added the C-support Category: Not necessarily a bug, but someone asking for support label Jun 14, 2020
@RalfJung
Copy link
Member

The code in that playground has UB, so this is Miri doing its job.

Or is your problem about align_offset (commented out in the playground) not working as it does at run-time? See #1074 for that.

@cmazakas
Copy link
Author

What's UB about it?

Fwiw, I ripped that code sample for the stdlib docs so maybe a PR is in order.

@bjorn3
Copy link
Member

bjorn3 commented Jun 14, 2020

Let's assume that ptr is 2. Then u16_ptr is 3. This is not aligned to 2 as required by dereferencing a pointer to a u16. If offset was ptr.align_offset(align_of::<u16>()), then the offset would be 0 when the ptr was already correctly aligned for a u16. If ptr wasn't, then offset would be either the minimum value required to correctly align ptr to 2 or usize::max_value(). For miri align_offset() returns usize::max_value() due to #1074.

If you want to read from unaligned memory, you have to use std::ptr::read_unaligned.

@cmazakas
Copy link
Author

Oh, that.

Yeah, that's probably UB in most cases. That was the offset I was getting during normal runtime so I just hard-coded it into the playground link. "Real code" would have align_offset just work or manually round up to the next aligned.

Or is your problem about align_offset (commented out in the playground) not working as it does at run-time? See #1074 for that.

I was caught off guard by this, yeah.

@RalfJung
Copy link
Member

"Real code" would have align_offset just work or manually round up to the next aligned.

Yeah, Miri's align_offset is strange. It's spec-conforming, but still surprising.

I think resolving #1074 will resolve your confusion though, so closing as a duplicate. Let me know if there's anything beyond #1074 that you ran into here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-support Category: Not necessarily a bug, but someone asking for support
Projects
None yet
Development

No branches or pull requests

3 participants