Development - #446
Merged
Merged
Conversation
…rce() calls in malloc - Add __clib4_resource field to struct _clib4 for caching the global resource pointer - Initialize cache once in stdlib_memory_init instead of calling OpenResource() on every allocation - Update __get_wmem_allocator, __memory_lock, and __memory_unlock to use cached pointer - Eliminates 3 OpenResource() calls per malloc/free (hot path optimization) - Maintains global allocator semantics and thread-safety through shared resource semaphore
dlopen(NULL):
- Previously returned NULL/ENOENT; POSIX requires it to return a
handle for the main program (entire application search scope).
- Now opens SOBJS:libc.so or PROGDIR:SObjs/libc.so so that the
returned handle is a real DSO handle on which DLSym() can find
LOCAL-scope symbols (malloc, getcwd, etc.) directly.
- Falls back to __dl_root_handle as a sentinel when no libc path
is available; dlsym/dlclose recognise and handle the sentinel.
dlsym / symbol resolution (xpcshell/powerfox regression):
- When a process already linked with clib4 calls dlopen("libc.so"),
the OS4 ELF loader returns a valid handle but DLSym() on it
returns ELF32_SYMBOL_NOT_FOUND because the symbols are already
present in the global ELF scope (provided by clib4.library).
- Added a fallback: if DLSym fails with ELF32_SYMBOL_NOT_FOUND on
a specific handle, retry with ELF32_RTLD_DEFAULT (global scope).
This mirrors the GNU ld.so behaviour for pre-loaded libraries.
dlclose(NULL-sentinel):
- dlclose() on the __dl_root_handle sentinel is now a documented
no-op (closing the main program handle must not crash).
Adds test_programs/dlopen/dlsym_test.c to exercise all cases.
Root cause of the xpcshell/powerfox DSI crash at exit:
1. InitSHLibs(hSelf, TRUE) loads libc.so (refcount=1) and registers
its destructors in the process __EXT_DTOR_LIST__.
2. JS ctypes opens libc.so via dlopen() -> refcount=2.
3. XPCOM/ctypes shutdown calls dlclose() on all opened handles.
If the refcount reaches 0, the ELF loader unmaps libc.so.
4. call_main() then runs _end_ctors(__EXT_DTOR_LIST__), which calls
function pointers into the now-unmapped libc.so -> DSI crash
(r30 = invalid TOC base, lwz from freed memory).
Fix: open a permanent 'anchor' handle on libc.so at the start of
call_main() and release it after _end_ctors() has finished, before
shared_obj_init(FALSE). This guarantees the ELF reference count
never drops to zero while any destructor might still reference
libc.so code or data.
The anchor is opened in call_main() rather than reent_init() to
avoid a re-entrant libOpen() loop: at call_main() time clib4 is
fully initialised (__fully_initialized == TRUE, pr_UID set), so
a re-entrant libOpen() triggered by loading libc.so takes the
early-return path safely. Opening the anchor during reent_init()
would cause libc.so to call OpenLibrary(clib4) -> libOpen ->
reent_init -> DLOpen(libc.so) -> infinite loop.
As a side effect this also keeps libc.so symbols resolvable via
dlsym() throughout the entire process lifetime, which fixed the
powerfox OS.File ctypes symbol resolution failures.
Changes:
- dos.h: add void *__dl_libc_anchor to struct _clib4 (at end,
ABI-compatible)
- libc_init_global.c (reent_init): only set __dl_root_handle;
no DLOpen here to avoid the init loop.
- libc_init_global.c (reent_exit): DLClose anchor as safety net
(will already be NULL if call_main ran normally).
- stdlib/main.c (call_main): open anchor before shared_obj_init(TRUE),
close it after _end_ctors() and before shared_obj_init(FALSE).
SOBJS:libc.so belongs to newlib, not clib4. Use PROGDIR:libc.so as the fallback path for the libc anchor and dlopen(NULL) self-handle lookup.
Conflict in library/dos.h resolved: keep __dl_libc_anchor (added by the fix branch) after __clib4_resource (added by the malloc optimization branch). Both fields are at the end of struct _clib4, preserving ABI compatibility.
* fix: memory leaks in cleanup paths - unistd/init_exit.c: free UnlinkNode structs after RemHead in unistd_exit - stdlib/arg.c: save command_line to __command_line_ptr (assignment was missing); save nti_argv0 in __clib4->unused2, free in arg_exit (was orphaned) - stdlib/atexit.c: free dynamically-allocated ExitTrapNode entries (>32) in __exit_trap_trigger after invoking each handler - locale/dcngettext.c: munmap mofile->map before free(mofile) in dcngettext_exit; null out g_mofile after freeing - unistd/wildcard_expand.c: free quote_vector static buffer in __wildcard_expand_exit - dos.h: document usage of unused2 field - posix/mmap_internal.h, mmap.c, munmap.c: mmap cleanup improvements * Added mmap_internal.h to main.c to fix compilation error Added 2 new threads tests * DON'T USE SOBJS:libc.so when using __dl_root_handle! * Removed DOS obsolete flags and removed <dos/obsolete.h> include file
Fixes for the pipelines
Add malloc_usable_size() using the existing wmem allocator metadata: - wmem: add wsize() function pointer to _wmem_allocator_t vtable - wmem: implement wmem_block_size() in O(1) via wmem_block_pre_t/chunk->len - wmem: implement wmem_block_fast_size() in O(1) via chunk->size - wmem: implement wmem_simple_size() via sizes[] array scan - wmem: implement wmem_strict_size() via blocks list scan - wmem: expose wmem_alloc_size() public API in wmem_core.c/h - stdlib: add malloc_usable_size.c with malloc_usable_size() / __malloc_usable_size_r() - include: declare malloc_usable_size() in malloc.h - test: add test_programs/memory/malloc_usable_size.c
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.
Preparing new release