[PATCH v2] malloc: Improve malloc initialization
Florian Weimer
fweimer@redhat.com
Wed Apr 2 12:33:18 GMT 2025
* Wilco Dijkstra:
> diff --git a/elf/libc_early_init.c b/elf/libc_early_init.c
> index 07202317534a4edbd2202c6e45770094cadb67f6..6ab14fb239a02d8da356a1fc5bdb307aabf35705 100644
> --- a/elf/libc_early_init.c
> +++ b/elf/libc_early_init.c
> @@ -29,9 +29,19 @@
> _Bool __libc_initial;
> #endif
>
> +static void __attribute_noinline__
> +myfree (void *p)
> +{
> + free (p);
> +}
> +
> void
> __libc_early_init (_Bool initial)
> {
> + /* Initialize malloc using an out-of-range malloc. */
> + volatile size_t size = -1;
> + myfree (malloc (size));
> +
> /* Initialize ctype data. */
> __ctype_init ();
>
This callos malloc/free from an interposed malloc before its ELF
constructor has run. I expect this to result in crashes for some
mallocs because they don't expect that we bypass initialization in this
way. In the future, we might even call __libc_early_init immediately
after libc.so relocation, before other shared objects are relocated.
If we want to pre-initialize malloc, we should explicitly call a
glibc-specific initialization routine from __libc_early_init, using
call_function_static_weak.
Thanks,
Florian
More information about the Libc-alpha
mailing list