client/tests/crt_time.c (4890B)
1 #include <assert.h> 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <string.h> 5 #ifdef _WIN32 6 #include <windows.h> 7 #else 8 #include <stdlib.h> 9 #include <time.h> 10 #include "guest_crt.h" 11 #include "guest_memory.h" 12 recomp_func_t recomp_lookup(uint32_t address) { abort(); } 13 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } 14 15 static time_t current_time; 16 time_t __wrap_time(time_t *output) 17 { 18 if (output) *output = current_time; 19 return current_time; 20 } 21 22 static void local_date_formats_exactly_nine_bytes(void) 23 { 24 const struct { time_t seconds; const char *zone, *text; } cases[] = { 25 {0, "UTC0", "01/01/70"}, 26 {951782400, "UTC0", "02/29/00"}, 27 {946684799, "UTC0", "12/31/99"}, 28 {946684800, "UTC0", "01/01/00"}, 29 {2147483647, "UTC0", "01/19/38"}, 30 {0, "PST8", "12/31/69"}, 31 {28800, "PST8", "01/01/70"}, 32 }; 33 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { 34 current_time = cases[i].seconds; 35 setenv("TZ", cases[i].zone, 1); 36 tzset(); 37 memset((void *)ADDR(0x50000100), 0xa5, 11); 38 MEM32(guest_crt_errno_address()) = 777; 39 g_esp = 0x5000ff00; 40 MEM32(g_esp + 4) = 0x50000101; 41 g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; 42 43 guest_crt_lookup("_strdate")(); 44 45 assert(g_eax == 0x50000101 && g_esp == 0x5000ff04); 46 assert(!strcmp((char *)ADDR(g_eax), cases[i].text)); 47 assert(MEM8(0x50000100) == 0xa5 && MEM8(0x5000010a) == 0xa5); 48 assert(MEM32(guest_crt_errno_address()) == 777); 49 assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); 50 } 51 } 52 #endif 53 54 int main(void) 55 { 56 const struct { int32_t seconds, fields[9]; const char *text; } cases[] = { 57 {0, {0, 0, 0, 1, 0, 70, 4, 0, 0}, "Thu Jan 01 00:00:00 1970\n"}, 58 {86400, {0, 0, 0, 2, 0, 70, 5, 1, 0}, "Fri Jan 02 00:00:00 1970\n"}, 59 {951782400, {0, 0, 0, 29, 1, 100, 2, 59, 0}, "Tue Feb 29 00:00:00 2000\n"}, 60 {2147483647, {7, 14, 3, 19, 0, 138, 2, 18, 0}, "Tue Jan 19 03:14:07 2038\n"}, 61 {-1, {-1, 0, 0, 1, 0, 70, 4, 0, 0}, NULL}, 62 {-43200, {0, 0, -12, 1, 0, 70, 4, 0, 0}, NULL}, 63 }; 64 #ifdef _WIN32 65 HMODULE library = LoadLibraryA("msvcrt.dll"); 66 int32_t *(__cdecl *convert)(const int32_t *) = (void *)GetProcAddress(library, "gmtime"); 67 char *(__cdecl *format)(const int32_t *) = (void *)GetProcAddress(library, "asctime"); 68 int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(library, "_errno"); 69 assert(convert && format && get_errno); 70 #else 71 assert(guest_map("CRT time test", 0x50000000, 0x10000)); 72 g_fs_base = 0x50000000; 73 #endif 74 const int32_t *previous = NULL; 75 for (unsigned i = 0; i < 6; ++i) { 76 #ifdef _WIN32 77 *get_errno() = 777; 78 #else 79 MEM32(guest_crt_errno_address()) = 777; 80 MEM32(0x50000100) = cases[i].seconds; 81 g_esp = 0x5000ff00; 82 MEM32(g_esp + 4) = 0x50000100; 83 g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; 84 #endif 85 86 #ifdef _WIN32 87 const int32_t *fields = convert(&cases[i].seconds); 88 #else 89 guest_crt_lookup("gmtime")(); 90 assert(g_eax && g_esp == 0x5000ff04); 91 uint32_t address = g_eax; 92 const int32_t *fields = (void *)ADDR(address); 93 #endif 94 95 assert(fields && !memcmp(fields, cases[i].fields, 36)); 96 assert(!previous || previous == fields); 97 previous = fields; 98 #ifdef _WIN32 99 assert(*get_errno() == 777); 100 const char *text = format(fields); 101 int error = *get_errno(); 102 #else 103 assert(MEM32(guest_crt_errno_address()) == 777); 104 g_esp = 0x5000ff00; 105 MEM32(g_esp + 4) = address; 106 guest_crt_lookup("asctime")(); 107 const char *text = g_eax ? (char *)ADDR(g_eax) : NULL; 108 int error = MEM32(guest_crt_errno_address()); 109 assert(g_esp == 0x5000ff04); 110 assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); 111 #endif 112 if (cases[i].text) assert(text && !strcmp(text, cases[i].text) && error == 777); 113 else assert(!text && error == 22); 114 } 115 const int32_t invalid[] = {-43201, INT32_MIN}; 116 for (unsigned i = 0; i < 2; ++i) { 117 #ifdef _WIN32 118 *get_errno() = 777; 119 #else 120 MEM32(guest_crt_errno_address()) = 777; 121 MEM32(0x50000100) = invalid[i]; 122 g_esp = 0x5000ff00; 123 MEM32(g_esp + 4) = 0x50000100; 124 #endif 125 126 #ifdef _WIN32 127 const int32_t *result = convert(&invalid[i]); 128 #else 129 guest_crt_lookup("gmtime")(); 130 #endif 131 132 #ifdef _WIN32 133 assert(!result && *get_errno() == 22); 134 #else 135 assert(!g_eax && MEM32(guest_crt_errno_address()) == 22); 136 assert(g_esp == 0x5000ff04); 137 #endif 138 } 139 #ifndef _WIN32 140 local_date_formats_exactly_nine_bytes(); 141 #endif 142 puts("CRT time: epoch, leap day, 2038 limit, negative boundary, invalid times, x86 layout, stable storage, errno and ABI passed"); 143 }