Skip to content

Commit

Permalink
Rollup merge of #108047 - oli-obk:machine->🞋, r=RalfJung
Browse files Browse the repository at this point in the history
Use `target` instead of `machine` for mir interpreter integer handling.

The naming of `machine` only makes sense from a mir interpreter internals perspective, but outside users talk about the `target` platform. As per rust-lang/rust#108029 (comment)

r? `@RalfJung`
  • Loading branch information
matthiaskrgr authored Feb 15, 2023
2 parents 24bfe03 + d4253c5 commit 453b286
Show file tree
Hide file tree
Showing 25 changed files with 107 additions and 107 deletions.
2 changes: 1 addition & 1 deletion src/borrow_tracker/stacked_borrows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ trait EvalContextPrivExt<'mir: 'ecx, 'tcx: 'mir, 'ecx>: crate::MiriInterpCxExt<'
throw_ub!(PointerOutOfBounds {
alloc_id,
alloc_size,
ptr_offset: this.machine_usize_to_isize(base_offset.bytes()),
ptr_offset: this.target_usize_to_isize(base_offset.bytes()),
ptr_size: size,
msg: CheckInAllocMsg::InboundsTest
});
Expand Down
4 changes: 2 additions & 2 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl MainThreadState {
this.machine.main_fn_ret_place.unwrap().ptr,
this.machine.layouts.isize,
);
let exit_code = this.read_machine_isize(&ret_place.into())?;
let exit_code = this.read_target_isize(&ret_place.into())?;
// Need to call this ourselves since we are not going to return to the scheduler
// loop, and we want the main thread TLS to not show up as memory leaks.
this.terminate_active_thread()?;
Expand Down Expand Up @@ -287,7 +287,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
// First argument is constructed later, because it's skipped if the entry function uses #[start].

