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

Detect (non-raw) borrows of null ZST pointers in CheckNull #136601

Merged
merged 2 commits into from
Feb 9, 2025

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Feb 5, 2025

Fixes #136568. Ensures that we check that borrows of derefs are non-null in the CheckNull pass even if it's a ZST pointee.

I'm actually surprised that this is UB in Miri, but if it's certainly UB, then this PR modifies the null check to be stricter. I couldn't find anywhere in https://doc.rust-lang.org/reference/behavior-considered-undefined.html that discusses this case specifically, but I didn't read it too closely, or perhaps it's just missing a bullet point.

On the contrary, if this is actually erroneous UB in Miri, then I'm happy to close this (and perhaps fix the null check in Miri to exclude ZSTs?)

On the double contrary, if this is still an "open question", I'm also happy to close this and wait for a decision to be made.

r? @saethlin cc @RalfJung (perhaps you feel strongly about this change)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 5, 2025
@rustbot
Copy link
Collaborator

rustbot commented Feb 5, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@theemathas
Copy link
Contributor

theemathas commented Feb 5, 2025

I think this is definitely UB, because null references, even of ZSTs, already currently cause "unexpected" behavior.

For example, this code prints None, even in debug mode:

fn main() {
    let null_ref = unsafe { &*std::ptr::null::<()>() };
    println!("{:?}", Some(null_ref));
}

This code panics in debug mode, but prints None () in release mode.

fn main() {
    let null_ref = unsafe { &*std::ptr::null::<()>() };
    let x = Some(std::hint::black_box(null_ref));
    let y = x.unwrap();
    println!("{x:?} {y:?}");
}

@compiler-errors
Copy link
Member Author

Oh, that is a good point. I forgot that null references of ZSTs are definitely UB bc of the niche.

@theemathas

This comment was marked as resolved.

@RalfJung
Copy link
Member

RalfJung commented Feb 6, 2025

References are non-null (validity/language invariant), and "creating an invalid value" is insta-UB. So yes, &*ptr::null::<T>() is UB for every T.

@saethlin
Copy link
Member

saethlin commented Feb 7, 2025

r=me with comments tweaked ^

@saethlin saethlin added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 7, 2025
@rustbot
Copy link
Collaborator

rustbot commented Feb 8, 2025

This PR changes Stable MIR

cc @oli-obk, @celinval, @ouz-a

@compiler-errors
Copy link
Member Author

I've also fixed the typo of occured -> occurred.

@bors r=saethlin

@bors
Copy link
Contributor

bors commented Feb 8, 2025

📌 Commit a61537f has been approved by saethlin

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 8, 2025
Urgau added a commit to Urgau/rust that referenced this pull request Feb 8, 2025
…=saethlin

Detect (non-raw) borrows of null ZST pointers in CheckNull

Fixes rust-lang#136568. Ensures that we check that borrows of derefs are non-null in the `CheckNull` pass **even if** it's a ZST pointee.

I'm actually surprised that this is UB in Miri, but if it's certainly UB, then this PR modifies the null check to be stricter. I couldn't find anywhere in https://doc.rust-lang.org/reference/behavior-considered-undefined.html that discusses this case specifically, but I didn't read it too closely, or perhaps it's just missing a bullet point.

On the contrary, if this is actually erroneous UB in Miri, then I'm happy to close this (and perhaps fix the null check in Miri to exclude ZSTs?)

On the double contrary, if this is still an "open question", I'm also happy to close this and wait for a decision to be made.

r? `@saethlin` cc `@RalfJung` (perhaps you feel strongly about this change)
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 9, 2025
Rollup of 5 pull requests

Successful merges:

 - rust-lang#134679 (Windows: remove readonly files)
 - rust-lang#136213 (Allow Rust to use a number of libc filesystem calls)
 - rust-lang#136530 (Implement `x perf` directly in bootstrap)
 - rust-lang#136601 (Detect (non-raw) borrows of null ZST pointers in CheckNull)
 - rust-lang#136659 (Pick the max DWARF version when LTO'ing modules with different versions )

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 9, 2025
Rollup of 5 pull requests

Successful merges:

 - rust-lang#134679 (Windows: remove readonly files)
 - rust-lang#136213 (Allow Rust to use a number of libc filesystem calls)
 - rust-lang#136530 (Implement `x perf` directly in bootstrap)
 - rust-lang#136601 (Detect (non-raw) borrows of null ZST pointers in CheckNull)
 - rust-lang#136659 (Pick the max DWARF version when LTO'ing modules with different versions )

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 9, 2025
Rollup of 5 pull requests

Successful merges:

 - rust-lang#134679 (Windows: remove readonly files)
 - rust-lang#136213 (Allow Rust to use a number of libc filesystem calls)
 - rust-lang#136530 (Implement `x perf` directly in bootstrap)
 - rust-lang#136601 (Detect (non-raw) borrows of null ZST pointers in CheckNull)
 - rust-lang#136659 (Pick the max DWARF version when LTO'ing modules with different versions )

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e5bc12e into rust-lang:master Feb 9, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Feb 9, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 9, 2025
Rollup merge of rust-lang#136601 - compiler-errors:borrow-null-zst, r=saethlin

Detect (non-raw) borrows of null ZST pointers in CheckNull

Fixes rust-lang#136568. Ensures that we check that borrows of derefs are non-null in the `CheckNull` pass **even if** it's a ZST pointee.

I'm actually surprised that this is UB in Miri, but if it's certainly UB, then this PR modifies the null check to be stricter. I couldn't find anywhere in https://doc.rust-lang.org/reference/behavior-considered-undefined.html that discusses this case specifically, but I didn't read it too closely, or perhaps it's just missing a bullet point.

On the contrary, if this is actually erroneous UB in Miri, then I'm happy to close this (and perhaps fix the null check in Miri to exclude ZSTs?)

On the double contrary, if this is still an "open question", I'm also happy to close this and wait for a decision to be made.

r? ``@saethlin`` cc ``@RalfJung`` (perhaps you feel strongly about this change)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Null ptr check doesn't catch null reference to ZSTs
6 participants