PR 78534 Regression on 32-bit targets
[official-gcc.git] / libbacktrace / ztest.c
blobaab358d586323f03b31a3ff61984f3718ffe2c7e
1 /* ztest.c -- Test for libbacktrace inflate code.
2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
33 #include "config.h"
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
43 #ifdef HAVE_ZLIB
44 #include <zlib.h>
45 #endif
47 #include "backtrace.h"
48 #include "backtrace-supported.h"
50 #include "internal.h"
51 #include "testlib.h"
53 #ifndef HAVE_CLOCK_GETTIME
55 typedef int xclockid_t;
57 static int
58 xclock_gettime (xclockid_t id ATTRIBUTE_UNUSED,
59 struct timespec *ts ATTRIBUTE_UNUSED)
61 errno = EINVAL;
62 return -1;
65 #define clockid_t xclockid_t
66 #define clock_gettime xclock_gettime
67 #undef CLOCK_REALTIME
68 #define CLOCK_REALTIME 0
70 #endif /* !defined(HAVE_CLOCK_GETTIME) */
72 #ifdef CLOCK_PROCESS_CPUTIME_ID
73 #define ZLIB_CLOCK_GETTIME_ARG CLOCK_PROCESS_CPUTIME_ID
74 #else
75 #define ZLIB_CLOCK_GETTIME_ARG CLOCK_REALTIME
76 #endif
78 /* Some tests for the local zlib inflation code. */
80 struct zlib_test
82 const char *name;
83 const char *uncompressed;
84 const char *compressed;
85 size_t compressed_len;
88 /* Error callback. */
90 static void
91 error_callback_compress (void *vdata, const char *msg, int errnum)
93 fprintf (stderr, "%s", msg);
94 if (errnum > 0)
95 fprintf (stderr, ": %s", strerror (errnum));
96 fprintf (stderr, "\n");
97 exit (EXIT_FAILURE);
100 static const struct zlib_test tests[] =
103 "empty",
105 "\x78\x9c\x03\x00\x00\x00\x00\x01",
109 "hello",
110 "hello, world\n",
111 ("\x78\x9c\xca\x48\xcd\xc9\xc9\xd7\x51\x28\xcf"
112 "\x2f\xca\x49\xe1\x02\x04\x00\x00\xff\xff\x21\xe7\x04\x93"),
116 "goodbye",
117 "goodbye, world",
118 ("\x78\x9c\x4b\xcf\xcf\x4f\x49\xaa"
119 "\x4c\xd5\x51\x28\xcf\x2f\xca\x49"
120 "\x01\x00\x28\xa5\x05\x5e"),
125 /* Test the hand coded samples. */
127 static void
128 test_samples (struct backtrace_state *state)
130 size_t i;
132 for (i = 0; i < sizeof tests / sizeof tests[0]; ++i)
134 char *p;
135 size_t v;
136 size_t j;
137 unsigned char *uncompressed;
138 size_t uncompressed_len;
140 p = malloc (12 + tests[i].compressed_len);
141 memcpy (p, "ZLIB", 4);
142 v = strlen (tests[i].uncompressed);
143 for (j = 0; j < 8; ++j)
144 p[j + 4] = (v >> ((7 - j) * 8)) & 0xff;
145 memcpy (p + 12, tests[i].compressed, tests[i].compressed_len);
146 uncompressed = NULL;
147 uncompressed_len = 0;
148 if (!backtrace_uncompress_zdebug (state, (unsigned char *) p,
149 tests[i].compressed_len + 12,
150 error_callback_compress, NULL,
151 &uncompressed, &uncompressed_len))
153 fprintf (stderr, "test %s: uncompress failed\n", tests[i].name);
154 ++failures;
156 else
158 if (uncompressed_len != v)
160 fprintf (stderr,
161 "test %s: got uncompressed length %zu, want %zu\n",
162 tests[i].name, uncompressed_len, v);
163 ++failures;
165 else if (memcmp (tests[i].uncompressed, uncompressed, v) != 0)
167 size_t j;
169 fprintf (stderr, "test %s: uncompressed data mismatch\n",
170 tests[i].name);
171 for (j = 0; j < v; ++j)
172 if (tests[i].uncompressed[j] != uncompressed[j])
173 fprintf (stderr, " %zu: got %#x want %#x\n", j,
174 uncompressed[j], tests[i].uncompressed[j]);
175 ++failures;
177 else
178 printf ("PASS: inflate %s\n", tests[i].name);
180 backtrace_free (state, uncompressed, uncompressed_len,
181 error_callback_compress, NULL);
186 #ifdef HAVE_ZLIB
188 /* Given a set of TRIALS timings, discard the lowest and highest
189 values and return the mean average of the rest. */
191 static size_t
192 average_time (const size_t *times, size_t trials)
194 size_t imax;
195 size_t max;
196 size_t imin;
197 size_t min;
198 size_t i;
199 size_t sum;
201 imin = 0;
202 imax = 0;
203 min = times[0];
204 max = times[0];
205 for (i = 1; i < trials; ++i)
207 if (times[i] < min)
209 imin = i;
210 min = times[i];
212 if (times[i] > max)
214 imax = i;
215 max = times[i];
219 sum = 0;
220 for (i = 0; i < trials; ++i)
222 if (i != imax && i != imin)
223 sum += times[i];
225 return sum / (trials - 2);
228 #endif
230 /* Test a larger text, if available. */
232 static void
233 test_large (struct backtrace_state *state)
235 #ifdef HAVE_ZLIB
236 unsigned char *orig_buf;
237 size_t orig_bufsize;
238 size_t i;
239 char *compressed_buf;
240 size_t compressed_bufsize;
241 unsigned long compress_sizearg;
242 unsigned char *uncompressed_buf;
243 size_t uncompressed_bufsize;
244 int r;
245 clockid_t cid;
246 struct timespec ts1;
247 struct timespec ts2;
248 size_t ctime;
249 size_t ztime;
250 const size_t trials = 16;
251 size_t ctimes[16];
252 size_t ztimes[16];
253 static const char * const names[] = {
254 "Mark.Twain-Tom.Sawyer.txt",
255 "../libgo/go/compress/testdata/Mark.Twain-Tom.Sawyer.txt"
258 orig_buf = NULL;
259 orig_bufsize = 0;
260 uncompressed_buf = NULL;
261 compressed_buf = NULL;
263 for (i = 0; i < sizeof names / sizeof names[0]; ++i)
265 size_t len;
266 char *namebuf;
267 FILE *e;
268 struct stat st;
269 char *rbuf;
270 size_t got;
272 len = strlen (SRCDIR) + strlen (names[i]) + 2;
273 namebuf = malloc (len);
274 if (namebuf == NULL)
276 perror ("malloc");
277 goto fail;
279 snprintf (namebuf, len, "%s/%s", SRCDIR, names[i]);
280 e = fopen (namebuf, "r");
281 free (namebuf);
282 if (e == NULL)
283 continue;
284 if (fstat (fileno (e), &st) < 0)
286 perror ("fstat");
287 fclose (e);
288 continue;
290 rbuf = malloc (st.st_size);
291 if (rbuf == NULL)
293 perror ("malloc");
294 goto fail;
296 got = fread (rbuf, 1, st.st_size, e);
297 fclose (e);
298 if (got > 0)
300 orig_buf = rbuf;
301 orig_bufsize = got;
302 break;
304 free (rbuf);
307 if (orig_buf == NULL)
309 /* We couldn't find an input file. */
310 printf ("UNSUPPORTED: inflate large\n");
311 return;
314 compressed_bufsize = compressBound (orig_bufsize) + 12;
315 compressed_buf = malloc (compressed_bufsize);
316 if (compressed_buf == NULL)
318 perror ("malloc");
319 goto fail;
322 compress_sizearg = compressed_bufsize - 12;
323 r = compress (compressed_buf + 12, &compress_sizearg,
324 orig_buf, orig_bufsize);
325 if (r != Z_OK)
327 fprintf (stderr, "zlib compress failed: %d\n", r);
328 goto fail;
331 compressed_bufsize = compress_sizearg + 12;
333 /* Prepare the header that our library expects. */
334 memcpy (compressed_buf, "ZLIB", 4);
335 for (i = 0; i < 8; ++i)
336 compressed_buf[i + 4] = (orig_bufsize >> ((7 - i) * 8)) & 0xff;
338 uncompressed_buf = malloc (orig_bufsize);
339 if (uncompressed_buf == NULL)
341 perror ("malloc");
342 goto fail;
344 uncompressed_bufsize = orig_bufsize;
346 if (!backtrace_uncompress_zdebug (state, compressed_buf, compressed_bufsize,
347 error_callback_compress, NULL,
348 &uncompressed_buf, &uncompressed_bufsize))
350 fprintf (stderr, "inflate large: backtrace_uncompress_zdebug failed\n");
351 goto fail;
354 if (uncompressed_bufsize != orig_bufsize)
356 fprintf (stderr,
357 "inflate large: got uncompressed length %zu, want %zu\n",
358 uncompressed_bufsize, orig_bufsize);
359 goto fail;
362 if (memcmp (uncompressed_buf, orig_buf, uncompressed_bufsize) != 0)
364 fprintf (stderr, "inflate large: uncompressed data mismatch\n");
365 goto fail;
368 printf ("PASS: inflate large\n");
370 for (i = 0; i < trials; ++i)
372 unsigned long uncompress_sizearg;
374 cid = ZLIB_CLOCK_GETTIME_ARG;
375 if (clock_gettime (cid, &ts1) < 0)
377 if (errno == EINVAL)
378 return;
379 perror ("clock_gettime");
380 return;
383 if (!backtrace_uncompress_zdebug (state, compressed_buf,
384 compressed_bufsize,
385 error_callback_compress, NULL,
386 &uncompressed_buf,
387 &uncompressed_bufsize))
389 fprintf (stderr,
390 ("inflate large: "
391 "benchmark backtrace_uncompress_zdebug failed\n"));
392 return;
395 if (clock_gettime (cid, &ts2) < 0)
397 perror ("clock_gettime");
398 return;
401 ctime = (ts2.tv_sec - ts1.tv_sec) * 1000000000;
402 ctime += ts2.tv_nsec - ts1.tv_nsec;
403 ctimes[i] = ctime;
405 if (clock_gettime (cid, &ts1) < 0)
407 perror("clock_gettime");
408 return;
411 uncompress_sizearg = uncompressed_bufsize;
412 r = uncompress (uncompressed_buf, &uncompress_sizearg,
413 compressed_buf + 12, compressed_bufsize - 12);
415 if (clock_gettime (cid, &ts2) < 0)
417 perror ("clock_gettime");
418 return;
421 if (r != Z_OK)
423 fprintf (stderr,
424 "inflate large: benchmark zlib uncompress failed: %d\n",
426 return;
429 ztime = (ts2.tv_sec - ts1.tv_sec) * 1000000000;
430 ztime += ts2.tv_nsec - ts1.tv_nsec;
431 ztimes[i] = ztime;
434 /* Toss the highest and lowest times and average the rest. */
435 ctime = average_time (ctimes, trials);
436 ztime = average_time (ztimes, trials);
438 printf ("backtrace: %zu ns\n", ctime);
439 printf ("zlib : %zu ns\n", ztime);
440 printf ("ratio : %g\n", (double) ztime / (double) ctime);
442 return;
444 fail:
445 printf ("FAIL: inflate large\n");
446 ++failures;
448 if (orig_buf != NULL)
449 free (orig_buf);
450 if (compressed_buf != NULL)
451 free (compressed_buf);
452 if (uncompressed_buf != NULL)
453 free (uncompressed_buf);
455 #else /* !HAVE_ZLIB */
457 printf ("UNSUPPORTED: inflate large\n");
459 #endif /* !HAVE_ZLIB */
463 main (int argc ATTRIBUTE_UNUSED, char **argv)
465 struct backtrace_state *state;
467 state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
468 error_callback_create, NULL);
470 test_samples (state);
471 test_large (state);
473 exit (failures != 0 ? EXIT_FAILURE : EXIT_SUCCESS);