1 /* Test and measure string and memory functions.
2 Copyright (C) 1999-2019 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>
28 extern impl_t __start_impls
[], __stop_impls
[];
30 #define IMPL(name, test) \
32 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
33 = { __STRING (name), (void (*) (void))name, test };
41 #undef __USE_STRING_INLINES
43 /* We are compiled under _ISOMAC, so libc-symbols.h does not do this
46 #ifdef HAVE_CC_INHIBIT_LOOP_TO_LIBCALL
47 # define inhibit_loop_to_libcall \
48 __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
50 # define inhibit_loop_to_libcall
59 #include <sys/param.h>
65 #include <ifunc-impl-list.h>
70 # define TEST_FUNCTION test_main
71 # define TIMEOUT (4 * 60)
72 # define OPT_ITERATIONS 10000
73 # define OPT_RANDOM 10001
74 # define OPT_SEED 10002
76 unsigned char *buf1
, *buf2
;
82 size_t iterations
= 100000;
83 # define ITERATIONS_OPTIONS \
84 { "iterations", required_argument, NULL, OPT_ITERATIONS },
85 # define ITERATIONS_PROCESS \
86 case OPT_ITERATIONS: \
87 iterations = strtoul (optarg, NULL, 0); \
89 # define ITERATIONS iterations
91 # define ITERATIONS_OPTIONS
92 # define ITERATIONS_PROCESS
95 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
96 { "random", no_argument, NULL, OPT_RANDOM }, \
97 { "seed", required_argument, NULL, OPT_SEED },
99 static void __attribute__ ((used
))
100 cmdline_process_function (int c
)
107 int fdr
= open ("/dev/urandom", O_RDONLY
);
108 if (fdr
< 0 || read (fdr
, &seed
, sizeof (seed
)) != sizeof (seed
))
117 seed
= strtoul (optarg
, NULL
, 0);
122 # define CMDLINE_PROCESS cmdline_process_function
124 #define CALL(impl, ...) \
125 (* (proto_t) (impl)->fn) (__VA_ARGS__)
128 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
129 static struct libc_ifunc_impl func_list
[32];
130 static int func_count
;
131 static int impl_count
= -1;
132 static impl_t
*impl_array
;
134 # define FOR_EACH_IMPL(impl, notall) \
137 if (impl_count == -1) \
140 if (func_count != 0) \
143 impl_t *skip = NULL, *a; \
144 for (impl = __start_impls; impl < __stop_impls; ++impl) \
145 if (strcmp (impl->name, TEST_NAME) == 0) \
149 a = impl_array = malloc ((impl_count + func_count) * \
151 for (impl = __start_impls; impl < __stop_impls; ++impl) \
154 for (f = 0; f < func_count; f++) \
155 if (func_list[f].usable) \
157 a->name = func_list[f].name; \
158 a->fn = func_list[f].fn; \
162 impl_count = a - impl_array; \
166 impl_count = __stop_impls - __start_impls; \
167 impl_array = __start_impls; \
171 for (count = 0; count < impl_count; ++count, ++impl) \
172 if (!notall || impl->test)
174 # define FOR_EACH_IMPL(impl, notall) \
175 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
176 if (!notall || impl->test)
187 func_count
= __libc_ifunc_impl_list (TEST_NAME
, func_list
,
189 / sizeof func_list
[0]));
192 page_size
= 2 * getpagesize ();
194 if (page_size
< MIN_PAGE_SIZE
)
195 page_size
= MIN_PAGE_SIZE
;
197 buf1
= mmap (0, (BUF1PAGES
+ 1) * page_size
, PROT_READ
| PROT_WRITE
,
198 MAP_PRIVATE
| MAP_ANON
, -1, 0);
199 if (buf1
== MAP_FAILED
)
200 error (EXIT_FAILURE
, errno
, "mmap failed");
201 if (mprotect (buf1
+ BUF1PAGES
* page_size
, page_size
, PROT_NONE
))
202 error (EXIT_FAILURE
, errno
, "mprotect failed");
203 buf2
= mmap (0, 2 * page_size
, PROT_READ
| PROT_WRITE
,
204 MAP_PRIVATE
| MAP_ANON
, -1, 0);
205 if (buf2
== MAP_FAILED
)
206 error (EXIT_FAILURE
, errno
, "mmap failed");
207 if (mprotect (buf2
+ page_size
, page_size
, PROT_NONE
))
208 error (EXIT_FAILURE
, errno
, "mprotect failed");
211 printf ("Setting seed to 0x%x\n", seed
);
215 memset (buf1
, 0xa5, BUF1PAGES
* page_size
);
216 memset (buf2
, 0x5a, page_size
);