Skip to content

Commit 446fa22

Browse files
committed
add to_u64() helper method and use it where appropriate
1 parent 0ba2a3a commit 446fa22

File tree

12 files changed

+37
-26
lines changed

12 files changed

+37
-26
lines changed

src/tools/miri/src/alloc_addresses/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
168168
AllocKind::Dead => unreachable!(),
169169
};
170170
// We don't have to expose this pointer yet, we do that in `prepare_for_native_call`.
171-
return interp_ok(base_ptr.addr().try_into().unwrap());
171+
return interp_ok(base_ptr.addr().to_u64());
172172
}
173173
// We are not in native lib mode, so we control the addresses ourselves.
174174
if let Some((reuse_addr, clock)) = global_state.reuse.take_addr(

src/tools/miri/src/alloc_addresses/reuse_pool.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rand::Rng;
44
use rustc_abi::{Align, Size};
55

66
use crate::concurrency::VClock;
7+
use crate::helpers::ToUsize as _;
78
use crate::{MemoryKind, MiriConfig, ThreadId};
89

910
const MAX_POOL_SIZE: usize = 64;
@@ -46,7 +47,7 @@ impl ReusePool {
4647
}
4748

4849
fn subpool(&mut self, align: Align) -> &mut Vec<(u64, Size, ThreadId, VClock)> {
49-
let pool_idx: usize = align.bytes().trailing_zeros().try_into().unwrap();
50+
let pool_idx: usize = align.bytes().trailing_zeros().to_usize();
5051
if self.pool.len() <= pool_idx {
5152
self.pool.resize(pool_idx + 1, Vec::new());
5253
}

src/tools/miri/src/alloc_bytes.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::{alloc, slice};
55
use rustc_abi::{Align, Size};
66
use rustc_middle::mir::interpret::AllocBytes;
77

8+
use crate::helpers::ToU64 as _;
9+
810
/// Allocation bytes that explicitly handle the layout of the data they're storing.
911
/// This is necessary to interface with native code that accesses the program store in Miri.
1012
#[derive(Debug)]
@@ -21,7 +23,7 @@ pub struct MiriAllocBytes {
2123
impl Clone for MiriAllocBytes {
2224
fn clone(&self) -> Self {
2325
let bytes: Cow<'_, [u8]> = Cow::Borrowed(self);
24-
let align = Align::from_bytes(self.layout.align().try_into().unwrap()).unwrap();
26+
let align = Align::from_bytes(self.layout.align().to_u64()).unwrap();
2527
MiriAllocBytes::from_bytes(bytes, align)
2628
}
2729
}
@@ -90,7 +92,7 @@ impl AllocBytes for MiriAllocBytes {
9092
let align = align.bytes();
9193
// SAFETY: `alloc_fn` will only be used with `size != 0`.
9294
let alloc_fn = |layout| unsafe { alloc::alloc(layout) };
93-
let alloc_bytes = MiriAllocBytes::alloc_with(size.try_into().unwrap(), align, alloc_fn)
95+
let alloc_bytes = MiriAllocBytes::alloc_with(size.to_u64(), align, alloc_fn)
9496
.unwrap_or_else(|()| {
9597
panic!("Miri ran out of memory: cannot create allocation of {size} bytes")
9698
});

src/tools/miri/src/helpers.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,3 +1423,15 @@ impl ToUsize for u32 {
14231423
self.try_into().unwrap()
14241424
}
14251425
}
1426+
1427+
/// Similarly, a maximum address size of `u64` is assumed widely here, so let's have ergonomic
1428+
/// converion from `usize` to `u64`.
1429+
pub trait ToU64 {
1430+
fn to_u64(self) -> u64;
1431+
}
1432+
1433+
impl ToU64 for usize {
1434+
fn to_u64(self) -> u64 {
1435+
self.try_into().unwrap()
1436+
}
1437+
}

src/tools/miri/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub use crate::eval::{
133133
AlignmentCheck, BacktraceStyle, IsolatedOp, MiriConfig, MiriEntryFnType, RejectOpWith,
134134
ValidationMode, create_ecx, eval_entry,
135135
};
136-
pub use crate::helpers::{AccessKind, EvalContextExt as _, ToUsize as _};
136+
pub use crate::helpers::{AccessKind, EvalContextExt as _, ToU64 as _, ToUsize as _};
137137
pub use crate::intrinsics::EvalContextExt as _;
138138
pub use crate::machine::{
139139
AllocExtra, DynMachineCallback, FrameExtra, MachineCallback, MemoryKind, MiriInterpCx,

src/tools/miri/src/shims/backtrace.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
2525

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

28-
this.write_scalar(Scalar::from_target_usize(frame_count.try_into().unwrap(), this), dest)
28+
this.write_scalar(Scalar::from_target_usize(frame_count.to_u64(), this), dest)
2929
}
3030

3131
fn handle_miri_get_backtrace(
@@ -70,7 +70,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
7070
}
7171
1 =>
7272
for (i, ptr) in ptrs.into_iter().enumerate() {
73-
let offset = ptr_layout.size.checked_mul(i.try_into().unwrap(), this).unwrap();
73+
let offset = ptr_layout.size.checked_mul(i.to_u64(), this).unwrap();
7474

7575
let op_place = buf_place.offset(offset, ptr_layout, this)?;
7676

@@ -158,11 +158,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
158158
}
159159
1 => {
160160
this.write_scalar(
161-
Scalar::from_target_usize(name.len().try_into().unwrap(), this),
161+
Scalar::from_target_usize(name.len().to_u64(), this),
162162
&this.project_field(dest, 0)?,
163163
)?;
164164
this.write_scalar(
165-
Scalar::from_target_usize(filename.len().try_into().unwrap(), this),
165+
Scalar::from_target_usize(filename.len().to_u64(), this),
166166
&this.project_field(dest, 1)?,
167167
)?;
168168
}

src/tools/miri/src/shims/unix/android/thread.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::helpers::check_min_vararg_count;
77
use crate::shims::unix::thread::{EvalContextExt as _, ThreadNameResult};
88
use crate::*;
99

10-
const TASK_COMM_LEN: u32 = 16;
10+
const TASK_COMM_LEN: u64 = 16;
1111

1212
pub fn prctl<'tcx>(
1313
ecx: &mut MiriInterpCx<'tcx>,
@@ -29,20 +29,16 @@ pub fn prctl<'tcx>(
2929
let thread = ecx.pthread_self()?;
3030
// The Linux kernel silently truncates long names.
3131
// https://www.man7.org/linux/man-pages/man2/PR_SET_NAME.2const.html
32-
let res = ecx.pthread_setname_np(
33-
thread,
34-
name,
35-
TASK_COMM_LEN.to_usize(),
36-
/* truncate */ true,
37-
)?;
32+
let res =
33+
ecx.pthread_setname_np(thread, name, TASK_COMM_LEN, /* truncate */ true)?;
3834
assert_eq!(res, ThreadNameResult::Ok);
3935
Scalar::from_u32(0)
4036
}
4137
op if op == pr_get_name => {
4238
let [name] = check_min_vararg_count("prctl(PR_GET_NAME, ...)", varargs)?;
4339
let name = ecx.read_scalar(name)?;
4440
let thread = ecx.pthread_self()?;
45-
let len = Scalar::from_target_usize(TASK_COMM_LEN.into(), ecx);
41+
let len = Scalar::from_target_usize(TASK_COMM_LEN, ecx);
4642
ecx.check_ptr_access(
4743
name.to_pointer(ecx)?,
4844
Size::from_bytes(TASK_COMM_LEN),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
2424
// Threading
2525
"pthread_setname_np" => {
2626
let [thread, name] = this.check_shim(abi, Conv::C, link_name, args)?;
27-
let max_len = usize::MAX; // FreeBSD does not seem to have a limit.
27+
let max_len = u64::MAX; // FreeBSD does not seem to have a limit.
2828
let res = match this.pthread_setname_np(
2929
this.read_scalar(thread)?,
3030
this.read_scalar(name)?,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::*;
1414
// The documentation of glibc complains that the kernel never exposes
1515
// TASK_COMM_LEN through the headers, so it's assumed to always be 16 bytes
1616
// long including a null terminator.
17-
const TASK_COMM_LEN: u32 = 16;
17+
const TASK_COMM_LEN: u64 = 16;
1818

1919
pub fn is_dyn_sym(name: &str) -> bool {
2020
matches!(name, "statx")
@@ -80,7 +80,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
8080
let res = match this.pthread_setname_np(
8181
this.read_scalar(thread)?,
8282
this.read_scalar(name)?,
83-
TASK_COMM_LEN.to_usize(),
83+
TASK_COMM_LEN,
8484
/* truncate */ false,
8585
)? {
8686
ThreadNameResult::Ok => Scalar::from_u32(0),
@@ -96,7 +96,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
9696
// In case of glibc, the length of the output buffer must
9797
// be not shorter than TASK_COMM_LEN.
9898
let len = this.read_scalar(len)?;
99-
let res = if len.to_target_usize(this)? >= TASK_COMM_LEN.into() {
99+
let res = if len.to_target_usize(this)? >= TASK_COMM_LEN {
100100
match this.pthread_getname_np(
101101
this.read_scalar(thread)?,
102102
this.read_scalar(name)?,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
186186
let res = match this.pthread_setname_np(
187187
thread,
188188
this.read_scalar(name)?,
189-
this.eval_libc("MAXTHREADNAMESIZE").to_target_usize(this)?.try_into().unwrap(),
189+
this.eval_libc("MAXTHREADNAMESIZE").to_target_usize(this)?,
190190
/* truncate */ false,
191191
)? {
192192
ThreadNameResult::Ok => Scalar::from_u32(0),

src/tools/miri/src/shims/unix/thread.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
8686
&mut self,
8787
thread: Scalar,
8888
name: Scalar,
89-
name_max_len: usize,
89+
name_max_len: u64,
9090
truncate: bool,
9191
) -> InterpResult<'tcx, ThreadNameResult> {
9292
let this = self.eval_context_mut();
@@ -99,9 +99,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
9999
let mut name = this.read_c_str(name)?.to_owned();
100100

101101
// Comparing with `>=` to account for null terminator.
102-
if name.len() >= name_max_len {
102+
if name.len().to_u64() >= name_max_len {
103103
if truncate {
104-
name.truncate(name_max_len.saturating_sub(1));
104+
name.truncate(name_max_len.saturating_sub(1).try_into().unwrap());
105105
} else {
106106
return interp_ok(ThreadNameResult::NameTooLong);
107107
}

src/tools/miri/src/shims/x86/sha.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
4343
// We reverse the order because x86 is little endian but the copied implementation uses
4444
// big endian.
4545
for (i, part) in val.into_iter().rev().enumerate() {
46-
let projected = &ecx.project_index(dest, i.try_into().unwrap())?;
46+
let projected = &ecx.project_index(dest, i.to_u64())?;
4747
ecx.write_scalar(Scalar::from_u32(part), projected)?;
4848
}
4949
interp_ok(())

0 commit comments

Comments
 (0)