about summary refs log tree commit diff
path: root/malloc
diff options
context:
space:
mode:
authorSiddhesh Poyarekar <siddhesh@sourceware.org>2021-07-22 18:38:08 +0530
committerSiddhesh Poyarekar <siddhesh@sourceware.org>2021-07-22 18:38:08 +0530
commitb5bd5bfe88f496463ec9fab680a8edf64d7c2a42 (patch)
tree51978efe075143c64fcd622b051faa49572928db /malloc
parent9dad716d4d2993f50b165747781244bd7c43bc95 (diff)
downloadglibc-b5bd5bfe88f496463ec9fab680a8edf64d7c2a42.tar.gz
glibc-b5bd5bfe88f496463ec9fab680a8edf64d7c2a42.tar.xz
glibc-b5bd5bfe88f496463ec9fab680a8edf64d7c2a42.zip
glibc.malloc.check: Wean away from malloc hooks
The malloc-check debugging feature is tightly integrated into glibc
malloc, so thanks to an idea from Florian Weimer, much of the malloc
implementation has been moved into libc_malloc_debug.so to support
malloc-check.  Due to this, glibc malloc and malloc-check can no
longer work together; they use altogether different (but identical)
structures for heap management.  This should not make a difference
though since the malloc check hook is not disabled anywhere.
malloc_set_state does, but it does so early enough that it shouldn't
cause any problems.

The malloc check tunable is now in the debug DSO and has no effect
when the DSO is not preloaded.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Tested-by: Carlos O'Donell <carlos@redhat.com>
Diffstat (limited to 'malloc')
-rw-r--r--malloc/Makefile3
-rw-r--r--malloc/Versions12
-rw-r--r--malloc/arena.c17
-rw-r--r--malloc/hooks.c9
-rw-r--r--malloc/malloc-check.c63
-rw-r--r--malloc/malloc-debug.c154
-rw-r--r--malloc/malloc.c35
7 files changed, 213 insertions, 80 deletions
diff --git a/malloc/Makefile b/malloc/Makefile
index eb5f5560bb..b89af21d19 100644
--- a/malloc/Makefile
+++ b/malloc/Makefile
@@ -133,7 +133,7 @@ test-extras = \
 libmemusage-routines = memusage
 libmemusage-inhibit-o = $(filter-out .os,$(object-suffixes))
 
-libc_malloc_debug-routines = malloc-debug
+libc_malloc_debug-routines = malloc-debug $(sysdep_malloc_debug_routines)
 libc_malloc_debug-inhibit-o = $(filter-out .os,$(object-suffixes))
 
 $(objpfx)tst-malloc-backtrace: $(shared-thread-library)
@@ -251,6 +251,7 @@ tst-malloc-usable-tunables-ENV = GLIBC_TUNABLES=glibc.malloc.check=3 \
 
 tst-mxfast-ENV = GLIBC_TUNABLES=glibc.malloc.tcache_count=0:glibc.malloc.mxfast=0
 
+CPPFLAGS-malloc-debug.c += -DUSE_TCACHE=0
 ifeq ($(experimental-malloc),yes)
 CPPFLAGS-malloc.c += -DUSE_TCACHE=1
 else
diff --git a/malloc/Versions b/malloc/Versions
index 71d933de19..cbb73d18c1 100644
--- a/malloc/Versions
+++ b/malloc/Versions
@@ -119,13 +119,25 @@ libc_malloc_debug {
     mprobe;
     mtrace;
     muntrace;
+
+    mallinfo;
+    malloc_stats;
+    malloc_trim;
+    malloc_usable_size;
+    mallopt;
   }
   GLIBC_2.2 {
     mcheck_check_all;
     mcheck_pedantic;
     posix_memalign;
   }
+  GLIBC_2.10 {
+    malloc_info;
+  }
   GLIBC_2.16 {
     aligned_alloc;
   }
+  GLIBC_2.33 {
+    mallinfo2;
+  }
 }
diff --git a/malloc/arena.c b/malloc/arena.c
index 840426f9fb..edcaa8816d 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -79,7 +79,9 @@ static __thread mstate thread_arena attribute_tls_model_ie;
    acquired after free_list_lock has been acquired.  */
 
 __libc_lock_define_initialized (static, free_list_lock);
+#if IS_IN (libc)
 static size_t narenas = 1;
