Skip to content

Commit

Permalink
implement ExitThread
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Sep 22, 2024
1 parent faba6df commit 88cde64
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ fn main() -> anyhow::Result<ExitCode> {
machine.dump_state(0);
}
x86::CPUState::Exit(_) => {}
x86::CPUState::Running | x86::CPUState::Blocked(_) | x86::CPUState::SysCall => {
x86::CPUState::Running | x86::CPUState::Blocked(_) | x86::CPUState::SysCall | x86::CPUState::Free => {
unreachable!()
}
x86::CPUState::DebugBreak => todo!(),
Expand Down
Binary file modified win32/dll/kernel32.dll
Binary file not shown.
12 changes: 11 additions & 1 deletion win32/src/winapi/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,11 @@ pub mod kernel32 {
let uExitCode = <u32>::from_stack(mem, esp + 4u32);
winapi::kernel32::ExitProcess(machine, uExitCode).to_raw()
}
pub unsafe fn ExitThread(machine: &mut Machine, esp: u32) -> u32 {
let mem = machine.mem().detach();
let dwExitCode = <u32>::from_stack(mem, esp + 4u32);
winapi::kernel32::ExitThread(machine, dwExitCode).to_raw()
}
pub unsafe fn FileTimeToSystemTime(machine: &mut Machine, esp: u32) -> u32 {
let mem = machine.mem().detach();
let lpFileTime = <Option<&FILETIME>>::from_stack(mem, esp + 4u32);
Expand Down Expand Up @@ -3119,6 +3124,10 @@ pub mod kernel32 {
name: "ExitProcess",
func: crate::shims::Handler::Sync(impls::ExitProcess),
};
pub const ExitThread: Shim = Shim {
name: "ExitThread",
func: crate::shims::Handler::Sync(impls::ExitThread),
};
pub const FileTimeToSystemTime: Shim = Shim {
name: "FileTimeToSystemTime",
func: crate::shims::Handler::Sync(impls::FileTimeToSystemTime),
Expand Down Expand Up @@ -3728,7 +3737,7 @@ pub mod kernel32 {
func: crate::shims::Handler::Async(impls::retrowin32_thread_main),
};
}
const SHIMS: [Shim; 167usize] = [
const SHIMS: [Shim; 168usize] = [
shims::AcquireSRWLockExclusive,
shims::AcquireSRWLockShared,
shims::AddVectoredExceptionHandler,
Expand All @@ -3744,6 +3753,7 @@ pub mod kernel32 {
shims::DisableThreadLibraryCalls,
shims::EnterCriticalSection,
shims::ExitProcess,
shims::ExitThread,
shims::FileTimeToSystemTime,
shims::FindClose,
shims::FindFirstFileA,
Expand Down
3 changes: 1 addition & 2 deletions win32/src/winapi/kernel32/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,6 @@ pub async fn retrowin32_main(machine: &mut Machine, entry_point: u32) -> u32 {
#[win32_derive::dllexport]
pub async fn retrowin32_thread_main(machine: &mut Machine, entry_point: u32, param: u32) -> u32 {
machine.call_x86(entry_point, vec![param]).await;
log::warn!("TODO: thread exiting, but we don't have a way to stop a single thread yet");
kernel32::exit_process(machine, 0);
machine.emu.x86.cpu_mut().state = x86::CPUState::Free;
0
}
15 changes: 15 additions & 0 deletions win32/src/winapi/kernel32/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ pub async fn CreateThread(
}
}

#[win32_derive::dllexport]
pub fn ExitThread(machine: &mut Machine, dwExitCode: u32) {
if machine.emu.x86.cur_cpu == 0 {
panic!("ExitThread called on main thread");
}

log::info!(
"ExitThread({}) on cpu {}",
dwExitCode,
machine.emu.x86.cur_cpu
);

machine.emu.x86.cpu_mut().state = x86::CPUState::Free;
}

#[win32_derive::dllexport]
pub fn ResumeThread(_machine: &mut Machine, hThread: HTHREAD) -> u32 {
1
Expand Down
3 changes: 2 additions & 1 deletion x86/src/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum CPUState {
SysCall,
Error(String),
Exit(u32),
Free,
}

impl CPUState {
Expand Down Expand Up @@ -245,7 +246,7 @@ impl X86 {
let mut soonest = None;
for (i, cpu) in self.cpus.iter().enumerate() {
match cpu.state {
CPUState::Running => {}
CPUState::Running | CPUState::Free => {}
CPUState::DebugBreak
| CPUState::Error(_)
| CPUState::Exit(_)
Expand Down

0 comments on commit 88cde64

Please sign in to comment.