-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Taint the type of ill-formed (unsized) statics #144226
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
Changes from 1 commit
e70d213
6b4181f
8322078
ec81464
7c64961
8817572
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//! Regression test for #121176 | ||
//! KnownPanicsLint used to assert ABI compatibility in the interpreter, | ||
//! which ICEs with unsized statics. | ||
//@ needs-rustc-debug-assertions | ||
|
||
use std::fmt::Debug; | ||
|
||
static STATIC_1: dyn Debug + Sync = *(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For instance, this is a clearly ill-formed static. Nothing should ever look at its MIR. Trying to make the MIR interpreter APIs resistant against bogus MIR is a pointless game of whack-a-mole. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea those cases are straight forward to prevent within There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That doesn't quite work since we allow extern statics that have extern types, which are unsized. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, we can check the tail manually for slices and dyn trait There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But that requires unfolding the type which will trigger the same cycle error, won't it? |
||
//~^ ERROR the size for values of type `(dyn Debug + Sync + 'static)` cannot be known | ||
//~| ERROR type `()` cannot be dereferenced | ||
|
||
fn main() { | ||
println!("{:?}", &STATIC_1); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time | ||
--> $DIR/static-by-value-dyn.rs:8:1 | ||
| | ||
LL | static STATIC_1: dyn Debug + Sync = *(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)` | ||
= note: statics and constants must have a statically known size | ||
|
||
error[E0614]: type `()` cannot be dereferenced | ||
--> $DIR/static-by-value-dyn.rs:8:37 | ||
| | ||
LL | static STATIC_1: dyn Debug + Sync = *(); | ||
| ^^^ can't be dereferenced | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
Some errors have detailed explanations: E0277, E0614. | ||
For more information about an error, try `rustc --explain E0277`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//! Regression test for #140332 | ||
//! KnownPanicsLint used to assert ABI compatibility in the interpreter, | ||
//! which ICEs with unsized statics. | ||
|
||
static mut S: [i8] = ["Some thing"; 1]; | ||
//~^ ERROR the size for values of type `[i8]` cannot be known | ||
//~| ERROR mismatched types | ||
//~| ERROR mismatched types | ||
|
||
fn main() { | ||
assert_eq!(S, [0; 1]); | ||
//~^ ERROR use of mutable static is unsafe | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
error[E0277]: the size for values of type `[i8]` cannot be known at compilation time | ||
--> $DIR/static-by-value-slice.rs:5:1 | ||
| | ||
LL | static mut S: [i8] = ["Some thing"; 1]; | ||
| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `[i8]` | ||
= note: statics and constants must have a statically known size | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/static-by-value-slice.rs:5:23 | ||
| | ||
LL | static mut S: [i8] = ["Some thing"; 1]; | ||
| ^^^^^^^^^^^^ expected `i8`, found `&str` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/static-by-value-slice.rs:5:22 | ||
| | ||
LL | static mut S: [i8] = ["Some thing"; 1]; | ||
| ^^^^^^^^^^^^^^^^^ expected `[i8]`, found `[i8; 1]` | ||
|
||
error[E0133]: use of mutable static is unsafe and requires unsafe function or block | ||
--> $DIR/static-by-value-slice.rs:11:16 | ||
| | ||
LL | assert_eq!(S, [0; 1]); | ||
| ^ use of mutable static | ||
| | ||
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
Some errors have detailed explanations: E0133, E0277, E0308. | ||
For more information about an error, try `rustc --explain E0133`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//! Regression test for #139872 | ||
//! KnownPanicsLint used to assert ABI compatibility in the interpreter, | ||
//! which ICEs with unsized statics. | ||
|
||
enum E { | ||
V16(u16), | ||
V32(u32), | ||
} | ||
|
||
static C: (E, u16, str) = (E::V16(0xDEAD), 0x600D, 0xBAD); | ||
//~^ ERROR the size for values of type `str` cannot be known | ||
//~| ERROR the size for values of type `str` cannot be known | ||
//~| ERROR mismatched types | ||
|
||
pub fn main() { | ||
let (_, n, _) = C; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
error[E0277]: the size for values of type `str` cannot be known at compilation time | ||
--> $DIR/static-by-value-str.rs:10:1 | ||
| | ||
LL | static C: (E, u16, str) = (E::V16(0xDEAD), 0x600D, 0xBAD); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: within `(E, u16, str)`, the trait `Sized` is not implemented for `str` | ||
= note: required because it appears within the type `(E, u16, str)` | ||
= note: statics and constants must have a statically known size | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/static-by-value-str.rs:10:52 | ||
| | ||
LL | static C: (E, u16, str) = (E::V16(0xDEAD), 0x600D, 0xBAD); | ||
| ^^^^^ expected `str`, found integer | ||
|
||
error[E0277]: the size for values of type `str` cannot be known at compilation time | ||
--> $DIR/static-by-value-str.rs:10:27 | ||
| | ||
LL | static C: (E, u16, str) = (E::V16(0xDEAD), 0x600D, 0xBAD); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: within `(E, u16, str)`, the trait `Sized` is not implemented for `str` | ||
= note: required because it appears within the type `(E, u16, str)` | ||
= note: tuples must have a statically known size to be initialized | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
Some errors have detailed explanations: E0277, E0308. | ||
For more information about an error, try `rustc --explain E0277`. |
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'm not a fan of these functions. The point of the assertions is that they are a last line of defense to detect defective callers. They are not exhaustive checks. If the caller can't ensure that the value has the right type, that can only be fixed in the caller.
IOW,
matches_abi
here really is more of amaybe_matches_abi
. It is necessary, but not sufficient. And trying to make it sufficient is the wrong approach; the right approach is figuring out why someone is feeding bogus data into these functions.