+#endif
 static mstate free_list;
 
 /* list_lock prevents concurrent writes to the next member of struct
@@ -207,14 +209,6 @@ __malloc_fork_unlock_child (void)
 }
 
 #if HAVE_TUNABLES
-static void
-TUNABLE_CALLBACK (set_mallopt_check) (tunable_val_t *valp)
-{
-  int32_t value = (int32_t) valp->numval;
-  if (value != 0)
-    __malloc_check_init ();
-}
-
 # define TUNABLE_CALLBACK_FNDECL(__name, __type) \
 static inline int do_ ## __name (__type value);				      \
 static void									      \
@@ -309,7 +303,7 @@ ptmalloc_init (void)
     }
 #endif
 
-#ifdef SHARED
+#if defined SHARED && IS_IN (libc)
   /* 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
@@ -323,7 +317,6 @@ ptmalloc_init (void)
   malloc_init_state (&main_arena);
 
 #if HAVE_TUNABLES
-  TUNABLE_GET (check, int32_t, TUNABLE_CALLBACK (set_mallopt_check));
   TUNABLE_GET (top_pad, size_t, TUNABLE_CALLBACK (set_top_pad));
   TUNABLE_GET (perturb, int32_t, TUNABLE_CALLBACK (set_perturb_byte));
   TUNABLE_GET (mmap_threshold, size_t, TUNABLE_CALLBACK (set_mmap_threshold));
@@ -401,8 +394,6 @@ ptmalloc_init (void)
             }
         }
     }
-  if (s && s[0] != '\0' && s[0] != '0')
-    __malloc_check_init ();
 #endif
 }
 
@@ -672,6 +663,7 @@ heap_trim (heap_info *heap, size_t pad)
 
 /* Create a new arena with initial size "size".  */
 
+#if IS_IN (libc)
 /* If REPLACED_ARENA is not NULL, detach it from this thread.  Must be
    called while free_list_lock is held.  */
 static void
@@ -947,6 +939,7 @@ arena_get_retry (mstate ar_ptr, size_t bytes)
 
   return ar_ptr;
 }
+#endif
 
 void
 __malloc_arena_thread_freeres (void)
diff --git a/malloc/hooks.c b/malloc/hooks.c
index 8e9fefe6c3..6c212fbc21 100644
--- a/malloc/hooks.c
+++ b/malloc/hooks.c
@@ -39,8 +39,6 @@ void *weak_variable (*__malloc_hook) (size_t, const void *) = NULL;
 void *weak_variable (*__realloc_hook) (void *, size_t, const void *) = NULL;
 void *weak_variable (*__memalign_hook) (size_t, size_t, const void *) = NULL;
 
-#include "malloc-check.c"
-
 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_25)
 
 /* Support for restoring dumped heaps contained in historic Emacs
@@ -108,13 +106,6 @@ malloc_set_state (void *msptr)
      calls calloc and thus must be called only afterwards, so there
      cannot be more than one thread when we reach this point.  */
 
-  /* Disable the malloc hooks (and malloc checking).  */
-  __malloc_hook = NULL;
-  __realloc_hook = NULL;
-  __free_hook = NULL;
-  __memalign_hook = NULL;
-  using_malloc_checking = 0;
-
   /* Patch the dumped heap.  We no longer try to integrate into the
      existing heap.  Instead, we mark the existing chunks as mmapped.
      Together with the update to dumped_main_arena_start and
diff --git a/malloc/malloc-check.c b/malloc/malloc-check.c
index dcab880510..a444c7478e 100644
--- a/malloc/malloc-check.c
+++ b/malloc/malloc-check.c
@@ -17,20 +17,8 @@
    License along with the GNU C Library; see the file COPYING.LIB.  If
    not, see <https://www.gnu.org/licenses/>.  */
 
-
-/* Whether we are using malloc checking.  */
-static int using_malloc_checking;
-
-/* Activate a standard set of debugging hooks. */
-void
-__malloc_check_init (void)
-{
-  using_malloc_checking = 1;
-  __malloc_hook = malloc_check;
-  __free_hook = free_check;
-  __realloc_hook = realloc_check;
-  __memalign_hook = memalign_check;
-}
+#define __mremap mremap
+#include "malloc.c"
 
 /* When memory is tagged, the checking data is stored in the user part
    of the chunk.  We can't rely on the user not having modified the
@@ -63,14 +51,13 @@ magicbyte (const void *p)
    must reach it with this iteration, otherwise we have witnessed a memory
    corruption.  */
 static size_t
