aorecomp

Anarchy Online classic in the browser

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


client/runtime/linux_host.c (10659B)


      1 #define _GNU_SOURCE
      2 #include "browser_profile.h"
      3 #include "recomp_types.h"
      4 #include "game-data.h"
      5 #include "game-modules.h"
      6 #include "guest_crt.h"
      7 #include "linux_imports.h"
      8 #include "linux_graphics.h"
      9 #include "guest_exception.h"
     10 #include "guest_msvcp.h"
     11 #include "../vendor/msvcp60/source/module.h"
     12 #include "../vendor/ace/source/module.h"
     13 #include <assert.h>
     14 #include <strings.h>
     15 #include <pthread.h>
     16 #include <unistd.h>
     17 #include <sys/syscall.h>
     18 #include "linux_thread.h"
     19 #include "linux_snapshot.h"
     20 
     21 static struct { const char *name; uint32_t base, entry; int disable_thread_calls; }
     22     modules[sizeof(game_modules) / sizeof(game_modules[0]) + 2];
     23 static const int module_count = sizeof(modules) / sizeof(modules[0]);
     24 
     25 int linux_module_entry(unsigned index, const char **name, uint32_t *base, uint32_t *size)
     26 {
     27     if (index >= module_count) return 0;
     28     *name = modules[index].name;
     29     *base = modules[index].base;
     30     *size = MEM32(*base + MEM32(*base + 0x3c) + 80);
     31     return 1;
     32 }
     33 
     34 /* The retained PE metadata describes guest imports/exports, never host pointers. */
     35 static uint32_t directory(uint32_t base, unsigned index)
     36 {
     37     return base + MEM32(base + MEM32(base + 0x3c) + 24 + 96 + index * 8);
     38 }
     39 
     40 static int module_named(const char *name)
     41 {
     42     for (int i = 0; i < module_count; ++i)
     43         if (!strcasecmp(name, modules[i].name)) return i;
     44     return -1;
     45 }
     46 
     47 static uint32_t exported(int module, const char *name, uint32_t ordinal)
     48 {
     49     uint32_t base = modules[module].base;
     50     uint32_t exports = directory(base, 0);
     51     if (exports == base) return 0;
     52     uint32_t functions = base + MEM32(exports + 28);
     53     if (name) {
     54         uint32_t names = base + MEM32(exports + 32);
     55         uint32_t ordinals = base + MEM32(exports + 36);
     56         for (uint32_t i = 0; i < MEM32(exports + 24); ++i) {
     57             if (!strcmp(name, (const char *)ADDR(base + MEM32(names + i * 4)))) {
     58                 uint32_t rva = MEM32(functions + MEM16(ordinals + i * 2) * 4);
     59                 return rva ? base + rva : 0;
     60             }
     61         }
     62     } else if (ordinal >= MEM32(exports + 16) &&
     63                ordinal - MEM32(exports + 16) < MEM32(exports + 20)) {
     64         uint32_t rva = MEM32(functions + (ordinal - MEM32(exports + 16)) * 4);
     65         return rva ? base + rva : 0;
     66     }
     67     return 0;
     68 }
     69 
     70 uint32_t linux_module_export(uint32_t handle, const char *name, uint32_t ordinal)
     71 {
     72     for (int i = 0; i < module_count; ++i) {
     73         if (modules[i].base != handle) continue;
     74         uint32_t address = exported(i, name, ordinal);
     75         if (!address) MEM32(g_fs_base + 0x34) = 127; /* ERROR_PROC_NOT_FOUND */
     76         return address;
     77     }
     78     MEM32(g_fs_base + 0x34) = 126; /* ERROR_MOD_NOT_FOUND */
     79     return 0;
     80 }
     81 
     82 static void bind_modules(void)
     83 {
     84     for (int i = 0; i < module_count; ++i) {
     85         uint32_t base = modules[i].base;
     86         uint32_t desc = directory(base, 1);
     87         if (desc == base) continue;
     88         for (; MEM32(desc + 12); desc += 20) {
     89             const char *dll = (const char *)ADDR(base + MEM32(desc + 12));
     90             int module = module_named(dll);
     91             uint32_t names = base + MEM32(desc);
     92             uint32_t slots = base + MEM32(desc + 16);
     93             if (names == base) names = slots;
     94             for (; MEM32(names); names += 4, slots += 4) {
     95                 uint32_t entry = MEM32(names);
     96                 const char *name = entry & 0x80000000u ? NULL : (const char *)ADDR(base + entry + 2);
     97                 uint32_t ordinal = name ? 0 : entry & 0xffffu;
     98                 if (name && !strcasecmp(dll, "msvcp60.dll") && guest_msvcp_lookup(name))
     99                     MEM32(slots) = linux_import_bind(dll, name, ordinal);
    100                 else
    101                     MEM32(slots) = module >= 0 ? exported(module, name, ordinal) : linux_import_bind(dll, name, ordinal);
    102                 if (!MEM32(slots)) {
    103                     fprintf(stderr, "Missing game export: %s!%s ordinal %u\n", dll, name ? name : "", ordinal);
    104                     exit(3);
    105                 }
    106             }
    107         }
    108     }
    109 }
    110 
    111 recomp_func_t recomp_lookup_manual(uint32_t va) { (void)va; return NULL; }
    112 
    113 recomp_func_t recomp_lookup(uint32_t va)
    114 {
    115     recomp_func_t adapter = guest_exception_virtual(va);
    116     if (!adapter) adapter = linux_graphics_virtual(va);
    117     if (adapter) return adapter;
    118     if (va >= 0xf0000000u) return NULL;
    119     /* Guest code identities are immutable; virtual adapters retain their dispatch state. */
    120     static _Thread_local struct { uint32_t address; recomp_func_t function; } cache[4096];
    121     unsigned bucket = (va >> 4) % 4096;
    122     if (cache[bucket].function && cache[bucket].address == va) return cache[bucket].function;
    123     const recomp_dispatch_entry_t *table = recomp_dispatch_table;
    124     uint32_t lo = 0, hi = recomp_dispatch_count;
    125     if (va >= 0x46000000u && va < 0x47000000u) {
    126         table = msvcp_dispatch;
    127         hi = msvcp_dispatch_count;
    128     } else if (va >= 0x47000000u && va < 0x48000000u) {
    129         table = ace_dispatch;
    130         hi = ace_dispatch_count;
    131     }
    132     while (lo < hi) {
    133         uint32_t mid = lo + (hi - lo) / 2;
    134         if (table[mid].address == va) {
    135             cache[bucket].address = va;
    136             cache[bucket].function = table[mid].func;
    137             return table[mid].func;
    138         }
    139         if (table[mid].address < va) lo = mid + 1;
    140         else hi = mid;
    141     }
    142     return NULL;
    143 }
    144 
    145 uint32_t recomp_funcinfo(uint32_t handler)
    146 {
    147     uint32_t info = game_funcinfo(handler);
    148     if (!info) info = msvcp_funcinfo(handler);
    149     return info ? info : ace_funcinfo(handler);
    150 }
    151 
    152 static void disable_thread_library_calls(void)
    153 {
    154     uint32_t module = MEM32(g_esp + 4);
    155     g_eax = 0;
    156     for (int i = 0; i < module_count; ++i)
    157         if (modules[i].base == module) { modules[i].disable_thread_calls = 1; g_eax = 1; }
    158     if (!g_eax) MEM32(g_fs_base + 0x34) = 126; /* ERROR_MOD_NOT_FOUND */
    159     g_esp += 8;
    160 }
    161 
    162 static void get_module_handle(void)
    163 {
    164     uint32_t name = MEM32(g_esp + 4);
    165     int module = module_named(name ? (const char *)ADDR(name) : "Clientr.exe");
    166     g_eax = module >= 0 ? modules[module].base : 0;
    167     if (!g_eax) MEM32(g_fs_base + 0x34) = 126; /* ERROR_MOD_NOT_FOUND */
    168     g_esp += 8;
    169 }
    170 
    171 recomp_func_t linux_module_lookup(const char *name)
    172 {
    173     if (!strcmp(name, "GetModuleHandleA")) return get_module_handle;
    174     if (!strcmp(name, "DisableThreadLibraryCalls")) return disable_thread_library_calls;
    175     return NULL;
    176 }
    177 
    178 static void call_guest(uint32_t entry, const uint32_t *args, unsigned count)
    179 {
    180     recomp_func_t function = recomp_lookup(entry);
    181     if (!function) {
    182         fprintf(stderr, "Missing guest entry %08x\n", entry);
    183         exit(3);
    184     }
    185     uint32_t stack = g_esp;
    186     for (unsigned i = count; i; --i) PUSH32(g_esp, args[i - 1]);
    187     PUSH32(g_esp, RECOMP_RETADDR);
    188     function();
    189     if (g_esp != stack) {
    190         fprintf(stderr, "Guest entry %08x returned with unbalanced stack\n", entry);
    191         exit(3);
    192     }
    193 }
    194 
    195 static unsigned char attached[sizeof(modules) / sizeof(modules[0])];
    196 static int initialization_order[sizeof(modules) / sizeof(modules[0])];
    197 static int initialized_count;
    198 static pthread_mutex_t loader_mutex = PTHREAD_MUTEX_INITIALIZER;
    199 
    200 void linux_thread_notify(uint32_t reason)
    201 {
    202     /* DLL callbacks follow dependency initialization order, reversed on detach. */
    203     if (pthread_mutex_lock(&loader_mutex)) abort();
    204     for (int i = 0; i < initialized_count; ++i) {
    205         int module = initialization_order[reason == 2 ? i : initialized_count - i - 1];
    206         if (modules[module].disable_thread_calls) continue;
    207         uint32_t args[] = {modules[module].base, reason, 0};
    208         call_guest(modules[module].entry, args, 3);
    209     }
    210     if (pthread_mutex_unlock(&loader_mutex)) abort();
    211 }
    212 
    213 
    214 static void attach_module(int module)
    215 {
    216     if (attached[module]) return;
    217     attached[module] = 1;
    218     uint32_t base = modules[module].base;
    219     uint32_t desc = directory(base, 1);
    220     if (desc != base) {
    221         for (; MEM32(desc + 12); desc += 20) {
    222             int dependency = module_named((const char *)ADDR(base + MEM32(desc + 12)));
    223             if (dependency >= 0) attach_module(dependency);
    224         }
    225     }
    226     if (MEM16(base + MEM32(base + 0x3c) + 22) & 0x2000) {
    227         fprintf(stderr, "[linux-host] attach %s\n", modules[module].name);
    228         uint32_t args[] = {base, 1, 1};
    229         call_guest(modules[module].entry, args, 3);
    230         initialization_order[initialized_count++] = module;
    231         if (!g_eax) {
    232             fprintf(stderr, "Guest DLL rejected initialization: %s\n", modules[module].name);
    233             exit(3);
    234         }
    235     }
    236 }
    237 
    238 int main(int argc, char **argv)
    239 {
    240     extern char **environ;
    241 #ifdef __EMSCRIPTEN__
    242     extern void browser_initialize(unsigned character_id);
    243     browser_profile_initialize(&g_cur_func);
    244     double setup_start = ao_profile_begin("asset setup");
    245     assert(argc > 3);
    246     browser_initialize((unsigned)strtoul(argv[3], NULL, 10));
    247     ao_profile_end("asset setup", setup_start, NULL);
    248 #endif
    249     setvbuf(stderr, NULL, _IONBF, 0);
    250     for (int i = 0; i < game_module_count; ++i) {
    251         modules[i].name = game_modules[i].name;
    252         modules[i].base = game_modules[i].base;
    253         modules[i].entry = game_modules[i].entry;
    254     }
    255     modules[game_module_count].name = "msvcp60.dll";
    256     modules[game_module_count].base = 0x46000000u;
    257     modules[game_module_count].entry = MSVCP_ENTRY;
    258     modules[game_module_count + 1].name = "ace.dll";
    259     modules[game_module_count + 1].base = 0x47000000u;
    260     modules[game_module_count + 1].entry = ACE_ENTRY;
    261     game_data_init();
    262     msvcp_data_init();
    263     ace_data_init();
    264     guest_crt_startup(argc, argv, environ);
    265     bind_modules();
    266     fprintf(stderr, "[linux-host] initialized %d modules; bound %u external imports\n",
    267             module_count, linux_import_count());
    268 
    269     /* Guest stack and thread information remain independent of the host ABI. */
    270     if (!guest_map("stack", 0x08000000u, 0x100000u) ||
    271         !guest_map("thread information", 0x08400000u, 0x10000u)) abort();
    272     g_esp = 0x080fff00u;
    273     g_fs_base = 0x08400000u;
    274     MEM32(g_fs_base) = 0xffffffffu;
    275     MEM32(g_fs_base + 4) = 0x08100000u;
    276     MEM32(g_fs_base + 8) = 0x08000000u;
    277     MEM32(g_fs_base + 0x18) = g_fs_base;
    278     MEM32(g_fs_base + 0x20) = getpid();
    279     MEM32(g_fs_base + 0x24) = gettid();
    280     int client = module_named("Clientr.exe");
    281     if (pthread_mutex_lock(&loader_mutex)) abort();
    282     attach_module(client);
    283     if (pthread_mutex_unlock(&loader_mutex)) abort();
    284     call_guest(modules[client].entry, NULL, 0);
    285 #ifdef __EMSCRIPTEN__
    286     linux_graphics_shutdown();
    287     extern void ao_game_closed(void);
    288     ao_game_closed();
    289 #endif
    290     return 0;
    291 }