client/tests/data_record.c (3014B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 #ifdef _WIN32 4 #include <windows.h> 5 #endif 6 7 void sub_240048B0(void); 8 void sub_24005150(void); 9 static unsigned length; 10 static void reset(void) { g_esp += 4; } 11 static void validate(void) { g_eax = 0; g_esp += 12; } 12 static void metadata(void) { g_eax = 0x53002000; g_esp += 4; } 13 static void allocate(void) 14 { 15 assert(MEM32(g_esp + 4) == length + 20); 16 g_eax = 0x51010000 - length - 20; 17 g_esp += 4; 18 } 19 20 recomp_func_t recomp_lookup(uint32_t address) 21 { 22 switch (address) { 23 case 0x24003200: return reset; 24 case 0x11110014: return validate; 25 case 0x11110020: return metadata; 26 case 0x24005150: return sub_24005150; 27 case 0x24009838: return allocate; 28 default: fprintf(stderr, "Unexpected data-record call %08x\n", address); abort(); 29 } 30 } 31 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } 32 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } 33 34 int main(void) 35 { 36 for (unsigned i = 0; i < 4; ++i) { 37 uint32_t address = 0x50000000 + i * 0x1000000; 38 #ifdef _WIN32 39 assert(VirtualAlloc((void *)(uintptr_t)address, 0x10000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == (void *)(uintptr_t)address); 40 #else 41 assert(guest_map("data record", address, 0x10000)); 42 #endif 43 } 44 const unsigned lengths[] = {0, 1, 2, 3, 4, 7, 8, 12, 2048}; 45 for (unsigned i = 0; i < sizeof(lengths) / sizeof(lengths[0]); ++i) { 46 length = lengths[i]; 47 uint32_t input = 0x50010000 - length - 21, output = 0x51010000 - length - 20; 48 memset((void *)ADDR(0x51000000), 0xa5, 0x10000); 49 MEM32(input) = 0; MEM32(input + 4) = 1; 50 MEM8(input + 8) = 0; 51 MEM32(input + 9) = 1234; MEM32(input + 13) = 1; MEM32(input + 17) = length; 52 for (unsigned j = 0; j < length; ++j) MEM8(input + 21 + j) = j * 13 + 7; 53 g_fs_base = 0x53001000; 54 MEM32(g_fs_base) = 0xffffffff; 55 MEM32(0x53002010) = 1; 56 MEM32(0x53003000) = 0x53004000; 57 MEM32(0x53003034) = 0x53005000; MEM32(0x53003038) = 0x53005004; 58 MEM32(0x53004014) = 0x11110014; MEM32(0x53004020) = 0x11110020; 59 MEM32(0x53005000) = 0; 60 g_esp = 0x5200ff00; g_ecx = 0x53003000; 61 MEM32(g_esp + 4) = input; MEM32(g_esp + 8) = length + 21; 62 g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; 63 64 sub_240048B0(); 65 66 assert(g_esp == 0x5200ff0c); 67 assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); 68 assert(MEM32(g_fs_base) == 0xffffffff); 69 assert(MEM32(0x53005000) == output); 70 assert(MEM32(output) == 1234 && MEM32(output + 4) == length && MEM32(output + 8) == length); 71 assert(MEM32(output + 12) == 1 && MEM32(output + 16) == length); 72 for (unsigned j = 0; j < length; ++j) assert(MEM8(output + 20 + j) == (unsigned char)(j * 13 + 7)); 73 assert(MEM8(output - 1) == 0xa5); 74 } 75 puts("data record: 9 page-ending payloads; bytes, metadata, canaries, FS and ABI passed"); 76 }