t_static_destructor.sh revision 1.1.2.2 1 # $NetBSD: t_static_destructor.sh,v 1.1.2.2 2017/05/19 00:22:59 pgoyette Exp $
2 #
3 # Copyright (c) 2017 The NetBSD Foundation, Inc.
4 # All rights reserved.
5 #
6 # This code is derived from software contributed to The NetBSD Foundation
7 # by Kamil Rytarowski.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29 #
30
31 atf_test_case static_destructor
32 static_destructor_head() {
33 atf_set "descr" "compile and run \"hello world\""
34 atf_set "require.progs" "c++"
35 }
36
37 atf_test_case static_destructor_pic
38 static_destructor_pic_head() {
39 atf_set "descr" "compile and run PIC \"hello world\""
40 atf_set "require.progs" "c++"
41 }
42
43 atf_test_case static_destructor_pie
44 static_destructor_pie_head() {
45 atf_set "descr" "compile and run position independent (PIE) \"hello world\""
46 atf_set "require.progs" "c++"
47 }
48
49 atf_test_case static_destructor32
50 static_destructor32_head() {
51 atf_set "descr" "compile and run \"hello world\" for/in netbsd32 emulation"
52 atf_set "require.progs" "c++ file diff cat"
53 }
54
55 static_destructor_body() {
56 cat > test.cpp << EOF
57 #include <iostream>
58 struct A {
59 int i;
60 A(int i):i(i){std::cout << "CTOR A" << std::endl;}
61 ~A() {std::cout << "DTOR A:" << i << std::endl;}
62 };
63 struct B {
64 A *m_a;
65 B(){static A s_a(10);m_a=&s_a;std::cout << "CTOR B" << std::endl;}
66 ~B(){std::cout << "DTOR B:" << (*m_a).i << std::endl;(*m_a).i = 20;}
67 };
68 int main(void) {struct B b;return 0;}
69 EOF
70 atf_check -s exit:0 -o ignore -e ignore c++ -o hello test.cpp
71 atf_check -s exit:0 -o inline:"CTOR A\nCTOR B\nDTOR B:10\nDTOR A:20\n" ./hello
72 }
73
74 static_destructor_pic_body() {
75 cat > test.cpp << EOF
76 #include <cstdlib>
77 int callpic(void);
78 int main(void) {callpic();exit(0);}
79 EOF
80 cat > pic.cpp << EOF
81 #include <iostream>
82 struct A {
83 int i;
84 A(int i):i(i){std::cout << "CTOR A" << std::endl;}
85 ~A() {std::cout << "DTOR A:" << i << std::endl;}
86 };
87 struct B {
88 A *m_a;
89 B(){static A s_a(10);m_a=&s_a;std::cout << "CTOR B" << std::endl;}
90 ~B(){std::cout << "DTOR B:" << (*m_a).i << std::endl;(*m_a).i = 20;}
91 };
92 int callpic(void) {struct B b;}
93 EOF
94
95 atf_check -s exit:0 -o ignore -e ignore \
96 c++ -fPIC -shared -o libtest.so pic.cpp
97 atf_check -s exit:0 -o ignore -e ignore \
98 c++ -o hello test.cpp -L. -ltest
99
100 export LD_LIBRARY_PATH=.
101 atf_check -s exit:0 -o inline:"CTOR A\nCTOR B\nDTOR B:10\nDTOR A:20\n" ./hello
102 }
103
104 static_destructor_pie_body() {
105 # check whether this arch supports -pie
106 if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
107 atf_skip "c++ -pie not supported on this architecture"
108 fi
109 cat > test.cpp << EOF
110 #include <iostream>
111 struct A {
112 int i;
113 A(int i):i(i){std::cout << "CTOR A" << std::endl;}
114 ~A() {std::cout << "DTOR A:" << i << std::endl;}
115 };
116 struct B {
117 A *m_a;
118 B(){static A s_a(10);m_a=&s_a;std::cout << "CTOR B" << std::endl;}
119 ~B(){std::cout << "DTOR B:" << (*m_a).i << std::endl;(*m_a).i = 20;}
120 };
121 int main(void) {struct B b;return 0;}
122 EOF
123 atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o hello test.cpp
124 atf_check -s exit:0 -o inline:"CTOR A\nCTOR B\nDTOR B:10\nDTOR A:20\n" ./hello
125 }
126
127 static_destructor32_body() {
128 # check whether this arch is 64bit
129 if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
130 atf_skip "this is not a 64 bit architecture"
131 fi
132 if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
133 atf_skip "c++ -m32 not supported on this architecture"
134 else
135 if fgrep -q _LP64 ./def32; then
136 atf_fail "c++ -m32 does not generate netbsd32 binaries"
137 fi
138 fi
139
140 cat > test.cpp << EOF
141 #include <iostream>
142 struct A {
143 int i;
144 A(int i):i(i){std::cout << "CTOR A" << std::endl;}
145 ~A() {std::cout << "DTOR A:" << i << std::endl;}
146 };
147 struct B {
148 A *m_a;
149 B(){static A s_a(10);m_a=&s_a;std::cout << "CTOR B" << std::endl;}
150 ~B(){std::cout << "DTOR B:" << (*m_a).i << std::endl;(*m_a).i = 20;}
151 };
152 int main(void) {struct B b;return 0;}
153 EOF
154 atf_check -s exit:0 -o ignore -e ignore c++ -o hello32 -m32 test.cpp
155 atf_check -s exit:0 -o ignore -e ignore c++ -o hello64 test.cpp
156 file -b ./hello32 > ./ftype32
157 file -b ./hello64 > ./ftype64
158 if diff ./ftype32 ./ftype64 >/dev/null; then
159 atf_fail "generated binaries do not differ"
160 fi
161 echo "32bit binaries on this platform are:"
162 cat ./ftype32
163 echo "While native (64bit) binaries are:"
164 cat ./ftype64
165 atf_check -s exit:0 -o inline:"CTOR A\nCTOR B\nDTOR B:10\nDTOR A:20\n" ./hello32
166
167 # do another test with static 32bit binaries
168 cat > test.cpp << EOF
169 #include <iostream>
170 struct A {
171 int i;
172 A(int i):i(i){std::cout << "CTOR A" << std::endl;}
173 ~A() {std::cout << "DTOR A:" << i << std::endl;}
174 };
175 struct B {
176 A *m_a;
177 B(){static A s_a(10);m_a=&s_a;std::cout << "CTOR B" << std::endl;}
178 ~B(){std::cout << "DTOR B:" << (*m_a).i << std::endl;(*m_a).i = 20;}
179 };
180 int main(void) {struct B b;return 0;}
181 EOF
182 atf_check -s exit:0 -o ignore -e ignore c++ -o hello -m32 \
183 -static test.cpp
184 atf_check -s exit:0 -o inline:"CTOR A\nCTOR B\nDTOR B:10\nDTOR A:20\n" ./hello
185 }
186
187 atf_init_test_cases()
188 {
189
190 atf_add_test_case static_destructor
191 atf_add_test_case static_destructor_pic
192 atf_add_test_case static_destructor_pie
193 atf_add_test_case static_destructor32
194 }
195