From ecf330f39eaed1f798445ae018cfb645517078bc Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 27 Oct 2020 16:58:08 +0100 Subject: [PATCH] test Box::into_raw aliasing --- tests/run-pass/box-pair-to-vec.rs | 28 --------- tests/run-pass/box.rs | 60 +++++++++++++++++++ .../{box-pair-to-vec.stdout => box.stdout} | 0 3 files changed, 60 insertions(+), 28 deletions(-) delete mode 100644 tests/run-pass/box-pair-to-vec.rs create mode 100644 tests/run-pass/box.rs rename tests/run-pass/{box-pair-to-vec.stdout => box.stdout} (100%) diff --git a/tests/run-pass/box-pair-to-vec.rs b/tests/run-pass/box-pair-to-vec.rs deleted file mode 100644 index 353afb9d321..00000000000 --- a/tests/run-pass/box-pair-to-vec.rs +++ /dev/null @@ -1,28 +0,0 @@ -#[repr(C)] -#[derive(Debug)] -struct PairFoo { - fst: Foo, - snd: Foo, -} - -#[derive(Debug)] -struct Foo(u64); -fn reinterstruct(box_pair: Box) -> Vec { - let ref_pair = Box::leak(box_pair) as *mut PairFoo; - let ptr_foo = unsafe { &mut (*ref_pair).fst as *mut Foo }; - unsafe { - Vec::from_raw_parts(ptr_foo, 2, 2) - } -} - -fn main() { - let pair_foo = Box::new(PairFoo { - fst: Foo(42), - snd: Foo(1337), - }); - println!("pair_foo = {:?}", pair_foo); - for (n, foo) in reinterstruct(pair_foo).into_iter().enumerate() { - println!("foo #{} = {:?}", n, foo); - } -} - diff --git a/tests/run-pass/box.rs b/tests/run-pass/box.rs new file mode 100644 index 00000000000..b89c0ac1286 --- /dev/null +++ b/tests/run-pass/box.rs @@ -0,0 +1,60 @@ +#![feature(ptr_internals)] + +fn main() { + into_raw(); + into_unique(); + boxed_pair_to_vec(); +} + +fn into_raw() { unsafe { + let b = Box::new(4i32); + let r = Box::into_raw(b); + + // "lose the tag" + let r2 = ((r as usize)+0) as *mut i32; + *(&mut *r2) = 7; + + // Use original ptr again + *(&mut *r) = 17; + drop(Box::from_raw(r)); +}} + +fn into_unique() { unsafe { + let b = Box::new(4i32); + let u = Box::into_unique(b); + + // "lose the tag" + let r = ((u.as_ptr() as usize)+0) as *mut i32; + *(&mut *r) = 7; + + // Use original ptr again. + drop(Box::from_raw(u.as_ptr())); +}} + +fn boxed_pair_to_vec() { + #[repr(C)] + #[derive(Debug)] + struct PairFoo { + fst: Foo, + snd: Foo, + } + + #[derive(Debug)] + struct Foo(u64); + fn reinterstruct(box_pair: Box) -> Vec { + let ref_pair = Box::leak(box_pair) as *mut PairFoo; + let ptr_foo = unsafe { &mut (*ref_pair).fst as *mut Foo }; + unsafe { + Vec::from_raw_parts(ptr_foo, 2, 2) + } + } + + let pair_foo = Box::new(PairFoo { + fst: Foo(42), + snd: Foo(1337), + }); + println!("pair_foo = {:?}", pair_foo); + for (n, foo) in reinterstruct(pair_foo).into_iter().enumerate() { + println!("foo #{} = {:?}", n, foo); + } +} diff --git a/tests/run-pass/box-pair-to-vec.stdout b/tests/run-pass/box.stdout similarity index 100% rename from tests/run-pass/box-pair-to-vec.stdout rename to tests/run-pass/box.stdout -- 2.44.0