Skip to content

Commit f7baefb

Browse files
committed
improved GetThreadId witha a handle search, but handles have to be rethinked
1 parent 1177bd4 commit f7baefb

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

crates/libmwemu/src/serialization/thread_context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub struct SerializableThreadContext {
3030
pub fls: Vec<u32>,
3131
pub fs: BTreeMap<u64, u64>,
3232
pub call_stack: Vec<(u64, u64)>, // the first address is the source of the call location and the second address is the destination of the call
33+
pub handle: u64
3334
}
3435

3536
impl From<&ThreadContext> for SerializableThreadContext {
@@ -56,6 +57,7 @@ impl From<&ThreadContext> for SerializableThreadContext {
5657
fls: thread.fls.clone(),
5758
fs: thread.fs.clone(),
5859
call_stack: thread.call_stack.clone(),
60+
handle: thread.handle
5961
}
6062
}
6163
}
@@ -84,6 +86,7 @@ impl From<SerializableThreadContext> for ThreadContext {
8486
fls: serialized.fls,
8587
fs: serialized.fs,
8688
call_stack: serialized.call_stack,
89+
handle: serialized.handle,
8790
}
8891
}
8992
}

crates/libmwemu/src/thread_context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct ThreadContext {
2525
pub fls: Vec<u32>,
2626
pub fs: BTreeMap<u64, u64>,
2727
pub call_stack: Vec<(u64, u64)>,
28+
pub handle: u64,
2829
}
2930

3031
impl ThreadContext {
@@ -51,6 +52,7 @@ impl ThreadContext {
5152
fls: Vec::new(),
5253
fs: BTreeMap::new(),
5354
call_stack: Vec::with_capacity(10000),
55+
handle: 0
5456
}
5557
}
5658
}

crates/libmwemu/src/winapi/winapi32/kernel32/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,23 @@ pub fn gateway(addr: u32, emu: &mut emu::Emu) -> String {
548548
}
549549

550550
fn GetThreadId(emu: &mut Emu) {
551+
let hndl = emu
552+
.maps
553+
.read_dword(emu.regs().get_esp() + 4)
554+
.expect("kernel32!GetThreadId bad handle parameter") as u64;
555+
551556
emu.stack_pop32(false);
552-
emu.regs_mut().rax = 0x2c2878;
557+
558+
559+
for i in 0..emu.threads.len() {
560+
if emu.threads[i].handle == hndl {
561+
emu.regs_mut().rax = emu.threads[i].id;
562+
log_red!(emu, "kernel32!GetThreadId hndl:{} (requested handle exists and its tid {})", hndl, emu.threads[i].id);
563+
return;
564+
}
565+
}
566+
log_red!(emu, "kernel32!GetThreadId hndl:{} (requested handle doesn't exist, returning a fake handle for now but should return zero.)", hndl);
567+
emu.regs_mut().rax = 0x2c2878; // if handle not found should return zero.
553568
}
554569

555570
lazy_static! {

crates/libmwemu/src/winapi/winapi64/kernel32/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,24 @@ pub fn gateway(addr: u64, emu: &mut emu::Emu) -> String {
568568
String::new()
569569
}
570570

571+
571572
fn GetThreadId(emu: &mut Emu) {
572-
let hThread = emu.regs().rcx;
573-
emu.regs_mut().rax = 0x2c2878;
573+
let hndl = emu.regs().rcx;
574+
575+
for i in 0..emu.threads.len() {
576+
if emu.threads[i].handle == hndl {
577+
emu.regs_mut().rax = emu.threads[i].id;
578+
log_red!(emu, "kernel32!GetThreadId hndl:{} (requested handle exists and its tid {})", hndl, emu.threads[i].id);
579+
return;
580+
}
581+
}
582+
log_red!(emu, "kernel32!GetThreadId hndl:{} (requested handle doesn't exist, returning a fake handle for now but should return zero.)", hndl);
583+
emu.regs_mut().rax = 0x2c2878; // if handle not found should return zero.
574584
}
575585

586+
587+
588+
576589
lazy_static! {
577590
pub static ref COUNT_READ: Mutex<u32> = Mutex::new(0);
578591
pub static ref COUNT_WRITE: Mutex<u32> = Mutex::new(0);

0 commit comments

Comments
 (0)