add to_u64() helper method and use it where appropriate

This commit is contained in:
Ralf Jung 2025-05-19 09:00:05 +02:00
parent 0ba2a3a818
commit 446fa22412
12 changed files with 37 additions and 26 deletions

View file

@ -168,7 +168,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
AllocKind::Dead => unreachable!(),
};
// We don't have to expose this pointer yet, we do that in `prepare_for_native_call`.
return interp_ok(base_ptr.addr().try_into().unwrap());
return interp_ok(base_ptr.addr().to_u64());
}
// We are not in native lib mode, so we control the addresses ourselves.
if let Some((reuse_addr, clock)) = global_state.reuse.take_addr(

View file

@ -4,6 +4,7 @@ use rand::Rng;
use rustc_abi::{Align, Size};
use crate::concurrency::VClock;
use crate::helpers::ToUsize as _;
use crate::{MemoryKind, MiriConfig, ThreadId};
const MAX_POOL_SIZE: usize = 64;
@ -46,7 +47,7 @@ impl ReusePool {
}
fn subpool(&mut self, align: Align) -> &mut Vec<(u64, Size, ThreadId, VClock)> {
let pool_idx: usize = align.bytes().trailing_zeros().try_into().unwrap();
let pool_idx: usize = align.bytes().trailing_zeros().to_usize();
if self.pool.len() <= pool_idx {
self.pool.resize(pool_idx + 1, Vec::new());
}

View file

@ -5,6 +5,8 @@ use std::{alloc, slice};
use rustc_abi::{Align, Size};
use rustc_middle::mir::interpret::AllocBytes;
use crate::helpers::ToU64 as _;
/// Allocation bytes that explicitly handle the layout of the data they're storing.
/// This is necessary to interface with native code that accesses the program store in Miri.
#[derive(Debug)]
@ -21,7 +23,7 @@ pub struct MiriAllocBytes {
impl Clone for MiriAllocBytes {
fn clone(&self) -> Self {
let bytes: Cow<'_, [u8]> = Cow::Borrowed(self);
let align = Align::from_bytes(self.layout.align().try_into().unwrap()).unwrap();
let align = Align::from_bytes(self.layout.align().to_u64()).unwrap();
MiriAllocBytes::from_bytes(bytes, align)
}
}
@ -90,7 +92,7 @@ impl AllocBytes for MiriAllocBytes {
let align = align.bytes();
// SAFETY: `alloc_fn` will only be used with `size != 0`.
let alloc_fn = |layout| unsafe { alloc::alloc(layout) };
let alloc_bytes = MiriAllocBytes::alloc_with(size.try_into().unwrap(), align, alloc_fn)
let alloc_bytes = MiriAllocBytes::alloc_with(size.to_u64(), align, alloc_fn)
.unwrap_or_else(|()| {
panic!("Miri ran out of memory: cannot create allocation of {size} bytes")
});

View file

@ -1423,3 +1423,15 @@ impl ToUsize for u32 {
self.try_into().unwrap()
}
}
/// Similarly, a maximum address size of `u64` is assumed widely here, so let's have ergonomic
/// converion from `usize` to `u64`.
pub trait ToU64 {
fn to_u64(self) -> u64;
}
impl ToU64 for usize {
fn to_u64(self) -> u64 {
self.try_into().unwrap()
}
}

View file

@ -133,7 +133,7 @@ pub use crate::eval::{
AlignmentCheck, BacktraceStyle, IsolatedOp, MiriConfig, MiriEntryFnType, RejectOpWith,
ValidationMode, create_ecx, eval_entry,
};
pub use crate::helpers::{AccessKind, EvalContextExt as _, ToUsize as _};
pub use crate::helpers::{AccessKind, EvalContextExt as _, ToU64 as _, ToUsize as _};
pub use crate::intrinsics::EvalContextExt as _;
pub use crate::machine::{
AllocExtra, DynMachineCallback, FrameExtra, MachineCallback, MemoryKind, MiriInterpCx,

View file

@ -25,7 +25,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let frame_count = this.active_thread_stack().len();
this.write_scalar(Scalar::from_target_usize(frame_count.try_into().unwrap(), this), dest)
this.write_scalar(Scalar::from_target_usize(frame_count.to_u64(), this), dest)
}
fn handle_miri_get_backtrace(
@ -70,7 +70,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
}
1 =>
for (i, ptr) in ptrs.into_iter().enumerate() {
let offset = ptr_layout.size.checked_mul(i.try_into().unwrap(), this).unwrap();
let offset = ptr_layout.size.checked_mul(i.to_u64(), this).unwrap();
let op_place = buf_place.offset(offset, ptr_layout, this)?;
@ -158,11 +158,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
}
1 => {
this.write_scalar(
Scalar::from_target_usize(name.len().try_into().unwrap(), this),
Scalar::from_target_usize(name.len().to_u64(), this),
&this.project_field(dest, 0)?,
)?;
this.write_scalar(
Scalar::from_target_usize(filename.len().try_into().unwrap(), this),
Scalar::from_target_usize(filename.len().to_u64(), this),
&this.project_field(dest, 1)?,
)?;
}

View file

@ -7,7 +7,7 @@ use crate::helpers::check_min_vararg_count;
use crate::shims::unix::thread::{EvalContextExt as _, ThreadNameResult};
use crate::*;
const TASK_COMM_LEN: u32 = 16;
const TASK_COMM_LEN: u64 = 16;
pub fn prctl<'tcx>(
ecx: &mut MiriInterpCx<'tcx>,
@ -29,12 +29,8 @@ pub fn prctl<'tcx>(
let thread = ecx.pthread_self()?;
// The Linux kernel silently truncates long names.
// https://www.man7.org/linux/man-pages/man2/PR_SET_NAME.2const.html
let res = ecx.pthread_setname_np(
thread,
name,
TASK_COMM_LEN.to_usize(),
/* truncate */ true,
)?;
let res =
ecx.pthread_setname_np(thread, name, TASK_COMM_LEN, /* truncate */ true)?;
assert_eq!(res, ThreadNameResult::Ok);
Scalar::from_u32(0)
}
@ -42,7 +38,7 @@ pub fn prctl<'tcx>(
let [name] = check_min_vararg_count("prctl(PR_GET_NAME, ...)", varargs)?;
let name = ecx.read_scalar(name)?;
let thread = ecx.pthread_self()?;
let len = Scalar::from_target_usize(TASK_COMM_LEN.into(), ecx);
let len = Scalar::from_target_usize(TASK_COMM_LEN, ecx);
ecx.check_ptr_access(
name.to_pointer(ecx)?,
Size::from_bytes(TASK_COMM_LEN),

View file

@ -24,7 +24,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Threading
"pthread_setname_np" => {
let [thread, name] = this.check_shim(abi, Conv::C, link_name, args)?;
let max_len = usize::MAX; // FreeBSD does not seem to have a limit.
let max_len = u64::MAX; // FreeBSD does not seem to have a limit.
let res = match this.pthread_setname_np(
this.read_scalar(thread)?,
this.read_scalar(name)?,

View file

@ -14,7 +14,7 @@ use crate::*;
// The documentation of glibc complains that the kernel never exposes
// TASK_COMM_LEN through the headers, so it's assumed to always be 16 bytes
// long including a null terminator.
const TASK_COMM_LEN: u32 = 16;
const TASK_COMM_LEN: u64 = 16;
pub fn is_dyn_sym(name: &str) -> bool {
matches!(name, "statx")
@ -80,7 +80,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let res = match this.pthread_setname_np(
this.read_scalar(thread)?,
this.read_scalar(name)?,
TASK_COMM_LEN.to_usize(),
TASK_COMM_LEN,
/* truncate */ false,
)? {
ThreadNameResult::Ok => Scalar::from_u32(0),
@ -96,7 +96,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// In case of glibc, the length of the output buffer must
// be not shorter than TASK_COMM_LEN.
let len = this.read_scalar(len)?;
let res = if len.to_target_usize(this)? >= TASK_COMM_LEN.into() {
let res = if len.to_target_usize(this)? >= TASK_COMM_LEN {
match this.pthread_getname_np(
this.read_scalar(thread)?,
this.read_scalar(name)?,

View file

@ -186,7 +186,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let res = match this.pthread_setname_np(
thread,
this.read_scalar(name)?,
this.eval_libc("MAXTHREADNAMESIZE").to_target_usize(this)?.try_into().unwrap(),
this.eval_libc("MAXTHREADNAMESIZE").to_target_usize(this)?,
/* truncate */ false,
)? {
ThreadNameResult::Ok => Scalar::from_u32(0),

View file

@ -86,7 +86,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
&mut self,
thread: Scalar,
name: Scalar,
name_max_len: usize,
name_max_len: u64,
truncate: bool,
) -> InterpResult<'tcx, ThreadNameResult> {
let this = self.eval_context_mut();
@ -99,9 +99,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let mut name = this.read_c_str(name)?.to_owned();
// Comparing with `>=` to account for null terminator.
if name.len() >= name_max_len {
if name.len().to_u64() >= name_max_len {
if truncate {
name.truncate(name_max_len.saturating_sub(1));
name.truncate(name_max_len.saturating_sub(1).try_into().unwrap());
} else {
return interp_ok(ThreadNameResult::NameTooLong);
}

View file

@ -43,7 +43,7 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// We reverse the order because x86 is little endian but the copied implementation uses
// big endian.
for (i, part) in val.into_iter().rev().enumerate() {
let projected = &ecx.project_index(dest, i.try_into().unwrap())?;
let projected = &ecx.project_index(dest, i.to_u64())?;
ecx.write_scalar(Scalar::from_u32(part), projected)?;
}
interp_ok(())