annotate gcc/asan.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* AddressSanitizer, a fast memory error detector.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2011-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Contributed by Kostya Serebryany <kcc@google.com>
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
8 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
9 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
10 version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
15 for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
18 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
19 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 #ifndef TREE_ASAN
kono
parents:
diff changeset
22 #define TREE_ASAN
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 extern void asan_function_start (void);
kono
parents:
diff changeset
25 extern void asan_finish_file (void);
kono
parents:
diff changeset
26 extern rtx_insn *asan_emit_stack_protection (rtx, rtx, unsigned int,
kono
parents:
diff changeset
27 HOST_WIDE_INT *, tree *, int);
kono
parents:
diff changeset
28 extern rtx_insn *asan_emit_allocas_unpoison (rtx, rtx, rtx_insn *);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
29 extern bool asan_protect_global (tree, bool ignore_decl_rtl_set_p = false);
111
kono
parents:
diff changeset
30 extern void initialize_sanitizer_builtins (void);
kono
parents:
diff changeset
31 extern tree asan_dynamic_init_call (bool);
kono
parents:
diff changeset
32 extern bool asan_expand_check_ifn (gimple_stmt_iterator *, bool);
kono
parents:
diff changeset
33 extern bool asan_expand_mark_ifn (gimple_stmt_iterator *);
kono
parents:
diff changeset
34 extern bool asan_expand_poison_ifn (gimple_stmt_iterator *, bool *,
kono
parents:
diff changeset
35 hash_map<tree, tree> &);
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 extern gimple_stmt_iterator create_cond_insert_point
kono
parents:
diff changeset
38 (gimple_stmt_iterator *, bool, bool, bool, basic_block *, basic_block *);
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 /* Alias set for accessing the shadow memory. */
kono
parents:
diff changeset
41 extern alias_set_type asan_shadow_set;
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 /* Hash set of labels that are either used in a goto, or their address
kono
parents:
diff changeset
44 has been taken. */
kono
parents:
diff changeset
45 extern hash_set <tree> *asan_used_labels;
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 /* Shadow memory is found at
kono
parents:
diff changeset
48 (address >> ASAN_SHADOW_SHIFT) + asan_shadow_offset (). */
kono
parents:
diff changeset
49 #define ASAN_SHADOW_SHIFT 3
kono
parents:
diff changeset
50 #define ASAN_SHADOW_GRANULARITY (1UL << ASAN_SHADOW_SHIFT)
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 /* Red zone size, stack and global variables are padded by ASAN_RED_ZONE_SIZE
kono
parents:
diff changeset
53 up to 2 * ASAN_RED_ZONE_SIZE - 1 bytes. */
kono
parents:
diff changeset
54 #define ASAN_RED_ZONE_SIZE 32
kono
parents:
diff changeset
55
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
56 /* Stack variable use more compact red zones. The size includes also
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
57 size of variable itself. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
58
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
59 #define ASAN_MIN_RED_ZONE_SIZE 16
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
60
111
kono
parents:
diff changeset
61 /* Shadow memory values for stack protection. Left is below protected vars,
kono
parents:
diff changeset
62 the first pointer in stack corresponding to that offset contains
kono
parents:
diff changeset
63 ASAN_STACK_FRAME_MAGIC word, the second pointer to a string describing
kono
parents:
diff changeset
64 the frame. Middle is for padding in between variables, right is
kono
parents:
diff changeset
65 above the last protected variable and partial immediately after variables
kono
parents:
diff changeset
66 up to ASAN_RED_ZONE_SIZE alignment. */
kono
parents:
diff changeset
67 #define ASAN_STACK_MAGIC_LEFT 0xf1
kono
parents:
diff changeset
68 #define ASAN_STACK_MAGIC_MIDDLE 0xf2
kono
parents:
diff changeset
69 #define ASAN_STACK_MAGIC_RIGHT 0xf3
kono
parents:
diff changeset
70 #define ASAN_STACK_MAGIC_USE_AFTER_RET 0xf5
kono
parents:
diff changeset
71 #define ASAN_STACK_MAGIC_USE_AFTER_SCOPE 0xf8
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 #define ASAN_STACK_FRAME_MAGIC 0x41b58ab3
kono
parents:
diff changeset
74 #define ASAN_STACK_RETIRED_MAGIC 0x45e0360e
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 #define ASAN_USE_AFTER_SCOPE_ATTRIBUTE "use after scope memory"
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 /* Various flags for Asan builtins. */
kono
parents:
diff changeset
79 enum asan_check_flags
kono
parents:
diff changeset
80 {
kono
parents:
diff changeset
81 ASAN_CHECK_STORE = 1 << 0,
kono
parents:
diff changeset
82 ASAN_CHECK_SCALAR_ACCESS = 1 << 1,
kono
parents:
diff changeset
83 ASAN_CHECK_NON_ZERO_LEN = 1 << 2,
kono
parents:
diff changeset
84 ASAN_CHECK_LAST = 1 << 3
kono
parents:
diff changeset
85 };
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 /* Flags for Asan check builtins. */
kono
parents:
diff changeset
88 #define IFN_ASAN_MARK_FLAGS DEF(POISON), DEF(UNPOISON)
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 enum asan_mark_flags
kono
parents:
diff changeset
91 {
kono
parents:
diff changeset
92 #define DEF(X) ASAN_MARK_##X
kono
parents:
diff changeset
93 IFN_ASAN_MARK_FLAGS
kono
parents:
diff changeset
94 #undef DEF
kono
parents:
diff changeset
95 };
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 /* Return true if STMT is ASAN_MARK with FLAG as first argument. */
kono
parents:
diff changeset
98 extern bool asan_mark_p (gimple *stmt, enum asan_mark_flags flag);
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 /* Return the size of padding needed to insert after a protected
kono
parents:
diff changeset
101 decl of SIZE. */
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 static inline unsigned int
kono
parents:
diff changeset
104 asan_red_zone_size (unsigned int size)
kono
parents:
diff changeset
105 {
kono
parents:
diff changeset
106 unsigned int c = size & (ASAN_RED_ZONE_SIZE - 1);
kono
parents:
diff changeset
107 return c ? 2 * ASAN_RED_ZONE_SIZE - c : ASAN_RED_ZONE_SIZE;
kono
parents:
diff changeset
108 }
kono
parents:
diff changeset
109
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
110 /* Return how much a stack variable occupis on a stack
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
111 including a space for red zone. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
112
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
113 static inline unsigned HOST_WIDE_INT
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
114 asan_var_and_redzone_size (unsigned HOST_WIDE_INT size)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
115 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
116 if (size <= 4)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
117 return 16;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
118 else if (size <= 16)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
119 return 32;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
120 else if (size <= 128)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
121 return size + 32;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
122 else if (size <= 512)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
123 return size + 64;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
124 else if (size <= 4096)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
125 return size + 128;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
126 else
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
127 return size + 256;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
128 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
129
111
kono
parents:
diff changeset
130 extern bool set_asan_shadow_offset (const char *);
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 extern void set_sanitized_sections (const char *);
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 extern bool asan_sanitize_stack_p (void);
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 extern bool asan_sanitize_allocas_p (void);
kono
parents:
diff changeset
137
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
138 extern hash_set<tree> *asan_handled_variables;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
139
111
kono
parents:
diff changeset
140 /* Return TRUE if builtin with given FCODE will be intercepted by
kono
parents:
diff changeset
141 libasan. */
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 static inline bool
kono
parents:
diff changeset
144 asan_intercepted_p (enum built_in_function fcode)
kono
parents:
diff changeset
145 {
kono
parents:
diff changeset
146 return fcode == BUILT_IN_INDEX
kono
parents:
diff changeset
147 || fcode == BUILT_IN_MEMCHR
kono
parents:
diff changeset
148 || fcode == BUILT_IN_MEMCMP
kono
parents:
diff changeset
149 || fcode == BUILT_IN_MEMCPY
kono
parents:
diff changeset
150 || fcode == BUILT_IN_MEMMOVE
kono
parents:
diff changeset
151 || fcode == BUILT_IN_MEMSET
kono
parents:
diff changeset
152 || fcode == BUILT_IN_STRCASECMP
kono
parents:
diff changeset
153 || fcode == BUILT_IN_STRCAT
kono
parents:
diff changeset
154 || fcode == BUILT_IN_STRCHR
kono
parents:
diff changeset
155 || fcode == BUILT_IN_STRCMP
kono
parents:
diff changeset
156 || fcode == BUILT_IN_STRCPY
kono
parents:
diff changeset
157 || fcode == BUILT_IN_STRDUP
kono
parents:
diff changeset
158 || fcode == BUILT_IN_STRLEN
kono
parents:
diff changeset
159 || fcode == BUILT_IN_STRNCASECMP
kono
parents:
diff changeset
160 || fcode == BUILT_IN_STRNCAT
kono
parents:
diff changeset
161 || fcode == BUILT_IN_STRNCMP
kono
parents:
diff changeset
162 || fcode == BUILT_IN_STRCSPN
kono
parents:
diff changeset
163 || fcode == BUILT_IN_STRPBRK
kono
parents:
diff changeset
164 || fcode == BUILT_IN_STRSPN
kono
parents:
diff changeset
165 || fcode == BUILT_IN_STRSTR
kono
parents:
diff changeset
166 || fcode == BUILT_IN_STRNCPY;
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 /* Return TRUE if we should instrument for use-after-scope sanity checking. */
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 static inline bool
kono
parents:
diff changeset
172 asan_sanitize_use_after_scope (void)
kono
parents:
diff changeset
173 {
kono
parents:
diff changeset
174 return (flag_sanitize_address_use_after_scope && asan_sanitize_stack_p ());
kono
parents:
diff changeset
175 }
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 /* Return true if DECL should be guarded on the stack. */
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 static inline bool
kono
parents:
diff changeset
180 asan_protect_stack_decl (tree decl)
kono
parents:
diff changeset
181 {
kono
parents:
diff changeset
182 return DECL_P (decl)
kono
parents:
diff changeset
183 && (!DECL_ARTIFICIAL (decl)
kono
parents:
diff changeset
184 || (asan_sanitize_use_after_scope () && TREE_ADDRESSABLE (decl)));
kono
parents:
diff changeset
185 }
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 /* Return true when flag_sanitize & FLAG is non-zero. If FN is non-null,
kono
parents:
diff changeset
188 remove all flags mentioned in "no_sanitize" of DECL_ATTRIBUTES. */
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 static inline bool
kono
parents:
diff changeset
191 sanitize_flags_p (unsigned int flag, const_tree fn = current_function_decl)
kono
parents:
diff changeset
192 {
kono
parents:
diff changeset
193 unsigned int result_flags = flag_sanitize & flag;
kono
parents:
diff changeset
194 if (result_flags == 0)
kono
parents:
diff changeset
195 return false;
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 if (fn != NULL_TREE)
kono
parents:
diff changeset
198 {
kono
parents:
diff changeset
199 tree value = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (fn));
kono
parents:
diff changeset
200 if (value)
kono
parents:
diff changeset
201 result_flags &= ~tree_to_uhwi (TREE_VALUE (value));
kono
parents:
diff changeset
202 }
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 return result_flags;
kono
parents:
diff changeset
205 }
kono
parents:
diff changeset
206
kono
parents:
diff changeset
207 #endif /* TREE_ASAN */