GCC Middle and Back End API Reference
asan.h
Go to the documentation of this file.
1/* AddressSanitizer, a fast memory error detector.
2 Copyright (C) 2011-2026 Free Software Foundation, Inc.
3 Contributed by Kostya Serebryany <kcc@google.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef TREE_ASAN
22#define TREE_ASAN
23
24extern void asan_function_start (void);
25extern void asan_finish_file (void);
26extern rtx_insn *asan_emit_stack_protection (rtx, rtx, unsigned int,
27 HOST_WIDE_INT *, tree *, int);
29extern bool asan_protect_global (tree, bool ignore_decl_rtl_set_p = false);
30extern void initialize_sanitizer_builtins (void);
31extern tree asan_dynamic_init_call (bool);
34extern bool asan_expand_poison_ifn (gimple_stmt_iterator *, bool *,
36extern rtx asan_memfn_rtl (tree);
37
38extern void
40
41extern void hwasan_record_frame_init ();
43extern void hwasan_emit_prologue ();
46extern rtx hwasan_frame_base ();
47extern void hwasan_maybe_emit_frame_base_init (void);
48extern bool stack_vars_base_reg_p (rtx);
49extern uint8_t hwasan_current_frame_tag ();
50extern void hwasan_increment_frame_tag ();
52extern void hwasan_finish_file (void);
53extern bool hwasan_sanitize_p (void);
54extern bool hwasan_sanitize_stack_p (void);
55extern bool hwasan_sanitize_allocas_p (void);
58extern bool gate_hwasan (void);
59
60extern bool memtag_sanitize_p (void);
61extern bool memtag_sanitize_stack_p (void);
62extern bool memtag_sanitize_allocas_p (void);
63extern bool gate_memtag (void);
64
65bool hwassist_sanitize_p (void);
67
69 (gimple_stmt_iterator *, bool, bool, bool, basic_block *, basic_block *);
70
71/* Alias set for accessing the shadow memory. */
73
74/* Hash set of labels that are either used in a goto, or their address
75 has been taken. */
76extern hash_set <tree> *asan_used_labels;
77
78/* Shadow memory is found at
79 (address >> ASAN_SHADOW_SHIFT) + asan_shadow_offset (). */
80#define ASAN_SHADOW_SHIFT 3
81#define ASAN_SHADOW_GRANULARITY (1UL << ASAN_SHADOW_SHIFT)
82
83/* Red zone size, stack and global variables are padded by ASAN_RED_ZONE_SIZE
84 up to 2 * ASAN_RED_ZONE_SIZE - 1 bytes. */
85#define ASAN_RED_ZONE_SIZE 32
86
87/* Stack variable use more compact red zones. The size includes also
88 size of variable itself. */
89
90#define ASAN_MIN_RED_ZONE_SIZE 16
91
92/* Shadow memory values for stack protection. Left is below protected vars,
93 the first pointer in stack corresponding to that offset contains
94 ASAN_STACK_FRAME_MAGIC word, the second pointer to a string describing
95 the frame. Middle is for padding in between variables, right is
96 above the last protected variable and partial immediately after variables
97 up to ASAN_RED_ZONE_SIZE alignment. */
98#define ASAN_STACK_MAGIC_LEFT 0xf1
99#define ASAN_STACK_MAGIC_MIDDLE 0xf2
100#define ASAN_STACK_MAGIC_RIGHT 0xf3
101#define ASAN_STACK_MAGIC_USE_AFTER_RET 0xf5
102#define ASAN_STACK_MAGIC_USE_AFTER_SCOPE 0xf8
103
104#define ASAN_STACK_FRAME_MAGIC 0x41b58ab3
105#define ASAN_STACK_RETIRED_MAGIC 0x45e0360e
106
107#define ASAN_USE_AFTER_SCOPE_ATTRIBUTE "use after scope memory"
108
109/* NOTE: The values below and the hooks under targetm.memtag define an ABI and
110 are hard-coded to these values in libhwasan, hence they can't be changed
111 independently here. */
112/* How many bits are used to store a tag in a pointer.
113 The default version uses the entire top byte of a pointer (i.e. 8 bits). */
114#define HWASAN_TAG_SIZE targetm.memtag.tag_bitsize ()
115/* Tag Granule of HWASAN shadow stack.
116 This is the size in real memory that each byte in the shadow memory refers
117 to. I.e. if a variable is X bytes long in memory then its tag in shadow
118 memory will span X / HWASAN_TAG_GRANULE_SIZE bytes.
119 Most variables will need to be aligned to this amount since two variables
120 that are neighbors in memory and share a tag granule would need to share the
121 same tag (the shared tag granule can only store one tag). */
122#define HWASAN_TAG_GRANULE_SIZE targetm.memtag.granule_size ()
123/* Define the tag for the stack background.
124 This defines what tag the stack pointer will be and hence what tag all
125 variables that are not given special tags are (e.g. spilled registers,
126 and parameters passed on the stack). */
127#define HWASAN_STACK_BACKGROUND gen_int_mode (0, QImode)
128
129/* Various flags for Asan builtins. */
137
138/* Flags for Asan check builtins. */
139#define IFN_ASAN_MARK_FLAGS DEF(POISON), DEF(UNPOISON)
140
142{
143#define DEF(X) ASAN_MARK_##X
145#undef DEF
146};
147
148/* Return true if STMT is ASAN_MARK with FLAG as first argument. */
149extern bool asan_mark_p (gimple *stmt, enum asan_mark_flags flag);
150
151/* Return the size of padding needed to insert after a protected
152 decl of SIZE. */
153
154inline unsigned int
155asan_red_zone_size (unsigned int size)
156{
157 unsigned int c = size & (ASAN_RED_ZONE_SIZE - 1);
158 return c ? 2 * ASAN_RED_ZONE_SIZE - c : ASAN_RED_ZONE_SIZE;
159}
160
161/* Return how much a stack variable occupis on a stack
162 including a space for red zone. */
163
164inline unsigned HOST_WIDE_INT
165asan_var_and_redzone_size (unsigned HOST_WIDE_INT size)
166{
167 if (size <= 4)
168 return 16;
169 else if (size <= 16)
170 return 32;
171 else if (size <= 128)
172 return size + 32;
173 else if (size <= 512)
174 return size + 64;
175 else if (size <= 4096)
176 return size + 128;
177 else
178 return size + 256;
179}
180
181extern bool set_asan_shadow_offset (const char *);
182
183extern bool asan_shadow_offset_set_p ();
184
185extern void set_sanitized_sections (const char *);
186
187extern bool asan_sanitize_stack_p (void);
188
189extern bool asan_sanitize_allocas_p (void);
190
192
193/* Return TRUE if builtin with given FCODE will be intercepted by
194 libasan. */
195
196inline bool
198{
199 /* This list should be kept up-to-date with upstream's version at
200 compiler-rt/lib/hwasan/hwasan_platform_interceptors.h. */
201 if (hwasan_sanitize_p ())
202 return fcode == BUILT_IN_MEMCMP
203 || fcode == BUILT_IN_MEMCPY
204 || fcode == BUILT_IN_MEMMOVE
205 || fcode == BUILT_IN_MEMSET;
206
207 return fcode == BUILT_IN_INDEX
208 || fcode == BUILT_IN_MEMCHR
209 || fcode == BUILT_IN_MEMCMP
210 || fcode == BUILT_IN_MEMCPY
211 || fcode == BUILT_IN_MEMMOVE
212 || fcode == BUILT_IN_MEMSET
213 || fcode == BUILT_IN_STRCASECMP
214 || fcode == BUILT_IN_STRCAT
215 || fcode == BUILT_IN_STRCHR
216 || fcode == BUILT_IN_STRCMP
217 || fcode == BUILT_IN_STRCPY
218 || fcode == BUILT_IN_STRDUP
219 || fcode == BUILT_IN_STRLEN
220 || fcode == BUILT_IN_STRNCASECMP
221 || fcode == BUILT_IN_STRNCAT
222 || fcode == BUILT_IN_STRNCMP
223 || fcode == BUILT_IN_STRCSPN
224 || fcode == BUILT_IN_STRPBRK
225 || fcode == BUILT_IN_STRSPN
226 || fcode == BUILT_IN_STRSTR
227 || fcode == BUILT_IN_STRNCPY;
228}
229
230/* Return TRUE if we should instrument for use-after-scope sanity checking. */
231
232inline bool
234{
235 return (flag_sanitize_address_use_after_scope
237}
238
239/* Return true if DECL should be guarded on the stack. */
240
241inline bool
248
249/* Return true when flag_sanitize & FLAG is non-zero. If FN is non-null,
250 remove all flags mentioned in "no_sanitize" of DECL_ATTRIBUTES. */
251
252inline bool
255{
256 sanitize_code_type result_flags = flag_sanitize & flag;
257 if (result_flags == 0)
258 return false;
259
260 if (fn != NULL_TREE)
261 {
262 tree value = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (fn));
263 if (value)
264 result_flags &= ~tree_to_uhwi (TREE_VALUE (value));
265 }
266
267 return result_flags;
268}
269
270/* Return true when coverage sanitization should happend for FN function. */
271
272inline bool
274{
275 return (flag_sanitize_coverage
276 && (fn == NULL_TREE
277 || lookup_attribute ("no_sanitize_coverage",
278 DECL_ATTRIBUTES (fn)) == NULL_TREE));
279}
280
281#endif /* TREE_ASAN */
hash_set< tree > * asan_used_labels
Definition asan.cc:260
bool hwasan_sanitize_p()
Definition asan.cc:1866
alias_set_type asan_shadow_set
Definition asan.cc:474
bool asan_sanitize_stack_p(void)
Definition asan.cc:364
bool hwassist_sanitize_stack_p()
Definition asan.cc:1978
hash_set< tree > * asan_handled_variables
Definition asan.cc:258
void asan_finish_file(void)
Definition asan.cc:3796
void hwasan_record_frame_init()
Definition asan.cc:4696
bool sanitize_flags_p(sanitize_code_type flag, const_tree fn=current_function_decl)
Definition asan.h:253
rtx_insn * hwasan_emit_untag_frame(rtx, rtx)
Definition asan.cc:4820
rtx_insn * asan_emit_allocas_unpoison(rtx, rtx, rtx_insn *)
Definition asan.cc:2388
void hwasan_maybe_emit_frame_base_init(void)
Definition asan.cc:4594
bool gate_hwasan(void)
Definition asan.cc:5036
bool hwasan_expand_mark_ifn(gimple_stmt_iterator *)
Definition asan.cc:5030
rtx_insn * asan_emit_stack_protection(rtx, rtx, unsigned int, HOST_WIDE_INT *, tree *, int)
Definition asan.cc:1997
bool asan_intercepted_p(enum built_in_function fcode)
Definition asan.h:197
bool memtag_sanitize_stack_p(void)
Definition asan.cc:1949
bool asan_protect_stack_decl(tree decl)
Definition asan.h:242
bool asan_expand_check_ifn(gimple_stmt_iterator *, bool)
Definition asan.cc:4053
gimple_stmt_iterator create_cond_insert_point(gimple_stmt_iterator *, bool, bool, bool, basic_block *, basic_block *)
Definition asan.cc:2606
tree asan_dynamic_init_call(bool)
Definition asan.cc:3328
bool sanitize_coverage_p(const_tree fn=current_function_decl)
Definition asan.h:273
rtx asan_memfn_rtl(tree)
Definition asan.cc:398
#define IFN_ASAN_MARK_FLAGS
Definition asan.h:139
bool hwasan_sanitize_p(void)
Definition asan.cc:1866
rtx hwasan_truncate_to_tag_size(rtx, rtx)
Definition asan.cc:4891
bool asan_sanitize_use_after_scope(void)
Definition asan.h:233
#define ASAN_RED_ZONE_SIZE
Definition asan.h:85
bool gate_memtag(void)
Definition asan.cc:5042
bool hwassist_sanitize_stack_p(void)
Definition asan.cc:1978
bool asan_protect_global(tree, bool ignore_decl_rtl_set_p=false)
Definition asan.cc:2429
bool asan_sanitize_allocas_p(void)
Definition asan.cc:370
void set_sanitized_sections(const char *)
Definition asan.cc:338
asan_mark_flags
Definition asan.h:142
bool stack_vars_base_reg_p(rtx)
Definition asan.cc:4576
unsigned HOST_WIDE_INT asan_var_and_redzone_size(unsigned HOST_WIDE_INT size)
Definition asan.h:165
void hwasan_finish_file(void)
Definition asan.cc:4868
bool asan_expand_mark_ifn(gimple_stmt_iterator *)
Definition asan.cc:3939
bool asan_expand_poison_ifn(gimple_stmt_iterator *, bool *, hash_map< tree, tree > &)
Definition asan.cc:4289
void hwasan_increment_frame_tag()
Definition asan.cc:4648
void asan_function_start(void)
Definition asan.cc:1559
void asan_maybe_insert_dynamic_shadow_at_function_entry(function *)
Definition asan.cc:513
bool set_asan_shadow_offset(const char *)
Definition asan.cc:317
bool memtag_sanitize_allocas_p(void)
Definition asan.cc:1956
asan_check_flags
Definition asan.h:131
@ ASAN_CHECK_NON_ZERO_LEN
Definition asan.h:134
@ ASAN_CHECK_LAST
Definition asan.h:135
@ ASAN_CHECK_SCALAR_ACCESS
Definition asan.h:133
@ ASAN_CHECK_STORE
Definition asan.h:132
bool asan_mark_p(gimple *stmt, enum asan_mark_flags flag)
Definition asan.cc:357
bool hwasan_expand_check_ifn(gimple_stmt_iterator *, bool)
Definition asan.cc:4944
rtx hwasan_get_frame_extent()
Definition asan.cc:4637
rtx hwasan_frame_base()
Definition asan.cc:4555
bool asan_shadow_offset_set_p()
Definition asan.cc:469
bool asan_sanitize_stack_p(void)
Definition asan.cc:364
void initialize_sanitizer_builtins(void)
Definition asan.cc:3576
bool memtag_sanitize_p(void)
Definition asan.cc:1942
bool hwasan_sanitize_allocas_p(void)
Definition asan.cc:1880
bool hwassist_sanitize_p(void)
Definition asan.cc:1971
void hwasan_record_stack_var(rtx, rtx, poly_int64, poly_int64)
Definition asan.cc:4617
uint8_t hwasan_current_frame_tag()
Definition asan.cc:4543
unsigned int asan_red_zone_size(unsigned int size)
Definition asan.h:155
bool hwasan_sanitize_stack_p(void)
Definition asan.cc:1873
void hwasan_emit_prologue()
Definition asan.cc:4736
tree lookup_attribute(const char *attr_name, tree list)
Definition attribs.h:223
Definition hash-map.h:40
Definition hash-set.h:37
struct basic_block_def * basic_block
Definition coretypes.h:372
struct rtx_def * rtx
Definition coretypes.h:57
const union tree_node * const_tree
Definition coretypes.h:98
union tree_node * tree
Definition coretypes.h:97
int alias_set_type
Definition coretypes.h:366
uint64_t sanitize_code_type
Definition flag-types.h:359
built_in_function
Definition genmatch.cc:1009
poly_int< NUM_POLY_INT_COEFFS, HOST_WIDE_INT > poly_int64
Definition poly-int-types.h:24
Definition genautomata.cc:499
Definition function.h:249
Definition gimple-iterator.h:26
Definition gimple.h:221
Definition rtl.h:546
tree current_function_decl
Definition toplev.cc:129
unsigned HOST_WIDE_INT tree_to_uhwi(const_tree t)
Definition tree.cc:6676
#define TREE_VALUE(NODE)
Definition tree.h:1241
#define DECL_ATTRIBUTES(NODE)
Definition tree.h:2894
#define DECL_ARTIFICIAL(NODE)
Definition tree.h:3020
#define TREE_ADDRESSABLE(NODE)
Definition tree.h:745
#define NULL_TREE
Definition tree.h:318
#define DECL_P(NODE)
Definition tree.h:232