t_ubsan_int_divzero.sh revision 1.2
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 int_divzero
42int_divzero_head() {
43	atf_set "descr" "Test Undefined Behavior for int division with zero"
44	atf_set "require.progs" "cc"
45}
46
47atf_test_case int_divzero_profile
48int_divzero_profile_head() {
49	atf_set "descr" "Test Undefined Behavior for int division with zero with profiling option"
50	atf_set "require.progs" "cc"
51}
52atf_test_case int_divzero_pic
53int_divzero_pic_head() {
54	atf_set "descr" "Test Undefined Behavior for int division with zero with position independent code (PIC) flag"
55	atf_set "require.progs" "cc"
56}
57atf_test_case int_divzero_pie
58int_divzero_pie_head() {
59	atf_set "descr" "Test Undefined Behavior for int division with zero with position independent execution (PIE) flag"
60	atf_set "require.progs" "cc"
61}
62atf_test_case int_divzero32
63int_divzero32_head() {
64	atf_set "descr" "Test Undefined Behavior for int division with zero in NetBSD_32 emulation"
65	atf_set "require.progs" "cc file diff cat"
66}
67
68
69int_divzero_body(){
70	cat > test.c << EOF
71#include <stdio.h>
72#include <stdlib.h>
73int main(int argc, char **argv) {volatile int l = argc; volatile int k = 0; l/= k; return l;}
74EOF
75
76	cc -fsanitize=integer-divide-by-zero -o test test.c 
77	atf_check -s signal:8 -e match:"division by zero" ./test
78}
79
80int_divzero_profile_body(){
81	cat > test.c << EOF
82#include <stdio.h>
83#include <stdlib.h>
84int main(int argc, char **argv) {volatile int l = argc; volatile int k = 0; l/= k; return l;}
85EOF
86
87	cc -fsanitize=integer-divide-by-zero -o test -pg test.c 
88	atf_check -s signal:8 -e match:"division by zero" ./test
89}
90
91int_divzero_pic_body(){
92	cat > test.c << EOF
93#include <stdio.h>
94#include <stdlib.h>
95int help(int);
96int main(int argc, char **argv) {return help(argc);}
97EOF
98
99	cat > pic.c << EOF
100#include <stdlib.h>
101#include <stdio.h>
102int help(int count) {volatile int l = count; volatile int k = 0; return l/k;}
103EOF
104
105	cc -fsanitize=integer-divide-by-zero -fPIC -shared -o libtest.so pic.c
106	cc -o test test.c -fsanitize=integer-divide-by-zero -L. -ltest
107
108	export LD_LIBRARY_PATH=.
109	atf_check -s signal:8 -e match:"division by zero" ./test
110}
111int_divzero_pie_body(){
112	
113	#check whether -pie flag is supported on this architecture
114	if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 
115		atf_set_skip "cc -pie not supported on this architecture"
116	fi
117	cat > test.c << EOF
118#include <stdio.h>
119#include <stdlib.h>
120int main(int argc, char **argv) {volatile int l = argc; int k = 0; l/= k; return l;}
121EOF
122
123	cc -fsanitize=integer-divide-by-zero -o test -fpie -pie test.c 
124	atf_check -s signal:8 -e match:"division by zero" ./test
125}
126
127
128int_divzero32_body(){
129	
130	# check what this architecture is, after all
131	if ! cc -dM -E - < /dev/null | grep -F -q _LP64; then
132		atf_skip "This is not a 64 bit architecture"
133	fi
134	if ! cc -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
135		atf_skip "cc -m32 Not supported on this architecture"
136	else
137		if grep -F -q _LP64 ./def32; then
138		atf_fail "cc -m32 Does not generate NetBSD32 binaries"
139		fi
140	fi
141
142	cat > test.c << EOF
143#include <stdio.h>
144#include <stdlib.h>
145int main(int argc, char **argv) {int l = argc; int k = 0; l/= k; return l;}
146EOF
147
148	cc -fsanitize=integer-divide-by-zero -o md32 -m32 test.c
149	cc -fsanitize=integer-divide-by-zero -o md64 test.c
150	file -b ./md32 > ./ftype32
151	file -b ./md64 > ./ftype64
152	if diff ./ftype32 ./ftype64 >/dev/null; then
153		atf_fail "Generated 32bit binaries do not differ from 64bit ones"
154	fi
155	echo "32bit binaries on this platform are:"
156	cat ./ftype32
157	echo "64bit binaries are on the other hand:"
158	cat ./ftype64
159	atf_check -s signal:8 -e match:"division by zero" ./md32
160
161	# Another test with profile 32bit binaries, just to make sure everything has been thoroughly done
162	cat > test.c << EOF
163#include <stdio.h>
164#include <stdlib.h>
165int main(int argc, char **argv) {int l = argc; int k = 0; l /= k; return l;}
166EOF
167
168	cc -fsanitize=integer-divide-by-zero -pg -m32 -o test test.c
169	atf_check -s signal:8  -e match:"division by zero" ./test
170}
171
172atf_test_case target_not_supported
173target_not_supported_head()
174{
175	atf_set "descr" "Test forced skip"
176}
177
178atf_init_test_cases()
179{
180	test_target
181	test $SUPPORT = 'n' && {
182		atf_add_test_case target_not_supported
183		return 0
184	}
185	atf_add_test_case int_divzero
186	atf_add_test_case int_divzero_profile
187	atf_add_test_case int_divzero_pie
188	atf_add_test_case int_divzero_pic
189	atf_add_test_case int_divzero32
190}
191