Skip to content

libcglue: reset __cleanup for newlib-nano too - #969

Open
kaisa847 wants to merge 1 commit into
ps2dev:masterfrom
kaisa847:fix/libcglue-cleanup-nano
Open

kaisa847 wants to merge 1 commit into
ps2dev:masterfrom
kaisa847:fix/libcglue-cleanup-nano

Conversation

@kaisa847

Copy link
Copy Markdown
Contributor

Problem

__locks_init() clears _REENT_CLEANUP(_REENT), so that __sinit() sets up stdio again after a restart via ExecPS2(). On such a restart .data survives, and with it _impure_data. The FILE structs and their locks are in .bss and 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 gives struct _reent a different layout:

full newlib newlib-nano (_REENT_SMALL)
sizeof(struct _reent) 288 76
offset of __cleanup 52 32
what is at offset 52 __cleanup _localtime_buf

So with newlib-nano the reset misses __cleanup and clears _localtime_buf instead. After a restart __sinit() sees the old mark and skips the setup, and the first printf() takes a NULL lock (TLB miss, BadVAddr 8). We hit this with ps2link, which uses newlib-nano and restarts itself via ExecPS2() when it starts without IPCONFIG.DAT (worked around there in ps2dev/ps2link#70).

Fix

__locks_init() now also clears the __cleanup slot of the _REENT_SMALL layout. A small mirror of the start of that layout gives the offset.

  • Full layout: that slot (offset 32) lies inside the _emergency scratch buffer, so clearing it does no harm. A _Static_assert checks this against the real header.
  • Small layout: the existing write to the full-layout offset hits _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.

  • Test program: it calls printf() (which sets __cleanup to cleanup_stdio), then calls __locks_init() as a restart would, and reads the __cleanup slot back.
  • Builds: each libc variant is linked once against libcglue from current master and once against libcglue with this PR.
libc libcglue __cleanup before after __locks_init()
full master 0x001016f0 (cleanup_stdio) NULL
full this PR 0x001016f0 NULL
nano master 0x001024b8 (cleanup_stdio) 0x001024b8 (not reset)
nano this PR 0x001024b8 NULL

🤖 Generated with Claude Code

__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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant