2008-11-12 05:55:19 +01:00
|
|
|
#include "sh.h"
|
|
|
|
|
2008-11-15 12:42:18 +01:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/aalloc.c,v 1.31 2008/11/15 11:42:18 tg Exp $");
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
/* mksh integration of aalloc */
|
|
|
|
|
2008-11-15 09:52:01 +01:00
|
|
|
#if defined(AALLOC_STATS) && !defined(AALLOC_WARN)
|
|
|
|
#define AALLOC_WARN aalloc_warn
|
|
|
|
static void aalloc_warn(const char *, ...)
|
|
|
|
__attribute__((format (printf, 1, 2)));
|
|
|
|
static void
|
|
|
|
aalloc_warn(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
FILE *of;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
if ((of = fopen("/tmp/aalloc.out", "ab+"))) {
|
|
|
|
fprintf(of, "%08X %5d: ", (unsigned int)time(NULL),
|
|
|
|
(int)getpid());
|
|
|
|
vfprintf(of, fmt, va);
|
|
|
|
putc('\n', of);
|
|
|
|
fclose(of);
|
|
|
|
} else
|
|
|
|
internal_verrorf(fmt, va);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-12 05:55:19 +01:00
|
|
|
#ifndef AALLOC_ABORT
|
|
|
|
#define AALLOC_ABORT internal_errorf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef AALLOC_WARN
|
|
|
|
#define AALLOC_WARN internal_warningf
|
|
|
|
#endif
|
|
|
|
|
2008-11-15 08:51:53 +01:00
|
|
|
#ifndef AALLOC_THREAD_ENTER
|
|
|
|
#define AALLOC_THREAD_ENTER(ap) /* nothing */
|
|
|
|
#define AALLOC_THREAD_LEAVE(ap) /* nothing */
|
|
|
|
#endif
|
|
|
|
|
2008-11-15 09:52:01 +01:00
|
|
|
#ifndef AALLOC_LEAK_SILENT
|
2008-11-13 00:34:02 +01:00
|
|
|
#define AALLOC_LEAK_SILENT /* the code does not yet clean up at exit */
|
2008-11-15 09:52:01 +01:00
|
|
|
#endif
|
2008-11-13 00:34:02 +01:00
|
|
|
|
2008-11-12 05:55:19 +01:00
|
|
|
#ifndef AALLOC_RANDOM
|
|
|
|
#if HAVE_ARC4RANDOM
|
|
|
|
#define AALLOC_RANDOM() arc4random()
|
|
|
|
#else
|
|
|
|
#define AALLOC_RANDOM() (rand() * RAND_MAX + rand())
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2008-11-15 09:52:01 +01:00
|
|
|
#undef AALLOC_SMALL
|
|
|
|
#ifdef MKSH_SMALL
|
|
|
|
#define AALLOC_SMALL /* skip sanity checks */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(DEBUG) && !defined(AALLOC_DEBUG)
|
|
|
|
#define AALLOC_DEBUG /* add extra sanity checks */
|
2008-11-12 05:55:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* generic area-based allocator built for mmap malloc or omalloc */
|
|
|
|
|
2008-11-15 09:52:01 +01:00
|
|
|
#if defined(AALLOC_SMALL)
|
|
|
|
#undef AALLOC_DEBUG
|
|
|
|
#undef AALLOC_STATS
|
2008-11-15 10:05:29 +01:00
|
|
|
#undef AALLOC_TRACK
|
2008-11-15 12:42:18 +01:00
|
|
|
#ifndef AALLOC_NO_COOKIES
|
|
|
|
#define AALLOC_NO_COOKIES
|
|
|
|
#endif
|
2008-11-15 10:05:29 +01:00
|
|
|
#elif defined(AALLOC_STATS) && !defined(AALLOC_TRACK)
|
|
|
|
#define AALLOC_TRACK
|
2008-11-15 09:52:01 +01:00
|
|
|
#endif
|
|
|
|
|
2008-11-12 05:55:19 +01:00
|
|
|
#define PVALIGN (sizeof (void *))
|
|
|
|
#define PVMASK (sizeof (void *) - 1)
|
2008-11-15 12:42:18 +01:00
|
|
|
#define isunaligned(p) (((ptrdiff_t)(p) & PVMASK) != 0)
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
#ifndef AALLOC_INITSZ
|
2008-11-12 06:46:14 +01:00
|
|
|
#define AALLOC_INITSZ (64 * PVALIGN) /* at least 4 pointers */
|
2008-11-12 05:55:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef /* unsigned */ ptrdiff_t TCookie;
|
2008-11-15 12:42:18 +01:00
|
|
|
typedef union {
|
|
|
|
/* unsigned */ ptrdiff_t xv;
|
|
|
|
} TCooked;
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
typedef union {
|
2008-11-15 12:42:18 +01:00
|
|
|
ptrdiff_t nv;
|
|
|
|
char *cp;
|
2008-11-12 05:55:19 +01:00
|
|
|
} TPtr;
|
|
|
|
|
2008-11-15 12:42:18 +01:00
|
|
|
#ifdef AALLOC_NO_COOKIES
|
|
|
|
#define ldcook(p, cv, c) ((p).nv = (cv).xv, (p).nv)
|
|
|
|
#define stcook(cv, p, c) ((cv).xv = (p).nv, (p).nv)
|
|
|
|
#define stcookp(cv, p, c) (xp.cp = (char *)(p), (cv).xv = xp.nv, xp.nv)
|
|
|
|
#define stcooki(cv, i, c) ((cv).xv = (xp.nv = (i)), xp.nv)
|
|
|
|
#define iscook(c) true
|
|
|
|
#else
|
|
|
|
#define ldcook(p, cv, c) ((p).nv = (cv).xv ^ (c), (p).nv)
|
|
|
|
#define stcook(cv, p, c) ((cv).xv = (p).nv ^ (c), (p).nv)
|
|
|
|
#define stcookp(cv, p, c) (xp.cp = (char *)(p), (cv).xv = xp.nv ^ (c), xp.nv)
|
|
|
|
#define stcooki(cv, i, c) ((cv).xv = (xp.nv = (i)) ^ (c), xp.nv)
|
|
|
|
#define iscook(c) isunaligned(c)
|
|
|
|
#endif
|
|
|
|
|
2008-11-12 05:55:19 +01:00
|
|
|
/*
|
|
|
|
* The separation between TBlock and TArea does not seem to make
|
|
|
|
* sense at first, especially in the !AALLOC_TRACK case, but is
|
|
|
|
* necessary to keep PArea values constant even if the storage is
|
|
|
|
* enlarged. While we could use an extensible array to keep track
|
|
|
|
* of the TBlock instances, kind of like we use TBlock.storage to
|
|
|
|
* track the allocations, it would require another TBlock member
|
|
|
|
* and a fair amount of backtracking; since omalloc can optimise
|
|
|
|
* pointer sized allocations like a !AALLOC_TRACK TArea, we don't
|
|
|
|
* do that then.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct TBlock {
|
2008-11-15 12:42:18 +01:00
|
|
|
#ifndef AALLOC_NO_COOKIES
|
|
|
|
TCookie bcookie;
|
|
|
|
#endif
|
2008-11-12 06:11:05 +01:00
|
|
|
char *endp;
|
|
|
|
char *last;
|
2008-11-12 05:55:19 +01:00
|
|
|
void *storage;
|
|
|
|
};
|
|
|
|
typedef struct TBlock *PBlock;
|
|
|
|
|
|
|
|
struct TArea {
|
2008-11-15 12:42:18 +01:00
|
|
|
TCooked bp;
|
2008-11-12 05:55:19 +01:00
|
|
|
#ifdef AALLOC_TRACK
|
2008-11-15 12:42:18 +01:00
|
|
|
TCooked prev;
|
|
|
|
#ifndef AALLOC_NO_COOKIES
|
|
|
|
TCookie scookie;
|
|
|
|
#endif
|
2008-11-15 09:52:01 +01:00
|
|
|
#ifdef AALLOC_STATS
|
|
|
|
const char *name;
|
|
|
|
unsigned long numalloc;
|
|
|
|
unsigned long maxalloc;
|
|
|
|
bool isfree;
|
|
|
|
#endif
|
2008-11-12 05:55:19 +01:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2008-11-15 12:42:18 +01:00
|
|
|
static TCookie fcookie;
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
#ifdef AALLOC_TRACK
|
|
|
|
static PArea track;
|
|
|
|
static void track_check(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AALLOC_MPROTECT
|
|
|
|
#undef AALLOC_INITSZ
|
|
|
|
#define AALLOC_INITSZ pagesz
|
2008-11-12 06:55:43 +01:00
|
|
|
static long pagesz;
|
2008-11-12 06:32:34 +01:00
|
|
|
#define AALLOC_ALLOW(bp) mprotect((bp), (bp)->endp - (char *)(bp), \
|
|
|
|
PROT_READ | PROT_WRITE)
|
|
|
|
#define AALLOC_DENY(bp) mprotect((bp), (bp)->endp - (char *)(bp), \
|
|
|
|
PROT_NONE)
|
|
|
|
#define AALLOC_PEEK(bp) mprotect((bp), sizeof (struct TArea), \
|
|
|
|
PROT_READ | PROT_WRITE)
|
2008-11-12 05:55:19 +01:00
|
|
|
#else
|
|
|
|
#define AALLOC_ALLOW(bp) /* nothing */
|
|
|
|
#define AALLOC_DENY(bp) /* nothing */
|
|
|
|
#define AALLOC_PEEK(bp) /* nothing */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some nice properties: allocations are always PVALIGNed, which
|
|
|
|
* includes the pointers seen by our user, the forward and back
|
|
|
|
* pointers, the AALLOC_TRACK prev pointer, etc.
|
|
|
|
*/
|
|
|
|
|
2008-11-12 20:23:09 +01:00
|
|
|
#define safe_malloc(dest, len) do { \
|
|
|
|
(dest) = malloc((len)); \
|
|
|
|
safe_xalloc_common((dest), (len)); \
|
|
|
|
} while (/* CONSTCOND */ 0)
|
2008-11-12 05:55:19 +01:00
|
|
|
#define safe_realloc(dest, len) do { \
|
2008-11-12 20:23:09 +01:00
|
|
|
(dest) = realloc((dest), (len)); \
|
|
|
|
safe_xalloc_common((dest), (len)); \
|
|
|
|
} while (/* CONSTCOND */ 0)
|
|
|
|
#define safe_xalloc_common(dest, len) do { \
|
|
|
|
if ((dest) == NULL) \
|
2008-11-12 06:55:43 +01:00
|
|
|
AALLOC_ABORT("unable to allocate %lu bytes: %s", \
|
|
|
|
(unsigned long)(len), strerror(errno)); \
|
2008-11-15 12:42:18 +01:00
|
|
|
if (isunaligned(dest)) \
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_ABORT("unaligned malloc result: %p", (dest)); \
|
|
|
|
} while (/* CONSTCOND */ 0)
|
|
|
|
|
|
|
|
#define MUL_NO_OVERFLOW (1UL << (sizeof (size_t) * 8 / 2))
|
|
|
|
#define safe_muladd(nmemb, size, extra) do { \
|
|
|
|
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && \
|
|
|
|
nmemb > 0 && SIZE_MAX / nmemb < size) \
|
2008-11-12 06:55:43 +01:00
|
|
|
AALLOC_ABORT("attempted integer overflow: %lu * %lu", \
|
|
|
|
(unsigned long)nmemb, (unsigned long)size); \
|
2008-11-12 05:55:19 +01:00
|
|
|
size *= nmemb; \
|
|
|
|
if (size >= SIZE_MAX - extra) \
|
2008-11-12 06:55:43 +01:00
|
|
|
AALLOC_ABORT("unable to allocate %lu bytes: %s", \
|
|
|
|
(unsigned long)size, "value plus extra too big"); \
|
2008-11-12 06:46:45 +01:00
|
|
|
size += extra; \
|
2008-11-12 05:55:19 +01:00
|
|
|
} while (/* CONSTCOND */ 0)
|
|
|
|
|
2008-11-12 07:38:08 +01:00
|
|
|
static void adelete_leak(PArea, PBlock, bool, const char *);
|
2008-11-12 05:55:19 +01:00
|
|
|
static PBlock check_bp(PArea, const char *, TCookie);
|
2008-11-15 12:42:18 +01:00
|
|
|
static TCooked *check_ptr(void *, PArea, PBlock *, TPtr *, const char *,
|
|
|
|
const char *);
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
PArea
|
2008-11-15 09:52:01 +01:00
|
|
|
#ifdef AALLOC_STATS
|
|
|
|
anewEx(size_t hint, const char *friendly_name)
|
|
|
|
#else
|
2008-11-15 08:35:25 +01:00
|
|
|
anew(size_t hint)
|
2008-11-15 09:52:01 +01:00
|
|
|
#endif
|
2008-11-12 05:55:19 +01:00
|
|
|
{
|
|
|
|
PArea ap;
|
|
|
|
PBlock bp;
|
2008-11-15 12:42:18 +01:00
|
|
|
TPtr xp;
|
2008-11-12 05:55:19 +01:00
|
|
|
|
2008-11-15 08:51:53 +01:00
|
|
|
AALLOC_THREAD_ENTER(NULL)
|
|
|
|
|
2008-11-12 07:05:54 +01:00
|
|
|
#ifdef AALLOC_MPROTECT
|
|
|
|
if (!pagesz) {
|
|
|
|
if ((pagesz = sysconf(_SC_PAGESIZE)) == -1 ||
|
|
|
|
(size_t)pagesz < (size_t)PVALIGN)
|
|
|
|
AALLOC_ABORT("sysconf(_SC_PAGESIZE) failed: %ld %s",
|
|
|
|
pagesz, strerror(errno));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-12 05:55:19 +01:00
|
|
|
#ifdef AALLOC_DEBUG
|
|
|
|
if (PVALIGN != 2 && PVALIGN != 4 && PVALIGN != 8 && PVALIGN != 16)
|
2008-11-12 06:55:43 +01:00
|
|
|
AALLOC_ABORT("PVALIGN not a power of two: %lu",
|
|
|
|
(unsigned long)PVALIGN);
|
2008-11-15 12:42:18 +01:00
|
|
|
if (sizeof (TPtr) != sizeof (TCookie) || sizeof (TPtr) != PVALIGN ||
|
|
|
|
sizeof (TPtr) != sizeof (TCooked))
|
|
|
|
AALLOC_ABORT("TPtr sizes do not match: %lu, %lu, %lu, %lu",
|
2008-11-12 06:55:43 +01:00
|
|
|
(unsigned long)sizeof (TPtr),
|
2008-11-15 12:42:18 +01:00
|
|
|
(unsigned long)sizeof (TCookie), (unsigned long)PVALIGN,
|
|
|
|
(unsigned long)sizeof (TCooked));
|
2008-11-12 07:05:54 +01:00
|
|
|
if ((size_t)AALLOC_INITSZ < sizeof (struct TBlock))
|
2008-11-12 06:55:43 +01:00
|
|
|
AALLOC_ABORT("AALLOC_INITSZ constant too small: %lu < %lu",
|
|
|
|
(unsigned long)AALLOC_INITSZ,
|
|
|
|
(unsigned long)sizeof (struct TBlock));
|
2008-11-15 08:35:25 +01:00
|
|
|
if (hint != (1UL << (ffs(hint) - 1)))
|
|
|
|
AALLOC_ABORT("anew hint %lu not a power of two or too big",
|
|
|
|
(unsigned long)hint);
|
2008-11-12 05:55:19 +01:00
|
|
|
#endif
|
|
|
|
|
2008-11-15 12:42:18 +01:00
|
|
|
if (!fcookie) {
|
|
|
|
#ifdef AALLOC_NO_COOKIES
|
|
|
|
fcookie++;
|
|
|
|
#else
|
|
|
|
size_t v = 0;
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
/* ensure unaligned cookie */
|
|
|
|
do {
|
2008-11-15 12:42:18 +01:00
|
|
|
fcookie = AALLOC_RANDOM();
|
|
|
|
if (AALLOC_RANDOM() & 1)
|
|
|
|
v = AALLOC_RANDOM() & 7;
|
|
|
|
} while (!iscook(fcookie) || !v);
|
2008-11-12 05:55:19 +01:00
|
|
|
/* randomise seed afterwards */
|
|
|
|
while (v--)
|
|
|
|
AALLOC_RANDOM();
|
2008-11-15 12:42:18 +01:00
|
|
|
#endif
|
2008-11-12 05:55:19 +01:00
|
|
|
#ifdef AALLOC_TRACK
|
|
|
|
atexit(track_check);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-11-15 08:35:25 +01:00
|
|
|
safe_muladd(sizeof (void *), hint, 0);
|
|
|
|
if (hint < sizeof (struct TBlock))
|
|
|
|
hint = AALLOC_INITSZ;
|
|
|
|
|
2008-11-12 20:23:09 +01:00
|
|
|
safe_malloc(ap, sizeof (struct TArea));
|
2008-11-15 08:35:25 +01:00
|
|
|
safe_malloc(bp, hint);
|
2008-11-12 05:55:19 +01:00
|
|
|
/* ensure unaligned cookie */
|
2008-11-15 12:42:18 +01:00
|
|
|
#ifndef AALLOC_NO_COOKIES
|
2008-11-12 07:10:51 +01:00
|
|
|
do {
|
2008-11-15 12:42:18 +01:00
|
|
|
bp->bcookie = AALLOC_RANDOM();
|
|
|
|
} while (!iscook(bp->bcookie));
|
2008-11-12 07:10:51 +01:00
|
|
|
#endif
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
/* first byte after block */
|
2008-11-15 12:42:18 +01:00
|
|
|
bp->endp = (char *)bp + hint; /* bp + size of the block */
|
2008-11-12 05:55:19 +01:00
|
|
|
/* next entry (forward pointer) available for new allocation */
|
2008-11-12 06:11:05 +01:00
|
|
|
bp->last = (char *)&bp->storage; /* first entry */
|
2008-11-12 05:55:19 +01:00
|
|
|
|
2008-11-15 12:42:18 +01:00
|
|
|
(void)stcookp(ap->bp, bp, fcookie);
|
2008-11-12 05:55:19 +01:00
|
|
|
#ifdef AALLOC_TRACK
|
2008-11-15 12:42:18 +01:00
|
|
|
(void)stcookp(ap->prev, track, fcookie);
|
|
|
|
#ifndef AALLOC_NO_COOKIES
|
|
|
|
(void)stcooki(ap->scookie, bp->bcookie, fcookie);
|
|
|
|
#endif
|
2008-11-15 09:52:01 +01:00
|
|
|
#ifdef AALLOC_STATS
|
|
|
|
ap->name = friendly_name ? friendly_name : "(no name)";
|
|
|
|
ap->numalloc = 0;
|
|
|
|
ap->maxalloc = 0;
|
|
|
|
ap->isfree = false;
|
|
|
|
#endif
|
2008-11-12 05:55:19 +01:00
|
|
|
track = ap;
|
|
|
|
#endif
|
|
|
|
AALLOC_DENY(bp);
|
2008-11-15 08:51:53 +01:00
|
|
|
AALLOC_THREAD_LEAVE(NULL)
|
2008-11-12 05:55:19 +01:00
|
|
|
return (ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate block in Area “ap”, return unlocked block pointer.
|
|
|
|
* If “ocookie” is not 0, make sure block cookie is equal.
|
|
|
|
*/
|
|
|
|
static PBlock
|
2008-11-15 12:42:18 +01:00
|
|
|
check_bp(PArea ap, const char *funcname, TCookie ocookie __unused)
|
2008-11-12 05:55:19 +01:00
|
|
|
{
|
|
|
|
TPtr p;
|
|
|
|
PBlock bp;
|
|
|
|
|
2008-11-15 12:42:18 +01:00
|
|
|
(void)ldcook(p, ap->bp, fcookie);
|
|
|
|
#ifndef AALLOC_SMALL
|
|
|
|
if (!p.nv
|
2008-11-15 09:52:01 +01:00
|
|
|
#ifdef AALLOC_STATS
|
|
|
|
|| ap->isfree
|
|
|
|
#endif
|
|
|
|
) {
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_WARN("%s: area %p already freed", funcname, ap);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2008-11-15 12:42:18 +01:00
|
|
|
if (isunaligned(p.nv)) {
|
2008-11-12 06:55:43 +01:00
|
|
|
AALLOC_WARN("%s: area %p block pointer destroyed: %p",
|
2008-11-15 12:42:18 +01:00
|
|
|
funcname, ap, p.cp);
|
2008-11-12 05:55:19 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
2008-11-15 12:42:18 +01:00
|
|
|
#endif
|
|
|
|
bp = (PBlock)p.cp;
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_PEEK(bp);
|
2008-11-15 12:42:18 +01:00
|
|
|
#ifndef AALLOC_NO_COOKIES
|
2008-11-12 05:55:19 +01:00
|
|
|
if (ocookie && bp->cookie != ocookie) {
|
2008-11-12 06:55:43 +01:00
|
|
|
AALLOC_WARN("%s: block %p cookie destroyed: %p, %p",
|
|
|
|
funcname, bp, (void *)ocookie, (void *)bp->cookie);
|
2008-11-12 05:55:19 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
2008-11-15 12:42:18 +01:00
|
|
|
#endif
|
|
|
|
#ifndef AALLOC_SMALL
|
|
|
|
if (isunaligned(bp->endp) || isunaligned(bp->last)) {
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_WARN("%s: block %p data structure destroyed: %p, %p",
|
|
|
|
funcname, bp, bp->endp, bp->last);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2008-11-12 06:11:05 +01:00
|
|
|
if (bp->endp < (char *)bp) {
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_WARN("%s: block %p end pointer out of bounds: %p",
|
|
|
|
funcname, bp, bp->endp);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2008-11-12 06:27:01 +01:00
|
|
|
if ((bp->last < (char *)&bp->storage) || (bp->last > bp->endp)) {
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_WARN("%s: block %p last pointer out of bounds: "
|
|
|
|
"%p < %p < %p", funcname, bp, &bp->storage, bp->last,
|
|
|
|
bp->endp);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2008-11-15 12:42:18 +01:00
|
|
|
#endif
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_ALLOW(bp);
|
|
|
|
return (bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef AALLOC_TRACK
|
|
|
|
/*
|
|
|
|
* At exit, dump and free any leaked allocations, blocks and areas.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
track_check(void)
|
|
|
|
{
|
2008-11-12 08:36:19 +01:00
|
|
|
PArea tp;
|
2008-11-12 05:55:19 +01:00
|
|
|
PBlock bp;
|
2008-11-15 12:42:18 +01:00
|
|
|
TCookie xc = 0;
|
|
|
|
TPtr xp;
|
2008-11-12 05:55:19 +01:00
|
|
|
|
2008-11-15 08:59:46 +01:00
|
|
|
AALLOC_THREAD_ENTER(NULL)
|
2008-11-15 12:42:18 +01:00
|
|
|
tp = track;
|
|
|
|
while (tp) {
|
|
|
|
#ifndef AALLOC_NO_COOKIES
|
|
|
|
xc = ldcook(xp, tp->scookie, fcookie);
|
|
|
|
#endif
|
|
|
|
(void)ldcook(xp, tp->prev, fcookie);
|
2008-11-15 09:52:01 +01:00
|
|
|
#ifdef AALLOC_STATS
|
|
|
|
AALLOC_WARN("AALLOC_STATS for %s(%p): %lu allocated, %lu at "
|
|
|
|
"once, %sfree", tp->name, tp, tp->numalloc, tp->maxalloc,
|
|
|
|
tp->isfree ? "" : "not ");
|
|
|
|
if (tp->isfree)
|
2008-11-15 12:42:18 +01:00
|
|
|
goto track_check_next;
|
2008-11-15 09:52:01 +01:00
|
|
|
#endif
|
2008-11-15 12:42:18 +01:00
|
|
|
if (isunaligned(xp.nv) || !iscook(xc)) {
|
2008-11-12 05:55:19 +01:00
|
|
|
/* buffer overflow or something? */
|
|
|
|
AALLOC_WARN("AALLOC_TRACK data structure %p destroyed:"
|
2008-11-15 12:42:18 +01:00
|
|
|
" %p, %p; exiting", tp, xp.cp, (void *)xc);
|
2008-11-15 08:59:46 +01:00
|
|
|
break;
|
2008-11-12 05:55:19 +01:00
|
|
|
}
|
2008-11-15 12:42:18 +01:00
|
|
|
if (!(bp = check_bp(tp, "atexit:track_check", xc)))
|
|
|
|
goto track_check_next;
|
2008-11-12 07:44:04 +01:00
|
|
|
if (bp->last != (char *)&bp->storage)
|
2008-11-13 00:34:02 +01:00
|
|
|
#ifdef AALLOC_LEAK_SILENT
|
2008-11-12 08:36:19 +01:00
|
|
|
adelete_leak(tp, bp, false, "at exit");
|
2008-11-12 07:42:22 +01:00
|
|
|
#else
|
2008-11-12 08:36:19 +01:00
|
|
|
adelete_leak(tp, bp, true, "at exit");
|
2008-11-12 07:44:04 +01:00
|
|
|
else
|
2008-11-12 08:36:19 +01:00
|
|
|
AALLOC_WARN("leaking empty area %p (%p %lu)", tp,
|
2008-11-12 07:44:04 +01:00
|
|
|
bp, (unsigned long)(bp->endp - (char *)bp));
|
2008-11-12 07:42:22 +01:00
|
|
|
#endif
|
2008-11-12 05:55:19 +01:00
|
|
|
free(bp);
|
2008-11-15 12:42:18 +01:00
|
|
|
track_check_next:
|
2008-11-12 08:36:19 +01:00
|
|
|
free(tp);
|
2008-11-15 12:42:18 +01:00
|
|
|
tp = (PArea)xp.cp;
|
2008-11-12 05:55:19 +01:00
|
|
|
}
|
2008-11-15 08:59:46 +01:00
|
|
|
track = NULL;
|
|
|
|
AALLOC_THREAD_LEAVE(NULL)
|
2008-11-12 05:55:19 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-12 06:05:17 +01:00
|
|
|
static void
|
2008-11-12 07:38:08 +01:00
|
|
|
adelete_leak(PArea ap, PBlock bp, bool always_warn, const char *when)
|
2008-11-12 06:05:17 +01:00
|
|
|
{
|
2008-11-15 12:42:18 +01:00
|
|
|
TPtr xp;
|
2008-11-12 06:05:17 +01:00
|
|
|
|
2008-11-12 06:11:05 +01:00
|
|
|
while (bp->last > (char *)&bp->storage) {
|
2008-11-12 06:05:17 +01:00
|
|
|
bp->last -= PVALIGN;
|
2008-11-15 12:42:18 +01:00
|
|
|
(void)ldcook(xp, **((TCooked **)bp->last), bp->bcookie);
|
|
|
|
#ifndef AALLOC_SMALL
|
|
|
|
if (always_warn || xp.cp != bp->last)
|
|
|
|
AALLOC_WARN("leaking %s pointer %p in area %p (ofs %p "
|
|
|
|
"len %lu) %s", xp.cp != bp->last ? "underflown" :
|
|
|
|
"valid", *((char **)bp->last) + PVALIGN, ap, bp,
|
|
|
|
(unsigned long)(bp->endp - (char *)bp), when);
|
|
|
|
#endif
|
|
|
|
free(xp.cp);
|
2008-11-12 06:05:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-12 05:55:19 +01:00
|
|
|
void
|
|
|
|
adelete(PArea *pap)
|
|
|
|
{
|
2008-11-15 12:42:18 +01:00
|
|
|
PBlock bp;
|
|
|
|
#if defined(AALLOC_TRACK) && !defined(AALLOC_STATS)
|
2008-11-12 05:55:19 +01:00
|
|
|
PArea tp;
|
2008-11-15 12:42:18 +01:00
|
|
|
TCookie xc = 0;
|
|
|
|
TPtr xp;
|
2008-11-12 05:55:19 +01:00
|
|
|
#endif
|
|
|
|
|
2008-11-15 08:51:53 +01:00
|
|
|
AALLOC_THREAD_ENTER(*pap)
|
|
|
|
|
2008-11-12 06:05:17 +01:00
|
|
|
/* ignore invalid areas */
|
2008-11-12 07:12:56 +01:00
|
|
|
if ((bp = check_bp(*pap, "adelete", 0)) != NULL) {
|
2008-11-12 06:11:05 +01:00
|
|
|
if (bp->last != (char *)&bp->storage)
|
2008-11-12 07:38:08 +01:00
|
|
|
adelete_leak(*pap, bp, false, "in adelete");
|
2008-11-12 06:05:17 +01:00
|
|
|
free(bp);
|
|
|
|
}
|
2008-11-12 05:55:19 +01:00
|
|
|
|
2008-11-15 12:42:18 +01:00
|
|
|
#if defined(AALLOC_TRACK) && !defined(AALLOC_STATS)
|
|
|
|
/* if we are the last TArea allocated */
|
2008-11-12 05:55:19 +01:00
|
|
|
if (track == *pap) {
|
2008-11-15 12:42:18 +01:00
|
|
|
if (isunaligned(ldcook(xp, (*pap)->prev, fcookie))) {
|
|
|
|
AALLOC_WARN("AALLOC_TRACK data structure %p destroyed:"
|
|
|
|
" %p", *pap, xp.cp);
|
|
|
|
track = NULL;
|
|
|
|
} else
|
|
|
|
track = (PArea)xp.cp;
|
2008-11-12 05:55:19 +01:00
|
|
|
goto adelete_tracked;
|
|
|
|
}
|
|
|
|
/* find the TArea whose prev is *pap */
|
|
|
|
tp = track;
|
|
|
|
while (tp) {
|
2008-11-12 08:36:19 +01:00
|
|
|
#ifndef AALLOC_NO_COOKIES
|
2008-11-15 12:42:18 +01:00
|
|
|
xc = ldcook(xp, tp->scookie, fcookie);
|
2008-11-12 08:36:19 +01:00
|
|
|
#endif
|
2008-11-15 12:42:18 +01:00
|
|
|
(void)ldcook(xp, tp->prev, fcookie);
|
|
|
|
if (isunaligned(xp.nv) || !iscook(xc)) {
|
2008-11-12 08:36:19 +01:00
|
|
|
/* buffer overflow or something? */
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_WARN("AALLOC_TRACK data structure %p destroyed:"
|
2008-11-15 12:42:18 +01:00
|
|
|
" %p, %p; ignoring", tp, xp.cp, (void *)xc);
|
2008-11-12 05:55:19 +01:00
|
|
|
tp = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-15 12:42:18 +01:00
|
|
|
if (xp.cp == (char *)*pap)
|
2008-11-12 05:55:19 +01:00
|
|
|
break;
|
2008-11-15 12:42:18 +01:00
|
|
|
tp = (PArea)xp.cp;
|
2008-11-12 05:55:19 +01:00
|
|
|
}
|
|
|
|
if (tp)
|
2008-11-15 12:42:18 +01:00
|
|
|
tp->prev.xv = (*pap)->prev.xv; /* decouple *pap */
|
2008-11-12 05:55:19 +01:00
|
|
|
else
|
|
|
|
AALLOC_WARN("area %p not in found AALLOC_TRACK data structure",
|
|
|
|
*pap);
|
|
|
|
adelete_tracked:
|
|
|
|
#endif
|
2008-11-15 08:51:53 +01:00
|
|
|
AALLOC_THREAD_LEAVE(*pap)
|
2008-11-15 12:42:18 +01:00
|
|
|
#ifdef AALLOC_STATS
|
|
|
|
(*pap)->isfree = true;
|
|
|
|
#else
|
2008-11-12 05:55:19 +01:00
|
|
|
free(*pap);
|
2008-11-15 12:42:18 +01:00
|
|
|
#endif
|
2008-11-12 05:55:19 +01:00
|
|
|
*pap = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
alloc(size_t nmemb, size_t size, PArea ap)
|
|
|
|
{
|
|
|
|
PBlock bp;
|
2008-11-15 12:42:18 +01:00
|
|
|
TCooked *rp;
|
|
|
|
TPtr xp;
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
/* obtain the memory region requested, retaining guards */
|
|
|
|
safe_muladd(nmemb, size, sizeof (TPtr));
|
2008-11-15 12:42:18 +01:00
|
|
|
safe_malloc(rp, size);
|
2008-11-12 05:55:19 +01:00
|
|
|
|
2008-11-15 08:51:53 +01:00
|
|
|
AALLOC_THREAD_ENTER(ap)
|
|
|
|
|
2008-11-12 05:55:19 +01:00
|
|
|
/* chain into area */
|
|
|
|
if ((bp = check_bp(ap, "alloc", 0)) == NULL)
|
|
|
|
AALLOC_ABORT("cannot continue");
|
|
|
|
if (bp->last == bp->endp) {
|
2008-11-15 12:42:18 +01:00
|
|
|
TCooked **pp;
|
2008-11-12 05:55:19 +01:00
|
|
|
size_t bsz;
|
|
|
|
|
|
|
|
/* make room for more forward ptrs in the block allocation */
|
2008-11-12 06:11:05 +01:00
|
|
|
bsz = bp->endp - (char *)bp;
|
2008-11-12 06:55:43 +01:00
|
|
|
safe_muladd(2, bsz, 0);
|
2008-11-12 05:55:19 +01:00
|
|
|
safe_realloc(bp, bsz);
|
2008-11-12 06:27:01 +01:00
|
|
|
bp->last = (char *)bp + (bsz / 2);
|
|
|
|
bp->endp = (char *)bp + bsz;
|
2008-11-12 05:55:19 +01:00
|
|
|
|
2008-11-12 06:45:28 +01:00
|
|
|
/* all backpointers have to be adjusted */
|
2008-11-15 12:42:18 +01:00
|
|
|
pp = (TCooked **)&bp->storage;
|
|
|
|
while (pp < (TCooked **)bp->last) {
|
|
|
|
(void)stcookp(**pp, pp, bp->bcookie);
|
|
|
|
++pp;
|
2008-11-12 06:45:28 +01:00
|
|
|
}
|
|
|
|
|
2008-11-12 05:55:19 +01:00
|
|
|
/* “bp” has possibly changed, enter its new value into ap */
|
2008-11-15 12:42:18 +01:00
|
|
|
(void)stcookp(ap->bp, bp, fcookie);
|
2008-11-12 05:55:19 +01:00
|
|
|
}
|
2008-11-15 12:42:18 +01:00
|
|
|
memcpy(bp->last, &rp, PVALIGN); /* next available forward ptr */
|
|
|
|
/* store cooked backpointer to address of forward pointer */
|
|
|
|
(void)stcookp(*rp, bp->last, bp->bcookie);
|
2008-11-12 05:55:19 +01:00
|
|
|
bp->last += PVALIGN; /* advance next-avail pointer */
|
2008-11-15 09:52:01 +01:00
|
|
|
#ifdef AALLOC_STATS
|
|
|
|
ap->numalloc++;
|
|
|
|
{
|
|
|
|
unsigned long curalloc;
|
|
|
|
|
|
|
|
curalloc = (bp->last - (char *)&bp->storage) / PVALIGN;
|
|
|
|
if (curalloc > ap->maxalloc)
|
|
|
|
ap->maxalloc = curalloc;
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_DENY(bp);
|
2008-11-15 08:51:53 +01:00
|
|
|
AALLOC_THREAD_LEAVE(ap)
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
/* return aligned storage just after the cookied backpointer */
|
2008-11-15 12:42:18 +01:00
|
|
|
return ((char *)rp + PVALIGN);
|
2008-11-12 05:55:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
aresize(void *vp, size_t nmemb, size_t size, PArea ap)
|
|
|
|
{
|
|
|
|
PBlock bp;
|
2008-11-15 12:42:18 +01:00
|
|
|
TCooked *rp;
|
|
|
|
TPtr xp;
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
if (vp == NULL)
|
|
|
|
return (alloc(nmemb, size, ap));
|
|
|
|
|
2008-11-15 08:51:53 +01:00
|
|
|
AALLOC_THREAD_ENTER(ap)
|
|
|
|
|
2008-11-12 05:55:19 +01:00
|
|
|
/* validate allocation and backpointer against forward pointer */
|
2008-11-15 12:42:18 +01:00
|
|
|
if ((rp = check_ptr(vp, ap, &bp, &xp, "aresize", "")) == NULL)
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_ABORT("cannot continue");
|
|
|
|
|
|
|
|
/* move allocation to size and possibly new location */
|
|
|
|
safe_muladd(nmemb, size, sizeof (TPtr));
|
2008-11-15 12:42:18 +01:00
|
|
|
safe_realloc(rp, size);
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
/* write new address of allocation into the block forward pointer */
|
2008-11-15 12:42:18 +01:00
|
|
|
memcpy(xp.cp, &rp, PVALIGN);
|
|
|
|
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_DENY(bp);
|
2008-11-15 08:51:53 +01:00
|
|
|
AALLOC_THREAD_LEAVE(ap)
|
2008-11-12 05:55:19 +01:00
|
|
|
|
2008-11-15 12:42:18 +01:00
|
|
|
return ((char *)rp + PVALIGN);
|
2008-11-12 05:55:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to find “vp” inside Area “ap”, use “what” and “extra” for error msgs.
|
|
|
|
*
|
|
|
|
* If an error occurs, returns NULL with no side effects.
|
2008-11-15 12:42:18 +01:00
|
|
|
* Otherwise, returns address of the allocation, *bpp contains the unlocked
|
|
|
|
* block pointer, *xpp the uncookied backpointer.
|
2008-11-12 05:55:19 +01:00
|
|
|
*/
|
2008-11-15 12:42:18 +01:00
|
|
|
static TCooked *
|
|
|
|
check_ptr(void *vp, PArea ap, PBlock *bpp, TPtr *xpp, const char *what,
|
|
|
|
const char *extra)
|
2008-11-12 05:55:19 +01:00
|
|
|
{
|
2008-11-15 12:42:18 +01:00
|
|
|
TCooked *rp;
|
2008-11-12 05:55:19 +01:00
|
|
|
|
2008-11-15 12:42:18 +01:00
|
|
|
#ifndef AALLOC_SMALL
|
|
|
|
if (isunaligned(vp)) {
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_WARN("trying to %s rogue unaligned pointer %p from "
|
|
|
|
"area %p%s", what + 1, vp, ap, extra);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2008-11-15 12:42:18 +01:00
|
|
|
#endif
|
2008-11-12 05:55:19 +01:00
|
|
|
|
2008-11-15 12:42:18 +01:00
|
|
|
rp = (TCooked *)(((char *)vp) - PVALIGN);
|
|
|
|
#ifndef AALLOC_SMALL
|
|
|
|
if (!rp->xv) {
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_WARN("trying to %s already freed pointer %p from "
|
|
|
|
"area %p%s", what + 1, vp, ap, extra);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2008-11-15 12:42:18 +01:00
|
|
|
#endif
|
2008-11-12 05:55:19 +01:00
|
|
|
|
2008-11-15 12:42:18 +01:00
|
|
|
if ((*bpp = check_bp(ap, what, 0)) == NULL)
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_ABORT("cannot continue");
|
2008-11-15 12:42:18 +01:00
|
|
|
#ifndef AALLOC_SMALL
|
|
|
|
if (isunaligned(ldcook(*xpp, *rp, (*bpp)->bcookie))) {
|
|
|
|
AALLOC_WARN("trying to %s rogue pointer %p from area %p "
|
|
|
|
"(block %p..%p), backpointer %p unaligned%s",
|
|
|
|
what + 1, vp, ap, *bpp, (*bpp)->last, xpp->cp, extra);
|
|
|
|
} else if (xpp->cp < (char *)&(*bpp)->storage ||
|
|
|
|
xpp->cp >= (*bpp)->last) {
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_WARN("trying to %s rogue pointer %p from area %p "
|
|
|
|
"(block %p..%p), backpointer %p out of bounds%s",
|
2008-11-15 12:42:18 +01:00
|
|
|
what + 1, vp, ap, *bpp, (*bpp)->last, xpp->cp, extra);
|
|
|
|
} else if (*((TCooked **)xpp->cp) != rp) {
|
2008-11-12 05:55:19 +01:00
|
|
|
AALLOC_WARN("trying to %s rogue pointer %p from area %p "
|
|
|
|
"(block %p..%p), backpointer %p, forward pointer to "
|
2008-11-15 12:42:18 +01:00
|
|
|
"%p instead%s", what + 1, vp, ap, *bpp, (*bpp)->last,
|
|
|
|
xpp->cp, *((TCooked **)xpp->cp), extra);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
return (rp);
|
|
|
|
|
|
|
|
#ifndef AALLOC_SMALL
|
|
|
|
/* error case fall-through */
|
|
|
|
AALLOC_DENY(*bpp);
|
|
|
|
return (NULL);
|
|
|
|
#endif
|
2008-11-12 05:55:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
afree(void *vp, PArea ap)
|
|
|
|
{
|
|
|
|
PBlock bp;
|
2008-11-15 12:42:18 +01:00
|
|
|
TCooked *rp;
|
|
|
|
TPtr xp;
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
if (vp == NULL)
|
|
|
|
return;
|
|
|
|
|
2008-11-15 08:51:53 +01:00
|
|
|
AALLOC_THREAD_ENTER(ap)
|
|
|
|
|
2008-11-12 05:55:19 +01:00
|
|
|
/* validate allocation and backpointer, ignore rogues */
|
2008-11-15 12:42:18 +01:00
|
|
|
if ((rp = check_ptr(vp, ap, &bp, &xp, "afree", ", ignoring")) == NULL)
|
2008-11-15 08:51:53 +01:00
|
|
|
goto afree_done;
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
/* note: the block allocation does not ever shrink */
|
|
|
|
bp->last -= PVALIGN; /* mark the last forward pointer as free */
|
|
|
|
/* if our forward pointer was not the last one, relocate the latter */
|
2008-11-15 12:42:18 +01:00
|
|
|
if (xp.cp < bp->last) {
|
|
|
|
TCooked *tmp = *((TCooked **)bp->last);
|
2008-11-12 05:55:19 +01:00
|
|
|
|
2008-11-15 12:42:18 +01:00
|
|
|
(void)stcook(*tmp, xp, bp->bcookie); /* write new backptr */
|
|
|
|
memcpy(xp.cp, bp->last, PVALIGN); /* relocate fwd ptr */
|
2008-11-12 05:55:19 +01:00
|
|
|
}
|
2008-11-15 12:42:18 +01:00
|
|
|
rp->xv = 0; /* our backpointer, just in case, for double frees */
|
|
|
|
free(rp);
|
2008-11-12 05:55:19 +01:00
|
|
|
|
|
|
|
AALLOC_DENY(bp);
|
2008-11-15 08:51:53 +01:00
|
|
|
afree_done:
|
|
|
|
AALLOC_THREAD_LEAVE(ap)
|
2008-11-12 05:55:19 +01:00
|
|
|
return;
|
|
|
|
}
|