From: Ralf Jung Date: Sat, 11 Apr 2020 07:30:13 +0000 (+0200) Subject: organize compile-fail tests in folders X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=b2bf4ec2f5e6938dfcebe0690beabbef22652afb;p=rust.git organize compile-fail tests in folders --- diff --git a/tests/compile-fail/alignment.rs b/tests/compile-fail/alignment.rs deleted file mode 100644 index bac1b92075a..00000000000 --- a/tests/compile-fail/alignment.rs +++ /dev/null @@ -1,11 +0,0 @@ -fn main() { - // miri always gives allocations the worst possible alignment, so a `u8` array is guaranteed - // to be at the virtual location 1 (so one byte offset from the ultimate alignemnt location 0) - let mut x = [0u8; 20]; - let x_ptr: *mut u8 = &mut x[0]; - let y_ptr = x_ptr as *mut u64; - unsafe { - *y_ptr = 42; //~ ERROR accessing memory with alignment 1, but alignment - } - panic!("unreachable in miri"); -} diff --git a/tests/compile-fail/alloc/deallocate-bad-alignment.rs b/tests/compile-fail/alloc/deallocate-bad-alignment.rs new file mode 100644 index 00000000000..621c05344b4 --- /dev/null +++ b/tests/compile-fail/alloc/deallocate-bad-alignment.rs @@ -0,0 +1,10 @@ +use std::alloc::{alloc, dealloc, Layout}; + +// error-pattern: allocation has size 1 and alignment 1, but gave size 1 and alignment 2 + +fn main() { + unsafe { + let x = alloc(Layout::from_size_align_unchecked(1, 1)); + dealloc(x, Layout::from_size_align_unchecked(1, 2)); + } +} diff --git a/tests/compile-fail/alloc/deallocate-bad-size.rs b/tests/compile-fail/alloc/deallocate-bad-size.rs new file mode 100644 index 00000000000..386bb56b909 --- /dev/null +++ b/tests/compile-fail/alloc/deallocate-bad-size.rs @@ -0,0 +1,10 @@ +use std::alloc::{alloc, dealloc, Layout}; + +// error-pattern: allocation has size 1 and alignment 1, but gave size 2 and alignment 1 + +fn main() { + unsafe { + let x = alloc(Layout::from_size_align_unchecked(1, 1)); + dealloc(x, Layout::from_size_align_unchecked(2, 1)); + } +} diff --git a/tests/compile-fail/alloc/deallocate-twice.rs b/tests/compile-fail/alloc/deallocate-twice.rs new file mode 100644 index 00000000000..67312b0d96d --- /dev/null +++ b/tests/compile-fail/alloc/deallocate-twice.rs @@ -0,0 +1,11 @@ +use std::alloc::{alloc, dealloc, Layout}; + +// error-pattern: dereferenced after this allocation got freed + +fn main() { + unsafe { + let x = alloc(Layout::from_size_align_unchecked(1, 1)); + dealloc(x, Layout::from_size_align_unchecked(1, 1)); + dealloc(x, Layout::from_size_align_unchecked(1, 1)); + } +} diff --git a/tests/compile-fail/alloc/reallocate-bad-size.rs b/tests/compile-fail/alloc/reallocate-bad-size.rs new file mode 100644 index 00000000000..7826f26f9b8 --- /dev/null +++ b/tests/compile-fail/alloc/reallocate-bad-size.rs @@ -0,0 +1,10 @@ +use std::alloc::{alloc, realloc, Layout}; + +// error-pattern: allocation has size 1 and alignment 1, but gave size 2 and alignment 1 + +fn main() { + unsafe { + let x = alloc(Layout::from_size_align_unchecked(1, 1)); + realloc(x, Layout::from_size_align_unchecked(2, 1), 1); + } +} diff --git a/tests/compile-fail/alloc/reallocate-change-alloc.rs b/tests/compile-fail/alloc/reallocate-change-alloc.rs new file mode 100644 index 00000000000..29702967696 --- /dev/null +++ b/tests/compile-fail/alloc/reallocate-change-alloc.rs @@ -0,0 +1,9 @@ +use std::alloc::{alloc, realloc, Layout}; + +fn main() { + unsafe { + let x = alloc(Layout::from_size_align_unchecked(1, 1)); + realloc(x, Layout::from_size_align_unchecked(1, 1), 1); + let _z = *x; //~ ERROR dereferenced after this allocation got freed + } +} diff --git a/tests/compile-fail/alloc/reallocate-dangling.rs b/tests/compile-fail/alloc/reallocate-dangling.rs new file mode 100644 index 00000000000..702ddc0724a --- /dev/null +++ b/tests/compile-fail/alloc/reallocate-dangling.rs @@ -0,0 +1,11 @@ +use std::alloc::{alloc, dealloc, realloc, Layout}; + +// error-pattern: dereferenced after this allocation got freed + +fn main() { + unsafe { + let x = alloc(Layout::from_size_align_unchecked(1, 1)); + dealloc(x, Layout::from_size_align_unchecked(1, 1)); + realloc(x, Layout::from_size_align_unchecked(1, 1), 1); + } +} diff --git a/tests/compile-fail/alloc/stack_free.rs b/tests/compile-fail/alloc/stack_free.rs new file mode 100644 index 00000000000..50a590e448a --- /dev/null +++ b/tests/compile-fail/alloc/stack_free.rs @@ -0,0 +1,10 @@ +// Validation/SB changes why we fail +// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows + +// error-pattern: deallocating stack variable memory using Rust heap deallocation operation + +fn main() { + let x = 42; + let bad_box = unsafe { std::mem::transmute::<&i32, Box>(&x) }; + drop(bad_box); +} diff --git a/tests/compile-fail/assume.rs b/tests/compile-fail/assume.rs deleted file mode 100644 index 7b18cab7980..00000000000 --- a/tests/compile-fail/assume.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![feature(core_intrinsics)] - -fn main() { - let x = 5; - unsafe { - std::intrinsics::assume(x < 10); - std::intrinsics::assume(x > 1); - std::intrinsics::assume(x > 42); //~ `assume` intrinsic called with `false` - } -} diff --git a/tests/compile-fail/atomic_unaligned.rs b/tests/compile-fail/atomic_unaligned.rs deleted file mode 100644 index bdec0ff504b..00000000000 --- a/tests/compile-fail/atomic_unaligned.rs +++ /dev/null @@ -1,12 +0,0 @@ -#![feature(core_intrinsics)] - -fn main() { - // Do a 4-aligned u64 atomic access. That should be UB on all platforms, - // even if u64 only has alignment 4. - let z = [0u32; 2]; - let zptr = &z as *const _ as *const u64; - unsafe { - ::std::intrinsics::atomic_load(zptr); - //~^ ERROR accessing memory with alignment 4, but alignment 8 is required - } -} diff --git a/tests/compile-fail/cast_box_int_to_fn_ptr.rs b/tests/compile-fail/cast_box_int_to_fn_ptr.rs deleted file mode 100644 index 9eea9d92dcd..00000000000 --- a/tests/compile-fail/cast_box_int_to_fn_ptr.rs +++ /dev/null @@ -1,11 +0,0 @@ -// Validation makes this fail in the wrong place -// compile-flags: -Zmiri-disable-validation - -fn main() { - let b = Box::new(42); - let g = unsafe { - std::mem::transmute::<&Box, &fn(i32)>(&b) - }; - - (*g)(42) //~ ERROR it does not point to a function -} diff --git a/tests/compile-fail/cast_fn_ptr1.rs b/tests/compile-fail/cast_fn_ptr1.rs deleted file mode 100644 index 3702ec8c94c..00000000000 --- a/tests/compile-fail/cast_fn_ptr1.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - fn f() {} - - let g = unsafe { - std::mem::transmute::(f) - }; - - g(42) //~ ERROR calling a function with more arguments than it expected -} diff --git a/tests/compile-fail/cast_fn_ptr2.rs b/tests/compile-fail/cast_fn_ptr2.rs deleted file mode 100644 index 39f0867489a..00000000000 --- a/tests/compile-fail/cast_fn_ptr2.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - fn f(_ : (i32,i32)) {} - - let g = unsafe { - std::mem::transmute::(f) - }; - - g(42) //~ ERROR calling a function with argument of type (i32, i32) passing data of type i32 -} diff --git a/tests/compile-fail/cast_fn_ptr3.rs b/tests/compile-fail/cast_fn_ptr3.rs deleted file mode 100644 index 3523db24fa3..00000000000 --- a/tests/compile-fail/cast_fn_ptr3.rs +++ /dev/null @@ -1,10 +0,0 @@ -fn main() { - fn f(_ : (i32,i32)) {} - - let g = unsafe { - std::mem::transmute::(f) - }; - - g() //~ ERROR calling a function with fewer arguments than it requires -} - diff --git a/tests/compile-fail/cast_fn_ptr4.rs b/tests/compile-fail/cast_fn_ptr4.rs deleted file mode 100644 index 22a36a71cef..00000000000 --- a/tests/compile-fail/cast_fn_ptr4.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - fn f(_ : *const [i32]) {} - - let g = unsafe { - std::mem::transmute::(f) - }; - - g(&42 as *const i32) //~ ERROR calling a function with argument of type *const [i32] passing data of type *const i32 -} diff --git a/tests/compile-fail/cast_fn_ptr5.rs b/tests/compile-fail/cast_fn_ptr5.rs deleted file mode 100644 index fb2b4403363..00000000000 --- a/tests/compile-fail/cast_fn_ptr5.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - fn f() -> u32 { 42 } - - let g = unsafe { - std::mem::transmute:: u32, fn()>(f) - }; - - g() //~ ERROR calling a function with return type u32 passing return place of type () -} diff --git a/tests/compile-fail/cast_int_to_fn_ptr.rs b/tests/compile-fail/cast_int_to_fn_ptr.rs deleted file mode 100644 index 3000779a933..00000000000 --- a/tests/compile-fail/cast_int_to_fn_ptr.rs +++ /dev/null @@ -1,10 +0,0 @@ -// Validation makes this fail in the wrong place -// compile-flags: -Zmiri-disable-validation - -fn main() { - let g = unsafe { - std::mem::transmute::(42) - }; - - g(42) //~ ERROR invalid use of 42 as a pointer -} diff --git a/tests/compile-fail/copy_null.rs b/tests/compile-fail/copy_null.rs deleted file mode 100644 index b14bdc4b386..00000000000 --- a/tests/compile-fail/copy_null.rs +++ /dev/null @@ -1,13 +0,0 @@ -#![feature(intrinsics)] - -// Directly call intrinsic to avoid debug assertions in libstd -extern "rust-intrinsic" { - fn copy_nonoverlapping(src: *const T, dst: *mut T, count: usize); -} - -fn main() { - let mut data = [0u16; 4]; - let ptr = &mut data[0] as *mut u16; - // Even copying 0 elements from NULL should error. - unsafe { copy_nonoverlapping(std::ptr::null(), ptr, 0); } //~ ERROR: invalid use of NULL pointer -} diff --git a/tests/compile-fail/copy_overflow.rs b/tests/compile-fail/copy_overflow.rs deleted file mode 100644 index c75cf6917b1..00000000000 --- a/tests/compile-fail/copy_overflow.rs +++ /dev/null @@ -1,10 +0,0 @@ -// error-pattern: overflow computing total size of `copy` -use std::mem; - -fn main() { - let x = 0; - let mut y = 0; - unsafe { - (&mut y as *mut i32).copy_from(&x, 1usize << (mem::size_of::() * 8 - 1)); - } -} diff --git a/tests/compile-fail/copy_overlapping.rs b/tests/compile-fail/copy_overlapping.rs deleted file mode 100644 index 76e1e20d217..00000000000 --- a/tests/compile-fail/copy_overlapping.rs +++ /dev/null @@ -1,16 +0,0 @@ -//error-pattern: copy_nonoverlapping called on overlapping ranges -#![feature(intrinsics)] - -// Directly call intrinsic to avoid debug assertions in libstd -extern "rust-intrinsic" { - fn copy_nonoverlapping(src: *const T, dst: *mut T, count: usize); -} - -fn main() { - let mut data = [0u8; 16]; - unsafe { - let a = data.as_mut_ptr(); - let b = a.wrapping_offset(1) as *mut _; - copy_nonoverlapping(a, b, 2); - } -} diff --git a/tests/compile-fail/copy_unaligned.rs b/tests/compile-fail/copy_unaligned.rs deleted file mode 100644 index a2a47622391..00000000000 --- a/tests/compile-fail/copy_unaligned.rs +++ /dev/null @@ -1,14 +0,0 @@ -//error-pattern: accessing memory with alignment 1, but alignment 2 is required -#![feature(intrinsics)] - -// Directly call intrinsic to avoid debug assertions in libstd -extern "rust-intrinsic" { - fn copy_nonoverlapping(src: *const T, dst: *mut T, count: usize); -} - -fn main() { - let mut data = [0u16; 8]; - let ptr = (&mut data[0] as *mut u16 as *mut u8).wrapping_add(1) as *mut u16; - // Even copying 0 elements to something unaligned should error - unsafe { copy_nonoverlapping(&data[5], ptr, 0); } -} diff --git a/tests/compile-fail/ctlz_nonzero.rs b/tests/compile-fail/ctlz_nonzero.rs deleted file mode 100644 index e82ed89da18..00000000000 --- a/tests/compile-fail/ctlz_nonzero.rs +++ /dev/null @@ -1,15 +0,0 @@ -#![feature(intrinsics)] - -mod rusti { - extern "rust-intrinsic" { - pub fn ctlz_nonzero(x: T) -> T; - } -} - -pub fn main() { - unsafe { - use crate::rusti::*; - - ctlz_nonzero(0u8); //~ ERROR `ctlz_nonzero` called on 0 - } -} diff --git a/tests/compile-fail/cttz_nonzero.rs b/tests/compile-fail/cttz_nonzero.rs deleted file mode 100644 index 205b5520811..00000000000 --- a/tests/compile-fail/cttz_nonzero.rs +++ /dev/null @@ -1,15 +0,0 @@ -#![feature(intrinsics)] - -mod rusti { - extern "rust-intrinsic" { - pub fn cttz_nonzero(x: T) -> T; - } -} - -pub fn main() { - unsafe { - use crate::rusti::*; - - cttz_nonzero(0u8); //~ ERROR `cttz_nonzero` called on 0 - } -} diff --git a/tests/compile-fail/dangling_pointer_deref.rs b/tests/compile-fail/dangling_pointer_deref.rs deleted file mode 100644 index f2c7ec584fe..00000000000 --- a/tests/compile-fail/dangling_pointer_deref.rs +++ /dev/null @@ -1,8 +0,0 @@ -fn main() { - let p = { - let b = Box::new(42); - &*b as *const i32 - }; - let x = unsafe { *p }; //~ ERROR dereferenced after this allocation got freed - panic!("this should never print: {}", x); -} diff --git a/tests/compile-fail/dangling_pointers/dangling_pointer_deref.rs b/tests/compile-fail/dangling_pointers/dangling_pointer_deref.rs new file mode 100644 index 00000000000..f2c7ec584fe --- /dev/null +++ b/tests/compile-fail/dangling_pointers/dangling_pointer_deref.rs @@ -0,0 +1,8 @@ +fn main() { + let p = { + let b = Box::new(42); + &*b as *const i32 + }; + let x = unsafe { *p }; //~ ERROR dereferenced after this allocation got freed + panic!("this should never print: {}", x); +} diff --git a/tests/compile-fail/dangling_pointers/dangling_zst_deref.rs b/tests/compile-fail/dangling_pointers/dangling_zst_deref.rs new file mode 100644 index 00000000000..13e5f9d3217 --- /dev/null +++ b/tests/compile-fail/dangling_pointers/dangling_zst_deref.rs @@ -0,0 +1,7 @@ +fn main() { + let p = { + let b = Box::new(42); + &*b as *const i32 as *const () + }; + let _x = unsafe { *p }; //~ ERROR dereferenced after this allocation got freed +} diff --git a/tests/compile-fail/dangling_pointers/deref-invalid-ptr.rs b/tests/compile-fail/dangling_pointers/deref-invalid-ptr.rs new file mode 100644 index 00000000000..e0bba8c7c73 --- /dev/null +++ b/tests/compile-fail/dangling_pointers/deref-invalid-ptr.rs @@ -0,0 +1,7 @@ +// This should fail even without validation. +// compile-flags: -Zmiri-disable-validation + +fn main() { + let x = 16usize as *const u32; + let _y = unsafe { &*x as *const u32 }; //~ ERROR invalid use of 16 as a pointer +} diff --git a/tests/compile-fail/dangling_pointers/deref-partially-dangling.rs b/tests/compile-fail/dangling_pointers/deref-partially-dangling.rs new file mode 100644 index 00000000000..a6d3aae414d --- /dev/null +++ b/tests/compile-fail/dangling_pointers/deref-partially-dangling.rs @@ -0,0 +1,8 @@ +// Deref a raw ptr to access a field of a large struct, where the field +// is allocated but not the entire struct is. +fn main() { + let x = (1, 13); + let xptr = &x as *const _ as *const (i32, i32, i32); + let val = unsafe { (*xptr).1 }; //~ ERROR pointer must be in-bounds at offset 12, but is outside bounds of alloc + assert_eq!(val, 13); +} diff --git a/tests/compile-fail/dangling_pointers/maybe_null_pointer_deref_zst.rs b/tests/compile-fail/dangling_pointers/maybe_null_pointer_deref_zst.rs new file mode 100644 index 00000000000..d9f5ad4c696 --- /dev/null +++ b/tests/compile-fail/dangling_pointers/maybe_null_pointer_deref_zst.rs @@ -0,0 +1,5 @@ +fn main() { + // This pointer *could* be NULL so we cannot load from it, not even at ZST + let ptr = (&0u8 as *const u8).wrapping_sub(0x800) as *const (); + let _x: () = unsafe { *ptr }; //~ ERROR outside bounds +} diff --git a/tests/compile-fail/dangling_pointers/maybe_null_pointer_write_zst.rs b/tests/compile-fail/dangling_pointers/maybe_null_pointer_write_zst.rs new file mode 100644 index 00000000000..ef46a469c3a --- /dev/null +++ b/tests/compile-fail/dangling_pointers/maybe_null_pointer_write_zst.rs @@ -0,0 +1,8 @@ +fn main() { + // This pointer *could* be NULL so we cannot load from it, not even at ZST. + // Not using the () type here, as writes of that type do not even have MIR generated. + // Also not assigning directly as that's array initialization, not assignment. + let zst_val = [1u8; 0]; + let ptr = (&0u8 as *const u8).wrapping_sub(0x800) as *mut [u8; 0]; + unsafe { *ptr = zst_val; } //~ ERROR outside bounds +} diff --git a/tests/compile-fail/dangling_pointers/out_of_bounds_read1.rs b/tests/compile-fail/dangling_pointers/out_of_bounds_read1.rs new file mode 100644 index 00000000000..0c175af5266 --- /dev/null +++ b/tests/compile-fail/dangling_pointers/out_of_bounds_read1.rs @@ -0,0 +1,5 @@ +fn main() { + let v: Vec = vec![1, 2]; + let x = unsafe { *v.as_ptr().wrapping_offset(5) }; //~ ERROR outside bounds of alloc + panic!("this should never print: {}", x); +} diff --git a/tests/compile-fail/dangling_pointers/out_of_bounds_read2.rs b/tests/compile-fail/dangling_pointers/out_of_bounds_read2.rs new file mode 100644 index 00000000000..0c175af5266 --- /dev/null +++ b/tests/compile-fail/dangling_pointers/out_of_bounds_read2.rs @@ -0,0 +1,5 @@ +fn main() { + let v: Vec = vec![1, 2]; + let x = unsafe { *v.as_ptr().wrapping_offset(5) }; //~ ERROR outside bounds of alloc + panic!("this should never print: {}", x); +} diff --git a/tests/compile-fail/dangling_pointers/wild_pointer_deref.rs b/tests/compile-fail/dangling_pointers/wild_pointer_deref.rs new file mode 100644 index 00000000000..5780cccdb84 --- /dev/null +++ b/tests/compile-fail/dangling_pointers/wild_pointer_deref.rs @@ -0,0 +1,5 @@ +fn main() { + let p = 44 as *const i32; + let x = unsafe { *p }; //~ ERROR invalid use of 44 as a pointer + panic!("this should never print: {}", x); +} diff --git a/tests/compile-fail/dangling_zst_deref.rs b/tests/compile-fail/dangling_zst_deref.rs deleted file mode 100644 index 13e5f9d3217..00000000000 --- a/tests/compile-fail/dangling_zst_deref.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() { - let p = { - let b = Box::new(42); - &*b as *const i32 as *const () - }; - let _x = unsafe { *p }; //~ ERROR dereferenced after this allocation got freed -} diff --git a/tests/compile-fail/deallocate-bad-alignment.rs b/tests/compile-fail/deallocate-bad-alignment.rs deleted file mode 100644 index 621c05344b4..00000000000 --- a/tests/compile-fail/deallocate-bad-alignment.rs +++ /dev/null @@ -1,10 +0,0 @@ -use std::alloc::{alloc, dealloc, Layout}; - -// error-pattern: allocation has size 1 and alignment 1, but gave size 1 and alignment 2 - -fn main() { - unsafe { - let x = alloc(Layout::from_size_align_unchecked(1, 1)); - dealloc(x, Layout::from_size_align_unchecked(1, 2)); - } -} diff --git a/tests/compile-fail/deallocate-bad-size.rs b/tests/compile-fail/deallocate-bad-size.rs deleted file mode 100644 index 386bb56b909..00000000000 --- a/tests/compile-fail/deallocate-bad-size.rs +++ /dev/null @@ -1,10 +0,0 @@ -use std::alloc::{alloc, dealloc, Layout}; - -// error-pattern: allocation has size 1 and alignment 1, but gave size 2 and alignment 1 - -fn main() { - unsafe { - let x = alloc(Layout::from_size_align_unchecked(1, 1)); - dealloc(x, Layout::from_size_align_unchecked(2, 1)); - } -} diff --git a/tests/compile-fail/deallocate-twice.rs b/tests/compile-fail/deallocate-twice.rs deleted file mode 100644 index 67312b0d96d..00000000000 --- a/tests/compile-fail/deallocate-twice.rs +++ /dev/null @@ -1,11 +0,0 @@ -use std::alloc::{alloc, dealloc, Layout}; - -// error-pattern: dereferenced after this allocation got freed - -fn main() { - unsafe { - let x = alloc(Layout::from_size_align_unchecked(1, 1)); - dealloc(x, Layout::from_size_align_unchecked(1, 1)); - dealloc(x, Layout::from_size_align_unchecked(1, 1)); - } -} diff --git a/tests/compile-fail/deref-invalid-ptr.rs b/tests/compile-fail/deref-invalid-ptr.rs deleted file mode 100644 index 561017293a1..00000000000 --- a/tests/compile-fail/deref-invalid-ptr.rs +++ /dev/null @@ -1,7 +0,0 @@ -// This should fail even without validation. -// compile-flags: -Zmiri-disable-validation - -fn main() { - let x = 2usize as *const u32; - let _y = unsafe { &*x as *const u32 }; //~ ERROR invalid use of 2 as a pointer -} diff --git a/tests/compile-fail/deref-partially-dangling.rs b/tests/compile-fail/deref-partially-dangling.rs deleted file mode 100644 index a6d3aae414d..00000000000 --- a/tests/compile-fail/deref-partially-dangling.rs +++ /dev/null @@ -1,8 +0,0 @@ -// Deref a raw ptr to access a field of a large struct, where the field -// is allocated but not the entire struct is. -fn main() { - let x = (1, 13); - let xptr = &x as *const _ as *const (i32, i32, i32); - let val = unsafe { (*xptr).1 }; //~ ERROR pointer must be in-bounds at offset 12, but is outside bounds of alloc - assert_eq!(val, 13); -} diff --git a/tests/compile-fail/deref_fn_ptr.rs b/tests/compile-fail/deref_fn_ptr.rs deleted file mode 100644 index e604f96ea16..00000000000 --- a/tests/compile-fail/deref_fn_ptr.rs +++ /dev/null @@ -1,8 +0,0 @@ -fn f() {} - -fn main() { - let x: u8 = unsafe { - *std::mem::transmute::(f) //~ ERROR contains a function - }; - panic!("this should never print: {}", x); -} diff --git a/tests/compile-fail/div-by-zero-1.rs b/tests/compile-fail/div-by-zero-1.rs deleted file mode 100644 index d67d06dc1e6..00000000000 --- a/tests/compile-fail/div-by-zero-1.rs +++ /dev/null @@ -1,9 +0,0 @@ -#![feature(core_intrinsics)] - -use std::intrinsics::*; - -fn main() { - unsafe { - let _n = unchecked_div(1i64, 0); //~ERROR dividing by zero - } -} diff --git a/tests/compile-fail/div-by-zero-2.rs b/tests/compile-fail/div-by-zero-2.rs deleted file mode 100644 index e904049e3b4..00000000000 --- a/tests/compile-fail/div-by-zero-2.rs +++ /dev/null @@ -1,9 +0,0 @@ -#![feature(core_intrinsics)] - -use std::intrinsics::*; - -fn main() { - unsafe { - let _n = unchecked_rem(3u32, 0); //~ ERROR calculating the remainder with a divisor of zero - } -} diff --git a/tests/compile-fail/exact_div1.rs b/tests/compile-fail/exact_div1.rs deleted file mode 100644 index 171bedeadc6..00000000000 --- a/tests/compile-fail/exact_div1.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![feature(core_intrinsics)] -fn main() { - // divison by 0 - unsafe { std::intrinsics::exact_div(2, 0); } //~ ERROR divisor of zero -} diff --git a/tests/compile-fail/exact_div2.rs b/tests/compile-fail/exact_div2.rs deleted file mode 100644 index 9b9ae807088..00000000000 --- a/tests/compile-fail/exact_div2.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![feature(core_intrinsics)] -fn main() { - // divison with a remainder - unsafe { std::intrinsics::exact_div(2u16, 3); } //~ ERROR 2u16 cannot be divided by 3u16 without remainder -} diff --git a/tests/compile-fail/exact_div3.rs b/tests/compile-fail/exact_div3.rs deleted file mode 100644 index 1cd112acfc2..00000000000 --- a/tests/compile-fail/exact_div3.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![feature(core_intrinsics)] -fn main() { - // signed divison with a remainder - unsafe { std::intrinsics::exact_div(-19i8, 2); } //~ ERROR -19i8 cannot be divided by 2i8 without remainder -} diff --git a/tests/compile-fail/exact_div4.rs b/tests/compile-fail/exact_div4.rs deleted file mode 100644 index c241b2df660..00000000000 --- a/tests/compile-fail/exact_div4.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![feature(core_intrinsics)] -fn main() { - // divison of MIN by -1 - unsafe { std::intrinsics::exact_div(i64::MIN, -1); } //~ ERROR result of dividing MIN by -1 cannot be represented -} diff --git a/tests/compile-fail/execute_memory.rs b/tests/compile-fail/execute_memory.rs deleted file mode 100644 index 2e6b58a753c..00000000000 --- a/tests/compile-fail/execute_memory.rs +++ /dev/null @@ -1,12 +0,0 @@ -// Validation makes this fail in the wrong place -// compile-flags: -Zmiri-disable-validation - -#![feature(box_syntax)] - -fn main() { - let x = box 42; - unsafe { - let f = std::mem::transmute::, fn()>(x); - f() //~ ERROR function pointer but it does not point to a function - } -} diff --git a/tests/compile-fail/fn_ptr_offset.rs b/tests/compile-fail/fn_ptr_offset.rs deleted file mode 100644 index 7e509d53c26..00000000000 --- a/tests/compile-fail/fn_ptr_offset.rs +++ /dev/null @@ -1,14 +0,0 @@ -// Validation makes this fail in the wrong place -// compile-flags: -Zmiri-disable-validation - -use std::mem; - -fn f() {} - -fn main() { - let x : fn() = f; - let y : *mut u8 = unsafe { mem::transmute(x) }; - let y = y.wrapping_offset(1); - let x : fn() = unsafe { mem::transmute(y) }; - x(); //~ ERROR function pointer but it does not point to a function -} diff --git a/tests/compile-fail/function_pointers/cast_box_int_to_fn_ptr.rs b/tests/compile-fail/function_pointers/cast_box_int_to_fn_ptr.rs new file mode 100644 index 00000000000..9eea9d92dcd --- /dev/null +++ b/tests/compile-fail/function_pointers/cast_box_int_to_fn_ptr.rs @@ -0,0 +1,11 @@ +// Validation makes this fail in the wrong place +// compile-flags: -Zmiri-disable-validation + +fn main() { + let b = Box::new(42); + let g = unsafe { + std::mem::transmute::<&Box, &fn(i32)>(&b) + }; + + (*g)(42) //~ ERROR it does not point to a function +} diff --git a/tests/compile-fail/function_pointers/cast_fn_ptr1.rs b/tests/compile-fail/function_pointers/cast_fn_ptr1.rs new file mode 100644 index 00000000000..3702ec8c94c --- /dev/null +++ b/tests/compile-fail/function_pointers/cast_fn_ptr1.rs @@ -0,0 +1,9 @@ +fn main() { + fn f() {} + + let g = unsafe { + std::mem::transmute::(f) + }; + + g(42) //~ ERROR calling a function with more arguments than it expected +} diff --git a/tests/compile-fail/function_pointers/cast_fn_ptr2.rs b/tests/compile-fail/function_pointers/cast_fn_ptr2.rs new file mode 100644 index 00000000000..39f0867489a --- /dev/null +++ b/tests/compile-fail/function_pointers/cast_fn_ptr2.rs @@ -0,0 +1,9 @@ +fn main() { + fn f(_ : (i32,i32)) {} + + let g = unsafe { + std::mem::transmute::(f) + }; + + g(42) //~ ERROR calling a function with argument of type (i32, i32) passing data of type i32 +} diff --git a/tests/compile-fail/function_pointers/cast_fn_ptr3.rs b/tests/compile-fail/function_pointers/cast_fn_ptr3.rs new file mode 100644 index 00000000000..3523db24fa3 --- /dev/null +++ b/tests/compile-fail/function_pointers/cast_fn_ptr3.rs @@ -0,0 +1,10 @@ +fn main() { + fn f(_ : (i32,i32)) {} + + let g = unsafe { + std::mem::transmute::(f) + }; + + g() //~ ERROR calling a function with fewer arguments than it requires +} + diff --git a/tests/compile-fail/function_pointers/cast_fn_ptr4.rs b/tests/compile-fail/function_pointers/cast_fn_ptr4.rs new file mode 100644 index 00000000000..22a36a71cef --- /dev/null +++ b/tests/compile-fail/function_pointers/cast_fn_ptr4.rs @@ -0,0 +1,9 @@ +fn main() { + fn f(_ : *const [i32]) {} + + let g = unsafe { + std::mem::transmute::(f) + }; + + g(&42 as *const i32) //~ ERROR calling a function with argument of type *const [i32] passing data of type *const i32 +} diff --git a/tests/compile-fail/function_pointers/cast_fn_ptr5.rs b/tests/compile-fail/function_pointers/cast_fn_ptr5.rs new file mode 100644 index 00000000000..fb2b4403363 --- /dev/null +++ b/tests/compile-fail/function_pointers/cast_fn_ptr5.rs @@ -0,0 +1,9 @@ +fn main() { + fn f() -> u32 { 42 } + + let g = unsafe { + std::mem::transmute:: u32, fn()>(f) + }; + + g() //~ ERROR calling a function with return type u32 passing return place of type () +} diff --git a/tests/compile-fail/function_pointers/cast_int_to_fn_ptr.rs b/tests/compile-fail/function_pointers/cast_int_to_fn_ptr.rs new file mode 100644 index 00000000000..3000779a933 --- /dev/null +++ b/tests/compile-fail/function_pointers/cast_int_to_fn_ptr.rs @@ -0,0 +1,10 @@ +// Validation makes this fail in the wrong place +// compile-flags: -Zmiri-disable-validation + +fn main() { + let g = unsafe { + std::mem::transmute::(42) + }; + + g(42) //~ ERROR invalid use of 42 as a pointer +} diff --git a/tests/compile-fail/function_pointers/deref_fn_ptr.rs b/tests/compile-fail/function_pointers/deref_fn_ptr.rs new file mode 100644 index 00000000000..e604f96ea16 --- /dev/null +++ b/tests/compile-fail/function_pointers/deref_fn_ptr.rs @@ -0,0 +1,8 @@ +fn f() {} + +fn main() { + let x: u8 = unsafe { + *std::mem::transmute::(f) //~ ERROR contains a function + }; + panic!("this should never print: {}", x); +} diff --git a/tests/compile-fail/function_pointers/execute_memory.rs b/tests/compile-fail/function_pointers/execute_memory.rs new file mode 100644 index 00000000000..2e6b58a753c --- /dev/null +++ b/tests/compile-fail/function_pointers/execute_memory.rs @@ -0,0 +1,12 @@ +// Validation makes this fail in the wrong place +// compile-flags: -Zmiri-disable-validation + +#![feature(box_syntax)] + +fn main() { + let x = box 42; + unsafe { + let f = std::mem::transmute::, fn()>(x); + f() //~ ERROR function pointer but it does not point to a function + } +} diff --git a/tests/compile-fail/function_pointers/fn_ptr_offset.rs b/tests/compile-fail/function_pointers/fn_ptr_offset.rs new file mode 100644 index 00000000000..7e509d53c26 --- /dev/null +++ b/tests/compile-fail/function_pointers/fn_ptr_offset.rs @@ -0,0 +1,14 @@ +// Validation makes this fail in the wrong place +// compile-flags: -Zmiri-disable-validation + +use std::mem; + +fn f() {} + +fn main() { + let x : fn() = f; + let y : *mut u8 = unsafe { mem::transmute(x) }; + let y = y.wrapping_offset(1); + let x : fn() = unsafe { mem::transmute(y) }; + x(); //~ ERROR function pointer but it does not point to a function +} diff --git a/tests/compile-fail/intptrcast_alignment_check.rs b/tests/compile-fail/intptrcast_alignment_check.rs deleted file mode 100644 index 1a8df5eaced..00000000000 --- a/tests/compile-fail/intptrcast_alignment_check.rs +++ /dev/null @@ -1,12 +0,0 @@ -// Even with intptrcast and without validation, we want to be *sure* to catch bugs -// that arise from pointers being insufficiently aligned. The only way to achieve -// that is not not let programs exploit integer information for alignment, so here -// we test that this is indeed the case. -fn main() { - let x = &mut [0u8; 3]; - let base_addr = x as *mut _ as usize; - let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr+1 }; - let u16_ptr = base_addr_aligned as *mut u16; - unsafe { *u16_ptr = 2; } //~ ERROR memory with alignment 1, but alignment 2 is required - println!("{:?}", x); -} diff --git a/tests/compile-fail/intrinsics/assume.rs b/tests/compile-fail/intrinsics/assume.rs new file mode 100644 index 00000000000..7b18cab7980 --- /dev/null +++ b/tests/compile-fail/intrinsics/assume.rs @@ -0,0 +1,10 @@ +#![feature(core_intrinsics)] + +fn main() { + let x = 5; + unsafe { + std::intrinsics::assume(x < 10); + std::intrinsics::assume(x > 1); + std::intrinsics::assume(x > 42); //~ `assume` intrinsic called with `false` + } +} diff --git a/tests/compile-fail/intrinsics/copy_null.rs b/tests/compile-fail/intrinsics/copy_null.rs new file mode 100644 index 00000000000..b14bdc4b386 --- /dev/null +++ b/tests/compile-fail/intrinsics/copy_null.rs @@ -0,0 +1,13 @@ +#![feature(intrinsics)] + +// Directly call intrinsic to avoid debug assertions in libstd +extern "rust-intrinsic" { + fn copy_nonoverlapping(src: *const T, dst: *mut T, count: usize); +} + +fn main() { + let mut data = [0u16; 4]; + let ptr = &mut data[0] as *mut u16; + // Even copying 0 elements from NULL should error. + unsafe { copy_nonoverlapping(std::ptr::null(), ptr, 0); } //~ ERROR: invalid use of NULL pointer +} diff --git a/tests/compile-fail/intrinsics/copy_overflow.rs b/tests/compile-fail/intrinsics/copy_overflow.rs new file mode 100644 index 00000000000..c75cf6917b1 --- /dev/null +++ b/tests/compile-fail/intrinsics/copy_overflow.rs @@ -0,0 +1,10 @@ +// error-pattern: overflow computing total size of `copy` +use std::mem; + +fn main() { + let x = 0; + let mut y = 0; + unsafe { + (&mut y as *mut i32).copy_from(&x, 1usize << (mem::size_of::() * 8 - 1)); + } +} diff --git a/tests/compile-fail/intrinsics/copy_overlapping.rs b/tests/compile-fail/intrinsics/copy_overlapping.rs new file mode 100644 index 00000000000..76e1e20d217 --- /dev/null +++ b/tests/compile-fail/intrinsics/copy_overlapping.rs @@ -0,0 +1,16 @@ +//error-pattern: copy_nonoverlapping called on overlapping ranges +#![feature(intrinsics)] + +// Directly call intrinsic to avoid debug assertions in libstd +extern "rust-intrinsic" { + fn copy_nonoverlapping(src: *const T, dst: *mut T, count: usize); +} + +fn main() { + let mut data = [0u8; 16]; + unsafe { + let a = data.as_mut_ptr(); + let b = a.wrapping_offset(1) as *mut _; + copy_nonoverlapping(a, b, 2); + } +} diff --git a/tests/compile-fail/intrinsics/copy_unaligned.rs b/tests/compile-fail/intrinsics/copy_unaligned.rs new file mode 100644 index 00000000000..a2a47622391 --- /dev/null +++ b/tests/compile-fail/intrinsics/copy_unaligned.rs @@ -0,0 +1,14 @@ +//error-pattern: accessing memory with alignment 1, but alignment 2 is required +#![feature(intrinsics)] + +// Directly call intrinsic to avoid debug assertions in libstd +extern "rust-intrinsic" { + fn copy_nonoverlapping(src: *const T, dst: *mut T, count: usize); +} + +fn main() { + let mut data = [0u16; 8]; + let ptr = (&mut data[0] as *mut u16 as *mut u8).wrapping_add(1) as *mut u16; + // Even copying 0 elements to something unaligned should error + unsafe { copy_nonoverlapping(&data[5], ptr, 0); } +} diff --git a/tests/compile-fail/intrinsics/ctlz_nonzero.rs b/tests/compile-fail/intrinsics/ctlz_nonzero.rs new file mode 100644 index 00000000000..e82ed89da18 --- /dev/null +++ b/tests/compile-fail/intrinsics/ctlz_nonzero.rs @@ -0,0 +1,15 @@ +#![feature(intrinsics)] + +mod rusti { + extern "rust-intrinsic" { + pub fn ctlz_nonzero(x: T) -> T; + } +} + +pub fn main() { + unsafe { + use crate::rusti::*; + + ctlz_nonzero(0u8); //~ ERROR `ctlz_nonzero` called on 0 + } +} diff --git a/tests/compile-fail/intrinsics/cttz_nonzero.rs b/tests/compile-fail/intrinsics/cttz_nonzero.rs new file mode 100644 index 00000000000..205b5520811 --- /dev/null +++ b/tests/compile-fail/intrinsics/cttz_nonzero.rs @@ -0,0 +1,15 @@ +#![feature(intrinsics)] + +mod rusti { + extern "rust-intrinsic" { + pub fn cttz_nonzero(x: T) -> T; + } +} + +pub fn main() { + unsafe { + use crate::rusti::*; + + cttz_nonzero(0u8); //~ ERROR `cttz_nonzero` called on 0 + } +} diff --git a/tests/compile-fail/intrinsics/div-by-zero-1.rs b/tests/compile-fail/intrinsics/div-by-zero-1.rs new file mode 100644 index 00000000000..d67d06dc1e6 --- /dev/null +++ b/tests/compile-fail/intrinsics/div-by-zero-1.rs @@ -0,0 +1,9 @@ +#![feature(core_intrinsics)] + +use std::intrinsics::*; + +fn main() { + unsafe { + let _n = unchecked_div(1i64, 0); //~ERROR dividing by zero + } +} diff --git a/tests/compile-fail/intrinsics/div-by-zero-2.rs b/tests/compile-fail/intrinsics/div-by-zero-2.rs new file mode 100644 index 00000000000..e904049e3b4 --- /dev/null +++ b/tests/compile-fail/intrinsics/div-by-zero-2.rs @@ -0,0 +1,9 @@ +#![feature(core_intrinsics)] + +use std::intrinsics::*; + +fn main() { + unsafe { + let _n = unchecked_rem(3u32, 0); //~ ERROR calculating the remainder with a divisor of zero + } +} diff --git a/tests/compile-fail/intrinsics/exact_div1.rs b/tests/compile-fail/intrinsics/exact_div1.rs new file mode 100644 index 00000000000..171bedeadc6 --- /dev/null +++ b/tests/compile-fail/intrinsics/exact_div1.rs @@ -0,0 +1,5 @@ +#![feature(core_intrinsics)] +fn main() { + // divison by 0 + unsafe { std::intrinsics::exact_div(2, 0); } //~ ERROR divisor of zero +} diff --git a/tests/compile-fail/intrinsics/exact_div2.rs b/tests/compile-fail/intrinsics/exact_div2.rs new file mode 100644 index 00000000000..9b9ae807088 --- /dev/null +++ b/tests/compile-fail/intrinsics/exact_div2.rs @@ -0,0 +1,5 @@ +#![feature(core_intrinsics)] +fn main() { + // divison with a remainder + unsafe { std::intrinsics::exact_div(2u16, 3); } //~ ERROR 2u16 cannot be divided by 3u16 without remainder +} diff --git a/tests/compile-fail/intrinsics/exact_div3.rs b/tests/compile-fail/intrinsics/exact_div3.rs new file mode 100644 index 00000000000..1cd112acfc2 --- /dev/null +++ b/tests/compile-fail/intrinsics/exact_div3.rs @@ -0,0 +1,5 @@ +#![feature(core_intrinsics)] +fn main() { + // signed divison with a remainder + unsafe { std::intrinsics::exact_div(-19i8, 2); } //~ ERROR -19i8 cannot be divided by 2i8 without remainder +} diff --git a/tests/compile-fail/intrinsics/exact_div4.rs b/tests/compile-fail/intrinsics/exact_div4.rs new file mode 100644 index 00000000000..c241b2df660 --- /dev/null +++ b/tests/compile-fail/intrinsics/exact_div4.rs @@ -0,0 +1,5 @@ +#![feature(core_intrinsics)] +fn main() { + // divison of MIN by -1 + unsafe { std::intrinsics::exact_div(i64::MIN, -1); } //~ ERROR result of dividing MIN by -1 cannot be represented +} diff --git a/tests/compile-fail/intrinsics/out_of_bounds_ptr_1.rs b/tests/compile-fail/intrinsics/out_of_bounds_ptr_1.rs new file mode 100644 index 00000000000..20bcc36f049 --- /dev/null +++ b/tests/compile-fail/intrinsics/out_of_bounds_ptr_1.rs @@ -0,0 +1,8 @@ +// error-pattern: must be in-bounds at offset 5, but is outside bounds of alloc +fn main() { + let v = [0i8; 4]; + let x = &v as *const i8; + // The error is inside another function, so we cannot match it by line + let x = unsafe { x.offset(5) }; + panic!("this should never print: {:?}", x); +} diff --git a/tests/compile-fail/intrinsics/out_of_bounds_ptr_2.rs b/tests/compile-fail/intrinsics/out_of_bounds_ptr_2.rs new file mode 100644 index 00000000000..1e4759ddc46 --- /dev/null +++ b/tests/compile-fail/intrinsics/out_of_bounds_ptr_2.rs @@ -0,0 +1,7 @@ +// error-pattern: overflowing in-bounds pointer arithmetic +fn main() { + let v = [0i8; 4]; + let x = &v as *const i8; + let x = unsafe { x.offset(-1) }; + panic!("this should never print: {:?}", x); +} diff --git a/tests/compile-fail/intrinsics/overflowing-unchecked-rsh.rs b/tests/compile-fail/intrinsics/overflowing-unchecked-rsh.rs new file mode 100644 index 00000000000..730d01597f4 --- /dev/null +++ b/tests/compile-fail/intrinsics/overflowing-unchecked-rsh.rs @@ -0,0 +1,11 @@ +#![feature(core_intrinsics)] + +use std::intrinsics::*; + +//error-pattern: overflowing shift by 64 in `unchecked_shr` + +fn main() { + unsafe { + let _n = unchecked_shr(1i64, 64); + } +} diff --git a/tests/compile-fail/intrinsics/ptr_offset_0_plus_0.rs b/tests/compile-fail/intrinsics/ptr_offset_0_plus_0.rs new file mode 100644 index 00000000000..96a9fb8402f --- /dev/null +++ b/tests/compile-fail/intrinsics/ptr_offset_0_plus_0.rs @@ -0,0 +1,7 @@ +// error-pattern: invalid use of NULL pointer + +fn main() { + let x = 0 as *mut i32; + let _x = x.wrapping_offset(8); // ok, this has no inbounds tag + let _x = unsafe { x.offset(0) }; // UB despite offset 0, NULL is never inbounds +} diff --git a/tests/compile-fail/intrinsics/ptr_offset_int_plus_int.rs b/tests/compile-fail/intrinsics/ptr_offset_int_plus_int.rs new file mode 100644 index 00000000000..f0cf00884e1 --- /dev/null +++ b/tests/compile-fail/intrinsics/ptr_offset_int_plus_int.rs @@ -0,0 +1,8 @@ +// error-pattern: invalid use of 1 as a pointer + +fn main() { + // Can't offset an integer pointer by non-zero offset. + unsafe { + let _val = (1 as *mut u8).offset(1); + } +} diff --git a/tests/compile-fail/intrinsics/ptr_offset_int_plus_ptr.rs b/tests/compile-fail/intrinsics/ptr_offset_int_plus_ptr.rs new file mode 100644 index 00000000000..705ca68970a --- /dev/null +++ b/tests/compile-fail/intrinsics/ptr_offset_int_plus_ptr.rs @@ -0,0 +1,9 @@ +// error-pattern: invalid use of 1 as a pointer + +fn main() { + let ptr = Box::into_raw(Box::new(0u32)); + // Can't start with an integer pointer and get to something usable + unsafe { + let _val = (1 as *mut u8).offset(ptr as isize); + } +} diff --git a/tests/compile-fail/intrinsics/ptr_offset_overflow.rs b/tests/compile-fail/intrinsics/ptr_offset_overflow.rs new file mode 100644 index 00000000000..452bf341c9e --- /dev/null +++ b/tests/compile-fail/intrinsics/ptr_offset_overflow.rs @@ -0,0 +1,6 @@ +//error-pattern: overflowing in-bounds pointer arithmetic +fn main() { + let v = [1i8, 2]; + let x = &v[1] as *const i8; + let _val = unsafe { x.offset(isize::MIN) }; +} diff --git a/tests/compile-fail/intrinsics/ptr_offset_ptr_plus_0.rs b/tests/compile-fail/intrinsics/ptr_offset_ptr_plus_0.rs new file mode 100644 index 00000000000..d07ecc4dc7f --- /dev/null +++ b/tests/compile-fail/intrinsics/ptr_offset_ptr_plus_0.rs @@ -0,0 +1,7 @@ +// error-pattern: outside bounds of alloc + +fn main() { + let x = Box::into_raw(Box::new(0u32)); + let x = x.wrapping_offset(8); // ok, this has no inbounds tag + let _x = unsafe { x.offset(0) }; // UB despite offset 0, the pointer is not inbounds of the only object it can point to +} diff --git a/tests/compile-fail/intrinsics/unchecked_add1.rs b/tests/compile-fail/intrinsics/unchecked_add1.rs new file mode 100644 index 00000000000..f48b91422c6 --- /dev/null +++ b/tests/compile-fail/intrinsics/unchecked_add1.rs @@ -0,0 +1,5 @@ +#![feature(core_intrinsics)] +fn main() { + // MAX overflow + unsafe { std::intrinsics::unchecked_add(40000u16, 30000); } //~ ERROR overflow executing `unchecked_add` +} diff --git a/tests/compile-fail/intrinsics/unchecked_add2.rs b/tests/compile-fail/intrinsics/unchecked_add2.rs new file mode 100644 index 00000000000..150986541c3 --- /dev/null +++ b/tests/compile-fail/intrinsics/unchecked_add2.rs @@ -0,0 +1,5 @@ +#![feature(core_intrinsics)] +fn main() { + // MIN overflow + unsafe { std::intrinsics::unchecked_add(-30000i16, -8000); } //~ ERROR overflow executing `unchecked_add` +} diff --git a/tests/compile-fail/intrinsics/unchecked_div1.rs b/tests/compile-fail/intrinsics/unchecked_div1.rs new file mode 100644 index 00000000000..53d80007646 --- /dev/null +++ b/tests/compile-fail/intrinsics/unchecked_div1.rs @@ -0,0 +1,5 @@ +#![feature(core_intrinsics)] +fn main() { + // MIN/-1 cannot be represented + unsafe { std::intrinsics::unchecked_div(i16::MIN, -1); } //~ ERROR overflow executing `unchecked_div` +} diff --git a/tests/compile-fail/intrinsics/unchecked_mul1.rs b/tests/compile-fail/intrinsics/unchecked_mul1.rs new file mode 100644 index 00000000000..050e3ff2437 --- /dev/null +++ b/tests/compile-fail/intrinsics/unchecked_mul1.rs @@ -0,0 +1,5 @@ +#![feature(core_intrinsics)] +fn main() { + // MAX overflow + unsafe { std::intrinsics::unchecked_mul(300u16, 250u16); } //~ ERROR overflow executing `unchecked_mul` +} diff --git a/tests/compile-fail/intrinsics/unchecked_mul2.rs b/tests/compile-fail/intrinsics/unchecked_mul2.rs new file mode 100644 index 00000000000..4fb77783b4c --- /dev/null +++ b/tests/compile-fail/intrinsics/unchecked_mul2.rs @@ -0,0 +1,5 @@ +#![feature(core_intrinsics)] +fn main() { + // MIN overflow + unsafe { std::intrinsics::unchecked_mul(1_000_000_000i32, -4); } //~ ERROR overflow executing `unchecked_mul` +} diff --git a/tests/compile-fail/intrinsics/unchecked_sub1.rs b/tests/compile-fail/intrinsics/unchecked_sub1.rs new file mode 100644 index 00000000000..69b32dd319b --- /dev/null +++ b/tests/compile-fail/intrinsics/unchecked_sub1.rs @@ -0,0 +1,5 @@ +#![feature(core_intrinsics)] +fn main() { + // MIN overflow + unsafe { std::intrinsics::unchecked_sub(14u32, 22); } //~ ERROR overflow executing `unchecked_sub` +} diff --git a/tests/compile-fail/intrinsics/unchecked_sub2.rs b/tests/compile-fail/intrinsics/unchecked_sub2.rs new file mode 100644 index 00000000000..5609ea7a3ed --- /dev/null +++ b/tests/compile-fail/intrinsics/unchecked_sub2.rs @@ -0,0 +1,5 @@ +#![feature(core_intrinsics)] +fn main() { + // MAX overflow + unsafe { std::intrinsics::unchecked_sub(30000i16, -7000); } //~ ERROR overflow executing `unchecked_sub` +} diff --git a/tests/compile-fail/intrinsics/write_bytes_null.rs b/tests/compile-fail/intrinsics/write_bytes_null.rs new file mode 100644 index 00000000000..e80222162ee --- /dev/null +++ b/tests/compile-fail/intrinsics/write_bytes_null.rs @@ -0,0 +1,10 @@ +#![feature(intrinsics)] + +// Directly call intrinsic to avoid debug assertions in libstd +extern "rust-intrinsic" { + fn write_bytes(dst: *mut T, val: u8, count: usize); +} + +fn main() { + unsafe { write_bytes::(std::ptr::null_mut(), 0, 0) }; //~ ERROR invalid use of NULL pointer +} diff --git a/tests/compile-fail/intrinsics/write_bytes_overflow.rs b/tests/compile-fail/intrinsics/write_bytes_overflow.rs new file mode 100644 index 00000000000..a6bf2acb16f --- /dev/null +++ b/tests/compile-fail/intrinsics/write_bytes_overflow.rs @@ -0,0 +1,9 @@ +// error-pattern: overflow computing total size of `write_bytes` +use std::mem; + +fn main() { + let mut y = 0; + unsafe { + (&mut y as *mut i32).write_bytes(0u8, 1usize << (mem::size_of::() * 8 - 1)); + } +} diff --git a/tests/compile-fail/libc_pthread_mutex_destroy_locked.rs b/tests/compile-fail/libc_pthread_mutex_destroy_locked.rs deleted file mode 100644 index e7ed8ad2962..00000000000 --- a/tests/compile-fail/libc_pthread_mutex_destroy_locked.rs +++ /dev/null @@ -1,16 +0,0 @@ -// ignore-windows: No libc on Windows - -#![feature(rustc_private)] - -extern crate libc; - -fn main() { - unsafe { - let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed(); - assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0); - let mut mutex: libc::pthread_mutex_t = std::mem::zeroed(); - assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0); - assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0); - libc::pthread_mutex_destroy(&mut mutex as *mut _); //~ ERROR destroyed a locked mutex - } -} diff --git a/tests/compile-fail/libc_pthread_mutex_normal_deadlock.rs b/tests/compile-fail/libc_pthread_mutex_normal_deadlock.rs deleted file mode 100644 index 7034bf64ec9..00000000000 --- a/tests/compile-fail/libc_pthread_mutex_normal_deadlock.rs +++ /dev/null @@ -1,16 +0,0 @@ -// ignore-windows: No libc on Windows - -#![feature(rustc_private)] - -extern crate libc; - -fn main() { - unsafe { - let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed(); - assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0); - let mut mutex: libc::pthread_mutex_t = std::mem::zeroed(); - assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0); - assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0); - libc::pthread_mutex_lock(&mut mutex as *mut _); //~ ERROR deadlock - } -} diff --git a/tests/compile-fail/libc_pthread_mutex_normal_unlock_unlocked.rs b/tests/compile-fail/libc_pthread_mutex_normal_unlock_unlocked.rs deleted file mode 100644 index 65de62484d5..00000000000 --- a/tests/compile-fail/libc_pthread_mutex_normal_unlock_unlocked.rs +++ /dev/null @@ -1,17 +0,0 @@ -// ignore-windows: No libc on Windows - -#![feature(rustc_private)] - -extern crate libc; - -fn main() { - unsafe { - let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed(); - assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0); - let mut mutex: libc::pthread_mutex_t = std::mem::zeroed(); - assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0); - assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0); - assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0); - libc::pthread_mutex_unlock(&mut mutex as *mut _); //~ ERROR was not locked - } -} diff --git a/tests/compile-fail/libc_pthread_rwlock_destroy_read_locked.rs b/tests/compile-fail/libc_pthread_rwlock_destroy_read_locked.rs deleted file mode 100644 index 8750a7388fc..00000000000 --- a/tests/compile-fail/libc_pthread_rwlock_destroy_read_locked.rs +++ /dev/null @@ -1,13 +0,0 @@ -// ignore-windows: No libc on Windows - -#![feature(rustc_private)] - -extern crate libc; - -fn main() { - let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); - unsafe { - assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0); - libc::pthread_rwlock_destroy(rw.get()); //~ ERROR destroyed a locked rwlock - } -} diff --git a/tests/compile-fail/libc_pthread_rwlock_destroy_write_locked.rs b/tests/compile-fail/libc_pthread_rwlock_destroy_write_locked.rs deleted file mode 100644 index aecccfa5031..00000000000 --- a/tests/compile-fail/libc_pthread_rwlock_destroy_write_locked.rs +++ /dev/null @@ -1,13 +0,0 @@ -// ignore-windows: No libc on Windows - -#![feature(rustc_private)] - -extern crate libc; - -fn main() { - let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); - unsafe { - assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0); - libc::pthread_rwlock_destroy(rw.get()); //~ ERROR destroyed a locked rwlock - } -} diff --git a/tests/compile-fail/libc_pthread_rwlock_read_write_deadlock.rs b/tests/compile-fail/libc_pthread_rwlock_read_write_deadlock.rs deleted file mode 100644 index dd4707d60e4..00000000000 --- a/tests/compile-fail/libc_pthread_rwlock_read_write_deadlock.rs +++ /dev/null @@ -1,13 +0,0 @@ -// ignore-windows: No libc on Windows - -#![feature(rustc_private)] - -extern crate libc; - -fn main() { - let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); - unsafe { - assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0); - libc::pthread_rwlock_wrlock(rw.get()); //~ ERROR: deadlock - } -} diff --git a/tests/compile-fail/libc_pthread_rwlock_unlock_unlocked.rs b/tests/compile-fail/libc_pthread_rwlock_unlock_unlocked.rs deleted file mode 100644 index 8b3de53828d..00000000000 --- a/tests/compile-fail/libc_pthread_rwlock_unlock_unlocked.rs +++ /dev/null @@ -1,12 +0,0 @@ -// ignore-windows: No libc on Windows - -#![feature(rustc_private)] - -extern crate libc; - -fn main() { - let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); - unsafe { - libc::pthread_rwlock_unlock(rw.get()); //~ ERROR was not locked - } -} diff --git a/tests/compile-fail/libc_pthread_rwlock_write_read_deadlock.rs b/tests/compile-fail/libc_pthread_rwlock_write_read_deadlock.rs deleted file mode 100644 index 1b460e7174d..00000000000 --- a/tests/compile-fail/libc_pthread_rwlock_write_read_deadlock.rs +++ /dev/null @@ -1,13 +0,0 @@ -// ignore-windows: No libc on Windows - -#![feature(rustc_private)] - -extern crate libc; - -fn main() { - let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); - unsafe { - assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0); - libc::pthread_rwlock_rdlock(rw.get()); //~ ERROR: deadlock - } -} diff --git a/tests/compile-fail/libc_pthread_rwlock_write_write_deadlock.rs b/tests/compile-fail/libc_pthread_rwlock_write_write_deadlock.rs deleted file mode 100644 index cc327ec46bc..00000000000 --- a/tests/compile-fail/libc_pthread_rwlock_write_write_deadlock.rs +++ /dev/null @@ -1,13 +0,0 @@ -// ignore-windows: No libc on Windows - -#![feature(rustc_private)] - -extern crate libc; - -fn main() { - let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); - unsafe { - assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0); - libc::pthread_rwlock_wrlock(rw.get()); //~ ERROR: deadlock - } -} diff --git a/tests/compile-fail/maybe_null_pointer_deref_zst.rs b/tests/compile-fail/maybe_null_pointer_deref_zst.rs deleted file mode 100644 index d9f5ad4c696..00000000000 --- a/tests/compile-fail/maybe_null_pointer_deref_zst.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - // This pointer *could* be NULL so we cannot load from it, not even at ZST - let ptr = (&0u8 as *const u8).wrapping_sub(0x800) as *const (); - let _x: () = unsafe { *ptr }; //~ ERROR outside bounds -} diff --git a/tests/compile-fail/maybe_null_pointer_write_zst.rs b/tests/compile-fail/maybe_null_pointer_write_zst.rs deleted file mode 100644 index ef46a469c3a..00000000000 --- a/tests/compile-fail/maybe_null_pointer_write_zst.rs +++ /dev/null @@ -1,8 +0,0 @@ -fn main() { - // This pointer *could* be NULL so we cannot load from it, not even at ZST. - // Not using the () type here, as writes of that type do not even have MIR generated. - // Also not assigning directly as that's array initialization, not assignment. - let zst_val = [1u8; 0]; - let ptr = (&0u8 as *const u8).wrapping_sub(0x800) as *mut [u8; 0]; - unsafe { *ptr = zst_val; } //~ ERROR outside bounds -} diff --git a/tests/compile-fail/out_of_bounds_ptr_1.rs b/tests/compile-fail/out_of_bounds_ptr_1.rs deleted file mode 100644 index 20bcc36f049..00000000000 --- a/tests/compile-fail/out_of_bounds_ptr_1.rs +++ /dev/null @@ -1,8 +0,0 @@ -// error-pattern: must be in-bounds at offset 5, but is outside bounds of alloc -fn main() { - let v = [0i8; 4]; - let x = &v as *const i8; - // The error is inside another function, so we cannot match it by line - let x = unsafe { x.offset(5) }; - panic!("this should never print: {:?}", x); -} diff --git a/tests/compile-fail/out_of_bounds_ptr_2.rs b/tests/compile-fail/out_of_bounds_ptr_2.rs deleted file mode 100644 index 1e4759ddc46..00000000000 --- a/tests/compile-fail/out_of_bounds_ptr_2.rs +++ /dev/null @@ -1,7 +0,0 @@ -// error-pattern: overflowing in-bounds pointer arithmetic -fn main() { - let v = [0i8; 4]; - let x = &v as *const i8; - let x = unsafe { x.offset(-1) }; - panic!("this should never print: {:?}", x); -} diff --git a/tests/compile-fail/out_of_bounds_read1.rs b/tests/compile-fail/out_of_bounds_read1.rs deleted file mode 100644 index 0c175af5266..00000000000 --- a/tests/compile-fail/out_of_bounds_read1.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - let v: Vec = vec![1, 2]; - let x = unsafe { *v.as_ptr().wrapping_offset(5) }; //~ ERROR outside bounds of alloc - panic!("this should never print: {}", x); -} diff --git a/tests/compile-fail/out_of_bounds_read2.rs b/tests/compile-fail/out_of_bounds_read2.rs deleted file mode 100644 index 0c175af5266..00000000000 --- a/tests/compile-fail/out_of_bounds_read2.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - let v: Vec = vec![1, 2]; - let x = unsafe { *v.as_ptr().wrapping_offset(5) }; //~ ERROR outside bounds of alloc - panic!("this should never print: {}", x); -} diff --git a/tests/compile-fail/overflowing-unchecked-rsh.rs b/tests/compile-fail/overflowing-unchecked-rsh.rs deleted file mode 100644 index 730d01597f4..00000000000 --- a/tests/compile-fail/overflowing-unchecked-rsh.rs +++ /dev/null @@ -1,11 +0,0 @@ -#![feature(core_intrinsics)] - -use std::intrinsics::*; - -//error-pattern: overflowing shift by 64 in `unchecked_shr` - -fn main() { - unsafe { - let _n = unchecked_shr(1i64, 64); - } -} diff --git a/tests/compile-fail/ptr_offset_0_plus_0.rs b/tests/compile-fail/ptr_offset_0_plus_0.rs deleted file mode 100644 index 96a9fb8402f..00000000000 --- a/tests/compile-fail/ptr_offset_0_plus_0.rs +++ /dev/null @@ -1,7 +0,0 @@ -// error-pattern: invalid use of NULL pointer - -fn main() { - let x = 0 as *mut i32; - let _x = x.wrapping_offset(8); // ok, this has no inbounds tag - let _x = unsafe { x.offset(0) }; // UB despite offset 0, NULL is never inbounds -} diff --git a/tests/compile-fail/ptr_offset_int_plus_int.rs b/tests/compile-fail/ptr_offset_int_plus_int.rs deleted file mode 100644 index f0cf00884e1..00000000000 --- a/tests/compile-fail/ptr_offset_int_plus_int.rs +++ /dev/null @@ -1,8 +0,0 @@ -// error-pattern: invalid use of 1 as a pointer - -fn main() { - // Can't offset an integer pointer by non-zero offset. - unsafe { - let _val = (1 as *mut u8).offset(1); - } -} diff --git a/tests/compile-fail/ptr_offset_int_plus_ptr.rs b/tests/compile-fail/ptr_offset_int_plus_ptr.rs deleted file mode 100644 index 705ca68970a..00000000000 --- a/tests/compile-fail/ptr_offset_int_plus_ptr.rs +++ /dev/null @@ -1,9 +0,0 @@ -// error-pattern: invalid use of 1 as a pointer - -fn main() { - let ptr = Box::into_raw(Box::new(0u32)); - // Can't start with an integer pointer and get to something usable - unsafe { - let _val = (1 as *mut u8).offset(ptr as isize); - } -} diff --git a/tests/compile-fail/ptr_offset_overflow.rs b/tests/compile-fail/ptr_offset_overflow.rs deleted file mode 100644 index 452bf341c9e..00000000000 --- a/tests/compile-fail/ptr_offset_overflow.rs +++ /dev/null @@ -1,6 +0,0 @@ -//error-pattern: overflowing in-bounds pointer arithmetic -fn main() { - let v = [1i8, 2]; - let x = &v[1] as *const i8; - let _val = unsafe { x.offset(isize::MIN) }; -} diff --git a/tests/compile-fail/ptr_offset_ptr_plus_0.rs b/tests/compile-fail/ptr_offset_ptr_plus_0.rs deleted file mode 100644 index d07ecc4dc7f..00000000000 --- a/tests/compile-fail/ptr_offset_ptr_plus_0.rs +++ /dev/null @@ -1,7 +0,0 @@ -// error-pattern: outside bounds of alloc - -fn main() { - let x = Box::into_raw(Box::new(0u32)); - let x = x.wrapping_offset(8); // ok, this has no inbounds tag - let _x = unsafe { x.offset(0) }; // UB despite offset 0, the pointer is not inbounds of the only object it can point to -} diff --git a/tests/compile-fail/reallocate-bad-size.rs b/tests/compile-fail/reallocate-bad-size.rs deleted file mode 100644 index 7826f26f9b8..00000000000 --- a/tests/compile-fail/reallocate-bad-size.rs +++ /dev/null @@ -1,10 +0,0 @@ -use std::alloc::{alloc, realloc, Layout}; - -// error-pattern: allocation has size 1 and alignment 1, but gave size 2 and alignment 1 - -fn main() { - unsafe { - let x = alloc(Layout::from_size_align_unchecked(1, 1)); - realloc(x, Layout::from_size_align_unchecked(2, 1), 1); - } -} diff --git a/tests/compile-fail/reallocate-change-alloc.rs b/tests/compile-fail/reallocate-change-alloc.rs deleted file mode 100644 index 29702967696..00000000000 --- a/tests/compile-fail/reallocate-change-alloc.rs +++ /dev/null @@ -1,9 +0,0 @@ -use std::alloc::{alloc, realloc, Layout}; - -fn main() { - unsafe { - let x = alloc(Layout::from_size_align_unchecked(1, 1)); - realloc(x, Layout::from_size_align_unchecked(1, 1), 1); - let _z = *x; //~ ERROR dereferenced after this allocation got freed - } -} diff --git a/tests/compile-fail/reallocate-dangling.rs b/tests/compile-fail/reallocate-dangling.rs deleted file mode 100644 index 702ddc0724a..00000000000 --- a/tests/compile-fail/reallocate-dangling.rs +++ /dev/null @@ -1,11 +0,0 @@ -use std::alloc::{alloc, dealloc, realloc, Layout}; - -// error-pattern: dereferenced after this allocation got freed - -fn main() { - unsafe { - let x = alloc(Layout::from_size_align_unchecked(1, 1)); - dealloc(x, Layout::from_size_align_unchecked(1, 1)); - realloc(x, Layout::from_size_align_unchecked(1, 1), 1); - } -} diff --git a/tests/compile-fail/reference_to_packed.rs b/tests/compile-fail/reference_to_packed.rs deleted file mode 100644 index 08104e917d2..00000000000 --- a/tests/compile-fail/reference_to_packed.rs +++ /dev/null @@ -1,19 +0,0 @@ -// This should fail even without validation/SB -// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows - -#![allow(dead_code, unused_variables)] - -#[repr(packed)] -struct Foo { - x: i32, - y: i32, -} - -fn main() { - let foo = Foo { - x: 42, - y: 99, - }; - let p = unsafe { &foo.x }; - let i = *p; //~ ERROR memory with alignment 1, but alignment 4 is required -} diff --git a/tests/compile-fail/stack_free.rs b/tests/compile-fail/stack_free.rs deleted file mode 100644 index 50a590e448a..00000000000 --- a/tests/compile-fail/stack_free.rs +++ /dev/null @@ -1,10 +0,0 @@ -// Validation/SB changes why we fail -// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows - -// error-pattern: deallocating stack variable memory using Rust heap deallocation operation - -fn main() { - let x = 42; - let bad_box = unsafe { std::mem::transmute::<&i32, Box>(&x) }; - drop(bad_box); -} diff --git a/tests/compile-fail/sync/libc_pthread_mutex_destroy_locked.rs b/tests/compile-fail/sync/libc_pthread_mutex_destroy_locked.rs new file mode 100644 index 00000000000..e7ed8ad2962 --- /dev/null +++ b/tests/compile-fail/sync/libc_pthread_mutex_destroy_locked.rs @@ -0,0 +1,16 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + unsafe { + let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed(); + assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0); + let mut mutex: libc::pthread_mutex_t = std::mem::zeroed(); + assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0); + assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0); + libc::pthread_mutex_destroy(&mut mutex as *mut _); //~ ERROR destroyed a locked mutex + } +} diff --git a/tests/compile-fail/sync/libc_pthread_mutex_normal_deadlock.rs b/tests/compile-fail/sync/libc_pthread_mutex_normal_deadlock.rs new file mode 100644 index 00000000000..7034bf64ec9 --- /dev/null +++ b/tests/compile-fail/sync/libc_pthread_mutex_normal_deadlock.rs @@ -0,0 +1,16 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + unsafe { + let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed(); + assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0); + let mut mutex: libc::pthread_mutex_t = std::mem::zeroed(); + assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0); + assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0); + libc::pthread_mutex_lock(&mut mutex as *mut _); //~ ERROR deadlock + } +} diff --git a/tests/compile-fail/sync/libc_pthread_mutex_normal_unlock_unlocked.rs b/tests/compile-fail/sync/libc_pthread_mutex_normal_unlock_unlocked.rs new file mode 100644 index 00000000000..65de62484d5 --- /dev/null +++ b/tests/compile-fail/sync/libc_pthread_mutex_normal_unlock_unlocked.rs @@ -0,0 +1,17 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + unsafe { + let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed(); + assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0); + let mut mutex: libc::pthread_mutex_t = std::mem::zeroed(); + assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0); + assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0); + assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0); + libc::pthread_mutex_unlock(&mut mutex as *mut _); //~ ERROR was not locked + } +} diff --git a/tests/compile-fail/sync/libc_pthread_rwlock_destroy_read_locked.rs b/tests/compile-fail/sync/libc_pthread_rwlock_destroy_read_locked.rs new file mode 100644 index 00000000000..8750a7388fc --- /dev/null +++ b/tests/compile-fail/sync/libc_pthread_rwlock_destroy_read_locked.rs @@ -0,0 +1,13 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); + unsafe { + assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0); + libc::pthread_rwlock_destroy(rw.get()); //~ ERROR destroyed a locked rwlock + } +} diff --git a/tests/compile-fail/sync/libc_pthread_rwlock_destroy_write_locked.rs b/tests/compile-fail/sync/libc_pthread_rwlock_destroy_write_locked.rs new file mode 100644 index 00000000000..aecccfa5031 --- /dev/null +++ b/tests/compile-fail/sync/libc_pthread_rwlock_destroy_write_locked.rs @@ -0,0 +1,13 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); + unsafe { + assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0); + libc::pthread_rwlock_destroy(rw.get()); //~ ERROR destroyed a locked rwlock + } +} diff --git a/tests/compile-fail/sync/libc_pthread_rwlock_read_write_deadlock.rs b/tests/compile-fail/sync/libc_pthread_rwlock_read_write_deadlock.rs new file mode 100644 index 00000000000..dd4707d60e4 --- /dev/null +++ b/tests/compile-fail/sync/libc_pthread_rwlock_read_write_deadlock.rs @@ -0,0 +1,13 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); + unsafe { + assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0); + libc::pthread_rwlock_wrlock(rw.get()); //~ ERROR: deadlock + } +} diff --git a/tests/compile-fail/sync/libc_pthread_rwlock_unlock_unlocked.rs b/tests/compile-fail/sync/libc_pthread_rwlock_unlock_unlocked.rs new file mode 100644 index 00000000000..8b3de53828d --- /dev/null +++ b/tests/compile-fail/sync/libc_pthread_rwlock_unlock_unlocked.rs @@ -0,0 +1,12 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); + unsafe { + libc::pthread_rwlock_unlock(rw.get()); //~ ERROR was not locked + } +} diff --git a/tests/compile-fail/sync/libc_pthread_rwlock_write_read_deadlock.rs b/tests/compile-fail/sync/libc_pthread_rwlock_write_read_deadlock.rs new file mode 100644 index 00000000000..1b460e7174d --- /dev/null +++ b/tests/compile-fail/sync/libc_pthread_rwlock_write_read_deadlock.rs @@ -0,0 +1,13 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); + unsafe { + assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0); + libc::pthread_rwlock_rdlock(rw.get()); //~ ERROR: deadlock + } +} diff --git a/tests/compile-fail/sync/libc_pthread_rwlock_write_write_deadlock.rs b/tests/compile-fail/sync/libc_pthread_rwlock_write_write_deadlock.rs new file mode 100644 index 00000000000..cc327ec46bc --- /dev/null +++ b/tests/compile-fail/sync/libc_pthread_rwlock_write_write_deadlock.rs @@ -0,0 +1,13 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); + unsafe { + assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0); + libc::pthread_rwlock_wrlock(rw.get()); //~ ERROR: deadlock + } +} diff --git a/tests/compile-fail/unaligned_pointers/alignment.rs b/tests/compile-fail/unaligned_pointers/alignment.rs new file mode 100644 index 00000000000..bac1b92075a --- /dev/null +++ b/tests/compile-fail/unaligned_pointers/alignment.rs @@ -0,0 +1,11 @@ +fn main() { + // miri always gives allocations the worst possible alignment, so a `u8` array is guaranteed + // to be at the virtual location 1 (so one byte offset from the ultimate alignemnt location 0) + let mut x = [0u8; 20]; + let x_ptr: *mut u8 = &mut x[0]; + let y_ptr = x_ptr as *mut u64; + unsafe { + *y_ptr = 42; //~ ERROR accessing memory with alignment 1, but alignment + } + panic!("unreachable in miri"); +} diff --git a/tests/compile-fail/unaligned_pointers/atomic_unaligned.rs b/tests/compile-fail/unaligned_pointers/atomic_unaligned.rs new file mode 100644 index 00000000000..bdec0ff504b --- /dev/null +++ b/tests/compile-fail/unaligned_pointers/atomic_unaligned.rs @@ -0,0 +1,12 @@ +#![feature(core_intrinsics)] + +fn main() { + // Do a 4-aligned u64 atomic access. That should be UB on all platforms, + // even if u64 only has alignment 4. + let z = [0u32; 2]; + let zptr = &z as *const _ as *const u64; + unsafe { + ::std::intrinsics::atomic_load(zptr); + //~^ ERROR accessing memory with alignment 4, but alignment 8 is required + } +} diff --git a/tests/compile-fail/unaligned_pointers/intptrcast_alignment_check.rs b/tests/compile-fail/unaligned_pointers/intptrcast_alignment_check.rs new file mode 100644 index 00000000000..1a8df5eaced --- /dev/null +++ b/tests/compile-fail/unaligned_pointers/intptrcast_alignment_check.rs @@ -0,0 +1,12 @@ +// Even with intptrcast and without validation, we want to be *sure* to catch bugs +// that arise from pointers being insufficiently aligned. The only way to achieve +// that is not not let programs exploit integer information for alignment, so here +// we test that this is indeed the case. +fn main() { + let x = &mut [0u8; 3]; + let base_addr = x as *mut _ as usize; + let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr+1 }; + let u16_ptr = base_addr_aligned as *mut u16; + unsafe { *u16_ptr = 2; } //~ ERROR memory with alignment 1, but alignment 2 is required + println!("{:?}", x); +} diff --git a/tests/compile-fail/unaligned_pointers/reference_to_packed.rs b/tests/compile-fail/unaligned_pointers/reference_to_packed.rs new file mode 100644 index 00000000000..08104e917d2 --- /dev/null +++ b/tests/compile-fail/unaligned_pointers/reference_to_packed.rs @@ -0,0 +1,19 @@ +// This should fail even without validation/SB +// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows + +#![allow(dead_code, unused_variables)] + +#[repr(packed)] +struct Foo { + x: i32, + y: i32, +} + +fn main() { + let foo = Foo { + x: 42, + y: 99, + }; + let p = unsafe { &foo.x }; + let i = *p; //~ ERROR memory with alignment 1, but alignment 4 is required +} diff --git a/tests/compile-fail/unaligned_pointers/unaligned_ptr1.rs b/tests/compile-fail/unaligned_pointers/unaligned_ptr1.rs new file mode 100644 index 00000000000..ee1a1300423 --- /dev/null +++ b/tests/compile-fail/unaligned_pointers/unaligned_ptr1.rs @@ -0,0 +1,9 @@ +// This should fail even without validation +// compile-flags: -Zmiri-disable-validation + +fn main() { + let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error. + let x = &x[0] as *const _ as *const u32; + // This must fail because alignment is violated: the allocation's base is not sufficiently aligned. + let _x = unsafe { *x }; //~ ERROR memory with alignment 2, but alignment 4 is required +} diff --git a/tests/compile-fail/unaligned_pointers/unaligned_ptr2.rs b/tests/compile-fail/unaligned_pointers/unaligned_ptr2.rs new file mode 100644 index 00000000000..853d890ecf0 --- /dev/null +++ b/tests/compile-fail/unaligned_pointers/unaligned_ptr2.rs @@ -0,0 +1,10 @@ +// This should fail even without validation. +// compile-flags: -Zmiri-disable-validation + +fn main() { + let x = [2u32, 3]; // Make it big enough so we don't get an out-of-bounds error. + let x = (x.as_ptr() as *const u8).wrapping_offset(3) as *const u32; + // This must fail because alignment is violated: the offset is not sufficiently aligned. + // Also make the offset not a power of 2, that used to ICE. + let _x = unsafe { *x }; //~ ERROR memory with alignment 1, but alignment 4 is required +} diff --git a/tests/compile-fail/unaligned_pointers/unaligned_ptr3.rs b/tests/compile-fail/unaligned_pointers/unaligned_ptr3.rs new file mode 100644 index 00000000000..43f6b472da0 --- /dev/null +++ b/tests/compile-fail/unaligned_pointers/unaligned_ptr3.rs @@ -0,0 +1,11 @@ +// This should fail even without validation. +// compile-flags: -Zmiri-disable-validation + +fn main() { + let x = [2u16, 3, 4, 5]; // Make it big enough so we don't get an out-of-bounds error. + let x = &x[0] as *const _ as *const *const u8; // cast to ptr-to-ptr, so that we load a ptr + // This must fail because alignment is violated. Test specifically for loading pointers, + // which have special code in miri's memory. + let _x = unsafe { *x }; + //~^ ERROR memory with alignment 2, but alignment +} diff --git a/tests/compile-fail/unaligned_pointers/unaligned_ptr_zst.rs b/tests/compile-fail/unaligned_pointers/unaligned_ptr_zst.rs new file mode 100644 index 00000000000..31f88c83814 --- /dev/null +++ b/tests/compile-fail/unaligned_pointers/unaligned_ptr_zst.rs @@ -0,0 +1,10 @@ +// This should fail even without validation +// compile-flags: -Zmiri-disable-validation + +fn main() { + let x = &2u16; + let x = x as *const _ as *const [u32; 0]; + // This must fail because alignment is violated. Test specifically for loading ZST. + let _x = unsafe { *x }; + //~^ ERROR memory with alignment 2, but alignment 4 is required +} diff --git a/tests/compile-fail/unaligned_ptr1.rs b/tests/compile-fail/unaligned_ptr1.rs deleted file mode 100644 index ee1a1300423..00000000000 --- a/tests/compile-fail/unaligned_ptr1.rs +++ /dev/null @@ -1,9 +0,0 @@ -// This should fail even without validation -// compile-flags: -Zmiri-disable-validation - -fn main() { - let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error. - let x = &x[0] as *const _ as *const u32; - // This must fail because alignment is violated: the allocation's base is not sufficiently aligned. - let _x = unsafe { *x }; //~ ERROR memory with alignment 2, but alignment 4 is required -} diff --git a/tests/compile-fail/unaligned_ptr2.rs b/tests/compile-fail/unaligned_ptr2.rs deleted file mode 100644 index 853d890ecf0..00000000000 --- a/tests/compile-fail/unaligned_ptr2.rs +++ /dev/null @@ -1,10 +0,0 @@ -// This should fail even without validation. -// compile-flags: -Zmiri-disable-validation - -fn main() { - let x = [2u32, 3]; // Make it big enough so we don't get an out-of-bounds error. - let x = (x.as_ptr() as *const u8).wrapping_offset(3) as *const u32; - // This must fail because alignment is violated: the offset is not sufficiently aligned. - // Also make the offset not a power of 2, that used to ICE. - let _x = unsafe { *x }; //~ ERROR memory with alignment 1, but alignment 4 is required -} diff --git a/tests/compile-fail/unaligned_ptr3.rs b/tests/compile-fail/unaligned_ptr3.rs deleted file mode 100644 index 43f6b472da0..00000000000 --- a/tests/compile-fail/unaligned_ptr3.rs +++ /dev/null @@ -1,11 +0,0 @@ -// This should fail even without validation. -// compile-flags: -Zmiri-disable-validation - -fn main() { - let x = [2u16, 3, 4, 5]; // Make it big enough so we don't get an out-of-bounds error. - let x = &x[0] as *const _ as *const *const u8; // cast to ptr-to-ptr, so that we load a ptr - // This must fail because alignment is violated. Test specifically for loading pointers, - // which have special code in miri's memory. - let _x = unsafe { *x }; - //~^ ERROR memory with alignment 2, but alignment -} diff --git a/tests/compile-fail/unaligned_ptr_zst.rs b/tests/compile-fail/unaligned_ptr_zst.rs deleted file mode 100644 index 31f88c83814..00000000000 --- a/tests/compile-fail/unaligned_ptr_zst.rs +++ /dev/null @@ -1,10 +0,0 @@ -// This should fail even without validation -// compile-flags: -Zmiri-disable-validation - -fn main() { - let x = &2u16; - let x = x as *const _ as *const [u32; 0]; - // This must fail because alignment is violated. Test specifically for loading ZST. - let _x = unsafe { *x }; - //~^ ERROR memory with alignment 2, but alignment 4 is required -} diff --git a/tests/compile-fail/unchecked_add1.rs b/tests/compile-fail/unchecked_add1.rs deleted file mode 100644 index f48b91422c6..00000000000 --- a/tests/compile-fail/unchecked_add1.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![feature(core_intrinsics)] -fn main() { - // MAX overflow - unsafe { std::intrinsics::unchecked_add(40000u16, 30000); } //~ ERROR overflow executing `unchecked_add` -} diff --git a/tests/compile-fail/unchecked_add2.rs b/tests/compile-fail/unchecked_add2.rs deleted file mode 100644 index 150986541c3..00000000000 --- a/tests/compile-fail/unchecked_add2.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![feature(core_intrinsics)] -fn main() { - // MIN overflow - unsafe { std::intrinsics::unchecked_add(-30000i16, -8000); } //~ ERROR overflow executing `unchecked_add` -} diff --git a/tests/compile-fail/unchecked_div1.rs b/tests/compile-fail/unchecked_div1.rs deleted file mode 100644 index 53d80007646..00000000000 --- a/tests/compile-fail/unchecked_div1.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![feature(core_intrinsics)] -fn main() { - // MIN/-1 cannot be represented - unsafe { std::intrinsics::unchecked_div(i16::MIN, -1); } //~ ERROR overflow executing `unchecked_div` -} diff --git a/tests/compile-fail/unchecked_mul1.rs b/tests/compile-fail/unchecked_mul1.rs deleted file mode 100644 index 050e3ff2437..00000000000 --- a/tests/compile-fail/unchecked_mul1.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![feature(core_intrinsics)] -fn main() { - // MAX overflow - unsafe { std::intrinsics::unchecked_mul(300u16, 250u16); } //~ ERROR overflow executing `unchecked_mul` -} diff --git a/tests/compile-fail/unchecked_mul2.rs b/tests/compile-fail/unchecked_mul2.rs deleted file mode 100644 index 4fb77783b4c..00000000000 --- a/tests/compile-fail/unchecked_mul2.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![feature(core_intrinsics)] -fn main() { - // MIN overflow - unsafe { std::intrinsics::unchecked_mul(1_000_000_000i32, -4); } //~ ERROR overflow executing `unchecked_mul` -} diff --git a/tests/compile-fail/unchecked_sub1.rs b/tests/compile-fail/unchecked_sub1.rs deleted file mode 100644 index 69b32dd319b..00000000000 --- a/tests/compile-fail/unchecked_sub1.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![feature(core_intrinsics)] -fn main() { - // MIN overflow - unsafe { std::intrinsics::unchecked_sub(14u32, 22); } //~ ERROR overflow executing `unchecked_sub` -} diff --git a/tests/compile-fail/unchecked_sub2.rs b/tests/compile-fail/unchecked_sub2.rs deleted file mode 100644 index 5609ea7a3ed..00000000000 --- a/tests/compile-fail/unchecked_sub2.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![feature(core_intrinsics)] -fn main() { - // MAX overflow - unsafe { std::intrinsics::unchecked_sub(30000i16, -7000); } //~ ERROR overflow executing `unchecked_sub` -} diff --git a/tests/compile-fail/wild_pointer_deref.rs b/tests/compile-fail/wild_pointer_deref.rs deleted file mode 100644 index 5780cccdb84..00000000000 --- a/tests/compile-fail/wild_pointer_deref.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - let p = 44 as *const i32; - let x = unsafe { *p }; //~ ERROR invalid use of 44 as a pointer - panic!("this should never print: {}", x); -} diff --git a/tests/compile-fail/write_bytes_null.rs b/tests/compile-fail/write_bytes_null.rs deleted file mode 100644 index e80222162ee..00000000000 --- a/tests/compile-fail/write_bytes_null.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![feature(intrinsics)] - -// Directly call intrinsic to avoid debug assertions in libstd -extern "rust-intrinsic" { - fn write_bytes(dst: *mut T, val: u8, count: usize); -} - -fn main() { - unsafe { write_bytes::(std::ptr::null_mut(), 0, 0) }; //~ ERROR invalid use of NULL pointer -} diff --git a/tests/compile-fail/write_bytes_overflow.rs b/tests/compile-fail/write_bytes_overflow.rs deleted file mode 100644 index a6bf2acb16f..00000000000 --- a/tests/compile-fail/write_bytes_overflow.rs +++ /dev/null @@ -1,9 +0,0 @@ -// error-pattern: overflow computing total size of `write_bytes` -use std::mem; - -fn main() { - let mut y = 0; - unsafe { - (&mut y as *mut i32).write_bytes(0u8, 1usize << (mem::size_of::() * 8 - 1)); - } -}