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

Insert null checks for pointer dereferences when debug assertions are enabled #134424

Merged
merged 2 commits into from
Jan 31, 2025

Conversation

1c3t3a
Copy link
Contributor

@1c3t3a 1c3t3a commented Dec 17, 2024

Similar to how the alignment is already checked, this adds a check
for null pointer dereferences in debug mode. It is implemented similarly
to the alignment check as a MirPass.

This inserts checks in the same places as the CheckAlignment pass and additionally
also inserts checks for Borrows, so code like

let ptr: *const u32 = std::ptr::null();
let val: &u32 = unsafe { &*ptr };

will have a check inserted on dereference. This is done because null references
are UB. The alignment check doesn't cover these places, because in &(*ptr).field,
the exact requirement is that the final reference must be aligned. This is something to
consider further enhancements of the alignment check.

For now this is implemented as a separate MirPass, to make it easy to disable
this check if necessary.

This is related to a 2025H1 project goal for better UB checks in debug
mode: rust-lang/rust-project-goals#177.

r? @saethlin

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

rustbot commented Dec 17, 2024

This PR changes Stable MIR

cc @oli-obk, @celinval, @ouz-a

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

Some changes occurred to the CTFE machinery

cc @rust-lang/wg-const-eval

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

@oli-obk
Copy link
Contributor

oli-obk commented Dec 17, 2024

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 17, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 17, 2024
Insert null checks for pointer dereferences when debug assertions are enabled

Similar to how the alignment is already checked, this adds a check
for null pointer dereferences in debug mode. It is implemented similarly
to the alignment check as a MirPass.

This is related to a 2025H1 project goal for better UB checks in debug
mode: rust-lang/rust-project-goals#177.

r? `@saethlin`
@bors
Copy link
Contributor

bors commented Dec 17, 2024

⌛ Trying commit 52b1360 with merge 61e98dc...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Dec 17, 2024

☀️ Try build successful - checks-actions
Build commit: 61e98dc (61e98dc17f0786f1c120ea5366e1680772b3aa14)

@rust-timer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (61e98dc): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

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

Max RSS (memory usage)

Results (primary 3.0%, secondary 3.8%)

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)
3.0% [2.6%, 3.5%] 2
Regressions ❌
(secondary)
3.8% [2.4%, 5.1%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.0% [2.6%, 3.5%] 2

Cycles

Results (secondary -3.4%)

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

Binary size

Results (primary -0.1%, secondary -0.1%)

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.1% [0.0%, 0.3%] 10
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-0.6%, -0.1%] 9
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 1
All ❌✅ (primary) -0.1% [-0.6%, 0.3%] 19

Bootstrap: 768.397s -> 772.491s (0.53%)
Artifact size: 330.36 MiB -> 330.37 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 17, 2024
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a requested review from RalfJung and saethlin December 20, 2024 22:41
@rust-log-analyzer

This comment has been minimized.

@saethlin
Copy link
Member

saethlin commented Dec 21, 2024

For PRs like this that change codegen, x test ui is almost always better to work off than trying to look at CI, because running the whole UI test suite will exercise your change on a lot of small programs, so if something in your change is broken it's usually pretty easy to narrow it down.

CI always tests using a stage2 build, so if you break codegen, you'll often get a CI failure while using your new compiler to build itself, which is hard to debug from.

@RalfJung
Copy link
Member

Is there a specific reason that this is a separate MIR pass from the null ptr check? Together they form the pointer validity checks, so I don't quite see why those would be checked separately.

@1c3t3a
Copy link
Contributor Author

1c3t3a commented Dec 27, 2024

Is there a specific reason that this is a separate MIR pass from the null ptr check? Together they form the pointer validity checks, so I don't quite see why those would be checked separately.

