Skip to content

Commit 13f9aa1

Browse files
committed
Auto merge of rust-lang#74664 - pnadon:Miri-rename-undef-uninit, r=RalfJung
Miri rename undef uninit Renamed parts of code within the `librustc_middle/mir/interpret/` directory. Related issue [rust-lang#71193](rust-lang#71193)
2 parents 461707c + ef9c4f5 commit 13f9aa1

File tree

12 files changed

+94
-94
lines changed

12 files changed

+94
-94
lines changed

src/librustc_codegen_ssa/mir/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
883883
let ptr = Pointer::new(AllocId(0), offset);
884884
alloc
885885
.read_scalar(&bx, ptr, size)
886-
.and_then(|s| s.not_undef())
886+
.and_then(|s| s.check_init())
887887
.unwrap_or_else(|e| {
888888
bx.tcx().sess.span_err(
889889
span,

src/librustc_middle/mir/interpret/allocation.rs

+47-47
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<Tag> Allocation<Tag> {
105105
Allocation::from_bytes(slice, Align::from_bytes(1).unwrap())
106106
}
107107

108-
pub fn undef(size: Size, align: Align) -> Self {
108+
pub fn uninit(size: Size, align: Align) -> Self {
109109
Allocation {
110110
bytes: vec![0; size.bytes_usize()],
111111
relocations: Relocations::new(),
@@ -153,7 +153,7 @@ impl<Tag, Extra> Allocation<Tag, Extra> {
153153
self.size.bytes_usize()
154154
}
155155

156-
/// Looks at a slice which may describe undefined bytes or describe a relocation. This differs
156+
/// Looks at a slice which may describe uninitialized bytes or describe a relocation. This differs
157157
/// from `get_bytes_with_undef_and_ptr` in that it does no relocation checks (even on the
158158
/// edges) at all. It further ignores `AllocationExtra` callbacks.
159159
/// This must not be used for reads affecting the interpreter execution.
@@ -192,7 +192,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
192192
offset.bytes_usize()..end
193193
}
194194

195-
/// The last argument controls whether we error out when there are undefined
195+
/// The last argument controls whether we error out when there are uninitialized
196196
/// or pointer bytes. You should never call this, call `get_bytes` or
197197
/// `get_bytes_with_undef_and_ptr` instead,
198198
///
@@ -206,12 +206,12 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
206206
cx: &impl HasDataLayout,
207207
ptr: Pointer<Tag>,
208208
size: Size,
209-
check_defined_and_ptr: bool,
209+
check_init_and_ptr: bool,
210210
) -> InterpResult<'tcx, &[u8]> {
211211
let range = self.check_bounds(ptr.offset, size);
212212

213-
if check_defined_and_ptr {
214-
self.check_defined(ptr, size)?;
213+
if check_init_and_ptr {
214+
self.check_init(ptr, size)?;
215215
self.check_relocations(cx, ptr, size)?;
216216
} else {
217217
// We still don't want relocations on the *edges*.
@@ -239,7 +239,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
239239
self.get_bytes_internal(cx, ptr, size, true)
240240
}
241241

242-
/// It is the caller's responsibility to handle undefined and pointer bytes.
242+
/// It is the caller's responsibility to handle uninitialized and pointer bytes.
243243
/// However, this still checks that there are no relocations on the *edges*.
244244
///
245245
/// It is the caller's responsibility to check bounds and alignment beforehand.
@@ -267,7 +267,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
267267
) -> InterpResult<'tcx, &mut [u8]> {
268268
let range = self.check_bounds(ptr.offset, size);
269269

270-
self.mark_definedness(ptr, size, true);
270+
self.mark_init(ptr, size, true);
271271
self.clear_relocations(cx, ptr, size)?;
272272

273273
AllocationExtra::memory_written(self, ptr, size)?;
@@ -303,7 +303,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
303303

304304
/// Validates that `ptr.offset` and `ptr.offset + size` do not point to the middle of a
305305
/// relocation. If `allow_ptr_and_undef` is `false`, also enforces that the memory in the
306-
/// given range contains neither relocations nor undef bytes.
306+
/// given range contains neither relocations nor uninitialized bytes.
307307
pub fn check_bytes(
308308
&self,
309309
cx: &impl HasDataLayout,
@@ -313,9 +313,9 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
313313
) -> InterpResult<'tcx> {
314314
// Check bounds and relocations on the edges.
315315
self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
316-
// Check undef and ptr.
316+
// Check uninit and ptr.
317317
if !allow_ptr_and_undef {
318-
self.check_defined(ptr, size)?;
318+
self.check_init(ptr, size)?;
319319
self.check_relocations(cx, ptr, size)?;
320320
}
321321
Ok(())
@@ -364,7 +364,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
364364
let bytes = self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
365365
// Uninit check happens *after* we established that the alignment is correct.
366366
// We must not return `Ok()` for unaligned pointers!
367-
if self.is_defined(ptr, size).is_err() {
367+
if self.is_init(ptr, size).is_err() {
368368
// This inflates uninitialized bytes to the entire scalar, even if only a few
369369
// bytes are uninitialized.
370370
return Ok(ScalarMaybeUninit::Uninit);
@@ -416,7 +416,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
416416
let val = match val {
417417
ScalarMaybeUninit::Scalar(scalar) => scalar,
418418
ScalarMaybeUninit::Uninit => {
419-
self.mark_definedness(ptr, type_size, false);
419+
self.mark_init(ptr, type_size, false);
420420
return Ok(());
421421
}
422422
};
@@ -512,7 +512,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
512512
let start = ptr.offset;
513513
let end = start + size; // `Size` addition
514514

515-
// Mark parts of the outermost relocations as undefined if they partially fall outside the
515+
// Mark parts of the outermost relocations as uninitialized if they partially fall outside the
516516
// given range.
517517
if first < start {
518518
self.init_mask.set_range(first, start, false);
@@ -542,20 +542,20 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
542542
}
543543
}
544544

545-
/// Undefined bytes.
545+
/// Uninitialized bytes.
546546
impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
547-
/// Checks whether the given range is entirely defined.
547+
/// Checks whether the given range is entirely initialized.
548548
///
549-
/// Returns `Ok(())` if it's defined. Otherwise returns the range of byte
550-
/// indexes of the first contiguous undefined access.
551-
fn is_defined(&self, ptr: Pointer<Tag>, size: Size) -> Result<(), Range<Size>> {
549+
/// Returns `Ok(())` if it's initialized. Otherwise returns the range of byte
550+
/// indexes of the first contiguous uninitialized access.
551+
fn is_init(&self, ptr: Pointer<Tag>, size: Size) -> Result<(), Range<Size>> {
552552
self.init_mask.is_range_initialized(ptr.offset, ptr.offset + size) // `Size` addition
553553
}
554554

555-
/// Checks that a range of bytes is defined. If not, returns the `InvalidUndefBytes`
556-
/// error which will report the first range of bytes which is undefined.
557-
fn check_defined(&self, ptr: Pointer<Tag>, size: Size) -> InterpResult<'tcx> {
558-
self.is_defined(ptr, size).or_else(|idx_range| {
555+
/// Checks that a range of bytes is initialized. If not, returns the `InvalidUninitBytes`
556+
/// error which will report the first range of bytes which is uninitialized.
557+
fn check_init(&self, ptr: Pointer<Tag>, size: Size) -> InterpResult<'tcx> {
558+
self.is_init(ptr, size).or_else(|idx_range| {
559559
throw_ub!(InvalidUninitBytes(Some(Box::new(UninitBytesAccess {
560560
access_ptr: ptr.erase_tag(),
561561
access_size: size,
@@ -565,44 +565,44 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
565565
})
566566
}
567567

568-
pub fn mark_definedness(&mut self, ptr: Pointer<Tag>, size: Size, new_state: bool) {
568+
pub fn mark_init(&mut self, ptr: Pointer<Tag>, size: Size, is_init: bool) {
569569
if size.bytes() == 0 {
570570
return;
571571
}
572-
self.init_mask.set_range(ptr.offset, ptr.offset + size, new_state);
572+
self.init_mask.set_range(ptr.offset, ptr.offset + size, is_init);
573573
}
574574
}
575575

576-
/// Run-length encoding of the undef mask.
576+
/// Run-length encoding of the uninit mask.
577577
/// Used to copy parts of a mask multiple times to another allocation.
578-
pub struct AllocationDefinedness {
579-
/// The definedness of the first range.
578+
pub struct InitMaskCompressed {
579+
/// Whether the first range is initialized.
580580
initial: bool,
581581
/// The lengths of ranges that are run-length encoded.
582-
/// The definedness of the ranges alternate starting with `initial`.
582+
/// The initialization state of the ranges alternate starting with `initial`.
583583
ranges: smallvec::SmallVec<[u64; 1]>,
584584
}
585585

586-
impl AllocationDefinedness {
587-
pub fn all_bytes_undef(&self) -> bool {
588-
// The `ranges` are run-length encoded and of alternating definedness.
589-
// So if `ranges.len() > 1` then the second block is a range of defined.
586+
impl InitMaskCompressed {
587+
pub fn no_bytes_init(&self) -> bool {
588+
// The `ranges` are run-length encoded and of alternating initialization state.
589+
// So if `ranges.len() > 1` then the second block is an initialized range.
590590
!self.initial && self.ranges.len() == 1
591591
}
592592
}
593593

594-
/// Transferring the definedness mask to other allocations.
594+
/// Transferring the initialization mask to other allocations.
595595
impl<Tag, Extra> Allocation<Tag, Extra> {
596-
/// Creates a run-length encoding of the undef mask.
597-
pub fn compress_undef_range(&self, src: Pointer<Tag>, size: Size) -> AllocationDefinedness {
596+
/// Creates a run-length encoding of the initialization mask.
597+
pub fn compress_undef_range(&self, src: Pointer<Tag>, size: Size) -> InitMaskCompressed {
598598
// Since we are copying `size` bytes from `src` to `dest + i * size` (`for i in 0..repeat`),
599-
// a naive undef mask copying algorithm would repeatedly have to read the undef mask from
599+
// a naive initialization mask copying algorithm would repeatedly have to read the initialization mask from
600600
// the source and write it to the destination. Even if we optimized the memory accesses,
601601
// we'd be doing all of this `repeat` times.
602-
// Therefore we precompute a compressed version of the undef mask of the source value and
602+
// Therefore we precompute a compressed version of the initialization mask of the source value and
603603
// then write it back `repeat` times without computing any more information from the source.
604604

605-
// A precomputed cache for ranges of defined/undefined bits
605+
// A precomputed cache for ranges of initialized / uninitialized bits
606606
// 0000010010001110 will become
607607
// `[5, 1, 2, 1, 3, 3, 1]`,
608608
// where each element toggles the state.
@@ -613,7 +613,7 @@ impl<Tag, Extra> Allocation<Tag, Extra> {
613613
let mut cur = initial;
614614

615615
for i in 1..size.bytes() {
616-
// FIXME: optimize to bitshift the current undef block's bits and read the top bit.
616+
// FIXME: optimize to bitshift the current uninitialized block's bits and read the top bit.
617617
if self.init_mask.get(src.offset + Size::from_bytes(i)) == cur {
618618
cur_len += 1;
619619
} else {
@@ -625,13 +625,13 @@ impl<Tag, Extra> Allocation<Tag, Extra> {
625625

626626
ranges.push(cur_len);
627627

628-
AllocationDefinedness { ranges, initial }
628+
InitMaskCompressed { ranges, initial }
629629
}
630630

631-
/// Applies multiple instances of the run-length encoding to the undef mask.
632-
pub fn mark_compressed_undef_range(
631+
/// Applies multiple instances of the run-length encoding to the initialization mask.
632+
pub fn mark_compressed_init_range(
633633
&mut self,
634-
defined: &AllocationDefinedness,
634+
defined: &InitMaskCompressed,
635635
dest: Pointer<Tag>,
636636
size: Size,
637637
repeat: u64,
@@ -740,7 +740,7 @@ impl<Tag: Copy, Extra> Allocation<Tag, Extra> {
740740
}
741741

742742
////////////////////////////////////////////////////////////////////////////////
743-
// Undefined byte tracking
743+
// Uninitialized byte tracking
744744
////////////////////////////////////////////////////////////////////////////////
745745

746746
type Block = u64;
@@ -778,11 +778,11 @@ impl InitMask {
778778

779779
match idx {
780780
Some(idx) => {
781-
let undef_end = (idx.bytes()..end.bytes())
781+
let uninit_end = (idx.bytes()..end.bytes())
782782
.map(Size::from_bytes)
783783
.find(|&i| self.get(i))
784784
.unwrap_or(end);
785-
Err(idx..undef_end)
785+
Err(idx..uninit_end)
786786
}
787787
None => Ok(()),
788788
}

src/librustc_middle/mir/interpret/value.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ impl<'tcx, Tag> ScalarMaybeUninit<Tag> {
606606
}
607607

608608
#[inline]
609-
pub fn not_undef(self) -> InterpResult<'static, Scalar<Tag>> {
609+
pub fn check_init(self) -> InterpResult<'static, Scalar<Tag>> {
610610
match self {
611611
ScalarMaybeUninit::Scalar(scalar) => Ok(scalar),
612612
ScalarMaybeUninit::Uninit => throw_ub!(InvalidUninitBytes(None)),
@@ -615,72 +615,72 @@ impl<'tcx, Tag> ScalarMaybeUninit<Tag> {
615615

616616
#[inline(always)]
617617
pub fn to_bool(self) -> InterpResult<'tcx, bool> {
618-
self.not_undef()?.to_bool()
618+
self.check_init()?.to_bool()
619619
}
620620

621621
#[inline(always)]
622622
pub fn to_char(self) -> InterpResult<'tcx, char> {
623-
self.not_undef()?.to_char()
623+
self.check_init()?.to_char()
624624
}
625625

626626
#[inline(always)]
627627
pub fn to_f32(self) -> InterpResult<'tcx, Single> {
628-
self.not_undef()?.to_f32()
628+
self.check_init()?.to_f32()
629629
}
630630

631631
#[inline(always)]
632632
pub fn to_f64(self) -> InterpResult<'tcx, Double> {
633-
self.not_undef()?.to_f64()
633+
self.check_init()?.to_f64()
634634
}
635635

636636
#[inline(always)]
637637
pub fn to_u8(self) -> InterpResult<'tcx, u8> {
638-
self.not_undef()?.to_u8()
638+
self.check_init()?.to_u8()
639639
}
640640

641641
#[inline(always)]
642642
pub fn to_u16(self) -> InterpResult<'tcx, u16> {
643-
self.not_undef()?.to_u16()
643+
self.check_init()?.to_u16()
644644
}
645645

646646
#[inline(always)]
647647
pub fn to_u32(self) -> InterpResult<'tcx, u32> {
648-
self.not_undef()?.to_u32()
648+
self.check_init()?.to_u32()
649649
}
650650

651651
#[inline(always)]
652652
pub fn to_u64(self) -> InterpResult<'tcx, u64> {
653-
self.not_undef()?.to_u64()
653+
self.check_init()?.to_u64()
654654
}
655655

656656
#[inline(always)]
657657
pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
658-
self.not_undef()?.to_machine_usize(cx)
658+
self.check_init()?.to_machine_usize(cx)
659659
}
660660

661661
#[inline(always)]
662662
pub fn to_i8(self) -> InterpResult<'tcx, i8> {
663-
self.not_undef()?.to_i8()
663+
self.check_init()?.to_i8()
664664
}
665665

666666
#[inline(always)]
667667
pub fn to_i16(self) -> InterpResult<'tcx, i16> {
668-
self.not_undef()?.to_i16()
668+
self.check_init()?.to_i16()
669669
}
670670

671671
#[inline(always)]
672672
pub fn to_i32(self) -> InterpResult<'tcx, i32> {
673-
self.not_undef()?.to_i32()
673+
self.check_init()?.to_i32()
674674
}
675675

676676
#[inline(always)]
677677
pub fn to_i64(self) -> InterpResult<'tcx, i64> {
678-
self.not_undef()?.to_i64()
678+
self.check_init()?.to_i64()
679679
}
680680

681681
#[inline(always)]
682682
pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64> {
683-
self.not_undef()?.to_machine_isize(cx)
683+
self.check_init()?.to_machine_isize(cx)
684684
}
685685
}
686686

src/librustc_mir/const_eval/eval_queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub(super) fn op_to_const<'tcx>(
154154
ScalarMaybeUninit::Uninit => to_const_value(op.assert_mem_place(ecx)),
155155
},
156156
Immediate::ScalarPair(a, b) => {
157-
let (data, start) = match a.not_undef().unwrap() {
157+
let (data, start) = match a.check_init().unwrap() {
158158
Scalar::Ptr(ptr) => {
159159
(ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory(), ptr.offset.bytes())
160160
}

0 commit comments

Comments
 (0)