Skip to content

Commit 6f5cf12

Browse files
committed
test for detecting bad data inside trait objects / slices
1 parent ad8deba commit 6f5cf12

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

src/test/ui/union-ub-fat-ptr.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
// normalize-stderr-test "allocation \d+" -> "allocation N"
1414
// normalize-stderr-test "size \d+" -> "size N"
1515

16+
union BoolTransmute {
17+
val: u8,
18+
bl: bool,
19+
}
20+
1621
#[repr(C)]
1722
#[derive(Copy, Clone)]
1823
struct SliceRepr {
@@ -63,34 +68,43 @@ union DynTransmute {
6368
}
6469

6570
trait Trait {}
71+
impl Trait for bool {}
6672

6773
// OK
6874
const A: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.str};
69-
// bad
75+
// bad str
7076
const B: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.str};
7177
//~^ ERROR this constant likely exhibits undefined behavior
72-
// bad
78+
// bad str
7379
const C: &str = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.str};
7480
//~^ ERROR this constant likely exhibits undefined behavior
7581

7682
// OK
7783
const A2: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.slice};
78-
// bad
84+
// bad slice
7985
const B2: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.slice};
8086
//~^ ERROR this constant likely exhibits undefined behavior
81-
// bad
87+
// bad slice
8288
const C2: &[u8] = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.slice};
8389
//~^ ERROR this constant likely exhibits undefined behavior
8490

85-
// bad
91+
// bad trait object
8692
const D: &Trait = unsafe { DynTransmute { repr: DynRepr { ptr: &92, vtable: &3 } }.rust};
8793
//~^ ERROR this constant likely exhibits undefined behavior
88-
// bad
94+
// bad trait object
8995
const E: &Trait = unsafe { DynTransmute { repr2: DynRepr2 { ptr: &92, vtable: &3 } }.rust};
9096
//~^ ERROR this constant likely exhibits undefined behavior
91-
// bad
97+
// bad trait object
9298
const F: &Trait = unsafe { DynTransmute { bad: BadDynRepr { ptr: &92, vtable: 3 } }.rust};
9399
//~^ ERROR this constant likely exhibits undefined behavior
94100

101+
// bad data *inside* the trait object
102+
const G: &Trait = &unsafe { BoolTransmute { val: 3 }.bl };
103+
//~^ ERROR this constant likely exhibits undefined behavior
104+
105+
// bad data *inside* the slice
106+
const H: &[bool] = &[unsafe { BoolTransmute { val: 3 }.bl }];
107+
//~^ ERROR this constant likely exhibits undefined behavior
108+
95109
fn main() {
96110
}

src/test/ui/union-ub-fat-ptr.stderr

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,75 @@
11
error[E0080]: this constant likely exhibits undefined behavior
2-
--> $DIR/union-ub-fat-ptr.rs:70:1
2+
--> $DIR/union-ub-fat-ptr.rs:76:1
33
|
44
LL | const B: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.str};
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access at offset N, outside bounds of allocation N which has size N
66
|
77
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
88

99
error[E0080]: this constant likely exhibits undefined behavior
10-
--> $DIR/union-ub-fat-ptr.rs:73:1
10+
--> $DIR/union-ub-fat-ptr.rs:79:1
1111
|
1212
LL | const C: &str = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.str};
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered length is not a valid integer
1414
|
1515
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
1616

1717
error[E0080]: this constant likely exhibits undefined behavior
18-
--> $DIR/union-ub-fat-ptr.rs:79:1
18+
--> $DIR/union-ub-fat-ptr.rs:85:1
1919
|
2020
LL | const B2: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.slice};
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access at offset N, outside bounds of allocation N which has size N
2222
|
2323
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
2424

2525
error[E0080]: this constant likely exhibits undefined behavior
26-
--> $DIR/union-ub-fat-ptr.rs:82:1
26+
--> $DIR/union-ub-fat-ptr.rs:88:1
2727
|
2828
LL | const C2: &[u8] = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.slice};
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered length is not a valid integer
3030
|
3131
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
3232

3333
error[E0080]: this constant likely exhibits undefined behavior
34-
--> $DIR/union-ub-fat-ptr.rs:86:1
34+
--> $DIR/union-ub-fat-ptr.rs:92:1
3535
|
3636
LL | const D: &Trait = unsafe { DynTransmute { repr: DynRepr { ptr: &92, vtable: &3 } }.rust};
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ tried to access memory with alignment N, but alignment N is required
3838
|
3939
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
4040

4141
error[E0080]: this constant likely exhibits undefined behavior
42-
--> $DIR/union-ub-fat-ptr.rs:89:1
42+
--> $DIR/union-ub-fat-ptr.rs:95:1
4343
|
4444
LL | const E: &Trait = unsafe { DynTransmute { repr2: DynRepr2 { ptr: &92, vtable: &3 } }.rust};
4545
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ a memory access tried to interpret some bytes as a pointer
4646
|
4747
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
4848

4949
error[E0080]: this constant likely exhibits undefined behavior
50-
--> $DIR/union-ub-fat-ptr.rs:92:1
50+
--> $DIR/union-ub-fat-ptr.rs:98:1
5151
|
5252
LL | const F: &Trait = unsafe { DynTransmute { bad: BadDynRepr { ptr: &92, vtable: 3 } }.rust};
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered vtable address is not a pointer
5454
|
5555
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
5656

57-
error: aborting due to 7 previous errors
57+
error[E0080]: this constant likely exhibits undefined behavior
58+
--> $DIR/union-ub-fat-ptr.rs:102:1
59+
|
60+
LL | const G: &Trait = &unsafe { BoolTransmute { val: 3 }.bl };
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .data_ptr, but expected something in the range 0..=1
62+
|
63+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
64+
65+
error[E0080]: this constant likely exhibits undefined behavior
66+
--> $DIR/union-ub-fat-ptr.rs:106:1
67+
|
68+
LL | const H: &[bool] = &[unsafe { BoolTransmute { val: 3 }.bl }];
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .data_ptr[0], but expected something in the range 0..=1
70+
|
71+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
72+
73+
error: aborting due to 9 previous errors
5874

5975
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)