-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #1602 - RalfJung:box, r=RalfJung
test Box::into_raw aliasing Directly test aliasing problems caused by `Box::into_raw` issues (like we have them again right now due to rust-lang/rust#77187, but the pinned rustc is older than that so this should still be able to land).
- Loading branch information
Showing
3 changed files
with
60 additions
and
28 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PairFoo>) -> Vec<Foo> { | ||
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); | ||
} | ||
} |
File renamed without changes.