Skip to content

Commit 3db45f0

Browse files
committed
add test for printing per-byte provenance
1 parent c3a7ca1 commit 3db45f0

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//@error-pattern: memory is uninitialized at [0x4..0x8]
2+
//@normalize-stderr-test: "a[0-9]+" -> "ALLOC"
3+
#![feature(strict_provenance)]
4+
5+
// Test printing allocations that contain single-byte provenance.
6+
7+
use std::alloc::{alloc, dealloc, Layout};
8+
use std::mem::{self, MaybeUninit};
9+
use std::slice::from_raw_parts;
10+
11+
fn byte_with_provenance<T>(val: u8, prov: *const T) -> MaybeUninit<u8> {
12+
let ptr = prov.with_addr(val as usize);
13+
let bytes: [MaybeUninit<u8>; mem::size_of::<*const ()>()] = unsafe { mem::transmute(ptr) };
14+
let lsb = if cfg!(target_endian = "little") { 0 } else { bytes.len() - 1 };
15+
bytes[lsb]
16+
}
17+
18+
fn main() {
19+
let layout = Layout::from_size_align(16, 8).unwrap();
20+
unsafe {
21+
let ptr = alloc(layout);
22+
let ptr_raw = ptr.cast::<MaybeUninit<u8>>();
23+
*ptr_raw.add(0) = byte_with_provenance(0x42, &42u8);
24+
*ptr.add(1) = 0x12;
25+
*ptr.add(2) = 0x13;
26+
*ptr_raw.add(3) = byte_with_provenance(0x43, &0u8);
27+
let slice1 = from_raw_parts(ptr, 8);
28+
let slice2 = from_raw_parts(ptr.add(8), 8);
29+
drop(slice1.cmp(slice2));
30+
dealloc(ptr, layout);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: Undefined Behavior: reading memory at ALLOC[0x0..0x8], but memory is uninitialized at [0x4..0x8], and this operation requires initialized memory
2+
--> RUSTLIB/core/src/slice/cmp.rs:LL:CC
3+
|
4+
LL | let mut order = unsafe { memcmp(left.as_ptr(), right.as_ptr(), len) as isize };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reading memory at ALLOC[0x0..0x8], but memory is uninitialized at [0x4..0x8], and this operation requires initialized memory
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 `<u8 as core::slice::cmp::SliceOrd>::compare` at RUSTLIB/core/src/slice/cmp.rs:LL:CC
11+
= note: inside `core::slice::cmp::<impl std::cmp::Ord for [u8]>::cmp` at RUSTLIB/core/src/slice/cmp.rs:LL:CC
12+
note: inside `main` at $DIR/uninit_buffer_with_provenance.rs:LL:CC
13+
--> $DIR/uninit_buffer_with_provenance.rs:LL:CC
14+
|
15+
LL | drop(slice1.cmp(slice2));
16+
| ^^^^^^^^^^^^^^^^^^
17+
18+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
19+
20+
Uninitialized memory occurred at ALLOC[0x4..0x8], in this allocation:
21+
ALLOC (Rust heap, size: 16, align: 8) {
22+
╾42[ALLOC]<TAG> (1 ptr byte)╼ 12 13 ╾43[ALLOC]<TAG> (1 ptr byte)╼ __ __ __ __ __ __ __ __ __ __ __ __ │ ━..━░░░░░░░░░░░░
23+
}
24+
ALLOC (global (static or const), size: 1, align: 1) {
25+
2a │ *
26+
}
27+
ALLOC (global (static or const), size: 1, align: 1) {
28+
00 │ .
29+
}
30+
31+
error: aborting due to previous error
32+

0 commit comments

Comments
 (0)