1 /* Test and measure string and memory functions.
2 Copyright (C) 1999-2014 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>
29 extern impl_t __start_impls
[], __stop_impls
[];
31 #define IMPL(name, test) \
33 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
34 = { __STRING (name), (void (*) (void))name, test };
42 #undef __USE_STRING_INLINES
48 #include <sys/param.h>
54 #include <ifunc-impl-list.h>
59 # define TEST_FUNCTION test_main ()
60 # define TIMEOUT (4 * 60)
61 # define OPT_ITERATIONS 10000
62 # define OPT_RANDOM 10001
63 # define OPT_SEED 10002
65 unsigned char *buf1
, *buf2
;
71 size_t iterations
= 100000;
72 # define ITERATIONS_OPTIONS \
73 { "iterations", required_argument, NULL, OPT_ITERATIONS },
74 # define ITERATIONS_PROCESS \
75 case OPT_ITERATIONS: \
76 iterations = strtoul (optarg, NULL, 0); \
78 # define ITERATIONS iterations
80 # define ITERATIONS_OPTIONS
81 # define ITERATIONS_PROCESS
84 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
85 { "random", no_argument, NULL, OPT_RANDOM }, \
86 { "seed", required_argument, NULL, OPT_SEED },
87 # define CMDLINE_PROCESS ITERATIONS_PROCESS \
90 int fdr = open ("/dev/urandom", O_RDONLY); \
92 if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed)) \
101 seed = strtoul (optarg, NULL, 0); \
105 #define CALL(impl, ...) \
106 (* (proto_t) (impl)->fn) (__VA_ARGS__)
108 #if defined TEST_IFUNC && defined TEST_NAME
109 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
110 static struct libc_ifunc_impl func_list
[32];
111 static int func_count
;
112 static int impl_count
= -1;
113 static impl_t
*impl_array
;
115 # define FOR_EACH_IMPL(impl, notall) \
118 if (impl_count == -1) \
121 if (func_count != 0) \
124 impl_t *skip = NULL, *a; \
125 for (impl = __start_impls; impl < __stop_impls; ++impl) \
126 if (strcmp (impl->name, TEST_NAME) == 0) \
130 a = impl_array = malloc ((impl_count + func_count) * \
132 for (impl = __start_impls; impl < __stop_impls; ++impl) \
135 for (f = 0; f < func_count; f++) \
136 if (func_list[f].usable) \
138 a->name = func_list[f].name; \
139 a->fn = func_list[f].fn; \
143 impl_count = a - impl_array; \
147 impl_count = __stop_impls - __start_impls; \
148 impl_array = __start_impls; \
152 for (count = 0; count < impl_count; ++count, ++impl) \
153 if (!notall || impl->test)
155 # define FOR_EACH_IMPL(impl, notall) \
156 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
157 if (!notall || impl->test)
167 #if defined TEST_IFUNC && defined TEST_NAME
168 func_count
= __libc_ifunc_impl_list (TEST_NAME
, func_list
,
170 / sizeof func_list
[0]));
173 page_size
= 2 * getpagesize ();
175 if (page_size
< MIN_PAGE_SIZE
)
176 page_size
= MIN_PAGE_SIZE
;
178 buf1
= mmap (0, (BUF1PAGES
+ 1) * page_size
, PROT_READ
| PROT_WRITE
,
179 MAP_PRIVATE
| MAP_ANON
, -1, 0);
180 if (buf1
== MAP_FAILED
)
181 error (EXIT_FAILURE
, errno
, "mmap failed");
182 if (mprotect (buf1
+ BUF1PAGES
* page_size
, page_size
, PROT_NONE
))
183 error (EXIT_FAILURE
, errno
, "mprotect failed");
184 buf2
= mmap (0, 2 * page_size
, PROT_READ
| PROT_WRITE
,
185 MAP_PRIVATE
| MAP_ANON
, -1, 0);
186 if (buf2
== MAP_FAILED
)
187 error (EXIT_FAILURE
, errno
, "mmap failed");
188 if (mprotect (buf2
+ page_size
, page_size
, PROT_NONE
))
189 error (EXIT_FAILURE
, errno
, "mprotect failed");
192 printf ("Setting seed to 0x%x\n", seed
);
196 memset (buf1
, 0xa5, BUF1PAGES
* page_size
);
197 memset (buf2
, 0x5a, page_size
);