From cb5533cff21a1e7b929d96142ead825de25b2cbc Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 17 May 2021 14:42:27 +0200 Subject: [PATCH] remove some functions that were only used by Miri --- .../src/mir/interpret/allocation.rs | 23 ------ compiler/rustc_mir/src/interpret/memory.rs | 75 ------------------- 2 files changed, 98 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index 766d6a06f7e59..fae05cb0a37db 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -276,29 +276,6 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra> Allocation { /// Reading and writing. impl<'tcx, Tag: Copy, Extra: AllocationExtra> Allocation { - /// Reads bytes until a `0` is encountered. Will error if the end of the allocation is reached - /// before a `0` is found. - /// - /// Most likely, you want to call `Memory::read_c_str` instead of this method. - pub fn read_c_str( - &self, - cx: &impl HasDataLayout, - ptr: Pointer, - ) -> InterpResult<'tcx, &[u8]> { - let offset = ptr.offset.bytes_usize(); - Ok(match self.bytes[offset..].iter().position(|&c| c == 0) { - Some(size) => { - let size_with_null = Size::from_bytes(size) + Size::from_bytes(1); - // Go through `get_bytes` for checks and AllocationExtra hooks. - // We read the null, so we include it in the request, but we want it removed - // from the result, so we do subslicing. - &self.get_bytes(cx, ptr, size_with_null)?[..size] - } - // This includes the case where `offset` is out-of-bounds to begin with. - None => throw_ub!(UnterminatedCString(ptr.erase_tag())), - }) - } - /// Validates that `ptr.offset` and `ptr.offset + size` do not point to the middle of a /// relocation. If `allow_uninit_and_ptr` is `false`, also enforces that the memory in the /// given range contains neither relocations nor uninitialized bytes. diff --git a/compiler/rustc_mir/src/interpret/memory.rs b/compiler/rustc_mir/src/interpret/memory.rs index 1aaa56403f464..4030b3175fc83 100644 --- a/compiler/rustc_mir/src/interpret/memory.rs +++ b/compiler/rustc_mir/src/interpret/memory.rs @@ -804,41 +804,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { self.get_raw(ptr.alloc_id)?.get_bytes(self, ptr, size) } - /// Reads a 0-terminated sequence of bytes from memory. Returns them as a slice. - /// - /// Performs appropriate bounds checks. - pub fn read_c_str(&self, ptr: Scalar) -> InterpResult<'tcx, &[u8]> { - let ptr = self.force_ptr(ptr)?; // We need to read at least 1 byte, so we *need* a ptr. - self.get_raw(ptr.alloc_id)?.read_c_str(self, ptr) - } - - /// Reads a 0x0000-terminated u16-sequence from memory. Returns them as a Vec. - /// Terminator 0x0000 is not included in the returned Vec. - /// - /// Performs appropriate bounds checks. - pub fn read_wide_str(&self, ptr: Scalar) -> InterpResult<'tcx, Vec> { - let size_2bytes = Size::from_bytes(2); - let align_2bytes = Align::from_bytes(2).unwrap(); - // We need to read at least 2 bytes, so we *need* a ptr. - let mut ptr = self.force_ptr(ptr)?; - let allocation = self.get_raw(ptr.alloc_id)?; - let mut u16_seq = Vec::new(); - - loop { - ptr = self - .check_ptr_access(ptr.into(), size_2bytes, align_2bytes)? - .expect("cannot be a ZST"); - let single_u16 = allocation.read_scalar(self, ptr, size_2bytes)?.to_u16()?; - if single_u16 != 0x0000 { - u16_seq.push(single_u16); - ptr = ptr.offset(size_2bytes, self)?; - } else { - break; - } - } - Ok(u16_seq) - } - /// Writes the given stream of bytes into memory. /// /// Performs appropriate bounds checks. @@ -866,46 +831,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { self.get_raw_mut(ptr.alloc_id)?.write_bytes(&tcx, ptr, src) } - /// Writes the given stream of u16s into memory. - /// - /// Performs appropriate bounds checks. - pub fn write_u16s( - &mut self, - ptr: Scalar, - src: impl IntoIterator, - ) -> InterpResult<'tcx> { - let mut src = src.into_iter(); - let (lower, upper) = src.size_hint(); - let len = upper.expect("can only write bounded iterators"); - assert_eq!(lower, len, "can only write iterators with a precise length"); - - let size = Size::from_bytes(lower); - let ptr = match self.check_ptr_access(ptr, size, Align::from_bytes(2).unwrap())? { - Some(ptr) => ptr, - None => { - // zero-sized access - assert_matches!( - src.next(), - None, - "iterator said it was empty but returned an element" - ); - return Ok(()); - } - }; - let tcx = self.tcx; - let allocation = self.get_raw_mut(ptr.alloc_id)?; - - for idx in 0..len { - let val = Scalar::from_u16( - src.next().expect("iterator was shorter than it said it would be"), - ); - let offset_ptr = ptr.offset(Size::from_bytes(idx) * 2, &tcx)?; // `Size` multiplication - allocation.write_scalar(&tcx, offset_ptr, val.into(), Size::from_bytes(2))?; - } - assert_matches!(src.next(), None, "iterator was longer than it said it would be"); - Ok(()) - } - /// Expects the caller to have checked bounds and alignment. pub fn copy( &mut self,