Debian Codegraph @

gcc/testsuite/lib/ubsan-dg.exp

raw
1 # Copyright (C) 2013-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=undefined is error-free for trivial
18 # code, 0 otherwise.
19
20 set orig_ubsan_options_saved 0
21 set orig_ubsan_options 0
22
23 proc check_effective_target_fsanitize_undefined {} {
24 return [check_runtime fsanitize_undefined {
25 int main (void) { return 0; }
26 } "-fsanitize=undefined"]
27 }
28
29 #
30 # ubsan_link_flags -- compute library path and flags to find libubsan.
31 # (originally from g++.exp)
32 #
33
34 proc ubsan_link_flags { paths needs_cxx } {
35 global srcdir
36 global ld_library_path
37 global shlib_ext
38 global ubsan_saved_library_path
39
40 set gccpath ${paths}
41 set flags ""
42
43 set shlib_ext [get_shlib_extension]
44 set ubsan_saved_library_path $ld_library_path
45
46 # Providing -B instead of -L means that it works for targets that use
47 # spec substitution for handling -static-xxxxx, it also works for targets
48 # the use the startfile paths to provide a runpath for uninstalled test.
49 # Each -B option will produce a -L on the link line (for paths that exist).
50 if { $gccpath != "" } {
51 if { [file exists "${gccpath}/libsanitizer/ubsan/.libs/libubsan.a"]
52 || [file exists "${gccpath}/libsanitizer/ubsan/.libs/libubsan.${shlib_ext}"] } {
53 append flags " -B${gccpath}/libsanitizer/ "
54 append flags " -B${gccpath}/libsanitizer/ubsan/ "
55 append flags " -B${gccpath}/libsanitizer/ubsan/.libs"
56 append ld_library_path ":${gccpath}/libsanitizer/ubsan/.libs"
57 }
58 # libasan links to libstdc++, so we must include it for C testcases.
59 if { $needs_cxx && ( [file exists "${gccpath}/libstdc++-v3/src/.libs/libstdc++.a"]
60 || [file exists "${gccpath}/libstdc++-v3/src/.libs/libstdc++.${shlib_ext}"] ) } {
61 append flags " -B${gccpath}/libstdc++-v3/src/.libs "
62 append ld_library_path ":${gccpath}/libstdc++-v3/src/.libs"
63 }
64 } else {
65 global tool_root_dir
66
67 set libubsan [lookfor_file ${tool_root_dir} libubsan]
68 if { $libubsan != "" } {
69 append flags "-L${libubsan} "
70 append ld_library_path ":${libubsan}"
71 }
72 }
73
74 set_ld_library_path_env_vars
75
76 return "$flags"
77 }
78
79 #
80 # ubsan_init -- called at the start of each subdir of tests
81 #
82
83 proc ubsan_init { args } {
84 global TEST_ALWAYS_FLAGS
85 global ALWAYS_CXXFLAGS
86 global TOOL_OPTIONS
87 global ubsan_saved_TEST_ALWAYS_FLAGS
88 global ubsan_saved_ALWAYS_CXXFLAGS
89 global orig_ubsan_options_saved
90 global orig_ubsan_options
91 set needs_cxx [lindex $args 0]
92
93 if { $orig_ubsan_options_saved == 0 } {
94 # Save the original environment.
95 if [info exists env(UBSAN_OPTIONS)] {
96 set orig_ubsan_options "$env(UBSAN_OPTIONS)"
97 set orig_ubsan_options_saved 1
98 }
99 }
100 setenv UBSAN_OPTIONS color=never
101
102 set link_flags ""
103 if ![is_remote host] {
104 if [info exists TOOL_OPTIONS] {
105 set link_flags "[ubsan_link_flags [get_multilibs ${TOOL_OPTIONS}] $needs_cxx]"
106 } else {
107 set link_flags "[ubsan_link_flags [get_multilibs] $needs_cxx]"
108 }
109 }
110
111 if [info exists TEST_ALWAYS_FLAGS] {
112 set ubsan_saved_TEST_ALWAYS_FLAGS $TEST_ALWAYS_FLAGS
113 }
114 if [info exists ALWAYS_CXXFLAGS] {
115 set ubsan_saved_ALWAYS_CXXFLAGS $ALWAYS_CXXFLAGS
116 set ALWAYS_CXXFLAGS [concat "{ldflags=$link_flags}" $ALWAYS_CXXFLAGS]
117 } else {
118 if [info exists TEST_ALWAYS_FLAGS] {
119 set TEST_ALWAYS_FLAGS "$link_flags $TEST_ALWAYS_FLAGS"
120 } else {
121 set TEST_ALWAYS_FLAGS "$link_flags"
122 }
123 }
124 }
125
126 #
127 # ubsan_finish -- called at the end of each subdir of tests
128 #
129
130 proc ubsan_finish { args } {
131 global TEST_ALWAYS_FLAGS
132 global ubsan_saved_TEST_ALWAYS_FLAGS
133 global ubsan_saved_ALWAYS_CXXFLAGS
134 global ubsan_saved_library_path
135 global ld_library_path
136 global orig_ubsan_options_saved
137 global orig_ubsan_options
138
139 if { $orig_ubsan_options_saved } {
140 setenv UBSAN_OPTIONS "$orig_ubsan_options"
141 } elseif [info exists env(UBSAN_OPTIONS)] {
142 unsetenv UBSAN_OPTIONS
143 }
144
145 if [info exists ubsan_saved_ALWAYS_CXXFLAGS ] {
146 set ALWAYS_CXXFLAGS $ubsan_saved_ALWAYS_CXXFLAGS
147 } else {
148 if [info exists ubsan_saved_TEST_ALWAYS_FLAGS] {
149 set TEST_ALWAYS_FLAGS $ubsan_saved_TEST_ALWAYS_FLAGS
150 } else {
151 unset TEST_ALWAYS_FLAGS
152 }
153 }
154 if [info exists ubsan_saved_library_path ] {
155 set ld_library_path $ubsan_saved_library_path
156 set_ld_library_path_env_vars
157 }
158 clear_effective_target_cache
159 }