t_ubsan_vla_out_of_bounds.sh revision 1.4
1# Copyright (c) 2018 The NetBSD Foundation, Inc.
2# All rights reserved.
3#
4# This code is derived from software contributed to The NetBSD Foundation
5# by Harry Pantazis.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27#
28
29test_target()
30{
31	SUPPORT='n'
32	if ! echo __GNUC__ | cc -E - | grep -q __GNUC__; then
33		SUPPORT='y'
34	fi
35
36	if ! echo __clang__ | cc -E - | grep -q __clang__; then
37		SUPPORT='y'
38	fi
39}
40
41atf_test_case vla_out_of_bounds
42vla_out_of_bounds_head() {
43	atf_set "descr" "Test Undefined Behavior for vla (Variable Length Array) out of bounds"
44	atf_set "require.progs" "cc"
45}
46
47atf_test_case vla_out_of_bounds_profile
48vla_out_of_bounds_profile_head() {
49	atf_set "descr" "Test Undefined Behavior for vla (Variable Length Array) out of bounds with profiling option"
50	atf_set "require.progs" "cc"
51}
52atf_test_case vla_out_of_bounds_pic
53vla_out_of_bounds_pic_head() {
54	atf_set "descr" "Test Undefined Behavior for vla (Variable Length Array) out of bounds with position independent code (PIC) flag"
55	atf_set "require.progs" "cc"
56}
57atf_test_case vla_out_of_bounds_pie
58vla_out_of_bounds_pie_head() {
59	atf_set "descr" "Test Undefined Behavior for vla (Variable Length Array) out of bounds with position independent execution (PIE) flag"
60	atf_set "require.progs" "cc"
61}
62atf_test_case vla_out_of_bounds32
63vla_out_of_bounds32_head() {
64	atf_set "descr" "Test Undefined Behavior for vla (Variable Length Array) out of bounds in NetBSD_32 emulation"
65	atf_set "require.progs" "cc file diff cat"
66}
67
68
69vla_out_of_bounds_body(){
70	cat > test.c << EOF
71#include <stdio.h>
72#include <stdlib.h>
73int main(int argc, char **argv) {volatile int val1 = argc, val2 = argc+1; volatile int arr[val1]; arr[val2] = argc; exit(0);}
74EOF
75
76	cc -fsanitize=undefined -o test test.c
77	atf_check -e match:"out of bounds" ./test
78}
79
80vla_out_of_bounds_profile_body(){
81	cat > test.c << EOF
82#include <stdio.h>
83#include <stdlib.h>
84int main(int argc, char **argv) {volatile int val1 = argc, val2 = argc+1; volatile int arr[val1]; arr[val2] = argc; exit(0);}
85EOF
86
87	cc -fsanitize=undefined -o test -pg test.c
88	atf_check -e match:"out of bounds" ./test
89}
90
91vla_out_of_bounds_pic_body(){
92	cat > test.c << EOF
93#include <stdio.h>
94#include <stdlib.h>
95void help(int);
96int main(int argc, char **argv) {help(argc); exit(0);}
97EOF
98
99	cat > pic.c << EOF
100#include <stdlib.h>
101#include <stdio.h>
102#include <limits.h>
103void help(int count) {volatile int val1 = count, val2 = count+1; volatile int arr[val1]; arr[val2] = count; }
104EOF
105
106	cc -fsanitize=undefined -fPIC -shared -o libtest.so pic.c
107	cc -o test test.c -fsanitize=undefined -L. -ltest
108
109	export LD_LIBRARY_PATH=.
110	atf_check -e match:"out of bounds" ./test
111}
112
113vla_out_of_bounds_pie_body(){
114
115	#check whether -pie flag is supported on this architecture
116	if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
117		atf_set_skip "cc -pie not supported on this architecture"
118	fi
119	cat > test.c << EOF
120#include <stdio.h>
121#include <stdlib.h>
122
123int main(int argc, char **argv) {volatile int val1 = argc, val2 = argc+1; volatile int arr[val1]; arr[val2] = argc; exit(0);}
124EOF
125
126	cc -fsanitize=undefined -o test -fpie -pie test.c
127	atf_check -e match:"out of bounds" ./test
128}
129
130
131vla_out_of_bounds32_body(){
132
133	# check what this architecture is, after all
134	if ! cc -dM -E - < /dev/null | grep -F -q _LP64; then
135		atf_skip "This is not a 64 bit architecture"
136	fi
137	if ! cc -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
138		atf_skip "cc -m32 Not supported on this architecture"
139	else
140		if grep -F -q _LP64 ./def32; then
141		atf_fail "cc -m32 Does not generate NetBSD32 binaries"
142		fi
143	fi
144
145	cat > test.c << EOF
146#include <stdio.h>
147#include <stdlib.h>
148int main(int argc, char **argv) {volatile int val1 = argc, val2 = argc+1; volatile int arr[val1]; arr[val2] = argc; exit(0);}
149EOF
150
151	cc -fsanitize=undefined -o md32 -m32 test.c
152	cc -fsanitize=undefined -o md64 test.c
153	file -b ./md32 > ./ftype32
154	file -b ./md64 > ./ftype64
155	if diff ./ftype32 ./ftype64 >/dev/null; then
156		atf_fail "Generated 32bit binaries do not differ from 64bit ones"
157	fi
158	echo "32bit binaries on this platform are:"
159	cat ./ftype32
160	echo "64bit binaries are on the other hand:"
161	cat ./ftype64
162	atf_check -e match:"out of bounds" ./md32
163
164	# Another test with profile 32bit binaries, just to make sure everything has been thoroughly done
165	cat > test.c << EOF
166#include <stdio.h>
167#include <stdlib.h>
168
169int main(int argc, char **argv) { volatile int val1 = argc, val2 = argc+1; volatile int arr[val1]; arr[val2] = argc; exit(0);}
170EOF
171
172	cc -fsanitize=undefined -pg -m32 -o test test.c
173	atf_check -e match:"out of bounds" ./test
174}
175
176atf_test_case target_not_supported
177target_not_supported_head()
178{
179	atf_set "descr" "Test forced skip"
180}
181
182atf_init_test_cases()
183{
184	test_target
185	test $SUPPORT = 'n' && {
186		atf_add_test_case target_not_supported
187		return 0
188	}
189	atf_add_test_case vla_out_of_bounds
190#	atf_add_test_case vla_out_of_bounds_profile
191	atf_add_test_case vla_out_of_bounds_pie
192	atf_add_test_case vla_out_of_bounds_pic
193#	atf_add_test_case vla_out_of_bounds32
194}
195