aorecomp

Anarchy Online classic in the browser

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


client/tests/linecrypt.c (3253B)


      1 #include "recomp_types.h"
      2 #include "guest_memory.h"
      3 #include "guest_crt.h"
      4 #include <assert.h>
      5 
      6 void game_data_yr_dll(void);
      7 void sub_45001000(void);
      8 void sub_45001100(void);
      9 
     10 recomp_func_t recomp_lookup_manual(uint32_t va) { (void)va; return NULL; }
     11 recomp_func_t recomp_lookup(uint32_t va)
     12 {
     13     if (va == 0x45001000u) return sub_45001000;
     14     if (va == 0x45001100u) return sub_45001100;
     15     return NULL;
     16 }
     17 
     18 #ifdef _WIN32
     19 /* The Windows reference calls the native CRT with the guest cdecl arguments. */
     20 static void reference_strncpy(void)
     21 {
     22     g_eax = (uintptr_t)strncpy((char *)ADDR(MEM32(g_esp + 4)),
     23                              (const char *)ADDR(MEM32(g_esp + 8)), MEM32(g_esp + 12));
     24     g_esp += 4;
     25 }
     26 
     27 static void reference_strchr(void)
     28 {
     29     g_eax = (uintptr_t)strchr((const char *)ADDR(MEM32(g_esp + 4)), MEM32(g_esp + 8));
     30     g_esp += 4;
     31 }
     32 #endif
     33 
     34 recomp_func_t recomp_lookup_import(uint32_t va)
     35 {
     36 #ifdef _WIN32
     37     if (va == 0xf0000000u) return reference_strncpy;
     38     if (va == 0xf0000004u) return reference_strchr;
     39 #else
     40     if (va == 0xf0000000u) return guest_crt_lookup("strncpy");
     41     if (va == 0xf0000004u) return guest_crt_lookup("strchr");
     42 #endif
     43     fprintf(stderr, "Unexpected test import 0x%08x\n", va);
     44     abort();
     45 }
     46 
     47 static void memory_test(void)
     48 {
     49     assert(guest_map("test memory", 0x50000000u, 0x20000u));
     50     assert(guest_map("separate range", 0x5ffe0000u, 0x10000u));
     51 
     52     MEM32(0x5000ffffu) = 0x12345678u;
     53     MEM32(0x5ffe0001u) = 0x5000ffffu;
     54 
     55     assert(MEM8(0x50010000u) == 0x56);
     56     assert(MEM32(MEM32(0x5ffe0001u)) == 0x12345678u);
     57     assert(ADDR(0) == 0);
     58 #ifdef RECOMP_PORTABLE
     59     assert(ADDR(0x50000000u) != 0x50000000u);
     60     assert(sizeof(void *) == 8);
     61 #else
     62     assert(ADDR(0x50000000u) == 0x50000000u);
     63     assert(sizeof(void *) == 4);
     64 #endif
     65 }
     66 
     67 int main(void)
     68 {
     69     memory_test();
     70     game_data_yr_dll();
     71     assert(guest_map("test stack", 0x10000000u, 0x10000u));
     72     MEM32(0x45002010u) = 0xf0000000u;
     73     MEM32(0x45002018u) = 0xf0000004u;
     74     uint32_t digest = 2166136261u, random = 0x12345678u;
     75 
     76     for (unsigned sample = 0; sample < 1024; ++sample) {
     77         unsigned length = sample % 256;
     78         unsigned char *text = (unsigned char *)ADDR(0x5000fff0u);
     79         for (unsigned i = 0; i < length; ++i) {
     80             random = random * 1664525u + 1013904223u;
     81             text[i] = 1 + random % 255;
     82         }
     83         text[length] = 0;
     84         memcpy((void *)ADDR(0x50001000u), "AO1491!?", 9);
     85         g_esp = 0x1000ff00u;
     86         MEM32(g_esp) = RECOMP_RETADDR;
     87         MEM32(g_esp + 4) = 0x5000fff0u;
     88         MEM32(g_esp + 8) = 0x50001000u;
     89         g_ebx = 0x11223344u;
     90         g_ebp = 0x55667788u;
     91         g_esi = 0x99aabbccu;
     92         g_edi = 0xddeeff00u;
     93 
     94         sub_45001000();
     95 
     96         assert(g_eax == 0);
     97         assert(g_esp == 0x1000ff04u);
     98         assert(g_ebx == 0x11223344u && g_ebp == 0x55667788u);
     99         assert(g_esi == 0x99aabbccu && g_edi == 0xddeeff00u);
    100         assert(text[length] == 0);
    101         for (unsigned i = 0; i <= length; ++i)
    102             digest = (digest ^ text[i]) * 16777619u;
    103     }
    104     assert(digest == 0xae00848au);
    105     assert(g_icall_count == 262144u);
    106     printf("linecrypt: 1024 cases, digest=%08x, indirect calls=%u\n", digest, g_icall_count);
    107     return 0;
    108 }