client/tests/files.c (12693B)
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 #else 9 #include "linux_file.h" 10 #include "guest_memory.h" 11 #include <unistd.h> 12 13 static uint32_t call(const char *name, unsigned count, const uint32_t *args) 14 { 15 g_esp = 0x5000ff00u; 16 for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = args[i]; 17 g_ebx = 0x12345678; 18 g_esi = 0x23456789; 19 g_edi = 0x3456789a; 20 g_ebp = 0x456789ab; 21 linux_file_lookup(name)(); 22 assert(g_esp == 0x5000ff00u + (count + 1) * 4); 23 assert(g_ebx == 0x12345678 && g_esi == 0x23456789); 24 assert(g_edi == 0x3456789a && g_ebp == 0x456789ab); 25 return g_eax; 26 } 27 #endif 28 29 static uint32_t check_open(const char *path, unsigned expected_error) 30 { 31 #ifdef _WIN32 32 SetLastError(77); 33 #else 34 strcpy((char *)ADDR(0x50002000u), path); 35 MEM32(g_fs_base + 0x34) = 77; 36 uint32_t args[] = {0x50002000u, 0x80000000u, 1, 0, 3, 0x80, 0}; 37 #endif 38 39 #ifdef _WIN32 40 uint32_t handle = (uintptr_t)CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, 41 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 42 unsigned error = GetLastError(); 43 #else 44 uint32_t handle = call("CreateFileA", 7, args); 45 unsigned error = MEM32(g_fs_base + 0x34); 46 #endif 47 48 if (error != expected_error) fprintf(stderr, "open %s: error %u\n", path, error); 49 assert(error == expected_error); 50 assert((handle == UINT32_MAX) == (expected_error != 0)); 51 if (!expected_error) assert((int32_t)handle >= 0); 52 return handle; 53 } 54 55 static void check_read(uint32_t handle, unsigned requested, const char *expected, 56 unsigned expected_count, unsigned expected_error) 57 { 58 unsigned char bytes[24]; 59 uint32_t count[] = {0xa5a5a5a5, 99, 0xa5a5a5a5}; 60 memset(bytes, 0xa5, sizeof(bytes)); 61 #ifdef _WIN32 62 SetLastError(77); 63 #else 64 memcpy((void *)ADDR(0x50003000u), bytes, sizeof(bytes)); 65 memcpy((void *)ADDR(0x50004000u), count, sizeof(count)); 66 MEM32(g_fs_base + 0x34) = 77; 67 uint32_t args[] = {handle, requested ? 0x50003004u : 0, requested, 0x50004004u, 0}; 68 #endif 69 70 #ifdef _WIN32 71 int result = ReadFile((HANDLE)(uintptr_t)handle, requested ? bytes + 4 : NULL, 72 requested, (DWORD *)&count[1], NULL); 73 unsigned error = GetLastError(); 74 #else 75 int result = call("ReadFile", 5, args); 76 unsigned error = MEM32(g_fs_base + 0x34); 77 memcpy(bytes, (void *)ADDR(0x50003000u), sizeof(bytes)); 78 memcpy(count, (void *)ADDR(0x50004000u), sizeof(count)); 79 #endif 80 81 assert(result == (expected_error == 77)); 82 assert(error == expected_error && count[1] == expected_count); 83 assert(count[0] == 0xa5a5a5a5 && count[2] == 0xa5a5a5a5); 84 assert(!memcmp(bytes + 4, expected, expected_count)); 85 for (unsigned i = 0; i < 4; ++i) assert(bytes[i] == 0xa5); 86 for (unsigned i = 4 + expected_count; i < sizeof(bytes); ++i) assert(bytes[i] == 0xa5); 87 } 88 89 static void seek_preserves_positions_and_high_word_on_errors(uint32_t handle) 90 { 91 const struct { int32_t low, high; unsigned has_high, method, result; int32_t output_high; unsigned error; } cases[] = { 92 {0, 0, 0, 0, 0, 0, 77}, {2, 0, 0, 0, 2, 0, 77}, 93 {-1, 0, 0, 1, 1, 0, 77}, {-1, 0, 0, 0, UINT32_MAX, 0, 131}, 94 {0, 0, 0, 9, UINT32_MAX, 0, 87}, {-1, 0, 0, 2, 5, 0, 77}, 95 {-1, 0, 1, 0, UINT32_MAX, 0, 0}, {16, 1, 1, 0, 16, 1, 77}, 96 {0, 0, 0, 1, UINT32_MAX, 0, 87}, {0, 0, 1, 1, 16, 1, 77}, 97 {0, -1, 1, 0, UINT32_MAX, -1, 131}, {0, 0, 1, 1, 16, 1, 77}, 98 {2, 0, 1, 0, 2, 0, 77}, 99 }; 100 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { 101 uint32_t high[] = {0xa5a5a5a5, cases[i].high, 0xa5a5a5a5}; 102 #ifdef _WIN32 103 SetLastError(77); 104 #else 105 memcpy((void *)ADDR(0x50004000u), high, sizeof(high)); 106 MEM32(g_fs_base + 0x34) = 77; 107 uint32_t args[] = {handle, cases[i].low, cases[i].has_high ? 0x50004004u : 0, cases[i].method}; 108 #endif 109 110 #ifdef _WIN32 111 uint32_t result = SetFilePointer((HANDLE)(uintptr_t)handle, cases[i].low, 112 cases[i].has_high ? (LONG *)&high[1] : NULL, cases[i].method); 113 unsigned error = GetLastError(); 114 #else 115 uint32_t result = call("SetFilePointer", 4, args); 116 unsigned error = MEM32(g_fs_base + 0x34); 117 memcpy(high, (void *)ADDR(0x50004000u), sizeof(high)); 118 #endif 119 120 assert(result == cases[i].result && error == cases[i].error); 121 assert(high[0] == 0xa5a5a5a5 && high[2] == 0xa5a5a5a5); 122 assert(high[1] == (uint32_t)cases[i].output_high); 123 } 124 } 125 126 static void check_close(uint32_t handle, int expected) 127 { 128 #ifdef _WIN32 129 SetLastError(77); 130 #else 131 MEM32(g_fs_base + 0x34) = 77; 132 #endif 133 134 #ifdef _WIN32 135 int result = CloseHandle((HANDLE)(uintptr_t)handle); 136 unsigned error = GetLastError(); 137 #else 138 int result = call("CloseHandle", 1, &handle); 139 unsigned error = MEM32(g_fs_base + 0x34); 140 #endif 141 142 assert(result == expected && error == (expected ? 77 : 6)); 143 } 144 145 static void unbuffered_database_reads_use_aligned_guest_buffers(void) 146 { 147 #ifdef _WIN32 148 unsigned char *buffer = VirtualAlloc(NULL, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); 149 assert(buffer); 150 #else 151 unsigned char *buffer = (void *)ADDR(0x50005000u); 152 #endif 153 const char *paths[] = {"fixture.dat", "created.dat"}; 154 for (unsigned file_index = 0; file_index < 2; ++file_index) { 155 #ifdef _WIN32 156 uint32_t handle = (uintptr_t)CreateFileA(paths[file_index], GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 157 NULL, OPEN_ALWAYS, FILE_FLAG_NO_BUFFERING | FILE_FLAG_RANDOM_ACCESS, NULL); 158 unsigned error = GetLastError(); 159 #else 160 strcpy((char *)ADDR(0x50002000u), paths[file_index]); 161 uint32_t args[] = {0x50002000u, 0x80000000u, 3, 0, 4, 0x30000000u, 0}; 162 uint32_t handle = call("CreateFileA", 7, args); 163 unsigned error = MEM32(g_fs_base + 0x34); 164 #endif 165 assert((int32_t)handle >= 0 && error == (file_index ? 0 : 183)); 166 const struct { unsigned size, offset, count, error; } cases[] = { 167 {1, 0, 0, 87}, {512, 1, 0, 87}, {512, 0, 6, 77}, {0, 0, 0, 77}, 168 }; 169 for (unsigned i = 0; i < 4; ++i) { 170 memset(buffer, 0xa5, 1024); 171 uint32_t count = 99; 172 #ifdef _WIN32 173 assert(SetFilePointer((HANDLE)(uintptr_t)handle, 0, NULL, FILE_BEGIN) == 0); 174 SetLastError(77); 175 #else 176 uint32_t seek[] = {handle, 0, 0, 0}; 177 assert(call("SetFilePointer", 4, seek) == 0); 178 MEM32(0x50004004u) = 99; 179 MEM32(g_fs_base + 0x34) = 77; 180 uint32_t read[] = {handle, 0x50005000u + cases[i].offset, cases[i].size, 0x50004004u, 0}; 181 #endif 182 183 #ifdef _WIN32 184 int result = ReadFile((HANDLE)(uintptr_t)handle, buffer + cases[i].offset, 185 cases[i].size, (DWORD *)&count, NULL); 186 error = GetLastError(); 187 #else 188 int result = call("ReadFile", 5, read); 189 error = MEM32(g_fs_base + 0x34); 190 count = MEM32(0x50004004u); 191 #endif 192 193 assert(result == (cases[i].error == 77) && error == cases[i].error); 194 assert(count == (file_index ? 0 : cases[i].count)); 195 assert(!memcmp(buffer, "A\0\xff" "B\r\n", count)); 196 for (unsigned j = count; j < 1024; ++j) assert(buffer[j] == 0xa5); 197 } 198 #ifdef _WIN32 199 uint32_t invalid_seek = SetFilePointer((HANDLE)(uintptr_t)handle, 1, NULL, FILE_BEGIN); 200 unsigned seek_error = GetLastError(); 201 uint32_t position = SetFilePointer((HANDLE)(uintptr_t)handle, 0, NULL, FILE_CURRENT); 202 #else 203 uint32_t seek[] = {handle, 1, 0, 0}; 204 uint32_t invalid_seek = call("SetFilePointer", 4, seek); 205 unsigned seek_error = MEM32(g_fs_base + 0x34); 206 seek[1] = 0; 207 seek[3] = 1; 208 uint32_t position = call("SetFilePointer", 4, seek); 209 #endif 210 assert(invalid_seek == UINT32_MAX && seek_error == 87 && position == 0); 211 check_close(handle, 1); 212 } 213 #ifdef _WIN32 214 assert(VirtualFree(buffer, 0, MEM_RELEASE)); 215 #endif 216 assert(!remove("created.dat")); 217 } 218 219 static void exclusive_diagnostic_reads(void) 220 { 221 const char *paths[] = {"missing.dat", "fixture.dat", "fixture.dat"}; 222 uint32_t handles[3]; 223 const unsigned errors[] = {2, 0, 32}; 224 for (unsigned i = 0; i < 3; ++i) { 225 #ifdef _WIN32 226 SetLastError(77); 227 #else 228 strcpy((char *)ADDR(0x50002000u), paths[i]); 229 MEM32(g_fs_base + 0x34) = 77; 230 uint32_t args[] = {0x50002000u, 0x80000000u, 0, 0, 3, 0x80000000u, 0}; 231 #endif 232 233 #ifdef _WIN32 234 handles[i] = (uintptr_t)CreateFileA(paths[i], GENERIC_READ, 0, NULL, 235 OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL); 236 unsigned error = GetLastError(); 237 #else 238 handles[i] = call("CreateFileA", 7, args); 239 unsigned error = MEM32(g_fs_base + 0x34); 240 #endif 241 242 assert(error == errors[i]); 243 assert((handles[i] == UINT32_MAX) == (i != 1)); 244 } 245 check_open("fixture.dat", 32); 246 check_read(handles[1], 6, "A\0\xff" "B\r\n", 6, 77); 247 check_close(handles[1], 1); 248 check_close(check_open("fixture.dat", 0), 1); 249 } 250 251 static void file_size_reports_bytes_and_invalid_handles(void) 252 { 253 uint32_t handle = check_open("fixture.dat", 0); 254 for (unsigned invalid = 0; invalid < 2; ++invalid) { 255 for (unsigned with_high = 0; with_high < 2; ++with_high) { 256 uint32_t high = 77; 257 #ifdef _WIN32 258 SetLastError(77); 259 #else 260 MEM32(g_fs_base + 0x34) = 77; 261 MEM32(0x50002000u) = high; 262 uint32_t args[] = {invalid ? UINT32_MAX : handle, with_high ? 0x50002000u : 0}; 263 #endif 264 265 #ifdef _WIN32 266 uint32_t size = GetFileSize((HANDLE)(uintptr_t)(invalid ? UINT32_MAX : handle), 267 with_high ? (DWORD *)&high : NULL); 268 unsigned error = GetLastError(); 269 #else 270 uint32_t size = call("GetFileSize", 2, args); 271 unsigned error = MEM32(g_fs_base + 0x34); 272 high = MEM32(0x50002000u); 273 #endif 274 275 assert(size == (invalid ? UINT32_MAX : 6)); 276 assert(error == (invalid ? 6 : 77)); 277 assert(high == (!invalid && with_high ? 0 : 77)); 278 } 279 } 280 check_close(handle, 1); 281 } 282 283 int main(void) 284 { 285 /* Own a disposable directory and create the binary fixture through native stdio. */ 286 #ifdef _WIN32 287 char temporary[MAX_PATH], directory[MAX_PATH]; 288 assert(GetTempPathA(sizeof(temporary), temporary)); 289 assert(GetTempFileNameA(temporary, "aof", 0, directory)); 290 assert(DeleteFileA(directory) && CreateDirectoryA(directory, NULL)); 291 assert(SetCurrentDirectoryA(directory)); 292 #else 293 assert(guest_map("file test memory", 0x50000000u, 0x10000u)); 294 g_fs_base = 0x50001000u; 295 char directory[] = "/tmp/ao-file-test-XXXXXX"; 296 assert(mkdtemp(directory) && !chdir(directory)); 297 #endif 298 FILE *file = fopen("fixture.dat", "wb"); 299 assert(file && fwrite("A\0\xff" "B\r\n", 1, 6, file) == 6 && !fclose(file)); 300 301 check_open("missing.dat", 2); 302 check_open("missing\\fixture.dat", 3); 303 check_open("fixture.dat\\child", 3); 304 check_open(".", 5); 305 uint32_t first = check_open(".\\FIXTURE.DAT", 0); 306 uint32_t second = check_open("./fixture.dat", 0); 307 assert(first != second); 308 check_read(first, 2, "A\0", 2, 77); 309 check_read(first, 12, "\xff" "B\r\n", 4, 77); 310 check_read(first, 1, "", 0, 77); 311 check_read(first, 0, "", 0, 77); 312 check_read(second, 12, "A\0\xff" "B\r\n", 6, 77); 313 seek_preserves_positions_and_high_word_on_errors(first); 314 check_read(first, 12, "\xff" "B\r\n", 4, 77); 315 check_close(first, 1); 316 check_read(first, 12, "", 0, 6); 317 check_close(first, 0); 318 check_close(second, 1); 319 unbuffered_database_reads_use_aligned_guest_buffers(); 320 exclusive_diagnostic_reads(); 321 file_size_reports_bytes_and_invalid_handles(); 322 323 /* The read-only adapter must leave the fixture intact. */ 324 file = fopen("fixture.dat", "rb"); 325 unsigned char bytes[7]; 326 assert(file && fread(bytes, 1, sizeof(bytes), file) == 6 && !fclose(file)); 327 assert(!memcmp(bytes, "A\0\xff" "B\r\n", 6)); 328 assert(!remove("fixture.dat")); 329 #ifdef _WIN32 330 assert(SetCurrentDirectoryA(temporary) && RemoveDirectoryA(directory)); 331 #else 332 assert(!chdir("/tmp") && !rmdir(directory)); 333 #endif 334 puts("files: shared opens, binary/direct reads, 13 seek cases, independent positions, EOF, alignment, errors, close and guest ABI passed"); 335 puts("diagnostic files: exclusive read/write-through mode, sharing conflicts, missing files, size and invalid handles passed"); 336 }