Home | History | Annotate | Line # | Download | only in c++
      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 shadow
     43 shadow_head() {
     44 	atf_set "descr" "Test memory sanitizer for shadow interface"
     45 	atf_set "require.progs" "c++ paxctl"
     46 }
     47 
     48 atf_test_case shadow_profile
     49 shadow_profile_head() {
     50 	atf_set "descr" "Test memory sanitizer for shadow with profiling option"
     51 	atf_set "require.progs" "c++ paxctl"
     52 }
     53 atf_test_case shadow_pic
     54 shadow_pic_head() {
     55 	atf_set "descr" "Test memory sanitizer for shadow with position independent code (PIC) flag"
     56 	atf_set "require.progs" "c++ paxctl"
     57 }
     58 atf_test_case shadow_pie
     59 shadow_pie_head() {
     60 	atf_set "descr" "Test memory sanitizer for shadow with position independent execution (PIE) flag"
     61 	atf_set "require.progs" "c++ paxctl"
     62 }
     63 
     64 shadow_body(){
     65 	cat > test.cc << EOF
     66 #include <stdio.h>
     67 #include <stdlib.h>
     68 #include <sanitizer/msan_interface.h>
     69 
     70 int main(int argc, char **argv) {
     71   char str[] = "abc";
     72   char str2[] = "cdefghi";
     73   __msan_poison(str + 2, 1);
     74   __msan_copy_shadow(str2 + 2, str, 4);
     75   printf("%ld\n", __msan_test_shadow(str, 4));
     76   __msan_print_shadow(str2, 8);
     77   return 0;
     78 }
     79 EOF
     80 
     81 	c++ -fsanitize=memory -o test test.cc
     82 	paxctl +a test
     83 	atf_check -s ignore -o match:"2" -e match:"00000000 ff000000" ./test
     84 }
     85 
     86 shadow_profile_body(){
     87 	cat > test.cc << EOF
     88 #include <stdio.h>
     89 #include <stdlib.h>
     90 #include <sanitizer/msan_interface.h>
     91 
     92 int main(int argc, char **argv) {
     93   char str[] = "abc";
     94   char str2[] = "cdefghi";
     95   __msan_poison(str + 2, 1);
     96   __msan_copy_shadow(str2 + 2, str, 4);
     97   printf("%ld\n", __msan_test_shadow(str, 4));
     98   __msan_print_shadow(str2, 8);
     99   return 0;
    100 }
    101 EOF
    102 
    103 	c++ -fsanitize=memory -static -o test -pg test.cc
    104 	paxctl +a test
    105 	atf_check -s ignore -o match:"2" -e match:"00000000 ff000000" ./test
    106 }
    107 
    108 shadow_pic_body(){
    109 	cat > test.cc << EOF
    110 #include <stdio.h>
    111 #include <stdlib.h>
    112 int help(int);
    113 int main(int argc, char **argv) {return help(argc);}
    114 EOF
    115 
    116 	cat > pic.cc << EOF
    117 #include <stdio.h>
    118 #include <stdlib.h>
    119 #include <sanitizer/msan_interface.h>
    120 
    121 int help(int argc) {
    122   char str[] = "abc";
    123   char str2[] = "cdefghi";
    124   __msan_poison(str + 2, 1);
    125   __msan_copy_shadow(str2 + 2, str, 4);
    126   printf("%ld\n", __msan_test_shadow(str, 4));
    127   __msan_print_shadow(str2, 8);
    128   return 0;
    129 }
    130 EOF
    131 
    132 	c++ -fsanitize=memory -fPIC -shared -o libtest.so pic.cc
    133 	c++ -o test test.cc -fsanitize=memory -L. -ltest
    134 	paxctl +a test
    135 
    136 	export LD_LIBRARY_PATH=.
    137 	atf_check -s ignore -o match:"2" -e match:"00000000 ff000000" ./test
    138 }
    139 shadow_pie_body(){
    140 
    141 	#check whether -pie flag is supported on this architecture
    142 	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
    143 		atf_set_skip "c++ -pie not supported on this architecture"
    144 	fi
    145 	cat > test.cc << EOF
    146 #include <stdio.h>
    147 #include <stdlib.h>
    148 #include <sanitizer/msan_interface.h>
    149 
    150 int main(int argc, char **argv) {
    151   char str[] = "abc";
    152   char str2[] = "cdefghi";
    153   __msan_poison(str + 2, 1);
    154   __msan_copy_shadow(str2 + 2, str, 4);
    155   printf("%ld\n", __msan_test_shadow(str, 4));
    156   __msan_print_shadow(str2, 8);
    157   return 0;
    158 }
    159 EOF
    160 
    161 	c++ -fsanitize=memory -o test -fpie -pie test.cc
    162 	paxctl +a test
    163 	atf_check -s ignore -o match:"2" -e match:"00000000 ff000000" ./test
    164 }
    165 
    166 atf_test_case target_not_supported
    167 target_not_supported_head()
    168 {
    169 	atf_set "descr" "Test forced skip"
    170 }
    171 
    172 target_not_supported_body()
    173 {
    174 	atf_skip "Target is not supported"
    175 }
    176 
    177 atf_init_test_cases()
    178 {
    179 	test_target
    180 	test $SUPPORT = 'n' && {
    181 		atf_add_test_case target_not_supported
    182 		return 0
    183 	}
    184 	atf_add_test_case shadow
    185 	atf_add_test_case shadow_profile
    186 	atf_add_test_case shadow_pie
    187 	atf_add_test_case shadow_pic
    188 }
    189