client/tests/ace_token.c (7666B)
1 #define _GNU_SOURCE 2 #include "recomp_types.h" 3 #include "guest_memory.h" 4 #include <assert.h> 5 #ifdef _WIN32 6 #define WIN32_LEAN_AND_MEAN 7 #include <windows.h> 8 static HMODULE library; 9 10 /* Inspect the native ABI as well as return values and object contents. */ 11 static volatile uint32_t native_ebx, native_esi, native_edi, native_ebp; 12 static volatile uint32_t native_frame, native_stack, native_expected_stack; 13 __attribute__((naked)) static uint32_t native_call(FARPROC function, uint32_t object, 14 unsigned count, uint32_t a, uint32_t b) 15 { 16 __asm__( 17 "pushl %ebp\n movl %esp,%ebp\n pushl %ebx\n pushl %esi\n pushl %edi\n" 18 "movl %ebp,_native_frame\n movl %esp,_native_expected_stack\n" 19 "movl $0x12345678,%ebx\n movl $0x23456789,%esi\n movl $0x3456789a,%edi\n" 20 "cmpl $2,16(%ebp)\n jne 1f\n pushl 24(%ebp)\n" 21 "1: cmpl $0,16(%ebp)\n je 2f\n pushl 20(%ebp)\n" 22 "2: movl 12(%ebp),%ecx\n calll *8(%ebp)\n" 23 "movl %ebx,_native_ebx\n movl %esi,_native_esi\n movl %edi,_native_edi\n" 24 "movl %ebp,_native_ebp\n movl %esp,_native_stack\n" 25 "movl _native_frame,%ebp\n leal -12(%ebp),%esp\n" 26 "popl %edi\n popl %esi\n popl %ebx\n popl %ebp\n retl\n"); 27 } 28 #else 29 #include "linux_platform.h" 30 #include <unistd.h> 31 32 void ace_data_init(void); 33 void sub_4708FA90(void); 34 void sub_47087E50(void); 35 void sub_47057A60(void); 36 void sub_4708FB70(void); 37 void sub_47087E30(void); 38 void sub_470913B0(void); 39 void sub_4708FF80(void); 40 void sub_4708FBD0(void); 41 void sub_470559D0(void); 42 void sub_47090300(void); 43 44 recomp_func_t recomp_lookup_manual(uint32_t address) { (void)address; return NULL; } 45 46 recomp_func_t recomp_lookup(uint32_t address) 47 { 48 switch (address) { 49 case 0x4708fa90u: return sub_4708FA90; 50 case 0x47087e50u: return sub_47087E50; 51 case 0x47057a60u: return sub_47057A60; 52 case 0x4708fb70u: return sub_4708FB70; 53 case 0x47087e30u: return sub_47087E30; 54 case 0x470913b0u: return sub_470913B0; 55 case 0x4708ff80u: return sub_4708FF80; 56 case 0x4708fbd0u: return sub_4708FBD0; 57 case 0x470559d0u: return sub_470559D0; 58 case 0x47090300u: return sub_47090300; 59 default: return NULL; 60 } 61 } 62 63 static const struct { uint32_t iat; const char *name; } imports[] = { 64 {0x470ac1a4u, "InitializeCriticalSection"}, 65 {0x470ac19cu, "DeleteCriticalSection"}, 66 {0x470ac180u, "EnterCriticalSection"}, 67 {0x470ac190u, "LeaveCriticalSection"}, 68 {0x470ac14cu, "GetCurrentThreadId"}, 69 }; 70 71 recomp_func_t recomp_lookup_import(uint32_t address) 72 { 73 for (unsigned i = 0; i < sizeof(imports) / sizeof(imports[0]); ++i) { 74 if (address != 0xf0000000u + i * 4) continue; 75 recomp_func_t function = linux_platform_lookup("kernel32.dll", imports[i].name); 76 assert(function); 77 return function; 78 } 79 fprintf(stderr, "Unexpected ACE token dependency %08x from %08x\n", address, g_cur_func); 80 abort(); 81 } 82 #endif 83 84 static uint32_t module_base, null_thread, current_thread; 85 static uint32_t digest = 2166136261u; 86 static unsigned snapshots; 87 88 static uint32_t call(const char *name, uint32_t address, unsigned count, uint32_t a, uint32_t b) 89 { 90 #ifdef _WIN32 91 FARPROC function = GetProcAddress(library, name); 92 assert(function); 93 uint32_t result = native_call(function, 0x50001000u, count, a, b); 94 assert(native_ebx == 0x12345678u && native_esi == 0x23456789u); 95 assert(native_edi == 0x3456789au && native_ebp == native_frame); 96 assert(native_stack == native_expected_stack); 97 return result; 98 #else 99 (void)name; 100 g_esp = 0x5000ff00u; 101 g_ecx = 0x50001000u; 102 g_ebx = 0x12345678u; 103 g_esi = 0x23456789u; 104 g_edi = 0x3456789au; 105 g_ebp = 0x456789abu; 106 MEM32(g_esp) = RECOMP_RETADDR; 107 MEM32(g_esp + 4) = a; 108 MEM32(g_esp + 8) = b; 109 recomp_func_t function = recomp_lookup(address); 110 assert(function); 111 function(); 112 assert(g_esp == 0x5000ff04u + count * 4); 113 assert(g_ebx == 0x12345678u && g_esi == 0x23456789u); 114 assert(g_edi == 0x3456789au && g_ebp == 0x456789abu); 115 assert(MEM32(g_fs_base) == UINT32_MAX); 116 return g_eax; 117 #endif 118 } 119 120 static void hash(uint32_t value) { digest = (digest ^ value) * 16777619u; } 121 122 static void snapshot(unsigned depth, int destroyed) 123 { 124 assert(MEM32(0x50000ffcu) == 0xaaaaaaaa); 125 assert(MEM32(0x50001048u) == 0xbbbbbbbb); 126 uint32_t vtable = MEM32(0x50001000u); 127 assert(vtable - module_base == 0xafdc0u); 128 hash(vtable - module_base); 129 hash(MEM32(vtable) - module_base); 130 hash(MEM32(vtable + 4) - module_base); 131 for (uint32_t offset = 4; offset <= 16; offset += 4) { 132 assert(MEM32(0x50001000u + offset) == 0); 133 hash(MEM32(0x50001000u + offset)); 134 } 135 136 /* Host-specific critical-section bookkeeping is opaque; ownership is not. */ 137 if (!destroyed) { 138 assert(MEM32(0x5000101cu) == 0); 139 assert(MEM32(0x50001020u) == 0); 140 } 141 assert(MEM32(0x5000102cu) == (uint32_t)destroyed); 142 assert(MEM32(0x50001030u) == (depth ? current_thread : null_thread)); 143 assert(MEM32(0x50001034u) == (depth ? 2u : 0u)); 144 assert(MEM32(0x50001038u) == 0); 145 assert(MEM32(0x5000103cu) == (depth ? depth - 1 : 0)); 146 assert(MEM32(0x50001040u) == 0); 147 assert(MEM32(0x50001044u) == 1); 148 hash(MEM32(0x5000102cu)); 149 hash(MEM32(0x50001030u) == current_thread); 150 for (uint32_t offset = 0x34; offset < 0x48; offset += 4) hash(MEM32(0x50001000u + offset)); 151 ++snapshots; 152 } 153 154 static void recursive_token_lifecycle(void) 155 { 156 for (unsigned cycle = 0; cycle < 256; ++cycle) { 157 memset((void *)ADDR(0x50001000u), 0xcc, 0x48); 158 MEM32(0x50000ffcu) = 0xaaaaaaaa; 159 MEM32(0x50001048u) = 0xbbbbbbbb; 160 unsigned depth = cycle % 16 + 1; 161 uint32_t result = call("??0ACE_Token@@QAE@PBDPAX@Z", 0x4708fa90u, 2, 0, 0); 162 assert(result == 0x50001000u); 163 snapshot(0, 0); 164 165 for (unsigned i = 1; i <= depth; ++i) { 166 /* A zero deadline still allows immediate and recursive acquisition. */ 167 result = call("?acquire@ACE_Token@@QAEHPAVACE_Time_Value@@@Z", 0x4708ff80u, 168 1, cycle & 1 ? 0x50002000u : 0, 0); 169 assert(result == 0); 170 hash(result); 171 snapshot(i, 0); 172 } 173 for (unsigned i = depth; i; --i) { 174 result = call("?release@ACE_Token@@QAEHXZ", 0x47090300u, 0, 0, 0); 175 assert(result == 0); 176 hash(result); 177 snapshot(i - 1, 0); 178 } 179 call("??1ACE_Token@@UAE@XZ", 0x4708fb70u, 0, 0, 0); 180 181 snapshot(0, 1); 182 } 183 } 184 185 int main(int argc, char **argv) 186 { 187 assert(guest_map("ACE token test memory", 0x50000000u, 0x10000u)); 188 #ifdef _WIN32 189 assert(argc == 2); 190 library = LoadLibraryExA(argv[1], NULL, LOAD_WITH_ALTERED_SEARCH_PATH); 191 assert(library); 192 module_base = (uint32_t)(uintptr_t)library; 193 uint32_t *null_thread_export = (uint32_t *)GetProcAddress(library, "?NULL_thread@ACE_OS@@2KA"); 194 assert(null_thread_export); 195 null_thread = *null_thread_export; 196 current_thread = GetCurrentThreadId(); 197 #else 198 (void)argc; 199 (void)argv; 200 ace_data_init(); 201 module_base = 0x47000000u; 202 null_thread = MEM32(0x4710e900u); 203 current_thread = (uint32_t)gettid(); 204 g_fs_base = 0x50008000u; 205 MEM32(g_fs_base) = UINT32_MAX; 206 for (unsigned i = 0; i < sizeof(imports) / sizeof(imports[0]); ++i) 207 MEM32(imports[i].iat) = 0xf0000000u + i * 4; 208 #endif 209 assert(current_thread != null_thread); 210 recursive_token_lifecycle(); 211 assert(snapshots == 4864); 212 assert(digest == 0x4cecc1c5u); 213 printf("ACE token: %u snapshots, digest %08x\n", snapshots, digest); 214 return 0; 215 }