vtv_malloc.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2012-2013
  2. // Free Software Foundation
  3. //
  4. // This file is part of GCC.
  5. //
  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.
  10. // GCC is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // Under Section 7 of GPL version 3, you are granted additional
  15. // permissions described in the GCC Runtime Library Exception, version
  16. // 3.1, as published by the Free Software Foundation.
  17. // You should have received a copy of the GNU General Public License and
  18. // a copy of the GCC Runtime Library Exception along with this program;
  19. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. // <http://www.gnu.org/licenses/>.
  21. #ifndef _VTV_MALLOC_H
  22. #define _VTV_MALLOC_H 1
  23. #include <stdlib.h>
  24. /* Alignment mask for any object returned by the VTV memory pool */
  25. #ifdef __LP64__
  26. #define VTV_ALIGNMENT_MASK (0x7)
  27. #else
  28. #define VTV_ALIGNMENT_MASK (0x3)
  29. #endif
  30. /* The following function is used to instrument the compiler and find
  31. out how many cycles are being spent in various vtable verification
  32. runtime library functions. */
  33. #ifdef __x86_64__
  34. static inline unsigned long
  35. rdtsc ()
  36. {
  37. unsigned long long hi, lo;
  38. asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
  39. return hi << 32 | lo;
  40. }
  41. #elif defined (__i386__)
  42. static inline unsigned long long
  43. rdtsc ()
  44. {
  45. unsigned long long var;
  46. asm volatile ("rdtsc" : "=A" (var));
  47. return var;
  48. }
  49. #else
  50. static inline unsigned long long
  51. rdtsc ()
  52. {
  53. /* Create an empty function for unknown architectures, so that the
  54. calls to this function in vtv_malloc.cc and vtv_rts.cc do not cause
  55. compilation errors. */
  56. return ((unsigned long long) 0);
  57. }
  58. #endif
  59. /* The following variables are used only for debugging and performance tuning
  60. purposes. Therefore they do not need to be "protected". They cannot be used
  61. to attack the vtable verification system and if they become corrupted it will
  62. not affect the correctness or security of any of the rest of the vtable
  63. verification feature. */
  64. extern unsigned int num_calls_to_mprotect;
  65. extern unsigned int num_pages_protected;
  66. extern unsigned int num_calls_to_regset;
  67. extern unsigned int num_calls_to_regpair;
  68. extern unsigned long long mprotect_cycles;
  69. extern unsigned long long regset_cycles;
  70. extern unsigned long long regpair_cycles;
  71. /* Function declarations. */
  72. extern void __vtv_malloc_init (void);
  73. extern void *__vtv_malloc (size_t size) __attribute__ ((malloc));
  74. extern void __vtv_free (void * ptr);
  75. extern void __vtv_malloc_protect (void);
  76. extern void __vtv_malloc_unprotect (void);
  77. extern void __vtv_malloc_stats (void);
  78. extern void __vtv_malloc_dump_stats (void);
  79. extern int __vtv_count_mmapped_pages (void);
  80. #if defined (__CYGWIN__) || defined (__MINGW32__)
  81. extern "C" int mprotect (void *addr, int len, int prot);
  82. #define PROT_READ 0x1
  83. #define PROT_WRITE 0x2
  84. #endif
  85. #endif /* vtv_malloc.h */