Skip to content

Commit 4584e75

Browse files
committed
better error messages for invalid boxes (and a few more tests)
1 parent f481547 commit 4584e75

File tree

5 files changed

+51
-24
lines changed

5 files changed

+51
-24
lines changed

src/librustc_mir/interpret/validity.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,11 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M
307307
}
308308

309309
/// Check a reference or `Box`.
310-
fn check_safe_pointer(&mut self, value: OpTy<'tcx, M::PointerTag>) -> InterpResult<'tcx> {
310+
fn check_safe_pointer(
311+
&mut self,
312+
value: OpTy<'tcx, M::PointerTag>,
313+
kind: &str,
314+
) -> InterpResult<'tcx> {
311315
let value = self.ecx.read_immediate(value)?;
312316
// Handle wide pointers.
313317
// Check metadata early, for better diagnostics
@@ -337,25 +341,26 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M
337341
);
338342
match err.kind {
339343
err_unsup!(InvalidNullPointerUsage) => {
340-
throw_validation_failure!("a NULL reference", self.path)
344+
throw_validation_failure!(format_args!("a NULL {}", kind), self.path)
341345
}
342346
err_unsup!(AlignmentCheckFailed { required, has }) => {
343347
throw_validation_failure!(
344348
format_args!(
345-
"an unaligned reference \
349+
"an unaligned {} \
346350
(required {} byte alignment but found {})",
351+
kind,
347352
required.bytes(),
348353
has.bytes()
349354
),
350355
self.path
351356
)
352357
}
353358
err_unsup!(ReadBytesAsPointer) => throw_validation_failure!(
354-
"a dangling reference (created from integer)",
359+
format_args!("a dangling {} (created from integer)", kind),
355360
self.path
356361
),
357362
_ => throw_validation_failure!(
358-
"a dangling reference (not entirely in bounds)",
363+
format_args!("a dangling {} (not entirely in bounds)", kind),
359364
self.path
360365
),
361366
}
@@ -449,11 +454,11 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M
449454
Ok(true)
450455
}
451456
ty::Ref(..) => {
452-
self.check_safe_pointer(value)?;
457+
self.check_safe_pointer(value, "reference")?;
453458
Ok(true)
454459
}
455460
ty::Adt(def, ..) if def.is_box() => {
456-
self.check_safe_pointer(value)?;
461+
self.check_safe_pointer(value, "box")?;
457462
Ok(true)
458463
}
459464
ty::FnPtr(_sig) => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
1010

1111
const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
1212
//~^ ERROR it is undefined behavior to use this value
13-
//~^^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
13+
//~^^ type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
1414

1515
const NULL: &u16 = unsafe { mem::transmute(0usize) };
1616
//~^ ERROR it is undefined behavior to use this value

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0080]: it is undefined behavior to use this value
1010
--> $DIR/ub-ref.rs:11:1
1111
|
1212
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)
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
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 rustc repository if you believe it should not be considered undefined behavior.
1616

@@ -26,7 +26,7 @@ error[E0080]: it is undefined behavior to use this value
2626
--> $DIR/ub-ref.rs:18:1
2727
|
2828
LL | const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a NULL reference
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a NULL box
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 rustc repository if you believe it should not be considered undefined behavior.
3232

@@ -66,7 +66,7 @@ error[E0080]: it is undefined behavior to use this value
6666
--> $DIR/ub-ref.rs:36:1
6767
|
6868
LL | const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
69-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (created from integer)
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling box (created from integer)
7070
|
7171
= 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.
7272

src/test/ui/consts/const-eval/ub-wide-ptr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) };
6060
// bad slice: length not an int
6161
const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) };
6262
//~^ ERROR it is undefined behavior to use this value
63+
// bad slice box: length too big
64+
const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) };
65+
//~^ ERROR it is undefined behavior to use this value
66+
// bad slice box: length not an int
67+
const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) };
68+
//~^ ERROR it is undefined behavior to use this value
6369

6470
// bad data *inside* the slice
6571
const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];

src/test/ui/consts/const-eval/ub-wide-ptr.stderr

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,31 +67,47 @@ LL | const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) };
6767
= 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.
6868

