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