OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This file defines symbols to override the same functions in the Visual C++ | |
6 // CRT implementation. | |
7 | |
8 #include <windows.h> | |
9 #include <malloc.h> | |
10 | |
11 extern "C" { | |
12 | |
13 namespace { | |
14 | |
15 // Definitions of the following heap management functions provided by | |
16 // the SyzyAsan library: | |
17 // heap_create, heap_destroy, heap_alloc, heap_realloc, and heap_free | |
18 typedef HANDLE(WINAPI* HeapCreatePtr)(DWORD, SIZE_T, SIZE_T); | |
19 typedef BOOL(WINAPI* HeapDestroyPtr)(HANDLE); | |
20 typedef LPVOID(WINAPI* HeapAllocPtr)(HANDLE, DWORD, SIZE_T); | |
21 typedef LPVOID(WINAPI* HeapReAllocPtr)(HANDLE, DWORD, LPVOID, SIZE_T); | |
22 typedef BOOL(WINAPI* HeapFreePtr)(HANDLE, DWORD, LPVOID); | |
23 typedef BOOL(WINAPI* HeapDestroyPtr)(HANDLE); | |
24 struct AsanRuntimePointers { | |
25 HANDLE asan_heap = nullptr; | |
26 HMODULE asan_module = nullptr; | |
27 HeapCreatePtr heap_create = nullptr; | |
28 HeapAllocPtr heap_alloc = nullptr; | |
29 HeapFreePtr heap_free = nullptr; | |
30 HeapReAllocPtr heap_realloc = nullptr; | |
31 HeapDestroyPtr heap_destroy = nullptr; | |
32 AsanRuntimePointers(){}; | |
33 }; | |
34 | |
35 // It retreives the handle for the syzyasan_rtl.dll module | |
Sigurður Ásgeirsson
2017/07/31 19:05:52
nit: speling [sic]
njanevsk
2017/08/01 15:46:43
Done.
| |
36 // and the heap functions that it provides. | |
37 void ResolveAsanRuntimePointers(AsanRuntimePointers* asan_runtime_ptrs) { | |
38 if (asan_runtime_ptrs->asan_module == nullptr) { | |
39 asan_runtime_ptrs->asan_module = GetModuleHandle(L"syzyasan_rtl.dll"); | |
40 if (asan_runtime_ptrs->asan_module != nullptr) { | |
41 asan_runtime_ptrs->heap_create = reinterpret_cast<HeapCreatePtr>( | |
42 ::GetProcAddress(asan_runtime_ptrs->asan_module, "asan_HeapCreate")); | |
43 asan_runtime_ptrs->heap_alloc = reinterpret_cast<HeapAllocPtr>( | |
44 ::GetProcAddress(asan_runtime_ptrs->asan_module, "asan_HeapAlloc")); | |
45 asan_runtime_ptrs->heap_free = reinterpret_cast<HeapFreePtr>( | |
46 ::GetProcAddress(asan_runtime_ptrs->asan_module, "asan_HeapFree")); | |
47 asan_runtime_ptrs->heap_realloc = reinterpret_cast<HeapReAllocPtr>( | |
48 ::GetProcAddress(asan_runtime_ptrs->asan_module, "asan_HeapReAlloc")); | |
49 asan_runtime_ptrs->heap_destroy = reinterpret_cast<HeapDestroyPtr>( | |
50 ::GetProcAddress(asan_runtime_ptrs->asan_module, "asan_HeapDestroy")); | |
51 asan_runtime_ptrs->asan_heap = asan_runtime_ptrs->heap_create(0, 0, 0); | |
52 } | |
53 } | |
54 } | |
55 | |
56 // It returns a pointer to a static data structure containing the syzyasan | |
57 // module and the heap methods. | |
58 const AsanRuntimePointers* GetAsanHeapShim() { | |
59 static AsanRuntimePointers* asan_runtime_ptrs = new AsanRuntimePointers(); | |
Sigurður Ásgeirsson
2017/07/31 19:05:52
better for this to be a straight-up member than a
njanevsk
2017/08/01 15:46:43
Done.
| |
60 if (asan_runtime_ptrs->asan_module == nullptr) { | |
Sigurður Ásgeirsson
2017/07/31 19:05:52
This is not threadsafe, plus there's the question
njanevsk
2017/08/01 15:46:43
Done.
| |
61 ResolveAsanRuntimePointers(asan_runtime_ptrs); | |
62 } | |
63 return asan_runtime_ptrs; | |
64 } | |
65 | |
66 inline HANDLE get_heap_handle() { | |
67 return GetAsanHeapShim()->asan_heap; | |
68 } | |
69 | |
70 } // namespace | |
71 | |
72 // These symbols override the CRT's implementation of the same functions. | |
73 __declspec(restrict) void* malloc(size_t size) { | |
74 return GetAsanHeapShim()->heap_alloc(get_heap_handle(), 0, size); | |
75 } | |
76 | |
77 void free(void* ptr) { | |
78 GetAsanHeapShim()->heap_free(get_heap_handle(), 0, ptr); | |
79 } | |
80 | |
81 __declspec(restrict) void* realloc(void* ptr, size_t size) { | |
82 return GetAsanHeapShim()->heap_realloc(get_heap_handle(), 0, ptr, size); | |
83 } | |
84 | |
85 __declspec(restrict) void* calloc(size_t n, size_t size) { | |
86 void* ptr = malloc(size * n); | |
87 if (ptr != nullptr) | |
88 ::memset(ptr, 0, size * n); | |
89 return ptr; | |
90 } | |
91 | |
92 // The symbols | |
93 // * __acrt_heap | |
94 // * __acrt_initialize_heap | |
95 // * __acrt_uninitialize_heap | |
96 // * _get_heap_handle | |
97 // must be overridden all or none, as they are otherwise supplied | |
98 // by heap_handle.obj in the ucrt.lib file. | |
99 HANDLE __acrt_heap = nullptr; | |
100 | |
101 bool __acrt_initialize_heap() { | |
102 // TODO(sebmarchand): Check if it is possible to use asan_heap here. | |
103 __acrt_heap = ::HeapCreate(0, 0, 0); | |
104 return true; | |
105 } | |
106 | |
107 bool __acrt_uninitialize_heap() { | |
108 ::HeapDestroy(__acrt_heap); | |
109 __acrt_heap = nullptr; | |
110 return true; | |
111 } | |
112 | |
113 intptr_t _get_heap_handle(void) { | |
114 return reinterpret_cast<intptr_t>(__acrt_heap); | |
115 } | |
116 | |
117 } // extern "C" | |
OLD | NEW |