about summary refs log tree commit diff
path: root/nptl/tst-tls3-malloc.c
blob: 5eab3cdbb440efc54889a0700ba6411ad18bdba4 (plain) (blame)
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
167
168
169
170
171
172
173
174
175
176
/* Test TLS allocation with an interposed malloc.
   Copyright (C) 2016 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

/* Reuse the test.  */
#include "tst-tls3.c"

#include <sys/mman.h>

/* Interpose a minimal malloc implementation.  This implementation
   deliberately interposes just a restricted set of symbols, to detect
   if the TLS code bypasses the interposed malloc.  */

/* Lock to guard malloc internals.  */
static pthread_mutex_t malloc_lock = PTHREAD_MUTEX_INITIALIZER;

/* Information about an allocation chunk.  */
struct malloc_chunk
{
  /* Start of the allocation.  */
  void *start;
  /* Size of the allocation.  */
  size_t size;
};

enum { malloc_chunk_count = 1000 };
static struct malloc_chunk chunks[malloc_chunk_count];

/* Lock the malloc lock.  */
static void
xlock (void)
{
  int ret = pthread_mutex_lock (&malloc_lock);
  if (ret != 0)
    {
      errno = ret;
      printf ("error: pthread_mutex_lock: %m\n");
      _exit (1);
    }
}

/* Unlock the malloc lock.  */
static void
xunlock (void)
{
  int ret = pthread_mutex_unlock (&malloc_lock);
  if (ret != 0)
    {
      errno = ret;
      printf ("error: pthread_mutex_unlock: %m\n");
      _exit (1);
    }
}

/* Internal malloc without locking and registration.  */
static void *
malloc_internal (size_t size)
{
  void *result = mmap (NULL, size, PROT_READ | PROT_WRITE,
                       MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (result == MAP_FAILED)
    {
      printf ("error: mmap: %m\n");
      _exit (1);
    }
  return result;
}

void *
malloc (size_t size)
{
  if (size == 0)
    size = 1;
  xlock ();
  void *result = malloc_internal (size);
  for (int i = 0; i < malloc_chunk_count; ++i)
    if (chunks[i].start == NULL)
      {
        chunks[i].start = result;
        chunks[i].size = size;
        xunlock ();
        return result;
      }
  xunlock ();
  printf ("error: no place to store chunk pointer\n");
  _exit (1);
}

void *
calloc (size_t a, size_t b)
{
  if (b != 0 && a > SIZE_MAX / b)
    return NULL;
  /* malloc uses mmap, which provides zeroed memory.  */
  return malloc (a * b);
}

static void
xunmap (void *ptr, size_t size)
{
  int ret = munmap (ptr, size);
  if (ret < 0)
    {
      printf ("error: munmap (%p, %zu) failed: %m\n", ptr, size);
      _exit (1);
    }
}

void
free (void *ptr)
{
  if (ptr == NULL)
    return;

  xlock ();
  for (int i = 0; i < malloc_chunk_count; ++i)
    if (chunks[i].start == ptr)
      {
        xunmap (ptr, chunks[i].size);
        chunks[i] = (struct malloc_chunk) {};
        xunlock ();
        return;
      }
  xunlock ();
  printf ("error: tried to free non-allocated pointer %p\n", ptr);
  _exit (1);
}

void *
realloc (void *old, size_t size)
{
  if (old != NULL)
    {
      xlock ();
      for (int i = 0; i < malloc_chunk_count; ++i)
        if (chunks[i].start == old)
          {
            size_t old_size = chunks[i].size;
            void *result;
            if (old_size < size)
              {
                result = malloc_internal (size);
                /* Reuse the slot for the new allocation.  */
                memcpy (result, old, old_size);
                xunmap (old, old_size);
                chunks[i].start = result;
                chunks[i].size = size;
              }
            else
              /* Old size is not smaller, so reuse the old
                 allocation.  */
              result = old;
            xunlock ();
            return result;
          }
      xunlock ();
      printf ("error: tried to realloc non-allocated pointer %p\n", old);
      _exit (1);
    }
  else
    return malloc (size);
}