aorecomp

Anarchy Online classic in the browser

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


client/tests/platform_diagnostics.c (4333B)


      1 #define _GNU_SOURCE
      2 #include "recomp_types.h"
      3 #include "guest_memory.h"
      4 #include "guest_crt.h"
      5 #include "guest_heap.h"
      6 #include "guest_msvcp.h"
      7 #include "linux_platform.h"
      8 #include <assert.h>
      9 #include <sys/stat.h>
     10 #include <sys/utsname.h>
     11 #include <unistd.h>
     12 
     13 void game_data_ffr_dll(void);
     14 void sub_2F009AA0(void);
     15 void sub_2F0070F0(void);
     16 
     17 recomp_func_t recomp_lookup_manual(uint32_t address) { (void)address; return NULL; }
     18 recomp_func_t recomp_lookup(uint32_t address)
     19 {
     20     if (address == 0x2f009aa0u) return sub_2F009AA0;
     21     if (address == 0x2f0117dcu) return guest_crt_lookup("??2@YAPAXI@Z");
     22     if (address == 0xf0000000u) return NULL;
     23     fprintf(stderr, "Unexpected diagnostics guest call %08x\n", address);
     24     abort();
     25 }
     26 recomp_func_t recomp_lookup_import(uint32_t address)
     27 {
     28     assert(address == 0xf0000000u);
     29     return linux_platform_lookup("kernel32.dll", "DeleteFileA");
     30 }
     31 
     32 static void system_descriptions_reach_guest_state(void)
     33 {
     34     struct utsname system;
     35     assert(!uname(&system));
     36     strcpy((char *)ADDR(0x2f01f5a0u), "AnarchyCrashLog.txt");
     37     FILE *stale = fopen("AnarchyCrashLog.txt", "w");
     38     assert(stale && fputs("stale report", stale) >= 0 && !fclose(stale));
     39     MEM32(0x2f013054u) = 0xf0000000u;
     40     memset((void *)ADDR(0x2f01f048u), 0, 64);
     41     guest_msvcp_assign_host(0x2f01f048u, "stale OS description");
     42     MEM32(0x2f01f044u) = MEM32(0x2f01f088u) = 0x12345678u;
     43     g_esp = 0x5000ff00u;
     44     g_ebx = 0x12345678u;
     45     g_esi = 0x23456789u;
     46     g_edi = 0x3456789au;
     47     g_ebp = 0x456789abu;
     48 
     49     sub_2F0070F0();
     50 
     51     assert(!strcmp((const char *)ADDR(MEM32(0x2f01f04cu)), "Linux"));
     52     assert(!strcmp((const char *)ADDR(MEM32(0x2f01f05cu)), system.release));
     53     assert(strstr((const char *)ADDR(MEM32(0x2f01f06cu)), system.machine));
     54     assert(strstr((const char *)ADDR(MEM32(0x2f01f07cu)), " MB"));
     55     for (uint32_t object = 0x2f01f048u; object < 0x2f01f088u; object += 16) {
     56         assert(MEM32(object + 8) == strlen((const char *)ADDR(MEM32(object + 4))));
     57         assert(MEM32(object + 12) >= MEM32(object + 8));
     58         guest_msvcp_assign_host(object, "");
     59         assert(MEM32(object + 4) == 0 && MEM32(object + 8) == 0);
     60     }
     61     assert(MEM32(0x2f01f044u) == 0x12345678u && MEM32(0x2f01f088u) == 0x12345678u);
     62     uint32_t records = MEM32(0x2f01f154u);
     63     assert(records);
     64     for (unsigned i = 4; i < 48; i += 4) assert(MEM32(records + i) == 0);
     65     guest_free(records);
     66     assert(g_esp == 0x5000ff04u);
     67     assert(g_ebx == 0x12345678u && g_esi == 0x23456789u);
     68     assert(g_edi == 0x3456789au && g_ebp == 0x456789abu);
     69     assert(MEM8(0x2f01f131u) == 1);
     70     assert(access("AnarchyCrashLog.txt", F_OK) != 0);
     71     assert(!unlink("DXDiagOutput.txt"));
     72 }
     73 
     74 static void report_is_written(void)
     75 {
     76     g_esp = 0x5000ff00u;
     77     g_ebx = 0x12345678u;
     78     g_esi = 0x23456789u;
     79     g_edi = 0x3456789au;
     80     g_ebp = 0x456789abu;
     81     MEM8(0x2f01f131u) = 0;
     82 
     83     sub_2F009AA0();
     84 
     85     assert(MEM8(0x2f01f131u) == 1);
     86     assert(g_esp == 0x5000ff04u);
     87     assert(g_ebx == 0x12345678u && g_esi == 0x23456789u);
     88     assert(g_edi == 0x3456789au && g_ebp == 0x456789abu);
     89     FILE *file = fopen("DXDiagOutput.txt", "r");
     90     assert(file);
     91     char text[1024];
     92     size_t length = fread(text, 1, sizeof(text) - 1, file);
     93     text[length] = 0;
     94     assert(!fclose(file));
     95     assert(strstr(text, "System: Linux\n"));
     96     assert(strstr(text, "Host pointers: 64 bits\nGuest addresses: 32 bits\n"));
     97     assert(strstr(text, "DirectX diagnostics are unavailable on this platform.\n"));
     98     assert(!unlink("DXDiagOutput.txt"));
     99 }
    100 
    101 static void unavailable_report_clears_flag(void)
    102 {
    103     assert(!mkdir("DXDiagOutput.txt", 0700));
    104     MEM8(0x2f01f131u) = 1;
    105     g_esp = 0x5000ff00u;
    106 
    107     sub_2F009AA0();
    108 
    109     assert(MEM8(0x2f01f131u) == 0);
    110     assert(g_esp == 0x5000ff04u);
    111     assert(!rmdir("DXDiagOutput.txt"));
    112 }
    113 
    114 int main(void)
    115 {
    116     char directory[] = "/tmp/ao-diagnostics-XXXXXX";
    117     assert(mkdtemp(directory) && !chdir(directory));
    118     game_data_ffr_dll();
    119     assert(guest_map("diagnostic stack", 0x50000000u, 0x10000));
    120     report_is_written();
    121     unavailable_report_clears_flag();
    122     system_descriptions_reach_guest_state();
    123     assert(!chdir("/") && !rmdir(directory));
    124     puts("platform diagnostics: native report, guest system strings, crash state, failure and ABI passed");
    125     return 0;
    126 }