2022-07-08 11:08:32 -05:00
|
|
|
//@compile-flags: -Zmiri-strict-provenance
|
2021-06-03 10:24:10 -05:00
|
|
|
|
2016-03-19 00:19:39 -05:00
|
|
|
fn empty() -> &'static str {
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hello() -> &'static str {
|
|
|
|
"Hello, world!"
|
|
|
|
}
|
2016-03-19 00:20:59 -05:00
|
|
|
|
|
|
|
fn hello_bytes() -> &'static [u8; 13] {
|
|
|
|
b"Hello, world!"
|
|
|
|
}
|
2016-03-21 19:51:25 -05:00
|
|
|
|
|
|
|
fn hello_bytes_fat() -> &'static [u8] {
|
|
|
|
b"Hello, world!"
|
|
|
|
}
|
2016-04-22 03:34:14 -05:00
|
|
|
|
2016-05-31 05:05:25 -05:00
|
|
|
fn fat_pointer_on_32_bit() {
|
|
|
|
Some(5).expect("foo");
|
|
|
|
}
|
|
|
|
|
2019-02-06 04:47:32 -06:00
|
|
|
fn str_indexing() {
|
|
|
|
let mut x = "Hello".to_string();
|
2022-06-21 01:40:39 -05:00
|
|
|
let _v = &mut x[..3]; // Test IndexMut on String.
|
2019-02-06 04:47:32 -06:00
|
|
|
}
|
|
|
|
|
2021-06-03 10:24:10 -05:00
|
|
|
fn unique_aliasing() {
|
|
|
|
// This is a regression test for the aliasing rules of a `Unique<T>` pointer.
|
|
|
|
// At the time of writing this test case, Miri does not treat `Unique<T>`
|
|
|
|
// pointers as a special case, these are treated like any other raw pointer.
|
|
|
|
// However, there are existing Github issues which may lead to `Unique<T>`
|
|
|
|
// becoming a special case through asserting unique ownership over the pointee:
|
|
|
|
// - https://github.com/rust-lang/unsafe-code-guidelines/issues/258
|
|
|
|
// - https://github.com/rust-lang/unsafe-code-guidelines/issues/262
|
|
|
|
// Below, the calls to `String::remove` and `String::insert[_str]` follow
|
|
|
|
// code paths that would trigger undefined behavior in case `Unique<T>`
|
|
|
|
// would ever assert semantic ownership over the pointee. Internally,
|
|
|
|
// these methods call `self.vec.as_ptr()` and `self.vec.as_mut_ptr()` on
|
|
|
|
// the vector of bytes that are backing the `String`. That `Vec<u8>` holds a
|
|
|
|
// `Unique<u8>` internally. The second call to `Vec::as_mut_ptr(&mut self)`
|
|
|
|
// would then invalidate the pointers derived from `Vec::as_ptr(&self)`.
|
|
|
|
// Note that as long as `Unique<T>` is treated like any other raw pointer,
|
|
|
|
// this test case should pass. It is merely here as a canary test for
|
|
|
|
// potential future undefined behavior.
|
|
|
|
let mut x = String::from("Hello");
|
|
|
|
assert_eq!(x.remove(0), 'H');
|
|
|
|
x.insert(0, 'H');
|
|
|
|
assert_eq!(x, "Hello");
|
|
|
|
x.insert_str(x.len(), ", world!");
|
|
|
|
assert_eq!(x, "Hello, world!");
|
|
|
|
}
|
|
|
|
|
2016-06-17 23:52:24 -05:00
|
|
|
fn main() {
|
|
|
|
assert_eq!(empty(), "");
|
|
|
|
assert_eq!(hello(), "Hello, world!");
|
|
|
|
assert_eq!(hello_bytes(), b"Hello, world!");
|
|
|
|
assert_eq!(hello_bytes_fat(), b"Hello, world!");
|
2019-02-06 04:47:32 -06:00
|
|
|
|
2016-06-17 23:52:24 -05:00
|
|
|
fat_pointer_on_32_bit(); // Should run without crashing.
|
2019-02-06 04:47:32 -06:00
|
|
|
str_indexing();
|
2021-06-03 10:24:10 -05:00
|
|
|
unique_aliasing();
|
2016-06-17 23:52:24 -05:00
|
|
|
}
|