| 1 |
# Copyright (C) 2020-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 |
load_lib asan-dg.exp |
| 18 |
|
| 19 |
# Return 1 if target can compile a binary for hardware address |
| 20 |
# sanitization, 0 otherwise. |
| 21 |
# |
| 22 |
# NOTE: This should only be used between calls to hwasan_init and |
| 23 |
# hwasan_finish. It is therefore defined here rather than in |
| 24 |
# target-supports.exp. |
| 25 |
|
| 26 |
proc check_effective_target_fsanitize_hwaddress {} { |
| 27 |
if ![check_no_compiler_messages fsanitize_hwaddress executable { |
| 28 |
int main (void) { return 0; } |
| 29 |
}] { |
| 30 |
return 0; |
| 31 |
} |
| 32 |
return 1; |
| 33 |
} |
| 34 |
|
| 35 |
# Return 1 if target can compile and run a binary for hardware address |
| 36 |
# sanitization, 0 otherwise. |
| 37 |
# |
| 38 |
# NOTE: This should only be used between calls to hwasan_init and |
| 39 |
# hwasan_finish. It is therefore defined here rather than in |
| 40 |
# target-supports.exp. |
| 41 |
|
| 42 |
proc check_effective_target_hwaddress_exec {} { |
| 43 |
if ![check_runtime hwaddress_exec { |
| 44 |
#ifdef __cplusplus |
| 45 |
extern "C" { |
| 46 |
#endif |
| 47 |
extern int arch_prctl (int, unsigned long int *); |
| 48 |
extern int prctl(int, unsigned long, unsigned long, unsigned long, unsigned long); |
| 49 |
#ifdef __cplusplus |
| 50 |
} |
| 51 |
#endif |
| 52 |
int main (void) { |
| 53 |
#ifdef __x86_64__ |
| 54 |
# ifdef __LP64__ |
| 55 |
# define ARCH_GET_UNTAG_MASK 0x4001 |
| 56 |
# define LAM_U57_MASK (0x3fULL << 57) |
| 57 |
unsigned long mask = 0; |
| 58 |
if (arch_prctl(ARCH_GET_UNTAG_MASK, &mask) != 0) |
| 59 |
return 1; |
| 60 |
if (mask != ~LAM_U57_MASK) |
| 61 |
return 1; |
| 62 |
return 0; |
| 63 |
# endif |
| 64 |
return 1; |
| 65 |
#else |
| 66 |
#define PR_SET_TAGGED_ADDR_CTRL 55 |
| 67 |
#define PR_GET_TAGGED_ADDR_CTRL 56 |
| 68 |
#define PR_TAGGED_ADDR_ENABLE (1UL << 0) |
| 69 |
if (prctl (PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0) == -1) |
| 70 |
return 1; |
| 71 |
if (prctl(PR_SET_TAGGED_ADDR_CTRL, PR_TAGGED_ADDR_ENABLE, 0, 0, 0) == -1 |
| 72 |
|| !prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0)) |
| 73 |
return 1; |
| 74 |
return 0; |
| 75 |
#endif |
| 76 |
} |
| 77 |
}] { |
| 78 |
return 0; |
| 79 |
} |
| 80 |
return 1; |
| 81 |
} |
| 82 |
|
| 83 |
proc hwasan_include_flags {} { |
| 84 |
global srcdir |
| 85 |
global TESTING_IN_BUILD_TREE |
| 86 |
|
| 87 |
set flags "" |
| 88 |
|
| 89 |
if { [is_remote host] || ! [info exists TESTING_IN_BUILD_TREE] } { |
| 90 |
return "${flags}" |
| 91 |
} |
| 92 |
|
| 93 |
set flags "-I$srcdir/../../libsanitizer/include" |
| 94 |
|
| 95 |
return "$flags" |
| 96 |
} |
| 97 |
|
| 98 |
# |
| 99 |
# hwasan_link_flags -- compute library path and flags to find libhwasan. |
| 100 |
# (implementation in asan-dg.exp) |
| 101 |
# |
| 102 |
|
| 103 |
proc hwasan_link_flags { paths needs_cxx } { |
| 104 |
return [asan_link_flags_1 $paths hwasan $needs_cxx] |
| 105 |
} |
| 106 |
|
| 107 |
# |
| 108 |
# hwasan_init -- called at the start of each subdir of tests |
| 109 |
# |
| 110 |
|
| 111 |
proc hwasan_init { args } { |
| 112 |
global TEST_ALWAYS_FLAGS |
| 113 |
global ALWAYS_CXXFLAGS |
| 114 |
global TOOL_OPTIONS |
| 115 |
global hwasan_saved_TEST_ALWAYS_FLAGS |
| 116 |
global hwasan_saved_ALWAYS_CXXFLAGS |
| 117 |
set needs_cxx [lindex $args 0] |
| 118 |
|
| 119 |
setenv HWASAN_OPTIONS "random_tags=0" |
| 120 |
|
| 121 |
if [istarget x86_64-*-*] { |
| 122 |
set target_hwasan_flags "-mlam=u57" |
| 123 |
} else { |
| 124 |
set target_hwasan_flags "" |
| 125 |
} |
| 126 |
|
| 127 |
set link_flags "" |
| 128 |
if ![is_remote host] { |
| 129 |
if [info exists TOOL_OPTIONS] { |
| 130 |
set link_flags "[hwasan_link_flags [get_multilibs ${TOOL_OPTIONS}] $needs_cxx]" |
| 131 |
} else { |
| 132 |
set link_flags "[hwasan_link_flags [get_multilibs] $needs_cxx]" |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
set include_flags "[hwasan_include_flags]" |
| 137 |
|
| 138 |
if [info exists TEST_ALWAYS_FLAGS] { |
| 139 |
set hwasan_saved_TEST_ALWAYS_FLAGS $TEST_ALWAYS_FLAGS |
| 140 |
} |
| 141 |
if [info exists ALWAYS_CXXFLAGS] { |
| 142 |
set hwasan_saved_ALWAYS_CXXFLAGS $ALWAYS_CXXFLAGS |
| 143 |
set ALWAYS_CXXFLAGS [concat "{ldflags=$link_flags}" $ALWAYS_CXXFLAGS] |
| 144 |
set ALWAYS_CXXFLAGS [concat "{additional_flags=-fsanitize=hwaddress $target_hwasan_flags --param hwasan-random-frame-tag=0 -g $include_flags}" $ALWAYS_CXXFLAGS] |
| 145 |
} else { |
| 146 |
if [info exists TEST_ALWAYS_FLAGS] { |
| 147 |
set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=hwaddress $target_hwasan_flags --param hwasan-random-frame-tag=0 -g $include_flags $TEST_ALWAYS_FLAGS" |
| 148 |
} else { |
| 149 |
set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=hwaddress $target_hwasan_flags --param hwasan-random-frame-tag=0 -g $include_flags" |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
# |
| 155 |
# hwasan_finish -- called at the start of each subdir of tests |
| 156 |
# |
| 157 |
|
| 158 |
proc hwasan_finish { args } { |
| 159 |
global TEST_ALWAYS_FLAGS |
| 160 |
global hwasan_saved_TEST_ALWAYS_FLAGS |
| 161 |
global hwasan_saved_ALWAYS_CXXFLAGS |
| 162 |
global hwasan_saved_library_path |
| 163 |
global ld_library_path |
| 164 |
|
| 165 |
unsetenv HWASAN_OPTIONS |
| 166 |
|
| 167 |
if [info exists hwasan_saved_ALWAYS_CXXFLAGS ] { |
| 168 |
set ALWAYS_CXXFLAGS $hwasan_saved_ALWAYS_CXXFLAGS |
| 169 |
} else { |
| 170 |
if [info exists hwasan_saved_TEST_ALWAYS_FLAGS] { |
| 171 |
set TEST_ALWAYS_FLAGS $hwasan_saved_TEST_ALWAYS_FLAGS |
| 172 |
} else { |
| 173 |
unset TEST_ALWAYS_FLAGS |
| 174 |
} |
| 175 |
} |
| 176 |
if [info exists hwasan_saved_library_path] { |
| 177 |
set ld_library_path $hwasan_saved_library_path |
| 178 |
set_ld_library_path_env_vars |
| 179 |
} |
| 180 |
clear_effective_target_cache |
| 181 |
} |
| 182 |
|
| 183 |
# Utility for running gtest hwasan emulation under dejagnu, invoked via dg-final. |
| 184 |
# Call pass if variable has the desired value, otherwise fail. |
| 185 |
# |
| 186 |
# Argument 0 handles expected failures and the like |
| 187 |
proc hwasan-gtest { args } { |
| 188 |
asan-gtest {*}$args |
| 189 |
} |