Skip to content

Commit 19bd72e

Browse files
committed
convert remaining try_validation to new macro
1 parent aa2eaca commit 19bd72e

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

src/librustc_mir/interpret/validity.rs

+25-27
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,16 @@ macro_rules! throw_validation_failure {
3737
}};
3838
}
3939

40-
/// Returns a validation failure for any Err value of $e.
41-
// FIXME: Replace all usages of try_validation_catchall! with try_validation!.
42-
macro_rules! try_validation_catchall {
43-
($e:expr, $what:expr, $where:expr $(, $expected:expr )?) => {{
44-
try_validation!($e, $where,
45-
_ => { "{}", $what } $( expected { "{}", $expected } )?,
46-
)
47-
}};
48-
}
49-
/// Like try_validation, but will throw a validation error if any of the patterns in $p are
50-
/// matched. Other errors are passed back to the caller, unchanged. This lets you use the patterns
51-
/// as a kind of validation blacklist:
40+
/// If $e throws an error matching the pattern, throw a validation failure.
41+
/// Other errors are passed back to the caller, unchanged -- and if they reach the root of
42+
/// the visitor, we make sure only validation errors and `InvalidProgram` errors are left.
43+
/// This lets you use the patterns as a kind of validation whitelist, asserting which errors
44+
/// can possibly happen:
5245
///
5346
/// ```
5447
/// let v = try_validation!(some_fn(), some_path, {
5548
/// Foo | Bar | Baz => { "some failure" },
5649
/// });
57-
/// // Failures that match $p are thrown up as validation errors, but other errors are passed back
58-
/// // unchanged.
5950
/// ```
6051
///
6152
/// An additional expected parameter can also be added to the failure message:
@@ -316,19 +307,21 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
316307
err_ub!(PointerOutOfBounds { .. }) |
317308
err_ub!(AlignmentCheckFailed { .. }) |
318309
err_ub!(DanglingIntPointer(..)) |
319-
err_unsup!(ReadBytesAsPointer) => {
320-
"dangling or unaligned vtable pointer in wide pointer or too small vtable"
321-
},
310+
err_unsup!(ReadBytesAsPointer) =>
311+
{ "dangling or unaligned vtable pointer in wide pointer or too small vtable" },
322312
);
323-
try_validation_catchall!(
313+
try_validation!(
324314
self.ecx.read_drop_type_from_vtable(vtable),
325-
"invalid drop fn in vtable",
326-
self.path
315+
self.path,
316+
err_ub!(DanglingIntPointer(..)) |
317+
err_ub!(InvalidFunctionPointer(..)) |
318+
err_unsup!(ReadBytesAsPointer) =>
319+
{ "invalid drop fn in vtable" },
327320
);
328-
try_validation_catchall!(
321+
try_validation!(
329322
self.ecx.read_size_and_align_from_vtable(vtable),
330-
"invalid size or align in vtable",
331-
self.path
323+
self.path,
324+
err_unsup!(ReadPointerAsBytes) => { "invalid size or align in vtable" },
332325
);
333326
// FIXME: More checks for the vtable.
334327
}
@@ -558,11 +551,13 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
558551
}
559552
ty::FnPtr(_sig) => {
560553
let value = self.ecx.read_scalar(value)?;
561-
let _fn = try_validation_catchall!(
554+
let _fn = try_validation!(
562555
value.not_undef().and_then(|ptr| self.ecx.memory.get_fn(ptr)),
563-
value,
564556
self.path,
565-
"a function pointer"
557+
err_ub!(DanglingIntPointer(..)) |
558+
err_ub!(InvalidFunctionPointer(..)) |
559+
err_unsup!(ReadBytesAsPointer) =>
560+
{ "{}", value } expected { "a function pointer" },
566561
);
567562
// FIXME: Check if the signature matches
568563
Ok(true)
@@ -895,7 +890,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
895890
// validate and each caller will know best what to do with them.
896891
Err(err) if matches!(err.kind, InterpError::InvalidProgram(_)) => Err(err),
897892
// Avoid other errors as those do not show *where* in the value the issue lies.
898-
Err(err) => bug!("Unexpected error during validation: {}", err),
893+
Err(err) => {
894+
err.print_backtrace();
895+
bug!("Unexpected error during validation: {}", err);
896+
}
899897
}
900898
}
901899

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

+6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ const TRAIT_OBJ_INT_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, 4usize)
106106
//~^ ERROR it is undefined behavior to use this value
107107
const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
108108
//~^ ERROR it is undefined behavior to use this value
109+
const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) };
110+
//~^ ERROR it is undefined behavior to use this value
111+
const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) };
112+
//~^ ERROR it is undefined behavior to use this value
113+
const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: &dyn Trait = unsafe { mem::transmute((&92u8, &[&42u8; 8])) };
114+
//~^ ERROR it is undefined behavior to use this value
109115

110116
// bad data *inside* the trait object
111117
const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };

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

+29-5
Original file line numberDiff line numberDiff line change
@@ -166,42 +166,66 @@ LL | const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92
166166
|
167167
= 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.
168168

169+
error[E0080]: it is undefined behavior to use this value
170+
--> $DIR/ub-wide-ptr.rs:109:1
171+
|
172+
LL | const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) };
173+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered invalid drop fn in vtable
174+
|
175+
= 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.
176+
169177
error[E0080]: it is undefined behavior to use this value
170178
--> $DIR/ub-wide-ptr.rs:111:1
171179
|
180+
LL | const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) };
181+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered invalid drop fn in vtable
182+
|
183+
= 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.
184+
185+
error[E0080]: it is undefined behavior to use this value
186+
--> $DIR/ub-wide-ptr.rs:113:1
187+
|
188+
LL | const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: &dyn Trait = unsafe { mem::transmute((&92u8, &[&42u8; 8])) };
189+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered invalid drop fn in vtable
190+
|
191+
= 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.
192+
193+
error[E0080]: it is undefined behavior to use this value
194+
--> $DIR/ub-wide-ptr.rs:117:1
195+
|
172196
LL | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };
173197
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x03 at .<deref>.<dyn-downcast>, but expected a boolean
174198
|
175199
= 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.
176200

177201
error[E0080]: it is undefined behavior to use this value
178-
--> $DIR/ub-wide-ptr.rs:115:1
202+
--> $DIR/ub-wide-ptr.rs:121:1
179203
|
180204
LL | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
181205
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling or unaligned vtable pointer in wide pointer or too small vtable
182206
|
183207
= 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.
184208

185209
error[E0080]: it is undefined behavior to use this value
186-
--> $DIR/ub-wide-ptr.rs:117:1
210+
--> $DIR/ub-wide-ptr.rs:123:1
187211
|
188212
LL | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
189213
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling or unaligned vtable pointer in wide pointer or too small vtable
190214
|
191215
= 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.
192216

193217
error[E0080]: could not evaluate static initializer
194-
--> $DIR/ub-wide-ptr.rs:123:5
218+
--> $DIR/ub-wide-ptr.rs:129:5
195219
|
196220
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
197221
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ inbounds test failed: 0x0 is not a valid pointer
198222

199223
error[E0080]: could not evaluate static initializer
200-
--> $DIR/ub-wide-ptr.rs:127:5
224+
--> $DIR/ub-wide-ptr.rs:133:5
201225
|
202226
LL | mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
203227
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: pointer must be in-bounds at offset N, but is outside bounds of allocN which has size N
204228

205-
error: aborting due to 25 previous errors
229+
error: aborting due to 28 previous errors
206230

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

0 commit comments

Comments
 (0)