aorecomp

Anarchy Online classic in the browser

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


client/tests/dynamic_imports.c (9987B)


      1 #include "linux_imports.h"
      2 #include "guest_memory.h"
      3 #include "guest_crt.h"
      4 #include "guest_stdio.h"
      5 #include <assert.h>
      6 #include <sys/wait.h>
      7 #include <unistd.h>
      8 
      9 static uint32_t exit_stream;
     10 static void exit_callback(void)
     11 {
     12     uint32_t stack = g_esp;
     13     PUSH32(g_esp, exit_stream);
     14     PUSH32(g_esp, 'C');
     15     PUSH32(g_esp, RECOMP_RETADDR);
     16     guest_stdio_lookup("fputc")();
     17     g_esp = stack + 4;
     18 }
     19 
     20 static void thread_entry(void)
     21 {
     22     g_eax = MEM32(g_esp + 4) + 1;
     23     g_esp += 8;
     24 }
     25 recomp_func_t recomp_lookup(uint32_t address)
     26 {
     27     if (address == 0x11002000) return exit_callback;
     28     return address == 0x11001000 ? thread_entry : NULL;
     29 }
     30 recomp_func_t linux_module_lookup(const char *name) { (void)name; return NULL; }
     31 int linux_module_entry(unsigned index, const char **name, uint32_t *base, uint32_t *size) { return 0; }
     32 uint32_t linux_module_export(uint32_t handle, const char *name, uint32_t ordinal)
     33 {
     34     if (handle == 0x11000000) {
     35         if (name ? !strcmp(name, "ThreadEntry") : ordinal == 1) return 0x11001000;
     36         MEM32(g_fs_base + 0x34) = 127;
     37     } else MEM32(g_fs_base + 0x34) = 126;
     38     return 0;
     39 }
     40 void linux_thread_notify(uint32_t reason) { (void)reason; }
     41 void recomp_eh_thread_exit(void) {}
     42 void native32_shim_CxxThrowException(void) { abort(); }
     43 void guest_longjmp(void) { abort(); }
     44 
     45 static uint32_t call(const char *name, uint32_t first, uint32_t second)
     46 {
     47     g_esp = 0x5000ff00u;
     48     g_ebx = 0x12345678u;
     49     g_esi = 0x23456789u;
     50     g_edi = 0x3456789au;
     51     g_ebp = 0x456789abu;
     52     g_cur_func = 0x280028f0u;
     53     MEM32(g_esp) = RECOMP_RETADDR;
     54     MEM32(g_esp + 4) = first;
     55     MEM32(g_esp + 8) = second;
     56     recomp_lookup_import(linux_import_bind("kernel32.dll", name, 0))();
     57     assert(g_esp == 0x5000ff00u +
     58            (!strcmp(name, "GetProcAddress") || !strcmp(name, "WaitForSingleObject") ||
     59             !strcmp(name, "GetExitCodeThread") ? 12 : 8));
     60     assert(g_ebx == 0x12345678u && g_esi == 0x23456789u);
     61     assert(g_edi == 0x3456789au && g_ebp == 0x456789abu);
     62     assert(g_cur_func == 0x280028f0u);
     63     return g_eax;
     64 }
     65 
     66 static void window_symbols_survive_library_release(void)
     67 {
     68     strcpy((char *)ADDR(0x50000000u), "UsEr32.DlL");
     69     static const char *names[] = {
     70         "PeekMessageW", "GetMessageW", "DispatchMessageW", "DefWindowProcW",
     71         "RegisterClassW", "UnregisterClassW", "CreateWindowExW",
     72         "PeekMessageA", "GetMessageA", "DispatchMessageA", "DefWindowProcA",
     73         "RegisterClassA", "UnregisterClassA", "CreateWindowExA",
     74     };
     75     uint32_t symbols[14];
     76     MEM32(g_fs_base + 0x34) = 77;
     77 
     78     uint32_t library = call("LoadLibraryA", 0x50000000u, 0);
     79     assert(call("LoadLibraryA", 0x50000000u, 0) == library);
     80     for (unsigned i = 0; i < 14; ++i) {
     81         strcpy((char *)ADDR(0x50000100u), names[i]);
     82         symbols[i] = call("GetProcAddress", library, 0x50000100u);
     83     }
     84     uint32_t released = call("FreeLibrary", library, 0);
     85     uint32_t released_again = call("FreeLibrary", library, 0);
     86 
     87     assert(library == 0xf2000000u && released && released_again);
     88     assert(MEM32(g_fs_base + 0x34) == 77);
     89     for (unsigned i = 0; i < 14; ++i) {
     90         assert(symbols[i] >= 0xf0000000u && symbols[i] < 0xf0004000u);
     91         assert(symbols[i] == linux_import_bind("USER32.dll", names[i], 0));
     92         strcpy((char *)ADDR(0x50000100u), names[i]);
     93         assert(call("GetProcAddress", library, 0x50000100u) == symbols[i]);
     94         for (unsigned j = 0; j < i; ++j) assert(symbols[i] != symbols[j]);
     95     }
     96 }
     97 
     98 static void failed_lookups_report_errors(void)
     99 {
    100     strcpy((char *)ADDR(0x50000100u), "peekmessagew");
    101 
    102     uint32_t wrong_case = call("GetProcAddress", 0xf2000000u, 0x50000100u);
    103     uint32_t name_error = MEM32(g_fs_base + 0x34);
    104     uint32_t invalid_module = call("GetProcAddress", 0, 0x50000100u);
    105     uint32_t module_error = MEM32(g_fs_base + 0x34);
    106     uint32_t invalid_release = call("FreeLibrary", 0, 0);
    107 
    108     assert(!wrong_case && name_error == 127);
    109     assert(!invalid_module && module_error == 126);
    110     assert(!invalid_release && MEM32(g_fs_base + 0x34) == 126);
    111 }
    112 
    113 static void import_names_are_owned_and_calls_are_dispatched(void)
    114 {
    115     char dll[] = "KERNEL32.dll", name[] = "GetCurrentProcessId";
    116     uint32_t symbol = linux_import_bind(dll, name, 0);
    117     unsigned count = linux_import_count();
    118     memset(dll, 'x', sizeof(dll) - 1);
    119     memset(name, 'x', sizeof(name) - 1);
    120     g_esp = 0x5000ff00u;
    121 
    122     recomp_lookup_import(symbol)();
    123 
    124     assert(g_eax == (uint32_t)getpid() && g_esp == 0x5000ff04u);
    125     assert(linux_import_bind("kernel32.dll", "GetCurrentProcessId", 0) == symbol);
    126     assert(linux_import_count() == count);
    127 }
    128 
    129 static void resolved_message_loop_consumes_quit(void)
    130 {
    131     strcpy((char *)ADDR(0x50000100u), "GetMessageW");
    132     uint32_t symbol = call("GetProcAddress", 0xf2000000u, 0x50000100u);
    133     g_esp = 0x5000ff00;
    134     MEM32(g_esp + 4) = 73;
    135     recomp_lookup_import(linux_import_bind("user32.dll", "PostQuitMessage", 0))();
    136     memset((void *)ADDR(0x500001fc), 0xa5, 36);
    137     g_esp = 0x5000ff00;
    138     MEM32(g_esp + 4) = 0x50000200;
    139     MEM32(g_esp + 8) = MEM32(g_esp + 12) = MEM32(g_esp + 16) = 0;
    140 
    141     recomp_lookup_import(symbol)();
    142 
    143     assert(g_eax == 0 && g_esp == 0x5000ff14);
    144     assert(MEM32(0x50000200) == 0 && MEM32(0x50000204) == 0x12);
    145     assert(MEM32(0x50000208) == 73);
    146     assert(MEM32(0x500001fc) == 0xa5a5a5a5 && MEM32(0x5000021c) == 0xa5a5a5a5);
    147 }
    148 
    149 static void file_and_event_handles_close_through_imports(void)
    150 {
    151     char path[] = "/tmp/ao-file-import-XXXXXX";
    152     int descriptor = mkstemp(path);
    153     assert(descriptor >= 0 && !close(descriptor));
    154     strcpy((char *)ADDR(0x50002000u), path);
    155     g_esp = 0x5000ff00u;
    156     MEM32(g_esp + 4) = 0x50002000u;
    157     MEM32(g_esp + 8) = 0x80000000u;
    158     MEM32(g_esp + 12) = 1;
    159     MEM32(g_esp + 16) = 0;
    160     MEM32(g_esp + 20) = 3;
    161     MEM32(g_esp + 24) = 0x80;
    162     MEM32(g_esp + 28) = 0;
    163 
    164     recomp_lookup_import(linux_import_bind("kernel32.dll", "CreateFileA", 0))();
    165     uint32_t file = g_eax;
    166     assert(file != UINT32_MAX && g_esp == 0x5000ff20u);
    167     g_esp = 0x5000ff00u;
    168     for (unsigned i = 1; i <= 4; ++i) MEM32(g_esp + i * 4) = 0;
    169     recomp_lookup_import(linux_import_bind("kernel32.dll", "CreateEventA", 0))();
    170     uint32_t event = g_eax;
    171     assert(event && event != file && g_esp == 0x5000ff14u);
    172     uint32_t file_closed = call("CloseHandle", file, 0);
    173     uint32_t event_closed = call("CloseHandle", event, 0);
    174 
    175     assert(file_closed == 1 && event_closed == 1);
    176     assert(call("CloseHandle", file, 0) == 0 && MEM32(g_fs_base + 0x34) == 6);
    177     assert(call("CloseHandle", event, 0) == 0 && MEM32(g_fs_base + 0x34) == 6);
    178     assert(!unlink(path));
    179 }
    180 
    181 static void thread_handles_dispatch_through_imports(void)
    182 {
    183     uint32_t begin = linux_import_bind("msvcrt.dll", "_beginthreadex", 0);
    184     g_esp = 0x5000ff00;
    185     uint32_t args[] = {0, 0, 0x11001000, 41, 0, 0x50003000};
    186     for (unsigned i = 0; i < 6; ++i) MEM32(g_esp + 4 + i * 4) = args[i];
    187 
    188     recomp_lookup_import(begin)();
    189     uint32_t thread = g_eax;
    190     assert(thread && g_esp == 0x5000ff04 && MEM32(0x50003000));
    191     uint32_t result = call("WaitForSingleObject", thread, 10000);
    192     uint32_t queried = call("GetExitCodeThread", thread, 0x50003004);
    193     uint32_t closed = call("CloseHandle", thread, 0);
    194 
    195     assert(result == 0 && queried == 1 && MEM32(0x50003004) == 42 && closed == 1);
    196     assert(call("WaitForSingleObject", thread, 0) == UINT32_MAX && MEM32(g_fs_base + 0x34) == 6);
    197 }
    198 
    199 static void game_exports_return_callable_guest_addresses(void)
    200 {
    201     strcpy((char *)ADDR(0x50002000), "ThreadEntry");
    202     MEM32(g_fs_base + 0x34) = 777;
    203 
    204     uint32_t address = call("GetProcAddress", 0x11000000, 0x50002000);
    205 
    206     assert(address == 0x11001000 && MEM32(g_fs_base + 0x34) == 777);
    207     assert(call("GetProcAddress", 0x11000000, 1) == address);
    208     assert(call("GetProcAddress", 0x11000000, 2) == 0 && MEM32(g_fs_base + 0x34) == 127);
    209     g_esp = 0x5000ff00;
    210     MEM32(g_esp + 4) = 41;
    211     recomp_lookup(address)();
    212     assert(g_eax == 42 && g_esp == 0x5000ff08);
    213 }
    214 
    215 static void process_exit_runs_callbacks_and_flushes_guest_files(void)
    216 {
    217     char path[] = "/tmp/ao-exit-XXXXXX";
    218     int descriptor = mkstemp(path);
    219     assert(descriptor >= 0);
    220     strcpy((char *)ADDR(0x50002000), path);
    221     strcpy((char *)ADDR(0x50002100), "wb");
    222     fflush(NULL);
    223     pid_t child = fork();
    224     assert(child >= 0);
    225     if (!child) {
    226         g_esp = 0x5000ff00;
    227         MEM32(g_esp + 4) = 0x50002000;
    228         MEM32(g_esp + 8) = 0x50002100;
    229         guest_stdio_lookup("fopen")();
    230         exit_stream = g_eax;
    231         assert(exit_stream);
    232         g_esp = 0x5000ff00;
    233         MEM32(g_esp + 4) = 'B';
    234         MEM32(g_esp + 8) = exit_stream;
    235         guest_stdio_lookup("fputc")();
    236         g_esp = 0x5000ff00;
    237         MEM32(g_esp + 4) = 0x11002000;
    238         guest_crt_lookup("_onexit")();
    239 
    240         g_esp = 0x5000ff00;
    241         MEM32(g_esp + 4) = 37;
    242         recomp_lookup_import(linux_import_bind("msvcrt.dll", "exit", 0))();
    243         _exit(99);
    244     }
    245 
    246     int status;
    247     assert(waitpid(child, &status, 0) == child);
    248     assert(WIFEXITED(status) && WEXITSTATUS(status) == 37);
    249     char bytes[3];
    250     assert(read(descriptor, bytes, sizeof(bytes)) == 2 && !memcmp(bytes, "BC", 2));
    251     assert(!close(descriptor) && !unlink(path));
    252 }
    253 
    254 int main(void)
    255 {
    256     assert(guest_map("dynamic imports test", 0x50000000u, 0x10000u));
    257     g_fs_base = 0x50004000u;
    258     process_exit_runs_callbacks_and_flushes_guest_files();
    259     window_symbols_survive_library_release();
    260     failed_lookups_report_errors();
    261     import_names_are_owned_and_calls_are_dispatched();
    262     resolved_message_loop_consumes_quit();
    263     file_and_event_handles_close_through_imports();
    264     thread_handles_dispatch_through_imports();
    265     game_exports_return_callable_guest_addresses();
    266     puts("dynamic imports: 14 window symbols, stable guest IDs, lifetime, errors, dispatch and ABI passed");
    267     puts("exit: guest callbacks, buffered output and process status passed");
    268     return 0;
    269 }