xref: /freebsd/bin/sh/memalloc.c (revision 90aea514c6249118e880d75972d063362f4bf492)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kenneth Almquist.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lint
36 #endif /* not lint */
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include "shell.h"
40 #include "output.h"
41 #include "memalloc.h"
42 #include "error.h"
43 #include "mystring.h"
44 #include "expand.h"
45 #include <stdlib.h>
46 #include <unistd.h>
47 
48 static void
49 badalloc(const char *message)
50 {
51 	write(2, message, strlen(message));
52 	abort();
53 }
54 
55 /*
56  * Like malloc, but returns an error when out of space.
57  */
58 
59 pointer
60 ckmalloc(size_t nbytes)
61 {
62 	pointer p;
63 
64 	if (!is_int_on())
65 		badalloc("Unsafe ckmalloc() call\n");
66 	p = malloc(nbytes);
67 	if (p == NULL)
68 		error("Out of space");
69 	return p;
70 }
71 
72 
73 /*
74  * Same for realloc.
75  */
76 
77 pointer
78 ckrealloc(pointer p, int nbytes)
79 {
80 	if (!is_int_on())
81 		badalloc("Unsafe ckrealloc() call\n");
82 	p = realloc(p, nbytes);
83 	if (p == NULL)
84 		error("Out of space");
85 	return p;
86 }
87 
88 void
89 ckfree(pointer p)
90 {
91 	if (!is_int_on())
92 		badalloc("Unsafe ckfree() call\n");
93 	free(p);
94 }
95 
96 
97 /*
98  * Make a copy of a string in safe storage.
99  */
100 
101 char *
102 savestr(const char *s)
103 {
104 	char *p;
105 	size_t len;
106 
107 	len = strlen(s);
108 	p = ckmalloc(len + 1);
109 	memcpy(p, s, len + 1);
110 	return p;
111 }
112 
113 
114 /*
115  * Parse trees for commands are allocated in lifo order, so we use a stack
116  * to make this more efficient, and also to avoid all sorts of exception
117  * handling code to handle interrupts in the middle of a parse.
118  *
119  * The size 496 was chosen because with 16-byte alignment the total size
120  * for the allocated block is 512.
121  */
122 
123 #define MINSIZE 496		/* minimum size of a block. */
124 
125 
126 struct stack_block {
127 	struct stack_block *prev;
128 	/* Data follows */
129 };
130 #define SPACE(sp)	((char*)(sp) + ALIGN(sizeof(struct stack_block)))
131 
132 static struct stack_block *stackp;
133 char *stacknxt;
134 int stacknleft;
135 char *sstrend;
136 
137 
138 static void
139 stnewblock(int nbytes)
140 {
141 	struct stack_block *sp;
142 	int allocsize;
143 
144 	if (nbytes < MINSIZE)
145 		nbytes = MINSIZE;
146 
147 	allocsize = ALIGN(sizeof(struct stack_block)) + ALIGN(nbytes);
148 
149 	INTOFF;
150 	sp = ckmalloc(allocsize);
151 	sp->prev = stackp;
152 	stacknxt = SPACE(sp);
153 	stacknleft = allocsize - (stacknxt - (char*)sp);
154 	sstrend = stacknxt + stacknleft;
155 	stackp = sp;
156 	INTON;
157 }
158 
159 
160 pointer
161 stalloc(int nbytes)
162 {
163 	char *p;
164 
165 	nbytes = ALIGN(nbytes);
166 	if (nbytes > stacknleft)
167 		stnewblock(nbytes);
168 	p = stacknxt;
169 	stacknxt += nbytes;
170 	stacknleft -= nbytes;
171 	return p;
172 }
173 
174 
175 void
176 stunalloc(pointer p)
177 {
178 	if (p == NULL) {		/*DEBUG */
179 		write(STDERR_FILENO, "stunalloc\n", 10);
180 		abort();
181 	}
182 	stacknleft += stacknxt - (char *)p;
183 	stacknxt = p;
184 }
185 
186 
187 char *
188 stsavestr(const char *s)
189 {
190 	char *p;
191 	size_t len;
192 
193 	len = strlen(s);
194 	p = stalloc(len + 1);
195 	memcpy(p, s, len + 1);
196 	return p;
197 }
198 
199 
200 void
201 setstackmark(struct stackmark *mark)
202 {
203 	mark->stackp = stackp;
204 	mark->stacknxt = stacknxt;
205 	mark->stacknleft = stacknleft;
206 	/* Ensure this block stays in place. */
207 	if (stackp != NULL && stacknxt == SPACE(stackp))
208 		stalloc(1);
209 }
210 
211 
212 void
213 popstackmark(struct stackmark *mark)
214 {
215 	struct stack_block *sp;
216 
217 	INTOFF;
218 	while (stackp != mark->stackp) {
219 		sp = stackp;
220 		stackp = sp->prev;
221 		ckfree(sp);
222 	}
223 	stacknxt = mark->stacknxt;
224 	stacknleft = mark->stacknleft;
225 	if (stacknleft != 0)
226 		sstrend = stacknxt + stacknleft;
227 	else
228 		sstrend = stacknxt;
229 	INTON;
230 }
231 
232 
233 /*
234  * When the parser reads in a string, it wants to stick the string on the
235  * stack and only adjust the stack pointer when it knows how big the
236  * string is.  Stackblock (defined in stack.h) returns a pointer to a block
237  * of space on top of the stack and stackblocklen returns the length of
238  * this block.  Growstackblock will grow this space by at least one byte,
239  * possibly moving it (like realloc).  Grabstackblock actually allocates the
240  * part of the block that has been used.
241  */
242 
243 static void
244 growstackblock(int min)
245 {
246 	char *p;
247 	int newlen;
248 	char *oldspace;
249 	int oldlen;
250 	struct stack_block *sp;
251 	struct stack_block *oldstackp;
252 
253 	if (min < stacknleft)
254 		min = stacknleft;
255 	if ((unsigned int)min >=
256 	    INT_MAX / 2 - ALIGN(sizeof(struct stack_block)))
257 		error("Out of space");
258 	min += stacknleft;
259 	min += ALIGN(sizeof(struct stack_block));
260 	newlen = 512;
261 	while (newlen < min)
262 		newlen <<= 1;
263 	oldspace = stacknxt;
264 	oldlen = stacknleft;
265 
266 	if (stackp != NULL && stacknxt == SPACE(stackp)) {
267 		INTOFF;
268 		oldstackp = stackp;
269 		stackp = oldstackp->prev;
270 		sp = ckrealloc((pointer)oldstackp, newlen);
271 		sp->prev = stackp;
272 		stackp = sp;
273 		stacknxt = SPACE(sp);
274 		stacknleft = newlen - (stacknxt - (char*)sp);
275 		sstrend = stacknxt + stacknleft;
276 		INTON;
277 	} else {
278 		newlen -= ALIGN(sizeof(struct stack_block));
279 		p = stalloc(newlen);
280 		if (oldlen != 0)
281 			memcpy(p, oldspace, oldlen);
282 		stunalloc(p);
283 	}
284 }
285 
286 
287 
288 /*
289  * The following routines are somewhat easier to use that the above.
290  * The user declares a variable of type STACKSTR, which may be declared
291  * to be a register.  The macro STARTSTACKSTR initializes things.  Then
292  * the user uses the macro STPUTC to add characters to the string.  In
293  * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
294  * grown as necessary.  When the user is done, she can just leave the
295  * string there and refer to it using stackblock().  Or she can allocate
296  * the space for it using grabstackstr().  If it is necessary to allow
297  * someone else to use the stack temporarily and then continue to grow
298  * the string, the user should use grabstack to allocate the space, and
299  * then call ungrabstr(p) to return to the previous mode of operation.
300  *
301  * USTPUTC is like STPUTC except that it doesn't check for overflow.
302  * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
303  * is space for at least one character.
304  */
305 
306 static char *
307 growstrstackblock(int n, int min)
308 {
309 	growstackblock(min);
310 	return stackblock() + n;
311 }
312 
313 char *
314 growstackstr(void)
315 {
316 	int len;
317 
318 	len = stackblocksize();
319 	return (growstrstackblock(len, 0));
320 }
321 
322 
323 /*
324  * Called from CHECKSTRSPACE.
325  */
326 
327 char *
328 makestrspace(int min, char *p)
329 {
330 	int len;
331 
332 	len = p - stackblock();
333 	return (growstrstackblock(len, min));
334 }
335 
336 
337 char *
338 stputbin(const char *data, size_t len, char *p)
339 {
340 	CHECKSTRSPACE(len, p);
341 	memcpy(p, data, len);
342 	return (p + len);
343 }
344 
345 char *
346 stputs(const char *data, char *p)
347 {
348 	return (stputbin(data, strlen(data), p));
349 }
350