Commit Graph

21 Commits

Author SHA1 Message Date
Feuris d119945a45 fix(libc): allocate from realloc(NULL, 0) as malloc(0) does
realloc(NULL, 0) returned NULL without allocating anything, while malloc(0) returns a unique pointer. C11 7.22.3.5p3 says realloc with a null pointer behaves like malloc for the specified size, so code that grows an empty buffer from zero elements (p = realloc(p, n * size) with n == 0) and treats NULL as out of memory failed on its first call.

FreeBSD 11.4 never returns NULL from realloc for a zero size; je_realloc rounds the size up before choosing between resizing and allocating (contrib/jemalloc/src/jemalloc.c, je_realloc).

A null pointer with size 0 now reaches the allocator's realloc, which allocates as malloc(0) does. A non-null pointer with size 0 is still freed and NULL returned, as before.

Co-authored-by: milescoviello <kitslayer29@gmail.com>
2026-10-12 00:11:17 -03:00
pushkarreddyy 06f84010bb feat(libc): implement reallocf
Two catalog titles stop on the absent libSceLibcInternal export reallocf
(YMZO9ChZb0E) on main 0f863c10:

- PPSA97510 PSXS5 2.2.0
- PPSA98510 SwanStationPS5 1.1.3

    FAIL: unresolved ELF import YMZO9ChZb0E from libSceLibcInternal.prx

reallocf follows FreeBSD 11 lib/libc/stdlib/reallocf.c (freebsd-src
stable/11 5ed7eb0d97ba4436218e810f61bd059acba984c2) on top of the guest
realloc: it reallocates through the application heap, and when realloc
fails (the application allocator returns null or the default heap
rejects the size) it frees the block through the same allocator, sets
ENOMEM and returns null. Size 0 frees and returns null as realloc does
here, so the block is not freed twice. Other heap failures throw, as
for realloc. The NID matches tools/nid_names.py and aerolib.

The application heap tests cover grow, shrink, a null pointer, size 0
and failure with both the replacement and the default allocator.

Co-Authored-By: screamm <noreply@github.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-11 18:16:07 +05:30
Adrià 11cb1b75ab
Merge pull request #2397 from NahuelGenchi/fix/libc-posix-memalign-huge
fix(libc): return ENOMEM from posix_memalign for sizes the default heap rejects
2026-10-10 11:15:44 +02:00
Nahuel Genchi 230625e080 fix(libc): return ENOMEM from posix_memalign for sizes the default heap rejects 2026-10-09 15:03:07 -03:00
Achraf Reyani d8c5d142b6 fix(libc): allocate getcwd's buffer from the application heap
getcwd(NULL, size) returned memory from the guest heap directly, while
the caller frees it with free(), which goes to the application heap.
With a title's malloc replacement registered, that free reached the
replacement with a pointer it never returned. Allocate from the
application heap like strdup, and report ENOMEM when it returns null.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 00:26:48 +09:00
Adrià 8cb527165a
Merge pull request #1120 from TideOPL/feat/libc-sce-v2-runtime-symbols
feat(libc): export the __sce_v2 _Xbad_function_call and sized aligned delete
2026-10-08 09:40:20 +02:00
RomainROCH 0e01d95abd fix(libc): narrow nothrow allocation failure handling 2026-10-07 15:45:15 +02:00
TideOPL 183491b263 feat(libc): export the __sce_v2 _Xbad_function_call and sized aligned delete
PPSA16064 (Minecraft Dungeons II) ships its own libc.prx, which imports
two C++ runtime symbols from libSceLibcInternal that no host library
exported, so the title failed to load:

- std::__sce_v2::_Xbad_function_call() (_ZNSt8__sce_v219_Xbad_function_callEv),
  the inline-namespace spelling of the existing std::_Xbad_function_call.
  It throws std::bad_function_call the same way.
- operator delete(void*, std::size_t, std::align_val_t)
  (_ZdlPvmSt11align_val_t), the sized form of the existing aligned
  delete. It frees the pointer the same way and ignores the size.

application_heap_default frees an aligned block through the sized form
and checks that a null pointer frees nothing. exception_runtime checks
that the new thrower throws a standard exception; it is built only on
Linux and was not run here (Windows 11, MinGW GCC 15.2).
2026-10-07 14:08:13 +01:00
Dean 9ab937b261
Merge pull request #973 from ebarr24/codex/libc-posix-memalign-errors
fix(libc): return posix_memalign allocation errors
2026-10-07 18:01:22 +13:00
Ethan Barr c6dde16023 fix(libc): preserve invalid allocator result diagnostics 2026-10-06 16:44:58 -07:00
Ethan Barr db49ba97b5 fix(libc): return posix_memalign allocation errors 2026-10-06 16:41:01 -07:00
Dextroid17 cc7bda30e7 fix(libc): register the application heap finalizer when libc loads
The finalizer was registered with std::atexit from the heap handshake, which
runs after libc's own __cxa_atexit runner and the title's exit callbacks
register theirs. std::atexit runs handlers in reverse registration order, so
the heap was torn down first: an exit user that allocated then threw
"application heap: allocator has been finalized" out of a destructor path,
which terminates the process.

Register the finalizer from a namespace-scope object instead, so it is
registered when libc is loaded and therefore runs last, and make finalize()
idempotent rather than throwing on a second pass.

application_heap_exit_order registers a guest exit callback that allocates
through the heap and aborts with "application heap: allocator has been
finalized" on the previous code.
2026-10-06 18:40:06 -04:00
Tomás Ortellado e1bfd03f23 fix(libc): return null when strdup allocation fails 2026-10-05 08:57:56 -03:00
RomainROCH 8f76c0a57f fix(libc): release aligned allocations through the application heap 2026-10-01 22:43:44 +02:00
asimplestray 4816e105ad fix(tests): match default convention of ApplicationHeapRealign 2026-09-30 23:55:00 +00:00
asimplestray b42db4b6f3 feat(libc): implement RaiseEv, catchReturnFromMain and reallocalign 2026-09-30 23:55:00 +00:00
boykopovar 244b915c75
Merge branch 'main' into feat/libc-runtime-functions 2026-09-28 09:01:41 +03:00
Dean Galvin a599eca727 feat(libc): operator new/delete, bounds-checked functions, char locale facets and runtime helpers
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ButHENCbXXt8h4EZuWUabJ
2026-09-28 14:22:48 +13:00
SamG-Coder 67fce999c5 fix(libc): support SDK startup without allocator replacements 2026-09-28 09:03:10 +10:00
Pacuka 998605fe4e fix(libc): swap calloc and realloc slots of the malloc replacement table
SceLibcMallocReplace lists calloc before realloc (malloc, free, calloc,
realloc, memalign, reallocalign, posix_memalign, ...), as the table in
retail titles shows. realloc therefore called the application's calloc
with the old pointer as the element count, and calloc called realloc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 11:15:16 +02:00
boykopovar 9f2b41b9a1 fix(libc): route guest allocations through application heap 2026-09-24 06:53:12 +03:00