[v2,2/2] malloc: Use __libc_initial to detect an inner libc

Message ID d15e429fa392913292880f71fcbccf4826cd52d8.1606908528.git.fweimer@redhat.com
State Committed
Commit 29a4db291b1282f4088e549391d86b6b470f22be
Headers
Series [v2,1/2] Replace __libc_multiple_libcs with __libc_initial flag |

Commit Message

Florian Weimer Dec. 2, 2020, 11:30 a.m. UTC
  The secondary/non-primary/inner libc (loaded via dlmopen, LD_AUDIT,
static dlopen) must not use sbrk to allocate member because that would
interfere with allocations in the outer libc.  On Linux, this does not
matter because sbrk itself was changed to fail in secondary libcs.

 _dl_addr occasionally shows up in profiles, but had to be used before
because __libc_multiple_libs was unreliable.  So this change achieves
a slight reduction in startup time.
---
v2: Adjust to the first patch.

 malloc/arena.c  | 13 +++++--------
 malloc/malloc.c |  2 ++
 2 files changed, 7 insertions(+), 8 deletions(-)
  

Comments

Adhemerval Zanella Dec. 16, 2020, 1:08 p.m. UTC | #1
On 02/12/2020 08:30, Florian Weimer via Libc-alpha wrote:
> The secondary/non-primary/inner libc (loaded via dlmopen, LD_AUDIT,
> static dlopen) must not use sbrk to allocate member because that would
> interfere with allocations in the outer libc.  On Linux, this does not
> matter because sbrk itself was changed to fail in secondary libcs.
> 
>  _dl_addr occasionally shows up in profiles, but had to be used before
> because __libc_multiple_libs was unreliable.  So this change achieves
> a slight reduction in startup time.

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
> v2: Adjust to the first patch.
> 
>  malloc/arena.c  | 13 +++++--------
>  malloc/malloc.c |  2 ++
>  2 files changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/malloc/arena.c b/malloc/arena.c
> index 202daf15b0..3c9c0ecd86 100644
> --- a/malloc/arena.c
> +++ b/malloc/arena.c
> @@ -294,14 +294,11 @@ ptmalloc_init (void)
>    __malloc_initialized = 0;
>  
>  #ifdef SHARED
> -  /* In case this libc copy is in a non-default namespace, never use brk.
> -     Likewise if dlopened from statically linked program.  */
> -  Dl_info di;
> -  struct link_map *l;
> -
> -  if (_dl_open_hook != NULL
> -      || (_dl_addr (ptmalloc_init, &di, &l, NULL) != 0
> -          && l->l_ns != LM_ID_BASE))
> +  /* In case this libc copy is in a non-default namespace, never use
> +     brk.  Likewise if dlopened from statically linked program.  The
> +     generic sbrk implementation also enforces this, but it is not
> +     used on Hurd.  */
> +  if (!__libc_initial)
>      __morecore = __failing_morecore;
>  #endif
>  

Ok.

> diff --git a/malloc/malloc.c b/malloc/malloc.c
> index 5b87bdb081..0834e4181a 100644
> --- a/malloc/malloc.c
> +++ b/malloc/malloc.c
> @@ -247,6 +247,8 @@
>  /* For SINGLE_THREAD_P.  */
>  #include <sysdep-cancel.h>
>  
> +#include <libc-internal.h>
> +
>  /*
>    Debugging:
>  
> 

Ok.
  

Patch

diff --git a/malloc/arena.c b/malloc/arena.c
index 202daf15b0..3c9c0ecd86 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -294,14 +294,11 @@  ptmalloc_init (void)
   __malloc_initialized = 0;
 
 #ifdef SHARED
-  /* In case this libc copy is in a non-default namespace, never use brk.
-     Likewise if dlopened from statically linked program.  */
-  Dl_info di;
-  struct link_map *l;
-
-  if (_dl_open_hook != NULL
-      || (_dl_addr (ptmalloc_init, &di, &l, NULL) != 0
-          && l->l_ns != LM_ID_BASE))
+  /* In case this libc copy is in a non-default namespace, never use
+     brk.  Likewise if dlopened from statically linked program.  The
+     generic sbrk implementation also enforces this, but it is not
+     used on Hurd.  */
+  if (!__libc_initial)
     __morecore = __failing_morecore;
 #endif
 
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 5b87bdb081..0834e4181a 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -247,6 +247,8 @@ 
 /* For SINGLE_THREAD_P.  */
 #include <sysdep-cancel.h>
 
+#include <libc-internal.h>
+
 /*
   Debugging: