1 # $NetBSD: t_call_once.sh,v 1.7 2025/04/16 01:52:42 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 28 atf_test_case call_once 29 call_once_head() { 30 atf_set "descr" "compile and run std::call_once" 31 atf_set "require.progs" "c++" 32 } 33 34 atf_test_case call_once_profile 35 call_once_profile_head() { 36 atf_set "descr" "compile and run std::call_once with profiling option" 37 atf_set "require.progs" "c++" 38 } 39 40 atf_test_case call_once_pic 41 call_once_pic_head() { 42 atf_set "descr" "compile and run PIC std::call_once" 43 atf_set "require.progs" "c++" 44 } 45 46 atf_test_case call_once_pic_32 47 call_once_pic_32_head() { 48 atf_set "descr" "compile and run 32-bit PIC std::call_once" 49 atf_set "require.progs" "c++" 50 } 51 52 atf_test_case call_once_pic_profile 53 call_once_pic_head() { 54 atf_set "descr" "compile and run PIC std::call_once with profiling flag" 55 atf_set "require.progs" "c++" 56 } 57 58 atf_test_case call_once_pic_profile_32 59 call_once_pic_profile_32_head() { 60 atf_set "descr" "compile and run 32-bit PIC std::call_once with profiling flag" 61 atf_set "require.progs" "c++" 62 } 63 64 atf_test_case call_once_profile_32 65 call_once_profile_32_head() { 66 atf_set "descr" "compile and run 32-bit std::call_once with profiling flag" 67 atf_set "require.progs" "c++" 68 } 69 70 atf_test_case call_once_pie 71 call_once_pie_head() { 72 atf_set "descr" "compile and run position independent (PIE) std::call_once" 73 atf_set "require.progs" "c++" 74 } 75 76 atf_test_case call_once_32 77 call_once_32_head() { 78 atf_set "descr" "compile and run std::call_once for/in netbsd32 emulation" 79 atf_set "require.progs" "c++ file diff cat" 80 } 81 82 atf_test_case call_once_static 83 call_once_static_head() { 84 atf_set "descr" "compile and run std::call_once with static flag" 85 atf_set "require.progs" "c++" 86 } 87 88 call_once_body() { 89 cat > test.cpp << EOF 90 #include <cstdio> 91 #include <thread> 92 #include <mutex> 93 std::once_flag flag; 94 int main(void) { 95 std::call_once(flag, [](){ printf("hello, world!\n"); }); 96 return 0; 97 } 98 EOF 99 atf_check -s exit:0 -o ignore -e ignore c++ -o call_once test.cpp -pthread 100 atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once 101 } 102 103 call_once_profile_body() { 104 case `uname -m` in 105 riscv) atf_expect_fail "PR port-riscv/59301:" \ 106 " riscv: missing MKPROFILE=yes support" 107 ;; 108 esac 109 110 cat > test.cpp << EOF 111 #include <cstdio> 112 #include <thread> 113 #include <mutex> 114 std::once_flag flag; 115 int main(void) { 116 std::call_once(flag, [](){ printf("hello, world!\n"); }); 117 return 0; 118 } 119 EOF 120 atf_check -s exit:0 -o ignore -e ignore c++ -static -pg -o call_once test.cpp -pthread 121 atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once 122 } 123 124 call_once_profile_32_body() { 125 # check whether this arch is 64bit 126 if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then 127 atf_skip "this is not a 64 bit architecture" 128 fi 129 if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then 130 atf_skip "c++ -m32 not supported on this architecture" 131 else 132 if fgrep -q _LP64 ./def32; then 133 atf_fail "c++ -m32 does not generate netbsd32 binaries" 134 fi 135 fi 136 137 case `uname -m` in 138 riscv) atf_expect_fail "PR port-riscv/59301:" \ 139 " riscv: missing MKPROFILE=yes support" 140 ;; 141 esac 142 143 cat > test.cpp << EOF 144 #include <cstdio> 145 #include <thread> 146 #include <mutex> 147 std::once_flag flag; 148 int main(void) { 149 std::call_once(flag, [](){ printf("hello, world!\n"); }); 150 return 0; 151 } 152 EOF 153 atf_check -s exit:0 -o ignore -e ignore c++ -static -m32 -pg -o call_once test.cpp -pthread 154 atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once 155 } 156 157 call_once_pic_body() { 158 cat > test.cpp << EOF 159 #include <stdlib.h> 160 int callpic(void); 161 int main(void) {callpic();exit(0);} 162 EOF 163 cat > pic.cpp << EOF 164 #include <cstdio> 165 #include <thread> 166 #include <mutex> 167 std::once_flag flag; 168 int callpic(void) { 169 std::call_once(flag, [](){ printf("hello, world!\n"); }); 170 return 0; 171 } 172 EOF 173 174 atf_check -s exit:0 -o ignore -e ignore \ 175 c++ -fPIC -shared -o libtest.so pic.cpp 176 atf_check -s exit:0 -o ignore -e ignore \ 177 c++ -o call_once test.cpp -L. -ltest -pthread 178 179 export LD_LIBRARY_PATH=. 180 atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once 181 } 182 183 call_once_pic_32_body() { 184 # check whether this arch is 64bit 185 if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then 186 atf_skip "this is not a 64 bit architecture" 187 fi 188 if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then 189 atf_skip "c++ -m32 not supported on this architecture" 190 else 191 if fgrep -q _LP64 ./def32; then 192 atf_fail "c++ -m32 does not generate netbsd32 binaries" 193 fi 194 fi 195 196 cat > test.cpp << EOF 197 #include <stdlib.h> 198 int callpic(void); 199 int main(void) {callpic();exit(0);} 200 EOF 201 cat > pic.cpp << EOF 202 #include <cstdio> 203 #include <thread> 204 #include <mutex> 205 std::once_flag flag; 206 int callpic(void) { 207 std::call_once(flag, [](){ printf("hello, world!\n"); }); 208 return 0; 209 } 210 EOF 211 212 atf_check -s exit:0 -o ignore -e ignore \ 213 c++ -m32 -fPIC -shared -o libtest.so pic.cpp 214 atf_check -s exit:0 -o ignore -e ignore \ 215 c++ -m32 -o call_once test.cpp -L. -ltest -pthread 216 217 export LD_LIBRARY_PATH=. 218 atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once 219 } 220 221 call_once_pic_profile_body() { 222 case `uname -m` in 223 riscv) atf_expect_fail "PR port-riscv/59301:" \ 224 " riscv: missing MKPROFILE=yes support" 225 ;; 226 esac 227 228 cat > test.cpp << EOF 229 #include <stdlib.h> 230 int callpic(void); 231 int main(void) {callpic();exit(0);} 232 EOF 233 cat > pic.cpp << EOF 234 #include <cstdio> 235 #include <thread> 236 #include <mutex> 237 std::once_flag flag; 238 int callpic(void) { 239 std::call_once(flag, [](){ printf("hello, world!\n"); }); 240 return 0; 241 } 242 EOF 243 244 atf_check -s exit:0 -o ignore -e ignore \ 245 c++ -pg -fPIC -shared -o libtest.so pic.cpp 246 atf_check -s exit:0 -o ignore -e ignore \ 247 c++ -pg -o call_once test.cpp -L. -ltest -pthread 248 249 export LD_LIBRARY_PATH=. 250 atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once 251 } 252 253 call_once_pic_profile_32_body() { 254 # check whether this arch is 64bit 255 if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then 256 atf_skip "this is not a 64 bit architecture" 257 fi 258 if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then 259 atf_skip "c++ -m32 not supported on this architecture" 260 else 261 if fgrep -q _LP64 ./def32; then 262 atf_fail "c++ -m32 does not generate netbsd32 binaries" 263 fi 264 fi 265 266 case `uname -m` in 267 riscv) atf_expect_fail "PR port-riscv/59301:" \ 268 " riscv: missing MKPROFILE=yes support" 269 ;; 270 esac 271 272 cat > test.cpp << EOF 273 #include <stdlib.h> 274 int callpic(void); 275 int main(void) {callpic();exit(0);} 276 EOF 277 cat > pic.cpp << EOF 278 #include <cstdio> 279 #include <thread> 280 #include <mutex> 281 std::once_flag flag; 282 int callpic(void) { 283 std::call_once(flag, [](){ printf("hello, world!\n"); }); 284 return 0; 285 } 286 EOF 287 288 atf_check -s exit:0 -o ignore -e ignore \ 289 c++ -m32 -pg -fPIC -shared -o libtest.so pic.cpp 290 atf_check -s exit:0 -o ignore -e ignore \ 291 c++ -m32 -pg -o call_once test.cpp -L. -ltest -pthread 292 293 export LD_LIBRARY_PATH=. 294 atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once 295 } 296 297 call_once_pie_body() { 298 # check whether this arch supports -pie 299 if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 300 atf_skip "c++ -pie not supported on this architecture" 301 fi 302 cat > test.cpp << EOF 303 #include <cstdio> 304 #include <thread> 305 #include <mutex> 306 std::once_flag flag; 307 int main(void) { 308 std::call_once(flag, [](){ printf("hello, world!\n"); }); 309 return 0; 310 } 311 EOF 312 atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o call_once test.cpp -pthread 313 atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once 314 } 315 316 call_once_32_body() { 317 # check whether this arch is 64bit 318 if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then 319 atf_skip "this is not a 64 bit architecture" 320 fi 321 if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then 322 atf_skip "c++ -m32 not supported on this architecture" 323 else 324 if fgrep -q _LP64 ./def32; then 325 atf_fail "c++ -m32 does not generate netbsd32 binaries" 326 fi 327 fi 328 329 cat > test.cpp << EOF 330 #include <cstdio> 331 #include <thread> 332 #include <mutex> 333 std::once_flag flag; 334 int main(void) { 335 std::call_once(flag, [](){ printf("hello, world!\n"); }); 336 return 0; 337 } 338 EOF 339 atf_check -s exit:0 -o ignore -e ignore c++ -o call_once_32 -m32 test.cpp -pthread 340 atf_check -s exit:0 -o ignore -e ignore c++ -o call_once_64 test.cpp -pthread 341 file -b ./call_once_32 > ./ftype32 342 file -b ./call_once_64 > ./ftype64 343 if diff ./ftype32 ./ftype64 >/dev/null; then 344 atf_fail "generated binaries do not differ" 345 fi 346 echo "32bit binaries on this platform are:" 347 cat ./ftype32 348 echo "While native (64bit) binaries are:" 349 cat ./ftype64 350 atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once_32 351 352 # do another test with static 32bit binaries 353 cat > test.cpp << EOF 354 #include <cstdio> 355 #include <thread> 356 #include <mutex> 357 std::once_flag flag; 358 int main(void) { 359 std::call_once(flag, [](){ printf("hello, world!\n"); }); 360 return 0; 361 } 362 EOF 363 atf_check -s exit:0 -o ignore -e ignore c++ -o call_once -m32 -pthread \ 364 -static test.cpp 365 atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once 366 } 367 368 call_once_static_body() { 369 cat > test.cpp << EOF 370 #include <cstdio> 371 #include <thread> 372 #include <mutex> 373 std::once_flag flag; 374 int main(void) { 375 std::call_once(flag, [](){ printf("hello, world!\n"); }); 376 return 0; 377 } 378 EOF 379 atf_check -s exit:0 -o ignore -e ignore c++ -static -o call_once test.cpp -pthread 380 atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once 381 } 382 383 atf_init_test_cases() 384 { 385 386 atf_add_test_case call_once 387 atf_add_test_case call_once_profile 388 atf_add_test_case call_once_pic 389 atf_add_test_case call_once_pie 390 atf_add_test_case call_once_32 391 atf_add_test_case call_once_static 392 atf_add_test_case call_once_pic_32 393 atf_add_test_case call_once_pic_profile 394 atf_add_test_case call_once_pic_profile_32 395 atf_add_test_case call_once_profile_32 396 } 397