Fix warning from latest GCC in tst-printf.c
[glibc.git] / malloc / tst-interpose-aux.c
blob77866b2e5d457fec1d89e31613a08467a6745083
1 /* Minimal malloc implementation for interposition tests.
2 Copyright (C) 2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <http://www.gnu.org/licenses/>. */
19 #include "tst-interpose-aux.h"
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <sys/uio.h>
29 #include <unistd.h>
31 #if INTERPOSE_THREADS
32 #include <pthread.h>
33 #endif
35 /* Print the error message and terminate the process with status 1. */
36 __attribute__ ((noreturn))
37 __attribute__ ((format (printf, 1, 2)))
38 static void *
39 fail (const char *format, ...)
41 /* This assumes that vsnprintf will not call malloc. It does not do
42 so for the format strings we use. */
43 char message[4096];
44 va_list ap;
45 va_start (ap, format);
46 vsnprintf (message, sizeof (message), format, ap);
47 va_end (ap);
49 enum { count = 3 };
50 struct iovec iov[count];
52 iov[0].iov_base = (char *) "error: ";
53 iov[1].iov_base = (char *) message;
54 iov[2].iov_base = (char *) "\n";
56 for (int i = 0; i < count; ++i)
57 iov[i].iov_len = strlen (iov[i].iov_base);
59 int unused __attribute__ ((unused));
60 unused = writev (STDOUT_FILENO, iov, count);
61 _exit (1);
64 #if INTERPOSE_THREADS
65 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
66 #endif
68 static void
69 lock (void)
71 #if INTERPOSE_THREADS
72 int ret = pthread_mutex_lock (&mutex);
73 if (ret != 0)
75 errno = ret;
76 fail ("pthread_mutex_lock: %m");
78 #endif
81 static void
82 unlock (void)
84 #if INTERPOSE_THREADS
85 int ret = pthread_mutex_unlock (&mutex);
86 if (ret != 0)
88 errno = ret;
89 fail ("pthread_mutex_unlock: %m");
91 #endif
94 struct __attribute__ ((aligned (__alignof__ (max_align_t)))) allocation_header
96 size_t allocation_index;
97 size_t allocation_size;
100 /* Array of known allocations, to track invalid frees. */
101 enum { max_allocations = 65536 };
102 static struct allocation_header *allocations[max_allocations];
103 static size_t allocation_index;
104 static size_t deallocation_count;
106 /* Sanity check for successful malloc interposition. */
107 __attribute__ ((destructor))
108 static void
109 check_for_allocations (void)
111 if (allocation_index == 0)
113 /* Make sure that malloc is called at least once from libc. */
114 void *volatile ptr = strdup ("ptr");
115 free (ptr);
116 /* Compiler barrier. The strdup function calls malloc, which
117 updates allocation_index, but strdup is marked __THROW, so
118 the compiler could optimize away the reload. */
119 __asm__ volatile ("" ::: "memory");
120 /* If the allocation count is still zero, it means we did not
121 interpose malloc successfully. */
122 if (allocation_index == 0)
123 fail ("malloc does not seem to have been interposed");
127 static struct allocation_header *get_header (const char *op, void *ptr)
129 struct allocation_header *header = ((struct allocation_header *) ptr) - 1;
130 if (header->allocation_index >= allocation_index)
131 fail ("%s: %p: invalid allocation index: %zu (not less than %zu)",
132 op, ptr, header->allocation_index, allocation_index);
133 if (allocations[header->allocation_index] != header)
134 fail ("%s: %p: allocation pointer does not point to header, but %p",
135 op, ptr, allocations[header->allocation_index]);
136 return header;
139 /* Internal helper functions. Those must be called while the lock is
140 acquired. */
142 static void *
143 malloc_internal (size_t size)
145 if (allocation_index == max_allocations)
147 errno = ENOMEM;
148 return NULL;
150 size_t allocation_size = size + sizeof (struct allocation_header);
151 if (allocation_size < size)
153 errno = ENOMEM;
154 return NULL;
157 size_t index = allocation_index++;
158 void *result = mmap (NULL, allocation_size, PROT_READ | PROT_WRITE,
159 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
160 if (result == MAP_FAILED)
161 return NULL;
162 allocations[index] = result;
163 *allocations[index] = (struct allocation_header)
165 .allocation_index = index,
166 .allocation_size = allocation_size
168 return allocations[index] + 1;
171 static void
172 free_internal (const char *op, struct allocation_header *header)
174 size_t index = header->allocation_index;
175 int result = mprotect (header, header->allocation_size, PROT_NONE);
176 if (result != 0)
177 fail ("%s: mprotect (%p, %zu): %m", op, header, header->allocation_size);
178 /* Catch double-free issues. */
179 allocations[index] = NULL;
180 ++deallocation_count;
183 static void *
184 realloc_internal (void *ptr, size_t new_size)
186 struct allocation_header *header = get_header ("realloc", ptr);
187 size_t old_size = header->allocation_size - sizeof (struct allocation_header);
188 if (old_size >= new_size)
189 return ptr;
191 void *newptr = malloc_internal (new_size);
192 if (newptr == NULL)
193 return NULL;
194 memcpy (newptr, ptr, old_size);
195 free_internal ("realloc", header);
196 return newptr;
199 /* Public interfaces. These functions must perform locking. */
201 size_t
202 malloc_allocation_count (void)
204 lock ();
205 size_t count = allocation_index;
206 unlock ();
207 return count;
210 size_t
211 malloc_deallocation_count (void)
213 lock ();
214 size_t count = deallocation_count;
215 unlock ();
216 return count;
218 void *
219 malloc (size_t size)
221 lock ();
222 void *result = malloc_internal (size);
223 unlock ();
224 return result;
227 void
228 free (void *ptr)
230 if (ptr == NULL)
231 return;
232 lock ();
233 struct allocation_header *header = get_header ("free", ptr);
234 free_internal ("free", header);
235 unlock ();
238 void *
239 calloc (size_t a, size_t b)
241 if (b > 0 && a > SIZE_MAX / b)
243 errno = ENOMEM;
244 return NULL;
246 lock ();
247 /* malloc_internal uses mmap, so the memory is zeroed. */
248 void *result = malloc_internal (a * b);
249 unlock ();
250 return result;
253 void *
254 realloc (void *ptr, size_t n)
256 if (n ==0)
258 free (ptr);
259 return NULL;
261 else if (ptr == NULL)
262 return malloc (n);
263 else
265 lock ();
266 void *result = realloc_internal (ptr, n);
267 unlock ();
268 return result;