gcc/testsuite/
[official-gcc.git] / libvtv / vtv_malloc.cc
blob8aaa636e0e304b7e265affef0e5c3921e7d85cd8
1 /* Copyright (C) 2012-2013
2 Free Software Foundation
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 /* This file is part of the vtable verification runtime library. It
26 contains our memory allocation and deallocation routines, which we
27 use in order to keep track of the pages in memory in which our sets
28 of valid vtable pointes are stored. (We need to know the pages so
29 we can set the protections on them appropriately). For more
30 information about the vtable verification feature, see the comments
31 in vtv_rts.cc. We use the existing obstack implementation in our
32 memory allocation scheme. */
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <sys/mman.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <stdio.h>
42 #include "vtv_utils.h"
43 #include "vtv_malloc.h"
44 #include "obstack.h"
46 /* The following variables are used only for debugging and performance tuning
47 purposes. Therefore they do not need to be "protected". They cannot be used
48 to attack the vtable verification system and if they become corrupted it will
49 not affect the correctness or security of any of the rest of the vtable
50 verification feature. */
52 unsigned int num_calls_to_mprotect = 0;
53 unsigned int num_pages_protected = 0;
54 unsigned int long long mprotect_cycles = 0;
56 /* Put the following variables in our ".vtable_map_vars" section so
57 that they are protected. They are explicitly unprotected and
58 protected again by calls to __vtv_unprotect and __vtv_protect */
60 static struct obstack vtv_obstack VTV_PROTECTED_VAR;
61 static void *current_chunk VTV_PROTECTED_VAR = 0;
62 static size_t current_chunk_size VTV_PROTECTED_VAR = 0;
63 static int malloc_initialized VTV_PROTECTED_VAR = 0;
65 /* The function goes through and counts all the pages we have allocated
66 so far. It returns the page count. */
68 int
69 __vtv_count_mmapped_pages (void)
71 int count = 0;
72 struct _obstack_chunk * ci = (struct _obstack_chunk *) current_chunk;
73 while (ci)
75 count++;
76 ci = ci->prev;
79 return count;
82 /* This function goes through all of the pages we have allocated so
83 far and calls mprotect to change the protections on the pages,
84 according to the value of PROTECTION_FLAG. */
86 static void
87 change_protections_on_data_chunks (int protection_flag)
89 struct _obstack_chunk *ci;
90 ci = (struct _obstack_chunk *) current_chunk;
92 while (ci)
94 /* Initial set up for mprotect call.*/
95 struct _obstack_chunk *protect_start = ci;
96 size_t chunk_size;
97 size_t total_size;
98 unsigned int num_pages_in_chunk;
99 char *next_page;
100 unsigned long long start, end;
101 int result;
104 /* As long as the next 'chunk' is adjacent to the current one,
105 keep going down the list. */
108 chunk_size = (ci->limit - (char *) ci);
109 total_size = (ci->limit - (char *) protect_start);
110 num_pages_in_chunk = chunk_size / VTV_PAGE_SIZE;
111 if (chunk_size % VTV_PAGE_SIZE > 0)
112 num_pages_in_chunk++;
113 next_page = (char *) ci + (num_pages_in_chunk * VTV_PAGE_SIZE);
114 ci = ci->prev;
115 } while (ci && (char *) ci == next_page);
117 VTV_DEBUG_ASSERT (((unsigned long) protect_start & (VTV_PAGE_SIZE - 1))
118 == 0);
120 /* Protect the contiguous chunks so far. */
121 start = rdtsc ();
122 result = mprotect (protect_start, total_size, protection_flag);
123 end = rdtsc ();
124 mprotect_cycles += end - start;
125 if (result == -1)
126 VTV_error ();
127 num_calls_to_mprotect++;
128 num_pages_protected += (total_size + VTV_PAGE_SIZE - 1)/ VTV_PAGE_SIZE;
131 #ifdef VTV_DEBUG
132 VTV_malloc_dump_stats ();
133 #endif
136 /* This function makes all of our allocated pages read-only. */
138 void
139 __vtv_malloc_protect (void)
141 change_protections_on_data_chunks (PROT_READ);
144 /* This function makes all of our allocated pages read-write. */
146 void
147 __vtv_malloc_unprotect (void)
149 change_protections_on_data_chunks (PROT_READ | PROT_WRITE);
152 /* Allocates a SIZE-sized chunk of memory that is aligned to a page
153 boundary. The amount of memory requested (SIZE) must be a multiple
154 of the page size. Note: We must use mmap to allocate the memory;
155 using malloc here will cause problems. */
157 static void *
158 obstack_chunk_alloc (size_t size)
160 /* Increase size to the next multiple of VTV_PAGE_SIZE. */
161 size = (size + (VTV_PAGE_SIZE - 1)) & (~(VTV_PAGE_SIZE - 1));
162 VTV_DEBUG_ASSERT ((size & (VTV_PAGE_SIZE - 1)) == 0);
163 void *allocated;
165 if ((allocated = mmap (NULL, size, PROT_READ | PROT_WRITE,
166 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) == 0)
167 VTV_error ();
169 VTV_DEBUG_ASSERT (((unsigned long) allocated & (VTV_PAGE_SIZE - 1)) == 0);
171 current_chunk = allocated;
172 current_chunk_size = size;
173 return allocated;
176 static void
177 obstack_chunk_free (size_t)
179 /* Do nothing. For our purposes there should be very little
180 de-allocation. */
183 /* This function sets up and initializes the obstack pieces for our
184 memory allocation scheme. */
186 void
187 __vtv_malloc_init (void)
189 /* Make sure we only execute the main body of this function ONCE. */
190 if (malloc_initialized)
191 return;
193 if (VTV_PAGE_SIZE != sysconf (_SC_PAGE_SIZE))
194 VTV_error ();
196 obstack_chunk_size (&vtv_obstack) = VTV_PAGE_SIZE;
197 obstack_alignment_mask (&vtv_obstack) = sizeof (long) - 1;
198 /* We guarantee that the obstack alloc failed handler will never be
199 called because in case the allocation of the chunk fails, it will
200 never return */
201 obstack_alloc_failed_handler = NULL;
203 obstack_init (&vtv_obstack);
204 malloc_initialized = 1;
207 /* This is our external interface for the memory allocation. SIZE is
208 the requested number of bytes to be allocated/ */
210 void *
211 __vtv_malloc (size_t size)
213 return obstack_alloc (&vtv_obstack, size);
217 /* This is our external interface for memory deallocation. */
219 void
220 __vtv_free (void *)
222 /* Do nothing. We dont care about recovering unneded memory at this
223 time. */
227 /* This is a debugging function tat collects statistics about our
228 memory allocation. */
229 void
230 __vtv_malloc_stats (void)
232 int count = 0;
233 struct _obstack_chunk * ci = (struct _obstack_chunk *) current_chunk;
234 while (ci)
236 count++;
237 ci = ci->prev;
239 fprintf (stderr,
240 "__vtv_malloc_stats:\n Page Size = %lu bytes\n "
241 "Number of pages = %d\n", static_cast<unsigned long>(VTV_PAGE_SIZE),
242 count);
245 /* This is a debugging function. It writes out our memory allocation
246 statistics to a log file. */
248 void
249 __vtv_malloc_dump_stats (void)
251 static int fd = -1;
253 if (fd == -1)
254 fd = __vtv_open_log ("vtv_mem_protection.log");
255 if (fd == -1)
256 return;
258 int count = 0;
259 struct _obstack_chunk * ci = (struct _obstack_chunk *) current_chunk;
260 while (ci)
262 count++;
263 ci = ci->prev;
266 __vtv_add_to_log (fd, "__vtv_malloc_protect protected=%d pages\n", count);