[PATCH v3] malloc: Improve malloc initialization

Florian Weimer fw@deneb.enyo.de
Fri May 9 10:19:06 GMT 2025


* Wilco Dijkstra:

> Hi Florian,
>
>>>>> Move malloc initialization to __libc_early_init.  Use a hidden __ptmalloc_init
>>>>> for initialization and a weak call to avoid pulling in the system malloc in a
>>>>> static binary.  All previous initialization checks can now be removed.
>>>>>
>>>>> Passes regress, OK for commit?
>>>>
>>>> Can't you remove __malloc_initialized altogether?  And find some way
>>>> to avoid the fork lock in case of interposed malloc?
>>>
>>> Yes, we can remove the __malloc_initialized global - vfork only uses
>>> the callback if multithreaded, at which point you likely used malloc
>>> somewhere already.
>>
>> We still need to make sure that the built-in fork handler works with
>> with an uninitialized malloc, in case glibc's malloc has been
>> interposed and cannot piggyback on the malloc call from
>> pthread_create.
>
> The initialization is done unconditionally startup, so when the vfork callback
> is used, malloc will always be initialized even if interposed.

Ahh, and the fork handlers aren't present in statically linked builds
if malloc is unterposed.  So this should indeed work without further
complications.


More information about the Libc-alpha mailing list