Skip to content

Commit

Permalink
Run the csu tests on a DSO. This builds the tests into a shared library,
Browse files Browse the repository at this point in the history
then runs these from the base test programs. With this we can check
crtbeginS.o and crtendS.o are working as expected.

MFC with:	r339738
Sponsored by:	DARPA, AFRL
  • Loading branch information
zxombie committed Oct 30, 2018
1 parent 6660b31 commit 9e6da10
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 15 deletions.
2 changes: 2 additions & 0 deletions etc/mtree/BSD.tests.dist
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@
csu
dynamic
..
dynamiclib
..
static
..
..
Expand Down
2 changes: 2 additions & 0 deletions lib/csu/tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# $FreeBSD$

SUBDIR= dso
TESTS_SUBDIRS= dynamic
TESTS_SUBDIRS+= dynamiclib
TESTS_SUBDIRS+= static

.include <bsd.test.mk>
17 changes: 15 additions & 2 deletions lib/csu/tests/cxx_constructors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,18 @@ __FBSDID("$FreeBSD$");
#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

static volatile int constructor_run;
static bool run_destructor_test = false;
struct Foo {
Foo() {
constructor_run = 1;
Expand All @@ -53,8 +61,12 @@ struct Foo {
}
};
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)
{
Expand Down Expand Up @@ -90,3 +102,4 @@ ATF_INIT_TEST_CASES(tcs)
ATF_ADD_TEST_CASE(tcs, cxx_constructor);
ATF_ADD_TEST_CASE(tcs, cxx_destructor);
}
#endif
25 changes: 25 additions & 0 deletions lib/csu/tests/dso/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# $FreeBSD$

.PATH: ${.CURDIR:H}
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>
17 changes: 17 additions & 0 deletions lib/csu/tests/dynamiclib/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# $FreeBSD$

.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>
39 changes: 35 additions & 4 deletions lib/csu/tests/fini_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,24 @@ __FBSDID("$FreeBSD$");
#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);

static bool run_dtors_test = false;
static bool run_fini_array_test = false;
bool run_dtors_test = false;
bool run_fini_array_test = false;

static void
dtors_handler(void)
Expand All @@ -57,7 +64,9 @@ dtors_handler(void)
}
__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)
{
Expand Down Expand Up @@ -85,7 +94,9 @@ ATF_TC_BODY(dtors_test, tc)
break;
}
}
#endif

#ifndef DSO_BASE
static void
fini_array_handler(void)
{
Expand All @@ -95,7 +106,9 @@ fini_array_handler(void)
}
__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)
{
Expand All @@ -118,15 +131,32 @@ ATF_TC_BODY(fini_array_test, tc)
break;
}
}
#endif

#ifndef DSO_BASE
extern void *__dso_handle;

void
dso_handle_check(void)
{
void *dso = __dso_handle;

#ifdef DSO_LIB
ATF_REQUIRE_MSG(dso != NULL,
"Null __dso_handle in DSO");
#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)
{

ATF_REQUIRE_MSG(__dso_handle == NULL,
"Invalid __dso_handle in non-DSO");
dso_handle_check();
}

ATF_TP_ADD_TCS(tp)
Expand All @@ -138,3 +168,4 @@ ATF_TP_ADD_TCS(tp)

return (atf_no_error());
}
#endif
53 changes: 44 additions & 9 deletions lib/csu/tests/init_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,36 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#ifndef DSO_LIB
#include <atf-c.h>
#endif

#include <crt.h>

typedef void (*func_ptr)(void);

static volatile int jcr_run;
static const func_ptr *jcr_ptr;
static volatile int ctors_run;
static volatile int preinit_array_run;
static volatile int preinit_array_state = -1;
static volatile int init_array_run;
static volatile int init_array_state = -1;
extern volatile int jcr_run;
extern const func_ptr *jcr_ptr;
extern const void *jcr_func_ptr;
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 jcr_run;
const func_ptr *jcr_ptr;
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;

void _Jv_RegisterClasses(const func_ptr *);

__section(".jcr") __used static func_ptr jcr_func = (func_ptr)1;
__section(".jcr") __used func_ptr static jcr_func = (func_ptr)1;
const void *jcr_func_ptr = &jcr_func;

void
_Jv_RegisterClasses(const func_ptr *jcr)
Expand All @@ -57,16 +70,20 @@ _Jv_RegisterClasses(const func_ptr *jcr)
jcr_run = 1;
jcr_ptr = jcr;
}
#endif

#ifndef DSO_LIB
ATF_TC_WITHOUT_HEAD(jcr_test);
ATF_TC_BODY(jcr_test, tc)
{

ATF_REQUIRE_MSG(jcr_run == 1, ".jcr not run");
ATF_REQUIRE_MSG(jcr_ptr == &jcr_func,
ATF_REQUIRE_MSG(jcr_ptr == jcr_func_ptr,
"Incorrect pointer passed to _Jv_RegisterClasses");
}
#endif

#ifndef DSO_BASE
static void
ctors_handler(void)
{
Expand All @@ -75,7 +92,9 @@ ctors_handler(void)
}
__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)
{
Expand All @@ -86,7 +105,9 @@ ATF_TC_BODY(ctors_test, tc)
ATF_REQUIRE_MSG(ctors_run == 0, ".ctors run");
#endif
}
#endif

#ifndef DSO_BASE
static void
preinit_array_handler(void)
{
Expand All @@ -96,16 +117,25 @@ preinit_array_handler(void)
}
__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)
{
Expand All @@ -115,14 +145,18 @@ init_array_handler(void)
}
__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)
Expand All @@ -135,3 +169,4 @@ ATF_TP_ADD_TCS(tp)

return (atf_no_error());
}
#endif

0 comments on commit 9e6da10

Please sign in to comment.