forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#71655 - RalfJung:const-pattern-soundness, r…
…=oli-obk Miri: better document and fix dynamic const pattern soundness checks rust-lang/const-eval#42 got me thinking about soundness for consts being used in patterns, and I found a hole in our existing dynamic checks: a const referring to a mutable static *in a different crate* was not caught. This PR fixes that. It also adds some comments that explain which invariants are crucial for soundness of const-patterns. Curiously, trying to weaponize this soundness hole failed: pattern matching compilation ICEd when encountering the cross-crate static, saying "expected allocation ID alloc0 to point to memory". I don't know why that would happen, statics *should* be entirely normal memory for pattern matching to access. r? @oli-obk Cc @rust-lang/wg-const-eval
- Loading branch information
Showing
11 changed files
with
250 additions
and
24 deletions.
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.