Skip to main content

miri/shims/windows/
thread.rs

1use rustc_abi::ExternAbi;
2
3use self::shims::windows::handle::{EvalContextExt as _, Handle, PseudoHandle};
4use crate::*;
5
6impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
7
8#[allow(non_snake_case)]
9pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
10    fn CreateThread(
11        &mut self,
12        security_op: &OpTy<'tcx>,
13        stacksize_op: &OpTy<'tcx>,
14        start_op: &OpTy<'tcx>,
15        arg_op: &OpTy<'tcx>,
16        flags_op: &OpTy<'tcx>,
17        thread_id_op: &OpTy<'tcx>,
18    ) -> InterpResult<'tcx, ThreadId> {
19        let ecx = self.eval_context_mut();
20
21        let security = ecx.read_pointer(security_op)?;
22        // stacksize is ignored, but still needs to be a valid usize
23        ecx.read_target_usize(stacksize_op)?;
24        let start_routine = ecx.read_pointer(start_op)?;
25        let func_arg = ecx.read_immediate(arg_op)?;
26        let flags = ecx.read_scalar(flags_op)?.to_u32()?;
27        let thread_id_place = ecx.deref_pointer(thread_id_op)?;
28
29        let stack_size_param_is_a_reservation =
30            ecx.eval_windows_u32("c", "STACK_SIZE_PARAM_IS_A_RESERVATION");
31
32        // We ignore the stack size, so we also ignore the
33        // `STACK_SIZE_PARAM_IS_A_RESERVATION` flag.
34        if flags != 0 && flags != stack_size_param_is_a_reservation {
35            throw_unsup_format!("unsupported `dwCreationFlags` {} in `CreateThread`", flags)
36        }
37
38        if !ecx.ptr_is_null(security)? {
39            throw_unsup_format!("non-null `lpThreadAttributes` in `CreateThread`")
40        }
41
42        let thread_id = ecx.start_regular_thread(
43            start_routine,
44            ExternAbi::System { unwind: false },
45            func_arg,
46            ecx.layout_of(ecx.tcx.types.u32)?,
47        )?;
48        if !ecx.ptr_is_null(thread_id_place.ptr())? {
49            ecx.write_int(thread_id.to_u32(), &thread_id_place)?;
50        }
51        interp_ok(thread_id)
52    }
53
54    fn WaitForSingleObject(
55        &mut self,
56        handle_op: &OpTy<'tcx>,
57        timeout_op: &OpTy<'tcx>,
58        return_dest: &MPlaceTy<'tcx>,
59    ) -> InterpResult<'tcx> {
60        let ecx = self.eval_context_mut();
61
62        let handle = ecx.read_handle(handle_op, "WaitForSingleObject")?;
63        let timeout = ecx.read_scalar(timeout_op)?.to_u32()?;
64
65        let joined_thread_id = match handle {
66            Handle::Thread(thread) => thread,
67            // Unlike on posix, the outcome of joining the current thread is not documented.
68            // On current Windows, it just deadlocks.
69            Handle::Pseudo(PseudoHandle::CurrentThread) => ecx.active_thread(),
70            _ => ecx.invalid_handle("WaitForSingleObject")?,
71        };
72
73        if timeout != ecx.eval_windows_u32("c", "INFINITE") {
74            throw_unsup_format!("`WaitForSingleObject` with non-infinite timeout");
75        }
76
77        ecx.join_thread(
78            joined_thread_id,
79            /* success_retval */ ecx.eval_windows("c", "WAIT_OBJECT_0"),
80            return_dest,
81        )?;
82
83        interp_ok(())
84    }
85}