fix: preserve EDX across usercall trampoline cdecl calls
Browse filesOriginal __usercall functions often don't touch EDX, but our cdecl impl
functions are free to clobber it. Callers (e.g. FUN_0055fda0) rely on
EDX surviving calls to IsWormInSpecialState — the corrupted EDX caused
an access violation writing to near-null address 0x7EA9.
All 7 trampoline macro arms now push/pop EDX around the cdecl call,
with stack param offsets adjusted by +4.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
crates/openwa-wormkit/src/hook.rs
CHANGED
|
@@ -23,13 +23,16 @@
|
|
| 23 |
/// ```
|
| 24 |
macro_rules! usercall_trampoline {
|
| 25 |
// 1 register arg, 0 stack params, plain ret
|
|
|
|
| 26 |
(fn $name:ident; impl_fn = $impl:path; reg = $reg:ident) => {
|
| 27 |
#[unsafe(naked)]
|
| 28 |
unsafe extern "C" fn $name() {
|
| 29 |
core::arch::naked_asm!(
|
|
|
|
| 30 |
concat!("push ", stringify!($reg)),
|
| 31 |
"call {impl_fn}",
|
| 32 |
"add esp, 4",
|
|
|
|
| 33 |
"ret",
|
| 34 |
impl_fn = sym $impl,
|
| 35 |
);
|
|
@@ -42,10 +45,12 @@ macro_rules! usercall_trampoline {
|
|
| 42 |
#[unsafe(naked)]
|
| 43 |
unsafe extern "C" fn $name() {
|
| 44 |
core::arch::naked_asm!(
|
| 45 |
-
"push
|
|
|
|
| 46 |
concat!("push ", stringify!($reg)),
|
| 47 |
"call {impl_fn}",
|
| 48 |
"add esp, 8",
|
|
|
|
| 49 |
concat!("ret ", $ret),
|
| 50 |
impl_fn = sym $impl,
|
| 51 |
);
|
|
@@ -53,18 +58,18 @@ macro_rules! usercall_trampoline {
|
|
| 53 |
};
|
| 54 |
|
| 55 |
// 2 register args, 1 stack param, ret N
|
| 56 |
-
// Stack layout at entry: [ESP+0]=retaddr, [ESP+4]=arg
|
| 57 |
-
// Push: stack_arg, reg2, reg1 → impl_fn(reg1, reg2, stack_arg)
|
| 58 |
(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident];
|
| 59 |
stack_params = 1; ret_bytes = $ret:literal) => {
|
| 60 |
#[unsafe(naked)]
|
| 61 |
unsafe extern "C" fn $name() {
|
| 62 |
core::arch::naked_asm!(
|
| 63 |
-
"push
|
|
|
|
| 64 |
concat!("push ", stringify!($r2)),
|
| 65 |
concat!("push ", stringify!($r1)),
|
| 66 |
"call {impl_fn}",
|
| 67 |
"add esp, 12",
|
|
|
|
| 68 |
concat!("ret ", $ret),
|
| 69 |
impl_fn = sym $impl,
|
| 70 |
);
|
|
@@ -72,19 +77,19 @@ macro_rules! usercall_trampoline {
|
|
| 72 |
};
|
| 73 |
|
| 74 |
// 2 register args, 2 stack params, ret N
|
| 75 |
-
// Stack layout at entry: [ESP+0]=retaddr, [ESP+4]=arg1, [ESP+8]=arg2
|
| 76 |
-
// Push: stack_arg2, stack_arg1, reg2, reg1 → impl_fn(reg1, reg2, stack_arg1, stack_arg2)
|
| 77 |
(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident];
|
| 78 |
stack_params = 2; ret_bytes = $ret:literal) => {
|
| 79 |
#[unsafe(naked)]
|
| 80 |
unsafe extern "C" fn $name() {
|
| 81 |
core::arch::naked_asm!(
|
| 82 |
-
"push
|
| 83 |
-
"push [esp+
|
|
|
|
| 84 |
concat!("push ", stringify!($r2)),
|
| 85 |
concat!("push ", stringify!($r1)),
|
| 86 |
"call {impl_fn}",
|
| 87 |
"add esp, 16",
|
|
|
|
| 88 |
concat!("ret ", $ret),
|
| 89 |
impl_fn = sym $impl,
|
| 90 |
);
|
|
@@ -92,15 +97,16 @@ macro_rules! usercall_trampoline {
|
|
| 92 |
};
|
| 93 |
|
| 94 |
// 2 register args, 0 stack params, plain ret
|
| 95 |
-
// Push: reg2, reg1 → impl_fn(reg1, reg2)
|
| 96 |
(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident]) => {
|
| 97 |
#[unsafe(naked)]
|
| 98 |
unsafe extern "C" fn $name() {
|
| 99 |
core::arch::naked_asm!(
|
|
|
|
| 100 |
concat!("push ", stringify!($r2)),
|
| 101 |
concat!("push ", stringify!($r1)),
|
| 102 |
"call {impl_fn}",
|
| 103 |
"add esp, 8",
|
|
|
|
| 104 |
"ret",
|
| 105 |
impl_fn = sym $impl,
|
| 106 |
);
|
|
@@ -108,20 +114,20 @@ macro_rules! usercall_trampoline {
|
|
| 108 |
};
|
| 109 |
|
| 110 |
// 2 register args, 3 stack params, ret N
|
| 111 |
-
// Stack layout at entry: [ESP+0]=retaddr, [ESP+4]=arg1, [ESP+8]=arg2, [ESP+C]=arg3
|
| 112 |
-
// Push: stack_arg3, stack_arg2, stack_arg1, reg2, reg1 → impl_fn(reg1, reg2, stack_arg1, stack_arg2, stack_arg3)
|
| 113 |
(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident];
|
| 114 |
stack_params = 3; ret_bytes = $ret:literal) => {
|
| 115 |
#[unsafe(naked)]
|
| 116 |
unsafe extern "C" fn $name() {
|
| 117 |
core::arch::naked_asm!(
|
| 118 |
-
"push
|
| 119 |
-
"push [esp+
|
| 120 |
-
"push [esp+
|
|
|
|
| 121 |
concat!("push ", stringify!($r2)),
|
| 122 |
concat!("push ", stringify!($r1)),
|
| 123 |
"call {impl_fn}",
|
| 124 |
"add esp, 20",
|
|
|
|
| 125 |
concat!("ret ", $ret),
|
| 126 |
impl_fn = sym $impl,
|
| 127 |
);
|
|
@@ -129,16 +135,17 @@ macro_rules! usercall_trampoline {
|
|
| 129 |
};
|
| 130 |
|
| 131 |
// 3 register args, 0 stack params, plain ret
|
| 132 |
-
// Push: reg3, reg2, reg1 → impl_fn(reg1, reg2, reg3)
|
| 133 |
(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident, $r3:ident]) => {
|
| 134 |
#[unsafe(naked)]
|
| 135 |
unsafe extern "C" fn $name() {
|
| 136 |
core::arch::naked_asm!(
|
|
|
|
| 137 |
concat!("push ", stringify!($r3)),
|
| 138 |
concat!("push ", stringify!($r2)),
|
| 139 |
concat!("push ", stringify!($r1)),
|
| 140 |
"call {impl_fn}",
|
| 141 |
"add esp, 12",
|
|
|
|
| 142 |
"ret",
|
| 143 |
impl_fn = sym $impl,
|
| 144 |
);
|
|
|
|
| 23 |
/// ```
|
| 24 |
macro_rules! usercall_trampoline {
|
| 25 |
// 1 register arg, 0 stack params, plain ret
|
| 26 |
+
// EDX saved/restored: cdecl may clobber it, but original __usercall may not
|
| 27 |
(fn $name:ident; impl_fn = $impl:path; reg = $reg:ident) => {
|
| 28 |
#[unsafe(naked)]
|
| 29 |
unsafe extern "C" fn $name() {
|
| 30 |
core::arch::naked_asm!(
|
| 31 |
+
"push edx",
|
| 32 |
concat!("push ", stringify!($reg)),
|
| 33 |
"call {impl_fn}",
|
| 34 |
"add esp, 4",
|
| 35 |
+
"pop edx",
|
| 36 |
"ret",
|
| 37 |
impl_fn = sym $impl,
|
| 38 |
);
|
|
|
|
| 45 |
#[unsafe(naked)]
|
| 46 |
unsafe extern "C" fn $name() {
|
| 47 |
core::arch::naked_asm!(
|
| 48 |
+
"push edx",
|
| 49 |
+
"push [esp+8]",
|
| 50 |
concat!("push ", stringify!($reg)),
|
| 51 |
"call {impl_fn}",
|
| 52 |
"add esp, 8",
|
| 53 |
+
"pop edx",
|
| 54 |
concat!("ret ", $ret),
|
| 55 |
impl_fn = sym $impl,
|
| 56 |
);
|
|
|
|
| 58 |
};
|
| 59 |
|
| 60 |
// 2 register args, 1 stack param, ret N
|
|
|
|
|
|
|
| 61 |
(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident];
|
| 62 |
stack_params = 1; ret_bytes = $ret:literal) => {
|
| 63 |
#[unsafe(naked)]
|
| 64 |
unsafe extern "C" fn $name() {
|
| 65 |
core::arch::naked_asm!(
|
| 66 |
+
"push edx",
|
| 67 |
+
"push [esp+8]",
|
| 68 |
concat!("push ", stringify!($r2)),
|
| 69 |
concat!("push ", stringify!($r1)),
|
| 70 |
"call {impl_fn}",
|
| 71 |
"add esp, 12",
|
| 72 |
+
"pop edx",
|
| 73 |
concat!("ret ", $ret),
|
| 74 |
impl_fn = sym $impl,
|
| 75 |
);
|
|
|
|
| 77 |
};
|
| 78 |
|
| 79 |
// 2 register args, 2 stack params, ret N
|
|
|
|
|
|
|
| 80 |
(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident];
|
| 81 |
stack_params = 2; ret_bytes = $ret:literal) => {
|
| 82 |
#[unsafe(naked)]
|
| 83 |
unsafe extern "C" fn $name() {
|
| 84 |
core::arch::naked_asm!(
|
| 85 |
+
"push edx",
|
| 86 |
+
"push [esp+12]",
|
| 87 |
+
"push [esp+12]",
|
| 88 |
concat!("push ", stringify!($r2)),
|
| 89 |
concat!("push ", stringify!($r1)),
|
| 90 |
"call {impl_fn}",
|
| 91 |
"add esp, 16",
|
| 92 |
+
"pop edx",
|
| 93 |
concat!("ret ", $ret),
|
| 94 |
impl_fn = sym $impl,
|
| 95 |
);
|
|
|
|
| 97 |
};
|
| 98 |
|
| 99 |
// 2 register args, 0 stack params, plain ret
|
|
|
|
| 100 |
(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident]) => {
|
| 101 |
#[unsafe(naked)]
|
| 102 |
unsafe extern "C" fn $name() {
|
| 103 |
core::arch::naked_asm!(
|
| 104 |
+
"push edx",
|
| 105 |
concat!("push ", stringify!($r2)),
|
| 106 |
concat!("push ", stringify!($r1)),
|
| 107 |
"call {impl_fn}",
|
| 108 |
"add esp, 8",
|
| 109 |
+
"pop edx",
|
| 110 |
"ret",
|
| 111 |
impl_fn = sym $impl,
|
| 112 |
);
|
|
|
|
| 114 |
};
|
| 115 |
|
| 116 |
// 2 register args, 3 stack params, ret N
|
|
|
|
|
|
|
| 117 |
(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident];
|
| 118 |
stack_params = 3; ret_bytes = $ret:literal) => {
|
| 119 |
#[unsafe(naked)]
|
| 120 |
unsafe extern "C" fn $name() {
|
| 121 |
core::arch::naked_asm!(
|
| 122 |
+
"push edx",
|
| 123 |
+
"push [esp+16]",
|
| 124 |
+
"push [esp+16]",
|
| 125 |
+
"push [esp+16]",
|
| 126 |
concat!("push ", stringify!($r2)),
|
| 127 |
concat!("push ", stringify!($r1)),
|
| 128 |
"call {impl_fn}",
|
| 129 |
"add esp, 20",
|
| 130 |
+
"pop edx",
|
| 131 |
concat!("ret ", $ret),
|
| 132 |
impl_fn = sym $impl,
|
| 133 |
);
|
|
|
|
| 135 |
};
|
| 136 |
|
| 137 |
// 3 register args, 0 stack params, plain ret
|
|
|
|
| 138 |
(fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident, $r3:ident]) => {
|
| 139 |
#[unsafe(naked)]
|
| 140 |
unsafe extern "C" fn $name() {
|
| 141 |
core::arch::naked_asm!(
|
| 142 |
+
"push edx",
|
| 143 |
concat!("push ", stringify!($r3)),
|
| 144 |
concat!("push ", stringify!($r2)),
|
| 145 |
concat!("push ", stringify!($r1)),
|
| 146 |
"call {impl_fn}",
|
| 147 |
"add esp, 12",
|
| 148 |
+
"pop edx",
|
| 149 |
"ret",
|
| 150 |
impl_fn = sym $impl,
|
| 151 |
);
|