From c1401daf3ff4c70bda523b5d2c46ffe09732d5cc Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 28 Sep 2024 13:41:58 +0200 Subject: [PATCH] add tests for validity of Box with custom allocator --- .../validity/box-custom-alloc-dangling-ptr.rs | 32 ++++++++++++++++ .../box-custom-alloc-dangling-ptr.stderr | 15 ++++++++ .../box-custom-alloc-invalid-alloc.rs | 37 +++++++++++++++++++ .../box-custom-alloc-invalid-alloc.stderr | 15 ++++++++ 4 files changed, 99 insertions(+) create mode 100644 src/tools/miri/tests/fail/validity/box-custom-alloc-dangling-ptr.rs create mode 100644 src/tools/miri/tests/fail/validity/box-custom-alloc-dangling-ptr.stderr create mode 100644 src/tools/miri/tests/fail/validity/box-custom-alloc-invalid-alloc.rs create mode 100644 src/tools/miri/tests/fail/validity/box-custom-alloc-invalid-alloc.stderr 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 0000000000000..5fb81296494e5 --- /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` is an `Aggregate` + +unsafe impl std::alloc::Allocator for MyAlloc { + fn allocate(&self, _layout: Layout) -> Result, std::alloc::AllocError> { + unimplemented!() + } + + unsafe fn deallocate(&self, _ptr: NonNull, _layout: Layout) { + unimplemented!() + } +} + +#[repr(C)] +struct MyBox { + ptr: NonNull, + alloc: MyAlloc, +} + +fn main() { + let b = MyBox { ptr: NonNull::::dangling(), alloc: MyAlloc(0, 0) }; + let _b: Box = 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 0000000000000..76d7e66cfc55a --- /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 0000000000000..101a550593f90 --- /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` 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, std::alloc::AllocError> { + unimplemented!() + } + + unsafe fn deallocate(&self, _ptr: NonNull, _layout: Layout) { + unimplemented!() + } +} + +#[repr(C)] +struct MyBox { + ptr: NonNull, + alloc: MaybeUninit, +} + +fn main() { + let b = MyBox { ptr: NonNull::from(&42), alloc: MaybeUninit::uninit() }; + let _b: Box = 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 0000000000000..e151f80dde3dc --- /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 +