commit 50363f0c63ffd62312fd363e386af71e238ab35a
parent 95bac150ca7c9b70090d6bb58e47adef1a792e67
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 12:50:39 +0000
Preserve legacy text and 64-bit saved file positions
Diffstat:
2 files changed, 135 insertions(+), 15 deletions(-)
diff --git a/decompiled/runtime/guest_stdio.c b/decompiled/runtime/guest_stdio.c
@@ -13,7 +13,10 @@
#include <unistd.h>
/* FILE pointers and buffers are guest addresses. Native descriptors stay here. */
-struct stream { uint32_t file; int descriptor, text, text_eof, lookahead; };
+struct stream {
+ uint32_t file, input_bytes;
+ int descriptor, text, text_eof, lookahead, input_lf;
+};
static struct stream streams[512];
static unsigned stream_count;
@@ -322,14 +325,25 @@ static int64_t stream_position(struct stream *stream)
if (stream->descriptor < 0) return file_error(EBADF);
off_t position = lseek(stream->descriptor, 0, SEEK_CUR);
if (position < 0) return file_error(errno);
+ stream->text_eof = 0; /* MSVCRT's descriptor seek also clears its Control-Z state. */
if (stream->lookahead >= 0) --position;
uint32_t file = stream->file, flags = MEM32(file + 12);
if (flags & 1) {
- if (stream->text && MEM32(file + 4)) {
- fprintf(stderr, "Positioning buffered guest text input is not implemented\n");
- abort();
+ uint32_t unread = MEM32(file + 4);
+ if (!stream->text || !unread || (flags & 4)) position -= unread;
+ else {
+ struct stat info;
+ if (fstat(stream->descriptor, &info)) return file_error(errno);
+ uint32_t pointer = MEM32(file), base = MEM32(file + 8);
+ if (position == info.st_size) {
+ position -= unread + ((flags & 0x2000) ? 1 : 0);
+ for (uint32_t i = 0; i < unread; ++i) if (MEM8(pointer + i) == '\n') --position;
+ } else {
+ position -= stream->input_bytes + stream->input_lf;
+ position += pointer - base;
+ for (uint32_t p = base; p < pointer; ++p) if (MEM8(p) == '\n') ++position;
+ }
}
- position -= MEM32(file + 4);
} else if ((flags & 2) && !(flags & 4)) {
uint32_t start = MEM32(file + 8), end = MEM32(file);
position += end - start;
@@ -346,15 +360,13 @@ static void crt_ftell_locked(void)
g_esp += 4;
}
-static void crt_fseek_locked(void)
+static void seek_stream(struct stream *stream, int64_t offset, uint32_t origin)
{
- struct stream *stream = find_stream(MEM32(g_esp + 4));
- int64_t offset = (int32_t)MEM32(g_esp + 8);
- uint32_t origin = MEM32(g_esp + 12), file = stream->file;
- if (origin > 2) { g_eax = file_error(EINVAL); g_esp += 4; return; }
+ uint32_t file = stream->file;
+ if (origin > 2) { g_eax = file_error(EINVAL); return; }
if (origin == SEEK_CUR) {
int64_t position = stream_position(stream);
- if (position < 0) { g_eax = UINT32_MAX; g_esp += 4; return; }
+ if (position < 0) { g_eax = UINT32_MAX; return; }
offset += position;
origin = SEEK_SET;
}
@@ -368,8 +380,30 @@ static void crt_fseek_locked(void)
if (!g_eax) {
stream->text_eof = 0;
stream->lookahead = -1;
+ MEM32(file + 12) &= ~0x2000u;
}
}
+}
+
+static void crt_fseek_locked(void)
+{
+ seek_stream(find_stream(MEM32(g_esp + 4)), (int32_t)MEM32(g_esp + 8), MEM32(g_esp + 12));
+ g_esp += 4;
+}
+
+static void crt_fgetpos_locked(void)
+{
+ int64_t position = stream_position(find_stream(MEM32(g_esp + 4)));
+ memcpy((void *)ADDR(MEM32(g_esp + 8)), &position, sizeof(position));
+ g_eax = position == -1 ? UINT32_MAX : 0;
+ g_esp += 4;
+}
+
+static void crt_fsetpos_locked(void)
+{
+ int64_t position;
+ memcpy(&position, (void *)ADDR(MEM32(g_esp + 8)), sizeof(position));
+ seek_stream(find_stream(MEM32(g_esp + 4)), position, SEEK_SET);
g_esp += 4;
}
@@ -399,16 +433,22 @@ static int fill_input(struct stream *stream)
stream->lookahead = -1;
count = 1;
} else {
- do count = read(stream->descriptor, buffer, flags & 4 ? 1 : MEM32(file + 24));
+ do count = read(stream->descriptor, buffer, flags & 4 ? 2 : MEM32(file + 24));
while (count < 0 && errno == EINTR);
}
/* Text buffers expose translated bytes even to inline guest getc code. */
if (stream->text && count > 0) {
+ stream->input_bytes = count;
+ stream->input_lf = buffer[0] == '\n';
ssize_t translated = 0;
for (ssize_t i = 0; i < count; ++i) {
unsigned char character = buffer[i];
- if (character == 0x1a) { stream->text_eof = 1; break; }
+ if (character == 0x1a) {
+ MEM32(file + 12) |= 0x2000;
+ stream->text_eof = 1;
+ break;
+ }
if (character == '\r') {
if (i + 1 < count) {
if (buffer[i + 1] == '\n') { character = '\n'; ++i; }
@@ -422,7 +462,15 @@ static int fill_input(struct stream *stream)
return file_error(errno);
}
if (got) {
- if (next == '\n') character = '\n';
+ if (next == '\n') {
+ if (translated) {
+ /* Keep a split CRLF's LF for the next regular-file read. */
+ if (lseek(stream->descriptor, -1, SEEK_CUR) < 0) stream->lookahead = next;
+ break;
+ }
+ character = '\n';
+ ++stream->input_bytes;
+ }
else stream->lookahead = next;
}
}
@@ -696,6 +744,8 @@ static void crt_chmod(void) { call_serialized(crt_chmod_locked); }
static void crt_stat(void) { call_serialized(crt_stat_locked); }
static void crt_ftell(void) { call_serialized(crt_ftell_locked); }
static void crt_fseek(void) { call_serialized(crt_fseek_locked); }
+static void crt_fgetpos(void) { call_serialized(crt_fgetpos_locked); }
+static void crt_fsetpos(void) { call_serialized(crt_fsetpos_locked); }
static void crt_fread(void) { call_serialized(crt_fread_locked); }
static void crt_fgetc(void) { call_serialized(crt_fgetc_locked); }
static void crt_fgets(void) { call_serialized(crt_fgets_locked); }
@@ -712,6 +762,7 @@ recomp_func_t guest_stdio_lookup(const char *name)
{"printf", crt_printf},
{"_chmod", crt_chmod}, {"_stat", crt_stat},
{"ftell", crt_ftell}, {"fseek", crt_fseek}, {"fread", crt_fread},
+ {"fgetpos", crt_fgetpos}, {"fsetpos", crt_fsetpos},
{"fgetc", crt_fgetc}, {"getc", crt_fgetc}, {"_filbuf", crt_filbuf},
{"fgets", crt_fgets},
{"fscanf", crt_fscanf},
diff --git a/decompiled/tests/stdio_input.c b/decompiled/tests/stdio_input.c
@@ -243,6 +243,11 @@ static void line_input_translation_and_boundaries(void)
uint32_t result = call("fgets", 3, line);
assert(result == bytes + 1);
+ if (memcmp(memory(bytes + 1), expected[i], lengths[i] + 1)) {
+ fprintf(stderr, "Line mismatch: buffering=%u line=%u\n", buffering, i);
+ for (unsigned j = 0; j < 16; ++j) fprintf(stderr, "%02x ", *(unsigned char *)memory(bytes + 1 + j));
+ fputc('\n', stderr);
+ }
assert(!memcmp(memory(bytes + 1), expected[i], lengths[i] + 1));
assert(*(unsigned char *)memory(bytes) == 0xa5);
assert(*(unsigned char *)memory(bytes + lengths[i] + 2) == 0xa5);
@@ -342,6 +347,68 @@ static void scanned_strings_preserve_delimiters(void)
assert(!call("fclose", 1, &file) && !remove("scan.txt"));
}
+static void text_positions_match_legacy_buffer_rules(void)
+{
+ const char *input[] = {"a\r\nb\r\nc", "a\nb\nc", "a\rb\r", "abc\x1axyz"};
+ const int positions[][2][8] = {
+ {{1, 3, 4, 6, 7, 7}, {2, 3, 4, 6, 7, 7}},
+ {{-1, 1, 2, 4, 5, 5}, {1, 2, 3, 4, 5, 5}},
+ {{1, 2, 3, 4, 4}, {1, 2, 3, 4, 4}},
+ {{4, 5, 7, 7}, {1, 2, 4, 5, 6, 7, 7}}
+ };
+ for (unsigned i = 0; i < 4; ++i) for (unsigned buffering = 0; buffering < 3; ++buffering) {
+ FILE *native = fopen("positions.txt", "wb");
+ assert(native && fwrite(input[i], 1, strlen(input[i]), native) == strlen(input[i]) && !fclose(native));
+ uint32_t file = open_file("positions.txt", "rt");
+ uint32_t set[] = {file, 0, buffering == 2 ? 4 : 0, 2};
+ if (buffering) assert(!call("setvbuf", 4, set));
+ uint32_t storage[] = {0xa5a5a5a5, 0, 0, 0xa5a5a5a5};
+ uint32_t bytes = address(storage, sizeof(storage));
+ uint32_t getpos[] = {file, bytes + 4};
+
+ for (unsigned j = 0; j < 8; ++j) {
+ uint32_t character = call("fgetc", 1, &file);
+ uint32_t result = call("fgetpos", 2, getpos);
+ int64_t position;
+ memcpy(&position, memory(bytes + 4), sizeof(position));
+
+ int expected = positions[i][buffering != 0][j];
+ assert(position == expected && result == (expected == -1 ? UINT32_MAX : 0));
+ assert((int32_t)call("ftell", 1, &file) == expected);
+ assert(*(uint32_t *)memory(bytes) == 0xa5a5a5a5 && *(uint32_t *)memory(bytes + 12) == 0xa5a5a5a5);
+ if (character == UINT32_MAX) break;
+ }
+ assert(!call("fclose", 1, &file));
+ }
+ assert(!remove("positions.txt"));
+}
+
+static void saved_positions_restore_text_and_support_64_bits(void)
+{
+ FILE *native = fopen("positions.txt", "wb");
+ assert(native && fwrite("a\r\nb\r\nc", 1, 7, native) == 7 && !fclose(native));
+ uint32_t file = open_file("positions.txt", "rt");
+ uint32_t storage[4] = {0};
+ uint32_t bytes = address(storage, sizeof(storage));
+ uint32_t position[] = {file, bytes};
+
+ assert(call("fgetc", 1, &file) == 'a' && !call("fgetpos", 2, position));
+ while (call("fgetc", 1, &file) != UINT32_MAX) {}
+ assert(!call("fsetpos", 2, position));
+ uint32_t next = call("fgetc", 1, &file);
+
+ assert(next == '\n' && call("fgetc", 1, &file) == 'b');
+ assert(!call("fclose", 1, &file));
+ file = open_file("positions.txt", "rb");
+ position[0] = file;
+ uint64_t large = 0x100000003ull;
+ memcpy(memory(bytes), &large, sizeof(large));
+ assert(!call("fsetpos", 2, position));
+ position[1] = bytes + 8;
+ assert(!call("fgetpos", 2, position) && !memcmp(memory(bytes + 8), &large, sizeof(large)));
+ assert(!call("fclose", 1, &file) && !remove("positions.txt"));
+}
+
int main(void)
{
#ifdef _WIN32
@@ -369,13 +436,15 @@ int main(void)
line_input_capacity_and_binary_bytes();
text_reads_cross_buffer_boundaries();
scanned_strings_preserve_delimiters();
+ text_positions_match_legacy_buffer_rules();
+ saved_positions_restore_text_and_support_64_bits();
assert(!remove("input.bin") && !remove("update.bin") && !remove("text.txt") && !remove("write-only.bin"));
#ifdef _WIN32
assert(SetCurrentDirectoryA(temporary) && RemoveDirectoryA(directory));
#else
assert(!chdir("/tmp") && !rmdir(directory));
#endif
- puts("stdio input: positions, binary reads, direct buffers, partial elements, EOF, update streams, text output, fgets/CRLF/control-Z, fscanf strings and ABI passed");
+ puts("stdio input: 64-bit/text positions, binary reads, direct buffers, partial elements, EOF, update streams, text output, fgets/CRLF/control-Z, fscanf strings and ABI passed");
return 0;
}