annotate libbacktrace/ztest.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* ztest.c -- Test for libbacktrace inflate code.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2017-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Written by Ian Lance Taylor, Google.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
6 modification, are permitted provided that the following conditions are
kono
parents:
diff changeset
7 met:
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 (1) Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
10 notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 (2) Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
13 notice, this list of conditions and the following disclaimer in
kono
parents:
diff changeset
14 the documentation and/or other materials provided with the
kono
parents:
diff changeset
15 distribution.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 (3) The name of the author may not be used to
kono
parents:
diff changeset
18 endorse or promote products derived from this software without
kono
parents:
diff changeset
19 specific prior written permission.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
kono
parents:
diff changeset
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
kono
parents:
diff changeset
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
kono
parents:
diff changeset
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
kono
parents:
diff changeset
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
kono
parents:
diff changeset
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
kono
parents:
diff changeset
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
kono
parents:
diff changeset
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
kono
parents:
diff changeset
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
kono
parents:
diff changeset
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
kono
parents:
diff changeset
31 POSSIBILITY OF SUCH DAMAGE. */
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 #include "config.h"
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 #include <errno.h>
kono
parents:
diff changeset
36 #include <stdio.h>
kono
parents:
diff changeset
37 #include <stdlib.h>
kono
parents:
diff changeset
38 #include <string.h>
kono
parents:
diff changeset
39 #include <time.h>
kono
parents:
diff changeset
40 #include <sys/types.h>
kono
parents:
diff changeset
41 #include <sys/stat.h>
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 #ifdef HAVE_ZLIB
kono
parents:
diff changeset
44 #include <zlib.h>
kono
parents:
diff changeset
45 #endif
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 #include "backtrace.h"
kono
parents:
diff changeset
48 #include "backtrace-supported.h"
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 #include "internal.h"
kono
parents:
diff changeset
51 #include "testlib.h"
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 #ifndef HAVE_CLOCK_GETTIME
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 typedef int xclockid_t;
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 static int
kono
parents:
diff changeset
58 xclock_gettime (xclockid_t id ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
59 struct timespec *ts ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
60 {
kono
parents:
diff changeset
61 errno = EINVAL;
kono
parents:
diff changeset
62 return -1;
kono
parents:
diff changeset
63 }
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 #define clockid_t xclockid_t
kono
parents:
diff changeset
66 #define clock_gettime xclock_gettime
kono
parents:
diff changeset
67 #undef CLOCK_REALTIME
kono
parents:
diff changeset
68 #define CLOCK_REALTIME 0
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 #endif /* !defined(HAVE_CLOCK_GETTIME) */
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 #ifdef CLOCK_PROCESS_CPUTIME_ID
kono
parents:
diff changeset
73 #define ZLIB_CLOCK_GETTIME_ARG CLOCK_PROCESS_CPUTIME_ID
kono
parents:
diff changeset
74 #else
kono
parents:
diff changeset
75 #define ZLIB_CLOCK_GETTIME_ARG CLOCK_REALTIME
kono
parents:
diff changeset
76 #endif
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 /* Some tests for the local zlib inflation code. */
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 struct zlib_test
kono
parents:
diff changeset
81 {
kono
parents:
diff changeset
82 const char *name;
kono
parents:
diff changeset
83 const char *uncompressed;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
84 size_t uncompressed_len;
111
kono
parents:
diff changeset
85 const char *compressed;
kono
parents:
diff changeset
86 size_t compressed_len;
kono
parents:
diff changeset
87 };
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 /* Error callback. */
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 static void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
92 error_callback_compress (void *vdata ATTRIBUTE_UNUSED, const char *msg,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
93 int errnum)
111
kono
parents:
diff changeset
94 {
kono
parents:
diff changeset
95 fprintf (stderr, "%s", msg);
kono
parents:
diff changeset
96 if (errnum > 0)
kono
parents:
diff changeset
97 fprintf (stderr, ": %s", strerror (errnum));
kono
parents:
diff changeset
98 fprintf (stderr, "\n");
kono
parents:
diff changeset
99 exit (EXIT_FAILURE);
kono
parents:
diff changeset
100 }
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 static const struct zlib_test tests[] =
kono
parents:
diff changeset
103 {
kono
parents:
diff changeset
104 {
kono
parents:
diff changeset
105 "empty",
kono
parents:
diff changeset
106 "",
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
107 0,
111
kono
parents:
diff changeset
108 "\x78\x9c\x03\x00\x00\x00\x00\x01",
kono
parents:
diff changeset
109 8,
kono
parents:
diff changeset
110 },
kono
parents:
diff changeset
111 {
kono
parents:
diff changeset
112 "hello",
kono
parents:
diff changeset
113 "hello, world\n",
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
114 0,
111
kono
parents:
diff changeset
115 ("\x78\x9c\xca\x48\xcd\xc9\xc9\xd7\x51\x28\xcf"
kono
parents:
diff changeset
116 "\x2f\xca\x49\xe1\x02\x04\x00\x00\xff\xff\x21\xe7\x04\x93"),
kono
parents:
diff changeset
117 25,
kono
parents:
diff changeset
118 },
kono
parents:
diff changeset
119 {
kono
parents:
diff changeset
120 "goodbye",
kono
parents:
diff changeset
121 "goodbye, world",
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
122 0,
111
kono
parents:
diff changeset
123 ("\x78\x9c\x4b\xcf\xcf\x4f\x49\xaa"
kono
parents:
diff changeset
124 "\x4c\xd5\x51\x28\xcf\x2f\xca\x49"
kono
parents:
diff changeset
125 "\x01\x00\x28\xa5\x05\x5e"),
kono
parents:
diff changeset
126 22,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
127 },
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
128 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
129 "ranges",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
130 ("\xcc\x11\x00\x00\x00\x00\x00\x00\xd5\x13\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
131 "\x1c\x14\x00\x00\x00\x00\x00\x00\x72\x14\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
132 "\x9d\x14\x00\x00\x00\x00\x00\x00\xd5\x14\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
133 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
134 "\xfb\x12\x00\x00\x00\x00\x00\x00\x09\x13\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
135 "\x0c\x13\x00\x00\x00\x00\x00\x00\xcb\x13\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
136 "\x29\x14\x00\x00\x00\x00\x00\x00\x4e\x14\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
137 "\x9d\x14\x00\x00\x00\x00\x00\x00\xd5\x14\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
138 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
139 "\xfb\x12\x00\x00\x00\x00\x00\x00\x09\x13\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
140 "\x67\x13\x00\x00\x00\x00\x00\x00\xcb\x13\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
141 "\x9d\x14\x00\x00\x00\x00\x00\x00\xd5\x14\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
142 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
143 "\x5f\x0b\x00\x00\x00\x00\x00\x00\x6c\x0b\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
144 "\x7d\x0b\x00\x00\x00\x00\x00\x00\x7e\x0c\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
145 "\x38\x0f\x00\x00\x00\x00\x00\x00\x5c\x0f\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
146 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
147 "\x83\x0c\x00\x00\x00\x00\x00\x00\xfa\x0c\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
148 "\xfd\x0d\x00\x00\x00\x00\x00\x00\xef\x0e\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
149 "\x14\x0f\x00\x00\x00\x00\x00\x00\x38\x0f\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
150 "\x9f\x0f\x00\x00\x00\x00\x00\x00\xac\x0f\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
151 "\xdb\x0f\x00\x00\x00\x00\x00\x00\xff\x0f\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
152 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
153 "\xfd\x0d\x00\x00\x00\x00\x00\x00\xd8\x0e\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
154 "\x9f\x0f\x00\x00\x00\x00\x00\x00\xac\x0f\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
155 "\xdb\x0f\x00\x00\x00\x00\x00\x00\xff\x0f\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
156 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
157 "\xfa\x0c\x00\x00\x00\x00\x00\x00\xea\x0d\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
158 "\xef\x0e\x00\x00\x00\x00\x00\x00\x14\x0f\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
159 "\x5c\x0f\x00\x00\x00\x00\x00\x00\x9f\x0f\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
160 "\xac\x0f\x00\x00\x00\x00\x00\x00\xdb\x0f\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
161 "\xff\x0f\x00\x00\x00\x00\x00\x00\x2c\x10\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
162 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
163 "\x60\x11\x00\x00\x00\x00\x00\x00\xd1\x16\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
164 "\x40\x0b\x00\x00\x00\x00\x00\x00\x2c\x10\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
165 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
166 "\x7a\x00\x00\x00\x00\x00\x00\x00\xb6\x00\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
167 "\x9f\x01\x00\x00\x00\x00\x00\x00\xa7\x01\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
168 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
169 "\x7a\x00\x00\x00\x00\x00\x00\x00\xa9\x00\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
170 "\x9f\x01\x00\x00\x00\x00\x00\x00\xa7\x01\x00\x00\x00\x00\x00\x00"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
171 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
172 672,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
173 ("\x78\x9c\x3b\x23\xc8\x00\x06\x57\x85\x21\xb4\x8c\x08\x84\x2e\x82"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
174 "\xd2\x73\xa1\xf4\x55\x28\x8d\x0e\x7e\x0b\x41\x68\x4e\xa8\x7e\x1e"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
175 "\x28\x7d\x1a\x4a\x6b\x42\xf5\xf9\x91\x69\x5e\x3a\x9a\x79\x84\xf4"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
176 "\xc7\x73\x43\xe8\x1c\x28\x5d\x0b\xa5\xeb\x78\x20\xb4\x05\x3f\x84"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
177 "\x8e\xe1\xc7\xae\xbf\x19\xaa\xee\x17\x94\xfe\xcb\x0b\xa1\xdf\xf3"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
178 "\x41\x68\x11\x7e\x54\x73\xe6\x43\xe9\x35\x50\xfa\x36\x94\xfe\x8f"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
179 "\xc3\x7c\x98\x79\x37\xf8\xc8\xd3\x0f\x73\xd7\x2b\x1c\xee\x8a\x21"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
180 "\xd2\x5d\x3a\x02\xd8\xcd\x4f\x80\xa6\x87\x8b\x62\x10\xda\x81\x1b"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
181 "\xbf\xfa\x2a\x28\xbd\x0d\x4a\xcf\x67\x84\xd0\xcb\x19\xf1\xab\x5f"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
182 "\x49\xa4\x7a\x00\x48\x97\x29\xd4"),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
183 152,
111
kono
parents:
diff changeset
184 }
kono
parents:
diff changeset
185 };
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 /* Test the hand coded samples. */
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 static void
kono
parents:
diff changeset
190 test_samples (struct backtrace_state *state)
kono
parents:
diff changeset
191 {
kono
parents:
diff changeset
192 size_t i;
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 for (i = 0; i < sizeof tests / sizeof tests[0]; ++i)
kono
parents:
diff changeset
195 {
kono
parents:
diff changeset
196 char *p;
kono
parents:
diff changeset
197 size_t v;
kono
parents:
diff changeset
198 size_t j;
kono
parents:
diff changeset
199 unsigned char *uncompressed;
kono
parents:
diff changeset
200 size_t uncompressed_len;
kono
parents:
diff changeset
201
kono
parents:
diff changeset
202 p = malloc (12 + tests[i].compressed_len);
kono
parents:
diff changeset
203 memcpy (p, "ZLIB", 4);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
204 v = tests[i].uncompressed_len;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
205 if (v == 0)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
206 v = strlen (tests[i].uncompressed);
111
kono
parents:
diff changeset
207 for (j = 0; j < 8; ++j)
kono
parents:
diff changeset
208 p[j + 4] = (v >> ((7 - j) * 8)) & 0xff;
kono
parents:
diff changeset
209 memcpy (p + 12, tests[i].compressed, tests[i].compressed_len);
kono
parents:
diff changeset
210 uncompressed = NULL;
kono
parents:
diff changeset
211 uncompressed_len = 0;
kono
parents:
diff changeset
212 if (!backtrace_uncompress_zdebug (state, (unsigned char *) p,
kono
parents:
diff changeset
213 tests[i].compressed_len + 12,
kono
parents:
diff changeset
214 error_callback_compress, NULL,
kono
parents:
diff changeset
215 &uncompressed, &uncompressed_len))
kono
parents:
diff changeset
216 {
kono
parents:
diff changeset
217 fprintf (stderr, "test %s: uncompress failed\n", tests[i].name);
kono
parents:
diff changeset
218 ++failures;
kono
parents:
diff changeset
219 }
kono
parents:
diff changeset
220 else
kono
parents:
diff changeset
221 {
kono
parents:
diff changeset
222 if (uncompressed_len != v)
kono
parents:
diff changeset
223 {
kono
parents:
diff changeset
224 fprintf (stderr,
kono
parents:
diff changeset
225 "test %s: got uncompressed length %zu, want %zu\n",
kono
parents:
diff changeset
226 tests[i].name, uncompressed_len, v);
kono
parents:
diff changeset
227 ++failures;
kono
parents:
diff changeset
228 }
kono
parents:
diff changeset
229 else if (memcmp (tests[i].uncompressed, uncompressed, v) != 0)
kono
parents:
diff changeset
230 {
kono
parents:
diff changeset
231 size_t j;
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 fprintf (stderr, "test %s: uncompressed data mismatch\n",
kono
parents:
diff changeset
234 tests[i].name);
kono
parents:
diff changeset
235 for (j = 0; j < v; ++j)
kono
parents:
diff changeset
236 if (tests[i].uncompressed[j] != uncompressed[j])
kono
parents:
diff changeset
237 fprintf (stderr, " %zu: got %#x want %#x\n", j,
kono
parents:
diff changeset
238 uncompressed[j], tests[i].uncompressed[j]);
kono
parents:
diff changeset
239 ++failures;
kono
parents:
diff changeset
240 }
kono
parents:
diff changeset
241 else
kono
parents:
diff changeset
242 printf ("PASS: inflate %s\n", tests[i].name);
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 backtrace_free (state, uncompressed, uncompressed_len,
kono
parents:
diff changeset
245 error_callback_compress, NULL);
kono
parents:
diff changeset
246 }
kono
parents:
diff changeset
247 }
kono
parents:
diff changeset
248 }
kono
parents:
diff changeset
249
kono
parents:
diff changeset
250 #ifdef HAVE_ZLIB
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 /* Given a set of TRIALS timings, discard the lowest and highest
kono
parents:
diff changeset
253 values and return the mean average of the rest. */
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 static size_t
kono
parents:
diff changeset
256 average_time (const size_t *times, size_t trials)
kono
parents:
diff changeset
257 {
kono
parents:
diff changeset
258 size_t imax;
kono
parents:
diff changeset
259 size_t max;
kono
parents:
diff changeset
260 size_t imin;
kono
parents:
diff changeset
261 size_t min;
kono
parents:
diff changeset
262 size_t i;
kono
parents:
diff changeset
263 size_t sum;
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 imin = 0;
kono
parents:
diff changeset
266 imax = 0;
kono
parents:
diff changeset
267 min = times[0];
kono
parents:
diff changeset
268 max = times[0];
kono
parents:
diff changeset
269 for (i = 1; i < trials; ++i)
kono
parents:
diff changeset
270 {
kono
parents:
diff changeset
271 if (times[i] < min)
kono
parents:
diff changeset
272 {
kono
parents:
diff changeset
273 imin = i;
kono
parents:
diff changeset
274 min = times[i];
kono
parents:
diff changeset
275 }
kono
parents:
diff changeset
276 if (times[i] > max)
kono
parents:
diff changeset
277 {
kono
parents:
diff changeset
278 imax = i;
kono
parents:
diff changeset
279 max = times[i];
kono
parents:
diff changeset
280 }
kono
parents:
diff changeset
281 }
kono
parents:
diff changeset
282
kono
parents:
diff changeset
283 sum = 0;
kono
parents:
diff changeset
284 for (i = 0; i < trials; ++i)
kono
parents:
diff changeset
285 {
kono
parents:
diff changeset
286 if (i != imax && i != imin)
kono
parents:
diff changeset
287 sum += times[i];
kono
parents:
diff changeset
288 }
kono
parents:
diff changeset
289 return sum / (trials - 2);
kono
parents:
diff changeset
290 }
kono
parents:
diff changeset
291
kono
parents:
diff changeset
292 #endif
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 /* Test a larger text, if available. */
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 static void
kono
parents:
diff changeset
297 test_large (struct backtrace_state *state)
kono
parents:
diff changeset
298 {
kono
parents:
diff changeset
299 #ifdef HAVE_ZLIB
kono
parents:
diff changeset
300 unsigned char *orig_buf;
kono
parents:
diff changeset
301 size_t orig_bufsize;
kono
parents:
diff changeset
302 size_t i;
kono
parents:
diff changeset
303 char *compressed_buf;
kono
parents:
diff changeset
304 size_t compressed_bufsize;
kono
parents:
diff changeset
305 unsigned long compress_sizearg;
kono
parents:
diff changeset
306 unsigned char *uncompressed_buf;
kono
parents:
diff changeset
307 size_t uncompressed_bufsize;
kono
parents:
diff changeset
308 int r;
kono
parents:
diff changeset
309 clockid_t cid;
kono
parents:
diff changeset
310 struct timespec ts1;
kono
parents:
diff changeset
311 struct timespec ts2;
kono
parents:
diff changeset
312 size_t ctime;
kono
parents:
diff changeset
313 size_t ztime;
kono
parents:
diff changeset
314 const size_t trials = 16;
kono
parents:
diff changeset
315 size_t ctimes[16];
kono
parents:
diff changeset
316 size_t ztimes[16];
kono
parents:
diff changeset
317 static const char * const names[] = {
kono
parents:
diff changeset
318 "Mark.Twain-Tom.Sawyer.txt",
kono
parents:
diff changeset
319 "../libgo/go/compress/testdata/Mark.Twain-Tom.Sawyer.txt"
kono
parents:
diff changeset
320 };
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 orig_buf = NULL;
kono
parents:
diff changeset
323 orig_bufsize = 0;
kono
parents:
diff changeset
324 uncompressed_buf = NULL;
kono
parents:
diff changeset
325 compressed_buf = NULL;
kono
parents:
diff changeset
326
kono
parents:
diff changeset
327 for (i = 0; i < sizeof names / sizeof names[0]; ++i)
kono
parents:
diff changeset
328 {
kono
parents:
diff changeset
329 size_t len;
kono
parents:
diff changeset
330 char *namebuf;
kono
parents:
diff changeset
331 FILE *e;
kono
parents:
diff changeset
332 struct stat st;
kono
parents:
diff changeset
333 char *rbuf;
kono
parents:
diff changeset
334 size_t got;
kono
parents:
diff changeset
335
kono
parents:
diff changeset
336 len = strlen (SRCDIR) + strlen (names[i]) + 2;
kono
parents:
diff changeset
337 namebuf = malloc (len);
kono
parents:
diff changeset
338 if (namebuf == NULL)
kono
parents:
diff changeset
339 {
kono
parents:
diff changeset
340 perror ("malloc");
kono
parents:
diff changeset
341 goto fail;
kono
parents:
diff changeset
342 }
kono
parents:
diff changeset
343 snprintf (namebuf, len, "%s/%s", SRCDIR, names[i]);
kono
parents:
diff changeset
344 e = fopen (namebuf, "r");
kono
parents:
diff changeset
345 free (namebuf);
kono
parents:
diff changeset
346 if (e == NULL)
kono
parents:
diff changeset
347 continue;
kono
parents:
diff changeset
348 if (fstat (fileno (e), &st) < 0)
kono
parents:
diff changeset
349 {
kono
parents:
diff changeset
350 perror ("fstat");
kono
parents:
diff changeset
351 fclose (e);
kono
parents:
diff changeset
352 continue;
kono
parents:
diff changeset
353 }
kono
parents:
diff changeset
354 rbuf = malloc (st.st_size);
kono
parents:
diff changeset
355 if (rbuf == NULL)
kono
parents:
diff changeset
356 {
kono
parents:
diff changeset
357 perror ("malloc");
kono
parents:
diff changeset
358 goto fail;
kono
parents:
diff changeset
359 }
kono
parents:
diff changeset
360 got = fread (rbuf, 1, st.st_size, e);
kono
parents:
diff changeset
361 fclose (e);
kono
parents:
diff changeset
362 if (got > 0)
kono
parents:
diff changeset
363 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
364 orig_buf = (unsigned char *) rbuf;
111
kono
parents:
diff changeset
365 orig_bufsize = got;
kono
parents:
diff changeset
366 break;
kono
parents:
diff changeset
367 }
kono
parents:
diff changeset
368 free (rbuf);
kono
parents:
diff changeset
369 }
kono
parents:
diff changeset
370
kono
parents:
diff changeset
371 if (orig_buf == NULL)
kono
parents:
diff changeset
372 {
kono
parents:
diff changeset
373 /* We couldn't find an input file. */
kono
parents:
diff changeset
374 printf ("UNSUPPORTED: inflate large\n");
kono
parents:
diff changeset
375 return;
kono
parents:
diff changeset
376 }
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 compressed_bufsize = compressBound (orig_bufsize) + 12;
kono
parents:
diff changeset
379 compressed_buf = malloc (compressed_bufsize);
kono
parents:
diff changeset
380 if (compressed_buf == NULL)
kono
parents:
diff changeset
381 {
kono
parents:
diff changeset
382 perror ("malloc");
kono
parents:
diff changeset
383 goto fail;
kono
parents:
diff changeset
384 }
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 compress_sizearg = compressed_bufsize - 12;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
387 r = compress ((unsigned char *) compressed_buf + 12, &compress_sizearg,
111
kono
parents:
diff changeset
388 orig_buf, orig_bufsize);
kono
parents:
diff changeset
389 if (r != Z_OK)
kono
parents:
diff changeset
390 {
kono
parents:
diff changeset
391 fprintf (stderr, "zlib compress failed: %d\n", r);
kono
parents:
diff changeset
392 goto fail;
kono
parents:
diff changeset
393 }
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 compressed_bufsize = compress_sizearg + 12;
kono
parents:
diff changeset
396
kono
parents:
diff changeset
397 /* Prepare the header that our library expects. */
kono
parents:
diff changeset
398 memcpy (compressed_buf, "ZLIB", 4);
kono
parents:
diff changeset
399 for (i = 0; i < 8; ++i)
kono
parents:
diff changeset
400 compressed_buf[i + 4] = (orig_bufsize >> ((7 - i) * 8)) & 0xff;
kono
parents:
diff changeset
401
kono
parents:
diff changeset
402 uncompressed_buf = malloc (orig_bufsize);
kono
parents:
diff changeset
403 if (uncompressed_buf == NULL)
kono
parents:
diff changeset
404 {
kono
parents:
diff changeset
405 perror ("malloc");
kono
parents:
diff changeset
406 goto fail;
kono
parents:
diff changeset
407 }
kono
parents:
diff changeset
408 uncompressed_bufsize = orig_bufsize;
kono
parents:
diff changeset
409
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
410 if (!backtrace_uncompress_zdebug (state, (unsigned char *) compressed_buf,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
411 compressed_bufsize,
111
kono
parents:
diff changeset
412 error_callback_compress, NULL,
kono
parents:
diff changeset
413 &uncompressed_buf, &uncompressed_bufsize))
kono
parents:
diff changeset
414 {
kono
parents:
diff changeset
415 fprintf (stderr, "inflate large: backtrace_uncompress_zdebug failed\n");
kono
parents:
diff changeset
416 goto fail;
kono
parents:
diff changeset
417 }
kono
parents:
diff changeset
418
kono
parents:
diff changeset
419 if (uncompressed_bufsize != orig_bufsize)
kono
parents:
diff changeset
420 {
kono
parents:
diff changeset
421 fprintf (stderr,
kono
parents:
diff changeset
422 "inflate large: got uncompressed length %zu, want %zu\n",
kono
parents:
diff changeset
423 uncompressed_bufsize, orig_bufsize);
kono
parents:
diff changeset
424 goto fail;
kono
parents:
diff changeset
425 }
kono
parents:
diff changeset
426
kono
parents:
diff changeset
427 if (memcmp (uncompressed_buf, orig_buf, uncompressed_bufsize) != 0)
kono
parents:
diff changeset
428 {
kono
parents:
diff changeset
429 fprintf (stderr, "inflate large: uncompressed data mismatch\n");
kono
parents:
diff changeset
430 goto fail;
kono
parents:
diff changeset
431 }
kono
parents:
diff changeset
432
kono
parents:
diff changeset
433 printf ("PASS: inflate large\n");
kono
parents:
diff changeset
434
kono
parents:
diff changeset
435 for (i = 0; i < trials; ++i)
kono
parents:
diff changeset
436 {
kono
parents:
diff changeset
437 unsigned long uncompress_sizearg;
kono
parents:
diff changeset
438
kono
parents:
diff changeset
439 cid = ZLIB_CLOCK_GETTIME_ARG;
kono
parents:
diff changeset
440 if (clock_gettime (cid, &ts1) < 0)
kono
parents:
diff changeset
441 {
kono
parents:
diff changeset
442 if (errno == EINVAL)
kono
parents:
diff changeset
443 return;
kono
parents:
diff changeset
444 perror ("clock_gettime");
kono
parents:
diff changeset
445 return;
kono
parents:
diff changeset
446 }
kono
parents:
diff changeset
447
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
448 if (!backtrace_uncompress_zdebug (state,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
449 (unsigned char *) compressed_buf,
111
kono
parents:
diff changeset
450 compressed_bufsize,
kono
parents:
diff changeset
451 error_callback_compress, NULL,
kono
parents:
diff changeset
452 &uncompressed_buf,
kono
parents:
diff changeset
453 &uncompressed_bufsize))
kono
parents:
diff changeset
454 {
kono
parents:
diff changeset
455 fprintf (stderr,
kono
parents:
diff changeset
456 ("inflate large: "
kono
parents:
diff changeset
457 "benchmark backtrace_uncompress_zdebug failed\n"));
kono
parents:
diff changeset
458 return;
kono
parents:
diff changeset
459 }
kono
parents:
diff changeset
460
kono
parents:
diff changeset
461 if (clock_gettime (cid, &ts2) < 0)
kono
parents:
diff changeset
462 {
kono
parents:
diff changeset
463 perror ("clock_gettime");
kono
parents:
diff changeset
464 return;
kono
parents:
diff changeset
465 }
kono
parents:
diff changeset
466
kono
parents:
diff changeset
467 ctime = (ts2.tv_sec - ts1.tv_sec) * 1000000000;
kono
parents:
diff changeset
468 ctime += ts2.tv_nsec - ts1.tv_nsec;
kono
parents:
diff changeset
469 ctimes[i] = ctime;
kono
parents:
diff changeset
470
kono
parents:
diff changeset
471 if (clock_gettime (cid, &ts1) < 0)
kono
parents:
diff changeset
472 {
kono
parents:
diff changeset
473 perror("clock_gettime");
kono
parents:
diff changeset
474 return;
kono
parents:
diff changeset
475 }
kono
parents:
diff changeset
476
kono
parents:
diff changeset
477 uncompress_sizearg = uncompressed_bufsize;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
478 r = uncompress ((unsigned char *) uncompressed_buf, &uncompress_sizearg,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
479 (unsigned char *) compressed_buf + 12,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
480 compressed_bufsize - 12);
111
kono
parents:
diff changeset
481
kono
parents:
diff changeset
482 if (clock_gettime (cid, &ts2) < 0)
kono
parents:
diff changeset
483 {
kono
parents:
diff changeset
484 perror ("clock_gettime");
kono
parents:
diff changeset
485 return;
kono
parents:
diff changeset
486 }
kono
parents:
diff changeset
487
kono
parents:
diff changeset
488 if (r != Z_OK)
kono
parents:
diff changeset
489 {
kono
parents:
diff changeset
490 fprintf (stderr,
kono
parents:
diff changeset
491 "inflate large: benchmark zlib uncompress failed: %d\n",
kono
parents:
diff changeset
492 r);
kono
parents:
diff changeset
493 return;
kono
parents:
diff changeset
494 }
kono
parents:
diff changeset
495
kono
parents:
diff changeset
496 ztime = (ts2.tv_sec - ts1.tv_sec) * 1000000000;
kono
parents:
diff changeset
497 ztime += ts2.tv_nsec - ts1.tv_nsec;
kono
parents:
diff changeset
498 ztimes[i] = ztime;
kono
parents:
diff changeset
499 }
kono
parents:
diff changeset
500
kono
parents:
diff changeset
501 /* Toss the highest and lowest times and average the rest. */
kono
parents:
diff changeset
502 ctime = average_time (ctimes, trials);
kono
parents:
diff changeset
503 ztime = average_time (ztimes, trials);
kono
parents:
diff changeset
504
kono
parents:
diff changeset
505 printf ("backtrace: %zu ns\n", ctime);
kono
parents:
diff changeset
506 printf ("zlib : %zu ns\n", ztime);
kono
parents:
diff changeset
507 printf ("ratio : %g\n", (double) ztime / (double) ctime);
kono
parents:
diff changeset
508
kono
parents:
diff changeset
509 return;
kono
parents:
diff changeset
510
kono
parents:
diff changeset
511 fail:
kono
parents:
diff changeset
512 printf ("FAIL: inflate large\n");
kono
parents:
diff changeset
513 ++failures;
kono
parents:
diff changeset
514
kono
parents:
diff changeset
515 if (orig_buf != NULL)
kono
parents:
diff changeset
516 free (orig_buf);
kono
parents:
diff changeset
517 if (compressed_buf != NULL)
kono
parents:
diff changeset
518 free (compressed_buf);
kono
parents:
diff changeset
519 if (uncompressed_buf != NULL)
kono
parents:
diff changeset
520 free (uncompressed_buf);
kono
parents:
diff changeset
521
kono
parents:
diff changeset
522 #else /* !HAVE_ZLIB */
kono
parents:
diff changeset
523
kono
parents:
diff changeset
524 printf ("UNSUPPORTED: inflate large\n");
kono
parents:
diff changeset
525
kono
parents:
diff changeset
526 #endif /* !HAVE_ZLIB */
kono
parents:
diff changeset
527 }
kono
parents:
diff changeset
528
kono
parents:
diff changeset
529 int
kono
parents:
diff changeset
530 main (int argc ATTRIBUTE_UNUSED, char **argv)
kono
parents:
diff changeset
531 {
kono
parents:
diff changeset
532 struct backtrace_state *state;
kono
parents:
diff changeset
533
kono
parents:
diff changeset
534 state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
kono
parents:
diff changeset
535 error_callback_create, NULL);
kono
parents:
diff changeset
536
kono
parents:
diff changeset
537 test_samples (state);
kono
parents:
diff changeset
538 test_large (state);
kono
parents:
diff changeset
539
kono
parents:
diff changeset
540 exit (failures != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
kono
parents:
diff changeset
541 }