annotate gcc/asan.h @ 16:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
kono
parents:
diff changeset
1 /* AddressSanitizer, a fast memory error detector.
kono
parents:
diff changeset
2 Copyright (C) 2011-2017 Free Software Foundation, Inc.
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 *);
kono
parents:
diff changeset
29 extern bool asan_protect_global (tree);
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
kono
parents:
diff changeset
56 /* Shadow memory values for stack protection. Left is below protected vars,
kono
parents:
diff changeset
57 the first pointer in stack corresponding to that offset contains
kono
parents:
diff changeset
58 ASAN_STACK_FRAME_MAGIC word, the second pointer to a string describing
kono
parents:
diff changeset
59 the frame. Middle is for padding in between variables, right is
kono
parents:
diff changeset
60 above the last protected variable and partial immediately after variables
kono
parents:
diff changeset
61 up to ASAN_RED_ZONE_SIZE alignment. */
kono
parents:
diff changeset
62 #define ASAN_STACK_MAGIC_LEFT 0xf1
kono
parents:
diff changeset
63 #define ASAN_STACK_MAGIC_MIDDLE 0xf2
kono
parents:
diff changeset
64 #define ASAN_STACK_MAGIC_RIGHT 0xf3
kono
parents:
diff changeset
65 #define ASAN_STACK_MAGIC_USE_AFTER_RET 0xf5
kono
parents:
diff changeset
66 #define ASAN_STACK_MAGIC_USE_AFTER_SCOPE 0xf8
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 #define ASAN_STACK_FRAME_MAGIC 0x41b58ab3
kono
parents:
diff changeset
69 #define ASAN_STACK_RETIRED_MAGIC 0x45e0360e
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 #define ASAN_USE_AFTER_SCOPE_ATTRIBUTE "use after scope memory"
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 /* Various flags for Asan builtins. */
kono
parents:
diff changeset
74 enum asan_check_flags
kono
parents:
diff changeset
75 {
kono
parents:
diff changeset
76 ASAN_CHECK_STORE = 1 << 0,
kono
parents:
diff changeset
77 ASAN_CHECK_SCALAR_ACCESS = 1 << 1,
kono
parents:
diff changeset
78 ASAN_CHECK_NON_ZERO_LEN = 1 << 2,
kono
parents:
diff changeset
79 ASAN_CHECK_LAST = 1 << 3
kono
parents:
diff changeset
80 };
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 /* Flags for Asan check builtins. */
kono
parents:
diff changeset
83 #define IFN_ASAN_MARK_FLAGS DEF(POISON), DEF(UNPOISON)
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 enum asan_mark_flags
kono
parents:
diff changeset
86 {
kono
parents:
diff changeset
87 #define DEF(X) ASAN_MARK_##X
kono
parents:
diff changeset
88 IFN_ASAN_MARK_FLAGS
kono
parents:
diff changeset
89 #undef DEF
kono
parents:
diff changeset
90 };
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 /* Return true if STMT is ASAN_MARK with FLAG as first argument. */
kono
parents:
diff changeset
93 extern bool asan_mark_p (gimple *stmt, enum asan_mark_flags flag);
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 /* Return the size of padding needed to insert after a protected
kono
parents:
diff changeset
96 decl of SIZE. */
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 static inline unsigned int
kono
parents:
diff changeset
99 asan_red_zone_size (unsigned int size)
kono
parents:
diff changeset
100 {
kono
parents:
diff changeset
101 unsigned int c = size & (ASAN_RED_ZONE_SIZE - 1);
kono
parents:
diff changeset
102 return c ? 2 * ASAN_RED_ZONE_SIZE - c : ASAN_RED_ZONE_SIZE;
kono
parents:
diff changeset
103 }
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 extern bool set_asan_shadow_offset (const char *);
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 extern void set_sanitized_sections (const char *);
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 extern bool asan_sanitize_stack_p (void);
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 extern bool asan_sanitize_allocas_p (void);
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 /* Return TRUE if builtin with given FCODE will be intercepted by
kono
parents:
diff changeset
114 libasan. */
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 static inline bool
kono
parents:
diff changeset
117 asan_intercepted_p (enum built_in_function fcode)
kono
parents:
diff changeset
118 {
kono
parents:
diff changeset
119 return fcode == BUILT_IN_INDEX
kono
parents:
diff changeset
120 || fcode == BUILT_IN_MEMCHR
kono
parents:
diff changeset
121 || fcode == BUILT_IN_MEMCMP
kono
parents:
diff changeset
122 || fcode == BUILT_IN_MEMCPY
kono
parents:
diff changeset
123 || fcode == BUILT_IN_MEMMOVE
kono
parents:
diff changeset
124 || fcode == BUILT_IN_MEMSET
kono
parents:
diff changeset
125 || fcode == BUILT_IN_STRCASECMP
kono
parents:
diff changeset
126 || fcode == BUILT_IN_STRCAT
kono
parents:
diff changeset
127 || fcode == BUILT_IN_STRCHR
kono
parents:
diff changeset
128 || fcode == BUILT_IN_STRCMP
kono
parents:
diff changeset
129 || fcode == BUILT_IN_STRCPY
kono
parents:
diff changeset
130 || fcode == BUILT_IN_STRDUP
kono
parents:
diff changeset
131 || fcode == BUILT_IN_STRLEN
kono
parents:
diff changeset
132 || fcode == BUILT_IN_STRNCASECMP
kono
parents:
diff changeset
133 || fcode == BUILT_IN_STRNCAT
kono
parents:
diff changeset
134 || fcode == BUILT_IN_STRNCMP
kono
parents:
diff changeset
135 || fcode == BUILT_IN_STRCSPN
kono
parents:
diff changeset
136 || fcode == BUILT_IN_STRPBRK
kono
parents:
diff changeset
137 || fcode == BUILT_IN_STRSPN
kono
parents:
diff changeset
138 || fcode == BUILT_IN_STRSTR
kono
parents:
diff changeset
139 || fcode == BUILT_IN_STRNCPY;
kono
parents:
diff changeset
140 }
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 /* Return TRUE if we should instrument for use-after-scope sanity checking. */
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 static inline bool
kono
parents:
diff changeset
145 asan_sanitize_use_after_scope (void)
kono
parents:
diff changeset
146 {
kono
parents:
diff changeset
147 return (flag_sanitize_address_use_after_scope && asan_sanitize_stack_p ());
kono
parents:
diff changeset
148 }
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 /* Return true if DECL should be guarded on the stack. */
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 static inline bool
kono
parents:
diff changeset
153 asan_protect_stack_decl (tree decl)
kono
parents:
diff changeset
154 {
kono
parents:
diff changeset
155 return DECL_P (decl)
kono
parents:
diff changeset
156 && (!DECL_ARTIFICIAL (decl)
kono
parents:
diff changeset
157 || (asan_sanitize_use_after_scope () && TREE_ADDRESSABLE (decl)));
kono
parents:
diff changeset
158 }
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 /* Return true when flag_sanitize & FLAG is non-zero. If FN is non-null,
kono
parents:
diff changeset
161 remove all flags mentioned in "no_sanitize" of DECL_ATTRIBUTES. */
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 static inline bool
kono
parents:
diff changeset
164 sanitize_flags_p (unsigned int flag, const_tree fn = current_function_decl)
kono
parents:
diff changeset
165 {
kono
parents:
diff changeset
166 unsigned int result_flags = flag_sanitize & flag;
kono
parents:
diff changeset
167 if (result_flags == 0)
kono
parents:
diff changeset
168 return false;
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 if (fn != NULL_TREE)
kono
parents:
diff changeset
171 {
kono
parents:
diff changeset
172 tree value = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (fn));
kono
parents:
diff changeset
173 if (value)
kono
parents:
diff changeset
174 result_flags &= ~tree_to_uhwi (TREE_VALUE (value));
kono
parents:
diff changeset
175 }
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 return result_flags;
kono
parents:
diff changeset
178 }
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 #endif /* TREE_ASAN */