aboutsummaryrefslogtreecommitdiff
path: root/vtable-security/libstdc++-v3/libsupc++/vtv_malloc.cc
blob: d1a8647ae3430bb923cfa492e7c92a2ec110b6ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (C) 2012
// Free Software Foundation
//
// This file is part of GCC.
//
// GCC is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3, or (at your option)
// any later version.

// GCC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdio.h>

#include "vtv_malloc.h"
#include <obstack.h>

/* Set the following macro to 1 to get internal debugging messages */
#define VTV_DEBUG 0

/* Put the following variables in a rel.ro section so that the are protected.
   They are explicitly unprotected and protected again by calls to VTV_unprotect
   and VTV_protect */

static struct obstack VTV_obstack VTV_PROTECTED_VAR;
static unsigned long page_size VTV_PROTECTED_VAR = 0;
static void * current_chunk VTV_PROTECTED_VAR = 0;
static size_t current_chunk_size VTV_PROTECTED_VAR = 0;
static int malloc_initialized VTV_PROTECTED_VAR = 0;

/* TODO: Define what is the expected behavior of assert and error */
/* Handling of runtime error */
void
VTV_error (void)
{
  abort();
}

#define VTV_assert(EXPR) ((void)(!(EXPR) ? VTV_error() : (void) 0))

void
VTV_malloc_protect (void)
{
  struct _obstack_chunk * ci;
  ci = (struct _obstack_chunk *) current_chunk;
  while (ci)
  {
    VTV_assert(((unsigned long)ci & (page_size - 1)) == 0);
    if (mprotect(ci, (ci->limit - (char *)ci), PROT_READ) == -1)
      VTV_error();
    ci = ci->prev;
  }
#if (VTV_DEBUG == 1)
  {
    int count = 0;
    ci = (struct _obstack_chunk *) current_chunk;
    while (ci)
    {
      count++;
      ci = ci->prev;
    }
    fprintf(stderr, "VTV_malloc_protect(): protected %d pages\n", count);
  }
#endif
}

void
VTV_malloc_unprotect (void)
{
  struct _obstack_chunk * ci;
  ci = (struct _obstack_chunk *) current_chunk;
  while (ci)
  {
    VTV_assert(((unsigned long)ci & (page_size - 1)) == 0);
    if (mprotect(ci, (ci->limit - (char *)ci), PROT_READ | PROT_WRITE) == -1)
      VTV_error();
    ci = ci->prev;
  }
}

/* Allocates a chunk of memory that is aligned to a page boundary.
   The amount of memory requested must be a multiple of the page size */
static void *
obstack_chunk_alloc (size_t size)
{
  /* TODO: Why do we need to support chunk sizes less that page size? */
  /* Get size to next multiple of page_size */
  size = (size + (page_size - 1)) & (~(page_size - 1));
  VTV_assert((size & (page_size - 1)) == 0);
  void * allocated;

  if ((allocated = mmap (NULL, size, PROT_READ | PROT_WRITE,
                         MAP_PRIVATE | MAP_ANONYMOUS,  -1, 0)) == 0)
    VTV_error();

  VTV_assert(((unsigned long) allocated & (page_size - 1)) == 0);

  current_chunk = allocated;
  current_chunk_size = size;
  return allocated;
}

static void
obstack_chunk_free (size_t )
{
  /* Do nothing. For our purposes there should be very little de-allocation. */
}

void
VTV_malloc_init (void)
{
  /* Make sure we only execute the main body of this function ONCE.  */
  if (malloc_initialized)
    return;

  page_size = sysconf(_SC_PAGE_SIZE);

  obstack_chunk_size (&VTV_obstack) = page_size;
  obstack_alignment_mask(&VTV_obstack) = sizeof(long) - 1;
  /* We guarantee that the obstack alloc failed handler will never be called because
     in case the allocation of the chunk fails, it will never return */
  obstack_alloc_failed_handler = NULL;

  obstack_init(&VTV_obstack);
  malloc_initialized = 1;
}

void *
VTV_malloc (size_t size)
{
  return obstack_alloc (&VTV_obstack, size);
}

void
VTV_free (void *)
{
  /* Do nothing. We dont care about recovering unneded memory */
}

void
VTV_malloc_stats (void)
{
  int count = 0;
   struct _obstack_chunk * ci = (struct _obstack_chunk *) current_chunk;
  while (ci)
  {
    count++;
    ci = ci->prev;
  }
  fprintf(stderr, "VTV_malloc_stats:\n  Page Size = %lu bytes\n  Number of pages = %d\n", page_size, count);
}