From 066a284557ff6e6a2aa19084f599f167a724af7b Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 30 May 2018 17:43:54 +0200 Subject: [PATCH] `to_u64` and `to_bytes` are horribly easy to use wrongly. --- src/fn_call.rs | 8 ++++---- src/lib.rs | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/fn_call.rs b/src/fn_call.rs index fdaa819c08f7..72efcd6ede07 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -316,7 +316,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, ' "memrchr" => { let ptr = self.into_ptr(args[0].value)?; - let val = self.value_to_scalar(args[1])?.to_u64()? as u8; + let val = self.value_to_scalar(args[1])?.to_bytes()? as u8; let num = self.value_to_scalar(args[2])?.to_u64()?; if let Some(idx) = self.memory.read_bytes(ptr, Size::from_bytes(num))?.iter().rev().position( |&c| c == val, @@ -331,7 +331,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, ' "memchr" => { let ptr = self.into_ptr(args[0].value)?; - let val = self.value_to_scalar(args[1])?.to_u64()? as u8; + let val = self.value_to_scalar(args[1])?.to_bytes()? as u8; let num = self.value_to_scalar(args[2])?.to_u64()?; if let Some(idx) = self.memory.read_bytes(ptr, Size::from_bytes(num))?.iter().position( |&c| c == val, @@ -414,9 +414,9 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, ' } "write" => { - let fd = self.value_to_scalar(args[0])?.to_u64()?; + let fd = self.value_to_scalar(args[0])?.to_bytes()?; let buf = self.into_ptr(args[1].value)?; - let n = self.value_to_scalar(args[2])?.to_u64()?; + let n = self.value_to_scalar(args[2])?.to_bytes()? as u64; trace!("Called write({:?}, {:?}, {:?})", fd, buf, n); let result = if fd == 1 || fd == 2 { // stdout/stderr diff --git a/src/lib.rs b/src/lib.rs index 6d7c0b05f80f..550965573cb4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,6 +64,9 @@ pub trait ScalarExt { fn from_f64(f: f64) -> Self; fn to_u64(self) -> EvalResult<'static, u64>; fn is_null(self) -> EvalResult<'static, bool>; + /// HACK: this function just extracts all bits if `defined != 0` + /// Mainly used for args of C-functions and we should totally correctly fetch the size + /// of their arguments fn to_bytes(self) -> EvalResult<'static, u128>; }