Skip to content

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

Merged
bors merged 2 commits into
rust-lang:masterfrom
veera-sivarajan:fix-87525
Oct 6, 2024
Merged

Add a Lint for Pointer to Integer Transmutes in Consts#130540
bors merged 2 commits into
rust-lang:masterfrom
veera-sivarajan:fix-87525

Conversation

@veera-sivarajan

Copy link
Copy Markdown
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

rustbot commented Sep 19, 2024

Copy link
Copy Markdown
Collaborator

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

rustbot commented Sep 19, 2024

Copy link
Copy Markdown
Collaborator

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
Comment thread compiler/rustc_lint_defs/src/builtin.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@scottmcm

Copy link
Copy Markdown
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 label Sep 19, 2024
@veera-sivarajan

veera-sivarajan commented Sep 19, 2024

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
Copy Markdown
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

RalfJung commented Sep 22, 2024

Copy link
Copy Markdown
Member

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

rustbot commented Sep 28, 2024

Copy link
Copy Markdown
Collaborator

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.

@flip1995 flip1995 Sep 30, 2024

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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.

@estebank estebank left a comment

Copy link
Copy Markdown
Contributor

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?

Comment thread compiler/rustc_lint_defs/src/builtin.rs Outdated

Copy link
Copy Markdown
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.

Comment thread library/core/src/ptr/mod.rs
@RalfJung

RalfJung commented Oct 4, 2024

Copy link
Copy Markdown
Member

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

estebank commented Oct 5, 2024

Copy link
Copy Markdown
Contributor

@bors r+

@bors

bors commented Oct 5, 2024

Copy link
Copy Markdown
Collaborator

📌 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

bors commented Oct 6, 2024

Copy link
Copy Markdown
Collaborator

⌛ Testing commit ab86735 with merge daebce4...

@bors

bors commented Oct 6, 2024

Copy link
Copy Markdown
Collaborator

☀️ 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
@rustbot rustbot added this to the 1.83.0 milestone Oct 6, 2024
@rust-timer

Copy link
Copy Markdown
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%)

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add lint against ptr-to-int transmutes in consts

10 participants