Rebase.
[official-gcc.git] / gcc / testsuite / lib / asan-dg.exp
blob7a12160bb19cf4a68b1107619f8f76046bd78b79
1 # Copyright (C) 2012-2014 Free Software Foundation, Inc.
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.
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.
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/>.
17 # Return 1 if compilation with -fsanitize=address is error-free for trivial
18 # code, 0 otherwise.
20 proc check_effective_target_faddress_sanitizer {} {
21 return [check_no_compiler_messages faddress_sanitizer object {
22 void foo (void) { }
23 } "-fsanitize=address"]
26 proc asan_include_flags {} {
27 global srcdir
28 global TESTING_IN_BUILD_TREE
30 set flags ""
32 if { [is_remote host] || ! [info exists TESTING_IN_BUILD_TREE] } {
33 return "${flags}"
36 set flags "-I$srcdir/../../libsanitizer/include"
38 return "$flags"
42 # asan_link_flags -- compute library path and flags to find libasan.
43 # (originally from g++.exp)
46 proc asan_link_flags { paths } {
47 global srcdir
48 global ld_library_path
49 global shlib_ext
51 set gccpath ${paths}
52 set flags ""
54 set shlib_ext [get_shlib_extension]
56 if { $gccpath != "" } {
57 if { [file exists "${gccpath}/libsanitizer/asan/.libs/libasan.a"]
58 || [file exists "${gccpath}/libsanitizer/asan/.libs/libasan.${shlib_ext}"] } {
59 append flags " -B${gccpath}/libsanitizer/ "
60 append flags " -B${gccpath}/libsanitizer/asan/ "
61 append flags " -L${gccpath}/libsanitizer/asan/.libs "
62 append ld_library_path ":${gccpath}/libsanitizer/asan/.libs"
64 } else {
65 global tool_root_dir
67 set libasan [lookfor_file ${tool_root_dir} libasan]
68 if { $libasan != "" } {
69 append flags "-L${libasan} "
70 append ld_library_path ":${libasan}"
74 set_ld_library_path_env_vars
76 return "$flags"
80 # asan_init -- called at the start of each subdir of tests
83 proc asan_init { args } {
84 global TEST_ALWAYS_FLAGS
85 global ALWAYS_CXXFLAGS
86 global TOOL_OPTIONS
87 global asan_saved_TEST_ALWAYS_FLAGS
89 set link_flags ""
90 if ![is_remote host] {
91 if [info exists TOOL_OPTIONS] {
92 set link_flags "[asan_link_flags [get_multilibs ${TOOL_OPTIONS}]]"
93 } else {
94 set link_flags "[asan_link_flags [get_multilibs]]"
98 set include_flags "[asan_include_flags]"
100 if [info exists TEST_ALWAYS_FLAGS] {
101 set asan_saved_TEST_ALWAYS_FLAGS $TEST_ALWAYS_FLAGS
103 if [info exists ALWAYS_CXXFLAGS] {
104 set ALWAYS_CXXFLAGS [concat "{ldflags=$link_flags}" $ALWAYS_CXXFLAGS]
105 set ALWAYS_CXXFLAGS [concat "{additional_flags=-fsanitize=address -g $include_flags}" $ALWAYS_CXXFLAGS]
106 } else {
107 if [info exists TEST_ALWAYS_FLAGS] {
108 set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=address -g $include_flags $TEST_ALWAYS_FLAGS"
109 } else {
110 set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=address -g $include_flags"
113 if { $link_flags != "" } {
114 return 1
116 return 0
120 # asan_finish -- called at the start of each subdir of tests
123 proc asan_finish { args } {
124 global TEST_ALWAYS_FLAGS
125 global asan_saved_TEST_ALWAYS_FLAGS
127 if [info exists asan_saved_TEST_ALWAYS_FLAGS] {
128 set TEST_ALWAYS_FLAGS $asan_saved_TEST_ALWAYS_FLAGS
129 } else {
130 unset TEST_ALWAYS_FLAGS
134 # Symbolize lines like
135 # #2 0xdeadbeef (/some/path/libsanitizer.so.0.0.0+0xbeef)
136 # in $output using addr2line to
137 # #2 0xdeadbeef in foobar file:123
138 proc asan_symbolize { output } {
139 set addresses [regexp -inline -all -line "^ *#\[0-9\]+ 0x\[0-9a-f\]+ \[(\](\[^)\]+)\[+\](0x\[0-9a-f\]+)\[)\]$" "$output"]
140 if { [llength $addresses] > 0 } {
141 set addr2line_name [find_binutils_prog addr2line]
142 set idx 1
143 while { $idx < [llength $addresses] } {
144 set key [regsub -all "\[\]\[\]" [lindex $addresses $idx] "\\\\&"]
145 set val [lindex $addresses [expr $idx + 1]]
146 lappend arr($key) $val
147 set idx [expr $idx + 3]
149 foreach key [array names arr] {
150 set args "-f -e $key $arr($key)"
151 set status [remote_exec host "$addr2line_name" "$args"]
152 if { [lindex $status 0] > 0 } continue
153 regsub -all "\r\n" [lindex $status 1] "\n" addr2line_output
154 regsub -all "\[\n\r\]BFD: \[^\n\r\]*" $addr2line_output "" addr2line_output
155 regsub -all "^BFD: \[^\n\r\]*\[\n\r\]" $addr2line_output "" addr2line_output
156 set addr2line_output [regexp -inline -all -line "^\[^\n\r]*" $addr2line_output]
157 set idx 0
158 foreach val $arr($key) {
159 if { [expr $idx + 1] < [llength $addr2line_output] } {
160 set fnname [lindex $addr2line_output $idx]
161 set fileline [lindex $addr2line_output [expr $idx + 1]]
162 if { "$fnname" != "??" } {
163 set newkey "$key+$val"
164 set repl($newkey) "$fnname $fileline"
166 set idx [expr $idx + 2]
170 set idx 0
171 set new_output ""
172 while {[regexp -start $idx -indices " #\[0-9\]+ 0x\[0-9a-f\]+ \[(\](\[^)\]+\[+\]0x\[0-9a-f\]+)\[)\]" "$output" -> addr] > 0} {
173 set low [lindex $addr 0]
174 set high [lindex $addr 1]
175 set val [string range "$output" $low $high]
176 append new_output [string range "$output" $idx [expr $low - 2]]
177 if [info exists repl($val)] {
178 append new_output "in $repl($val)"
179 } else {
180 append new_output "($val)"
182 set idx [expr $high + 2]
184 append new_output [string range "$output" $idx [string length "$output"]]
185 return "$new_output"
187 return "$output"
190 # Return a list of gtest tests, printed in the form
191 # DEJAGNU_GTEST_TEST AddressSanitizer_SimpleDeathTest
192 # DEJAGNU_GTEST_TEST AddressSanitizer_VariousMallocsTest
193 proc asan_get_gtest_test_list { output } {
194 set idx 0
195 set ret ""
196 while {[regexp -start $idx -indices "DEJAGNU_GTEST_TEST (\[^\n\r\]*)(\r\n|\n|\r)" "$output" -> testname] > 0} {
197 set low [lindex $testname 0]
198 set high [lindex $testname 1]
199 set val [string range "$output" $low $high]
200 lappend ret $val
201 set idx [expr $high + 1]
203 return $ret
206 # Return a list of gtest EXPECT_DEATH tests, printed in the form
207 # DEJAGNU_GTEST_EXPECT_DEATH1 statement DEJAGNU_GTEST_EXPECT_DEATH1 regexp DEJAGNU_GTEST_EXPECT_DEATH1
208 # DEJAGNU_GTEST_EXPECT_DEATH2 other statement DEJAGNU_GTEST_EXPECT_DEATH2 other regexp DEJAGNU_GTEST_EXPECT_DEATH2
209 proc asan_get_gtest_expect_death_list { output } {
210 set idx 0
211 set ret ""
212 while {[regexp -start $idx -indices "DEJAGNU_GTEST_EXPECT_DEATH(\[0-9\]*)" "$output" -> id ] > 0} {
213 set low [lindex $id 0]
214 set high [lindex $id 1]
215 set val_id [string range "$output" $low $high]
216 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 }
217 set low [lindex $statement 0]
218 set high [lindex $statement 1]
219 set val_statement [string range "$output" $low $high]
220 set low [lindex $regexpr 0]
221 set high [lindex $regexpr 1]
222 set val_regexpr [string range "$output" $low $high]
223 lappend ret [list "$val_id" "$val_statement" "$val_regexpr"]
224 set idx [lindex $whole 1]
226 return $ret
229 # Replace ${tool}_load with a wrapper so that we can symbolize the output.
230 if { [info procs ${tool}_load] != [list] \
231 && [info procs saved_asan_${tool}_load] == [list] } {
232 rename ${tool}_load saved_asan_${tool}_load
234 proc ${tool}_load { program args } {
235 global tool
236 global asan_last_gtest_test_list
237 global asan_last_gtest_expect_death_list
238 set result [eval [list saved_asan_${tool}_load $program] $args]
239 set output [lindex $result 1]
240 set symbolized_output [asan_symbolize "$output"]
241 set asan_last_gtest_test_list [asan_get_gtest_test_list "$output"]
242 set asan_last_gtest_expect_death_list [asan_get_gtest_expect_death_list "$output"]
243 set result [list [lindex $result 0] $symbolized_output]
244 return $result
248 # Utility for running gtest asan emulation under dejagnu, invoked via dg-final.
249 # Call pass if variable has the desired value, otherwise fail.
251 # Argument 0 handles expected failures and the like
252 proc asan-gtest { args } {
253 global tool
254 global asan_last_gtest_test_list
255 global asan_last_gtest_expect_death_list
257 if { ![info exists asan_last_gtest_test_list] } { return }
258 if { [llength $asan_last_gtest_test_list] == 0 } { return }
259 if { ![isnative] || [is_remote target] } { return }
261 set gtest_test_list $asan_last_gtest_test_list
262 unset asan_last_gtest_test_list
264 if { [llength $args] >= 1 } {
265 switch [dg-process-target [lindex $args 0]] {
266 "S" { }
267 "N" { return }
268 "F" { setup_xfail "*-*-*" }
269 "P" { }
273 # This assumes that we are three frames down from dg-test, and that
274 # it still stores the filename of the testcase in a local variable "name".
275 # A cleaner solution would require a new DejaGnu release.
276 upvar 2 name testcase
277 upvar 2 prog prog
279 set output_file "[file rootname [file tail $prog]].exe"
281 foreach gtest $gtest_test_list {
282 set testname "$testcase $gtest"
283 set status -1
285 setenv DEJAGNU_GTEST_ARG "$gtest"
286 set result [${tool}_load ./$output_file $gtest]
287 unsetenv DEJAGNU_GTEST_ARG
288 set status [lindex $result 0]
289 set output [lindex $result 1]
290 if { "$status" == "pass" } {
291 pass "$testname execution test"
292 if { [info exists asan_last_gtest_expect_death_list] } {
293 set gtest_expect_death_list $asan_last_gtest_expect_death_list
294 foreach gtest_death $gtest_expect_death_list {
295 set id [lindex $gtest_death 0]
296 set testname "$testcase $gtest [lindex $gtest_death 1]"
297 set regexpr [lindex $gtest_death 2]
298 set status -1
300 setenv DEJAGNU_GTEST_ARG "$gtest:$id"
301 set result [${tool}_load ./$output_file "$gtest:$id"]
302 unsetenv DEJAGNU_GTEST_ARG
303 set status [lindex $result 0]
304 set output [lindex $result 1]
305 if { "$status" == "fail" } {
306 pass "$testname execution test"
307 if { ![regexp $regexpr ${output}] } {
308 fail "$testname output pattern test"
309 send_log "Output should match: $regexpr\n"
310 } else {
311 pass "$testname output pattern test"
313 } elseif { "$status" == "pass" } {
314 fail "$testname execution test"
315 } else {
316 $status "$testname execution test"
320 } else {
321 $status "$testname execution test"
323 unset asan_last_gtest_expect_death_list
326 return