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