aorecomp

Anarchy Online classic in the browser

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


client/tests/tls.c (4126B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #ifdef _WIN32
      5 #include <windows.h>
      6 #else
      7 #include "linux_tls.h"
      8 #include "guest_memory.h"
      9 #include <pthread.h>
     10 
     11 static uint32_t call(const char *name, unsigned count, const uint32_t *arguments)
     12 {
     13     g_esp = 0x5000ff00u;
     14     for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = arguments[i];
     15     linux_tls_lookup(name)();
     16     assert(g_esp == 0x5000ff00u + (count + 1) * 4);
     17     return g_eax;
     18 }
     19 #endif
     20 
     21 static void clear_error(void)
     22 {
     23 #ifdef _WIN32
     24     SetLastError(777);
     25 #else
     26     MEM32(g_fs_base + 0x34) = 777;
     27 #endif
     28 }
     29 
     30 static uint32_t error(void)
     31 {
     32 #ifdef _WIN32
     33     return GetLastError();
     34 #else
     35     return MEM32(g_fs_base + 0x34);
     36 #endif
     37 }
     38 
     39 static uint32_t allocate(void)
     40 {
     41 #ifdef _WIN32
     42     return TlsAlloc();
     43 #else
     44     return call("TlsAlloc", 0, NULL);
     45 #endif
     46 }
     47 
     48 static uint32_t release(uint32_t index)
     49 {
     50 #ifdef _WIN32
     51     return TlsFree(index);
     52 #else
     53     return call("TlsFree", 1, &index);
     54 #endif
     55 }
     56 
     57 static uint32_t get(uint32_t index)
     58 {
     59 #ifdef _WIN32
     60     return (uintptr_t)TlsGetValue(index);
     61 #else
     62     return call("TlsGetValue", 1, &index);
     63 #endif
     64 }
     65 
     66 static uint32_t set(uint32_t index, uint32_t value)
     67 {
     68 #ifdef _WIN32
     69     return TlsSetValue(index, (void *)(uintptr_t)value);
     70 #else
     71     uint32_t args[] = {index, value};
     72     return call("TlsSetValue", 2, args);
     73 #endif
     74 }
     75 
     76 static void values_and_reuse(void)
     77 {
     78     clear_error();
     79     uint32_t index = allocate();
     80     assert(index != UINT32_MAX && error() == 777);
     81     assert(get(index) == 0 && error() == 0);
     82 
     83     clear_error();
     84     assert(set(index, 0xfedcba98) && error() == 777);
     85     assert(get(index) == 0xfedcba98 && error() == 0);
     86     clear_error();
     87     assert(release(index) && error() == 777);
     88 
     89     assert(get(index) == 0 && error() == 0);
     90     assert(set(index, 123) && get(index) == 123);
     91     assert(!release(index) && error() == 87);
     92     uint32_t reused = allocate();
     93     assert(reused != UINT32_MAX && get(reused) == 0);
     94     assert(release(reused));
     95 }
     96 
     97 #ifdef _WIN32
     98 static DWORD WINAPI thread_values(void *argument)
     99 #else
    100 static void *thread_values(void *argument)
    101 #endif
    102 {
    103 #ifndef _WIN32
    104     g_fs_base = 0x50002000u;
    105 #endif
    106     uint32_t index = (uintptr_t)argument;
    107     assert(get(index) == 0);
    108     assert(set(index, 0x87654321) && get(index) == 0x87654321);
    109     return 0;
    110 }
    111 
    112 static void values_are_local_to_each_thread(void)
    113 {
    114     uint32_t index = allocate();
    115     assert(index != UINT32_MAX && set(index, 0x12345678));
    116 
    117 #ifdef _WIN32
    118     HANDLE thread = CreateThread(NULL, 0, thread_values, (void *)(uintptr_t)index, 0, NULL);
    119     assert(thread && WaitForSingleObject(thread, 1000) == WAIT_OBJECT_0);
    120     assert(CloseHandle(thread));
    121 #else
    122     pthread_t thread;
    123     assert(!pthread_create(&thread, NULL, thread_values, (void *)(uintptr_t)index));
    124     assert(!pthread_join(thread, NULL));
    125 #endif
    126 
    127     assert(get(index) == 0x12345678);
    128     assert(release(index));
    129 }
    130 
    131 static void capacity_and_invalid_indices(void)
    132 {
    133     uint32_t indices[1088];
    134     unsigned count = 0;
    135     for (;;) {
    136         uint32_t index = allocate();
    137         if (index == UINT32_MAX) break;
    138         assert(count < 1088);
    139         indices[count++] = index;
    140     }
    141     assert(count >= 1024 && error() == 8);
    142 
    143     for (unsigned i = 0; i < count; ++i) {
    144         assert(get(indices[i]) == 0);
    145         assert(set(indices[i], 0x80000000u + i));
    146     }
    147 
    148     for (unsigned i = 0; i < count; ++i) {
    149         assert(get(indices[i]) == 0x80000000u + i);
    150         assert(release(indices[i]));
    151     }
    152     assert(get(1088) == 0 && error() == 87);
    153     assert(!set(1088, 42) && error() == 87);
    154     assert(!release(1088) && error() == 87);
    155     assert(get(UINT32_MAX) == 0 && error() == 87);
    156     assert(!set(UINT32_MAX, 42) && error() == 87);
    157     assert(!release(UINT32_MAX) && error() == 87);
    158 }
    159 
    160 int main(void)
    161 {
    162 #ifndef _WIN32
    163     assert(guest_map("TLS test memory", 0x50000000u, 0x10000u));
    164     g_fs_base = 0x50001000u;
    165 #endif
    166     values_and_reuse();
    167     values_are_local_to_each_thread();
    168     capacity_and_invalid_indices();
    169     puts("TLS: guest values, thread isolation, reuse, exhaustion and last-error behavior passed");
    170     return 0;
    171 }