summaryrefslogtreecommitdiff
path: root/lib/malloc/malloc.c
diff options
context:
space:
mode:
authorChet Ramey <chet.ramey@case.edu>2020-12-06 15:51:17 -0500
committerChet Ramey <chet.ramey@case.edu>2020-12-06 15:51:17 -0500
commit8868edaf2250e09c4e9a1c75ffe3274f28f38581 (patch)
treed9a7812ab6cd2f45c5021755c4c094b19dab1b51 /lib/malloc/malloc.c
parent36f2c406ff27995392a9247dfa90672fdaf7dc43 (diff)
downloadbash-8868edaf2250e09c4e9a1c75ffe3274f28f38581.tar.gz
bash-5.1 distribution sources and documentationbash-5.1
Diffstat (limited to 'lib/malloc/malloc.c')
-rw-r--r--lib/malloc/malloc.c111
1 files changed, 85 insertions, 26 deletions
diff --git a/lib/malloc/malloc.c b/lib/malloc/malloc.c
index 5621adf..439f8ef 100644
--- a/lib/malloc/malloc.c
+++ b/lib/malloc/malloc.c
@@ -1,6 +1,6 @@
/* malloc.c - dynamic memory allocation for bash. */
-/* Copyright (C) 1985-2005 Free Software Foundation, Inc.
+/* Copyright (C) 1985-2020 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne-Again SHell.
@@ -25,6 +25,8 @@
*
* Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
*
+ * [VERY] old explanation:
+ *
* This is a very fast storage allocator. It allocates blocks of a small
* number of different sizes, and keeps free lists of each size. Blocks
* that don't exactly fit are passed up to the next larger size. In this
@@ -84,6 +86,10 @@
#include <errno.h>
#include <stdio.h>
+#if !defined (botch)
+#include <stdlib.h>
+#endif
+
#if defined (HAVE_MMAP)
#include <sys/mman.h>
#endif
@@ -134,22 +140,35 @@
enough room in the block for the new size. Range checking is always
done. */
union mhead {
+#if SIZEOF_CHAR_P == 8
+ bits64_t mh_align[2]; /* 16 */
+#else
bits64_t mh_align; /* 8 */
+#endif
struct {
char mi_alloc; /* ISALLOC or ISFREE */ /* 1 */
char mi_index; /* index in nextf[] */ /* 1 */
/* Remainder are valid only when block is allocated */
u_bits16_t mi_magic2; /* should be == MAGIC2 */ /* 2 */
u_bits32_t mi_nbytes; /* # of bytes allocated */ /* 4 */
+#if SIZEOF_CHAR_P == 8
+ char mi_magic8[8]; /* MAGIC1 guard bytes */ /* 8 */
+#endif
} minfo;
};
#define mh_alloc minfo.mi_alloc
#define mh_index minfo.mi_index
#define mh_nbytes minfo.mi_nbytes
#define mh_magic2 minfo.mi_magic2
+#define mh_magic8 minfo.mi_magic8
#define MOVERHEAD sizeof(union mhead)
+
+#if SIZEOF_CHAR_P == 8
+#define MALIGN_MASK 15
+#else
#define MALIGN_MASK 7 /* one less than desired alignment */
+#endif
typedef union _malloc_guard {
char s[4];
@@ -163,6 +182,8 @@ typedef union _malloc_guard {
to describe the overhead for when the block is in use,
and we do not want the free-list pointer to count in that. */
+/* If SIZEOF_CHAR_P == 8, this goes into the mh_magic8 buffer at the end of
+ the rest of the struct. This may need adjusting. */
#define CHAIN(a) \
(*(union mhead **) (sizeof (char *) + (char *) (a)))
@@ -170,13 +191,14 @@ typedef union _malloc_guard {
and end of each allocated block, and make sure they are undisturbed
whenever a free or a realloc occurs. */
-/* Written in the 2 bytes before the block's real space (-4 bytes) */
+/* Written in the bytes before the block's real space (-SIZEOF_CHAR_P bytes) */
+#define MAGIC1 0x55
#define MAGIC2 0x5555
#define MSLOP 4 /* 4 bytes extra for u_bits32_t size */
/* How many bytes are actually allocated for a request of size N --
- rounded up to nearest multiple of 8 after accounting for malloc
- overhead. */
+ rounded up to nearest multiple of 2*SIZEOF_CHAR_P after accounting for
+ malloc overhead. */
#define ALLOCATED_BYTES(n) \
(((n) + MOVERHEAD + MSLOP + MALIGN_MASK) & ~MALIGN_MASK)
@@ -204,12 +226,12 @@ typedef union _malloc_guard {
/* Should we use mmap for large allocations? */
#if defined (HAVE_MMAP)
-# if !defined (MAP_ANON) && defined (MAP_ANONYMOUS)
-# define MAP_ANON MAP_ANONYMOUS
+# if defined (MAP_ANON) && !defined (MAP_ANONYMOUS)
+# define MAP_ANONYMOUS MAP_ANON
# endif
#endif
-#if defined (HAVE_MMAP) && defined (MAP_ANON)
+#if defined (HAVE_MMAP) && defined (MAP_ANONYMOUS)
# define USE_MMAP
#endif
@@ -273,32 +295,32 @@ extern int errno;
#endif
/* Declarations for internal functions */
-static PTR_T internal_malloc __P((size_t, const char *, int, int));
-static PTR_T internal_realloc __P((PTR_T, size_t, const char *, int, int));
-static void internal_free __P((PTR_T, const char *, int, int));
-static PTR_T internal_memalign __P((size_t, size_t, const char *, int, int));
+static PTR_T internal_malloc PARAMS((size_t, const char *, int, int));
+static PTR_T internal_realloc PARAMS((PTR_T, size_t, const char *, int, int));
+static void internal_free PARAMS((PTR_T, const char *, int, int));
+static PTR_T internal_memalign PARAMS((size_t, size_t, const char *, int, int));
#ifndef NO_CALLOC
-static PTR_T internal_calloc __P((size_t, size_t, const char *, int, int));
-static void internal_cfree __P((PTR_T, const char *, int, int));
+static PTR_T internal_calloc PARAMS((size_t, size_t, const char *, int, int));
+static void internal_cfree PARAMS((PTR_T, const char *, int, int));
#endif
#ifndef NO_VALLOC
-static PTR_T internal_valloc __P((size_t, const char *, int, int));
+static PTR_T internal_valloc PARAMS((size_t, const char *, int, int));
#endif
#if defined (botch)
extern void botch ();
#else
-static void botch __P((const char *, const char *, int));
+static void botch PARAMS((const char *, const char *, int));
#endif
-static void xbotch __P((PTR_T, int, const char *, const char *, int));
+static void xbotch PARAMS((PTR_T, int, const char *, const char *, int));
#if !HAVE_DECL_SBRK
extern char *sbrk ();
#endif /* !HAVE_DECL_SBRK */
#ifdef SHELL
-extern int interrupt_immediately, running_trap;
-extern int signal_is_trapped __P((int));
+extern int running_trap;
+extern int signal_is_trapped PARAMS((int));
#endif
#ifdef MALLOC_STATS
@@ -317,8 +339,8 @@ int malloc_mmap_threshold = MMAP_THRESHOLD;
char _malloc_trace_buckets[NBUCKETS];
/* These should really go into a header file. */
-extern void mtrace_alloc __P((const char *, PTR_T, size_t, const char *, int));
-extern void mtrace_free __P((PTR_T, int, const char *, int));
+extern void mtrace_alloc PARAMS((const char *, PTR_T, size_t, const char *, int));
+extern void mtrace_free PARAMS((PTR_T, int, const char *, int));
#endif
#if !defined (botch)
@@ -598,9 +620,9 @@ morecore (nu)
blocked_sigs = 0;
#ifdef SHELL
# if defined (SIGCHLD)
- if (interrupt_immediately || running_trap || signal_is_trapped (SIGINT) || signal_is_trapped (SIGCHLD))
+ if (running_trap || signal_is_trapped (SIGINT) || signal_is_trapped (SIGCHLD))
# else
- if (interrupt_immediately || running_trap || signal_is_trapped (SIGINT))
+ if (running_trap || signal_is_trapped (SIGINT))
# endif
#endif
{
@@ -689,7 +711,7 @@ morecore (nu)
memtop += sbrk_amt;
- /* shouldn't happen, but just in case -- require 8-byte alignment */
+ /* shouldn't happen, but just in case -- require 8- or 16-byte alignment */
if ((long)mp & MALIGN_MASK)
{
mp = (union mhead *) (((long)mp + MALIGN_MASK) & ~MALIGN_MASK);
@@ -719,8 +741,13 @@ malloc_debug_dummy ()
write (1, "malloc_debug_dummy\n", 19);
}
+#if SIZEOF_CHAR_P == 8
+#define PREPOP_BIN 3
+#define PREPOP_SIZE 64
+#else
#define PREPOP_BIN 2
#define PREPOP_SIZE 32
+#endif
static int
pagealign ()
@@ -756,8 +783,8 @@ pagealign ()
memtop += sbrk_needed;
/* Take the memory which would otherwise be wasted and populate the most
- popular bin (2 == 32 bytes) with it. Add whatever we need to curbrk
- to make things 32-byte aligned, compute how many 32-byte chunks we're
+ popular bin (3 == 64 bytes) with it. Add whatever we need to curbrk
+ to make things 64-byte aligned, compute how many 64-byte chunks we're
going to get, and set up the bin. */
curbrk += sbrk_needed & (PREPOP_SIZE - 1);
sbrk_needed -= sbrk_needed & (PREPOP_SIZE - 1);
@@ -818,7 +845,7 @@ internal_malloc (n, file, line, flags) /* get a block */
if (nbytes <= binsize(nunits))
break;
- /* Silently reject too-large requests. */
+ /* Silently reject too-large requests. XXX - can increase this if HAVE_MMAP */
if (nunits >= NBUCKETS)
return ((PTR_T) NULL);
@@ -859,6 +886,11 @@ internal_malloc (n, file, line, flags) /* get a block */
p->mh_magic2 = MAGIC2;
p->mh_nbytes = n;
+#if SIZEOF_CHAR_P == 8
+ /* Begin guard */
+ MALLOC_MEMSET ((char *)p->mh_magic8, MAGIC1, 8);
+#endif
+
/* End guard */
mg.i = n;
z = mg.s;
@@ -893,6 +925,14 @@ internal_malloc (n, file, line, flags) /* get a block */
_malloc_ckwatch (p + 1, file, line, W_ALLOC, n);
#endif
+#if defined (MALLOC_DEBUG)
+ z = (char *) (p + 1);
+ /* Check alignment of returned pointer */
+ if ((unsigned long)z & MALIGN_MASK)
+ fprintf (stderr, "malloc: %s:%d: warning: request for %d bytes not aligned on %d byte boundary\r\n",
+ file ? file : _("unknown"), line, p->mh_nbytes, MALIGN_MASK+1);
+#endif
+
return (PTR_T) (p + 1);
}
@@ -952,6 +992,15 @@ internal_free (mem, file, line, flags)
if (IN_BUCKET(nbytes, nunits) == 0)
xbotch (mem, ERR_UNDERFLOW,
_("free: underflow detected; mh_nbytes out of range"), file, line);
+#if SIZEOF_CHAR_P == 8
+ {
+ int i;
+ for (i = 0, z = p->mh_magic8; i < 8; i++)
+ if (*z++ != MAGIC1)
+ xbotch (mem, ERR_UNDERFLOW,
+ _("free: underflow detected; magic8 corrupted"), file, line);
+ }
+#endif
ap += p->mh_nbytes;
z = mg.s;
@@ -1083,6 +1132,16 @@ internal_realloc (mem, n, file, line, flags)
if (IN_BUCKET(nbytes, nunits) == 0)
xbotch (mem, ERR_UNDERFLOW,
_("realloc: underflow detected; mh_nbytes out of range"), file, line);
+#if SIZEOF_CHAR_P == 8
+ {
+ int i;
+ for (i = 0, z = p->mh_magic8; i < 8; i++)
+ if (*z++ != MAGIC1)
+ xbotch (mem, ERR_UNDERFLOW,
+ _("realloc: underflow detected; magic8 corrupted"), file, line);
+
+ }
+#endif
m = (char *)mem + (tocopy = p->mh_nbytes);
z = mg.s;