aorecomp

Anarchy Online classic in the browser

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


client/tests/browser_socket_events.c (4401B)


      1 #include "linux_network.h"
      2 #include "linux_sync.h"
      3 #include "guest_memory.h"
      4 #include <assert.h>
      5 #include <errno.h>
      6 #undef pthread_mutex_trylock
      7 #include <pthread.h>
      8 #include <stdatomic.h>
      9 #include <emscripten/threading.h>
     10 
     11 static atomic_int status, blocked_lock, retries;
     12 static pthread_mutex_t *socket_mutex;
     13 
     14 /* Simulate contention at each public event-delivery boundary on the UI thread. */
     15 int test_mutex_trylock(pthread_mutex_t *mutex)
     16 {
     17     if (emscripten_is_main_runtime_thread()) {
     18         if (!socket_mutex) socket_mutex = mutex;
     19         int blocked = atomic_load(&blocked_lock);
     20         if ((blocked == 1 && mutex == socket_mutex) ||
     21             (blocked == 2 && mutex != socket_mutex)) {
     22             atomic_fetch_add(&retries, 1);
     23             return EBUSY;
     24         }
     25     }
     26     return pthread_mutex_trylock(mutex);
     27 }
     28 
     29 /* Keep readiness under test control while using the production timer and C adapter. */
     30 int ao_socket_create(void) { return 0x09000000; }
     31 void ao_socket_connect(int handle, int port) {}
     32 int ao_socket_status(int handle) { return atomic_load(&status); }
     33 int ao_socket_available(int handle) { return 1; }
     34 int ao_socket_send(int handle, const void *data, int size) { return size; }
     35 int ao_socket_receive(int handle, void *data, int size, int peek)
     36 {
     37     *(unsigned char *)data = 0x5a;
     38     return 1;
     39 }
     40 void ao_socket_close(int handle) {}
     41 uint32_t guest_crt_errno_address(void) { return 0x50001000; }
     42 
     43 static uint32_t call(const char *name, unsigned count, const uint32_t *arguments)
     44 {
     45     recomp_func_t function = linux_network_lookup(name, 0);
     46     if (!function) function = linux_sync_lookup(name);
     47     assert(function);
     48     g_esp = 0x5000f000;
     49     for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = arguments[i];
     50     function();
     51     assert(g_esp == 0x5000f000 + (count + 1) * 4);
     52     return g_eax;
     53 }
     54 
     55 static void readiness_survives_contention(int lock)
     56 {
     57     atomic_store(&status, 0);
     58     atomic_store(&retries, 0);
     59     atomic_store(&blocked_lock, lock);
     60     uint32_t socket = call("socket", 3, (uint32_t[]){2, 1, 0});
     61     uint32_t event = call("CreateEventA", 4, (uint32_t[]){0, 1, 0, 0});
     62     assert(socket == 0x09000000 && event);
     63     assert(!call("WSAEventSelect", 3, (uint32_t[]){socket, event, 33}));
     64     atomic_store(&status, 2);
     65 
     66     uint32_t waiting = call("WaitForSingleObject", 2, (uint32_t[]){event, 100});
     67     atomic_store(&blocked_lock, 0);
     68     uint32_t delivered = call("WaitForSingleObject", 2, (uint32_t[]){event, 1000});
     69     assert(!call("WSAEnumNetworkEvents", 3, (uint32_t[]){socket, event, 0x50002000}));
     70 
     71     assert(waiting == 258 && atomic_load(&retries) > 0);
     72     assert(delivered == 0 && MEM32(0x50002000) == 1);
     73     assert(MEM32(0x50002018) == 0);
     74 
     75     uint32_t disarmed = call("WaitForSingleObject", 2, (uint32_t[]){event, 25});
     76     uint32_t received = call("recv", 4, (uint32_t[]){socket, 0x50003000, 1, 0});
     77     uint32_t rearmed = call("WaitForSingleObject", 2, (uint32_t[]){event, 1000});
     78     assert(!call("WSAEnumNetworkEvents", 3, (uint32_t[]){socket, event, 0x50002000}));
     79 
     80     assert(disarmed == 258 && received == 1 && MEM8(0x50003000) == 0x5a);
     81     assert(rearmed == 0 && MEM32(0x50002000) == 1);
     82 
     83     atomic_store(&status, 4);
     84     uint32_t closed = call("WaitForSingleObject", 2, (uint32_t[]){event, 1000});
     85     assert(!call("WSAEnumNetworkEvents", 3, (uint32_t[]){socket, event, 0x50002000}));
     86 
     87     assert(closed == 0 && MEM32(0x50002000) == 32 && MEM32(0x50002018) == 0);
     88 
     89     atomic_store(&status, 0);
     90     assert(!call("WSAEventSelect", 3, (uint32_t[]){socket, event, 33}));
     91 
     92     atomic_store(&status, 8);
     93     uint32_t failed = call("WaitForSingleObject", 2, (uint32_t[]){event, 1000});
     94     assert(!call("WSAEnumNetworkEvents", 3, (uint32_t[]){socket, event, 0x50002000}));
     95 
     96     assert(failed == 0 && MEM32(0x50002000) == 32 && MEM32(0x50002018) == 10054);
     97     assert(!call("closesocket", 1, &socket));
     98     assert(call("CloseHandle", 1, &event));
     99 }
    100 
    101 int main(void)
    102 {
    103     assert(!emscripten_is_main_runtime_thread());
    104     assert(guest_map("socket event test", 0x50000000, 0x10000));
    105     g_fs_base = 0x50000000;
    106     assert(!call("WSAStartup", 2, (uint32_t[]){0x0202, 0x50001000}));
    107     readiness_survives_contention(1);
    108     readiness_survives_contention(2);
    109     assert(!call("WSACleanup", 0, NULL));
    110     puts("socket events: nonblocking UI callbacks, contention, rearming, close and error delivery passed");
    111     return 0;
    112 }