#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