1 | /* $Id: alloc.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Defined Constants And Macros *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #if defined(RTMEM_WRAP_TO_EF_APIS) && !defined(RTMEM_NO_WRAP_TO_EF_APIS)
|
---|
32 | # undef RTMEM_WRAP_TO_EF_APIS
|
---|
33 | # define RTALLOC_USE_EFENCE 1
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | /*#define RTMEMALLOC_USE_TRACKER*/
|
---|
37 | /* Don't enable the tracker when building the minimal IPRT. */
|
---|
38 | #ifdef RT_MINI
|
---|
39 | # undef RTMEMALLOC_USE_TRACKER
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #if defined(RTMEMALLOC_USE_TRACKER) && defined(RTALLOC_USE_EFENCE)
|
---|
43 | # error "Cannot define both RTMEMALLOC_USE_TRACKER and RTALLOC_USE_EFENCE!"
|
---|
44 | #endif
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Header Files *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | #include "alloc-ef.h"
|
---|
51 | #include <iprt/mem.h>
|
---|
52 |
|
---|
53 | #include <iprt/asm.h>
|
---|
54 | #include <iprt/assert.h>
|
---|
55 | #ifdef RTMEMALLOC_USE_TRACKER
|
---|
56 | # include <iprt/memtracker.h>
|
---|
57 | #endif
|
---|
58 | #include <iprt/param.h>
|
---|
59 | #include <iprt/string.h>
|
---|
60 | #include "internal/mem.h"
|
---|
61 |
|
---|
62 | #include <stdlib.h>
|
---|
63 |
|
---|
64 | #undef RTMemTmpAlloc
|
---|
65 | #undef RTMemTmpAllocTag
|
---|
66 | #undef RTMemTmpAllocZ
|
---|
67 | #undef RTMemTmpAllocZTag
|
---|
68 | #undef RTMemTmpFree
|
---|
69 | #undef RTMemTmpFreeZ
|
---|
70 | #undef RTMemAlloc
|
---|
71 | #undef RTMemAllocTag
|
---|
72 | #undef RTMemAllocZ
|
---|
73 | #undef RTMemAllocZTag
|
---|
74 | #undef RTMemAllocVar
|
---|
75 | #undef RTMemAllocVarTag
|
---|
76 | #undef RTMemAllocZVar
|
---|
77 | #undef RTMemAllocZVarTag
|
---|
78 | #undef RTMemRealloc
|
---|
79 | #undef RTMemReallocTag
|
---|
80 | #undef RTMemFree
|
---|
81 | #undef RTMemFreeZ
|
---|
82 | #undef RTMemDup
|
---|
83 | #undef RTMemDupTag
|
---|
84 | #undef RTMemDupEx
|
---|
85 | #undef RTMemDupExTag
|
---|
86 |
|
---|
87 | #undef RTALLOC_USE_EFENCE
|
---|
88 |
|
---|
89 |
|
---|
90 | #ifdef IPRT_WITH_GCC_SANITIZER
|
---|
91 | /**
|
---|
92 | * Checks if @a pszTag is a leak tag.
|
---|
93 | *
|
---|
94 | * @returns true if leak tag, false if not.
|
---|
95 | * @param pszTag Tage to inspect.
|
---|
96 | */
|
---|
97 | DECLINLINE(bool) rtMemIsLeakTag(const char *pszTag)
|
---|
98 | {
|
---|
99 | char ch = *pszTag;
|
---|
100 | if (ch != 'w')
|
---|
101 | { /* likely */ }
|
---|
102 | else
|
---|
103 | return pszTag[1] == 'i'
|
---|
104 | && pszTag[2] == 'l'
|
---|
105 | && pszTag[3] == 'l'
|
---|
106 | && pszTag[4] == '-'
|
---|
107 | && pszTag[5] == 'l'
|
---|
108 | && pszTag[6] == 'e'
|
---|
109 | && pszTag[7] == 'a'
|
---|
110 | && pszTag[8] == 'k';
|
---|
111 | if (ch != 'm')
|
---|
112 | return false;
|
---|
113 | return pszTag[1] == 'm'
|
---|
114 | && pszTag[2] == 'a'
|
---|
115 | && pszTag[3] == 'y'
|
---|
116 | && pszTag[4] == '-'
|
---|
117 | && pszTag[5] == 'l'
|
---|
118 | && pszTag[6] == 'e'
|
---|
119 | && pszTag[7] == 'a'
|
---|
120 | && pszTag[8] == 'k';
|
---|
121 | }
|
---|
122 | #endif /* IPRT_WITH_GCC_SANITIZER */
|
---|
123 |
|
---|
124 |
|
---|
125 | RTDECL(void *) RTMemTmpAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
|
---|
126 | {
|
---|
127 | return RTMemAllocTag(cb, pszTag);
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | RTDECL(void *) RTMemTmpAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
|
---|
132 | {
|
---|
133 | return RTMemAllocZTag(cb, pszTag);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | RTDECL(void) RTMemTmpFree(void *pv) RT_NO_THROW_DEF
|
---|
138 | {
|
---|
139 | RTMemFree(pv);
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | RTDECL(void) RTMemTmpFreeZ(void *pv, size_t cb) RT_NO_THROW_DEF
|
---|
144 | {
|
---|
145 | RTMemFreeZ(pv, cb);
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | RTDECL(void *) RTMemAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
|
---|
150 | {
|
---|
151 | #ifdef RTALLOC_USE_EFENCE
|
---|
152 | void *pv = rtR3MemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, cb, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
153 |
|
---|
154 | #else /* !RTALLOC_USE_EFENCE */
|
---|
155 |
|
---|
156 | AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
|
---|
157 | # ifdef RTMEMALLOC_USE_TRACKER
|
---|
158 | void *pv = RTMemTrackerHdrAlloc(malloc(cb + sizeof(RTMEMTRACKERHDR)), cb, pszTag, ASMReturnAddress(), RTMEMTRACKERMETHOD_ALLOC);
|
---|
159 | # else
|
---|
160 | void *pv = malloc(cb); NOREF(pszTag);
|
---|
161 | # endif
|
---|
162 | AssertMsg(pv, ("malloc(%#zx) failed!!!\n", cb));
|
---|
163 | AssertMsg( cb < RTMEM_ALIGNMENT
|
---|
164 | || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
|
---|
165 | || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
|
---|
166 | , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
|
---|
167 | #endif /* !RTALLOC_USE_EFENCE */
|
---|
168 | #ifdef IPRT_WITH_GCC_SANITIZER
|
---|
169 | if (rtMemIsLeakTag(pszTag))
|
---|
170 | __lsan_ignore_object(pv);
|
---|
171 | #endif
|
---|
172 | return pv;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | RTDECL(void *) RTMemAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
|
---|
177 | {
|
---|
178 | #ifdef RTALLOC_USE_EFENCE
|
---|
179 | void *pv = rtR3MemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, cb, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
180 |
|
---|
181 | #else /* !RTALLOC_USE_EFENCE */
|
---|
182 |
|
---|
183 | AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
|
---|
184 |
|
---|
185 | # ifdef RTMEMALLOC_USE_TRACKER
|
---|
186 | void *pv = RTMemTrackerHdrAlloc(calloc(1, cb + sizeof(RTMEMTRACKERHDR)), cb, pszTag, ASMReturnAddress(), RTMEMTRACKERMETHOD_ALLOCZ);
|
---|
187 | #else
|
---|
188 | void *pv = calloc(1, cb); NOREF(pszTag);
|
---|
189 | #endif
|
---|
190 | AssertMsg(pv, ("calloc(1,%#zx) failed!!!\n", cb));
|
---|
191 | AssertMsg( cb < RTMEM_ALIGNMENT
|
---|
192 | || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
|
---|
193 | || ( (cb & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
|
---|
194 | , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
|
---|
195 | #endif /* !RTALLOC_USE_EFENCE */
|
---|
196 | #ifdef IPRT_WITH_GCC_SANITIZER
|
---|
197 | if (rtMemIsLeakTag(pszTag))
|
---|
198 | __lsan_ignore_object(pv);
|
---|
199 | #endif
|
---|
200 | return pv;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | RTDECL(void *) RTMemAllocVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW_DEF
|
---|
205 | {
|
---|
206 | size_t cbAligned;
|
---|
207 | if (cbUnaligned >= 16)
|
---|
208 | cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
|
---|
209 | else
|
---|
210 | cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
|
---|
211 | #ifdef RTALLOC_USE_EFENCE
|
---|
212 | void *pv = rtR3MemAlloc("AllocVar", RTMEMTYPE_RTMEMALLOC, cbUnaligned, cbAligned, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
213 | #else
|
---|
214 | void *pv = RTMemAllocTag(cbAligned, pszTag);
|
---|
215 | #endif
|
---|
216 | return pv;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | RTDECL(void *) RTMemAllocZVarTag(size_t cbUnaligned, const char *pszTag) RT_NO_THROW_DEF
|
---|
221 | {
|
---|
222 | size_t cbAligned;
|
---|
223 | if (cbUnaligned >= 16)
|
---|
224 | cbAligned = RT_ALIGN_Z(cbUnaligned, 16);
|
---|
225 | else
|
---|
226 | cbAligned = RT_ALIGN_Z(cbUnaligned, sizeof(void *));
|
---|
227 | #ifdef RTALLOC_USE_EFENCE
|
---|
228 | void *pv = rtR3MemAlloc("AllocZVar", RTMEMTYPE_RTMEMALLOCZ, cbUnaligned, cbAligned, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
229 | #else
|
---|
230 | void *pv = RTMemAllocZTag(cbAligned, pszTag);
|
---|
231 | #endif
|
---|
232 | return pv;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | RTDECL(void *) RTMemReallocTag(void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW_DEF
|
---|
237 | {
|
---|
238 | #ifdef RTALLOC_USE_EFENCE
|
---|
239 | void *pv = rtR3MemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, pszTag, ASMReturnAddress(), NULL, 0, NULL);
|
---|
240 |
|
---|
241 | #else /* !RTALLOC_USE_EFENCE */
|
---|
242 |
|
---|
243 | # ifdef RTMEMALLOC_USE_TRACKER
|
---|
244 | void *pvRealOld = RTMemTrackerHdrReallocPrep(pvOld, 0, pszTag, ASMReturnAddress());
|
---|
245 | size_t cbRealNew = cbNew || !pvRealOld ? cbNew + sizeof(RTMEMTRACKERHDR) : 0;
|
---|
246 | void *pvNew = realloc(pvRealOld, cbRealNew);
|
---|
247 | void *pv = RTMemTrackerHdrReallocDone(pvNew, cbNew, pvOld, pszTag, ASMReturnAddress());
|
---|
248 | # else
|
---|
249 | void *pv = realloc(pvOld, cbNew); NOREF(pszTag);
|
---|
250 | # endif
|
---|
251 | AssertMsg(pv || !cbNew, ("realloc(%p, %#zx) failed!!!\n", pvOld, cbNew));
|
---|
252 | AssertMsg( cbNew < RTMEM_ALIGNMENT
|
---|
253 | || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1))
|
---|
254 | || ( (cbNew & RTMEM_ALIGNMENT) + ((uintptr_t)pv & RTMEM_ALIGNMENT)) == RTMEM_ALIGNMENT
|
---|
255 | , ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
|
---|
256 | #endif /* !RTALLOC_USE_EFENCE */
|
---|
257 | return pv;
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | RTDECL(void) RTMemFree(void *pv) RT_NO_THROW_DEF
|
---|
262 | {
|
---|
263 | if (pv)
|
---|
264 | #ifdef RTALLOC_USE_EFENCE
|
---|
265 | rtR3MemFree("Free", RTMEMTYPE_RTMEMFREE, pv, 0, ASMReturnAddress(), NULL, 0, NULL);
|
---|
266 | #else
|
---|
267 | # ifdef RTMEMALLOC_USE_TRACKER
|
---|
268 | pv = RTMemTrackerHdrFree(pv, 0, NULL, ASMReturnAddress(), RTMEMTRACKERMETHOD_FREE);
|
---|
269 | # endif
|
---|
270 | free(pv);
|
---|
271 | #endif
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | RTDECL(void) RTMemFreeZ(void *pv, size_t cb) RT_NO_THROW_DEF
|
---|
276 | {
|
---|
277 | if (pv)
|
---|
278 | {
|
---|
279 | #ifdef RTALLOC_USE_EFENCE
|
---|
280 | rtR3MemFree("Free", RTMEMTYPE_RTMEMFREEZ, pv, cb, ASMReturnAddress(), NULL, 0, NULL);
|
---|
281 | #else
|
---|
282 | # ifdef RTMEMALLOC_USE_TRACKER
|
---|
283 | pv = RTMemTrackerHdrFree(pv, cb, NULL, ASMReturnAddress(), RTMEMTRACKERMETHOD_FREE);
|
---|
284 | # endif
|
---|
285 | RT_BZERO(pv, cb);
|
---|
286 | free(pv);
|
---|
287 | #endif
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 |
|
---|
293 | DECLHIDDEN(void *) rtMemBaseAlloc(size_t cb)
|
---|
294 | {
|
---|
295 | Assert(cb > 0 && cb < _1M);
|
---|
296 | return malloc(cb);
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | DECLHIDDEN(void) rtMemBaseFree(void *pv)
|
---|
301 | {
|
---|
302 | free(pv);
|
---|
303 | }
|
---|
304 |
|
---|