aboutsummaryrefslogtreecommitdiff
path: root/lib/csu/tests
diff options
context:
space:
mode:
Diffstat (limited to 'lib/csu/tests')
-rw-r--r--lib/csu/tests/Makefile12
-rw-r--r--lib/csu/tests/Makefile.inc1
-rw-r--r--lib/csu/tests/Makefile.tests11
-rw-r--r--lib/csu/tests/cxx_constructors.cc102
-rw-r--r--lib/csu/tests/dso/Makefile25
-rw-r--r--lib/csu/tests/dynamic/Makefile7
-rw-r--r--lib/csu/tests/dynamiclib/Makefile15
-rw-r--r--lib/csu/tests/dynamicpie/Makefile7
-rw-r--r--lib/csu/tests/errno/Makefile18
-rw-r--r--lib/csu/tests/errno/errno_test.c23
-rw-r--r--lib/csu/tests/fini_test.c168
-rw-r--r--lib/csu/tests/init_test.c140
-rw-r--r--lib/csu/tests/static/Makefile5
13 files changed, 534 insertions, 0 deletions
diff --git a/lib/csu/tests/Makefile b/lib/csu/tests/Makefile
new file mode 100644
index 000000000000..b76ef590c88f
--- /dev/null
+++ b/lib/csu/tests/Makefile
@@ -0,0 +1,12 @@
+PACKAGE= tests
+
+SUBDIR= dso
+TESTS_SUBDIRS= dynamic
+TESTS_SUBDIRS+= dynamiclib
+TESTS_SUBDIRS+= dynamicpie
+TESTS_SUBDIRS+= errno
+TESTS_SUBDIRS+= static
+
+SUBDIR_DEPEND_dynamiclib=dso
+
+.include <bsd.test.mk>
diff --git a/lib/csu/tests/Makefile.inc b/lib/csu/tests/Makefile.inc
new file mode 100644
index 000000000000..2cb85b8d5d43
--- /dev/null
+++ b/lib/csu/tests/Makefile.inc
@@ -0,0 +1 @@
+TESTSDIR:= ${TESTSBASE}/${RELDIR:C/csu\/tests/csu/}
diff --git a/lib/csu/tests/Makefile.tests b/lib/csu/tests/Makefile.tests
new file mode 100644
index 000000000000..52179057b2a3
--- /dev/null
+++ b/lib/csu/tests/Makefile.tests
@@ -0,0 +1,11 @@
+ATF_TESTS_C+= init_test
+ATF_TESTS_C+= fini_test
+ATF_TESTS_CXX+= cxx_constructors
+
+WARNS?= 3
+
+.if exists(${.CURDIR:H:H}/${MACHINE_ARCH})
+CFLAGS+= -I${.CURDIR:H:H}/${MACHINE_ARCH}
+.else
+CFLAGS+= -I${.CURDIR:H:H}/${MACHINE_CPUARCH}
+.endif
diff --git a/lib/csu/tests/cxx_constructors.cc b/lib/csu/tests/cxx_constructors.cc
new file mode 100644
index 000000000000..3c04053884d2
--- /dev/null
+++ b/lib/csu/tests/cxx_constructors.cc
@@ -0,0 +1,102 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2018 Andrew Turner
+ *
+ * This software was developed by SRI International and the University of
+ * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
+ * ("CTSRD"), as part of the DARPA CRASH research programme.
+ *
+ * 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 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 AUTHOR 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 <sys/wait.h>
+
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#ifndef DSO_LIB
+#include <atf-c++.hpp>
+#endif
+
+extern volatile int constructor_run;
+extern bool run_destructor_test;
+
+#ifndef DSO_BASE
+volatile int constructor_run;
+bool run_destructor_test = false;
+#endif
+
+struct Foo {
+ Foo() {
+ constructor_run = 1;
+ }
+ ~Foo() {
+ if (run_destructor_test)
+ _exit(1);
+ }
+};
+extern Foo foo;
+
+#ifndef DSO_BASE
+Foo foo;
+#endif
+
+#ifndef DSO_LIB
+ATF_TEST_CASE_WITHOUT_HEAD(cxx_constructor);
+ATF_TEST_CASE_BODY(cxx_constructor)
+{
+
+ ATF_REQUIRE(constructor_run == 1);
+}
+
+ATF_TEST_CASE_WITHOUT_HEAD(cxx_destructor);
+ATF_TEST_CASE_BODY(cxx_destructor)
+{
+ pid_t pid, wpid;
+ int status;
+
+ pid = fork();
+ switch(pid) {
+ case -1:
+ break;
+ case 0:
+ run_destructor_test = true;
+ exit(0);
+ default:
+ while ((wpid = waitpid(pid, &status, 0)) == -1 &&
+ errno == EINTR)
+ ;
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+ break;
+ }
+}
+
+ATF_INIT_TEST_CASES(tcs)
+{
+
+ ATF_ADD_TEST_CASE(tcs, cxx_constructor);
+ ATF_ADD_TEST_CASE(tcs, cxx_destructor);
+}
+#endif
diff --git a/lib/csu/tests/dso/Makefile b/lib/csu/tests/dso/Makefile
new file mode 100644
index 000000000000..431168de0328
--- /dev/null
+++ b/lib/csu/tests/dso/Makefile
@@ -0,0 +1,25 @@
+.PATH: ${.CURDIR:H}
+
+PACKAGE= tests
+SHLIB= h_csu
+SHLIB_NAME= libh_csu.so
+SHLIB_MAJOR= 1
+
+WITHOUT_STATIC=
+WITHOUT_PROFILE=
+WITHOUT_PIC=
+
+CFLAGS+= -DDSO_LIB
+
+.include "../Makefile.tests"
+SRCS=
+.for src in ${ATF_TESTS_C}
+SRCS+= ${src}.c
+.endfor
+.for src in ${ATF_TESTS_CXX}
+SRCS+= ${src}.cc
+.endfor
+
+LIBDIR= ${TESTSBASE}/lib/csu/dynamiclib
+
+.include <bsd.lib.mk>
diff --git a/lib/csu/tests/dynamic/Makefile b/lib/csu/tests/dynamic/Makefile
new file mode 100644
index 000000000000..e9e57201cb9a
--- /dev/null
+++ b/lib/csu/tests/dynamic/Makefile
@@ -0,0 +1,7 @@
+.PATH: ${.CURDIR:H}
+
+.include <src.opts.mk>
+MK_PIE= no
+
+.include "../Makefile.tests"
+.include <bsd.test.mk>
diff --git a/lib/csu/tests/dynamiclib/Makefile b/lib/csu/tests/dynamiclib/Makefile
new file mode 100644
index 000000000000..13a9b837684e
--- /dev/null
+++ b/lib/csu/tests/dynamiclib/Makefile
@@ -0,0 +1,15 @@
+.PATH: ${.CURDIR:H}
+CFLAGS+= -DDSO_BASE
+DPADD+= ${.OBJDIR:H}/dso/libh_csu.so
+LDFLAGS+= -Wl,-rpath,${TESTSDIR} -L${.OBJDIR:H}/dso
+LDADD+= -lh_csu
+
+.include "../Makefile.tests"
+
+.for test in ${ATF_TESTS_C}
+ATF_TESTS_CXX+= ${test}
+SRCS.${test}= ${test}.c
+.endfor
+ATF_TESTS_C:=
+
+.include <bsd.test.mk>
diff --git a/lib/csu/tests/dynamicpie/Makefile b/lib/csu/tests/dynamicpie/Makefile
new file mode 100644
index 000000000000..204bef0c7d10
--- /dev/null
+++ b/lib/csu/tests/dynamicpie/Makefile
@@ -0,0 +1,7 @@
+.PATH: ${.CURDIR:H}
+
+.include <src.opts.mk>
+MK_PIE= yes
+
+.include "../Makefile.tests"
+.include <bsd.test.mk>
diff --git a/lib/csu/tests/errno/Makefile b/lib/csu/tests/errno/Makefile
new file mode 100644
index 000000000000..eae54a936294
--- /dev/null
+++ b/lib/csu/tests/errno/Makefile
@@ -0,0 +1,18 @@
+PLAIN_TESTS_C= errno_test \
+ errno_static_test \
+ errno_thr_test \
+ errno_thr_static_test
+
+SRCS.errno_static_test= errno_test.c
+LDFLAGS.errno_static_test= -static
+
+SRCS.errno_thr_test= errno_test.c
+LIBADD.errno_thr_test= pthread
+
+SRCS.errno_thr_static_test= errno_test.c
+LDFLAGS.errno_thr_static_test= -static
+LIBADD.errno_thr_static_test= pthread
+
+MK_PIE:= no
+
+.include <bsd.test.mk>
diff --git a/lib/csu/tests/errno/errno_test.c b/lib/csu/tests/errno/errno_test.c
new file mode 100644
index 000000000000..d190c7fd2959
--- /dev/null
+++ b/lib/csu/tests/errno/errno_test.c
@@ -0,0 +1,23 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 Mark Johnston <markj@FreeBSD.org>
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+
+static void __attribute__((constructor))
+f(void)
+{
+ errno = 42;
+}
+
+int
+main(void)
+{
+ /* errno must be zero upon program startup. */
+ if (errno != 0)
+ exit(1);
+ exit(0);
+}
diff --git a/lib/csu/tests/fini_test.c b/lib/csu/tests/fini_test.c
new file mode 100644
index 000000000000..79dbceb8a37c
--- /dev/null
+++ b/lib/csu/tests/fini_test.c
@@ -0,0 +1,168 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2018 Andrew Turner
+ *
+ * This software was developed by SRI International and the University of
+ * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
+ * ("CTSRD"), as part of the DARPA CRASH research programme.
+ *
+ * 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 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 AUTHOR 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 <sys/wait.h>
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include <crt.h>
+
+extern bool run_dtors_test;
+extern bool run_fini_array_test;
+void dso_handle_check(void);
+
+
+#ifndef DSO_BASE
+typedef void (*func_ptr)(void);
+
+bool run_dtors_test = false;
+bool run_fini_array_test = false;
+
+static void
+dtors_handler(void)
+{
+
+ if (run_dtors_test)
+ _exit(1);
+}
+__section(".dtors") __used static func_ptr dtors_func =
+ &dtors_handler;
+#endif
+
+#ifndef DSO_LIB
+ATF_TC_WITHOUT_HEAD(dtors_test);
+ATF_TC_BODY(dtors_test, tc)
+{
+ pid_t pid, wpid;
+ int status;
+
+ pid = fork();
+ switch(pid) {
+ case -1:
+ break;
+ case 0:
+ run_dtors_test = true;
+ exit(0);
+ default:
+ while ((wpid = waitpid(pid, &status, 0)) == -1 &&
+ errno == EINTR)
+ ;
+#ifdef HAVE_CTORS
+ ATF_REQUIRE_MSG(WEXITSTATUS(status) == 1,
+ ".dtors failed to run");
+#else
+ ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
+ ".dtors incorrectly ran");
+#endif
+ break;
+ }
+}
+#endif
+
+#ifndef DSO_BASE
+static void
+fini_array_handler(void)
+{
+
+ if (run_fini_array_test)
+ _exit(1);
+}
+__section(".fini_array") __used static func_ptr fini_array_func =
+ &fini_array_handler;
+#endif
+
+#ifndef DSO_LIB
+ATF_TC_WITHOUT_HEAD(fini_array_test);
+ATF_TC_BODY(fini_array_test, tc)
+{
+ pid_t pid, wpid;
+ int status;
+
+ pid = fork();
+ switch(pid) {
+ case -1:
+ break;
+ case 0:
+ run_fini_array_test = true;
+ exit(0);
+ default:
+ while ((wpid = waitpid(pid, &status, 0)) == -1 &&
+ errno == EINTR)
+ ;
+ ATF_REQUIRE_MSG(WEXITSTATUS(status) == 1,
+ ".fini_array failed to run");
+ break;
+ }
+}
+#endif
+
+#ifndef DSO_BASE
+extern void *__dso_handle;
+
+void
+dso_handle_check(void)
+{
+ void *dso = __dso_handle;
+
+#if defined(DSO_LIB) || defined(__PIE__)
+ ATF_REQUIRE_MSG(dso != NULL,
+ "Null __dso_handle in DSO/PIE");
+#else
+ ATF_REQUIRE_MSG(dso == NULL,
+ "Invalid __dso_handle in non-DSO");
+#endif
+}
+#endif
+
+#ifndef DSO_LIB
+ATF_TC_WITHOUT_HEAD(dso_handle_test);
+ATF_TC_BODY(dso_handle_test, tc)
+{
+
+ dso_handle_check();
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, dtors_test);
+ ATF_TP_ADD_TC(tp, fini_array_test);
+ ATF_TP_ADD_TC(tp, dso_handle_test);
+
+ return (atf_no_error());
+}
+#endif
diff --git a/lib/csu/tests/init_test.c b/lib/csu/tests/init_test.c
new file mode 100644
index 000000000000..2d4484735f76
--- /dev/null
+++ b/lib/csu/tests/init_test.c
@@ -0,0 +1,140 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2018 Andrew Turner
+ *
+ * This software was developed by SRI International and the University of
+ * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
+ * ("CTSRD"), as part of the DARPA CRASH research programme.
+ *
+ * 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 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 AUTHOR 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/cdefs.h>
+#ifndef DSO_LIB
+#include <atf-c.h>
+#endif
+
+#include <crt.h>
+
+typedef void (*func_ptr)(void);
+
+extern volatile int ctors_run;
+extern volatile int preinit_array_run;
+extern volatile int preinit_array_state;
+extern volatile int init_array_run;
+extern volatile int init_array_state;
+
+#ifndef DSO_BASE
+volatile int ctors_run;
+volatile int preinit_array_run;
+volatile int preinit_array_state = -1;
+volatile int init_array_run;
+volatile int init_array_state = -1;
+#endif
+
+#ifndef DSO_BASE
+static void
+ctors_handler(void)
+{
+
+ ctors_run = 1;
+}
+__section(".ctors") __used static func_ptr ctors_func =
+ &ctors_handler;
+#endif
+
+#ifndef DSO_LIB
+ATF_TC_WITHOUT_HEAD(ctors_test);
+ATF_TC_BODY(ctors_test, tc)
+{
+
+#ifdef HAVE_CTORS
+ ATF_REQUIRE_MSG(ctors_run == 1, ".ctors not run");
+#else
+ ATF_REQUIRE_MSG(ctors_run == 0, ".ctors run");
+#endif
+}
+#endif
+
+#if !defined(DSO_BASE) && !defined(DSO_LIB)
+static void
+preinit_array_handler(void)
+{
+
+ preinit_array_run = 1;
+ preinit_array_state = init_array_run;
+}
+__section(".preinit_array") __used static func_ptr preinit_array_func =
+ &preinit_array_handler;
+#endif
+
+#ifndef DSO_LIB
+ATF_TC_WITHOUT_HEAD(preinit_array_test);
+ATF_TC_BODY(preinit_array_test, tc)
+{
+
+#ifdef DSO_BASE
+ /* Check .preinit_array wasn't run in a DSO */
+ ATF_REQUIRE_MSG(preinit_array_run == 0, ".preinit_array run in DSO");
+#else
+ ATF_REQUIRE_MSG(preinit_array_run == 1, ".preinit_array not run");
+ ATF_REQUIRE_MSG(preinit_array_state == 0,
+ ".preinit_array was not run before .init_array");
+#endif
+}
+#endif
+
+#ifndef DSO_BASE
+static void
+init_array_handler(void)
+{
+
+ init_array_run = 1;
+ init_array_state = preinit_array_run;
+}
+__section(".init_array") __used static func_ptr init_array_func =
+ &init_array_handler;
+#endif
+
+#ifndef DSO_LIB
+ATF_TC_WITHOUT_HEAD(init_array_test);
+ATF_TC_BODY(init_array_test, tc)
+{
+
+ ATF_REQUIRE_MSG(init_array_run == 1, ".init_array not run");
+#ifndef DSO_BASE
+ ATF_REQUIRE_MSG(init_array_state == 1,
+ ".init_array was not run after .preinit_array");
+#endif
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, ctors_test);
+ ATF_TP_ADD_TC(tp, preinit_array_test);
+ ATF_TP_ADD_TC(tp, init_array_test);
+
+ return (atf_no_error());
+}
+#endif
diff --git a/lib/csu/tests/static/Makefile b/lib/csu/tests/static/Makefile
new file mode 100644
index 000000000000..e76c49c93a1a
--- /dev/null
+++ b/lib/csu/tests/static/Makefile
@@ -0,0 +1,5 @@
+.PATH: ${.CURDIR:H}
+NO_SHARED=
+
+.include "../Makefile.tests"
+.include <bsd.test.mk>