-malloc_check_get_size (mchunkptr p)
+malloc_check_get_size (void *mem)
 {
   size_t size;
   unsigned char c;
+  mchunkptr p = mem2chunk (mem);
   unsigned char magic = magicbyte (p);
 
-  assert (using_malloc_checking == 1);
-
   for (size = CHUNK_HDR_SZ + memsize (p) - 1;
        (c = *SAFE_CHAR_OFFSET (p, size)) != magic;
        size -= c)
@@ -203,7 +190,7 @@ top_check (void)
 }
 
 static void *
-malloc_check (size_t sz, const void *caller)
+malloc_check (size_t sz)
 {
   void *victim;
   size_t nb;
@@ -222,7 +209,7 @@ malloc_check (size_t sz, const void *caller)
 }
 
 static void
-free_check (void *mem, const void *caller)
+free_check (void *mem)
 {
   mchunkptr p;
 
@@ -256,7 +243,7 @@ free_check (void *mem, const void *caller)
 }
 
 static void *
-realloc_check (void *oldmem, size_t bytes, const void *caller)
+realloc_check (void *oldmem, size_t bytes)
 {
   INTERNAL_SIZE_T chnb;
   void *newmem = 0;
@@ -269,11 +256,11 @@ realloc_check (void *oldmem, size_t bytes, const void *caller)
       return NULL;
     }
   if (oldmem == 0)
-    return malloc_check (bytes, NULL);
+    return malloc_check (bytes);
 
   if (bytes == 0)
     {
-      free_check (oldmem, NULL);
+      free_check (oldmem);
       return NULL;
     }
 
@@ -348,12 +335,12 @@ invert:
 }
 
 static void *
-memalign_check (size_t alignment, size_t bytes, const void *caller)
+memalign_check (size_t alignment, size_t bytes)
 {
   void *mem;
 
   if (alignment <= MALLOC_ALIGNMENT)
-    return malloc_check (bytes, NULL);
+    return malloc_check (bytes);
 
   if (alignment < MINSIZE)
     alignment = MINSIZE;
@@ -363,14 +350,14 @@ memalign_check (size_t alignment, size_t bytes, const void *caller)
   if (alignment > SIZE_MAX / 2 + 1)
     {
       __set_errno (EINVAL);
-      return 0;
+      return NULL;
     }
 
   /* Check for overflow.  */
   if (bytes > SIZE_MAX - alignment - MINSIZE)
     {
       __set_errno (ENOMEM);
-      return 0;
+      return NULL;
     }
 
   /* Make sure alignment is power of 2.  */
@@ -388,3 +375,27 @@ memalign_check (size_t alignment, size_t bytes, const void *caller)
   __libc_lock_unlock (main_arena.mutex);
   return mem2mem_check (tag_new_usable (mem), bytes);
 }
+
+static void
+TUNABLE_CALLBACK (set_mallopt_check) (tunable_val_t *valp)
+{
+  int32_t value = (int32_t) valp->numval;
+  if (value != 0)
+    __malloc_debug_enable (MALLOC_CHECK_HOOK);
+}
+
+static bool
+initialize_malloc_check (void)
+{
+  /* This is the copy of the malloc initializer that we pulled in along with
+     malloc-check.  This does not affect any of the libc malloc structures.  */
+  ptmalloc_init ();
+#if HAVE_TUNABLES
+  TUNABLE_GET (check, int32_t, TUNABLE_CALLBACK (set_mallopt_check));
+#else
+  const char *s = secure_getenv ("MALLOC_CHECK_");
+  if (s && s[0] != '\0' && s[0] != '0')
+    __malloc_debug_enable (MALLOC_CHECK_HOOK);
+#endif
+  return __is_malloc_debug_enabled (MALLOC_CHECK_HOOK);
+}
diff --git a/malloc/malloc-debug.c b/malloc/malloc-debug.c
index 9942124e02..f5290aaa6d 100644
--- a/malloc/malloc-debug.c
+++ b/malloc/malloc-debug.c
@@ -50,6 +50,7 @@ enum malloc_debug_hooks
   MALLOC_NONE_HOOK = 0,
   MALLOC_MCHECK_HOOK = 1 << 0, /* mcheck()  */
   MALLOC_MTRACE_HOOK = 1 << 1, /* mtrace()  */
+  MALLOC_CHECK_HOOK = 1 << 2,  /* MALLOC_CHECK_ or glibc.malloc.check.  */
 };
 static unsigned __malloc_debugging_hooks;
 
