aorecomp

Anarchy Online classic in the browser

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


client/tests/guest_stdio.c (11618B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #ifdef _WIN32
      7 #include <windows.h>
      8 static HMODULE crt;
      9 #else
     10 #include "guest_stdio.h"
     11 #include "guest_crt.h"
     12 #include "guest_memory.h"
     13 #include <unistd.h>
     14 recomp_func_t recomp_lookup(uint32_t address) { fprintf(stderr, "Unexpected stdio guest callback %08x\n", address); abort(); }
     15 recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
     16 #endif
     17 
     18 static void *memory(uint32_t address)
     19 {
     20 #ifdef _WIN32
     21     return (void *)(uintptr_t)address;
     22 #else
     23     return (void *)ADDR(address);
     24 #endif
     25 }
     26 
     27 static uint32_t address(const void *bytes, unsigned size)
     28 {
     29 #ifdef _WIN32
     30     return (uintptr_t)bytes;
     31 #else
     32     static uint32_t next = 0x50004000u;
     33     uint32_t result = next;
     34     memcpy(memory(result), bytes, size);
     35     next += (size + 3) & ~3u;
     36     assert(next < 0x5000e000u);
     37     return result;
     38 #endif
     39 }
     40 
     41 static uint32_t string(const char *text) { return address(text, strlen(text) + 1); }
     42 
     43 static uint32_t call(const char *name, unsigned count, const uint32_t *arguments)
     44 {
     45 #ifdef _WIN32
     46     uint32_t args[4] = {0};
     47     if (count) memcpy(args, arguments, count * 4);
     48     uint32_t (__cdecl *function)(uint32_t, uint32_t, uint32_t, uint32_t) = (void *)GetProcAddress(crt, name);
     49     assert(function);
     50     return function(args[0], args[1], args[2], args[3]);
     51 #else
     52     g_esp = 0x5000ff00u;
     53     if (count) memcpy(memory(g_esp + 4), arguments, count * 4);
     54     g_ebx = 0x12345678;
     55     g_esi = 0x23456789;
     56     g_edi = 0x3456789a;
     57     g_ebp = 0x456789ab;
     58     recomp_func_t function = guest_stdio_lookup(name);
     59     if (!function) function = guest_crt_lookup(name);
     60     assert(function);
     61     function();
     62     assert(g_esp == 0x5000ff04u);
     63     assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
     64     assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
     65     return g_eax;
     66 #endif
     67 }
     68 
     69 static uint32_t open_file(const char *name, const char *mode)
     70 {
     71     uint32_t args[] = {string(name), string(mode)};
     72     return call("fopen", 2, args);
     73 }
     74 
     75 static void check_contents(const char *path, const void *expected, unsigned size)
     76 {
     77     unsigned char bytes[256];
     78     FILE *file = fopen(path, "rb");
     79     assert(file);
     80     size_t count = fread(bytes, 1, sizeof(bytes), file);
     81     assert(!fclose(file));
     82     if (count != size || memcmp(bytes, expected, size)) {
     83         printf("File %s: %u bytes, expected %u\n", path, (unsigned)count, size);
     84         fflush(stdout);
     85     }
     86     assert(count == size && !memcmp(bytes, expected, size));
     87 }
     88 
     89 static void text_output_honors_direct_buffer_writes(uint32_t stderr_file)
     90 {
     91     uint32_t reopen[] = {string("text.txt"), string("w"), stderr_file};
     92     assert(call("freopen", 3, reopen) == stderr_file);
     93     uint32_t *fields = memory(stderr_file);
     94     assert(fields[0] == 0 && fields[1] == 0 && fields[2] == 0 && fields[3] == 2 && fields[4] == 2);
     95     uint32_t put[] = {'a', stderr_file};
     96 
     97     assert(call("fputc", 2, put) == 'a');
     98     assert(fields[0] - fields[2] == 1 && fields[1] == 4095 && fields[3] == 0xa && fields[6] == 4096);
     99     *(unsigned char *)memory(fields[0]++) = '\n';
    100     --fields[1]; /* Same field aliases used by translated MSVCP60 streambuf. */
    101     assert(call("fflush", 1, &stderr_file) == 0);
    102 
    103     check_contents("text.txt", "a\r\n", 3);
    104     assert(fields[0] == fields[2] && fields[1] == 0 && fields[3] == 0xa);
    105     uint32_t write[] = {string("b\r\n"), 1, 3, stderr_file};
    106     assert(call("fwrite", 4, write) == 3);
    107     uint32_t puts[] = {string("tail\n"), stderr_file};
    108     assert((int32_t)call("fputs", 2, puts) >= 0);
    109     assert(call("fclose", 1, &stderr_file) == 0);
    110     check_contents("text.txt", "a\r\nb\r\r\ntail\r\n", 13);
    111     assert(fields[0] == 0 && fields[1] == 0 && fields[2] == 0 && fields[3] == 0);
    112 }
    113 
    114 static void binary_output_preserves_bytes_and_user_buffer(void)
    115 {
    116     uint32_t file = open_file("binary.bin", "wb");
    117     assert(file);
    118     unsigned char canaries[66];
    119     memset(canaries, 0xa5, sizeof(canaries));
    120     uint32_t buffer = address(canaries, sizeof(canaries));
    121     uint32_t setbuf[] = {file, buffer + 1, 0, 63};
    122     assert(call("setvbuf", 4, setbuf) == 0);
    123     uint32_t *fields = memory(file);
    124     assert(fields[0] == buffer + 1 && fields[2] == buffer + 1 && fields[3] == 0x502 && fields[6] == 62);
    125     const unsigned char bytes[] = {'x', 0, '\r', '\n', 0xff};
    126     uint32_t write[] = {address(bytes, sizeof(bytes)), 1, sizeof(bytes), file};
    127 
    128     for (unsigned i = 0; i < 20; ++i) assert(call("fwrite", 4, write) == sizeof(bytes));
    129     uint32_t all = 0;
    130     assert(call("fflush", 1, &all) == 0);
    131 
    132     unsigned char expected[101];
    133     for (unsigned i = 0; i < 20; ++i) memcpy(expected + i * 5, bytes, 5);
    134     check_contents("binary.bin", expected, 100);
    135     assert(*(unsigned char *)memory(buffer) == 0xa5 && *(unsigned char *)memory(buffer + 65) == 0xa5);
    136     assert(*(unsigned char *)memory(buffer + 63) == 0xa5 && *(unsigned char *)memory(buffer + 64) == 0xa5);
    137     assert(fields[0] == buffer + 1 && fields[1] == 0);
    138     assert(call("fclose", 1, &file) == 0);
    139     file = open_file("BINARY.BIN", "ab");
    140     assert(file);
    141     uint32_t put[] = {'z', file};
    142     assert(call("fputc", 2, put) == 'z' && call("fclose", 1, &file) == 0);
    143     expected[100] = 'z';
    144     check_contents("binary.bin", expected, 101);
    145 }
    146 
    147 static void unbuffered_output_is_visible_immediately(void)
    148 {
    149     uint32_t file = open_file("unbuffered.txt", "w");
    150     assert(file);
    151     uint32_t setbuf[] = {file, 0, 4, 0};
    152     assert(call("setvbuf", 4, setbuf) == 0);
    153     uint32_t put[] = {'\n', file};
    154 
    155     assert(call("fputc", 2, put) == '\n');
    156 
    157     check_contents("unbuffered.txt", "\r\n", 2);
    158     uint32_t *fields = memory(file);
    159     assert(fields[0] == file + 20 && fields[2] == file + 20 && fields[1] == 0 && fields[3] == 6 && fields[6] == 2);
    160     assert(call("fclose", 1, &file) == 0);
    161 }
    162 
    163 static void reopen_failure_closes_the_old_stream(void)
    164 {
    165     uint32_t file = open_file("closed.txt", "wb");
    166     assert(file);
    167     uint32_t put[] = {'c', file};
    168     assert(call("fputc", 2, put) == 'c');
    169     uint32_t *error = memory(call("_errno", 0, NULL));
    170     *error = 777;
    171     uint32_t reopen[] = {string("missing/file.txt"), string("r"), file};
    172 
    173     assert(call("freopen", 3, reopen) == 0);
    174 
    175     assert(*error == 2);
    176     assert(((uint32_t *)memory(file))[3] == 0);
    177     check_contents("closed.txt", "c", 1);
    178 }
    179 
    180 static void readonly_stream_reports_write_error(void)
    181 {
    182     uint32_t file = open_file("closed.txt", "rb");
    183     assert(file);
    184     uint32_t *error = memory(call("_errno", 0, NULL));
    185     *error = 777;
    186     uint32_t put[] = {'x', file};
    187 
    188     uint32_t result = call("fputc", 2, put);
    189     if (result != UINT32_MAX || *error != 9 || !(((uint32_t *)memory(file))[3] & 0x20)) {
    190         printf("readonly put=%08x errno=%u flags=%x\n", result, *error, ((uint32_t *)memory(file))[3]);
    191         fflush(stdout);
    192     }
    193     assert(result == UINT32_MAX);
    194 
    195     assert(*error == 9 && (((uint32_t *)memory(file))[3] & 0x20));
    196     assert(call("fclose", 1, &file) == 0);
    197     check_contents("closed.txt", "c", 1);
    198     uint32_t directory = open_file(".", "rb");
    199     if (directory || *error != 13) { printf("directory=%08x errno=%u\n", directory, *error); fflush(stdout); }
    200     assert(!directory && *error == 13);
    201 }
    202 
    203 static void cexit_preserves_pending_stream_state(void)
    204 {
    205     uint32_t file = open_file("exit.txt", "wb");
    206     assert(file);
    207     uint32_t put[] = {'e', file};
    208     assert(call("fputc", 2, put) == 'e');
    209 
    210     call("_cexit", 0, NULL);
    211 
    212     check_contents("exit.txt", "", 0);
    213     uint32_t *fields = memory(file);
    214     assert(fields[0] - fields[2] == 1);
    215     assert(call("fclose", 1, &file) == 0);
    216     check_contents("exit.txt", "e", 1);
    217 }
    218 
    219 static void formatted_output_uses_guest_arguments_and_text_stream(void)
    220 {
    221     uint32_t file = open_file("formatted.txt", "w");
    222     assert(file);
    223     assert(call("_fileno", 1, &file) == ((uint32_t *)memory(file))[4]);
    224     uint32_t direct[] = {file, string("id=%08x\n"), 0xabcd};
    225     uint32_t values[] = {string("value"), (uint32_t)-42, 0};
    226     uint32_t list[] = {file, string("[%s] %ld%c\n"), address(values, sizeof(values))};
    227 
    228     assert(call("fprintf", 3, direct) == 12);
    229     assert(call("vfprintf", 3, list) == 13);
    230     assert(call("fclose", 1, &file) == 0);
    231 
    232     const char expected[] = "id=0000abcd\r\n[value] -42\0\r\n";
    233     check_contents("formatted.txt", expected, sizeof(expected) - 1);
    234 }
    235 
    236 static void additional_streams_have_independent_buffers_and_descriptors(void)
    237 {
    238     uint32_t files[24];
    239     for (unsigned i = 0; i < 24; ++i) {
    240         files[i] = open_file("many.bin", "ab");
    241         assert(files[i] && call("_fileno", 1, &files[i]) == i + 2);
    242         for (unsigned j = 0; j < i; ++j) assert(files[i] != files[j]);
    243     }
    244 
    245     for (unsigned i = 0; i < 24; ++i) {
    246         uint32_t put[] = {i, files[i]};
    247         assert(call("fputc", 2, put) == i);
    248     }
    249     for (unsigned i = 24; i; --i) assert(call("fclose", 1, &files[i - 1]) == 0);
    250 
    251     const unsigned char expected[] = {23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12,
    252                                       11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
    253     check_contents("many.bin", expected, sizeof(expected));
    254 }
    255 
    256 static void fwrite_returns_element_counts(void)
    257 {
    258     uint32_t file = open_file("elements.bin", "wb");
    259     assert(file);
    260     uint32_t write[] = {string("abcdef"), 2, 3, file};
    261 
    262     assert(call("fwrite", 4, write) == 3);
    263     write[1] = 0;
    264     assert(call("fwrite", 4, write) == 0);
    265     write[1] = 2;
    266     write[2] = 0;
    267     assert(call("fwrite", 4, write) == 0);
    268     assert(call("fclose", 1, &file) == 0);
    269 
    270     check_contents("elements.bin", "abcdef", 6);
    271 }
    272 
    273 static void formatted_stdout_uses_guest_arguments(uint32_t stdout_file)
    274 {
    275     uint32_t args[] = {string("Using RdbDSN %s\n"), string("fixture")};
    276 
    277     uint32_t length = call("printf", 2, args);
    278     uint32_t flushed = call("fflush", 1, &stdout_file);
    279 
    280     assert(length == 21 && flushed == 0);
    281 }
    282 
    283 int main(void)
    284 {
    285 #ifdef _WIN32
    286     crt = LoadLibraryA("msvcrt.dll");
    287     assert(crt);
    288     uint32_t stderr_file = (uintptr_t)GetProcAddress(crt, "_iob") + 64;
    289     char temporary[MAX_PATH], directory[MAX_PATH];
    290     assert(GetTempPathA(sizeof(temporary), temporary));
    291     assert(GetTempFileNameA(temporary, "aos", 0, directory));
    292     assert(DeleteFileA(directory) && CreateDirectoryA(directory, NULL) && SetCurrentDirectoryA(directory));
    293 #else
    294     assert(guest_map("stdio test memory", 0x50000000u, 0x10000u));
    295     g_fs_base = 0x50001000u;
    296     uint32_t stderr_file = guest_crt_data("_iob") + 64;
    297     char directory[] = "/tmp/ao-stdio-test-XXXXXX";
    298     assert(mkdtemp(directory) && !chdir(directory));
    299 #endif
    300     text_output_honors_direct_buffer_writes(stderr_file);
    301     binary_output_preserves_bytes_and_user_buffer();
    302     unbuffered_output_is_visible_immediately();
    303     reopen_failure_closes_the_old_stream();
    304     readonly_stream_reports_write_error();
    305     formatted_output_uses_guest_arguments_and_text_stream();
    306     additional_streams_have_independent_buffers_and_descriptors();
    307     fwrite_returns_element_counts();
    308     cexit_preserves_pending_stream_state();
    309     formatted_stdout_uses_guest_arguments(stderr_file - 32);
    310     assert(!remove("text.txt") && !remove("binary.bin") && !remove("unbuffered.txt") && !remove("closed.txt") && !remove("exit.txt") && !remove("formatted.txt") && !remove("many.bin") && !remove("elements.bin"));
    311 #ifdef _WIN32
    312     assert(SetCurrentDirectoryA(temporary) && RemoveDirectoryA(directory));
    313 #else
    314     assert(!chdir("/tmp") && !rmdir(directory));
    315 #endif
    316     puts("stdio output: text/binary bytes, direct FILE writes, user buffers, flush, append and reopen failures passed");
    317     return 0;
    318 }