xref: /netbsd-src/tests/usr.bin/c++/t_asan_off_by_one.sh (revision 16dce51364ebe8aeafbae46bc5aa167b8115bc45)
1#	$NetBSD: t_asan_off_by_one.sh,v 1.1 2018/04/04 23:53:26 kamil Exp $
2#
3# Copyright (c) 2018 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Siddharth Muralee.
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
31SUPPORT='n'
32test_target() {
33	if uname -m | grep -q "amd64"; then
34		SUPPORT='y'
35	fi
36
37	if uname -m | grep -q "i386"; then
38		SUPPORT='y'
39	fi
40}
41
42atf_test_case off_by_one
43off_by_one_head() {
44	atf_set "descr" "compile and run \"Off by one example\""
45	atf_set "require.progs" "c++ paxctl"
46}
47
48atf_test_case off_by_one_profile
49off_by_one_profile_head() {
50	atf_set "descr" "compile and run \"Off by one example\" with profiling option"
51	atf_set "require.progs" "c++ paxctl"
52}
53
54atf_test_case off_by_one_pic
55off_by_one_pic_head() {
56	atf_set "descr" "compile and run PIC \"Off by one example\""
57	atf_set "require.progs" "c++ paxctl"
58}
59
60atf_test_case off_by_one_pie
61off_by_one_pie_head() {
62	atf_set "descr" "compile and run position independent (PIE) \"Off by one example\""
63	atf_set "require.progs" "c++ paxctl"
64}
65
66atf_test_case off_by_one32
67off_by_one32_head() {
68	atf_set "descr" "compile and run \"Off by one example\" for/in netbsd32 emulation"
69	atf_set "require.progs" "c++ paxctl file diff cat"
70}
71
72atf_test_case target_not_supported
73target_not_supported_head()
74{
75	atf_set "descr" "Test forced skip"
76}
77
78off_by_one_body() {
79	cat > test.cpp << EOF
80#include <stdio.h>
81#include <stdlib.h>
82int foo() {
83	int arr[5];
84	for (int i = 0; i <= 5 ; i++) {
85		arr[i] = 0;
86	}
87}
88int main() {foo(); printf("CHECK\n"); exit(0);}
89EOF
90	c++ -fsanitize=address -o test test.cpp
91	paxctl +a test
92	atf_check -s not-exit:0 -o not-match:"CHECK\n" -e match:"stack-buffer-overflow" ./test
93}
94
95off_by_one_profile_body() {
96	cat > test.cpp << EOF
97#include <stdio.h>
98#include <stdlib.h>
99int foo() {
100	int arr[5];
101	for (int i = 0; i <= 5 ; i++) {
102		arr[i] = 0;
103	}
104}
105int main() {foo(); printf("CHECK\n"); exit(0);}
106EOF
107	c++ -fsanitize=address -o test -pg test.cpp
108	paxctl +a test
109	atf_check -s not-exit:0 -o not-match:"CHECK\n" -e match:"stack-buffer-overflow" ./test
110}
111
112off_by_one_pic_body() {
113	cat > test.cpp << EOF
114#include <stdio.h>
115#include <stdlib.h>
116int foo();
117int main() {foo(); printf("CHECK\n"); exit(0);}
118EOF
119	cat > pic.cpp << EOF
120#include <stdio.h>
121#include <stdlib.h>
122int foo() {
123	int arr[5];
124	for (int i = 0; i <= 5 ; i++) {
125		arr[i] = 0;
126	}
127}
128EOF
129	c++ -fPIC -fsanitize=address -shared -o libtest.so pic.cpp
130	c++ -o test test.cpp -fsanitize=address -L. -ltest
131
132	export LD_LIBRARY_PATH=.
133	atf_check -s not-exit:0 -o not-match:"CHECK\n" -e match:"stack-buffer-overflow" ./test
134}
135
136off_by_one_pie_body() {
137	# check whether this arch supports -pice
138	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
139		atf_set_skip "c++ -pie not supported on this architecture"
140	fi
141	cat > test.cpp << EOF
142#include <stdio.h>
143#include <stdlib.h>
144int foo() {
145	int arr[5];
146	for (int i = 0; i <= 5 ; i++) {
147		arr[i] = 0;
148	}
149}
150int main() {foo(); printf("CHECK\n"); exit(0);}
151EOF
152	c++ -fsanitize=address -o test -fpie -pie test.cpp
153	paxctl +a test
154	atf_check -s not-exit:0 -o not-match:"CHECK\n" -e match:"stack-buffer-overflow" ./test
155}
156
157off_by_one32_body() {
158	# check whether this arch is 64bit
159	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
160		atf_skip "this is not a 64 bit architecture"
161	fi
162	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
163		atf_skip "c++ -m32 not supported on this architecture"
164	else
165		if fgrep -q _LP64 ./def32; then
166		atf_fail "c++ -m32 does not generate netbsd32 binaries"
167	fi
168fi
169
170	cat > test.cpp << EOF
171#include <stdio.h>
172#include <stdlib.h>
173int foo() {
174	int arr[5];
175	for (int i = 0; i <= 5 ; i++) {
176		arr[i] = 0;
177	}
178}
179int main() {foo(); printf("CHECK\n"); exit(0);}
180EOF
181	c++ -fsanitize=address -o obo32 -m32 test.cpp
182	c++ -fsanitize=address -o obo64 test.cpp
183	file -b ./obo32 > ./ftype32
184	file -b ./obo64 > ./ftype64
185	if diff ./ftype32 ./ftype64 >/dev/null; then
186		atf_fail "generated binaries do not differ"
187	fi
188	echo "32bit binaries on this platform are:"
189	cat ./ftype32
190	echo "While native (64bit) binaries are:"
191	cat ./ftype64
192	paxctl +a obo32
193	atf_check -s not-exit:0 -o not-match:"CHECK\n" -e match:"stack-buffer-overflow" ./obo32
194
195# and another test with profile 32bit binaries
196	cat > test.cpp << EOF
197#include <stdio.h>
198#include <stdlib.h>
199int foo() {
200	int arr[5];
201	for (int i = 0; i <= 5 ; i++) {
202		arr[i] = 0;
203	}
204}
205int main() {foo(); printf("CHECK\n"); exit(0);}
206EOF
207	c++ -fsanitize=address -o test -pg test.cpp
208	paxctl +a test
209	atf_check -s not-exit:0 -o not-match:"CHECK\n" -e match:"stack-buffer-overflow" ./test
210}
211
212target_not_supported_body()
213{
214	atf_skip "Target is not supported"
215}
216
217atf_init_test_cases()
218{
219	test_target
220	test $SUPPORT = 'n' && {
221		atf_add_test_case target_not_supported
222		return 0
223	}
224
225	atf_add_test_case off_by_one
226	atf_add_test_case off_by_one_profile
227	atf_add_test_case off_by_one_pic
228	atf_add_test_case off_by_one_pie
229	atf_add_test_case off_by_one32
230	# static option not supported
231	# -static and -fsanitize=address can't be used together for compilation
232	# (gcc version  5.4.0 and clang 7.1) tested on April 2nd 2018.
233}
234