client/tests/linux_platform.c (7348B)
1 #include "linux_platform.h" 2 #include "guest_crt.h" 3 #include "guest_memory.h" 4 #include <assert.h> 5 #include <unistd.h> 6 7 void game_data_hr_dll(void); 8 void sub_32001000(void); 9 void sub_32001030(void); 10 void sub_32001110(void); 11 void sub_320011B0(void); 12 void sub_32001210(void); 13 14 recomp_func_t recomp_lookup_manual(uint32_t address) { (void)address; return NULL; } 15 recomp_func_t recomp_lookup(uint32_t address) 16 { 17 if (address == 0x32001110u) return sub_32001110; 18 if (address == 0x320011b0u) return sub_320011B0; 19 return NULL; 20 } 21 22 recomp_func_t recomp_lookup_import(uint32_t address) 23 { 24 if (address == 0xf0000000u) return linux_platform_lookup("WINMM.dll", "timeGetTime"); 25 if (address == 0xf0000004u) return guest_crt_lookup("_ftol"); 26 fprintf(stderr, "Unexpected timer import %08x\n", address); 27 abort(); 28 } 29 30 static void performance_clock_measures_sleep(void) 31 { 32 g_esp = 0x5000ff00u; 33 MEM32(g_esp + 4) = 0x50001000u; 34 MEM32(0x50001008u) = 0x12345678u; 35 linux_platform_lookup("KERNEL32.dll", "QueryPerformanceFrequency")(); 36 uint64_t frequency = MEM64(0x50001000u); 37 assert(frequency > 0); 38 assert(g_eax == 1 && g_esp == 0x5000ff08u); 39 g_esp = 0x5000ff00u; 40 linux_platform_lookup("KERNEL32.dll", "QueryPerformanceCounter")(); 41 uint64_t before = MEM64(0x50001000u); 42 43 g_esp = 0x5000ff00u; 44 MEM32(g_esp + 4) = 20; 45 linux_platform_lookup("KERNEL32.dll", "Sleep")(); 46 assert(g_esp == 0x5000ff08u); 47 g_esp = 0x5000ff00u; 48 MEM32(g_esp + 4) = 0x50001000u; 49 linux_platform_lookup("KERNEL32.dll", "QueryPerformanceCounter")(); 50 51 assert(MEM64(0x50001000u) - before >= frequency / 50); 52 assert(MEM32(0x50001008u) == 0x12345678u); 53 assert(g_eax == 1 && g_esp == 0x5000ff08u); 54 } 55 56 static void game_delta_timer_uses_linux_clock(void) 57 { 58 game_data_hr_dll(); 59 MEM32(0x32002020u) = 0xf0000000u; 60 MEM32(0x32002014u) = 0xf0000004u; 61 g_esp = 0x5000ff00u; 62 g_ecx = 0x50002000u; 63 sub_32001000(); 64 assert(g_eax == 0x50002000u && g_esp == 0x5000ff04u); 65 66 g_esp = 0x5000ff00u; 67 MEM32(g_esp + 4) = 20; 68 linux_platform_lookup("KERNEL32.dll", "Sleep")(); 69 g_esp = 0x5000ff00u; 70 g_ecx = 0x50002000u; 71 MEM32(g_esp + 4) = 0x50002010u; 72 sub_32001030(); 73 assert(g_eax == 0x50002010u && g_esp == 0x5000ff08u); 74 g_esp = 0x5000ff00u; 75 g_ecx = 0x50002010u; 76 sub_32001210(); 77 78 assert(g_eax >= 15); 79 assert(g_edx == 0 && g_esp == 0x5000ff04u); 80 assert(*(float *)ADDR(0x50002010u) >= 0.015f); 81 } 82 83 static void last_error_uses_guest_thread_state(void) 84 { 85 g_fs_base = 0x50003000u; 86 g_esp = 0x5000ff00u; 87 MEM32(g_esp + 4) = 123; 88 89 linux_platform_lookup("KERNEL32.dll", "SetLastError")(); 90 linux_platform_lookup("KERNEL32.dll", "GetLastError")(); 91 92 assert(g_eax == 123 && MEM32(g_fs_base + 0x34) == 123); 93 assert(g_esp == 0x5000ff0cu); 94 } 95 96 static void critical_sections_allow_recursive_entry(void) 97 { 98 g_esp = 0x5000ff00u; 99 MEM32(g_esp + 4) = 0x50004000u; 100 linux_platform_lookup("KERNEL32.dll", "InitializeCriticalSection")(); 101 102 g_esp = 0x5000ff00u; 103 linux_platform_lookup("KERNEL32.dll", "EnterCriticalSection")(); 104 g_esp = 0x5000ff00u; 105 linux_platform_lookup("KERNEL32.dll", "EnterCriticalSection")(); 106 assert(MEM32(0x50004008u) == 2); 107 g_esp = 0x5000ff00u; 108 linux_platform_lookup("KERNEL32.dll", "GetCurrentThreadId")(); 109 assert(g_eax != 0 && g_eax == MEM32(0x5000400cu)); 110 assert(g_esp == 0x5000ff04u); 111 g_esp = 0x5000ff00u; 112 linux_platform_lookup("KERNEL32.dll", "LeaveCriticalSection")(); 113 g_esp = 0x5000ff00u; 114 linux_platform_lookup("KERNEL32.dll", "LeaveCriticalSection")(); 115 116 assert(MEM32(0x50004004u) == UINT32_MAX); 117 assert(MEM32(0x50004008u) == 0 && MEM32(0x5000400cu) == 0); 118 assert(g_esp == 0x5000ff08u); 119 g_esp = 0x5000ff00u; 120 linux_platform_lookup("KERNEL32.dll", "DeleteCriticalSection")(); 121 } 122 123 static void interlocked_operations_preserve_32_bit_results(void) 124 { 125 MEM32(0x50004000u) = 123; 126 g_esp = 0x5000ff00u; 127 MEM32(g_esp + 4) = 0x50004000u; 128 MEM32(g_esp + 8) = UINT32_MAX; 129 130 linux_platform_lookup("KERNEL32.dll", "InterlockedExchange")(); 131 assert(g_eax == 123 && g_esp == 0x5000ff0cu); 132 g_esp = 0x5000ff00u; 133 linux_platform_lookup("KERNEL32.dll", "InterlockedIncrement")(); 134 assert(g_eax == 0); 135 g_esp = 0x5000ff00u; 136 linux_platform_lookup("KERNEL32.dll", "InterlockedDecrement")(); 137 138 assert(g_eax == UINT32_MAX && MEM32(0x50004000u) == UINT32_MAX); 139 assert(g_esp == 0x5000ff08u); 140 } 141 142 static void startup_info_uses_32_bit_layout(void) 143 { 144 uint32_t info = 0x50005004u; 145 memset((void *)ADDR(info - 4), 0xa5, 76); 146 g_esp = 0x5000ff00u; 147 MEM32(g_esp + 4) = info; 148 149 linux_platform_lookup("KERNEL32.dll", "GetStartupInfoA")(); 150 151 assert(MEM32(info) == 68); 152 for (unsigned offset = 4; offset < 68; ++offset) assert(MEM8(info + offset) == 0); 153 assert(MEM32(info - 4) == 0xa5a5a5a5u && MEM32(info + 68) == 0xa5a5a5a5u); 154 assert(g_esp == 0x5000ff08u); 155 } 156 157 static void process_id_is_the_native_process(void) 158 { 159 g_esp = 0x5000ff00u; 160 MEM32(g_fs_base + 0x34) = 777; 161 162 linux_platform_lookup("KERNEL32.dll", "GetCurrentProcessId")(); 163 164 assert(g_eax == (uint32_t)getpid()); 165 assert(g_esp == 0x5000ff04u); 166 assert(MEM32(g_fs_base + 0x34) == 777); 167 } 168 169 static void exception_filter_registration_returns_previous_guest_callback(void) 170 { 171 g_esp = 0x5000ff00u; 172 MEM32(g_esp + 4) = 0x28001234; 173 MEM32(g_fs_base + 0x34) = 777; 174 175 linux_platform_lookup("KERNEL32.dll", "SetUnhandledExceptionFilter")(); 176 assert(g_eax == 0); 177 g_esp = 0x5000ff00u; 178 MEM32(g_esp + 4) = 0x28005678; 179 linux_platform_lookup("KERNEL32.dll", "SetUnhandledExceptionFilter")(); 180 assert(g_eax == 0x28001234); 181 g_esp = 0x5000ff00u; 182 MEM32(g_esp + 4) = 0; 183 linux_platform_lookup("KERNEL32.dll", "SetUnhandledExceptionFilter")(); 184 185 assert(g_eax == 0x28005678); 186 assert(g_esp == 0x5000ff08u); 187 assert(MEM32(g_fs_base + 0x34) == 777); 188 } 189 190 static void handle_count_preserves_the_requested_value(void) 191 { 192 const uint32_t counts[] = {0, 1, 20, 255, 512, 65536, UINT32_MAX}; 193 for (unsigned i = 0; i < sizeof(counts) / sizeof(counts[0]); ++i) { 194 g_esp = 0x5000ff00u; 195 MEM32(g_esp + 4) = counts[i]; 196 MEM32(g_fs_base + 0x34) = 777; 197 g_ebx = 0x12345678; 198 g_esi = 0x23456789; 199 g_edi = 0x3456789a; 200 g_ebp = 0x456789ab; 201 202 linux_platform_lookup("KERNEL32.dll", "SetHandleCount")(); 203 204 assert(g_eax == counts[i] && g_esp == 0x5000ff08u); 205 assert(MEM32(g_fs_base + 0x34) == 777); 206 assert(g_ebx == 0x12345678 && g_esi == 0x23456789); 207 assert(g_edi == 0x3456789a && g_ebp == 0x456789ab); 208 } 209 } 210 211 int main(void) 212 { 213 assert(guest_map("test memory and stack", 0x50000000u, 0x10000u)); 214 performance_clock_measures_sleep(); 215 game_delta_timer_uses_linux_clock(); 216 last_error_uses_guest_thread_state(); 217 critical_sections_allow_recursive_entry(); 218 interlocked_operations_preserve_32_bit_results(); 219 startup_info_uses_32_bit_layout(); 220 process_id_is_the_native_process(); 221 exception_filter_registration_returns_previous_guest_callback(); 222 handle_count_preserves_the_requested_value(); 223 puts("Linux platform: clocks, AO delta timer, last error, recursive locks and atomics passed"); 224 return 0; 225 }