Module Name: src Committed By: kamil Date: Tue Sep 22 01:09:33 UTC 2020
Modified Files: src/distrib/sets/lists/debug: mi src/distrib/sets/lists/tests: mi src/tests/libexec/ld.elf_so: Makefile Added Files: src/tests/libexec/ld.elf_so: t_rtld_r_debug.c Log Message: Add new RTLD test file for r_debug New tests: - self - dlopen Both check whether the r_debug structure seems to be well-formed, without and with a dlopen(3) call. To generate a diff of this commit: cvs rdiff -u -r1.337 -r1.338 src/distrib/sets/lists/debug/mi cvs rdiff -u -r1.924 -r1.925 src/distrib/sets/lists/tests/mi cvs rdiff -u -r1.10 -r1.11 src/tests/libexec/ld.elf_so/Makefile cvs rdiff -u -r0 -r1.1 src/tests/libexec/ld.elf_so/t_rtld_r_debug.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/distrib/sets/lists/debug/mi diff -u src/distrib/sets/lists/debug/mi:1.337 src/distrib/sets/lists/debug/mi:1.338 --- src/distrib/sets/lists/debug/mi:1.337 Tue Sep 15 07:00:04 2020 +++ src/distrib/sets/lists/debug/mi Tue Sep 22 01:09:32 2020 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.337 2020/09/15 07:00:04 mrg Exp $ +# $NetBSD: mi,v 1.338 2020/09/22 01:09:32 kamil Exp $ ./etc/mtree/set.debug comp-sys-root ./usr/lib comp-sys-usr compatdir ./usr/lib/i18n/libBIG5_g.a comp-c-debuglib debuglib,compatfile @@ -2359,6 +2359,7 @@ ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_dlerror-false.debug tests-libexec-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_dlinfo.debug tests-libexec-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_dlvsym.debug tests-libexec-debug debug,atf,pic,compattestfile +./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_rtld_r_debug.debug tests-libexec-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/libexec/ld.elf_so/t_ifunc.debug tests-libexec-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/modules/k_helper3.debug tests-sys-debug debug,atf,rump ./usr/libdata/debug/usr/tests/modules/t_builtin.debug tests-sys-debug debug,atf,rump Index: src/distrib/sets/lists/tests/mi diff -u src/distrib/sets/lists/tests/mi:1.924 src/distrib/sets/lists/tests/mi:1.925 --- src/distrib/sets/lists/tests/mi:1.924 Mon Sep 14 06:44:50 2020 +++ src/distrib/sets/lists/tests/mi Tue Sep 22 01:09:33 2020 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.924 2020/09/14 06:44:50 rillig Exp $ +# $NetBSD: mi,v 1.925 2020/09/22 01:09:33 kamil Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -3768,6 +3768,7 @@ ./usr/tests/libexec/ld.elf_so/t_dlinfo tests-libexec-tests compattestfile,atf,pic ./usr/tests/libexec/ld.elf_so/t_dlvsym tests-libexec-tests compattestfile,atf,pic ./usr/tests/libexec/ld.elf_so/t_ifunc tests-libexec-tests compattestfile,atf,pic +./usr/tests/libexec/ld.elf_so/t_rtld_r_debug tests-libexec-tests compattestfile,atf,pic ./usr/tests/libexec/ld.elf_so/t_thread_local_dtor tests-libexec-tests compattestfile,atf,pic ./usr/tests/modules tests-sys-tests compattestfile,atf ./usr/tests/net tests-net-tests compattestfile,atf Index: src/tests/libexec/ld.elf_so/Makefile diff -u src/tests/libexec/ld.elf_so/Makefile:1.10 src/tests/libexec/ld.elf_so/Makefile:1.11 --- src/tests/libexec/ld.elf_so/Makefile:1.10 Tue May 14 19:07:07 2019 +++ src/tests/libexec/ld.elf_so/Makefile Tue Sep 22 01:09:32 2020 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.10 2019/05/14 19:07:07 christos Exp $ +# $NetBSD: Makefile,v 1.11 2020/09/22 01:09:32 kamil Exp $ # NOMAN= # defined @@ -17,6 +17,9 @@ SUBDIR+= helper_dso1 helper_dso3 .WAIT h TESTSDIR= ${TESTSBASE}/libexec/ld.elf_so TESTS_C+= t_dlerror-cleared t_dlerror-false t_dlinfo t_dlvsym t_ifunc +TESTS_C+= t_rtld_r_debug + +COPTS.t_rtld_r_debug.c += ${${ACTIVE_CC} == "gcc" :? -Wno-maybe-uninitialized :} LDADD.t_dlerror-false= -Wl,-rpath,/var/nonexistent/lib LDADD.t_dlvsym= -Wl,-rpath,${TESTSDIR}/h_helper_symver_dso2 Added files: Index: src/tests/libexec/ld.elf_so/t_rtld_r_debug.c diff -u /dev/null src/tests/libexec/ld.elf_so/t_rtld_r_debug.c:1.1 --- /dev/null Tue Sep 22 01:09:33 2020 +++ src/tests/libexec/ld.elf_so/t_rtld_r_debug.c Tue Sep 22 01:09:32 2020 @@ -0,0 +1,164 @@ +/* $NetBSD: t_rtld_r_debug.c,v 1.1 2020/09/22 01:09:32 kamil Exp $ */ + +/* + * Copyright (c) 2020 The NetBSD Foundation, Inc. + * All rights reserved. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND + * CONTRIBUTORS ``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 FOUNDATION OR CONTRIBUTORS 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 <sys/types.h> + +#include <atf-c.h> +#include <dlfcn.h> +#include <link_elf.h> +#include <stdbool.h> + +#include "h_macros.h" + +static long int +getauxval(unsigned int type) +{ + const AuxInfo *aux; + + for (aux = _dlauxinfo(); aux->a_type != AT_NULL; ++aux) { + if (type == aux->a_type) + return aux->a_v; + } + + return 0; +} + +static Elf_Dyn * +get_dynamic_section(void) +{ + uintptr_t relocbase = (uintptr_t)~0U; + const Elf_Phdr *phdr; + Elf_Half phnum; + const Elf_Phdr *phlimit, *dynphdr; + + phdr = (void *)getauxval(AT_PHDR); + phnum = (Elf_Half)getauxval(AT_PHNUM); + + ATF_CHECK(phdr != NULL); + ATF_CHECK(phnum != (Elf_Half)~0); + + phlimit = phdr + phnum; + dynphdr = NULL; + + for (; phdr < phlimit; ++phdr) { + if (phdr->p_type == PT_DYNAMIC) + dynphdr = phdr; + if (phdr->p_type == PT_PHDR) + relocbase = (uintptr_t)phdr - phdr->p_vaddr; + } + + return (Elf_Dyn *)((uint8_t *)dynphdr->p_vaddr + relocbase); +} + +static struct r_debug * +get_rtld_r_debug(void) +{ + struct r_debug *debug; + Elf_Dyn *dynp; + + for (dynp = get_dynamic_section(); dynp->d_tag != DT_NULL; dynp++) { + if (dynp->d_tag == DT_DEBUG) { + debug = (void *)dynp->d_un.d_val; + break; + } + } + ATF_CHECK(debug != NULL); + + return debug; +} + +static void +check_r_debug_return_link_map(const char *name, struct link_map **rmap) +{ + struct r_debug *debug; + struct link_map *map; + void *loader; + bool found; + + loader = NULL; + debug = get_rtld_r_debug(); + ATF_CHECK(debug != NULL); + ATF_CHECK(debug->r_version == R_DEBUG_VERSION); + map = debug->r_map; + ATF_CHECK(map != NULL); + + for (found = false; map; map = map->l_next) { + if (strstr(map->l_name, name) != NULL) { + if (rmap) + *rmap = map; + found = true; + } else if (strstr(map->l_name, "ld.elf_so") != NULL) { + loader = (void *)map->l_addr; + } + } + ATF_CHECK(found); + ATF_CHECK(loader != NULL); + ATF_CHECK(debug->r_brk != NULL); + ATF_CHECK(debug->r_state == RT_CONSISTENT); + ATF_CHECK(debug->r_ldbase == loader); +} + +ATF_TC(self); +ATF_TC_HEAD(self, tc) +{ + atf_tc_set_md_var(tc, "descr", "check whether r_debug is well-formed"); +} +ATF_TC_BODY(self, tc) +{ + check_r_debug_return_link_map("t_rtld_r_debug", NULL); +} + +ATF_TC(dlopen); +ATF_TC_HEAD(dlopen, tc) +{ + atf_tc_set_md_var(tc, "descr", + "check whether r_debug is well-formed after an dlopen(3) call"); +} +ATF_TC_BODY(dlopen, tc) +{ + void *handle; + struct link_map *map, *r_map; + + handle = dlopen("libutil.so", RTLD_LAZY); + ATF_CHECK(handle); + + check_r_debug_return_link_map("libutil.so", &r_map); + + RZ(dlinfo(handle, RTLD_DI_LINKMAP, &map)); + + ATF_CHECK(map == r_map); + dlclose(handle); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, self); + ATF_TP_ADD_TC(tp, dlopen); + return atf_no_error(); +}