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 // The following typedefs are function pointers for the functions: | |
16 // heap_create, heap_destroy, heap_alloc, heap_realloc, and heap_free | |
17 // from the ucrt library that we are overwritting with syzyasan_rtl.dll. | |
Sébastien Marchand
2017/07/25 20:07:07
That's not true, these functions are heap pointers
njanevsk
2017/07/26 16:23:45
Done.
| |
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 | |
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 }; | |
32 | |
33 AsanRuntimePointers asan_runtime_ptrs; | |
34 | |
35 void resolve_asan_runtime_pointers() { | |
Sébastien Marchand
2017/07/25 20:07:07
CamelCase.
njanevsk
2017/07/26 16:23:45
Done.
| |
36 if (asan_shim.asan_module == nullptr) { | |
37 asan_shim.asan_module = GetModuleHandle(L"syzyasan_rtl.dll"); | |
38 asan_shim.heap_create = reinterpret_cast<HeapCreatePtr>( | |
39 ::GetProcAddress(asan_shim.asan_module, "asan_HeapCreate")); | |
40 asan_shim.heap_alloc = reinterpret_cast<HeapAllocPtr>( | |
41 ::GetProcAddress(asan_shim.asan_module, "asan_HeapAlloc")); | |
42 asan_shim.heap_free = reinterpret_cast<HeapFreePtr>( | |
43 ::GetProcAddress(asan_shim.asan_module, "asan_HeapFree")); | |
44 asan_shim.heap_realloc = reinterpret_cast<HeapReAllocPtr>( | |
45 ::GetProcAddress(asan_shim.asan_module, "asan_HeapReAlloc")); | |
46 } | |
47 } | |
48 | |
49 inline HANDLE get_heap_handle() { | |
50 resolve_asan_runtime_pointers(); | |
51 return asan_shim.asan_heap; | |
52 } | |
53 | |
54 } // namespace | |
55 | |
56 // This function behaves similarly to MSVC's _set_new_mode. | |
57 // If flag is 0 (default), calls to malloc will behave normally. | |
58 // If flag is 1, calls to malloc will behave like calls to new, | |
59 // and the std_new_handler will be invoked on failure. | |
60 // Returns the previous mode. | |
61 // | |
62 // Replaces _set_new_mode in ucrt\heap\new_mode.cpp | |
63 int _set_new_mode(int flag) { | |
64 return 0; | |
65 } | |
66 | |
67 // Replaces _query_new_mode in ucrt\heap\new_mode.cpp | |
68 int _query_new_mode() { | |
69 return 0; | |
70 } | |
71 | |
72 // These symbols override the CRT's implementation of the same functions. | |
73 __declspec(restrict) void* malloc(size_t size) { | |
74 resolve_asan_runtime_pointers(); | |
Sébastien Marchand
2017/07/25 20:07:07
I don't really like having these calls in every fu
njanevsk
2017/07/26 16:23:45
Done.
| |
75 return asan_shim.heap_alloc(get_heap_handle(), 0, size); | |
76 } | |
77 | |
78 void free(void* ptr) { | |
79 resolve_asan_runtime_pointers(); | |
80 asan_shim.heap_free(get_heap_handle(), 0, ptr); | |
81 } | |
82 | |
83 __declspec(restrict) void* realloc(void* ptr, size_t size) { | |
84 resolve_asan_runtime_pointers(); | |
85 return asan_shim.heap_realloc(get_heap_handle(), 0, ptr, size); | |
86 } | |
87 | |
88 __declspec(restrict) void* calloc(size_t n, size_t size) { | |
89 resolve_asan_runtime_pointers(); | |
90 void* ptr = malloc(size * n); | |
91 if (ptr != nullptr) | |
92 ::memset(ptr, 0, size * n); | |
93 return ptr; | |
94 } | |
95 | |
96 // The symbols | |
97 // * __acrt_heap | |
98 // * __acrt_initialize_heap | |
99 // * __acrt_uninitialize_heap | |
100 // * _get_heap_handle | |
101 // must be overridden all or none, as they are otherwise supplied | |
102 // by heap_handle.obj in the ucrt.lib file. | |
103 HANDLE __acrt_heap = nullptr; | |
104 | |
105 bool __acrt_initialize_heap() { | |
106 __acrt_heap = ::HeapCreate(0, 0, 0); | |
107 return true; | |
108 } | |
109 | |
110 bool __acrt_uninitialize_heap() { | |
111 ::HeapDestroy(__acrt_heap); | |
112 __acrt_heap = nullptr; | |
113 return true; | |
114 } | |
115 | |
116 intptr_t _get_heap_handle(void) { | |
117 return reinterpret_cast<intptr_t>(__acrt_heap); | |
118 } | |
119 | |
120 } // extern "C" | |
OLD | NEW |