aorecomp

Anarchy Online classic in the browser

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


client/tests/stdio_concurrency.c (4003B)


      1 #include "guest_stdio.h"
      2 #include "guest_crt.h"
      3 #include "guest_memory.h"
      4 #include <assert.h>
      5 #include <pthread.h>
      6 #include <stdatomic.h>
      7 #include <time.h>
      8 #include <unistd.h>
      9 
     10 static _Thread_local int blocked_reader;
     11 static _Atomic int entered, release_read;
     12 ssize_t __real_read(int descriptor, void *buffer, size_t size);
     13 ssize_t __wrap_read(int descriptor, void *buffer, size_t size)
     14 {
     15     if (blocked_reader) {
     16         entered = 1;
     17         while (!release_read) {
     18             struct timespec delay = {0, 1000000};
     19             nanosleep(&delay, NULL);
     20         }
     21     }
     22     return __real_read(descriptor, buffer, size);
     23 }
     24 recomp_func_t recomp_lookup(uint32_t address) { (void)address; abort(); }
     25 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
     26 
     27 static uint32_t call(const char *name, unsigned count, const uint32_t *arguments)
     28 {
     29     uint32_t stack = g_esp;
     30     if (count) memcpy((void *)ADDR(stack + 4), arguments, count * 4);
     31     recomp_func_t function = guest_stdio_lookup(name);
     32     assert(function);
     33     function();
     34     assert(g_esp == stack + 4);
     35     g_esp = stack;
     36     return g_eax;
     37 }
     38 
     39 static void *read_blocked_file(void *argument)
     40 {
     41     g_fs_base = 0x50020000;
     42     g_esp = 0x5003ff00;
     43     blocked_reader = 1;
     44     uint32_t arguments[] = {0x50028000, 1, 1, (uintptr_t)argument};
     45     assert(call("fread", 4, arguments) == 1);
     46     assert(MEM8(0x50028000) == 'A');
     47     return NULL;
     48 }
     49 
     50 int main(void)
     51 {
     52     assert(guest_map("concurrent stdio threads", 0x50000000, 0x40000));
     53     g_fs_base = 0x50001000;
     54     g_esp = 0x5000ff00;
     55     char directory[] = "/tmp/ao-stdio-concurrency-XXXXXX";
     56     assert(mkdtemp(directory) && !chdir(directory));
     57     FILE *input = fopen("first.bin", "wb");
     58     assert(input && fwrite("AB", 1, 2, input) == 2 && !fclose(input));
     59     input = fopen("second.bin", "wb");
     60     assert(input && fwrite("CD", 1, 2, input) == 2 && !fclose(input));
     61     strcpy((void *)ADDR(0x50004000), "first.bin");
     62     strcpy((void *)ADDR(0x50004100), "rb");
     63     strcpy((void *)ADDR(0x50004200), "output.txt");
     64     strcpy((void *)ADDR(0x50004300), "w");
     65     strcpy((void *)ADDR(0x50004400), "second.bin");
     66     uint32_t first = call("fopen", 2, (uint32_t[]){0x50004000, 0x50004100});
     67     assert(first);
     68     pthread_t reader;
     69     assert(!pthread_create(&reader, NULL, read_blocked_file, (void *)(uintptr_t)first));
     70     for (unsigned i = 0; !entered && i < 5000; ++i) {
     71         struct timespec delay = {0, 1000000};
     72         nanosleep(&delay, NULL);
     73     }
     74     assert(entered);
     75 
     76     uint32_t second = call("fopen", 2, (uint32_t[]){0x50004400, 0x50004100});
     77     assert(second && second != first);
     78     uint32_t read_count = call("fread", 4, (uint32_t[]){0x50005000, 1, 2, second});
     79     uint32_t position = call("ftell", 1, &second);
     80     uint32_t stat_result = call("_stat", 2, (uint32_t[]){0x50004400, 0x50005200});
     81     uint32_t reopened = call("freopen", 3, (uint32_t[]){0x50004400, 0x50004100, second});
     82     uint32_t output = call("fopen", 2, (uint32_t[]){0x50004200, 0x50004300});
     83     assert(output);
     84     assert(call("fputc", 2, (uint32_t[]){'X', output}) == 'X');
     85     uint32_t flush_result = call("fflush", 1, (uint32_t[]){0});
     86     assert(!call("fclose", 1, &output));
     87 
     88     assert(entered && !release_read);
     89     assert(read_count == 2 && position == 2 && !stat_result && MEM32(0x50005214) == 2);
     90     assert(MEM8(0x50005000) == 'C' && MEM8(0x50005001) == 'D');
     91     assert(reopened == second && !call("ftell", 1, &second) && !flush_result);
     92     input = fopen("output.txt", "rb");
     93     assert(input && fgetc(input) == 'X' && fgetc(input) == EOF && !fclose(input));
     94     release_read = 1;
     95     assert(!pthread_join(reader, NULL));
     96     assert(!call("fclose", 1, &first) && !call("fclose", 1, &second));
     97     assert(!remove("first.bin") && !remove("second.bin") && !remove("output.txt"));
     98     assert(!chdir("/tmp") && !rmdir(directory));
     99     puts("stdio concurrency: blocked input leaves other opens, reads, stat, reopen, output and flush-all responsive");
    100     return 0;
    101 }