Skip to content

Commit 6b376dc

Browse files
committed
get rid of to_bytes hack
1 parent 383d215 commit 6b376dc

File tree

4 files changed

+14
-39
lines changed

4 files changed

+14
-39
lines changed

src/fn_call.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
277277

278278
"memrchr" => {
279279
let ptr = this.read_scalar(args[0])?.not_undef()?;
280-
let val = this.read_scalar(args[1])?.to_bytes()? as u8;
280+
let val = this.read_scalar(args[1])?.to_i32()? as u8;
281281
let num = this.read_scalar(args[2])?.to_usize(this)?;
282282
if let Some(idx) = this.memory().read_bytes(ptr, Size::from_bytes(num))?
283283
.iter().rev().position(|&c| c == val)
@@ -291,7 +291,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
291291

292292
"memchr" => {
293293
let ptr = this.read_scalar(args[0])?.not_undef()?;
294-
let val = this.read_scalar(args[1])?.to_bytes()? as u8;
294+
let val = this.read_scalar(args[1])?.to_i32()? as u8;
295295
let num = this.read_scalar(args[2])?.to_usize(this)?;
296296
if let Some(idx) = this.memory().read_bytes(ptr, Size::from_bytes(num))?.iter().position(
297297
|&c| c == val,
@@ -379,9 +379,9 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
379379
}
380380

381381
"write" => {
382-
let fd = this.read_scalar(args[0])?.to_bytes()?;
382+
let fd = this.read_scalar(args[0])?.to_i32()?;
383383
let buf = this.read_scalar(args[1])?.not_undef()?;
384-
let n = this.read_scalar(args[2])?.to_bytes()? as u64;
384+
let n = this.read_scalar(args[2])?.to_usize(&*this.tcx)?;
385385
trace!("Called write({:?}, {:?}, {:?})", fd, buf, n);
386386
let result = if fd == 1 || fd == 2 {
387387
// stdout/stderr
@@ -489,18 +489,18 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
489489
this.write_null(dest)?;
490490
}
491491
"pthread_key_delete" => {
492-
let key = this.read_scalar(args[0])?.to_bytes()?;
492+
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
493493
this.machine.tls.delete_tls_key(key)?;
494494
// Return success (0)
495495
this.write_null(dest)?;
496496
}
497497
"pthread_getspecific" => {
498-
let key = this.read_scalar(args[0])?.to_bytes()?;
498+
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
499499
let ptr = this.machine.tls.load_tls(key)?;
500500
this.write_scalar(ptr, dest)?;
501501
}
502502
"pthread_setspecific" => {
503-
let key = this.read_scalar(args[0])?.to_bytes()?;
503+
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
504504
let new_ptr = this.read_scalar(args[1])?.not_undef()?;
505505
this.machine.tls.store_tls(key, new_ptr)?;
506506

@@ -586,12 +586,12 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
586586
this.write_scalar(Scalar::from_uint(key, dest.layout.size), dest)?;
587587
}
588588
"TlsGetValue" => {
589-
let key = this.read_scalar(args[0])?.to_bytes()?;
589+
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
590590
let ptr = this.machine.tls.load_tls(key)?;
591591
this.write_scalar(ptr, dest)?;
592592
}
593593
"TlsSetValue" => {
594-
let key = this.read_scalar(args[0])?.to_bytes()?;
594+
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
595595
let new_ptr = this.read_scalar(args[1])?.not_undef()?;
596596
this.machine.tls.store_tls(key, new_ptr)?;
597597

src/helpers.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,6 @@ use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
55

66
use crate::*;
77

8-
pub trait ScalarExt {
9-
/// HACK: this function just extracts all bits if `defined != 0`
10-
/// Mainly used for args of C-functions and we should totally correctly fetch the size
11-
/// of their arguments
12-
fn to_bytes(self) -> EvalResult<'static, u128>;
13-
}
14-
15-
impl<Tag> ScalarExt for Scalar<Tag> {
16-
fn to_bytes(self) -> EvalResult<'static, u128> {
17-
match self {
18-
Scalar::Bits { bits, size } => {
19-
assert_ne!(size, 0);
20-
Ok(bits)
21-
},
22-
Scalar::Ptr(_) => err!(ReadPointerAsBytes),
23-
}
24-
}
25-
}
26-
27-
impl<Tag> ScalarExt for ScalarMaybeUndef<Tag> {
28-
fn to_bytes(self) -> EvalResult<'static, u128> {
29-
self.not_undef()?.to_bytes()
30-
}
31-
}
32-
338
impl<'a, 'mir, 'tcx> EvalContextExt<'a, 'mir, 'tcx> for crate::MiriEvalContext<'a, 'mir, 'tcx> {}
349
pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a, 'mir, 'tcx> {
3510
/// Get an instance for a path.

src/intrinsic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc::mir::interpret::{EvalResult, PointerArithmetic};
66

77
use crate::{
88
PlaceTy, OpTy, Immediate, Scalar, ScalarMaybeUndef, Borrow,
9-
ScalarExt, OperatorEvalContextExt
9+
OperatorEvalContextExt
1010
};
1111

1212
impl<'a, 'mir, 'tcx> EvalContextExt<'a, 'mir, 'tcx> for crate::MiriEvalContext<'a, 'mir, 'tcx> {}
@@ -227,7 +227,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
227227
let a = this.read_immediate(args[0])?;
228228
let b = this.read_immediate(args[1])?;
229229
// check x % y != 0
230-
if this.binary_op_imm(mir::BinOp::Rem, a, b)?.0.to_bytes()? != 0 {
230+
if this.binary_op_imm(mir::BinOp::Rem, a, b)?.0.to_bits(dest.layout.size)? != 0 {
231231
return err!(ValidationFailure(format!("exact_div: {:?} cannot be divided by {:?}", a, b)));
232232
}
233233
this.binop_ignore_overflow(mir::BinOp::Div, a, b, dest)?;
@@ -375,7 +375,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
375375
"unchecked_div" => {
376376
let l = this.read_immediate(args[0])?;
377377
let r = this.read_immediate(args[1])?;
378-
let rval = r.to_scalar()?.to_bytes()?;
378+
let rval = r.to_scalar()?.to_bits(args[1].layout.size)?;
379379
if rval == 0 {
380380
return err!(Intrinsic(format!("Division by 0 in unchecked_div")));
381381
}
@@ -390,7 +390,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
390390
"unchecked_rem" => {
391391
let l = this.read_immediate(args[0])?;
392392
let r = this.read_immediate(args[1])?;
393-
let rval = r.to_scalar()?.to_bytes()?;
393+
let rval = r.to_scalar()?.to_bits(args[1].layout.size)?;
394394
if rval == 0 {
395395
return err!(Intrinsic(format!("Division by 0 in unchecked_rem")));
396396
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub use crate::intrinsic::EvalContextExt as IntrinsicEvalContextExt;
4343
pub use crate::tls::{EvalContextExt as TlsEvalContextExt, TlsData};
4444
use crate::range_map::RangeMap;
4545
#[allow(unused_imports)] // FIXME rustc bug https://github.com/rust-lang/rust/issues/53682
46-
pub use crate::helpers::{ScalarExt, EvalContextExt as HelpersEvalContextExt};
46+
pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};
4747
use crate::mono_hash_map::MonoHashMap;
4848
pub use crate::stacked_borrows::{EvalContextExt as StackedBorEvalContextExt};
4949

0 commit comments

Comments
 (0)