client/tests/guest_rand.c (4930B)
1 #include <assert.h> 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #ifdef _WIN32 6 #define WIN32_LEAN_AND_MEAN 7 #include <windows.h> 8 static FARPROC native_rand, native_srand; 9 static volatile uint32_t native_ebx, native_esi, native_edi, native_ebp; 10 static volatile uint32_t native_frame, native_stack, native_expected_stack; 11 12 /* Capture the real DLL's cdecl stack and callee-saved register behavior. */ 13 __attribute__((naked)) static uint32_t native_call(FARPROC function, unsigned count, uint32_t argument) 14 { 15 __asm__( 16 "pushl %ebp\n movl %esp,%ebp\n pushl %ebx\n pushl %esi\n pushl %edi\n" 17 "movl %ebp,_native_frame\n" 18 "movl $0x12345678,%ebx\n movl $0x23456789,%esi\n movl $0x3456789a,%edi\n" 19 "cmpl $0,12(%ebp)\n je 1f\n pushl 16(%ebp)\n" 20 "1: movl %esp,_native_expected_stack\n calll *8(%ebp)\n" 21 "movl %ebx,_native_ebx\n movl %esi,_native_esi\n movl %edi,_native_edi\n" 22 "movl %ebp,_native_ebp\n movl %esp,_native_stack\n" 23 "movl _native_frame,%ebp\n leal -12(%ebp),%esp\n" 24 "popl %edi\n popl %esi\n popl %ebx\n popl %ebp\n retl\n"); 25 } 26 #else 27 #include "guest_crt.h" 28 #include "guest_memory.h" 29 #include <pthread.h> 30 31 recomp_func_t recomp_lookup(uint32_t address) 32 { 33 fprintf(stderr, "Unexpected random-test guest call %08x\n", address); 34 abort(); 35 } 36 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } 37 #endif 38 39 static uint32_t digest = 2166136261u; 40 static unsigned samples; 41 42 static uint32_t call(int seed, uint32_t value) 43 { 44 #ifdef _WIN32 45 uint32_t result = native_call(seed ? native_srand : native_rand, seed, value); 46 assert(native_stack == native_expected_stack); 47 assert(native_ebp == native_frame && native_ebx == 0x12345678u); 48 assert(native_esi == 0x23456789u && native_edi == 0x3456789au); 49 return result; 50 #else 51 g_esp = 0x5000ff00u; 52 MEM32(g_esp) = RECOMP_RETADDR; 53 MEM32(g_esp + 4) = value; 54 g_ebx = 0x12345678u; 55 g_esi = 0x23456789u; 56 g_edi = 0x3456789au; 57 g_ebp = 0x456789abu; 58 recomp_func_t function = guest_crt_lookup(seed ? "srand" : "rand"); 59 assert(function); 60 function(); 61 assert(g_esp == 0x5000ff04u); 62 assert(MEM32(g_esp) == value); 63 assert(g_ebx == 0x12345678u && g_esi == 0x23456789u); 64 assert(g_edi == 0x3456789au && g_ebp == 0x456789abu); 65 return g_eax; 66 #endif 67 } 68 69 static uint32_t next_random(void) 70 { 71 uint32_t value = call(0, 0); 72 assert(value <= 32767); 73 digest = (digest ^ value) * 16777619u; 74 ++samples; 75 return value; 76 } 77 78 static void default_sequence(void) 79 { 80 const uint32_t expected[] = {41, 18467, 6334, 26500, 19169}; 81 uint32_t actual[5]; 82 83 for (unsigned i = 0; i < 5; ++i) actual[i] = next_random(); 84 85 for (unsigned i = 0; i < 5; ++i) assert(actual[i] == expected[i]); 86 } 87 88 static void long_sequences_repeat_after_reseeding(void) 89 { 90 const uint32_t seeds[] = {0, 1, UINT32_MAX, 0x80000000u, 0x12345678u}; 91 for (unsigned seed = 0; seed < 5; ++seed) { 92 uint32_t sequence_hashes[2] = {2166136261u, 2166136261u}; 93 94 for (unsigned pass = 0; pass < 2; ++pass) { 95 call(1, seeds[seed]); 96 for (unsigned i = 0; i < 65536; ++i) 97 sequence_hashes[pass] = (sequence_hashes[pass] ^ next_random()) * 16777619u; 98 } 99 100 assert(sequence_hashes[0] == sequence_hashes[1]); 101 } 102 } 103 104 #ifdef _WIN32 105 static DWORD WINAPI thread_sequence(void *unused) 106 #else 107 static void *thread_sequence(void *unused) 108 #endif 109 { 110 (void)unused; 111 default_sequence(); 112 call(1, 0x12345678u); 113 for (unsigned i = 0; i < 1024; ++i) next_random(); 114 return 0; 115 } 116 117 static void threads_have_independent_seeds(void) 118 { 119 call(1, 1); 120 assert(next_random() == 41); 121 122 /* Join before updating the shared sequence digest from the parent. */ 123 for (unsigned i = 0; i < 2; ++i) { 124 #ifdef _WIN32 125 HANDLE thread = CreateThread(NULL, 0, thread_sequence, NULL, 0, NULL); 126 assert(thread); 127 assert(WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0); 128 assert(CloseHandle(thread)); 129 #else 130 pthread_t thread; 131 assert(pthread_create(&thread, NULL, thread_sequence, NULL) == 0); 132 assert(pthread_join(thread, NULL) == 0); 133 #endif 134 } 135 136 assert(next_random() == 18467); 137 assert(next_random() == 6334); 138 } 139 140 int main(void) 141 { 142 #ifdef _WIN32 143 HMODULE library = LoadLibraryA("msvcrt.dll"); 144 assert(library); 145 native_rand = GetProcAddress(library, "rand"); 146 native_srand = GetProcAddress(library, "srand"); 147 assert(native_rand && native_srand); 148 #else 149 assert(guest_map("random test stack", 0x50000000u, 0x10000u)); 150 #endif 151 default_sequence(); 152 long_sequences_repeat_after_reseeding(); 153 threads_have_independent_seeds(); 154 assert(samples == 657426); 155 assert(digest == 0x0a542854u); 156 printf("MSVCRT rand: %u samples, digest %08x, reseeding and thread isolation passed\n", samples, digest); 157 return 0; 158 }