vtv_malloc.cc 8.4 KB

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