_ => {}
}
+ if self.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
+ // FIXME: return a real value in case the target allocation has an
+ // alignment bigger than the one requested
+ let n = u128::max_value();
+ let amt = 128 - self.memory.pointer_size().bytes() * 8;
+ let (dest, return_to_block) = destination.unwrap();
+ let ty = self.tcx.types.usize;
+ self.write_scalar(dest, Scalar::from_u128((n << amt) >> amt), ty)?;
+ self.goto_block(return_to_block);
+ return Ok(true);
+ }
+
let mir = match self.load_mir(instance.def) {
Ok(mir) => mir,
Err(EvalError { kind: EvalErrorKind::NoMirFor(path), .. }) => {
match &link_name[..] {
"malloc" => {
- let size = self.value_to_scalar(args[0])?.to_u64()?;
+ let size = self.value_to_scalar(args[0])?.to_usize(self)?;
if size == 0 {
self.write_null(dest, dest_ty)?;
} else {
let align = self.tcx.data_layout.pointer_align;
- let ptr = self.memory.allocate(Size::from_bytes(size), align, Some(MemoryKind::C.into()))?;
+ let ptr = self.memory.allocate(Size::from_bytes(size), align, MemoryKind::C.into())?;
self.write_scalar(dest, Scalar::Ptr(ptr), dest_ty)?;
}
}
//
// libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)
// is called if a `HashMap` is created the regular way.
- match self.value_to_scalar(args[0])?.to_u64()? {
+ match self.value_to_scalar(args[0])?.to_usize(self)? {
318 | 511 => {
return err!(Unimplemented(
"miri does not support random number generators".to_owned(),
)?;
let mut args = self.frame().mir.args_iter();
- let arg_local = args.next().ok_or(
+ let arg_local = args.next().ok_or_else(||
EvalErrorKind::AbiViolation(
"Argument to __rust_maybe_catch_panic does not take enough arguments."
.to_owned(),
"memcmp" => {
let left = self.into_ptr(args[0].value)?;
let right = self.into_ptr(args[1].value)?;
- let n = Size::from_bytes(self.value_to_scalar(args[2])?.to_u64()?);
+ let n = Size::from_bytes(self.value_to_scalar(args[2])?.to_usize(self)?);
let result = {
let left_bytes = self.memory.read_bytes(left, n)?;
use std::cmp::Ordering::*;
match left_bytes.cmp(right_bytes) {
- Less => -1i8,
+ Less => -1i32,
Equal => 0,
Greater => 1,
}
self.write_scalar(
dest,
- Scalar::from_i8(result),
+ Scalar::from_i32(result),
dest_ty,
)?;
}
"memrchr" => {
let ptr = self.into_ptr(args[0].value)?;
- let val = self.value_to_scalar(args[1])?.to_u64()? as u8;
- let num = self.value_to_scalar(args[2])?.to_u64()?;
+ let val = self.value_to_scalar(args[1])?.to_bytes()? as u8;
+ let num = self.value_to_scalar(args[2])?.to_usize(self)?;
if let Some(idx) = self.memory.read_bytes(ptr, Size::from_bytes(num))?.iter().rev().position(
|&c| c == val,
)
"memchr" => {
let ptr = self.into_ptr(args[0].value)?;
- let val = self.value_to_scalar(args[1])?.to_u64()? as u8;
- let num = self.value_to_scalar(args[2])?.to_u64()?;
+ let val = self.value_to_scalar(args[1])?.to_bytes()? as u8;
+ let num = self.value_to_scalar(args[2])?.to_usize(self)?;
if let Some(idx) = self.memory.read_bytes(ptr, Size::from_bytes(num))?.iter().position(
|&c| c == val,
)
let value_copy = self.memory.allocate(
Size::from_bytes((value.len() + 1) as u64),
Align::from_bytes(1, 1).unwrap(),
- Some(MemoryKind::Env.into()),
+ MemoryKind::Env.into(),
)?;
self.memory.write_bytes(value_copy.into(), &value)?;
let trailing_zero_ptr = value_copy.offset(Size::from_bytes(value.len() as u64), &self)?.into();
}
"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
}
"sysconf" => {
- let name = self.value_to_scalar(args[0])?.to_u64()?;
+ let name = self.value_to_scalar(args[0])?.to_usize(self)?;
trace!("sysconf() called with name {}", name);
// cache the sysconf integers via miri's global cache
// Figure out how large a pthread TLS key actually is. This is libc::pthread_key_t.
let key_type = args[0].ty.builtin_deref(true)
- .ok_or(EvalErrorKind::AbiViolation("Wrong signature used for pthread_key_create: First argument must be a raw pointer.".to_owned()))?.ty;
+ .ok_or_else(|| EvalErrorKind::AbiViolation("Wrong signature used for pthread_key_create: First argument must be a raw pointer.".to_owned()))?.ty;
let key_size = self.layout_of(key_type)?.size;
// Create key and write it into the memory where key_ptr wants it
self.write_null(dest, dest_ty)?;
}
"pthread_key_delete" => {
- // The conversion into TlsKey here is a little fishy, but should work as long as usize >= libc::pthread_key_t
- let key = self.value_to_scalar(args[0])?.to_u64()? as TlsKey;
+ let key = self.value_to_scalar(args[0])?.to_bytes()?;
self.memory.delete_tls_key(key)?;
// Return success (0)
self.write_null(dest, dest_ty)?;
}
"pthread_getspecific" => {
- // The conversion into TlsKey here is a little fishy, but should work as long as usize >= libc::pthread_key_t
- let key = self.value_to_scalar(args[0])?.to_u64()? as TlsKey;
+ let key = self.value_to_scalar(args[0])?.to_bytes()?;
let ptr = self.memory.load_tls(key)?;
self.write_ptr(dest, ptr, dest_ty)?;
}
"pthread_setspecific" => {
- // The conversion into TlsKey here is a little fishy, but should work as long as usize >= libc::pthread_key_t
- let key = self.value_to_scalar(args[0])?.to_u64()? as TlsKey;
+ let key = self.value_to_scalar(args[0])?.to_bytes()?;
let new_ptr = self.into_ptr(args[1].value)?;
self.memory.store_tls(key, new_ptr)?;
match &path[..] {
// Allocators are magic. They have no MIR, even when the rest of libstd does.
"alloc::alloc::::__rust_alloc" => {
- let size = self.value_to_scalar(args[0])?.to_u64()?;
- let align = self.value_to_scalar(args[1])?.to_u64()?;
+ let size = self.value_to_scalar(args[0])?.to_usize(self)?;
+ let align = self.value_to_scalar(args[1])?.to_usize(self)?;
if size == 0 {
return err!(HeapAllocZeroBytes);
}
}
let ptr = self.memory.allocate(Size::from_bytes(size),
Align::from_bytes(align, align).unwrap(),
- Some(MemoryKind::Rust.into()))?;
+ MemoryKind::Rust.into())?;
self.write_scalar(dest, Scalar::Ptr(ptr), dest_ty)?;
}
"alloc::alloc::::__rust_alloc_zeroed" => {
- let size = self.value_to_scalar(args[0])?.to_u64()?;
- let align = self.value_to_scalar(args[1])?.to_u64()?;
+ let size = self.value_to_scalar(args[0])?.to_usize(self)?;
+ let align = self.value_to_scalar(args[1])?.to_usize(self)?;
if size == 0 {
return err!(HeapAllocZeroBytes);
}
}
let ptr = self.memory.allocate(Size::from_bytes(size),
Align::from_bytes(align, align).unwrap(),
- Some(MemoryKind::Rust.into()))?;
+ MemoryKind::Rust.into())?;
self.memory.write_repeat(ptr.into(), 0, Size::from_bytes(size))?;
self.write_scalar(dest, Scalar::Ptr(ptr), dest_ty)?;
}
"alloc::alloc::::__rust_dealloc" => {
let ptr = self.into_ptr(args[0].value)?.to_ptr()?;
- let old_size = self.value_to_scalar(args[1])?.to_u64()?;
- let align = self.value_to_scalar(args[2])?.to_u64()?;
+ let old_size = self.value_to_scalar(args[1])?.to_usize(self)?;
+ let align = self.value_to_scalar(args[2])?.to_usize(self)?;
if old_size == 0 {
return err!(HeapAllocZeroBytes);
}
}
"alloc::alloc::::__rust_realloc" => {
let ptr = self.into_ptr(args[0].value)?.to_ptr()?;
- let old_size = self.value_to_scalar(args[1])?.to_u64()?;
- let align = self.value_to_scalar(args[2])?.to_u64()?;
- let new_size = self.value_to_scalar(args[3])?.to_u64()?;
+ let old_size = self.value_to_scalar(args[1])?.to_usize(self)?;
+ let align = self.value_to_scalar(args[2])?.to_usize(self)?;
+ let new_size = self.value_to_scalar(args[3])?.to_usize(self)?;
if old_size == 0 || new_size == 0 {
return err!(HeapAllocZeroBytes);
}
// current frame.
self.dump_local(dest);
self.goto_block(dest_block);
- return Ok(());
+ Ok(())
}
fn write_null(&mut self, dest: Place, dest_ty: Ty<'tcx>) -> EvalResult<'tcx> {