// Second argument (argc): length of `config.args`.
let argc = Scalar::from_machine_usize(u64::try_from(config.args.len()).unwrap(), &ecx);
let argc = Scalar::from_target_usize(u64::try_from(config.args.len()).unwrap(), &ecx);
// Third argument (`argv`): created from `config.args`.
let argv = {
// Put each argument in memory, collect pointers.
Expand Down
4 changes: 2 additions & 2 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let this = self.eval_context_mut();
let seconds_place = this.mplace_field(tp, 0)?;
let seconds_scalar = this.read_scalar(&seconds_place.into())?;
let seconds = seconds_scalar.to_machine_isize(this)?;
let seconds = seconds_scalar.to_target_isize(this)?;
let nanoseconds_place = this.mplace_field(tp, 1)?;
let nanoseconds_scalar = this.read_scalar(&nanoseconds_place.into())?;
let nanoseconds = nanoseconds_scalar.to_machine_isize(this)?;
let nanoseconds = nanoseconds_scalar.to_target_isize(this)?;

Ok(try {
// tv_sec must be non-negative.
Expand Down
2 changes: 1 addition & 1 deletion src/intptrcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl<'mir, 'tcx> GlobalStateInner {
.checked_add(max(size.bytes(), 1))
.ok_or_else(|| err_exhaust!(AddressSpaceFull))?;
// Even if `Size` didn't overflow, we might still have filled up the address space.
if global_state.next_base_addr > ecx.machine_usize_max() {
if global_state.next_base_addr > ecx.target_usize_max() {
throw_exhaust!(AddressSpaceFull);
}
// Given that `next_base_addr` increases in each allocation, pushing the
Expand Down
6 changes: 3 additions & 3 deletions src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriInterpCx<'mir, 'tcx> {
Offset => {
assert!(left.layout.ty.is_unsafe_ptr());
let ptr = left.to_scalar().to_pointer(self)?;
let offset = right.to_scalar().to_machine_isize(self)?;
let offset = right.to_scalar().to_target_isize(self)?;

let pointee_ty =
left.layout.ty.builtin_deref(true).expect("Offset called on non-ptr type").ty;
Expand All @@ -73,14 +73,14 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriInterpCx<'mir, 'tcx> {
// We do the actual operation with usize-typed scalars.
let left = ImmTy::from_uint(ptr.addr().bytes(), self.machine.layouts.usize);
let right = ImmTy::from_uint(
right.to_scalar().to_machine_usize(self)?,
right.to_scalar().to_target_usize(self)?,
self.machine.layouts.usize,
);
let (result, overflowing, _ty) =
self.overflowing_binary_op(bin_op, &left, &right)?;
// Construct a new pointer with the provenance of `ptr` (the LHS).
let result_ptr =
Pointer::new(ptr.provenance, Size::from_bytes(result.to_machine_usize(self)?));
Pointer::new(ptr.provenance, Size::from_bytes(result.to_target_usize(self)?));
(Scalar::from_maybe_pointer(result_ptr, self), overflowing, left.layout.ty)
}

Expand Down
6 changes: 3 additions & 3 deletions src/shims/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {

let frame_count = this.active_thread_stack().len();

this.write_scalar(Scalar::from_machine_usize(frame_count.try_into().unwrap(), this), dest)
this.write_scalar(Scalar::from_target_usize(frame_count.try_into().unwrap(), this), dest)
}

fn handle_miri_get_backtrace(
Expand Down Expand Up @@ -205,11 +205,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
}
1 => {
this.write_scalar(
Scalar::from_machine_usize(name.len().try_into().unwrap(), this),
Scalar::from_target_usize(name.len().try_into().unwrap(), this),
&this.mplace_field(&dest, 0)?.into(),
)?;
this.write_scalar(
Scalar::from_machine_usize(filename.len().try_into().unwrap(), this),
Scalar::from_target_usize(filename.len().try_into().unwrap(), this),
&this.mplace_field(&dest, 1)?.into(),
)?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/shims/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
this.assert_target_os_is_unix("getcwd");

let buf = this.read_pointer(buf_op)?;
let size = this.read_machine_usize(size_op)?;
let size = this.read_target_usize(size_op)?;

if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`getcwd`", reject_with)?;
Expand Down
4 changes: 2 additions & 2 deletions src/shims/ffi_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
ty::Int(IntTy::Isize) => {
// This will fail if host != target, but then the entire FFI thing probably won't work well
// in that situation.
return Ok(CArg::ISize(k.to_machine_isize(cx)?.try_into().unwrap()));
return Ok(CArg::ISize(k.to_target_isize(cx)?.try_into().unwrap()));
}
// the uints
ty::Uint(UintTy::U8) => {
Expand All @@ -54,7 +54,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
ty::Uint(UintTy::Usize) => {
// This will fail if host != target, but then the entire FFI thing probably won't work well
// in that situation.
return Ok(CArg::USize(k.to_machine_usize(cx)?.try_into().unwrap()));
return Ok(CArg::USize(k.to_target_usize(cx)?.try_into().unwrap()));
}
_ => {}
}
Expand Down
38 changes: 19 additions & 19 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let [ptr, out, out_size] = this.check_shim(abi, Abi::Rust, link_name, args)?;
let ptr = this.read_pointer(ptr)?;
let out = this.read_pointer(out)?;
let out_size = this.read_scalar(out_size)?.to_machine_usize(this)?;
let out_size = this.read_scalar(out_size)?.to_target_usize(this)?;

// The host affects program behavior here, so this requires isolation to be disabled.
this.check_no_isolation("`miri_host_to_target_path`")?;
Expand Down Expand Up @@ -490,7 +490,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let [bytes] = this.check_shim(abi, Abi::Rust, link_name, args)?;
let (ptr, len) = this.read_immediate(bytes)?.to_scalar_pair();
let ptr = ptr.to_pointer(this)?;
let len = len.to_machine_usize(this)?;
let len = len.to_target_usize(this)?;
let msg = this.read_bytes_ptr_strip_provenance(ptr, Size::from_bytes(len))?;

// Note: we're ignoring errors writing to host stdout/stderr.
Expand All @@ -504,15 +504,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
// Standard C allocation
"malloc" => {
let [size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let size = this.read_machine_usize(size)?;
let size = this.read_target_usize(size)?;
let res = this.malloc(size, /*zero_init:*/ false, MiriMemoryKind::C)?;
this.write_pointer(res, dest)?;
}
"calloc" => {
let [items, len] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let items = this.read_machine_usize(items)?;
let len = this.read_machine_usize(len)?;
let items = this.read_target_usize(items)?;
let len = this.read_target_usize(len)?;
let size = items
.checked_mul(len)
.ok_or_else(|| err_ub_format!("overflow during calloc size computation"))?;
Expand All @@ -528,16 +528,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let [old_ptr, new_size] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let old_ptr = this.read_pointer(old_ptr)?;
let new_size = this.read_machine_usize(new_size)?;
let new_size = this.read_target_usize(new_size)?;
let res = this.realloc(old_ptr, new_size, MiriMemoryKind::C)?;
this.write_pointer(res, dest)?;
}

// Rust allocation
"__rust_alloc" | "miri_alloc" => {
let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
let size = this.read_machine_usize(size)?;
let align = this.read_machine_usize(align)?;
let size = this.read_target_usize(size)?;
let align = this.read_target_usize(align)?;

let default = |this: &mut MiriInterpCx<'mir, 'tcx>| {
Self::check_alloc_request(size, align)?;
Expand Down Expand Up @@ -569,8 +569,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
}
"__rust_alloc_zeroed" => {
let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
let size = this.read_machine_usize(size)?;
let align = this.read_machine_usize(align)?;
let size = this.read_target_usize(size)?;
let align = this.read_target_usize(align)?;

return this.emulate_allocator(Symbol::intern("__rg_alloc_zeroed"), |this| {
Self::check_alloc_request(size, align)?;
Expand All @@ -593,8 +593,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
"__rust_dealloc" | "miri_dealloc" => {
let [ptr, old_size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
let ptr = this.read_pointer(ptr)?;
let old_size = this.read_machine_usize(old_size)?;
let align = this.read_machine_usize(align)?;
let old_size = this.read_target_usize(old_size)?;
let align = this.read_target_usize(align)?;

let default = |this: &mut MiriInterpCx<'mir, 'tcx>| {
let memory_kind = match link_name.as_str() {
Expand Down Expand Up @@ -625,9 +625,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let [ptr, old_size, align, new_size] =
this.check_shim(abi, Abi::Rust, link_name, args)?;
let ptr = this.read_pointer(ptr)?;
let old_size = this.read_machine_usize(old_size)?;
let align = this.read_machine_usize(align)?;
let new_size = this.read_machine_usize(new_size)?;
let old_size = this.read_target_usize(old_size)?;
let align = this.read_target_usize(align)?;
let new_size = this.read_target_usize(new_size)?;
// No need to check old_size; we anyway check that they match the allocation.

return this.emulate_allocator(Symbol::intern("__rg_realloc"), |this| {
Expand All @@ -651,7 +651,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let left = this.read_pointer(left)?;
let right = this.read_pointer(right)?;
let n = Size::from_bytes(this.read_machine_usize(n)?);
let n = Size::from_bytes(this.read_target_usize(n)?);

let result = {
let left_bytes = this.read_bytes_ptr_strip_provenance(left, n)?;
Expand All @@ -672,7 +672,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let ptr = this.read_pointer(ptr)?;
let val = this.read_scalar(val)?.to_i32()?;
let num = this.read_machine_usize(num)?;
let num = this.read_target_usize(num)?;
// The docs say val is "interpreted as unsigned char".
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let val = val as u8;
Expand All @@ -696,7 +696,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let ptr = this.read_pointer(ptr)?;
let val = this.read_scalar(val)?.to_i32()?;
let num = this.read_machine_usize(num)?;
let num = this.read_target_usize(num)?;
// The docs say val is "interpreted as unsigned char".
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let val = val as u8;
Expand All @@ -717,7 +717,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let ptr = this.read_pointer(ptr)?;
let n = this.read_c_str(ptr)?.len();
this.write_scalar(
Scalar::from_machine_usize(u64::try_from(n).unwrap(), this),
Scalar::from_target_usize(u64::try_from(n).unwrap(), this),
dest,
)?;
}
Expand Down
6 changes: 3 additions & 3 deletions src/shims/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let ty_layout = this.layout_of(ty)?;
let val_byte = this.read_scalar(val_byte)?.to_u8()?;
let ptr = this.read_pointer(ptr)?;
let count = this.read_machine_usize(count)?;
// `checked_mul` enforces a too small bound (the correct one would probably be machine_isize_max),
let count = this.read_target_usize(count)?;
// `checked_mul` enforces a too small bound (the correct one would probably be target_isize_max),
// but no actual allocation can be big enough for the difference to be noticeable.
let byte_count = ty_layout.size.checked_mul(count, this).ok_or_else(|| {
err_ub_format!("overflow computing total size of `{intrinsic_name}`")
Expand All @@ -124,7 +124,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let [ptr, mask] = check_arg_count(args)?;

let ptr = this.read_pointer(ptr)?;
let mask = this.read_machine_usize(mask)?;
let mask = this.read_target_usize(mask)?;

let masked_addr = Size::from_bytes(ptr.addr().bytes() & mask);

Expand Down
2 changes: 1 addition & 1 deletion src/shims/intrinsics/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
}
Op::WrappingOffset => {
let ptr = left.to_scalar().to_pointer(this)?;
let offset_count = right.to_scalar().to_machine_isize(this)?;
let offset_count = right.to_scalar().to_target_isize(this)?;
let pointee_ty = left.layout.ty.builtin_deref(true).unwrap().ty;

let pointee_size = i64::try_from(this.layout_of(pointee_ty)?.size.bytes()).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/shims/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
return Ok(false);
}

let req_align = this.read_machine_usize(align_op)?;
let req_align = this.read_target_usize(align_op)?;

// Stop if the alignment is not a power of two.
if !req_align.is_power_of_two() {
Expand All @@ -106,7 +106,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
}

// Return error result (usize::MAX), and jump to caller.
this.write_scalar(Scalar::from_machine_usize(this.machine_usize_max(), this), dest)?;
this.write_scalar(Scalar::from_target_usize(this.target_usize_max(), this), dest)?;
this.go_to_block(ret);
Ok(true)
}
Expand Down
4 changes: 2 additions & 2 deletions src/shims/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<'tcx> TlsData<'tcx> {
) -> InterpResult<'tcx> {
match self.keys.get_mut(&key) {
Some(TlsEntry { data, .. }) => {
if new_data.to_machine_usize(cx)? != 0 {
if new_data.to_target_usize(cx)? != 0 {
trace!("TLS key {} for thread {:?} stored: {:?}", key, thread_id, new_data);
data.insert(thread_id, new_data);
} else {
Expand Down Expand Up @@ -356,7 +356,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
state.last_key = Some(key);
trace!("Running TLS dtor {:?} on {:?} at {:?}", instance, ptr, active_thread);
assert!(
!ptr.to_machine_usize(this).unwrap() != 0,
!ptr.to_target_usize(this).unwrap() != 0,
"data can't be NULL when dtor is called!"
);

Expand Down
Loading

0 comments on commit 453b286

Please sign in to comment.