1 /*-
2  * Copyright (c) 2009, 2010, 2011, 2013, 2014, 2016
3  *	mirabilos <m@mirbsd.org>
4  *
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.
10  *
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.
19  */
20 
21 #include "sh.h"
22 #ifdef MKSH_ALLOC_CATCH_UNDERRUNS
23 #include <err.h>
24 #endif
25 
26 __RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.26 2016/02/26 21:53:36 tg Exp $");
27 
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)))
31 #else
32 #define remalloc(p,n)	realloc_osi((p), (n))
33 #endif
34 
35 
36 static struct lalloc_common *findptr(struct lalloc_common **, char *, Area *);
37 
38 #ifndef MKSH_ALLOC_CATCH_UNDERRUNS
39 #define ALLOC_ISUNALIGNED(p) (((size_t)(p)) % sizeof(struct lalloc_common))
40 #else
41 #define ALLOC_ISUNALIGNED(p) (((size_t)(p)) & 4095)
42 #undef remalloc
43 #undef free_osimalloc
44 
45 static void
free_osimalloc(void * ptr)46 free_osimalloc(void *ptr)
47 {
48 	struct lalloc_item *lp = ptr;
49 
50 	if (munmap(lp, lp->len))
51 		err(1, "free_osimalloc");
52 }
53 
54 static void *
remalloc(void * ptr,size_t size)55 remalloc(void *ptr, size_t size)
56 {
57 	struct lalloc_item *lp, *lold = ptr;
58 
59 	size = (size + 4095) & ~(size_t)4095;
60 
61 	if (lold && lold->len >= size)
62 		return (ptr);
63 
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");
71 	lp->len = size;
72 
73 	if (lold) {
74 		memcpy(((char *)lp) + 8192, ((char *)lold) + 8192,
75 		    lold->len - 8192);
76 		if (munmap(lold, lold->len))
77 			err(1, "remalloc: munmap");
78 	}
79 
80 	return (lp);
81 }
82 #endif
83 
84 void
ainit(Area * ap)85 ainit(Area *ap)
86 {
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));
91 		fflush(stderr);
92 		abort();
93 	}
94 #endif
95 	/* area pointer and items share struct lalloc_common */
96 	ap->next = NULL;
97 }
98 
99 static struct lalloc_common *
findptr(struct lalloc_common ** lpp,char * ptr,Area * ap)100 findptr(struct lalloc_common **lpp, char *ptr, Area *ap)
101 {
102 	void *lp;
103 
104 #ifndef MKSH_SMALL
105 	if (ALLOC_ISUNALIGNED(ptr))
106 		goto fail;
107 #endif
108 	/* get address of ALLOC_ITEM from user item */
109 	/*
110 	 * note: the alignment of "ptr" to ALLOC_ITEM is checked
111 	 * above; the "void *" gets us rid of a gcc 2.95 warning
112 	 */
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) {
117 #ifndef MKSH_SMALL
118  fail:
119 #endif
120 #ifdef DEBUG
121 			internal_warningf("rogue pointer %zX in ap %zX",
122 			    (size_t)ptr, (size_t)ap);
123 			/* try to get a coredump */
124 			abort();
125 #else
126 			internal_errorf("rogue pointer %zX", (size_t)ptr);
127 #endif
128 		}
129 	return (ap);
130 }
131 
132 void *
aresize2(void * ptr,size_t fac1,size_t fac2,Area * ap)133 aresize2(void *ptr, size_t fac1, size_t fac2, Area *ap)
134 {
135 	if (notoktomul(fac1, fac2))
136 		internal_errorf(Tintovfl, fac1, '*', fac2);
137 	return (aresize(ptr, fac1 * fac2, ap));
138 }
139 
140 void *
aresize(void * ptr,size_t numb,Area * ap)141 aresize(void *ptr, size_t numb, Area *ap)
142 {
143 	struct lalloc_common *lp = NULL;
144 
145 	/* resizing (true) or newly allocating? */
146 	if (ptr != NULL) {
147 		struct lalloc_common *pp;
148 
149 		pp = findptr(&lp, ptr, ap);
150 		pp->next = lp->next;
151 	}
152 
153 	if (notoktoadd(numb, sizeof(ALLOC_ITEM)) ||
154 	    (lp = remalloc(lp, numb + sizeof(ALLOC_ITEM))) == NULL
155 #ifndef MKSH_SMALL
156 	    || ALLOC_ISUNALIGNED(lp)
157 #endif
158 	    )
159 		internal_errorf(Toomem, numb);
160 	/* area pointer and items share struct lalloc_common */
161 	lp->next = ap->next;
162 	ap->next = lp;
163 	/* return user item address */
164 	return ((char *)lp + sizeof(ALLOC_ITEM));
165 }
166 
167 void
afree(void * ptr,Area * ap)168 afree(void *ptr, Area *ap)
169 {
170 	if (ptr != NULL) {
171 		struct lalloc_common *lp, *pp;
172 
173 		pp = findptr(&lp, ptr, ap);
174 		/* unhook */
175 		pp->next = lp->next;
176 		/* now free ALLOC_ITEM */
177 		free_osimalloc(lp);
178 	}
179 }
180 
181 void
afreeall(Area * ap)182 afreeall(Area *ap)
183 {
184 	struct lalloc_common *lp;
185 
186 	/* traverse group (linked list) */
187 	while ((lp = ap->next) != NULL) {
188 		/* make next ALLOC_ITEM head of list */
189 		ap->next = lp->next;
190 		/* free old head */
191 		free_osimalloc(lp);
192 	}
193 }
194