vtv_malloc.h 3.1 KB

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