aorecomp

Anarchy Online classic in the browser

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


client/tests/new_handler.c (5757B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #ifdef _WIN32
      6 #include <windows.h>
      7 static HMODULE crt;
      8 #else
      9 #include "guest_crt.h"
     10 #include "guest_memory.h"
     11 #endif
     12 
     13 static const char *set_handler = "?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z";
     14 static unsigned calls, retries, expected_size;
     15 
     16 static int handler(unsigned size)
     17 {
     18     assert(size == expected_size);
     19     ++calls;
     20     return calls <= retries ? -7 : 0; /* Any nonzero return requests another try. */
     21 }
     22 
     23 #ifndef _WIN32
     24 static void guest_handler(void)
     25 {
     26     g_eax = handler(MEM32(g_esp + 4));
     27     g_ecx = 0x11223344u;
     28     g_edx = 0x55667788u;
     29     g_cur_func = 0x11110000u;
     30     g_esp += 4;
     31 }
     32 recomp_func_t recomp_lookup(uint32_t address)
     33 {
     34     assert(address == 0x11110000u);
     35     return guest_handler;
     36 }
     37 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
     38 #endif
     39 
     40 static uint32_t call(const char *name, uint32_t first, uint32_t second)
     41 {
     42 #ifdef _WIN32
     43     uint32_t (__cdecl *function)(uint32_t, uint32_t) = (void *)GetProcAddress(crt, name);
     44     if (!function) fprintf(stderr, "Missing MSVCRT export %s\n", name);
     45     assert(function);
     46     return function(first, second);
     47 #else
     48     g_esp = 0x5000ff00u;
     49     g_ebx = 0x12345678u;
     50     g_esi = 0x23456789u;
     51     g_edi = 0x3456789au;
     52     g_ebp = 0x456789abu;
     53     g_cur_func = 0x22220000u;
     54     MEM32(g_esp) = RECOMP_RETADDR;
     55     MEM32(g_esp + 4) = first;
     56     MEM32(g_esp + 8) = second;
     57     recomp_func_t function = guest_crt_lookup(name);
     58     assert(function);
     59     function();
     60     assert(g_esp == 0x5000ff04u && g_cur_func == 0x22220000u);
     61     assert(g_ebx == 0x12345678u && g_esi == 0x23456789u);
     62     assert(g_edi == 0x3456789au && g_ebp == 0x456789abu);
     63     return g_eax;
     64 #endif
     65 }
     66 
     67 static void *memory(uint32_t address)
     68 {
     69 #ifdef _WIN32
     70     return (void *)(uintptr_t)address;
     71 #else
     72     return (void *)ADDR(address);
     73 #endif
     74 }
     75 
     76 static void registration_preserves_previous_values(uint32_t callback)
     77 {
     78     call(set_handler, 0, 0);
     79     call("?_set_new_mode@@YAHH@Z", 0, 0);
     80 
     81     uint32_t previous = call(set_handler, callback, 0);
     82     uint32_t replaced = call(set_handler, callback, 0);
     83     uint32_t mode = call("?_set_new_mode@@YAHH@Z", 1, 0);
     84     uint32_t invalid = call("?_set_new_mode@@YAHH@Z", 2, 0);
     85 
     86     assert(previous == 0 && replaced == callback && mode == 0);
     87     assert(invalid == UINT32_MAX);
     88     assert(*(int *)memory(call("_errno", 0, 0)) == 22);
     89     assert(call("?_set_new_mode@@YAHH@Z", 0, 0) == 1);
     90     assert(call(set_handler, 0, 0) == callback);
     91     assert(call("??2@YAPAXI@Z", 0xfffff000u, 0) == 0);
     92     call(set_handler, callback, 0);
     93 }
     94 
     95 static void allocation_failure(const char *name, uint32_t size, unsigned mode,
     96                                unsigned expected_calls)
     97 {
     98     call("?_set_new_mode@@YAHH@Z", mode, 0);
     99     uint32_t allocation = call("malloc", 32, 0);
    100     assert(allocation);
    101     memset(memory(allocation), 0x5a, 32);
    102     expected_size = size;
    103     calls = 0;
    104     retries = 3;
    105     *(int *)memory(call("_errno", 0, 0)) = 77;
    106     uint32_t first = size, second = 0;
    107     if (!strcmp(name, "realloc")) { first = allocation; second = size; }
    108     if (!strcmp(name, "calloc")) { first = 1; second = size; }
    109 
    110     uint32_t result = call(name, first, second);
    111     int error = *(int *)memory(call("_errno", 0, 0));
    112 
    113     if (result || calls != expected_calls || error != 12) {
    114         fprintf(stderr, "%s size=%08x mode=%u result=%08x calls=%u expected=%u errno=%d\n",
    115                 name, size, mode, result, calls, expected_calls, error);
    116     }
    117     assert(result == 0 && calls == expected_calls && error == 12);
    118     for (unsigned i = 0; i < 32; ++i) assert(((unsigned char *)memory(allocation))[i] == 0x5a);
    119     call("free", allocation, 0);
    120 }
    121 
    122 static void successful_and_zero_allocations(void)
    123 {
    124     calls = 0;
    125     call("?_set_new_mode@@YAHH@Z", 1, 0);
    126     *(int *)memory(call("_errno", 0, 0)) = 77;
    127 
    128     uint32_t zero = call("??2@YAPAXI@Z", 0, 0);
    129     uint32_t bytes = call("calloc", 8, 4);
    130     uint32_t resized = call("realloc", bytes, 64);
    131 
    132     assert(zero && bytes && resized && !calls);
    133     assert(*(int *)memory(call("_errno", 0, 0)) == 77);
    134     for (unsigned i = 0; i < 32; ++i) assert(((unsigned char *)memory(resized))[i] == 0);
    135     assert(call("realloc", resized, 0) == 0);
    136     call("free", zero, 0);
    137     assert(!calls);
    138 }
    139 
    140 int main(void)
    141 {
    142 #ifdef _WIN32
    143     crt = LoadLibraryA("msvcrt.dll");
    144     assert(crt);
    145     uint32_t callback = (uintptr_t)handler;
    146 #else
    147     assert(guest_map("new-handler fixture", 0x50000000u, 0x10000));
    148     g_fs_base = 0x50004000u;
    149     uint32_t callback = 0x11110000u;
    150 #endif
    151     registration_preserves_previous_values(callback);
    152     allocation_failure("malloc", 0xfffff000u, 0, 0);
    153     allocation_failure("malloc", 0xfffff000u, 1, 4);
    154     allocation_failure("realloc", 0xfffff000u, 0, 0);
    155     allocation_failure("realloc", 0xfffff000u, 1, 4);
    156     allocation_failure("calloc", 0xfffff000u, 0, 0);
    157     allocation_failure("calloc", 0xfffff000u, 1, 4);
    158     allocation_failure("??2@YAPAXI@Z", 0xfffff000u, 0, 4);
    159     allocation_failure("??2@YAPAXI@Z", 0xfffff000u, 1, 5);
    160     allocation_failure("malloc", UINT32_MAX, 0, 1);
    161     allocation_failure("malloc", UINT32_MAX, 1, 1);
    162     allocation_failure("realloc", UINT32_MAX, 0, 1);
    163     allocation_failure("realloc", UINT32_MAX, 1, 1);
    164     allocation_failure("calloc", UINT32_MAX, 0, 0);
    165     allocation_failure("calloc", UINT32_MAX, 1, 0);
    166     allocation_failure("??2@YAPAXI@Z", UINT32_MAX, 0, 4);
    167     allocation_failure("??2@YAPAXI@Z", UINT32_MAX, 1, 4);
    168     successful_and_zero_allocations();
    169     call(set_handler, 0, 0);
    170     call("?_set_new_mode@@YAHH@Z", 0, 0);
    171     puts("new handler: registration, modes, 16 failure paths, retries, realloc preservation and ABI passed");
    172     return 0;
    173 }