1*4bb9965cSskrll# $NetBSD: asan_common.subr,v 1.4 2022/06/12 08:55:36 skrll Exp $ 2ff295eccSmgorny# 3ff295eccSmgorny# Copyright (c) 2018, 2019 The NetBSD Foundation, Inc. 4ff295eccSmgorny# All rights reserved. 5ff295eccSmgorny# 6ff295eccSmgorny# Redistribution and use in source and binary forms, with or without 7ff295eccSmgorny# modification, are permitted provided that the following conditions 8ff295eccSmgorny# are met: 9ff295eccSmgorny# 1. Redistributions of source code must retain the above copyright 10ff295eccSmgorny# notice, this list of conditions and the following disclaimer. 11ff295eccSmgorny# 2. Redistributions in binary form must reproduce the above copyright 12ff295eccSmgorny# notice, this list of conditions and the following disclaimer in the 13ff295eccSmgorny# documentation and/or other materials provided with the distribution. 14ff295eccSmgorny# 15ff295eccSmgorny# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16ff295eccSmgorny# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17ff295eccSmgorny# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18ff295eccSmgorny# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19ff295eccSmgorny# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20ff295eccSmgorny# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21ff295eccSmgorny# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22ff295eccSmgorny# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23ff295eccSmgorny# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24ff295eccSmgorny# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25ff295eccSmgorny# POSSIBILITY OF SUCH DAMAGE. 26ff295eccSmgorny# 27ff295eccSmgorny 28ff295eccSmgornySUPPORT='n' 29ff295eccSmgornytest_target() { 306281bd10Sskrll if uname -p | grep -q "aarch64"; then 316281bd10Sskrll SUPPORT='y' 326281bd10Sskrll fi 336281bd10Sskrll 34ff295eccSmgorny if uname -m | grep -q "amd64"; then 35ff295eccSmgorny SUPPORT='y' 36ff295eccSmgorny fi 37ff295eccSmgorny 38ff295eccSmgorny if uname -m | grep -q "i386"; then 39ff295eccSmgorny SUPPORT='y' 40ff295eccSmgorny fi 41ff295eccSmgorny} 42ff295eccSmgorny 43ff295eccSmgornyatf_test_case target_not_supported 44ff295eccSmgornytarget_not_supported_head() 45ff295eccSmgorny{ 46ff295eccSmgorny atf_set "descr" "Test forced skip" 47ff295eccSmgorny} 48ff295eccSmgorny 49ff295eccSmgornytarget_not_supported_body() 50ff295eccSmgorny{ 51ff295eccSmgorny atf_skip "Target is not supported" 52ff295eccSmgorny} 53ff295eccSmgorny 54ff295eccSmgorny# Add a new test case, with head & body. 55ff295eccSmgorny# asan_test_case <test-name> <description> <check-output> 56ff295eccSmgornyasan_test_case() { 57ff295eccSmgorny atf_test_case "$1" 58ff295eccSmgorny eval "$1_head() { 59ff295eccSmgorny atf_set 'descr' 'compile and run \"$2\"' 60ff295eccSmgorny atf_set 'require.progs' 'c++ paxctl' 61ff295eccSmgorny }" 62ff295eccSmgorny 63ff295eccSmgorny atf_test_case "$1_profile" 64ff295eccSmgorny eval "$1_head() { 65ff295eccSmgorny atf_set 'descr' 'compile and run \"$2\" with profiling option' 66ff295eccSmgorny atf_set 'require.progs' 'c++ paxctl' 67ff295eccSmgorny }" 68ff295eccSmgorny 69ff295eccSmgorny atf_test_case "$1_pic" 70ff295eccSmgorny eval "$1_head() { 71ff295eccSmgorny atf_set 'descr' 'compile and run PIC \"$2\"' 72ff295eccSmgorny atf_set 'require.progs' 'c++ paxctl' 73ff295eccSmgorny }" 74ff295eccSmgorny 75ff295eccSmgorny atf_test_case "$1_pie" 76ff295eccSmgorny eval "$1_head() { 77ff295eccSmgorny atf_set 'descr' 'compile and run position independent (PIE) \"$2\"' 78ff295eccSmgorny atf_set 'require.progs' 'c++ paxctl' 79ff295eccSmgorny }" 80ff295eccSmgorny 81ff295eccSmgorny atf_test_case "${1}32" 82ff295eccSmgorny eval "$1_head() { 83ff295eccSmgorny atf_set 'descr' 'compile and run \"$2\" for/in netbsd32 emulation' 84ff295eccSmgorny atf_set 'require.progs' 'c++ paxctl file diff cat' 85ff295eccSmgorny }" 86ff295eccSmgorny 87ff295eccSmgorny eval "$1_body() { 88ff295eccSmgorny echo \"\$ASAN_CODE\" > test.cpp 89ff295eccSmgorny c++ -fsanitize=address -o test test.cpp 90ff295eccSmgorny paxctl +a test 91ff295eccSmgorny atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test 92ff295eccSmgorny } 93ff295eccSmgorny 94ff295eccSmgorny $1_profile_body() { 95ff295eccSmgorny echo \"\$ASAN_CODE\" > test.cpp 96*4bb9965cSskrll c++ -fsanitize=address -static -o test -pg test.cpp 97ff295eccSmgorny paxctl +a test 98ff295eccSmgorny atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test 99ff295eccSmgorny } 100ff295eccSmgorny 101ff295eccSmgorny $1_pic_body() { 102ff295eccSmgorny echo \"\$ASAN_CODE\" > test.cpp 103ff295eccSmgorny c++ -DPIC_FOO -fsanitize=address -fPIC -shared -o libtest.so test.cpp 104ff295eccSmgorny c++ -DPIC_MAIN -o test test.cpp -fsanitize=address -L. -ltest 105ff295eccSmgorny paxctl +a test 106ff295eccSmgorny 107ff295eccSmgorny export LD_LIBRARY_PATH=. 108ff295eccSmgorny atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test 109ff295eccSmgorny } 110ff295eccSmgorny 111ff295eccSmgorny $1_pie_body() { 112ff295eccSmgorny # check whether this arch supports -pice 113ff295eccSmgorny if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 114ff295eccSmgorny atf_set_skip 'c++ -pie not supported on this architecture' 115ff295eccSmgorny fi 116ff295eccSmgorny echo \"\$ASAN_CODE\" > test.cpp 117ff295eccSmgorny c++ -fsanitize=address -o test -fpie -pie test.cpp 118ff295eccSmgorny paxctl +a test 119ff295eccSmgorny atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test 120ff295eccSmgorny } 121ff295eccSmgorny 122ff295eccSmgorny ${1}32_body() { 123ff295eccSmgorny # check whether this arch is 64bit 124ff295eccSmgorny if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then 125ff295eccSmgorny atf_skip 'this is not a 64 bit architecture' 126ff295eccSmgorny fi 127ff295eccSmgorny if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then 128ff295eccSmgorny atf_skip 'c++ -m32 not supported on this architecture' 129ff295eccSmgorny else 130ff295eccSmgorny if fgrep -q _LP64 ./def32; then 131ff295eccSmgorny atf_fail 'c++ -m32 does not generate netbsd32 binaries' 132ff295eccSmgorny fi 133ff295eccSmgorny fi 134ff295eccSmgorny 135ff295eccSmgorny echo \"\$ASAN_CODE\" > test.cpp 136ff295eccSmgorny c++ -fsanitize=address -o df32 -m32 test.cpp 137ff295eccSmgorny c++ -fsanitize=address -o df64 test.cpp 138ff295eccSmgorny file -b ./df32 > ./ftype32 139ff295eccSmgorny file -b ./df64 > ./ftype64 140ff295eccSmgorny if diff ./ftype32 ./ftype64 >/dev/null; then 141ff295eccSmgorny atf_fail 'generated binaries do not differ' 142ff295eccSmgorny fi 143ff295eccSmgorny echo '32bit binaries on this platform are:' 144ff295eccSmgorny cat ./ftype32 145ff295eccSmgorny echo 'While native (64bit) binaries are:' 146ff295eccSmgorny cat ./ftype64 147ff295eccSmgorny paxctl +a df32 148ff295eccSmgorny atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./df32 149ff295eccSmgorny 150ff295eccSmgorny# and another test with profile 32bit binaries 151*4bb9965cSskrll c++ -fsanitize=address -static -o test -pg -m32 test.cpp 152ff295eccSmgorny paxctl +a test 153ff295eccSmgorny atf_check -s not-exit:0 -o not-match:'CHECK\n' -e match:'$3' ./test 154ff295eccSmgorny }" 155ff295eccSmgorny} 156ff295eccSmgorny 157ff295eccSmgornyasan_add_test_cases() { 158ff295eccSmgorny test_target 159ff295eccSmgorny test $SUPPORT = 'n' && { 160ff295eccSmgorny atf_add_test_case target_not_supported 161ff295eccSmgorny return 0 162ff295eccSmgorny } 163ff295eccSmgorny 164ff295eccSmgorny atf_add_test_case "$1" 165ff295eccSmgorny# atf_add_test_case "$1_profile" 166ff295eccSmgorny atf_add_test_case "$1_pic" 167ff295eccSmgorny atf_add_test_case "$1_pie" 168ff295eccSmgorny# atf_add_test_case "${1}32" 169ff295eccSmgorny # static option not supported 170ff295eccSmgorny # -static and -fsanitize=address can't be used together for compilation 171ff295eccSmgorny # (gcc version 5.4.0 and clang 7.1) tested on April 2nd 2018. 172ff295eccSmgorny} 173