forked from intel/IntelSEAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.cpp
291 lines (242 loc) · 10.5 KB
/
memory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*********************************************************************************************************************************************************************************************************************************************************************************************
# Intel® Single Event API
#
# This file is provided under the BSD 3-Clause license.
# Copyright (c) 2015, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
# Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
# Neither the name of the Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
**********************************************************************************************************************************************************************************************************************************************************************************************/
#include "ittnotify.h"
#if defined(_DEBUG) && !defined(__ANDROID__)
#ifdef UNICODE
__itt_heap_function g_heap = __itt_heap_function_create(L"CRT", L"Memory");
#else
__itt_heap_function g_heap = __itt_heap_function_create("CRT", "Memory");
#endif
#ifdef _WIN32
#if _MSC_VER == 1800 //VS2013
#define _CRTBLD //hack, no words
#include <../crt/src/dbgint.h> //for definition of _CrtMemBlockHeader
#undef _CRTBLD
#elif _MSC_VER >= 1900 //VS2015
struct _CrtMemBlockHeader //it's in debug_heap.cpp now
{
_CrtMemBlockHeader* _block_header_next;
_CrtMemBlockHeader* _block_header_prev;
char const* _file_name;
int _line_number;
int _block_use;
size_t _data_size;
long lRequest;
};
#endif
#include <crtdbg.h>
class CRecursionScope
{
bool& m_bRecursion;
public:
CRecursionScope(bool& bRecursion)
: m_bRecursion(bRecursion)
{
m_bRecursion = true;
}
~CRecursionScope()
{
m_bRecursion = false;
}
};
int AllocHook(int allocType, void *userData, size_t size, int, long requestNumber, const unsigned char *, int)
{
static __declspec(thread) bool bRecursion = false;
if (bRecursion) return 1;
CRecursionScope scope(bRecursion);
switch (allocType)
{
case _HOOK_ALLOC:
{
//In crt hooks we don't know address of the block on allocation, using request number as id
void* fakePtr = (void*)(uintptr_t)requestNumber;
__itt_heap_allocate_begin(g_heap, size, 0); //since we are called before real allocation we can't measure the time of it
__itt_heap_allocate_end(g_heap, &fakePtr, size, 0);
break;
}
case _HOOK_FREE:
{
//requestNumber is not passed here on _HOOK_FREE, using a bit of knowledge of the internals
requestNumber = (((_CrtMemBlockHeader*)userData)-1)->lRequest;
void* fakePtr = (void*)(uintptr_t)requestNumber;
__itt_heap_free_begin(g_heap, fakePtr); //since we are called before real deallocation we can't measure the time of it
__itt_heap_free_end(g_heap, fakePtr);
break;
}
}
return 1;
}
bool InitMemHooks()
{
_CrtSetAllocHook(AllocHook);
return true;
}
bool bInit = InitMemHooks();
#else
#include <pthread.h>
#include <assert.h>
#include <execinfo.h>
#include <dlfcn.h>
#include <string.h>
pthread_key_t AllocTLSKey()
{
pthread_key_t key = -1;
int res = pthread_key_create(&key, nullptr);
assert(0 == res);
return key;
}
pthread_key_t tls_key = AllocTLSKey();
class CRecursionScope
{
public:
CRecursionScope()
{
int res = pthread_setspecific(tls_key, &tls_key);
assert(0 == res);
}
~CRecursionScope()
{
int res = pthread_setspecific(tls_key, nullptr);
assert(0 == res);
}
};
bool HasSEAInStack()
{
const size_t stack_depth = 100;
void *trace[stack_depth] = {};
#ifdef __APPLE__
const int frames_to_skip = 3;
#else
const int frames_to_skip = 7;
#endif
int trace_size = backtrace(trace, stack_depth);
for (int i = frames_to_skip; i < trace_size; ++i)
{
Dl_info dl_info = {};
dladdr(trace[i], &dl_info);
if (dl_info.dli_fname && strstr(dl_info.dli_fname, "/IntelSEAPI."))
return true;
}
return false;
}
#if defined(__APPLE__) && defined(_DEBUG)
#include <malloc/malloc.h>
#include <mach/mach.h>
#include <sys/syscall.h>
#include <unistd.h>
void* (*g_origMalloc)(struct _malloc_zone_t *zone, size_t size) = nullptr;
void* MallocHook(struct _malloc_zone_t *zone, size_t size)
{
if (pthread_getspecific(tls_key) || HasSEAInStack())
return g_origMalloc(zone, size);
CRecursionScope scope;
__itt_heap_allocate_begin(g_heap, size, 0);
void* res = g_origMalloc(zone, size);
__itt_heap_allocate_end(g_heap, &res, size, 0);
return res;
}
void (*g_origFree)(struct _malloc_zone_t *zone, void *ptr) = nullptr;
void FreeHook(struct _malloc_zone_t *zone, void *ptr)
{
if (pthread_getspecific(tls_key) || HasSEAInStack())
return g_origFree(zone, ptr);
CRecursionScope scope;
__itt_heap_free_begin(g_heap, ptr);
g_origFree(zone, ptr);
__itt_heap_free_end(g_heap, ptr);
}
void (*g_origFreeDefSize)(struct _malloc_zone_t *zone, void *ptr, size_t size);
void FreeDefSizeHook(struct _malloc_zone_t *zone, void *ptr, size_t)
{
FreeHook(zone, ptr);
}
bool InitMemHooks()
{
malloc_zone_t* pMallocZone = malloc_default_zone();
if (!pMallocZone) return false;
vm_protect(mach_task_self(), (uintptr_t)pMallocZone, sizeof(malloc_zone_t), 0, VM_PROT_READ | VM_PROT_WRITE);//remove the write protection
g_origMalloc = pMallocZone->malloc;
pMallocZone->malloc = MallocHook;
g_origFree = pMallocZone->free;
pMallocZone->free = FreeHook;
g_origFreeDefSize = pMallocZone->free_definite_size;
pMallocZone->free_definite_size = FreeDefSizeHook;
vm_protect(mach_task_self(), (uintptr_t)pMallocZone, sizeof(malloc_zone_t), 0, VM_PROT_READ);//put the write protection back
return true;
}
bool bInit = InitMemHooks();
#elif defined(__linux__) && defined(_DEBUG)
#include <stdio.h>
#include <dlfcn.h>
#include <malloc.h>
#ifdef __MALLOC_DEPRECATED
void *malloc(size_t size)
{
static void* (*g_origMalloc)(size_t) = (void*(*)(size_t))dlsym(RTLD_NEXT, "malloc");
if (pthread_getspecific(tls_key) || HasSEAInStack())
return g_origMalloc(size);
CRecursionScope scope;
__itt_heap_allocate_begin(g_heap, size, 0);
void* res = g_origMalloc(size);
__itt_heap_allocate_end(g_heap, &res, size, 0);
return res;
}
void free(void *ptr)
{
static void (*g_origFree)(void *ptr) = (void (*)(void *))dlsym(RTLD_NEXT, "free");
if (pthread_getspecific(tls_key) || HasSEAInStack())
return g_origFree(ptr);
CRecursionScope scope;
__itt_heap_free_begin(g_heap, ptr);
g_origFree(ptr);
__itt_heap_free_end(g_heap, ptr);
}
#else
void (*g_origFree) (void *__ptr, const void *) = nullptr;
void *(*g_origMalloc)(size_t __size, const void *) = nullptr;
void InitHooks()
{
g_origMalloc = __malloc_hook;
g_origFree = __free_hook;
__malloc_hook = MallocHook;
__free_hook = FreeHook;
}
void (* volatile __malloc_initialize_hook)() = InitHooks;
void* MallocHook(size_t size, const void * context)
{
if (pthread_getspecific(tls_key) || HasSEAInStack())
return g_origMalloc(size, context);
CRecursionScope scope;
__itt_heap_allocate_begin(g_heap, size, 0);
void* res = g_origMalloc(size, context);
__itt_heap_allocate_end(g_heap, &res, size, 0);
return res;
}
void FreeHook(void* ptr, const void* context)
{
if (pthread_getspecific(tls_key) || HasSEAInStack())
return g_origFree(ptr, context);
CRecursionScope scope;
__itt_heap_free_begin(g_heap, ptr);
g_origFree(ptr, context);
__itt_heap_free_end(g_heap, ptr);
}
#endif
#endif
#endif
#endif// _DEBUG