Auto merge of #85414 - RalfJung:rollup-ueqcik4, r=RalfJung · rust-lang/rust@3e99439 (original) (raw)

`@@ -244,7 +244,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {

`

244

244

`let new_ptr = self.allocate(new_size, new_align, kind);

`

245

245

`let old_size = match old_size_and_align {

`

246

246

`Some((size, _align)) => size,

`

247

``

`-

None => self.get_raw(ptr.alloc_id)?.size,

`

``

247

`+

None => self.get_raw(ptr.alloc_id)?.size(),

`

248

248

`};

`

249

249

`self.copy(ptr, new_ptr, old_size.min(new_size), /nonoverlapping/ true)?;

`

250

250

`self.deallocate(ptr, old_size_and_align, kind)?;

`

`@@ -306,11 +306,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {

`

306

306

`);

`

307

307

`}

`

308

308

`if let Some((size, align)) = old_size_and_align {

`

309

``

`-

if size != alloc.size || align != alloc.align {

`

``

309

`+

if size != alloc.size() || align != alloc.align {

`

310

310

`throw_ub_format!(

`

311

311

`"incorrect layout on deallocation: {} has size {} and alignment {}, but gave size {} and alignment {}",

`

312

312

` ptr.alloc_id,

`

313

``

`-

alloc.size.bytes(),

`

``

313

`+

alloc.size().bytes(),

`

314

314

` alloc.align.bytes(),

`

315

315

` size.bytes(),

`

316

316

` align.bytes(),

`

`@@ -319,11 +319,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {

`

319

319

`}

`

320

320

``

321

321

`// Let the machine take some extra action

`

322

``

`-

let size = alloc.size;

`

``

322

`+

let size = alloc.size();

`

323

323

`AllocationExtra::memory_deallocated(&mut alloc, ptr, size)?;

`

324

324

``

325

325

`// Don't forget to remember size and align of this now-dead allocation

`

326

``

`-

let old = self.dead_alloc_map.insert(ptr.alloc_id, (alloc.size, alloc.align));

`

``

326

`+

let old = self.dead_alloc_map.insert(ptr.alloc_id, (alloc.size(), alloc.align));

`

327

327

`if old.is_some() {

`

328

328

`bug!("Nothing can be deallocated twice");

`

329

329

`}

`

`@@ -586,7 +586,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {

`

586

586

`` // a) cause cycles in case id refers to a static

``

587

587

`// b) duplicate a global's allocation in miri

`

588

588

`if let Some((_, alloc)) = self.alloc_map.get(id) {

`

589

``

`-

return Ok((alloc.size, alloc.align));

`

``

589

`+

return Ok((alloc.size(), alloc.align));

`

590

590

`}

`

591

591

``

592

592

`// # Function pointers

`

`@@ -614,7 +614,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {

`

614

614

`Some(GlobalAlloc::Memory(alloc)) => {

`

615

615

`// Need to duplicate the logic here, because the global allocations have

`

616

616

`// different associated types than the interpreter-local ones.

`

617

``

`-

Ok((alloc.size, alloc.align))

`

``

617

`+

Ok((alloc.size(), alloc.align))

`

618

618

`}

`

619

619

`Some(GlobalAlloc::Function(_)) => bug!("We already checked function pointers above"),

`

620

620

`// The rest must be dead.

`

`@@ -804,41 +804,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {

`

804

804

`self.get_raw(ptr.alloc_id)?.get_bytes(self, ptr, size)

`

805

805

`}

`

806

806

``

807

``

`-

/// Reads a 0-terminated sequence of bytes from memory. Returns them as a slice.

`

808

``

`-

///

`

809

``

`-

/// Performs appropriate bounds checks.

`

810

``

`-

pub fn read_c_str(&self, ptr: Scalar<M::PointerTag>) -> InterpResult<'tcx, &[u8]> {

`

811

``

`-

let ptr = self.force_ptr(ptr)?; // We need to read at least 1 byte, so we need a ptr.

`

812

``

`-

self.get_raw(ptr.alloc_id)?.read_c_str(self, ptr)

`

813

``

`-

}

`

814

``

-

815

``

`-

/// Reads a 0x0000-terminated u16-sequence from memory. Returns them as a Vec.

`

816

``

