aorecomp

Anarchy Online classic in the browser

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


client/runtime/linux_snapshot.c (3756B)


      1 #include "linux_snapshot.h"
      2 #include <pthread.h>
      3 #include <unistd.h>
      4 
      5 /* All guest modules are compiled into one ELF and registered before startup.
      6  * Their immutable table is the snapshot; only each handle's cursor can change. */
      7 static struct snapshot { uint32_t handle; unsigned next; struct snapshot *link; } *snapshots;
      8 static uint32_t next_handle = 0x07000000;
      9 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
     10 
     11 static void create_snapshot(void)
     12 {
     13     uint32_t flags = MEM32(g_esp + 4), process = MEM32(g_esp + 8);
     14     if (flags != 8 || (process && process != (uint32_t)getpid())) {
     15         fprintf(stderr, "Only current-process module snapshots are implemented: flags=%x pid=%u\n", flags, process);
     16         abort();
     17     }
     18     struct snapshot *snapshot = calloc(1, sizeof(*snapshot));
     19     g_eax = UINT32_MAX;
     20     if (snapshot) {
     21         if (pthread_mutex_lock(&mutex)) abort();
     22         if (next_handle >= 0x08000000) abort();
     23         snapshot->handle = next_handle;
     24         next_handle += 4;
     25         snapshot->link = snapshots;
     26         snapshots = snapshot;
     27         g_eax = snapshot->handle;
     28         if (pthread_mutex_unlock(&mutex)) abort();
     29     } else MEM32(g_fs_base + 0x34) = 8;
     30     g_esp += 12;
     31 }
     32 
     33 static struct snapshot *find_snapshot_locked(uint32_t handle)
     34 {
     35     for (struct snapshot *snapshot = snapshots; snapshot; snapshot = snapshot->link)
     36         if (snapshot->handle == handle) return snapshot;
     37     MEM32(g_fs_base + 0x34) = 6;
     38     return NULL;
     39 }
     40 
     41 static void module_entry(int first)
     42 {
     43     uint32_t output = MEM32(g_esp + 8);
     44     if (pthread_mutex_lock(&mutex)) abort();
     45     struct snapshot *snapshot = find_snapshot_locked(MEM32(g_esp + 4));
     46     g_eax = 0;
     47     if (snapshot) {
     48         if (MEM32(output) < 548) MEM32(g_fs_base + 0x34) = 87; /* ERROR_INVALID_PARAMETER */
     49         else {
     50             const char *name;
     51             uint32_t base, size;
     52             unsigned index = first ? 0 : snapshot->next;
     53             if (linux_module_entry(index, &name, &base, &size)) {
     54                 /* MODULEENTRY32 retains the guest's x86 field and pointer widths. */
     55                 MEM32(output) = 548;
     56                 MEM32(output + 4) = 1;
     57                 MEM32(output + 8) = getpid();
     58                 MEM32(output + 12) = MEM32(output + 16) = 0xffff;
     59                 MEM32(output + 20) = MEM32(output + 28) = base;
     60                 MEM32(output + 24) = size;
     61                 strcpy((char *)ADDR(output + 32), name);
     62                 ssize_t length = readlink("/proc/self/exe", (char *)ADDR(output + 288), 259);
     63                 if (length < 0) { perror("module executable path"); abort(); }
     64                 MEM8(output + 288 + length) = 0;
     65                 snapshot->next = index + 1;
     66                 g_eax = 1;
     67             } else MEM32(g_fs_base + 0x34) = 18; /* ERROR_NO_MORE_FILES */
     68         }
     69     }
     70     if (pthread_mutex_unlock(&mutex)) abort();
     71     g_esp += 12;
     72 }
     73 
     74 static void module_first(void) { module_entry(1); }
     75 static void module_next(void) { module_entry(0); }
     76 
     77 static void close_snapshot(void)
     78 {
     79     if (pthread_mutex_lock(&mutex)) abort();
     80     struct snapshot *snapshot = find_snapshot_locked(MEM32(g_esp + 4));
     81     g_eax = snapshot != NULL;
     82     if (snapshot) {
     83         struct snapshot **link = &snapshots;
     84         while (*link != snapshot) link = &(*link)->link;
     85         *link = snapshot->link;
     86         free(snapshot);
     87     }
     88     if (pthread_mutex_unlock(&mutex)) abort();
     89     g_esp += 8;
     90 }
     91 
     92 recomp_func_t linux_snapshot_lookup(const char *name)
     93 {
     94     if (!strcmp(name, "CreateToolhelp32Snapshot")) return create_snapshot;
     95     if (!strcmp(name, "Module32First")) return module_first;
     96     if (!strcmp(name, "Module32Next")) return module_next;
     97     if (!strcmp(name, "CloseHandle")) return close_snapshot;
     98     return NULL;
     99 }