client/tests/msvcp_stdio_data.c (6153B)
1 #include "recomp_types.h" 2 #include "guest_memory.h" 3 #include <assert.h> 4 #ifdef _WIN32 5 #define WIN32_LEAN_AND_MEAN 6 #include <windows.h> 7 static HMODULE library; 8 #else 9 #include "guest_crt.h" 10 11 void msvcp_data_init(void); 12 void sub_46001C27(void); 13 void sub_46001BE5(void); 14 void sub_4600E63B(void); 15 void sub_4600E65E(void); 16 void sub_4600E6EB(void); 17 void sub_4600294F(void); 18 void sub_4600297C(void); 19 20 recomp_func_t recomp_lookup_manual(uint32_t address) { (void)address; return NULL; } 21 22 recomp_func_t recomp_lookup(uint32_t address) 23 { 24 switch (address) { 25 case 0x46001c27u: return sub_46001C27; 26 case 0x46001be5u: return sub_46001BE5; 27 case 0x4600e63bu: return sub_4600E63B; 28 case 0x4600e65eu: return sub_4600E65E; 29 case 0x4600e6ebu: return sub_4600E6EB; 30 case 0x4600294fu: return sub_4600294F; 31 case 0x4600297cu: return sub_4600297C; 32 default: return NULL; 33 } 34 } 35 36 recomp_func_t recomp_lookup_import(uint32_t address) 37 { 38 fprintf(stderr, "Unexpected stdio-data dependency %08x from %08x\n", address, g_cur_func); 39 abort(); 40 } 41 #endif 42 43 #define FILEBUF "?$basic_filebuf@DU?$char_traits@D@std@@@std@@" 44 #define STREAMBUF "?$basic_streambuf@DU?$char_traits@D@std@@@std@@" 45 static uint32_t digest = 2166136261u; 46 47 static void hash(uint32_t value) { digest = (digest ^ value) * 16777619u; } 48 49 static uint32_t call(const char *name, uint32_t address, unsigned count, uint32_t a, uint32_t b) 50 { 51 #ifdef _WIN32 52 (void)address; 53 FARPROC function = GetProcAddress(library, name); 54 assert(function); 55 if (count == 2) { 56 ((void (__thiscall *)(void *, uint32_t, uint32_t))function)((void *)0x50001000u, a, b); 57 return 0; 58 } 59 if (count == 1) return ((uint32_t (__thiscall *)(void *, int))function)((void *)0x50001000u, a); 60 return ((uint32_t (__thiscall *)(void *))function)((void *)0x50001000u); 61 #else 62 (void)name; 63 g_esp = 0x5000ff00u; 64 g_ecx = 0x50001000u; 65 g_ebx = 0x12345678u; 66 g_esi = 0x23456789u; 67 g_edi = 0x3456789au; 68 g_ebp = 0x456789abu; 69 MEM32(g_esp) = RECOMP_RETADDR; 70 MEM32(g_esp + 4) = a; 71 MEM32(g_esp + 8) = b; 72 recomp_func_t function = recomp_lookup(address); 73 assert(function); 74 function(); 75 assert(g_esp == 0x5000ff04u + count * 4); 76 assert(g_ebx == 0x12345678u && g_esi == 0x23456789u); 77 assert(g_edi == 0x3456789au && g_ebp == 0x456789abu); 78 return g_eax; 79 #endif 80 } 81 82 static void initial_iob_data(uint32_t iob) 83 { 84 assert(iob); 85 86 for (unsigned index = 0; index < 20; ++index) { 87 uint32_t file = iob + index * 32; 88 uint32_t fields[8]; 89 for (unsigned field = 0; field < 8; ++field) fields[field] = MEM32(file + field * 4); 90 91 assert((fields[0] != 0) == (index == 0)); 92 assert(fields[1] == 0 && fields[2] == fields[0]); 93 assert(fields[3] == (index == 0 ? 0x101u : index < 3 ? 2u : 0)); 94 assert(fields[4] == (index < 3 ? index : 0)); 95 assert(fields[5] == 0 && fields[6] == (index == 0 ? 4096u : 0)); 96 assert(fields[7] == 0); 97 hash(fields[0] != 0); 98 hash(fields[2] == fields[0]); 99 for (unsigned field = 1; field < 8; ++field) if (field != 2) hash(fields[field]); 100 } 101 } 102 103 static void buffer_aliases_share_file_state(void) 104 { 105 MEM32(0x50000ffcu) = 0xaaaaaaaa; 106 MEM32(0x50001054u) = 0xbbbbbbbb; 107 MEM32(0x50002000u) = 0x50003000u; 108 MEM32(0x50002004u) = 3; 109 MEM32(0x50002008u) = 0x50003000u; 110 MEM32(0x5000200cu) = 1; 111 MEM8(0x50003000u) = 0x80; 112 MEM8(0x50003001u) = 0xff; 113 MEM8(0x50003002u) = 0x41; 114 115 /* Flag2 isolates the exported FILE-binding ABI from locale construction. */ 116 call("?_Init@" FILEBUF "IAEXPAU_iobuf@@W4_Initfl@12@@Z", 0x46001c27u, 2, 0x50002000u, 2); 117 uint32_t peek = call("?sgetc@" STREAMBUF "QAEHXZ", 0x4600e65eu, 0, 0, 0); 118 uint32_t consumed = call("?sbumpc@" STREAMBUF "QAEHXZ", 0x4600e63bu, 0, 0, 0); 119 120 assert(peek == 0x80 && consumed == 0x80); 121 assert(MEM32(0x50002000u) == 0x50003001u && MEM32(0x50002004u) == 2); 122 assert(MEM32(0x50002008u) == 0x50003000u); 123 assert(MEM32(0x5000101cu) == 0x50002000u && MEM32(0x50001020u) == 0x50002000u); 124 assert(MEM32(0x5000100cu) == 0x50002008u && MEM32(0x50001010u) == 0x50002008u); 125 assert(MEM32(0x5000102cu) == 0x50002004u && MEM32(0x50001030u) == 0x50002004u); 126 assert(MEM32(0x50001050u) == 0x50002000u); 127 hash(peek); 128 hash(consumed); 129 hash(MEM32(0x50002000u) - MEM32(0x50002008u)); 130 hash(MEM32(0x50002004u)); 131 assert(call("?sbumpc@" STREAMBUF "QAEHXZ", 0x4600e63bu, 0, 0, 0) == 0xff); 132 assert(MEM32(0x50002000u) == 0x50003002u && MEM32(0x50002004u) == 1); 133 assert(MEM32(0x50000ffcu) == 0xaaaaaaaa && MEM32(0x50001054u) == 0xbbbbbbbb); 134 } 135 136 static void output_updates_file_buffer(void) 137 { 138 MEM32(0x50002000u) = 0x50003000u; 139 MEM32(0x50002004u) = 3; 140 MEM32(0x50002008u) = 0x50003000u; 141 MEM32(0x5000200cu) = 2; 142 call("?_Init@" FILEBUF "IAEXPAU_iobuf@@W4_Initfl@12@@Z", 0x46001c27u, 2, 0x50002000u, 2); 143 144 uint32_t result = call("?sputc@" STREAMBUF "QAEHD@Z", 0x4600294fu, 1, 0x51, 0); 145 146 assert(result == 0x51 && MEM8(0x50003000u) == 0x51); 147 assert(MEM32(0x50002000u) == 0x50003001u && MEM32(0x50002004u) == 2); 148 assert(MEM32(0x50002008u) == 0x50003000u); 149 assert(MEM8(0x50003001u) == 0xff && MEM8(0x50003002u) == 0x41); 150 assert(MEM32(0x50000ffcu) == 0xaaaaaaaa && MEM32(0x50001054u) == 0xbbbbbbbb); 151 hash(result); 152 hash(MEM8(0x50003000u)); 153 hash(MEM32(0x50002000u) - MEM32(0x50002008u)); 154 hash(MEM32(0x50002004u)); 155 } 156 157 int main(int argc, char **argv) 158 { 159 assert(guest_map("stdio data test", 0x50000000u, 0x10000u)); 160 #ifdef _WIN32 161 assert(argc == 2); 162 HMODULE crt = LoadLibraryA("msvcrt.dll"); 163 assert(crt); 164 initial_iob_data((uint32_t)(uintptr_t)GetProcAddress(crt, "_iob")); 165 library = LoadLibraryA(argv[1]); 166 assert(library); 167 #else 168 (void)argc; 169 (void)argv; 170 initial_iob_data(guest_crt_data("_iob")); 171 msvcp_data_init(); 172 #endif 173 buffer_aliases_share_file_state(); 174 output_updates_file_buffer(); 175 assert(digest == 0xa1a3c99eu); 176 printf("VC6 stdio data: 20 FILE entries, shared input/output buffers, digest %08x\n", digest); 177 return 0; 178 }