diff options
Diffstat (limited to 'libexec/rtld-elf/tests')
22 files changed, 807 insertions, 0 deletions
diff --git a/libexec/rtld-elf/tests/Makefile b/libexec/rtld-elf/tests/Makefile new file mode 100644 index 000000000000..c4b3baab4cb8 --- /dev/null +++ b/libexec/rtld-elf/tests/Makefile @@ -0,0 +1,19 @@ +SUBDIR+= libpythagoras libdeep libval libval2 target +TESTS_SUBDIRS+= rtld_deepbind + +SUBDIR_DEPEND_libdeep= libval2 +SUBDIR_DEPEND_rtld_deepbind= libval +SUBDIR_DEPEND_target= libpythagoras + +ATF_TESTS_C= ld_library_pathfds +ATF_TESTS_C+= ld_preload_fds + +.for t in ${ATF_TESTS_C} +SRCS.$t= $t.c common.c +.endfor + +ATF_TESTS_C+= dlopen_test + +WARNS?= 3 + +.include <bsd.test.mk> diff --git a/libexec/rtld-elf/tests/Makefile.depend b/libexec/rtld-elf/tests/Makefile.depend new file mode 100644 index 000000000000..1af0c88e099c --- /dev/null +++ b/libexec/rtld-elf/tests/Makefile.depend @@ -0,0 +1,17 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + gnu/lib/csu \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/atf/libatf-c \ + lib/libc \ + lib/libcompiler_rt \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/libexec/rtld-elf/tests/Makefile.inc b/libexec/rtld-elf/tests/Makefile.inc new file mode 100644 index 000000000000..3bd0b8590cdc --- /dev/null +++ b/libexec/rtld-elf/tests/Makefile.inc @@ -0,0 +1,3 @@ +PACKAGE?= tests +NO_DEV_PACKAGE= +TESTSDIR?= ${TESTSBASE}/libexec/rtld-elf diff --git a/libexec/rtld-elf/tests/common.c b/libexec/rtld-elf/tests/common.c new file mode 100644 index 000000000000..b878e8284e07 --- /dev/null +++ b/libexec/rtld-elf/tests/common.c @@ -0,0 +1,79 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * Copyright 2014 Jonathan Anderson. + * Copyright 2021 Mariusz Zaborski <oshogbo@FreeBSD.org> + * + * 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 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 <atf-c.h> +#include <fcntl.h> +#include <stdio.h> + +#include "common.h" + +void +expect_success(int binary, char *senv) +{ + char * const env[] = { senv, NULL }; + + try_to_run(binary, 0, env, "the hypotenuse of 3 and 4 is 5\n", ""); +} + +void +expect_missing_library(int binary, char *senv) +{ + char * const env[] = { senv, NULL }; + + try_to_run(binary, 1, env, "", + "ld-elf.so.1: Shared object \"libpythagoras.so.0\" not found," + " required by \"target\"\n"); +} + +void +try_to_run(int binary, int exit_status, char * const *env, + const char *expected_out, const char *expected_err) +{ + pid_t child = atf_utils_fork(); + + if (child == 0) { + char * const args[] = { "target", NULL }; + + fexecve(binary, args, env); + atf_tc_fail("fexecve() failed"); + } + + atf_utils_wait(child, exit_status, expected_out, expected_err); +} + +int +opendir_fd(const char *name) +{ + + return open(name, O_RDONLY | O_DIRECTORY); +} + +int +opendirat(int parent, const char *name) +{ + + return openat(parent, name, O_RDONLY | O_DIRECTORY); +} diff --git a/libexec/rtld-elf/tests/common.h b/libexec/rtld-elf/tests/common.h new file mode 100644 index 000000000000..7465305897d6 --- /dev/null +++ b/libexec/rtld-elf/tests/common.h @@ -0,0 +1,41 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * Copyright 2014 Jonathan Anderson. + * Copyright 2021 Mariusz Zaborski <oshogbo@vexillium.org> + * + * 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 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. + */ + +#ifndef _LD_COMMON_H_ +#define _LD_COMMON_H_ + +#define TARGET_ELF_NAME "target" +#define TARGET_LIBRARY "libpythagoras.so.0" + +void expect_success(int binary, char *senv); +void expect_missing_library(int binary, char *senv); + +void try_to_run(int binary, int expected_exit_status, char * const *env, + const char *expected_out, const char *expected_err); +int opendir_fd(const char *name); +int opendirat(int parent, const char *name); + +#endif /* _LD_COMMON_H_ */ diff --git a/libexec/rtld-elf/tests/dlopen_test.c b/libexec/rtld-elf/tests/dlopen_test.c new file mode 100644 index 000000000000..ab1e8da1cb41 --- /dev/null +++ b/libexec/rtld-elf/tests/dlopen_test.c @@ -0,0 +1,52 @@ +/*- + * + * Copyright (C) 2024 Kyle Evans <kevans@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + * + */ + +#include <dlfcn.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(dlopen_basic); +ATF_TC_BODY(dlopen_basic, tc) +{ + void *hdl, *sym; + + hdl = dlopen("libthr.so", RTLD_NOW); + ATF_REQUIRE(hdl != NULL); + + sym = dlsym(hdl, "pthread_create"); + ATF_REQUIRE(sym != NULL); + + dlclose(hdl); + + sym = dlsym(hdl, "pthread_create"); + ATF_REQUIRE(sym == NULL); +} + +ATF_TC_WITHOUT_HEAD(dlopen_recursing); +ATF_TC_BODY(dlopen_recursing, tc) +{ + void *hdl; + + /* + * If this doesn't crash, we're OK; a regression at one point caused + * some infinite recursion here. + */ + hdl = dlopen("libthr.so", RTLD_NOW | RTLD_GLOBAL); + ATF_REQUIRE(hdl != NULL); + + dlclose(hdl); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, dlopen_basic); + ATF_TP_ADD_TC(tp, dlopen_recursing); + + return atf_no_error(); +} diff --git a/libexec/rtld-elf/tests/ld_library_pathfds.c b/libexec/rtld-elf/tests/ld_library_pathfds.c new file mode 100644 index 000000000000..0d0feb9a0f74 --- /dev/null +++ b/libexec/rtld-elf/tests/ld_library_pathfds.c @@ -0,0 +1,169 @@ +/*- + * Copyright 2014 Jonathan Anderson. + * 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 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 <atf-c.h> +#include <fcntl.h> +#include <stdio.h> + +#include "common.h" + +struct descriptors { + int binary; + int testdir; + int root; + int etc; + int usr; +}; + + +static void setup(struct descriptors *, const atf_tc_t *); + + +ATF_TC_WITHOUT_HEAD(missing_library); +ATF_TC_BODY(missing_library, tc) +{ + struct descriptors files; + + setup(&files, tc); + expect_missing_library(files.binary, NULL); +} + + +ATF_TC_WITHOUT_HEAD(wrong_library_directories); +ATF_TC_BODY(wrong_library_directories, tc) +{ + struct descriptors files; + char *pathfds; + + setup(&files, tc); + ATF_REQUIRE( + asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.etc) > 0); + + expect_missing_library(files.binary, pathfds); +} + + +ATF_TC_WITHOUT_HEAD(bad_library_directories); +ATF_TC_BODY(bad_library_directories, tc) +{ + struct descriptors files; + char *pathfds; + + setup(&files, tc); + ATF_REQUIRE(asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=::") > 0); + + expect_missing_library(files.binary, pathfds); +} + + +ATF_TC_WITHOUT_HEAD(single_library_directory); +ATF_TC_BODY(single_library_directory, tc) +{ + struct descriptors files; + char *pathfds; + + setup(&files, tc); + ATF_REQUIRE( + asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.testdir) > 0); + + expect_success(files.binary, pathfds); +} + + +ATF_TC_WITHOUT_HEAD(first_library_directory); +ATF_TC_BODY(first_library_directory, tc) +{ + struct descriptors files; + char *pathfds; + + setup(&files, tc); + ATF_REQUIRE( + asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d", + files.testdir, files.etc) > 0); + + expect_success(files.binary, pathfds); +} + + +ATF_TC_WITHOUT_HEAD(middle_library_directory); +ATF_TC_BODY(middle_library_directory, tc) +{ + struct descriptors files; + char *pathfds; + + setup(&files, tc); + ATF_REQUIRE( + asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d:%d", + files.root, files.testdir, files.usr) > 0); + + expect_success(files.binary, pathfds); +} + + +ATF_TC_WITHOUT_HEAD(last_library_directory); +ATF_TC_BODY(last_library_directory, tc) +{ + struct descriptors files; + char *pathfds; + + setup(&files, tc); + ATF_REQUIRE( + asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d", + files.root, files.testdir) > 0); + + expect_success(files.binary, pathfds); +} + + + +/* Register test cases with ATF. */ +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, missing_library); + ATF_TP_ADD_TC(tp, wrong_library_directories); + ATF_TP_ADD_TC(tp, bad_library_directories); + ATF_TP_ADD_TC(tp, single_library_directory); + ATF_TP_ADD_TC(tp, first_library_directory); + ATF_TP_ADD_TC(tp, middle_library_directory); + ATF_TP_ADD_TC(tp, last_library_directory); + + return atf_no_error(); +} + + +static void +setup(struct descriptors *dp, const atf_tc_t *tc) +{ + + dp->testdir = opendir_fd(atf_tc_get_config_var(tc, "srcdir")); + ATF_REQUIRE(dp->testdir >= 0); + ATF_REQUIRE( + (dp->binary = openat(dp->testdir, TARGET_ELF_NAME, O_RDONLY)) >= 0); + + ATF_REQUIRE((dp->root = opendir_fd("/")) >= 0); + ATF_REQUIRE((dp->etc = opendirat(dp->root, "etc")) >= 0); + ATF_REQUIRE((dp->usr = opendirat(dp->root, "usr")) >= 0); +} + diff --git a/libexec/rtld-elf/tests/ld_preload_fds.c b/libexec/rtld-elf/tests/ld_preload_fds.c new file mode 100644 index 000000000000..ce620c6b1d2a --- /dev/null +++ b/libexec/rtld-elf/tests/ld_preload_fds.c @@ -0,0 +1,106 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * Copyright 2021 Mariusz Zaborski <oshogbo@FreeBSD.org> + * + * 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 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 <atf-c.h> +#include <fcntl.h> +#include <stdio.h> + +#include "common.h" + +int binaryfd; +int libraryfd; + +static void +setup(const atf_tc_t *tc) +{ + int testdir; + + testdir = opendir_fd(atf_tc_get_config_var(tc, "srcdir")); + ATF_REQUIRE(testdir >= 0); + + binaryfd = openat(testdir, TARGET_ELF_NAME, O_RDONLY); + ATF_REQUIRE(binaryfd >= 0); + libraryfd = openat(testdir, TARGET_LIBRARY, O_RDONLY); + ATF_REQUIRE(libraryfd >= 0); + + close(testdir); +} + +ATF_TC_WITHOUT_HEAD(missing_library); +ATF_TC_BODY(missing_library, tc) +{ + + setup(tc); + expect_missing_library(binaryfd, NULL); +} + +ATF_TC_WITHOUT_HEAD(bad_librarys); +ATF_TC_BODY(bad_librarys, tc) +{ + char *senv; + + ATF_REQUIRE(asprintf(&senv, "LD_PRELOAD_FDS=::") > 0); + + setup(tc); + expect_missing_library(binaryfd, senv); +} + +ATF_TC_WITHOUT_HEAD(single_library); +ATF_TC_BODY(single_library, tc) +{ + char *senv; + + setup(tc); + + ATF_REQUIRE( + asprintf(&senv, "LD_PRELOAD_FDS=%d", libraryfd) > 0); + + expect_success(binaryfd, senv); +} + +ATF_TC_WITHOUT_HEAD(two_librarys); +ATF_TC_BODY(two_librarys, tc) +{ + char *senv; + + setup(tc); + + ATF_REQUIRE( + asprintf(&senv, "LD_PRELOAD_FDS=%d:%d", libraryfd, libraryfd) > 0); + + expect_success(binaryfd, senv); +} + +/* Register test cases with ATF. */ +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, missing_library); + ATF_TP_ADD_TC(tp, bad_librarys); + ATF_TP_ADD_TC(tp, single_library); + ATF_TP_ADD_TC(tp, two_librarys); + + return atf_no_error(); +} diff --git a/libexec/rtld-elf/tests/libdeep/Makefile b/libexec/rtld-elf/tests/libdeep/Makefile new file mode 100644 index 000000000000..5b8e47e12291 --- /dev/null +++ b/libexec/rtld-elf/tests/libdeep/Makefile @@ -0,0 +1,14 @@ +SHLIB?= deep +SHLIB_MAJOR= 0 + +LIBDIR= ${TESTSBASE}/libexec/rtld-elf/rtld_deepbind +SHLIBDIR= ${TESTSBASE}/libexec/rtld-elf/rtld_deepbind + +SRCS= libdeep.c + +LIBVAL2= ${.OBJDIR}/../libval2 +LDFLAGS+= -L${LIBVAL2} -Wl,-rpath,'$$ORIGIN' +DPADD+= -lval2 +LDADD+= -lval2 + +.include <bsd.lib.mk> diff --git a/libexec/rtld-elf/tests/libdeep/libdeep.c b/libexec/rtld-elf/tests/libdeep/libdeep.c new file mode 100644 index 000000000000..e570769300cf --- /dev/null +++ b/libexec/rtld-elf/tests/libdeep/libdeep.c @@ -0,0 +1,28 @@ +/*- + * + * Copyright (C) 2023 NetApp, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + * + */ + +#include <stdio.h> + +int get_value(void); +int proxy_get_value(void); +void set_value(int); +void proxy_set_value(int); + +int +proxy_get_value(void) +{ + + return (get_value()); +} + +void +proxy_set_value(int val) +{ + + return (set_value(val)); +} diff --git a/libexec/rtld-elf/tests/libpythagoras/Makefile b/libexec/rtld-elf/tests/libpythagoras/Makefile new file mode 100644 index 000000000000..0e3fa37acf8a --- /dev/null +++ b/libexec/rtld-elf/tests/libpythagoras/Makefile @@ -0,0 +1,13 @@ +.include <bsd.own.mk> + +LIB= pythagoras +SHLIB_MAJOR= 0 + +LIBDIR= ${TESTSBASE}/libexec/rtld-elf +SHLIBDIR= ${TESTSBASE}/libexec/rtld-elf + +SRCS= pythagoras.c + +LIBADD= m + +.include <bsd.lib.mk> diff --git a/libexec/rtld-elf/tests/libpythagoras/Makefile.depend b/libexec/rtld-elf/tests/libpythagoras/Makefile.depend new file mode 100644 index 000000000000..f4be5a66c350 --- /dev/null +++ b/libexec/rtld-elf/tests/libpythagoras/Makefile.depend @@ -0,0 +1,16 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + gnu/lib/csu \ + include \ + lib/${CSU_DIR} \ + lib/libc \ + lib/libcompiler_rt \ + lib/msun \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/libexec/rtld-elf/tests/libpythagoras/pythagoras.c b/libexec/rtld-elf/tests/libpythagoras/pythagoras.c new file mode 100644 index 000000000000..54bc7457c2d6 --- /dev/null +++ b/libexec/rtld-elf/tests/libpythagoras/pythagoras.c @@ -0,0 +1,40 @@ +/*- + * Copyright 2014 Jonathan Anderson. + * 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 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 <errno.h> +#include <math.h> + +#include "pythagoras.h" + +double +pythagorean_theorem(double a, double b) +{ + + if (a <= 0 || b <= 0) { + errno = ERANGE; + return (-1.0); + } + return (sqrt(pow(a, 2) + pow(b, 2))); +} diff --git a/libexec/rtld-elf/tests/libpythagoras/pythagoras.h b/libexec/rtld-elf/tests/libpythagoras/pythagoras.h new file mode 100644 index 000000000000..4c5bcd774011 --- /dev/null +++ b/libexec/rtld-elf/tests/libpythagoras/pythagoras.h @@ -0,0 +1,26 @@ +/*- + * Copyright 2014 Jonathan Anderson. + * 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 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. + */ + +double pythagorean_theorem(double, double); diff --git a/libexec/rtld-elf/tests/libval/Makefile b/libexec/rtld-elf/tests/libval/Makefile new file mode 100644 index 000000000000..913564e9ad0f --- /dev/null +++ b/libexec/rtld-elf/tests/libval/Makefile @@ -0,0 +1,9 @@ +SHLIB?= val +SHLIB_MAJOR= 0 + +LIBDIR= ${TESTSBASE}/libexec/rtld-elf/rtld_deepbind +SHLIBDIR= ${TESTSBASE}/libexec/rtld-elf/rtld_deepbind + +SRCS= libval.c + +.include <bsd.lib.mk> diff --git a/libexec/rtld-elf/tests/libval/libval.c b/libexec/rtld-elf/tests/libval/libval.c new file mode 100644 index 000000000000..97c97a0310a7 --- /dev/null +++ b/libexec/rtld-elf/tests/libval/libval.c @@ -0,0 +1,26 @@ +/*- + * + * Copyright (C) 2023 NetApp, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + * + */ + +static int val; + +int get_value(void); +void set_value(int); + +int +get_value(void) +{ + + return (val); +} + +void +set_value(int nval) +{ + + val = nval; +} diff --git a/libexec/rtld-elf/tests/libval2/Makefile b/libexec/rtld-elf/tests/libval2/Makefile new file mode 100644 index 000000000000..f353a248b1b8 --- /dev/null +++ b/libexec/rtld-elf/tests/libval2/Makefile @@ -0,0 +1,7 @@ +LIBVAL= ${.CURDIR}/../libval + +# Just rebuild libval +.PATH: ${LIBVAL:tA} +SHLIB?= val2 + +.include "${LIBVAL}/Makefile" diff --git a/libexec/rtld-elf/tests/rtld_deepbind/Makefile b/libexec/rtld-elf/tests/rtld_deepbind/Makefile new file mode 100644 index 000000000000..7054a846449d --- /dev/null +++ b/libexec/rtld-elf/tests/rtld_deepbind/Makefile @@ -0,0 +1,9 @@ +TESTSDIR?= ${TESTSBASE}/libexec/rtld-elf/rtld_deepbind +ATF_TESTS_C= rtld_deepbind + +LIBVAL= ${.OBJDIR}/../libval +LDFLAGS.rtld_deepbind+= -L${LIBVAL} -Wl,-rpath,'$$ORIGIN' +DPADD+= -lval +LDADD+= -lval + +.include <bsd.test.mk> diff --git a/libexec/rtld-elf/tests/rtld_deepbind/rtld_deepbind.c b/libexec/rtld-elf/tests/rtld_deepbind/rtld_deepbind.c new file mode 100644 index 000000000000..4fe3c185982a --- /dev/null +++ b/libexec/rtld-elf/tests/rtld_deepbind/rtld_deepbind.c @@ -0,0 +1,65 @@ +/*- + * + * Copyright (C) 2023 NetApp, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + * + */ + +#include <dlfcn.h> + +#include <atf-c.h> + +int get_value(void); +void set_value(int); + +#define APP_VALUE 5 +#define LIB_VALUE 20 + +ATF_TC_WITHOUT_HEAD(deepbind_simple); +ATF_TC_BODY(deepbind_simple, tc) +{ + void *hdl; + void (*proxy_set_value)(int); + int (*proxy_get_value)(void); + int app_value, lib_value; + + set_value(APP_VALUE); + + /* + * libdeep has a dependency on libval2.so, which is a rebuild of + * libval.so that provides get_value() and set_value() for both us and + * the lib. The lib's get_value() and set_value() should bind to the + * versions in libval2 instead of libval with RTLD_DEEPBIND. + */ + hdl = dlopen("$ORIGIN/libdeep.so", RTLD_LAZY | RTLD_DEEPBIND); + ATF_REQUIRE(hdl != NULL); + + proxy_set_value = dlsym(hdl, "proxy_set_value"); + ATF_REQUIRE(proxy_set_value != NULL); + + proxy_get_value = dlsym(hdl, "proxy_get_value"); + ATF_REQUIRE(proxy_get_value != NULL); + + (*proxy_set_value)(LIB_VALUE); + + lib_value = (*proxy_get_value)(); + app_value = get_value(); + + /* + * In the initial implementation or if libdeep.so is *not* linked + * against its own libval2, then these both return the later set + * LIB_VALUE (20) as they bind to the symbol provided by libval and + * use its .bss val. + */ + ATF_REQUIRE_INTEQ(lib_value, LIB_VALUE); + ATF_REQUIRE_INTEQ(app_value, APP_VALUE); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, deepbind_simple); + + return atf_no_error(); +} diff --git a/libexec/rtld-elf/tests/target/Makefile b/libexec/rtld-elf/tests/target/Makefile new file mode 100644 index 000000000000..8069229dedd8 --- /dev/null +++ b/libexec/rtld-elf/tests/target/Makefile @@ -0,0 +1,15 @@ +.include <bsd.own.mk> + +PROG= target +BINDIR= ${TESTSDIR} + +WARNS?= 3 +CFLAGS+= -I${.CURDIR}/../libpythagoras + +LDFLAGS+= -L${.OBJDIR}/../libpythagoras +DPADD+= ${.OBJDIR}/../libpythagoras/libpythagoras.a +LDADD= -lpythagoras + +MAN= + +.include <bsd.prog.mk> diff --git a/libexec/rtld-elf/tests/target/Makefile.depend b/libexec/rtld-elf/tests/target/Makefile.depend new file mode 100644 index 000000000000..a1a93996c771 --- /dev/null +++ b/libexec/rtld-elf/tests/target/Makefile.depend @@ -0,0 +1,16 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + gnu/lib/csu \ + include \ + lib/${CSU_DIR} \ + lib/libc \ + lib/libcompiler_rt \ + libexec/rtld-elf/tests/libpythagoras \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/libexec/rtld-elf/tests/target/target.c b/libexec/rtld-elf/tests/target/target.c new file mode 100644 index 000000000000..8ec19f53c1b5 --- /dev/null +++ b/libexec/rtld-elf/tests/target/target.c @@ -0,0 +1,37 @@ +/*- + * Copyright 2014 Jonathan Anderson. + * 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 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 "pythagoras.h" + +#include <stdio.h> + +int +main(int argc, char *argv[]) +{ + float hypotenuse = pythagorean_theorem(3, 4); + printf("the hypotenuse of 3 and 4 is %d\n", (int) hypotenuse); + + return 0; +} |