mirror of
https://github.com/rust-lang/rust.git
synced 2026-06-01 22:18:23 +03:00
catch errors more locally around read_discriminant
This commit is contained in:
@@ -276,19 +276,21 @@ fn aggregate_field_path_elem(&mut self, layout: TyAndLayout<'tcx>, field: usize)
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_elem(
|
||||
fn with_elem<R>(
|
||||
&mut self,
|
||||
new_op: OpTy<'tcx, M::PointerTag>,
|
||||
elem: PathElem,
|
||||
) -> InterpResult<'tcx> {
|
||||
f: impl FnOnce(&mut Self) -> InterpResult<'tcx, R>,
|
||||
) -> InterpResult<'tcx, R> {
|
||||
// Remember the old state
|
||||
let path_len = self.path.len();
|
||||
// Perform operation
|
||||
// Record new element
|
||||
self.path.push(elem);
|
||||
self.visit_value(new_op)?;
|
||||
// Perform operation
|
||||
let r = f(self)?;
|
||||
// Undo changes
|
||||
self.path.truncate(path_len);
|
||||
Ok(())
|
||||
// Done
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
fn check_wide_ptr_meta(
|
||||
@@ -649,6 +651,21 @@ fn ecx(&self) -> &InterpCx<'mir, 'tcx, M> {
|
||||
&self.ecx
|
||||
}
|
||||
|
||||
fn read_discriminant(&mut self, op: OpTy<'tcx, M::PointerTag>) -> InterpResult<'tcx, VariantIdx> {
|
||||
self.with_elem(PathElem::EnumTag, move |this| {
|
||||
Ok(try_validation!(
|
||||
this.ecx.read_discriminant(op),
|
||||
this.path,
|
||||
err_ub!(InvalidTag(val)) =>
|
||||
{ "{}", val } expected { "a valid enum tag" },
|
||||
err_ub!(InvalidUninitBytes(None)) =>
|
||||
{ "uninitialized bytes" } expected { "a valid enum tag" },
|
||||
err_unsup!(ReadPointerAsBytes) =>
|
||||
{ "a pointer" } expected { "a valid enum tag" },
|
||||
).1)
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_field(
|
||||
&mut self,
|
||||
@@ -657,7 +674,7 @@ fn visit_field(
|
||||
new_op: OpTy<'tcx, M::PointerTag>,
|
||||
) -> InterpResult<'tcx> {
|
||||
let elem = self.aggregate_field_path_elem(old_op.layout, field);
|
||||
self.visit_elem(new_op, elem)
|
||||
self.with_elem(elem, move |this| this.visit_value(new_op))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -673,7 +690,7 @@ fn visit_variant(
|
||||
ty::Generator(..) => PathElem::GeneratorState(variant_id),
|
||||
_ => bug!("Unexpected type with variant: {:?}", old_op.layout.ty),
|
||||
};
|
||||
self.visit_elem(new_op, name)
|
||||
self.with_elem(name, move |this| this.visit_value(new_op))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@@ -696,18 +713,8 @@ fn visit_value(&mut self, op: OpTy<'tcx, M::PointerTag>) -> InterpResult<'tcx> {
|
||||
// Sanity check: `builtin_deref` does not know any pointers that are not primitive.
|
||||
assert!(op.layout.ty.builtin_deref(true).is_none());
|
||||
|
||||
// Recursively walk the type. Translate some possible errors to something nicer.
|
||||
try_validation!(
|
||||
self.walk_value(op),
|
||||
self.path,
|
||||
err_ub!(InvalidTag(val)) =>
|
||||
{ "{}", val } expected { "a valid enum tag" },
|
||||
// `InvalidUninitBytes` can be caused by `read_discriminant` in Miri if all initialized tags are valid.
|
||||
err_ub!(InvalidUninitBytes(None)) =>
|
||||
{ "uninitialized bytes" } expected { "a valid enum tag" },
|
||||
err_unsup!(ReadPointerAsBytes) =>
|
||||
{ "a pointer" } expected { "plain (non-pointer) bytes" },
|
||||
);
|
||||
// Recursively walk the value at its type.
|
||||
self.walk_value(op)?;
|
||||
|
||||
// *After* all of this, check the ABI. We need to check the ABI to handle
|
||||
// types like `NonNull` where the `Scalar` info is more restrictive than what
|
||||
@@ -822,6 +829,9 @@ fn visit_aggregate(
|
||||
|
||||
throw_validation_failure!(self.path, { "uninitialized bytes" })
|
||||
}
|
||||
err_unsup!(ReadPointerAsBytes) =>
|
||||
throw_validation_failure!(self.path, { "a pointer" } expected { "plain (non-pointer) bytes" }),
|
||||
|
||||
// Propagate upwards (that will also check for unexpected errors).
|
||||
_ => return Err(err),
|
||||
}
|
||||
|
||||
@@ -125,6 +125,15 @@ pub trait $visitor_trait_name<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>>: Sized {
|
||||
fn ecx(&$($mutability)? self)
|
||||
-> &$($mutability)? InterpCx<'mir, 'tcx, M>;
|
||||
|
||||
/// `read_discriminant` can be hooked for better error messages.
|
||||
#[inline(always)]
|
||||
fn read_discriminant(
|
||||
&mut self,
|
||||
op: OpTy<'tcx, M::PointerTag>,
|
||||
) -> InterpResult<'tcx, VariantIdx> {
|
||||
Ok(self.ecx().read_discriminant(op)?.1)
|
||||
}
|
||||
|
||||
// Recursive actions, ready to be overloaded.
|
||||
/// Visits the given value, dispatching as appropriate to more specialized visitors.
|
||||
#[inline(always)]
|
||||
@@ -245,7 +254,7 @@ fn walk_value(&mut self, v: Self::V) -> InterpResult<'tcx>
|
||||
// with *its* fields.
|
||||
Variants::Multiple { .. } => {
|
||||
let op = v.to_op(self.ecx())?;
|
||||
let idx = self.ecx().read_discriminant(op)?.1;
|
||||
let idx = self.read_discriminant(op)?;
|
||||
let inner = v.project_downcast(self.ecx(), idx)?;
|
||||
trace!("walk_value: variant layout: {:#?}", inner.layout());
|
||||
// recurse with the inner type
|
||||
|
||||
@@ -5,7 +5,7 @@ LL | / static FOO: (&Foo, &Bar) = unsafe {(
|
||||
LL | | Union { u8: &BAR }.foo,
|
||||
LL | | Union { u8: &BAR }.bar,
|
||||
LL | | )};
|
||||
| |___^ type validation failed: encountered 0x05 at .1.<deref>, but expected a valid enum tag
|
||||
| |___^ type validation failed: encountered 0x05 at .1.<deref>.<enum-tag>, but expected a valid enum tag
|
||||
|
|
||||
= 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.
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ error[E0080]: it is undefined behavior to use this value
|
||||
--> $DIR/ub-enum.rs:24:1
|
||||
|
|
||||
LL | const BAD_ENUM: Enum = unsafe { mem::transmute(1usize) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x00000001, but expected a valid enum tag
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x00000001 at .<enum-tag>, but expected a valid enum tag
|
||||
|
|
||||
= 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.
|
||||
|
||||
@@ -26,7 +26,7 @@ error[E0080]: it is undefined behavior to use this value
|
||||
--> $DIR/ub-enum.rs:42:1
|
||||
|
|
||||
LL | const BAD_ENUM2: Enum2 = unsafe { mem::transmute(0usize) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x00000000, but expected a valid enum tag
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x00000000 at .<enum-tag>, but expected a valid enum tag
|
||||
|
|
||||
= 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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user