diff options
Diffstat (limited to 'lib/malloc')
-rw-r--r-- | lib/malloc/imalloc.h | 12 | ||||
-rw-r--r-- | lib/malloc/malloc.c | 111 | ||||
-rw-r--r-- | lib/malloc/mstats.h | 10 | ||||
-rw-r--r-- | lib/malloc/shmalloc.h | 36 | ||||
-rw-r--r-- | lib/malloc/stats.c | 8 | ||||
-rw-r--r-- | lib/malloc/table.c | 12 | ||||
-rw-r--r-- | lib/malloc/table.h | 20 | ||||
-rw-r--r-- | lib/malloc/trace.c | 4 | ||||
-rw-r--r-- | lib/malloc/watch.h | 4 |
9 files changed, 138 insertions, 79 deletions
diff --git a/lib/malloc/imalloc.h b/lib/malloc/imalloc.h index 82b09eba..d07adac2 100644 --- a/lib/malloc/imalloc.h +++ b/lib/malloc/imalloc.h @@ -1,6 +1,6 @@ /* imalloc.h -- internal malloc definitions shared by source files. */ -/* Copyright (C) 2001-2003 Free Software Foundation, Inc. +/* Copyright (C) 2001-2020 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -67,11 +67,11 @@ # endif /* HAVE_BCOPY */ #endif /* !__GNUC__ */ -#if !defined (__P) +#if !defined (PARAMS) # if defined (__STDC__) || defined (__GNUC__) || defined (__cplusplus) || defined (PROTOTYPES) -# define __P(protos) protos +# define PARAMS(protos) protos # else -# define __P(protos) () +# define PARAMS(protos) () # endif #endif @@ -167,7 +167,7 @@ do { \ #include <signal.h> -extern void _malloc_block_signals __P((sigset_t *, sigset_t *)); -extern void _malloc_unblock_signals __P((sigset_t *, sigset_t *)); +extern void _malloc_block_signals PARAMS((sigset_t *, sigset_t *)); +extern void _malloc_unblock_signals PARAMS((sigset_t *, sigset_t *)); #endif /* _IMALLOC_H */ diff --git a/lib/malloc/malloc.c b/lib/malloc/malloc.c index 5621adf4..439f8ef1 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; diff --git a/lib/malloc/mstats.h b/lib/malloc/mstats.h index dac02980..ce8aaeca 100644 --- a/lib/malloc/mstats.h +++ b/lib/malloc/mstats.h @@ -1,6 +1,6 @@ /* mstats.h - definitions for malloc statistics */ -/* Copyright (C) 2001-2003 Free Software Foundation, Inc. +/* Copyright (C) 2001-2020 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne-Again SHell. @@ -104,10 +104,10 @@ struct bucket_stats { int nmmap; /* currently unused */ }; -extern struct bucket_stats malloc_bucket_stats __P((int)); -extern struct _malstats malloc_stats __P((void)); -extern void print_malloc_stats __P((char *)); -extern void trace_malloc_stats __P((char *, char *)); +extern struct bucket_stats malloc_bucket_stats PARAMS((int)); +extern struct _malstats malloc_stats PARAMS((void)); +extern void print_malloc_stats PARAMS((char *)); +extern void trace_malloc_stats PARAMS((char *, char *)); #endif /* MALLOC_STATS */ diff --git a/lib/malloc/shmalloc.h b/lib/malloc/shmalloc.h index 2dac6154..d51193eb 100644 --- a/lib/malloc/shmalloc.h +++ b/lib/malloc/shmalloc.h @@ -1,6 +1,6 @@ /* Functions (currently) for use by the shell to do malloc debugging and tracking. */ -/* Copyright (C) 2001-2003 Free Software Foundation, Inc. +/* Copyright (C) 2001-2020 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne-Again SHell. @@ -21,11 +21,11 @@ #ifndef _SH_MALLOC_H #define _SH_MALLOC_H -#ifndef __P +#ifndef PARAMS # if defined (__STDC__) || defined (__GNUC__) || defined (__cplusplus) -# define __P(protos) protos +# define PARAMS(protos) protos # else -# define __P(protos) () +# define PARAMS(protos) () # endif #endif @@ -41,30 +41,30 @@ #endif /* PTR_T */ -extern PTR_T sh_malloc __P((size_t, const char *, int)); -extern PTR_T sh_realloc __P((PTR_T, size_t, const char *, int)); -extern void sh_free __P((PTR_T, const char *, int)); +extern PTR_T sh_malloc PARAMS((size_t, const char *, int)); +extern PTR_T sh_realloc PARAMS((PTR_T, size_t, const char *, int)); +extern void sh_free PARAMS((PTR_T, const char *, int)); -extern PTR_T sh_memalign __P((size_t, size_t, const char *, int)); +extern PTR_T sh_memalign PARAMS((size_t, size_t, const char *, int)); -extern PTR_T sh_calloc __P((size_t, size_t, const char *, int)); -extern void sh_cfree __P((PTR_T, const char *, int)); +extern PTR_T sh_calloc PARAMS((size_t, size_t, const char *, int)); +extern void sh_cfree PARAMS((PTR_T, const char *, int)); -extern PTR_T sh_valloc __P((size_t, const char *, int)); +extern PTR_T sh_valloc PARAMS((size_t, const char *, int)); /* trace.c */ -extern int malloc_set_trace __P((int)); +extern int malloc_set_trace PARAMS((int)); extern void malloc_set_tracefp (); /* full prototype requires stdio.h */ -extern void malloc_set_tracefn __P((char *, char *)); +extern void malloc_set_tracefn PARAMS((char *, char *)); /* table.c */ -extern void mregister_dump_table __P((void)); -extern void mregister_table_init __P((void)); -extern int malloc_set_register __P((int)); +extern void mregister_dump_table PARAMS((void)); +extern void mregister_table_init PARAMS((void)); +extern int malloc_set_register PARAMS((int)); /* stats.c */ -extern void print_malloc_stats __P((char *)); +extern void print_malloc_stats PARAMS((char *)); extern void fprint_malloc_stats (); /* full prototype requires stdio.h */ -extern void trace_malloc_stats __P((char *, char *)); +extern void trace_malloc_stats PARAMS((char *, char *)); #endif diff --git a/lib/malloc/stats.c b/lib/malloc/stats.c index 05247970..b38df9f4 100644 --- a/lib/malloc/stats.c +++ b/lib/malloc/stats.c @@ -1,6 +1,6 @@ /* stats.c - malloc statistics */ -/* Copyright (C) 2001-2003 Free Software Foundation, Inc. +/* Copyright (C) 2001-2020 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne-Again SHell. @@ -34,13 +34,13 @@ #include "mstats.h" -extern int malloc_free_blocks __P((int)); +extern int malloc_free_blocks PARAMS((int)); extern int malloc_mmap_threshold; extern struct _malstats _mstats; -extern FILE *_imalloc_fopen __P((char *, char *, char *, char *, size_t)); +extern FILE *_imalloc_fopen PARAMS((char *, char *, char *, char *, size_t)); struct bucket_stats malloc_bucket_stats (size) @@ -110,7 +110,7 @@ _print_malloc_stats (s, fp) if (i == malloc_mmap_threshold+1) fprintf (fp, "--------\n"); if (v.nmal > 0) - fprintf (fp, "%8lu\t%4d\t%6d\t%5d\t%8d\t%8d %5d %8d\n", (unsigned long)v.blocksize, v.nfree, v.nused, v.nmal, v.nmorecore, v.nlesscore, v.nsplit, v.ncoalesce); + fprintf (fp, "%8lu\t%4d\t%6d\t%5d%8d\t%8d %5d %8d\n", (unsigned long)v.blocksize, v.nfree, v.nused, v.nmal, v.nmorecore, v.nlesscore, v.nsplit, v.ncoalesce); totfree += v.nfree * v.blocksize; totused += v.nused * v.blocksize; } diff --git a/lib/malloc/table.c b/lib/malloc/table.c index 97946dc7..e6acbf4a 100644 --- a/lib/malloc/table.c +++ b/lib/malloc/table.c @@ -1,6 +1,6 @@ /* table.c - bookkeeping functions for allocated memory */ -/* Copyright (C) 2001-2003 Free Software Foundation, Inc. +/* Copyright (C) 2001-2020 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -29,15 +29,15 @@ #include "table.h" #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 extern int malloc_register; #ifdef MALLOC_REGISTER -extern FILE *_imalloc_fopen __P((char *, char *, char *, char *, size_t)); +extern FILE *_imalloc_fopen PARAMS((char *, char *, char *, char *, size_t)); #define FIND_ALLOC 0x01 /* find slot for new allocation */ #define FIND_EXIST 0x02 /* find slot for existing entry for free() or search */ @@ -174,7 +174,7 @@ mregister_alloc (tag, mem, size, file, line) /* Block all signals in case we are executed from a signal handler. */ blocked_sigs = 0; #ifdef SHELL - if (interrupt_immediately || running_trap || signal_is_trapped (SIGINT) || signal_is_trapped (SIGCHLD)) + if (running_trap || signal_is_trapped (SIGINT) || signal_is_trapped (SIGCHLD)) #endif { _malloc_block_signals (&set, &oset); @@ -229,7 +229,7 @@ mregister_free (mem, size, file, line) /* Block all signals in case we are executed from a signal handler. */ blocked_sigs = 0; #ifdef SHELL - if (interrupt_immediately || running_trap || signal_is_trapped (SIGINT) || signal_is_trapped (SIGCHLD)) + if (running_trap || signal_is_trapped (SIGINT) || signal_is_trapped (SIGCHLD)) #endif { _malloc_block_signals (&set, &oset); diff --git a/lib/malloc/table.h b/lib/malloc/table.h index 7494f0a5..92866cf2 100644 --- a/lib/malloc/table.h +++ b/lib/malloc/table.h @@ -1,6 +1,6 @@ /* table.h - definitions for tables for keeping track of allocated memory */ -/* Copyright (C) 2001-2003 Free Software Foundation, Inc. +/* Copyright (C) 2001-2020 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne-Again SHell. @@ -57,12 +57,12 @@ typedef struct mr_table { #define REG_TABLE_SIZE 8192 -extern mr_table_t *mr_table_entry __P((PTR_T)); -extern void mregister_alloc __P((const char *, PTR_T, size_t, const char *, int)); -extern void mregister_free __P((PTR_T, int, const char *, int)); +extern mr_table_t *mr_table_entry PARAMS((PTR_T)); +extern void mregister_alloc PARAMS((const char *, PTR_T, size_t, const char *, int)); +extern void mregister_free PARAMS((PTR_T, int, const char *, int)); extern void mregister_describe_mem (); -extern void mregister_dump_table __P((void)); -extern void mregister_table_init __P((void)); +extern void mregister_dump_table PARAMS((void)); +extern void mregister_table_init PARAMS((void)); typedef struct ma_table { const char *file; @@ -70,10 +70,10 @@ typedef struct ma_table { int nalloc; } ma_table_t; -extern void mlocation_register_alloc __P((const char *, int)); -extern void mlocation_table_init __P((void)); -extern void mlocation_dump_table __P((void)); -extern void mlocation_write_table __P((void)); +extern void mlocation_register_alloc PARAMS((const char *, int)); +extern void mlocation_table_init PARAMS((void)); +extern void mlocation_dump_table PARAMS((void)); +extern void mlocation_write_table PARAMS((void)); /* NOTE: HASH_MIX taken from dmalloc (http://dmalloc.com) */ diff --git a/lib/malloc/trace.c b/lib/malloc/trace.c index 95898b70..391ca9d8 100644 --- a/lib/malloc/trace.c +++ b/lib/malloc/trace.c @@ -1,6 +1,6 @@ /* trace.c - tracing functions for malloc */ -/* Copyright (C) 2001-2003 Free Software Foundation, Inc. +/* Copyright (C) 2001-2020 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -35,7 +35,7 @@ static int _mtrace_verbose = 0; #ifdef MALLOC_TRACE -extern FILE *_imalloc_fopen __P((char *, char *, char *, char *, size_t)); +extern FILE *_imalloc_fopen PARAMS((char *, char *, char *, char *, size_t)); FILE *_mtrace_fp = NULL; extern char _malloc_trace_buckets[]; diff --git a/lib/malloc/watch.h b/lib/malloc/watch.h index 6782acc4..2a0f4970 100644 --- a/lib/malloc/watch.h +++ b/lib/malloc/watch.h @@ -1,6 +1,6 @@ /* watch.h - definitions for tables for keeping track of allocated memory */ -/* Copyright (C) 2001-2003 Free Software Foundation, Inc. +/* Copyright (C) 2001-2020 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne-Again SHell. @@ -34,7 +34,7 @@ extern int _malloc_nwatch; -extern void _malloc_ckwatch __P((PTR_T, const char *, int, int, unsigned long)); +extern void _malloc_ckwatch PARAMS((PTR_T, const char *, int, int, unsigned long)); #endif /* MALLOC_WATCH */ |