1 | /* $Id: alloc-ef.h 76560 2019-01-01 03:01:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, electric fence.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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 | #ifndef IPRT_INCLUDED_SRC_r3_alloc_ef_h
|
---|
28 | #define IPRT_INCLUDED_SRC_r3_alloc_ef_h
|
---|
29 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
30 | # pragma once
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | /*******************************************************************************
|
---|
34 | * Defined Constants And Macros *
|
---|
35 | *******************************************************************************/
|
---|
36 | #if defined(DOXYGEN_RUNNING)
|
---|
37 | # define RTALLOC_USE_EFENCE
|
---|
38 | # define RTALLOC_EFENCE_IN_FRONT
|
---|
39 | # define RTALLOC_EFENCE_FREE_FILL 'f'
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | /** @def RTALLOC_USE_EFENCE
|
---|
43 | * If defined the electric fence put up for ALL allocations by RTMemAlloc(),
|
---|
44 | * RTMemAllocZ(), RTMemRealloc(), RTMemTmpAlloc() and RTMemTmpAllocZ().
|
---|
45 | */
|
---|
46 | #if 0
|
---|
47 | # define RTALLOC_USE_EFENCE
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | /** @def RTALLOC_EFENCE_SIZE
|
---|
51 | * The size of the fence. This must be page aligned.
|
---|
52 | */
|
---|
53 | #define RTALLOC_EFENCE_SIZE PAGE_SIZE
|
---|
54 |
|
---|
55 | /** @def RTALLOC_EFENCE_ALIGNMENT
|
---|
56 | * The allocation alignment, power of two of course.
|
---|
57 | *
|
---|
58 | * Use this for working around misaligned sizes, usually stemming from
|
---|
59 | * allocating a string or something after the main structure. When you
|
---|
60 | * encounter this, please fix the allocation to RTMemAllocVar or RTMemAllocZVar.
|
---|
61 | */
|
---|
62 | #if 0
|
---|
63 | # define RTALLOC_EFENCE_ALIGNMENT (ARCH_BITS / 8)
|
---|
64 | #else
|
---|
65 | # define RTALLOC_EFENCE_ALIGNMENT 1
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | /** @def RTALLOC_EFENCE_IN_FRONT
|
---|
69 | * Define this to put the fence up in front of the block.
|
---|
70 | * The default (when this isn't defined) is to up it up after the block.
|
---|
71 | */
|
---|
72 | //# define RTALLOC_EFENCE_IN_FRONT
|
---|
73 |
|
---|
74 | /** @def RTALLOC_EFENCE_TRACE
|
---|
75 | * Define this to support actual free and reallocation of blocks.
|
---|
76 | */
|
---|
77 | #define RTALLOC_EFENCE_TRACE
|
---|
78 |
|
---|
79 | /** @def RTALLOC_EFENCE_FREE_DELAYED
|
---|
80 | * This define will enable free() delay and protection of the freed data
|
---|
81 | * while it's being delayed. The value of RTALLOC_EFENCE_FREE_DELAYED defines
|
---|
82 | * the threshold of the delayed blocks.
|
---|
83 | * Delayed blocks does not consume any physical memory, only virtual address space.
|
---|
84 | * Requires RTALLOC_EFENCE_TRACE.
|
---|
85 | */
|
---|
86 | #define RTALLOC_EFENCE_FREE_DELAYED (20 * _1M)
|
---|
87 |
|
---|
88 | /** @def RTALLOC_EFENCE_FREE_FILL
|
---|
89 | * This define will enable memset(,RTALLOC_EFENCE_FREE_FILL,)'ing the user memory
|
---|
90 | * in the block before freeing/decommitting it. This is useful in GDB since GDB
|
---|
91 | * appears to be able to read the content of the page even after it's been
|
---|
92 | * decommitted.
|
---|
93 | * Requires RTALLOC_EFENCE_TRACE.
|
---|
94 | */
|
---|
95 | #if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS) || defined(DOXYGEN_RUNNING)
|
---|
96 | # define RTALLOC_EFENCE_FREE_FILL 'f'
|
---|
97 | #endif
|
---|
98 |
|
---|
99 | /** @def RTALLOC_EFENCE_FILLER
|
---|
100 | * This define will enable memset(,RTALLOC_EFENCE_FILLER,)'ing the allocated
|
---|
101 | * memory when the API doesn't require it to be zero'd.
|
---|
102 | */
|
---|
103 | #define RTALLOC_EFENCE_FILLER 0xef
|
---|
104 |
|
---|
105 | /** @def RTALLOC_EFENCE_NOMAN_FILLER
|
---|
106 | * This define will enable memset(,RTALLOC_EFENCE_NOMAN_FILLER,)'ing the
|
---|
107 | * unprotected but not allocated area of memory, the so called no man's land.
|
---|
108 | */
|
---|
109 | #define RTALLOC_EFENCE_NOMAN_FILLER 0xaa
|
---|
110 |
|
---|
111 | /** @def RTALLOC_EFENCE_FENCE_FILLER
|
---|
112 | * This define will enable memset(,RTALLOC_EFENCE_FENCE_FILLER,)'ing the
|
---|
113 | * fence itself, as debuggers can usually read them.
|
---|
114 | */
|
---|
115 | #define RTALLOC_EFENCE_FENCE_FILLER 0xcc
|
---|
116 |
|
---|
117 | #if defined(DOXYGEN_RUNNING)
|
---|
118 | /** @def RTALLOC_EFENCE_CPP
|
---|
119 | * This define will enable the new and delete wrappers.
|
---|
120 | */
|
---|
121 | # define RTALLOC_EFENCE_CPP
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | #if defined(RUNNING_DOXYGEN)
|
---|
125 | /** @def RTALLOC_REPLACE_MALLOC
|
---|
126 | * Replaces the malloc, calloc, realloc, free and friends in libc (experimental).
|
---|
127 | * Set in LocalConfig.kmk. Requires RTALLOC_EFENCE_TRACE to work. */
|
---|
128 | # define RTALLOC_REPLACE_MALLOC
|
---|
129 | #endif
|
---|
130 | #if defined(RTALLOC_REPLACE_MALLOC) && !defined(RTALLOC_EFENCE_TRACE)
|
---|
131 | # error "RTALLOC_REPLACE_MALLOC requires RTALLOC_EFENCE_TRACE."
|
---|
132 | #endif
|
---|
133 |
|
---|
134 |
|
---|
135 | /*******************************************************************************
|
---|
136 | * Header Files *
|
---|
137 | *******************************************************************************/
|
---|
138 | #ifdef RT_OS_WINDOWS
|
---|
139 | # include <iprt/win/windows.h>
|
---|
140 | #else
|
---|
141 | # include <sys/mman.h>
|
---|
142 | #endif
|
---|
143 | #include <iprt/avl.h>
|
---|
144 | #include <iprt/thread.h>
|
---|
145 |
|
---|
146 |
|
---|
147 | /*******************************************************************************
|
---|
148 | * Structures and Typedefs *
|
---|
149 | *******************************************************************************/
|
---|
150 | /**
|
---|
151 | * Allocation types.
|
---|
152 | */
|
---|
153 | typedef enum RTMEMTYPE
|
---|
154 | {
|
---|
155 | RTMEMTYPE_RTMEMALLOC,
|
---|
156 | RTMEMTYPE_RTMEMALLOCZ,
|
---|
157 | RTMEMTYPE_RTMEMREALLOC,
|
---|
158 | RTMEMTYPE_RTMEMFREE,
|
---|
159 |
|
---|
160 | RTMEMTYPE_NEW,
|
---|
161 | RTMEMTYPE_NEW_ARRAY,
|
---|
162 | RTMEMTYPE_DELETE,
|
---|
163 | RTMEMTYPE_DELETE_ARRAY
|
---|
164 | } RTMEMTYPE;
|
---|
165 |
|
---|
166 | #ifdef RTALLOC_EFENCE_TRACE
|
---|
167 | /**
|
---|
168 | * Node tracking a memory allocation.
|
---|
169 | */
|
---|
170 | typedef struct RTMEMBLOCK
|
---|
171 | {
|
---|
172 | /** Avl node code, key is the user block pointer. */
|
---|
173 | AVLPVNODECORE Core;
|
---|
174 | /** Allocation type. */
|
---|
175 | RTMEMTYPE enmType;
|
---|
176 | /** The unaligned size of the block. */
|
---|
177 | size_t cbUnaligned;
|
---|
178 | /** The aligned size of the block. */
|
---|
179 | size_t cbAligned;
|
---|
180 | /** The allocation tag (read-only string). */
|
---|
181 | const char *pszTag;
|
---|
182 | /** The return address of the allocator function. */
|
---|
183 | void *pvCaller;
|
---|
184 | /** Line number of the alloc call. */
|
---|
185 | unsigned iLine;
|
---|
186 | /** File from within the allocation was made. */
|
---|
187 | const char *pszFile;
|
---|
188 | /** Function from within the allocation was made. */
|
---|
189 | const char *pszFunction;
|
---|
190 | } RTMEMBLOCK, *PRTMEMBLOCK;
|
---|
191 |
|
---|
192 | #endif
|
---|
193 |
|
---|
194 |
|
---|
195 | /*******************************************************************************
|
---|
196 | * Internal Functions *
|
---|
197 | ******************************************************************************/
|
---|
198 | RT_C_DECLS_BEGIN
|
---|
199 | RTDECL(void *) rtR3MemAlloc(const char *pszOp, RTMEMTYPE enmType, size_t cbUnaligned, size_t cbAligned,
|
---|
200 | const char *pszTag, void *pvCaller, RT_SRC_POS_DECL);
|
---|
201 | RTDECL(void *) rtR3MemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew,
|
---|
202 | const char *pszTag, void *pvCaller, RT_SRC_POS_DECL);
|
---|
203 | RTDECL(void) rtR3MemFree(const char *pszOp, RTMEMTYPE enmType, void *pv, void *pvCaller, RT_SRC_POS_DECL);
|
---|
204 | RT_C_DECLS_END
|
---|
205 |
|
---|
206 |
|
---|
207 | /*******************************************************************************
|
---|
208 | * Global Variables *
|
---|
209 | *******************************************************************************/
|
---|
210 | #ifdef RTALLOC_REPLACE_MALLOC
|
---|
211 | RT_C_DECLS_BEGIN
|
---|
212 | extern void * (*g_pfnOrgMalloc)(size_t);
|
---|
213 | extern void * (*g_pfnOrgCalloc)(size_t, size_t);
|
---|
214 | extern void * (*g_pfnOrgRealloc)(void *, size_t);
|
---|
215 | extern void (*g_pfnOrgFree)(void *);
|
---|
216 | RT_C_DECLS_END
|
---|
217 | #endif
|
---|
218 |
|
---|
219 | #endif
|
---|
220 |
|
---|