add a bunch of compile-fail tests for validation
This commit is contained in:
parent
5e018b1deb
commit
34685044f9
13
tests/compile-fail/validation_aliasing_mut4.rs
Normal file
13
tests/compile-fail/validation_aliasing_mut4.rs
Normal file
@ -0,0 +1,13 @@
|
||||
#![allow(unused_variables)]
|
||||
|
||||
mod safe {
|
||||
use std::cell::Cell;
|
||||
|
||||
// Make sure &mut UnsafeCell also has a lock to it
|
||||
pub fn safe(x: &mut Cell<i32>, y: &i32) {} //~ ERROR: in conflict with lock WriteLock
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = &mut 0 as *mut _;
|
||||
unsafe { safe::safe(&mut *(x as *mut _), &*x) };
|
||||
}
|
22
tests/compile-fail/validation_buggy_split_at_mut.rs
Normal file
22
tests/compile-fail/validation_buggy_split_at_mut.rs
Normal file
@ -0,0 +1,22 @@
|
||||
#![allow(unused_variables)]
|
||||
|
||||
mod safe {
|
||||
use std::slice::from_raw_parts_mut;
|
||||
|
||||
pub fn split_at_mut<T>(self_: &mut [T], mid: usize) -> (&mut [T], &mut [T]) {
|
||||
let len = self_.len();
|
||||
let ptr = self_.as_mut_ptr();
|
||||
|
||||
unsafe {
|
||||
assert!(mid <= len);
|
||||
|
||||
(from_raw_parts_mut(ptr, len - mid), // BUG: should be "mid" instead of "len - mid"
|
||||
from_raw_parts_mut(ptr.offset(mid as isize), len - mid))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut array = [1,2,3,4];
|
||||
let _x = safe::split_at_mut(&mut array, 0); //~ ERROR: in conflict with lock WriteLock
|
||||
}
|
15
tests/compile-fail/validation_illegal_write.rs
Normal file
15
tests/compile-fail/validation_illegal_write.rs
Normal file
@ -0,0 +1,15 @@
|
||||
#![allow(unused_variables)]
|
||||
|
||||
mod safe {
|
||||
pub(crate) fn safe(x: &u32) {
|
||||
let x : &mut u32 = unsafe { &mut *(x as *const _ as *mut _) };
|
||||
*x = 42; //~ ERROR: in conflict with lock ReadLock
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let target = &mut 42;
|
||||
let target_ref = ⌖
|
||||
// do a reborrow, but we keep the lock
|
||||
safe::safe(&*target);
|
||||
}
|
20
tests/compile-fail/validation_pointer_smuggling.rs
Normal file
20
tests/compile-fail/validation_pointer_smuggling.rs
Normal file
@ -0,0 +1,20 @@
|
||||
#![allow(unused_variables)]
|
||||
|
||||
static mut PTR: *mut u8 = 0 as *mut _;
|
||||
|
||||
fn fun1(x: &mut u8) {
|
||||
unsafe {
|
||||
PTR = x;
|
||||
}
|
||||
}
|
||||
|
||||
fn fun2() {
|
||||
// Now we use a pointer we are not allowed to use
|
||||
let _x = unsafe { *PTR }; //~ ERROR: in conflict with lock WriteLock
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut val = 0;
|
||||
fun1(&mut val);
|
||||
fun2();
|
||||
}
|
16
tests/compile-fail/validation_recover1.rs
Normal file
16
tests/compile-fail/validation_recover1.rs
Normal file
@ -0,0 +1,16 @@
|
||||
#![allow(unused_variables)]
|
||||
|
||||
#[repr(u32)]
|
||||
enum Bool { True }
|
||||
|
||||
mod safe {
|
||||
pub(crate) fn safe(x: &mut super::Bool) {
|
||||
let x = x as *mut _ as *mut u32;
|
||||
unsafe { *x = 44; } // out-of-bounds enum discriminant
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut x = Bool::True;
|
||||
safe::safe(&mut x); //~ ERROR: invalid enum discriminant
|
||||
}
|
14
tests/compile-fail/validation_recover2.rs
Normal file
14
tests/compile-fail/validation_recover2.rs
Normal file
@ -0,0 +1,14 @@
|
||||
#![allow(unused_variables)]
|
||||
|
||||
mod safe {
|
||||
// This makes a ref that was passed to us via &mut alias with things it should not alias with
|
||||
pub(crate) fn safe(x: &mut &u32, target: &mut u32) {
|
||||
unsafe { *x = &mut *(target as *mut _); }
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let target = &mut 42;
|
||||
let mut target_alias = &42; // initial dummy value
|
||||
safe::safe(&mut target_alias, target); //~ ERROR: in conflict with lock ReadLock
|
||||
}
|
15
tests/compile-fail/validation_recover3.rs
Normal file
15
tests/compile-fail/validation_recover3.rs
Normal file
@ -0,0 +1,15 @@
|
||||
#![allow(unused_variables)]
|
||||
|
||||
mod safe {
|
||||
pub(crate) fn safe(x: *mut u32) {
|
||||
unsafe { *x = 42; } //~ ERROR: in conflict with lock WriteLock
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let target = &mut 42u32;
|
||||
let target2 = target as *mut _;
|
||||
drop(&mut *target); // reborrow
|
||||
// Now make sure we still got the lock
|
||||
safe::safe(target2);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user