1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2018 Andrew Turner
5 *
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
8 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/types.h>
33#include <sys/wait.h>
34
35#include <errno.h>
36#include <stdbool.h>
37#include <stdlib.h>
38#include <unistd.h>
39
40#include <atf-c.h>
41
42#include <crt.h>
43
44extern bool run_dtors_test;
45extern bool run_fini_array_test;
46void dso_handle_check(void);
47
48
49#ifndef DSO_BASE
50typedef void (*func_ptr)(void);
51
52bool run_dtors_test = false;
53bool run_fini_array_test = false;
54
55static void
56dtors_handler(void)
57{
58
59	if (run_dtors_test)
60		_exit(1);
61}
62__section(".dtors") __used static func_ptr dtors_func =
63    &dtors_handler;
64#endif
65
66#ifndef DSO_LIB
67ATF_TC_WITHOUT_HEAD(dtors_test);
68ATF_TC_BODY(dtors_test, tc)
69{
70	pid_t pid, wpid;
71	int status;
72
73	pid = fork();
74	switch(pid) {
75	case -1:
76		break;
77	case 0:
78		run_dtors_test = true;
79		exit(0);
80	default:
81		while ((wpid = waitpid(pid, &status, 0)) == -1 &&
82		    errno == EINTR)
83			;
84#ifdef HAVE_CTORS
85		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 1,
86		    ".dtors failed to run");
87#else
88		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
89		    ".dtors incorrectly ran");
90#endif
91		break;
92	}
93}
94#endif
95
96#ifndef DSO_BASE
97static void
98fini_array_handler(void)
99{
100
101	if (run_fini_array_test)
102		_exit(1);
103}
104__section(".fini_array") __used static func_ptr fini_array_func =
105    &fini_array_handler;
106#endif
107
108#ifndef DSO_LIB
109ATF_TC_WITHOUT_HEAD(fini_array_test);
110ATF_TC_BODY(fini_array_test, tc)
111{
112	pid_t pid, wpid;
113	int status;
114
115	pid = fork();
116	switch(pid) {
117	case -1:
118		break;
119	case 0:
120		run_fini_array_test = true;
121		exit(0);
122	default:
123		while ((wpid = waitpid(pid, &status, 0)) == -1 &&
124		    errno == EINTR)
125			;
126		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 1,
127		    ".fini_array failed to run");
128		break;
129	}
130}
131#endif
132
133#ifndef DSO_BASE
134extern void *__dso_handle;
135
136void
137dso_handle_check(void)
138{
139	void *dso = __dso_handle;
140
141#if defined(DSO_LIB) || defined(__PIE__)
142	ATF_REQUIRE_MSG(dso != NULL,
143	    "Null __dso_handle in DSO/PIE");
144#else
145	ATF_REQUIRE_MSG(dso == NULL,
146	    "Invalid __dso_handle in non-DSO");
147#endif
148}
149#endif
150
151#ifndef DSO_LIB
152ATF_TC_WITHOUT_HEAD(dso_handle_test);
153ATF_TC_BODY(dso_handle_test, tc)
154{
155
156	dso_handle_check();
157}
158
159ATF_TP_ADD_TCS(tp)
160{
161
162	ATF_TP_ADD_TC(tp, dtors_test);
163	ATF_TP_ADD_TC(tp, fini_array_test);
164	ATF_TP_ADD_TC(tp, dso_handle_test);
165
166	return (atf_no_error());
167}
168#endif
169