client/tests/threads.c (18801B)
1 #include <assert.h> 2 #include <stdint.h> 3 #include <stdio.h> 4 #ifdef _WIN32 5 #define WIN32_LEAN_AND_MEAN 6 #include <windows.h> 7 static uintptr_t (__cdecl *native_begin)(void *, unsigned, unsigned (__stdcall *)(void *), void *, unsigned, unsigned *); 8 static void (__cdecl *native_end)(unsigned); 9 static uintptr_t (__cdecl *native_begin_automatic)(void (__cdecl *)(void *), unsigned, void *); 10 static void (__cdecl *native_end_automatic)(void); 11 static int *(__cdecl *native_errno)(void); 12 static CRITICAL_SECTION counter_lock; 13 static unsigned counter; 14 #else 15 #include "linux_thread.h" 16 #include "linux_tls.h" 17 #include "linux_sync.h" 18 #include "linux_platform.h" 19 #include "guest_heap.h" 20 #include "guest_crt.h" 21 #include "guest_exception.h" 22 #include <pthread.h> 23 #include <stdatomic.h> 24 static pthread_barrier_t cpu_barrier; 25 static _Atomic unsigned attached, detached; 26 static _Atomic unsigned timer_calls, timer_id, timer_user; 27 static _Thread_local int notified; 28 void sub_42001000(void); 29 void sub_42001100(void); 30 void native32_shim_CxxThrowException(void); 31 uint32_t recomp_funcinfo(uint32_t handler) { assert(handler == 0x42002000); return 0x42003000; } 32 recomp_func_t recomp_lookup_manual(uint32_t address) { (void)address; return NULL; } 33 34 static void throw_and_catch(void) 35 { 36 uint32_t stack = g_esp, frame = g_ebp, fs = MEM32(g_fs_base); 37 PUSH32(g_esp, RECOMP_RETADDR); 38 sub_42001000(); 39 assert(g_eax == 42 && g_esp == stack && g_ebp == frame && MEM32(g_fs_base) == fs); 40 } 41 42 static uint32_t call(recomp_func_t function, unsigned count, const uint32_t *args, int cdecl) 43 { 44 uint32_t stack = g_esp; 45 g_esp -= 0x100; 46 MEM32(g_esp) = RECOMP_RETADDR; 47 for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = args[i]; 48 function(); 49 assert(g_esp == stack - 0x100 + (cdecl ? 4 : (count + 1) * 4)); 50 g_esp = stack; 51 return g_eax; 52 } 53 54 void linux_thread_notify(uint32_t reason) 55 { 56 assert(MEM32(g_fs_base + 0x18) == g_fs_base); 57 assert(MEM32(g_fs_base) == UINT32_MAX); 58 assert(g_esp > MEM32(g_fs_base + 8) && g_esp < MEM32(g_fs_base + 4)); 59 assert(MEM32(g_fs_base + 4) - MEM32(g_fs_base + 8) == 0x100000u); 60 if (reason == 2) { 61 assert(!notified && MEM32(MEM32(g_fs_base + 8)) == 0); 62 MEM32(MEM32(g_fs_base + 8)) = 0xfeedcafe; 63 notified = 1; 64 ++attached; 65 } 66 else { assert(reason == 3 && notified); notified = 0; ++detached; } 67 throw_and_catch(); 68 g_eax = 0xdeadbeef; /* Notifications must not replace a thread's exit code. */ 69 } 70 71 recomp_func_t recomp_lookup_import(uint32_t address) 72 { 73 fprintf(stderr, "Unexpected thread-test import %08x\n", address); 74 abort(); 75 } 76 #endif 77 78 static uint32_t tls_index, ready, gate, done; 79 static uint32_t observed_ids[4]; 80 81 static void increment_counter(void) 82 { 83 #ifdef _WIN32 84 EnterCriticalSection(&counter_lock); 85 ++counter; 86 LeaveCriticalSection(&counter_lock); 87 #else 88 uint32_t section = 0x50002000; 89 call(linux_platform_lookup("kernel32.dll", "EnterCriticalSection"), 1, §ion, 0); 90 ++MEM32(0x50002100); 91 call(linux_platform_lookup("kernel32.dll", "LeaveCriticalSection"), 1, §ion, 0); 92 #endif 93 } 94 95 static uint32_t tls_get(void) 96 { 97 #ifdef _WIN32 98 return (uintptr_t)TlsGetValue(tls_index); 99 #else 100 return call(linux_tls_lookup("TlsGetValue"), 1, &tls_index, 0); 101 #endif 102 } 103 104 static void tls_set(uint32_t value) 105 { 106 #ifdef _WIN32 107 assert(TlsSetValue(tls_index, (void *)(uintptr_t)value)); 108 #else 109 uint32_t args[] = {tls_index, value}; 110 assert(call(linux_tls_lookup("TlsSetValue"), 2, args, 0)); 111 #endif 112 } 113 114 static uint32_t semaphore(void) 115 { 116 #ifdef _WIN32 117 return (uintptr_t)CreateSemaphoreA(NULL, 0, 4, NULL); 118 #else 119 uint32_t args[] = {0, 0, 4, 0}; 120 return call(linux_sync_lookup("CreateSemaphoreA"), 4, args, 0); 121 #endif 122 } 123 124 static void signal_semaphore(uint32_t handle, uint32_t count) 125 { 126 #ifdef _WIN32 127 assert(ReleaseSemaphore((HANDLE)(uintptr_t)handle, count, NULL)); 128 #else 129 uint32_t args[] = {handle, count, 0}; 130 assert(call(linux_sync_lookup("ReleaseSemaphore"), 3, args, 0)); 131 #endif 132 } 133 134 static uint32_t wait(uint32_t handle, uint32_t timeout, int thread) 135 { 136 #ifdef _WIN32 137 return WaitForSingleObject((HANDLE)(uintptr_t)handle, timeout); 138 #else 139 uint32_t args[] = {handle, timeout}; 140 return call(thread ? linux_thread_lookup("WaitForSingleObject") : linux_sync_lookup("WaitForSingleObject"), 2, args, 0); 141 #endif 142 } 143 144 static uint32_t close_handle(uint32_t handle, int thread) 145 { 146 #ifdef _WIN32 147 return CloseHandle((HANDLE)(uintptr_t)handle); 148 #else 149 return call(thread ? linux_thread_lookup("CloseHandle") : linux_sync_lookup("CloseHandle"), 1, &handle, 0); 150 #endif 151 } 152 153 static uint32_t exit_code(uint32_t handle) 154 { 155 #ifdef _WIN32 156 DWORD code; 157 assert(GetExitCodeThread((HANDLE)(uintptr_t)handle, &code)); 158 return code; 159 #else 160 uint32_t args[] = {handle, g_esp - 0x200}; 161 assert(call(linux_thread_lookup("GetExitCodeThread"), 2, args, 0)); 162 return MEM32(args[1]); 163 #endif 164 } 165 166 static uint32_t resume(uint32_t handle) 167 { 168 #ifdef _WIN32 169 return ResumeThread((HANDLE)(uintptr_t)handle); 170 #else 171 return call(linux_thread_lookup("ResumeThread"), 1, &handle, 0); 172 #endif 173 } 174 175 static unsigned worker(uint32_t tag) 176 { 177 assert(tls_get() == 0); 178 tls_set(0x12340000 + tag); 179 #ifdef _WIN32 180 observed_ids[tag] = GetCurrentThreadId(); 181 assert(*native_errno() == 0); 182 *native_errno() = 100 + tag; 183 #else 184 assert(notified); 185 observed_ids[tag] = call(linux_platform_lookup("kernel32.dll", "GetCurrentThreadId"), 0, NULL, 0); 186 assert(observed_ids[tag] == MEM32(g_fs_base + 0x24)); 187 assert(MEM32(guest_crt_errno_address()) == 0); 188 MEM32(guest_crt_errno_address()) = 100 + tag; 189 #endif 190 if (tag) { 191 signal_semaphore(ready, 1); 192 assert(wait(gate, 10000, 0) == 0); 193 } 194 195 #ifndef _WIN32 196 if (tag == 1 || tag == 2) { 197 /* Both guest CPUs must retain every register while the other executes. 198 * Allocation, reallocation and freeing also contend on the shared heap. */ 199 for (unsigned i = 0; i < 128; ++i) { 200 g_eax = tag; g_ecx = tag + 1; g_edx = tag + 2; g_ebx = tag + 3; 201 g_esi = tag + 4; g_edi = tag + 5; g_ebp = tag + 6; 202 g_flag_k = tag; g_flag_a = tag + 7; g_flag_b = tag + 8; g_flag_cf = tag & 1; 203 g_fp_top = tag; g_st[tag] = tag + 0.5; g_fpu_cw = 0x27f + tag * 0x400; 204 g_mm[0] = 0x1234567800000000ull + tag; 205 memset(g_xmm, tag, sizeof(g_xmm)); 206 g_mxcsr = 0x1f80 + tag * 0x2000; 207 g_seg_fs = tag; g_gs_base = tag + 9; g_cur_func = tag + 10; 208 g_icall_trace[0] = tag + 11; g_icall_from[0] = tag + 12; 209 g_icall_trace_idx = tag + 13; g_icall_count = tag + 14; 210 g_st_i64[0] = 0x123456789abcdef0ll + tag; 211 g_backedges = tag + 15; 212 uint32_t stack = g_esp, teb = g_fs_base; 213 MEM32(stack - 4) = tag; 214 int barrier = pthread_barrier_wait(&cpu_barrier); 215 assert(!barrier || barrier == PTHREAD_BARRIER_SERIAL_THREAD); 216 217 uint32_t memory = guest_malloc(31 + i); 218 assert(memory); 219 memset((void *)ADDR(memory), tag, 31 + i); 220 memory = guest_realloc(memory, 4096 + i); 221 assert(memory && MEM8(memory) == tag && MEM8(memory + 30 + i) == tag); 222 guest_free(memory); 223 assert(g_eax == tag && g_ecx == tag + 1 && g_edx == tag + 2 && g_ebx == tag + 3); 224 assert(g_esi == tag + 4 && g_edi == tag + 5 && g_ebp == tag + 6); 225 assert(g_flag_k == tag && g_flag_a == tag + 7 && g_flag_b == tag + 8 && g_flag_cf == (tag & 1)); 226 assert(g_fp_top == tag && g_st[tag] == tag + 0.5 && g_fpu_cw == 0x27f + tag * 0x400); 227 assert(g_mm[0] == 0x1234567800000000ull + tag && ((unsigned char *)g_xmm)[127] == tag); 228 assert(g_mxcsr == 0x1f80 + tag * 0x2000 && g_seg_fs == tag && g_gs_base == tag + 9); 229 assert(g_cur_func == tag + 10 && g_icall_trace[0] == tag + 11 && g_icall_from[0] == tag + 12); 230 assert(g_icall_trace_idx == tag + 13 && g_icall_count == tag + 14); 231 assert(g_st_i64[0] == 0x123456789abcdef0ll + tag && g_backedges == tag + 15); 232 assert(g_esp == stack && g_fs_base == teb && MEM32(stack - 4) == tag); 233 increment_counter(); 234 throw_and_catch(); 235 uint32_t object = guest_malloc(12); 236 assert(object); 237 g_ecx = object; 238 call(guest_exception_lookup("??0exception@@QAE@XZ"), 0, NULL, 0); 239 uint32_t message = call(guest_exception_lookup("?what@exception@@UBEPBDXZ"), 0, NULL, 0); 240 assert(!strcmp((const char *)ADDR(message), "Unknown exception")); 241 call(guest_exception_lookup("??1exception@@UAE@XZ"), 0, NULL, 0); 242 guest_free(object); 243 barrier = pthread_barrier_wait(&cpu_barrier); 244 assert(!barrier || barrier == PTHREAD_BARRIER_SERIAL_THREAD); 245 } 246 } 247 assert(MEM32(guest_crt_errno_address()) == 100 + tag); 248 #else 249 if (tag == 1 || tag == 2) 250 for (unsigned i = 0; i < 128; ++i) increment_counter(); 251 assert(*native_errno() == 100 + (int)tag); 252 #endif 253 assert(tls_get() == 0x12340000 + tag); 254 if (tag == 3) signal_semaphore(done, 1); 255 return 0x12345600 + tag; 256 } 257 258 static void automatic_worker(uint32_t mode) 259 { 260 assert(tls_get() == 0); 261 tls_set(0x76543210); 262 if (mode != 2) { 263 signal_semaphore(ready, 1); 264 assert(wait(gate, 10000, 0) == 0); 265 } 266 assert(tls_get() == 0x76543210); 267 signal_semaphore(done, 1); 268 if (mode == 1) { 269 #ifdef _WIN32 270 native_end_automatic(); 271 #else 272 call(linux_thread_lookup("_endthread"), 0, NULL, 1); 273 #endif 274 abort(); 275 } 276 } 277 278 #ifdef _WIN32 279 static void __cdecl automatic_entry(void *argument) { automatic_worker((uintptr_t)argument); } 280 static unsigned __stdcall returning_entry(void *argument) { return worker((uintptr_t)argument); } 281 static unsigned __stdcall exiting_entry(void *argument) { native_end(worker((uintptr_t)argument)); abort(); } 282 #else 283 static void automatic_entry(void) { automatic_worker(MEM32(g_esp + 4)); g_esp += 4; } 284 static void returning_entry(void) { g_eax = worker(MEM32(g_esp + 4)); g_esp += 8; } 285 static void exiting_entry(void) 286 { 287 uint32_t code = worker(MEM32(g_esp + 4)); 288 /* Model _endthreadex abandoning a generated C++ frame and its FS record. */ 289 recomp_ehframe_t abandoned; 290 recomp_eh_push(&abandoned); 291 MEM32(g_fs_base) = g_esp - 0x1000; 292 call(linux_thread_lookup("_endthreadex"), 1, &code, 1); 293 abort(); 294 } 295 static void timer_entry(void) 296 { 297 assert(notified && !tls_get()); 298 assert(!MEM32(g_esp + 8) && !MEM32(g_esp + 16) && !MEM32(g_esp + 20)); 299 timer_id = MEM32(g_esp + 4); 300 timer_user = MEM32(g_esp + 12); 301 ++timer_calls; 302 signal_semaphore(done, 1); 303 g_esp += 24; 304 } 305 recomp_func_t recomp_lookup(uint32_t address) 306 { 307 if (address == 0x11001000) return returning_entry; 308 if (address == 0x11002000) return exiting_entry; 309 if (address == 0x11003000) return automatic_entry; 310 if (address == 0x11004000) return timer_entry; 311 if (address == 0x42001000) return sub_42001000; 312 if (address == 0x42001100) return sub_42001100; 313 if (address == 0x42002000) return native32_shim_CxxThrowException; 314 return NULL; 315 } 316 #endif 317 318 static uint32_t begin(uint32_t tag, int suspended, int exits, uint32_t *id) 319 { 320 #ifdef _WIN32 321 return native_begin(NULL, 0, exits ? exiting_entry : returning_entry, (void *)(uintptr_t)tag, suspended ? 4 : 0, id); 322 #else 323 uint32_t args[] = {0, 0, exits ? 0x11002000 : 0x11001000, tag, suspended ? 4 : 0, g_esp - 0x200}; 324 uint32_t handle = call(linux_thread_lookup("_beginthreadex"), 6, args, 1); 325 *id = MEM32(args[5]); 326 return handle; 327 #endif 328 } 329 330 static void suspended_thread_returns_and_remains_signaled(void) 331 { 332 uint32_t id = 0; 333 uint32_t thread = begin(0, 1, 0, &id); 334 assert(thread && id && exit_code(thread) == 259); 335 336 assert(wait(thread, 0, 1) == 258); 337 assert(wait(thread, 10, 1) == 258); 338 assert(resume(thread) == 1); 339 assert(resume(thread) == 0); 340 assert(wait(thread, 10000, 1) == 0); 341 342 assert(id == observed_ids[0]); 343 assert(exit_code(thread) == 0x12345600); 344 assert(wait(thread, 0, 1) == 0 && wait(thread, 0, 1) == 0); 345 assert(close_handle(thread, 1)); 346 assert(!close_handle(thread, 1)); 347 assert(wait(thread, 0, 1) == UINT32_MAX); 348 } 349 350 static void concurrent_threads_keep_private_state(void) 351 { 352 uint32_t ids[2]; 353 uint32_t first = begin(1, 0, 0, &ids[0]); 354 uint32_t second = begin(2, 0, 1, &ids[1]); 355 assert(first && second && first != second && ids[0] != ids[1]); 356 assert(wait(ready, 10000, 0) == 0 && wait(ready, 10000, 0) == 0); 357 358 signal_semaphore(gate, 2); 359 assert(wait(first, 10000, 1) == 0); 360 assert(wait(second, 10000, 1) == 0); 361 362 assert(exit_code(first) == 0x12345601 && exit_code(second) == 0x12345602); 363 assert(ids[0] == observed_ids[1] && ids[1] == observed_ids[2]); 364 #ifdef _WIN32 365 assert(counter == 256); 366 #else 367 assert(MEM32(0x50002100) == 256); 368 #endif 369 assert(tls_get() == 0xabcdef01); 370 assert(close_handle(first, 1) && close_handle(second, 1)); 371 } 372 373 static void closing_a_running_thread_does_not_terminate_it(void) 374 { 375 uint32_t id; 376 uint32_t thread = begin(3, 0, 0, &id); 377 assert(thread && wait(ready, 10000, 0) == 0); 378 379 assert(close_handle(thread, 1)); 380 signal_semaphore(gate, 1); 381 382 assert(wait(done, 10000, 0) == 0); 383 assert(id == observed_ids[3]); 384 } 385 386 static void automatic_thread_closes_its_handle(uint32_t mode) 387 { 388 #ifdef _WIN32 389 uint32_t thread = native_begin_automatic(automatic_entry, 0, (void *)(uintptr_t)mode); 390 #else 391 uint32_t args[] = {0x11003000, 0, mode}; 392 uint32_t thread = call(linux_thread_lookup("_beginthread"), 3, args, 1); 393 #endif 394 assert(thread && thread != UINT32_MAX); 395 if (mode != 2) assert(wait(ready, 10000, 0) == 0 && exit_code(thread) == 259); 396 397 if (mode != 2) signal_semaphore(gate, 1); 398 assert(wait(done, 10000, 0) == 0); 399 400 /* The semaphore precedes CRT cleanup; wait for automatic handle invalidation. */ 401 int open = 1; 402 for (unsigned i = 0; open && i < 1000; ++i) { 403 #ifdef _WIN32 404 DWORD code; 405 open = GetExitCodeThread((HANDLE)(uintptr_t)thread, &code); 406 if (!open) assert(GetLastError() == 6); 407 else Sleep(1); 408 #else 409 uint32_t query[] = {thread, g_esp - 0x200}; 410 open = call(linux_thread_lookup("GetExitCodeThread"), 2, query, 0); 411 if (!open) assert(MEM32(g_fs_base + 0x34) == 6); 412 else { 413 uint32_t delay = 1; 414 call(linux_platform_lookup("kernel32.dll", "Sleep"), 1, &delay, 0); 415 } 416 #endif 417 } 418 assert(!open && tls_get() == 0xabcdef01); 419 } 420 421 #ifndef _WIN32 422 static void one_shot_timer_calls_guest_callback_once(void) 423 { 424 recomp_func_t set = linux_thread_lookup("timeSetEvent"), kill = linux_thread_lookup("timeKillEvent"); 425 assert(set && kill); 426 uint32_t arguments[] = {20, 0, 0x11004000, 0x12345678, 0}; 427 428 uint32_t timer = call(set, 5, arguments, 0); 429 assert(timer && !wait(done, 1000, 0)); 430 uint32_t delay = 50; 431 call(linux_platform_lookup("kernel32.dll", "Sleep"), 1, &delay, 0); 432 433 assert(timer_calls == 1 && timer_id == timer && timer_user == 0x12345678); 434 assert(tls_get() == 0xabcdef01); 435 assert(call(kill, 1, &timer, 0) == 11); 436 } 437 438 static void cancelled_timer_does_not_call_guest_callback(void) 439 { 440 uint32_t arguments[] = {100, 0, 0x11004000, 0xabcdef, 0}; 441 uint32_t timer = call(linux_thread_lookup("timeSetEvent"), 5, arguments, 0); 442 assert(timer); 443 444 uint32_t result = call(linux_thread_lookup("timeKillEvent"), 1, &timer, 0); 445 446 assert(!result && wait(done, 200, 0) == 258); 447 assert(timer_calls == 1); 448 assert(call(linux_thread_lookup("timeKillEvent"), 1, &timer, 0) == 11); 449 } 450 #endif 451 452 int main(void) 453 { 454 #ifdef _WIN32 455 HMODULE crt = LoadLibraryA("msvcrt.dll"); 456 assert(crt); 457 native_begin = (void *)GetProcAddress(crt, "_beginthreadex"); 458 native_end = (void *)GetProcAddress(crt, "_endthreadex"); 459 native_begin_automatic = (void *)GetProcAddress(crt, "_beginthread"); 460 native_end_automatic = (void *)GetProcAddress(crt, "_endthread"); 461 native_errno = (void *)GetProcAddress(crt, "_errno"); 462 assert(native_begin && native_end && native_begin_automatic && native_end_automatic && native_errno); 463 tls_index = TlsAlloc(); 464 InitializeCriticalSection(&counter_lock); 465 #else 466 assert(guest_map("thread test main stack and TEB", 0x50000000, 0x20000)); 467 g_esp = 0x5000ff00; 468 g_fs_base = 0x50010000; 469 /* The generated throw/catch fixture uses VC6 metadata, with no x86 code. */ 470 assert(guest_map("thread test EH metadata", 0x42000000, 0x10000)); 471 MEM32(0x42003000) = 0x19930520; 472 MEM32(0x42003004) = 1; MEM32(0x42003008) = 0x42003100; 473 MEM32(0x4200300c) = 1; MEM32(0x42003010) = 0x42003200; 474 MEM32(0x42003100) = UINT32_MAX; 475 MEM32(0x42003208) = 1; MEM32(0x4200320c) = 1; 476 MEM32(0x42003210) = 0x42003300; 477 MEM32(0x4200330c) = 0x42001100; 478 assert(!pthread_barrier_init(&cpu_barrier, NULL, 2)); 479 tls_index = call(linux_tls_lookup("TlsAlloc"), 0, NULL, 0); 480 uint32_t section = 0x50002000; 481 call(linux_platform_lookup("kernel32.dll", "InitializeCriticalSection"), 1, §ion, 0); 482 #endif 483 assert(tls_index != UINT32_MAX); 484 tls_set(0xabcdef01); 485 ready = semaphore(); gate = semaphore(); done = semaphore(); 486 assert(ready && gate && done); 487 suspended_thread_returns_and_remains_signaled(); 488 concurrent_threads_keep_private_state(); 489 closing_a_running_thread_does_not_terminate_it(); 490 automatic_thread_closes_its_handle(0); 491 automatic_thread_closes_its_handle(1); 492 for (unsigned i = 0; i < 32; ++i) automatic_thread_closes_its_handle(2); 493 #ifndef _WIN32 494 /* The closed thread signals done before its detach callback completes. */ 495 for (unsigned i = 0; atomic_load(&detached) < 38 && i < 1000; ++i) { 496 uint32_t delay = 1; 497 call(linux_platform_lookup("kernel32.dll", "Sleep"), 1, &delay, 0); 498 } 499 assert(attached == 38 && detached == 38); 500 one_shot_timer_calls_guest_callback_once(); 501 cancelled_timer_does_not_call_guest_callback(); 502 assert(!pthread_barrier_destroy(&cpu_barrier)); 503 assert(call(linux_tls_lookup("TlsFree"), 1, &tls_index, 0)); 504 call(linux_platform_lookup("kernel32.dll", "DeleteCriticalSection"), 1, §ion, 0); 505 #else 506 assert(TlsFree(tls_index)); 507 DeleteCriticalSection(&counter_lock); 508 #endif 509 assert(close_handle(ready, 0) && close_handle(gate, 0) && close_handle(done, 0)); 510 puts("Threads: concurrent execution, private TLS/errno, IDs, suspended start, waits, exit codes and handle lifetime passed"); 511 puts("_beginthread: cdecl arguments, returned/explicit exit, automatic handle close and 32 immediate exits passed"); 512 return 0; 513 }