From fdbb118dfa5f7d8bf8afc5b1e9405808f56f0b53 Mon Sep 17 00:00:00 2001 From: kaisa847 <226997300+kaisa847@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:05:13 +0100 Subject: [PATCH] libcglue: reset __cleanup for newlib-nano too __locks_init() clears _REENT_CLEANUP(_REENT), so that __sinit() sets up stdio again after a restart via ExecPS2(). There .data, and with it _impure_data, survives, while the FILE structs and their locks in .bss are cleared. libcglue is built once against the full newlib headers, but it is also linked with libc_nano, which is built with _REENT_SMALL. There struct _reent has a different layout: __cleanup is at offset 32, not 52. So with newlib-nano the reset missed __cleanup (and cleared _localtime_buf instead), __sinit() skipped the setup after a restart, and the first printf() took a NULL lock (TLB miss, BadVAddr 8). Also clear the __cleanup slot of the _REENT_SMALL layout. In the full layout, that slot lies inside the _emergency scratch buffer, which a static assertion checks, so clearing it there is harmless. Tested on real hardware (PSX DESR-5100, EE program started via ps2link): the program calls printf() (which sets __cleanup), then __locks_init() as on a restart, and reads the __cleanup slot back. - libc_nano, unpatched libcglue: __cleanup stays 0x001024b8 (cleanup_stdio). - libc_nano, patched libcglue: __cleanup is NULL. - full libc: NULL with and without this patch. Co-Authored-By: Claude Opus 5.5 --- ee/libcglue/src/lock.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/ee/libcglue/src/lock.c b/ee/libcglue/src/lock.c index d683eefdb948..2ca5e11327f9 100644 --- a/ee/libcglue/src/lock.c +++ b/ee/libcglue/src/lock.c @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include #include #include @@ -228,6 +230,36 @@ extern struct __lock __lock___tz_mutex; extern struct __lock __lock___dd_hash_mutex; extern struct __lock __lock___arc4random_mutex; +/* libcglue is built once, against the full newlib headers, but it is also + * linked with newlib-nano (libc_nano). That one is built with _REENT_SMALL, + * where struct _reent has a different layout and __cleanup sits at a smaller + * offset. This mirrors the start of the _REENT_SMALL layout from + * , up to __cleanup. */ +struct __reent_small_head { + int _errno; + void *_stdin, *_stdout, *_stderr; + int _inc; + void *_emergency; +#ifdef _REENT_BACKWARD_BINARY_COMPAT + int _reserved_0; + int _reserved_1; +#endif + void *_locale; + void *_mp; + void (*__cleanup)(struct _reent *); +}; + +#define __REENT_SMALL_CLEANUP_OFFSET offsetof(struct __reent_small_head, __cleanup) + +/* In the full layout, that slot lies inside the _emergency scratch buffer, + * so clearing it there does no harm. In the small layout, the full-layout + * __cleanup slot is _localtime_buf, a lazily allocated pointer that is NULL + * at a first start and points into the old heap after a restart. */ +_Static_assert(__REENT_SMALL_CLEANUP_OFFSET >= offsetof(struct _reent, _emergency) && + __REENT_SMALL_CLEANUP_OFFSET + sizeof(void *) <= + offsetof(struct _reent, _emergency) + sizeof(((struct _reent *)0)->_emergency), + "the _REENT_SMALL __cleanup slot must lie inside _emergency of the full layout"); + void __locks_init() { /* Reset stdio initialization state so __sinit will reinitialize @@ -236,6 +268,8 @@ void __locks_init() * ExecPS2), while FILE structs and locks live in .bss (zeroed). * Without this, __sinit skips reinitialization and uses NULL locks. */ _REENT_CLEANUP(_REENT) = NULL; + /* The same for newlib-nano, see struct __reent_small_head above. */ + memset((char *)_REENT + __REENT_SMALL_CLEANUP_OFFSET, 0, sizeof(void *)); _LOCK_T lock_malloc = &__lock___malloc_recursive_mutex; _LOCK_T lock_atexit = &__lock___atexit_recursive_mutex;