Radix cross Linux Toolchains

Toolchains for all supported by Radix cross Linux devices

80 Commits   2 Branches   13 Tags
    11         kx /* glibc.malloc.check implementation.
    11         kx    Copyright (C) 2001-2023 Free Software Foundation, Inc.
    11         kx    This file is part of the GNU C Library.
    11         kx 
    11         kx    The GNU C Library is free software; you can redistribute it and/or
    11         kx    modify it under the terms of the GNU Lesser General Public License as
    11         kx    published by the Free Software Foundation; either version 2.1 of the
    11         kx    License, or (at your option) any later version.
    11         kx 
    11         kx    The GNU C Library is distributed in the hope that it will be useful,
    11         kx    but WITHOUT ANY WARRANTY; without even the implied warranty of
    11         kx    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    11         kx    Lesser General Public License for more details.
    11         kx 
    11         kx    You should have received a copy of the GNU Lesser General Public
    11         kx    License along with the GNU C Library; see the file COPYING.LIB.  If
    11         kx    not, see <https://www.gnu.org/licenses/>.  */
    11         kx 
    11         kx #define __mremap mremap
    11         kx #include "malloc.c"
    11         kx 
    11         kx /* When memory is tagged, the checking data is stored in the user part
    11         kx    of the chunk.  We can't rely on the user not having modified the
    11         kx    tags, so fetch the tag at each location before dereferencing
    11         kx    it.  */
    11         kx #define SAFE_CHAR_OFFSET(p,offset) \
    11         kx   ((unsigned char *) tag_at (((unsigned char *) p) + offset))
    11         kx 
    11         kx /* A simple, standard set of debugging hooks.  Overhead is `only' one
    11         kx    byte per chunk; still this will catch most cases of double frees or
    11         kx    overruns.  The goal here is to avoid obscure crashes due to invalid
    11         kx    usage, unlike in the MALLOC_DEBUG code. */
    11         kx 
    11         kx static unsigned char
    11         kx magicbyte (const void *p)
    11         kx {
    11         kx   unsigned char magic;
    11         kx 
    11         kx   magic = (((uintptr_t) p >> 3) ^ ((uintptr_t) p >> 11)) & 0xFF;
    11         kx   /* Do not return 1.  See the comment in mem2mem_check().  */
    11         kx   if (magic == 1)
    11         kx     ++magic;
    11         kx   return magic;
    11         kx }
    11         kx 
    11         kx /* Visualize the chunk as being partitioned into blocks of 255 bytes from the
    11         kx    highest address of the chunk, downwards.  The end of each block tells
    11         kx    us the size of that block, up to the actual size of the requested
    11         kx    memory.  Our magic byte is right at the end of the requested size, so we
    11         kx    must reach it with this iteration, otherwise we have witnessed a memory
    11         kx    corruption.  */
    11         kx static size_t
    11         kx malloc_check_get_size (void *mem)
    11         kx {
    11         kx   size_t size;
    11         kx   unsigned char c;
    11         kx   mchunkptr p = mem2chunk (mem);
    11         kx   unsigned char magic = magicbyte (p);
    11         kx 
    11         kx   for (size = CHUNK_HDR_SZ + memsize (p) - 1;
    11         kx        (c = *SAFE_CHAR_OFFSET (p, size)) != magic;
    11         kx        size -= c)
    11         kx     {
    11         kx       if (c <= 0 || size < (c + CHUNK_HDR_SZ))
    11         kx 	malloc_printerr ("malloc_check_get_size: memory corruption");
    11         kx     }
    11         kx 
    11         kx   /* chunk2mem size.  */
    11         kx   return size - CHUNK_HDR_SZ;
    11         kx }
    11         kx 
    11         kx /* Instrument a chunk with overrun detector byte(s) and convert it
    11         kx    into a user pointer with requested size req_sz. */
    11         kx 
    11         kx static void *
    11         kx mem2mem_check (void *ptr, size_t req_sz)
    11         kx {
    11         kx   mchunkptr p;
    11         kx   unsigned char *m_ptr = ptr;
    11         kx   size_t max_sz, block_sz, i;
    11         kx   unsigned char magic;
    11         kx 
    11         kx   if (!ptr)
    11         kx     return ptr;
    11         kx 
    11         kx   p = mem2chunk (ptr);
    11         kx   magic = magicbyte (p);
    11         kx   max_sz = memsize (p);
    11         kx 
    11         kx   for (i = max_sz - 1; i > req_sz; i -= block_sz)
    11         kx     {
    11         kx       block_sz = MIN (i - req_sz, 0xff);
    11         kx       /* Don't allow the magic byte to appear in the chain of length bytes.
    11         kx          For the following to work, magicbyte cannot return 0x01.  */
    11         kx       if (block_sz == magic)
    11         kx         --block_sz;
    11         kx 
    11         kx       *SAFE_CHAR_OFFSET (m_ptr, i) = block_sz;
    11         kx     }
    11         kx   *SAFE_CHAR_OFFSET (m_ptr, req_sz) = magic;
    11         kx   return (void *) m_ptr;
    11         kx }
    11         kx 
    11         kx /* Convert a pointer to be free()d or realloc()ed to a valid chunk
    11         kx    pointer.  If the provided pointer is not valid, return NULL. */
    11         kx 
    11         kx static mchunkptr
    11         kx mem2chunk_check (void *mem, unsigned char **magic_p)
    11         kx {
    11         kx   mchunkptr p;
    11         kx   INTERNAL_SIZE_T sz, c;
    11         kx   unsigned char magic;
    11         kx 
    11         kx   if (!aligned_OK (mem))
    11         kx     return NULL;
    11         kx 
    11         kx   p = mem2chunk (mem);
    11         kx   sz = chunksize (p);
    11         kx   magic = magicbyte (p);
    11         kx   if (!chunk_is_mmapped (p))
    11         kx     {
    11         kx       /* Must be a chunk in conventional heap memory. */
    11         kx       int contig = contiguous (&main_arena);
    11         kx       if ((contig &&
    11         kx            ((char *) p < mp_.sbrk_base ||
    11         kx             ((char *) p + sz) >= (mp_.sbrk_base + main_arena.system_mem))) ||
    11         kx           sz < MINSIZE || sz & MALLOC_ALIGN_MASK || !inuse (p) ||
    11         kx           (!prev_inuse (p) && ((prev_size (p) & MALLOC_ALIGN_MASK) != 0 ||
    11         kx                                (contig && (char *) prev_chunk (p) < mp_.sbrk_base) ||
    11         kx                                next_chunk (prev_chunk (p)) != p)))
    11         kx         return NULL;
    11         kx 
    11         kx       for (sz = CHUNK_HDR_SZ + memsize (p) - 1;
    11         kx 	   (c = *SAFE_CHAR_OFFSET (p, sz)) != magic;
    11         kx 	   sz -= c)
    11         kx         {
    11         kx           if (c == 0 || sz < (c + CHUNK_HDR_SZ))
    11         kx             return NULL;
    11         kx         }
    11         kx     }
    11         kx   else
    11         kx     {
    11         kx       unsigned long offset, page_mask = GLRO (dl_pagesize) - 1;
    11         kx 
    11         kx       /* mmap()ed chunks have MALLOC_ALIGNMENT or higher power-of-two
    11         kx          alignment relative to the beginning of a page.  Check this
    11         kx          first. */
    11         kx       offset = (unsigned long) mem & page_mask;
    11         kx       if ((offset != MALLOC_ALIGNMENT && offset != 0 && offset != 0x10 &&
    11         kx            offset != 0x20 && offset != 0x40 && offset != 0x80 && offset != 0x100 &&
    11         kx            offset != 0x200 && offset != 0x400 && offset != 0x800 && offset != 0x1000 &&
    11         kx            offset < 0x2000) ||
    11         kx           !chunk_is_mmapped (p) || prev_inuse (p) ||
    11         kx           ((((unsigned long) p - prev_size (p)) & page_mask) != 0) ||
    11         kx           ((prev_size (p) + sz) & page_mask) != 0)
    11         kx         return NULL;
    11         kx 
    11         kx       for (sz = CHUNK_HDR_SZ + memsize (p) - 1;
    11         kx 	   (c = *SAFE_CHAR_OFFSET (p, sz)) != magic;
    11         kx 	   sz -= c)
    11         kx         {
    11         kx           if (c == 0 || sz < (c + CHUNK_HDR_SZ))
    11         kx             return NULL;
    11         kx         }
    11         kx     }
    11         kx 
    11         kx   unsigned char* safe_p = SAFE_CHAR_OFFSET (p, sz);
    11         kx   *safe_p ^= 0xFF;
    11         kx   if (magic_p)
    11         kx     *magic_p = safe_p;
    11         kx   return p;
    11         kx }
    11         kx 
    11         kx /* Check for corruption of the top chunk.  */
    11         kx static void
    11         kx top_check (void)
    11         kx {
    11         kx   mchunkptr t = top (&main_arena);
    11         kx 
    11         kx   if (t == initial_top (&main_arena) ||
    11         kx       (!chunk_is_mmapped (t) &&
    11         kx        chunksize (t) >= MINSIZE &&
    11         kx        prev_inuse (t) &&
    11         kx        (!contiguous (&main_arena) ||
    11         kx         (char *) t + chunksize (t) == mp_.sbrk_base + main_arena.system_mem)))
    11         kx     return;
    11         kx 
    11         kx   malloc_printerr ("malloc: top chunk is corrupt");
    11         kx }
    11         kx 
    11         kx static void *
    11         kx malloc_check (size_t sz)
    11         kx {
    11         kx   void *victim;
    11         kx   size_t nb;
    11         kx 
    11         kx   if (__builtin_add_overflow (sz, 1, &nb))
    11         kx     {
    11         kx       __set_errno (ENOMEM);
    11         kx       return NULL;
    11         kx     }
    11         kx 
    11         kx   __libc_lock_lock (main_arena.mutex);
    11         kx   top_check ();
    11         kx   victim = _int_malloc (&main_arena, nb);
    11         kx   __libc_lock_unlock (main_arena.mutex);
    11         kx   return mem2mem_check (tag_new_usable (victim), sz);
    11         kx }
    11         kx 
    11         kx static void
    11         kx free_check (void *mem)
    11         kx {
    11         kx   mchunkptr p;
    11         kx 
    11         kx   if (!mem)
    11         kx     return;
    11         kx 
    11         kx   int err = errno;
    11         kx 
    11         kx   /* Quickly check that the freed pointer matches the tag for the memory.
    11         kx      This gives a useful double-free detection.  */
    11         kx   if (__glibc_unlikely (mtag_enabled))
    11         kx     *(volatile char *)mem;
    11         kx 
    11         kx   __libc_lock_lock (main_arena.mutex);
    11         kx   p = mem2chunk_check (mem, NULL);
    11         kx   if (!p)
    11         kx     malloc_printerr ("free(): invalid pointer");
    11         kx   if (chunk_is_mmapped (p))
    11         kx     {
    11         kx       __libc_lock_unlock (main_arena.mutex);
    11         kx       munmap_chunk (p);
    11         kx     }
    11         kx   else
    11         kx     {
    11         kx       /* Mark the chunk as belonging to the library again.  */
    11         kx       (void)tag_region (chunk2mem (p), memsize (p));
    11         kx       _int_free (&main_arena, p, 1);
    11         kx       __libc_lock_unlock (main_arena.mutex);
    11         kx     }
    11         kx   __set_errno (err);
    11         kx }
    11         kx 
    11         kx static void *
    11         kx realloc_check (void *oldmem, size_t bytes)
    11         kx {
    11         kx   INTERNAL_SIZE_T chnb;
    11         kx   void *newmem = 0;
    11         kx   unsigned char *magic_p = NULL;
    11         kx   size_t rb;
    11         kx 
    11         kx   if (__builtin_add_overflow (bytes, 1, &rb))
    11         kx     {
    11         kx       __set_errno (ENOMEM);
    11         kx       return NULL;
    11         kx     }
    11         kx   if (oldmem == 0)
    11         kx     return malloc_check (bytes);
    11         kx 
    11         kx   if (bytes == 0)
    11         kx     {
    11         kx       free_check (oldmem);
    11         kx       return NULL;
    11         kx     }
    11         kx 
    11         kx   /* Quickly check that the freed pointer matches the tag for the memory.
    11         kx      This gives a useful double-free detection.  */
    11         kx   if (__glibc_unlikely (mtag_enabled))
    11         kx     *(volatile char *)oldmem;
    11         kx 
    11         kx   __libc_lock_lock (main_arena.mutex);
    11         kx   const mchunkptr oldp = mem2chunk_check (oldmem, &magic_p);
    11         kx   __libc_lock_unlock (main_arena.mutex);
    11         kx   if (!oldp)
    11         kx     malloc_printerr ("realloc(): invalid pointer");
    11         kx   const INTERNAL_SIZE_T oldsize = chunksize (oldp);
    11         kx 
    11         kx   chnb = checked_request2size (rb);
    11         kx   if (chnb == 0)
    11         kx     {
    11         kx       __set_errno (ENOMEM);
    11         kx       goto invert;
    11         kx     }
    11         kx 
    11         kx   __libc_lock_lock (main_arena.mutex);
    11         kx 
    11         kx   if (chunk_is_mmapped (oldp))
    11         kx     {
    11         kx #if HAVE_MREMAP
    11         kx       mchunkptr newp = mremap_chunk (oldp, chnb);
    11         kx       if (newp)
    11         kx         newmem = chunk2mem_tag (newp);
    11         kx       else
    11         kx #endif
    11         kx       {
    11         kx 	/* Note the extra SIZE_SZ overhead. */
    11         kx         if (oldsize - SIZE_SZ >= chnb)
    11         kx           newmem = oldmem; /* do nothing */
    11         kx         else
    11         kx           {
    11         kx             /* Must alloc, copy, free. */
    11         kx 	    top_check ();
    11         kx 	    newmem = _int_malloc (&main_arena, rb);
    11         kx             if (newmem)
    11         kx               {
    11         kx                 memcpy (newmem, oldmem, oldsize - CHUNK_HDR_SZ);
    11         kx                 munmap_chunk (oldp);
    11         kx               }
    11         kx           }
    11         kx       }
    11         kx     }
    11         kx   else
    11         kx     {
    11         kx       top_check ();
    11         kx       newmem = _int_realloc (&main_arena, oldp, oldsize, chnb);
    11         kx     }
    11         kx 
    11         kx   DIAG_PUSH_NEEDS_COMMENT;
    11         kx #if __GNUC_PREREQ (7, 0)
    11         kx   /* GCC 7 warns about magic_p may be used uninitialized.  But we never
    11         kx      reach here if magic_p is uninitialized.  */
    11         kx   DIAG_IGNORE_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
    11         kx #endif
    11         kx   /* mem2chunk_check changed the magic byte in the old chunk.
    11         kx      If newmem is NULL, then the old chunk will still be used though,
    11         kx      so we need to invert that change here.  */
    11         kx invert:
    11         kx   if (newmem == NULL)
    11         kx     *magic_p ^= 0xFF;
    11         kx   DIAG_POP_NEEDS_COMMENT;
    11         kx 
    11         kx   __libc_lock_unlock (main_arena.mutex);
    11         kx 
    11         kx   return mem2mem_check (tag_new_usable (newmem), bytes);
    11         kx }
    11         kx 
    11         kx static void *
    11         kx memalign_check (size_t alignment, size_t bytes)
    11         kx {
    11         kx   void *mem;
    11         kx 
    11         kx   if (alignment <= MALLOC_ALIGNMENT)
    11         kx     return malloc_check (bytes);
    11         kx 
    11         kx   if (alignment < MINSIZE)
    11         kx     alignment = MINSIZE;
    11         kx 
    11         kx   /* If the alignment is greater than SIZE_MAX / 2 + 1 it cannot be a
    11         kx      power of 2 and will cause overflow in the check below.  */
    11         kx   if (alignment > SIZE_MAX / 2 + 1)
    11         kx     {
    11         kx       __set_errno (EINVAL);
    11         kx       return NULL;
    11         kx     }
    11         kx 
    11         kx   /* Check for overflow.  */
    11         kx   if (bytes > SIZE_MAX - alignment - MINSIZE)
    11         kx     {
    11         kx       __set_errno (ENOMEM);
    11         kx       return NULL;
    11         kx     }
    11         kx 
    11         kx   /* Make sure alignment is power of 2.  */
    11         kx   if (!powerof2 (alignment))
    11         kx     {
    11         kx       size_t a = MALLOC_ALIGNMENT * 2;
    11         kx       while (a < alignment)
    11         kx         a <<= 1;
    11         kx       alignment = a;
    11         kx     }
    11         kx 
    11         kx   __libc_lock_lock (main_arena.mutex);
    11         kx   top_check ();
    11         kx   mem = _int_memalign (&main_arena, alignment, bytes + 1);
    11         kx   __libc_lock_unlock (main_arena.mutex);
    11         kx   return mem2mem_check (tag_new_usable (mem), bytes);
    11         kx }
    11         kx 
    11         kx #if HAVE_TUNABLES
    11         kx static void
    11         kx TUNABLE_CALLBACK (set_mallopt_check) (tunable_val_t *valp)
    11         kx {
    11         kx   int32_t value = (int32_t) valp->numval;
    11         kx   if (value != 0)
    11         kx     __malloc_debug_enable (MALLOC_CHECK_HOOK);
    11         kx }
    11         kx #endif
    11         kx 
    11         kx static bool
    11         kx initialize_malloc_check (void)
    11         kx {
    11         kx   /* This is the copy of the malloc initializer that we pulled in along with
    11         kx      malloc-check.  This does not affect any of the libc malloc structures.  */
    11         kx   ptmalloc_init ();
    11         kx #if HAVE_TUNABLES
    11         kx   TUNABLE_GET (check, int32_t, TUNABLE_CALLBACK (set_mallopt_check));
    11         kx #else
    11         kx   const char *s = secure_getenv ("MALLOC_CHECK_");
    11         kx   if (s && s[0] != '\0' && s[0] != '0')
    11         kx     __malloc_debug_enable (MALLOC_CHECK_HOOK);
    11         kx #endif
    11         kx   return __is_malloc_debug_enabled (MALLOC_CHECK_HOOK);
    11         kx }