-
Notifications
You must be signed in to change notification settings - Fork 353
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
ReadPointerAsBytes error in ::std::str::from_utf8() #190
Comments
I'm guessing the error gets triggered at this line: https://github.com/rust-lang/rust/blob/f09576c4a41727a8d10bbfd8fd3fb2e10e1be3b3/src/libcore/str/mod.rs#L1433 |
Indeed, this program hits a similar error: // test_ptr.rs
fn main() {
let x: &[u8] = &[1,2,3,4,5];
let ptr = x.as_ptr();
let _align = (ptr as usize) & (::std::mem::size_of::<usize>() - 1);
}
|
Yea... that's a known limitation. We can make it work by making such computations work on the pointer offset + the allocation alignment. It would be deterministic, but different from what real world code is doing. Unfortunately you still couldn't read u16 even after fixing the alignment of the pointer, because in miri the allocation makes the alignment... @eddyb do you think it would be OK to support reading u16 from an alloc with u8 alignment if the pointer is aligned? We can simulate maximal misalignment by making every allocation bigger than requested and offsetting its inner value by the alignment |
I am against doing anything of the sort - it's possible to support modifying bits under the alignment without having different semantics from runtime, but that won't make this code work. Don't think there's much we can do here, that's not SMT-ish. |
What we could do is add an intrinsic to rustc: |
I opened an issue on the rustc repo: rust-lang/rust#42561 |
I gather that the problem here is that the execution under miri could diverge from the execution under rustc. @oli-obk, it seems to me that your proposed intrinsic has the same problem. E.g. with it, miri would never execute this branch. |
That branch is just an optimization as I understand the code. |
Oh, I see, the way it is done you can pretend every byte is unaligned and it will use the outer loop. |
So all of this should "just work" if/when miri gets an implementation of the intptrcast-model, right? Or is the plan to support some of this even when doing CTFE? |
I'd like to support some of it in ctfe. But this issue will be solved by the rfc I opened and then trivially work and improve many homebrewn alignment optimizations. |
Ah, I see. Makes sense. |
The We could make it smarter, but there's no real need to. |
The text was updated successfully, but these errors were encountered: