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 | |
| 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 __attribute__((no_sanitize_address)) asan_runtime_ptrs; | |
| 34 | |
| 35 void ResolveAsanRuntimePointers(AsanRuntimePointers* asan_runtime_ptrs) { | |
|
Sébastien Marchand
2017/07/26 19:12:02
Add a comment to describe what this function does.
njanevsk
2017/07/27 18:02:00
Done.
| |
| 36 if (asan_runtime_ptrs->asan_module == nullptr) { | |
| 37 asan_runtime_ptrs->asan_module = GetModuleHandle(L"syzyasan_rtl.dll"); | |
| 38 asan_runtime_ptrs->heap_create = reinterpret_cast<HeapCreatePtr>( | |
| 39 ::GetProcAddress(asan_runtime_ptrs->asan_module, "asan_HeapCreate")); | |
| 40 asan_runtime_ptrs->heap_alloc = reinterpret_cast<HeapAllocPtr>( | |
| 41 ::GetProcAddress(asan_runtime_ptrs->asan_module, "asan_HeapAlloc")); | |
| 42 asan_runtime_ptrs->heap_free = reinterpret_cast<HeapFreePtr>( | |
| 43 ::GetProcAddress(asan_runtime_ptrs->asan_module, "asan_HeapFree")); | |
| 44 asan_runtime_ptrs->heap_realloc = reinterpret_cast<HeapReAllocPtr>( | |
| 45 ::GetProcAddress(asan_runtime_ptrs->asan_module, "asan_HeapReAlloc")); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 const AsanRuntimePointers* GetAsanHeapShim() { | |
|
Sébastien Marchand
2017/07/26 19:12:02
Ditto, comment.
njanevsk
2017/07/27 18:01:59
Done.
| |
| 50 static AsanRuntimePointers asan_runtime_ptrs; | |
|
Sébastien Marchand
2017/07/26 19:12:02
Add "= {}"
njanevsk
2017/07/27 18:01:59
Done.
| |
| 51 if (asan_runtime_ptrs.asan_module == nullptr) { | |
| 52 ResolveAsanRuntimePointers(&asan_runtime_ptrs); | |
| 53 } | |
| 54 return &asan_runtime_ptrs; | |
| 55 } | |
| 56 | |
| 57 inline HANDLE get_heap_handle() { | |
| 58 return GetAsanHeapShim()->asan_heap; | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 // These symbols override the CRT's implementation of the same functions. | |
| 64 __declspec(restrict) void* malloc(size_t size) { | |
| 65 return GetAsanHeapShim()->heap_alloc(get_heap_handle(), 0, size); | |
| 66 } | |
| 67 | |
| 68 void free(void* ptr) { | |
| 69 GetAsanHeapShim()->heap_free(get_heap_handle(), 0, ptr); | |
| 70 } | |
| 71 | |
| 72 __declspec(restrict) void* realloc(void* ptr, size_t size) { | |
| 73 return GetAsanHeapShim()->heap_realloc(get_heap_handle(), 0, ptr, size); | |
| 74 } | |
| 75 | |
| 76 __declspec(restrict) void* calloc(size_t n, size_t size) { | |
| 77 void* ptr = malloc(size * n); | |
| 78 if (ptr != nullptr) | |
| 79 ::memset(ptr, 0, size * n); | |
| 80 return ptr; | |
| 81 } | |
| 82 | |
| 83 // The symbols | |
| 84 // * __acrt_heap | |
| 85 // * __acrt_initialize_heap | |
| 86 // * __acrt_uninitialize_heap | |
| 87 // * _get_heap_handle | |
| 88 // must be overridden all or none, as they are otherwise supplied | |
| 89 // by heap_handle.obj in the ucrt.lib file. | |
| 90 HANDLE __acrt_heap = nullptr; | |
| 91 | |
| 92 bool __acrt_initialize_heap() { | |
| 93 __acrt_heap = ::HeapCreate(0, 0, 0); | |
|
Sébastien Marchand
2017/07/26 19:12:02
Ideally I'd like to use an Asan heap here too but
njanevsk
2017/07/27 18:02:00
Done.
| |
| 94 return true; | |
| 95 } | |
| 96 | |
| 97 bool __acrt_uninitialize_heap() { | |
| 98 ::HeapDestroy(__acrt_heap); | |
| 99 __acrt_heap = nullptr; | |
| 100 return true; | |
| 101 } | |
| 102 | |
| 103 intptr_t _get_heap_handle(void) { | |
| 104 return reinterpret_cast<intptr_t>(__acrt_heap); | |
| 105 } | |
| 106 | |
| 107 } // extern "C" | |
| OLD | NEW |