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