xref: /openbsd-src/lib/libc/stdlib/malloc.c (revision 760f5d4877df33e782c4175c0c112280e8adf092)
1 /*	$OpenBSD: malloc.c,v 1.283 2023/05/10 07:58:06 otto Exp $	*/
2 /*
3  * Copyright (c) 2008, 2010, 2011, 2016, 2023 Otto Moerbeek <otto@drijf.net>
4  * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org>
5  * Copyright (c) 2008 Damien Miller <djm@openbsd.org>
6  * Copyright (c) 2000 Poul-Henning Kamp <phk@FreeBSD.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 /*
22  * If we meet some day, and you think this stuff is worth it, you
23  * can buy me a beer in return. Poul-Henning Kamp
24  */
25 
26 #ifndef MALLOC_SMALL
27 #define MALLOC_STATS
28 #endif
29 
30 #include <sys/types.h>
31 #include <sys/queue.h>
32 #include <sys/mman.h>
33 #include <sys/sysctl.h>
34 #include <uvm/uvmexp.h>
35 #include <errno.h>
36 #include <stdarg.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #ifdef MALLOC_STATS
44 #include <sys/tree.h>
45 #include <sys/ktrace.h>
46 #include <dlfcn.h>
47 #endif
48 
49 #include "thread_private.h"
50 #include <tib.h>
51 
52 #define MALLOC_PAGESHIFT	_MAX_PAGE_SHIFT
53 
54 #define MALLOC_MINSHIFT		4
55 #define MALLOC_MAXSHIFT		(MALLOC_PAGESHIFT - 1)
56 #define MALLOC_PAGESIZE		(1UL << MALLOC_PAGESHIFT)
57 #define MALLOC_MINSIZE		(1UL << MALLOC_MINSHIFT)
58 #define MALLOC_PAGEMASK		(MALLOC_PAGESIZE - 1)
59 #define MASK_POINTER(p)		((void *)(((uintptr_t)(p)) & ~MALLOC_PAGEMASK))
60 
61 #define MALLOC_MAXCHUNK		(1 << MALLOC_MAXSHIFT)
62 #define MALLOC_MAXCACHE		256
63 #define MALLOC_DELAYED_CHUNK_MASK	15
64 #ifdef MALLOC_STATS
65 #define MALLOC_INITIAL_REGIONS	512
66 #else
67 #define MALLOC_INITIAL_REGIONS	(MALLOC_PAGESIZE / sizeof(struct region_info))
68 #endif
69 #define MALLOC_DEFAULT_CACHE	64
70 #define MALLOC_CHUNK_LISTS	4
71 #define CHUNK_CHECK_LENGTH	32
72 
73 #define B2SIZE(b)		((b) * MALLOC_MINSIZE)
74 #define B2ALLOC(b)		((b) == 0 ? MALLOC_MINSIZE : \
75 				    (b) * MALLOC_MINSIZE)
76 #define BUCKETS 		(MALLOC_MAXCHUNK / MALLOC_MINSIZE)
77 
78 /*
79  * We move allocations between half a page and a whole page towards the end,
80  * subject to alignment constraints. This is the extra headroom we allow.
81  * Set to zero to be the most strict.
82  */
83 #define MALLOC_LEEWAY		0
84 #define MALLOC_MOVE_COND(sz)	((sz) - mopts.malloc_guard < 		\
85 				    MALLOC_PAGESIZE - MALLOC_LEEWAY)
86 #define MALLOC_MOVE(p, sz)  	(((char *)(p)) +			\
87 				    ((MALLOC_PAGESIZE - MALLOC_LEEWAY -	\
88 			    	    ((sz) - mopts.malloc_guard)) & 	\
89 				    ~(MALLOC_MINSIZE - 1)))
90 
91 #define PAGEROUND(x)  (((x) + (MALLOC_PAGEMASK)) & ~MALLOC_PAGEMASK)
92 
93 /*
94  * What to use for Junk.  This is the byte value we use to fill with
95  * when the 'J' option is enabled. Use SOME_JUNK right after alloc,
96  * and SOME_FREEJUNK right before free.
97  */
98 #define SOME_JUNK		0xdb	/* deadbeef */
99 #define SOME_FREEJUNK		0xdf	/* dead, free */
100 #define SOME_FREEJUNK_ULL	0xdfdfdfdfdfdfdfdfULL
101 
102 #define MMAP(sz,f)	mmap(NULL, (sz), PROT_READ | PROT_WRITE, \
103     MAP_ANON | MAP_PRIVATE | (f), -1, 0)
104 
105 #define MMAPNONE(sz,f)	mmap(NULL, (sz), PROT_NONE, \
106     MAP_ANON | MAP_PRIVATE | (f), -1, 0)
107 
108 #define MMAPA(a,sz,f)	mmap((a), (sz), PROT_READ | PROT_WRITE, \
109     MAP_ANON | MAP_PRIVATE | (f), -1, 0)
110 
111 struct region_info {
112 	void *p;		/* page; low bits used to mark chunks */
113 	uintptr_t size;		/* size for pages, or chunk_info pointer */
114 #ifdef MALLOC_STATS
115 	void *f;		/* where allocated from */
116 #endif
117 };
118 
119 LIST_HEAD(chunk_head, chunk_info);
120 
121 /*
122  * Two caches, one for "small" regions, one for "big".
123  * Small cache is an array per size, big cache is one array with different
124  * sized regions
125  */
126 #define MAX_SMALLCACHEABLE_SIZE	32
127 #define MAX_BIGCACHEABLE_SIZE	512
128 /* If the total # of pages is larger than this, evict before inserting */
129 #define BIGCACHE_FILL(sz)	(MAX_BIGCACHEABLE_SIZE * (sz) / 4)
130 
131 struct smallcache {
132 	void **pages;
133 	ushort length;
134 	ushort max;
135 };
136 
137 struct bigcache {
138 	void *page;
139 	size_t psize;
140 };
141 
142 struct dir_info {
143 	u_int32_t canary1;
144 	int active;			/* status of malloc */
145 	struct region_info *r;		/* region slots */
146 	size_t regions_total;		/* number of region slots */
147 	size_t regions_free;		/* number of free slots */
148 	size_t rbytesused;		/* random bytes used */
149 	char *func;			/* current function */
150 	int malloc_junk;		/* junk fill? */
151 	int mmap_flag;			/* extra flag for mmap */
152 	int mutex;
153 	int malloc_mt;			/* multi-threaded mode? */
154 					/* lists of free chunk info structs */
155 	struct chunk_head chunk_info_list[BUCKETS + 1];
156 					/* lists of chunks with free slots */
157 	struct chunk_head chunk_dir[BUCKETS + 1][MALLOC_CHUNK_LISTS];
158 					/* delayed free chunk slots */
159 	void *delayed_chunks[MALLOC_DELAYED_CHUNK_MASK + 1];
160 	u_char rbytes[32];		/* random bytes */
161 					/* free pages cache */
162 	struct smallcache smallcache[MAX_SMALLCACHEABLE_SIZE];
163 	size_t bigcache_used;
164 	size_t bigcache_size;
165 	struct bigcache *bigcache;
166 	void *chunk_pages;
167 	size_t chunk_pages_used;
168 #ifdef MALLOC_STATS
169 	size_t inserts;
170 	size_t insert_collisions;
171 	size_t finds;
172 	size_t find_collisions;
173 	size_t deletes;
174 	size_t delete_moves;
175 	size_t cheap_realloc_tries;
176 	size_t cheap_reallocs;
177 	size_t malloc_used;		/* bytes allocated */
178 	size_t malloc_guarded;		/* bytes used for guards */
179 	size_t pool_searches;		/* searches for pool */
180 	size_t other_pool;		/* searches in other pool */
181 #define STATS_ADD(x,y)	((x) += (y))
182 #define STATS_SUB(x,y)	((x) -= (y))
183 #define STATS_INC(x)	((x)++)
184 #define STATS_ZERO(x)	((x) = 0)
185 #define STATS_SETF(x,y)	((x)->f = (y))
186 #else
187 #define STATS_ADD(x,y)	/* nothing */
188 #define STATS_SUB(x,y)	/* nothing */
189 #define STATS_INC(x)	/* nothing */
190 #define STATS_ZERO(x)	/* nothing */
191 #define STATS_SETF(x,y)	/* nothing */
192 #endif /* MALLOC_STATS */
193 	u_int32_t canary2;
194 };
195 
196 static void unmap(struct dir_info *d, void *p, size_t sz, size_t clear);
197 
198 /*
199  * This structure describes a page worth of chunks.
200  *
201  * How many bits per u_short in the bitmap
202  */
203 #define MALLOC_BITS		(NBBY * sizeof(u_short))
204 struct chunk_info {
205 	LIST_ENTRY(chunk_info) entries;
206 	void *page;			/* pointer to the page */
207 	u_short canary;
208 	u_short bucket;
209 	u_short free;			/* how many free chunks */
210 	u_short total;			/* how many chunks */
211 	u_short offset;			/* requested size table offset */
212 	u_short bits[1];		/* which chunks are free */
213 };
214 
215 struct malloc_readonly {
216 					/* Main bookkeeping information */
217 	struct dir_info *malloc_pool[_MALLOC_MUTEXES];
218 	u_int	malloc_mutexes;		/* how much in actual use? */
219 	int	malloc_freecheck;	/* Extensive double free check */
220 	int	malloc_freeunmap;	/* mprotect free pages PROT_NONE? */
221 	int	def_malloc_junk;	/* junk fill? */
222 	int	malloc_realloc;		/* always realloc? */
223 	int	malloc_xmalloc;		/* xmalloc behaviour? */
224 	u_int	chunk_canaries;		/* use canaries after chunks? */
225 	int	internal_funcs;		/* use better recallocarray/freezero? */
226 	u_int	def_maxcache;		/* free pages we cache */
227 	u_int	junk_loc;		/* variation in location of junk */
228 	size_t	malloc_guard;		/* use guard pages after allocations? */
229 #ifdef MALLOC_STATS
230 	int	malloc_stats;		/* dump leak report at end */
231 	int	malloc_verbose;		/* dump verbose statistics at end */
232 #define	DO_STATS	mopts.malloc_stats
233 #else
234 #define	DO_STATS	0
235 #endif
236 	u_int32_t malloc_canary;	/* Matched against ones in pool */
237 };
238 
239 
240 /* This object is mapped PROT_READ after initialisation to prevent tampering */
241 static union {
242 	struct malloc_readonly mopts;
243 	u_char _pad[MALLOC_PAGESIZE];
244 } malloc_readonly __attribute__((aligned(MALLOC_PAGESIZE)))
245 		__attribute__((section(".openbsd.mutable")));
246 #define mopts	malloc_readonly.mopts
247 
248 char		*malloc_options;	/* compile-time options */
249 
250 static __dead void wrterror(struct dir_info *d, char *msg, ...)
251     __attribute__((__format__ (printf, 2, 3)));
252 
253 #ifdef MALLOC_STATS
254 void malloc_dump(void);
255 PROTO_NORMAL(malloc_dump);
256 static void malloc_exit(void);
257 #endif
258 #define CALLER	(DO_STATS ? __builtin_return_address(0) : NULL)
259 
260 /* low bits of r->p determine size: 0 means >= page size and r->size holding
261  * real size, otherwise low bits is the bucket + 1
262  */
263 #define REALSIZE(sz, r)						\
264 	(sz) = (uintptr_t)(r)->p & MALLOC_PAGEMASK,		\
265 	(sz) = ((sz) == 0 ? (r)->size : B2SIZE((sz) - 1))
266 
267 static inline size_t
268 hash(void *p)
269 {
270 	size_t sum;
271 	uintptr_t u;
272 
273 	u = (uintptr_t)p >> MALLOC_PAGESHIFT;
274 	sum = u;
275 	sum = (sum << 7) - sum + (u >> 16);
276 #ifdef __LP64__
277 	sum = (sum << 7) - sum + (u >> 32);
278 	sum = (sum << 7) - sum + (u >> 48);
279 #endif
280 	return sum;
281 }
282 
283 static inline struct dir_info *
284 getpool(void)
285 {
286 	if (mopts.malloc_pool[1] == NULL || !mopts.malloc_pool[1]->malloc_mt)
287 		return mopts.malloc_pool[1];
288 	else	/* first one reserved for special pool */
289 		return mopts.malloc_pool[1 + TIB_GET()->tib_tid %
290 		    (mopts.malloc_mutexes - 1)];
291 }
292 
293 static __dead void
294 wrterror(struct dir_info *d, char *msg, ...)
295 {
296 	int		saved_errno = errno;
297 	va_list		ap;
298 
299 	dprintf(STDERR_FILENO, "%s(%d) in %s(): ", __progname,
300 	    getpid(), (d != NULL && d->func) ? d->func : "unknown");
301 	va_start(ap, msg);
302 	vdprintf(STDERR_FILENO, msg, ap);
303 	va_end(ap);
304 	dprintf(STDERR_FILENO, "\n");
305 
306 #ifdef MALLOC_STATS
307 	if (DO_STATS && mopts.malloc_verbose)
308 		malloc_dump();
309 #endif
310 
311 	errno = saved_errno;
312 
313 	abort();
314 }
315 
316 static void
317 rbytes_init(struct dir_info *d)
318 {
319 	arc4random_buf(d->rbytes, sizeof(d->rbytes));
320 	/* add 1 to account for using d->rbytes[0] */
321 	d->rbytesused = 1 + d->rbytes[0] % (sizeof(d->rbytes) / 2);
322 }
323 
324 static inline u_char
325 getrbyte(struct dir_info *d)
326 {
327 	u_char x;
328 
329 	if (d->rbytesused >= sizeof(d->rbytes))
330 		rbytes_init(d);
331 	x = d->rbytes[d->rbytesused++];
332 	return x;
333 }
334 
335 static void
336 omalloc_parseopt(char opt)
337 {
338 	switch (opt) {
339 	case '+':
340 		mopts.malloc_mutexes <<= 1;
341 		if (mopts.malloc_mutexes > _MALLOC_MUTEXES)
342 			mopts.malloc_mutexes = _MALLOC_MUTEXES;
343 		break;
344 	case '-':
345 		mopts.malloc_mutexes >>= 1;
346 		if (mopts.malloc_mutexes < 2)
347 			mopts.malloc_mutexes = 2;
348 		break;
349 	case '>':
350 		mopts.def_maxcache <<= 1;
351 		if (mopts.def_maxcache > MALLOC_MAXCACHE)
352 			mopts.def_maxcache = MALLOC_MAXCACHE;
353 		break;
354 	case '<':
355 		mopts.def_maxcache >>= 1;
356 		break;
357 	case 'c':
358 		mopts.chunk_canaries = 0;
359 		break;
360 	case 'C':
361 		mopts.chunk_canaries = 1;
362 		break;
363 #ifdef MALLOC_STATS
364 	case 'd':
365 		mopts.malloc_stats = 0;
366 		break;
367 	case 'D':
368 		mopts.malloc_stats = 1;
369 		break;
370 #endif /* MALLOC_STATS */
371 	case 'f':
372 		mopts.malloc_freecheck = 0;
373 		mopts.malloc_freeunmap = 0;
374 		break;
375 	case 'F':
376 		mopts.malloc_freecheck = 1;
377 		mopts.malloc_freeunmap = 1;
378 		break;
379 	case 'g':
380 		mopts.malloc_guard = 0;
381 		break;
382 	case 'G':
383 		mopts.malloc_guard = MALLOC_PAGESIZE;
384 		break;
385 	case 'j':
386 		if (mopts.def_malloc_junk > 0)
387 			mopts.def_malloc_junk--;
388 		break;
389 	case 'J':
390 		if (mopts.def_malloc_junk < 2)
391 			mopts.def_malloc_junk++;
392 		break;
393 	case 'r':
394 		mopts.malloc_realloc = 0;
395 		break;
396 	case 'R':
397 		mopts.malloc_realloc = 1;
398 		break;
399 	case 'u':
400 		mopts.malloc_freeunmap = 0;
401 		break;
402 	case 'U':
403 		mopts.malloc_freeunmap = 1;
404 		break;
405 #ifdef MALLOC_STATS
406 	case 'v':
407 		mopts.malloc_verbose = 0;
408 		break;
409 	case 'V':
410 		mopts.malloc_verbose = 1;
411 		break;
412 #endif /* MALLOC_STATS */
413 	case 'x':
414 		mopts.malloc_xmalloc = 0;
415 		break;
416 	case 'X':
417 		mopts.malloc_xmalloc = 1;
418 		break;
419 	default:
420 		dprintf(STDERR_FILENO, "malloc() warning: "
421                     "unknown char in MALLOC_OPTIONS\n");
422 		break;
423 	}
424 }
425 
426 static void
427 omalloc_init(void)
428 {
429 	char *p, *q, b[16];
430 	int i, j;
431 	const int mib[2] = { CTL_VM, VM_MALLOC_CONF };
432 	size_t sb;
433 
434 	/*
435 	 * Default options
436 	 */
437 	mopts.malloc_mutexes = 8;
438 	mopts.def_malloc_junk = 1;
439 	mopts.def_maxcache = MALLOC_DEFAULT_CACHE;
440 
441 	for (i = 0; i < 3; i++) {
442 		switch (i) {
443 		case 0:
444 			sb = sizeof(b);
445 			j = sysctl(mib, 2, b, &sb, NULL, 0);
446 			if (j != 0)
447 				continue;
448 			p = b;
449 			break;
450 		case 1:
451 			if (issetugid() == 0)
452 				p = getenv("MALLOC_OPTIONS");
453 			else
454 				continue;
455 			break;
456 		case 2:
457 			p = malloc_options;
458 			break;
459 		default:
460 			p = NULL;
461 		}
462 
463 		for (; p != NULL && *p != '\0'; p++) {
464 			switch (*p) {
465 			case 'S':
466 				for (q = "CFGJ"; *q != '\0'; q++)
467 					omalloc_parseopt(*q);
468 				mopts.def_maxcache = 0;
469 				break;
470 			case 's':
471 				for (q = "cfgj"; *q != '\0'; q++)
472 					omalloc_parseopt(*q);
473 				mopts.def_maxcache = MALLOC_DEFAULT_CACHE;
474 				break;
475 			default:
476 				omalloc_parseopt(*p);
477 				break;
478 			}
479 		}
480 	}
481 
482 #ifdef MALLOC_STATS
483 	if (DO_STATS && (atexit(malloc_exit) == -1)) {
484 		dprintf(STDERR_FILENO, "malloc() warning: atexit(2) failed."
485 		    " Will not be able to dump stats on exit\n");
486 	}
487 #endif
488 
489 	while ((mopts.malloc_canary = arc4random()) == 0)
490 		;
491 	mopts.junk_loc = arc4random();
492 	if (mopts.chunk_canaries)
493 		do {
494 			mopts.chunk_canaries = arc4random();
495 		} while ((u_char)mopts.chunk_canaries == 0 ||
496 		    (u_char)mopts.chunk_canaries == SOME_FREEJUNK);
497 }
498 
499 static void
500 omalloc_poolinit(struct dir_info *d, int mmap_flag)
501 {
502 	int i, j;
503 
504 	d->r = NULL;
505 	d->rbytesused = sizeof(d->rbytes);
506 	d->regions_free = d->regions_total = 0;
507 	for (i = 0; i <= BUCKETS; i++) {
508 		LIST_INIT(&d->chunk_info_list[i]);
509 		for (j = 0; j < MALLOC_CHUNK_LISTS; j++)
510 			LIST_INIT(&d->chunk_dir[i][j]);
511 	}
512 	d->mmap_flag = mmap_flag;
513 	d->malloc_junk = mopts.def_malloc_junk;
514 	d->canary1 = mopts.malloc_canary ^ (u_int32_t)(uintptr_t)d;
515 	d->canary2 = ~d->canary1;
516 }
517 
518 static int
519 omalloc_grow(struct dir_info *d)
520 {
521 	size_t newtotal;
522 	size_t newsize;
523 	size_t mask;
524 	size_t i, oldpsz;
525 	struct region_info *p;
526 
527 	if (d->regions_total > SIZE_MAX / sizeof(struct region_info) / 2)
528 		return 1;
529 
530 	newtotal = d->regions_total == 0 ? MALLOC_INITIAL_REGIONS :
531 	    d->regions_total * 2;
532 	newsize = PAGEROUND(newtotal * sizeof(struct region_info));
533 	mask = newtotal - 1;
534 
535 	/* Don't use cache here, we don't want user uaf touch this */
536 	p = MMAP(newsize, d->mmap_flag);
537 	if (p == MAP_FAILED)
538 		return 1;
539 
540 	STATS_ADD(d->malloc_used, newsize);
541 	STATS_ZERO(d->inserts);
542 	STATS_ZERO(d->insert_collisions);
543 	for (i = 0; i < d->regions_total; i++) {
544 		void *q = d->r[i].p;
545 		if (q != NULL) {
546 			size_t index = hash(q) & mask;
547 			STATS_INC(d->inserts);
548 			while (p[index].p != NULL) {
549 				index = (index - 1) & mask;
550 				STATS_INC(d->insert_collisions);
551 			}
552 			p[index] = d->r[i];
553 		}
554 	}
555 
556 	if (d->regions_total > 0) {
557 		oldpsz = PAGEROUND(d->regions_total * sizeof(struct region_info));
558 		/* clear to avoid meta info ending up in the cache */
559 		unmap(d, d->r, oldpsz, oldpsz);
560 	}
561 	d->regions_free += newtotal - d->regions_total;
562 	d->regions_total = newtotal;
563 	d->r = p;
564 	return 0;
565 }
566 
567 /*
568  * The hashtable uses the assumption that p is never NULL. This holds since
569  * non-MAP_FIXED mappings with hint 0 start at BRKSIZ.
570  */
571 static int
572 insert(struct dir_info *d, void *p, size_t sz, void *f)
573 {
574 	size_t index;
575 	size_t mask;
576 	void *q;
577 
578 	if (d->regions_free * 4 < d->regions_total || d->regions_total == 0) {
579 		if (omalloc_grow(d))
580 			return 1;
581 	}
582 	mask = d->regions_total - 1;
583 	index = hash(p) & mask;
584 	q = d->r[index].p;
585 	STATS_INC(d->inserts);
586 	while (q != NULL) {
587 		index = (index - 1) & mask;
588 		q = d->r[index].p;
589 		STATS_INC(d->insert_collisions);
590 	}
591 	d->r[index].p = p;
592 	d->r[index].size = sz;
593 	STATS_SETF(&d->r[index], f);
594 	d->regions_free--;
595 	return 0;
596 }
597 
598 static struct region_info *
599 find(struct dir_info *d, void *p)
600 {
601 	size_t index;
602 	size_t mask = d->regions_total - 1;
603 	void *q, *r;
604 
605 	if (mopts.malloc_canary != (d->canary1 ^ (u_int32_t)(uintptr_t)d) ||
606 	    d->canary1 != ~d->canary2)
607 		wrterror(d, "internal struct corrupt");
608 	if (d->r == NULL)
609 		return NULL;
610 	p = MASK_POINTER(p);
611 	index = hash(p) & mask;
612 	r = d->r[index].p;
613 	q = MASK_POINTER(r);
614 	STATS_INC(d->finds);
615 	while (q != p && r != NULL) {
616 		index = (index - 1) & mask;
617 		r = d->r[index].p;
618 		q = MASK_POINTER(r);
619 		STATS_INC(d->find_collisions);
620 	}
621 	return (q == p && r != NULL) ? &d->r[index] : NULL;
622 }
623 
624 static void
625 delete(struct dir_info *d, struct region_info *ri)
626 {
627 	/* algorithm R, Knuth Vol III section 6.4 */
628 	size_t mask = d->regions_total - 1;
629 	size_t i, j, r;
630 
631 	if (d->regions_total & (d->regions_total - 1))
632 		wrterror(d, "regions_total not 2^x");
633 	d->regions_free++;
634 	STATS_INC(d->deletes);
635 
636 	i = ri - d->r;
637 	for (;;) {
638 		d->r[i].p = NULL;
639 		d->r[i].size = 0;
640 		j = i;
641 		for (;;) {
642 			i = (i - 1) & mask;
643 			if (d->r[i].p == NULL)
644 				return;
645 			r = hash(d->r[i].p) & mask;
646 			if ((i <= r && r < j) || (r < j && j < i) ||
647 			    (j < i && i <= r))
648 				continue;
649 			d->r[j] = d->r[i];
650 			STATS_INC(d->delete_moves);
651 			break;
652 		}
653 
654 	}
655 }
656 
657 static inline void
658 junk_free(int junk, void *p, size_t sz)
659 {
660 	size_t i, step = 1;
661 	uint64_t *lp = p;
662 
663 	if (junk == 0 || sz == 0)
664 		return;
665 	sz /= sizeof(uint64_t);
666 	if (junk == 1) {
667 		if (sz > MALLOC_PAGESIZE / sizeof(uint64_t))
668 			sz = MALLOC_PAGESIZE / sizeof(uint64_t);
669 		step = sz / 4;
670 		if (step == 0)
671 			step = 1;
672 	}
673 	/* Do not always put the free junk bytes in the same spot.
674 	   There is modulo bias here, but we ignore that. */
675 	for (i = mopts.junk_loc % step; i < sz; i += step)
676 		lp[i] = SOME_FREEJUNK_ULL;
677 }
678 
679 static inline void
680 validate_junk(struct dir_info *pool, void *p, size_t sz)
681 {
682 	size_t i, step = 1;
683 	uint64_t *lp = p;
684 
685 	if (pool->malloc_junk == 0 || sz == 0)
686 		return;
687 	sz /= sizeof(uint64_t);
688 	if (pool->malloc_junk == 1) {
689 		if (sz > MALLOC_PAGESIZE / sizeof(uint64_t))
690 			sz = MALLOC_PAGESIZE / sizeof(uint64_t);
691 		step = sz / 4;
692 		if (step == 0)
693 			step = 1;
694 	}
695 	/* see junk_free */
696 	for (i = mopts.junk_loc % step; i < sz; i += step) {
697 		if (lp[i] != SOME_FREEJUNK_ULL)
698 			wrterror(pool, "write after free %p", p);
699 	}
700 }
701 
702 
703 /*
704  * Cache maintenance.
705  * Opposed to the regular region data structure, the sizes in the
706  * cache are in MALLOC_PAGESIZE units.
707  */
708 static void
709 unmap(struct dir_info *d, void *p, size_t sz, size_t clear)
710 {
711 	size_t psz = sz >> MALLOC_PAGESHIFT;
712 	void *r;
713 	u_short i;
714 	struct smallcache *cache;
715 
716 	if (sz != PAGEROUND(sz) || psz == 0)
717 		wrterror(d, "munmap round");
718 
719 	if (d->bigcache_size > 0 && psz > MAX_SMALLCACHEABLE_SIZE &&
720 	    psz <= MAX_BIGCACHEABLE_SIZE) {
721 		u_short base = getrbyte(d);
722 		u_short j;
723 
724 		/* don't look through all slots */
725 		for (j = 0; j < d->bigcache_size / 4; j++) {
726 			i = (base + j) & (d->bigcache_size - 1);
727 			if (d->bigcache_used <
728 			    BIGCACHE_FILL(d->bigcache_size))  {
729 				if (d->bigcache[i].psize == 0)
730 					break;
731 			} else {
732 				if (d->bigcache[i].psize != 0)
733 					break;
734 			}
735 		}
736 		/* if we didn't find a preferred slot, use random one */
737 		if (d->bigcache[i].psize != 0) {
738 			size_t tmp;
739 
740 			r = d->bigcache[i].page;
741 			d->bigcache_used -= d->bigcache[i].psize;
742 			tmp = d->bigcache[i].psize << MALLOC_PAGESHIFT;
743 			if (!mopts.malloc_freeunmap)
744 				validate_junk(d, r, tmp);
745 			if (munmap(r, tmp))
746 				 wrterror(d, "munmap %p", r);
747 			STATS_SUB(d->malloc_used, tmp);
748 		}
749 
750 		if (clear > 0)
751 			explicit_bzero(p, clear);
752 		if (mopts.malloc_freeunmap) {
753 			if (mprotect(p, sz, PROT_NONE))
754 				wrterror(d, "mprotect %p", r);
755 		} else
756 			junk_free(d->malloc_junk, p, sz);
757 		d->bigcache[i].page = p;
758 		d->bigcache[i].psize = psz;
759 		d->bigcache_used += psz;
760 		return;
761 	}
762 	if (psz > MAX_SMALLCACHEABLE_SIZE || d->smallcache[psz - 1].max == 0) {
763 		if (munmap(p, sz))
764 			wrterror(d, "munmap %p", p);
765 		STATS_SUB(d->malloc_used, sz);
766 		return;
767 	}
768 	cache = &d->smallcache[psz - 1];
769 	if (cache->length == cache->max) {
770 		int fresh;
771 		/* use a random slot */
772 		i = getrbyte(d) & (cache->max - 1);
773 		r = cache->pages[i];
774 		fresh = (uintptr_t)r & 1;
775 		*(uintptr_t*)&r &= ~1ULL;
776 		if (!fresh && !mopts.malloc_freeunmap)
777 			validate_junk(d, r, sz);
778 		if (munmap(r, sz))
779 			wrterror(d, "munmap %p", r);
780 		STATS_SUB(d->malloc_used, sz);
781 		cache->length--;
782 	} else
783 		i = cache->length;
784 
785 	/* fill slot */
786 	if (clear > 0)
787 		explicit_bzero(p, clear);
788 	if (mopts.malloc_freeunmap)
789 		mprotect(p, sz, PROT_NONE);
790 	else
791 		junk_free(d->malloc_junk, p, sz);
792 	cache->pages[i] = p;
793 	cache->length++;
794 }
795 
796 static void *
797 map(struct dir_info *d, size_t sz, int zero_fill)
798 {
799 	size_t psz = sz >> MALLOC_PAGESHIFT;
800 	u_short i;
801 	void *p;
802 	struct smallcache *cache;
803 
804 	if (mopts.malloc_canary != (d->canary1 ^ (u_int32_t)(uintptr_t)d) ||
805 	    d->canary1 != ~d->canary2)
806 		wrterror(d, "internal struct corrupt");
807 	if (sz != PAGEROUND(sz) || psz == 0)
808 		wrterror(d, "map round");
809 
810 
811 	if (d->bigcache_size > 0 && psz > MAX_SMALLCACHEABLE_SIZE &&
812 	    psz <= MAX_BIGCACHEABLE_SIZE) {
813 		size_t base = getrbyte(d);
814 		size_t cached = d->bigcache_used;
815 		ushort j;
816 
817 		for (j = 0; j < d->bigcache_size && cached >= psz; j++) {
818 			i = (j + base) & (d->bigcache_size - 1);
819 			if (d->bigcache[i].psize == psz) {
820 				p = d->bigcache[i].page;
821 				d->bigcache_used -= psz;
822 				d->bigcache[i].page = NULL;
823 				d->bigcache[i].psize = 0;
824 
825 				if (!mopts.malloc_freeunmap)
826 					validate_junk(d, p, sz);
827 				if (mopts.malloc_freeunmap)
828 					mprotect(p, sz, PROT_READ | PROT_WRITE);
829 				if (zero_fill)
830 					memset(p, 0, sz);
831 				else if (mopts.malloc_freeunmap)
832 					junk_free(d->malloc_junk, p, sz);
833 				return p;
834 			}
835 			cached -= d->bigcache[i].psize;
836 		}
837 	}
838 	if (psz <= MAX_SMALLCACHEABLE_SIZE && d->smallcache[psz - 1].max > 0) {
839 		cache = &d->smallcache[psz - 1];
840 		if (cache->length > 0) {
841 			int fresh;
842 			if (cache->length == 1)
843 				p = cache->pages[--cache->length];
844 			else {
845 				i = getrbyte(d) % cache->length;
846 				p = cache->pages[i];
847 				cache->pages[i] = cache->pages[--cache->length];
848 			}
849 			/* check if page was not junked, i.e. "fresh
850 			   we use the lsb of the pointer for that */
851 			fresh = (uintptr_t)p & 1UL;
852 			*(uintptr_t*)&p &= ~1UL;
853 			if (!fresh && !mopts.malloc_freeunmap)
854 				validate_junk(d, p, sz);
855 			if (mopts.malloc_freeunmap)
856 				mprotect(p, sz, PROT_READ | PROT_WRITE);
857 			if (zero_fill)
858 				memset(p, 0, sz);
859 			else if (mopts.malloc_freeunmap)
860 				junk_free(d->malloc_junk, p, sz);
861 			return p;
862 		}
863 		if (psz <= 1) {
864 			p = MMAP(cache->max * sz, d->mmap_flag);
865 			if (p != MAP_FAILED) {
866 				STATS_ADD(d->malloc_used, cache->max * sz);
867 				cache->length = cache->max - 1;
868 				for (i = 0; i < cache->max - 1; i++) {
869 					void *q = (char*)p + i * sz;
870 					cache->pages[i] = q;
871 					/* mark pointer in slot as not junked */
872 					*(uintptr_t*)&cache->pages[i] |= 1UL;
873 				}
874 				if (mopts.malloc_freeunmap)
875 					mprotect(p, (cache->max - 1) * sz,
876 					    PROT_NONE);
877 				p = (char*)p + (cache->max - 1) * sz;
878 				/* zero fill not needed, freshly mmapped */
879 				return p;
880 			}
881 		}
882 
883 	}
884 	p = MMAP(sz, d->mmap_flag);
885 	if (p != MAP_FAILED)
886 		STATS_ADD(d->malloc_used, sz);
887 	/* zero fill not needed */
888 	return p;
889 }
890 
891 static void
892 init_chunk_info(struct dir_info *d, struct chunk_info *p, u_int bucket)
893 {
894 	u_int i;
895 
896 	p->bucket = bucket;
897 	p->total = p->free = MALLOC_PAGESIZE / B2ALLOC(bucket);
898 	p->offset = bucket == 0 ? 0xdead : howmany(p->total, MALLOC_BITS);
899 	p->canary = (u_short)d->canary1;
900 
901 	/* set all valid bits in the bitmap */
902  	i = p->total - 1;
903 	memset(p->bits, 0xff, sizeof(p->bits[0]) * (i / MALLOC_BITS));
904 	p->bits[i / MALLOC_BITS] = (2U << (i % MALLOC_BITS)) - 1;
905 }
906 
907 static struct chunk_info *
908 alloc_chunk_info(struct dir_info *d, u_int bucket)
909 {
910 	struct chunk_info *p;
911 
912 	if (LIST_EMPTY(&d->chunk_info_list[bucket])) {
913 		const size_t chunk_pages = 64;
914 		size_t size, count, i;
915 		char *q;
916 
917 		count = MALLOC_PAGESIZE / B2ALLOC(bucket);
918 
919 		size = howmany(count, MALLOC_BITS);
920 		size = sizeof(struct chunk_info) + (size - 1) * sizeof(u_short);
921 		if (mopts.chunk_canaries)
922 			size += count * sizeof(u_short);
923 		size = _ALIGN(size);
924 		count = MALLOC_PAGESIZE / size;
925 
926 		/* Don't use cache here, we don't want user uaf touch this */
927 		if (d->chunk_pages_used == chunk_pages ||
928 		     d->chunk_pages == NULL) {
929 			q = MMAP(MALLOC_PAGESIZE * chunk_pages, d->mmap_flag);
930 			if (q == MAP_FAILED)
931 				return NULL;
932 			d->chunk_pages = q;
933 			d->chunk_pages_used = 0;
934 			STATS_ADD(d->malloc_used, MALLOC_PAGESIZE *
935 			    chunk_pages);
936 		}
937 		q = (char *)d->chunk_pages + d->chunk_pages_used *
938 		    MALLOC_PAGESIZE;
939 		d->chunk_pages_used++;
940 
941 		for (i = 0; i < count; i++, q += size) {
942 			p = (struct chunk_info *)q;
943 			LIST_INSERT_HEAD(&d->chunk_info_list[bucket], p, entries);
944 		}
945 	}
946 	p = LIST_FIRST(&d->chunk_info_list[bucket]);
947 	LIST_REMOVE(p, entries);
948 	if (p->total == 0)
949 		init_chunk_info(d, p, bucket);
950 	return p;
951 }
952 
953 /*
954  * Allocate a page of chunks
955  */
956 static struct chunk_info *
957 omalloc_make_chunks(struct dir_info *d, u_int bucket, u_int listnum)
958 {
959 	struct chunk_info *bp;
960 	void *pp;
961 
962 	/* Allocate a new bucket */
963 	pp = map(d, MALLOC_PAGESIZE, 0);
964 	if (pp == MAP_FAILED)
965 		return NULL;
966 
967 	/* memory protect the page allocated in the malloc(0) case */
968 	if (bucket == 0 && mprotect(pp, MALLOC_PAGESIZE, PROT_NONE) == -1)
969 		goto err;
970 
971 	bp = alloc_chunk_info(d, bucket);
972 	if (bp == NULL)
973 		goto err;
974 	bp->page = pp;
975 
976 	if (insert(d, (void *)((uintptr_t)pp | (bucket + 1)), (uintptr_t)bp,
977 	    NULL))
978 		goto err;
979 	LIST_INSERT_HEAD(&d->chunk_dir[bucket][listnum], bp, entries);
980 	return bp;
981 
982 err:
983 	unmap(d, pp, MALLOC_PAGESIZE, 0);
984 	return NULL;
985 }
986 
987 static inline unsigned int
988 lb(u_int x)
989 {
990 	/* I need an extension just for integer-length (: */
991 	return (sizeof(int) * CHAR_BIT - 1) - __builtin_clz(x);
992 }
993 
994 /* https://pvk.ca/Blog/2015/06/27/linear-log-bucketing-fast-versatile-simple/
995    via Tony Finch */
996 static inline unsigned int
997 bin_of(unsigned int size)
998 {
999 	const unsigned int linear = 6;
1000 	const unsigned int subbin = 2;
1001 
1002 	unsigned int mask, range, rounded, sub_index, rounded_size;
1003 	unsigned int n_bits, shift;
1004 
1005 	n_bits = lb(size | (1U << linear));
1006 	shift = n_bits - subbin;
1007 	mask = (1ULL << shift) - 1;
1008 	rounded = size + mask; /* XXX: overflow. */
1009 	sub_index = rounded >> shift;
1010 	range = n_bits - linear;
1011 
1012 	rounded_size = rounded & ~mask;
1013 	return rounded_size;
1014 }
1015 
1016 static inline u_short
1017 find_bucket(u_short size)
1018 {
1019 	/* malloc(0) is special */
1020 	if (size == 0)
1021 		return 0;
1022 	if (size < MALLOC_MINSIZE)
1023 		size = MALLOC_MINSIZE;
1024 	if (mopts.def_maxcache != 0)
1025 		size = bin_of(size);
1026 	return howmany(size, MALLOC_MINSIZE);
1027 }
1028 
1029 static void
1030 fill_canary(char *ptr, size_t sz, size_t allocated)
1031 {
1032 	size_t check_sz = allocated - sz;
1033 
1034 	if (check_sz > CHUNK_CHECK_LENGTH)
1035 		check_sz = CHUNK_CHECK_LENGTH;
1036 	memset(ptr + sz, mopts.chunk_canaries, check_sz);
1037 }
1038 
1039 /*
1040  * Allocate a chunk
1041  */
1042 static void *
1043 malloc_bytes(struct dir_info *d, size_t size, void *f)
1044 {
1045 	u_int i, r, bucket, listnum;
1046 	size_t k;
1047 	u_short	*lp;
1048 	struct chunk_info *bp;
1049 	void *p;
1050 
1051 	if (mopts.malloc_canary != (d->canary1 ^ (u_int32_t)(uintptr_t)d) ||
1052 	    d->canary1 != ~d->canary2)
1053 		wrterror(d, "internal struct corrupt");
1054 
1055 	bucket = find_bucket(size);
1056 
1057 	r = ((u_int)getrbyte(d) << 8) | getrbyte(d);
1058 	listnum = r % MALLOC_CHUNK_LISTS;
1059 
1060 	/* If it's empty, make a page more of that size chunks */
1061 	if ((bp = LIST_FIRST(&d->chunk_dir[bucket][listnum])) == NULL) {
1062 		bp = omalloc_make_chunks(d, bucket, listnum);
1063 		if (bp == NULL)
1064 			return NULL;
1065 	}
1066 
1067 	if (bp->canary != (u_short)d->canary1)
1068 		wrterror(d, "chunk info corrupted");
1069 
1070 	/* bias, as bp->total is not a power of 2 */
1071 	i = (r / MALLOC_CHUNK_LISTS) % bp->total;
1072 
1073 	/* potentially start somewhere in a short */
1074 	lp = &bp->bits[i / MALLOC_BITS];
1075 	if (*lp) {
1076 		int j = i % MALLOC_BITS; /* j must be signed */
1077 		k = ffs(*lp >> j);
1078 		if (k != 0) {
1079 			k += j - 1;
1080 			goto found;
1081 		}
1082 	}
1083 	/* no bit halfway, go to next full short */
1084 	i /= MALLOC_BITS;
1085 	for (;;) {
1086 		if (++i >= howmany(bp->total, MALLOC_BITS))
1087 			i = 0;
1088 		lp = &bp->bits[i];
1089 		if (*lp) {
1090 			k = ffs(*lp) - 1;
1091 			break;
1092 		}
1093 	}
1094 found:
1095 	if (i == 0 && k == 0 && DO_STATS) {
1096 		struct region_info *r = find(d, bp->page);
1097 		STATS_SETF(r, f);
1098 	}
1099 
1100 	*lp ^= 1 << k;
1101 
1102 	/* If there are no more free, remove from free-list */
1103 	if (--bp->free == 0)
1104 		LIST_REMOVE(bp, entries);
1105 
1106 	/* Adjust to the real offset of that chunk */
1107 	k += (lp - bp->bits) * MALLOC_BITS;
1108 
1109 	if (mopts.chunk_canaries && size > 0)
1110 		bp->bits[bp->offset + k] = size;
1111 
1112 	k *= B2ALLOC(bp->bucket);
1113 
1114 	p = (char *)bp->page + k;
1115 	if (bp->bucket > 0) {
1116 		if (d->malloc_junk == 2)
1117 			memset(p, SOME_JUNK, B2SIZE(bp->bucket));
1118 		else if (mopts.chunk_canaries)
1119 			fill_canary(p, size, B2SIZE(bp->bucket));
1120 	}
1121 	return p;
1122 }
1123 
1124 static void
1125 validate_canary(struct dir_info *d, u_char *ptr, size_t sz, size_t allocated)
1126 {
1127 	size_t check_sz = allocated - sz;
1128 	u_char *p, *q;
1129 
1130 	if (check_sz > CHUNK_CHECK_LENGTH)
1131 		check_sz = CHUNK_CHECK_LENGTH;
1132 	p = ptr + sz;
1133 	q = p + check_sz;
1134 
1135 	while (p < q) {
1136 		if (*p != (u_char)mopts.chunk_canaries && *p != SOME_JUNK) {
1137 			wrterror(d, "chunk canary corrupted %p %#tx@%#zx%s",
1138 			    ptr, p - ptr, sz,
1139 			    *p == SOME_FREEJUNK ? " (double free?)" : "");
1140 		}
1141 		p++;
1142 	}
1143 }
1144 
1145 static uint32_t
1146 find_chunknum(struct dir_info *d, struct chunk_info *info, void *ptr, int check)
1147 {
1148 	uint32_t chunknum;
1149 
1150 	if (info->canary != (u_short)d->canary1)
1151 		wrterror(d, "chunk info corrupted");
1152 
1153 	/* Find the chunk number on the page */
1154 	chunknum = ((uintptr_t)ptr & MALLOC_PAGEMASK) / B2ALLOC(info->bucket);
1155 
1156 	if ((uintptr_t)ptr & (MALLOC_MINSIZE - 1))
1157 		wrterror(d, "modified chunk-pointer %p", ptr);
1158 	if (info->bits[chunknum / MALLOC_BITS] &
1159 	    (1U << (chunknum % MALLOC_BITS)))
1160 		wrterror(d, "chunk is already free %p", ptr);
1161 	if (check && info->bucket > 0) {
1162 		validate_canary(d, ptr, info->bits[info->offset + chunknum],
1163 		    B2SIZE(info->bucket));
1164 	}
1165 	return chunknum;
1166 }
1167 
1168 /*
1169  * Free a chunk, and possibly the page it's on, if the page becomes empty.
1170  */
1171 static void
1172 free_bytes(struct dir_info *d, struct region_info *r, void *ptr)
1173 {
1174 	struct chunk_head *mp;
1175 	struct chunk_info *info;
1176 	uint32_t chunknum;
1177 	uint32_t listnum;
1178 
1179 	info = (struct chunk_info *)r->size;
1180 	chunknum = find_chunknum(d, info, ptr, 0);
1181 
1182 	if (chunknum == 0)
1183 		STATS_SETF(r, NULL);
1184 
1185 	info->bits[chunknum / MALLOC_BITS] |= 1U << (chunknum % MALLOC_BITS);
1186 	info->free++;
1187 
1188 	if (info->free == 1) {
1189 		/* Page became non-full */
1190 		listnum = getrbyte(d) % MALLOC_CHUNK_LISTS;
1191 		mp = &d->chunk_dir[info->bucket][listnum];
1192 		LIST_INSERT_HEAD(mp, info, entries);
1193 		return;
1194 	}
1195 
1196 	if (info->free != info->total)
1197 		return;
1198 
1199 	LIST_REMOVE(info, entries);
1200 
1201 	if (info->bucket == 0 && !mopts.malloc_freeunmap)
1202 		mprotect(info->page, MALLOC_PAGESIZE, PROT_READ | PROT_WRITE);
1203 	unmap(d, info->page, MALLOC_PAGESIZE, 0);
1204 
1205 	delete(d, r);
1206 	mp = &d->chunk_info_list[info->bucket];
1207 	LIST_INSERT_HEAD(mp, info, entries);
1208 }
1209 
1210 
1211 
1212 static void *
1213 omalloc(struct dir_info *pool, size_t sz, int zero_fill, void *f)
1214 {
1215 	void *p;
1216 	size_t psz;
1217 
1218 	if (sz > MALLOC_MAXCHUNK) {
1219 		if (sz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) {
1220 			errno = ENOMEM;
1221 			return NULL;
1222 		}
1223 		sz += mopts.malloc_guard;
1224 		psz = PAGEROUND(sz);
1225 		p = map(pool, psz, zero_fill);
1226 		if (p == MAP_FAILED) {
1227 			errno = ENOMEM;
1228 			return NULL;
1229 		}
1230 		if (insert(pool, p, sz, f)) {
1231 			unmap(pool, p, psz, 0);
1232 			errno = ENOMEM;
1233 			return NULL;
1234 		}
1235 		if (mopts.malloc_guard) {
1236 			if (mprotect((char *)p + psz - mopts.malloc_guard,
1237 			    mopts.malloc_guard, PROT_NONE))
1238 				wrterror(pool, "mprotect");
1239 			STATS_ADD(pool->malloc_guarded, mopts.malloc_guard);
1240 		}
1241 
1242 		if (MALLOC_MOVE_COND(sz)) {
1243 			/* fill whole allocation */
1244 			if (pool->malloc_junk == 2)
1245 				memset(p, SOME_JUNK, psz - mopts.malloc_guard);
1246 			/* shift towards the end */
1247 			p = MALLOC_MOVE(p, sz);
1248 			/* fill zeros if needed and overwritten above */
1249 			if (zero_fill && pool->malloc_junk == 2)
1250 				memset(p, 0, sz - mopts.malloc_guard);
1251 		} else {
1252 			if (pool->malloc_junk == 2) {
1253 				if (zero_fill)
1254 					memset((char *)p + sz -
1255 					    mopts.malloc_guard, SOME_JUNK,
1256 					    psz - sz);
1257 				else
1258 					memset(p, SOME_JUNK,
1259 					    psz - mopts.malloc_guard);
1260 			} else if (mopts.chunk_canaries)
1261 				fill_canary(p, sz - mopts.malloc_guard,
1262 				    psz - mopts.malloc_guard);
1263 		}
1264 
1265 	} else {
1266 		/* takes care of SOME_JUNK */
1267 		p = malloc_bytes(pool, sz, f);
1268 		if (zero_fill && p != NULL && sz > 0)
1269 			memset(p, 0, sz);
1270 	}
1271 
1272 	return p;
1273 }
1274 
1275 /*
1276  * Common function for handling recursion.  Only
1277  * print the error message once, to avoid making the problem
1278  * potentially worse.
1279  */
1280 static void
1281 malloc_recurse(struct dir_info *d)
1282 {
1283 	static int noprint;
1284 
1285 	if (noprint == 0) {
1286 		noprint = 1;
1287 		wrterror(d, "recursive call");
1288 	}
1289 	d->active--;
1290 	_MALLOC_UNLOCK(d->mutex);
1291 	errno = EDEADLK;
1292 }
1293 
1294 void
1295 _malloc_init(int from_rthreads)
1296 {
1297 	u_int i, j, nmutexes;
1298 	struct dir_info *d;
1299 
1300 	_MALLOC_LOCK(1);
1301 	if (!from_rthreads && mopts.malloc_pool[1]) {
1302 		_MALLOC_UNLOCK(1);
1303 		return;
1304 	}
1305 	if (!mopts.malloc_canary) {
1306 		char *p;
1307 		size_t sz, d_avail;
1308 
1309 		omalloc_init();
1310 		/*
1311 		 * Allocate dir_infos with a guard page on either side. Also
1312 		 * randomise offset inside the page at which the dir_infos
1313 		 * lay (subject to alignment by 1 << MALLOC_MINSHIFT)
1314 		 */
1315 		sz = mopts.malloc_mutexes * sizeof(*d) + 2 * MALLOC_PAGESIZE;
1316 		if ((p = MMAPNONE(sz, 0)) == MAP_FAILED)
1317 			wrterror(NULL, "malloc_init mmap1 failed");
1318 		if (mprotect(p + MALLOC_PAGESIZE, mopts.malloc_mutexes * sizeof(*d),
1319 		    PROT_READ | PROT_WRITE))
1320 			wrterror(NULL, "malloc_init mprotect1 failed");
1321 		if (mimmutable(p, sz))
1322 			wrterror(NULL, "malloc_init mimmutable1 failed");
1323 		d_avail = (((mopts.malloc_mutexes * sizeof(*d) + MALLOC_PAGEMASK) &
1324 		    ~MALLOC_PAGEMASK) - (mopts.malloc_mutexes * sizeof(*d))) >>
1325 		    MALLOC_MINSHIFT;
1326 		d = (struct dir_info *)(p + MALLOC_PAGESIZE +
1327 		    (arc4random_uniform(d_avail) << MALLOC_MINSHIFT));
1328 		STATS_ADD(d[1].malloc_used, sz);
1329 		for (i = 0; i < mopts.malloc_mutexes; i++)
1330 			mopts.malloc_pool[i] = &d[i];
1331 		mopts.internal_funcs = 1;
1332 		if (((uintptr_t)&malloc_readonly & MALLOC_PAGEMASK) == 0) {
1333 			if (mprotect(&malloc_readonly, sizeof(malloc_readonly),
1334 			    PROT_READ))
1335 				wrterror(NULL, "malloc_init mprotect r/o failed");
1336 			if (mimmutable(&malloc_readonly, sizeof(malloc_readonly)))
1337 				wrterror(NULL, "malloc_init mimmutable r/o failed");
1338 		}
1339 	}
1340 
1341 	nmutexes = from_rthreads ? mopts.malloc_mutexes : 2;
1342 	for (i = 0; i < nmutexes; i++) {
1343 		d = mopts.malloc_pool[i];
1344 		d->malloc_mt = from_rthreads;
1345 		if (d->canary1 == ~d->canary2)
1346 			continue;
1347 		if (i == 0) {
1348 			omalloc_poolinit(d, MAP_CONCEAL);
1349 			d->malloc_junk = 2;
1350 			d->bigcache_size = 0;
1351 			for (j = 0; j < MAX_SMALLCACHEABLE_SIZE; j++)
1352 				d->smallcache[j].max = 0;
1353 		} else {
1354 			size_t sz = 0;
1355 
1356 			omalloc_poolinit(d, 0);
1357 			d->malloc_junk = mopts.def_malloc_junk;
1358 			d->bigcache_size = mopts.def_maxcache;
1359 			for (j = 0; j < MAX_SMALLCACHEABLE_SIZE; j++) {
1360 				d->smallcache[j].max =
1361 				    mopts.def_maxcache >> (j / 8);
1362 				sz += d->smallcache[j].max * sizeof(void *);
1363 			}
1364 			sz += d->bigcache_size * sizeof(struct bigcache);
1365 			if (sz > 0) {
1366 				void *p = MMAP(sz, 0);
1367 				if (p == MAP_FAILED)
1368 					wrterror(NULL,
1369 					    "malloc_init mmap2 failed");
1370 				if (mimmutable(p, sz))
1371 					wrterror(NULL, "malloc_init mimmutable2 failed");
1372 				for (j = 0; j < MAX_SMALLCACHEABLE_SIZE; j++) {
1373 					d->smallcache[j].pages = p;
1374 					p = (char *)p + d->smallcache[j].max *
1375 					    sizeof(void *);
1376 				}
1377 				d->bigcache = p;
1378 			}
1379 		}
1380 		d->mutex = i;
1381 	}
1382 
1383 	_MALLOC_UNLOCK(1);
1384 }
1385 DEF_STRONG(_malloc_init);
1386 
1387 #define PROLOGUE(p, fn)			\
1388 	d = (p); 			\
1389 	if (d == NULL) { 		\
1390 		_malloc_init(0);	\
1391 		d = (p);		\
1392 	}				\
1393 	_MALLOC_LOCK(d->mutex);		\
1394 	d->func = fn;			\
1395 	if (d->active++) {		\
1396 		malloc_recurse(d);	\
1397 		return NULL;		\
1398 	}				\
1399 
1400 #define EPILOGUE()				\
1401 	d->active--;				\
1402 	_MALLOC_UNLOCK(d->mutex);		\
1403 	if (r == NULL && mopts.malloc_xmalloc)	\
1404 		wrterror(d, "out of memory");	\
1405 	if (r != NULL)				\
1406 		errno = saved_errno;		\
1407 
1408 void *
1409 malloc(size_t size)
1410 {
1411 	void *r;
1412 	struct dir_info *d;
1413 	int saved_errno = errno;
1414 
1415 	PROLOGUE(getpool(), "malloc")
1416 	r = omalloc(d, size, 0, CALLER);
1417 	EPILOGUE()
1418 	return r;
1419 }
1420 /*DEF_STRONG(malloc);*/
1421 
1422 void *
1423 malloc_conceal(size_t size)
1424 {
1425 	void *r;
1426 	struct dir_info *d;
1427 	int saved_errno = errno;
1428 
1429 	PROLOGUE(mopts.malloc_pool[0], "malloc_conceal")
1430 	r = omalloc(d, size, 0, CALLER);
1431 	EPILOGUE()
1432 	return r;
1433 }
1434 DEF_WEAK(malloc_conceal);
1435 
1436 static struct region_info *
1437 findpool(void *p, struct dir_info *argpool, struct dir_info **foundpool,
1438     char **saved_function)
1439 {
1440 	struct dir_info *pool = argpool;
1441 	struct region_info *r = find(pool, p);
1442 
1443 	STATS_INC(pool->pool_searches);
1444 	if (r == NULL) {
1445 		u_int i, nmutexes;
1446 
1447 		nmutexes = mopts.malloc_pool[1]->malloc_mt ? mopts.malloc_mutexes : 2;
1448 		STATS_INC(pool->other_pool);
1449 		for (i = 1; i < nmutexes; i++) {
1450 			u_int j = (argpool->mutex + i) & (nmutexes - 1);
1451 
1452 			pool->active--;
1453 			_MALLOC_UNLOCK(pool->mutex);
1454 			pool = mopts.malloc_pool[j];
1455 			_MALLOC_LOCK(pool->mutex);
1456 			pool->active++;
1457 			r = find(pool, p);
1458 			if (r != NULL) {
1459 				*saved_function = pool->func;
1460 				pool->func = argpool->func;
1461 				break;
1462 			}
1463 		}
1464 		if (r == NULL)
1465 			wrterror(argpool, "bogus pointer (double free?) %p", p);
1466 	}
1467 	*foundpool = pool;
1468 	return r;
1469 }
1470 
1471 static void
1472 ofree(struct dir_info **argpool, void *p, int clear, int check, size_t argsz)
1473 {
1474 	struct region_info *r;
1475 	struct dir_info *pool;
1476 	char *saved_function;
1477 	size_t sz;
1478 
1479 	r = findpool(p, *argpool, &pool, &saved_function);
1480 
1481 	REALSIZE(sz, r);
1482 	if (pool->mmap_flag) {
1483 		clear = 1;
1484 		if (!check) {
1485 			argsz = sz;
1486 			if (sz > MALLOC_MAXCHUNK)
1487 				argsz -= mopts.malloc_guard;
1488 		}
1489 	}
1490 	if (check) {
1491 		if (sz <= MALLOC_MAXCHUNK) {
1492 			if (mopts.chunk_canaries && sz > 0) {
1493 				struct chunk_info *info =
1494 				    (struct chunk_info *)r->size;
1495 				uint32_t chunknum =
1496 				    find_chunknum(pool, info, p, 0);
1497 
1498 				if (info->bits[info->offset + chunknum] < argsz)
1499 					wrterror(pool, "recorded size %hu"
1500 					    " < %zu",
1501 					    info->bits[info->offset + chunknum],
1502 					    argsz);
1503 			} else {
1504 				if (sz < argsz)
1505 					wrterror(pool, "chunk size %zu < %zu",
1506 					    sz, argsz);
1507 			}
1508 		} else if (sz - mopts.malloc_guard < argsz) {
1509 			wrterror(pool, "recorded size %zu < %zu",
1510 			    sz - mopts.malloc_guard, argsz);
1511 		}
1512 	}
1513 	if (sz > MALLOC_MAXCHUNK) {
1514 		if (!MALLOC_MOVE_COND(sz)) {
1515 			if (r->p != p)
1516 				wrterror(pool, "bogus pointer %p", p);
1517 			if (mopts.chunk_canaries)
1518 				validate_canary(pool, p,
1519 				    sz - mopts.malloc_guard,
1520 				    PAGEROUND(sz - mopts.malloc_guard));
1521 		} else {
1522 			/* shifted towards the end */
1523 			if (p != MALLOC_MOVE(r->p, sz))
1524 				wrterror(pool, "bogus moved pointer %p", p);
1525 			p = r->p;
1526 		}
1527 		if (mopts.malloc_guard) {
1528 			if (sz < mopts.malloc_guard)
1529 				wrterror(pool, "guard size");
1530 			if (!mopts.malloc_freeunmap) {
1531 				if (mprotect((char *)p + PAGEROUND(sz) -
1532 				    mopts.malloc_guard, mopts.malloc_guard,
1533 				    PROT_READ | PROT_WRITE))
1534 					wrterror(pool, "mprotect");
1535 			}
1536 			STATS_SUB(pool->malloc_guarded, mopts.malloc_guard);
1537 		}
1538 		unmap(pool, p, PAGEROUND(sz), clear ? argsz : 0);
1539 		delete(pool, r);
1540 	} else {
1541 		void *tmp;
1542 		u_int i;
1543 
1544 		/* Validate and optionally canary check */
1545 		struct chunk_info *info = (struct chunk_info *)r->size;
1546 		if (B2SIZE(info->bucket) != sz)
1547 			wrterror(pool, "internal struct corrupt");
1548 		find_chunknum(pool, info, p, mopts.chunk_canaries);
1549 
1550 		if (mopts.malloc_freecheck) {
1551 			for (i = 0; i <= MALLOC_DELAYED_CHUNK_MASK; i++) {
1552 				tmp = pool->delayed_chunks[i];
1553 				if (tmp == p)
1554 					wrterror(pool,
1555 					    "double free %p", p);
1556 				if (tmp != NULL) {
1557 					size_t tmpsz;
1558 
1559 					r = find(pool, tmp);
1560 					if (r == NULL)
1561 						wrterror(pool,
1562 						    "bogus pointer ("
1563 						    "double free?) %p", tmp);
1564 					REALSIZE(tmpsz, r);
1565 					validate_junk(pool, tmp, tmpsz);
1566 				}
1567 			}
1568 		}
1569 
1570 		if (clear && argsz > 0)
1571 			explicit_bzero(p, argsz);
1572 		junk_free(pool->malloc_junk, p, sz);
1573 
1574 		i = getrbyte(pool) & MALLOC_DELAYED_CHUNK_MASK;
1575 		tmp = p;
1576 		p = pool->delayed_chunks[i];
1577 		if (tmp == p)
1578 			wrterror(pool, "double free %p", p);
1579 		pool->delayed_chunks[i] = tmp;
1580 		if (p != NULL) {
1581 			r = find(pool, p);
1582 			if (r == NULL)
1583 				wrterror(pool,
1584 				    "bogus pointer (double free?) %p", p);
1585 			if (!mopts.malloc_freecheck) {
1586 				REALSIZE(sz, r);
1587 				validate_junk(pool, p, sz);
1588 			}
1589 			free_bytes(pool, r, p);
1590 		}
1591 	}
1592 
1593 	if (*argpool != pool) {
1594 		pool->func = saved_function;
1595 		*argpool = pool;
1596 	}
1597 }
1598 
1599 void
1600 free(void *ptr)
1601 {
1602 	struct dir_info *d;
1603 	int saved_errno = errno;
1604 
1605 	/* This is legal. */
1606 	if (ptr == NULL)
1607 		return;
1608 
1609 	d = getpool();
1610 	if (d == NULL)
1611 		wrterror(d, "free() called before allocation");
1612 	_MALLOC_LOCK(d->mutex);
1613 	d->func = "free";
1614 	if (d->active++) {
1615 		malloc_recurse(d);
1616 		return;
1617 	}
1618 	ofree(&d, ptr, 0, 0, 0);
1619 	d->active--;
1620 	_MALLOC_UNLOCK(d->mutex);
1621 	errno = saved_errno;
1622 }
1623 /*DEF_STRONG(free);*/
1624 
1625 static void
1626 freezero_p(void *ptr, size_t sz)
1627 {
1628 	explicit_bzero(ptr, sz);
1629 	free(ptr);
1630 }
1631 
1632 void
1633 freezero(void *ptr, size_t sz)
1634 {
1635 	struct dir_info *d;
1636 	int saved_errno = errno;
1637 
1638 	/* This is legal. */
1639 	if (ptr == NULL)
1640 		return;
1641 
1642 	if (!mopts.internal_funcs) {
1643 		freezero_p(ptr, sz);
1644 		return;
1645 	}
1646 
1647 	d = getpool();
1648 	if (d == NULL)
1649 		wrterror(d, "freezero() called before allocation");
1650 	_MALLOC_LOCK(d->mutex);
1651 	d->func = "freezero";
1652 	if (d->active++) {
1653 		malloc_recurse(d);
1654 		return;
1655 	}
1656 	ofree(&d, ptr, 1, 1, sz);
1657 	d->active--;
1658 	_MALLOC_UNLOCK(d->mutex);
1659 	errno = saved_errno;
1660 }
1661 DEF_WEAK(freezero);
1662 
1663 static void *
1664 orealloc(struct dir_info **argpool, void *p, size_t newsz, void *f)
1665 {
1666 	struct region_info *r;
1667 	struct dir_info *pool;
1668 	char *saved_function;
1669 	struct chunk_info *info;
1670 	size_t oldsz, goldsz, gnewsz;
1671 	void *q, *ret;
1672 	uint32_t chunknum;
1673 	int forced;
1674 
1675 	if (p == NULL)
1676 		return omalloc(*argpool, newsz, 0, f);
1677 
1678 	if (newsz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) {
1679 		errno = ENOMEM;
1680 		return  NULL;
1681 	}
1682 
1683 	r = findpool(p, *argpool, &pool, &saved_function);
1684 
1685 	REALSIZE(oldsz, r);
1686 	if (oldsz <= MALLOC_MAXCHUNK) {
1687 		if (DO_STATS || mopts.chunk_canaries) {
1688 			info = (struct chunk_info *)r->size;
1689 			chunknum = find_chunknum(pool, info, p, 0);
1690 		}
1691 	}
1692 
1693 	goldsz = oldsz;
1694 	if (oldsz > MALLOC_MAXCHUNK) {
1695 		if (oldsz < mopts.malloc_guard)
1696 			wrterror(pool, "guard size");
1697 		oldsz -= mopts.malloc_guard;
1698 	}
1699 
1700 	gnewsz = newsz;
1701 	if (gnewsz > MALLOC_MAXCHUNK)
1702 		gnewsz += mopts.malloc_guard;
1703 
1704 	forced = mopts.malloc_realloc || pool->mmap_flag;
1705 	if (newsz > MALLOC_MAXCHUNK && oldsz > MALLOC_MAXCHUNK && !forced) {
1706 		/* First case: from n pages sized allocation to m pages sized
1707 		   allocation, m > n */
1708 		size_t roldsz = PAGEROUND(goldsz);
1709 		size_t rnewsz = PAGEROUND(gnewsz);
1710 
1711 		if (rnewsz < roldsz && rnewsz > roldsz / 2 &&
1712 		    roldsz - rnewsz < mopts.def_maxcache * MALLOC_PAGESIZE &&
1713 		    !mopts.malloc_guard) {
1714 
1715 			ret = p;
1716 			goto done;
1717 		}
1718 
1719 		if (rnewsz > roldsz) {
1720 			/* try to extend existing region */
1721 			if (!mopts.malloc_guard) {
1722 				void *hint = (char *)r->p + roldsz;
1723 				size_t needed = rnewsz - roldsz;
1724 
1725 				STATS_INC(pool->cheap_realloc_tries);
1726 				q = MMAPA(hint, needed, MAP_FIXED | __MAP_NOREPLACE | pool->mmap_flag);
1727 				if (q == hint) {
1728 					STATS_ADD(pool->malloc_used, needed);
1729 					if (pool->malloc_junk == 2)
1730 						memset(q, SOME_JUNK, needed);
1731 					r->size = gnewsz;
1732 					if (r->p != p) {
1733 						/* old pointer is moved */
1734 						memmove(r->p, p, oldsz);
1735 						p = r->p;
1736 					}
1737 					if (mopts.chunk_canaries)
1738 						fill_canary(p, newsz,
1739 						    PAGEROUND(newsz));
1740 					STATS_SETF(r, f);
1741 					STATS_INC(pool->cheap_reallocs);
1742 					ret = p;
1743 					goto done;
1744 				}
1745 			}
1746 		} else if (rnewsz < roldsz) {
1747 			/* shrink number of pages */
1748 			if (mopts.malloc_guard) {
1749 				if (mprotect((char *)r->p + rnewsz -
1750 				    mopts.malloc_guard, mopts.malloc_guard,
1751 				    PROT_NONE))
1752 					wrterror(pool, "mprotect");
1753 			}
1754 			if (munmap((char *)r->p + rnewsz, roldsz - rnewsz))
1755 				wrterror(pool, "munmap %p", (char *)r->p +
1756 				    rnewsz);
1757 			STATS_SUB(pool->malloc_used, roldsz - rnewsz);
1758 			r->size = gnewsz;
1759 			if (MALLOC_MOVE_COND(gnewsz)) {
1760 				void *pp = MALLOC_MOVE(r->p, gnewsz);
1761 				memmove(pp, p, newsz);
1762 				p = pp;
1763 			} else if (mopts.chunk_canaries)
1764 				fill_canary(p, newsz, PAGEROUND(newsz));
1765 			STATS_SETF(r, f);
1766 			ret = p;
1767 			goto done;
1768 		} else {
1769 			/* number of pages remains the same */
1770 			void *pp = r->p;
1771 
1772 			r->size = gnewsz;
1773 			if (MALLOC_MOVE_COND(gnewsz))
1774 				pp = MALLOC_MOVE(r->p, gnewsz);
1775 			if (p != pp) {
1776 				memmove(pp, p, oldsz < newsz ? oldsz : newsz);
1777 				p = pp;
1778 			}
1779 			if (p == r->p) {
1780 				if (newsz > oldsz && pool->malloc_junk == 2)
1781 					memset((char *)p + newsz, SOME_JUNK,
1782 					    rnewsz - mopts.malloc_guard -
1783 					    newsz);
1784 				if (mopts.chunk_canaries)
1785 					fill_canary(p, newsz, PAGEROUND(newsz));
1786 			}
1787 			STATS_SETF(r, f);
1788 			ret = p;
1789 			goto done;
1790 		}
1791 	}
1792 	if (oldsz <= MALLOC_MAXCHUNK && oldsz > 0 &&
1793 	    newsz <= MALLOC_MAXCHUNK && newsz > 0 &&
1794 	    !forced && find_bucket(newsz) == find_bucket(oldsz)) {
1795 		/* do not reallocate if new size fits good in existing chunk */
1796 		if (pool->malloc_junk == 2)
1797 			memset((char *)p + newsz, SOME_JUNK, oldsz - newsz);
1798 		if (mopts.chunk_canaries) {
1799 			info->bits[info->offset + chunknum] = newsz;
1800 			fill_canary(p, newsz, B2SIZE(info->bucket));
1801 		}
1802 		if (DO_STATS && chunknum == 0)
1803 			STATS_SETF(r, f);
1804 		ret = p;
1805 	} else if (newsz != oldsz || forced) {
1806 		/* create new allocation */
1807 		q = omalloc(pool, newsz, 0, f);
1808 		if (q == NULL) {
1809 			ret = NULL;
1810 			goto done;
1811 		}
1812 		if (newsz != 0 && oldsz != 0)
1813 			memcpy(q, p, oldsz < newsz ? oldsz : newsz);
1814 		ofree(&pool, p, 0, 0, 0);
1815 		ret = q;
1816 	} else {
1817 		/* oldsz == newsz */
1818 		if (newsz != 0)
1819 			wrterror(pool, "realloc internal inconsistency");
1820 		if (DO_STATS && chunknum == 0)
1821 			STATS_SETF(r, f);
1822 		ret = p;
1823 	}
1824 done:
1825 	if (*argpool != pool) {
1826 		pool->func = saved_function;
1827 		*argpool = pool;
1828 	}
1829 	return ret;
1830 }
1831 
1832 void *
1833 realloc(void *ptr, size_t size)
1834 {
1835 	struct dir_info *d;
1836 	void *r;
1837 	int saved_errno = errno;
1838 
1839 	PROLOGUE(getpool(), "realloc")
1840 	r = orealloc(&d, ptr, size, CALLER);
1841 	EPILOGUE()
1842 	return r;
1843 }
1844 /*DEF_STRONG(realloc);*/
1845 
1846 /*
1847  * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
1848  * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
1849  */
1850 #define MUL_NO_OVERFLOW	(1UL << (sizeof(size_t) * 4))
1851 
1852 void *
1853 calloc(size_t nmemb, size_t size)
1854 {
1855 	struct dir_info *d;
1856 	void *r;
1857 	int saved_errno = errno;
1858 
1859 	PROLOGUE(getpool(), "calloc")
1860 	if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1861 	    nmemb > 0 && SIZE_MAX / nmemb < size) {
1862 		d->active--;
1863 		_MALLOC_UNLOCK(d->mutex);
1864 		if (mopts.malloc_xmalloc)
1865 			wrterror(d, "out of memory");
1866 		errno = ENOMEM;
1867 		return NULL;
1868 	}
1869 
1870 	size *= nmemb;
1871 	r = omalloc(d, size, 1, CALLER);
1872 	EPILOGUE()
1873 	return r;
1874 }
1875 /*DEF_STRONG(calloc);*/
1876 
1877 void *
1878 calloc_conceal(size_t nmemb, size_t size)
1879 {
1880 	struct dir_info *d;
1881 	void *r;
1882 	int saved_errno = errno;
1883 
1884 	PROLOGUE(mopts.malloc_pool[0], "calloc_conceal")
1885 	if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1886 	    nmemb > 0 && SIZE_MAX / nmemb < size) {
1887 		d->active--;
1888 		_MALLOC_UNLOCK(d->mutex);
1889 		if (mopts.malloc_xmalloc)
1890 			wrterror(d, "out of memory");
1891 		errno = ENOMEM;
1892 		return NULL;
1893 	}
1894 
1895 	size *= nmemb;
1896 	r = omalloc(d, size, 1, CALLER);
1897 	EPILOGUE()
1898 	return r;
1899 }
1900 DEF_WEAK(calloc_conceal);
1901 
1902 static void *
1903 orecallocarray(struct dir_info **argpool, void *p, size_t oldsize,
1904     size_t newsize, void *f)
1905 {
1906 	struct region_info *r;
1907 	struct dir_info *pool;
1908 	char *saved_function;
1909 	void *newptr;
1910 	size_t sz;
1911 
1912 	if (p == NULL)
1913 		return omalloc(*argpool, newsize, 1, f);
1914 
1915 	if (oldsize == newsize)
1916 		return p;
1917 
1918 	r = findpool(p, *argpool, &pool, &saved_function);
1919 
1920 	REALSIZE(sz, r);
1921 	if (sz <= MALLOC_MAXCHUNK) {
1922 		if (mopts.chunk_canaries && sz > 0) {
1923 			struct chunk_info *info = (struct chunk_info *)r->size;
1924 			uint32_t chunknum = find_chunknum(pool, info, p, 0);
1925 
1926 			if (info->bits[info->offset + chunknum] != oldsize)
1927 				wrterror(pool, "recorded old size %hu != %zu",
1928 				    info->bits[info->offset + chunknum],
1929 				    oldsize);
1930 		}
1931 	} else if (oldsize < (sz - mopts.malloc_guard) / 2)
1932 		wrterror(pool, "recorded old size %zu != %zu",
1933 		    sz - mopts.malloc_guard, oldsize);
1934 
1935 	newptr = omalloc(pool, newsize, 0, f);
1936 	if (newptr == NULL)
1937 		goto done;
1938 
1939 	if (newsize > oldsize) {
1940 		memcpy(newptr, p, oldsize);
1941 		memset((char *)newptr + oldsize, 0, newsize - oldsize);
1942 	} else
1943 		memcpy(newptr, p, newsize);
1944 
1945 	ofree(&pool, p, 1, 0, oldsize);
1946 
1947 done:
1948 	if (*argpool != pool) {
1949 		pool->func = saved_function;
1950 		*argpool = pool;
1951 	}
1952 
1953 	return newptr;
1954 }
1955 
1956 static void *
1957 recallocarray_p(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
1958 {
1959 	size_t oldsize, newsize;
1960 	void *newptr;
1961 
1962 	if (ptr == NULL)
1963 		return calloc(newnmemb, size);
1964 
1965 	if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1966 	    newnmemb > 0 && SIZE_MAX / newnmemb < size) {
1967 		errno = ENOMEM;
1968 		return NULL;
1969 	}
1970 	newsize = newnmemb * size;
1971 
1972 	if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1973 	    oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
1974 		errno = EINVAL;
1975 		return NULL;
1976 	}
1977 	oldsize = oldnmemb * size;
1978 
1979 	/*
1980 	 * Don't bother too much if we're shrinking just a bit,
1981 	 * we do not shrink for series of small steps, oh well.
1982 	 */
1983 	if (newsize <= oldsize) {
1984 		size_t d = oldsize - newsize;
1985 
1986 		if (d < oldsize / 2 && d < MALLOC_PAGESIZE) {
1987 			memset((char *)ptr + newsize, 0, d);
1988 			return ptr;
1989 		}
1990 	}
1991 
1992 	newptr = malloc(newsize);
1993 	if (newptr == NULL)
1994 		return NULL;
1995 
1996 	if (newsize > oldsize) {
1997 		memcpy(newptr, ptr, oldsize);
1998 		memset((char *)newptr + oldsize, 0, newsize - oldsize);
1999 	} else
2000 		memcpy(newptr, ptr, newsize);
2001 
2002 	explicit_bzero(ptr, oldsize);
2003 	free(ptr);
2004 
2005 	return newptr;
2006 }
2007 
2008 void *
2009 recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
2010 {
2011 	struct dir_info *d;
2012 	size_t oldsize = 0, newsize;
2013 	void *r;
2014 	int saved_errno = errno;
2015 
2016 	if (!mopts.internal_funcs)
2017 		return recallocarray_p(ptr, oldnmemb, newnmemb, size);
2018 
2019 	PROLOGUE(getpool(), "recallocarray")
2020 
2021 	if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
2022 	    newnmemb > 0 && SIZE_MAX / newnmemb < size) {
2023 		d->active--;
2024 		_MALLOC_UNLOCK(d->mutex);
2025 		if (mopts.malloc_xmalloc)
2026 			wrterror(d, "out of memory");
2027 		errno = ENOMEM;
2028 		return NULL;
2029 	}
2030 	newsize = newnmemb * size;
2031 
2032 	if (ptr != NULL) {
2033 		if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
2034 		    oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
2035 			d->active--;
2036 			_MALLOC_UNLOCK(d->mutex);
2037 			errno = EINVAL;
2038 			return NULL;
2039 		}
2040 		oldsize = oldnmemb * size;
2041 	}
2042 
2043 	r = orecallocarray(&d, ptr, oldsize, newsize, CALLER);
2044 	EPILOGUE()
2045 	return r;
2046 }
2047 DEF_WEAK(recallocarray);
2048 
2049 static void *
2050 mapalign(struct dir_info *d, size_t alignment, size_t sz, int zero_fill)
2051 {
2052 	char *p, *q;
2053 
2054 	if (alignment < MALLOC_PAGESIZE || ((alignment - 1) & alignment) != 0)
2055 		wrterror(d, "mapalign bad alignment");
2056 	if (sz != PAGEROUND(sz))
2057 		wrterror(d, "mapalign round");
2058 
2059 	/* Allocate sz + alignment bytes of memory, which must include a
2060 	 * subrange of size bytes that is properly aligned.  Unmap the
2061 	 * other bytes, and then return that subrange.
2062 	 */
2063 
2064 	/* We need sz + alignment to fit into a size_t. */
2065 	if (alignment > SIZE_MAX - sz)
2066 		return MAP_FAILED;
2067 
2068 	p = map(d, sz + alignment, zero_fill);
2069 	if (p == MAP_FAILED)
2070 		return MAP_FAILED;
2071 	q = (char *)(((uintptr_t)p + alignment - 1) & ~(alignment - 1));
2072 	if (q != p) {
2073 		if (munmap(p, q - p))
2074 			wrterror(d, "munmap %p", p);
2075 	}
2076 	if (munmap(q + sz, alignment - (q - p)))
2077 		wrterror(d, "munmap %p", q + sz);
2078 	STATS_SUB(d->malloc_used, alignment);
2079 
2080 	return q;
2081 }
2082 
2083 static void *
2084 omemalign(struct dir_info *pool, size_t alignment, size_t sz, int zero_fill,
2085     void *f)
2086 {
2087 	size_t psz;
2088 	void *p;
2089 
2090 	/* If between half a page and a page, avoid MALLOC_MOVE. */
2091 	if (sz > MALLOC_MAXCHUNK && sz < MALLOC_PAGESIZE)
2092 		sz = MALLOC_PAGESIZE;
2093 	if (alignment <= MALLOC_PAGESIZE) {
2094 		size_t pof2;
2095 		/*
2096 		 * max(size, alignment) rounded up to power of 2 is enough
2097 		 * to assure the requested alignment. Large regions are
2098 		 * always page aligned.
2099 		 */
2100 		if (sz < alignment)
2101 			sz = alignment;
2102 		if (sz < MALLOC_PAGESIZE) {
2103 			pof2 = MALLOC_MINSIZE;
2104 			while (pof2 < sz)
2105 				pof2 <<= 1;
2106 		} else
2107 			pof2 = sz;
2108 		return omalloc(pool, pof2, zero_fill, f);
2109 	}
2110 
2111 	if (sz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) {
2112 		errno = ENOMEM;
2113 		return NULL;
2114 	}
2115 
2116 	if (sz < MALLOC_PAGESIZE)
2117 		sz = MALLOC_PAGESIZE;
2118 	sz += mopts.malloc_guard;
2119 	psz = PAGEROUND(sz);
2120 
2121 	p = mapalign(pool, alignment, psz, zero_fill);
2122 	if (p == MAP_FAILED) {
2123 		errno = ENOMEM;
2124 		return NULL;
2125 	}
2126 
2127 	if (insert(pool, p, sz, f)) {
2128 		unmap(pool, p, psz, 0);
2129 		errno = ENOMEM;
2130 		return NULL;
2131 	}
2132 
2133 	if (mopts.malloc_guard) {
2134 		if (mprotect((char *)p + psz - mopts.malloc_guard,
2135 		    mopts.malloc_guard, PROT_NONE))
2136 			wrterror(pool, "mprotect");
2137 		STATS_ADD(pool->malloc_guarded, mopts.malloc_guard);
2138 	}
2139 
2140 	if (pool->malloc_junk == 2) {
2141 		if (zero_fill)
2142 			memset((char *)p + sz - mopts.malloc_guard,
2143 			    SOME_JUNK, psz - sz);
2144 		else
2145 			memset(p, SOME_JUNK, psz - mopts.malloc_guard);
2146 	} else if (mopts.chunk_canaries)
2147 		fill_canary(p, sz - mopts.malloc_guard,
2148 		    psz - mopts.malloc_guard);
2149 
2150 	return p;
2151 }
2152 
2153 int
2154 posix_memalign(void **memptr, size_t alignment, size_t size)
2155 {
2156 	struct dir_info *d;
2157 	int res, saved_errno = errno;
2158 	void *r;
2159 
2160 	/* Make sure that alignment is a large enough power of 2. */
2161 	if (((alignment - 1) & alignment) != 0 || alignment < sizeof(void *))
2162 		return EINVAL;
2163 
2164 	d = getpool();
2165 	if (d == NULL) {
2166 		_malloc_init(0);
2167 		d = getpool();
2168 	}
2169 	_MALLOC_LOCK(d->mutex);
2170 	d->func = "posix_memalign";
2171 	if (d->active++) {
2172 		malloc_recurse(d);
2173 		goto err;
2174 	}
2175 	r = omemalign(d, alignment, size, 0, CALLER);
2176 	d->active--;
2177 	_MALLOC_UNLOCK(d->mutex);
2178 	if (r == NULL) {
2179 		if (mopts.malloc_xmalloc)
2180 			wrterror(d, "out of memory");
2181 		goto err;
2182 	}
2183 	errno = saved_errno;
2184 	*memptr = r;
2185 	return 0;
2186 
2187 err:
2188 	res = errno;
2189 	errno = saved_errno;
2190 	return res;
2191 }
2192 /*DEF_STRONG(posix_memalign);*/
2193 
2194 void *
2195 aligned_alloc(size_t alignment, size_t size)
2196 {
2197 	struct dir_info *d;
2198 	int saved_errno = errno;
2199 	void *r;
2200 
2201 	/* Make sure that alignment is a positive power of 2. */
2202 	if (((alignment - 1) & alignment) != 0 || alignment == 0) {
2203 		errno = EINVAL;
2204 		return NULL;
2205 	};
2206 	/* Per spec, size should be a multiple of alignment */
2207 	if ((size & (alignment - 1)) != 0) {
2208 		errno = EINVAL;
2209 		return NULL;
2210 	}
2211 
2212 	PROLOGUE(getpool(), "aligned_alloc")
2213 	r = omemalign(d, alignment, size, 0, CALLER);
2214 	EPILOGUE()
2215 	return r;
2216 }
2217 /*DEF_STRONG(aligned_alloc);*/
2218 
2219 #ifdef MALLOC_STATS
2220 
2221 static void
2222 ulog(const char *format, ...)
2223 {
2224 	va_list ap;
2225 	static char* buf;
2226 	static size_t filled;
2227 	int len;
2228 
2229 	if (buf == NULL)
2230 		buf = MMAP(KTR_USER_MAXLEN, 0);
2231 	if (buf == MAP_FAILED)
2232 		return;
2233 
2234 	va_start(ap, format);
2235 	len = vsnprintf(buf + filled, KTR_USER_MAXLEN - filled, format, ap);
2236 	va_end(ap);
2237 	if (len < 0)
2238 		return;
2239 	if (len > KTR_USER_MAXLEN - filled)
2240 		len = KTR_USER_MAXLEN - filled;
2241 	filled += len;
2242 	if (filled > 0) {
2243 		if (filled == KTR_USER_MAXLEN || buf[filled - 1] == '\n') {
2244 			utrace("malloc", buf, filled);
2245 			filled = 0;
2246 		}
2247 	}
2248 }
2249 
2250 struct malloc_leak {
2251 	void *f;
2252 	size_t total_size;
2253 	int count;
2254 };
2255 
2256 struct leaknode {
2257 	RBT_ENTRY(leaknode) entry;
2258 	struct malloc_leak d;
2259 };
2260 
2261 static inline int
2262 leakcmp(const struct leaknode *e1, const struct leaknode *e2)
2263 {
2264 	return e1->d.f < e2->d.f ? -1 : e1->d.f > e2->d.f;
2265 }
2266 
2267 RBT_HEAD(leaktree, leaknode);
2268 RBT_PROTOTYPE(leaktree, leaknode, entry, leakcmp);
2269 RBT_GENERATE(leaktree, leaknode, entry, leakcmp);
2270 
2271 static void
2272 putleakinfo(struct leaktree *leaks, void *f, size_t sz, int cnt)
2273 {
2274 	struct leaknode key, *p;
2275 	static struct leaknode *page;
2276 	static unsigned int used;
2277 
2278 	if (cnt == 0 || page == MAP_FAILED)
2279 		return;
2280 
2281 	key.d.f = f;
2282 	p = RBT_FIND(leaktree, leaks, &key);
2283 	if (p == NULL) {
2284 		if (page == NULL ||
2285 		    used >= MALLOC_PAGESIZE / sizeof(struct leaknode)) {
2286 			page = MMAP(MALLOC_PAGESIZE, 0);
2287 			if (page == MAP_FAILED)
2288 				return;
2289 			used = 0;
2290 		}
2291 		p = &page[used++];
2292 		p->d.f = f;
2293 		p->d.total_size = sz * cnt;
2294 		p->d.count = cnt;
2295 		RBT_INSERT(leaktree, leaks, p);
2296 	} else {
2297 		p->d.total_size += sz * cnt;
2298 		p->d.count += cnt;
2299 	}
2300 }
2301 
2302 static void
2303 dump_leaks(struct leaktree *leaks)
2304 {
2305 	struct leaknode *p;
2306 
2307 	ulog("Leak report:\n");
2308 	ulog("                 f     sum      #    avg\n");
2309 
2310 	RBT_FOREACH(p, leaktree, leaks) {
2311 		Dl_info info;
2312 		const char *caller = p->d.f;
2313 		const char *object = ".";
2314 
2315 		if (caller != NULL) {
2316 			if (dladdr(p->d.f, &info) != 0) {
2317 				caller -= (uintptr_t)info.dli_fbase;
2318 				object = info.dli_fname;
2319 			}
2320 		}
2321 		ulog("%18p %7zu %6u %6zu addr2line -e %s %p\n",
2322 		    p->d.f, p->d.total_size, p->d.count,
2323 		    p->d.total_size / p->d.count,
2324 		    object, caller);
2325 	}
2326 }
2327 
2328 static void
2329 dump_chunk(struct leaktree* leaks, struct chunk_info *p, void *f,
2330     int fromfreelist)
2331 {
2332 	while (p != NULL) {
2333 		if (mopts.malloc_verbose)
2334 			ulog("chunk %18p %18p %4zu %d/%d\n",
2335 			    p->page, ((p->bits[0] & 1) ? NULL : f),
2336 			    B2SIZE(p->bucket), p->free, p->total);
2337 		if (!fromfreelist) {
2338 			size_t sz =  B2SIZE(p->bucket);
2339 			if (p->bits[0] & 1)
2340 				putleakinfo(leaks, NULL, sz, p->total -
2341 				    p->free);
2342 			else {
2343 				putleakinfo(leaks, f, sz, 1);
2344 				putleakinfo(leaks, NULL, sz,
2345 				    p->total - p->free - 1);
2346 			}
2347 			break;
2348 		}
2349 		p = LIST_NEXT(p, entries);
2350 		if (mopts.malloc_verbose && p != NULL)
2351 			ulog("       ->");
2352 	}
2353 }
2354 
2355 static void
2356 dump_free_chunk_info(struct dir_info *d, struct leaktree *leaks)
2357 {
2358 	int i, j, count;
2359 	struct chunk_info *p;
2360 
2361 	ulog("Free chunk structs:\n");
2362 	ulog("Bkt) #CI                     page"
2363 	    "                  f size free/n\n");
2364 	for (i = 0; i <= BUCKETS; i++) {
2365 		count = 0;
2366 		LIST_FOREACH(p, &d->chunk_info_list[i], entries)
2367 			count++;
2368 		for (j = 0; j < MALLOC_CHUNK_LISTS; j++) {
2369 			p = LIST_FIRST(&d->chunk_dir[i][j]);
2370 			if (p == NULL && count == 0)
2371 				continue;
2372 			if (j == 0)
2373 				ulog("%3d) %3d ", i, count);
2374 			else
2375 				ulog("         ");
2376 			if (p != NULL)
2377 				dump_chunk(leaks, p, NULL, 1);
2378 			else
2379 				ulog(".\n");
2380 		}
2381 	}
2382 
2383 }
2384 
2385 static void
2386 dump_free_page_info(struct dir_info *d)
2387 {
2388 	struct smallcache *cache;
2389 	size_t i, total = 0;
2390 
2391 	ulog("Cached in small cache:\n");
2392 	for (i = 0; i < MAX_SMALLCACHEABLE_SIZE; i++) {
2393 		cache = &d->smallcache[i];
2394 		if (cache->length != 0)
2395 			ulog("%zu(%u): %u = %zu\n", i + 1, cache->max,
2396 			    cache->length, cache->length * (i + 1));
2397 		total += cache->length * (i + 1);
2398 	}
2399 
2400 	ulog("Cached in big cache: %zu/%zu\n", d->bigcache_used,
2401 	    d->bigcache_size);
2402 	for (i = 0; i < d->bigcache_size; i++) {
2403 		if (d->bigcache[i].psize != 0)
2404 			ulog("%zu: %zu\n", i, d->bigcache[i].psize);
2405 		total += d->bigcache[i].psize;
2406 	}
2407 	ulog("Free pages cached: %zu\n", total);
2408 }
2409 
2410 static void
2411 malloc_dump1(int poolno, struct dir_info *d, struct leaktree *leaks)
2412 {
2413 	size_t i, realsize;
2414 
2415 	if (mopts.malloc_verbose) {
2416 		ulog("Malloc dir of %s pool %d at %p\n", __progname, poolno, d);
2417 		ulog("MT=%d J=%d Fl=%x\n", d->malloc_mt, d->malloc_junk,
2418 		    d->mmap_flag);
2419 		ulog("Region slots free %zu/%zu\n",
2420 			d->regions_free, d->regions_total);
2421 		ulog("Finds %zu/%zu\n", d->finds, d->find_collisions);
2422 		ulog("Inserts %zu/%zu\n", d->inserts, d->insert_collisions);
2423 		ulog("Deletes %zu/%zu\n", d->deletes, d->delete_moves);
2424 		ulog("Cheap reallocs %zu/%zu\n",
2425 		    d->cheap_reallocs, d->cheap_realloc_tries);
2426 		ulog("Other pool searches %zu/%zu\n",
2427 		    d->other_pool, d->pool_searches);
2428 		ulog("In use %zu\n", d->malloc_used);
2429 		ulog("Guarded %zu\n", d->malloc_guarded);
2430 		dump_free_chunk_info(d, leaks);
2431 		dump_free_page_info(d);
2432 		ulog("Hash table:\n");
2433 		ulog("slot)  hash d  type               page                  "
2434 		    "f size [free/n]\n");
2435 	}
2436 	for (i = 0; i < d->regions_total; i++) {
2437 		if (d->r[i].p != NULL) {
2438 			size_t h = hash(d->r[i].p) &
2439 			    (d->regions_total - 1);
2440 			if (mopts.malloc_verbose)
2441 				ulog("%4zx) #%4zx %zd ",
2442 			        i, h, h - i);
2443 			REALSIZE(realsize, &d->r[i]);
2444 			if (realsize > MALLOC_MAXCHUNK) {
2445 				putleakinfo(leaks, d->r[i].f, realsize, 1);
2446 				if (mopts.malloc_verbose)
2447 					ulog("pages %18p %18p %zu\n", d->r[i].p,
2448 				        d->r[i].f, realsize);
2449 			} else
2450 				dump_chunk(leaks,
2451 				    (struct chunk_info *)d->r[i].size,
2452 				    d->r[i].f, 0);
2453 		}
2454 	}
2455 	if (mopts.malloc_verbose)
2456 		ulog("\n");
2457 }
2458 
2459 static void
2460 malloc_dump0(int poolno, struct dir_info *pool, struct leaktree *leaks)
2461 {
2462 	int i;
2463 	void *p;
2464 	struct region_info *r;
2465 
2466 	if (pool == NULL || pool->r == NULL)
2467 		return;
2468 	for (i = 0; i < MALLOC_DELAYED_CHUNK_MASK + 1; i++) {
2469 		p = pool->delayed_chunks[i];
2470 		if (p == NULL)
2471 			continue;
2472 		r = find(pool, p);
2473 		if (r == NULL)
2474 			wrterror(pool, "bogus pointer in malloc_dump %p", p);
2475 		free_bytes(pool, r, p);
2476 		pool->delayed_chunks[i] = NULL;
2477 	}
2478 	malloc_dump1(poolno, pool, leaks);
2479 }
2480 
2481 void
2482 malloc_dump(void)
2483 {
2484 	int i;
2485 	int saved_errno = errno;
2486 
2487 	/* XXX leak when run multiple times */
2488 	struct leaktree leaks = RBT_INITIALIZER(&leaks);
2489 
2490 	for (i = 0; i < mopts.malloc_mutexes; i++)
2491 		malloc_dump0(i, mopts.malloc_pool[i], &leaks);
2492 
2493 	dump_leaks(&leaks);
2494 	ulog("\n");
2495 	errno = saved_errno;
2496 }
2497 DEF_WEAK(malloc_dump);
2498 
2499 static void
2500 malloc_exit(void)
2501 {
2502 	int save_errno = errno;
2503 
2504 	ulog("******** Start dump %s *******\n", __progname);
2505 	ulog("M=%u I=%d F=%d U=%d J=%d R=%d X=%d C=%d cache=%u "
2506 	    "G=%zu\n",
2507 	    mopts.malloc_mutexes,
2508 	    mopts.internal_funcs, mopts.malloc_freecheck,
2509 	    mopts.malloc_freeunmap, mopts.def_malloc_junk,
2510 	    mopts.malloc_realloc, mopts.malloc_xmalloc,
2511 	    mopts.chunk_canaries, mopts.def_maxcache,
2512 	    mopts.malloc_guard);
2513 
2514 	malloc_dump();
2515 	ulog("******** End dump %s *******\n", __progname);
2516 	errno = save_errno;
2517 }
2518 
2519 #endif /* MALLOC_STATS */
2520