client/tests/archive_string.c (2912B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 4 void sub_24003930(void); 5 static unsigned length, allocations; 6 7 static void read_string(void) 8 { 9 assert(MEM32(g_esp + 8) == 6); 10 MEM32(MEM32(g_esp + 12)) = 0x50010000 - length; 11 MEM32(MEM32(g_esp + 16)) = length; 12 g_eax = 0; 13 g_esp += 24; 14 } 15 16 static void grow_string(void) 17 { 18 uint32_t data = allocations++ ? 0x53002001 : 0x51001001; 19 MEM32(g_ecx + 4) = data; 20 MEM32(g_ecx + 12) = MEM32(g_esp + 4); 21 MEM8(data - 1) = 1; 22 g_eax = 1; 23 g_esp += 12; 24 } 25 26 static void tidy_string(void) { g_esp += 8; } 27 static void set_length(void) 28 { 29 MEM32(g_ecx + 8) = MEM32(g_esp + 4); 30 MEM8(MEM32(g_ecx + 4) + MEM32(g_ecx + 8)) = 0; 31 g_esp += 8; 32 } 33 34 recomp_func_t recomp_lookup(uint32_t address) 35 { 36 switch (address) { 37 case 0x24004200: return read_string; 38 case 0x100: return grow_string; 39 case 0x104: return tidy_string; 40 case 0x108: return set_length; 41 default: fprintf(stderr, "Unexpected archive-string call %08x\n", address); abort(); 42 } 43 } 44 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } 45 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } 46 47 int main(void) 48 { 49 for (unsigned i = 0; i < 4; ++i) 50 assert(guest_map("archive string", 0x50000000 + i * 0x1000000, 0x10000)); 51 assert(guest_map("archive imports", 0x24000000, 0x10000)); 52 MEM32(0x2400B048) = 0x100; 53 MEM32(0x2400B020) = 0x104; 54 MEM32(0x2400B03C) = 0x108; 55 MEM32(0x2400B044) = 0x53003000; 56 const unsigned cases[][2] = {{4, 4}, {0, 0}, {1, 1}, {3, 3}, {8, 8}, {8, 4}, {8, 3}}; 57 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { 58 length = cases[i][0]; 59 allocations = 0; 60 memset((void *)ADDR(0x51000000), 0xa5, 0x10000); 61 memset((void *)ADDR(0x53000000), 0xa5, 0x10000); 62 memset((void *)ADDR(0x53000000), 0, 16); 63 MEM32(0x53003000) = cases[i][1]; 64 for (unsigned j = 0; j < length; ++j) MEM8(0x50010000 - length + j) = j + 1; 65 g_fs_base = 0x53001000; 66 MEM32(g_fs_base) = 0xffffffff; 67 g_esp = 0x5200ff00; 68 MEM32(g_esp + 4) = 0x42; 69 MEM32(g_esp + 8) = 0x53000000; 70 MEM32(g_esp + 12) = 0; 71 g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; 72 73 sub_24003930(); 74 75 assert(g_eax == 0 && g_esp == 0x5200ff10); 76 assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); 77 assert(MEM32(g_fs_base) == 0xffffffff); 78 assert(MEM32(0x53000008) == cases[i][1]); 79 uint32_t data = MEM32(0x53000004); 80 for (unsigned j = 0; j < cases[i][1]; ++j) assert(MEM8(data + j) == j + 1); 81 assert(MEM8(data + cases[i][1]) == 0); 82 assert(MEM8(data + cases[i][1] + 1) == 0xa5); 83 assert(MEM8(data - 2) == 0xa5); 84 } 85 puts("archive string: page-ending and empty strings, truncation, canaries, FS and ABI passed"); 86 }