Skip to content

Commit 5697f73

Browse files
committed
Auto merge of rust-lang#3485 - RalfJung:read_byte_slice, r=RalfJung
move read_byte_slice to general helpers file, next to read_c_str
2 parents 0f44382 + 62e84d5 commit 5697f73

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/tools/miri/src/helpers.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,10 +912,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
912912
})
913913
}
914914

915+
/// Read bytes from a byte slice.
916+
fn read_byte_slice<'a>(
917+
&'a self,
918+
slice: &ImmTy<'tcx, Provenance>,
919+
) -> InterpResult<'tcx, &'a [u8]>
920+
where
921+
'mir: 'a,
922+
{
923+
let this = self.eval_context_ref();
924+
let (ptr, len) = slice.to_scalar_pair();
925+
let ptr = ptr.to_pointer(this)?;
926+
let len = len.to_target_usize(this)?;
927+
let bytes = this.read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(len))?;
928+
Ok(bytes)
929+
}
930+
915931
/// Read a sequence of bytes until the first null terminator.
916932
fn read_c_str<'a>(&'a self, ptr: Pointer<Option<Provenance>>) -> InterpResult<'tcx, &'a [u8]>
917933
where
918-
'tcx: 'a,
919934
'mir: 'a,
920935
{
921936
let this = self.eval_context_ref();

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
305305

306306
impl<'mir, 'tcx: 'mir> EvalContextExtPriv<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
307307
trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
308-
/// Read bytes from a `(ptr, len)` argument
309-
fn read_byte_slice<'i>(&'i self, bytes: &OpTy<'tcx, Provenance>) -> InterpResult<'tcx, &'i [u8]>
310-
where
311-
'mir: 'i,
312-
{
313-
let this = self.eval_context_ref();
314-
let (ptr, len) = this.read_immediate(bytes)?.to_scalar_pair();
315-
let ptr = ptr.to_pointer(this)?;
316-
let len = len.to_target_usize(this)?;
317-
let bytes = this.read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(len))?;
318-
Ok(bytes)
319-
}
320-
321308
/// Returns the minimum alignment for the target architecture for allocations of the given size.
322309
fn min_align(&self, size: u64, kind: MiriMemoryKind) -> Align {
323310
let this = self.eval_context_ref();
@@ -466,7 +453,9 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
466453
let [ptr, nth_parent, name] = this.check_shim(abi, Abi::Rust, link_name, args)?;
467454
let ptr = this.read_pointer(ptr)?;
468455
let nth_parent = this.read_scalar(nth_parent)?.to_u8()?;
469-
let name = this.read_byte_slice(name)?;
456+
let name = this.read_immediate(name)?;
457+
458+
let name = this.read_byte_slice(&name)?;
470459
// We must make `name` owned because we need to
471460
// end the shared borrow from `read_byte_slice` before we can
472461
// start the mutable borrow for `give_pointer_debug_name`.
@@ -527,7 +516,8 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
527516
// README for details.
528517
"miri_write_to_stdout" | "miri_write_to_stderr" => {
529518
let [msg] = this.check_shim(abi, Abi::Rust, link_name, args)?;
530-
let msg = this.read_byte_slice(msg)?;
519+
let msg = this.read_immediate(msg)?;
520+
let msg = this.read_byte_slice(&msg)?;
531521
// Note: we're ignoring errors writing to host stdout/stderr.
532522
let _ignore = match link_name.as_str() {
533523
"miri_write_to_stdout" => std::io::stdout().write_all(msg),

0 commit comments

Comments
 (0)