Debian Codegraph @

gcc/testsuite/lib/asan-dg.exp

raw
1 # Copyright (C) 2012-2025 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with GCC; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
16
17 # Return 1 if compilation with -fsanitize=address is error-free for trivial
18 # code, 0 otherwise.
19 #
20 # NOTE: This should only be used between calls to asan_init and asan_finish.
21 # It is therefore defined here rather than in target-supports.exp.
22
23 proc check_effective_target_fsanitize_address {} {
24 if ![check_no_compiler_messages fsanitize_address executable {
25 int main (void) { return 0; }
26 }] {
27 return 0;
28 }
29
30 # asan doesn't work if there's a ulimit on virtual memory.
31 if ![is_remote target] {
32 if [catch {exec sh -c "ulimit -v"} ulimit_v] {
33 # failed to get ulimit
34 } elseif [regexp {^[0-9]+$} $ulimit_v] {
35 # ulimit -v gave a numeric limit
36 warning "skipping asan tests due to ulimit -v"
37 return 0;
38 }
39 }
40
41 return 1;
42 }
43
44 proc asan_include_flags {} {
45 global srcdir
46 global TESTING_IN_BUILD_TREE
47
48 set flags ""
49
50 if { [is_remote host] || ! [info exists TESTING_IN_BUILD_TREE] } {
51 return "${flags}"
52 }
53
54 set flags "-I$srcdir/../../libsanitizer/include"
55
56 return "$flags"
57 }
58
59 #
60 # asan_link_flags -- compute library path and flags to find libasan.
61 # (originally from g++.exp)
62 #
63
64 proc asan_link_flags_1 { paths lib need_stdcxx} {
65 global srcdir
66 global ld_library_path
67 global shlib_ext
68 global ${lib}_saved_library_path
69
70 set gccpath ${paths}
71 set flags ""
72
73 set shlib_ext [get_shlib_extension]
74 set ${lib}_saved_library_path $ld_library_path
75
76 # Providing -B instead of -L means that it works for targets that use
77 # spec substitution for handling -static-xxxxx, it also works for targets
78 # the use the startfile paths to provide a runpath for uninstalled test.
79 # Each -B option will produce a -L on the link line (for paths that exist).
80 if { $gccpath != "" } {
81 if { [file exists "${gccpath}/libsanitizer/${lib}/.libs/lib${lib}.a"]
82 || [file exists "${gccpath}/libsanitizer/${lib}/.libs/lib${lib}.${shlib_ext}"] } {
83 append flags " -B${gccpath}/libsanitizer/ "
84 append flags " -B${gccpath}/libsanitizer/${lib}/ "
85 append flags " -B${gccpath}/libsanitizer/${lib}/.libs "
86 append ld_library_path ":${gccpath}/libsanitizer/${lib}/.libs"
87 }
88 # libasan links to libstdc++, so we must include it for C testcases.
89 if { $need_stdcxx && ( [file exists "${gccpath}/libstdc++-v3/src/.libs/libstdc++.a"]
90 || [file exists "${gccpath}/libstdc++-v3/src/.libs/libstdc++.${shlib_ext}"] ) } {
91 append flags " -B${gccpath}/libstdc++-v3/src/.libs "
92 append ld_library_path ":${gccpath}/libstdc++-v3/src/.libs"
93 }
94 } else {
95 global tool_root_dir
96
97 set libdir [lookfor_file ${tool_root_dir} lib${lib}]
98 if { $libdir != "" } {
99 append flags "-L${libdir} "
100 append ld_library_path ":${libdir}"
101 }
102 }
103
104 set_ld_library_path_env_vars
105
106 return "$flags"
107 }
108
109 proc asan_link_flags { paths need_stdcxx } {
110 return [asan_link_flags_1 $paths asan $need_stdcxx]
111 }
112
113 #
114 # asan_init -- called at the start of each subdir of tests
115 #
116
117 proc asan_init { args } {
118 global TEST_ALWAYS_FLAGS
119 global ALWAYS_CXXFLAGS
120 global TOOL_OPTIONS
121 global asan_saved_TEST_ALWAYS_FLAGS
122 global asan_saved_ALWAYS_CXXFLAGS
123
124 setenv ASAN_OPTIONS "color=never"
125
126 set needs_cxx [lindex $args 0]
127 set link_flags ""
128 if ![is_remote host] {
129 if [info exists TOOL_OPTIONS] {
130 set link_flags "[asan_link_flags [get_multilibs ${TOOL_OPTIONS}] $needs_cxx]"
131 } else {
132 set link_flags "[asan_link_flags [get_multilibs] $needs_cxx]"
133 }
134 }
135
136 set include_flags "[asan_include_flags]"
137
138 if [info exists TEST_ALWAYS_FLAGS] {
139 set asan_saved_TEST_ALWAYS_FLAGS $TEST_ALWAYS_FLAGS
140 }
141 if [info exists ALWAYS_CXXFLAGS] {
142 set asan_saved_ALWAYS_CXXFLAGS $ALWAYS_CXXFLAGS
143 set ALWAYS_CXXFLAGS [concat "{ldflags=$link_flags}" $ALWAYS_CXXFLAGS]
144 set ALWAYS_CXXFLAGS [concat "{additional_flags=-fsanitize=address -g $include_flags}" $ALWAYS_CXXFLAGS]
145 } else {
146 if [info exists TEST_ALWAYS_FLAGS] {
147 set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=address -g $include_flags $TEST_ALWAYS_FLAGS"
148 } else {
149 set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=address -g $include_flags"
150 }
151 }
152 }
153
154 #
155 # asan_finish -- called at the start of each subdir of tests
156 #
157
158 proc asan_finish { args } {
159 global TEST_ALWAYS_FLAGS
160 global asan_saved_TEST_ALWAYS_FLAGS
161 global asan_saved_ALWAYS_CXXFLAGS
162 global asan_saved_library_path
163 global ld_library_path
164
165 if [info exists asan_saved_ALWAYS_CXXFLAGS ] {
166 set ALWAYS_CXXFLAGS $asan_saved_ALWAYS_CXXFLAGS
167 } else {
168 if [info exists asan_saved_TEST_ALWAYS_FLAGS] {
169 set TEST_ALWAYS_FLAGS $asan_saved_TEST_ALWAYS_FLAGS
170 } else {
171 unset TEST_ALWAYS_FLAGS
172 }
173 }
174 if [info exists asan_saved_library_path ] {
175 set ld_library_path $asan_saved_library_path
176 set_ld_library_path_env_vars
177 }
178 clear_effective_target_cache
179 }
180
181 # Symbolize lines like
182 # #2 0xdeadbeef (/some/path/libsanitizer.so.0.0.0+0xbeef)
183 # in $output using addr2line to
184 # #2 0xdeadbeef in foobar file:123
185 proc asan_symbolize { output } {
186 set addresses [regexp -inline -all -line "^ *#\[0-9\]+ 0x\[0-9a-f\]+ \[(\](\[^)\]+)\[+\](0x\[0-9a-f\]+)\[)\]$" "$output"]
187 if { [llength $addresses] > 0 } {
188 set addr2line_name [find_binutils_prog addr2line]
189 set idx 1
190 while { $idx < [llength $addresses] } {
191 set key [regsub -all "\[\]\[\]" [lindex $addresses $idx] "\\\\&"]
192 set val [lindex $addresses [expr $idx + 1]]
193 lappend arr($key) $val
194 set idx [expr $idx + 3]
195 }
196 foreach key [array names arr] {
197 set args "-f -e $key $arr($key)"
198 set status [remote_exec host "$addr2line_name" "$args"]
199 if { [lindex $status 0] > 0 } continue
200 regsub -all "\r\n" [lindex $status 1] "\n" addr2line_output
201 regsub -all "\[\n\r\]BFD: \[^\n\r\]*" $addr2line_output "" addr2line_output
202 regsub -all "^BFD: \[^\n\r\]*\[\n\r\]" $addr2line_output "" addr2line_output
203 set addr2line_output [regexp -inline -all -line "^\[^\n\r]*" $addr2line_output]
204 set idx 0
205 foreach val $arr($key) {
206 if { [expr $idx + 1] < [llength $addr2line_output] } {
207 set fnname [lindex $addr2line_output $idx]
208 set fileline [lindex $addr2line_output [expr $idx + 1]]
209 if { "$fnname" != "??" } {
210 set newkey "$key+$val"
211 set repl($newkey) "$fnname $fileline"
212 }
213 set idx [expr $idx + 2]
214 }
215 }
216 }
217 set idx 0
218 set new_output ""
219 while {[regexp -start $idx -indices " #\[0-9\]+ 0x\[0-9a-f\]+ \[(\](\[^)\]+\[+\]0x\[0-9a-f\]+)\[)\]" "$output" -> addr] > 0} {
220 set low [lindex $addr 0]
221 set high [lindex $addr 1]
222 set val [string range "$output" $low $high]
223 append new_output [string range "$output" $idx [expr $low - 2]]
224 if [info exists repl($val)] {
225 append new_output "in $repl($val)"
226 } else {
227 append new_output "($val)"
228 }
229 set idx [expr $high + 2]
230 }
231 append new_output [string range "$output" $idx [string length "$output"]]
232 return "$new_output"
233 }
234 return "$output"
235 }
236
237 # Return a list of gtest tests, printed in the form
238 # DEJAGNU_GTEST_TEST AddressSanitizer_SimpleDeathTest
239 # DEJAGNU_GTEST_TEST AddressSanitizer_VariousMallocsTest
240 proc asan_get_gtest_test_list { output } {
241 set idx 0
242 set ret ""
243 while {[regexp -start $idx -indices "DEJAGNU_GTEST_TEST (\[^\n\r\]*)(\r\n|\n|\r)" "$output" -> testname] > 0} {
244 set low [lindex $testname 0]
245 set high [lindex $testname 1]
246 set val [string range "$output" $low $high]
247 lappend ret $val
248 set idx [expr $high + 1]
249 }
250 return $ret
251 }
252
253 # Return a list of gtest EXPECT_DEATH tests, printed in the form
254 # DEJAGNU_GTEST_EXPECT_DEATH1 statement DEJAGNU_GTEST_EXPECT_DEATH1 regexp DEJAGNU_GTEST_EXPECT_DEATH1
255 # DEJAGNU_GTEST_EXPECT_DEATH2 other statement DEJAGNU_GTEST_EXPECT_DEATH2 other regexp DEJAGNU_GTEST_EXPECT_DEATH2
256 proc asan_get_gtest_expect_death_list { output } {
257 set idx 0
258 set ret ""
259 while {[regexp -start $idx -indices "DEJAGNU_GTEST_EXPECT_DEATH(\[0-9\]*)" "$output" -> id ] > 0} {
260 set low [lindex $id 0]
261 set high [lindex $id 1]
262 set val_id [string range "$output" $low $high]
263 if {[regexp -start $low -indices "$val_id (.*) DEJAGNU_GTEST_EXPECT_DEATH$val_id (.*) DEJAGNU_GTEST_EXPECT_DEATH$val_id\[\n\r\]" "$output" whole statement regexpr ] == 0} { break }
264 set low [lindex $statement 0]
265 set high [lindex $statement 1]
266 set val_statement [string range "$output" $low $high]
267 set low [lindex $regexpr 0]
268 set high [lindex $regexpr 1]
269 set val_regexpr [string range "$output" $low $high]
270 lappend ret [list "$val_id" "$val_statement" "$val_regexpr"]
271 set idx [lindex $whole 1]
272 }
273 return $ret
274 }
275
276 # Replace ${tool}_load with a wrapper so that we can symbolize the output.
277 if { [info procs ${tool}_load] != [list] \
278 && [info procs saved_asan_${tool}_load] == [list] } {
279 rename ${tool}_load saved_asan_${tool}_load
280
281 proc ${tool}_load { program args } {
282 global tool
283 global asan_last_gtest_test_list
284 global asan_last_gtest_expect_death_list
285 set result [eval [list saved_asan_${tool}_load $program] $args]
286 set output [lindex $result 1]
287 set symbolized_output [asan_symbolize "$output"]
288 set asan_last_gtest_test_list [asan_get_gtest_test_list "$output"]
289 set asan_last_gtest_expect_death_list [asan_get_gtest_expect_death_list "$output"]
290 set result [list [lindex $result 0] $symbolized_output]
291 return $result
292 }
293 }
294
295 # Utility for running gtest asan emulation under dejagnu, invoked via dg-final.
296 # Call pass if variable has the desired value, otherwise fail.
297 #
298 # Argument 0 handles expected failures and the like
299 proc asan-gtest { args } {
300 global tool
301 global asan_last_gtest_test_list
302 global asan_last_gtest_expect_death_list
303
304 if { ![info exists asan_last_gtest_test_list] } { return }
305 if { [llength $asan_last_gtest_test_list] == 0 } { return }
306 if { ![isnative] || [is_remote target] } { return }
307
308 set gtest_test_list $asan_last_gtest_test_list
309 unset asan_last_gtest_test_list
310
311 if { [llength $args] >= 1 } {
312 switch [dg-process-target [lindex $args 0]] {
313 "S" { }
314 "N" { return }
315 "F" { setup_xfail "*-*-*" }
316 "P" { }
317 }
318 }
319
320 # This assumes that we are three frames down from dg-test, and that
321 # it still stores the filename of the testcase in a local variable "name".
322 # A cleaner solution would require a new DejaGnu release.
323 upvar 2 name testcase
324 upvar 2 prog prog
325
326 set output_file "[file rootname [file tail $prog]].exe"
327
328 foreach gtest $gtest_test_list {
329 set testname "$testcase $gtest"
330 set status -1
331
332 setenv DEJAGNU_GTEST_ARG "$gtest"
333 set result [${tool}_load ./$output_file $gtest]
334 unsetenv DEJAGNU_GTEST_ARG
335 set status [lindex $result 0]
336 set output [lindex $result 1]
337 if { "$status" == "pass" } {
338 pass "$testname execution test"
339 if { [info exists asan_last_gtest_expect_death_list] } {
340 set gtest_expect_death_list $asan_last_gtest_expect_death_list
341 foreach gtest_death $gtest_expect_death_list {
342 set id [lindex $gtest_death 0]
343 set testname "$testcase $gtest [lindex $gtest_death 1]"
344 set regexpr [lindex $gtest_death 2]
345 set status -1
346
347 setenv DEJAGNU_GTEST_ARG "$gtest:$id"
348 set result [${tool}_load ./$output_file "$gtest:$id"]
349 unsetenv DEJAGNU_GTEST_ARG
350 set status [lindex $result 0]
351 set output [lindex $result 1]
352 if { "$status" == "fail" } {
353 pass "$testname execution test"
354 if { ![regexp $regexpr ${output}] } {
355 fail "$testname output pattern test"
356 send_log "Output should match: $regexpr\n"
357 } else {
358 pass "$testname output pattern test"
359 }
360 } elseif { "$status" == "pass" } {
361 fail "$testname execution test"
362 } else {
363 $status "$testname execution test"
364 }
365 }
366 }
367 } else {
368 $status "$testname execution test"
369 }
370 unset asan_last_gtest_expect_death_list
371 }
372
373 return
374 }