convert malloc use under libc-internal locks to use internal allocator
authorRich Felker <dalias@aerifal.cx>
Wed, 11 Nov 2020 18:08:42 +0000 (13:08 -0500)
committerRich Felker <dalias@aerifal.cx>
Wed, 11 Nov 2020 18:31:50 +0000 (13:31 -0500)
this change lifts undocumented restrictions on calls by replacement
mallocs to libc functions that might take these locks, and sets the
stage for lifting restrictions on the child execution environment
after multithreaded fork.

care is taken to #define macros to replace all four functions (malloc,
calloc, realloc, free) even if not all of them will be used, using an
undefined symbol name for the ones intended not to be used so that any
inadvertent future use will be caught at compile time rather than
directed to the wrong implementation.

ldso/dynlink.c
src/aio/aio.c
src/exit/atexit.c
src/ldso/dlerror.c
src/locale/dcngettext.c
src/locale/locale_map.c
src/thread/sem_open.c
src/time/__tz.c

index 502e52c..61714f4 100644 (file)
 #include "libc.h"
 #include "dynlink.h"
 
+#define malloc __libc_malloc
+#define calloc __libc_calloc
+#define realloc __libc_realloc
+#define free __libc_free
+
 static void error(const char *, ...);
 
 #define MAXP2(a,b) (-(-(a)&-(b)))
index b488e3d..e004f98 100644 (file)
 #include "pthread_impl.h"
 #include "aio_impl.h"
 
+#define malloc __libc_malloc
+#define calloc __libc_calloc
+#define realloc __libc_realloc
+#define free __libc_free
+
 /* The following is a threads-based implementation of AIO with minimal
  * dependence on implementation details. Most synchronization is
  * performed with pthread primitives, but atomics and futex operations
index 160d277..fcd940f 100644 (file)
@@ -3,6 +3,11 @@
 #include "libc.h"
 #include "lock.h"
 
+#define malloc __libc_malloc
+#define calloc __libc_calloc
+#define realloc undef
+#define free undef
+
 /* Ensure that at least 32 atexit handlers can be registered without malloc */
 #define COUNT 32
 
index d8bbfc0..c782ca6 100644 (file)
@@ -5,6 +5,11 @@
 #include "dynlink.h"
 #include "lock.h"
 
+#define malloc __libc_malloc
+#define calloc __libc_calloc
+#define realloc __libc_realloc
+#define free __libc_free
+
 char *dlerror()
 {
        pthread_t self = __pthread_self();
index 4c30439..39a98e8 100644 (file)
 #include "pleval.h"
 #include "lock.h"
 
+#define malloc __libc_malloc
+#define calloc __libc_calloc
+#define realloc undef
+#define free undef
+
 struct binding {
        struct binding *next;
        int dirlen;
index e7eede6..94f1b04 100644 (file)
@@ -1,10 +1,16 @@
 #include <locale.h>
 #include <string.h>
 #include <sys/mman.h>
+#include <stdlib.h>
 #include "locale_impl.h"
 #include "libc.h"
 #include "lock.h"
 
+#define malloc __libc_malloc
+#define calloc undef
+#define realloc undef
+#define free undef
+
 const char *__lctrans_impl(const char *msg, const struct __locale_map *lm)
 {
        const char *trans = 0;
index 6fb0c5b..dad8f17 100644 (file)
 #include <pthread.h>
 #include "lock.h"
 
+#define malloc __libc_malloc
+#define calloc __libc_calloc
+#define realloc undef
+#define free undef
+
 static struct {
        ino_t ino;
        sem_t *sem;
index 49a7371..3044d20 100644 (file)
@@ -7,6 +7,11 @@
 #include "libc.h"
 #include "lock.h"
 
+#define malloc __libc_malloc
+#define calloc undef
+#define realloc undef
+#define free undef
+
 long  __timezone = 0;
 int   __daylight = 0;
 char *__tzname[2] = { 0, 0 };