client/tests/msvcp_strings.c (7563B)
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_msvcp.h" 10 #endif 11 12 #define STRING_TYPE "?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@" 13 static const char *ctor = "??0" STRING_TYPE "QAE@ABV?$allocator@D@1@@Z"; 14 static const char *copy_ctor = "??0" STRING_TYPE "QAE@ABV01@@Z"; 15 static const char *dtor = "??1" STRING_TYPE "QAE@XZ"; 16 static const char *assign_bytes = "?assign@" STRING_TYPE "QAEAAV12@PBDI@Z"; 17 static const char *assign_string = "?assign@" STRING_TYPE "QAEAAV12@ABV12@II@Z"; 18 static const char *erase = "?erase@" STRING_TYPE "QAEAAV12@II@Z"; 19 static const char *freeze = "?_Freeze@" STRING_TYPE "AAEXXZ"; 20 static const char *grow = "?_Grow@" STRING_TYPE "AAE_NI_N@Z"; 21 static const char *copy = "?_Copy@" STRING_TYPE "AAEXI@Z"; 22 static const char *eos = "?_Eos@" STRING_TYPE "AAEXI@Z"; 23 static const char *less = "??Mstd@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@0@0@Z"; 24 25 /* The reference invokes real x86 thiscall exports; Linux invokes guest adapters. */ 26 static uint32_t call(const char *name, uint32_t object, unsigned count, 27 uint32_t a, uint32_t b, uint32_t c) 28 { 29 #ifdef _WIN32 30 FARPROC function = GetProcAddress(library, name); 31 assert(function); 32 if (!object) return ((uint32_t (__cdecl *)(uint32_t, uint32_t))function)(a, b); 33 switch (count) { 34 case 0: return ((uint32_t (__thiscall *)(void *))function)((void *)(uintptr_t)object); 35 case 1: return ((uint32_t (__thiscall *)(void *, uint32_t))function)((void *)(uintptr_t)object, a); 36 case 2: return ((uint32_t (__thiscall *)(void *, uint32_t, uint32_t))function)((void *)(uintptr_t)object, a, b); 37 case 3: return ((uint32_t (__thiscall *)(void *, uint32_t, uint32_t, uint32_t))function)((void *)(uintptr_t)object, a, b, c); 38 } 39 abort(); 40 #else 41 g_esp = 0x5000ff00u; 42 g_ecx = object; 43 g_ebx = 0x12345678u; 44 g_esi = 0x23456789u; 45 g_edi = 0x3456789au; 46 g_ebp = 0x456789abu; 47 MEM32(g_esp) = RECOMP_RETADDR; 48 MEM32(g_esp + 4) = a; 49 MEM32(g_esp + 8) = b; 50 MEM32(g_esp + 12) = c; 51 recomp_func_t function = guest_msvcp_lookup(name); 52 assert(function); 53 function(); 54 assert(g_esp == 0x5000ff04u + (object ? count * 4 : 0)); 55 assert(g_ebx == 0x12345678u && g_esi == 0x23456789u); 56 assert(g_edi == 0x3456789au && g_ebp == 0x456789abu); 57 return g_eax; 58 #endif 59 } 60 61 static uint32_t digest = 2166136261u; 62 static unsigned snapshots; 63 64 static void hash(uint32_t value) 65 { 66 digest = (digest ^ value) * 16777619u; 67 } 68 69 static void snapshot(uint32_t object) 70 { 71 uint32_t data = MEM32(object + 4), size = MEM32(object + 8); 72 hash(MEM8(object)); 73 hash(size); 74 hash(MEM32(object + 12)); 75 hash(data != 0); 76 if (data) { 77 hash(MEM8(data - 1)); 78 for (uint32_t i = 0; i <= size; ++i) hash(MEM8(data + i)); 79 assert(MEM8(data + size) == 0); 80 } 81 ++snapshots; 82 } 83 84 static void shares_then_detaches(void) 85 { 86 call(ctor, 0x50001000u, 1, 0x50003000u, 0, 0); 87 call(assign_bytes, 0x50001000u, 2, 0x50002000u, 63, 0); 88 89 call(copy_ctor, 0x50001020u, 1, 0x50001000u, 0, 0); 90 91 assert(MEM32(0x50001004u) == MEM32(0x50001024u)); 92 assert(MEM8(MEM32(0x50001004u) - 1) == 1); 93 snapshot(0x50001000u); 94 snapshot(0x50001020u); 95 call(freeze, 0x50001020u, 0, 0, 0, 0); 96 assert(MEM32(0x50001004u) != MEM32(0x50001024u)); 97 assert(MEM8(MEM32(0x50001004u) - 1) == 0); 98 assert(MEM8(MEM32(0x50001024u) - 1) == 255); 99 snapshot(0x50001000u); 100 snapshot(0x50001020u); 101 call(dtor, 0x50001000u, 0, 0, 0, 0); 102 call(dtor, 0x50001020u, 0, 0, 0, 0); 103 } 104 105 static void exercises_growth_and_sharing(void) 106 { 107 uint32_t random = 0x13579bdfu; 108 for (unsigned trial = 0; trial < 1024; ++trial) { 109 random = random * 1664525u + 1013904223u; 110 uint32_t length = (random >> 16) % 128; 111 call(ctor, 0x50001000u, 1, 0x50003000u, 0, 0); 112 call(ctor, 0x50001020u, 1, 0x50003000u, 0, 0); 113 114 call(assign_bytes, 0x50001000u, 2, 0x50002000u, length, 0); 115 call(assign_string, 0x50001020u, 3, 0x50001000u, 0, UINT32_MAX); 116 snapshot(0x50001000u); 117 snapshot(0x50001020u); 118 if (trial & 1) call(freeze, 0x50001020u, 0, 0, 0, 0); 119 uint32_t offset = length ? (random & 0xff) % (length + 1) : 0; 120 call(erase, 0x50001020u, 2, offset, (random >> 8) % 100, 0); 121 snapshot(0x50001000u); 122 snapshot(0x50001020u); 123 hash(call(less, 0, 2, 0x50001000u, 0x50001020u, 0) & 255); 124 call(assign_string, 0x50001000u, 3, 0x50001000u, offset, 13); 125 snapshot(0x50001000u); 126 call(copy, 0x50001020u, 1, trial % 67, 0, 0); 127 snapshot(0x50001020u); 128 uint32_t requested = (trial * 7) % 73; 129 hash(call(grow, 0x50001020u, 2, requested, trial & 1, 0) & 255); 130 snapshot(0x50001020u); 131 if (requested) call(eos, 0x50001020u, 1, 0, 0, 0); 132 133 assert(MEM32(0x50001008u) <= 13); 134 assert(MEM32(0x50001028u) == 0); 135 snapshot(0x50001020u); 136 call(dtor, 0x50001000u, 0, 0, 0, 0); 137 call(dtor, 0x50001020u, 0, 0, 0, 0); 138 } 139 } 140 141 static void preserves_embedded_nulls_until_split(void) 142 { 143 MEM8(0x50002004u) = 0; 144 call(ctor, 0x50001000u, 1, 0x50003000u, 0, 0); 145 call(assign_bytes, 0x50001000u, 2, 0x50002000u, 9, 0); 146 call(copy_ctor, 0x50001020u, 1, 0x50001000u, 0, 0); 147 148 call(erase, 0x50001020u, 2, 0, 0, 0); 149 150 assert(MEM32(0x50001008u) == 9); 151 assert(MEM32(0x50001028u) == 4); 152 snapshot(0x50001000u); 153 snapshot(0x50001020u); 154 call(dtor, 0x50001000u, 0, 0, 0, 0); 155 call(dtor, 0x50001020u, 0, 0, 0, 0); 156 MEM8(0x50002004u) = 37; 157 } 158 159 static void copies_instead_of_overflowing_refcount(void) 160 { 161 call(ctor, 0x50001000u, 1, 0x50003000u, 0, 0); 162 call(assign_bytes, 0x50001000u, 2, 0x50002000u, 17, 0); 163 for (unsigned i = 0; i < 254; ++i) 164 call(copy_ctor, 0x50004000u + i * 16, 1, 0x50001000u, 0, 0); 165 166 call(copy_ctor, 0x50001020u, 1, 0x50001000u, 0, 0); 167 168 assert(MEM32(0x50001004u) != MEM32(0x50001024u)); 169 assert(MEM8(MEM32(0x50001004u) - 1) == 254); 170 assert(MEM8(MEM32(0x50001024u) - 1) == 0); 171 snapshot(0x50001000u); 172 snapshot(0x50001020u); 173 for (unsigned i = 0; i < 254; ++i) call(dtor, 0x50004000u + i * 16, 0, 0, 0, 0); 174 call(dtor, 0x50001000u, 0, 0, 0, 0); 175 call(dtor, 0x50001020u, 0, 0, 0, 0); 176 } 177 178 int main(int argc, char **argv) 179 { 180 #ifdef _WIN32 181 assert(argc == 2); 182 library = LoadLibraryA(argv[1]); 183 assert(library); 184 #endif 185 assert(guest_map("string test memory", 0x50000000u, 0x10000u)); 186 MEM8(0x50003000u) = 0x5a; 187 for (unsigned i = 0; i < 256; ++i) MEM8(0x50002000u + i) = 33 + i % 90; 188 MEM8(0x50002100u) = 0; 189 shares_then_detaches(); 190 exercises_growth_and_sharing(); 191 preserves_embedded_nulls_until_split(); 192 copies_instead_of_overflowing_refcount(); 193 const char *npos = "?npos@" STRING_TYPE "2IB"; 194 const char *nullstr = "?_C@?1??_Nullstr@" STRING_TYPE "CAPBDXZ@4DB"; 195 #ifdef _WIN32 196 assert(*(uint32_t *)GetProcAddress(library, npos) == UINT32_MAX); 197 assert(*(char *)GetProcAddress(library, nullstr) == 0); 198 #else 199 assert(MEM32(guest_msvcp_data(npos)) == UINT32_MAX); 200 assert(MEM8(guest_msvcp_data(nullstr)) == 0); 201 #endif 202 assert(call("?max_size@" STRING_TYPE "QBEIXZ", 0x50001000u, 0, 0, 0, 0) == 0xfffffffdu); 203 assert(snapshots == 8200); 204 assert(digest == 0x8d3d3d76u); 205 printf("VC6 strings: %u snapshots, digest %08x\n", snapshots, digest); 206 return 0; 207 }