* [PATCH] malloc: Clean up __malloc_initialized
@ 2021-06-18 9:09 Siddhesh Poyarekar
2021-06-18 9:36 ` Florian Weimer
0 siblings, 1 reply; 5+ messages in thread
From: Siddhesh Poyarekar @ 2021-06-18 9:09 UTC (permalink / raw)
To: libc-alpha
It can be assumed that ptmalloc_init is always called in a
single-threaded context because pthread_create calls malloc before
spawning a thread. As a result, a boolean value for
__malloc_initialized is sufficient to ensure that the function body is
executed only once.
Further, __malloc_initialized is mentioned in Versions when it is
actually an internal symbol. Drop that reference and also remove its
declaration from include/malloc.h since ideally it should not be used
anywhere outside malloc. mcheck uses it (and hence needs an extern
decl), but mcheck is broken enough that this use is just a small chink
in an already cracked vase.
---
include/malloc.h | 6 ------
malloc/Versions | 2 +-
malloc/arena.c | 16 +++++++++-------
malloc/malloc.c | 14 +++++++-------
malloc/mcheck.c | 5 ++++-
5 files changed, 21 insertions(+), 22 deletions(-)
diff --git a/include/malloc.h b/include/malloc.h
index b77761f74d..f9c9610548 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -5,12 +5,6 @@
# ifndef _ISOMAC
# include <rtld-malloc.h>
-/* In the GNU libc we rename the global variable
- `__malloc_initialized' to `__libc_malloc_initialized'. */
-#define __malloc_initialized __libc_malloc_initialized
-/* Nonzero if the malloc is already initialized. */
-extern int __malloc_initialized attribute_hidden;
-
struct malloc_state;
typedef struct malloc_state *mstate;
diff --git a/malloc/Versions b/malloc/Versions
index 6693c46ee2..62e4698a08 100644
--- a/malloc/Versions
+++ b/malloc/Versions
@@ -9,7 +9,7 @@ libc {
__libc_valloc;
__malloc_initialize_hook; __free_hook; __malloc_hook; __realloc_hook;
__memalign_hook; __after_morecore_hook;
- __malloc_initialized; __default_morecore; __morecore;
+ __default_morecore; __morecore;
# functions used in inline functions or macros
_obstack_allocated_p; _obstack_begin; _obstack_begin_1;
diff --git a/malloc/arena.c b/malloc/arena.c
index 7eb110445e..bb14bc5ca9 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -97,7 +97,7 @@ static mstate free_list;
__libc_lock_define_initialized (static, list_lock);
/* Already initialized? */
-int __malloc_initialized = -1;
+bool __malloc_initialized;
/**************************************************************************/
@@ -143,7 +143,7 @@ int __malloc_initialized = -1;
void
__malloc_fork_lock_parent (void)
{
- if (__malloc_initialized < 1)
+ if (!__malloc_initialized)
return;
/* We do not acquire free_list_lock here because we completely
@@ -163,7 +163,7 @@ __malloc_fork_lock_parent (void)
void
__malloc_fork_unlock_parent (void)
{
- if (__malloc_initialized < 1)
+ if (!__malloc_initialized)
return;
for (mstate ar_ptr = &main_arena;; )
@@ -179,7 +179,7 @@ __malloc_fork_unlock_parent (void)
void
__malloc_fork_unlock_child (void)
{
- if (__malloc_initialized < 1)
+ if (!__malloc_initialized)
return;
/* Push all arenas to the free list, except thread_arena, which is
@@ -287,13 +287,16 @@ extern struct dl_open_hook *_dl_open_hook;
libc_hidden_proto (_dl_open_hook);
#endif
+/* Malloc initialization. We assume that the function is always called in a
+ single threaded context because pthread_create calls malloc before spawning
+ a thread. */
static void
ptmalloc_init (void)
{
- if (__malloc_initialized >= 0)
+ if (__malloc_initialized)
return;
- __malloc_initialized = 0;
+ __malloc_initialized = true;
#ifdef USE_MTAG
if ((TUNABLE_GET_FULL (glibc, mem, tagging, int32_t, NULL) & 1) != 0)
@@ -410,7 +413,6 @@ ptmalloc_init (void)
if (hook != NULL)
(*hook)();
#endif
- __malloc_initialized = 1;
}
/* Managing heaps and arenas (for concurrent threads) */
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 0e2e1747e0..0ba50a497b 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3562,7 +3562,7 @@ libc_hidden_def (__libc_memalign)
void *
__libc_valloc (size_t bytes)
{
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
void *address = RETURN_ADDRESS (0);
@@ -3573,7 +3573,7 @@ __libc_valloc (size_t bytes)
void *
__libc_pvalloc (size_t bytes)
{
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
void *address = RETURN_ADDRESS (0);
@@ -5077,7 +5077,7 @@ __malloc_trim (size_t s)
{
int result = 0;
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
mstate ar_ptr = &main_arena;
@@ -5212,7 +5212,7 @@ __libc_mallinfo2 (void)
struct mallinfo2 m;
mstate ar_ptr;
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
memset (&m, 0, sizeof (m));
@@ -5263,7 +5263,7 @@ __malloc_stats (void)
mstate ar_ptr;
unsigned int in_use_b = mp_.mmapped_mem, system_b = in_use_b;
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
_IO_flockfile (stderr);
int old_flags2 = stderr->_flags2;
@@ -5432,7 +5432,7 @@ __libc_mallopt (int param_number, int value)
mstate av = &main_arena;
int res = 1;
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
__libc_lock_lock (av->mutex);
@@ -5691,7 +5691,7 @@ __malloc_info (int options, FILE *fp)
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
fputs ("<malloc version=\"1\">\n", fp);
diff --git a/malloc/mcheck.c b/malloc/mcheck.c
index 2a1fc645d4..93d7992a41 100644
--- a/malloc/mcheck.c
+++ b/malloc/mcheck.c
@@ -21,6 +21,7 @@
# define _MALLOC_INTERNAL
# include <malloc.h>
# include <mcheck.h>
+# include <stdbool.h>
# include <stdint.h>
# include <stdio.h>
# include <libintl.h>
@@ -372,10 +373,12 @@ mabort (enum mcheck_status status)
int
mcheck (void (*func) (enum mcheck_status))
{
+ extern bool __malloc_initialized;
+
abortfunc = (func != NULL) ? func : &mabort;
/* These hooks may not be safely inserted if malloc is already in use. */
- if (__malloc_initialized <= 0 && !mcheck_used)
+ if (!__malloc_initialized && !mcheck_used)
{
/* We call malloc() once here to ensure it is initialized. */
void *p = malloc (0);
--
2.31.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] malloc: Clean up __malloc_initialized
2021-06-18 9:09 [PATCH] malloc: Clean up __malloc_initialized Siddhesh Poyarekar
@ 2021-06-18 9:36 ` Florian Weimer
2021-06-18 9:53 ` [Patch v2] " Siddhesh Poyarekar
0 siblings, 1 reply; 5+ messages in thread
From: Florian Weimer @ 2021-06-18 9:36 UTC (permalink / raw)
To: Siddhesh Poyarekar via Libc-alpha; +Cc: Siddhesh Poyarekar
* Siddhesh Poyarekar via Libc-alpha:
> -/* In the GNU libc we rename the global variable
> - `__malloc_initialized' to `__libc_malloc_initialized'. */
> -#define __malloc_initialized __libc_malloc_initialized
> -/* Nonzero if the malloc is already initialized. */
> -extern int __malloc_initialized attribute_hidden;
You need to provide the attribute_hidden somewhere, otherwise you'll get
worse code on some targets.
> diff --git a/malloc/mcheck.c b/malloc/mcheck.c
> index 2a1fc645d4..93d7992a41 100644
> --- a/malloc/mcheck.c
> +++ b/malloc/mcheck.c
> @@ -21,6 +21,7 @@
> # define _MALLOC_INTERNAL
> # include <malloc.h>
> # include <mcheck.h>
> +# include <stdbool.h>
> # include <stdint.h>
> # include <stdio.h>
> # include <libintl.h>
> @@ -372,10 +373,12 @@ mabort (enum mcheck_status status)
> int
> mcheck (void (*func) (enum mcheck_status))
> {
> + extern bool __malloc_initialized;
> +
This suggests that __malloc_initialized should remain in the wrapper
header.
Thanks,
Florian
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Patch v2] malloc: Clean up __malloc_initialized
2021-06-18 9:36 ` Florian Weimer
@ 2021-06-18 9:53 ` Siddhesh Poyarekar
2021-06-22 4:47 ` Siddhesh Poyarekar
2021-06-22 12:46 ` Siddhesh Poyarekar
0 siblings, 2 replies; 5+ messages in thread
From: Siddhesh Poyarekar @ 2021-06-18 9:53 UTC (permalink / raw)
To: libc-alpha
It can be assumed that ptmalloc_init is always called in a
single-threaded context because pthread_create calls malloc before
spawning a thread. As a result, a boolean value for
__malloc_initialized is sufficient to ensure that the function body is
executed only once.
Further, __malloc_initialized is mentioned in Versions when it is
actually an internal symbol. Drop that reference and the macro
definition to __libc_malloc_initialized since it does not have an
external use.
---
Changes from v1:
- Retained attribute_hidden extern declaration.
include/malloc.h | 6 ++----
malloc/Versions | 2 +-
malloc/arena.c | 16 +++++++++-------
malloc/malloc.c | 14 +++++++-------
malloc/mcheck.c | 2 +-
5 files changed, 20 insertions(+), 20 deletions(-)
diff --git a/include/malloc.h b/include/malloc.h
index b77761f74d..f8b7ebf250 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -4,12 +4,10 @@
# ifndef _ISOMAC
# include <rtld-malloc.h>
+# include <stdbool.h>
-/* In the GNU libc we rename the global variable
- `__malloc_initialized' to `__libc_malloc_initialized'. */
-#define __malloc_initialized __libc_malloc_initialized
/* Nonzero if the malloc is already initialized. */
-extern int __malloc_initialized attribute_hidden;
+extern bool __malloc_initialized attribute_hidden;
struct malloc_state;
typedef struct malloc_state *mstate;
diff --git a/malloc/Versions b/malloc/Versions
index 6693c46ee2..62e4698a08 100644
--- a/malloc/Versions
+++ b/malloc/Versions
@@ -9,7 +9,7 @@ libc {
__libc_valloc;
__malloc_initialize_hook; __free_hook; __malloc_hook; __realloc_hook;
__memalign_hook; __after_morecore_hook;
- __malloc_initialized; __default_morecore; __morecore;
+ __default_morecore; __morecore;
# functions used in inline functions or macros
_obstack_allocated_p; _obstack_begin; _obstack_begin_1;
diff --git a/malloc/arena.c b/malloc/arena.c
index 7eb110445e..bb14bc5ca9 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -97,7 +97,7 @@ static mstate free_list;
__libc_lock_define_initialized (static, list_lock);
/* Already initialized? */
-int __malloc_initialized = -1;
+bool __malloc_initialized;
/**************************************************************************/
@@ -143,7 +143,7 @@ int __malloc_initialized = -1;
void
__malloc_fork_lock_parent (void)
{
- if (__malloc_initialized < 1)
+ if (!__malloc_initialized)
return;
/* We do not acquire free_list_lock here because we completely
@@ -163,7 +163,7 @@ __malloc_fork_lock_parent (void)
void
__malloc_fork_unlock_parent (void)
{
- if (__malloc_initialized < 1)
+ if (!__malloc_initialized)
return;
for (mstate ar_ptr = &main_arena;; )
@@ -179,7 +179,7 @@ __malloc_fork_unlock_parent (void)
void
__malloc_fork_unlock_child (void)
{
- if (__malloc_initialized < 1)
+ if (!__malloc_initialized)
return;
/* Push all arenas to the free list, except thread_arena, which is
@@ -287,13 +287,16 @@ extern struct dl_open_hook *_dl_open_hook;
libc_hidden_proto (_dl_open_hook);
#endif
+/* Malloc initialization. We assume that the function is always called in a
+ single threaded context because pthread_create calls malloc before spawning
+ a thread. */
static void
ptmalloc_init (void)
{
- if (__malloc_initialized >= 0)
+ if (__malloc_initialized)
return;
- __malloc_initialized = 0;
+ __malloc_initialized = true;
#ifdef USE_MTAG
if ((TUNABLE_GET_FULL (glibc, mem, tagging, int32_t, NULL) & 1) != 0)
@@ -410,7 +413,6 @@ ptmalloc_init (void)
if (hook != NULL)
(*hook)();
#endif
- __malloc_initialized = 1;
}
/* Managing heaps and arenas (for concurrent threads) */
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 0e2e1747e0..0ba50a497b 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3562,7 +3562,7 @@ libc_hidden_def (__libc_memalign)
void *
__libc_valloc (size_t bytes)
{
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
void *address = RETURN_ADDRESS (0);
@@ -3573,7 +3573,7 @@ __libc_valloc (size_t bytes)
void *
__libc_pvalloc (size_t bytes)
{
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
void *address = RETURN_ADDRESS (0);
@@ -5077,7 +5077,7 @@ __malloc_trim (size_t s)
{
int result = 0;
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
mstate ar_ptr = &main_arena;
@@ -5212,7 +5212,7 @@ __libc_mallinfo2 (void)
struct mallinfo2 m;
mstate ar_ptr;
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
memset (&m, 0, sizeof (m));
@@ -5263,7 +5263,7 @@ __malloc_stats (void)
mstate ar_ptr;
unsigned int in_use_b = mp_.mmapped_mem, system_b = in_use_b;
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
_IO_flockfile (stderr);
int old_flags2 = stderr->_flags2;
@@ -5432,7 +5432,7 @@ __libc_mallopt (int param_number, int value)
mstate av = &main_arena;
int res = 1;
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
__libc_lock_lock (av->mutex);
@@ -5691,7 +5691,7 @@ __malloc_info (int options, FILE *fp)
- if (__malloc_initialized < 0)
+ if (!__malloc_initialized)
ptmalloc_init ();
fputs ("<malloc version=\"1\">\n", fp);
diff --git a/malloc/mcheck.c b/malloc/mcheck.c
index 2a1fc645d4..acc6ad2e05 100644
--- a/malloc/mcheck.c
+++ b/malloc/mcheck.c
@@ -375,7 +375,7 @@ mcheck (void (*func) (enum mcheck_status))
abortfunc = (func != NULL) ? func : &mabort;
/* These hooks may not be safely inserted if malloc is already in use. */
- if (__malloc_initialized <= 0 && !mcheck_used)
+ if (!__malloc_initialized && !mcheck_used)
{
/* We call malloc() once here to ensure it is initialized. */
void *p = malloc (0);
--
2.31.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Patch v2] malloc: Clean up __malloc_initialized
2021-06-18 9:53 ` [Patch v2] " Siddhesh Poyarekar
@ 2021-06-22 4:47 ` Siddhesh Poyarekar
2021-06-22 12:46 ` Siddhesh Poyarekar
1 sibling, 0 replies; 5+ messages in thread
From: Siddhesh Poyarekar @ 2021-06-22 4:47 UTC (permalink / raw)
To: libc-alpha; +Cc: Florian Weimer
On 6/18/21 3:23 PM, Siddhesh Poyarekar via Libc-alpha wrote:
> It can be assumed that ptmalloc_init is always called in a
> single-threaded context because pthread_create calls malloc before
> spawning a thread. As a result, a boolean value for
> __malloc_initialized is sufficient to ensure that the function body is
> executed only once.
>
> Further, __malloc_initialized is mentioned in Versions when it is
> actually an internal symbol. Drop that reference and the macro
> definition to __libc_malloc_initialized since it does not have an
> external use.
> ---
> Changes from v1:
> - Retained attribute_hidden extern declaration.
Florian what do you think? I've made the change you requested.
Siddhesh
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Patch v2] malloc: Clean up __malloc_initialized
2021-06-18 9:53 ` [Patch v2] " Siddhesh Poyarekar
2021-06-22 4:47 ` Siddhesh Poyarekar
@ 2021-06-22 12:46 ` Siddhesh Poyarekar
1 sibling, 0 replies; 5+ messages in thread
From: Siddhesh Poyarekar @ 2021-06-22 12:46 UTC (permalink / raw)
To: libc-alpha
On 6/18/21 3:23 PM, Siddhesh Poyarekar via Libc-alpha wrote:
> It can be assumed that ptmalloc_init is always called in a
> single-threaded context because pthread_create calls malloc before
> spawning a thread. As a result, a boolean value for
> __malloc_initialized is sufficient to ensure that the function body is
> executed only once.
>
> Further, __malloc_initialized is mentioned in Versions when it is
> actually an internal symbol. Drop that reference and the macro
> definition to __libc_malloc_initialized since it does not have an
> external use.
Ahh no, this breaks mcheck initialization, which depends on the
intermediate value to ensure it is initialized only once. I'll figure
out a way to work this into the hooks removal patchset since it
simplifies the mcheck functionality a bit.
Siddhesh
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2021-06-22 12:46 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-18 9:09 [PATCH] malloc: Clean up __malloc_initialized Siddhesh Poyarekar
2021-06-18 9:36 ` Florian Weimer
2021-06-18 9:53 ` [Patch v2] " Siddhesh Poyarekar
2021-06-22 4:47 ` Siddhesh Poyarekar
2021-06-22 12:46 ` Siddhesh Poyarekar
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).