-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Fix malformed transmute handling during mir build #154144
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
Open
Human9000-bit
wants to merge
5
commits into
rust-lang:main
Choose a base branch
from
Human9000-bit:const-prop-transmute-async-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
568e49d
Make `check_transmutes` query return `Result`
Human9000-bit 70116e1
Update transmute test for typeck child cases
Human9000-bit 788c916
Taint the body in case of malformed transmutes
Human9000-bit 6f08425
Fix error handling in `check_transmutes`
Human9000-bit 694c138
bless tests where error messages disappeared because of body tainting
Human9000-bit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| //@ only-64bit | ||
|
|
||
| pub const ZST: &[u8] = unsafe { std::mem::transmute(1usize) }; | ||
| //~^ ERROR transmuting from 8-byte type to 16-byte type | ||
| //~^ ERROR: cannot transmute between types of different sizes, or dependently-sized types [E0512] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| error[E0080]: transmuting from 8-byte type to 16-byte type: `usize` -> `&[u8]` | ||
| error[E0512]: cannot transmute between types of different sizes, or dependently-sized types | ||
| --> $DIR/issue-79494.rs:3:33 | ||
| | | ||
| LL | pub const ZST: &[u8] = unsafe { std::mem::transmute(1usize) }; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `ZST` failed here | ||
| | ^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: source type: `usize` (64 bits) | ||
| = note: target type: `&[u8]` (128 bits) | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0080`. | ||
| For more information about this error, try `rustc --explain E0512`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| //@compile-flags: -Zmir-enable-passes=+DataflowConstProp --crate-type lib | ||
| //@ edition:2021 | ||
| pub async fn a() -> u32 { | ||
| unsafe { std::mem::transmute(1u64) } | ||
| //~^error: cannot transmute between types of different sizes, or dependently-sized types | ||
| } | ||
|
|
||
| pub async fn b() -> u32 { | ||
| let closure = || unsafe { std::mem::transmute(1u64) }; | ||
| //~^ ERROR: cannot transmute between types of different sizes, or dependently-sized types [E0512] | ||
| closure() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| error[E0512]: cannot transmute between types of different sizes, or dependently-sized types | ||
| --> $DIR/transmute-in-async-fn.rs:4:14 | ||
| | | ||
| LL | unsafe { std::mem::transmute(1u64) } | ||
| | ^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: source type: `u64` (64 bits) | ||
| = note: target type: `u32` (32 bits) | ||
|
|
||
| error[E0512]: cannot transmute between types of different sizes, or dependently-sized types | ||
| --> $DIR/transmute-in-async-fn.rs:9:31 | ||
| | | ||
| LL | let closure = || unsafe { std::mem::transmute(1u64) }; | ||
| | ^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: source type: `u64` (64 bits) | ||
| = note: target type: `u32` (32 bits) | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0512`. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error is emitted in typeck iirc. It should be tainting the body. If it isnt, it's likely that
tcx.dcx()is used instead ofinfcx.dcx()Please check if we can avoid changing anything in const prop by properly tainting wherever this error is emitted
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first glance there isn't
infcxwhere the error is emitted, and no obvious way to get oneThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is it being emitted? If it is within the typeck query, we can probably change something further up the call stack ( you can try using
-Ztreat-err-as-bugto find out where the error is emitted and what the call stack is while emitting itUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is emitted in
check_transmutesran byrun_required_analyses:rust/compiler/rustc_interface/src/passes.rs
Lines 1146 to 1149 in 981cf69
and the very error emission happens here:
rust/compiler/rustc_hir_typeck/src/intrinsicck.rs
Lines 138 to 142 in 981cf69
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wow. Didn't know that was changed (#145469)
I'll need to think on it. Unsure why it was necessary to live in a separate query
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that would fix this optimizer ICE, but i didn't suggest that because i assumed we'd get the same ICE in CTFE, and the only fix for that is to run the query earlier, where it would likely cycle again
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to do that at the start of
run_passof dataflow const prop. It ICEd fordef_idbeing a typeck child.So we can't just slap
check_transmutesfor everydef_id(at least in mir opts)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can invoke
is_typeck_childto invoke check_transmutes on the parent (there's also a method for getting the typeck parent).This will fix the ICE (if you also make the check_transmute query return a Result<(), ErrorReported> and don't do anything if it's an error).
Tho at that point it may be better to do what cjgillot suggested and check it at the start of thr optimized_mir query and just return a dummy body or the unoptimized body.
I'm just sceptical it fixed the ICE in general as it should be possibe to produce the ICE in const eval by evaluating code that has the same transmute problem
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm apparently not: https://play.rust-lang.org/?version=nightly&mode=release&edition=2024&gist=31291deac2b05c040502d266b33e2f69
It seems very weird that we report an error here, but that's probably an oversight. The ctfe error should be an ICE
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently your example is getting caught in well-formedness checking during HIR analysis, which uses different methods of interpreter compared to dataflow const prop opt