vtv_malloc.cc 8.4 KB

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