@@ -73,6 +74,7 @@ __malloc_debug_disable (enum malloc_debug_hooks flag)
 
 #include "mcheck.c"
 #include "mtrace.c"
+#include "malloc-check.c"
 
 extern void (*__malloc_initialize_hook) (void);
 compat_symbol_reference (libc, __malloc_initialize_hook,
@@ -105,13 +107,18 @@ generic_hook_ini (void)
   __malloc_hook = NULL;
   __realloc_hook = NULL;
   __memalign_hook = NULL;
-  /* The compiler does not know that these functions are allocators, so it will
-     not try to optimize it away.  */
-  __libc_free (__libc_malloc (0));
+
+  /* malloc check does not quite co-exist with libc malloc, so initialize
+     either on or the other.  */
+  if (!initialize_malloc_check ())
+    /* The compiler does not know that these functions are allocators, so it
+       will not try to optimize it away.  */
+    __libc_free (__libc_malloc (0));
 
   void (*hook) (void) = __malloc_initialize_hook;
   if (hook != NULL)
     (*hook)();
+
   debug_initialized = 1;
 }
 
@@ -149,10 +156,11 @@ __debug_malloc (size_t bytes)
 
   void *victim = NULL;
   size_t orig_bytes = bytes;
-  if (!__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK)
-      || !malloc_mcheck_before (&bytes, &victim))
+  if ((!__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK)
+       || !malloc_mcheck_before (&bytes, &victim)))
     {
-      victim = __libc_malloc (bytes);
+      victim = (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK)
+		? malloc_check (bytes) : __libc_malloc (bytes));
     }
   if (__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK) && victim != NULL)
     victim = malloc_mcheck_after (victim, orig_bytes);
@@ -175,10 +183,13 @@ __debug_free (void *mem)
 
   if (__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK))
     mem = free_mcheck (mem);
+
+  if (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))
+    free_check (mem);
+  else
+    __libc_free (mem);
   if (__is_malloc_debug_enabled (MALLOC_MTRACE_HOOK))
     free_mtrace (mem, RETURN_ADDRESS (0));
-
-  __libc_free (mem);
 }
 strong_alias (__debug_free, free)
 
@@ -193,10 +204,13 @@ __debug_realloc (void *oldmem, size_t bytes)
   size_t orig_bytes = bytes, oldsize = 0;
   void *victim = NULL;
 
-  if (!__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK)
-      || !realloc_mcheck_before (&oldmem, &bytes, &oldsize, &victim))
+  if ((!__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK)
+       || !realloc_mcheck_before (&oldmem, &bytes, &oldsize, &victim)))
     {
-      victim = __libc_realloc (oldmem, bytes);
+      if (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))
+	victim =  realloc_check (oldmem, bytes);
+      else
+	victim = __libc_realloc (oldmem, bytes);
     }
   if (__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK) && victim != NULL)
     victim = realloc_mcheck_after (victim, oldmem, orig_bytes,
@@ -219,10 +233,12 @@ _debug_mid_memalign (size_t alignment, size_t bytes, const void *address)
   void *victim = NULL;
   size_t orig_bytes = bytes;
 
-  if (!__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK)
-      || !memalign_mcheck_before (alignment, &bytes, &victim))
+  if ((!__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK)
+       || !memalign_mcheck_before (alignment, &bytes, &victim)))
     {
-      victim = __libc_memalign (alignment, bytes);
+      victim = (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK)
+		? memalign_check (alignment, bytes)
+		: __libc_memalign (alignment, bytes));
     }
   if (__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK) && victim != NULL)
     victim = memalign_mcheck_after (victim, alignment, orig_bytes);
@@ -316,10 +332,11 @@ __debug_calloc (size_t nmemb, size_t size)
   size_t orig_bytes = bytes;
   void *victim = NULL;
 
-  if (!__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK)
-      || !malloc_mcheck_before (&bytes, &victim))
+  if ((!__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK)
+       || !malloc_mcheck_before (&bytes, &victim)))
     {
-      victim = __libc_malloc (bytes);
+      victim = (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK)
+		? malloc_check (bytes) : __libc_malloc (bytes));
     }
   if (victim != NULL)
     {
@@ -333,3 +350,106 @@ __debug_calloc (size_t nmemb, size_t size)
   return victim;
 }
 strong_alias (__debug_calloc, calloc)
