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