Files
955f274388 Crash handling: a crash can no longer hang the game; guest threads exit on the host stack (#183)
* Crash handling: a crash can no longer hang the game; guest threads exit on the host stack

On a OnePlus 13 the game froze and Android reported "not responding" (ANR at 10:10:25; the same crash in an earlier
session on an older build):
1. A guest thread called ExitThread. thread_exit ran pthread_exit on the guest's own stack (dm_thread_enter switched
   to it; some are a dozen or so KiB), and pthread_exit's TLS destructors (je_tsd_cleanup -> tcache flush -> arena
   purge -> chunk_recycle) overflowed it: SIGSEGV with fault address == sp in je_extent_tree_szsnad_remove, while
   holding a jemalloc arena lock.
2. on_crash then ran the thread dump; the Android thread_info shim read /proc/<tid>/stat with fopen / fread, fread
   allocated a stdio buffer, and malloc waited on the arena lock the crashing thread itself held. Every other thread
   needing memory blocked behind it (the UI thread drawing a bitmap), so after 5 s the system showed "not responding"
   instead of the process dying with a crash record.

Changes:
- hle_thread.c: thread_main records a sigjmp_buf on its own (1 MiB host) stack before entering the guest stack;
  thread_exit does its bookkeeping and siglongjmps back there, and pthread_exit runs on the host stack. The jump point
  is found through a pthread key, not thr_t, because thread_exit may free t. A thread without one (the process main
  thread) exits in place as before. The gate never swaps x18, so the jump restores plain host state.
- crash.c on_crash: the first part (CRASH line, registers, frame-pointer backtrace) is now signal-safe: write(2) from a
  stack buffer, guest modules through module_of (a static table), other addresses through /proc/self/maps on Linux /
  Android (library + offset, as the dladdr fallback printed) -- no stdio, malloc, dladdr or locks. The second part
  (dladdr symbols, the symbolised backtrace and the thread dump) runs under a 3-second alarm (crash_deadman) that
  exits the process if it gets stuck on a lock the crashing thread holds, before the 5-second ANR.
- android/compat/mach_thread.c thread_info: reads another thread's stat with open / read and a hand parser instead of
  fopen / fread / sscanf, so the dump no longer allocates there at all.

Tests: dm/host/tests/t_crash_hang.c (added to the host test Makefile) forks a child per case, installs the real crash
handler and crashes: dump returns (exits at once), dump blocks forever, and the crashing thread holds a mutex the dump
then takes (the phone's shape). Built for arm64 with the NDK statically (-DDM_STATIC_TEST) and run on a PC through
qemu: all three pass (the stuck ones exit at 3.0 s with 139); against main's crash.c the two stuck cases hang until
killed after 8 s. thread_info on another thread checked the same way (1.00 s CPU for a thread spinning 1 s). The
debug APK builds. Not yet run on a phone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Threads: log when a guest thread exits through the host-stack path (first 20, then every 50th)

A smoke test on a Redmi K50 Ultra (Blood Moor, Den of Evil, three autosaves) never saw a guest thread exit, so the new
exit path ran unobserved. The line is printed after the jump back, on the 1 MiB host stack, where printf is safe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Yuechuan Yang <22333122+yyan160@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 19:10:00 +08:00
..