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

Lint on invalid usage of UnsafeCell::raw_get in reference casting #115166

Merged

Conversation

Urgau
Copy link
Member

@Urgau Urgau commented Aug 24, 2023

This PR proposes to take into account UnsafeCell::raw_get method call for non-Freeze types for the invalid_reference_casting lint.

The goal of this is to catch those kind of invalid reference casting:

fn as_mut<T>(x: &T) -> &mut T {
    unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *const _) }
    //~^ ERROR casting `&T` to `&mut T` is undefined behavior
}

r? @est31

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 24, 2023
Comment on lines +58 to +66
fn as_mut<T>(x: &T) -> &mut T {
unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *const _) }
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
}

fn as_mut_i32(x: &i32) -> &mut i32 {
unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *const _) }
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
}
Copy link
Member Author

@Urgau Urgau Aug 24, 2023

Choose a reason for hiding this comment

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

@RalfJung can you confirm (or deny) that we can lint on those expressions ?

Copy link
Member

Choose a reason for hiding this comment

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

Those expressions are definitely bogus.

Can you confirm that the lint will not fire if the type of x is changed to &Cell<i32>?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I can.

I already added some tests below to make sure we don't lint on them, but added a test with &Cell<i32> anyway (just to be on the safe side).

@Urgau Urgau force-pushed the invalid_ref_casting-invalid-unsafecell-usage branch from fc348de to 89800a2 Compare August 24, 2023 13:00
@est31
Copy link
Member

est31 commented Sep 6, 2023

@bors r+

@bors
Copy link
Collaborator

bors commented Sep 6, 2023

📌 Commit 89800a2 has been approved by est31

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 Sep 6, 2023
@bors
Copy link
Collaborator

bors commented Sep 7, 2023

⌛ Testing commit 89800a2 with merge 4e5b31c...

@bors
Copy link
Collaborator

bors commented Sep 7, 2023

☀️ Test successful - checks-actions
Approved by: est31
Pushing 4e5b31c to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 7, 2023
@bors bors merged commit 4e5b31c into rust-lang:master Sep 7, 2023
@rustbot rustbot added this to the 1.74.0 milestone Sep 7, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4e5b31c): comparison URL.

Overall result: ✅ 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
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 1
Improvements ✅
(secondary)
-0.6% [-0.6%, -0.6%] 2
All ❌✅ (primary) -0.2% [-0.2%, -0.2%] 1

Max RSS (memory usage)

Results

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.7% [0.7%, 0.7%] 1
Regressions ❌
(secondary)
1.1% [1.1%, 1.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.7% [0.7%, 0.7%] 1

Cycles

Results

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.3% [3.3%, 3.3%] 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: 628.074s -> 628.991s (0.15%)
Artifact size: 317.96 MiB -> 317.97 MiB (0.00%)

@Urgau Urgau deleted the invalid_ref_casting-invalid-unsafecell-usage branch September 7, 2023 08:06
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-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants