4 use rustc_span::Symbol;
5 use rustc_target::abi::Size;
6 use rustc_target::spec::abi::Abi;
9 use shims::foreign_items::EmulateByNameResult;
10 use shims::windows::sync::EvalContextExt as _;
12 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
13 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
14 fn emulate_foreign_item_by_name(
18 args: &[OpTy<'tcx, Tag>],
19 dest: &PlaceTy<'tcx, Tag>,
20 _ret: mir::BasicBlock,
21 ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
22 let this = self.eval_context_mut();
26 // NTSTATUS = LONH = i32
27 // DWORD = ULONG = u32
30 match &*link_name.as_str() {
31 // Environment related shims
32 "GetEnvironmentVariableW" => {
33 let [name, buf, size] =
34 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
35 let result = this.GetEnvironmentVariableW(name, buf, size)?;
36 this.write_scalar(Scalar::from_u32(result), dest)?;
38 "SetEnvironmentVariableW" => {
40 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
41 let result = this.SetEnvironmentVariableW(name, value)?;
42 this.write_scalar(Scalar::from_i32(result), dest)?;
44 "GetEnvironmentStringsW" => {
45 let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
46 let result = this.GetEnvironmentStringsW()?;
47 this.write_pointer(result, dest)?;
49 "FreeEnvironmentStringsW" => {
51 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
52 let result = this.FreeEnvironmentStringsW(env_block)?;
53 this.write_scalar(Scalar::from_i32(result), dest)?;
55 "GetCurrentDirectoryW" => {
57 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
58 let result = this.GetCurrentDirectoryW(size, buf)?;
59 this.write_scalar(Scalar::from_u32(result), dest)?;
61 "SetCurrentDirectoryW" => {
63 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
64 let result = this.SetCurrentDirectoryW(path)?;
65 this.write_scalar(Scalar::from_i32(result), dest)?;
70 let [handle, flags, size] =
71 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
72 this.read_scalar(handle)?.to_machine_isize(this)?;
73 let flags = this.read_scalar(flags)?.to_u32()?;
74 let size = this.read_scalar(size)?.to_machine_usize(this)?;
75 let zero_init = (flags & 0x00000008) != 0; // HEAP_ZERO_MEMORY
76 let res = this.malloc(size, zero_init, MiriMemoryKind::WinHeap)?;
77 this.write_pointer(res, dest)?;
80 let [handle, flags, ptr] =
81 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
82 this.read_scalar(handle)?.to_machine_isize(this)?;
83 this.read_scalar(flags)?.to_u32()?;
84 let ptr = this.read_pointer(ptr)?;
85 this.free(ptr, MiriMemoryKind::WinHeap)?;
86 this.write_scalar(Scalar::from_i32(1), dest)?;
89 let [handle, flags, ptr, size] =
90 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
91 this.read_scalar(handle)?.to_machine_isize(this)?;
92 this.read_scalar(flags)?.to_u32()?;
93 let ptr = this.read_pointer(ptr)?;
94 let size = this.read_scalar(size)?.to_machine_usize(this)?;
95 let res = this.realloc(ptr, size, MiriMemoryKind::WinHeap)?;
96 this.write_pointer(res, dest)?;
102 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
103 let error = this.read_scalar(error)?.check_init()?;
104 this.set_last_error(error)?;
107 let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
108 let last_error = this.get_last_error()?;
109 this.write_scalar(last_error, dest)?;
112 // Querying system information
114 use crate::rustc_middle::ty::{layout::LayoutOf, TyKind, UintTy};
117 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
118 let system_info = this.deref_operand(system_info)?;
119 // Initialize with `0`.
120 this.write_bytes_ptr(
122 iter::repeat(0u8).take(system_info.layout.size.bytes() as usize),
124 // Set selected fields.
125 let dword_ty = this.tcx.mk_ty(TyKind::Uint(UintTy::U32));
126 let dword_layout = this.layout_of(dword_ty)?;
128 let page_size = system_info.offset(
135 Scalar::from_int(PAGE_SIZE, dword_layout.size),
138 // Set number of processors.
139 let num_cpus = system_info.offset(
140 Size::from_bytes(32),
145 this.write_scalar(Scalar::from_int(NUM_CPUS, dword_layout.size), &num_cpus.into())?;
148 // Thread-local storage
150 // This just creates a key; Windows does not natively support TLS destructors.
152 // Create key and return it.
153 let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
154 let key = this.machine.tls.create_tls_key(None, dest.layout.size)?;
155 this.write_scalar(Scalar::from_uint(key, dest.layout.size), dest)?;
158 let [key] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
159 let key = u128::from(this.read_scalar(key)?.to_u32()?);
160 let active_thread = this.get_active_thread();
161 let ptr = this.machine.tls.load_tls(key, active_thread, this)?;
162 this.write_scalar(ptr, dest)?;
166 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
167 let key = u128::from(this.read_scalar(key)?.to_u32()?);
168 let active_thread = this.get_active_thread();
169 let new_data = this.read_scalar(new_ptr)?.check_init()?;
170 this.machine.tls.store_tls(key, active_thread, new_data, &*this.tcx)?;
172 // Return success (`1`).
173 this.write_scalar(Scalar::from_i32(1), dest)?;
176 // Access to command-line arguments
177 "GetCommandLineW" => {
178 let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
180 this.machine.cmd_line.expect("machine must be initialized").ptr,
185 // Time related shims
186 "GetSystemTimeAsFileTime" => {
187 #[allow(non_snake_case)]
189 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
190 this.GetSystemTimeAsFileTime(LPFILETIME)?;
192 "QueryPerformanceCounter" => {
193 #[allow(non_snake_case)]
194 let [lpPerformanceCount] =
195 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
196 let result = this.QueryPerformanceCounter(lpPerformanceCount)?;
197 this.write_scalar(Scalar::from_i32(result), dest)?;
199 "QueryPerformanceFrequency" => {
200 #[allow(non_snake_case)]
202 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
203 let result = this.QueryPerformanceFrequency(lpFrequency)?;
204 this.write_scalar(Scalar::from_i32(result), dest)?;
207 // Synchronization primitives
208 "AcquireSRWLockExclusive" => {
209 let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
210 this.AcquireSRWLockExclusive(ptr)?;
212 "ReleaseSRWLockExclusive" => {
213 let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
214 this.ReleaseSRWLockExclusive(ptr)?;
216 "TryAcquireSRWLockExclusive" => {
217 let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
218 let ret = this.TryAcquireSRWLockExclusive(ptr)?;
219 this.write_scalar(Scalar::from_u8(ret), dest)?;
221 "AcquireSRWLockShared" => {
222 let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
223 this.AcquireSRWLockShared(ptr)?;
225 "ReleaseSRWLockShared" => {
226 let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
227 this.ReleaseSRWLockShared(ptr)?;
229 "TryAcquireSRWLockShared" => {
230 let [ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
231 let ret = this.TryAcquireSRWLockShared(ptr)?;
232 this.write_scalar(Scalar::from_u8(ret), dest)?;
235 // Dynamic symbol loading
236 "GetProcAddress" => {
237 #[allow(non_snake_case)]
238 let [hModule, lpProcName] =
239 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
240 this.read_scalar(hModule)?.to_machine_isize(this)?;
241 let name = this.read_c_str(this.read_pointer(lpProcName)?)?;
242 if let Some(dlsym) = Dlsym::from_str(name, &this.tcx.sess.target.os)? {
243 let ptr = this.create_fn_alloc_ptr(FnVal::Other(dlsym));
244 this.write_pointer(ptr, dest)?;
246 this.write_null(dest)?;
251 "SystemFunction036" => {
252 // This is really 'RtlGenRandom'.
254 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
255 let ptr = this.read_pointer(ptr)?;
256 let len = this.read_scalar(len)?.to_u32()?;
257 this.gen_random(ptr, len.into())?;
258 this.write_scalar(Scalar::from_bool(true), dest)?;
260 "BCryptGenRandom" => {
261 let [algorithm, ptr, len, flags] =
262 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
263 let algorithm = this.read_scalar(algorithm)?;
264 let ptr = this.read_pointer(ptr)?;
265 let len = this.read_scalar(len)?.to_u32()?;
266 let flags = this.read_scalar(flags)?.to_u32()?;
268 // ^ BCRYPT_USE_SYSTEM_PREFERRED_RNG
270 "BCryptGenRandom is supported only with the BCRYPT_USE_SYSTEM_PREFERRED_RNG flag"
273 if algorithm.to_machine_usize(this)? != 0 {
275 "BCryptGenRandom algorithm must be NULL when the flag is BCRYPT_USE_SYSTEM_PREFERRED_RNG"
278 this.gen_random(ptr, len.into())?;
279 this.write_null(dest)?; // STATUS_SUCCESS
281 "GetConsoleScreenBufferInfo" => {
282 // `term` needs this, so we fake it.
283 let [console, buffer_info] =
284 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
285 this.read_scalar(console)?.to_machine_isize(this)?;
286 this.deref_operand(buffer_info)?;
287 // Indicate an error.
288 // FIXME: we should set last_error, but to what?
289 this.write_null(dest)?;
291 "GetConsoleMode" => {
292 // Windows "isatty" (in libtest) needs this, so we fake it.
293 let [console, mode] =
294 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
295 this.read_scalar(console)?.to_machine_isize(this)?;
296 this.deref_operand(mode)?;
297 // Indicate an error.
298 // FIXME: we should set last_error, but to what?
299 this.write_null(dest)?;
301 "SwitchToThread" => {
302 let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
303 // Note that once Miri supports concurrency, this will need to return a nonzero
304 // value if this call does result in switching to another thread.
305 this.write_null(dest)?;
309 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
310 let which = this.read_scalar(which)?.to_i32()?;
311 // We just make this the identity function, so we know later in `NtWriteFile` which
312 // one it is. This is very fake, but libtest needs it so we cannot make it a
314 this.write_scalar(Scalar::from_machine_isize(which.into(), this), dest)?;
317 // Better error for attempts to create a thread
319 let [_, _, _, _, _, _] =
320 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
322 this.handle_unsupported("can't create threads on Windows")?;
323 return Ok(EmulateByNameResult::AlreadyJumped);
326 // Incomplete shims that we "stub out" just to get pre-main initialization code to work.
327 // These shims are enabled only when the caller is in the standard library.
328 "GetProcessHeap" if this.frame_in_std() => {
329 let [] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
330 // Just fake a HANDLE
331 this.write_scalar(Scalar::from_machine_isize(1, this), dest)?;
333 "GetModuleHandleA" if this.frame_in_std() => {
334 #[allow(non_snake_case)]
335 let [_lpModuleName] =
336 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
337 // We need to return something non-null here to make `compat_fn!` work.
338 this.write_scalar(Scalar::from_machine_isize(1, this), dest)?;
340 "SetConsoleTextAttribute" if this.frame_in_std() => {
341 #[allow(non_snake_case)]
342 let [_hConsoleOutput, _wAttribute] =
343 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
344 // Pretend these does not exist / nothing happened, by returning zero.
345 this.write_null(dest)?;
347 "AddVectoredExceptionHandler" if this.frame_in_std() => {
348 #[allow(non_snake_case)]
349 let [_First, _Handler] =
350 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
351 // Any non zero value works for the stdlib. This is just used for stack overflows anyway.
352 this.write_scalar(Scalar::from_machine_usize(1, this), dest)?;
354 "SetThreadStackGuarantee" if this.frame_in_std() => {
355 #[allow(non_snake_case)]
356 let [_StackSizeInBytes] =
357 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
358 // Any non zero value works for the stdlib. This is just used for stack overflows anyway.
359 this.write_scalar(Scalar::from_u32(1), dest)?;
361 | "InitializeCriticalSection"
362 | "EnterCriticalSection"
363 | "LeaveCriticalSection"
364 | "DeleteCriticalSection"
365 if this.frame_in_std() =>
367 #[allow(non_snake_case)]
368 let [_lpCriticalSection] =
369 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
371 this.get_total_thread_count(),
373 "concurrency on Windows is not supported"
375 // Nothing to do, not even a return value.
376 // (Windows locks are reentrant, and we have only 1 thread,
377 // so not doing any futher checks here is at least not incorrect.)
379 "TryEnterCriticalSection" if this.frame_in_std() => {
380 #[allow(non_snake_case)]
381 let [_lpCriticalSection] =
382 this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
384 this.get_total_thread_count(),
386 "concurrency on Windows is not supported"
388 // There is only one thread, so this always succeeds and returns TRUE.
389 this.write_scalar(Scalar::from_i32(1), dest)?;
392 _ => return Ok(EmulateByNameResult::NotSupported),
395 Ok(EmulateByNameResult::NeedsJumping)