use std::iter;
use rustc_middle::mir;
+use rustc_span::Symbol;
use rustc_target::abi::Size;
+use rustc_target::spec::abi::Abi;
use crate::*;
-use helpers::check_arg_count;
+use shims::foreign_items::EmulateByNameResult;
+use shims::windows::sync::EvalContextExt as _;
+use smallvec::SmallVec;
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
fn emulate_foreign_item_by_name(
&mut self,
- link_name: &str,
+ link_name: Symbol,
+ abi: Abi,
args: &[OpTy<'tcx, Tag>],
- dest: PlaceTy<'tcx, Tag>,
+ dest: &PlaceTy<'tcx, Tag>,
_ret: mir::BasicBlock,
- ) -> InterpResult<'tcx, bool> {
+ ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
let this = self.eval_context_mut();
// Windows API stubs.
// HANDLE = isize
+ // NTSTATUS = LONH = i32
// DWORD = ULONG = u32
// BOOL = i32
// BOOLEAN = u8
- match link_name {
+ match link_name.as_str() {
// Environment related shims
"GetEnvironmentVariableW" => {
- let &[name, buf, size] = check_arg_count(args)?;
+ let [name, buf, size] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let result = this.GetEnvironmentVariableW(name, buf, size)?;
this.write_scalar(Scalar::from_u32(result), dest)?;
}
"SetEnvironmentVariableW" => {
- let &[name, value] = check_arg_count(args)?;
+ let [name, value] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let result = this.SetEnvironmentVariableW(name, value)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"GetEnvironmentStringsW" => {
- let &[] = check_arg_count(args)?;
+ let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let result = this.GetEnvironmentStringsW()?;
- this.write_scalar(result, dest)?;
+ this.write_pointer(result, dest)?;
}
"FreeEnvironmentStringsW" => {
- let &[env_block] = check_arg_count(args)?;
+ let [env_block] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let result = this.FreeEnvironmentStringsW(env_block)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"GetCurrentDirectoryW" => {
- let &[size, buf] = check_arg_count(args)?;
+ let [size, buf] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let result = this.GetCurrentDirectoryW(size, buf)?;
this.write_scalar(Scalar::from_u32(result), dest)?;
}
"SetCurrentDirectoryW" => {
- let &[path] = check_arg_count(args)?;
+ let [path] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let result = this.SetCurrentDirectoryW(path)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
- // File related shims
- "GetStdHandle" => {
- let &[which] = check_arg_count(args)?;
- let which = this.read_scalar(which)?.to_i32()?;
- // We just make this the identity function, so we know later in `WriteFile`
- // which one it is.
- this.write_scalar(Scalar::from_machine_isize(which.into(), this), dest)?;
- }
- "WriteFile" => {
- let &[handle, buf, n, written_ptr, overlapped] = check_arg_count(args)?;
- this.read_scalar(overlapped)?.to_machine_usize(this)?; // this is a poiner, that we ignore
- let handle = this.read_scalar(handle)?.to_machine_isize(this)?;
- let buf = this.read_scalar(buf)?.check_init()?;
- let n = this.read_scalar(n)?.to_u32()?;
- let written_place = this.deref_operand(written_ptr)?;
- // Spec says to always write `0` first.
- this.write_null(written_place.into())?;
- let written = if handle == -11 || handle == -12 {
- // stdout/stderr
- use std::io::{self, Write};
-
- let buf_cont = this.memory.read_bytes(buf, Size::from_bytes(u64::from(n)))?;
- let res = if handle == -11 {
- io::stdout().write(buf_cont)
- } else {
- io::stderr().write(buf_cont)
- };
- res.ok().map(|n| n as u32)
- } else {
- throw_unsup_format!("on Windows, writing to anything except stdout/stderr is not supported")
- };
- // If there was no error, write back how much was written.
- if let Some(n) = written {
- this.write_scalar(Scalar::from_u32(n), written_place.into())?;
- }
- // Return whether this was a success.
- this.write_scalar(
- Scalar::from_i32(if written.is_some() { 1 } else { 0 }),
- dest,
- )?;
- }
-
// Allocation
"HeapAlloc" => {
- let &[handle, flags, size] = check_arg_count(args)?;
+ let [handle, flags, size] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
this.read_scalar(handle)?.to_machine_isize(this)?;
let flags = this.read_scalar(flags)?.to_u32()?;
let size = this.read_scalar(size)?.to_machine_usize(this)?;
let zero_init = (flags & 0x00000008) != 0; // HEAP_ZERO_MEMORY
- let res = this.malloc(size, zero_init, MiriMemoryKind::WinHeap);
- this.write_scalar(res, dest)?;
+ let res = this.malloc(size, zero_init, MiriMemoryKind::WinHeap)?;
+ this.write_pointer(res, dest)?;
}
"HeapFree" => {
- let &[handle, flags, ptr] = check_arg_count(args)?;
+ let [handle, flags, ptr] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
this.read_scalar(handle)?.to_machine_isize(this)?;
this.read_scalar(flags)?.to_u32()?;
- let ptr = this.read_scalar(ptr)?.check_init()?;
+ let ptr = this.read_pointer(ptr)?;
this.free(ptr, MiriMemoryKind::WinHeap)?;
this.write_scalar(Scalar::from_i32(1), dest)?;
}
"HeapReAlloc" => {
- let &[handle, flags, ptr, size] = check_arg_count(args)?;
+ let [handle, flags, ptr, size] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
this.read_scalar(handle)?.to_machine_isize(this)?;
this.read_scalar(flags)?.to_u32()?;
- let ptr = this.read_scalar(ptr)?.check_init()?;
+ let ptr = this.read_pointer(ptr)?;
let size = this.read_scalar(size)?.to_machine_usize(this)?;
let res = this.realloc(ptr, size, MiriMemoryKind::WinHeap)?;
- this.write_scalar(res, dest)?;
+ this.write_pointer(res, dest)?;
}
// errno
"SetLastError" => {
- let &[error] = check_arg_count(args)?;
+ let [error] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let error = this.read_scalar(error)?.check_init()?;
this.set_last_error(error)?;
}
"GetLastError" => {
- let &[] = check_arg_count(args)?;
+ let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let last_error = this.get_last_error()?;
this.write_scalar(last_error, dest)?;
}
// Querying system information
"GetSystemInfo" => {
- let &[system_info] = check_arg_count(args)?;
+ let [system_info] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let system_info = this.deref_operand(system_info)?;
// Initialize with `0`.
- this.memory.write_bytes(
+ this.write_bytes_ptr(
system_info.ptr,
iter::repeat(0u8).take(system_info.layout.size.bytes() as usize),
)?;
+ // Set selected fields.
+ let word_layout = this.machine.layouts.u16;
+ let dword_layout = this.machine.layouts.u32;
+ let usize_layout = this.machine.layouts.usize;
+
+ // Using `mplace_field` is error-prone, see: https://github.com/rust-lang/miri/issues/2136.
+ // Pointer fields have different sizes on different targets.
+ // To avoid all these issue we calculate the offsets ourselves.
+ let field_sizes = [
+ word_layout.size, // 0, wProcessorArchitecture : WORD
+ word_layout.size, // 1, wReserved : WORD
+ dword_layout.size, // 2, dwPageSize : DWORD
+ usize_layout.size, // 3, lpMinimumApplicationAddress : LPVOID
+ usize_layout.size, // 4, lpMaximumApplicationAddress : LPVOID
+ usize_layout.size, // 5, dwActiveProcessorMask : DWORD_PTR
+ dword_layout.size, // 6, dwNumberOfProcessors : DWORD
+ dword_layout.size, // 7, dwProcessorType : DWORD
+ dword_layout.size, // 8, dwAllocationGranularity : DWORD
+ word_layout.size, // 9, wProcessorLevel : WORD
+ word_layout.size, // 10, wProcessorRevision : WORD
+ ];
+ let field_offsets: SmallVec<[Size; 11]> = field_sizes
+ .iter()
+ .copied()
+ .scan(Size::ZERO, |a, x| {
+ let res = Some(*a);
+ *a += x;
+ res
+ })
+ .collect();
+
+ // Set page size.
+ let page_size = system_info.offset(
+ field_offsets[2],
+ MemPlaceMeta::None,
+ dword_layout,
+ &this.tcx,
+ )?;
+ this.write_scalar(
+ Scalar::from_int(PAGE_SIZE, dword_layout.size),
+ &page_size.into(),
+ )?;
// Set number of processors.
- let dword_size = Size::from_bytes(4);
- let num_cpus = this.mplace_field(system_info, 6)?;
- this.write_scalar(Scalar::from_int(NUM_CPUS, dword_size), num_cpus.into())?;
+ let num_cpus = system_info.offset(
+ field_offsets[6],
+ MemPlaceMeta::None,
+ dword_layout,
+ &this.tcx,
+ )?;
+ this.write_scalar(Scalar::from_int(NUM_CPUS, dword_layout.size), &num_cpus.into())?;
}
// Thread-local storage
// This just creates a key; Windows does not natively support TLS destructors.
// Create key and return it.
- let &[] = check_arg_count(args)?;
+ let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let key = this.machine.tls.create_tls_key(None, dest.layout.size)?;
this.write_scalar(Scalar::from_uint(key, dest.layout.size), dest)?;
}
"TlsGetValue" => {
- let &[key] = check_arg_count(args)?;
+ let [key] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let key = u128::from(this.read_scalar(key)?.to_u32()?);
let active_thread = this.get_active_thread();
let ptr = this.machine.tls.load_tls(key, active_thread, this)?;
this.write_scalar(ptr, dest)?;
}
"TlsSetValue" => {
- let &[key, new_ptr] = check_arg_count(args)?;
+ let [key, new_ptr] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let key = u128::from(this.read_scalar(key)?.to_u32()?);
let active_thread = this.get_active_thread();
- let new_ptr = this.read_scalar(new_ptr)?.check_init()?;
- this.machine.tls.store_tls(key, active_thread, this.test_null(new_ptr)?)?;
+ let new_data = this.read_scalar(new_ptr)?.check_init()?;
+ this.machine.tls.store_tls(key, active_thread, new_data, &*this.tcx)?;
// Return success (`1`).
this.write_scalar(Scalar::from_i32(1), dest)?;
// Access to command-line arguments
"GetCommandLineW" => {
- let &[] = check_arg_count(args)?;
- this.write_scalar(
- this.machine.cmd_line.expect("machine must be initialized"),
+ let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ this.write_pointer(
+ this.machine.cmd_line.expect("machine must be initialized").ptr,
dest,
)?;
}
// Time related shims
"GetSystemTimeAsFileTime" => {
#[allow(non_snake_case)]
- let &[LPFILETIME] = check_arg_count(args)?;
+ let [LPFILETIME] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
this.GetSystemTimeAsFileTime(LPFILETIME)?;
}
"QueryPerformanceCounter" => {
#[allow(non_snake_case)]
- let &[lpPerformanceCount] = check_arg_count(args)?;
+ let [lpPerformanceCount] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let result = this.QueryPerformanceCounter(lpPerformanceCount)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"QueryPerformanceFrequency" => {
#[allow(non_snake_case)]
- let &[lpFrequency] = check_arg_count(args)?;
+ let [lpFrequency] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
let result = this.QueryPerformanceFrequency(lpFrequency)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
+ // Synchronization primitives
+ "AcquireSRWLockExclusive" => {
+ let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ this.AcquireSRWLockExclusive(ptr)?;
+ }
+ "ReleaseSRWLockExclusive" => {
+ let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ this.ReleaseSRWLockExclusive(ptr)?;
+ }
+ "TryAcquireSRWLockExclusive" => {
+ let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ let ret = this.TryAcquireSRWLockExclusive(ptr)?;
+ this.write_scalar(Scalar::from_u8(ret), dest)?;
+ }
+ "AcquireSRWLockShared" => {
+ let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ this.AcquireSRWLockShared(ptr)?;
+ }
+ "ReleaseSRWLockShared" => {
+ let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ this.ReleaseSRWLockShared(ptr)?;
+ }
+ "TryAcquireSRWLockShared" => {
+ let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ let ret = this.TryAcquireSRWLockShared(ptr)?;
+ this.write_scalar(Scalar::from_u8(ret), dest)?;
+ }
+
// Dynamic symbol loading
"GetProcAddress" => {
#[allow(non_snake_case)]
- let &[hModule, lpProcName] = check_arg_count(args)?;
+ let [hModule, lpProcName] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
this.read_scalar(hModule)?.to_machine_isize(this)?;
- let name = this.memory.read_c_str(this.read_scalar(lpProcName)?.check_init()?)?;
+ let name = this.read_c_str(this.read_pointer(lpProcName)?)?;
if let Some(dlsym) = Dlsym::from_str(name, &this.tcx.sess.target.os)? {
- let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
- this.write_scalar(Scalar::from(ptr), dest)?;
+ let ptr = this.create_fn_alloc_ptr(FnVal::Other(dlsym));
+ this.write_pointer(ptr, dest)?;
} else {
this.write_null(dest)?;
}
// Miscellaneous
"SystemFunction036" => {
- // The actual name of 'RtlGenRandom'
- let &[ptr, len] = check_arg_count(args)?;
- let ptr = this.read_scalar(ptr)?.check_init()?;
+ // This is really 'RtlGenRandom'.
+ let [ptr, len] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ let ptr = this.read_pointer(ptr)?;
let len = this.read_scalar(len)?.to_u32()?;
this.gen_random(ptr, len.into())?;
this.write_scalar(Scalar::from_bool(true), dest)?;
}
+ "BCryptGenRandom" => {
+ let [algorithm, ptr, len, flags] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ let algorithm = this.read_scalar(algorithm)?;
+ let ptr = this.read_pointer(ptr)?;
+ let len = this.read_scalar(len)?.to_u32()?;
+ let flags = this.read_scalar(flags)?.to_u32()?;
+ if flags != 2 {
+ // ^ BCRYPT_USE_SYSTEM_PREFERRED_RNG
+ throw_unsup_format!(
+ "BCryptGenRandom is supported only with the BCRYPT_USE_SYSTEM_PREFERRED_RNG flag"
+ );
+ }
+ if algorithm.to_machine_usize(this)? != 0 {
+ throw_unsup_format!(
+ "BCryptGenRandom algorithm must be NULL when the flag is BCRYPT_USE_SYSTEM_PREFERRED_RNG"
+ );
+ }
+ this.gen_random(ptr, len.into())?;
+ this.write_null(dest)?; // STATUS_SUCCESS
+ }
"GetConsoleScreenBufferInfo" => {
// `term` needs this, so we fake it.
- let &[console, buffer_info] = check_arg_count(args)?;
+ let [console, buffer_info] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
this.read_scalar(console)?.to_machine_isize(this)?;
this.deref_operand(buffer_info)?;
// Indicate an error.
}
"GetConsoleMode" => {
// Windows "isatty" (in libtest) needs this, so we fake it.
- let &[console, mode] = check_arg_count(args)?;
+ let [console, mode] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
this.read_scalar(console)?.to_machine_isize(this)?;
this.deref_operand(mode)?;
// Indicate an error.
this.write_null(dest)?;
}
"SwitchToThread" => {
- let &[] = check_arg_count(args)?;
+ let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
// Note that once Miri supports concurrency, this will need to return a nonzero
// value if this call does result in switching to another thread.
this.write_null(dest)?;
}
+ "GetStdHandle" => {
+ let [which] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ let which = this.read_scalar(which)?.to_i32()?;
+ // We just make this the identity function, so we know later in `NtWriteFile` which
+ // one it is. This is very fake, but libtest needs it so we cannot make it a
+ // std-only shim.
+ this.write_scalar(Scalar::from_machine_isize(which.into(), this), dest)?;
+ }
// Better error for attempts to create a thread
"CreateThread" => {
- throw_unsup_format!("Miri does not support concurrency on Windows");
+ let [_, _, _, _, _, _] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+
+ this.handle_unsupported("can't create threads on Windows")?;
+ return Ok(EmulateByNameResult::AlreadyJumped);
}
// Incomplete shims that we "stub out" just to get pre-main initialization code to work.
// These shims are enabled only when the caller is in the standard library.
- "GetProcessHeap" if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
- let &[] = check_arg_count(args)?;
+ "GetProcessHeap" if this.frame_in_std() => {
+ let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
// Just fake a HANDLE
this.write_scalar(Scalar::from_machine_isize(1, this), dest)?;
}
- "GetModuleHandleW" if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
+ "GetModuleHandleA" if this.frame_in_std() => {
#[allow(non_snake_case)]
- let &[_lpModuleName] = check_arg_count(args)?;
- // Pretend this does not exist / nothing happened, by returning zero.
- this.write_null(dest)?;
+ let [_lpModuleName] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ // We need to return something non-null here to make `compat_fn!` work.
+ this.write_scalar(Scalar::from_machine_isize(1, this), dest)?;
}
- "SetConsoleTextAttribute" if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
+ "SetConsoleTextAttribute" if this.frame_in_std() => {
#[allow(non_snake_case)]
- let &[_hConsoleOutput, _wAttribute] = check_arg_count(args)?;
+ let [_hConsoleOutput, _wAttribute] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
// Pretend these does not exist / nothing happened, by returning zero.
this.write_null(dest)?;
}
- "AddVectoredExceptionHandler" if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
+ "AddVectoredExceptionHandler" if this.frame_in_std() => {
#[allow(non_snake_case)]
- let &[_First, _Handler] = check_arg_count(args)?;
+ let [_First, _Handler] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
// Any non zero value works for the stdlib. This is just used for stack overflows anyway.
this.write_scalar(Scalar::from_machine_usize(1, this), dest)?;
}
+ "SetThreadStackGuarantee" if this.frame_in_std() => {
+ #[allow(non_snake_case)]
+ let [_StackSizeInBytes] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ // Any non zero value works for the stdlib. This is just used for stack overflows anyway.
+ this.write_scalar(Scalar::from_u32(1), dest)?;
+ }
| "InitializeCriticalSection"
| "EnterCriticalSection"
| "LeaveCriticalSection"
| "DeleteCriticalSection"
- if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
+ if this.frame_in_std() =>
+ {
#[allow(non_snake_case)]
- let &[_lpCriticalSection] = check_arg_count(args)?;
- assert_eq!(this.get_total_thread_count(), 1, "concurrency on Windows is not supported");
+ let [_lpCriticalSection] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ assert_eq!(
+ this.get_total_thread_count(),
+ 1,
+ "concurrency on Windows is not supported"
+ );
// Nothing to do, not even a return value.
// (Windows locks are reentrant, and we have only 1 thread,
// so not doing any futher checks here is at least not incorrect.)
}
- "TryEnterCriticalSection"
- if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
+ "TryEnterCriticalSection" if this.frame_in_std() => {
#[allow(non_snake_case)]
- let &[_lpCriticalSection] = check_arg_count(args)?;
- assert_eq!(this.get_total_thread_count(), 1, "concurrency on Windows is not supported");
+ let [_lpCriticalSection] =
+ this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ assert_eq!(
+ this.get_total_thread_count(),
+ 1,
+ "concurrency on Windows is not supported"
+ );
// There is only one thread, so this always succeeds and returns TRUE.
this.write_scalar(Scalar::from_i32(1), dest)?;
}
+ "GetCurrentThread" if this.frame_in_std() => {
+ let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
+ this.write_scalar(Scalar::from_machine_isize(1, this), dest)?;
+ }
- _ => throw_unsup_format!("can't call foreign function: {}", link_name),
+ _ => return Ok(EmulateByNameResult::NotSupported),
}
- Ok(true)
+ Ok(EmulateByNameResult::NeedsJumping)
}
}
-