about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2024-09-28 13:41:58 +0200
committerRalf Jung <post@ralfj.de>2024-09-28 13:44:13 +0200
commitc1401daf3ff4c70bda523b5d2c46ffe09732d5cc (patch)
tree4c8d9a7ac6559516372f723c7c26389978d1992f /src
parent68790c052f942f9b3aca1e3f57a1380ec82f3f00 (diff)
downloadrust-c1401daf3ff4c70bda523b5d2c46ffe09732d5cc.tar.gz
rust-c1401daf3ff4c70bda523b5d2c46ffe09732d5cc.zip
add tests for validity of Box with custom allocator
Diffstat (limited to 'src')
-rw-r--r--src/tools/miri/tests/fail/validity/box-custom-alloc-dangling-ptr.rs32
-rw-r--r--src/tools/miri/tests/fail/validity/box-custom-alloc-dangling-ptr.stderr15
-rw-r--r--src/tools/miri/tests/fail/validity/box-custom-alloc-invalid-alloc.rs37
-rw-r--r--src/tools/miri/tests/fail/validity/box-custom-alloc-invalid-alloc.stderr15
4 files changed, 99 insertions, 0 deletions
diff --git a/src/tools/miri/tests/fail/validity/box-custom-alloc-dangling-ptr.rs b/src/tools/miri/tests/fail/validity/box-custom-alloc-dangling-ptr.rs
new file mode 100644
index 00000000000..5fb81296494
--- /dev/null
+++ b/src/tools/miri/tests/fail/validity/box-custom-alloc-dangling-ptr.rs
@@ -0,0 +1,32 @@
+//! Ensure that a box with a custom allocator detects when the pointer is dangling.
+#![feature(allocator_api)]
+// This should not need the aliasing model.
+//@compile-flags: -Zmiri-disable-stacked-borrows
+use std::alloc::Layout;
+use std::ptr::NonNull;
+
+#[allow(unused)]
+struct MyAlloc(usize, usize); // make sure `Box<T, MyAlloc>` is an `Aggregate`
+
+unsafe impl std::alloc::Allocator for MyAlloc {
+    fn allocate(&self, _layout: Layout) -> Result<NonNull<[u8]>, std::alloc::AllocError> {
+        unimplemented!()
+    }
+
+    unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {
+        unimplemented!()
+    }
+}
+
+#[repr(C)]
+struct MyBox<T> {
+    ptr: NonNull<T>,
+    alloc: MyAlloc,
+}
+
+fn main() {
+    let b = MyBox { ptr: NonNull::<i32>::dangling(), alloc: MyAlloc(0, 0) };
+    let _b: Box<i32, MyAlloc> = unsafe {
+        std::mem::transmute(b) //~ERROR: dangling box
+    };
+}
diff --git a/src/tools/miri/tests/fail/validity/box-custom-alloc-dangling-ptr.stderr b/src/tools/miri/tests/fail/validity/box-custom-alloc-dangling-ptr.stderr
new file mode 100644
index 00000000000..76d7e66cfc5
--- /dev/null
+++ b/src/tools/miri/tests/fail/validity/box-custom-alloc-dangling-ptr.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: constructing invalid value: encountered a dangling box (0x4[noalloc] has no provenance)
+  --> tests/fail/validity/box-custom-alloc-dangling-ptr.rs:LL:CC
+   |
+LL |         std::mem::transmute(b)
+   |         ^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling box (0x4[noalloc] has no provenance)
+   |
+   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
+   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
+   = note: BACKTRACE:
+   = note: inside `main` at tests/fail/validity/box-custom-alloc-dangling-ptr.rs:LL:CC
+
+note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
+
+error: aborting due to 1 previous error
+
diff --git a/src/tools/miri/tests/fail/validity/box-custom-alloc-invalid-alloc.rs b/src/tools/miri/tests/fail/validity/box-custom-alloc-invalid-alloc.rs
new file mode 100644
index 00000000000..101a550593f
--- /dev/null
+++ b/src/tools/miri/tests/fail/validity/box-custom-alloc-invalid-alloc.rs
@@ -0,0 +1,37 @@
+//! Ensure that a box with a custom allocator detects when the allocator itself is invalid.
+#![feature(allocator_api)]
+// This should not need the aliasing model.
+//@compile-flags: -Zmiri-disable-stacked-borrows
+use std::alloc::Layout;
+use std::mem::MaybeUninit;
+use std::ptr::NonNull;
+
+// make sure `Box<T, MyAlloc>` is an `Aggregate`
+#[allow(unused)]
+struct MyAlloc {
+    my_alloc_field1: usize,
+    my_alloc_field2: usize,
+}
+
+unsafe impl std::alloc::Allocator for MyAlloc {
+    fn allocate(&self, _layout: Layout) -> Result<NonNull<[u8]>, std::alloc::AllocError> {
+        unimplemented!()
+    }
+
+    unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {
+        unimplemented!()
+    }
+}
+
+#[repr(C)]
+struct MyBox<T> {
+    ptr: NonNull<T>,
+    alloc: MaybeUninit<MyAlloc>,
+}
+
+fn main() {
+    let b = MyBox { ptr: NonNull::from(&42), alloc: MaybeUninit::uninit() };
+    let _b: Box<i32, MyAlloc> = unsafe {
+        std::mem::transmute(b) //~ERROR: uninitialized memory
+    };
+}
diff --git a/src/tools/miri/tests/fail/validity/box-custom-alloc-invalid-alloc.stderr b/src/tools/miri/tests/fail/validity/box-custom-alloc-invalid-alloc.stderr
new file mode 100644
index 00000000000..e151f80dde3
--- /dev/null
+++ b/src/tools/miri/tests/fail/validity/box-custom-alloc-invalid-alloc.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: constructing invalid value at .1.my_alloc_field1: encountered uninitialized memory, but expected an integer
+  --> tests/fail/validity/box-custom-alloc-invalid-alloc.rs:LL:CC
+   |
+LL |         std::mem::transmute(b)
+   |         ^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .1.my_alloc_field1: encountered uninitialized memory, but expected an integer
+   |
+   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
+   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
+   = note: BACKTRACE:
+   = note: inside `main` at tests/fail/validity/box-custom-alloc-invalid-alloc.rs:LL:CC
+
+note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
+
+error: aborting due to 1 previous error
+