Kaynağa Gözat

convert malloc use under libc-internal locks to use internal allocator

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.
Rich Felker 4 yıl önce
ebeveyn
işleme
34952fe5de

+ 5 - 0
ldso/dynlink.c

@@ -23,6 +23,11 @@
 #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)))

+ 5 - 0
src/aio/aio.c

@@ -11,6 +11,11 @@
 #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

+ 5 - 0
src/exit/atexit.c

@@ -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
 

+ 5 - 0
src/ldso/dlerror.c

@@ -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();

+ 5 - 0
src/locale/dcngettext.c

@@ -11,6 +11,11 @@
 #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;

+ 6 - 0
src/locale/locale_map.c

@@ -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;

+ 5 - 0
src/thread/sem_open.c

@@ -13,6 +13,11 @@
 #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;

+ 5 - 0
src/time/__tz.c

@@ -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 };