aorecomp

Anarchy Online classic in the browser

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


client/tests/guest_crt.c (16713B)


      1 #include "guest_crt.h"
      2 #include "guest_memory.h"
      3 #include "guest_heap.h"
      4 #include <assert.h>
      5 #include <time.h>
      6 
      7 static void first_constructor(void)
      8 {
      9     MEM32(0x50002000u) = MEM32(0x50002000u) * 10 + 1;
     10     g_cur_func = 0x11110000u;
     11     g_esp += 4;
     12 }
     13 
     14 static void second_constructor(void)
     15 {
     16     MEM32(0x50002000u) = MEM32(0x50002000u) * 10 + 2;
     17     g_cur_func = 0x22220000u;
     18     g_esp += 4;
     19 }
     20 
     21 static void abort_callback(void)
     22 {
     23     assert(MEM32(g_esp + 4) == 22);
     24     ++MEM32(0x50003000u);
     25     g_cur_func = 0x44440000u;
     26     g_esp += 4;
     27 }
     28 
     29 recomp_func_t recomp_lookup(uint32_t address)
     30 {
     31     if (address == 0x11110000u) return first_constructor;
     32     if (address == 0x22220000u) return second_constructor;
     33     if (address == 0x44440000u) return abort_callback;
     34     return NULL;
     35 }
     36 
     37 recomp_func_t recomp_lookup_import(uint32_t address)
     38 {
     39     fprintf(stderr, "Unexpected constructor address %08x\n", address);
     40     abort();
     41 }
     42 
     43 static void initializes_callbacks_in_order(void)
     44 {
     45     g_esp = 0x5000ff00u;
     46     g_cur_func = 0x33330000u;
     47     MEM32(g_esp) = RECOMP_RETADDR;
     48     MEM32(g_esp + 4) = 0x50001000u;
     49     MEM32(g_esp + 8) = 0x5000100cu;
     50     MEM32(0x50001000u) = 0x11110000u;
     51     MEM32(0x50001004u) = 0;
     52     MEM32(0x50001008u) = 0x22220000u;
     53 
     54     guest_crt_lookup("_initterm")();
     55 
     56     assert(MEM32(0x50002000u) == 12);
     57     assert(g_esp == 0x5000ff04u);
     58     assert(g_cur_func == 0x33330000u);
     59 }
     60 
     61 static void calloc_rejects_32_bit_overflow(void)
     62 {
     63     g_esp = 0x5000ff00u;
     64     MEM32(g_esp + 4) = 0x10000u;
     65     MEM32(g_esp + 8) = 0x10000u;
     66 
     67     guest_crt_lookup("calloc")();
     68 
     69     assert(g_eax == 0);
     70     assert(g_esp == 0x5000ff04u);
     71 }
     72 
     73 static void calloc_zeroes_reused_memory(void)
     74 {
     75     uint32_t allocation = guest_malloc(32);
     76     assert(allocation);
     77     memset((void *)ADDR(allocation), 0x55, 32);
     78     guest_free(allocation);
     79     g_esp = 0x5000ff00u;
     80     MEM32(g_esp + 4) = 4;
     81     MEM32(g_esp + 8) = 8;
     82 
     83     guest_crt_lookup("calloc")();
     84 
     85     assert(g_eax == allocation);
     86     assert(MEM64(g_eax) == 0 && MEM64(g_eax + 8) == 0);
     87     assert(MEM64(g_eax + 16) == 0 && MEM64(g_eax + 24) == 0);
     88     assert(g_esp == 0x5000ff04u);
     89     guest_free(g_eax);
     90 }
     91 
     92 static void dllonexit_preserves_callbacks_when_growing(void)
     93 {
     94     MEM32(0x50003000u) = guest_malloc(128);
     95     MEM32(0x50003004u) = MEM32(0x50003000u);
     96 
     97     for (uint32_t i = 0; i < 100; ++i) {
     98         g_esp = 0x5000ff00u;
     99         MEM32(g_esp + 4) = 0x11110000u + i * 4;
    100         MEM32(g_esp + 8) = 0x50003000u;
    101         MEM32(g_esp + 12) = 0x50003004u;
    102         guest_crt_lookup("__dllonexit")();
    103         assert(g_eax == 0x11110000u + i * 4);
    104         assert(g_esp == 0x5000ff04u);
    105     }
    106 
    107     uint32_t begin = MEM32(0x50003000u);
    108     assert(MEM32(0x50003004u) == begin + 400);
    109     assert(MEM32(begin) == 0x11110000u);
    110     assert(MEM32(begin + 128) == 0x11110080u);
    111     assert(MEM32(begin + 396) == 0x1111018cu);
    112     guest_free(begin);
    113 }
    114 
    115 static void eh_prolog_installs_guest_frame(void)
    116 {
    117     g_esp = 0x5000ff00u;
    118     MEM32(g_esp) = RECOMP_RETADDR;
    119     g_fs_base = 0x50004000u;
    120     MEM32(g_fs_base) = UINT32_MAX;
    121     g_eax = 0x12345678u;
    122     g_ebp = 0x5000ff80u;
    123 
    124     guest_crt_lookup("_EH_prolog")();
    125 
    126     assert(g_esp == 0x5000fef4u && g_ebp == 0x5000ff00u);
    127     assert(MEM32(g_fs_base) == 0x5000fef4u);
    128     assert(MEM32(g_esp) == UINT32_MAX);
    129     assert(MEM32(g_esp + 4) == 0x12345678u);
    130     assert(MEM32(g_esp + 8) == UINT32_MAX);
    131     assert(MEM32(g_ebp) == 0x5000ff80u);
    132     assert(g_eax == RECOMP_RETADDR);
    133 }
    134 
    135 static void rounding_returns_on_guest_fpu_stack(void)
    136 {
    137     const char *functions[] = {"floor", "ceil"};
    138     const double inputs[] = {1.75, -1.25, 0.0, -0.0, 0x1p52, 0x1p-1074, -0x1p-1074, INFINITY, -INFINITY};
    139     const double expected[][9] = {
    140         {1.0, -2.0, 0.0, -0.0, 0x1p52, 0, -1, INFINITY, -INFINITY},
    141         {2.0, -1.0, 0.0, -0.0, 0x1p52, 1, -0.0, INFINITY, -INFINITY},
    142     };
    143     for (unsigned function = 0; function < 2; ++function)
    144     for (unsigned i = 0; i < 9; ++i) {
    145         g_esp = 0x5000ff00u;
    146         MEMD(g_esp + 4) = inputs[i];
    147         g_fp_top = 1;
    148         g_st[0] = 123.5;
    149 
    150         guest_crt_lookup(functions[function])();
    151 
    152         assert(g_st[0] == expected[function][i] && signbit(g_st[0]) == signbit(expected[function][i]));
    153         assert(g_fp_top == 2 && g_st[1] == 123.5);
    154         assert(g_esp == 0x5000ff04u);
    155         assert(MEMD(0x5000ff04u) == inputs[i]);
    156     }
    157 }
    158 
    159 static void standard_stream_data_preserves_32_bit_layout(void)
    160 {
    161     uint32_t streams = guest_crt_data("_iob");
    162 
    163     uint32_t input_buffer = MEM32(streams);
    164     MEM8(input_buffer + 4095) = 0x5a;
    165 
    166     assert(MEM32(streams + 4) == 0 && MEM32(streams + 8) == input_buffer);
    167     assert(MEM32(streams + 12) == 0x101 && MEM32(streams + 16) == 0);
    168     assert(MEM32(streams + 24) == 4096 && MEM32(streams + 28) == 0);
    169     assert(MEM32(streams + 32) == 0 && MEM32(streams + 40) == 0);
    170     assert(MEM32(streams + 44) == 2 && MEM32(streams + 48) == 1);
    171     assert(MEM32(streams + 76) == 2 && MEM32(streams + 80) == 2);
    172     assert(MEM32(streams + 19 * 32 + 16) == 0);
    173     assert(MEM8(input_buffer + 4095) == 0x5a);
    174     assert(guest_crt_data("_iob") == streams);
    175     MEM8(input_buffer + 4095) = 0;
    176 }
    177 
    178 static void startup_copies_arguments_and_environment(void)
    179 {
    180     char executable[] = "./game";
    181     char login[] = "localtest";
    182     char *argv[] = {executable, login, "73", "", "with space", "say\"hi", "a b\\", "c\\d", NULL};
    183     char setting[] = "ANARCHY_SETUP=Setupf";
    184     char *environment[] = {setting, "EMPTY=", NULL};
    185 
    186     guest_crt_startup(8, argv, environment);
    187     login[0] = 'X';
    188     setting[0] = 'X';
    189     g_esp = 0x5000ff00u;
    190     MEM32(g_esp + 4) = 0x50005000u;
    191     MEM32(g_esp + 8) = 0x50005004u;
    192     MEM32(g_esp + 12) = 0x50005008u;
    193     MEM32(g_esp + 16) = 0;
    194     MEM32(g_esp + 20) = 0x5000500cu;
    195     MEM32(0x5000500cu) = 0;
    196     guest_crt_lookup("__getmainargs")();
    197 
    198     uint32_t arguments = MEM32(0x50005004u), variables = MEM32(0x50005008u);
    199     assert(g_eax == 0 && g_esp == 0x5000ff04u);
    200     assert(MEM32(0x50005000u) == 8 && MEM32(arguments + 32) == 0);
    201     assert(!strcmp((char *)ADDR(MEM32(arguments + 4)), "localtest"));
    202     assert(!strcmp((char *)ADDR(MEM32(arguments + 12)), ""));
    203     assert(!strcmp((char *)ADDR(MEM32(arguments + 20)), "say\"hi"));
    204     assert(!strcmp((char *)ADDR(MEM32(variables)), "ANARCHY_SETUP=Setupf"));
    205     assert(!strcmp((char *)ADDR(MEM32(variables + 4)), "EMPTY="));
    206     assert(MEM32(variables + 8) == 0);
    207     assert(MEM32(guest_crt_data("__initenv")) == variables);
    208     assert(MEM32(guest_crt_data("_pgmptr")) == MEM32(arguments));
    209     assert(!strcmp((char *)ADDR(MEM32(guest_crt_data("_acmdln"))),
    210                    "./game localtest 73 \"\" \"with space\" \"say\\\"hi\" \"a b\\\\\" c\\d"));
    211 }
    212 
    213 static void startup_exposes_writable_crt_modes(void)
    214 {
    215     g_esp = 0x5000ff00u;
    216     MEM32(g_esp + 4) = 2;
    217 
    218     guest_crt_lookup("__set_app_type")();
    219     guest_crt_lookup("__p__fmode")();
    220     MEM32(g_eax) = 0x8000;
    221     guest_crt_lookup("__p__commode")();
    222     MEM32(g_eax) = 0x4000;
    223 
    224     assert(MEM32(guest_crt_data("__app_type")) == 2);
    225     assert(MEM32(guest_crt_data("_fmode")) == 0x8000);
    226     assert(MEM32(guest_crt_data("_commode")) == 0x4000);
    227     assert(g_esp == 0x5000ff0cu);
    228 }
    229 
    230 static void startup_selects_53_bit_floating_point(void)
    231 {
    232     g_esp = 0x5000ff00u;
    233     MEM32(g_esp + 4) = 0x10000;
    234     MEM32(g_esp + 8) = 0x30000;
    235     g_fpu_cw = 0x027f;
    236 
    237     guest_crt_lookup("_controlfp")();
    238 
    239     assert(g_eax == 0x9001f && g_fpu_cw == 0x027f);
    240     assert(g_esp == 0x5000ff04u);
    241 }
    242 
    243 static void exit_callbacks_run_once_in_reverse_order(void)
    244 {
    245     MEM32(0x50002000u) = 0;
    246     for (uint32_t callback = 0x11110000u; callback <= 0x22220000u; callback += 0x11110000u) {
    247         g_esp = 0x5000ff00u;
    248         MEM32(g_esp + 4) = callback;
    249         guest_crt_lookup("_onexit")();
    250         assert(g_eax == callback && g_esp == 0x5000ff04u);
    251     }
    252     g_esp = 0x5000ff00u;
    253     g_cur_func = 0x33330000u;
    254 
    255     guest_crt_lookup("_cexit")();
    256     guest_crt_lookup("_cexit")();
    257 
    258     assert(MEM32(0x50002000u) == 21);
    259     assert(g_esp == 0x5000ff08u && g_cur_func == 0x33330000u);
    260 }
    261 
    262 static void errno_storage_follows_the_guest_thread(void)
    263 {
    264     assert(guest_map("first CRT thread", 0x51000000u, 0x10000u));
    265     assert(guest_map("second CRT thread", 0x51010000u, 0x10000u));
    266     uint32_t saved_thread = g_fs_base;
    267     g_fs_base = 0x51000000u;
    268     MEM32(g_fs_base + 0x34) = 777;
    269     g_esp = 0x5000ff00u;
    270 
    271     guest_crt_lookup("_errno")();
    272     uint32_t first = g_eax;
    273     assert(MEM32(first) == 0);
    274     MEM32(first) = 22;
    275     guest_crt_lookup("_errno")();
    276     assert(g_eax == first && MEM32(g_eax) == 22);
    277     g_fs_base = 0x51010000u;
    278     guest_crt_lookup("_errno")();
    279     uint32_t second = g_eax;
    280     assert(MEM32(second) == 0);
    281     MEM32(second) = 12;
    282     g_fs_base = 0x51000000u;
    283     guest_crt_lookup("_errno")();
    284 
    285     assert(g_eax == first && first != second);
    286     assert(MEM32(first) == 22 && MEM32(second) == 12);
    287     assert(MEM32(g_fs_base + 0x34) == 777);
    288     assert(g_esp == 0x5000ff10u);
    289     g_fs_base = saved_thread;
    290 }
    291 
    292 static void reverse_string_search_returns_guest_addresses(void)
    293 {
    294     strcpy((char *)ADDR(0x50006000u), "a.b.c\xff");
    295     const uint32_t chars[] = {'.', 'x', 0, 0x12e, 0xff, 0x1ff, UINT32_MAX};
    296     const uint32_t expected[] = {0x50006003u, 0, 0x50006006u, 0x50006003u,
    297                                  0x50006005u, 0x50006005u, 0x50006005u};
    298     const char *names[] = {"strrchr", "_mbsrchr"};
    299     for (unsigned i = 0; i < 14; ++i) {
    300         g_esp = 0x5000ff00u;
    301         MEM32(g_esp + 4) = 0x50006000u;
    302         MEM32(g_esp + 8) = chars[i % 7];
    303 
    304         guest_crt_lookup(names[i / 7])();
    305 
    306         assert(g_eax == expected[i % 7]);
    307         assert(g_esp == 0x5000ff04u);
    308     }
    309 }
    310 
    311 static void error_mode_preserves_previous_mode_and_errno(void)
    312 {
    313     uint32_t saved_thread = g_fs_base;
    314     g_fs_base = 0x51000000u;
    315     g_esp = 0x5000ff00u;
    316     guest_crt_lookup("_errno")();
    317     uint32_t error = g_eax;
    318     const int modes[] = {3, 1, 3, 2, 3, 0, 3, 4, -1, 3};
    319     const int previous[] = {0, 0, 1, 1, 2, 2, 0, -1, -1, 0};
    320     for (unsigned i = 0; i < 10; ++i) {
    321         MEM32(error) = 777;
    322         g_esp = 0x5000ff00u;
    323         MEM32(g_esp + 4) = modes[i];
    324 
    325         guest_crt_lookup("_set_error_mode")();
    326 
    327         assert((int32_t)g_eax == previous[i]);
    328         assert(MEM32(error) == (modes[i] == 4 || modes[i] == -1 ? 22 : 777));
    329         assert(g_esp == 0x5000ff04u);
    330     }
    331     g_fs_base = saved_thread;
    332 }
    333 
    334 static void synchronous_abort_signal_calls_guest_handler_once(void)
    335 {
    336     MEM32(0x50003000u) = 0;
    337     g_esp = 0x5000ff00u;
    338     MEM32(g_esp + 4) = 22;
    339     MEM32(g_esp + 8) = 0x44440000u;
    340     guest_crt_lookup("signal")();
    341     assert(g_eax == 0 && g_esp == 0x5000ff04u);
    342     g_cur_func = 0x55550000u;
    343     g_esp = 0x5000ff00u;
    344 
    345     guest_crt_lookup("raise")();
    346 
    347     assert(MEM32(0x50003000u) == 1 && g_eax == 0);
    348     assert(g_esp == 0x5000ff04u && g_cur_func == 0x55550000u);
    349     g_esp = 0x5000ff00u;
    350     MEM32(g_esp + 8) = 1;
    351     guest_crt_lookup("signal")();
    352     assert(g_eax == 0); /* Delivery reset the installed handler. */
    353     g_esp = 0x5000ff00u;
    354     guest_crt_lookup("raise")();
    355     assert(g_eax == 0 && MEM32(0x50003000u) == 1);
    356     g_esp = 0x5000ff00u;
    357     MEM32(g_esp + 8) = 0;
    358     guest_crt_lookup("signal")();
    359     assert(g_eax == 1);
    360 }
    361 
    362 static void strcpy_returns_guest_pointer_and_copies_terminator(void)
    363 {
    364     assert(guest_map("strcpy source", 0x53000000u, 0x20000u));
    365     assert(guest_map("strcpy destination", 0x52000000u, 0x20000u));
    366     const char *inputs[] = {"", "a", "cd_image\\data\\db\\ResourceDatabase.dat", "\x81\xff\x7f"};
    367     for (unsigned i = 0; i < sizeof(inputs) / sizeof(inputs[0]); ++i) {
    368         size_t length = strlen(inputs[i]);
    369         memcpy((void *)ADDR(0x5300fffeu), inputs[i], length + 1);
    370         memset((void *)ADDR(0x5200fffcu), 0xa5, 64);
    371         g_esp = 0x5000ff00u;
    372         MEM32(g_esp + 4) = 0x5200fffdu;
    373         MEM32(g_esp + 8) = 0x5300fffeu;
    374         g_ebx = 0x12345678;
    375         g_esi = 0x23456789;
    376         g_edi = 0x3456789a;
    377         g_ebp = 0x456789ab;
    378 
    379         guest_crt_lookup("strcpy")();
    380 
    381         assert(g_eax == 0x5200fffdu && g_esp == 0x5000ff04u);
    382         assert(!memcmp((void *)ADDR(0x5200fffdu), inputs[i], length + 1));
    383         assert(!memcmp((void *)ADDR(0x5300fffeu), inputs[i], length + 1));
    384         assert(MEM8(0x5200fffcu) == 0xa5);
    385         for (unsigned offset = length + 1; offset < 63; ++offset)
    386             assert(MEM8(0x5200fffdu + offset) == 0xa5);
    387         assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
    388         assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
    389     }
    390 }
    391 
    392 static void time_writes_only_a_32_bit_guest_value(void)
    393 {
    394     for (unsigned output = 0; output < 2; ++output) {
    395         memset((void *)ADDR(0x50007000u), 0xa5, 12);
    396         MEM32(guest_crt_errno_address()) = 777;
    397         g_esp = 0x5000ff00u;
    398         MEM32(g_esp + 4) = output ? 0x50007004u : 0;
    399         g_ebx = 0x12345678;
    400         g_esi = 0x23456789;
    401         g_edi = 0x3456789a;
    402         g_ebp = 0x456789ab;
    403         time_t before = time(NULL);
    404 
    405         guest_crt_lookup("time")();
    406         time_t after = time(NULL);
    407 
    408         assert(g_eax >= before && g_eax <= after);
    409         assert(MEM32(0x50007000u) == 0xa5a5a5a5);
    410         assert(MEM32(0x50007008u) == 0xa5a5a5a5);
    411         assert(MEM32(0x50007004u) == (output ? g_eax : 0xa5a5a5a5));
    412         assert(MEM32(guest_crt_errno_address()) == 777);
    413         assert(g_esp == 0x5000ff04u);
    414         assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
    415         assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
    416     }
    417 }
    418 
    419 static void substring_search_returns_guest_addresses(void)
    420 {
    421     strcpy((char *)ADDR(0x50008003u), "aaabaaa\xff");
    422     const char *needles[] = {"", "aaa", "aab", "ba", "\xff", "aaabaaa\xff", "abz", "aaabaaa\xff!"};
    423     const uint32_t expected[] = {0x50008003u, 0x50008003u, 0x50008004u, 0x50008006u,
    424                                  0x5000800au, 0x50008003u, 0, 0};
    425     for (unsigned i = 0; i < sizeof(needles) / sizeof(needles[0]); ++i) {
    426         strcpy((char *)ADDR(0x50009001u), needles[i]);
    427         g_esp = 0x5000ff00u;
    428         MEM32(g_esp + 4) = 0x50008003u;
    429         MEM32(g_esp + 8) = 0x50009001u;
    430 
    431         guest_crt_lookup("strstr")();
    432 
    433         assert(g_eax == expected[i] && g_esp == 0x5000ff04u);
    434         assert(!strcmp((char *)ADDR(0x50008003u), "aaabaaa\xff"));
    435         assert(!strcmp((char *)ADDR(0x50009001u), needles[i]));
    436     }
    437 }
    438 
    439 static void rejected_character_search_returns_guest_length(void)
    440 {
    441     const struct { const char *text, *reject; uint32_t length; } cases[] = {
    442         {"", ":", 0}, {"abc", "", 3}, {"abc", "a", 0},
    443         {"abc", "bc", 1}, {"abc", "c", 2}, {"abc", "z", 3},
    444         {"abc\0hidden", "h", 3}, {"aa:bb:cc", ":;", 2},
    445         {"\x80\xff:x", ":", 2}, {"\x80\xff:x", "\xff", 1},
    446     };
    447     for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
    448         strcpy((char *)ADDR(0x50008003u), cases[i].text);
    449         strcpy((char *)ADDR(0x50009001u), cases[i].reject);
    450         g_esp = 0x5000ff00u;
    451         MEM32(g_esp + 4) = 0x50008003u;
    452         MEM32(g_esp + 8) = 0x50009001u;
    453         MEM32(guest_crt_errno_address()) = 777;
    454         g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456;
    455 
    456         guest_crt_lookup("strcspn")();
    457 
    458         assert(g_eax == cases[i].length && g_esp == 0x5000ff04u);
    459         assert(!strcmp((char *)ADDR(0x50008003u), cases[i].text));
    460         assert(!strcmp((char *)ADDR(0x50009001u), cases[i].reject));
    461         assert(MEM32(guest_crt_errno_address()) == 777);
    462         assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
    463     }
    464 }
    465 
    466 int main(void)
    467 {
    468     assert(guest_map("test memory and stack", 0x50000000u, 0x10000u));
    469     g_fs_base = 0x50004000u;
    470     initializes_callbacks_in_order();
    471     calloc_rejects_32_bit_overflow();
    472     calloc_zeroes_reused_memory();
    473     dllonexit_preserves_callbacks_when_growing();
    474     eh_prolog_installs_guest_frame();
    475     standard_stream_data_preserves_32_bit_layout();
    476     rounding_returns_on_guest_fpu_stack();
    477     startup_copies_arguments_and_environment();
    478     startup_exposes_writable_crt_modes();
    479     startup_selects_53_bit_floating_point();
    480     exit_callbacks_run_once_in_reverse_order();
    481     errno_storage_follows_the_guest_thread();
    482     reverse_string_search_returns_guest_addresses();
    483     error_mode_preserves_previous_mode_and_errno();
    484     synchronous_abort_signal_calls_guest_handler_once();
    485     strcpy_returns_guest_pointer_and_copies_terminator();
    486     time_writes_only_a_32_bit_guest_value();
    487     substring_search_returns_guest_addresses();
    488     rejected_character_search_returns_guest_length();
    489     assert(MEM32(guest_crt_data("_adjust_fdiv")) == 0);
    490     puts("guest CRT: constructors, heap, EH frame, FILE data, rounding and startup state passed");
    491     return 0;
    492 }