-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Miri: better document and fix dynamic const pattern soundness checks #71655
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e8ffa21
better document const-pattern dynamic soundness checks, and fix a sou…
RalfJung a84e2a0
add test for const-ref-to-cross-crate-mutable-static
RalfJung a089801
clarify comment
RalfJung 979bbf2
also test reference into static field
RalfJung a03355d
some more test cases
RalfJung 07772fc
expand comment in memory.rs with extra soundness concerns
RalfJung 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 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 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 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
1 change: 1 addition & 0 deletions
1
src/test/ui/consts/miri_unleashed/auxiliary/static_cross_crate.rs
This file contains 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 @@ | ||
pub static mut ZERO: [u8; 1] = [0]; |
This file contains 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
16 changes: 8 additions & 8 deletions
16
src/test/ui/consts/miri_unleashed/const_refers_to_static.stderr
This file contains 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 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 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
35 changes: 35 additions & 0 deletions
35
src/test/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs
This file contains 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,35 @@ | ||
// compile-flags: -Zunleash-the-miri-inside-of-you | ||
// aux-build:static_cross_crate.rs | ||
#![allow(const_err)] | ||
|
||
#![feature(exclusive_range_pattern)] | ||
#![feature(half_open_range_patterns)] | ||
|
||
extern crate static_cross_crate; | ||
|
||
// Sneaky: reference to a mutable static. | ||
// Allowing this would be a disaster for pattern matching, we could violate exhaustiveness checking! | ||
const SLICE_MUT: &[u8; 1] = { //~ ERROR undefined behavior to use this value | ||
//~| NOTE encountered a reference pointing to a static variable | ||
//~| NOTE | ||
unsafe { &static_cross_crate::ZERO } | ||
//~^ WARN skipping const checks | ||
//~| WARN skipping const checks | ||
}; | ||
|
||
pub fn test(x: &[u8; 1]) -> bool { | ||
match x { | ||
SLICE_MUT => true, | ||
//~^ ERROR could not evaluate constant pattern | ||
//~| ERROR could not evaluate constant pattern | ||
&[1..] => false, | ||
} | ||
} | ||
|
||
fn main() { | ||
unsafe { | ||
static_cross_crate::ZERO[0] = 1; | ||
} | ||
// Now the pattern is not exhaustive any more! | ||
test(&[0]); | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.stderr
This file contains 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,41 @@ | ||
warning: skipping const checks | ||
--> $DIR/const_refers_to_static_cross_crate.rs:15:15 | ||
| | ||
LL | unsafe { &static_cross_crate::ZERO } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
warning: skipping const checks | ||
--> $DIR/const_refers_to_static_cross_crate.rs:15:15 | ||
| | ||
LL | unsafe { &static_cross_crate::ZERO } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error[E0080]: it is undefined behavior to use this value | ||
--> $DIR/const_refers_to_static_cross_crate.rs:12:1 | ||
| | ||
LL | / const SLICE_MUT: &[u8; 1] = { | ||
LL | | | ||
LL | | | ||
LL | | unsafe { &static_cross_crate::ZERO } | ||
LL | | | ||
LL | | | ||
LL | | }; | ||
| |__^ type validation failed: encountered a reference pointing to a static variable | ||
| | ||
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. | ||
|
||
error: could not evaluate constant pattern | ||
--> $DIR/const_refers_to_static_cross_crate.rs:22:9 | ||
| | ||
LL | SLICE_MUT => true, | ||
| ^^^^^^^^^ | ||
|
||
error: could not evaluate constant pattern | ||
--> $DIR/const_refers_to_static_cross_crate.rs:22:9 | ||
| | ||
LL | SLICE_MUT => true, | ||
| ^^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors; 2 warnings emitted | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
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.
won't this be worked around if you do
&static_cross_crate::ZERO[0]
here, and then pattern matching suddenly sees the mutable memory?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.
Why would that happen? It's still a reference to a
GlobalAlloc::Static
.I can add that testcase.
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 are right, that error does not get caught. But I do not understand why. The pointer should be the same, shouldn't it?
Are we somewhere accidentally "leaking" the internal pointer of the static, as opposed to the lazy one?
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.
Ah, never mind -- we have to actually use the constant to cause the error. (Not sure why though, we should be evaluating all consts whether they are used or not... probably because I did
allow(const_err)
.)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.
If that case isn't doing it, then using
&&[u8]
for the static and using&*static_cross_crate::ZERO
will definitely do it, or by using an enum and getting a reference to a field. Any operation that needs to actually read from the static in order to produce the reference will yield the internalAllocId
instead of the lazy one.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 don't think that's true. The code in
memory.rs
is very careful to never leak the internalAllocId
back to the interpreter.Also, any code that actually reads from the static will fail the
before_access_static
dynamic check, and this is already covered.