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

const-eval: detect more pointers as definitely not-null #133700

Merged
merged 1 commit into from
Jan 18, 2025

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Dec 1, 2024

This fixes #133523 by making the scalar_may_be_null check smarter: for instance, an odd offset in any 2-aligned allocation can never be null, even if it is out-of-bounds.

More generally, if an allocation with unknown base address B is aligned to alignment N, and a pointer is at offset X inside that allocation, then we know that (B + X) mod N = B mod N + X mod N = X mod N. Since 0 mod N is definitely 0, if we learn that X mod N is not 0 we can deduce that B + X is not 0.

This is immediately visible on stable, via ptr.is_null() (and, more subtly, by not raising a UB error when such a pointer is used somewhere that a non-null pointer is required). Therefore nominating for @rust-lang/lang.

@rustbot
Copy link
Collaborator

rustbot commented Dec 1, 2024

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Dec 1, 2024
@rustbot
Copy link
Collaborator

rustbot commented Dec 1, 2024

Some changes occurred to the CTFE machinery

cc @rust-lang/wg-const-eval

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

@RalfJung RalfJung added T-lang Relevant to the language team, which will review and decide on the PR/issue. I-lang-nominated Nominated for discussion during a lang team meeting. labels Dec 1, 2024
@RalfJung
Copy link
Member Author

RalfJung commented Dec 2, 2024

r? @lcnr

@rustbot rustbot assigned lcnr and unassigned davidtwco Dec 2, 2024
@lcnr
Copy link
Contributor

lcnr commented Dec 2, 2024

r=me after lang approval (idk if it needs a full FCP, it is observable by users after all)

@tmandry
Copy link
Member

tmandry commented Dec 4, 2024

@RalfJung @JakobDegen Could you elaborate on the motivation? I agree it would be nice if #133523 compiled but I find myself asking "how clever is clever enough" for these checks. Do you feel comfortable with writing this behavior into the language spec?

@nikomatsakis

This comment was marked as outdated.

@rfcbot

This comment was marked as outdated.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Dec 4, 2024
@nikomatsakis
Copy link
Contributor

Oh dear. @rfcbot cancel

@labels -T-compiler

@nikomatsakis
Copy link
Contributor

what am I doing :)

@nikomatsakis
Copy link
Contributor

@rustbot labels -T-compiler

@rfcbot fcp cancel

@rustbot rustbot removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 4, 2024
@rfcbot
Copy link

rfcbot commented Dec 4, 2024

@nikomatsakis proposal cancelled.

@rfcbot rfcbot removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Dec 4, 2024
@nikomatsakis
Copy link
Contributor

@rfcbot fcp merge

Based on discussion in the lang-team meeting we felt this needed an FCP. We discussed a few points we'd like to see clarified

  • overall this doesn't seem to be an undue complication on the model itself -- i.e., it's not adding new information into the abstracted form of values that CTFE thinks about, only making better use of the data it already has

but it is still complicating the spec, and it's not obvious when this function (or any other) will be "smart enough", so @tmandry was looking for better motivation than an issue (does this represent a real-world pattern?). The other question came from @pnkfelix who was wondering if the logic could be invalidated by people casting unaligned pointers or doing other things that don't respect alignment.

@rfcbot
Copy link

rfcbot commented Dec 4, 2024

Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Dec 4, 2024
@nikomatsakis
Copy link
Contributor

@rustbot labels +T-compiler

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Jan 18, 2025
@rfcbot
Copy link

rfcbot commented Jan 18, 2025

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@RalfJung
Copy link
Member Author

@bors r=lcnr
(based on this)

@bors
Copy link
Contributor

bors commented Jan 18, 2025

📌 Commit b438e46 has been approved by lcnr

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jan 18, 2025

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

@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-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Jan 18, 2025
@RalfJung
Copy link
Member Author

@bors r=lcnr

@bors
Copy link
Contributor

bors commented Jan 18, 2025

📌 Commit e1dda10 has been approved by lcnr

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jan 18, 2025

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 18, 2025
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#133700 (const-eval: detect more pointers as definitely not-null)
 - rust-lang#135290 (Encode constraints that hold at all points as logical edges in location-sensitive polonius)
 - rust-lang#135478 (Run clippy for rustc_codegen_gcc on CI)
 - rust-lang#135583 (Move `std::pipe::*` into `std::io`)
 - rust-lang#135612 (Include x scripts in tarballs)
 - rust-lang#135624 (ci: mirror buildkit image to ghcr)
 - rust-lang#135661 (Stabilize `float_next_up_down`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit b78edd7 into rust-lang:master Jan 18, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 18, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 18, 2025
Rollup merge of rust-lang#133700 - RalfJung:const-non-null, r=lcnr

const-eval: detect more pointers as definitely not-null

