Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 54d95ed

Browse files
committed
catch InvalidUninitBytes during validation
1 parent 0cd7ff7 commit 54d95ed

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/librustc_mir/interpret/validity.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
366366
let place = try_validation!(
367367
self.ecx.ref_to_mplace(value),
368368
self.path,
369-
err_ub!(InvalidUninitBytes { .. }) => { "uninitialized {}", kind },
369+
err_ub!(InvalidUninitBytes(None)) => { "uninitialized {}", kind },
370370
);
371371
if place.layout.is_unsized() {
372372
self.check_wide_ptr_meta(place.meta, place.layout)?;
@@ -477,7 +477,8 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
477477
try_validation!(
478478
value.to_bool(),
479479
self.path,
480-
err_ub!(InvalidBool(..)) => { "{}", value } expected { "a boolean" },
480+
err_ub!(InvalidBool(..)) | err_ub!(InvalidUninitBytes(None)) =>
481+
{ "{}", value } expected { "a boolean" },
481482
);
482483
Ok(true)
483484
}
@@ -486,7 +487,8 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
486487
try_validation!(
487488
value.to_char(),
488489
self.path,
489-
err_ub!(InvalidChar(..)) => { "{}", value } expected { "a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)" },
490+
err_ub!(InvalidChar(..)) | err_ub!(InvalidUninitBytes(None)) =>
491+
{ "{}", value } expected { "a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)" },
490492
);
491493
Ok(true)
492494
}
@@ -515,7 +517,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
515517
let place = try_validation!(
516518
self.ecx.ref_to_mplace(self.ecx.read_immediate(value)?),
517519
self.path,
518-
err_ub!(InvalidUninitBytes { .. } ) => { "uninitialized raw pointer" },
520+
err_ub!(InvalidUninitBytes(None)) => { "uninitialized raw pointer" },
519521
);
520522
if place.layout.is_unsized() {
521523
self.check_wide_ptr_meta(place.meta, place.layout)?;
@@ -537,6 +539,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
537539
self.path,
538540
err_ub!(DanglingIntPointer(..)) |
539541
err_ub!(InvalidFunctionPointer(..)) |
542+
err_ub!(InvalidUninitBytes(None)) |
540543
err_unsup!(ReadBytesAsPointer) =>
541544
{ "{}", value } expected { "a function pointer" },
542545
);
@@ -593,7 +596,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
593596
let value = try_validation!(
594597
value.not_undef(),
595598
self.path,
596-
err_ub!(InvalidUninitBytes { .. }) => { "{}", value }
599+
err_ub!(InvalidUninitBytes(None)) => { "{}", value }
597600
expected { "something {}", wrapping_range_format(valid_range, max_hi) },
598601
);
599602
let bits = match value.to_bits_or_ptr(op.layout.size, self.ecx) {
@@ -699,6 +702,9 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
699702
self.path,
700703
err_ub!(InvalidTag(val)) =>
701704
{ "{}", val } expected { "a valid enum tag" },
705+
// `InvalidUninitBytes` can be caused by `read_discriminant` in Miri if all initialized tags are valid.
706+
err_ub!(InvalidUninitBytes(None)) =>
707+
{ "uninitialized bytes" } expected { "a valid enum tag" },
702708
err_unsup!(ReadPointerAsBytes) =>
703709
{ "a pointer" } expected { "plain (non-pointer) bytes" },
704710
);

0 commit comments

Comments
 (0)