+
+size_t
+malloc_usable_size (void *mem)
+{
+  if (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))
+    return malloc_check_get_size (mem);
+
+  return musable (mem);
+}
+
+#define LIBC_SYMBOL(sym) libc_ ## sym
+#define SYMHANDLE(sym) sym ## _handle
+
+#define LOAD_SYM(sym) ({ \
+  static void *SYMHANDLE (sym);						      \
+  if (SYMHANDLE (sym) == NULL)						      \
+    SYMHANDLE (sym) = dlsym (RTLD_NEXT, #sym);				      \
+  SYMHANDLE (sym);							      \
+})
+
+int
+malloc_info (int options, FILE *fp)
+{
+  if (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))
+    return __malloc_info (options, fp);
+
+  int (*LIBC_SYMBOL (malloc_info)) (int, FILE *) = LOAD_SYM (malloc_info);
+  if (LIBC_SYMBOL (malloc_info) == NULL)
+    return -1;
+
+  return LIBC_SYMBOL (malloc_info) (options, fp);
+}
+
+int
+mallopt (int param_number, int value)
+{
+  if (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))
+    return __libc_mallopt (param_number, value);
+
+  int (*LIBC_SYMBOL (mallopt)) (int, int) = LOAD_SYM (mallopt);
+  if (LIBC_SYMBOL (mallopt) == NULL)
+    return 0;
+
+  return LIBC_SYMBOL (mallopt) (param_number, value);
+}
+
+void
+malloc_stats (void)
+{
+  if (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))
+    return __malloc_stats ();
+
+  void (*LIBC_SYMBOL (malloc_stats)) (void) = LOAD_SYM (malloc_stats);
+  if (LIBC_SYMBOL (malloc_stats) == NULL)
+    return;
+
+  LIBC_SYMBOL (malloc_stats) ();
+}
+
+struct mallinfo2
+mallinfo2 (void)
+{
+  if (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))
+    return __libc_mallinfo2 ();
+
+  struct mallinfo2 (*LIBC_SYMBOL (mallinfo2)) (void) = LOAD_SYM (mallinfo2);
+  if (LIBC_SYMBOL (mallinfo2) == NULL)
+    {
+      struct mallinfo2 ret = {0};
+      return ret;
+    }
+
+  return LIBC_SYMBOL (mallinfo2) ();
+}
+
+struct mallinfo
+mallinfo (void)
+{
+  if (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))
+    return __libc_mallinfo ();
+
+  struct mallinfo (*LIBC_SYMBOL (mallinfo)) (void) = LOAD_SYM (mallinfo);
+  if (LIBC_SYMBOL (mallinfo) == NULL)
+    {
+      struct mallinfo ret = {0};
+      return ret;
+    }
+
+  return LIBC_SYMBOL (mallinfo) ();
+}
+
+int
+malloc_trim (size_t s)
+{
+  if (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))
+    return __malloc_trim (s);
+
+  int (*LIBC_SYMBOL (malloc_trim)) (size_t) = LOAD_SYM (malloc_trim);
+  if (LIBC_SYMBOL (malloc_trim) == NULL)
+    return 0;
+
+  return LIBC_SYMBOL (malloc_trim) (s);
+}
diff --git a/malloc/malloc.c b/malloc/malloc.c
index ed0316e690..b8fcb2f2d3 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -288,6 +288,7 @@
 #define MALLOC_DEBUG 0
 #endif
 
+#if IS_IN (libc)
 #ifndef NDEBUG
 # define __assert_fail(assertion, file, line, function)			\
 	 __malloc_assert(assertion, file, line, function)
@@ -307,6 +308,7 @@ __malloc_assert (const char *assertion, const char *file, unsigned int line,
   abort ();
 }
 #endif
+#endif
 
 #if USE_TCACHE
 /* We want 64 entries.  This is an arbitrary limit, which tunables can reduce.  */
@@ -592,6 +594,7 @@ tag_at (void *ptr)
 
 /* ---------- description of public routines ------------ */
 
+#if IS_IN (libc)
 /*
   malloc(size_t n)
   Returns a pointer to a newly allocated chunk of at least n bytes, or null
@@ -815,6 +818,7 @@ void     __malloc_stats(void);
   POSIX wrapper like memalign(), checking for validity of size.
 */
 int      __posix_memalign(void **, size_t, size_t);
+#endif /* IS_IN (libc) */
 
 /* mallopt tuning options */
 
