1*d3d203ebSskrll# $NetBSD: ubsan_common.subr,v 1.2 2022/06/12 08:55:36 skrll Exp $ 29bce57c1Smgorny# 39bce57c1Smgorny# Copyright (c) 2018, 2019 The NetBSD Foundation, Inc. 49bce57c1Smgorny# All rights reserved. 59bce57c1Smgorny# 69bce57c1Smgorny# Redistribution and use in source and binary forms, with or without 79bce57c1Smgorny# modification, are permitted provided that the following conditions 89bce57c1Smgorny# are met: 99bce57c1Smgorny# 1. Redistributions of source code must retain the above copyright 109bce57c1Smgorny# notice, this list of conditions and the following disclaimer. 119bce57c1Smgorny# 2. Redistributions in binary form must reproduce the above copyright 129bce57c1Smgorny# notice, this list of conditions and the following disclaimer in the 139bce57c1Smgorny# documentation and/or other materials provided with the distribution. 149bce57c1Smgorny# 159bce57c1Smgorny# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 169bce57c1Smgorny# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 179bce57c1Smgorny# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 189bce57c1Smgorny# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 199bce57c1Smgorny# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 209bce57c1Smgorny# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 219bce57c1Smgorny# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 229bce57c1Smgorny# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 239bce57c1Smgorny# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 249bce57c1Smgorny# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 259bce57c1Smgorny# POSSIBILITY OF SUCH DAMAGE. 269bce57c1Smgorny# 279bce57c1Smgorny 289bce57c1Smgornytest_target() 299bce57c1Smgorny{ 309bce57c1Smgorny SUPPORT='n' 319bce57c1Smgorny if ! echo __GNUC__ | cc -E - | grep -q __GNUC__; then 329bce57c1Smgorny SUPPORT='y' 339bce57c1Smgorny fi 349bce57c1Smgorny 359bce57c1Smgorny if ! echo __clang__ | cc -E - | grep -q __clang__; then 369bce57c1Smgorny SUPPORT='y' 379bce57c1Smgorny fi 389bce57c1Smgorny} 399bce57c1Smgorny 409bce57c1Smgornyatf_test_case target_not_supported 419bce57c1Smgornytarget_not_supported_head() 429bce57c1Smgorny{ 439bce57c1Smgorny atf_set "descr" "Test forced skip" 449bce57c1Smgorny} 459bce57c1Smgorny 469bce57c1Smgornytarget_not_supported_body() 479bce57c1Smgorny{ 489bce57c1Smgorny atf_skip "Target is not supported" 499bce57c1Smgorny} 509bce57c1Smgorny 519bce57c1Smgorny# Add a new test case, with head & body. 529bce57c1Smgorny# asan_test_case <test-name> <description> <check-output> 539bce57c1Smgornyubsan_test_case() { 549bce57c1Smgorny atf_test_case "$1" 559bce57c1Smgorny eval "$1_head() { 569bce57c1Smgorny atf_set 'descr' 'Test Undefined Behavior for $2' 579bce57c1Smgorny atf_set 'require.progs' 'cc' 589bce57c1Smgorny }" 599bce57c1Smgorny 609bce57c1Smgorny atf_test_case "$1_profile" 619bce57c1Smgorny eval "$1_head() { 629bce57c1Smgorny atf_set 'descr' 'Test Undefined Behavior for $2 with profiling option' 639bce57c1Smgorny atf_set 'require.progs' 'cc' 649bce57c1Smgorny }" 659bce57c1Smgorny 669bce57c1Smgorny atf_test_case "$1_pic" 679bce57c1Smgorny eval "$1_head() { 689bce57c1Smgorny atf_set 'descr' 'Test Undefined Behavior for $2 with position independent code (PIC) flag' 699bce57c1Smgorny atf_set 'require.progs' 'cc' 709bce57c1Smgorny }" 719bce57c1Smgorny 729bce57c1Smgorny atf_test_case "$1_pie" 739bce57c1Smgorny eval "$1_head() { 749bce57c1Smgorny atf_set 'descr' 'Test Undefined Behavior for $2 with position independent execution (PIE) flag' 759bce57c1Smgorny atf_set 'require.progs' 'cc' 769bce57c1Smgorny }" 779bce57c1Smgorny 789bce57c1Smgorny atf_test_case "${1}32" 799bce57c1Smgorny eval "$1_head() { 809bce57c1Smgorny atf_set 'descr' 'Test Undefined Behavior for $2 in NetBSD_32 emulation' 819bce57c1Smgorny atf_set 'require.progs' 'cc file diff cat' 829bce57c1Smgorny }" 839bce57c1Smgorny 849bce57c1Smgorny eval "$1_body() { 859bce57c1Smgorny echo \"\$UBSAN_CODE\" > test.c 869bce57c1Smgorny cc -fsanitize=undefined -o test test.c 879bce57c1Smgorny # note: ignoring exit status due to inconsistency between gcc/clang 889bce57c1Smgorny # (and between individual tests) 899bce57c1Smgorny atf_check -s ignore -e match:'$3' ./test 909bce57c1Smgorny } 919bce57c1Smgorny 929bce57c1Smgorny $1_profile_body() { 939bce57c1Smgorny echo \"\$UBSAN_CODE\" > test.c 94*d3d203ebSskrll cc -fsanitize=undefined -static -o test -pg test.c 959bce57c1Smgorny atf_check -s ignore -e match:'$3' ./test 969bce57c1Smgorny } 979bce57c1Smgorny 989bce57c1Smgorny $1_pic_body() { 999bce57c1Smgorny echo \"\$UBSAN_CODE\" > test.c 1009bce57c1Smgorny cc -DPIC_FOO -fsanitize=undefined -fPIC -shared -o libtest.so test.c 1019bce57c1Smgorny cc -DPIC_MAIN -o test test.c -fsanitize=undefined -L. -ltest 1029bce57c1Smgorny 1039bce57c1Smgorny export LD_LIBRARY_PATH=. 1049bce57c1Smgorny atf_check -s ignore -e match:'$3' ./test 1059bce57c1Smgorny } 1069bce57c1Smgorny 1079bce57c1Smgorny $1_pie_body() { 1089bce57c1Smgorny # check whether this arch supports -pice 1099bce57c1Smgorny if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 1109bce57c1Smgorny atf_set_skip 'cc -pie not supported on this architecture' 1119bce57c1Smgorny fi 1129bce57c1Smgorny echo \"\$UBSAN_CODE\" > test.c 1139bce57c1Smgorny cc -fsanitize=undefined -o test -fpie -pie test.c 1149bce57c1Smgorny atf_check -s ignore -e match:'$3' ./test 1159bce57c1Smgorny } 1169bce57c1Smgorny 1179bce57c1Smgorny ${1}32_body() { 1189bce57c1Smgorny # check whether this arch is 64bit 1199bce57c1Smgorny if ! cc -dM -E - < /dev/null | fgrep -q _LP64; then 1209bce57c1Smgorny atf_skip 'this is not a 64 bit architecture' 1219bce57c1Smgorny fi 1229bce57c1Smgorny if ! cc -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then 1239bce57c1Smgorny atf_skip 'cc -m32 not supported on this architecture' 1249bce57c1Smgorny else 1259bce57c1Smgorny if fgrep -q _LP64 ./def32; then 1269bce57c1Smgorny atf_fail 'cc -m32 does not generate netbsd32 binaries' 1279bce57c1Smgorny fi 1289bce57c1Smgorny fi 1299bce57c1Smgorny 1309bce57c1Smgorny echo \"\$UBSAN_CODE\" > test.c 1319bce57c1Smgorny cc -fsanitize=undefined -o df32 -m32 test.c 1329bce57c1Smgorny cc -fsanitize=undefined -o df64 test.c 1339bce57c1Smgorny file -b ./df32 > ./ftype32 1349bce57c1Smgorny file -b ./df64 > ./ftype64 1359bce57c1Smgorny if diff ./ftype32 ./ftype64 >/dev/null; then 1369bce57c1Smgorny atf_fail 'generated binaries do not differ' 1379bce57c1Smgorny fi 1389bce57c1Smgorny echo '32bit binaries on this platform are:' 1399bce57c1Smgorny cat ./ftype32 1409bce57c1Smgorny echo 'While native (64bit) binaries are:' 1419bce57c1Smgorny cat ./ftype64 1429bce57c1Smgorny atf_check -s ignore -e match:'$3' ./df32 1439bce57c1Smgorny 1449bce57c1Smgorny# and another test with profile 32bit binaries 145*d3d203ebSskrll cc -fsanitize=undefined -static -o test -pg -m32 test.c 1469bce57c1Smgorny atf_check -s ignore -e match:'$3' ./test 1479bce57c1Smgorny }" 1489bce57c1Smgorny} 1499bce57c1Smgorny 1509bce57c1Smgornyubsan_add_test_cases() { 1519bce57c1Smgorny test_target 1529bce57c1Smgorny test $SUPPORT = 'n' && { 1539bce57c1Smgorny atf_add_test_case target_not_supported 1549bce57c1Smgorny return 0 1559bce57c1Smgorny } 1569bce57c1Smgorny 1579bce57c1Smgorny atf_add_test_case "$1" 1589bce57c1Smgorny# atf_add_test_case "$1_profile" 1599bce57c1Smgorny atf_add_test_case "$1_pic" 1609bce57c1Smgorny atf_add_test_case "$1_pie" 1619bce57c1Smgorny# atf_add_test_case "${1}32" 1629bce57c1Smgorny} 163