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