@@ -1106,24 +1110,17 @@ static void     _int_free(mstate, mchunkptr, int);
 static void*  _int_realloc(mstate, mchunkptr, INTERNAL_SIZE_T,
 			   INTERNAL_SIZE_T);
 static void*  _int_memalign(mstate, size_t, size_t);
+#if IS_IN (libc)
 static void*  _mid_memalign(size_t, size_t, void *);
+#endif
 
 static void malloc_printerr(const char *str) __attribute__ ((noreturn));
 
-static void* mem2mem_check(void *p, size_t sz);
-static void top_check(void);
 static void munmap_chunk(mchunkptr p);
 #if HAVE_MREMAP
 static mchunkptr mremap_chunk(mchunkptr p, size_t new_size);
 #endif
 
-static void*   malloc_check(size_t sz, const void *caller);
-static void      free_check(void* mem, const void *caller);
-static void*   realloc_check(void* oldmem, size_t bytes,
-			       const void *caller);
-static void*   memalign_check(size_t alignment, size_t bytes,
-				const void *caller);
-
 /* ------------------ MMAP support ------------------  */
 
 
@@ -2385,7 +2382,9 @@ do_check_malloc_state (mstate av)
 
 
 /* ----------------- Support for debugging hooks -------------------- */
+#if IS_IN (libc)
 #include "hooks.c"
+#endif
 
 
 /* ----------- Routines dealing with system allocation -------------- */
@@ -3186,6 +3185,7 @@ tcache_thread_shutdown (void)
 
 #endif /* !USE_TCACHE  */
 
+#if IS_IN (libc)
 void *
 __libc_malloc (size_t bytes)
 {
@@ -3686,6 +3686,7 @@ __libc_calloc (size_t n, size_t elem_size)
 
   return mem;
 }
+#endif /* IS_IN (libc) */
 
 /*
    ------------------------------ malloc ------------------------------
@@ -5054,9 +5055,6 @@ musable (void *mem)
 
       p = mem2chunk (mem);
 
-      if (__builtin_expect (using_malloc_checking == 1, 0))
-	return malloc_check_get_size (p);
-
       if (chunk_is_mmapped (p))
 	{
 	  if (DUMPED_MAIN_ARENA_CHUNK (p))
@@ -5072,7 +5070,7 @@ musable (void *mem)
   return 0;
 }
 
-
+#if IS_IN (libc)
 size_t
 __malloc_usable_size (void *m)
 {
@@ -5081,12 +5079,12 @@ __malloc_usable_size (void *m)
   result = musable (m);
   return result;
 }
+#endif
 
 /*
    ------------------------------ mallinfo ------------------------------
    Accumulate malloc statistics for arena AV into M.
  */
-
 static void
 int_mallinfo (mstate av, struct mallinfo2 *m)
 {
@@ -5585,10 +5583,15 @@ extern char **__libc_argv attribute_hidden;
 static void
 malloc_printerr (const char *str)
 {
+#if IS_IN (libc)
   __libc_message (do_abort, "%s\n", str);
+#else
+  __libc_fatal (str);
+#endif
   __builtin_unreachable ();
 }
 
+#if IS_IN (libc)
 /* We need a wrapper function for one of the additions of POSIX.  */
 int
 __posix_memalign (void **memptr, size_t alignment, size_t size)
@@ -5618,6 +5621,7 @@ __posix_memalign (void **memptr, size_t alignment, size_t size)
   return ENOMEM;
 }
 weak_alias (__posix_memalign, posix_memalign)
+#endif
 
 
 int
@@ -5821,9 +5825,9 @@ __malloc_info (int options, FILE *fp)
 
   return 0;
 }
+#if IS_IN (libc)
 weak_alias (__malloc_info, malloc_info)
 
-
 strong_alias (__libc_calloc, __calloc) weak_alias (__libc_calloc, calloc)
 strong_alias (__libc_free, __free) strong_alias (__libc_free, free)
 strong_alias (__libc_malloc, __malloc) strong_alias (__libc_malloc, malloc)
@@ -5841,6 +5845,7 @@ strong_alias (__libc_mallopt, __mallopt) weak_alias (__libc_mallopt, mallopt)
 weak_alias (__malloc_stats, malloc_stats)
 weak_alias (__malloc_usable_size, malloc_usable_size)
 weak_alias (__malloc_trim, malloc_trim)
+#endif
 
 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_26)
 compat_symbol (libc, __libc_free, cfree, GLIBC_2_0);