1 # Copyright (c) 2018 The NetBSD Foundation, Inc. 2 # All rights reserved. 3 # 4 # This code is derived from software contributed to The NetBSD Foundation 5 # by Yang Zheng. 6 # 7 # Redistribution and use in source and binary forms, with or without 8 # modification, are permitted provided that the following conditions 9 # are met: 10 # 1. Redistributions of source code must retain the above copyright 11 # notice, this list of conditions and the following disclaimer. 12 # 2. Redistributions in binary form must reproduce the above copyright 13 # notice, this list of conditions and the following disclaimer in the 14 # documentation and/or other materials provided with the distribution. 15 # 16 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 # POSSIBILITY OF SUCH DAMAGE. 27 # 28 29 test_target() 30 { 31 SUPPORT='n' 32 if uname -m | grep -q "amd64" && command -v c++ >/dev/null 2>&1 && \ 33 ! echo __clang__ | c++ -E - | grep -q __clang__; then 34 # only clang with major version newer than 7 is supported 35 CLANG_MAJOR=`echo __clang_major__ | c++ -E - | grep -o '^[[:digit:]]'` 36 if [ "$CLANG_MAJOR" -ge "7" ]; then 37 SUPPORT='y' 38 fi 39 fi 40 } 41 42 atf_test_case partial_poison 43 partial_poison_head() { 44 atf_set "descr" "Test memory sanitizer for __msan_partial_poison interface" 45 atf_set "require.progs" "c++ paxctl" 46 } 47 48 atf_test_case partial_poison_profile 49 partial_poison_profile_head() { 50 atf_set "descr" "Test memory sanitizer for __msan_partial_poison with profiling option" 51 atf_set "require.progs" "c++ paxctl" 52 } 53 atf_test_case partial_poison_pic 54 partial_poison_pic_head() { 55 atf_set "descr" "Test memory sanitizer for __msan_partial_poison with position independent code (PIC) flag" 56 atf_set "require.progs" "c++ paxctl" 57 } 58 atf_test_case partial_poison_pie 59 partial_poison_pie_head() { 60 atf_set "descr" "Test memory sanitizer for __msan_partial_poison with position independent execution (PIE) flag" 61 atf_set "require.progs" "c++ paxctl" 62 } 63 64 partial_poison_body(){ 65 cat > test.cc << EOF 66 #include <stdint.h> 67 #include <sanitizer/msan_interface.h> 68 69 int main(void) { 70 char x[4]; 71 char x_s[4] = {0x77, 0x65, 0x43, 0x21}; 72 __msan_partial_poison(&x, &x_s, sizeof(x_s)); 73 __msan_print_shadow(&x, sizeof(x_s)); 74 return 0; 75 } 76 EOF 77 78 c++ -fsanitize=memory -o test test.cc 79 paxctl +a test 80 atf_check -s ignore -o ignore -e match:": 77654321" ./test 81 } 82 83 partial_poison_profile_body(){ 84 cat > test.cc << EOF 85 #include <stdint.h> 86 #include <sanitizer/msan_interface.h> 87 88 int main(void) { 89 char x[4]; 90 char x_s[4] = {0x77, 0x65, 0x43, 0x21}; 91 __msan_partial_poison(&x, &x_s, sizeof(x_s)); 92 __msan_print_shadow(&x, sizeof(x_s)); 93 return 0; 94 } 95 EOF 96 97 c++ -fsanitize=memory -static -o test -pg test.cc 98 paxctl +a test 99 atf_check -s ignore -o ignore -e match:": 77654321" ./test 100 } 101 102 partial_poison_pic_body(){ 103 cat > test.cc << EOF 104 #include <stdio.h> 105 #include <stdlib.h> 106 int help(int); 107 int main(int argc, char **argv) {return help(argc);} 108 EOF 109 110 cat > pic.cc << EOF 111 #include <stdint.h> 112 #include <sanitizer/msan_interface.h> 113 114 int help(int argc) { 115 char x[4]; 116 char x_s[4] = {0x77, 0x65, 0x43, 0x21}; 117 __msan_partial_poison(&x, &x_s, sizeof(x_s)); 118 __msan_print_shadow(&x, sizeof(x_s)); 119 return 0; 120 } 121 EOF 122 123 c++ -fsanitize=memory -fPIC -shared -o libtest.so pic.cc 124 c++ -o test test.cc -fsanitize=memory -L. -ltest 125 paxctl +a test 126 127 export LD_LIBRARY_PATH=. 128 atf_check -s ignore -o ignore -e match:": 77654321" ./test 129 } 130 partial_poison_pie_body(){ 131 132 #check whether -pie flag is supported on this architecture 133 if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 134 atf_set_skip "c++ -pie not supported on this architecture" 135 fi 136 cat > test.cc << EOF 137 #include <stdint.h> 138 #include <sanitizer/msan_interface.h> 139 140 int main(void) { 141 char x[4]; 142 char x_s[4] = {0x77, 0x65, 0x43, 0x21}; 143 __msan_partial_poison(&x, &x_s, sizeof(x_s)); 144 __msan_print_shadow(&x, sizeof(x_s)); 145 return 0; 146 } 147 EOF 148 149 c++ -fsanitize=memory -o test -fpie -pie test.cc 150 paxctl +a test 151 atf_check -s ignore -o ignore -e match:": 77654321" ./test 152 } 153 154 atf_test_case target_not_supported 155 target_not_supported_head() 156 { 157 atf_set "descr" "Test forced skip" 158 } 159 160 target_not_supported_body() 161 { 162 atf_skip "Target is not supported" 163 } 164 165 atf_init_test_cases() 166 { 167 test_target 168 test $SUPPORT = 'n' && { 169 atf_add_test_case target_not_supported 170 return 0 171 } 172 atf_add_test_case partial_poison 173 atf_add_test_case partial_poison_profile 174 atf_add_test_case partial_poison_pie 175 atf_add_test_case partial_poison_pic 176 } 177