1/* Test and measure string and memory functions.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <sys/cdefs.h>
20#include <support/support.h>
21
22typedef struct
23{
24 const char *name;
25 void (*fn) (void);
26 long test;
27} impl_t;
28extern impl_t __start_impls[], __stop_impls[];
29
30#define IMPL(name, test) \
31 impl_t tst_ ## name \
32 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
33 = { __STRING (name), (void (*) (void))name, test };
34
35#ifdef TEST_MAIN
36
37#ifndef _GNU_SOURCE
38#define _GNU_SOURCE
39#endif
40
41#undef __USE_STRING_INLINES
42
43/* We are compiled under _ISOMAC, so libc-symbols.h does not do this
44 for us. */
45#include "config.h"
46#ifdef HAVE_CC_INHIBIT_LOOP_TO_LIBCALL
47# define inhibit_loop_to_libcall \
48 __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
49#else
50# define inhibit_loop_to_libcall
51#endif
52
53#include <getopt.h>
54#include <stdint.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <sys/mman.h>
59#include <sys/param.h>
60#include <unistd.h>
61#include <fcntl.h>
62#include <error.h>
63#include <errno.h>
64#include <time.h>
65#include <ifunc-impl-list.h>
66#define GL(x) _##x
67#define GLRO(x) _##x
68
69
70# define TEST_FUNCTION test_main
71# ifndef TIMEOUT
72# define TIMEOUT (4 * 60)
73# endif
74# define OPT_ITERATIONS 10000
75# define OPT_RANDOM 10001
76# define OPT_SEED 10002
77
78unsigned char *buf1, *buf2;
79int ret, do_srandom;
80unsigned int seed;
81size_t page_size;
82
83# ifndef ITERATIONS
84size_t iterations = 100000;
85# define ITERATIONS_OPTIONS \
86 { "iterations", required_argument, NULL, OPT_ITERATIONS },
87# define ITERATIONS_PROCESS \
88 case OPT_ITERATIONS: \
89 iterations = strtoul (optarg, NULL, 0); \
90 break;
91# define ITERATIONS iterations
92# else
93# define ITERATIONS_OPTIONS
94# define ITERATIONS_PROCESS
95# endif
96
97# define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
98 { "random", no_argument, NULL, OPT_RANDOM }, \
99 { "seed", required_argument, NULL, OPT_SEED },
100
101static void __attribute__ ((used))
102cmdline_process_function (int c)
103{
104 switch (c)
105 {
106 ITERATIONS_PROCESS
107 case OPT_RANDOM:
108 {
109 int fdr = open (file: "/dev/urandom", O_RDONLY);
110 if (fdr < 0 || read (fdr, &seed, sizeof (seed)) != sizeof (seed))
111 seed = time (NULL);
112 if (fdr >= 0)
113 close (fd: fdr);
114 do_srandom = 1;
115 break;
116 }
117
118 case OPT_SEED:
119 seed = strtoul (optarg, NULL, 0);
120 do_srandom = 1;
121 break;
122 }
123}
124# define CMDLINE_PROCESS cmdline_process_function
125
126#define CALL(impl, ...) \
127 (* (proto_t) (impl)->fn) (__VA_ARGS__)
128
129#ifdef TEST_NAME
130/* Increase size of FUNC_LIST if assert is triggered at run-time. */
131static struct libc_ifunc_impl func_list[32];
132static int func_count;
133static int impl_count = -1;
134static impl_t *impl_array;
135
136# define FOR_EACH_IMPL(impl, notall) \
137 impl_t *impl; \
138 int count; \
139 if (impl_count == -1) \
140 { \
141 impl_count = 0; \
142 if (func_count != 0) \
143 { \
144 int f; \
145 impl_t *skip = NULL, *a; \
146 for (impl = __start_impls; impl < __stop_impls; ++impl) \
147 if (strcmp (impl->name, TEST_NAME) == 0) \
148 skip = impl; \
149 else \
150 impl_count++; \
151 a = impl_array = xmalloc ((impl_count + func_count) * \
152 sizeof (impl_t)); \
153 for (impl = __start_impls; impl < __stop_impls; ++impl) \
154 if (impl != skip) \
155 *a++ = *impl; \
156 for (f = 0; f < func_count; f++) \
157 if (func_list[f].usable) \
158 { \
159 a->name = func_list[f].name; \
160 a->fn = func_list[f].fn; \
161 a->test = 1; \
162 a++; \
163 } \
164 impl_count = a - impl_array; \
165 } \
166 else \
167 { \
168 impl_count = __stop_impls - __start_impls; \
169 impl_array = __start_impls; \
170 } \
171 } \
172 impl = impl_array; \
173 for (count = 0; count < impl_count; ++count, ++impl) \
174 if (!notall || impl->test)
175#else
176# define FOR_EACH_IMPL(impl, notall) \
177 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
178 if (!notall || impl->test)
179#endif
180
181#ifndef BUF1PAGES
182# define BUF1PAGES 1
183#endif
184
185static void
186test_init (void)
187{
188#ifdef TEST_NAME
189 func_count = __libc_ifunc_impl_list (TEST_NAME, array: func_list,
190 max: (sizeof func_list
191 / sizeof func_list[0]));
192#endif
193
194 page_size = 2 * getpagesize ();
195#ifdef MIN_PAGE_SIZE
196 if (page_size < MIN_PAGE_SIZE)
197 page_size = MIN_PAGE_SIZE;
198#endif
199 buf1 = mmap (addr: 0, len: (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE,
200 MAP_PRIVATE | MAP_ANON, fd: -1, offset: 0);
201 if (buf1 == MAP_FAILED)
202 error (EXIT_FAILURE, errno, format: "mmap failed");
203 if (mprotect (addr: buf1 + BUF1PAGES * page_size, len: page_size, PROT_NONE))
204 error (EXIT_FAILURE, errno, format: "mprotect failed");
205 buf2 = mmap (addr: 0, len: 2 * page_size, PROT_READ | PROT_WRITE,
206 MAP_PRIVATE | MAP_ANON, fd: -1, offset: 0);
207 if (buf2 == MAP_FAILED)
208 error (EXIT_FAILURE, errno, format: "mmap failed");
209 if (mprotect (addr: buf2 + page_size, len: page_size, PROT_NONE))
210 error (EXIT_FAILURE, errno, format: "mprotect failed");
211 if (do_srandom)
212 {
213 printf (format: "Setting seed to 0x%x\n", seed);
214 srandom (seed: seed);
215 }
216
217 memset (buf1, 0xa5, BUF1PAGES * page_size);
218 memset (buf2, 0x5a, page_size);
219}
220
221#endif
222

source code of glibc/string/test-string.h