aorecomp

Anarchy Online classic in the browser

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


tools/client/native32_probe.c (4682B)


      1 #define WIN32_LEAN_AND_MEAN
      2 #include <windows.h>
      3 #include <assert.h>
      4 #include <stdio.h>
      5 #include <string.h>
      6 #include "native32.h"
      7 
      8 /* Test bodies occupy guest addresses; the mapped original bytes remain NX. */
      9 static unsigned additions, reentries, floats;
     10 
     11 static void guest_add(void)
     12 {
     13     additions++;
     14     g_eax = MEM32(g_esp + 4) + MEM32(g_esp + 8);
     15     g_esp += 12;
     16 }
     17 
     18 static int __cdecl native_reenter(int a, int b)
     19 {
     20     int (__stdcall *callback)(int, int) = (void *)0x20001000;
     21     return callback(a, b) + 100;
     22 }
     23 
     24 static void guest_reenter(void)
     25 {
     26     reentries++;
     27     uint32_t a = MEM32(g_esp + 4), b = MEM32(g_esp + 8);
     28     PUSH32(g_esp, b);
     29     PUSH32(g_esp, a);
     30     PUSH32(g_esp, RECOMP_RETADDR);
     31     recomp_func_t native = recomp_lookup_import((uint32_t)(uintptr_t)native_reenter);
     32     native();
     33     g_esp += 8;
     34     g_esp += 12;
     35 }
     36 
     37 static void guest_float(void)
     38 {
     39     floats++;
     40     fp_push_impl(g_st, &g_fp_top, (double)MEM32(g_esp + 4) / 2);
     41     g_esp += 4;
     42 }
     43 
     44 static char scan_outputs[29][4];
     45 static uint32_t scan_target;
     46 
     47 static void guest_scan(void)
     48 {
     49     for (int i = 28; i >= 0; i--) PUSH32(g_esp, (uint32_t)(uintptr_t)scan_outputs[i]);
     50     PUSH32(g_esp, (uint32_t)(uintptr_t)
     51         "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s");
     52     PUSH32(g_esp, (uint32_t)(uintptr_t)
     53         "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29");
     54     PUSH32(g_esp, RECOMP_RETADDR);
     55     recomp_lookup_import(scan_target)();
     56     g_esp += 31 * 4;
     57     g_esp += 4;
     58 }
     59 
     60 const recomp_dispatch_entry_t recomp_dispatch_table[] = {
     61     {0x20001000, guest_add},
     62     {0x20001010, guest_float},
     63     {0x21001000, guest_reenter},
     64     {0x21001010, guest_scan},
     65 };
     66 const uint32_t recomp_dispatch_count = 4;
     67 recomp_func_t recomp_lookup_manual(uint32_t va) { return NULL; }
     68 
     69 static void assert_nx(uint32_t address)
     70 {
     71     MEMORY_BASIC_INFORMATION info;
     72     assert(VirtualQuery((void *)(uintptr_t)address, &info, sizeof(info)));
     73     assert(!(info.Protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ |
     74                             PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)));
     75 }
     76 
     77 int main(int argc, char **argv)
     78 {
     79     native32_init();
     80     char first[MAX_PATH], second[MAX_PATH];
     81     GetSystemDirectoryA(first, MAX_PATH);
     82     GetSystemDirectoryA(second, MAX_PATH);
     83     strcat(first, "\\version.dll");
     84     strcat(second, "\\msimg32.dll");
     85     assert(native32_map(first, 0x20000000));
     86     assert(native32_map(second, 0x21000000));
     87     assert(native32_module("version.dll") == 0x20000000);
     88     assert(native32_module("msimg32.dll") == 0x21000000);
     89     assert_nx(0x20001000);
     90     assert_nx(0x21001000);
     91 
     92     /* Negative tests are separate processes because refusal must terminate. */
     93     if (argc == 2 && strcmp(argv[1], "unknown-data") == 0) {
     94         unsigned char *base = (void *)0x20000000;
     95         IMAGE_NT_HEADERS32 *nt = (void *)(base + ((IMAGE_DOS_HEADER *)base)->e_lfanew);
     96         IMAGE_SECTION_HEADER *section = IMAGE_FIRST_SECTION(nt);
     97         for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; i++, section++) {
     98             if (!(section->Characteristics & IMAGE_SCN_MEM_EXECUTE)) {
     99                 assert_nx((uint32_t)(uintptr_t)base + section->VirtualAddress);
    100                 recomp_lookup_import((uint32_t)(uintptr_t)base + section->VirtualAddress);
    101                 return 1;
    102             }
    103         }
    104         return 2;
    105     }
    106     if (argc == 2 && strcmp(argv[1], "enable-original") == 0) {
    107         native32_set_native(0x20000000);
    108         return 1;
    109     }
    110 
    111     int (__stdcall *add)(int, int) = (void *)0x20001000;
    112     int (__stdcall *reenter)(int, int) = (void *)0x21001000;
    113     double (__cdecl *half)(int) = (void *)0x20001010;
    114     int sum = add(9, 4);
    115     int nested = reenter(9, 4);
    116     double value = half(7);
    117 
    118     assert(sum == 13 && nested == 113 && value == 3.5);
    119     assert(additions == 2 && reentries == 1 && floats == 1);
    120     assert_nx(0x20001000);
    121     assert_nx(0x21001000);
    122     puts("PASS: two mapped modules, NX callbacks, nested native/guest calls, float return");
    123 
    124     scan_target = (uint32_t)(uintptr_t)GetProcAddress(LoadLibraryA("msvcrt.dll"), "sscanf");
    125     assert(scan_target);
    126     const char expected[29][4] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
    127         "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
    128         "24", "25", "26", "27", "28", "29"};
    129 
    130     int (__cdecl *scan)(void) = (void *)0x21001010;
    131     int scanned = scan();
    132 
    133     assert(scanned == 29);
    134     assert(memcmp(scan_outputs, expected, sizeof(expected)) == 0);
    135     assert_nx(0x21001010);
    136     puts("PASS: native sscanf receives all 31 arguments through the guest bridge");
    137     return 0;
    138 }