clean up, avoid aliasing concerns in non-debug case

This commit is contained in:
tg 2016-02-26 21:53:37 +00:00
parent 4a21365067
commit f23ed68f00
3 changed files with 23 additions and 23 deletions

View File

@ -23,7 +23,7 @@
#include <err.h>
#endif
__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.25 2016/02/24 02:08:39 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.26 2016/02/26 21:53:36 tg Exp $");
/* build with CPPFLAGS+= -DUSE_REALLOC_MALLOC=0 on ancient systems */
#if defined(USE_REALLOC_MALLOC) && (USE_REALLOC_MALLOC == 0)
@ -36,7 +36,7 @@ __RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.25 2016/02/24 02:08:39 tg Exp $");
static struct lalloc_common *findptr(struct lalloc_common **, char *, Area *);
#ifndef MKSH_ALLOC_CATCH_UNDERRUNS
#define ALLOC_ISUNALIGNED(p) (((size_t)(p)) % ALLOC_SIZE)
#define ALLOC_ISUNALIGNED(p) (((size_t)(p)) % sizeof(struct lalloc_common))
#else
#define ALLOC_ISUNALIGNED(p) (((size_t)(p)) & 4095)
#undef remalloc
@ -107,10 +107,10 @@ findptr(struct lalloc_common **lpp, char *ptr, Area *ap)
#endif
/* get address of ALLOC_ITEM from user item */
/*
* note: the alignment of "ptr" to ALLOC_SIZE is checked
* note: the alignment of "ptr" to ALLOC_ITEM is checked
* above; the "void *" gets us rid of a gcc 2.95 warning
*/
*lpp = (lp = ptr - ALLOC_SIZE);
*lpp = (lp = ptr - sizeof(ALLOC_ITEM));
/* search for allocation item in group list */
while (ap->next != lp)
if ((ap = ap->next) == NULL) {
@ -126,7 +126,7 @@ findptr(struct lalloc_common **lpp, char *ptr, Area *ap)
internal_errorf("rogue pointer %zX", (size_t)ptr);
#endif
}
return ((void *)ap);
return (ap);
}
void *
@ -150,18 +150,18 @@ aresize(void *ptr, size_t numb, Area *ap)
pp->next = lp->next;
}
if (notoktoadd(numb, ALLOC_SIZE) ||
(lp = remalloc(lp, numb + ALLOC_SIZE)) == NULL
if (notoktoadd(numb, sizeof(ALLOC_ITEM)) ||
(lp = remalloc(lp, numb + sizeof(ALLOC_ITEM))) == NULL
#ifndef MKSH_SMALL
|| ALLOC_ISUNALIGNED(lp)
#endif
)
internal_errorf(Toomem, numb);
/* this only works because Area and ALLOC_ITEM share lalloc_common */
/* area pointer and items share struct lalloc_common */
lp->next = ap->next;
ap->next = lp;
/* return user item address */
return ((char *)lp + ALLOC_SIZE);
return ((char *)lp + sizeof(ALLOC_ITEM));
}
void

14
main.c
View File

@ -34,7 +34,7 @@
#include <locale.h>
#endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.309 2016/02/26 18:48:12 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.310 2016/02/26 21:53:36 tg Exp $");
extern char **environ;
@ -110,13 +110,13 @@ rndsetup(void)
} *bufptr;
char *cp;
cp = alloc(sizeof(*bufptr) - ALLOC_SIZE, APERM);
cp = alloc(sizeof(*bufptr) - sizeof(ALLOC_ITEM), APERM);
#ifdef DEBUG
/* clear the allocated space, for valgrind */
memset(cp, 0, sizeof(*bufptr) - ALLOC_SIZE);
memset(cp, 0, sizeof(*bufptr) - sizeof(ALLOC_ITEM));
#endif
/* undo what alloc() did to the malloc result address */
bufptr = (void *)(cp - ALLOC_SIZE);
bufptr = (void *)(cp - sizeof(ALLOC_ITEM));
/* PIE or something similar provides us with deltas here */
bufptr->dataptr = &rndsetupstate;
/* ASLR in at least Windows, Linux, some BSDs */
@ -931,9 +931,9 @@ newenv(int type)
* struct env includes ALLOC_ITEM for alignment constraints
* so first get the actually used memory, then assign it
*/
cp = alloc(sizeof(struct env) - ALLOC_SIZE, ATEMP);
cp = alloc(sizeof(struct env) - sizeof(ALLOC_ITEM), ATEMP);
/* undo what alloc() did to the malloc result address */
ep = (void *)(cp - ALLOC_SIZE);
ep = (void *)(cp - sizeof(ALLOC_ITEM));
/* initialise public members of struct env (not the ALLOC_ITEM) */
ainit(&ep->area);
ep->oenv = e;
@ -1029,7 +1029,7 @@ quitenv(struct shf *shf)
/* free the struct env - tricky due to the ALLOC_ITEM inside */
cp = (void *)ep;
afree(cp + ALLOC_SIZE, ATEMP);
afree(cp + sizeof(ALLOC_ITEM), ATEMP);
}
/* Called after a fork to cleanup stuff left over from parents environment */

14
sh.h
View File

@ -175,7 +175,7 @@
#endif
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.764 2016/02/26 20:56:45 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.765 2016/02/26 21:53:37 tg Exp $");
#endif
#define MKSH_VERSION "R52 2016/02/26"
@ -703,21 +703,21 @@ struct lalloc_common {
struct lalloc_common *next;
};
#ifdef MKSH_ALLOC_CATCH_UNDERRUNS
struct lalloc_item {
struct lalloc_common *next;
#ifdef MKSH_ALLOC_CATCH_UNDERRUNS
size_t len;
char dummy[8192 - sizeof(struct lalloc_common *) - sizeof(size_t)];
#endif
};
#endif
/* 2. sizes */
#ifdef MKSH_ALLOC_CATCH_UNDERRUNS
#define ALLOC_ITEM struct lalloc_item
#define ALLOC_SIZE (sizeof(ALLOC_ITEM))
#ifndef MKSH_ALLOC_CATCH_UNDERRUNS
#define ALLOC_OVERHEAD ALLOC_SIZE
#else
#define ALLOC_OVERHEAD 0
#else
#define ALLOC_ITEM struct lalloc_common
#define ALLOC_OVERHEAD (sizeof(ALLOC_ITEM))
#endif
/* 3. group structure */