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