ztest.c 15 KB

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