foxygit / fake-08-xbox360 Log in
commit 64a80200c9d98c14af806c96c92b9508bb0da1fa
Author:     mrfox <jens.se@icloud.com>
AuthorDate: Wed Nov 25 17:07:42 2026 +0100
Commit:     mrfox <jens.se@icloud.com>
CommitDate: Wed Nov 25 17:07:42 2026 +0100

    xex: fix directory listing on FAT32 USB sticks; fault test

    The USB stick's FAT32 driver returns one entry per NtQueryDirectoryFile
    and leaves NextEntryOffset unwritten (found by xsdk), so readdir followed
    whatever the heap held: FAKE-08 crashed after the first cart. The buffer
    is cleared before each query and offsets are bounds checked.

    The fault handler now passes on faults outside the title's code and ends
    the title straight after writing crash.txt; tests/fault checks it.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 platform/xex/Makefile             |  2 +-
 platform/xex/README.md            |  1 +
 platform/xex/runtime/fault.c      |  7 +++++--
 platform/xex/runtime/fs.c         | 12 +++++++++++-
 platform/xex/tests/fault/main.cpp | 24 ++++++++++++++++++++++++
 platform/xex/tests/fault/xex.toml |  3 +++
 6 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/platform/xex/Makefile b/platform/xex/Makefile
index f56985f..fef4636 100644
--- a/platform/xex/Makefile
+++ b/platform/xex/Makefile
@@ -29,7 +29,7 @@ RUNTIME_SRC := runtime/crt0.S runtime/crt.c runtime/sys.c runtime/fs.c runtime/p
                runtime/extra_imports.S sdk/thunks/ppc32.S
 RUNTIME_OBJ := $(patsubst %,$(BUILD)/%.o,$(RUNTIME_SRC))

-TESTS := hello fs video
+TESTS := hello fs video fault

 #---------------------------------------------------------------------------------
 # FAKE-08: the emulator core (../../source, ../../libs) and source/XexHost.cpp
diff --git a/platform/xex/README.md b/platform/xex/README.md
index c5d773c..65eb79e 100644
--- a/platform/xex/README.md
+++ b/platform/xex/README.md
@@ -107,5 +107,6 @@ The source is `source/XexHost.cpp`, the same logic as the libxenon host
 | Test | Checks |
 |---|---|
 | `tests/hello` | static constructors, C++ exceptions, `std::regex`, an 896 KB deep stack (progress in `game:/stack-test.txt`), notifications, exit to dashboard |
+| `tests/fault` | stores to address 0 after 2 s: the fault report must land in `game:/crash.txt` and the console go back to the dashboard |
 | `tests/fs` | time, `clock()`, directory listing, write/append/read/seek/stat/mkdir/remove on every device; writes `game:/fs-test-report.txt` |
 | `tests/video` | CPU-drawn frames at 60 fps through the title's own GPU ring, controller input (d-pad moves a square, A/B/X/Y colour it); a row of 16 boxes lights up with the pressed buttons and every controller change is logged to `game:/video-log.txt`; exits after 60 s (not while the Guide shows) or on Back+Start; the Xbox button opens the Guide, whose Xbox Home leaves |
diff --git a/platform/xex/runtime/fault.c b/platform/xex/runtime/fault.c
index c4f12f9..1931881 100644
--- a/platform/xex/runtime/fault.c
+++ b/platform/xex/runtime/fault.c
@@ -3,6 +3,7 @@
  * instruction, ...) writes game:/crash.txt (and DbgPrint) with the
  * exception code, the faulting instruction, the registers and the return
  * addresses along the stack, then ends the title back to the dashboard.
+ * Only faults in the title's own code are reported; others are passed on.
  * Look the addresses up with `xenon-addr2line -f -C -e app.elf <addr>`.
  *
  * How the kernel hands a title its faults was found on hardware by xsdk
@@ -101,6 +102,10 @@ static long fault_handler(exception_pointers *pointers)

     exception_record *rec = pointers->record;
     const uint8_t *ctx = pointers->context;
