Repository navigation
Conversation
__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 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
__locks_init()clears_REENT_CLEANUP(_REENT), so that__sinit()sets up stdio again after a restart viaExecPS2(). On such a restart.datasurvives, and with it_impure_data. TheFILEstructs and their locks are in.bssand get cleared.libcglue is built once, against the full newlib headers. It is also linked with
libc_nano(NEWLIB_NANO = 1/EE_NEWLIB_NANO), and that library is built with_REENT_SMALL, which givesstruct _reenta different layout:_REENT_SMALL)sizeof(struct _reent)__cleanup__cleanup_localtime_bufSo with newlib-nano the reset misses
__cleanupand clears_localtime_bufinstead. After a restart__sinit()sees the old mark and skips the setup, and the firstprintf()takes a NULL lock (TLB miss, BadVAddr 8). We hit this with ps2link, which uses newlib-nano and restarts itself viaExecPS2()when it starts withoutIPCONFIG.DAT(worked around there in ps2dev/ps2link#70).Fix
__locks_init()now also clears the__cleanupslot of the_REENT_SMALLlayout. A small mirror of the start of that layout gives the offset._emergencyscratch buffer, so clearing it does no harm. A_Static_assertchecks this against the real header._localtime_buf. That pointer is NULL at a first start and points into the old heap after a restart, so this change does not alter that.libcglue has no other code that depends on the layout of
struct _reent.Testing
Tested on real hardware: a PSX DESR-5100, with an EE test program started via ps2link.
printf()(which sets__cleanuptocleanup_stdio), then calls__locks_init()as a restart would, and reads the__cleanupslot back.__cleanupbefore__locks_init()0x001016f0(cleanup_stdio)0x001016f00x001024b8(cleanup_stdio)0x001024b8(not reset)0x001024b8🤖 Generated with Claude Code