Skip to content

Commit cec51f8

Browse files
committed
Use correct bit size when reading usize values
1 parent 8284b4e commit cec51f8

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

src/fn_call.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
194194

195195
match &link_name[..] {
196196
"malloc" => {
197-
let size = self.value_to_scalar(args[0])?.to_u64()?;
197+
let size = self.value_to_scalar(args[0])?.to_usize(self)?;
198198
if size == 0 {
199199
self.write_null(dest, dest_ty)?;
200200
} else {
@@ -221,7 +221,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
221221
//
222222
// libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)
223223
// is called if a `HashMap` is created the regular way.
224-
match self.value_to_scalar(args[0])?.to_u64()? {
224+
match self.value_to_scalar(args[0])?.to_usize(self)? {
225225
318 | 511 => {
226226
return err!(Unimplemented(
227227
"miri does not support random number generators".to_owned(),
@@ -293,7 +293,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
293293
"memcmp" => {
294294
let left = self.into_ptr(args[0].value)?;
295295
let right = self.into_ptr(args[1].value)?;
296-
let n = Size::from_bytes(self.value_to_scalar(args[2])?.to_u64()?);
296+
let n = Size::from_bytes(self.value_to_scalar(args[2])?.to_usize(self)?);
297297

298298
let result = {
299299
let left_bytes = self.memory.read_bytes(left, n)?;
@@ -317,7 +317,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
317317
"memrchr" => {
318318
let ptr = self.into_ptr(args[0].value)?;
319319
let val = self.value_to_scalar(args[1])?.to_bytes()? as u8;
320-
let num = self.value_to_scalar(args[2])?.to_u64()?;
320+
let num = self.value_to_scalar(args[2])?.to_usize(self)?;
321321
if let Some(idx) = self.memory.read_bytes(ptr, Size::from_bytes(num))?.iter().rev().position(
322322
|&c| c == val,
323323
)
@@ -332,7 +332,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
332332
"memchr" => {
333333
let ptr = self.into_ptr(args[0].value)?;
334334
let val = self.value_to_scalar(args[1])?.to_bytes()? as u8;
335-
let num = self.value_to_scalar(args[2])?.to_u64()?;
335+
let num = self.value_to_scalar(args[2])?.to_usize(self)?;
336336
if let Some(idx) = self.memory.read_bytes(ptr, Size::from_bytes(num))?.iter().position(
337337
|&c| c == val,
338338
)
@@ -457,7 +457,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
457457
}
458458

459459
"sysconf" => {
460-
let name = self.value_to_scalar(args[0])?.to_u64()?;
460+
let name = self.value_to_scalar(args[0])?.to_usize(self)?;
461461

462462
trace!("sysconf() called with name {}", name);
463463
// cache the sysconf integers via miri's global cache
@@ -646,8 +646,8 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
646646
match &path[..] {
647647
// Allocators are magic. They have no MIR, even when the rest of libstd does.
648648
"alloc::alloc::::__rust_alloc" => {
649-
let size = self.value_to_scalar(args[0])?.to_u64()?;
650-
let align = self.value_to_scalar(args[1])?.to_u64()?;
649+
let size = self.value_to_scalar(args[0])?.to_usize(self)?;
650+
let align = self.value_to_scalar(args[1])?.to_usize(self)?;
651651
if size == 0 {
652652
return err!(HeapAllocZeroBytes);
653653
}
@@ -660,8 +660,8 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
660660
self.write_scalar(dest, Scalar::Ptr(ptr), dest_ty)?;
661661
}
662662
"alloc::alloc::::__rust_alloc_zeroed" => {
663-
let size = self.value_to_scalar(args[0])?.to_u64()?;
664-
let align = self.value_to_scalar(args[1])?.to_u64()?;
663+
let size = self.value_to_scalar(args[0])?.to_usize(self)?;
664+
let align = self.value_to_scalar(args[1])?.to_usize(self)?;
665665
if size == 0 {
666666
return err!(HeapAllocZeroBytes);
667667
}
@@ -676,8 +676,8 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
676676
}
677677
"alloc::alloc::::__rust_dealloc" => {
678678
let ptr = self.into_ptr(args[0].value)?.to_ptr()?;
679-
let old_size = self.value_to_scalar(args[1])?.to_u64()?;
680-
let align = self.value_to_scalar(args[2])?.to_u64()?;
679+
let old_size = self.value_to_scalar(args[1])?.to_usize(self)?;
680+
let align = self.value_to_scalar(args[2])?.to_usize(self)?;
681681
if old_size == 0 {
682682
return err!(HeapAllocZeroBytes);
683683
}
@@ -692,9 +692,9 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
692692
}
693693
"alloc::alloc::::__rust_realloc" => {
694694
let ptr = self.into_ptr(args[0].value)?.to_ptr()?;
695-
let old_size = self.value_to_scalar(args[1])?.to_u64()?;
696-
let align = self.value_to_scalar(args[2])?.to_u64()?;
697-
let new_size = self.value_to_scalar(args[3])?.to_u64()?;
695+
let old_size = self.value_to_scalar(args[1])?.to_usize(self)?;
696+
let align = self.value_to_scalar(args[2])?.to_usize(self)?;
697+
let new_size = self.value_to_scalar(args[3])?.to_usize(self)?;
698698
if old_size == 0 || new_size == 0 {
699699
return err!(HeapAllocZeroBytes);
700700
}

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub trait ScalarExt {
6262
fn from_isize(i: i64, ptr_size: Size) -> Self;
6363
fn from_f32(f: f32) -> Self;
6464
fn from_f64(f: f64) -> Self;
65-
fn to_u64(self) -> EvalResult<'static, u64>;
65+
fn to_usize<'a, 'mir, 'tcx>(self, ecx: &rustc_mir::interpret::EvalContext<'a, 'mir, 'tcx, Evaluator<'tcx>>) -> EvalResult<'static, u64>;
6666
fn is_null(self) -> EvalResult<'static, bool>;
6767
/// HACK: this function just extracts all bits if `defined != 0`
6868
/// Mainly used for args of C-functions and we should totally correctly fetch the size
@@ -103,8 +103,8 @@ impl ScalarExt for Scalar {
103103
Scalar::Bits { bits: f.to_bits() as u128, defined: 64 }
104104
}
105105

106-
fn to_u64(self) -> EvalResult<'static, u64> {
107-
let b = self.to_bits(Size::from_bits(64))?;
106+
fn to_usize<'a, 'mir, 'tcx>(self, ecx: &rustc_mir::interpret::EvalContext<'a, 'mir, 'tcx, Evaluator<'tcx>>) -> EvalResult<'static, u64> {
107+
let b = self.to_bits(ecx.memory.pointer_size())?;
108108
assert_eq!(b as u64 as u128, b);
109109
Ok(b as u64)
110110
}

src/validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
119119
Index(v) => {
120120
let value = self.frame().get_local(v)?;
121121
let ty = self.tcx.tcx.types.usize;
122-
let n = self.value_to_scalar(ValTy { value, ty })?.to_u64()?;
122+
let n = self.value_to_scalar(ValTy { value, ty })?.to_usize(self)?;
123123
Index(n)
124124
},
125125
ConstantIndex { offset, min_length, from_end } =>

0 commit comments

Comments
 (0)