This fixes rust-lang#133523 by making the `scalar_may_be_null` check smarter: for instance, an odd offset in any 2-aligned allocation can never be null, even if it is out-of-bounds.

More generally, if an allocation with unknown base address B is aligned to alignment N, and a pointer is at offset X inside that allocation, then we know that `(B + X) mod N = B mod N + X mod N = X mod N`. Since `0 mod N` is definitely 0, if we learn that `X mod N` is *not* 0 we can deduce that `B + X` is not 0.

This is immediately visible on stable, via `ptr.is_null()` (and, more subtly, by not raising a UB error when such a pointer is used somewhere that a non-null pointer is required). Therefore nominating for `@rust-lang/lang.`
@RalfJung RalfJung deleted the const-non-null branch January 20, 2025 18:51
github-actions bot pushed a commit to tautschnig/verify-rust-std that referenced this pull request Feb 20, 2025
The "panic in const if CTFE doesn't know the answer" behavior was discussed to be the desired behavior in rust-lang#74939, and is currently how the function actually behaves.

I intentionally wrote this documentation to allow for the possibility that a panic might not occur even if the pointer is out of bounds, because of rust-lang#133700 and other potential changes in the future.
github-actions bot pushed a commit to tautschnig/verify-rust-std that referenced this pull request Feb 20, 2025
The "panic in const if CTFE doesn't know the answer" behavior was discussed to be the desired behavior in rust-lang#74939, and is currently how the function actually behaves.

I intentionally wrote this documentation to allow for the possibility that a panic might not occur even if the pointer is out of bounds, because of rust-lang#133700 and other potential changes in the future.
github-actions bot pushed a commit to carolynzech/rust that referenced this pull request Feb 20, 2025
The "panic in const if CTFE doesn't know the answer" behavior was discussed to be the desired behavior in rust-lang#74939, and is currently how the function actually behaves.

I intentionally wrote this documentation to allow for the possibility that a panic might not occur even if the pointer is out of bounds, because of rust-lang#133700 and other potential changes in the future.
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Feb 20, 2025
The "panic in const if CTFE doesn't know the answer" behavior was discussed to be the desired behavior in rust-lang#74939, and is currently how the function actually behaves.

I intentionally wrote this documentation to allow for the possibility that a panic might not occur even if the pointer is out of bounds, because of rust-lang#133700 and other potential changes in the future.
github-actions bot pushed a commit to thanhnguyen-aws/verify-rust-std that referenced this pull request Feb 21, 2025
The "panic in const if CTFE doesn't know the answer" behavior was discussed to be the desired behavior in rust-lang#74939, and is currently how the function actually behaves.

I intentionally wrote this documentation to allow for the possibility that a panic might not occur even if the pointer is out of bounds, because of rust-lang#133700 and other potential changes in the future.
github-actions bot pushed a commit to thanhnguyen-aws/verify-rust-std that referenced this pull request Feb 21, 2025
The "panic in const if CTFE doesn't know the answer" behavior was discussed to be the desired behavior in rust-lang#74939, and is currently how the function actually behaves.

I intentionally wrote this documentation to allow for the possibility that a panic might not occur even if the pointer is out of bounds, because of rust-lang#133700 and other potential changes in the future.
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Feb 22, 2025
The "panic in const if CTFE doesn't know the answer" behavior was discussed to be the desired behavior in rust-lang#74939, and is currently how the function actually behaves.

I intentionally wrote this documentation to allow for the possibility that a panic might not occur even if the pointer is out of bounds, because of rust-lang#133700 and other potential changes in the future.
github-actions bot pushed a commit to carolynzech/rust that referenced this pull request Feb 22, 2025
The "panic in const if CTFE doesn't know the answer" behavior was discussed to be the desired behavior in rust-lang#74939, and is currently how the function actually behaves.

I intentionally wrote this documentation to allow for the possibility that a panic might not occur even if the pointer is out of bounds, because of rust-lang#133700 and other potential changes in the future.
github-actions bot pushed a commit to tautschnig/verify-rust-std that referenced this pull request Feb 22, 2025
The "panic in const if CTFE doesn't know the answer" behavior was discussed to be the desired behavior in rust-lang#74939, and is currently how the function actually behaves.

I intentionally wrote this documentation to allow for the possibility that a panic might not occur even if the pointer is out of bounds, because of rust-lang#133700 and other potential changes in the future.
github-actions bot pushed a commit to thanhnguyen-aws/verify-rust-std that referenced this pull request Feb 22, 2025
The "panic in const if CTFE doesn't know the answer" behavior was discussed to be the desired behavior in rust-lang#74939, and is currently how the function actually behaves.

I intentionally wrote this documentation to allow for the possibility that a panic might not occur even if the pointer is out of bounds, because of rust-lang#133700 and other potential changes in the future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. 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. T-lang Relevant to the language team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive const-UB check on NonNull pointer