Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: syzygy/integration_tests/allocator_shim.cc

Issue 2984933002: Allocator shims design prototype. (Closed)
Patch Set: Response to reviewers comments. Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | syzygy/integration_tests/integration_tests.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
25 struct AsanRuntimePointers {
26 AsanRuntimePointers() {
27 // It retrieves the handle for the syzyasan_rtl.dll module
28 // and the heap functions that it provides.
29 if (asan_runtime.asan_module == nullptr) {
30 asan_runtime.asan_module = GetModuleHandle(L"syzyasan_rtl.dll");
31 if (asan_runtime.asan_module != nullptr) {
32 asan_runtime.heap_create = reinterpret_cast<HeapCreatePtr>(
33 ::GetProcAddress(asan_runtime.asan_module, "asan_HeapCreate"));
34 asan_runtime.heap_alloc = reinterpret_cast<HeapAllocPtr>(
35 ::GetProcAddress(asan_runtime.asan_module, "asan_HeapAlloc"));
36 asan_runtime.heap_free = reinterpret_cast<HeapFreePtr>(
37 ::GetProcAddress(asan_runtime.asan_module, "asan_HeapFree"));
38 asan_runtime.heap_realloc = reinterpret_cast<HeapReAllocPtr>(
39 ::GetProcAddress(asan_runtime.asan_module, "asan_HeapReAlloc"));
40 asan_runtime.heap_destroy = reinterpret_cast<HeapDestroyPtr>(
41 ::GetProcAddress(asan_runtime.asan_module, "asan_HeapDestroy"));
42 asan_runtime.asan_heap = asan_runtime.heap_create(0, 0, 0);
43 }
44 }
45 };
46
47 static AsanRuntimePointers asan_runtime;
48 HANDLE asan_heap = nullptr;
49 HMODULE asan_module = nullptr;
50 HeapCreatePtr heap_create = nullptr;
51 HeapAllocPtr heap_alloc = nullptr;
52 HeapFreePtr heap_free = nullptr;
53 HeapReAllocPtr heap_realloc = nullptr;
54 HeapDestroyPtr heap_destroy = nullptr;
55 };
56
57 // The no_sanitize_address attribute is needed to prevent instrumentation
58 // because that requires additional methods from Asan that are not supported
59 // in the SyzyAsan runtime library.
60 AsanRuntimePointers
61 __attribute__((no_sanitize_address)) AsanRuntimePointers::asan_runtime;
62
63 inline HANDLE get_heap_handle() {
64 return AsanRuntimePointers::asan_runtime.asan_heap;
65 }
66
67 } // namespace
68
69 // These symbols override the CRT's implementation of the same functions.
70 __declspec(restrict) void* malloc(size_t size) {
71 return AsanRuntimePointers::asan_runtime.heap_alloc(get_heap_handle(), 0,
72 size);
73 }
74
75 void free(void* ptr) {
76 AsanRuntimePointers::asan_runtime.heap_free(get_heap_handle(), 0, ptr);
77 }
78
79 __declspec(restrict) void* realloc(void* ptr, size_t size) {
80 return AsanRuntimePointers::asan_runtime.heap_realloc(get_heap_handle(), 0,
81 ptr, size);
82 }
83
84 __declspec(restrict) void* calloc(size_t n, size_t size) {
85 void* ptr = malloc(size * n);
86 if (ptr != nullptr)
87 ::memset(ptr, 0, size * n);
88 return ptr;
89 }
90
91 // The symbols
92 // * __acrt_heap
93 // * __acrt_initialize_heap
94 // * __acrt_uninitialize_heap
95 // * _get_heap_handle
96 // must be overridden all or none, as they are otherwise supplied
97 // by heap_handle.obj in the ucrt.lib file.
98 HANDLE __acrt_heap = nullptr;
99
100 bool __acrt_initialize_heap() {
101 // The core CRT functions don't use the CRT's memory management
102 // functions, instead they directly use |__acrt_heap| and calls the
103 // ::Heap* functions. Because of this it's not possible to replace this
104 // heap by an Asan one.
105 __acrt_heap = ::HeapCreate(0, 0, 0);
106 return true;
107 }
108
109 bool __acrt_uninitialize_heap() {
110 ::HeapDestroy(__acrt_heap);
111 __acrt_heap = nullptr;
112 return true;
113 }
114
115 intptr_t _get_heap_handle(void) {
116 return reinterpret_cast<intptr_t>(__acrt_heap);
117 }
118
119 } // extern "C"
OLDNEW
« no previous file with comments | « no previous file | syzygy/integration_tests/integration_tests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698