You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I expected Miri to sensibly report some UB, but instead I got the following:
error: Undefined Behavior: out-of-bounds `offset_from` origin: expected a pointer to the end of 4 bytes of memory, but got alloc4 which is at the beginning of the allocation
--> src/main.rs:3:9
|
3 | (&1_u8 as *const u8).offset_from(&2_u8);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds `offset_from` origin: expected a pointer to the end of 4 bytes of memory, but got alloc4 which is at the beginning of the allocation
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
= note: BACKTRACE:
= note: inside `main` at src/main.rs:3:9: 3:48
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
The error message doesn't make any sense. I don't know where Miri got the "4 bytes of memory" expectation from. So I think the error is incorrect.
Reproducible on the playground with rust version 1.86.0-nightly (2025-01-20 f3d1d47fd84dfcf7f513)
The text was updated successfully, but these errors were encountered:
error: Undefined Behavior: out-of-bounds `offset_from` origin: expected a pointer to the end of 51524 bytes of memory, but got alloc10 which is at the beginning of the allocation
--> src/main.rs:5:9
|
5 | (x as *const u8).offset_from(&2_u8);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds `offset_from` origin: expected a pointer to the end of 51524 bytes of memory, but got alloc10 which is at the beginning of the allocation
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
= note: BACKTRACE:
= note: inside `main` at src/main.rs:5:9: 5:44
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Note the "expected a pointer to the end of 51524 bytes of memory" in the error. I think this indicates that something has gone very wrong inside miri.
The error is confusing but not fundamentally incorrect: the nicest way to check "are these two pointers in the same allocation" that also accounts for edge cases like identical pointers is to check "is the memory range between them dereferenceable from both pointers". That's what Miri is doing, and it's where the "4 bytes of memory" expectation comes from: the two pointers happen to be 4 bytes apart in that case.
I still think that this is the nicest implementation strategy, but we should probably find a way to make the error less confusing.
I ran the following code with Miri:
I expected Miri to sensibly report some UB, but instead I got the following:
The error message doesn't make any sense. I don't know where Miri got the "4 bytes of memory" expectation from. So I think the error is incorrect.
Reproducible on the playground with rust version
1.86.0-nightly (2025-01-20 f3d1d47fd84dfcf7f513)
The text was updated successfully, but these errors were encountered: