Skip to content

Commit 7e79b9b

Browse files
committed
Add tests for repr(C)-non-clike-enum layout
1 parent 3d7a6fe commit 7e79b9b

File tree

2 files changed

+354
-0
lines changed

2 files changed

+354
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This test deserializes an enum in-place by transmuting to a union that
12+
// should have the same layout, and manipulating the tag and payloads
13+
// independently. This verifies that `repr(some_int)` has a stable representation,
14+
// and that we don't miscompile these kinds of manipulations.
15+
16+
use std::time::Duration;
17+
use std::mem;
18+
19+
#[repr(C, u8)]
20+
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
21+
enum MyEnum {
22+
A(u32), // Single primitive value
23+
B { x: u8, y: i16 }, // Composite, and the offset of `y` depends on tag being internal
24+
C, // Empty
25+
D(Option<u32>), // Contains an enum
26+
E(Duration), // Contains a struct
27+
}
28+
29+
#[repr(C)]
30+
struct MyEnumRepr {
31+
tag: MyEnumTag,
32+
payload: MyEnumPayload,
33+
}
34+
35+
#[repr(C)]
36+
#[allow(non_snake_case)]
37+
union MyEnumPayload {
38+
A: MyEnumVariantA,
39+
B: MyEnumVariantB,
40+
D: MyEnumVariantD,
41+
E: MyEnumVariantE,
42+
}
43+
44+
#[repr(u8)] #[derive(Copy, Clone)] enum MyEnumTag { A, B, C, D, E }
45+
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantA(u32);
46+
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantB {x: u8, y: i16 }
47+
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantD(Option<u32>);
48+
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantE(Duration);
49+
50+
fn main() {
51+
let result: Vec<Result<MyEnum, ()>> = vec![
52+
Ok(MyEnum::A(17)),
53+
Ok(MyEnum::B { x: 206, y: 1145 }),
54+
Ok(MyEnum::C),
55+
Err(()),
56+
Ok(MyEnum::D(Some(407))),
57+
Ok(MyEnum::D(None)),
58+
Ok(MyEnum::E(Duration::from_secs(100))),
59+
Err(()),
60+
];
61+
62+
// Binary serialized version of the above (little-endian)
63+
let input: Vec<u8> = vec![
64+
0, 17, 0, 0, 0,
65+
1, 206, 121, 4,
66+
2,
67+
8, /* invalid tag value */
68+
3, 0, 151, 1, 0, 0,
69+
3, 1,
70+
4, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71+
0, /* incomplete value */
72+
];
73+
74+
let mut output = vec![];
75+
let mut buf = &input[..];
76+
77+
unsafe {
78+
// This should be safe, because we don't match on it unless it's fully formed,
79+
// and it doesn't have a destructor.
80+
let mut dest: MyEnum = mem::uninitialized();
81+
while buf.len() > 0 {
82+
match parse_my_enum(&mut dest, &mut buf) {
83+
Ok(()) => output.push(Ok(dest)),
84+
Err(()) => output.push(Err(())),
85+
}
86+
}
87+
}
88+
89+
assert_eq!(output, result);
90+
}
91+
92+
fn parse_my_enum<'a>(dest: &'a mut MyEnum, buf: &mut &[u8]) -> Result<(), ()> {
93+
unsafe {
94+
// Should be correct to do this transmute.
95+
let dest: &'a mut MyEnumRepr = mem::transmute(dest);
96+
let tag = read_u8(buf)?;
97+
98+
dest.tag = match tag {
99+
0 => MyEnumTag::A,
100+
1 => MyEnumTag::B,
101+
2 => MyEnumTag::C,
102+
3 => MyEnumTag::D,
103+
4 => MyEnumTag::E,
104+
_ => return Err(()),
105+
};
106+
107+
match dest.tag {
108+
MyEnumTag::A => {
109+
dest.payload.A.0 = read_u32_le(buf)?;
110+
}
111+
MyEnumTag::B => {
112+
dest.payload.B.x = read_u8(buf)?;
113+
dest.payload.B.y = read_u16_le(buf)? as i16;
114+
}
115+
MyEnumTag::C => {
116+
/* do nothing */
117+
}
118+
MyEnumTag::D => {
119+
let is_some = read_u8(buf)? == 0;
120+
if is_some {
121+
dest.payload.D.0 = Some(read_u32_le(buf)?);
122+
} else {
123+
dest.payload.D.0 = None;
124+
}
125+
}
126+
MyEnumTag::E => {
127+
let secs = read_u64_le(buf)?;
128+
let nanos = read_u32_le(buf)?;
129+
dest.payload.E.0 = Duration::new(secs, nanos);
130+
}
131+
}
132+
Ok(())
133+
}
134+
}
135+
136+
137+
138+
// reader helpers
139+
140+
fn read_u64_le(buf: &mut &[u8]) -> Result<u64, ()> {
141+
if buf.len() < 8 { return Err(()) }
142+
let val = (buf[0] as u64) << 0
143+
| (buf[1] as u64) << 8
144+
| (buf[2] as u64) << 16
145+
| (buf[3] as u64) << 24
146+
| (buf[4] as u64) << 32
147+
| (buf[5] as u64) << 40
148+
| (buf[6] as u64) << 48
149+
| (buf[7] as u64) << 56;
150+
*buf = &buf[8..];
151+
Ok(val)
152+
}
153+
154+
fn read_u32_le(buf: &mut &[u8]) -> Result<u32, ()> {
155+
if buf.len() < 4 { return Err(()) }
156+
let val = (buf[0] as u32) << 0
157+
| (buf[1] as u32) << 8
158+
| (buf[2] as u32) << 16
159+
| (buf[3] as u32) << 24;
160+
*buf = &buf[4..];
161+
Ok(val)
162+
}
163+
164+
fn read_u16_le(buf: &mut &[u8]) -> Result<u16, ()> {
165+
if buf.len() < 2 { return Err(()) }
166+
let val = (buf[0] as u16) << 0
167+
| (buf[1] as u16) << 8;
168+
*buf = &buf[2..];
169+
Ok(val)
170+
}
171+
172+
fn read_u8(buf: &mut &[u8]) -> Result<u8, ()> {
173+
if buf.len() < 1 { return Err(()) }
174+
let val = buf[0];
175+
*buf = &buf[1..];
176+
Ok(val)
177+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This test deserializes an enum in-place by transmuting to a union that
12+
// should have the same layout, and manipulating the tag and payloads
13+
// independently. This verifies that `repr(some_int)` has a stable representation,
14+
// and that we don't miscompile these kinds of manipulations.
15+
16+
use std::time::Duration;
17+
use std::mem;
18+
19+
#[repr(C)]
20+
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
21+
enum MyEnum {
22+
A(u32), // Single primitive value
23+
B { x: u8, y: i16 }, // Composite, and the offset of `y` depends on tag being internal
24+
C, // Empty
25+
D(Option<u32>), // Contains an enum
26+
E(Duration), // Contains a struct
27+
}
28+
29+
#[repr(C)]
30+
struct MyEnumRepr {
31+
tag: MyEnumTag,
32+
payload: MyEnumPayload,
33+
}
34+
35+
#[repr(C)]
36+
#[allow(non_snake_case)]
37+
union MyEnumPayload {
38+
A: MyEnumVariantA,
39+
B: MyEnumVariantB,
40+
D: MyEnumVariantD,
41+
E: MyEnumVariantE,
42+
}
43+
44+
#[repr(C)] #[derive(Copy, Clone)] enum MyEnumTag { A, B, C, D, E }
45+
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantA(u32);
46+
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantB {x: u8, y: i16 }
47+
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantD(Option<u32>);
48+
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantE(Duration);
49+
50+
fn main() {
51+
let result: Vec<Result<MyEnum, ()>> = vec![
52+
Ok(MyEnum::A(17)),
53+
Ok(MyEnum::B { x: 206, y: 1145 }),
54+
Ok(MyEnum::C),
55+
Err(()),
56+
Ok(MyEnum::D(Some(407))),
57+
Ok(MyEnum::D(None)),
58+
Ok(MyEnum::E(Duration::from_secs(100))),
59+
Err(()),
60+
];
61+
62+
// Binary serialized version of the above (little-endian)
63+
let input: Vec<u8> = vec![
64+
0, 17, 0, 0, 0,
65+
1, 206, 121, 4,
66+
2,
67+
8, /* invalid tag value */
68+
3, 0, 151, 1, 0, 0,
69+
3, 1,
70+
4, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71+
0, /* incomplete value */
72+
];
73+
74+
let mut output = vec![];
75+
let mut buf = &input[..];
76+
77+
unsafe {
78+
// This should be safe, because we don't match on it unless it's fully formed,
79+
// and it doesn't have a destructor.
80+
let mut dest: MyEnum = mem::uninitialized();
81+
while buf.len() > 0 {
82+
match parse_my_enum(&mut dest, &mut buf) {
83+
Ok(()) => output.push(Ok(dest)),
84+
Err(()) => output.push(Err(())),
85+
}
86+
}
87+
}
88+
89+
assert_eq!(output, result);
90+
}
91+
92+
fn parse_my_enum<'a>(dest: &'a mut MyEnum, buf: &mut &[u8]) -> Result<(), ()> {
93+
unsafe {
94+
// Should be correct to do this transmute.
95+
let dest: &'a mut MyEnumRepr = mem::transmute(dest);
96+
let tag = read_u8(buf)?;
97+
98+
dest.tag = match tag {
99+
0 => MyEnumTag::A,
100+
1 => MyEnumTag::B,
101+
2 => MyEnumTag::C,
102+
3 => MyEnumTag::D,
103+
4 => MyEnumTag::E,
104+
_ => return Err(()),
105+
};
106+
107+
match dest.tag {
108+
MyEnumTag::A => {
109+
dest.payload.A.0 = read_u32_le(buf)?;
110+
}
111+
MyEnumTag::B => {
112+
dest.payload.B.x = read_u8(buf)?;
113+
dest.payload.B.y = read_u16_le(buf)? as i16;
114+
}
115+
MyEnumTag::C => {
116+
/* do nothing */
117+
}
118+
MyEnumTag::D => {
119+
let is_some = read_u8(buf)? == 0;
120+
if is_some {
121+
dest.payload.D.0 = Some(read_u32_le(buf)?);
122+
} else {
123+
dest.payload.D.0 = None;
124+
}
125+
}
126+
MyEnumTag::E => {
127+
let secs = read_u64_le(buf)?;
128+
let nanos = read_u32_le(buf)?;
129+
dest.payload.E.0 = Duration::new(secs, nanos);
130+
}
131+
}
132+
Ok(())
133+
}
134+
}
135+
136+
137+
138+
// reader helpers
139+
140+
fn read_u64_le(buf: &mut &[u8]) -> Result<u64, ()> {
141+
if buf.len() < 8 { return Err(()) }
142+
let val = (buf[0] as u64) << 0
143+
| (buf[1] as u64) << 8
144+
| (buf[2] as u64) << 16
145+
| (buf[3] as u64) << 24
146+
| (buf[4] as u64) << 32
147+
| (buf[5] as u64) << 40
148+
| (buf[6] as u64) << 48
149+
| (buf[7] as u64) << 56;
150+
*buf = &buf[8..];
151+
Ok(val)
152+
}
153+
154+
fn read_u32_le(buf: &mut &[u8]) -> Result<u32, ()> {
155+
if buf.len() < 4 { return Err(()) }
156+
let val = (buf[0] as u32) << 0
157+
| (buf[1] as u32) << 8
158+
| (buf[2] as u32) << 16
159+
| (buf[3] as u32) << 24;
160+
*buf = &buf[4..];
161+
Ok(val)
162+
}
163+
164+
fn read_u16_le(buf: &mut &[u8]) -> Result<u16, ()> {
165+
if buf.len() < 2 { return Err(()) }
166+
let val = (buf[0] as u16) << 0
167+
| (buf[1] as u16) << 8;
168+
*buf = &buf[2..];
169+
Ok(val)
170+
}
171+
172+
fn read_u8(buf: &mut &[u8]) -> Result<u8, ()> {
173+
if buf.len() < 1 { return Err(()) }
174+
let val = buf[0];
175+
*buf = &buf[1..];
176+
Ok(val)
177+
}

0 commit comments

Comments
 (0)