aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/tests/guest_eh.c (3188B)


      1 #include "recomp_types.h"
      2 #include "guest_memory.h"
      3 #include <assert.h>
      4 
      5 void sub_42001000(void);
      6 void sub_42001100(void);
      7 void native32_shim_CxxThrowException(void);
      8 static unsigned throws, catches, metadata_lookups;
      9 
     10 uint32_t recomp_funcinfo(uint32_t handler)
     11 {
     12     assert(handler == 0x42002000u);
     13     ++metadata_lookups;
     14     return 0x42003000u;
     15 }
     16 
     17 /* The Windows backend obtains the same metadata through its existing hook. */
     18 uint32_t game_funcinfo(uint32_t handler) { return recomp_funcinfo(handler); }
     19 recomp_func_t recomp_lookup_manual(uint32_t address) { (void)address; return NULL; }
     20 
     21 recomp_func_t recomp_lookup(uint32_t address)
     22 {
     23     if (address == 0x42001000u) return sub_42001000;
     24     if (address == 0x42001100u) { ++catches; return sub_42001100; }
     25     if (address == 0x42002000u) { ++throws; return native32_shim_CxxThrowException; }
     26     return NULL;
     27 }
     28 
     29 recomp_func_t recomp_lookup_import(uint32_t address)
     30 {
     31     fprintf(stderr, "Unexpected EH test dependency %08x from %08x\n", address, g_cur_func);
     32     abort();
     33 }
     34 
     35 static void catch_only_continuation_restores_stack(void)
     36 {
     37     for (unsigned i = 0; i < 1024; ++i) {
     38         uint32_t original_esp = 0x5000ff00u - (i % 8) * 128;
     39         uint32_t original_fs = i & 1 ? 0x50009000u : UINT32_MAX;
     40         g_esp = original_esp;
     41         g_ebp = 0x11223344u;
     42         g_ebx = 0x22334455u;
     43         g_esi = 0x33445566u;
     44         g_edi = 0x44556677u;
     45         g_fp_top = 0;
     46         g_eax = 1337;
     47         MEM32(g_fs_base) = original_fs;
     48         MEM32(0x50009000u) = UINT32_MAX;
     49         MEM32(0x50009004u) = 0x42002100u;
     50         MEM64(original_esp - 32) = 0;
     51 
     52         PUSH32(g_esp, RECOMP_RETADDR);
     53         sub_42001000();
     54 
     55         assert(g_eax == 42);
     56         assert(g_esp == original_esp);
     57         assert(MEM32(g_fs_base) == original_fs);
     58         assert(MEM32(0x50009000u) == UINT32_MAX && MEM32(0x50009004u) == 0x42002100u);
     59         assert(g_ebp == 0x11223344u && g_ebx == 0x22334455u);
     60         assert(g_esi == 0x33445566u && g_edi == 0x44556677u);
     61         assert(g_fp_top == 0);
     62         assert(MEM64(original_esp - 32) == 0x3ff0000000000000ull);
     63         assert(throws == i + 1 && catches == i + 1 && metadata_lookups == i + 1);
     64     }
     65 }
     66 
     67 int main(void)
     68 {
     69     assert(guest_map("EH metadata", 0x42000000u, 0x10000u));
     70     assert(guest_map("EH test stack and TIB", 0x50000000u, 0x10000u));
     71     g_fs_base = 0x50008000u;
     72 
     73     /* Construct VC6 metadata directly; guest code addresses contain no x86 bytes. */
     74     MEM32(0x42003000u) = 0x19930520u;
     75     MEM32(0x42003004u) = 1;
     76     MEM32(0x42003008u) = 0x42003100u;
     77     MEM32(0x4200300cu) = 1;
     78     MEM32(0x42003010u) = 0x42003200u;
     79     MEM32(0x42003100u) = UINT32_MAX;
     80     MEM32(0x42003104u) = 0;
     81     MEM32(0x42003200u) = 0;
     82     MEM32(0x42003204u) = 0;
     83     MEM32(0x42003208u) = 1;
     84     MEM32(0x4200320cu) = 1;
     85     MEM32(0x42003210u) = 0x42003300u;
     86     MEM32(0x42003300u) = 0;
     87     MEM32(0x42003304u) = 0;
     88     MEM32(0x42003308u) = 0;
     89     MEM32(0x4200330cu) = 0x42001100u;
     90     assert(MEM32(0x42001000u) == 0 && MEM32(0x42002000u) == 0);
     91     catch_only_continuation_restores_stack();
     92     printf("guest EH: %u throws/catches, return 42, stack/FS/registers/x87 restored\n", catches);
     93     return 0;
     94 }