ubsan_common.subr revision 1.2
11.2Sskrll#	$NetBSD: ubsan_common.subr,v 1.2 2022/06/12 08:55:36 skrll Exp $
21.1Smgorny#
31.1Smgorny# Copyright (c) 2018, 2019 The NetBSD Foundation, Inc.
41.1Smgorny# All rights reserved.
51.1Smgorny#
61.1Smgorny# Redistribution and use in source and binary forms, with or without
71.1Smgorny# modification, are permitted provided that the following conditions
81.1Smgorny# are met:
91.1Smgorny# 1. Redistributions of source code must retain the above copyright
101.1Smgorny#    notice, this list of conditions and the following disclaimer.
111.1Smgorny# 2. Redistributions in binary form must reproduce the above copyright
121.1Smgorny#    notice, this list of conditions and the following disclaimer in the
131.1Smgorny#    documentation and/or other materials provided with the distribution.
141.1Smgorny#
151.1Smgorny# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
161.1Smgorny# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
171.1Smgorny# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
181.1Smgorny# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
191.1Smgorny# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
201.1Smgorny# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
211.1Smgorny# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
221.1Smgorny# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
231.1Smgorny# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
241.1Smgorny# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
251.1Smgorny# POSSIBILITY OF SUCH DAMAGE.
261.1Smgorny#
271.1Smgorny
281.1Smgornytest_target()
291.1Smgorny{
301.1Smgorny	SUPPORT='n'
311.1Smgorny	if ! echo __GNUC__ | c++ -E - | grep -q __GNUC__; then
321.1Smgorny		SUPPORT='y'
331.1Smgorny	fi
341.1Smgorny
351.1Smgorny	if ! echo __clang__ | c++ -E - | grep -q __clang__; then
361.1Smgorny		SUPPORT='y'
371.1Smgorny	fi
381.1Smgorny}
391.1Smgorny
401.1Smgornyatf_test_case target_not_supported
411.1Smgornytarget_not_supported_head()
421.1Smgorny{
431.1Smgorny	atf_set "descr" "Test forced skip"
441.1Smgorny}
451.1Smgorny
461.1Smgornytarget_not_supported_body()
471.1Smgorny{
481.1Smgorny	atf_skip "Target is not supported"
491.1Smgorny}
501.1Smgorny
511.1Smgorny# Add a new test case, with head & body.
521.1Smgorny# asan_test_case <test-name> <description> <check-output>
531.1Smgornyubsan_test_case() {
541.1Smgorny	atf_test_case "$1"
551.1Smgorny	eval "$1_head() {
561.1Smgorny		atf_set 'descr' 'Test Undefined Behavior for $2'
571.1Smgorny		atf_set 'require.progs' 'c++'
581.1Smgorny	}"
591.1Smgorny
601.1Smgorny	atf_test_case "$1_profile"
611.1Smgorny	eval "$1_head() {
621.1Smgorny		atf_set 'descr' 'Test Undefined Behavior for $2 with profiling option'
631.1Smgorny		atf_set 'require.progs' 'c++'
641.1Smgorny	}"
651.1Smgorny
661.1Smgorny	atf_test_case "$1_pic"
671.1Smgorny	eval "$1_head() {
681.1Smgorny		atf_set 'descr' 'Test Undefined Behavior for $2 with position independent code (PIC) flag'
691.1Smgorny		atf_set 'require.progs' 'c++'
701.1Smgorny	}"
711.1Smgorny
721.1Smgorny	atf_test_case "$1_pie"
731.1Smgorny	eval "$1_head() {
741.1Smgorny		atf_set 'descr' 'Test Undefined Behavior for $2 with position independent execution (PIE) flag'
751.1Smgorny		atf_set 'require.progs' 'c++'
761.1Smgorny	}"
771.1Smgorny
781.1Smgorny	atf_test_case "${1}32"
791.1Smgorny	eval "$1_head() {
801.1Smgorny		atf_set 'descr' 'Test Undefined Behavior for $2 in NetBSD_32 emulation'
811.1Smgorny		atf_set 'require.progs' 'c++ file diff cat'
821.1Smgorny	}"
831.1Smgorny
841.1Smgorny	eval "$1_body() {
851.1Smgorny		echo \"\$UBSAN_CODE\" > test.cpp
861.1Smgorny		c++ -fsanitize=undefined -o test test.cpp
871.1Smgorny		# note: ignoring exit status due to inconsistency between gcc/clang
881.1Smgorny		# (and between individual tests)
891.1Smgorny		atf_check -s ignore -e match:'$3' ./test
901.1Smgorny	}
911.1Smgorny
921.1Smgorny	$1_profile_body() {
931.1Smgorny		echo \"\$UBSAN_CODE\" > test.cpp
941.2Sskrll		c++ -fsanitize=undefined -static -o test -pg test.cpp
951.1Smgorny		atf_check -s ignore -e match:'$3' ./test
961.1Smgorny	}
971.1Smgorny
981.1Smgorny	$1_pic_body() {
991.1Smgorny		echo \"\$UBSAN_CODE\" > test.cpp
1001.1Smgorny		c++ -DPIC_FOO -fsanitize=undefined -fPIC -shared -o libtest.so test.cpp
1011.1Smgorny		c++ -DPIC_MAIN -o test test.cpp -fsanitize=undefined -L. -ltest
1021.1Smgorny
1031.1Smgorny		export LD_LIBRARY_PATH=.
1041.1Smgorny		atf_check -s ignore -e match:'$3' ./test
1051.1Smgorny	}
1061.1Smgorny
1071.1Smgorny	$1_pie_body() {
1081.1Smgorny		# check whether this arch supports -pice
1091.1Smgorny		if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
1101.1Smgorny			atf_set_skip 'c++ -pie not supported on this architecture'
1111.1Smgorny		fi
1121.1Smgorny		echo \"\$UBSAN_CODE\" > test.cpp
1131.1Smgorny		c++ -fsanitize=undefined -o test -fpie -pie test.cpp
1141.1Smgorny		atf_check -s ignore -e match:'$3' ./test
1151.1Smgorny	}
1161.1Smgorny
1171.1Smgorny	${1}32_body() {
1181.1Smgorny		# check whether this arch is 64bit
1191.1Smgorny		if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
1201.1Smgorny			atf_skip 'this is not a 64 bit architecture'
1211.1Smgorny		fi
1221.1Smgorny		if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
1231.1Smgorny			atf_skip 'c++ -m32 not supported on this architecture'
1241.1Smgorny		else
1251.1Smgorny			if fgrep -q _LP64 ./def32; then
1261.1Smgorny				atf_fail 'c++ -m32 does not generate netbsd32 binaries'
1271.1Smgorny			fi
1281.1Smgorny		fi
1291.1Smgorny
1301.1Smgorny		echo \"\$UBSAN_CODE\" > test.cpp
1311.1Smgorny		c++ -fsanitize=undefined -o df32 -m32 test.cpp
1321.1Smgorny		c++ -fsanitize=undefined -o df64 test.cpp
1331.1Smgorny		file -b ./df32 > ./ftype32
1341.1Smgorny		file -b ./df64 > ./ftype64
1351.1Smgorny		if diff ./ftype32 ./ftype64 >/dev/null; then
1361.1Smgorny			atf_fail 'generated binaries do not differ'
1371.1Smgorny		fi
1381.1Smgorny		echo '32bit binaries on this platform are:'
1391.1Smgorny		cat ./ftype32
1401.1Smgorny		echo 'While native (64bit) binaries are:'
1411.1Smgorny		cat ./ftype64
1421.1Smgorny		atf_check -s ignore -e match:'$3' ./df32
1431.1Smgorny
1441.1Smgorny# and another test with profile 32bit binaries
1451.2Sskrll		c++ -fsanitize=undefined -static -o test -pg -m32 test.cpp
1461.1Smgorny		atf_check -s ignore -e match:'$3' ./test
1471.1Smgorny	}"
1481.1Smgorny}
1491.1Smgorny
1501.1Smgornyubsan_add_test_cases() {
1511.1Smgorny	test_target
1521.1Smgorny	test $SUPPORT = 'n' && {
1531.1Smgorny		atf_add_test_case target_not_supported
1541.1Smgorny		return 0
1551.1Smgorny	}
1561.1Smgorny
1571.1Smgorny	atf_add_test_case "$1"
1581.1Smgorny#	atf_add_test_case "$1_profile"
1591.1Smgorny	atf_add_test_case "$1_pic"
1601.1Smgorny	atf_add_test_case "$1_pie"
1611.1Smgorny#	atf_add_test_case "${1}32"
1621.1Smgorny}
163