annotate libvtv/vtv_malloc.cc @ 147:1ccf269e73e8

remove merge mark
author anatofuz
date Sun, 01 Mar 2020 16:36:00 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1 /* Copyright (C) 2012-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
2
kono
parents:
diff changeset
3 This file is part of GCC.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 GCC is free software; you can redistribute it and/or modify
kono
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
7 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
8 any later version.
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 GCC is distributed in the hope that it will be useful,
kono
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
13 GNU General Public License for more details.
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
16 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
17 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
20 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
22 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 /* This file is part of the vtable verification runtime library. It
kono
parents:
diff changeset
25 contains our memory allocation and deallocation routines, which we
kono
parents:
diff changeset
26 use in order to keep track of the pages in memory in which our sets
kono
parents:
diff changeset
27 of valid vtable pointes are stored. (We need to know the pages so
kono
parents:
diff changeset
28 we can set the protections on them appropriately). For more
kono
parents:
diff changeset
29 information about the vtable verification feature, see the comments
kono
parents:
diff changeset
30 in vtv_rts.cc. We use the existing obstack implementation in our
kono
parents:
diff changeset
31 memory allocation scheme. */
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 #include <stdlib.h>
kono
parents:
diff changeset
34 #include <unistd.h>
kono
parents:
diff changeset
35 #if defined (__CYGWIN__) || defined (__MINGW32__)
kono
parents:
diff changeset
36 #include <windows.h>
kono
parents:
diff changeset
37 #else
kono
parents:
diff changeset
38 #include <sys/mman.h>
kono
parents:
diff changeset
39 #endif
kono
parents:
diff changeset
40 #include <sys/types.h>
kono
parents:
diff changeset
41 #include <sys/stat.h>
kono
parents:
diff changeset
42 #include <fcntl.h>
kono
parents:
diff changeset
43 #include <stdio.h>
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 #include "vtv_utils.h"
kono
parents:
diff changeset
46 #include "vtv_malloc.h"
kono
parents:
diff changeset
47 #include "obstack.h"
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 /* The following variables are used only for debugging and performance tuning
kono
parents:
diff changeset
50 purposes. Therefore they do not need to be "protected". They cannot be used
kono
parents:
diff changeset
51 to attack the vtable verification system and if they become corrupted it will
kono
parents:
diff changeset
52 not affect the correctness or security of any of the rest of the vtable
kono
parents:
diff changeset
53 verification feature. */
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 unsigned int num_calls_to_mprotect = 0;
kono
parents:
diff changeset
56 unsigned int num_pages_protected = 0;
kono
parents:
diff changeset
57 unsigned int long long mprotect_cycles = 0;
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 /* Put the following variables in our ".vtable_map_vars" section so
kono
parents:
diff changeset
60 that they are protected. They are explicitly unprotected and
kono
parents:
diff changeset
61 protected again by calls to __vtv_unprotect and __vtv_protect */
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 static struct obstack vtv_obstack VTV_PROTECTED_VAR;
kono
parents:
diff changeset
64 static void *current_chunk VTV_PROTECTED_VAR = 0;
kono
parents:
diff changeset
65 static size_t current_chunk_size VTV_PROTECTED_VAR = 0;
kono
parents:
diff changeset
66 static int malloc_initialized VTV_PROTECTED_VAR = 0;
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 #if defined (__CYGWIN__) || defined (__MINGW32__)
kono
parents:
diff changeset
69 //sysconf(_SC_PAGE_SIZE) port
kono
parents:
diff changeset
70 long sysconf_SC_PAGE_SIZE()
kono
parents:
diff changeset
71 {
kono
parents:
diff changeset
72 SYSTEM_INFO si;
kono
parents:
diff changeset
73 GetSystemInfo(&si);
kono
parents:
diff changeset
74 long pageSize = (long)si.dwPageSize;
kono
parents:
diff changeset
75 return pageSize;
kono
parents:
diff changeset
76 //return 4096; // standard usermode 32bit pagesize in bytes // FIXME
kono
parents:
diff changeset
77 }
kono
parents:
diff changeset
78 #endif
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 /* The function goes through and counts all the pages we have allocated
kono
parents:
diff changeset
81 so far. It returns the page count. */
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 int
kono
parents:
diff changeset
84 __vtv_count_mmapped_pages (void)
kono
parents:
diff changeset
85 {
kono
parents:
diff changeset
86 int count = 0;
kono
parents:
diff changeset
87 struct _obstack_chunk * ci = (struct _obstack_chunk *) current_chunk;
kono
parents:
diff changeset
88 while (ci)
kono
parents:
diff changeset
89 {
kono
parents:
diff changeset
90 count++;
kono
parents:
diff changeset
91 ci = ci->prev;
kono
parents:
diff changeset
92 }
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 return count;
kono
parents:
diff changeset
95 }
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 /* This function goes through all of the pages we have allocated so
kono
parents:
diff changeset
98 far and calls mprotect to change the protections on the pages,
kono
parents:
diff changeset
99 according to the value of PROTECTION_FLAG. */
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 static void
kono
parents:
diff changeset
102 change_protections_on_data_chunks (int protection_flag)
kono
parents:
diff changeset
103 {
kono
parents:
diff changeset
104 struct _obstack_chunk *ci;
kono
parents:
diff changeset
105 ci = (struct _obstack_chunk *) current_chunk;
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 while (ci)
kono
parents:
diff changeset
108 {
kono
parents:
diff changeset
109 /* Initial set up for mprotect call.*/
kono
parents:
diff changeset
110 struct _obstack_chunk *protect_start = ci;
kono
parents:
diff changeset
111 size_t chunk_size;
kono
parents:
diff changeset
112 size_t total_size;
kono
parents:
diff changeset
113 unsigned int num_pages_in_chunk;
kono
parents:
diff changeset
114 char *next_page;
kono
parents:
diff changeset
115 unsigned long long start, end;
kono
parents:
diff changeset
116 int result;
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 /* As long as the next 'chunk' is adjacent to the current one,
kono
parents:
diff changeset
120 keep going down the list. */
kono
parents:
diff changeset
121 do
kono
parents:
diff changeset
122 {
kono
parents:
diff changeset
123 chunk_size = (ci->limit - (char *) ci);
kono
parents:
diff changeset
124 total_size = (ci->limit - (char *) protect_start);
kono
parents:
diff changeset
125 num_pages_in_chunk = chunk_size / VTV_PAGE_SIZE;
kono
parents:
diff changeset
126 if (chunk_size % VTV_PAGE_SIZE > 0)
kono
parents:
diff changeset
127 num_pages_in_chunk++;
kono
parents:
diff changeset
128 next_page = (char *) ci + (num_pages_in_chunk * VTV_PAGE_SIZE);
kono
parents:
diff changeset
129 ci = ci->prev;
kono
parents:
diff changeset
130 } while (ci && (char *) ci == next_page);
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 VTV_DEBUG_ASSERT (((unsigned long) protect_start & (VTV_PAGE_SIZE - 1))
kono
parents:
diff changeset
133 == 0);
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 /* Protect the contiguous chunks so far. */
kono
parents:
diff changeset
136 start = rdtsc ();
kono
parents:
diff changeset
137 result = mprotect (protect_start, total_size, protection_flag);
kono
parents:
diff changeset
138 end = rdtsc ();
kono
parents:
diff changeset
139 mprotect_cycles += end - start;
kono
parents:
diff changeset
140 if (result == -1)
kono
parents:
diff changeset
141 VTV_error ();
kono
parents:
diff changeset
142 num_calls_to_mprotect++;
kono
parents:
diff changeset
143 num_pages_protected += (total_size + VTV_PAGE_SIZE - 1)/ VTV_PAGE_SIZE;
kono
parents:
diff changeset
144 }
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 #ifdef VTV_DEBUG
kono
parents:
diff changeset
147 __vtv_malloc_dump_stats ();
kono
parents:
diff changeset
148 #endif
kono
parents:
diff changeset
149 }
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 /* This function makes all of our allocated pages read-only. */
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 void
kono
parents:
diff changeset
154 __vtv_malloc_protect (void)
kono
parents:
diff changeset
155 {
kono
parents:
diff changeset
156 change_protections_on_data_chunks (PROT_READ);
kono
parents:
diff changeset
157 }
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 /* This function makes all of our allocated pages read-write. */
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 void
kono
parents:
diff changeset
162 __vtv_malloc_unprotect (void)
kono
parents:
diff changeset
163 {
kono
parents:
diff changeset
164 change_protections_on_data_chunks (PROT_READ | PROT_WRITE);
kono
parents:
diff changeset
165 }
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 /* Allocates a SIZE-sized chunk of memory that is aligned to a page
kono
parents:
diff changeset
168 boundary. The amount of memory requested (SIZE) must be a multiple
kono
parents:
diff changeset
169 of the page size. Note: We must use mmap to allocate the memory;
kono
parents:
diff changeset
170 using malloc here will cause problems. */
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 static void *
kono
parents:
diff changeset
173 obstack_chunk_alloc (size_t size)
kono
parents:
diff changeset
174 {
kono
parents:
diff changeset
175 /* Increase size to the next multiple of VTV_PAGE_SIZE. */
kono
parents:
diff changeset
176 size = (size + (VTV_PAGE_SIZE - 1)) & (~(VTV_PAGE_SIZE - 1));
kono
parents:
diff changeset
177 VTV_DEBUG_ASSERT ((size & (VTV_PAGE_SIZE - 1)) == 0);
kono
parents:
diff changeset
178 void *allocated;
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 #if defined (__CYGWIN__) || defined (__MINGW32__)
kono
parents:
diff changeset
181 if ((allocated = VirtualAlloc(NULL, size, MEM_RESERVE|MEM_COMMIT,
kono
parents:
diff changeset
182 PAGE_READWRITE)) == 0)
kono
parents:
diff changeset
183 #else
kono
parents:
diff changeset
184 if ((allocated = mmap (NULL, size, PROT_READ | PROT_WRITE,
kono
parents:
diff changeset
185 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) == 0)
kono
parents:
diff changeset
186 #endif
kono
parents:
diff changeset
187 VTV_error ();
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 VTV_DEBUG_ASSERT (((unsigned long) allocated & (VTV_PAGE_SIZE - 1)) == 0);
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 current_chunk = allocated;
kono
parents:
diff changeset
192 current_chunk_size = size;
kono
parents:
diff changeset
193 return allocated;
kono
parents:
diff changeset
194 }
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 static void
kono
parents:
diff changeset
197 obstack_chunk_free (void *)
kono
parents:
diff changeset
198 {
kono
parents:
diff changeset
199 /* Do nothing. For our purposes there should be very little
kono
parents:
diff changeset
200 de-allocation. */
kono
parents:
diff changeset
201 }
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 /* This function sets up and initializes the obstack pieces for our
kono
parents:
diff changeset
204 memory allocation scheme. */
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 void
kono
parents:
diff changeset
207 __vtv_malloc_init (void)
kono
parents:
diff changeset
208 {
kono
parents:
diff changeset
209 /* Make sure we only execute the main body of this function ONCE. */
kono
parents:
diff changeset
210 if (malloc_initialized)
kono
parents:
diff changeset
211 return;
kono
parents:
diff changeset
212
kono
parents:
diff changeset
213 #if defined (__CYGWIN__) || defined (__MINGW32__)
kono
parents:
diff changeset
214 if (VTV_PAGE_SIZE != sysconf_SC_PAGE_SIZE())
kono
parents:
diff changeset
215 #else
kono
parents:
diff changeset
216 if (VTV_PAGE_SIZE != sysconf (_SC_PAGE_SIZE))
kono
parents:
diff changeset
217 #endif
kono
parents:
diff changeset
218 VTV_error ();
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 /* We guarantee that the obstack alloc failed handler will never be
kono
parents:
diff changeset
221 called because in case the allocation of the chunk fails, it will
kono
parents:
diff changeset
222 never return */
kono
parents:
diff changeset
223 obstack_alloc_failed_handler = NULL;
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 obstack_specify_allocation (&vtv_obstack, VTV_PAGE_SIZE, sizeof (long),
kono
parents:
diff changeset
226 obstack_chunk_alloc, obstack_chunk_free);
kono
parents:
diff changeset
227 malloc_initialized = 1;
kono
parents:
diff changeset
228 }
kono
parents:
diff changeset
229
kono
parents:
diff changeset
230 /* This is our external interface for the memory allocation. SIZE is
kono
parents:
diff changeset
231 the requested number of bytes to be allocated/ */
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 void *
kono
parents:
diff changeset
234 __vtv_malloc (size_t size)
kono
parents:
diff changeset
235 {
kono
parents:
diff changeset
236 return obstack_alloc (&vtv_obstack, size);
kono
parents:
diff changeset
237 }
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 /* This is our external interface for memory deallocation. */
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 void
kono
parents:
diff changeset
243 __vtv_free (void *)
kono
parents:
diff changeset
244 {
kono
parents:
diff changeset
245 /* Do nothing. We dont care about recovering unneded memory at this
kono
parents:
diff changeset
246 time. */
kono
parents:
diff changeset
247 }
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249
kono
parents:
diff changeset
250 /* This is a debugging function tat collects statistics about our
kono
parents:
diff changeset
251 memory allocation. */
kono
parents:
diff changeset
252 void
kono
parents:
diff changeset
253 __vtv_malloc_stats (void)
kono
parents:
diff changeset
254 {
kono
parents:
diff changeset
255 int count = 0;
kono
parents:
diff changeset
256 struct _obstack_chunk * ci = (struct _obstack_chunk *) current_chunk;
kono
parents:
diff changeset
257 while (ci)
kono
parents:
diff changeset
258 {
kono
parents:
diff changeset
259 count++;
kono
parents:
diff changeset
260 ci = ci->prev;
kono
parents:
diff changeset
261 }
kono
parents:
diff changeset
262 fprintf (stderr,
kono
parents:
diff changeset
263 "__vtv_malloc_stats:\n Page Size = %lu bytes\n "
kono
parents:
diff changeset
264 "Number of pages = %d\n", static_cast<unsigned long>(VTV_PAGE_SIZE),
kono
parents:
diff changeset
265 count);
kono
parents:
diff changeset
266 }
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 /* This is a debugging function. It writes out our memory allocation
kono
parents:
diff changeset
269 statistics to a log file. */
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 void
kono
parents:
diff changeset
272 __vtv_malloc_dump_stats (void)
kono
parents:
diff changeset
273 {
kono
parents:
diff changeset
274 static int fd = -1;
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 if (fd == -1)
kono
parents:
diff changeset
277 fd = __vtv_open_log ("vtv_mem_protection.log");
kono
parents:
diff changeset
278 if (fd == -1)
kono
parents:
diff changeset
279 return;
kono
parents:
diff changeset
280
kono
parents:
diff changeset
281 int count = 0;
kono
parents:
diff changeset
282 struct _obstack_chunk * ci = (struct _obstack_chunk *) current_chunk;
kono
parents:
diff changeset
283 while (ci)
kono
parents:
diff changeset
284 {
kono
parents:
diff changeset
285 count++;
kono
parents:
diff changeset
286 ci = ci->prev;
kono
parents:
diff changeset
287 }
kono
parents:
diff changeset
288
kono
parents:
diff changeset
289 __vtv_add_to_log (fd, "__vtv_malloc_protect protected=%d pages\n", count);
kono
parents:
diff changeset
290 }