client/runtime/guest_stdio.c (32745B)
1 #define _GNU_SOURCE 2 #include "browser_profile.h" 3 #include "linux_path.h" 4 #include <pthread.h> 5 #include "guest_stdio.h" 6 #include "guest_crt.h" 7 #include "guest_heap.h" 8 #include "guest_format.h" 9 #include <errno.h> 10 #include <ctype.h> 11 #include <fcntl.h> 12 #include <limits.h> 13 #include <strings.h> 14 #include <sys/stat.h> 15 #include <unistd.h> 16 17 /* FILE pointers and buffers are guest addresses. Native descriptors stay here. */ 18 struct stream { 19 pthread_mutex_t mutex; 20 int in_use, writable; 21 uint32_t file, input_bytes; 22 int descriptor, text, text_eof, lookahead, input_lf; 23 }; 24 static struct stream streams[512]; 25 static unsigned stream_count; 26 static unsigned char descriptor_numbers[512]; 27 static pthread_mutex_t registry_mutex = PTHREAD_MUTEX_INITIALIZER; 28 static pthread_once_t streams_once = PTHREAD_ONCE_INIT; 29 30 static int file_error(int error) 31 { 32 if (error == ENOTDIR) error = ENOENT; 33 if (error == EISDIR || error == EROFS) error = EACCES; 34 if (error == ENAMETOOLONG) error = 38; /* MSVCRT ENAMETOOLONG */ 35 MEM32(guest_crt_errno_address()) = error; 36 return -1; 37 } 38 39 static void initialize_streams(void) 40 { 41 for (unsigned i = 0; i < 512; ++i) 42 if (pthread_mutex_init(&streams[i].mutex, NULL)) abort(); 43 uint32_t iob = guest_crt_data("_iob"); 44 for (unsigned i = 0; i < 20; ++i) { 45 streams[i].file = iob + i * 32; 46 streams[i].descriptor = i < 3 ? fcntl(i, F_DUPFD_CLOEXEC, 3) : -1; 47 streams[i].in_use = i < 3; 48 streams[i].writable = i == 1 || i == 2; 49 descriptor_numbers[i] = i < 3; 50 streams[i].text = 1; 51 streams[i].lookahead = -1; 52 if (i < 3 && streams[i].descriptor < 0) { perror("standard stream duplication"); abort(); } 53 } 54 stream_count = 20; 55 } 56 57 static struct stream *find_stream(uint32_t file) 58 { 59 if (pthread_once(&streams_once, initialize_streams)) abort(); 60 if (pthread_mutex_lock(®istry_mutex)) abort(); 61 struct stream *stream = NULL; 62 for (unsigned i = 0; i < stream_count; ++i) 63 if (streams[i].file == file) { stream = &streams[i]; break; } 64 if (pthread_mutex_unlock(®istry_mutex)) abort(); 65 if (stream) return stream; 66 fprintf(stderr, "Unknown guest FILE address %08x\n", file); 67 abort(); 68 } 69 70 static int write_bytes(struct stream *stream, const char *data, size_t size) 71 { 72 while (size) { 73 ssize_t written = write(stream->descriptor, data, size); 74 if (written < 0 && errno == EINTR) continue; 75 if (written <= 0) { 76 MEM32(stream->file + 12) |= 0x20; /* _IOERR */ 77 return file_error(written < 0 ? errno : ENOSPC); 78 } 79 data += written; 80 size -= written; 81 } 82 return 0; 83 } 84 85 static int write_text(struct stream *stream, const char *data, size_t size) 86 { 87 if (!stream->text) return write_bytes(stream, data, size); 88 const char *end = data + size; 89 while (data < end) { 90 const char *newline = memchr(data, '\n', end - data); 91 const char *stop = newline ? newline : end; 92 if (write_bytes(stream, data, stop - data)) return -1; 93 if (newline && write_bytes(stream, "\r\n", 2)) return -1; 94 data = newline ? newline + 1 : end; 95 } 96 return 0; 97 } 98 99 static int flush_stream(struct stream *stream) 100 { 101 uint32_t file = stream->file, flags = MEM32(file + 12); 102 if (stream->descriptor < 0) return file_error(EBADF); 103 if (!(flags & 2)) { 104 /* Legacy fflush discards unread bytes without moving the descriptor. */ 105 MEM32(file) = MEM32(file + 8); 106 MEM32(file + 4) = 0; 107 return 0; 108 } 109 uint32_t base = MEM32(file + 8), pointer = MEM32(file); 110 int result = 0; 111 if (base && pointer != base && !(flags & 4)) { 112 if (pointer < base || pointer - base > MEM32(file + 24)) abort(); 113 result = write_text(stream, (const char *)ADDR(base), pointer - base); 114 } 115 MEM32(file) = base; 116 MEM32(file + 4) = 0; 117 if (flags & 0x80) MEM32(file + 12) &= ~2u; 118 return result; 119 } 120 121 static int flush_all(void) 122 { 123 /* Snapshot writable streams without waiting for unrelated input reads. */ 124 if (pthread_once(&streams_once, initialize_streams)) abort(); 125 struct stream *pending[512]; 126 unsigned count = 0; 127 if (pthread_mutex_lock(®istry_mutex)) abort(); 128 for (unsigned i = 0; i < stream_count; ++i) 129 if (streams[i].in_use && streams[i].writable) pending[count++] = &streams[i]; 130 if (pthread_mutex_unlock(®istry_mutex)) abort(); 131 int result = 0; 132 for (unsigned i = 0; i < count; ++i) { 133 if (pthread_mutex_lock(&pending[i]->mutex)) abort(); 134 if (pending[i]->descriptor >= 0 && (MEM32(pending[i]->file + 12) & 2)) 135 if (flush_stream(pending[i])) result = -1; 136 if (pthread_mutex_unlock(&pending[i]->mutex)) abort(); 137 } 138 return result; 139 } 140 141 static int close_stream(struct stream *stream) 142 { 143 if (stream->descriptor < 0) return file_error(EBADF); 144 int result = flush_stream(stream); 145 if (close(stream->descriptor)) result = file_error(errno); 146 uint32_t file = stream->file; 147 if (MEM32(file + 12) & 8) guest_free(MEM32(file + 8)); 148 MEM32(file) = MEM32(file + 4) = MEM32(file + 8) = MEM32(file + 12) = 0; 149 stream->descriptor = -1; 150 if (pthread_mutex_lock(®istry_mutex)) abort(); 151 descriptor_numbers[MEM32(file + 16)] = 0; 152 stream->writable = 0; 153 if (pthread_mutex_unlock(®istry_mutex)) abort(); 154 return result; 155 } 156 157 static uint32_t open_stream(struct stream *stream, const char *name, const char *mode) 158 { 159 int options, flags, text = MEM32(guest_crt_data("_fmode")) != 0x8000; 160 if (*mode == 'r') { options = O_RDONLY; flags = 1; } 161 else if (*mode == 'w') { options = O_WRONLY | O_CREAT | O_TRUNC; flags = 2; } 162 else if (*mode == 'a') { options = O_WRONLY | O_CREAT | O_APPEND; flags = 2; } 163 else { file_error(EINVAL); return 0; } 164 for (const char *p = mode + 1; *p; ++p) { 165 if (*p == '+') { options = (options & ~O_ACCMODE) | O_RDWR; flags = 0x80; } 166 else if (*p == 'b') text = 0; 167 else if (*p == 't') text = 1; 168 else { fprintf(stderr, "Unsupported guest stream mode: %s\n", mode); abort(); } 169 } 170 if ((name[0] && name[1] == ':') || (name[0] == '\\' && name[1] == '\\')) { 171 fprintf(stderr, "Windows drive/UNC stream paths are not implemented: %s\n", name); 172 abort(); 173 } 174 char *path = linux_path(name); 175 struct stat info; 176 if (!stat(path, &info) && (S_ISDIR(info.st_mode) || 177 ((options & O_ACCMODE) != O_RDONLY && !(info.st_mode & 0222)))) { 178 free(path); 179 file_error(EACCES); 180 return 0; 181 } 182 int descriptor = open(path, options | O_CLOEXEC, 0666); 183 int error = errno; 184 free(path); 185 if (descriptor < 0) { file_error(error); return 0; } 186 187 /* Reserve the guest number under the registry lock, after file I/O. */ 188 if (pthread_mutex_lock(®istry_mutex)) abort(); 189 unsigned number; 190 for (number = 0; number < 512 && descriptor_numbers[number]; ++number) {} 191 if (number == 512) abort(); 192 descriptor_numbers[number] = 1; 193 stream->writable = (flags & (2 | 0x80)) != 0; 194 if (pthread_mutex_unlock(®istry_mutex)) abort(); 195 stream->descriptor = descriptor; 196 stream->text = text; 197 stream->text_eof = 0; 198 stream->lookahead = -1; 199 MEM32(stream->file) = MEM32(stream->file + 4) = MEM32(stream->file + 8) = 0; 200 MEM32(stream->file + 12) = flags; 201 MEM32(stream->file + 16) = number; 202 MEM32(stream->file + 28) = 0; 203 return stream->file; 204 } 205 206 static void crt_fopen(void) 207 { 208 /* Reserve a FILE before opening it; neither open nor read holds the registry. */ 209 if (pthread_once(&streams_once, initialize_streams)) abort(); 210 if (pthread_mutex_lock(®istry_mutex)) abort(); 211 unsigned i; 212 for (i = 0; i < stream_count; ++i) if (!streams[i].in_use) break; 213 if (i == 512) { 214 if (pthread_mutex_unlock(®istry_mutex)) abort(); 215 file_error(EMFILE); g_eax = 0; g_esp += 4; 216 return; 217 } 218 if (i == stream_count) { 219 streams[i].file = guest_malloc(32); 220 if (!streams[i].file) { 221 if (pthread_mutex_unlock(®istry_mutex)) abort(); 222 file_error(ENOMEM); g_eax = 0; g_esp += 4; 223 return; 224 } 225 memset((void *)ADDR(streams[i].file), 0, 32); 226 streams[i].descriptor = -1; 227 ++stream_count; 228 } 229 streams[i].in_use = 1; 230 if (pthread_mutex_unlock(®istry_mutex)) abort(); 231 if (pthread_mutex_lock(&streams[i].mutex)) abort(); 232 g_eax = open_stream(&streams[i], (const char *)ADDR(MEM32(g_esp + 4)), 233 (const char *)ADDR(MEM32(g_esp + 8))); 234 if (!g_eax) { 235 if (pthread_mutex_lock(®istry_mutex)) abort(); 236 streams[i].in_use = 0; 237 if (pthread_mutex_unlock(®istry_mutex)) abort(); 238 } 239 if (pthread_mutex_unlock(&streams[i].mutex)) abort(); 240 g_esp += 4; 241 } 242 243 static void crt_freopen_locked(void) 244 { 245 struct stream *stream = find_stream(MEM32(g_esp + 12)); 246 if (pthread_mutex_lock(®istry_mutex)) abort(); 247 stream->in_use = 1; 248 if (pthread_mutex_unlock(®istry_mutex)) abort(); 249 if (stream->descriptor >= 0) close_stream(stream); 250 g_eax = open_stream(stream, (const char *)ADDR(MEM32(g_esp + 4)), 251 (const char *)ADDR(MEM32(g_esp + 8))); 252 if (!g_eax) { 253 if (pthread_mutex_lock(®istry_mutex)) abort(); 254 stream->in_use = 0; 255 if (pthread_mutex_unlock(®istry_mutex)) abort(); 256 } 257 g_esp += 4; 258 } 259 260 static int put_character(struct stream *stream, unsigned char character) 261 { 262 uint32_t file = stream->file, flags = MEM32(file + 12); 263 if (stream->descriptor < 0 || !(flags & (2 | 0x80))) { 264 MEM32(file + 12) |= 0x20; 265 return file_error(EBADF); 266 } 267 if (flags & 1) { 268 fprintf(stderr, "Switching buffered input to output requires guest input support\n"); 269 abort(); 270 } 271 MEM32(file + 12) |= 2; 272 if (flags & 4) return write_text(stream, (const char *)&character, 1) ? -1 : character; 273 if (!MEM32(file + 8)) { 274 uint32_t buffer = guest_malloc(4096); 275 if (!buffer) return file_error(ENOMEM); 276 MEM32(file) = MEM32(file + 8) = buffer; 277 MEM32(file + 24) = 4096; 278 MEM32(file + 12) |= 8; 279 } 280 if ((int32_t)MEM32(file + 4) <= 0) { 281 if (flush_stream(stream)) return -1; 282 MEM32(file + 12) |= 2; 283 MEM32(file + 4) = MEM32(file + 24); 284 } 285 MEM8(MEM32(file)) = character; 286 ++MEM32(file); 287 --MEM32(file + 4); 288 return character; 289 } 290 291 static void crt_fputc_locked(void) 292 { 293 g_eax = put_character(find_stream(MEM32(g_esp + 8)), MEM32(g_esp + 4)); 294 g_esp += 4; 295 } 296 297 static void crt_fwrite_locked(void) 298 { 299 uint32_t bytes = MEM32(g_esp + 4), size = MEM32(g_esp + 8), count = MEM32(g_esp + 12); 300 if (!size || !count) { g_eax = 0; g_esp += 4; return; } 301 uint64_t total = (uint64_t)size * count, written = 0; 302 if (total > UINT32_MAX) { file_error(EINVAL); g_eax = 0; g_esp += 4; return; } 303 struct stream *stream = find_stream(MEM32(g_esp + 16)); 304 while (written < total && put_character(stream, MEM8(bytes + (uint32_t)written)) >= 0) ++written; 305 g_eax = size ? written / size : 0; 306 g_esp += 4; 307 } 308 309 static void crt_fputs_locked(void) 310 { 311 const unsigned char *text = (const unsigned char *)ADDR(MEM32(g_esp + 4)); 312 struct stream *stream = find_stream(MEM32(g_esp + 8)); 313 g_eax = 0; 314 while (*text) if (put_character(stream, *text++) < 0) { g_eax = UINT32_MAX; break; } 315 g_esp += 4; 316 } 317 318 static void crt_fflush_locked(void) 319 { 320 uint32_t file = MEM32(g_esp + 4); 321 g_eax = file ? flush_stream(find_stream(file)) : flush_all(); 322 g_esp += 4; 323 } 324 325 static void crt_fclose_locked(void) 326 { 327 struct stream *stream = find_stream(MEM32(g_esp + 4)); 328 g_eax = close_stream(stream); 329 if (pthread_mutex_lock(®istry_mutex)) abort(); 330 stream->in_use = 0; 331 if (pthread_mutex_unlock(®istry_mutex)) abort(); 332 g_esp += 4; 333 } 334 335 static void crt_setvbuf_locked(void) 336 { 337 struct stream *stream = find_stream(MEM32(g_esp + 4)); 338 uint32_t file = stream->file, buffer = MEM32(g_esp + 8), mode = MEM32(g_esp + 12), size = MEM32(g_esp + 16); 339 g_eax = UINT32_MAX; 340 if (mode != 0 && mode != 0x40 && mode != 4) file_error(EINVAL); 341 else if (mode != 4 && (size < 2 || size > INT_MAX)) file_error(EINVAL); 342 else if (!flush_stream(stream)) { 343 size &= ~1u; /* MSVCRT buffers use an even byte count. */ 344 if (MEM32(file + 12) & 8) guest_free(MEM32(file + 8)); 345 MEM32(file + 12) &= ~(8u | 0x100u | 0x400u | 4u); 346 if (mode == 4) { buffer = file + 20; size = 2; MEM32(file + 12) |= 4; } 347 else if (buffer) MEM32(file + 12) |= 0x500; 348 else { 349 buffer = guest_malloc(size); 350 if (!buffer) { fprintf(stderr, "Guest stream buffer allocation failed\n"); abort(); } 351 MEM32(file + 12) |= 0x408; 352 } 353 MEM32(file) = MEM32(file + 8) = buffer; 354 MEM32(file + 4) = 0; 355 MEM32(file + 24) = size; 356 g_eax = 0; 357 } 358 g_esp += 4; 359 } 360 361 static void crt_fileno_locked(void) 362 { 363 struct stream *stream = find_stream(MEM32(g_esp + 4)); 364 g_eax = stream->descriptor >= 0 ? MEM32(stream->file + 16) : file_error(EBADF); 365 g_esp += 4; 366 } 367 368 static void print_file(uint32_t file, uint32_t format, uint32_t arguments) 369 { 370 struct stream *stream = find_stream(file); 371 uint32_t length; 372 char *text = guest_format_string(format, arguments, &length); 373 g_eax = length; 374 for (uint32_t i = 0; i < length; ++i) 375 if (put_character(stream, (unsigned char)text[i]) < 0) { g_eax = UINT32_MAX; break; } 376 free(text); 377 g_esp += 4; 378 } 379 380 static int64_t stream_position(struct stream *stream) 381 { 382 if (stream->descriptor < 0) return file_error(EBADF); 383 off_t position = lseek(stream->descriptor, 0, SEEK_CUR); 384 if (position < 0) return file_error(errno); 385 stream->text_eof = 0; /* MSVCRT's descriptor seek also clears its Control-Z state. */ 386 if (stream->lookahead >= 0) --position; 387 uint32_t file = stream->file, flags = MEM32(file + 12); 388 if (flags & 1) { 389 uint32_t unread = MEM32(file + 4); 390 if (!stream->text || !unread || (flags & 4)) position -= unread; 391 else { 392 struct stat info; 393 if (fstat(stream->descriptor, &info)) return file_error(errno); 394 uint32_t pointer = MEM32(file), base = MEM32(file + 8); 395 if (position == info.st_size) { 396 position -= unread + ((flags & 0x2000) ? 1 : 0); 397 for (uint32_t i = 0; i < unread; ++i) if (MEM8(pointer + i) == '\n') --position; 398 } else { 399 position -= stream->input_bytes + stream->input_lf; 400 position += pointer - base; 401 for (uint32_t p = base; p < pointer; ++p) if (MEM8(p) == '\n') ++position; 402 } 403 } 404 } else if ((flags & 2) && !(flags & 4)) { 405 uint32_t start = MEM32(file + 8), end = MEM32(file); 406 position += end - start; 407 if (stream->text) 408 for (uint32_t p = start; p < end; ++p) if (MEM8(p) == '\n') ++position; 409 } 410 return position; 411 } 412 413 static void crt_ftell_locked(void) 414 { 415 int64_t position = stream_position(find_stream(MEM32(g_esp + 4))); 416 g_eax = position > INT32_MAX ? file_error(EINVAL) : (uint32_t)position; 417 g_esp += 4; 418 } 419 420 static void seek_stream(struct stream *stream, int64_t offset, uint32_t origin) 421 { 422 uint32_t file = stream->file; 423 if (origin > 2) { g_eax = file_error(EINVAL); return; } 424 if (origin == SEEK_CUR) { 425 int64_t position = stream_position(stream); 426 if (position < 0) { g_eax = UINT32_MAX; return; } 427 offset += position; 428 origin = SEEK_SET; 429 } 430 MEM32(file + 12) &= ~0x10u; /* A seek clears EOF, including failed seeks. */ 431 if (flush_stream(stream)) g_eax = UINT32_MAX; 432 else { 433 if (MEM32(file + 12) & 0x80) MEM32(file + 12) &= ~3u; 434 else if ((MEM32(file + 12) & 9) == 9 && !(MEM32(file + 12) & 0x400)) 435 MEM32(file + 24) = 512; 436 g_eax = lseek(stream->descriptor, offset, origin) < 0 ? file_error(errno) : 0; 437 if (!g_eax) { 438 stream->text_eof = 0; 439 stream->lookahead = -1; 440 MEM32(file + 12) &= ~0x2000u; 441 } 442 } 443 } 444 445 static void crt_fseek_locked(void) 446 { 447 seek_stream(find_stream(MEM32(g_esp + 4)), (int32_t)MEM32(g_esp + 8), MEM32(g_esp + 12)); 448 g_esp += 4; 449 } 450 451 static void crt_fgetpos_locked(void) 452 { 453 int64_t position = stream_position(find_stream(MEM32(g_esp + 4))); 454 memcpy((void *)ADDR(MEM32(g_esp + 8)), &position, sizeof(position)); 455 g_eax = position == -1 ? UINT32_MAX : 0; 456 g_esp += 4; 457 } 458 459 static void crt_fsetpos_locked(void) 460 { 461 int64_t position; 462 memcpy(&position, (void *)ADDR(MEM32(g_esp + 8)), sizeof(position)); 463 seek_stream(find_stream(MEM32(g_esp + 4)), position, SEEK_SET); 464 g_esp += 4; 465 } 466 467 static int fill_input(struct stream *stream) 468 { 469 uint32_t file = stream->file, flags = MEM32(file + 12); 470 MEM32(file + 4) = 0; 471 if (stream->descriptor < 0 || !(flags & (1 | 0x80)) || (flags & 2)) { 472 MEM32(file + 12) |= 0x20; 473 return file_error(EBADF); 474 } 475 if (flags & 0x10) return -1; 476 MEM32(file + 12) |= 1; 477 if (!MEM32(file + 8)) { 478 uint32_t buffer = guest_malloc(4096); 479 if (!buffer) return file_error(ENOMEM); 480 MEM32(file + 8) = buffer; 481 MEM32(file + 24) = 4096; 482 MEM32(file + 12) |= 8; 483 } 484 MEM32(file) = MEM32(file + 8); 485 ssize_t count; 486 unsigned char *buffer = (void *)ADDR(MEM32(file)); 487 if (stream->text_eof) count = 0; 488 else if (stream->lookahead >= 0) { 489 buffer[0] = stream->lookahead; 490 stream->lookahead = -1; 491 count = 1; 492 } else { 493 double profile_start = ao_profile_begin("file read"); 494 do count = read(stream->descriptor, buffer, flags & 4 ? 2 : MEM32(file + 24)); 495 while (count < 0 && errno == EINTR); 496 ao_profile_end("file read", profile_start, NULL); 497 } 498 499 /* Text buffers expose translated bytes even to inline guest getc code. */ 500 if (stream->text && count > 0) { 501 stream->input_bytes = count; 502 stream->input_lf = buffer[0] == '\n'; 503 ssize_t translated = 0; 504 for (ssize_t i = 0; i < count; ++i) { 505 unsigned char character = buffer[i]; 506 if (character == 0x1a) { 507 MEM32(file + 12) |= 0x2000; 508 stream->text_eof = 1; 509 break; 510 } 511 if (character == '\r') { 512 if (i + 1 < count) { 513 if (buffer[i + 1] == '\n') { character = '\n'; ++i; } 514 } else { 515 unsigned char next; 516 ssize_t got; 517 do got = read(stream->descriptor, &next, 1); 518 while (got < 0 && errno == EINTR); 519 if (got < 0) { 520 MEM32(file + 12) |= 0x20; 521 return file_error(errno); 522 } 523 if (got) { 524 if (next == '\n') { 525 if (translated) { 526 /* Keep a split CRLF's LF for the next regular-file read. */ 527 if (lseek(stream->descriptor, -1, SEEK_CUR) < 0) stream->lookahead = next; 528 break; 529 } 530 character = '\n'; 531 ++stream->input_bytes; 532 } 533 else stream->lookahead = next; 534 } 535 } 536 } 537 buffer[translated++] = character; 538 } 539 count = translated; 540 } 541 if (count <= 0) { 542 MEM32(file + 12) |= count < 0 ? 0x20 : 0x10; 543 return count < 0 ? file_error(errno) : -1; 544 } 545 MEM32(file + 4) = count - 1; 546 int character = MEM8(MEM32(file)); 547 ++MEM32(file); 548 return character; 549 } 550 551 static void crt_filbuf_locked(void) 552 { 553 g_eax = fill_input(find_stream(MEM32(g_esp + 4))); 554 g_esp += 4; 555 } 556 557 static int get_character(struct stream *stream) 558 { 559 uint32_t file = stream->file; 560 if ((MEM32(file + 12) & 1) && (int32_t)MEM32(file + 4) > 0) { 561 --MEM32(file + 4); 562 int character = MEM8(MEM32(file)); 563 ++MEM32(file); 564 return character; 565 } 566 return fill_input(stream); 567 } 568 569 static void crt_fgetc_locked(void) 570 { 571 g_eax = get_character(find_stream(MEM32(g_esp + 4))); 572 g_esp += 4; 573 } 574 575 static void crt_ungetc_locked(void) 576 { 577 int character = MEM32(g_esp + 4); 578 struct stream *stream = find_stream(MEM32(g_esp + 8)); 579 uint32_t file = stream->file, flags = MEM32(file + 12); 580 g_eax = UINT32_MAX; 581 g_esp += 4; 582 if (character == EOF || !(flags & (1 | 0x80)) || (flags & 2)) return; 583 584 /* Guest C++ streams also inspect this buffer directly when putting back a byte. */ 585 if (!MEM32(file + 8)) { 586 uint32_t buffer = guest_malloc(4096); 587 if (!buffer) { file_error(ENOMEM); return; } 588 MEM32(file) = MEM32(file + 8) = buffer; 589 MEM32(file + 24) = 4096; 590 MEM32(file + 12) |= 8; 591 } 592 if (MEM32(file) == MEM32(file + 8)) { 593 if (MEM32(file + 4)) return; 594 ++MEM32(file); 595 } 596 --MEM32(file); 597 MEM8(MEM32(file)) = (unsigned char)character; 598 ++MEM32(file + 4); 599 MEM32(file + 12) = (MEM32(file + 12) & ~0x10u) | 1; 600 g_eax = (unsigned char)character; 601 } 602 603 static void crt_fgets_locked(void) 604 { 605 uint32_t output = MEM32(g_esp + 4); 606 int32_t size = MEM32(g_esp + 8); 607 g_eax = 0; 608 if (size > 0) { 609 struct stream *stream = find_stream(MEM32(g_esp + 12)); 610 int32_t copied = 0; 611 while (copied < size - 1) { 612 int character = get_character(stream); 613 if (character < 0) break; 614 MEM8(output + copied++) = character; 615 if (character == '\n') break; 616 } 617 if (copied || size == 1) { 618 MEM8(output + copied) = 0; 619 g_eax = output; 620 } 621 } 622 g_esp += 4; 623 } 624 625 static void crt_fscanf_locked(void) 626 { 627 struct stream *stream = find_stream(MEM32(g_esp + 4)); 628 const char *format = (void *)ADDR(MEM32(g_esp + 8)); 629 uint32_t argument = g_esp + 12, file = stream->file; 630 int assigned = 0, completed = 0; 631 while (*format) { 632 if (isspace((unsigned char)*format)) { 633 ++format; 634 int character; 635 do character = get_character(stream); 636 while (character >= 0 && isspace(character)); 637 if (character >= 0) { --MEM32(file); ++MEM32(file + 4); } 638 continue; 639 } 640 if (*format != '%' || format[1] == '%') { 641 if (*format == '%') ++format; 642 int character = get_character(stream); 643 if (character != (unsigned char)*format++) { 644 if (character >= 0) { --MEM32(file); ++MEM32(file + 4); } 645 else if (!completed) assigned = -1; 646 break; 647 } 648 continue; 649 } 650 651 /* String fields consume translated bytes, leaving the delimiter unread. */ 652 ++format; 653 int suppressed = *format == '*'; 654 if (suppressed) ++format; 655 uint32_t width = 0; 656 while (isdigit((unsigned char)*format)) { 657 if (width > (INT32_MAX - (*format - '0')) / 10) goto unsupported_scan; 658 width = width * 10 + (*format++ - '0'); 659 } 660 if (*format++ != 's') goto unsupported_scan; 661 if (!width) width = INT32_MAX; 662 uint32_t output = 0; 663 if (!suppressed) { output = MEM32(argument); argument += 4; } 664 int character; 665 do character = get_character(stream); 666 while (character >= 0 && isspace(character)); 667 if (character < 0) { if (!completed) assigned = -1; break; } 668 uint32_t copied = 0; 669 for (;;) { 670 if (!suppressed) MEM8(output + copied) = character; 671 if (++copied == width) break; 672 character = get_character(stream); 673 if (character < 0) break; 674 if (isspace(character)) { --MEM32(file); ++MEM32(file + 4); break; } 675 } 676 if (!suppressed) { MEM8(output + copied) = 0; ++assigned; } 677 ++completed; 678 } 679 g_eax = assigned; 680 g_esp += 4; 681 return; 682 unsupported_scan: 683 fprintf(stderr, "Unsupported guest fscanf format: %s\n", (char *)ADDR(MEM32(g_esp + 8))); 684 abort(); 685 } 686 687 static void crt_fread_locked(void) 688 { 689 uint32_t output = MEM32(g_esp + 4), size = MEM32(g_esp + 8), count = MEM32(g_esp + 12); 690 if (!size || !count) { g_eax = 0; g_esp += 4; return; } 691 uint64_t total = (uint64_t)size * count, copied = 0; 692 if (total > UINT32_MAX) { file_error(EINVAL); g_eax = 0; g_esp += 4; return; } 693 struct stream *stream = find_stream(MEM32(g_esp + 16)); 694 uint32_t file = stream->file; 695 if (!(MEM32(file + 12) & (1 | 0x80)) || (MEM32(file + 12) & 2)) { 696 fill_input(stream); 697 g_eax = 0; 698 g_esp += 4; 699 return; 700 } 701 while (copied < total) { 702 if ((int32_t)MEM32(file + 4) > 0) { 703 uint32_t available = MEM32(file + 4); 704 if (available > total - copied) available = total - copied; 705 memcpy((void *)ADDR(output + copied), (const void *)ADDR(MEM32(file)), available); 706 MEM32(file) += available; 707 MEM32(file + 4) -= available; 708 copied += available; 709 } else { 710 int character = fill_input(stream); 711 if (character < 0) break; 712 MEM8(output + copied++) = character; 713 } 714 } 715 g_eax = copied / size; 716 g_esp += 4; 717 } 718 719 static void crt_chmod(void) 720 { 721 const char *name = (const char *)ADDR(MEM32(g_esp + 4)); 722 uint32_t mode = MEM32(g_esp + 8); 723 if (strchr(name, ':') || !strncmp(name, "\\\\", 2)) { 724 fprintf(stderr, "Windows drive/UNC chmod paths are not implemented: %s\n", name); 725 abort(); 726 } 727 char *path = linux_path(name); 728 struct stat info; 729 if (stat(path, &info)) g_eax = file_error(errno); 730 else { 731 if (!S_ISREG(info.st_mode)) { 732 fprintf(stderr, "Guest chmod on non-regular files is not implemented: %s\n", name); 733 abort(); 734 } 735 /* MSVCRT controls the read-only attribute; preserve host read/execute 736 * permissions and restore owner write access when _S_IWRITE is set. */ 737 mode_t permissions = mode & 0x80 ? info.st_mode | S_IWUSR : info.st_mode & ~0222; 738 g_eax = chmod(path, permissions) ? file_error(errno) : 0; 739 } 740 free(path); 741 g_esp += 4; 742 } 743 744 static void crt_stat(void) 745 { 746 const char *name = (const char *)ADDR(MEM32(g_esp + 4)); 747 uint32_t output = MEM32(g_esp + 8); 748 if (strchr(name, ':') || !strncmp(name, "\\\\", 2)) { 749 fprintf(stderr, "Windows drive/UNC stat paths are not implemented: %s\n", name); 750 abort(); 751 } 752 size_t length = strlen(name); 753 /* The legacy CRT rejects trailing separators and wildcard queries. */ 754 if (!length || name[length - 1] == '/' || name[length - 1] == '\\' || strpbrk(name, "*?")) { 755 g_eax = file_error(ENOENT); 756 g_esp += 4; 757 return; 758 } 759 char *path = linux_path(name); 760 struct statx info; 761 if (statx(AT_FDCWD, path, 0, STATX_BASIC_STATS | STATX_BTIME, &info)) { 762 g_eax = file_error(errno); 763 free(path); 764 g_esp += 4; 765 return; 766 } 767 768 uint16_t mode = S_ISDIR(info.stx_mode) ? 0x4000 : 0x8000; 769 if (!S_ISDIR(info.stx_mode) && !S_ISREG(info.stx_mode)) { 770 fprintf(stderr, "Guest stat on special files is not implemented: %s\n", name); 771 abort(); 772 } 773 mode |= 0444; 774 if (info.stx_mode & 0222) mode |= 0222; 775 const char *extension = strrchr(path, '.'); 776 if (S_ISDIR(info.stx_mode) || (extension && 777 (!strcasecmp(extension, ".exe") || !strcasecmp(extension, ".com") || 778 !strcasecmp(extension, ".bat") || !strcasecmp(extension, ".cmd")))) mode |= 0111; 779 780 /* VC6 _stat is 36 bytes, with two untouched padding bytes at offset 14. 781 * Device zero identifies the portable filesystem; host inode/UID widths 782 * never enter this Windows structure. The legacy size field wraps at 32 bits. */ 783 MEM32(output) = MEM32(output + 16) = 0; 784 MEM16(output + 4) = MEM16(output + 10) = MEM16(output + 12) = 0; 785 MEM16(output + 6) = mode; 786 MEM16(output + 8) = 1; 787 MEM32(output + 20) = S_ISDIR(info.stx_mode) ? 0 : (uint32_t)info.stx_size; 788 int64_t times[] = {info.stx_atime.tv_sec, info.stx_mtime.tv_sec, 789 info.stx_mask & STATX_BTIME ? info.stx_btime.tv_sec : info.stx_mtime.tv_sec}; 790 for (unsigned i = 0; i < 3; ++i) { 791 if (times[i] < 0 || times[i] > INT32_MAX) { 792 MEM32(output + 24 + i * 4) = UINT32_MAX; 793 file_error(EINVAL); 794 } else MEM32(output + 24 + i * 4) = (uint32_t)times[i]; 795 } 796 free(path); 797 g_eax = 0; 798 g_esp += 4; 799 } 800 801 static void crt_fprintf_locked(void) { print_file(MEM32(g_esp + 4), MEM32(g_esp + 8), g_esp + 12); } 802 static void crt_vfprintf_locked(void) { print_file(MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 12)); } 803 static void crt_printf_locked(void) { print_file(guest_crt_data("_iob") + 32, MEM32(g_esp + 4), g_esp + 8); } 804 805 /* Only operations on the same FILE wait for its I/O and buffered position. */ 806 static void call_stream(recomp_func_t function, uint32_t file) 807 { 808 struct stream *stream = find_stream(file); 809 if (pthread_mutex_lock(&stream->mutex)) abort(); 810 function(); 811 if (pthread_mutex_unlock(&stream->mutex)) abort(); 812 } 813 814 static void crt_freopen(void) { call_stream(crt_freopen_locked, MEM32(g_esp + 12)); } 815 static void crt_fclose(void) { call_stream(crt_fclose_locked, MEM32(g_esp + 4)); } 816 static void crt_fflush(void) 817 { 818 if (MEM32(g_esp + 4)) call_stream(crt_fflush_locked, MEM32(g_esp + 4)); 819 else { g_eax = flush_all(); g_esp += 4; } 820 } 821 static void crt_fputc(void) { call_stream(crt_fputc_locked, MEM32(g_esp + 8)); } 822 static void crt_fputs(void) { call_stream(crt_fputs_locked, MEM32(g_esp + 8)); } 823 static void crt_setvbuf(void) { call_stream(crt_setvbuf_locked, MEM32(g_esp + 4)); } 824 static void crt_fileno(void) { call_stream(crt_fileno_locked, MEM32(g_esp + 4)); } 825 static void crt_fprintf(void) { call_stream(crt_fprintf_locked, MEM32(g_esp + 4)); } 826 static void crt_vfprintf(void) { call_stream(crt_vfprintf_locked, MEM32(g_esp + 4)); } 827 static void crt_printf(void) { call_stream(crt_printf_locked, guest_crt_data("_iob") + 32); } 828 static void crt_ftell(void) { call_stream(crt_ftell_locked, MEM32(g_esp + 4)); } 829 static void crt_fseek(void) { call_stream(crt_fseek_locked, MEM32(g_esp + 4)); } 830 static void crt_fgetpos(void) { call_stream(crt_fgetpos_locked, MEM32(g_esp + 4)); } 831 static void crt_fsetpos(void) { call_stream(crt_fsetpos_locked, MEM32(g_esp + 4)); } 832 static void crt_fgetc(void) { call_stream(crt_fgetc_locked, MEM32(g_esp + 4)); } 833 static void crt_ungetc(void) { call_stream(crt_ungetc_locked, MEM32(g_esp + 8)); } 834 static void crt_fscanf(void) { call_stream(crt_fscanf_locked, MEM32(g_esp + 4)); } 835 static void crt_filbuf(void) { call_stream(crt_filbuf_locked, MEM32(g_esp + 4)); } 836 837 /* Empty/invalid-size operations return without looking up a FILE, as in MSVCRT. */ 838 static void crt_fread(void) 839 { 840 uint64_t size = MEM32(g_esp + 8), count = MEM32(g_esp + 12); 841 if (!size || !count || size * count > UINT32_MAX) crt_fread_locked(); 842 else call_stream(crt_fread_locked, MEM32(g_esp + 16)); 843 } 844 static void crt_fwrite(void) 845 { 846 uint64_t size = MEM32(g_esp + 8), count = MEM32(g_esp + 12); 847 if (!size || !count || size * count > UINT32_MAX) crt_fwrite_locked(); 848 else call_stream(crt_fwrite_locked, MEM32(g_esp + 16)); 849 } 850 static void crt_fgets(void) 851 { 852 if ((int32_t)MEM32(g_esp + 8) <= 0) crt_fgets_locked(); 853 else call_stream(crt_fgets_locked, MEM32(g_esp + 12)); 854 } 855 856 recomp_func_t guest_stdio_lookup(const char *name) 857 { 858 static const struct { const char *name; recomp_func_t function; } functions[] = { 859 {"fopen", crt_fopen}, {"freopen", crt_freopen}, {"fclose", crt_fclose}, 860 {"fflush", crt_fflush}, {"fputc", crt_fputc}, {"fwrite", crt_fwrite}, 861 {"fputs", crt_fputs}, {"setvbuf", crt_setvbuf}, {"_fileno", crt_fileno}, 862 {"fprintf", crt_fprintf}, {"vfprintf", crt_vfprintf}, 863 {"printf", crt_printf}, 864 {"_chmod", crt_chmod}, {"_stat", crt_stat}, 865 {"ftell", crt_ftell}, {"fseek", crt_fseek}, {"fread", crt_fread}, 866 {"fgetpos", crt_fgetpos}, {"fsetpos", crt_fsetpos}, 867 {"fgetc", crt_fgetc}, {"getc", crt_fgetc}, {"_filbuf", crt_filbuf}, 868 {"ungetc", crt_ungetc}, 869 {"fgets", crt_fgets}, 870 {"fscanf", crt_fscanf}, 871 }; 872 for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i) 873 if (!strcmp(name, functions[i].name)) return functions[i].function; 874 return NULL; 875 }