Chromium Code Reviews| 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 | |
|
Sébastien Marchand
2017/08/08 15:11:55
Add a period.
njanevsk
2017/08/08 18:05:58
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 typedef BOOL(WINAPI* HeapDestroyPtr)(HANDLE); | |
| 24 | |
| 25 struct AsanRuntimePointers { | |
| 26 HANDLE asan_heap = nullptr; | |
|
Sébastien Marchand
2017/08/08 15:11:55
Move these after the constructor as specified in t
njanevsk
2017/08/08 18:05:58
Done.
| |
| 27 HMODULE asan_module = nullptr; | |
| 28 HeapCreatePtr heap_create = nullptr; | |
| 29 HeapAllocPtr heap_alloc = nullptr; | |
| 30 HeapFreePtr heap_free = nullptr; | |
| 31 HeapReAllocPtr heap_realloc = nullptr; | |
| 32 HeapDestroyPtr heap_destroy = nullptr; | |
| 33 static AsanRuntimePointers asan_runtime; | |
| 34 // It retrieves the handle for the syzyasan_rtl.dll module | |
|
Sébastien Marchand
2017/08/08 15:11:55
This should go in the body of the function.
njanevsk
2017/08/08 18:05:58
Done.
| |
| 35 // and the heap functions that it provides. | |
| 36 AsanRuntimePointers(){ | |
| 37 if (asan_runtime.asan_module == nullptr) { | |
| 38 asan_runtime.asan_module = GetModuleHandle(L"syzyasan_rtl.dll"); | |
| 39 if (asan_runtime.asan_module != nullptr) { | |
| 40 asan_runtime.heap_create = reinterpret_cast<HeapCreatePtr>( | |
| 41 ::GetProcAddress(asan_runtime.asan_module, "asan_HeapCreate")); | |
| 42 asan_runtime.heap_alloc = reinterpret_cast<HeapAllocPtr>( | |
| 43 ::GetProcAddress(asan_runtime.asan_module, "asan_HeapAlloc")); | |
| 44 asan_runtime.heap_free = reinterpret_cast<HeapFreePtr>( | |
| 45 ::GetProcAddress(asan_runtime.asan_module, "asan_HeapFree")); | |
| 46 asan_runtime.heap_realloc = reinterpret_cast<HeapReAllocPtr>( | |
| 47 ::GetProcAddress(asan_runtime.asan_module, "asan_HeapReAlloc")); | |
| 48 asan_runtime.heap_destroy = reinterpret_cast<HeapDestroyPtr>( | |
| 49 ::GetProcAddress(asan_runtime.asan_module, "asan_HeapDestroy")); | |
| 50 asan_runtime.asan_heap = asan_runtime.heap_create(0, 0, 0); | |
| 51 } | |
| 52 } | |
| 53 }; | |
| 54 }; | |
| 55 | |
| 56 AsanRuntimePointers __attribute__((no_sanitize_address)) AsanRuntimePointers::as an_runtime; | |
|
Sébastien Marchand
2017/08/08 15:11:55
This is > 80 characters.
Sébastien Marchand
2017/08/08 15:11:55
Add a comment to explain why you need to use the n
njanevsk
2017/08/08 18:05:58
Done.
njanevsk
2017/08/08 18:05:58
Done.
| |
| 57 | |
| 58 // It returns a pointer to a static data structure containing the syzyasan | |
| 59 // module and the heap methods. | |
| 60 const AsanRuntimePointers* GetAsanHeapShim() { | |
|
Sébastien Marchand
2017/08/08 15:11:55
Do we need this? Couldn't we use AsanRuntimePointe
njanevsk
2017/08/08 18:05:58
Done.
| |
| 61 return &AsanRuntimePointers::asan_runtime; | |
| 62 } | |
| 63 | |
| 64 inline HANDLE get_heap_handle() { | |
| 65 return GetAsanHeapShim()->asan_heap; | |
| 66 } | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 // These symbols override the CRT's implementation of the same functions. | |
| 71 __declspec(restrict) void* malloc(size_t size) { | |
| 72 return GetAsanHeapShim()->heap_alloc(get_heap_handle(), 0, size); | |
| 73 } | |
| 74 | |
| 75 void free(void* ptr) { | |
| 76 GetAsanHeapShim()->heap_free(get_heap_handle(), 0, ptr); | |
| 77 } | |
| 78 | |
| 79 __declspec(restrict) void* realloc(void* ptr, size_t size) { | |
| 80 return GetAsanHeapShim()->heap_realloc(get_heap_handle(), 0, ptr, size); | |
| 81 } | |
| 82 | |
| 83 __declspec(restrict) void* calloc(size_t n, size_t size) { | |
| 84 void* ptr = malloc(size * n); | |
| 85 if (ptr != nullptr) | |
| 86 ::memset(ptr, 0, size * n); | |
| 87 return ptr; | |
| 88 } | |
| 89 | |
| 90 // The symbols | |
| 91 // * __acrt_heap | |
| 92 // * __acrt_initialize_heap | |
| 93 // * __acrt_uninitialize_heap | |
| 94 // * _get_heap_handle | |
| 95 // must be overridden all or none, as they are otherwise supplied | |
| 96 // by heap_handle.obj in the ucrt.lib file. | |
| 97 HANDLE __acrt_heap = nullptr; | |
| 98 | |
| 99 bool __acrt_initialize_heap() { | |
| 100 // TODO(sebmarchand): Check if it is possible to use asan_heap here. | |
|
Sébastien Marchand
2017/08/08 15:11:55
Remove this TODO, add something like this instead:
njanevsk
2017/08/08 18:05:58
Done.
| |
| 101 __acrt_heap = ::HeapCreate(0, 0, 0); | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 bool __acrt_uninitialize_heap() { | |
| 106 ::HeapDestroy(__acrt_heap); | |
| 107 __acrt_heap = nullptr; | |
| 108 return true; | |
| 109 } | |
| 110 | |
| 111 intptr_t _get_heap_handle(void) { | |
| 112 return reinterpret_cast<intptr_t>(__acrt_heap); | |
| 113 } | |
| 114 | |
| 115 } // extern "C" | |
| OLD | NEW |