Browse Source
libbacktrace/ChangeLog: PR libbacktrace/93608 Add support for MiniDebugInfo. * elf.c (struct elf_view): Define. Replace most uses of backtrace_view with elf_view. (elf_get_view): New static functions. Replace most calls of backtrace_get_view with elf_get_view. (elf_release_view): New static functions. Replace most calls of backtrace_release_view with elf_release_view. (elf_uncompress_failed): Rename from elf_zlib_failed. Change all callers. (LZMA_STATES, LZMA_POS_STATES, LZMA_DIST_STATES): Define. (LZMA_DIST_SLOTS, LZMA_DIST_MODEL_START): Define. (LZMA_DIST_MODEL_END, LZMA_FULL_DISTANCES): Define. (LZMA_ALIGN_SIZE, LZMA_LEN_LOW_SYMBOLS): Define. (LZMA_LEN_MID_SYMBOLS, LZMA_LEN_HIGH_SYMBOLS): Define. (LZMA_LITERAL_CODERS_MAX, LZMA_LITERAL_CODER_SIZE): Define. (LZMA_PROB_IS_MATCH_LEN, LZMA_PROB_IS_REP_LEN): Define. (LZMA_PROB_IS_REP0_LEN, LZMA_PROB_IS_REP1_LEN): Define. (LZMA_PROB_IS_REP2_LEN, LZMA_PROB_IS_REP0_LONG_LEN): Define. (LZMA_PROB_DIST_SLOT_LEN, LZMA_PROB_DIST_SPECIAL_LEN): Define. (LZMA_PROB_DIST_ALIGN_LEN): Define. (LZMA_PROB_MATCH_LEN_CHOICE_LEN): Define. (LZMA_PROB_MATCH_LEN_CHOICE2_LEN): Define. (LZMA_PROB_MATCH_LEN_LOW_LEN): Define. (LZMA_PROB_MATCH_LEN_MID_LEN): Define. (LZMA_PROB_MATCH_LEN_HIGH_LEN): Define. (LZMA_PROB_REP_LEN_CHOICE_LEN): Define. (LZMA_PROB_REP_LEN_CHOICE2_LEN): Define. (LZMA_PROB_REP_LEN_LOW_LEN): Define. (LZMA_PROB_REP_LEN_MID_LEN): Define. (LZMA_PROB_REP_LEN_HIGH_LEN): Define. (LZMA_PROB_LITERAL_LEN): Define. (LZMA_PROB_IS_MATCH_OFFSET, LZMA_PROB_IS_REP_OFFSET): Define. (LZMA_PROB_IS_REP0_OFFSET, LZMA_PROB_IS_REP1_OFFSET): Define. (LZMA_PROB_IS_REP2_OFFSET): Define. (LZMA_PROB_IS_REP0_LONG_OFFSET): Define. (LZMA_PROB_DIST_SLOT_OFFSET): Define. (LZMA_PROB_DIST_SPECIAL_OFFSET): Define. (LZMA_PROB_DIST_ALIGN_OFFSET): Define. (LZMA_PROB_MATCH_LEN_CHOICE_OFFSET): Define. (LZMA_PROB_MATCH_LEN_CHOICE2_OFFSET): Define. (LZMA_PROB_MATCH_LEN_LOW_OFFSET): Define. (LZMA_PROB_MATCH_LEN_MID_OFFSET): Define. (LZMA_PROB_MATCH_LEN_HIGH_OFFSET): Define. (LZMA_PROB_REP_LEN_CHOICE_OFFSET): Define. (LZMA_PROB_REP_LEN_CHOICE2_OFFSET): Define. (LZMA_PROB_REP_LEN_LOW_OFFSET): Define. (LZMA_PROB_REP_LEN_MID_OFFSET): Define. (LZMA_PROB_REP_LEN_HIGH_OFFSET): Define. (LZMA_PROB_LITERAL_OFFSET): Define. (LZMA_PROB_TOTAL_COUNT): Define. (LZMA_IS_MATCH, LZMA_IS_REP, LZMA_IS_REP0): Define. (LZMA_IS_REP1, LZMA_IS_REP2, LZMA_IS_REP0_LONG): Define. (LZMA_DIST_SLOT, LZMA_DIST_SPECIAL, LZMA_DIST_ALIGN): Define. (LZMA_MATCH_LEN_CHOICE, LZMA_MATCH_LEN_CHOICE2): Define. (LZMA_MATCH_LEN_LOW, LZMA_MATCH_LEN_MID): Define. (LZMA_MATCH_LEN_HIGH, LZMA_REP_LEN_CHOICE): Define. (LZMA_REP_LEN_CHOICE2, LZMA_REP_LEN_LOW): Define. (LZMA_REP_LEN_MID, LZMA_REP_LEN_HIGH, LZMA_LITERAL): Define. (elf_lzma_varint): New static function. (elf_lzma_range_normalize): New static function. (elf_lzma_bit, elf_lzma_integer): New static functions. (elf_lzma_reverse_integer): New static function. (elf_lzma_len, elf_uncompress_lzma_block): New static functions. (elf_uncompress_lzma): New static function. (backtrace_uncompress_lzma): New function. (elf_add): Add memory and memory_size parameters. Change all callers. Look for .gnu_debugdata section, and, if found, decompress it and use it for symbols and debug info. Permit the descriptor parameter to be -1. * internal.h (backtrace_uncompress_lzma): Declare. * mtest.c: New file. * xztest.c: New file. * configure.ac: Check for nm, xz, and comm programs. Check for liblzma library. (HAVE_MINIDEBUG): Define. * Makefile.am (mtest_SOURCES): Define. (mtest_CFLAGS, mtest_LDADD): Define. (TESTS): Add mtest_minidebug if HAVE_MINIDEBUG. (%_minidebug): New pattern rule, if HAVE_MINIDEBUG. (xztest_SOURCES, xztest_CFLAGS, xztest_LDADD): Define. (xztest_alloc_SOURCES, xztest_alloc_CFLAGS): Define (xztest_alloc_LDADD): Define. (BUILDTESTS): Add mtest, xztest, xztest_alloc. (CLEANFILES): Add files created by minidebug pattern. (btest.lo): Correct INCDIR reference. (mtest.lo, xztest.lo, ztest.lo): New targets. * configure: Regenerate. * config.h.in: Regenerate. * Makefile.in: Regenerate. Close #38lib

