xztest.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* xztest.c -- Test for libbacktrace LZMA decoder.
  2. Copyright (C) 2020-2022 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 <limits.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #ifdef HAVE_LIBLZMA
  37. #include <lzma.h>
  38. #endif
  39. #include "backtrace.h"
  40. #include "backtrace-supported.h"
  41. #include "internal.h"
  42. #include "testlib.h"
  43. #ifndef HAVE_CLOCK_GETTIME
  44. typedef int xclockid_t;
  45. static int
  46. xclock_gettime (xclockid_t id ATTRIBUTE_UNUSED,
  47. struct timespec *ts ATTRIBUTE_UNUSED)
  48. {
  49. errno = EINVAL;
  50. return -1;
  51. }
  52. #define clockid_t xclockid_t
  53. #define clock_gettime xclock_gettime
  54. #undef CLOCK_REALTIME
  55. #define CLOCK_REALTIME 0
  56. #endif /* !defined(HAVE_CLOCK_GETTIME) */
  57. #ifdef CLOCK_PROCESS_CPUTIME_ID
  58. #define LIBLZMA_CLOCK_GETTIME_ARG CLOCK_PROCESS_CPUTIME_ID
  59. #else
  60. #define LIBLZMA_CLOCK_GETTIME_ARG CLOCK_REALTIME
  61. #endif
  62. /* Some tests for the local lzma inflation code. */
  63. struct lzma_test
  64. {
  65. const char *name;
  66. const char *uncompressed;
  67. size_t uncompressed_len;
  68. const char *compressed;
  69. size_t compressed_len;
  70. };
  71. /* Error callback. */
  72. static void
  73. error_callback_compress (void *vdata ATTRIBUTE_UNUSED, const char *msg,
  74. int errnum)
  75. {
  76. fprintf (stderr, "%s", msg);
  77. if (errnum > 0)
  78. fprintf (stderr, ": %s", strerror (errnum));
  79. fprintf (stderr, "\n");
  80. exit (EXIT_FAILURE);
  81. }
  82. static const struct lzma_test tests[] =
  83. {
  84. {
  85. "empty",
  86. "",
  87. 0,
  88. ("\xfd\x37\x7a\x58\x5a\x00\x00\x04\xe6\xd6\xb4\x46\x00\x00\x00\x00"
  89. "\x1c\xdf\x44\x21\x1f\xb6\xf3\x7d\x01\x00\x00\x00\x00\x04\x59\x5a"),
  90. 32,
  91. },
  92. {
  93. "hello",
  94. "hello, world\n",
  95. 0,
  96. ("\xfd\x37\x7a\x58\x5a\x00\x00\x04\xe6\xd6\xb4\x46\x02\x00\x21\x01"
  97. "\x16\x00\x00\x00\x74\x2f\xe5\xa3\x01\x00\x0c\x68\x65\x6c\x6c\x6f"
  98. "\x2c\x20\x77\x6f\x72\x6c\x64\x0a\x00\x00\x00\x00\x7b\x46\x5a\x81"
  99. "\xc9\x12\xb8\xea\x00\x01\x25\x0d\x71\x19\xc4\xb6\x1f\xb6\xf3\x7d"
  100. "\x01\x00\x00\x00\x00\x04\x59\x5a"),
  101. 72,
  102. },
  103. {
  104. "goodbye",
  105. "goodbye, world",
  106. 0,
  107. ("\xfd\x37\x7a\x58\x5a\x00\x00\x04\xe6\xd6\xb4\x46\x02\x00\x21\x01"
  108. "\x16\x00\x00\x00\x74\x2f\xe5\xa3\x01\x00\x0d\x67\x6f\x6f\x64\x62"
  109. "\x79\x65\x2c\x20\x77\x6f\x72\x6c\x64\x00\x00\x00\xf6\xf8\xa3\x33"
  110. "\x8c\x4e\xc9\x68\x00\x01\x26\x0e\x08\x1b\xe0\x04\x1f\xb6\xf3\x7d"
  111. "\x01\x00\x00\x00\x00\x04\x59\x5a"),
  112. 72,
  113. },
  114. };
  115. /* Test the hand coded samples. */
  116. static void
  117. test_samples (struct backtrace_state *state)
  118. {
  119. size_t i;
  120. for (i = 0; i < sizeof tests / sizeof tests[0]; ++i)
  121. {
  122. unsigned char *uncompressed;
  123. size_t uncompressed_len;
  124. uncompressed = NULL;
  125. uncompressed_len = 0;
  126. if (!backtrace_uncompress_lzma (state,
  127. ((const unsigned char *)
  128. tests[i].compressed),
  129. tests[i].compressed_len,
  130. error_callback_compress, NULL,
  131. &uncompressed, &uncompressed_len))
  132. {
  133. fprintf (stderr, "test %s: uncompress failed\n", tests[i].name);
  134. ++failures;
  135. }
  136. else
  137. {
  138. size_t v;
  139. v = tests[i].uncompressed_len;
  140. if (v == 0)
  141. v = strlen (tests[i].uncompressed);
  142. if (uncompressed_len != v)
  143. {
  144. fprintf (stderr,
  145. "test %s: got uncompressed length %zu, want %zu\n",
  146. tests[i].name, uncompressed_len, v);
  147. ++failures;
  148. }
  149. else if (v > 0 && memcmp (tests[i].uncompressed, uncompressed, v) != 0)
  150. {
  151. size_t j;
  152. fprintf (stderr, "test %s: uncompressed data mismatch\n",
  153. tests[i].name);
  154. for (j = 0; j < v; ++j)
  155. if (tests[i].uncompressed[j] != uncompressed[j])
  156. fprintf (stderr, " %zu: got %#x want %#x\n", j,
  157. uncompressed[j], tests[i].uncompressed[j]);
  158. ++failures;
  159. }
  160. else
  161. printf ("PASS: lzma %s\n", tests[i].name);
  162. backtrace_free (state, uncompressed, uncompressed_len,
  163. error_callback_compress, NULL);
  164. }
  165. }
  166. }
  167. #if HAVE_LIBLZMA
  168. /* Given a set of TRIALS timings, discard the lowest and highest
  169. values and return the mean average of the rest. */
  170. static size_t
  171. average_time (const size_t *times, size_t trials)
  172. {
  173. size_t imax;
  174. size_t max;
  175. size_t imin;
  176. size_t min;
  177. size_t i;
  178. size_t sum;
  179. imin = 0;
  180. imax = 0;
  181. min = times[0];
  182. max = times[0];
  183. for (i = 1; i < trials; ++i)
  184. {
  185. if (times[i] < min)
  186. {
  187. imin = i;
  188. min = times[i];
  189. }
  190. if (times[i] > max)
  191. {
  192. imax = i;
  193. max = times[i];
  194. }
  195. }
  196. sum = 0;
  197. for (i = 0; i < trials; ++i)
  198. {
  199. if (i != imax && i != imin)
  200. sum += times[i];
  201. }
  202. return sum / (trials - 2);
  203. }
  204. #endif
  205. /* Test a larger text, if available. */
  206. static void
  207. test_large (struct backtrace_state *state ATTRIBUTE_UNUSED)
  208. {
  209. #if HAVE_LIBLZMA
  210. unsigned char *orig_buf;
  211. size_t orig_bufsize;
  212. size_t i;
  213. lzma_stream initial_stream = LZMA_STREAM_INIT;
  214. lzma_stream stream;
  215. unsigned char *compressed_buf;
  216. size_t compressed_bufsize;
  217. unsigned char *uncompressed_buf;
  218. size_t uncompressed_bufsize;
  219. unsigned char *spare_buf;
  220. int r;
  221. clockid_t cid;
  222. struct timespec ts1;
  223. struct timespec ts2;
  224. size_t ctime;
  225. size_t ztime;
  226. const size_t trials = 16;
  227. size_t ctimes[16];
  228. size_t ztimes[16];
  229. static const char * const names[] = {
  230. "Isaac.Newton-Opticks.txt",
  231. "../libgo/go/testdata/Isaac.Newton-Opticks.txt",
  232. };
  233. orig_buf = NULL;
  234. orig_bufsize = 0;
  235. uncompressed_buf = NULL;
  236. compressed_buf = NULL;
  237. for (i = 0; i < sizeof names / sizeof names[0]; ++i)
  238. {
  239. size_t len;
  240. char *namebuf;
  241. FILE *e;
  242. struct stat st;
  243. char *rbuf;
  244. size_t got;
  245. len = strlen (SRCDIR) + strlen (names[i]) + 2;
  246. namebuf = malloc (len);
  247. if (namebuf == NULL)
  248. {
  249. perror ("malloc");
  250. goto fail;
  251. }
  252. snprintf (namebuf, len, "%s/%s", SRCDIR, names[i]);
  253. e = fopen (namebuf, "r");
  254. free (namebuf);
  255. if (e == NULL)
  256. continue;
  257. if (fstat (fileno (e), &st) < 0)
  258. {
  259. perror ("fstat");
  260. fclose (e);
  261. continue;
  262. }
  263. rbuf = malloc (st.st_size);
  264. if (rbuf == NULL)
  265. {
  266. perror ("malloc");
  267. goto fail;
  268. }
  269. got = fread (rbuf, 1, st.st_size, e);
  270. fclose (e);
  271. if (got > 0)
  272. {
  273. orig_buf = (unsigned char *) rbuf;
  274. orig_bufsize = got;
  275. break;
  276. }
  277. free (rbuf);
  278. }
  279. if (orig_buf == NULL)
  280. {
  281. /* We couldn't find an input file. */
  282. printf ("UNSUPPORTED: lzma large\n");
  283. return;
  284. }
  285. stream = initial_stream;
  286. r = lzma_easy_encoder (&stream, 6, LZMA_CHECK_CRC32);
  287. if (r != LZMA_OK)
  288. {
  289. fprintf (stderr, "lzma_easy_encoder failed: %d\n", r);
  290. goto fail;
  291. }
  292. compressed_bufsize = orig_bufsize + 100;
  293. compressed_buf = malloc (compressed_bufsize);
  294. if (compressed_buf == NULL)
  295. {
  296. perror ("malloc");
  297. goto fail;
  298. }
  299. stream.next_in = orig_buf;
  300. stream.avail_in = orig_bufsize;
  301. stream.next_out = compressed_buf;
  302. stream.avail_out = compressed_bufsize;
  303. do
  304. {
  305. r = lzma_code (&stream, LZMA_FINISH);
  306. if (r != LZMA_OK && r != LZMA_STREAM_END)
  307. {
  308. fprintf (stderr, "lzma_code failed: %d\n", r);
  309. goto fail;
  310. }
  311. }
  312. while (r != LZMA_STREAM_END);
  313. compressed_bufsize = stream.total_out;
  314. if (!backtrace_uncompress_lzma (state, (unsigned char *) compressed_buf,
  315. compressed_bufsize,
  316. error_callback_compress, NULL,
  317. &uncompressed_buf, &uncompressed_bufsize))
  318. {
  319. fprintf (stderr, "lzma large: backtrace_uncompress_lzma failed\n");
  320. goto fail;
  321. }
  322. if (uncompressed_bufsize != orig_bufsize)
  323. {
  324. fprintf (stderr,
  325. "lzma large: got uncompressed length %zu, want %zu\n",
  326. uncompressed_bufsize, orig_bufsize);
  327. goto fail;
  328. }
  329. if (memcmp (uncompressed_buf, orig_buf, uncompressed_bufsize) != 0)
  330. {
  331. fprintf (stderr, "lzma large: uncompressed data mismatch\n");
  332. goto fail;
  333. }
  334. printf ("PASS: lzma large\n");
  335. spare_buf = malloc (orig_bufsize);
  336. if (spare_buf == NULL)
  337. {
  338. perror ("malloc");
  339. goto fail;
  340. }
  341. for (i = 0; i < trials; ++i)
  342. {
  343. cid = LIBLZMA_CLOCK_GETTIME_ARG;
  344. if (clock_gettime (cid, &ts1) < 0)
  345. {
  346. if (errno == EINVAL)
  347. return;
  348. perror ("clock_gettime");
  349. return;
  350. }
  351. if (!backtrace_uncompress_lzma (state,
  352. (unsigned char *) compressed_buf,
  353. compressed_bufsize,
  354. error_callback_compress, NULL,
  355. &uncompressed_buf,
  356. &uncompressed_bufsize))
  357. {
  358. fprintf (stderr,
  359. ("lzma large: "
  360. "benchmark backtrace_uncompress_lzma failed\n"));
  361. return;
  362. }
  363. if (clock_gettime (cid, &ts2) < 0)
  364. {
  365. perror ("clock_gettime");
  366. return;
  367. }
  368. ctime = (ts2.tv_sec - ts1.tv_sec) * 1000000000;
  369. ctime += ts2.tv_nsec - ts1.tv_nsec;
  370. ctimes[i] = ctime;
  371. stream = initial_stream;
  372. r = lzma_auto_decoder (&stream, UINT64_MAX, 0);
  373. if (r != LZMA_OK)
  374. {
  375. fprintf (stderr, "lzma_stream_decoder failed: %d\n", r);
  376. goto fail;
  377. }
  378. stream.next_in = compressed_buf;
  379. stream.avail_in = compressed_bufsize;
  380. stream.next_out = spare_buf;
  381. stream.avail_out = orig_bufsize;
  382. if (clock_gettime (cid, &ts1) < 0)
  383. {
  384. perror("clock_gettime");
  385. return;
  386. }
  387. do
  388. {
  389. r = lzma_code (&stream, LZMA_FINISH);
  390. if (r != LZMA_OK && r != LZMA_STREAM_END)
  391. {
  392. fprintf (stderr, "lzma_code failed: %d\n", r);
  393. goto fail;
  394. }
  395. }
  396. while (r != LZMA_STREAM_END);
  397. if (clock_gettime (cid, &ts2) < 0)
  398. {
  399. perror ("clock_gettime");
  400. return;
  401. }
  402. ztime = (ts2.tv_sec - ts1.tv_sec) * 1000000000;
  403. ztime += ts2.tv_nsec - ts1.tv_nsec;
  404. ztimes[i] = ztime;
  405. }
  406. /* Toss the highest and lowest times and average the rest. */
  407. ctime = average_time (ctimes, trials);
  408. ztime = average_time (ztimes, trials);
  409. printf ("backtrace: %zu ns\n", ctime);
  410. printf ("liblzma : %zu ns\n", ztime);
  411. printf ("ratio : %g\n", (double) ztime / (double) ctime);
  412. return;
  413. fail:
  414. printf ("FAIL: lzma large\n");
  415. ++failures;
  416. if (orig_buf != NULL)
  417. free (orig_buf);
  418. if (compressed_buf != NULL)
  419. free (compressed_buf);
  420. if (uncompressed_buf != NULL)
  421. free (uncompressed_buf);
  422. #else /* !HAVE_LIBLZMA */
  423. printf ("UNSUPPORTED: lzma large\n");
  424. #endif /* !HAVE_LIBLZMA */
  425. }
  426. int
  427. main (int argc ATTRIBUTE_UNUSED, char **argv)
  428. {
  429. struct backtrace_state *state;
  430. state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
  431. error_callback_create, NULL);
  432. test_samples (state);
  433. test_large (state);
  434. exit (failures != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
  435. }