sunrpc: Test case for clnt_create "unix" buffer overflow (bug 22542)
[glibc.git] / benchtests / bench-strrchr.c
blobabdae60c51faca4ee36b382958a45497d2495be8
1 /* Measure STRCHR functions.
2 Copyright (C) 2013-2022 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
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #define TEST_MAIN
20 #ifdef WIDE
21 # define TEST_NAME "wcsrchr"
22 #else
23 # define TEST_NAME "strrchr"
24 #endif
25 #include "bench-string.h"
27 #define BIG_CHAR MAX_CHAR
29 #ifdef WIDE
30 # define SIMPLE_STRRCHR simple_wcsrchr
31 # define SMALL_CHAR 1273
32 #else
33 # define SIMPLE_STRRCHR simple_strrchr
34 # define SMALL_CHAR 127
35 #endif
37 typedef CHAR *(*proto_t) (const CHAR *, int);
38 CHAR *SIMPLE_STRRCHR (const CHAR *, int);
40 IMPL (SIMPLE_STRRCHR, 0)
41 IMPL (STRRCHR, 1)
43 CHAR *
44 SIMPLE_STRRCHR (const CHAR *s, int c)
46 const CHAR *ret = NULL;
48 for (; *s != '\0'; ++s)
49 if (*s == (CHAR) c)
50 ret = s;
52 return (CHAR *) (c == '\0' ? s : ret);
55 static void
56 do_one_test (impl_t *impl, const CHAR *s, int c, CHAR *exp_res)
58 CHAR *res = CALL (impl, s, c);
59 size_t i, iters = INNER_LOOP_ITERS8;
60 timing_t start, stop, cur;
62 if (res != exp_res)
64 error (0, 0, "Wrong result in function %s %p %p", impl->name,
65 res, exp_res);
66 ret = 1;
67 return;
70 TIMING_NOW (start);
71 for (i = 0; i < iters; ++i)
73 CALL (impl, s, c);
75 TIMING_NOW (stop);
77 TIMING_DIFF (cur, start, stop);
79 TIMING_PRINT_MEAN ((double) cur, (double) iters);
82 static void
83 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
84 /* For wcsrchr: align here means align not in bytes,
85 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
86 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
88 size_t i;
89 CHAR *result;
90 CHAR *buf = (CHAR *) buf1;
92 align &= 7;
93 if ((align + len) * sizeof (CHAR) >= page_size)
94 return;
96 for (i = 0; i < len; ++i)
98 buf[align + i] = (random () * random ()) & max_char;
99 if (!buf[align + i])
100 buf[align + i] = (random () * random ()) & max_char;
101 if (!buf[align + i])
102 buf[align + i] = 1;
103 if ((i > pos || pos >= len) && buf[align + i] == seek_char)
104 buf[align + i] = seek_char + 10 + (random () & 15);
106 buf[align + len] = 0;
108 if (pos < len)
110 buf[align + pos] = seek_char;
111 result = (CHAR *) (buf + align + pos);
113 else if (seek_char == 0)
114 result = (CHAR *) (buf + align + len);
115 else
116 result = NULL;
118 printf ("Length %4zd, alignment in bytes %2zd:", len, align * sizeof (CHAR));
120 FOR_EACH_IMPL (impl, 0)
121 do_one_test (impl, (CHAR *) (buf + align), seek_char, result);
123 putchar ('\n');
127 test_main (void)
129 size_t i;
131 test_init ();
133 printf ("%20s", "");
134 FOR_EACH_IMPL (impl, 0)
135 printf ("\t%s", impl->name);
136 putchar ('\n');
138 for (i = 1; i < 8; ++i)
140 do_test (0, 16 << i, 2048, 23, SMALL_CHAR);
141 do_test (i, 16 << i, 2048, 23, SMALL_CHAR);
144 for (i = 1; i < 8; ++i)
146 do_test (i, 64, 256, 23, SMALL_CHAR);
147 do_test (i, 64, 256, 23, BIG_CHAR);
150 for (i = 0; i < 32; ++i)
152 do_test (0, i, i + 1, 23, SMALL_CHAR);
153 do_test (0, i, i + 1, 23, BIG_CHAR);
156 for (i = 1; i < 8; ++i)
158 do_test (0, 16 << i, 2048, 0, SMALL_CHAR);
159 do_test (i, 16 << i, 2048, 0, SMALL_CHAR);
162 for (i = 1; i < 8; ++i)
164 do_test (i, 64, 256, 0, SMALL_CHAR);
165 do_test (i, 64, 256, 0, BIG_CHAR);
168 for (i = 0; i < 32; ++i)
170 do_test (0, i, i + 1, 0, SMALL_CHAR);
171 do_test (0, i, i + 1, 0, BIG_CHAR);
174 return ret;
177 #include <support/test-driver.c>