test more operations on dangling ZST pointers

This commit is contained in:
Ralf Jung
2018-09-15 16:35:37 +02:00
parent cf44c36640
commit cd138bcd0b
13 changed files with 48 additions and 27 deletions
@@ -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
}
@@ -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
}
@@ -0,0 +1,4 @@
fn main() {
let x: () = unsafe { *std::ptr::null() }; //~ ERROR constant evaluation error: invalid use of NULL pointer
panic!("this should never print: {:?}", x);
}
+3
View File
@@ -0,0 +1,3 @@
fn main() {
unsafe { *std::ptr::null_mut() = 0i32 }; //~ ERROR constant evaluation error: invalid use of NULL pointer
}
@@ -0,0 +1,6 @@
fn main() {
// 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];
unsafe { *std::ptr::null_mut() = zst_val }; //~ ERROR constant evaluation error: invalid use of NULL pointer
}
@@ -8,10 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// FIXME: remove the next line when https://github.com/rust-lang/rust/issues/43358 is resolved
// compile-flags: -Zmir-opt-level=0
// Check that you can cast between different pointers to trait objects
// whose vtable have the same kind (both lengths, or both trait pointers).
-2
View File
@@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// pretty-expanded FIXME #23616
/*!
* Try to double-check that static fns have the right size (with or
* without dummy env ptr, as appropriate) by iterating a size-2 array.
-2
View File
@@ -10,8 +10,6 @@
// Test that overloaded calls work with zero arity closures
// pretty-expanded FIXME #23616
fn main() {
let functions: [Box<Fn() -> Option<()>>; 1] = [Box::new(|| None)];
+5 -1
View File
@@ -55,7 +55,11 @@ fn main() {
assert_eq!(basic_ref_mut_var(), 3);
assert_eq!(tuple_ref_mut(), (10, 22));
assert_eq!(match_ref_mut(), 42);
// FIXME: improve this test... how?
// Compare even dangling pointers with NULL, and with others in the same allocation.
assert!(dangling_pointer() != std::ptr::null());
assert!(match dangling_pointer() as usize { 0 => false, _ => true });
let dangling = dangling_pointer();
assert!(dangling == dangling);
assert!(dangling.wrapping_add(1) != dangling);
}
@@ -22,8 +22,6 @@
// doing region-folding, when really all clients of the region-folding
// case only want to see FREE lifetime variables, not bound ones.
// pretty-expanded FIXME #23616
#![allow(unused_features)]
#![feature(box_syntax)]
-2
View File
@@ -10,8 +10,6 @@
// Test that a class with only sendable fields can be sent
// pretty-expanded FIXME #23616
use std::sync::mpsc::channel;
#[allow(dead_code)]
+17 -2
View File
@@ -11,8 +11,23 @@ fn use_zst() -> A {
}
fn main() {
// 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];
assert_eq!(zst_ret(), A);
assert_eq!(use_zst(), A);
let x = 42 as *mut ();
unsafe { *x = (); }
let x = 42 as *mut [u8; 0];
// reading and writing is okay
unsafe { *x = zst_val; }
unsafe { let _y = *x; }
// We should even be able to use "true" pointers for ZST when the allocation has been
// removed already. The box is for a non-ZST to make sure there actually is an allocation.
let mut x_box = Box::new(((), 1u8));
let x = &mut x_box.0 as *mut _ as *mut [u8; 0];
drop(x_box);
// reading and writing is okay
unsafe { *x = zst_val; }
unsafe { let _y = *x; }
}
-12
View File
@@ -1,12 +0,0 @@
#![allow(dead_code)]
#[derive(Debug)]
struct A;
fn main() {
// can't use assert_eq, b/c that will try to print the pointer addresses with full MIR enabled
// FIXME: Test disabled for now, see <https://github.com/solson/miri/issues/131>.
//assert!(&A as *const A as *const () == &() as *const _);
//assert!(&A as *const A == &A as *const A);
}