-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 all 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
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
3 changes: 3 additions & 0 deletions
3
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,3 @@ | ||
pub static mut ZERO: [u8; 1] = [0]; | ||
pub static ZERO_REF: &[u8; 1] = unsafe { &ZERO }; | ||
pub static mut OPT_ZERO: Option<u8> = Some(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
81 changes: 81 additions & 0 deletions
81
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,81 @@ | ||
// compile-flags: -Zunleash-the-miri-inside-of-you -Zdeduplicate-diagnostics | ||
// aux-build:static_cross_crate.rs | ||
#![allow(const_err)] | ||
|
||
#![feature(exclusive_range_pattern, half_open_range_patterns, const_if_match, const_panic)] | ||
|
||
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 | ||
}; | ||
|
||
const U8_MUT: &u8 = { //~ ERROR undefined behavior to use this value | ||
//~| NOTE encountered a reference pointing to a static variable | ||
//~| NOTE | ||
unsafe { &static_cross_crate::ZERO[0] } | ||
//~^ WARN skipping const checks | ||
}; | ||
|
||
// Also test indirection that reads from other static. This causes a const_err. | ||
#[warn(const_err)] //~ NOTE | ||
const U8_MUT2: &u8 = { //~ NOTE | ||
unsafe { &(*static_cross_crate::ZERO_REF)[0] } | ||
//~^ WARN skipping const checks | ||
//~| WARN [const_err] | ||
//~| NOTE constant accesses static | ||
}; | ||
#[warn(const_err)] //~ NOTE | ||
const U8_MUT3: &u8 = { //~ NOTE | ||
unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } } | ||
//~^ WARN skipping const checks | ||
//~| WARN [const_err] | ||
//~| NOTE constant accesses static | ||
}; | ||
|
||
pub fn test(x: &[u8; 1]) -> bool { | ||
match x { | ||
SLICE_MUT => true, | ||
//~^ ERROR could not evaluate constant pattern | ||
&[1..] => false, | ||
} | ||
} | ||
|
||
pub fn test2(x: &u8) -> bool { | ||
match x { | ||
U8_MUT => true, | ||
//~^ ERROR could not evaluate constant pattern | ||
&(1..) => false, | ||
} | ||
} | ||
|
||
// We need to use these *in a pattern* to trigger the failure... likely because | ||
// the errors above otherwise stop compilation too early? | ||
pub fn test3(x: &u8) -> bool { | ||
match x { | ||
U8_MUT2 => true, | ||
//~^ ERROR could not evaluate constant pattern | ||
&(1..) => false, | ||
} | ||
} | ||
pub fn test4(x: &u8) -> bool { | ||
match x { | ||
U8_MUT3 => true, | ||
//~^ 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]); | ||
test2(&0); | ||
} |
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.
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.