2 * Copyright (c) 2009, 2010, 2011, 2013, 2014, 2016
3 * mirabilos <m@mirbsd.org>
5 * Provided that these terms and disclaimer and all copyright notices
6 * are retained or reproduced in an accompanying document, permission
7 * is granted to deal in this work without restriction, including un-
8 * limited rights to use, publicly perform, distribute, sell, modify,
9 * merge, give away, or sublicence.
11 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
12 * the utmost extent permitted by applicable law, neither express nor
13 * implied; without malicious intent or gross negligence. In no event
14 * may a licensor, author or contributor be held liable for indirect,
15 * direct, other damage, loss, or other issues arising in any way out
16 * of dealing in the work, even if advised of the possibility of such
17 * damage or existence of a defect, except proven that it results out
18 * of said person's immediate fault when using the work as intended.
22 #ifdef MKSH_ALLOC_CATCH_UNDERRUNS
26 __RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.26 2016/02/26 21:53:36 tg Exp $");
28 /* build with CPPFLAGS+= -DUSE_REALLOC_MALLOC=0 on ancient systems */
29 #if defined(USE_REALLOC_MALLOC) && (USE_REALLOC_MALLOC == 0)
30 #define remalloc(p,n) ((p) == NULL ? malloc_osi(n) : realloc_osi((p), (n)))
32 #define remalloc(p,n) realloc_osi((p), (n))
36 static struct lalloc_common *findptr(struct lalloc_common **, char *, Area *);
38 #ifndef MKSH_ALLOC_CATCH_UNDERRUNS
39 #define ALLOC_ISUNALIGNED(p) (((size_t)(p)) % sizeof(struct lalloc_common))
41 #define ALLOC_ISUNALIGNED(p) (((size_t)(p)) & 4095)
46 free_osimalloc(void *ptr)
48 struct lalloc_item *lp = ptr;
50 if (munmap(lp, lp->len))
51 err(1, "free_osimalloc");
55 remalloc(void *ptr, size_t size)
57 struct lalloc_item *lp, *lold = ptr;
59 size = (size + 4095) & ~(size_t)4095;
61 if (lold && lold->len >= size)
64 if ((lp = mmap(NULL, size, PROT_READ | PROT_WRITE,
65 MAP_ANON | MAP_PRIVATE, -1, (off_t)0)) == MAP_FAILED)
66 err(1, "remalloc: mmap(%zu)", size);
67 if (ALLOC_ISUNALIGNED(lp))
68 errx(1, "remalloc: unaligned(%p)", lp);
69 if (mprotect(((char *)lp) + 4096, 4096, PROT_NONE))
70 err(1, "remalloc: mprotect");
74 memcpy(((char *)lp) + 8192, ((char *)lold) + 8192,
76 if (munmap(lold, lold->len))
77 err(1, "remalloc: munmap");
87 #ifdef MKSH_ALLOC_CATCH_UNDERRUNS
88 if (sysconf(_SC_PAGESIZE) != 4096) {
89 fprintf(stderr, "mksh: fatal: pagesize %lu not 4096!\n",
90 sysconf(_SC_PAGESIZE));
95 /* area pointer and items share struct lalloc_common */
99 static struct lalloc_common *
100 findptr(struct lalloc_common **lpp, char *ptr, Area *ap)
105 if (ALLOC_ISUNALIGNED(ptr))
108 /* get address of ALLOC_ITEM from user item */
110 * note: the alignment of "ptr" to ALLOC_ITEM is checked
111 * above; the "void *" gets us rid of a gcc 2.95 warning
113 *lpp = (lp = ptr - sizeof(ALLOC_ITEM));
114 /* search for allocation item in group list */
115 while (ap->next != lp)
116 if ((ap = ap->next) == NULL) {
121 internal_warningf("rogue pointer %zX in ap %zX",
122 (size_t)ptr, (size_t)ap);
123 /* try to get a coredump */
126 internal_errorf("rogue pointer %zX", (size_t)ptr);
133 aresize2(void *ptr, size_t fac1, size_t fac2, Area *ap)
135 if (notoktomul(fac1, fac2))
136 internal_errorf(Tintovfl, fac1, '*', fac2);
137 return (aresize(ptr, fac1 * fac2, ap));
141 aresize(void *ptr, size_t numb, Area *ap)
143 struct lalloc_common *lp = NULL;
145 /* resizing (true) or newly allocating? */
147 struct lalloc_common *pp;
149 pp = findptr(&lp, ptr, ap);
153 if (notoktoadd(numb, sizeof(ALLOC_ITEM)) ||
154 (lp = remalloc(lp, numb + sizeof(ALLOC_ITEM))) == NULL
156 || ALLOC_ISUNALIGNED(lp)
159 internal_errorf(Toomem, numb);
160 /* area pointer and items share struct lalloc_common */
163 /* return user item address */
164 return ((char *)lp + sizeof(ALLOC_ITEM));
168 afree(void *ptr, Area *ap)
171 struct lalloc_common *lp, *pp;
173 pp = findptr(&lp, ptr, ap);
176 /* now free ALLOC_ITEM */
184 struct lalloc_common *lp;
186 /* traverse group (linked list) */
187 while ((lp = ap->next) != NULL) {
188 /* make next ALLOC_ITEM head of list */