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

Add a Lint for Pointer to Integer Transmutes in Consts #130540

Merged
merged 2 commits into from
Oct 6, 2024

Conversation

veera-sivarajan
Copy link
Contributor

Fixes #87525

This PR adds a MirLint for pointer to integer transmutes in const functions and associated consts. The implementation closely follows this comment: #85769 (comment). More details about the implementation can be found in the comments.

Note: This could break some sound code as mentioned by RalfJung in #85769 (comment):

... technically const-code could transmute/cast an int to a ptr and then transmute it back and that would be correct -- so the lint will deny some sound code. Does not seem terribly likely though.

References:

  1. https://doc.rust-lang.org/std/mem/fn.transmute.html
  2. https://doc.rust-lang.org/reference/items/associated-items.html#associated-constants

@rustbot
Copy link
Collaborator

rustbot commented Sep 19, 2024

r? @estebank

rustbot has assigned @estebank.
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
Copy link
Collaborator

rustbot commented Sep 19, 2024

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@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 Sep 19, 2024
@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member

What's the motivation for this lint? https://doc.rust-lang.org/std/primitive.pointer.html#method.addr and https://doc.rust-lang.org/std/ptr/fn.without_provenance.html are transmutes, and I'd thought that this was considered basically fine now, albeit possibly not what you wanted if you need to preserve provenance.

@jieyouxu jieyouxu added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Sep 19, 2024
@veera-sivarajan
Copy link
Contributor Author

veera-sivarajan commented Sep 19, 2024

Transmuting a pointer to integer in const context is undefined behavior1. Usually, the evaluator will abort and emit an error when it comes across such undefined transmutes.

But const functions and associated consts are evaluated only when referenced. This can result in undefined behavior in a library going unnoticed until the function or constant is actually used. Therefore, this lint specifically targets pointer to integer transmutes in const functions and associated consts.

From what I can tell, this lint is not related to the functions you have linked because:

  1. addr() is not a const function.
  2. without_provenance() transmutes an integer to pointer.

Footnotes

  1. https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers

@scottmcm
Copy link
Member

Oh, of course; thank you. Brain fart on my part.

Seem hard to lint on when it's not always UB, because things like NonNull::dangling().addr() is a transmute but isn't UB.

I'd be tempted to run it on optimized mir where we can have const-folded a bunch of those cases away already, but then it'd be super inconsistent which we probably don't want either :/

@RalfJung
Copy link
Member

I'd be tempted to run it on optimized mir where we can have const-folded a bunch of those cases away already, but then it'd be super inconsistent which we probably don't want either :/

Also, we're not optimizing const MIR.

@RalfJung
Copy link
Member

RalfJung commented Sep 22, 2024

Seem hard to lint on when it's not always UB, because things like NonNull::dangling().addr() is a transmute but isn't UB.

Indeed, that's the tricky part. The lint triggers in this code which is exactly an example of that:

// SAFETY: This is just an inlined `p.addr()` (which is not
// a `const fn` so we cannot call it).
// During const eval, we hook this function to ensure that the pointer never
// has provenance, making this sound.
let addr: usize = unsafe { mem::transmute(p) };

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Sep 28, 2024

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@flip1995 flip1995 Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Clippy changes remove a test case that tests that a Clippy lint doesn't trigger for a certain pattern and adds this new test case, that tests a rustc lint in the Clippy test suite.

I think the better thing to do here would be to keep the test case where it is and allow the new rustc lint with an outer attribute just on the issue_12402 function. Maybe give the allow attribute a reason = "". We still need to test that our other transmute lints do not trigger on this and this new test case doesn't reflect what we want to actually test with this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address this request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the detailed feedback.

Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code change looks correct, but I have some questions.

Shouldn't we also account for as? Is align_offset invoking UB?

compiler/rustc_lint_defs/src/builtin.rs Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address this request.

library/core/src/ptr/mod.rs Show resolved Hide resolved
@RalfJung
Copy link
Member

RalfJung commented Oct 4, 2024

Shouldn't we also account for as?

as from ptr to int is just forbidden in const contexts.

Is align_offset invoking UB?

No, that function is treated specially by the const interpreter to ensure that the pointer argument is always actually an integer.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@estebank
Copy link
Contributor

estebank commented Oct 5, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Oct 5, 2024

📌 Commit ab86735 has been approved by estebank

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-review Status: Awaiting review from the assignee but also interested parties. labels Oct 5, 2024
@bors
Copy link
Contributor

bors commented Oct 6, 2024

⌛ Testing commit ab86735 with merge daebce4...

@bors
Copy link
Contributor

bors commented Oct 6, 2024

☀️ Test successful - checks-actions
Approved by: estebank
Pushing daebce4 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 6, 2024
@bors bors merged commit daebce4 into rust-lang:master Oct 6, 2024
7 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Oct 6, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (daebce4): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.2% [0.1%, 0.5%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 2.1%, secondary 1.6%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.1% [2.1%, 2.1%] 1
Regressions ❌
(secondary)
1.6% [0.5%, 3.6%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.1% [2.1%, 2.1%] 1

Cycles

Results (secondary 3.0%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.0% [3.0%, 3.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 774.072s -> 774.475s (0.05%)
Artifact size: 329.50 MiB -> 329.52 MiB (0.01%)

flip1995 pushed a commit to flip1995/rust that referenced this pull request Oct 18, 2024
Add a Lint for Pointer to Integer Transmutes in Consts

Fixes rust-lang#87525

This PR adds a MirLint for pointer to integer transmutes in const functions and associated consts. The implementation closely follows this comment: rust-lang#85769 (comment). More details about the implementation can be found in the comments.

Note: This could break some sound code as mentioned by RalfJung in rust-lang#85769 (comment):

> ... technically const-code could transmute/cast an int to a ptr and then transmute it back and that would be correct -- so the lint will deny some sound code. Does not seem terribly likely though.

References:
1. https://doc.rust-lang.org/std/mem/fn.transmute.html
2. https://doc.rust-lang.org/reference/items/associated-items.html#associated-constants
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add lint against ptr-to-int transmutes in consts
10 participants