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 check_mem
     43 check_mem_head() {
     44 	atf_set "descr" "Test memory sanitizer for __msan_check_mem_is_initialized interface"
     45 	atf_set "require.progs" "c++ paxctl"
     46 }
     47 
     48 atf_test_case check_mem_profile
     49 check_mem_profile_head() {
     50 	atf_set "descr" "Test memory sanitizer for __msan_check_mem_is_initialized with profiling option"
     51 	atf_set "require.progs" "c++ paxctl"
     52 }
     53 atf_test_case check_mem_pic
     54 check_mem_pic_head() {
     55 	atf_set "descr" "Test memory sanitizer for __msan_check_mem_is_initialized with position independent code (PIC) flag"
     56 	atf_set "require.progs" "c++ paxctl"
     57 }
     58 atf_test_case check_mem_pie
     59 check_mem_pie_head() {
     60 	atf_set "descr" "Test memory sanitizer for __msan_check_mem_is_initialized with position independent execution (PIE) flag"
     61 	atf_set "require.progs" "c++ paxctl"
     62 }
     63 
     64 check_mem_body(){
     65 	cat > test.cc << EOF
     66 #include <sanitizer/msan_interface.h>
     67 #include <stdlib.h>
     68 
     69 int main(int argc, char **argv) {
     70   int *volatile p = (int *)malloc(sizeof(int));
     71 
     72   __msan_check_mem_is_initialized(p, sizeof(*p));
     73   return 0;
     74 }
     75 EOF
     76 
     77 	c++ -fsanitize=memory -o test test.cc
     78 	paxctl +a test
     79 	atf_check -s ignore -o ignore -e match:"Uninitialized bytes in __msan_check_mem_is_initialized at offset 0 inside" ./test
     80 }
     81 
     82 check_mem_profile_body(){
     83 	cat > test.cc << EOF
     84 #include <sanitizer/msan_interface.h>
     85 #include <stdlib.h>
     86 
     87 int main(int argc, char **argv) {
     88   int *volatile p = (int *)malloc(sizeof(int));
     89 
     90   __msan_check_mem_is_initialized(p, sizeof(*p));
     91   return 0;
     92 }
     93 EOF
     94 
     95 	c++ -fsanitize=memory -static -o test -pg test.cc
     96 	paxctl +a test
     97 	atf_check -s ignore -o ignore -e match:"Uninitialized bytes in __msan_check_mem_is_initialized at offset 0 inside" ./test
     98 }
     99 
    100 check_mem_pic_body(){
    101 	cat > test.cc << EOF
    102 #include <stdio.h>
    103 #include <stdlib.h>
    104 int help(int);
    105 int main(int argc, char **argv) {return help(argc);}
    106 EOF
    107 
    108 	cat > pic.cc << EOF
    109 #include <sanitizer/msan_interface.h>
    110 #include <stdlib.h>
    111 
    112 int help(int argc) {
    113   int *volatile p = (int *)malloc(sizeof(int));
    114 
    115   __msan_check_mem_is_initialized(p, sizeof(*p));
    116   return 0;
    117 }
    118 EOF
    119 
    120 	c++ -fsanitize=memory -fPIC -shared -o libtest.so pic.cc
    121 	c++ -o test test.cc -fsanitize=memory -L. -ltest
    122 	paxctl +a test
    123 
    124 	export LD_LIBRARY_PATH=.
    125 	atf_check -s ignore -o ignore -e match:"Uninitialized bytes in __msan_check_mem_is_initialized at offset 0 inside" ./test
    126 }
    127 check_mem_pie_body(){
    128 
    129 	#check whether -pie flag is supported on this architecture
    130 	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
    131 		atf_set_skip "c++ -pie not supported on this architecture"
    132 	fi
    133 	cat > test.cc << EOF
    134 #include <sanitizer/msan_interface.h>
    135 #include <stdlib.h>
    136 
    137 int main(int argc, char **argv) {
    138   int *volatile p = (int *)malloc(sizeof(int));
    139 
    140   __msan_check_mem_is_initialized(p, sizeof(*p));
    141   return 0;
    142 }
    143 EOF
    144 
    145 	c++ -fsanitize=memory -o test -fpie -pie test.cc
    146 	paxctl +a test
    147 	atf_check -s ignore -o ignore -e match:"Uninitialized bytes in __msan_check_mem_is_initialized at offset 0 inside" ./test
    148 }
    149 
    150 atf_test_case target_not_supported
    151 target_not_supported_head()
    152 {
    153 	atf_set "descr" "Test forced skip"
    154 }
    155 
    156 target_not_supported_body()
    157 {
    158 	atf_skip "Target is not supported"
    159 }
    160 
    161 atf_init_test_cases()
    162 {
    163 	test_target
    164 	test $SUPPORT = 'n' && {
    165 		atf_add_test_case target_not_supported
    166 		return 0
    167 	}
    168 	atf_add_test_case check_mem
    169 	atf_add_test_case check_mem_profile
    170 	atf_add_test_case check_mem_pie
    171 	atf_add_test_case check_mem_pic
    172 }
    173