1*7c356e86SAndroid Build Coastguard Worker /*-
2*7c356e86SAndroid Build Coastguard Worker * Copyright (c) 2009, 2010, 2011, 2013, 2014, 2016
3*7c356e86SAndroid Build Coastguard Worker * mirabilos <[email protected]>
4*7c356e86SAndroid Build Coastguard Worker *
5*7c356e86SAndroid Build Coastguard Worker * Provided that these terms and disclaimer and all copyright notices
6*7c356e86SAndroid Build Coastguard Worker * are retained or reproduced in an accompanying document, permission
7*7c356e86SAndroid Build Coastguard Worker * is granted to deal in this work without restriction, including un-
8*7c356e86SAndroid Build Coastguard Worker * limited rights to use, publicly perform, distribute, sell, modify,
9*7c356e86SAndroid Build Coastguard Worker * merge, give away, or sublicence.
10*7c356e86SAndroid Build Coastguard Worker *
11*7c356e86SAndroid Build Coastguard Worker * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
12*7c356e86SAndroid Build Coastguard Worker * the utmost extent permitted by applicable law, neither express nor
13*7c356e86SAndroid Build Coastguard Worker * implied; without malicious intent or gross negligence. In no event
14*7c356e86SAndroid Build Coastguard Worker * may a licensor, author or contributor be held liable for indirect,
15*7c356e86SAndroid Build Coastguard Worker * direct, other damage, loss, or other issues arising in any way out
16*7c356e86SAndroid Build Coastguard Worker * of dealing in the work, even if advised of the possibility of such
17*7c356e86SAndroid Build Coastguard Worker * damage or existence of a defect, except proven that it results out
18*7c356e86SAndroid Build Coastguard Worker * of said person's immediate fault when using the work as intended.
19*7c356e86SAndroid Build Coastguard Worker */
20*7c356e86SAndroid Build Coastguard Worker
21*7c356e86SAndroid Build Coastguard Worker #include "sh.h"
22*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_ALLOC_CATCH_UNDERRUNS
23*7c356e86SAndroid Build Coastguard Worker #include <err.h>
24*7c356e86SAndroid Build Coastguard Worker #endif
25*7c356e86SAndroid Build Coastguard Worker
26*7c356e86SAndroid Build Coastguard Worker __RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.26 2016/02/26 21:53:36 tg Exp $");
27*7c356e86SAndroid Build Coastguard Worker
28*7c356e86SAndroid Build Coastguard Worker /* build with CPPFLAGS+= -DUSE_REALLOC_MALLOC=0 on ancient systems */
29*7c356e86SAndroid Build Coastguard Worker #if defined(USE_REALLOC_MALLOC) && (USE_REALLOC_MALLOC == 0)
30*7c356e86SAndroid Build Coastguard Worker #define remalloc(p,n) ((p) == NULL ? malloc_osi(n) : realloc_osi((p), (n)))
31*7c356e86SAndroid Build Coastguard Worker #else
32*7c356e86SAndroid Build Coastguard Worker #define remalloc(p,n) realloc_osi((p), (n))
33*7c356e86SAndroid Build Coastguard Worker #endif
34*7c356e86SAndroid Build Coastguard Worker
35*7c356e86SAndroid Build Coastguard Worker
36*7c356e86SAndroid Build Coastguard Worker static struct lalloc_common *findptr(struct lalloc_common **, char *, Area *);
37*7c356e86SAndroid Build Coastguard Worker
38*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_ALLOC_CATCH_UNDERRUNS
39*7c356e86SAndroid Build Coastguard Worker #define ALLOC_ISUNALIGNED(p) (((size_t)(p)) % sizeof(struct lalloc_common))
40*7c356e86SAndroid Build Coastguard Worker #else
41*7c356e86SAndroid Build Coastguard Worker #define ALLOC_ISUNALIGNED(p) (((size_t)(p)) & 4095)
42*7c356e86SAndroid Build Coastguard Worker #undef remalloc
43*7c356e86SAndroid Build Coastguard Worker #undef free_osimalloc
44*7c356e86SAndroid Build Coastguard Worker
45*7c356e86SAndroid Build Coastguard Worker static void
free_osimalloc(void * ptr)46*7c356e86SAndroid Build Coastguard Worker free_osimalloc(void *ptr)
47*7c356e86SAndroid Build Coastguard Worker {
48*7c356e86SAndroid Build Coastguard Worker struct lalloc_item *lp = ptr;
49*7c356e86SAndroid Build Coastguard Worker
50*7c356e86SAndroid Build Coastguard Worker if (munmap(lp, lp->len))
51*7c356e86SAndroid Build Coastguard Worker err(1, "free_osimalloc");
52*7c356e86SAndroid Build Coastguard Worker }
53*7c356e86SAndroid Build Coastguard Worker
54*7c356e86SAndroid Build Coastguard Worker static void *
remalloc(void * ptr,size_t size)55*7c356e86SAndroid Build Coastguard Worker remalloc(void *ptr, size_t size)
56*7c356e86SAndroid Build Coastguard Worker {
57*7c356e86SAndroid Build Coastguard Worker struct lalloc_item *lp, *lold = ptr;
58*7c356e86SAndroid Build Coastguard Worker
59*7c356e86SAndroid Build Coastguard Worker size = (size + 4095) & ~(size_t)4095;
60*7c356e86SAndroid Build Coastguard Worker
61*7c356e86SAndroid Build Coastguard Worker if (lold && lold->len >= size)
62*7c356e86SAndroid Build Coastguard Worker return (ptr);
63*7c356e86SAndroid Build Coastguard Worker
64*7c356e86SAndroid Build Coastguard Worker if ((lp = mmap(NULL, size, PROT_READ | PROT_WRITE,
65*7c356e86SAndroid Build Coastguard Worker MAP_ANON | MAP_PRIVATE, -1, (off_t)0)) == MAP_FAILED)
66*7c356e86SAndroid Build Coastguard Worker err(1, "remalloc: mmap(%zu)", size);
67*7c356e86SAndroid Build Coastguard Worker if (ALLOC_ISUNALIGNED(lp))
68*7c356e86SAndroid Build Coastguard Worker errx(1, "remalloc: unaligned(%p)", lp);
69*7c356e86SAndroid Build Coastguard Worker if (mprotect(((char *)lp) + 4096, 4096, PROT_NONE))
70*7c356e86SAndroid Build Coastguard Worker err(1, "remalloc: mprotect");
71*7c356e86SAndroid Build Coastguard Worker lp->len = size;
72*7c356e86SAndroid Build Coastguard Worker
73*7c356e86SAndroid Build Coastguard Worker if (lold) {
74*7c356e86SAndroid Build Coastguard Worker memcpy(((char *)lp) + 8192, ((char *)lold) + 8192,
75*7c356e86SAndroid Build Coastguard Worker lold->len - 8192);
76*7c356e86SAndroid Build Coastguard Worker if (munmap(lold, lold->len))
77*7c356e86SAndroid Build Coastguard Worker err(1, "remalloc: munmap");
78*7c356e86SAndroid Build Coastguard Worker }
79*7c356e86SAndroid Build Coastguard Worker
80*7c356e86SAndroid Build Coastguard Worker return (lp);
81*7c356e86SAndroid Build Coastguard Worker }
82*7c356e86SAndroid Build Coastguard Worker #endif
83*7c356e86SAndroid Build Coastguard Worker
84*7c356e86SAndroid Build Coastguard Worker void
ainit(Area * ap)85*7c356e86SAndroid Build Coastguard Worker ainit(Area *ap)
86*7c356e86SAndroid Build Coastguard Worker {
87*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_ALLOC_CATCH_UNDERRUNS
88*7c356e86SAndroid Build Coastguard Worker if (sysconf(_SC_PAGESIZE) != 4096) {
89*7c356e86SAndroid Build Coastguard Worker fprintf(stderr, "mksh: fatal: pagesize %lu not 4096!\n",
90*7c356e86SAndroid Build Coastguard Worker sysconf(_SC_PAGESIZE));
91*7c356e86SAndroid Build Coastguard Worker fflush(stderr);
92*7c356e86SAndroid Build Coastguard Worker abort();
93*7c356e86SAndroid Build Coastguard Worker }
94*7c356e86SAndroid Build Coastguard Worker #endif
95*7c356e86SAndroid Build Coastguard Worker /* area pointer and items share struct lalloc_common */
96*7c356e86SAndroid Build Coastguard Worker ap->next = NULL;
97*7c356e86SAndroid Build Coastguard Worker }
98*7c356e86SAndroid Build Coastguard Worker
99*7c356e86SAndroid Build Coastguard Worker static struct lalloc_common *
findptr(struct lalloc_common ** lpp,char * ptr,Area * ap)100*7c356e86SAndroid Build Coastguard Worker findptr(struct lalloc_common **lpp, char *ptr, Area *ap)
101*7c356e86SAndroid Build Coastguard Worker {
102*7c356e86SAndroid Build Coastguard Worker void *lp;
103*7c356e86SAndroid Build Coastguard Worker
104*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_SMALL
105*7c356e86SAndroid Build Coastguard Worker if (ALLOC_ISUNALIGNED(ptr))
106*7c356e86SAndroid Build Coastguard Worker goto fail;
107*7c356e86SAndroid Build Coastguard Worker #endif
108*7c356e86SAndroid Build Coastguard Worker /* get address of ALLOC_ITEM from user item */
109*7c356e86SAndroid Build Coastguard Worker /*
110*7c356e86SAndroid Build Coastguard Worker * note: the alignment of "ptr" to ALLOC_ITEM is checked
111*7c356e86SAndroid Build Coastguard Worker * above; the "void *" gets us rid of a gcc 2.95 warning
112*7c356e86SAndroid Build Coastguard Worker */
113*7c356e86SAndroid Build Coastguard Worker *lpp = (lp = ptr - sizeof(ALLOC_ITEM));
114*7c356e86SAndroid Build Coastguard Worker /* search for allocation item in group list */
115*7c356e86SAndroid Build Coastguard Worker while (ap->next != lp)
116*7c356e86SAndroid Build Coastguard Worker if ((ap = ap->next) == NULL) {
117*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_SMALL
118*7c356e86SAndroid Build Coastguard Worker fail:
119*7c356e86SAndroid Build Coastguard Worker #endif
120*7c356e86SAndroid Build Coastguard Worker #ifdef DEBUG
121*7c356e86SAndroid Build Coastguard Worker internal_warningf("rogue pointer %zX in ap %zX",
122*7c356e86SAndroid Build Coastguard Worker (size_t)ptr, (size_t)ap);
123*7c356e86SAndroid Build Coastguard Worker /* try to get a coredump */
124*7c356e86SAndroid Build Coastguard Worker abort();
125*7c356e86SAndroid Build Coastguard Worker #else
126*7c356e86SAndroid Build Coastguard Worker internal_errorf("rogue pointer %zX", (size_t)ptr);
127*7c356e86SAndroid Build Coastguard Worker #endif
128*7c356e86SAndroid Build Coastguard Worker }
129*7c356e86SAndroid Build Coastguard Worker return (ap);
130*7c356e86SAndroid Build Coastguard Worker }
131*7c356e86SAndroid Build Coastguard Worker
132*7c356e86SAndroid Build Coastguard Worker void *
aresize2(void * ptr,size_t fac1,size_t fac2,Area * ap)133*7c356e86SAndroid Build Coastguard Worker aresize2(void *ptr, size_t fac1, size_t fac2, Area *ap)
134*7c356e86SAndroid Build Coastguard Worker {
135*7c356e86SAndroid Build Coastguard Worker if (notoktomul(fac1, fac2))
136*7c356e86SAndroid Build Coastguard Worker internal_errorf(Tintovfl, fac1, '*', fac2);
137*7c356e86SAndroid Build Coastguard Worker return (aresize(ptr, fac1 * fac2, ap));
138*7c356e86SAndroid Build Coastguard Worker }
139*7c356e86SAndroid Build Coastguard Worker
140*7c356e86SAndroid Build Coastguard Worker void *
aresize(void * ptr,size_t numb,Area * ap)141*7c356e86SAndroid Build Coastguard Worker aresize(void *ptr, size_t numb, Area *ap)
142*7c356e86SAndroid Build Coastguard Worker {
143*7c356e86SAndroid Build Coastguard Worker struct lalloc_common *lp = NULL;
144*7c356e86SAndroid Build Coastguard Worker
145*7c356e86SAndroid Build Coastguard Worker /* resizing (true) or newly allocating? */
146*7c356e86SAndroid Build Coastguard Worker if (ptr != NULL) {
147*7c356e86SAndroid Build Coastguard Worker struct lalloc_common *pp;
148*7c356e86SAndroid Build Coastguard Worker
149*7c356e86SAndroid Build Coastguard Worker pp = findptr(&lp, ptr, ap);
150*7c356e86SAndroid Build Coastguard Worker pp->next = lp->next;
151*7c356e86SAndroid Build Coastguard Worker }
152*7c356e86SAndroid Build Coastguard Worker
153*7c356e86SAndroid Build Coastguard Worker if (notoktoadd(numb, sizeof(ALLOC_ITEM)) ||
154*7c356e86SAndroid Build Coastguard Worker (lp = remalloc(lp, numb + sizeof(ALLOC_ITEM))) == NULL
155*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_SMALL
156*7c356e86SAndroid Build Coastguard Worker || ALLOC_ISUNALIGNED(lp)
157*7c356e86SAndroid Build Coastguard Worker #endif
158*7c356e86SAndroid Build Coastguard Worker )
159*7c356e86SAndroid Build Coastguard Worker internal_errorf(Toomem, numb);
160*7c356e86SAndroid Build Coastguard Worker /* area pointer and items share struct lalloc_common */
161*7c356e86SAndroid Build Coastguard Worker lp->next = ap->next;
162*7c356e86SAndroid Build Coastguard Worker ap->next = lp;
163*7c356e86SAndroid Build Coastguard Worker /* return user item address */
164*7c356e86SAndroid Build Coastguard Worker return ((char *)lp + sizeof(ALLOC_ITEM));
165*7c356e86SAndroid Build Coastguard Worker }
166*7c356e86SAndroid Build Coastguard Worker
167*7c356e86SAndroid Build Coastguard Worker void
afree(void * ptr,Area * ap)168*7c356e86SAndroid Build Coastguard Worker afree(void *ptr, Area *ap)
169*7c356e86SAndroid Build Coastguard Worker {
170*7c356e86SAndroid Build Coastguard Worker if (ptr != NULL) {
171*7c356e86SAndroid Build Coastguard Worker struct lalloc_common *lp, *pp;
172*7c356e86SAndroid Build Coastguard Worker
173*7c356e86SAndroid Build Coastguard Worker pp = findptr(&lp, ptr, ap);
174*7c356e86SAndroid Build Coastguard Worker /* unhook */
175*7c356e86SAndroid Build Coastguard Worker pp->next = lp->next;
176*7c356e86SAndroid Build Coastguard Worker /* now free ALLOC_ITEM */
177*7c356e86SAndroid Build Coastguard Worker free_osimalloc(lp);
178*7c356e86SAndroid Build Coastguard Worker }
179*7c356e86SAndroid Build Coastguard Worker }
180*7c356e86SAndroid Build Coastguard Worker
181*7c356e86SAndroid Build Coastguard Worker void
afreeall(Area * ap)182*7c356e86SAndroid Build Coastguard Worker afreeall(Area *ap)
183*7c356e86SAndroid Build Coastguard Worker {
184*7c356e86SAndroid Build Coastguard Worker struct lalloc_common *lp;
185*7c356e86SAndroid Build Coastguard Worker
186*7c356e86SAndroid Build Coastguard Worker /* traverse group (linked list) */
187*7c356e86SAndroid Build Coastguard Worker while ((lp = ap->next) != NULL) {
188*7c356e86SAndroid Build Coastguard Worker /* make next ALLOC_ITEM head of list */
189*7c356e86SAndroid Build Coastguard Worker ap->next = lp->next;
190*7c356e86SAndroid Build Coastguard Worker /* free old head */
191*7c356e86SAndroid Build Coastguard Worker free_osimalloc(lp);
192*7c356e86SAndroid Build Coastguard Worker }
193*7c356e86SAndroid Build Coastguard Worker }
194