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 // If the syzyasan_rtl.dll is not loaded it loads it and the heap functions | |
|
Sébastien Marchand
2017/07/31 15:01:40
This comment isn't true, it only retrieves the mod
njanevsk
2017/07/31 18:57:28
Done.
| |
| 34 // that it provides. | |
| 35 void ResolveAsanRuntimePointers(AsanRuntimePointers* asan_runtime_ptrs) { | |
| 36 if (asan_runtime_ptrs->asan_module == nullptr) { | |
| 37 asan_runtime_ptrs->asan_module = GetModuleHandle(L"syzyasan_rtl.dll"); | |
|
Sigurður Ásgeirsson
2017/07/28 17:03:14
if this returns nullptr, then the calls below may
Sébastien Marchand
2017/07/31 15:01:40
Ha good point, it's probably safe as-is for the in
njanevsk
2017/07/31 18:57:28
I added a check to make sure that the module is re
| |
| 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 asan_runtime_ptrs->asan_heap = asan_runtime_ptrs->heap_create(0, 0, 0); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 // It returns a pointer to a static data structure containing the syzyasan | |
| 51 // module and | |
| 52 // the heap methods. | |
|
Sébastien Marchand
2017/07/31 15:01:40
Move this to the previous line.
njanevsk
2017/07/31 18:57:28
Done.
| |
| 53 const AsanRuntimePointers* GetAsanHeapShim() { | |
| 54 static AsanRuntimePointers asan_runtime_ptrs = {}; | |
|
Sigurður Ásgeirsson
2017/07/28 17:03:14
In Chrome land, we've now started to rely on threa
Sébastien Marchand
2017/07/31 15:01:40
Ha, that's cool. In this case you could add a cons
njanevsk
2017/07/31 18:57:28
Done.
| |
| 55 if (asan_runtime_ptrs.asan_module == nullptr) { | |
| 56 ResolveAsanRuntimePointers(&asan_runtime_ptrs); | |
| 57 } | |
| 58 return &asan_runtime_ptrs; | |
| 59 } | |
| 60 | |
| 61 inline HANDLE get_heap_handle() { | |
| 62 return GetAsanHeapShim()->asan_heap; | |
| 63 } | |
| 64 | |
| 65 } // namespace | |
| 66 | |
| 67 // These symbols override the CRT's implementation of the same functions. | |
| 68 __declspec(restrict) void* malloc(size_t size) { | |
| 69 return GetAsanHeapShim()->heap_alloc(get_heap_handle(), 0, size); | |
|
Sigurður Ásgeirsson
2017/07/28 17:03:14
does compiler do a decent job of eliminating the d
Sébastien Marchand
2017/07/31 15:01:40
Not that this shim is only intended to be used in
njanevsk
2017/07/31 18:57:28
Leaving it as it is for now.
| |
| 70 } | |
| 71 | |
| 72 void free(void* ptr) { | |
| 73 GetAsanHeapShim()->heap_free(get_heap_handle(), 0, ptr); | |
| 74 } | |
| 75 | |
| 76 __declspec(restrict) void* realloc(void* ptr, size_t size) { | |
| 77 return GetAsanHeapShim()->heap_realloc(get_heap_handle(), 0, ptr, size); | |
| 78 } | |
| 79 | |
| 80 __declspec(restrict) void* calloc(size_t n, size_t size) { | |
| 81 void* ptr = malloc(size * n); | |
| 82 if (ptr != nullptr) | |
| 83 ::memset(ptr, 0, size * n); | |
| 84 return ptr; | |
| 85 } | |
| 86 | |
| 87 // The symbols | |
| 88 // * __acrt_heap | |
| 89 // * __acrt_initialize_heap | |
| 90 // * __acrt_uninitialize_heap | |
| 91 // * _get_heap_handle | |
| 92 // must be overridden all or none, as they are otherwise supplied | |
| 93 // by heap_handle.obj in the ucrt.lib file. | |
| 94 HANDLE __acrt_heap = nullptr; | |
| 95 | |
| 96 bool __acrt_initialize_heap() { | |
|
Sigurður Ásgeirsson
2017/07/28 17:03:14
if you intend this for use in Chrome, then it won'
Sébastien Marchand
2017/07/31 15:01:40
This shim is not meant to be used in Chrome.
njanevsk
2017/07/31 18:57:28
Not fixing cause it is not meant to be use with Ch
Sigurður Ásgeirsson
2017/07/31 19:05:52
I don't understand why you'd want the integration
njanevsk
2017/08/01 15:46:43
I tried using the same heap but the Clang test cas
Sébastien Marchand
2017/08/03 21:49:45
The problem here is that we can't use a heap that
Sigurður Ásgeirsson
2017/08/04 13:55:13
No, this isn't OK, and this WILL fail in Chrome, s
| |
| 97 // TODO(sebmarchand): Check if it is possible to use asan_heap here. | |
| 98 __acrt_heap = ::HeapCreate(0, 0, 0); | |
| 99 return true; | |
| 100 } | |
| 101 | |
| 102 bool __acrt_uninitialize_heap() { | |
| 103 ::HeapDestroy(__acrt_heap); | |
| 104 __acrt_heap = nullptr; | |
| 105 return true; | |
| 106 } | |
| 107 | |
| 108 intptr_t _get_heap_handle(void) { | |
| 109 return reinterpret_cast<intptr_t>(__acrt_heap); | |
| 110 } | |
| 111 | |
| 112 } // extern "C" | |
| OLD | NEW |