aorecomp

Anarchy Online classic in the browser

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


client/tests/guest_format.c (9230B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #include <stdarg.h>
      7 #ifdef _WIN32
      8 #include <windows.h>
      9 static HMODULE crt;
     10 #else
     11 #include "guest_format.h"
     12 #include "guest_memory.h"
     13 #endif
     14 
     15 static uint32_t digest = 2166136261u;
     16 static unsigned cases;
     17 
     18 static void compare_case(const char *format, const uint32_t words[16], const char *expected)
     19 {
     20     const char *names[] = {"sprintf", "vsprintf", "_snprintf", "_vsnprintf"};
     21     unsigned length = strlen(expected);
     22     unsigned capacities[] = {0, 1, length ? length - 1 : 0, length, length + 1, 128, 0};
     23     for (unsigned api = 0; api < 4; ++api) {
     24         for (unsigned size = 0; size < (api < 2 ? 1 : 7); ++size) {
     25             unsigned capacity = api < 2 ? 128 : capacities[size];
     26             int sizing = api >= 2 && size == 6;
     27             unsigned char output[144], wanted[144];
     28             memset(output, 0xa5, sizeof(output));
     29             memset(wanted, 0xa5, sizeof(wanted));
     30             if (!sizing) {
     31                 memcpy(wanted + 4, expected, length < capacity ? length : capacity);
     32                 if (length < capacity) wanted[4 + length] = 0;
     33             }
     34             uint32_t arguments[16];
     35             memcpy(arguments, words, sizeof(arguments));
     36 #ifdef _WIN32
     37             for (unsigned i = 0; i < 16; ++i)
     38                 if (arguments[i] == 0x50004000u) arguments[i] = (uint32_t)(uintptr_t)"Anarchy Online";
     39             char *destination = sizing ? NULL : (char *)output + 4;
     40             void *function = (void *)GetProcAddress(crt, names[api]);
     41             assert(function);
     42 #else
     43             strcpy((char *)ADDR(0x50001000u), format);
     44             memcpy((void *)ADDR(0x50002000u), arguments, sizeof(arguments));
     45             memcpy((void *)ADDR(0x50003000u), output, sizeof(output));
     46             strcpy((char *)ADDR(0x50004000u), "Anarchy Online");
     47             g_esp = 0x5000ff00u;
     48             MEM32(g_esp + 4) = sizing ? 0 : 0x50003004u;
     49             unsigned slot = g_esp + 8;
     50             if (api >= 2) { MEM32(slot) = capacity; slot += 4; }
     51             MEM32(slot) = 0x50001000u;
     52             if (api & 1) MEM32(slot + 4) = 0x50002000u;
     53             else memcpy((void *)ADDR(slot + 4), arguments, sizeof(arguments));
     54             g_ebx = 0x12345678;
     55             g_esi = 0x23456789;
     56             g_edi = 0x3456789a;
     57             g_ebp = 0x456789ab;
     58 #endif
     59 
     60 #ifdef _WIN32
     61             int result;
     62             if (api == 1) result = ((int (__cdecl *)(char *, const char *, va_list))function)(destination, format, (va_list)arguments);
     63             else if (api == 3) result = ((int (__cdecl *)(char *, size_t, const char *, va_list))function)(destination, capacity, format, (va_list)arguments);
     64             else if (api == 0) result = ((int (__cdecl *)(char *, const char *, ...))function)(destination, format,
     65                 arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7],
     66                 arguments[8], arguments[9], arguments[10], arguments[11], arguments[12], arguments[13], arguments[14], arguments[15]);
     67             else result = ((int (__cdecl *)(char *, size_t, const char *, ...))function)(destination, capacity, format,
     68                 arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7],
     69                 arguments[8], arguments[9], arguments[10], arguments[11], arguments[12], arguments[13], arguments[14], arguments[15]);
     70 #else
     71             guest_format_lookup(names[api])();
     72             int result = (int32_t)g_eax;
     73             memcpy(output, (void *)ADDR(0x50003000u), sizeof(output));
     74             assert(g_esp == 0x5000ff04u);
     75             assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
     76             assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
     77 #endif
     78 
     79             int expected_result = !sizing && length > capacity ? -1 : (int)length;
     80             if (result != expected_result || memcmp(output, wanted, sizeof(output))) {
     81                 fprintf(stderr, "%s format '%s' capacity %u: result %d expected %d\n",
     82                         names[api], format, capacity, result, expected_result);
     83                 for (unsigned i = 4; i < 4 + length; ++i) fprintf(stderr, "%02x ", output[i]);
     84                 fputc('\n', stderr);
     85             }
     86             assert(result == expected_result);
     87             assert(!memcmp(output, wanted, sizeof(output)));
     88             digest = (digest ^ (uint32_t)result) * 16777619u;
     89             for (unsigned i = 0; i < sizeof(output); ++i) digest = (digest ^ output[i]) * 16777619u;
     90             ++cases;
     91         }
     92     }
     93 }
     94 
     95 int main(void)
     96 {
     97 #ifdef _WIN32
     98     crt = LoadLibraryA("msvcrt.dll");
     99     assert(crt);
    100 #else
    101     assert(guest_map("printf test memory", 0x50000000u, 0x10000u));
    102 #endif
    103     compare_case("%lx%d", (uint32_t[16]){0x4710e910, 1234}, "4710e9101234");
    104     compare_case("", (uint32_t[16]){0}, "");
    105     compare_case("literal %% text", (uint32_t[16]){0}, "literal % text");
    106     compare_case("%ld %lu %lx", (uint32_t[16]){0x80000000, 0xffffffff, 0xabcdef12}, "-2147483648 4294967295 abcdef12");
    107     compare_case("%hd %hu", (uint32_t[16]){0xffff8000, 0x1234ffff}, "-32768 65535");
    108     compare_case("%hhd %hhu %I32d %Id", (uint32_t[16]){0x80, 0x1234ff, 0xffffffff, 42}, "128 13567 -1 42");
    109     compare_case("%lld %llu", (uint32_t[16]){0, 0x80000000, 0xffffffff, 0xffffffff}, "-9223372036854775808 18446744073709551615");
    110     compare_case("%I64d %I64u %d", (uint32_t[16]){0, 0x80000000, 0xffffffff, 0xffffffff, 42}, "-9223372036854775808 18446744073709551615 42");
    111     compare_case("[%s][%.7s][%c]", (uint32_t[16]){0x50004000, 0x50004000, 'X'}, "[Anarchy Online][Anarchy][X]");
    112     compare_case("[%s]", (uint32_t[16]){0}, "[(null)]");
    113     compare_case("[%*.*s]", (uint32_t[16]){(uint32_t)-10, (uint32_t)-1, 0x50004000}, "[Anarchy Online]");
    114     compare_case("[%*.*s]", (uint32_t[16]){(uint32_t)-10, 3, 0x50004000}, "[Ana       ]");
    115     compare_case("[%#x][%#o][%.0d][%08.4d]", (uint32_t[16]){0, 0, 0, 12}, "[0][0][][    0012]");
    116     compare_case("[%+08d][% d][%-6u]", (uint32_t[16]){(uint32_t)-42, 42, 9}, "[-0000042][ 42][9     ]");
    117     compare_case("[%#x][%#X][%#o]", (uint32_t[16]){0xabcd, 0xabcd, 8}, "[0xabcd][0XABCD][010]");
    118     compare_case("[%p][%#p][%.12p][%012p]", (uint32_t[16]){0xabcd, 0xabcd, 0xabcd, 0xabcd}, "[0000ABCD][0X0000ABCD][0000ABCD][    0000ABCD]");
    119     compare_case("[%p][%-12p]", (uint32_t[16]){0, 0xabcd}, "[00000000][0000ABCD    ]");
    120     compare_case("[%#p][%+#012p]", (uint32_t[16]){0, 0}, "[00000000][    00000000]");
    121     assert(cases == 288 && digest == 0x3896b505);
    122     compare_case("%.f%%", (uint32_t[16]){0, 0x40590000}, "100%");
    123     compare_case("%f %d", (uint32_t[16]){0, 0x3ff40000, 7}, "1.250000 7");
    124     compare_case("[%08.2f][%+.1f]", (uint32_t[16]){0, 0x3ff40000, 0, 0x40040000}, "[00001.25][+2.5]");
    125     compare_case("%.0f %.0f", (uint32_t[16]){0, 0x40040000, 0, 0xc0040000}, "3 -3");
    126     compare_case("%.1f", (uint32_t[16]){0, 0x3ff40000}, "1.3");
    127     compare_case("%f", (uint32_t[16]){0, 0x80000000}, "-0.000000");
    128     compare_case("%*.*f", (uint32_t[16]){(uint32_t)-8, 2, 0, 0x3ff40000}, "1.25    ");
    129     compare_case("[%#6.0f]", (uint32_t[16]){0, 0x40040000}, "[    3.]");
    130     compare_case("%.17f", (uint32_t[16]){0, 0x3ed00000}, "0.00000381469726563");
    131     double fraction = 2.675;
    132     uint32_t floating[16] = {0};
    133     memcpy(floating, &fraction, sizeof(fraction));
    134     compare_case("%.2f", floating, "2.67");
    135     compare_case("%.0f %.0f %.0f %.0f", (uint32_t[16]){0, 0x400c0000, 0, 0x40120000,
    136         0, 0xc00c0000, 0, 0xc0120000}, "4 5 -4 -5");
    137     compare_case("%f", (uint32_t[16]){0, 0x3f800000}, "0.007813");
    138     assert(cases == 480 && digest == 0x401f5575);
    139     compare_case("%.*g %d", (uint32_t[16]){6, 0, 0x3ff40000, 7}, "1.25 7");
    140     compare_case("%.2g %.2g", (uint32_t[16]){0, 0x3ff40000, 0, 0xbff40000}, "1.3 -1.3");
    141     compare_case("%g %g", (uint32_t[16]){0, 0, 0, 0x80000000}, "0 -0");
    142     compare_case("%.0g %.1g", (uint32_t[16]){0, 0x40040000, 0, 0x40230000}, "3 1e+001");
    143     compare_case("%g %g", (uint32_t[16]){0, 0x412e8480, 0, 0x3eb00000}, "1e+006 9.53674e-007");
    144     compare_case("[%+012.1g][%-12.1g]", (uint32_t[16]){0, 0x412e8480, 0, 0x412e8480}, "[+000001e+006][1e+006      ]");
    145     compare_case("%#.6g", (uint32_t[16]){0, 0x3ff40000}, "1.25000");
    146     compare_case("%.*g", (uint32_t[16]){(uint32_t)-1, 0, 0x3ff40000}, "1.25");
    147     double general = 1250;
    148     memcpy(floating, &general, sizeof(general));
    149     compare_case("%.2g", floating, "1.3e+003");
    150     general = 2.675;
    151     memcpy(floating, &general, sizeof(general));
    152     compare_case("%.3g", floating, "2.67");
    153     assert(cases == 640 && digest == 0x686ca2b7);
    154     general = 0.500003814697265625;
    155     memcpy(floating, &general, sizeof(general));
    156     compare_case("%.17g", floating, "0.50000381469726563");
    157     general = 1050;
    158     memcpy(floating, &general, sizeof(general));
    159     compare_case("%.2g", floating, "1.1e+003");
    160     general = 10.5;
    161     memcpy(floating, &general, sizeof(general));
    162     compare_case("%.2g", floating, "11");
    163     assert(cases == 688 && digest == 0x2c8e2b85);
    164     printf("printf: %u cases, digest %08x, guest arguments, truncation and canaries passed\n", cases, digest);
    165     return 0;
    166 }