xref: /netbsd/tests/usr.bin/c++/t_pthread_once.sh (revision aed38f3c)
1#	$NetBSD: t_pthread_once.sh,v 1.6 2022/06/12 15:08:38 skrll Exp $
2#
3# Copyright (c) 2018 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28atf_test_case pthread_once
29pthread_once_head() {
30	atf_set "descr" "compile and run std::pthread_once"
31	atf_set "require.progs" "c++"
32}
33
34atf_test_case pthread_once_profile
35pthread_once_profile_head() {
36	atf_set "descr" "compile and run std::pthread_once with profiling option"
37	atf_set "require.progs" "c++"
38}
39
40atf_test_case pthread_once_pic
41pthread_once_pic_head() {
42	atf_set "descr" "compile and run PIC std::pthread_once"
43	atf_set "require.progs" "c++"
44}
45
46atf_test_case pthread_once_pic_32
47pthread_once_pic_32_head() {
48	atf_set "descr" "compile and run 32-bit PIC std::pthread_once"
49	atf_set "require.progs" "c++"
50}
51
52atf_test_case pthread_once_pic_profile
53pthread_once_pic_head() {
54	atf_set "descr" "compile and run PIC std::pthread_once with profiling &flag"
55	atf_set "require.progs" "c++"
56}
57
58atf_test_case pthread_once_pic_profile_32
59pthread_once_pic_profile_32_head() {
60	atf_set "descr" "compile and run 32-bit PIC std::pthread_once with profiling &flag"
61	atf_set "require.progs" "c++"
62}
63
64atf_test_case pthread_once_profile_32
65pthread_once_profile_32_head() {
66	atf_set "descr" "compile and run 32-bit std::pthread_once with profiling &flag"
67	atf_set "require.progs" "c++"
68}
69
70atf_test_case pthread_once_pie
71pthread_once_pie_head() {
72	atf_set "descr" "compile and run position independent (PIE) std::pthread_once"
73	atf_set "require.progs" "c++"
74}
75
76atf_test_case pthread_once_32
77pthread_once_32_head() {
78	atf_set "descr" "compile and run std::pthread_once for/in netbsd32 emulation"
79	atf_set "require.progs" "c++ file diff cat"
80}
81
82atf_test_case pthread_once_static
83pthread_once_static_head() {
84	atf_set "descr" "compile and run std::pthread_once with static &flag"
85	atf_set "require.progs" "c++"
86}
87
88pthread_once_body() {
89	cat > test.cpp << EOF
90#include <cstdio>
91#include <thread>
92int main(void) {
93        pthread_once_t flag = PTHREAD_ONCE_INIT;
94        pthread_once(&flag, [](){ printf("hello, world!\n"); });
95        return 0;
96}
97EOF
98	atf_check -s exit:0 -o ignore -e ignore c++ -o pthread_once test.cpp -pthread
99	atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once
100}
101
102pthread_once_profile_body() {
103	cat > test.cpp << EOF
104#include <cstdio>
105#include <thread>
106int main(void) {
107        pthread_once_t flag = PTHREAD_ONCE_INIT;
108        pthread_once(&flag, [](){ printf("hello, world!\n"); });
109        return 0;
110}
111EOF
112	atf_check -s exit:0 -o ignore -e ignore c++ -static -pg -o pthread_once test.cpp -pthread
113	atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once
114}
115
116pthread_once_profile_32_body() {
117	# check whether this arch is 64bit
118	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
119		atf_skip "this is not a 64 bit architecture"
120	fi
121	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
122		atf_skip "c++ -m32 not supported on this architecture"
123	else
124		if fgrep -q _LP64 ./def32; then
125			atf_fail "c++ -m32 does not generate netbsd32 binaries"
126		fi
127	fi
128
129	cat > test.cpp << EOF
130#include <cstdio>
131#include <thread>
132int main(void) {
133        pthread_once_t flag = PTHREAD_ONCE_INIT;
134        pthread_once(&flag, [](){ printf("hello, world!\n"); });
135        return 0;
136}
137EOF
138	atf_check -s exit:0 -o ignore -e ignore c++ -static -m32 -pg -o pthread_once test.cpp -pthread
139	atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once
140}
141
142pthread_once_pic_body() {
143	cat > test.cpp << EOF
144#include <stdlib.h>
145int callpic(void);
146int main(void) {callpic();exit(0);}
147EOF
148	cat > pic.cpp << EOF
149#include <cstdio>
150#include <thread>
151int callpic(void) {
152        pthread_once_t flag = PTHREAD_ONCE_INIT;
153        pthread_once(&flag, [](){ printf("hello, world!\n"); });
154        return 0;
155}
156EOF
157
158	atf_check -s exit:0 -o ignore -e ignore \
159	    c++ -fPIC -shared -o libtest.so pic.cpp
160	atf_check -s exit:0 -o ignore -e ignore \
161	    c++ -o pthread_once test.cpp -L. -ltest -pthread
162
163	export LD_LIBRARY_PATH=.
164	atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once
165}
166
167pthread_once_pic_32_body() {
168	# check whether this arch is 64bit
169	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
170		atf_skip "this is not a 64 bit architecture"
171	fi
172	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
173		atf_skip "c++ -m32 not supported on this architecture"
174	else
175		if fgrep -q _LP64 ./def32; then
176			atf_fail "c++ -m32 does not generate netbsd32 binaries"
177		fi
178	fi
179
180	cat > test.cpp << EOF
181#include <stdlib.h>
182int callpic(void);
183int main(void) {callpic();exit(0);}
184EOF
185	cat > pic.cpp << EOF
186#include <cstdio>
187#include <thread>
188int callpic(void) {
189        pthread_once_t flag = PTHREAD_ONCE_INIT;
190        pthread_once(&flag, [](){ printf("hello, world!\n"); });
191        return 0;
192}
193EOF
194
195	atf_check -s exit:0 -o ignore -e ignore \
196	    c++ -m32 -fPIC -shared -o libtest.so pic.cpp
197	atf_check -s exit:0 -o ignore -e ignore \
198	    c++ -m32 -o pthread_once test.cpp -L. -ltest -pthread
199
200	export LD_LIBRARY_PATH=.
201	atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once
202}
203
204pthread_once_pic_profile_body() {
205	cat > test.cpp << EOF
206#include <stdlib.h>
207int callpic(void);
208int main(void) {callpic();exit(0);}
209EOF
210	cat > pic.cpp << EOF
211#include <cstdio>
212#include <thread>
213int callpic(void) {
214        pthread_once_t flag = PTHREAD_ONCE_INIT;
215        pthread_once(&flag, [](){ printf("hello, world!\n"); });
216        return 0;
217}
218EOF
219
220	atf_check -s exit:0 -o ignore -e ignore \
221	    c++ -pg -fPIC -shared -o libtest.so pic.cpp
222	atf_check -s exit:0 -o ignore -e ignore \
223	    c++ -pg -o pthread_once test.cpp -L. -ltest -pthread
224
225	export LD_LIBRARY_PATH=.
226	atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once
227}
228
229pthread_once_pic_profile_32_body() {
230	# check whether this arch is 64bit
231	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
232		atf_skip "this is not a 64 bit architecture"
233	fi
234	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
235		atf_skip "c++ -m32 not supported on this architecture"
236	else
237		if fgrep -q _LP64 ./def32; then
238			atf_fail "c++ -m32 does not generate netbsd32 binaries"
239		fi
240	fi
241
242	cat > test.cpp << EOF
243#include <stdlib.h>
244int callpic(void);
245int main(void) {callpic();exit(0);}
246EOF
247	cat > pic.cpp << EOF
248#include <cstdio>
249#include <thread>
250int callpic(void) {
251        pthread_once_t flag = PTHREAD_ONCE_INIT;
252        pthread_once(&flag, [](){ printf("hello, world!\n"); });
253        return 0;
254}
255EOF
256
257	atf_check -s exit:0 -o ignore -e ignore \
258	    c++ -m32 -pg -fPIC -shared -o libtest.so pic.cpp
259	atf_check -s exit:0 -o ignore -e ignore \
260	    c++ -m32 -pg -o pthread_once test.cpp -L. -ltest -pthread
261
262	export LD_LIBRARY_PATH=.
263	atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once
264}
265
266pthread_once_pie_body() {
267	# check whether this arch supports -pie
268	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
269		atf_skip "c++ -pie not supported on this architecture"
270	fi
271	cat > test.cpp << EOF
272#include <cstdio>
273#include <thread>
274int main(void) {
275        pthread_once_t flag = PTHREAD_ONCE_INIT;
276        pthread_once(&flag, [](){ printf("hello, world!\n"); });
277        return 0;
278}
279EOF
280	atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o pthread_once test.cpp -pthread
281	atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once
282}
283
284pthread_once_32_body() {
285	# check whether this arch is 64bit
286	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
287		atf_skip "this is not a 64 bit architecture"
288	fi
289	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
290		atf_skip "c++ -m32 not supported on this architecture"
291	else
292		if fgrep -q _LP64 ./def32; then
293			atf_fail "c++ -m32 does not generate netbsd32 binaries"
294		fi
295	fi
296
297	cat > test.cpp << EOF
298#include <cstdio>
299#include <thread>
300int main(void) {
301        pthread_once_t flag = PTHREAD_ONCE_INIT;
302        pthread_once(&flag, [](){ printf("hello, world!\n"); });
303        return 0;
304}
305EOF
306	atf_check -s exit:0 -o ignore -e ignore c++ -o pthread_once_32 -m32 test.cpp -pthread
307	atf_check -s exit:0 -o ignore -e ignore c++ -o pthread_once_64 test.cpp -pthread
308	file -b ./pthread_once_32 > ./ftype32
309	file -b ./pthread_once_64 > ./ftype64
310	if diff ./ftype32 ./ftype64 >/dev/null; then
311		atf_fail "generated binaries do not differ"
312	fi
313	echo "32bit binaries on this platform are:"
314	cat ./ftype32
315	echo "While native (64bit) binaries are:"
316	cat ./ftype64
317	atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once_32
318
319	# do another test with static 32bit binaries
320	cat > test.cpp << EOF
321#include <cstdio>
322#include <thread>
323int main(void) {
324        pthread_once_t flag = PTHREAD_ONCE_INIT;
325        pthread_once(&flag, [](){ printf("hello, world!\n"); });
326        return 0;
327}
328EOF
329	atf_check -s exit:0 -o ignore -e ignore c++ -o pthread_once -m32 -pthread \
330	    -static test.cpp
331	atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once
332}
333
334pthread_once_static_body() {
335	cat > test.cpp << EOF
336#include <cstdio>
337#include <thread>
338int main(void) {
339        pthread_once_t flag = PTHREAD_ONCE_INIT;
340        pthread_once(&flag, [](){ printf("hello, world!\n"); });
341        return 0;
342}
343EOF
344	atf_check -s exit:0 -o ignore -e ignore c++ -static -o pthread_once test.cpp -pthread
345	atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once
346}
347
348atf_init_test_cases()
349{
350
351	atf_add_test_case pthread_once
352	atf_add_test_case pthread_once_profile
353	atf_add_test_case pthread_once_pic
354	atf_add_test_case pthread_once_pie
355	atf_add_test_case pthread_once_32
356	atf_add_test_case pthread_once_static
357	atf_add_test_case pthread_once_pic_32
358	atf_add_test_case pthread_once_pic_profile
359	atf_add_test_case pthread_once_pic_profile_32
360	atf_add_test_case pthread_once_profile_32
361}
362