From 3dcba56349ebc41b20948e982dc61a9ed9305f59 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 2 Apr 2022 00:05:27 -0400 Subject: [PATCH] add test for nasty example --- .../strict_provenance_transmute.rs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/compile-fail/strict_provenance_transmute.rs diff --git a/tests/compile-fail/strict_provenance_transmute.rs b/tests/compile-fail/strict_provenance_transmute.rs new file mode 100644 index 00000000000..0fc64295f94 --- /dev/null +++ b/tests/compile-fail/strict_provenance_transmute.rs @@ -0,0 +1,27 @@ +// compile-flags: -Zmiri-strict-provenance +#![feature(strict_provenance)] + +use std::mem; + +// This is the example from +// . + +unsafe fn deref(left: *const u8, right: *const u8) { + let left_int: usize = mem::transmute(left); //~ERROR expected initialized plain (non-pointer) bytes + let right_int: usize = mem::transmute(right); + if left_int == right_int { + // The compiler is allowed to replace `left_int` by `right_int` here... + let left_ptr: *const u8 = mem::transmute(left_int); + // ...which however means here it could be dereferencing the wrong pointer. + let _val = *left_ptr; + } +} + +fn main() { + let ptr1 = &0u8 as *const u8; + let ptr2 = &1u8 as *const u8; + unsafe { + // Two pointers with the same address but different provenance. + deref(ptr1, ptr2.with_addr(ptr1.addr())); + } +}