My reasoning here was that this is two separate checks that people maybe want to enable or disable separately (lets say they only like to pay the overhead for alignment and don't care about null). My thinking was that two separate MIR passes solve this problem the most straightforward way, but I am happy to discuss this.

@rust-log-analyzer

This comment has been minimized.

@1c3t3a
Copy link
Contributor Author

1c3t3a commented Jan 31, 2025

Should be fixed!

@saethlin
Copy link
Member

@bors r=saethlin

@bors
Copy link
Contributor

bors commented Jan 31, 2025

📌 Commit b151b51 has been approved by saethlin

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 Jan 31, 2025
@1c3t3a
Copy link
Contributor Author

1c3t3a commented Jan 31, 2025

Ahh just noticed that I can see this crashing with #135994! I can see how this plays out and then rebase the one that was not merged?

@saethlin
Copy link
Member

Yup. One of them has to land first. This should let you be a bit more efficient, though I'll still be watching:

@bors delegate=1c3t3a

@bors
Copy link
Contributor

bors commented Jan 31, 2025

✌️ @1c3t3a, you can now approve this pull request!

If @saethlin told you to "r=me" after making some further change, please make that change, then do @bors r=@saethlin

@bors
Copy link
Contributor

bors commented Jan 31, 2025

⌛ Testing commit b151b51 with merge aa4cfd0...

@bors
Copy link
Contributor

bors commented Jan 31, 2025

☀️ Test successful - checks-actions
Approved by: saethlin
Pushing aa4cfd0 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 31, 2025
@bors bors merged commit aa4cfd0 into rust-lang:master Jan 31, 2025
7 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 31, 2025
1c3t3a added a commit to 1c3t3a/rust that referenced this pull request Jan 31, 2025
The wording unsafe pointer is less common and not mentioned in a lot of
places, instead this is usually called a "raw pointer". For the sake of
uniformity, we rename this method.
This came up during the review of
rust-lang#134424.
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (aa4cfd0): comparison URL.

Overall result: ❌ regressions - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.3% [0.2%, 0.5%] 7
Regressions ❌
(secondary)
0.3% [0.2%, 0.5%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.3% [0.2%, 0.5%] 7

Max RSS (memory usage)

Results (primary -1.3%)

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)
3.9% [2.2%, 5.1%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-4.5% [-8.4%, -2.2%] 5
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.3% [-8.4%, 5.1%] 8

Cycles

Results (primary 2.7%, secondary 2.4%)

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.7% [0.7%, 4.8%] 2
Regressions ❌
(secondary)
2.4% [2.0%, 2.9%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.7% [0.7%, 4.8%] 2

Binary size

Results (primary 0.1%)

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.2% [0.0%, 0.5%] 43
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.4% [-0.6%, -0.1%] 4
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [-0.6%, 0.5%] 47

Bootstrap: 777.68s -> 777.108s (-0.07%)
Artifact size: 328.84 MiB -> 328.80 MiB (-0.01%)

@saethlin
Copy link
Member

saethlin commented Feb 1, 2025

@rustbot label: +perf-regression-triaged

Slight regression is expected, we're generating more MIR to insert new checks. We have evidence from a crater run that these checks find UB in the wild, which justifies the very small compile time slowdown.

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Feb 1, 2025
flip1995 pushed a commit to flip1995/rust that referenced this pull request Feb 6, 2025
Insert null checks for pointer dereferences when debug assertions are enabled

Similar to how the alignment is already checked, this adds a check
for null pointer dereferences in debug mode. It is implemented similarly
to the alignment check as a `MirPass`.

This inserts checks in the same places as the `CheckAlignment` pass and additionally
also inserts checks for `Borrows`, so code like
```rust
let ptr: *const u32 = std::ptr::null();
let val: &u32 = unsafe { &*ptr };
```
will have a check inserted on dereference. This is done because null references
are UB. The alignment check doesn't cover these places, because in `&(*ptr).field`,
the exact requirement is that the final reference must be aligned. This is something to
consider further enhancements of the alignment check.

For now this is implemented as a separate `MirPass`, to make it easy to disable
this check if necessary.

This is related to a 2025H1 project goal for better UB checks in debug
mode: rust-lang/rust-project-goals#177.

r? `@saethlin`
bjorn3 pushed a commit to bjorn3/rust that referenced this pull request Feb 7, 2025
Insert null checks for pointer dereferences when debug assertions are enabled

Similar to how the alignment is already checked, this adds a check
for null pointer dereferences in debug mode. It is implemented similarly
to the alignment check as a `MirPass`.

This inserts checks in the same places as the `CheckAlignment` pass and additionally
also inserts checks for `Borrows`, so code like
```rust
let ptr: *const u32 = std::ptr::null();
let val: &u32 = unsafe { &*ptr };
```
will have a check inserted on dereference. This is done because null references
are UB. The alignment check doesn't cover these places, because in `&(*ptr).field`,
the exact requirement is that the final reference must be aligned. This is something to
consider further enhancements of the alignment check.

For now this is implemented as a separate `MirPass`, to make it easy to disable
this check if necessary.

This is related to a 2025H1 project goal for better UB checks in debug
mode: rust-lang/rust-project-goals#177.

r? `@saethlin`
1c3t3a added a commit to 1c3t3a/rust that referenced this pull request Feb 10, 2025
The wording unsafe pointer is less common and not mentioned in a lot of
places, instead this is usually called a "raw pointer". For the sake of
uniformity, we rename this method.
This came up during the review of
rust-lang#134424.
carolynzech added a commit to carolynzech/kani that referenced this pull request Feb 10, 2025
carolynzech added a commit to carolynzech/kani that referenced this pull request Feb 10, 2025
github-merge-queue bot pushed a commit to model-checking/kani that referenced this pull request Feb 11, 2025
Upgrade toolchain to 2/10.

I **highly recommend** reviewing this PR commit-by-commit. The
description in each commit message links to the upstream PRs that
prompted those particular changes.

## Callouts
- 2/1 had a lot of formatting changes. I split the commits for that day
into formatting changes and functionality changes accordingly.
- 2/5 introduced a regression in our delayed UB instrumentation, so I
made a new fixme test. See #3881 for details.


## Culprit PRs:
rust-lang/rust#134424 
rust-lang/rust#130514
rust-lang/rust#135748
rust-lang/rust#136590
rust-lang/rust#135318
rust-lang/rust#135265

rust-lang/rust@bcb8565
rust-lang/rust#136471
rust-lang/rust#136645

Resolves #3863

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 12, 2025
Rename rustc_middle::Ty::is_unsafe_ptr to is_raw_ptr

The wording unsafe pointer is less common and not mentioned in a lot of places, instead this is usually called a "raw pointer". For the sake of uniformity, we rename this method.
This came up during the review of
rust-lang#134424.

r? `@Noratrieb`
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Feb 13, 2025
Rename rustc_middle::Ty::is_unsafe_ptr to is_raw_ptr

The wording unsafe pointer is less common and not mentioned in a lot of places, instead this is usually called a "raw pointer". For the sake of uniformity, we rename this method.
This came up during the review of
rust-lang/rust#134424.

r? `@Noratrieb`
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Feb 13, 2025
Rename rustc_middle::Ty::is_unsafe_ptr to is_raw_ptr

The wording unsafe pointer is less common and not mentioned in a lot of places, instead this is usually called a "raw pointer". For the sake of uniformity, we rename this method.
This came up during the review of
rust-lang/rust#134424.

r? `@Noratrieb`
bjorn3 pushed a commit to rust-lang/rustc_codegen_cranelift that referenced this pull request Feb 14, 2025
Rename rustc_middle::Ty::is_unsafe_ptr to is_raw_ptr

The wording unsafe pointer is less common and not mentioned in a lot of places, instead this is usually called a "raw pointer". For the sake of uniformity, we rename this method.
This came up during the review of
rust-lang/rust#134424.

r? `@Noratrieb`
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. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. relnotes Marks issues that should be documented in the release notes of the next release. 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.