t_static_destructor.sh revision 1.1
11.1Skamil#	$NetBSD: t_static_destructor.sh,v 1.1 2017/05/14 02:02:25 kamil Exp $
21.1Skamil#
31.1Skamil# Copyright (c) 2017 The NetBSD Foundation, Inc.
41.1Skamil# All rights reserved.
51.1Skamil#
61.1Skamil# This code is derived from software contributed to The NetBSD Foundation
71.1Skamil# by Kamil Rytarowski.
81.1Skamil#
91.1Skamil# Redistribution and use in source and binary forms, with or without
101.1Skamil# modification, are permitted provided that the following conditions
111.1Skamil# are met:
121.1Skamil# 1. Redistributions of source code must retain the above copyright
131.1Skamil#    notice, this list of conditions and the following disclaimer.
141.1Skamil# 2. Redistributions in binary form must reproduce the above copyright
151.1Skamil#    notice, this list of conditions and the following disclaimer in the
161.1Skamil#    documentation and/or other materials provided with the distribution.
171.1Skamil#
181.1Skamil# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
191.1Skamil# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
201.1Skamil# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
211.1Skamil# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
221.1Skamil# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
231.1Skamil# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
241.1Skamil# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
251.1Skamil# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
261.1Skamil# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
271.1Skamil# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
281.1Skamil# POSSIBILITY OF SUCH DAMAGE.
291.1Skamil#
301.1Skamil
311.1Skamilatf_test_case static_destructor
321.1Skamilstatic_destructor_head() {
331.1Skamil	atf_set "descr" "compile and run \"hello world\""
341.1Skamil	atf_set "require.progs" "c++"
351.1Skamil}
361.1Skamil
371.1Skamilatf_test_case static_destructor_pic
381.1Skamilstatic_destructor_pic_head() {
391.1Skamil	atf_set "descr" "compile and run PIC \"hello world\""
401.1Skamil	atf_set "require.progs" "c++"
411.1Skamil}
421.1Skamil
431.1Skamilatf_test_case static_destructor_pie
441.1Skamilstatic_destructor_pie_head() {
451.1Skamil	atf_set "descr" "compile and run position independent (PIE) \"hello world\""
461.1Skamil	atf_set "require.progs" "c++"
471.1Skamil}
481.1Skamil
491.1Skamilatf_test_case static_destructor32
501.1Skamilstatic_destructor32_head() {
511.1Skamil	atf_set "descr" "compile and run \"hello world\" for/in netbsd32 emulation"
521.1Skamil	atf_set "require.progs" "c++ file diff cat"
531.1Skamil}
541.1Skamil
551.1Skamilstatic_destructor_body() {
561.1Skamil	cat > test.cpp << EOF
571.1Skamil#include <iostream>
581.1Skamilstruct A {
591.1Skamil	int i;
601.1Skamil	A(int i):i(i){std::cout << "CTOR A" << std::endl;}
611.1Skamil	~A() {std::cout << "DTOR A:" << i << std::endl;}
621.1Skamil};
631.1Skamilstruct B {
641.1Skamil	A *m_a;
651.1Skamil	B(){static A s_a(10);m_a=&s_a;std::cout << "CTOR B" << std::endl;}
661.1Skamil	~B(){std::cout << "DTOR B:" << (*m_a).i << std::endl;(*m_a).i = 20;}
671.1Skamil};
681.1Skamilint main(void) {struct B b;return 0;}
691.1SkamilEOF
701.1Skamil	atf_check -s exit:0 -o ignore -e ignore c++ -o hello test.cpp
711.1Skamil	atf_check -s exit:0 -o inline:"CTOR A\nCTOR B\nDTOR B:10\nDTOR A:20\n" ./hello
721.1Skamil}
731.1Skamil
741.1Skamilstatic_destructor_pic_body() {
751.1Skamil	cat > test.cpp << EOF
761.1Skamil#include <cstdlib>
771.1Skamilint callpic(void);
781.1Skamilint main(void) {callpic();exit(0);}
791.1SkamilEOF
801.1Skamil	cat > pic.cpp << EOF
811.1Skamil#include <iostream>
821.1Skamilstruct A {
831.1Skamil	int i;
841.1Skamil	A(int i):i(i){std::cout << "CTOR A" << std::endl;}
851.1Skamil	~A() {std::cout << "DTOR A:" << i << std::endl;}
861.1Skamil};
871.1Skamilstruct B {
881.1Skamil	A *m_a;
891.1Skamil	B(){static A s_a(10);m_a=&s_a;std::cout << "CTOR B" << std::endl;}
901.1Skamil	~B(){std::cout << "DTOR B:" << (*m_a).i << std::endl;(*m_a).i = 20;}
911.1Skamil};
921.1Skamilint callpic(void) {struct B b;}
931.1SkamilEOF
941.1Skamil
951.1Skamil	atf_check -s exit:0 -o ignore -e ignore \
961.1Skamil	    c++ -fPIC -shared -o libtest.so pic.cpp
971.1Skamil	atf_check -s exit:0 -o ignore -e ignore \
981.1Skamil	    c++ -o hello test.cpp -L. -ltest
991.1Skamil
1001.1Skamil	export LD_LIBRARY_PATH=.
1011.1Skamil	atf_check -s exit:0 -o inline:"CTOR A\nCTOR B\nDTOR B:10\nDTOR A:20\n" ./hello
1021.1Skamil}
1031.1Skamil
1041.1Skamilstatic_destructor_pie_body() {
1051.1Skamil	# check whether this arch supports -pie
1061.1Skamil	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
1071.1Skamil		atf_skip "c++ -pie not supported on this architecture"
1081.1Skamil	fi
1091.1Skamil	cat > test.cpp << EOF
1101.1Skamil#include <iostream>
1111.1Skamilstruct A {
1121.1Skamil	int i;
1131.1Skamil	A(int i):i(i){std::cout << "CTOR A" << std::endl;}
1141.1Skamil	~A() {std::cout << "DTOR A:" << i << std::endl;}
1151.1Skamil};
1161.1Skamilstruct B {
1171.1Skamil	A *m_a;
1181.1Skamil	B(){static A s_a(10);m_a=&s_a;std::cout << "CTOR B" << std::endl;}
1191.1Skamil	~B(){std::cout << "DTOR B:" << (*m_a).i << std::endl;(*m_a).i = 20;}
1201.1Skamil};
1211.1Skamilint main(void) {struct B b;return 0;}
1221.1SkamilEOF
1231.1Skamil	atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o hello test.cpp
1241.1Skamil	atf_check -s exit:0 -o inline:"CTOR A\nCTOR B\nDTOR B:10\nDTOR A:20\n" ./hello
1251.1Skamil}
1261.1Skamil
1271.1Skamilstatic_destructor32_body() {
1281.1Skamil	# check whether this arch is 64bit
1291.1Skamil	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
1301.1Skamil		atf_skip "this is not a 64 bit architecture"
1311.1Skamil	fi
1321.1Skamil	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
1331.1Skamil		atf_skip "c++ -m32 not supported on this architecture"
1341.1Skamil	else
1351.1Skamil		if fgrep -q _LP64 ./def32; then
1361.1Skamil			atf_fail "c++ -m32 does not generate netbsd32 binaries"
1371.1Skamil		fi
1381.1Skamil	fi
1391.1Skamil
1401.1Skamil	cat > test.cpp << EOF
1411.1Skamil#include <iostream>
1421.1Skamilstruct A {
1431.1Skamil	int i;
1441.1Skamil	A(int i):i(i){std::cout << "CTOR A" << std::endl;}
1451.1Skamil	~A() {std::cout << "DTOR A:" << i << std::endl;}
1461.1Skamil};
1471.1Skamilstruct B {
1481.1Skamil	A *m_a;
1491.1Skamil	B(){static A s_a(10);m_a=&s_a;std::cout << "CTOR B" << std::endl;}
1501.1Skamil	~B(){std::cout << "DTOR B:" << (*m_a).i << std::endl;(*m_a).i = 20;}
1511.1Skamil};
1521.1Skamilint main(void) {struct B b;return 0;}
1531.1SkamilEOF
1541.1Skamil	atf_check -s exit:0 -o ignore -e ignore c++ -o hello32 -m32 test.cpp
1551.1Skamil	atf_check -s exit:0 -o ignore -e ignore c++ -o hello64 test.cpp
1561.1Skamil	file -b ./hello32 > ./ftype32
1571.1Skamil	file -b ./hello64 > ./ftype64
1581.1Skamil	if diff ./ftype32 ./ftype64 >/dev/null; then
1591.1Skamil		atf_fail "generated binaries do not differ"
1601.1Skamil	fi
1611.1Skamil	echo "32bit binaries on this platform are:"
1621.1Skamil	cat ./ftype32
1631.1Skamil	echo "While native (64bit) binaries are:"
1641.1Skamil	cat ./ftype64
1651.1Skamil	atf_check -s exit:0 -o inline:"CTOR A\nCTOR B\nDTOR B:10\nDTOR A:20\n" ./hello32
1661.1Skamil
1671.1Skamil	# do another test with static 32bit binaries
1681.1Skamil	cat > test.cpp << EOF
1691.1Skamil#include <iostream>
1701.1Skamilstruct A {
1711.1Skamil	int i;
1721.1Skamil	A(int i):i(i){std::cout << "CTOR A" << std::endl;}
1731.1Skamil	~A() {std::cout << "DTOR A:" << i << std::endl;}
1741.1Skamil};
1751.1Skamilstruct B {
1761.1Skamil	A *m_a;
1771.1Skamil	B(){static A s_a(10);m_a=&s_a;std::cout << "CTOR B" << std::endl;}
1781.1Skamil	~B(){std::cout << "DTOR B:" << (*m_a).i << std::endl;(*m_a).i = 20;}
1791.1Skamil};
1801.1Skamilint main(void) {struct B b;return 0;}
1811.1SkamilEOF
1821.1Skamil	atf_check -s exit:0 -o ignore -e ignore c++ -o hello -m32 \
1831.1Skamil	    -static test.cpp
1841.1Skamil	atf_check -s exit:0 -o inline:"CTOR A\nCTOR B\nDTOR B:10\nDTOR A:20\n" ./hello
1851.1Skamil}
1861.1Skamil
1871.1Skamilatf_init_test_cases()
1881.1Skamil{
1891.1Skamil
1901.1Skamil	atf_add_test_case static_destructor
1911.1Skamil	atf_add_test_case static_destructor_pic
1921.1Skamil	atf_add_test_case static_destructor_pie
1931.1Skamil	atf_add_test_case static_destructor32
1941.1Skamil}
195