Skip to content

Commit ef9c4f5

Browse files
committed
Miri: rename "undef" to "uninit"
renamed Allocation::check_defined_and_ptr to Allocation::check_init_and_ptr renamed Allocation::check_defined_and_ptr to Allocation::check_init_and_ptr in src/librustc_middle/mir/interpret/allocation.rs renamed Allocation::is_defined and Allocation::check_defined, fixed documentation renamed Allocation::is_defined and Allocation::check_defined to is_init and check_init respectively. Fixed documentation so it correctly refers to "initialization" instead of "defined"-ness renamed Allocation::mark_definedness renamed Allocation::mark_definedness to Allocation::mark_init Renamed new_state parameter in Allocation::mark_init Renamed new_state to is_init, as the latter is more descriptive. renamed functions in AllocationDefinedness renamed AllocationDefinedness::all_bytes_undef and AllocationDefinedness::mark_compressed_undef_range to no_bytes_init and mark_compressed_init_range respectively. renamed AllocationDefinedness to InitMaskCompressed renamed Immediate::to_scalar_or_undef renamed to to_scalar_or_uninit fixed comment references to "undef" Changed comments referring to "undef" and "definedness" to "initialization" and "initialization state" in src/librustc_mir/interpret/memory.rs and src/librustc_middle/mir/interpret/allocation.rs changed references to "undef" in comments and a variable Changed some comments referring to "undef" to use "uninitialized" instead. Also changed a variable from "undef_end" to "uninit_end". All changes were made within src/librustc_middle/mir/interpret/allocation.rs. Changed more comments referring to undef Changed comments to use "uninitialized" instead of "undef" in src/librustc_middle/mir/interpret/allocation.rs.
1 parent 55984b6 commit ef9c4f5

File tree

3 files changed

+59
-59
lines changed

3 files changed

+59
-59
lines changed

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_mir/interpret/memory.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
171171
align: Align,
172172
kind: MemoryKind<M::MemoryKind>,
173173
) -> Pointer<M::PointerTag> {
174-
let alloc = Allocation::undef(size, align);
174+
let alloc = Allocation::uninit(size, align);
175175
self.allocate_with(alloc, kind)
176176
}
177177

@@ -904,18 +904,18 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
904904

905905
let dest_bytes = dest_bytes.as_mut_ptr();
906906

907-
// Prepare a copy of the undef mask.
907+
// Prepare a copy of the initialization mask.
908908
let compressed = self.get_raw(src.alloc_id)?.compress_undef_range(src, size);
909909

910-
if compressed.all_bytes_undef() {
911-
// Fast path: If all bytes are `undef` then there is nothing to copy. The target range
912-
// is marked as undef but we otherwise omit changing the byte representation which may
913-
// be arbitrary for undef bytes.
910+
if compressed.no_bytes_init() {
911+
// Fast path: If all bytes are `uninit` then there is nothing to copy. The target range
912+
// is marked as unititialized but we otherwise omit changing the byte representation which may
913+
// be arbitrary for uninitialized bytes.
914914
// This also avoids writing to the target bytes so that the backing allocation is never
915-
// touched if the bytes stay undef for the whole interpreter execution. On contemporary
915+
// touched if the bytes stay uninitialized for the whole interpreter execution. On contemporary
916916
// operating system this can avoid physically allocating the page.
917917
let dest_alloc = self.get_raw_mut(dest.alloc_id)?;
918-
dest_alloc.mark_definedness(dest, size * length, false); // `Size` multiplication
918+
dest_alloc.mark_init(dest, size * length, false); // `Size` multiplication
919919
dest_alloc.mark_relocation_range(relocations);
920920
return Ok(());
921921
}
@@ -955,7 +955,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
955955
}
956956

957957
// now fill in all the data
958-
self.get_raw_mut(dest.alloc_id)?.mark_compressed_undef_range(
958+
self.get_raw_mut(dest.alloc_id)?.mark_compressed_init_range(
959959
&compressed,
960960
dest,
961961
size,

src/librustc_mir/interpret/operand.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'tcx, Tag> Immediate<Tag> {
6363
}
6464

6565
#[inline]
66-
pub fn to_scalar_or_undef(self) -> ScalarMaybeUninit<Tag> {
66+
pub fn to_scalar_or_uninit(self) -> ScalarMaybeUninit<Tag> {
6767
match self {
6868
Immediate::Scalar(val) => val,
6969
Immediate::ScalarPair(..) => bug!("Got a wide pointer where a scalar was expected"),
@@ -72,7 +72,7 @@ impl<'tcx, Tag> Immediate<Tag> {
7272

7373
#[inline]
7474
pub fn to_scalar(self) -> InterpResult<'tcx, Scalar<Tag>> {
75-
self.to_scalar_or_undef().check_init()
75+
self.to_scalar_or_uninit().check_init()
7676
}
7777

7878
#[inline]
@@ -333,7 +333,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
333333
&self,
334334
op: OpTy<'tcx, M::PointerTag>,
335335
) -> InterpResult<'tcx, ScalarMaybeUninit<M::PointerTag>> {
336-
Ok(self.read_immediate(op)?.to_scalar_or_undef())
336+
Ok(self.read_immediate(op)?.to_scalar_or_uninit())
337337
}
338338

339339
// Turn the wide MPlace into a string (must already be dereferenced!)

0 commit comments

Comments
 (0)