asan_common.subr revision 1.3
11.3Sskrll#	$NetBSD: asan_common.subr,v 1.3 2021/10/12 18:40:01 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.1SmgornySUPPORT='n'
291.1Smgornytest_target() {
301.3Sskrll	if uname -p | grep -q "aarch64"; then
311.3Sskrll		SUPPORT='y'
321.3Sskrll	fi
331.3Sskrll
341.1Smgorny	if uname -m | grep -q "amd64"; then
351.1Smgorny		SUPPORT='y'
361.1Smgorny	fi
371.1Smgorny
381.1Smgorny	if uname -m | grep -q "i386"; then
391.1Smgorny		SUPPORT='y'
401.1Smgorny	fi
411.1Smgorny}
421.1Smgorny
431.1Smgornyatf_test_case target_not_supported
441.1Smgornytarget_not_supported_head()
451.1Smgorny{
461.1Smgorny	atf_set "descr" "Test forced skip"
471.1Smgorny}
481.1Smgorny
491.1Smgornytarget_not_supported_body()
501.1Smgorny{
511.1Smgorny	atf_skip "Target is not supported"
521.1Smgorny}
531.1Smgorny
541.1Smgorny# Add a new test case, with head & body.
551.1Smgorny# asan_test_case <test-name> <description> <check-output>
561.1Smgornyasan_test_case() {
571.1Smgorny	atf_test_case "$1"
581.1Smgorny	eval "$1_head() {
591.1Smgorny		atf_set 'descr' 'compile and run \"$2\"'
601.1Smgorny		atf_set 'require.progs' 'c++ paxctl'
611.1Smgorny	}"
621.1Smgorny
631.1Smgorny	atf_test_case "$1_profile"
641.1Smgorny	eval "$1_head() {
651.1Smgorny		atf_set 'descr' 'compile and run \"$2\" with profiling option'
661.1Smgorny		atf_set 'require.progs' 'c++ paxctl'
671.1Smgorny	}"
681.1Smgorny
691.1Smgorny	atf_test_case "$1_pic"
701.1Smgorny	eval "$1_head() {
711.1Smgorny		atf_set 'descr' 'compile and run PIC \"$2\"'
721.1Smgorny		atf_set 'require.progs' 'c++ paxctl'
731.1Smgorny	}"
741.1Smgorny
751.1Smgorny	atf_test_case "$1_pie"
761.1Smgorny	eval "$1_head() {
771.1Smgorny		atf_set 'descr' 'compile and run position independent (PIE) \"$2\"'
781.1Smgorny		atf_set 'require.progs' 'c++ paxctl'
791.1Smgorny	}"
801.1Smgorny
811.1Smgorny	atf_test_case "${1}32"
821.1Smgorny	eval "$1_head() {
831.1Smgorny		atf_set 'descr' 'compile and run \"$2\" for/in netbsd32 emulation'
841.1Smgorny		atf_set 'require.progs' 'c++ paxctl file diff cat'
851.1Smgorny	}"
861.1Smgorny
871.1Smgorny	eval "$1_body() {
881.1Smgorny		echo \"\$ASAN_CODE\" > test.cpp
891.1Smgorny		c++ -fsanitize=address -o test test.cpp
901.1Smgorny		paxctl +a test
911.1Smgorny		atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test
921.1Smgorny	}
931.1Smgorny
941.1Smgorny	$1_profile_body() {
951.1Smgorny		echo \"\$ASAN_CODE\" > test.cpp
961.1Smgorny		c++ -fsanitize=address -o test -pg test.cpp
971.1Smgorny		paxctl +a test
981.1Smgorny		atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test
991.1Smgorny	}
1001.1Smgorny
1011.1Smgorny	$1_pic_body() {
1021.1Smgorny		echo \"\$ASAN_CODE\" > test.cpp
1031.1Smgorny		c++ -DPIC_FOO -fsanitize=address -fPIC -shared -o libtest.so test.cpp
1041.1Smgorny		c++ -DPIC_MAIN -o test test.cpp -fsanitize=address -L. -ltest
1051.1Smgorny		paxctl +a test
1061.1Smgorny
1071.1Smgorny		export LD_LIBRARY_PATH=.
1081.1Smgorny		atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test
1091.1Smgorny	}
1101.1Smgorny
1111.1Smgorny	$1_pie_body() {
1121.1Smgorny		# check whether this arch supports -pice
1131.1Smgorny		if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
1141.1Smgorny			atf_set_skip 'c++ -pie not supported on this architecture'
1151.1Smgorny		fi
1161.1Smgorny		echo \"\$ASAN_CODE\" > test.cpp
1171.1Smgorny		c++ -fsanitize=address -o test -fpie -pie test.cpp
1181.1Smgorny		paxctl +a test
1191.1Smgorny		atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test
1201.1Smgorny	}
1211.1Smgorny
1221.1Smgorny	${1}32_body() {
1231.1Smgorny		# check whether this arch is 64bit
1241.1Smgorny		if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
1251.1Smgorny			atf_skip 'this is not a 64 bit architecture'
1261.1Smgorny		fi
1271.1Smgorny		if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
1281.1Smgorny			atf_skip 'c++ -m32 not supported on this architecture'
1291.1Smgorny		else
1301.1Smgorny			if fgrep -q _LP64 ./def32; then
1311.1Smgorny				atf_fail 'c++ -m32 does not generate netbsd32 binaries'
1321.1Smgorny			fi
1331.1Smgorny		fi
1341.1Smgorny
1351.1Smgorny		echo \"\$ASAN_CODE\" > test.cpp
1361.1Smgorny		c++ -fsanitize=address -o df32 -m32 test.cpp
1371.1Smgorny		c++ -fsanitize=address -o df64 test.cpp
1381.1Smgorny		file -b ./df32 > ./ftype32
1391.1Smgorny		file -b ./df64 > ./ftype64
1401.1Smgorny		if diff ./ftype32 ./ftype64 >/dev/null; then
1411.1Smgorny			atf_fail 'generated binaries do not differ'
1421.1Smgorny		fi
1431.1Smgorny		echo '32bit binaries on this platform are:'
1441.1Smgorny		cat ./ftype32
1451.1Smgorny		echo 'While native (64bit) binaries are:'
1461.1Smgorny		cat ./ftype64
1471.1Smgorny		paxctl +a df32
1481.1Smgorny		atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./df32
1491.1Smgorny
1501.1Smgorny# and another test with profile 32bit binaries
1511.1Smgorny		c++ -fsanitize=address -o test -pg -m32 test.cpp
1521.1Smgorny		paxctl +a test
1531.1Smgorny		atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test
1541.1Smgorny	}"
1551.1Smgorny}
1561.1Smgorny
1571.1Smgornyasan_add_test_cases() {
1581.1Smgorny	test_target
1591.1Smgorny	test $SUPPORT = 'n' && {
1601.1Smgorny		atf_add_test_case target_not_supported
1611.1Smgorny		return 0
1621.1Smgorny	}
1631.1Smgorny
1641.1Smgorny	atf_add_test_case "$1"
1651.1Smgorny#	atf_add_test_case "$1_profile"
1661.1Smgorny	atf_add_test_case "$1_pic"
1671.1Smgorny	atf_add_test_case "$1_pie"
1681.1Smgorny#	atf_add_test_case "${1}32"
1691.1Smgorny	# static option not supported
1701.1Smgorny	# -static and -fsanitize=address can't be used together for compilation
1711.1Smgorny	# (gcc version 5.4.0 and clang 7.1) tested on April 2nd 2018.
1721.1Smgorny}
173