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

Confusing Miri error when byte_offset_from is used on two different invalid pointers. #3104

Closed
asquared31415 opened this issue Oct 4, 2023 · 2 comments · Fixed by rust-lang/rust#124923
Labels
A-diagnostics errors and warnings emitted by miri C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement

Comments

@asquared31415
Copy link
Contributor

asquared31415 commented Oct 4, 2023

The following code causes Miri to emit an error that I think does not properly describe the issue.

#![feature(strict_provenance)]
#![feature(pointer_byte_offsets)]
use core::ptr;

fn main() {
    unsafe {
        let base = ptr::without_provenance::<()>(10);
        let unit = &*base;
        let p1 = unit as *const ();

        let base = ptr::without_provenance::<()>(11);
        let unit = &*base;
        let p2 = unit as *const ();

        // Seems to work because they are same pointer
        // even though it's dangling.
        let _ = p1.byte_offset_from(p1);

        // UB because different allocations, but reports
        // the error being that p1 is dangling.
        let _ = p1.byte_offset_from(p2);
    }
}
error: Undefined Behavior: out-of-bounds `offset_from`: 0xa[noalloc] is a dangling pointer (it has no provenance)
  --> src/main.rs:21:17
   |
21 |         let _ = p1.byte_offset_from(p2);
   |                 ^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds `offset_from`: 0xa[noalloc] is a dangling pointer (it has no provenance)
   |
   = 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:21:17: 21:40

It is confusing that Miri seems to complain about the pointer being dangling, since it does not complain when a dangling pointer is offset from itself. I believe the true issue is that they are not pointers to the same object, but the fact that they're not really pointers to objects at all makes the error less useful. Additionally, Miri is seemingly fine with the code if both of the pointers are invalid with the same integer and emits error: Undefined Behavior: `ptr_offset_from` called on pointers into different allocations if only one pointer is invalid and the other is a pointer to an object (including freed).

I believe that this error should be the normal "offset_from called on pointers to different allocations" error. (perhaps with an additional note saying that the inputs are both invalid?)

@RalfJung
Copy link
Member

RalfJung commented Oct 4, 2023

Thanks for your feedback! We should indeed improve this message.

Miri is seemingly fine with the code if both of the pointers are invalid with the same integer

For the purpose of p1.byte_offset_from(p1), we consider there to be a zero-sized object living at every (non-zero) address. That's why that call is allowed.

I believe that this error should be the normal "offset_from called on pointers to different allocations" error. (perhaps with an additional note saying that the inputs are both invalid?)

I feel like saying "pointers to different allocations" could be confusing if they are not pointers to any allocation.

@RalfJung RalfJung added C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement A-diagnostics errors and warnings emitted by miri labels Apr 18, 2024
@RalfJung

This comment was marked as outdated.

rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 9, 2024
Rollup merge of rust-lang#124923 - RalfJung:offset-from-errors, r=compiler-errors

interpret/miri: better errors on failing offset_from

Fixes rust-lang/miri#3104
github-actions bot pushed a commit that referenced this issue May 11, 2024
interpret/miri: better errors on failing offset_from

Fixes #3104
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics errors and warnings emitted by miri C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants