forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteBarrierValidation.cpp
More file actions
436 lines (376 loc) · 14.4 KB
/
WriteBarrierValidation.cpp
File metadata and controls
436 lines (376 loc) · 14.4 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#if IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
#include "gc_wrapper.h"
#include "il2cpp-config.h"
#include "il2cpp-api.h"
#include "gc/WriteBarrierValidation.h"
#include "gc/GarbageCollector.h"
#include "os/Mutex.h"
#include "vm/StackTrace.h"
#include <algorithm>
#include <functional>
#include <map>
#include <string>
// On systems which have the posix backtrace API, you can turn on this define to get native stack traces.
// This can make it easier to debug issues with missing barriers.
#define HAS_POSIX_BACKTRACE IL2CPP_TARGET_OSX
#define HAS_WINDOWS_BACKTRACE IL2CPP_TARGET_WINDOWS
#if HAS_POSIX_BACKTRACE
#include <execinfo.h>
#endif
#if HAS_WINDOWS_BACKTRACE
#include <windows.h>
#endif
using namespace il2cpp::os;
using namespace il2cpp::vm;
#if !IL2CPP_GC_BOEHM
#error "Write Barrier Validation is specific to Boehm GC"
#endif
namespace il2cpp
{
namespace gc
{
enum AllocationKind
{
kPtrFree = 0,
kNormal = 1,
kUncollectable = 2,
kObject = 3
};
struct AllocationInfo
{
size_t size;
StackFrames stacktrace;
#if HAS_POSIX_BACKTRACE || HAS_WINDOWS_BACKTRACE
void *backtraceFrames[128];
int frameCount;
#endif
AllocationKind kind;
};
static std::map<void*, AllocationInfo, std::greater<void*> > g_Allocations;
static std::map<void**, void*> g_References;
static void* g_MinHeap = (void*)0xffffffffffffffffULL;
static void* g_MaxHeap = 0;
static WriteBarrierValidation::ExternalAllocationTrackerFunction g_ExternalAllocationTrackerFunction = NULL;
static WriteBarrierValidation::ExternalWriteBarrierTrackerFunction g_ExternalWriteBarrierTrackerFunction = NULL;
static baselib::ReentrantLock s_AllocationMutex;
static baselib::ReentrantLock s_WriteBarrierMutex;
extern "C" void* GC_malloc_kind(size_t size, int k);
#if HAS_POSIX_BACKTRACE
std::string GetStackPosix(const AllocationInfo &info)
{
std::string result;
char **frameStrings = backtrace_symbols(&info.backtraceFrames[0], info.frameCount);
int frameCount = std::min(info.frameCount, 32);
if (frameStrings != NULL)
{
for (int x = 0; x < frameCount; x++)
{
result += frameStrings[x];
result += "\n";
}
free(frameStrings);
}
return result;
}
#endif
void* GC_malloc_wrapper(size_t size, AllocationKind kind)
{
void* ptr = (char*)GC_malloc_kind(size, kind);
memset(ptr, 0, size);
if (g_ExternalAllocationTrackerFunction != NULL)
g_ExternalAllocationTrackerFunction(ptr, size, kind);
else
{
const StackFrames *trace = StackTrace::GetStackFrames();
os::FastAutoLock lock(&s_AllocationMutex);
AllocationInfo& allocation = g_Allocations[ptr];
allocation.size = size;
allocation.kind = kind;
if (trace != NULL)
allocation.stacktrace = *trace;
#if HAS_POSIX_BACKTRACE
allocation.frameCount = backtrace(&allocation.backtraceFrames[0], 128);
#endif
#if HAS_WINDOWS_BACKTRACE
allocation.frameCount = CaptureStackBackTrace(0, 128, &allocation.backtraceFrames[0], NULL);
#endif
g_MinHeap = std::min(g_MinHeap, ptr);
g_MaxHeap = std::max(g_MaxHeap, (void*)((char*)ptr + size));
}
return ptr;
}
extern "C" void GC_dirty_inner(void **ptr)
{
if (g_ExternalWriteBarrierTrackerFunction)
g_ExternalWriteBarrierTrackerFunction(ptr);
else
{
os::FastAutoLock lock(&s_WriteBarrierMutex);
g_References[ptr] = *ptr;
}
}
extern "C" void GC_free(void *ptr)
{
}
extern "C" void* GC_malloc(size_t size)
{
return GC_malloc_wrapper(size, kNormal);
}
extern "C" void* GC_gcj_malloc(size_t size, void * ptr_to_struct_containing_descr)
{
void ** ptr = (void**)GC_malloc_wrapper(size, kObject);
*ptr = ptr_to_struct_containing_descr;
return ptr;
}
extern "C" void* GC_CALL GC_gcj_vector_malloc(size_t size, void * ptr_to_struct_containing_descr)
{
void ** ptr = (void**)GC_malloc_wrapper(size, kObject);
*ptr = ptr_to_struct_containing_descr;
return ptr;
}
extern "C" void* GC_malloc_uncollectable(size_t size)
{
return GC_malloc_wrapper(size, kUncollectable);
}
extern "C" void* GC_malloc_atomic(size_t size)
{
return GC_malloc_wrapper(size, kPtrFree);
}
static std::string ObjectName(void* object, AllocationKind kind)
{
if (kind != kObject)
{
switch (kind)
{
case kPtrFree: return "Kind: kPtrFree";
case kNormal: return "Kind: kNormal";
case kUncollectable: return "Kind: kUncollectable";
default: return "?";
}
}
Il2CppClass* klass = il2cpp_object_get_class((Il2CppObject*)(object));
if (klass == NULL)
return "";
std::string name = il2cpp_class_get_name(klass);
Il2CppClass* parent = il2cpp_class_get_declaring_type(klass);
while (parent != NULL)
{
klass = parent;
parent = il2cpp_class_get_declaring_type(klass);
name = std::string(il2cpp_class_get_name(klass)) + "/" + name;
}
return std::string(il2cpp_class_get_namespace(klass)) + "::" + name;
}
static std::string GetReadableStackTrace(const StackFrames &stackTrace)
{
std::string str;
for (StackFrames::const_iterator i = stackTrace.begin(); i != stackTrace.end(); i++)
{
Il2CppClass* parent = il2cpp_method_get_declaring_type(i->method);
str += il2cpp_class_get_namespace(parent);
str += '.';
str += il2cpp_class_get_name(parent);
str += ':';
str += il2cpp_method_get_name(i->method);
str += '\n';
}
return str;
}
static std::string LogError(std::pair<void*, AllocationInfo> const & object, void** reference, void *refObject)
{
std::string msg;
char chbuf[1024];
snprintf(chbuf, 1024, "In object %p (%s) with size %zx at offset %zx, allocated at \n%s\n", object.first, ObjectName(object.first, object.second.kind).c_str(), object.second.size, (size_t)((char*)reference - (char*)object.first),
#if HAS_POSIX_BACKTRACE
GetStackPosix(object.second).c_str()
#else
GetReadableStackTrace(object.second.stacktrace).c_str()
#endif
);
msg += chbuf;
snprintf(chbuf, 1024, "Points to object %p of type (%s)\n", refObject, ObjectName(refObject, kPtrFree).c_str());
msg += chbuf;
return msg;
}
// Boehm internal constants
#define GC_DS_TAG_BITS 2
#define GC_DS_TAGS ((1 << GC_DS_TAG_BITS) - 1)
#define GC_DS_LENGTH 0 /* The entire word is a length in bytes that */
/* must be a multiple of 4. */
#define GC_DS_BITMAP 1 /* 30 (62) bits are a bitmap describing pointer */
#ifndef MARK_DESCR_OFFSET
# define MARK_DESCR_OFFSET sizeof(void*)
#endif
void WriteBarrierValidation::SetExternalAllocationTracker(ExternalAllocationTrackerFunction func)
{
g_ExternalAllocationTrackerFunction = func;
}
void WriteBarrierValidation::SetExternalWriteBarrierTracker(ExternalWriteBarrierTrackerFunction func)
{
g_ExternalWriteBarrierTrackerFunction = func;
}
/* Taken from https://randomascii.wordpress.com/2012/02/14/64-bit-made-easy/
Copyright 2012 Bruce Dawson. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
void ReserveBottomMemory()
{
#if defined(_WIN64)
static bool s_initialized = false;
if (s_initialized)
return;
s_initialized = true;
// Start by reserving large blocks of address space, and then
// gradually reduce the size in order to capture all of the
// fragments. Technically we should continue down to 64 KB but
// stopping at 1 MB is sufficient to keep most allocators out.
const size_t LOW_MEM_LINE = 0x1000000000LL; // Modified to reserve bottom 64 GB
size_t totalReservation = 0;
size_t numVAllocs = 0;
size_t numHeapAllocs = 0;
size_t oneMB = 1024 * 1024;
size_t sixtyFourKB = 64 * 1024; // Modified to continue down to 64 KB as our GC allocates chunks in 256 KB
for (size_t size = 256 * oneMB; size >= sixtyFourKB; size /= 2)
{
for (;;)
{
void* p = VirtualAlloc(0, size, MEM_RESERVE, PAGE_NOACCESS);
if (!p)
break;
if ((size_t)p >= LOW_MEM_LINE)
{
// We don't need this memory, so release it completely.
VirtualFree(p, 0, MEM_RELEASE);
break;
}
totalReservation += size;
++numVAllocs;
}
}
// Now repeat the same process but making heap allocations, to use up
// the already reserved heap blocks that are below the 4 GB line.
HANDLE heap = GetProcessHeap();
for (size_t blockSize = 64 * 1024; blockSize >= 16; blockSize /= 2)
{
for (;;)
{
void* p = HeapAlloc(heap, 0, blockSize);
if (!p)
break;
if ((size_t)p >= LOW_MEM_LINE)
{
// We don't need this memory, so release it completely.
HeapFree(heap, 0, p);
break;
}
totalReservation += blockSize;
++numHeapAllocs;
}
}
// Perversely enough the CRT doesn't use the process heap. Consume up
// the memory the CRT heap has already reserved.
for (size_t blockSize = 64 * 1024; blockSize >= 16; blockSize /= 2)
{
for (;;)
{
void* p = malloc(blockSize);
if (!p)
break;
if ((size_t)p >= LOW_MEM_LINE)
{
// We don't need this memory, so release it completely.
free(p);
break;
}
totalReservation += blockSize;
++numHeapAllocs;
}
}
// Print diagnostics showing how many allocations we had to make in
// order to reserve all of low memory, typically less than 200.
char buffer[1000];
sprintf_s(buffer, "Reserved %1.3f MB (%d vallocs,"
"%d heap allocs) of low-memory.\n",
totalReservation / (1024 * 1024.0),
(int)numVAllocs, (int)numHeapAllocs);
OutputDebugStringA(buffer);
#endif
}
void WriteBarrierValidation::Setup()
{
// Reserve bottom 64 GB of memory to force GC into high addresses
// This prevents hashes colliding with heap addresses and causing false detections.
ReserveBottomMemory();
GarbageCollector::Disable();
}
void WriteBarrierValidation::Run()
{
if (g_ExternalAllocationTrackerFunction != NULL)
return;
std::string msg;
msg = "<TestResult Name='WriteBarrierValidation'>\n<![CDATA[\n";
size_t errors = 0;
for (std::map<void*, AllocationInfo>::iterator i = g_Allocations.begin(); i != g_Allocations.end(); i++)
{
if (i->second.kind == kPtrFree)
continue;
intptr_t desc = (intptr_t)GC_NO_DESCRIPTOR;
if (i->second.kind == kObject)
{
desc = *(intptr_t*)(((char*)(*(void**)i->first)) + MARK_DESCR_OFFSET);
if ((desc & GC_DS_TAGS) != GC_DS_BITMAP)
desc = (intptr_t)GC_NO_DESCRIPTOR;
}
for (void** ptr = ((void**)i->first) + 2; ptr < (void**)((char*)i->first + i->second.size); ptr++)
{
if (desc != (intptr_t)GC_NO_DESCRIPTOR)
{
// if we have a GC descriptor bitmap, check if this address can be a pointer, skip otherwise.
size_t ptr_index = ptr - (void**)i->first;
if (((desc >> (sizeof(void*) * 8 - ptr_index - 1)) & 1) == 0)
continue;
}
void* ref = *ptr;
if (ref < g_MinHeap || ref >= g_MaxHeap)
continue;
std::map<void*, AllocationInfo>::iterator j = g_Allocations.lower_bound(ref);
if (j != g_Allocations.end() && j != i && j->second.kind != kUncollectable)
{
if (j->first <= ref && (void*)((char*)j->first + j->second.size) > ref)
{
std::map<void**, void*>::iterator trackedRef = g_References.find(ptr);
if (trackedRef == g_References.end())
{
char chbuf[1024];
snprintf(chbuf, 1024, "\n%p looks like a reference to %p, but was not found in tracked references\n", ptr, ref);
msg += chbuf;
errors++;
msg += LogError(*i, ptr, j->first);
}
else if (trackedRef->second != ref)
{
char chbuf[1024];
snprintf(chbuf, 1024, "\n%p does not match tracked value (%p!=%p).\n", ptr, trackedRef->second, ref);
msg += chbuf;
errors++;
msg += LogError(*i, ptr, j->first);
}
}
}
}
}
msg += "]]>\n</TestResult>\n";
if (errors > 0)
printf("%s", msg.c_str());
}
} /* gc */
} /* il2cpp */
#endif