Skip to content

Commit 5014d84

Browse files
committed
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).
2 parents 5fdea5b + 98d9715 commit 5014d84

File tree

4 files changed

+61
-29
lines changed

4 files changed

+61
-29
lines changed

rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
17cc9b6256c95c31944591aec683884fead4e3b6
1+
c9b606ed679dd94f3f920ad339c0d2be0952b16c

tests/run-pass/box-pair-to-vec.rs

-28
This file was deleted.

tests/run-pass/box.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#![feature(ptr_internals)]
2+
3+
fn main() {
4+
into_raw();
5+
into_unique();
6+
boxed_pair_to_vec();
7+
}
8+
9+
fn into_raw() { unsafe {
10+
let b = Box::new(4i32);
11+
let r = Box::into_raw(b);
12+
13+
// "lose the tag"
14+
let r2 = ((r as usize)+0) as *mut i32;
15+
*(&mut *r2) = 7;
16+
17+
// Use original ptr again
18+
*(&mut *r) = 17;
19+
drop(Box::from_raw(r));
20+
}}
21+
22+
fn into_unique() { unsafe {
23+
let b = Box::new(4i32);
24+
let u = Box::into_unique(b);
25+
26+
// "lose the tag"
27+
let r = ((u.as_ptr() as usize)+0) as *mut i32;
28+
*(&mut *r) = 7;
29+
30+
// Use original ptr again.
31+
drop(Box::from_raw(u.as_ptr()));
32+
}}
33+
34+
fn boxed_pair_to_vec() {
35+
#[repr(C)]
36+
#[derive(Debug)]
37+
struct PairFoo {
38+
fst: Foo,
39+
snd: Foo,
40+
}
41+
42+
#[derive(Debug)]
43+
struct Foo(u64);
44+
fn reinterstruct(box_pair: Box<PairFoo>) -> Vec<Foo> {
45+
let ref_pair = Box::leak(box_pair) as *mut PairFoo;
46+
let ptr_foo = unsafe { &mut (*ref_pair).fst as *mut Foo };
47+
unsafe {
48+
Vec::from_raw_parts(ptr_foo, 2, 2)
49+
}
50+
}
51+
52+
let pair_foo = Box::new(PairFoo {
53+
fst: Foo(42),
54+
snd: Foo(1337),
55+
});
56+
println!("pair_foo = {:?}", pair_foo);
57+
for (n, foo) in reinterstruct(pair_foo).into_iter().enumerate() {
58+
println!("foo #{} = {:?}", n, foo);
59+
}
60+
}
File renamed without changes.

0 commit comments

Comments
 (0)