`-

/// Terminator 0x0000 is not included in the returned Vec.

`

817

``

`-

///

`

818

``

`-

/// Performs appropriate bounds checks.

`

819

``

`-

pub fn read_wide_str(&self, ptr: Scalar<M::PointerTag>) -> InterpResult<'tcx, Vec> {

`

820

``

`-

let size_2bytes = Size::from_bytes(2);

`

821

``

`-

let align_2bytes = Align::from_bytes(2).unwrap();

`

822

``

`-

// We need to read at least 2 bytes, so we need a ptr.

`

823

``

`-

let mut ptr = self.force_ptr(ptr)?;

`

824

``

`-

let allocation = self.get_raw(ptr.alloc_id)?;

`

825

``

`-

let mut u16_seq = Vec::new();

`

826

``

-

827

``

`-

loop {

`

828

``

`-

ptr = self

`

829

``

`-

.check_ptr_access(ptr.into(), size_2bytes, align_2bytes)?

`

830

``

`-

.expect("cannot be a ZST");

`

831

``

`-

let single_u16 = allocation.read_scalar(self, ptr, size_2bytes)?.to_u16()?;

`

832

``

`-

if single_u16 != 0x0000 {

`

833

``

`-

u16_seq.push(single_u16);

`

834

``

`-

ptr = ptr.offset(size_2bytes, self)?;

`

835

``

`-

} else {

`

836

``

`-

break;

`

837

``

`-

}

`

838

``

`-

}

`

839

``

`-

Ok(u16_seq)

`

840

``

`-

}

`

841

``

-

842

807

`/// Writes the given stream of bytes into memory.

`

843

808

`///

`

844

809

`/// Performs appropriate bounds checks.

`

`@@ -866,46 +831,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {

`

866

831

`self.get_raw_mut(ptr.alloc_id)?.write_bytes(&tcx, ptr, src)

`

867

832

`}

`

868

833

``

869

``

`-

/// Writes the given stream of u16s into memory.

`

870

``

`-

///

`

871

``

`-

/// Performs appropriate bounds checks.

`

872

``

`-

pub fn write_u16s(

`

873

``

`-

&mut self,

`

874

``

`-

ptr: Scalar<M::PointerTag>,

`

875

``

`-

src: impl IntoIterator<Item = u16>,

`

876

``

`-

) -> InterpResult<'tcx> {

`

877

``

`-

let mut src = src.into_iter();

`

878

``

`-

let (lower, upper) = src.size_hint();

`

879

``

`-

let len = upper.expect("can only write bounded iterators");

`

880

``

`-

assert_eq!(lower, len, "can only write iterators with a precise length");

`

881

``

-

882

``

`-

let size = Size::from_bytes(lower);

`

883

``

`-

let ptr = match self.check_ptr_access(ptr, size, Align::from_bytes(2).unwrap())? {

`

884

``

`-

Some(ptr) => ptr,

`

885

``

`-

None => {

`

886

``

`-

// zero-sized access

`

887

``

`-

assert_matches!(

`

888

``

`-

src.next(),

`

889

``

`-

None,

`

890

``

`-

"iterator said it was empty but returned an element"

`

891

``

`-

);

`

892

``

`-

return Ok(());

`

893

``

`-

}

`

894

``

`-

};

`

895

``

`-

let tcx = self.tcx;

`

896

``

`-

let allocation = self.get_raw_mut(ptr.alloc_id)?;

`

897

``

-

898

``

`-

for idx in 0..len {

`

899

``

`-

let val = Scalar::from_u16(

`

900

``

`-

src.next().expect("iterator was shorter than it said it would be"),

`

901

``

`-

);

`

902

``

`` -

let offset_ptr = ptr.offset(Size::from_bytes(idx) * 2, &tcx)?; // Size multiplication

``

903

``

`-

allocation.write_scalar(&tcx, offset_ptr, val.into(), Size::from_bytes(2))?;

`

904

``

`-

}

`

905

``

`-

assert_matches!(src.next(), None, "iterator was longer than it said it would be");

`

906

``

`-

Ok(())

`

907

``

`-

}

`

908

``

-

909

834

`/// Expects the caller to have checked bounds and alignment.

`

910

835

`pub fn copy(

`

911

836

`&mut self,

`