aorecomp

Anarchy Online classic in the browser

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


client/tests/module_snapshot.c (3572B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #ifdef _WIN32
      6 #define WIN32_LEAN_AND_MEAN
      7 #include <windows.h>
      8 #include <tlhelp32.h>
      9 #else
     10 #include "linux_snapshot.h"
     11 #include <unistd.h>
     12 
     13 int linux_module_entry(unsigned index, const char **name, uint32_t *base, uint32_t *size)
     14 {
     15     if (index >= 2) return 0;
     16     *name = index ? "ar.dll" : "Clientr.exe";
     17     *base = index ? 0x24000000 : 0x28000000;
     18     *size = index ? 0x10000 : 0x20000;
     19     return 1;
     20 }
     21 
     22 static uint32_t call(const char *name, uint32_t first, uint32_t second)
     23 {
     24     g_esp = 0x5000ff00;
     25     MEM32(g_esp + 4) = first;
     26     MEM32(g_esp + 8) = second;
     27     g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456;
     28     linux_snapshot_lookup(name)();
     29     assert(g_esp == 0x5000ff00 + (!strcmp(name, "CloseHandle") ? 8 : 12));
     30     assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
     31     return g_eax;
     32 }
     33 #endif
     34 
     35 static uint32_t create(void)
     36 {
     37 #ifdef _WIN32
     38     return (uintptr_t)CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0);
     39 #else
     40     return call("CreateToolhelp32Snapshot", 8, 0);
     41 #endif
     42 }
     43 
     44 static uint32_t query(uint32_t snapshot, uint32_t *values, int first)
     45 {
     46 #ifdef _WIN32
     47     return first ? Module32First((HANDLE)(uintptr_t)snapshot, (MODULEENTRY32 *)values) :
     48         Module32Next((HANDLE)(uintptr_t)snapshot, (MODULEENTRY32 *)values);
     49 #else
     50     memcpy((void *)ADDR(0x50001000), values, 552);
     51     uint32_t result = call(first ? "Module32First" : "Module32Next", snapshot, 0x50001000);
     52     memcpy(values, (void *)ADDR(0x50001000), 552);
     53     return result;
     54 #endif
     55 }
     56 
     57 static uint32_t error(void)
     58 {
     59 #ifdef _WIN32
     60     return GetLastError();
     61 #else
     62     return MEM32(g_fs_base + 0x34);
     63 #endif
     64 }
     65 
     66 static uint32_t close_snapshot(uint32_t snapshot)
     67 {
     68 #ifdef _WIN32
     69     return CloseHandle((HANDLE)(uintptr_t)snapshot);
     70 #else
     71     return call("CloseHandle", snapshot, 0);
     72 #endif
     73 }
     74 
     75 static void enumeration_preserves_layout_and_independent_cursors(void)
     76 {
     77     uint32_t first = create(), second = create(), values[138];
     78     assert(first != UINT32_MAX && second != UINT32_MAX && first != second);
     79     memset(values, 0xa5, sizeof(values));
     80     values[0] = 548;
     81 
     82     assert(query(first, values, 1));
     83     uint32_t first_base = values[5];
     84     assert(query(first, values, 0));
     85     assert(values[5] != first_base);
     86     assert(query(second, values, 1));
     87 
     88     assert(values[0] == 548 && values[1] == 1);
     89 #ifdef _WIN32
     90     assert(values[2] == GetCurrentProcessId());
     91 #else
     92     assert(values[2] == (uint32_t)getpid());
     93     assert(values[5] == 0x28000000 && values[6] == 0x20000);
     94     assert(!strcmp((char *)(values + 8), "Clientr.exe"));
     95 #endif
     96     assert(values[5] == first_base && values[5] == values[7] && values[6]);
     97     assert(((char *)values)[32] && ((char *)values)[288]);
     98     assert(values[137] == 0xa5a5a5a5);
     99     while (query(second, values, 0)) {}
    100     assert(error() == 18);
    101     assert(query(second, values, 1) && values[5] == first_base);
    102     values[0] = 0;
    103     assert(!query(first, values, 1) && error() == 87);
    104     values[0] = 548;
    105     assert(close_snapshot(first));
    106     assert(!query(first, values, 1) && error() == 6);
    107     assert(!close_snapshot(first) && error() == 6);
    108     assert(query(second, values, 1));
    109     assert(close_snapshot(second));
    110 }
    111 
    112 int main(void)
    113 {
    114 #ifndef _WIN32
    115     assert(guest_map("snapshot fixture", 0x50000000, 0x10000));
    116     g_fs_base = 0x50003000;
    117 #endif
    118     enumeration_preserves_layout_and_independent_cursors();
    119     puts("Module snapshots: x86 layout, independent cursors, restart, end/errors, handle lifetime and ABI passed");
    120     return 0;
    121 }