S390: Do not call memcpy, memcmp, memset within libc.so via ifunc-plt.
[glibc.git] / string / test-string.h
blob728a7c226a87de62c6300f969f9fa1ad69fe7046
1 /* Test and measure string and memory functions.
2 Copyright (C) 1999-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <sys/cdefs.h>
22 typedef struct
24 const char *name;
25 void (*fn) (void);
26 long test;
27 } impl_t;
28 extern impl_t __start_impls[], __stop_impls[];
30 #define IMPL(name, test) \
31 impl_t tst_ ## name \
32 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
33 = { __STRING (name), (void (*) (void))name, test };
35 #ifdef TEST_MAIN
37 #ifndef _GNU_SOURCE
38 #define _GNU_SOURCE
39 #endif
41 #undef __USE_STRING_INLINES
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sys/mman.h>
47 #include <sys/param.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <error.h>
51 #include <errno.h>
52 #include <time.h>
53 #include <ifunc-impl-list.h>
54 #define GL(x) _##x
55 #define GLRO(x) _##x
58 # define TEST_FUNCTION test_main ()
59 # define TIMEOUT (4 * 60)
60 # define OPT_ITERATIONS 10000
61 # define OPT_RANDOM 10001
62 # define OPT_SEED 10002
64 unsigned char *buf1, *buf2;
65 int ret, do_srandom;
66 unsigned int seed;
67 size_t page_size;
69 # ifndef ITERATIONS
70 size_t iterations = 100000;
71 # define ITERATIONS_OPTIONS \
72 { "iterations", required_argument, NULL, OPT_ITERATIONS },
73 # define ITERATIONS_PROCESS \
74 case OPT_ITERATIONS: \
75 iterations = strtoul (optarg, NULL, 0); \
76 break;
77 # define ITERATIONS iterations
78 # else
79 # define ITERATIONS_OPTIONS
80 # define ITERATIONS_PROCESS
81 # endif
83 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
84 { "random", no_argument, NULL, OPT_RANDOM }, \
85 { "seed", required_argument, NULL, OPT_SEED },
86 # define CMDLINE_PROCESS ITERATIONS_PROCESS \
87 case OPT_RANDOM: \
88 { \
89 int fdr = open ("/dev/urandom", O_RDONLY); \
91 if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed)) \
92 seed = time (NULL); \
93 if (fdr >= 0) \
94 close (fdr); \
95 do_srandom = 1; \
96 break; \
97 } \
99 case OPT_SEED: \
100 seed = strtoul (optarg, NULL, 0); \
101 do_srandom = 1; \
102 break;
104 #define CALL(impl, ...) \
105 (* (proto_t) (impl)->fn) (__VA_ARGS__)
107 #ifdef TEST_NAME
108 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
109 static struct libc_ifunc_impl func_list[32];
110 static int func_count;
111 static int impl_count = -1;
112 static impl_t *impl_array;
114 # define FOR_EACH_IMPL(impl, notall) \
115 impl_t *impl; \
116 int count; \
117 if (impl_count == -1) \
119 impl_count = 0; \
120 if (func_count != 0) \
122 int f; \
123 impl_t *skip = NULL, *a; \
124 for (impl = __start_impls; impl < __stop_impls; ++impl) \
125 if (strcmp (impl->name, TEST_NAME) == 0) \
126 skip = impl; \
127 else \
128 impl_count++; \
129 a = impl_array = malloc ((impl_count + func_count) * \
130 sizeof (impl_t)); \
131 for (impl = __start_impls; impl < __stop_impls; ++impl) \
132 if (impl != skip) \
133 *a++ = *impl; \
134 for (f = 0; f < func_count; f++) \
135 if (func_list[f].usable) \
137 a->name = func_list[f].name; \
138 a->fn = func_list[f].fn; \
139 a->test = 1; \
140 a++; \
142 impl_count = a - impl_array; \
144 else \
146 impl_count = __stop_impls - __start_impls; \
147 impl_array = __start_impls; \
150 impl = impl_array; \
151 for (count = 0; count < impl_count; ++count, ++impl) \
152 if (!notall || impl->test)
153 #else
154 # define FOR_EACH_IMPL(impl, notall) \
155 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
156 if (!notall || impl->test)
157 #endif
159 #ifndef BUF1PAGES
160 # define BUF1PAGES 1
161 #endif
163 static void
164 test_init (void)
166 #ifdef TEST_NAME
167 func_count = __libc_ifunc_impl_list (TEST_NAME, func_list,
168 (sizeof func_list
169 / sizeof func_list[0]));
170 #endif
172 page_size = 2 * getpagesize ();
173 #ifdef MIN_PAGE_SIZE
174 if (page_size < MIN_PAGE_SIZE)
175 page_size = MIN_PAGE_SIZE;
176 #endif
177 buf1 = mmap (0, (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE,
178 MAP_PRIVATE | MAP_ANON, -1, 0);
179 if (buf1 == MAP_FAILED)
180 error (EXIT_FAILURE, errno, "mmap failed");
181 if (mprotect (buf1 + BUF1PAGES * page_size, page_size, PROT_NONE))
182 error (EXIT_FAILURE, errno, "mprotect failed");
183 buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
184 MAP_PRIVATE | MAP_ANON, -1, 0);
185 if (buf2 == MAP_FAILED)
186 error (EXIT_FAILURE, errno, "mmap failed");
187 if (mprotect (buf2 + page_size, page_size, PROT_NONE))
188 error (EXIT_FAILURE, errno, "mprotect failed");
189 if (do_srandom)
191 printf ("Setting seed to 0x%x\n", seed);
192 srandom (seed);
195 memset (buf1, 0xa5, BUF1PAGES * page_size);
196 memset (buf2, 0x5a, page_size);
199 #endif