xref: /freebsd/lib/csu/tests/fini_test.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 
36 #include <errno.h>
37 #include <stdbool.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 
41 #include <atf-c.h>
42 
43 #include <crt.h>
44 
45 extern bool run_dtors_test;
46 extern bool run_fini_array_test;
47 void dso_handle_check(void);
48 
49 
50 #ifndef DSO_BASE
51 typedef void (*func_ptr)(void);
52 
53 bool run_dtors_test = false;
54 bool run_fini_array_test = false;
55 
56 static void
57 dtors_handler(void)
58 {
59 
60 	if (run_dtors_test)
61 		_exit(1);
62 }
63 __section(".dtors") __used static func_ptr dtors_func =
64     &dtors_handler;
65 #endif
66 
67 #ifndef DSO_LIB
68 ATF_TC_WITHOUT_HEAD(dtors_test);
69 ATF_TC_BODY(dtors_test, tc)
70 {
71 	pid_t pid, wpid;
72 	int status;
73 
74 	pid = fork();
75 	switch(pid) {
76 	case -1:
77 		break;
78 	case 0:
79 		run_dtors_test = true;
80 		exit(0);
81 	default:
82 		while ((wpid = waitpid(pid, &status, 0)) == -1 &&
83 		    errno == EINTR)
84 			;
85 #ifdef HAVE_CTORS
86 		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 1,
87 		    ".dtors failed to run");
88 #else
89 		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
90 		    ".dtors incorrectly ran");
91 #endif
92 		break;
93 	}
94 }
95 #endif
96 
97 #ifndef DSO_BASE
98 static void
99 fini_array_handler(void)
100 {
101 
102 	if (run_fini_array_test)
103 		_exit(1);
104 }
105 __section(".fini_array") __used static func_ptr fini_array_func =
106     &fini_array_handler;
107 #endif
108 
109 #ifndef DSO_LIB
110 ATF_TC_WITHOUT_HEAD(fini_array_test);
111 ATF_TC_BODY(fini_array_test, tc)
112 {
113 	pid_t pid, wpid;
114 	int status;
115 
116 	pid = fork();
117 	switch(pid) {
118 	case -1:
119 		break;
120 	case 0:
121 		run_fini_array_test = true;
122 		exit(0);
123 	default:
124 		while ((wpid = waitpid(pid, &status, 0)) == -1 &&
125 		    errno == EINTR)
126 			;
127 		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 1,
128 		    ".fini_array failed to run");
129 		break;
130 	}
131 }
132 #endif
133 
134 #ifndef DSO_BASE
135 extern void *__dso_handle;
136 
137 void
138 dso_handle_check(void)
139 {
140 	void *dso = __dso_handle;
141 
142 #if defined(DSO_LIB) || defined(__PIE__)
143 	ATF_REQUIRE_MSG(dso != NULL,
144 	    "Null __dso_handle in DSO/PIE");
145 #else
146 	ATF_REQUIRE_MSG(dso == NULL,
147 	    "Invalid __dso_handle in non-DSO");
148 #endif
149 }
150 #endif
151 
152 #ifndef DSO_LIB
153 ATF_TC_WITHOUT_HEAD(dso_handle_test);
154 ATF_TC_BODY(dso_handle_test, tc)
155 {
156 
157 	dso_handle_check();
158 }
159 
160 ATF_TP_ADD_TCS(tp)
161 {
162 
163 	ATF_TP_ADD_TC(tp, dtors_test);
164 	ATF_TP_ADD_TC(tp, fini_array_test);
165 	ATF_TP_ADD_TC(tp, dso_handle_test);
166 
167 	return (atf_no_error());
168 }
169 #endif
170