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

@RalfJung
Copy link
Member Author

RalfJung commented Jan 7, 2025

@joshtriplett @scottmcm @tmandry have the answers above resolved your concerns, or is the plan that this will be discussed at some future lang team meeting before we proceed with FCP?

@tmandry
Copy link
Member

tmandry commented Jan 8, 2025

Thanks for the discussion. Reading #133523 (comment) (which I missed before) was helpful for understanding a real-world use case.

Making const eval less willing to give up in a case where we know a pointer can't be null makes sense to me.

@rfcbot reviewed

Do you think the behavior should be documented in the reference somewhere? I guess it stays out of detailed CTFE mechanics like this?

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Jan 8, 2025
@rfcbot
Copy link

rfcbot commented Jan 8, 2025

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Jan 8, 2025
@RalfJung
Copy link
Member Author

RalfJung commented Jan 8, 2025

AFAIK we don't say anything about is_null in the reference. We could update the fn is_null docs to say more precisely when it will work and when not; I am not sure if that's worth it.

@scottmcm
Copy link
Member

scottmcm commented Jan 8, 2025

Looking at how it works, I feel good about doing this. Adding the modulo check is straight-forward to explain, and fits into the existing data well. If it took a whole bunch of new complicated tracking I'm be more skeptical, but this check seems fine.

@traviscross traviscross removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Jan 8, 2025
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels 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
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