Skip to content

Commit 5790eb9

Browse files
committed
Auto merge of rust-lang#3922 - RalfJung:box-custom-alloc, r=RalfJung
add tests for validity of Box with custom allocator Ensure that the validity visitor visits both parts of a box with custom allocator using the right types.
2 parents 68790c0 + c1401da commit 5790eb9

4 files changed

+99
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//! Ensure that a box with a custom allocator detects when the pointer is dangling.
2+
#![feature(allocator_api)]
3+
// This should not need the aliasing model.
4+
//@compile-flags: -Zmiri-disable-stacked-borrows
5+
use std::alloc::Layout;
6+
use std::ptr::NonNull;
7+
8+
#[allow(unused)]
9+
struct MyAlloc(usize, usize); // make sure `Box<T, MyAlloc>` is an `Aggregate`
10+
11+
unsafe impl std::alloc::Allocator for MyAlloc {
12+
fn allocate(&self, _layout: Layout) -> Result<NonNull<[u8]>, std::alloc::AllocError> {
13+
unimplemented!()
14+
}
15+
16+
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {
17+
unimplemented!()
18+
}
19+
}
20+
21+
#[repr(C)]
22+
struct MyBox<T> {
23+
ptr: NonNull<T>,
24+
alloc: MyAlloc,
25+
}
26+
27+
fn main() {
28+
let b = MyBox { ptr: NonNull::<i32>::dangling(), alloc: MyAlloc(0, 0) };
29+
let _b: Box<i32, MyAlloc> = unsafe {
30+
std::mem::transmute(b) //~ERROR: dangling box
31+
};
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: constructing invalid value: encountered a dangling box (0x4[noalloc] has no provenance)
2+
--> tests/fail/validity/box-custom-alloc-dangling-ptr.rs:LL:CC
3+
|
4+
LL | std::mem::transmute(b)
5+
| ^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling box (0x4[noalloc] has no provenance)
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at tests/fail/validity/box-custom-alloc-dangling-ptr.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! Ensure that a box with a custom allocator detects when the allocator itself is invalid.
2+
#![feature(allocator_api)]
3+
// This should not need the aliasing model.
4+
//@compile-flags: -Zmiri-disable-stacked-borrows
5+
use std::alloc::Layout;
6+
use std::mem::MaybeUninit;
7+
use std::ptr::NonNull;
8+
9+
// make sure `Box<T, MyAlloc>` is an `Aggregate`
10+
#[allow(unused)]
11+
struct MyAlloc {
12+
my_alloc_field1: usize,
13+
my_alloc_field2: usize,
14+
}
15+
16+
unsafe impl std::alloc::Allocator for MyAlloc {
17+
fn allocate(&self, _layout: Layout) -> Result<NonNull<[u8]>, std::alloc::AllocError> {
18+
unimplemented!()
19+
}
20+
21+
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {
22+
unimplemented!()
23+
}
24+
}
25+
26+
#[repr(C)]
27+
struct MyBox<T> {
28+
ptr: NonNull<T>,
29+
alloc: MaybeUninit<MyAlloc>,
30+
}
31+
32+
fn main() {
33+
let b = MyBox { ptr: NonNull::from(&42), alloc: MaybeUninit::uninit() };
34+
let _b: Box<i32, MyAlloc> = unsafe {
35+
std::mem::transmute(b) //~ERROR: uninitialized memory
36+
};
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: constructing invalid value at .1.my_alloc_field1: encountered uninitialized memory, but expected an integer
2+
--> tests/fail/validity/box-custom-alloc-invalid-alloc.rs:LL:CC
3+
|
4+
LL | std::mem::transmute(b)
5+
| ^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .1.my_alloc_field1: encountered uninitialized memory, but expected an integer
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at tests/fail/validity/box-custom-alloc-invalid-alloc.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)