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