Skip to content

Commit 4807e93

Browse files
committed
test that we validate boxes
1 parent aa1435b commit 4807e93

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/librustc_mir/interpret/validity.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M
453453
Ok(true)
454454
}
455455
ty::Adt(def, ..) if def.is_box() => {
456-
// FIXME make sure we have a test for `Box`!
457456
self.check_safe_pointer(value)?;
458457
Ok(true)
459458
}

src/test/ui/consts/const-eval/ub-ref.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
88
//~^ ERROR it is undefined behavior to use this value
99
//~^^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
1010

11+
const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
12+
//~^ ERROR it is undefined behavior to use this value
13+
//~^^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
14+
1115
const NULL: &u16 = unsafe { mem::transmute(0usize) };
1216
//~^ ERROR it is undefined behavior to use this value
1317

18+
const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
19+
//~^ ERROR it is undefined behavior to use this value
20+
1421
// It is very important that we reject this: We do promote `&(4 * REF_AS_USIZE)`,
1522
// but that would fail to compile; so we ended up breaking user code that would
1623
// have worked fine had we not promoted.
@@ -20,7 +27,13 @@ const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
2027
const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
2128
//~^ ERROR it is undefined behavior to use this value
2229

30+
const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
31+
//~^ ERROR it is undefined behavior to use this value
32+
2333
const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
2434
//~^ ERROR it is undefined behavior to use this value
2535

36+
const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
37+
//~^ ERROR it is undefined behavior to use this value
38+
2639
fn main() {}

src/test/ui/consts/const-eval/ub-ref.stderr

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,67 @@ LL | const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
99
error[E0080]: it is undefined behavior to use this value
1010
--> $DIR/ub-ref.rs:11:1
1111
|
12+
LL | const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
14+
|
15+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
16+
17+
error[E0080]: it is undefined behavior to use this value
18+
--> $DIR/ub-ref.rs:15:1
19+
|
1220
LL | const NULL: &u16 = unsafe { mem::transmute(0usize) };
1321
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a NULL reference
1422
|
1523
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
1624

1725
error[E0080]: it is undefined behavior to use this value
18-
--> $DIR/ub-ref.rs:17:1
26+
--> $DIR/ub-ref.rs:18:1
27+
|
28+
LL | const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a NULL reference
30+
|
31+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
32+
33+
error[E0080]: it is undefined behavior to use this value
34+
--> $DIR/ub-ref.rs:24:1
1935
|
2036
LL | const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
2137
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain (non-pointer) bytes
2238
|
2339
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
2440

2541
error[E0080]: it is undefined behavior to use this value
26-
--> $DIR/ub-ref.rs:20:1
42+
--> $DIR/ub-ref.rs:27:1
2743
|
2844
LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
2945
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer at .<deref>, but expected plain (non-pointer) bytes
3046
|
3147
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
3248

3349
error[E0080]: it is undefined behavior to use this value
34-
--> $DIR/ub-ref.rs:23:1
50+
--> $DIR/ub-ref.rs:30:1
51+
|
52+
LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer at .<deref>, but expected plain (non-pointer) bytes
54+
|
55+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
56+
57+
error[E0080]: it is undefined behavior to use this value
58+
--> $DIR/ub-ref.rs:33:1
3559
|
3660
LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
3761
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (created from integer)
3862
|
3963
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
4064

41-
error: aborting due to 5 previous errors
65+
error[E0080]: it is undefined behavior to use this value
66+
--> $DIR/ub-ref.rs:36:1
67+
|
68+
LL | const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (created from integer)
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 rustc repository if you believe it should not be considered undefined behavior.
72+
73+
error: aborting due to 9 previous errors
4274

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

0 commit comments

Comments
 (0)