From: Antoine Busque Date: Thu, 30 Jul 2015 20:37:04 +0000 (-0400) Subject: Add unit tests for lttng_ust_elf X-Git-Tag: v2.8.0-rc1~99 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=22609c7a82c5c8ebc07c19cade9b4d6ff61bd0d0;p=lttng-ust.git Add unit tests for lttng_ust_elf This adds unit tests for UST's ELF parser. Also included are test ELF files for multiple architectures (x86, x86_64, armeb, aarch64_be). The procedure to generate these test files is described in `tests/ust-elf/README.md`. Signed-off-by: Antoine Busque Signed-off-by: Mathieu Desnoyers --- diff --git a/.gitignore b/.gitignore index 1039b1a0..b1f985b8 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,7 @@ tests/trace_event/trace_event_test tests/tracepoint/benchmark/tracepoint_benchmark tests/tracepoint/tracepoint_test tests/snprintf/prog +tests/ust-elf/prog tests/benchmark/bench1 tests/benchmark/bench2 diff --git a/configure.ac b/configure.ac index ede01952..cd92e192 100644 --- a/configure.ac +++ b/configure.ac @@ -402,6 +402,7 @@ AC_CONFIG_FILES([ tests/hello.cxx/Makefile tests/same_line_tracepoint/Makefile tests/snprintf/Makefile + tests/ust-elf/Makefile tests/benchmark/Makefile tests/utils/Makefile lttng-ust.pc diff --git a/tests/Makefile.am b/tests/Makefile.am index 30b0e9a0..3d7ceeb9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = utils hello same_line_tracepoint snprintf benchmark +SUBDIRS = utils hello same_line_tracepoint snprintf benchmark ust-elf if CXX_WORKS SUBDIRS += hello.cxx diff --git a/tests/unit_tests b/tests/unit_tests index 91101383..4d69789b 100644 --- a/tests/unit_tests +++ b/tests/unit_tests @@ -1 +1,2 @@ snprintf/test_snprintf +ust-elf/test_ust_elf diff --git a/tests/ust-elf/Makefile.am b/tests/ust-elf/Makefile.am new file mode 100644 index 00000000..3cb54423 --- /dev/null +++ b/tests/ust-elf/Makefile.am @@ -0,0 +1,24 @@ +AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/tests/utils + +noinst_PROGRAMS = prog +prog_SOURCES = prog.c +prog_LDADD = $(top_builddir)/liblttng-ust/liblttng-ust.la \ + $(top_builddir)/tests/utils/libtap.a + +SCRIPT_LIST = test_ust_elf + +dist_noinst_SCRIPTS = $(SCRIPT_LIST) + +all-local: + @if [ x"$(srcdir)" != x"$(builddir)" ]; then \ + for script in $(SCRIPT_LIST); do \ + cp -f $(srcdir)/$$script $(builddir); \ + done; \ + fi + +clean-local: + @if [ x"$(srcdir)" != x"$(builddir)" ]; then \ + for script in $(SCRIPT_LIST); do \ + rm -f $(builddir)/$$script; \ + done; \ + fi diff --git a/tests/ust-elf/README.md b/tests/ust-elf/README.md new file mode 100644 index 00000000..787af8c7 --- /dev/null +++ b/tests/ust-elf/README.md @@ -0,0 +1,42 @@ +lttng_ust_elf unit tests +======================== + +This is a series of unit tests for LTTng UST's ELF parser. The parser +is used to retrieve memory size, build ID, and debug link information +from ELF objects (standalone executable or shared object) for base +address statedump or dl events. + +The parser should technically be able to read ELF files from any 32- +or 64-bit architecture, little- or big- endian. + +However, to restrict the total amount of testing done, only 4 +architectures representing all combinations of bitness and endianness +are currently tested: + +* x86 +* x86_64 +* armeb +* aarch64_be + +For each architecture, there is a corresponding subdirectory under +`data`, and each of these directories contains exactly 2 files, +`main.elf` and `main.elf.debug`. + +The ELF files are generated from the trivial `main.c` program found in +`data/`, using GNU toolchains. The program contains a static array in +order to ensure the creation of a `.bss` section in the ELF file, +which is one of the multiple factors leading to different file and +in-memory size. + +The program is compiled with `gcc -g main.c -o main.elf`. On certain +architectures, it is necessary to explicitly specify the +`-Wl,--build-id=sha1` flags to include a build ID in the resulting +executable. + +The debug information bundled in `main.elf` is then copied into +`main.elf.debug` and stripped, and a debug link pointing to this file +is added to the executable. The commands used are as follow: + + $ objcopy --only-keep-debug main.elf main.elf.debug + $ strip -g main.elf + $ objcopy --add-gnu-debuglink=main.elf.debug main.elf diff --git a/tests/ust-elf/data/aarch64_be/main.elf b/tests/ust-elf/data/aarch64_be/main.elf new file mode 100644 index 00000000..c30502e8 Binary files /dev/null and b/tests/ust-elf/data/aarch64_be/main.elf differ diff --git a/tests/ust-elf/data/aarch64_be/main.elf.debug b/tests/ust-elf/data/aarch64_be/main.elf.debug new file mode 100755 index 00000000..c48fc19f Binary files /dev/null and b/tests/ust-elf/data/aarch64_be/main.elf.debug differ diff --git a/tests/ust-elf/data/armeb/main.elf b/tests/ust-elf/data/armeb/main.elf new file mode 100644 index 00000000..10e470c7 Binary files /dev/null and b/tests/ust-elf/data/armeb/main.elf differ diff --git a/tests/ust-elf/data/armeb/main.elf.debug b/tests/ust-elf/data/armeb/main.elf.debug new file mode 100755 index 00000000..0971af30 Binary files /dev/null and b/tests/ust-elf/data/armeb/main.elf.debug differ diff --git a/tests/ust-elf/data/main.c b/tests/ust-elf/data/main.c new file mode 100644 index 00000000..d756a506 --- /dev/null +++ b/tests/ust-elf/data/main.c @@ -0,0 +1,6 @@ +int main() +{ + char buf[100]; + + return 0; +} diff --git a/tests/ust-elf/data/x86/main.elf b/tests/ust-elf/data/x86/main.elf new file mode 100644 index 00000000..30f77466 Binary files /dev/null and b/tests/ust-elf/data/x86/main.elf differ diff --git a/tests/ust-elf/data/x86/main.elf.debug b/tests/ust-elf/data/x86/main.elf.debug new file mode 100755 index 00000000..ea0aafcc Binary files /dev/null and b/tests/ust-elf/data/x86/main.elf.debug differ diff --git a/tests/ust-elf/data/x86_64/main.elf b/tests/ust-elf/data/x86_64/main.elf new file mode 100644 index 00000000..341e4a22 Binary files /dev/null and b/tests/ust-elf/data/x86_64/main.elf differ diff --git a/tests/ust-elf/data/x86_64/main.elf.debug b/tests/ust-elf/data/x86_64/main.elf.debug new file mode 100755 index 00000000..275f2cae Binary files /dev/null and b/tests/ust-elf/data/x86_64/main.elf.debug differ diff --git a/tests/ust-elf/prog.c b/tests/ust-elf/prog.c new file mode 100644 index 00000000..bc36b321 --- /dev/null +++ b/tests/ust-elf/prog.c @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2015 Antoine Busque + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include + +#include +#include "tap.h" + +#define NUM_ARCH 4 +#define NUM_TESTS_PER_ARCH 11 +#define NUM_TESTS (NUM_ARCH * NUM_TESTS_PER_ARCH) + +/* + * Expected memsz were computed using libelf, build ID and debug link + * were determined through readelf. + */ +#define X86_MEMSZ 8192 +#define X86_64_MEMSZ 4194304 +#define ARMEB_MEMSZ 65536 +#define AARCH64_BE_MEMSZ 131072 + +#define X86_CRC 0x1531f73c +#define X86_64_CRC 0xa048a98f +#define ARMEB_CRC 0x9d40261b +#define AARCH64_BE_CRC 0x2b8cedce + +#define BUILD_ID_LEN 20 +#define DBG_FILE "main.elf.debug" + +static +void test_elf(const char *test_dir, const char *arch, uint64_t exp_memsz, + uint8_t *exp_build_id, uint32_t exp_crc) +{ + char path[PATH_MAX]; + struct lttng_ust_elf *elf = NULL; + int ret = 0; + uint64_t memsz = 0; + int has_build_id = 0; + uint8_t *build_id = NULL; + size_t build_id_len = 0; + int has_debug_link = 0; + char *dbg_file = NULL; + uint32_t crc = 0; + + diag("Testing %s support", arch); + + snprintf(path, PATH_MAX, "%s/data/%s/main.elf", test_dir, arch); + elf = lttng_ust_elf_create(path); + ok(elf != NULL, "lttng_ust_elf_create"); + + ret = lttng_ust_elf_get_memsz(elf, &memsz); + ok(ret == 0, "lttng_ust_elf_get_memsz returned successfully"); + ok(memsz == exp_memsz, + "memsz - expected: %lu, got: %lu", + exp_memsz, memsz); + + ret = lttng_ust_elf_get_build_id(elf, &build_id, &build_id_len, + &has_build_id); + ok(ret == 0, "lttng_ust_elf_get_build_id returned successfully"); + ok(has_build_id == 1, "build id marked as found"); + ok(build_id_len == BUILD_ID_LEN, + "build_id_len - expected: %u, got: %u", + BUILD_ID_LEN, build_id_len); + ok(memcmp(build_id, exp_build_id, build_id_len) == 0, + "build_id has expected value"); + + ret = lttng_ust_elf_get_debug_link(elf, &dbg_file, &crc, + &has_debug_link); + ok(ret == 0, "lttng_ust_elf_get_debug_link returned successfully"); + ok(has_debug_link == 1, "debug link marked as found"); + ok(strcmp(dbg_file, DBG_FILE) == 0, + "debug link filename - expected: %s, got: %s", + DBG_FILE, dbg_file); + ok(crc == exp_crc, + "debug link crc - expected: %#x, got: %#x", + exp_crc, crc); + + free(build_id); + free(dbg_file); + lttng_ust_elf_destroy(elf); +} + +int main(int argc, char **argv) +{ + uint8_t X86_BUILD_ID[BUILD_ID_LEN] = { + 0x27, 0x79, 0x2a, 0xe7, 0xaa, 0xef, 0x72, 0x5c, 0x9c, 0x52, + 0x80, 0xec, 0x1e, 0x18, 0xd8, 0x09, 0x02, 0xba, 0xbc, 0x82 + }; + uint8_t X86_64_BUILD_ID[BUILD_ID_LEN] = { + 0x0f, 0x87, 0xb2, 0xe2, 0x24, 0x9c, 0xe1, 0xc2, 0x24, 0xb1, + 0xf8, 0xb6, 0x65, 0x83, 0xa3, 0xc1, 0xcb, 0x30, 0x5c, 0x63 + }; + uint8_t ARMEB_BUILD_ID[BUILD_ID_LEN] = { + 0x60, 0x5d, 0x26, 0xa0, 0x0e, 0x30, 0xa4, 0x29, 0xf4, 0xf1, + 0x85, 0x53, 0xda, 0x90, 0x68, 0xe1, 0xf5, 0x67, 0xbe, 0x42 + }; + uint8_t AARCH64_BE_BUILD_ID[BUILD_ID_LEN] = { + 0xb9, 0x0a, 0xa0, 0xed, 0xd1, 0x41, 0x42, 0xc3, 0x34, 0x85, + 0xfa, 0x27, 0x2e, 0xa9, 0x2f, 0xd2, 0xe4, 0xf7, 0xb6, 0x60 + }; + const char *TEST_DIR = argv[1]; + + plan_tests(NUM_TESTS); + + test_elf(TEST_DIR, "x86", X86_MEMSZ, X86_BUILD_ID, X86_CRC); + test_elf(TEST_DIR, "x86_64", X86_64_MEMSZ, X86_64_BUILD_ID, X86_64_CRC); + test_elf(TEST_DIR, "armeb", ARMEB_MEMSZ, ARMEB_BUILD_ID, ARMEB_CRC); + test_elf(TEST_DIR, "aarch64_be", AARCH64_BE_MEMSZ, AARCH64_BE_BUILD_ID, + AARCH64_BE_CRC); + + return 0; +} diff --git a/tests/ust-elf/test_ust_elf b/tests/ust-elf/test_ust_elf new file mode 100755 index 00000000..cb7c7a94 --- /dev/null +++ b/tests/ust-elf/test_ust_elf @@ -0,0 +1,4 @@ +#!/bin/bash + +TEST_DIR=$(dirname $0) +./${TEST_DIR}/prog $TEST_DIR