2018-11-17 03:05:30 -06:00
|
|
|
fn demo_mut_advanced_unique(mut our: Box<i32>) -> i32 {
|
2022-06-21 01:40:39 -05:00
|
|
|
unknown_code_1(&*our);
|
2018-11-17 03:05:30 -06:00
|
|
|
|
2022-06-21 01:40:39 -05:00
|
|
|
// This "re-asserts" uniqueness of the reference: After writing, we know
|
|
|
|
// our tag is at the top of the stack.
|
|
|
|
*our = 5;
|
2018-11-17 03:05:30 -06:00
|
|
|
|
2022-06-21 01:40:39 -05:00
|
|
|
unknown_code_2();
|
2018-11-17 03:05:30 -06:00
|
|
|
|
2022-06-21 01:40:39 -05:00
|
|
|
// We know this will return 5
|
2022-06-26 20:14:18 -05:00
|
|
|
*our
|
2018-11-17 03:05:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now comes the evil context
|
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
static mut LEAK: *mut i32 = ptr::null_mut();
|
|
|
|
|
2022-06-21 01:40:39 -05:00
|
|
|
fn unknown_code_1(x: &i32) {
|
|
|
|
unsafe {
|
|
|
|
LEAK = x as *const _ as *mut _;
|
|
|
|
}
|
|
|
|
}
|
2018-11-17 03:05:30 -06:00
|
|
|
|
2022-06-21 01:40:39 -05:00
|
|
|
fn unknown_code_2() {
|
|
|
|
unsafe {
|
2022-07-11 06:44:55 -05:00
|
|
|
*LEAK = 7; //~ ERROR: borrow stack
|
2022-06-21 01:40:39 -05:00
|
|
|
}
|
|
|
|
}
|
2018-11-17 03:05:30 -06:00
|
|
|
|
|
|
|
fn main() {
|
2018-11-21 03:19:00 -06:00
|
|
|
demo_mut_advanced_unique(Box::new(0));
|
2018-11-17 03:05:30 -06:00
|
|
|
}
|