aorecomp

Anarchy Online classic in the browser

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


client/tests/resource_atomics.c (5135B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #ifdef _WIN32
      5 #define WIN32_LEAN_AND_MEAN
      6 #include <windows.h>
      7 static volatile uint32_t counter;
      8 #else
      9 #include "recomp_types.h"
     10 #include <pthread.h>
     11 void sub_31004590(void);
     12 void sub_31004740(void);
     13 void sub_310045B0(void);
     14 recomp_func_t recomp_lookup(uint32_t address) { (void)address; return NULL; }
     15 recomp_func_t recomp_lookup_manual(uint32_t address) { (void)address; return NULL; }
     16 recomp_func_t recomp_lookup_import(uint32_t address) { (void)address; abort(); }
     17 static _Thread_local uint32_t stack_base = 0x50000000;
     18 #endif
     19 
     20 static void set_counter(uint32_t value)
     21 {
     22 #ifdef _WIN32
     23     counter = value;
     24 #else
     25     MEM32(0x31051118) = value;
     26 #endif
     27 }
     28 
     29 static uint32_t get_counter(void)
     30 {
     31 #ifdef _WIN32
     32     return counter;
     33 #else
     34     return MEM32(0x31051118);
     35 #endif
     36 }
     37 
     38 static uint32_t change(int operation, uint32_t carry, uint32_t *flags)
     39 {
     40 #ifdef _WIN32
     41     uint32_t previous = counter;
     42     if (operation == 0) {
     43         __asm__ volatile("1: leal 1(%0),%%edx; lock cmpxchgl %%edx,%1; jne 1b; pushfl; popl %2"
     44                          : "+&a"(previous), "+m"(counter), "=r"(*flags) : : "edx", "cc", "memory");
     45     } else if (operation == 1) {
     46         __asm__ volatile("1: leal -1(%0),%%edx; lock cmpxchgl %%edx,%1; jne 1b; pushfl; popl %2"
     47                          : "+&a"(previous), "+m"(counter), "=r"(*flags) : : "edx", "cc", "memory");
     48     } else {
     49         __asm__ volatile("btl $0,%2; lock incl %1; pushfl; popl %0"
     50                          : "=r"(*flags), "+m"(counter) : "r"(carry) : "cc", "memory");
     51     }
     52     *flags &= 0x8d5;
     53     return previous;
     54 #else
     55     g_esp = stack_base + 0xff00;
     56     MEM32(g_esp) = RECOMP_RETADDR;
     57     MEM32(g_esp + 4) = 0x31051118;
     58     g_ecx = stack_base + 0x100;
     59     g_ebx = 0x12345678; g_esi = 0x23456789; g_edi = 0x3456789a; g_ebp = 0x456789ab;
     60     g_flag_k = FK_EFLAGS; g_flag_a = carry; g_flag_b = 0; g_flag_cf = carry;
     61     if (operation == 0) sub_31004590();
     62     else if (operation == 1) sub_31004740();
     63     else sub_310045B0();
     64     assert(g_esp == stack_base + 0xff04);
     65     assert(g_ebx == 0x12345678 && g_esi == 0x23456789 && g_edi == 0x3456789a && g_ebp == 0x456789ab);
     66     if (operation == 2) assert(MEM32(stack_base + 0x100) == 0x31051118 && g_eax == stack_base + 0x100);
     67     *flags = recomp_eflags(g_flag_k, g_flag_a, g_flag_b, g_flag_cf, 1) & 0x8d5;
     68     return operation == 2 ? g_flag_a : g_eax;
     69 #endif
     70 }
     71 
     72 static void boundaries_preserve_results_and_flags(void)
     73 {
     74     const uint32_t inputs[] = {0, 1, 0x7fffffff, 0x80000000, 0xffffffff};
     75     const uint32_t increment_flags[] = {0, 1, 0x894, 0x81, 0x54};
     76     for (unsigned i = 0; i < 5; ++i) {
     77         for (unsigned operation = 0; operation < 3; ++operation) {
     78             uint32_t flags;
     79             set_counter(inputs[i]);
     80 
     81             uint32_t previous = change(operation, i & 1, &flags);
     82 
     83             assert(previous == inputs[i]);
     84             assert(get_counter() == inputs[i] + (operation == 1 ? UINT32_MAX : 1));
     85             assert(flags == (operation == 2 ? increment_flags[i] : 0x44));
     86         }
     87     }
     88 }
     89 
     90 struct worker { unsigned index, operation; uint64_t sum; };
     91 #ifdef _WIN32
     92 static DWORD WINAPI contend(void *argument)
     93 #else
     94 static void *contend(void *argument)
     95 #endif
     96 {
     97     struct worker *worker = argument;
     98 #ifndef _WIN32
     99     stack_base = 0x50000000 + worker->index * 0x10000;
    100 #endif
    101     for (unsigned i = 0; i < 50000; ++i) {
    102         uint32_t flags;
    103         worker->sum += change(worker->operation, 0, &flags);
    104         if (worker->operation != 2) assert(flags == 0x44);
    105     }
    106     return 0;
    107 }
    108 
    109 static void concurrent_updates_are_not_lost(void)
    110 {
    111     for (unsigned operation = 0; operation < 3; ++operation) {
    112         struct worker workers[8];
    113 #ifdef _WIN32
    114         HANDLE threads[8];
    115 #else
    116         pthread_t threads[8];
    117 #endif
    118         set_counter(operation == 1 ? 400000 : 0);
    119 
    120         for (unsigned i = 0; i < 8; ++i) {
    121             workers[i] = (struct worker){i, operation, 0};
    122 #ifdef _WIN32
    123             threads[i] = CreateThread(NULL, 0, contend, &workers[i], 0, NULL);
    124             assert(threads[i]);
    125 #else
    126             assert(!pthread_create(&threads[i], NULL, contend, &workers[i]));
    127 #endif
    128         }
    129         uint64_t sum = 0;
    130         for (unsigned i = 0; i < 8; ++i) {
    131 #ifdef _WIN32
    132             assert(WaitForSingleObject(threads[i], 10000) == WAIT_OBJECT_0);
    133             assert(CloseHandle(threads[i]));
    134 #else
    135             assert(!pthread_join(threads[i], NULL));
    136 #endif
    137             sum += workers[i].sum;
    138         }
    139 
    140         assert(get_counter() == (operation == 1 ? 0 : 400000));
    141         /* INC does not return its previous value in the native instruction. */
    142         if (operation != 2) assert(sum == (operation == 1 ? 80000200000ull : 79999800000ull));
    143     }
    144 }
    145 
    146 int main(void)
    147 {
    148 #ifndef _WIN32
    149     assert(guest_map("resource atomic counter", 0x31050000, 0x10000));
    150     assert(guest_map("resource atomic stacks", 0x50000000, 0x80000));
    151 #endif
    152     boundaries_preserve_results_and_flags();
    153     concurrent_updates_are_not_lost();
    154     puts("AO resource atomics: 15 boundary/flag cases and 1200000 contended updates across 8 threads passed");
    155     return 0;
    156 }