1*d3d203ebSskrll# $NetBSD: asan_common.subr,v 1.4 2022/06/12 08:55:36 skrll Exp $ 22d0f481dSmgorny# 32d0f481dSmgorny# Copyright (c) 2018, 2019 The NetBSD Foundation, Inc. 42d0f481dSmgorny# All rights reserved. 52d0f481dSmgorny# 62d0f481dSmgorny# Redistribution and use in source and binary forms, with or without 72d0f481dSmgorny# modification, are permitted provided that the following conditions 82d0f481dSmgorny# are met: 92d0f481dSmgorny# 1. Redistributions of source code must retain the above copyright 102d0f481dSmgorny# notice, this list of conditions and the following disclaimer. 112d0f481dSmgorny# 2. Redistributions in binary form must reproduce the above copyright 122d0f481dSmgorny# notice, this list of conditions and the following disclaimer in the 132d0f481dSmgorny# documentation and/or other materials provided with the distribution. 142d0f481dSmgorny# 152d0f481dSmgorny# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 162d0f481dSmgorny# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 172d0f481dSmgorny# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 182d0f481dSmgorny# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 192d0f481dSmgorny# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 202d0f481dSmgorny# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 212d0f481dSmgorny# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 222d0f481dSmgorny# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 232d0f481dSmgorny# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 242d0f481dSmgorny# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 252d0f481dSmgorny# POSSIBILITY OF SUCH DAMAGE. 262d0f481dSmgorny# 272d0f481dSmgorny 282d0f481dSmgornySUPPORT='n' 292d0f481dSmgornytest_target() { 30e9aef787Sskrll if uname -p | grep -q "aarch64"; then 31e9aef787Sskrll SUPPORT='y' 32e9aef787Sskrll fi 33e9aef787Sskrll 342d0f481dSmgorny if uname -m | grep -q "amd64"; then 352d0f481dSmgorny SUPPORT='y' 362d0f481dSmgorny fi 372d0f481dSmgorny 382d0f481dSmgorny if uname -m | grep -q "i386"; then 392d0f481dSmgorny SUPPORT='y' 402d0f481dSmgorny fi 412d0f481dSmgorny} 422d0f481dSmgorny 432d0f481dSmgornyatf_test_case target_not_supported 442d0f481dSmgornytarget_not_supported_head() 452d0f481dSmgorny{ 462d0f481dSmgorny atf_set "descr" "Test forced skip" 472d0f481dSmgorny} 482d0f481dSmgorny 492d0f481dSmgornytarget_not_supported_body() 502d0f481dSmgorny{ 512d0f481dSmgorny atf_skip "Target is not supported" 522d0f481dSmgorny} 532d0f481dSmgorny 542d0f481dSmgorny# Add a new test case, with head & body. 552d0f481dSmgorny# asan_test_case <test-name> <description> <check-output> 562d0f481dSmgornyasan_test_case() { 572d0f481dSmgorny atf_test_case "$1" 582d0f481dSmgorny eval "$1_head() { 592d0f481dSmgorny atf_set 'descr' 'compile and run \"$2\"' 602d0f481dSmgorny atf_set 'require.progs' 'c++ paxctl' 612d0f481dSmgorny }" 622d0f481dSmgorny 632d0f481dSmgorny atf_test_case "$1_profile" 642d0f481dSmgorny eval "$1_head() { 652d0f481dSmgorny atf_set 'descr' 'compile and run \"$2\" with profiling option' 662d0f481dSmgorny atf_set 'require.progs' 'c++ paxctl' 672d0f481dSmgorny }" 682d0f481dSmgorny 692d0f481dSmgorny atf_test_case "$1_pic" 702d0f481dSmgorny eval "$1_head() { 712d0f481dSmgorny atf_set 'descr' 'compile and run PIC \"$2\"' 722d0f481dSmgorny atf_set 'require.progs' 'c++ paxctl' 732d0f481dSmgorny }" 742d0f481dSmgorny 752d0f481dSmgorny atf_test_case "$1_pie" 762d0f481dSmgorny eval "$1_head() { 772d0f481dSmgorny atf_set 'descr' 'compile and run position independent (PIE) \"$2\"' 782d0f481dSmgorny atf_set 'require.progs' 'c++ paxctl' 792d0f481dSmgorny }" 802d0f481dSmgorny 812d0f481dSmgorny atf_test_case "${1}32" 822d0f481dSmgorny eval "$1_head() { 832d0f481dSmgorny atf_set 'descr' 'compile and run \"$2\" for/in netbsd32 emulation' 842d0f481dSmgorny atf_set 'require.progs' 'c++ paxctl file diff cat' 852d0f481dSmgorny }" 862d0f481dSmgorny 872d0f481dSmgorny eval "$1_body() { 882d0f481dSmgorny echo \"\$ASAN_CODE\" > test.cpp 892d0f481dSmgorny c++ -fsanitize=address -o test test.cpp 902d0f481dSmgorny paxctl +a test 912d0f481dSmgorny atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test 922d0f481dSmgorny } 932d0f481dSmgorny 942d0f481dSmgorny $1_profile_body() { 952d0f481dSmgorny echo \"\$ASAN_CODE\" > test.cpp 96*d3d203ebSskrll c++ -fsanitize=address -static -o test -pg test.cpp 972d0f481dSmgorny paxctl +a test 982d0f481dSmgorny atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test 992d0f481dSmgorny } 1002d0f481dSmgorny 1012d0f481dSmgorny $1_pic_body() { 1022d0f481dSmgorny echo \"\$ASAN_CODE\" > test.cpp 1032d0f481dSmgorny c++ -DPIC_FOO -fsanitize=address -fPIC -shared -o libtest.so test.cpp 1042d0f481dSmgorny c++ -DPIC_MAIN -o test test.cpp -fsanitize=address -L. -ltest 1052d0f481dSmgorny paxctl +a test 1062d0f481dSmgorny 1072d0f481dSmgorny export LD_LIBRARY_PATH=. 1082d0f481dSmgorny atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test 1092d0f481dSmgorny } 1102d0f481dSmgorny 1112d0f481dSmgorny $1_pie_body() { 1122d0f481dSmgorny # check whether this arch supports -pice 1132d0f481dSmgorny if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 1142d0f481dSmgorny atf_set_skip 'c++ -pie not supported on this architecture' 1152d0f481dSmgorny fi 1162d0f481dSmgorny echo \"\$ASAN_CODE\" > test.cpp 1172d0f481dSmgorny c++ -fsanitize=address -o test -fpie -pie test.cpp 1182d0f481dSmgorny paxctl +a test 1192d0f481dSmgorny atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test 1202d0f481dSmgorny } 1212d0f481dSmgorny 1222d0f481dSmgorny ${1}32_body() { 1232d0f481dSmgorny # check whether this arch is 64bit 1242d0f481dSmgorny if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then 1252d0f481dSmgorny atf_skip 'this is not a 64 bit architecture' 1262d0f481dSmgorny fi 1272d0f481dSmgorny if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then 1282d0f481dSmgorny atf_skip 'c++ -m32 not supported on this architecture' 1292d0f481dSmgorny else 1302d0f481dSmgorny if fgrep -q _LP64 ./def32; then 1312d0f481dSmgorny atf_fail 'c++ -m32 does not generate netbsd32 binaries' 1322d0f481dSmgorny fi 1332d0f481dSmgorny fi 1342d0f481dSmgorny 1352d0f481dSmgorny echo \"\$ASAN_CODE\" > test.cpp 1362d0f481dSmgorny c++ -fsanitize=address -o df32 -m32 test.cpp 1372d0f481dSmgorny c++ -fsanitize=address -o df64 test.cpp 1382d0f481dSmgorny file -b ./df32 > ./ftype32 1392d0f481dSmgorny file -b ./df64 > ./ftype64 1402d0f481dSmgorny if diff ./ftype32 ./ftype64 >/dev/null; then 1412d0f481dSmgorny atf_fail 'generated binaries do not differ' 1422d0f481dSmgorny fi 1432d0f481dSmgorny echo '32bit binaries on this platform are:' 1442d0f481dSmgorny cat ./ftype32 1452d0f481dSmgorny echo 'While native (64bit) binaries are:' 1462d0f481dSmgorny cat ./ftype64 1472d0f481dSmgorny paxctl +a df32 1482d0f481dSmgorny atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./df32 1492d0f481dSmgorny 1502d0f481dSmgorny# and another test with profile 32bit binaries 151*d3d203ebSskrll c++ -fsanitize=address -static -o test -pg -m32 test.cpp 1522d0f481dSmgorny paxctl +a test 1532d0f481dSmgorny atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test 1542d0f481dSmgorny }" 1552d0f481dSmgorny} 1562d0f481dSmgorny 1572d0f481dSmgornyasan_add_test_cases() { 1582d0f481dSmgorny test_target 1592d0f481dSmgorny test $SUPPORT = 'n' && { 1602d0f481dSmgorny atf_add_test_case target_not_supported 1612d0f481dSmgorny return 0 1622d0f481dSmgorny } 1632d0f481dSmgorny 1642d0f481dSmgorny atf_add_test_case "$1" 1652d0f481dSmgorny# atf_add_test_case "$1_profile" 1662d0f481dSmgorny atf_add_test_case "$1_pic" 1672d0f481dSmgorny atf_add_test_case "$1_pie" 1682d0f481dSmgorny# atf_add_test_case "${1}32" 1692d0f481dSmgorny # static option not supported 1702d0f481dSmgorny # -static and -fsanitize=address can't be used together for compilation 1712d0f481dSmgorny # (gcc version 5.4.0 and clang 7.1) tested on April 2nd 2018. 1722d0f481dSmgorny} 173