6969
error[E0080]: it is undefined behavior to use this value
70-
--> $DIR/ub-wide-ptr.rs:65:1
70+
--> $DIR/ub-wide-ptr.rs:64:1
71+
|
72+
LL | const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) };
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling box (not entirely in bounds)
74+
|
75+
= 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.
76+
77+
error[E0080]: it is undefined behavior to use this value
78+
--> $DIR/ub-wide-ptr.rs:67:1
79+
|
80+
LL | const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) };
81+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered non-integer slice length in wide pointer
82+
|
83+
= 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.
84+
85+
error[E0080]: it is undefined behavior to use this value
86+
--> $DIR/ub-wide-ptr.rs:71:1
7187
|
7288
LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
7389
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .<deref>[0], but expected a boolean
7490
|
7591
= 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.
7692

7793
error[E0080]: it is undefined behavior to use this value
78-
--> $DIR/ub-wide-ptr.rs:71:1
94+
--> $DIR/ub-wide-ptr.rs:77:1
7995
|
8096
LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
8197
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .<deref>.0, but expected a boolean
8298
|
8399
= 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.
84100

85101
error[E0080]: it is undefined behavior to use this value
86-
--> $DIR/ub-wide-ptr.rs:74:1
102+
--> $DIR/ub-wide-ptr.rs:80:1
87103
|
88104
LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
89105
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .<deref>.1[0], but expected a boolean
90106
|
91107
= 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.
92108

93109
error[E0080]: it is undefined behavior to use this value
94-
--> $DIR/ub-wide-ptr.rs:81:1
110+
--> $DIR/ub-wide-ptr.rs:87:1
95111
|
96112
LL | / const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe {
97113
LL | |
@@ -103,65 +119,65 @@ LL | | };
103119
= 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.
104120

105121
error[E0080]: it is undefined behavior to use this value
106-
--> $DIR/ub-wide-ptr.rs:89:1
122+
--> $DIR/ub-wide-ptr.rs:95:1
107123
|
108124
LL | const TRAIT_OBJ_SHORT_VTABLE_1: &dyn Trait = unsafe { mem::transmute((&92u8, &3u8)) };
109125
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling or unaligned vtable pointer in wide pointer or too small vtable
110126
|
111127
= 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.
112128

113129
error[E0080]: it is undefined behavior to use this value
114-
--> $DIR/ub-wide-ptr.rs:92:1
130+
--> $DIR/ub-wide-ptr.rs:98:1
115131
|
116132
LL | const TRAIT_OBJ_SHORT_VTABLE_2: &dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
117133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling or unaligned vtable pointer in wide pointer or too small vtable
118134
|
119135
= 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.
120136

121137
error[E0080]: it is undefined behavior to use this value
122-
--> $DIR/ub-wide-ptr.rs:95:1
138+
--> $DIR/ub-wide-ptr.rs:101:1
123139
|
124140
LL | const TRAIT_OBJ_INT_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, 4usize)) };
125141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling or unaligned vtable pointer in wide pointer or too small vtable
126142
|
127143
= 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.
128144

129145
error[E0080]: it is undefined behavior to use this value
130-
--> $DIR/ub-wide-ptr.rs:99:1
146+
--> $DIR/ub-wide-ptr.rs:105:1
131147
|
132148
LL | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };
133149
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .<deref>.<dyn-downcast>, but expected a boolean
134150
|
135151
= 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.
136152

137153
error[E0080]: it is undefined behavior to use this value
138-
--> $DIR/ub-wide-ptr.rs:103:1
154+
--> $DIR/ub-wide-ptr.rs:109:1
139155
|
140156
LL | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
141157
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling or unaligned vtable pointer in wide pointer or too small vtable
142158
|
143159
= 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.
144160

145161
error[E0080]: it is undefined behavior to use this value
146-
--> $DIR/ub-wide-ptr.rs:105:1
162+
--> $DIR/ub-wide-ptr.rs:111:1
147163
|
148164
LL | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
149165
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling or unaligned vtable pointer in wide pointer or too small vtable
150166
|
151167
= 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.
152168

153169
error[E0080]: could not evaluate static initializer
154-
--> $DIR/ub-wide-ptr.rs:111:5
170+
--> $DIR/ub-wide-ptr.rs:117:5
155171
|
156172
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
157173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid use of NULL pointer
158174

159175
error[E0080]: could not evaluate static initializer
160-
--> $DIR/ub-wide-ptr.rs:115:5
176+
--> $DIR/ub-wide-ptr.rs:121:5
161177
|
162178
LL | mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
163179
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Memory access failed: pointer must be in-bounds at offset N, but is outside bounds of allocation N which has size N
164180

165-
error: aborting due to 20 previous errors
181+
error: aborting due to 22 previous errors
166182

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

0 commit comments

Comments
 (0)