9 changed files with 2911 additions and 164 deletions
@ -0,0 +1,401 @@
@@ -0,0 +1,401 @@
|
||||
/* mtest.c -- Minidebug test for libbacktrace library
|
||||
Copyright (C) 2020 Free Software Foundation, Inc. |
||||
Written by Ian Lance Taylor, Google. |
||||
|
||||
Redistribution and use in source and binary forms, with or without |
||||
modification, are permitted provided that the following conditions are |
||||
met: |
||||
|
||||
(1) Redistributions of source code must retain the above copyright |
||||
notice, this list of conditions and the following disclaimer. |
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright |
||||
notice, this list of conditions and the following disclaimer in |
||||
the documentation and/or other materials provided with the |
||||
distribution. |
||||
|
||||
(3) The name of the author may not be used to |
||||
endorse or promote products derived from this software without |
||||
specific prior written permission. |
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||||
POSSIBILITY OF SUCH DAMAGE. */ |
||||
|
||||
/* This program tests using libbacktrace with a program that uses the
|
||||
minidebuginfo format in a .gnu_debugdata section. See |
||||
https://sourceware.org/gdb/current/onlinedocs/gdb/MiniDebugInfo.html
|
||||
for a bit more information about minidebuginfo. What is relevant |
||||
for libbacktrace is that we have just a symbol table, with no debug |
||||
info, so we should be able to do a function backtrace, but we can't |
||||
do a file/line backtrace. */ |
||||
|
||||
#include <assert.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
|
||||
#include "backtrace.h" |
||||
#include "backtrace-supported.h" |
||||
|
||||
#include "testlib.h" |
||||
|
||||
static int test1 (void) __attribute__ ((noinline, noclone, unused)); |
||||
static int f2 (int) __attribute__ ((noinline, noclone)); |
||||
static int f3 (int, int) __attribute__ ((noinline, noclone)); |
||||
|
||||
/* Collected PC values. */ |
||||
|
||||
static uintptr_t addrs[20]; |
||||
|
||||
/* The backtrace callback function. This is like callback_one in
|
||||
testlib.c, but it saves the PC also. */ |
||||
|
||||
static int |
||||
callback_mtest (void *vdata, uintptr_t pc, const char *filename, int lineno, |
||||
const char *function) |
||||
{ |
||||
struct bdata *data = (struct bdata *) vdata; |
||||
|
||||
if (data->index >= sizeof addrs / sizeof addrs[0]) |
||||
{ |
||||
fprintf (stderr, "callback_mtest: callback called too many times\n"); |
||||
data->failed = 1; |
||||
return 1; |
||||
} |
||||
|
||||
addrs[data->index] = pc; |
||||
|
||||
return callback_one (vdata, pc, filename, lineno, function); |
||||
} |
||||
|
||||
/* Test the backtrace function with non-inlined functions. (We don't
|
||||
test with inlined functions because they won't work with minidebug |
||||
anyhow.) */ |
||||
|
||||
static int |
||||
test1 (void) |
||||
{ |
||||
/* Returning a value here and elsewhere avoids a tailcall which
|
||||
would mess up the backtrace. */ |
||||
return f2 (__LINE__) + 1; |
||||
} |
||||
|
||||
static int |
||||
f2 (int f1line) |
||||
{ |
||||
return f3 (f1line, __LINE__) + 2; |
||||
} |
||||
|
||||
static int |
||||
f3 (int f1line __attribute__ ((unused)), int f2line __attribute__ ((unused))) |
||||
{ |
||||
struct info all[20]; |
||||
struct bdata data; |
||||
int i; |
||||
size_t j; |
||||
|
||||
data.all = &all[0]; |
||||
data.index = 0; |
||||
data.max = 20; |
||||
data.failed = 0; |
||||
|
||||
i = backtrace_full (state, 0, callback_mtest, error_callback_one, &data); |
||||
|
||||
if (i != 0) |
||||
{ |
||||
fprintf (stderr, "test1: unexpected return value %d\n", i); |
||||
data.failed = 1; |
||||
} |
||||
|
||||
if (data.index < 3) |
||||
{ |
||||
fprintf (stderr, |
||||
"test1: not enough frames; got %zu, expected at least 3\n", |
||||
data.index); |
||||
data.failed = 1; |
||||
} |
||||
|
||||
/* When using minidebug we don't expect the function name here. */ |
||||
|
||||
for (j = 0; j < 3 && j < data.index; j++) |
||||
{ |
||||
if (all[j].function == NULL) |
||||
{ |
||||
struct symdata symdata; |
||||
|
||||
symdata.name = NULL; |
||||
symdata.val = 0; |
||||
symdata.size = 0; |
||||
symdata.failed = 0; |
||||
|
||||
i = backtrace_syminfo (state, addrs[j], callback_three, |
||||
error_callback_three, &symdata); |
||||
if (i == 0) |
||||
{ |
||||
fprintf (stderr, |
||||
("test1: [%zu], unexpected return value from " |
||||
"backtrace_syminfo %d\n"), |
||||
j, i); |
||||
data.failed = 1; |
||||
} |
||||
else if (symdata.name == NULL) |
||||
{ |
||||
fprintf (stderr, "test1: [%zu]: syminfo did not find name\n", j); |
||||
data.failed = 1; |
||||
} |
||||
else |
||||
all[j].function = strdup (symdata.name); |
||||
} |
||||
} |
||||
|
||||
if (all[0].function == NULL) |
||||
{ |
||||
fprintf (stderr, "test1: [0]: missing function name\n"); |
||||
data.failed = 1; |
||||
} |
||||
else if (strcmp (all[0].function, "f3") != 0) |
||||
{ |
||||
fprintf (stderr, "test1: [0]: got %s expected %s\n", |
||||
all[0].function, "f3"); |
||||
data.failed = 1; |
||||
} |
||||
|
||||
if (all[1].function == NULL) |
||||
{ |
||||
fprintf (stderr, "test1: [1]: missing function name\n"); |
||||
data.failed = 1; |
||||
} |
||||
else if (strcmp (all[1].function, "f2") != 0) |
||||
{ |
||||
fprintf (stderr, "test1: [1]: got %s expected %s\n", |
||||
all[0].function, "f2"); |
||||
data.failed = 1; |
||||
} |
||||
|
||||
if (all[2].function == NULL) |
||||
{ |
||||
fprintf (stderr, "test1: [2]: missing function name\n"); |
||||
data.failed = 1; |
||||
} |
||||
else if (strcmp (all[2].function, "test1") != 0) |
||||
{ |
||||
fprintf (stderr, "test1: [2]: got %s expected %s\n", |
||||
all[0].function, "test1"); |
||||
data.failed = 1; |
||||
} |
||||
|
||||
printf ("%s: backtrace_full noinline\n", data.failed ? "FAIL" : "PASS"); |
||||
|
||||
if (data.failed) |
||||
++failures; |
||||
|
||||
return failures; |
||||
} |
||||
|
||||
/* Test the backtrace_simple function with non-inlined functions. */ |
||||
|
||||
static int test3 (void) __attribute__ ((noinline, noclone, unused)); |
||||
static int f22 (int) __attribute__ ((noinline, noclone)); |
||||
static int f23 (int, int) __attribute__ ((noinline, noclone)); |
||||
|
||||
static int |
||||
test3 (void) |
||||
{ |
||||
return f22 (__LINE__) + 1; |
||||
} |
||||
|
||||
static int |
||||
f22 (int f1line) |
||||
{ |
||||
return f23 (f1line, __LINE__) + 2; |
||||
} |
||||
|
||||
static int |
||||
f23 (int f1line __attribute__ ((unused)), int f2line __attribute__ ((unused))) |
||||
{ |
||||
uintptr_t addrs[20]; |
||||
struct sdata data; |
||||
int i; |
||||
|
||||
data.addrs = &addrs[0]; |
||||
data.index = 0; |
||||
data.max = 20; |
||||
data.failed = 0; |
||||
|
||||
i = backtrace_simple (state, 0, callback_two, error_callback_two, &data); |
||||
|
||||
if (i != 0) |
||||
{ |
||||
fprintf (stderr, "test3: unexpected return value %d\n", i); |
||||
data.failed = 1; |
||||
} |
||||
|
||||
if (!data.failed) |
||||
{ |
||||
int j; |
||||
|
||||
for (j = 0; j < 3; ++j) |
||||
{ |
||||
struct symdata symdata; |
||||
|
||||
symdata.name = NULL; |
||||
symdata.val = 0; |
||||
symdata.size = 0; |
||||
symdata.failed = 0; |
||||
|
||||
i = backtrace_syminfo (state, addrs[j], callback_three, |
||||
error_callback_three, &symdata); |
||||
if (i == 0) |
||||
{ |
||||
fprintf (stderr, |
||||
("test3: [%d]: unexpected return value " |
||||
"from backtrace_syminfo %d\n"), |
||||
j, i); |
||||
symdata.failed = 1; |
||||
} |
||||
|
||||
if (!symdata.failed) |
||||
{ |
||||
const char *expected; |
||||
|
||||
switch (j) |
||||
{ |
||||
case 0: |
||||
expected = "f23"; |
||||
break; |
||||
case 1: |
||||
expected = "f22"; |
||||
break; |
||||
case 2: |
||||
expected = "test3"; |
||||
break; |
||||
default: |
||||
assert (0); |
||||
} |
||||
|
||||
if (symdata.name == NULL) |
||||
{ |
||||
fprintf (stderr, "test3: [%d]: NULL syminfo name\n", j); |
||||
symdata.failed = 1; |
||||
} |
||||
/* Use strncmp, not strcmp, because GCC might create a
|
||||
clone. */ |
||||
else if (strncmp (symdata.name, expected, strlen (expected)) |
||||
!= 0) |
||||
{ |
||||
fprintf (stderr, |
||||
("test3: [%d]: unexpected syminfo name " |
||||
"got %s expected %s\n"), |
||||
j, symdata.name, expected); |
||||
symdata.failed = 1; |
||||
} |
||||
} |
||||
|
||||
if (symdata.failed) |
||||
data.failed = 1; |
||||
} |
||||
} |
||||
|
||||
printf ("%s: backtrace_simple noinline\n", data.failed ? "FAIL" : "PASS"); |
||||
|
||||
if (data.failed) |
||||
++failures; |
||||
|
||||
return failures; |
||||
} |
||||
|
||||
int test5 (void) __attribute__ ((unused)); |
||||
|
||||
int global = 1; |
||||
|
||||
int |
||||
test5 (void) |
||||
{ |
||||
struct symdata symdata; |
||||
int i; |
||||
uintptr_t addr = (uintptr_t) &global; |
||||
|
||||
if (sizeof (global) > 1) |
||||
addr += 1; |
||||
|
||||
symdata.name = NULL; |
||||
symdata.val = 0; |
||||
symdata.size = 0; |
||||
symdata.failed = 0; |
||||
|
||||
i = backtrace_syminfo (state, addr, callback_three, |
||||
error_callback_three, &symdata); |
||||
if (i == 0) |
||||
{ |
||||
fprintf (stderr, |
||||
"test5: unexpected return value from backtrace_syminfo %d\n", |
||||
i); |
||||
symdata.failed = 1; |
||||
} |
||||
|
||||
if (!symdata.failed) |
||||
{ |
||||
if (symdata.name == NULL) |
||||
{ |
||||
fprintf (stderr, "test5: NULL syminfo name\n"); |
||||
symdata.failed = 1; |
||||
} |
||||
else if (!(strncmp (symdata.name, "global", 6) == 0 |
||||
&& (symdata.name[6] == '\0'|| symdata.name[6] == '.'))) |
||||
{ |
||||
fprintf (stderr, |
||||
"test5: unexpected syminfo name got %s expected %s\n", |
||||
symdata.name, "global"); |
||||
symdata.failed = 1; |
||||
} |
||||
else if (symdata.val != (uintptr_t) &global) |
||||
{ |
||||
fprintf (stderr, |
||||
"test5: unexpected syminfo value got %lx expected %lx\n", |
||||
(unsigned long) symdata.val, |
||||
(unsigned long) (uintptr_t) &global); |
||||
symdata.failed = 1; |
||||
} |
||||
else if (symdata.size != sizeof (global)) |
||||
{ |
||||
fprintf (stderr, |
||||
"test5: unexpected syminfo size got %lx expected %lx\n", |
||||
(unsigned long) symdata.size, |
||||
(unsigned long) sizeof (global)); |
||||
symdata.failed = 1; |
||||
} |
||||
} |
||||
|
||||
printf ("%s: backtrace_syminfo variable\n", |
||||
symdata.failed ? "FAIL" : "PASS"); |
||||
|
||||
if (symdata.failed) |
||||
++failures; |
||||
|
||||
return failures; |
||||
} |
||||
|
||||
int |
||||
main (int argc ATTRIBUTE_UNUSED, char **argv) |
||||
{ |
||||
state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS, |
||||
error_callback_create, NULL); |
||||
|
||||
#if BACKTRACE_SUPPORTED |
||||
test1 (); |
||||
test3 (); |
||||
#if BACKTRACE_SUPPORTS_DATA |
||||
test5 (); |
||||
#endif |
||||
#endif |
||||
|
||||
exit (failures ? EXIT_FAILURE : EXIT_SUCCESS); |
||||
} |
@ -0,0 +1,508 @@
@@ -0,0 +1,508 @@
|
||||
/* xztest.c -- Test for libbacktrace LZMA decoder.
|
||||
Copyright (C) 2020 Free Software Foundation, Inc. |
||||
Written by Ian Lance Taylor, Google. |
||||
|
||||
Redistribution and use in source and binary forms, with or without |
||||
modification, are permitted provided that the following conditions are |
||||
met: |
||||
|
||||
(1) Redistributions of source code must retain the above copyright |
||||
notice, this list of conditions and the following disclaimer. |
||||
|
||||
(2) Redistributions in binary form must reproduce the above copyright |
||||
notice, this list of conditions and the following disclaimer in |
||||
the documentation and/or other materials provided with the |
||||
distribution. |
||||
|
||||
(3) The name of the author may not be used to |
||||
endorse or promote products derived from this software without |
||||
specific prior written permission. |
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||||
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
||||
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||||
POSSIBILITY OF SUCH DAMAGE. */ |
||||
|
||||
#include "config.h" |
||||
|
||||
#include <errno.h> |
||||
#include <limits.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <time.h> |
||||
#include <sys/types.h> |
||||
#include <sys/stat.h> |
||||
|
||||
#ifdef HAVE_LIBLZMA |
||||
#include <lzma.h> |
||||
#endif |
||||
|
||||
#include "backtrace.h" |
||||
#include "backtrace-supported.h" |
||||
|
||||
#include "internal.h" |
||||
#include "testlib.h" |
||||
|
||||
#ifndef HAVE_CLOCK_GETTIME |
||||
|
||||
typedef int xclockid_t; |
||||
|
||||
static int |
||||
xclock_gettime (xclockid_t id ATTRIBUTE_UNUSED, |
||||
struct timespec *ts ATTRIBUTE_UNUSED) |
||||
{ |
||||
errno = EINVAL; |
||||
return -1; |
||||
} |
||||
|
||||
#define clockid_t xclockid_t |
||||
#define clock_gettime xclock_gettime |
||||
#undef CLOCK_REALTIME |
||||
#define CLOCK_REALTIME 0 |
||||
|
||||
#endif /* !defined(HAVE_CLOCK_GETTIME) */ |
||||
|
||||
#ifdef CLOCK_PROCESS_CPUTIME_ID |
||||
#define LIBLZMA_CLOCK_GETTIME_ARG CLOCK_PROCESS_CPUTIME_ID |
||||
#else |
||||
#define LIBLZMA_CLOCK_GETTIME_ARG CLOCK_REALTIME |
||||
#endif |
||||
|
||||
/* Some tests for the local lzma inflation code. */ |
||||
|
||||
struct lzma_test |
||||
{ |
||||
const char *name; |
||||
const char *uncompressed; |
||||
size_t uncompressed_len; |
||||
const char *compressed; |
||||
size_t compressed_len; |
||||
}; |
||||
|
||||
/* Error callback. */ |
||||
|
||||
static void |
||||
error_callback_compress (void *vdata ATTRIBUTE_UNUSED, const char *msg, |
||||
int errnum) |
||||
{ |
||||
fprintf (stderr, "%s", msg); |
||||
if (errnum > 0) |
||||
fprintf (stderr, ": %s", strerror (errnum)); |
||||
fprintf (stderr, "\n"); |
||||
exit (EXIT_FAILURE); |
||||
} |
||||
|
||||
static const struct lzma_test tests[] = |
||||
{ |
||||
{ |
||||
"empty", |
||||
"", |
||||
0, |
||||
("\xfd\x37\x7a\x58\x5a\x00\x00\x04\xe6\xd6\xb4\x46\x00\x00\x00\x00" |
||||
"\x1c\xdf\x44\x21\x1f\xb6\xf3\x7d\x01\x00\x00\x00\x00\x04\x59\x5a"), |
||||
32, |
||||
}, |
||||
{ |
||||
"hello", |
||||
"hello, world\n", |
||||
0, |
||||
("\xfd\x37\x7a\x58\x5a\x00\x00\x04\xe6\xd6\xb4\x46\x02\x00\x21\x01" |
||||
"\x16\x00\x00\x00\x74\x2f\xe5\xa3\x01\x00\x0c\x68\x65\x6c\x6c\x6f" |
||||
"\x2c\x20\x77\x6f\x72\x6c\x64\x0a\x00\x00\x00\x00\x7b\x46\x5a\x81" |
||||
"\xc9\x12\xb8\xea\x00\x01\x25\x0d\x71\x19\xc4\xb6\x1f\xb6\xf3\x7d" |
||||
"\x01\x00\x00\x00\x00\x04\x59\x5a"), |
||||
72, |
||||
}, |
||||
{ |
||||
"goodbye", |
||||
"goodbye, world", |
||||
0, |
||||
("\xfd\x37\x7a\x58\x5a\x00\x00\x04\xe6\xd6\xb4\x46\x02\x00\x21\x01" |
||||
"\x16\x00\x00\x00\x74\x2f\xe5\xa3\x01\x00\x0d\x67\x6f\x6f\x64\x62" |
||||
"\x79\x65\x2c\x20\x77\x6f\x72\x6c\x64\x00\x00\x00\xf6\xf8\xa3\x33" |
||||
"\x8c\x4e\xc9\x68\x00\x01\x26\x0e\x08\x1b\xe0\x04\x1f\xb6\xf3\x7d" |
||||
"\x01\x00\x00\x00\x00\x04\x59\x5a"), |
||||
72, |
||||
}, |
||||
}; |
||||
|
||||
/* Test the hand coded samples. */ |
||||
|
||||
static void |
||||
test_samples (struct backtrace_state *state) |
||||
{ |
||||
size_t i; |
||||
|
||||
for (i = 0; i < sizeof tests / sizeof tests[0]; ++i) |
||||
{ |
||||
unsigned char *uncompressed; |
||||
size_t uncompressed_len; |
||||
|
||||
uncompressed = NULL; |
||||
uncompressed_len = 0; |
||||
if (!backtrace_uncompress_lzma (state, |
||||
((const unsigned char *) |
||||
tests[i].compressed), |
||||
tests[i].compressed_len, |
||||
error_callback_compress, NULL, |
||||
&uncompressed, &uncompressed_len)) |
||||
{ |
||||
fprintf (stderr, "test %s: uncompress failed\n", tests[i].name); |
||||
++failures; |
||||
} |
||||
else |
||||
{ |
||||
size_t v; |
||||
|
||||
v = tests[i].uncompressed_len; |
||||
if (v == 0) |
||||
v = strlen (tests[i].uncompressed); |
||||
if (uncompressed_len != v) |
||||
{ |
||||
fprintf (stderr, |
||||
"test %s: got uncompressed length %zu, want %zu\n", |
||||
tests[i].name, uncompressed_len, v); |
||||
++failures; |
||||
} |
||||
else if (memcmp (tests[i].uncompressed, uncompressed, v) != 0) |
||||
{ |
||||
size_t j; |
||||
|
||||
fprintf (stderr, "test %s: uncompressed data mismatch\n", |
||||
tests[i].name); |
||||
for (j = 0; j < v; ++j) |
||||
if (tests[i].uncompressed[j] != uncompressed[j]) |
||||
fprintf (stderr, " %zu: got %#x want %#x\n", j, |
||||
uncompressed[j], tests[i].uncompressed[j]); |
||||
++failures; |
||||
} |
||||
else |
||||
printf ("PASS: lzma %s\n", tests[i].name); |
||||
|
||||
backtrace_free (state, uncompressed, uncompressed_len, |
||||
error_callback_compress, NULL); |
||||
} |
||||
} |
||||
} |
||||
|
||||
#if HAVE_LIBLZMA |
||||
|
||||
/* Given a set of TRIALS timings, discard the lowest and highest
|
||||
values and return the mean average of the rest. */ |
||||
|
||||
static size_t |
||||
average_time (const size_t *times, size_t trials) |
||||
{ |
||||
size_t imax; |
||||
size_t max; |
||||
size_t imin; |
||||
size_t min; |
||||
size_t i; |
||||
size_t sum; |
||||
|
||||
imin = 0; |
||||
imax = 0; |
||||
min = times[0]; |
||||
max = times[0]; |
||||
for (i = 1; i < trials; ++i) |
||||
{ |
||||
if (times[i] < min) |
||||
{ |
||||
imin = i; |
||||
min = times[i]; |
||||
} |
||||
if (times[i] > max) |
||||
{ |
||||
imax = i; |
||||
max = times[i]; |
||||
} |
||||
} |
||||
|
||||
sum = 0; |
||||
for (i = 0; i < trials; ++i) |
||||
{ |
||||
if (i != imax && i != imin) |
||||
sum += times[i]; |
||||
} |
||||
return sum / (trials - 2); |
||||
} |
||||
|
||||
#endif |
||||
|
||||
/* Test a larger text, if available. */ |
||||
|
||||
static void |
||||
test_large (struct backtrace_state *state ATTRIBUTE_UNUSED) |
||||
{ |
||||
#if HAVE_LIBLZMA |
||||
unsigned char *orig_buf; |
||||
size_t orig_bufsize; |
||||
size_t i; |
||||
lzma_stream initial_stream = LZMA_STREAM_INIT; |
||||
lzma_stream stream; |
||||
unsigned char *compressed_buf; |
||||
size_t compressed_bufsize; |
||||
unsigned char *uncompressed_buf; |
||||
size_t uncompressed_bufsize; |
||||
unsigned char *spare_buf; |
||||
int r; |
||||