1# $NetBSD: t_pthread_once.sh,v 1.3 2020/02/11 06:26:19 riastradh 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++ -pg -o pthread_once test.cpp -pthread 113 case `uname -p` in 114 aarch64) 115 atf_expect_fail 'cc -pg is busted on aarch64' 116 ;; 117 esac 118 atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once 119} 120 121pthread_once_profile_32_body() { 122 # check whether this arch is 64bit 123 if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then 124 atf_skip "this is not a 64 bit architecture" 125 fi 126 if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then 127 atf_skip "c++ -m32 not supported on this architecture" 128 else 129 if fgrep -q _LP64 ./def32; then 130 atf_fail "c++ -m32 does not generate netbsd32 binaries" 131 fi 132 fi 133 134 cat > test.cpp << EOF 135#include <cstdio> 136#include <thread> 137int main(void) { 138 pthread_once_t flag = PTHREAD_ONCE_INIT; 139 pthread_once(&flag, [](){ printf("hello, world!\n"); }); 140 return 0; 141} 142EOF 143 atf_check -s exit:0 -o ignore -e ignore c++ -m32 -pg -o pthread_once test.cpp -pthread 144 atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once 145} 146 147pthread_once_pic_body() { 148 cat > test.cpp << EOF 149#include <stdlib.h> 150int callpic(void); 151int main(void) {callpic();exit(0);} 152EOF 153 cat > pic.cpp << EOF 154#include <cstdio> 155#include <thread> 156int callpic(void) { 157 pthread_once_t flag = PTHREAD_ONCE_INIT; 158 pthread_once(&flag, [](){ printf("hello, world!\n"); }); 159 return 0; 160} 161EOF 162 163 atf_check -s exit:0 -o ignore -e ignore \ 164 c++ -fPIC -shared -o libtest.so pic.cpp 165 atf_check -s exit:0 -o ignore -e ignore \ 166 c++ -o pthread_once test.cpp -L. -ltest -pthread 167 168 export LD_LIBRARY_PATH=. 169 atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once 170} 171 172pthread_once_pic_32_body() { 173 # check whether this arch is 64bit 174 if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then 175 atf_skip "this is not a 64 bit architecture" 176 fi 177 if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then 178 atf_skip "c++ -m32 not supported on this architecture" 179 else 180 if fgrep -q _LP64 ./def32; then 181 atf_fail "c++ -m32 does not generate netbsd32 binaries" 182 fi 183 fi 184 185 cat > test.cpp << EOF 186#include <stdlib.h> 187int callpic(void); 188int main(void) {callpic();exit(0);} 189EOF 190 cat > pic.cpp << EOF 191#include <cstdio> 192#include <thread> 193int callpic(void) { 194 pthread_once_t flag = PTHREAD_ONCE_INIT; 195 pthread_once(&flag, [](){ printf("hello, world!\n"); }); 196 return 0; 197} 198EOF 199 200 atf_check -s exit:0 -o ignore -e ignore \ 201 c++ -m32 -fPIC -shared -o libtest.so pic.cpp 202 atf_check -s exit:0 -o ignore -e ignore \ 203 c++ -m32 -o pthread_once test.cpp -L. -ltest -pthread 204 205 export LD_LIBRARY_PATH=. 206 atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once 207} 208 209pthread_once_pic_profile_body() { 210 cat > test.cpp << EOF 211#include <stdlib.h> 212int callpic(void); 213int main(void) {callpic();exit(0);} 214EOF 215 cat > pic.cpp << EOF 216#include <cstdio> 217#include <thread> 218int callpic(void) { 219 pthread_once_t flag = PTHREAD_ONCE_INIT; 220 pthread_once(&flag, [](){ printf("hello, world!\n"); }); 221 return 0; 222} 223EOF 224 225 atf_check -s exit:0 -o ignore -e ignore \ 226 c++ -pg -fPIC -shared -o libtest.so pic.cpp 227 atf_check -s exit:0 -o ignore -e ignore \ 228 c++ -pg -o pthread_once test.cpp -L. -ltest -pthread 229 230 case `uname -p` in 231 aarch64) 232 atf_expect_fail 'cc -pg is busted on aarch64' 233 ;; 234 esac 235 export LD_LIBRARY_PATH=. 236 atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once 237} 238 239pthread_once_pic_profile_32_body() { 240 # check whether this arch is 64bit 241 if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then 242 atf_skip "this is not a 64 bit architecture" 243 fi 244 if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then 245 atf_skip "c++ -m32 not supported on this architecture" 246 else 247 if fgrep -q _LP64 ./def32; then 248 atf_fail "c++ -m32 does not generate netbsd32 binaries" 249 fi 250 fi 251 252 cat > test.cpp << EOF 253#include <stdlib.h> 254int callpic(void); 255int main(void) {callpic();exit(0);} 256EOF 257 cat > pic.cpp << EOF 258#include <cstdio> 259#include <thread> 260int callpic(void) { 261 pthread_once_t flag = PTHREAD_ONCE_INIT; 262 pthread_once(&flag, [](){ printf("hello, world!\n"); }); 263 return 0; 264} 265EOF 266 267 atf_check -s exit:0 -o ignore -e ignore \ 268 c++ -m32 -pg -fPIC -shared -o libtest.so pic.cpp 269 atf_check -s exit:0 -o ignore -e ignore \ 270 c++ -m32 -pg -o pthread_once test.cpp -L. -ltest -pthread 271 272 export LD_LIBRARY_PATH=. 273 atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once 274} 275 276pthread_once_pie_body() { 277 # check whether this arch supports -pie 278 if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 279 atf_skip "c++ -pie not supported on this architecture" 280 fi 281 cat > test.cpp << EOF 282#include <cstdio> 283#include <thread> 284int main(void) { 285 pthread_once_t flag = PTHREAD_ONCE_INIT; 286 pthread_once(&flag, [](){ printf("hello, world!\n"); }); 287 return 0; 288} 289EOF 290 atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o pthread_once test.cpp -pthread 291 atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once 292} 293 294pthread_once_32_body() { 295 # check whether this arch is 64bit 296 if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then 297 atf_skip "this is not a 64 bit architecture" 298 fi 299 if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then 300 atf_skip "c++ -m32 not supported on this architecture" 301 else 302 if fgrep -q _LP64 ./def32; then 303 atf_fail "c++ -m32 does not generate netbsd32 binaries" 304 fi 305 fi 306 307 cat > test.cpp << EOF 308#include <cstdio> 309#include <thread> 310int main(void) { 311 pthread_once_t flag = PTHREAD_ONCE_INIT; 312 pthread_once(&flag, [](){ printf("hello, world!\n"); }); 313 return 0; 314} 315EOF 316 atf_check -s exit:0 -o ignore -e ignore c++ -o pthread_once_32 -m32 test.cpp -pthread 317 atf_check -s exit:0 -o ignore -e ignore c++ -o pthread_once_64 test.cpp -pthread 318 file -b ./pthread_once_32 > ./ftype32 319 file -b ./pthread_once_64 > ./ftype64 320 if diff ./ftype32 ./ftype64 >/dev/null; then 321 atf_fail "generated binaries do not differ" 322 fi 323 echo "32bit binaries on this platform are:" 324 cat ./ftype32 325 echo "While native (64bit) binaries are:" 326 cat ./ftype64 327 atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once_32 328 329 # do another test with static 32bit binaries 330 cat > test.cpp << EOF 331#include <cstdio> 332#include <thread> 333int main(void) { 334 pthread_once_t flag = PTHREAD_ONCE_INIT; 335 pthread_once(&flag, [](){ printf("hello, world!\n"); }); 336 return 0; 337} 338EOF 339 atf_check -s exit:0 -o ignore -e ignore c++ -o pthread_once -m32 -pthread \ 340 -static test.cpp 341 atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once 342} 343 344pthread_once_static_body() { 345 cat > test.cpp << EOF 346#include <cstdio> 347#include <thread> 348int main(void) { 349 pthread_once_t flag = PTHREAD_ONCE_INIT; 350 pthread_once(&flag, [](){ printf("hello, world!\n"); }); 351 return 0; 352} 353EOF 354 atf_check -s exit:0 -o ignore -e ignore c++ -static -o pthread_once test.cpp -pthread 355 atf_check -s exit:0 -o inline:"hello, world!\n" ./pthread_once 356} 357 358atf_init_test_cases() 359{ 360 361 atf_add_test_case pthread_once 362 atf_add_test_case pthread_once_profile 363 atf_add_test_case pthread_once_pic 364 atf_add_test_case pthread_once_pie 365 atf_add_test_case pthread_once_32 366 atf_add_test_case pthread_once_static 367 atf_add_test_case pthread_once_pic_32 368 atf_add_test_case pthread_once_pic_profile 369 atf_add_test_case pthread_once_pic_profile_32 370 atf_add_test_case pthread_once_profile_32 371} 372