xref: /freebsd/libexec/rtld-elf/xmalloc.c (revision 4db3872aabc33088cf180599c5eaa23b6f58e6d1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 1996-1998 John D. Polstra.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <machine/cpu.h>
30 #include <machine/cpufunc.h>
31 #include <stddef.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include "rtld.h"
36 #include "rtld_printf.h"
37 #include "rtld_malloc.h"
38 #include "rtld_libc.h"
39 
40 static int rtld_malloc_spinlock;
41 
42 /*
43  * Almost always the rtld malloc is called under the write-locked rtld
44  * bind lock.  Due to this, xlock() would need a single CAS to take
45  * the spinlock.  But some places only own read-locked rtld bind lock,
46  * and then the spinlock protects the malloc structures from the
47  * parallel updates.
48  */
49 static void
50 xlock(void)
51 {
52 	while (atomic_cmpset_acq_int(&rtld_malloc_spinlock, 0, 1) == 0)
53 		cpu_spinwait();
54 }
55 
56 static void
57 xunlock(void)
58 {
59 	atomic_store_rel_int(&rtld_malloc_spinlock, 0);
60 }
61 
62 void *
63 xcalloc(size_t number, size_t size)
64 {
65 	void *p;
66 
67 	xlock();
68 	p = __crt_calloc(number, size);
69 	xunlock();
70 	if (p == NULL) {
71 		rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
72 		_exit(1);
73 	}
74 	return (p);
75 }
76 
77 void *
78 xmalloc(size_t size)
79 {
80 	void *p;
81 
82 	xlock();
83 	p = __crt_malloc(size);
84 	xunlock();
85 	if (p == NULL) {
86 		rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
87 		_exit(1);
88 	}
89 	return (p);
90 }
91 
92 char *
93 xstrdup(const char *str)
94 {
95 	char *copy;
96 	size_t len;
97 
98 	len = strlen(str) + 1;
99 	copy = xmalloc(len);
100 	memcpy(copy, str, len);
101 	return (copy);
102 }
103 
104 void *
105 xmalloc_aligned(size_t size, size_t align, size_t offset)
106 {
107 	void *res;
108 
109 	offset &= align - 1;
110 	if (align < sizeof(void *))
111 		align = sizeof(void *);
112 
113 	xlock();
114 	res = __crt_aligned_alloc_offset(align, size, offset);
115 	xunlock();
116 	if (res == NULL) {
117 		rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
118 		_exit(1);
119 	}
120 	return (res);
121 }
122 
123 void *
124 malloc(size_t size)
125 {
126 	void *p;
127 
128 	xlock();
129 	p = __crt_malloc(size);
130 	xunlock();
131 	return (p);
132 }
133 
134 void *
135 calloc(size_t num, size_t size)
136 {
137 	void *p;
138 
139 	xlock();
140 	p = __crt_calloc(num, size);
141 	xunlock();
142 	return (p);
143 }
144 
145 void
146 free(void *cp)
147 {
148 	xlock();
149 	__crt_free(cp);
150 	xunlock();
151 }
152 
153 void *
154 realloc(void *cp, size_t nbytes)
155 {
156 	void *p;
157 
158 	xlock();
159 	p = __crt_realloc(cp, nbytes);
160 	xunlock();
161 	return (p);
162 }
163