+    if (rec->address < CODE_START || rec->address >= CODE_END) {
+        inside = 0;
+        return 0;   /* EXCEPTION_CONTINUE_SEARCH: not in the title's code */
+    }

     report_fd = open("game:/crash.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
     out("FAKE-08 XEX crash: %s (code %08x) at %08x\n", code_name(rec->code), (unsigned)rec->code,
@@ -128,8 +133,6 @@ static long fault_handler(exception_pointers *pointers)
     }
     if (report_fd >= 0) close(report_fd);

-    xex_notify("FAKE-08 crashed: see crash.txt");
-    xex_sleep_ms(3000);
     XamLoaderTerminateTitle();
 }

diff --git a/platform/xex/runtime/fs.c b/platform/xex/runtime/fs.c
index b21b1e0..8fdd8b0 100644
--- a/platform/xex/runtime/fs.c
+++ b/platform/xex/runtime/fs.c
@@ -542,6 +542,11 @@ struct dirent *readdir(DIR *dir)
         return NULL;
     }
     if (dir->offset == ~0u) {
+        /* A FAT32 USB stick's driver returns one entry per call and leaves
+         * NextEntryOffset (and FileIndex) unwritten, so whatever the buffer
+         * held stays there: clear it, so a missing offset reads 0 (found by
+         * xsdk, docs/HARDWARE.md "Listing a folder on a FAT32 USB stick"). */
+        memset(dir->buffer, 0, sizeof(dir->buffer));
         IO_STATUS_BLOCK iosb;
         NTSTATUS status = NtQueryDirectoryFile(dir->handle, NULL, NULL, NULL, &iosb,
                                                dir->buffer, sizeof(dir->buffer), NULL,
@@ -553,6 +558,10 @@ struct dirent *readdir(DIR *dir)
         dir->offset = 0;
     }

+    if (dir->offset + sizeof(FILE_DIRECTORY_INFORMATION) > sizeof(dir->buffer)) {
+        dir->offset = ~0u;
+        return readdir(dir);
+    }
     FILE_DIRECTORY_INFORMATION *info = (FILE_DIRECTORY_INFORMATION *)(dir->buffer + dir->offset);
     ULONG len = info->FileNameLength;
     if (len > sizeof(dir->entry.d_name) - 1) len = sizeof(dir->entry.d_name) - 1;
@@ -561,7 +570,8 @@ struct dirent *readdir(DIR *dir)
     dir->entry.d_type = (info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? DT_DIR : DT_REG;
     dir->entry.d_ino = 0;

-    dir->offset = info->NextEntryOffset ? dir->offset + info->NextEntryOffset : ~0u;
+    ULONG next = info->NextEntryOffset;
+    dir->offset = (next && dir->offset + next < sizeof(dir->buffer)) ? dir->offset + next : ~0u;
     return &dir->entry;
 }

diff --git a/platform/xex/tests/fault/main.cpp b/platform/xex/tests/fault/main.cpp
new file mode 100644
index 0000000..c88ef9e
--- /dev/null
+++ b/platform/xex/tests/fault/main.cpp
@@ -0,0 +1,24 @@
+// Runtime check 4: the fault report. Stores to address 0 after 2 s; the
+// runtime's handler should write game:/crash.txt (exception, address,
+// registers, stack) and go back to the dashboard.
+#include <cstdio>
+#include "xex_runtime.h"
+
+static void __attribute__((noinline)) crashHere(volatile int *p) {
+    *p = 42;
+}
+
+static void __attribute__((noinline)) caller(volatile int *p) {
+    crashHere(p);
+    printf("fault: still alive after the store\n");
+}
+
+int main(int, char **) {
+    remove("game:/crash.txt");
+    xex_notify("fault test: crashing in 2 s");
+    xex_sleep_ms(2000);
+    caller((volatile int *)nullptr);
+    xex_notify("fault test: NOT caught");
+    xex_sleep_ms(4000);
+    return 0;
+}
diff --git a/platform/xex/tests/fault/xex.toml b/platform/xex/tests/fault/xex.toml
new file mode 100644
index 0000000..0854722
--- /dev/null
+++ b/platform/xex/tests/fault/xex.toml
@@ -0,0 +1,3 @@
+title_id = 0x46383038   # "F808"
+load_address = 0x82000000
+module_flags = ["TITLE"]