Blame lalloc.c

Packit 991819
/*-
Packit 991819
 * Copyright (c) 2009, 2010, 2011, 2013, 2014, 2016
Packit 991819
 *	mirabilos <m@mirbsd.org>
Packit 991819
 *
Packit 991819
 * Provided that these terms and disclaimer and all copyright notices
Packit 991819
 * are retained or reproduced in an accompanying document, permission
Packit 991819
 * is granted to deal in this work without restriction, including un-
Packit 991819
 * limited rights to use, publicly perform, distribute, sell, modify,
Packit 991819
 * merge, give away, or sublicence.
Packit 991819
 *
Packit 991819
 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
Packit 991819
 * the utmost extent permitted by applicable law, neither express nor
Packit 991819
 * implied; without malicious intent or gross negligence. In no event
Packit 991819
 * may a licensor, author or contributor be held liable for indirect,
Packit 991819
 * direct, other damage, loss, or other issues arising in any way out
Packit 991819
 * of dealing in the work, even if advised of the possibility of such
Packit 991819
 * damage or existence of a defect, except proven that it results out
Packit 991819
 * of said person's immediate fault when using the work as intended.
Packit 991819
 */
Packit 991819
Packit 991819
#include "sh.h"
Packit 991819
#ifdef MKSH_ALLOC_CATCH_UNDERRUNS
Packit 991819
#include <err.h>
Packit 991819
#endif
Packit 991819
Packit 991819
__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.26 2016/02/26 21:53:36 tg Exp $");
Packit 991819
Packit 991819
/* build with CPPFLAGS+= -DUSE_REALLOC_MALLOC=0 on ancient systems */
Packit 991819
#if defined(USE_REALLOC_MALLOC) && (USE_REALLOC_MALLOC == 0)
Packit 991819
#define remalloc(p,n)	((p) == NULL ? malloc_osi(n) : realloc_osi((p), (n)))
Packit 991819
#else
Packit 991819
#define remalloc(p,n)	realloc_osi((p), (n))
Packit 991819
#endif
Packit 991819
Packit 991819
Packit 991819
static struct lalloc_common *findptr(struct lalloc_common **, char *, Area *);
Packit 991819
Packit 991819
#ifndef MKSH_ALLOC_CATCH_UNDERRUNS
Packit 991819
#define ALLOC_ISUNALIGNED(p) (((size_t)(p)) % sizeof(struct lalloc_common))
Packit 991819
#else
Packit 991819
#define ALLOC_ISUNALIGNED(p) (((size_t)(p)) & 4095)
Packit 991819
#undef remalloc
Packit 991819
#undef free_osimalloc
Packit 991819
Packit 991819
static void
Packit 991819
free_osimalloc(void *ptr)
Packit 991819
{
Packit 991819
	struct lalloc_item *lp = ptr;
Packit 991819
Packit 991819
	if (munmap(lp, lp->len))
Packit 991819
		err(1, "free_osimalloc");
Packit 991819
}
Packit 991819
Packit 991819
static void *
Packit 991819
remalloc(void *ptr, size_t size)
Packit 991819
{
Packit 991819
	struct lalloc_item *lp, *lold = ptr;
Packit 991819
Packit 991819
	size = (size + 4095) & ~(size_t)4095;
Packit 991819
Packit 991819
	if (lold && lold->len >= size)
Packit 991819
		return (ptr);
Packit 991819
Packit 991819
	if ((lp = mmap(NULL, size, PROT_READ | PROT_WRITE,
Packit 991819
	    MAP_ANON | MAP_PRIVATE, -1, (off_t)0)) == MAP_FAILED)
Packit 991819
		err(1, "remalloc: mmap(%zu)", size);
Packit 991819
	if (ALLOC_ISUNALIGNED(lp))
Packit 991819
		errx(1, "remalloc: unaligned(%p)", lp);
Packit 991819
	if (mprotect(((char *)lp) + 4096, 4096, PROT_NONE))
Packit 991819
		err(1, "remalloc: mprotect");
Packit 991819
	lp->len = size;
Packit 991819
Packit 991819
	if (lold) {
Packit 991819
		memcpy(((char *)lp) + 8192, ((char *)lold) + 8192,
Packit 991819
		    lold->len - 8192);
Packit 991819
		if (munmap(lold, lold->len))
Packit 991819
			err(1, "remalloc: munmap");
Packit 991819
	}
Packit 991819
Packit 991819
	return (lp);
Packit 991819
}
Packit 991819
#endif
Packit 991819
Packit 991819
void
Packit 991819
ainit(Area *ap)
Packit 991819
{
Packit 991819
#ifdef MKSH_ALLOC_CATCH_UNDERRUNS
Packit 991819
	if (sysconf(_SC_PAGESIZE) != 4096) {
Packit 991819
		fprintf(stderr, "mksh: fatal: pagesize %lu not 4096!\n",
Packit 991819
		    sysconf(_SC_PAGESIZE));
Packit 991819
		fflush(stderr);
Packit 991819
		abort();
Packit 991819
	}
Packit 991819
#endif
Packit 991819
	/* area pointer and items share struct lalloc_common */
Packit 991819
	ap->next = NULL;
Packit 991819
}
Packit 991819
Packit 991819
static struct lalloc_common *
Packit 991819
findptr(struct lalloc_common **lpp, char *ptr, Area *ap)
Packit 991819
{
Packit 991819
	void *lp;
Packit 991819
Packit 991819
#ifndef MKSH_SMALL
Packit 991819
	if (ALLOC_ISUNALIGNED(ptr))
Packit 991819
		goto fail;
Packit 991819
#endif
Packit 991819
	/* get address of ALLOC_ITEM from user item */
Packit 991819
	/*
Packit 991819
	 * note: the alignment of "ptr" to ALLOC_ITEM is checked
Packit 991819
	 * above; the "void *" gets us rid of a gcc 2.95 warning
Packit 991819
	 */
Packit 991819
	*lpp = (lp = ptr - sizeof(ALLOC_ITEM));
Packit 991819
	/* search for allocation item in group list */
Packit 991819
	while (ap->next != lp)
Packit 991819
		if ((ap = ap->next) == NULL) {
Packit 991819
#ifndef MKSH_SMALL
Packit 991819
 fail:
Packit 991819
#endif
Packit 991819
#ifdef DEBUG
Packit 991819
			internal_warningf("rogue pointer %zX in ap %zX",
Packit 991819
			    (size_t)ptr, (size_t)ap);
Packit 991819
			/* try to get a coredump */
Packit 991819
			abort();
Packit 991819
#else
Packit 991819
			internal_errorf("rogue pointer %zX", (size_t)ptr);
Packit 991819
#endif
Packit 991819
		}
Packit 991819
	return (ap);
Packit 991819
}
Packit 991819
Packit 991819
void *
Packit 991819
aresize2(void *ptr, size_t fac1, size_t fac2, Area *ap)
Packit 991819
{
Packit 991819
	if (notoktomul(fac1, fac2))
Packit 991819
		internal_errorf(Tintovfl, fac1, '*', fac2);
Packit 991819
	return (aresize(ptr, fac1 * fac2, ap));
Packit 991819
}
Packit 991819
Packit 991819
void *
Packit 991819
aresize(void *ptr, size_t numb, Area *ap)
Packit 991819
{
Packit 991819
	struct lalloc_common *lp = NULL;
Packit 991819
Packit 991819
	/* resizing (true) or newly allocating? */
Packit 991819
	if (ptr != NULL) {
Packit 991819
		struct lalloc_common *pp;
Packit 991819
Packit 991819
		pp = findptr(&lp, ptr, ap);
Packit 991819
		pp->next = lp->next;
Packit 991819
	}
Packit 991819
Packit 991819
	if (notoktoadd(numb, sizeof(ALLOC_ITEM)) ||
Packit 991819
	    (lp = remalloc(lp, numb + sizeof(ALLOC_ITEM))) == NULL
Packit 991819
#ifndef MKSH_SMALL
Packit 991819
	    || ALLOC_ISUNALIGNED(lp)
Packit 991819
#endif
Packit 991819
	    )
Packit 991819
		internal_errorf(Toomem, numb);
Packit 991819
	/* area pointer and items share struct lalloc_common */
Packit 991819
	lp->next = ap->next;
Packit 991819
	ap->next = lp;
Packit 991819
	/* return user item address */
Packit 991819
	return ((char *)lp + sizeof(ALLOC_ITEM));
Packit 991819
}
Packit 991819
Packit 991819
void
Packit 991819
afree(void *ptr, Area *ap)
Packit 991819
{
Packit 991819
	if (ptr != NULL) {
Packit 991819
		struct lalloc_common *lp, *pp;
Packit 991819
Packit 991819
		pp = findptr(&lp, ptr, ap);
Packit 991819
		/* unhook */
Packit 991819
		pp->next = lp->next;
Packit 991819
		/* now free ALLOC_ITEM */
Packit 991819
		free_osimalloc(lp);
Packit 991819
	}
Packit 991819
}
Packit 991819
Packit 991819
void
Packit 991819
afreeall(Area *ap)
Packit 991819
{
Packit 991819
	struct lalloc_common *lp;
Packit 991819
Packit 991819
	/* traverse group (linked list) */
Packit 991819
	while ((lp = ap->next) != NULL) {
Packit 991819
		/* make next ALLOC_ITEM head of list */
Packit 991819
		ap->next = lp->next;
Packit 991819
		/* free old head */
Packit 991819
		free_osimalloc(lp);
Packit 991819
	}
Packit 991819
}