File: | build/gcc/ubsan.cc |
Warning: | line 598, column 21 Assigned value is garbage or undefined |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* UndefinedBehaviorSanitizer, undefined behavior detector. | |||
2 | Copyright (C) 2013-2023 Free Software Foundation, Inc. | |||
3 | Contributed by Marek Polacek <polacek@redhat.com> | |||
4 | ||||
5 | This file is part of GCC. | |||
6 | ||||
7 | GCC is free software; you can redistribute it and/or modify it under | |||
8 | the terms of the GNU General Public License as published by the Free | |||
9 | Software Foundation; either version 3, or (at your option) any later | |||
10 | version. | |||
11 | ||||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |||
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |||
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |||
15 | for more details. | |||
16 | ||||
17 | You should have received a copy of the GNU General Public License | |||
18 | along with GCC; see the file COPYING3. If not see | |||
19 | <http://www.gnu.org/licenses/>. */ | |||
20 | ||||
21 | #include "config.h" | |||
22 | #include "system.h" | |||
23 | #include "coretypes.h" | |||
24 | #include "backend.h" | |||
25 | #include "rtl.h" | |||
26 | #include "c-family/c-common.h" | |||
27 | #include "gimple.h" | |||
28 | #include "cfghooks.h" | |||
29 | #include "tree-pass.h" | |||
30 | #include "memmodel.h" | |||
31 | #include "tm_p.h" | |||
32 | #include "ssa.h" | |||
33 | #include "cgraph.h" | |||
34 | #include "tree-pretty-print.h" | |||
35 | #include "stor-layout.h" | |||
36 | #include "cfganal.h" | |||
37 | #include "gimple-iterator.h" | |||
38 | #include "output.h" | |||
39 | #include "cfgloop.h" | |||
40 | #include "ubsan.h" | |||
41 | #include "expr.h" | |||
42 | #include "stringpool.h" | |||
43 | #include "attribs.h" | |||
44 | #include "asan.h" | |||
45 | #include "gimplify-me.h" | |||
46 | #include "dfp.h" | |||
47 | #include "builtins.h" | |||
48 | #include "tree-object-size.h" | |||
49 | #include "tree-cfg.h" | |||
50 | #include "gimple-fold.h" | |||
51 | #include "varasm.h" | |||
52 | ||||
53 | /* Map from a tree to a VAR_DECL tree. */ | |||
54 | ||||
55 | struct GTY((for_user)) tree_type_map { | |||
56 | struct tree_map_base type; | |||
57 | tree decl; | |||
58 | }; | |||
59 | ||||
60 | struct tree_type_map_cache_hasher : ggc_cache_ptr_hash<tree_type_map> | |||
61 | { | |||
62 | static inline hashval_t | |||
63 | hash (tree_type_map *t) | |||
64 | { | |||
65 | return TYPE_UID (t->type.from)((tree_class_check ((t->type.from), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 65, __FUNCTION__))->type_common.uid); | |||
66 | } | |||
67 | ||||
68 | static inline bool | |||
69 | equal (tree_type_map *a, tree_type_map *b) | |||
70 | { | |||
71 | return a->type.from == b->type.from; | |||
72 | } | |||
73 | ||||
74 | static int | |||
75 | keep_cache_entry (tree_type_map *&m) | |||
76 | { | |||
77 | return ggc_marked_p (m->type.from); | |||
78 | } | |||
79 | }; | |||
80 | ||||
81 | static GTY ((cache)) | |||
82 | hash_table<tree_type_map_cache_hasher> *decl_tree_for_type; | |||
83 | ||||
84 | /* Lookup a VAR_DECL for TYPE, and return it if we find one. */ | |||
85 | ||||
86 | static tree | |||
87 | decl_for_type_lookup (tree type) | |||
88 | { | |||
89 | /* If the hash table is not initialized yet, create it now. */ | |||
90 | if (decl_tree_for_type == NULLnullptr) | |||
91 | { | |||
92 | decl_tree_for_type | |||
93 | = hash_table<tree_type_map_cache_hasher>::create_ggc (10); | |||
94 | /* That also means we don't have to bother with the lookup. */ | |||
95 | return NULL_TREE(tree) nullptr; | |||
96 | } | |||
97 | ||||
98 | struct tree_type_map *h, in; | |||
99 | in.type.from = type; | |||
100 | ||||
101 | h = decl_tree_for_type->find_with_hash (&in, TYPE_UID (type)((tree_class_check ((type), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 101, __FUNCTION__))->type_common.uid)); | |||
102 | return h ? h->decl : NULL_TREE(tree) nullptr; | |||
103 | } | |||
104 | ||||
105 | /* Insert a mapping TYPE->DECL in the VAR_DECL for type hashtable. */ | |||
106 | ||||
107 | static void | |||
108 | decl_for_type_insert (tree type, tree decl) | |||
109 | { | |||
110 | struct tree_type_map *h; | |||
111 | ||||
112 | h = ggc_alloc<tree_type_map> (); | |||
113 | h->type.from = type; | |||
114 | h->decl = decl; | |||
115 | *decl_tree_for_type->find_slot_with_hash (h, TYPE_UID (type)((tree_class_check ((type), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 115, __FUNCTION__))->type_common.uid), INSERT) = h; | |||
116 | } | |||
117 | ||||
118 | /* Helper routine, which encodes a value in the pointer_sized_int_node. | |||
119 | Arguments with precision <= POINTER_SIZE are passed directly, | |||
120 | the rest is passed by reference. T is a value we are to encode. | |||
121 | PHASE determines when this function is called. */ | |||
122 | ||||
123 | tree | |||
124 | ubsan_encode_value (tree t, enum ubsan_encode_value_phase phase) | |||
125 | { | |||
126 | tree type = TREE_TYPE (t)((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 126, __FUNCTION__))->typed.type); | |||
127 | scalar_mode mode = SCALAR_TYPE_MODE (type)(as_a <scalar_mode> ((tree_class_check ((type), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 127, __FUNCTION__))->type_common.mode)); | |||
128 | const unsigned int bitsize = GET_MODE_BITSIZE (mode); | |||
129 | if (bitsize <= POINTER_SIZE(((global_options.x_ix86_isa_flags & (1UL << 58)) != 0) ? 32 : ((8) * (((global_options.x_ix86_isa_flags & (1UL << 1)) != 0) ? 8 : 4)))) | |||
130 | switch (TREE_CODE (type)((enum tree_code) (type)->base.code)) | |||
131 | { | |||
132 | case BOOLEAN_TYPE: | |||
133 | case ENUMERAL_TYPE: | |||
134 | case INTEGER_TYPE: | |||
135 | return fold_build1 (NOP_EXPR, pointer_sized_int_node, t)fold_build1_loc (((location_t) 0), NOP_EXPR, global_trees[TI_POINTER_SIZED_TYPE ], t ); | |||
136 | case REAL_TYPE: | |||
137 | { | |||
138 | tree itype = build_nonstandard_integer_type (bitsize, true); | |||
139 | t = fold_build1 (VIEW_CONVERT_EXPR, itype, t)fold_build1_loc (((location_t) 0), VIEW_CONVERT_EXPR, itype, t ); | |||
140 | return fold_convert (pointer_sized_int_node, t)fold_convert_loc (((location_t) 0), global_trees[TI_POINTER_SIZED_TYPE ], t); | |||
141 | } | |||
142 | default: | |||
143 | gcc_unreachable ()(fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 143, __FUNCTION__)); | |||
144 | } | |||
145 | else | |||
146 | { | |||
147 | if (!DECL_P (t)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (t)->base.code))] == tcc_declaration) || !TREE_ADDRESSABLE (t)((t)->base.addressable_flag)) | |||
148 | { | |||
149 | /* The reason for this is that we don't want to pessimize | |||
150 | code by making vars unnecessarily addressable. */ | |||
151 | tree var; | |||
152 | if (phase != UBSAN_ENCODE_VALUE_GENERIC) | |||
153 | { | |||
154 | var = create_tmp_var (type); | |||
155 | mark_addressable (var); | |||
156 | } | |||
157 | else | |||
158 | { | |||
159 | var = create_tmp_var_raw (type); | |||
160 | TREE_ADDRESSABLE (var)((var)->base.addressable_flag) = 1; | |||
161 | DECL_CONTEXT (var)((contains_struct_check ((var), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 161, __FUNCTION__))->decl_minimal.context) = current_function_decl; | |||
162 | } | |||
163 | if (phase == UBSAN_ENCODE_VALUE_RTL) | |||
164 | { | |||
165 | rtx mem = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode), | |||
166 | type); | |||
167 | SET_DECL_RTL (var, mem)set_decl_rtl (var, mem); | |||
168 | expand_assignment (var, t, false); | |||
169 | return build_fold_addr_expr (var)build_fold_addr_expr_loc (((location_t) 0), (var)); | |||
170 | } | |||
171 | if (phase != UBSAN_ENCODE_VALUE_GENERIC) | |||
172 | { | |||
173 | tree tem = build2 (MODIFY_EXPR, void_type_nodeglobal_trees[TI_VOID_TYPE], var, t); | |||
174 | t = build_fold_addr_expr (var)build_fold_addr_expr_loc (((location_t) 0), (var)); | |||
175 | return build2 (COMPOUND_EXPR, TREE_TYPE (t)((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 175, __FUNCTION__))->typed.type), tem, t); | |||
176 | } | |||
177 | else | |||
178 | { | |||
179 | var = build4 (TARGET_EXPR, type, var, t, NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
180 | return build_fold_addr_expr (var)build_fold_addr_expr_loc (((location_t) 0), (var)); | |||
181 | } | |||
182 | } | |||
183 | else | |||
184 | return build_fold_addr_expr (t)build_fold_addr_expr_loc (((location_t) 0), (t)); | |||
185 | } | |||
186 | } | |||
187 | ||||
188 | /* Cached ubsan_get_type_descriptor_type () return value. */ | |||
189 | static GTY(()) tree ubsan_type_descriptor_type; | |||
190 | ||||
191 | /* Build | |||
192 | struct __ubsan_type_descriptor | |||
193 | { | |||
194 | unsigned short __typekind; | |||
195 | unsigned short __typeinfo; | |||
196 | char __typename[]; | |||
197 | } | |||
198 | type. */ | |||
199 | ||||
200 | static tree | |||
201 | ubsan_get_type_descriptor_type (void) | |||
202 | { | |||
203 | static const char *field_names[3] | |||
204 | = { "__typekind", "__typeinfo", "__typename" }; | |||
205 | tree fields[3], ret; | |||
206 | ||||
207 | if (ubsan_type_descriptor_type) | |||
208 | return ubsan_type_descriptor_type; | |||
209 | ||||
210 | tree itype = build_range_type (sizetypesizetype_tab[(int) stk_sizetype], size_zero_nodeglobal_trees[TI_SIZE_ZERO], NULL_TREE(tree) nullptr); | |||
211 | tree flex_arr_type = build_array_type (char_type_nodeinteger_types[itk_char], itype); | |||
212 | ||||
213 | ret = make_node (RECORD_TYPE); | |||
214 | for (int i = 0; i < 3; i++) | |||
215 | { | |||
216 | fields[i] = build_decl (UNKNOWN_LOCATION((location_t) 0), FIELD_DECL, | |||
217 | get_identifier (field_names[i])(__builtin_constant_p (field_names[i]) ? get_identifier_with_length ((field_names[i]), strlen (field_names[i])) : get_identifier (field_names[i])), | |||
218 | (i == 2) ? flex_arr_type | |||
219 | : short_unsigned_type_nodeinteger_types[itk_unsigned_short]); | |||
220 | DECL_CONTEXT (fields[i])((contains_struct_check ((fields[i]), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 220, __FUNCTION__))->decl_minimal.context) = ret; | |||
221 | if (i) | |||
222 | DECL_CHAIN (fields[i - 1])(((contains_struct_check (((contains_struct_check ((fields[i - 1]), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 222, __FUNCTION__))), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 222, __FUNCTION__))->common.chain)) = fields[i]; | |||
223 | } | |||
224 | tree type_decl = build_decl (input_location, TYPE_DECL, | |||
225 | get_identifier ("__ubsan_type_descriptor")(__builtin_constant_p ("__ubsan_type_descriptor") ? get_identifier_with_length (("__ubsan_type_descriptor"), strlen ("__ubsan_type_descriptor" )) : get_identifier ("__ubsan_type_descriptor")), | |||
226 | ret); | |||
227 | DECL_IGNORED_P (type_decl)((contains_struct_check ((type_decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 227, __FUNCTION__))->decl_common.ignored_flag) = 1; | |||
228 | DECL_ARTIFICIAL (type_decl)((contains_struct_check ((type_decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 228, __FUNCTION__))->decl_common.artificial_flag) = 1; | |||
229 | TYPE_FIELDS (ret)((tree_check3 ((ret), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 229, __FUNCTION__, (RECORD_TYPE), (UNION_TYPE), (QUAL_UNION_TYPE )))->type_non_common.values) = fields[0]; | |||
230 | TYPE_NAME (ret)((tree_class_check ((ret), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 230, __FUNCTION__))->type_common.name) = type_decl; | |||
231 | TYPE_STUB_DECL (ret)(((contains_struct_check (((tree_class_check ((ret), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 231, __FUNCTION__))), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 231, __FUNCTION__))->common.chain)) = type_decl; | |||
232 | TYPE_ARTIFICIAL (ret)((tree_class_check ((ret), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 232, __FUNCTION__))->base.nowarning_flag) = 1; | |||
233 | layout_type (ret); | |||
234 | ubsan_type_descriptor_type = ret; | |||
235 | return ret; | |||
236 | } | |||
237 | ||||
238 | /* Cached ubsan_get_source_location_type () return value. */ | |||
239 | static GTY(()) tree ubsan_source_location_type; | |||
240 | ||||
241 | /* Build | |||
242 | struct __ubsan_source_location | |||
243 | { | |||
244 | const char *__filename; | |||
245 | unsigned int __line; | |||
246 | unsigned int __column; | |||
247 | } | |||
248 | type. */ | |||
249 | ||||
250 | tree | |||
251 | ubsan_get_source_location_type (void) | |||
252 | { | |||
253 | static const char *field_names[3] | |||
254 | = { "__filename", "__line", "__column" }; | |||
255 | tree fields[3], ret; | |||
256 | if (ubsan_source_location_type) | |||
257 | return ubsan_source_location_type; | |||
258 | ||||
259 | tree const_char_type = build_qualified_type (char_type_nodeinteger_types[itk_char], | |||
260 | TYPE_QUAL_CONST); | |||
261 | ||||
262 | ret = make_node (RECORD_TYPE); | |||
263 | for (int i = 0; i < 3; i++) | |||
264 | { | |||
265 | fields[i] = build_decl (UNKNOWN_LOCATION((location_t) 0), FIELD_DECL, | |||
266 | get_identifier (field_names[i])(__builtin_constant_p (field_names[i]) ? get_identifier_with_length ((field_names[i]), strlen (field_names[i])) : get_identifier (field_names[i])), | |||
267 | (i == 0) ? build_pointer_type (const_char_type) | |||
268 | : unsigned_type_nodeinteger_types[itk_unsigned_int]); | |||
269 | DECL_CONTEXT (fields[i])((contains_struct_check ((fields[i]), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 269, __FUNCTION__))->decl_minimal.context) = ret; | |||
270 | if (i) | |||
271 | DECL_CHAIN (fields[i - 1])(((contains_struct_check (((contains_struct_check ((fields[i - 1]), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 271, __FUNCTION__))), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 271, __FUNCTION__))->common.chain)) = fields[i]; | |||
272 | } | |||
273 | tree type_decl = build_decl (input_location, TYPE_DECL, | |||
274 | get_identifier ("__ubsan_source_location")(__builtin_constant_p ("__ubsan_source_location") ? get_identifier_with_length (("__ubsan_source_location"), strlen ("__ubsan_source_location" )) : get_identifier ("__ubsan_source_location")), | |||
275 | ret); | |||
276 | DECL_IGNORED_P (type_decl)((contains_struct_check ((type_decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 276, __FUNCTION__))->decl_common.ignored_flag) = 1; | |||
277 | DECL_ARTIFICIAL (type_decl)((contains_struct_check ((type_decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 277, __FUNCTION__))->decl_common.artificial_flag) = 1; | |||
278 | TYPE_FIELDS (ret)((tree_check3 ((ret), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 278, __FUNCTION__, (RECORD_TYPE), (UNION_TYPE), (QUAL_UNION_TYPE )))->type_non_common.values) = fields[0]; | |||
279 | TYPE_NAME (ret)((tree_class_check ((ret), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 279, __FUNCTION__))->type_common.name) = type_decl; | |||
280 | TYPE_STUB_DECL (ret)(((contains_struct_check (((tree_class_check ((ret), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 280, __FUNCTION__))), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 280, __FUNCTION__))->common.chain)) = type_decl; | |||
281 | TYPE_ARTIFICIAL (ret)((tree_class_check ((ret), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 281, __FUNCTION__))->base.nowarning_flag) = 1; | |||
282 | layout_type (ret); | |||
283 | ubsan_source_location_type = ret; | |||
284 | return ret; | |||
285 | } | |||
286 | ||||
287 | /* Helper routine that returns a CONSTRUCTOR of __ubsan_source_location | |||
288 | type with its fields filled from a location_t LOC. */ | |||
289 | ||||
290 | static tree | |||
291 | ubsan_source_location (location_t loc) | |||
292 | { | |||
293 | expanded_location xloc; | |||
294 | tree type = ubsan_get_source_location_type (); | |||
295 | ||||
296 | xloc = expand_location (loc); | |||
297 | tree str; | |||
298 | if (xloc.file == NULLnullptr) | |||
299 | { | |||
300 | str = build_int_cst (ptr_type_nodeglobal_trees[TI_PTR_TYPE], 0); | |||
301 | xloc.line = 0; | |||
302 | xloc.column = 0; | |||
303 | } | |||
304 | else | |||
305 | { | |||
306 | /* Fill in the values from LOC. */ | |||
307 | size_t len = strlen (xloc.file) + 1; | |||
308 | str = build_string (len, xloc.file); | |||
309 | TREE_TYPE (str)((contains_struct_check ((str), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 309, __FUNCTION__))->typed.type) = build_array_type_nelts (char_type_nodeinteger_types[itk_char], len); | |||
310 | TREE_READONLY (str)((non_type_check ((str), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 310, __FUNCTION__))->base.readonly_flag) = 1; | |||
311 | TREE_STATIC (str)((str)->base.static_flag) = 1; | |||
312 | str = build_fold_addr_expr (str)build_fold_addr_expr_loc (((location_t) 0), (str)); | |||
313 | } | |||
314 | tree ctor = build_constructor_va (type, 3, NULL_TREE(tree) nullptr, str, NULL_TREE(tree) nullptr, | |||
315 | build_int_cst (unsigned_type_nodeinteger_types[itk_unsigned_int], | |||
316 | xloc.line), NULL_TREE(tree) nullptr, | |||
317 | build_int_cst (unsigned_type_nodeinteger_types[itk_unsigned_int], | |||
318 | xloc.column)); | |||
319 | TREE_CONSTANT (ctor)((non_type_check ((ctor), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 319, __FUNCTION__))->base.constant_flag) = 1; | |||
320 | TREE_STATIC (ctor)((ctor)->base.static_flag) = 1; | |||
321 | ||||
322 | return ctor; | |||
323 | } | |||
324 | ||||
325 | /* This routine returns a magic number for TYPE. */ | |||
326 | ||||
327 | static unsigned short | |||
328 | get_ubsan_type_info_for_type (tree type) | |||
329 | { | |||
330 | if (TREE_CODE (type)((enum tree_code) (type)->base.code) == REAL_TYPE) | |||
331 | return tree_to_uhwi (TYPE_SIZE (type)((tree_class_check ((type), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 331, __FUNCTION__))->type_common.size)); | |||
332 | else if (INTEGRAL_TYPE_P (type)(((enum tree_code) (type)->base.code) == ENUMERAL_TYPE || ( (enum tree_code) (type)->base.code) == BOOLEAN_TYPE || ((enum tree_code) (type)->base.code) == INTEGER_TYPE)) | |||
333 | { | |||
334 | int prec = exact_log2 (tree_to_uhwi (TYPE_SIZE (type)((tree_class_check ((type), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 334, __FUNCTION__))->type_common.size))); | |||
335 | gcc_assert (prec != -1)((void)(!(prec != -1) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 335, __FUNCTION__), 0 : 0)); | |||
336 | return (prec << 1) | !TYPE_UNSIGNED (type)((tree_class_check ((type), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 336, __FUNCTION__))->base.u.bits.unsigned_flag); | |||
337 | } | |||
338 | else | |||
339 | return 0; | |||
340 | } | |||
341 | ||||
342 | /* Counters for internal labels. ubsan_ids[0] for Lubsan_type, | |||
343 | ubsan_ids[1] for Lubsan_data labels. */ | |||
344 | static GTY(()) unsigned int ubsan_ids[2]; | |||
345 | ||||
346 | /* Helper routine that returns ADDR_EXPR of a VAR_DECL of a type | |||
347 | descriptor. It first looks into the hash table; if not found, | |||
348 | create the VAR_DECL, put it into the hash table and return the | |||
349 | ADDR_EXPR of it. TYPE describes a particular type. PSTYLE is | |||
350 | an enum controlling how we want to print the type. */ | |||
351 | ||||
352 | tree | |||
353 | ubsan_type_descriptor (tree type, enum ubsan_print_style pstyle) | |||
354 | { | |||
355 | /* See through any typedefs. */ | |||
356 | type = TYPE_MAIN_VARIANT (type)((tree_class_check ((type), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 356, __FUNCTION__))->type_common.main_variant); | |||
357 | ||||
358 | tree decl = decl_for_type_lookup (type); | |||
359 | /* It is possible that some of the earlier created DECLs were found | |||
360 | unused, in that case they weren't emitted and varpool_node::get | |||
361 | returns NULL node on them. But now we really need them. Thus, | |||
362 | renew them here. */ | |||
363 | if (decl != NULL_TREE(tree) nullptr && varpool_node::get (decl)) | |||
364 | return build_fold_addr_expr (decl)build_fold_addr_expr_loc (((location_t) 0), (decl)); | |||
365 | ||||
366 | tree dtype = ubsan_get_type_descriptor_type (); | |||
367 | tree type2 = type; | |||
368 | const char *tname = NULLnullptr; | |||
369 | pretty_printer pretty_name; | |||
370 | unsigned char deref_depth = 0; | |||
371 | unsigned short tkind, tinfo; | |||
372 | ||||
373 | /* Get the name of the type, or the name of the pointer type. */ | |||
374 | if (pstyle == UBSAN_PRINT_POINTER) | |||
375 | { | |||
376 | gcc_assert (POINTER_TYPE_P (type))((void)(!((((enum tree_code) (type)->base.code) == POINTER_TYPE || ((enum tree_code) (type)->base.code) == REFERENCE_TYPE )) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 376, __FUNCTION__), 0 : 0)); | |||
377 | type2 = TREE_TYPE (type)((contains_struct_check ((type), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 377, __FUNCTION__))->typed.type); | |||
378 | ||||
379 | /* Remove any '*' operators from TYPE. */ | |||
380 | while (POINTER_TYPE_P (type2)(((enum tree_code) (type2)->base.code) == POINTER_TYPE || ( (enum tree_code) (type2)->base.code) == REFERENCE_TYPE)) | |||
381 | deref_depth++, type2 = TREE_TYPE (type2)((contains_struct_check ((type2), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 381, __FUNCTION__))->typed.type); | |||
382 | ||||
383 | if (TREE_CODE (type2)((enum tree_code) (type2)->base.code) == METHOD_TYPE) | |||
384 | type2 = TYPE_METHOD_BASETYPE (type2)((tree_check2 ((type2), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 384, __FUNCTION__, (FUNCTION_TYPE), (METHOD_TYPE)))->type_non_common .maxval); | |||
385 | } | |||
386 | ||||
387 | /* If an array, get its type. */ | |||
388 | type2 = strip_array_types (type2); | |||
389 | ||||
390 | if (pstyle == UBSAN_PRINT_ARRAY) | |||
391 | { | |||
392 | while (POINTER_TYPE_P (type2)(((enum tree_code) (type2)->base.code) == POINTER_TYPE || ( (enum tree_code) (type2)->base.code) == REFERENCE_TYPE)) | |||
393 | deref_depth++, type2 = TREE_TYPE (type2)((contains_struct_check ((type2), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 393, __FUNCTION__))->typed.type); | |||
394 | } | |||
395 | ||||
396 | if (TYPE_NAME (type2)((tree_class_check ((type2), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 396, __FUNCTION__))->type_common.name) != NULLnullptr) | |||
397 | { | |||
398 | if (TREE_CODE (TYPE_NAME (type2))((enum tree_code) (((tree_class_check ((type2), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 398, __FUNCTION__))->type_common.name))->base.code) == IDENTIFIER_NODE) | |||
399 | tname = IDENTIFIER_POINTER (TYPE_NAME (type2))((const char *) (tree_check ((((tree_class_check ((type2), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 399, __FUNCTION__))->type_common.name)), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 399, __FUNCTION__, (IDENTIFIER_NODE)))->identifier.id.str ); | |||
400 | else if (DECL_NAME (TYPE_NAME (type2))((contains_struct_check ((((tree_class_check ((type2), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 400, __FUNCTION__))->type_common.name)), (TS_DECL_MINIMAL ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 400, __FUNCTION__))->decl_minimal.name) != NULLnullptr) | |||
401 | tname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type2)))((const char *) (tree_check ((((contains_struct_check ((((tree_class_check ((type2), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 401, __FUNCTION__))->type_common.name)), (TS_DECL_MINIMAL ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 401, __FUNCTION__))->decl_minimal.name)), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 401, __FUNCTION__, (IDENTIFIER_NODE)))->identifier.id.str ); | |||
402 | } | |||
403 | ||||
404 | if (tname == NULLnullptr) | |||
405 | /* We weren't able to determine the type name. */ | |||
406 | tname = "<unknown>"; | |||
407 | ||||
408 | pp_quote (&pretty_name)pp_character (&pretty_name, '\''); | |||
409 | ||||
410 | tree eltype = type; | |||
411 | if (pstyle == UBSAN_PRINT_POINTER) | |||
412 | { | |||
413 | pp_printf (&pretty_name, "%s%s%s%s%s%s%s", | |||
414 | TYPE_VOLATILE (type2)((tree_class_check ((type2), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 414, __FUNCTION__))->base.volatile_flag) ? "volatile " : "", | |||
415 | TYPE_READONLY (type2)((tree_class_check ((type2), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 415, __FUNCTION__))->base.readonly_flag) ? "const " : "", | |||
416 | TYPE_RESTRICT (type2)((tree_class_check ((type2), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 416, __FUNCTION__))->type_common.restrict_flag) ? "restrict " : "", | |||
417 | TYPE_ATOMIC (type2)((tree_class_check ((type2), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 417, __FUNCTION__))->base.u.bits.atomic_flag) ? "_Atomic " : "", | |||
418 | TREE_CODE (type2)((enum tree_code) (type2)->base.code) == RECORD_TYPE | |||
419 | ? "struct " | |||
420 | : TREE_CODE (type2)((enum tree_code) (type2)->base.code) == UNION_TYPE | |||
421 | ? "union " : "", tname, | |||
422 | deref_depth == 0 ? "" : " "); | |||
423 | while (deref_depth-- > 0) | |||
424 | pp_star (&pretty_name)pp_character (&pretty_name, '*'); | |||
425 | } | |||
426 | else if (pstyle == UBSAN_PRINT_ARRAY) | |||
427 | { | |||
428 | /* Pretty print the array dimensions. */ | |||
429 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE)((void)(!(((enum tree_code) (type)->base.code) == ARRAY_TYPE ) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 429, __FUNCTION__), 0 : 0)); | |||
430 | tree t = type; | |||
431 | pp_string (&pretty_name, tname); | |||
432 | pp_space (&pretty_name)pp_character (&pretty_name, ' '); | |||
433 | while (deref_depth-- > 0) | |||
434 | pp_star (&pretty_name)pp_character (&pretty_name, '*'); | |||
435 | while (TREE_CODE (t)((enum tree_code) (t)->base.code) == ARRAY_TYPE) | |||
436 | { | |||
437 | pp_left_bracket (&pretty_name)pp_character (&pretty_name, '['); | |||
438 | tree dom = TYPE_DOMAIN (t)((tree_check ((t), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 438, __FUNCTION__, (ARRAY_TYPE)))->type_non_common.values ); | |||
439 | if (dom != NULL_TREE(tree) nullptr | |||
440 | && TYPE_MAX_VALUE (dom)((tree_check5 ((dom), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 440, __FUNCTION__, (INTEGER_TYPE), (ENUMERAL_TYPE), (BOOLEAN_TYPE ), (REAL_TYPE), (FIXED_POINT_TYPE)))->type_non_common.maxval ) != NULL_TREE(tree) nullptr | |||
441 | && TREE_CODE (TYPE_MAX_VALUE (dom))((enum tree_code) (((tree_check5 ((dom), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 441, __FUNCTION__, (INTEGER_TYPE), (ENUMERAL_TYPE), (BOOLEAN_TYPE ), (REAL_TYPE), (FIXED_POINT_TYPE)))->type_non_common.maxval ))->base.code) == INTEGER_CST) | |||
442 | { | |||
443 | unsigned HOST_WIDE_INTlong m; | |||
444 | if (tree_fits_uhwi_p (TYPE_MAX_VALUE (dom)((tree_check5 ((dom), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 444, __FUNCTION__, (INTEGER_TYPE), (ENUMERAL_TYPE), (BOOLEAN_TYPE ), (REAL_TYPE), (FIXED_POINT_TYPE)))->type_non_common.maxval )) | |||
445 | && (m = tree_to_uhwi (TYPE_MAX_VALUE (dom)((tree_check5 ((dom), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 445, __FUNCTION__, (INTEGER_TYPE), (ENUMERAL_TYPE), (BOOLEAN_TYPE ), (REAL_TYPE), (FIXED_POINT_TYPE)))->type_non_common.maxval ))) + 1 != 0) | |||
446 | pp_unsigned_wide_integer (&pretty_name, m + 1)do { sprintf ((&pretty_name)->buffer->digit_buffer, "%" "l" "u", (unsigned long) m + 1); pp_string (&pretty_name , (&pretty_name)->buffer->digit_buffer); } while (0 ); | |||
447 | else | |||
448 | pp_wide_int (&pretty_name,do { print_dec (wi::add (wi::to_widest (((tree_check5 ((dom), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 449, __FUNCTION__, (INTEGER_TYPE), (ENUMERAL_TYPE), (BOOLEAN_TYPE ), (REAL_TYPE), (FIXED_POINT_TYPE)))->type_non_common.maxval )), 1), (&pretty_name)->buffer->digit_buffer, ((signop ) ((tree_class_check ((((contains_struct_check ((dom), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 450, __FUNCTION__))->typed.type)), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 450, __FUNCTION__))->base.u.bits.unsigned_flag))); pp_string (&pretty_name, (&pretty_name)->buffer->digit_buffer ); } while (0) | |||
449 | wi::add (wi::to_widest (TYPE_MAX_VALUE (dom)), 1),do { print_dec (wi::add (wi::to_widest (((tree_check5 ((dom), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 449, __FUNCTION__, (INTEGER_TYPE), (ENUMERAL_TYPE), (BOOLEAN_TYPE ), (REAL_TYPE), (FIXED_POINT_TYPE)))->type_non_common.maxval )), 1), (&pretty_name)->buffer->digit_buffer, ((signop ) ((tree_class_check ((((contains_struct_check ((dom), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 450, __FUNCTION__))->typed.type)), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 450, __FUNCTION__))->base.u.bits.unsigned_flag))); pp_string (&pretty_name, (&pretty_name)->buffer->digit_buffer ); } while (0) | |||
450 | TYPE_SIGN (TREE_TYPE (dom)))do { print_dec (wi::add (wi::to_widest (((tree_check5 ((dom), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 449, __FUNCTION__, (INTEGER_TYPE), (ENUMERAL_TYPE), (BOOLEAN_TYPE ), (REAL_TYPE), (FIXED_POINT_TYPE)))->type_non_common.maxval )), 1), (&pretty_name)->buffer->digit_buffer, ((signop ) ((tree_class_check ((((contains_struct_check ((dom), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 450, __FUNCTION__))->typed.type)), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 450, __FUNCTION__))->base.u.bits.unsigned_flag))); pp_string (&pretty_name, (&pretty_name)->buffer->digit_buffer ); } while (0); | |||
451 | } | |||
452 | else | |||
453 | /* ??? We can't determine the variable name; print VLA unspec. */ | |||
454 | pp_star (&pretty_name)pp_character (&pretty_name, '*'); | |||
455 | pp_right_bracket (&pretty_name)pp_character (&pretty_name, ']'); | |||
456 | t = TREE_TYPE (t)((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 456, __FUNCTION__))->typed.type); | |||
457 | } | |||
458 | ||||
459 | /* Save the tree with stripped types. */ | |||
460 | eltype = t; | |||
461 | } | |||
462 | else | |||
463 | pp_string (&pretty_name, tname); | |||
464 | ||||
465 | pp_quote (&pretty_name)pp_character (&pretty_name, '\''); | |||
466 | ||||
467 | switch (TREE_CODE (eltype)((enum tree_code) (eltype)->base.code)) | |||
468 | { | |||
469 | case BOOLEAN_TYPE: | |||
470 | case ENUMERAL_TYPE: | |||
471 | case INTEGER_TYPE: | |||
472 | tkind = 0x0000; | |||
473 | break; | |||
474 | case REAL_TYPE: | |||
475 | /* FIXME: libubsan right now only supports float, double and | |||
476 | long double type formats. */ | |||
477 | if (TYPE_MODE (eltype)((((enum tree_code) ((tree_class_check ((eltype), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 477, __FUNCTION__)))->base.code) == VECTOR_TYPE) ? vector_type_mode (eltype) : (eltype)->type_common.mode) == TYPE_MODE (float_type_node)((((enum tree_code) ((tree_class_check ((global_trees[TI_FLOAT_TYPE ]), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 477, __FUNCTION__)))->base.code) == VECTOR_TYPE) ? vector_type_mode (global_trees[TI_FLOAT_TYPE]) : (global_trees[TI_FLOAT_TYPE] )->type_common.mode) | |||
478 | || TYPE_MODE (eltype)((((enum tree_code) ((tree_class_check ((eltype), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 478, __FUNCTION__)))->base.code) == VECTOR_TYPE) ? vector_type_mode (eltype) : (eltype)->type_common.mode) == TYPE_MODE (double_type_node)((((enum tree_code) ((tree_class_check ((global_trees[TI_DOUBLE_TYPE ]), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 478, __FUNCTION__)))->base.code) == VECTOR_TYPE) ? vector_type_mode (global_trees[TI_DOUBLE_TYPE]) : (global_trees[TI_DOUBLE_TYPE ])->type_common.mode) | |||
479 | || TYPE_MODE (eltype)((((enum tree_code) ((tree_class_check ((eltype), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 479, __FUNCTION__)))->base.code) == VECTOR_TYPE) ? vector_type_mode (eltype) : (eltype)->type_common.mode) == TYPE_MODE (long_double_type_node)((((enum tree_code) ((tree_class_check ((global_trees[TI_LONG_DOUBLE_TYPE ]), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 479, __FUNCTION__)))->base.code) == VECTOR_TYPE) ? vector_type_mode (global_trees[TI_LONG_DOUBLE_TYPE]) : (global_trees[TI_LONG_DOUBLE_TYPE ])->type_common.mode)) | |||
480 | tkind = 0x0001; | |||
481 | else | |||
482 | tkind = 0xffff; | |||
483 | break; | |||
484 | default: | |||
485 | tkind = 0xffff; | |||
486 | break; | |||
487 | } | |||
488 | tinfo = get_ubsan_type_info_for_type (eltype); | |||
489 | ||||
490 | /* Create a new VAR_DECL of type descriptor. */ | |||
491 | const char *tmp = pp_formatted_text (&pretty_name); | |||
492 | size_t len = strlen (tmp) + 1; | |||
493 | tree str = build_string (len, tmp); | |||
494 | TREE_TYPE (str)((contains_struct_check ((str), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 494, __FUNCTION__))->typed.type) = build_array_type_nelts (char_type_nodeinteger_types[itk_char], len); | |||
495 | TREE_READONLY (str)((non_type_check ((str), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 495, __FUNCTION__))->base.readonly_flag) = 1; | |||
496 | TREE_STATIC (str)((str)->base.static_flag) = 1; | |||
497 | ||||
498 | char tmp_name[32]; | |||
499 | ASM_GENERATE_INTERNAL_LABEL (tmp_name, "Lubsan_type", ubsan_ids[0]++)do { char *__p; (tmp_name)[0] = '*'; (tmp_name)[1] = '.'; __p = stpcpy (&(tmp_name)[2], "Lubsan_type"); sprint_ul (__p , (unsigned long) (ubsan_ids[0]++)); } while (0); | |||
500 | decl = build_decl (UNKNOWN_LOCATION((location_t) 0), VAR_DECL, get_identifier (tmp_name)(__builtin_constant_p (tmp_name) ? get_identifier_with_length ((tmp_name), strlen (tmp_name)) : get_identifier (tmp_name)), | |||
501 | dtype); | |||
502 | TREE_STATIC (decl)((decl)->base.static_flag) = 1; | |||
503 | TREE_PUBLIC (decl)((decl)->base.public_flag) = 0; | |||
504 | DECL_ARTIFICIAL (decl)((contains_struct_check ((decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 504, __FUNCTION__))->decl_common.artificial_flag) = 1; | |||
505 | DECL_IGNORED_P (decl)((contains_struct_check ((decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 505, __FUNCTION__))->decl_common.ignored_flag) = 1; | |||
506 | DECL_EXTERNAL (decl)((contains_struct_check ((decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 506, __FUNCTION__))->decl_common.decl_flag_1) = 0; | |||
507 | DECL_SIZE (decl)((contains_struct_check ((decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 507, __FUNCTION__))->decl_common.size) | |||
508 | = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (TREE_TYPE (str)))size_binop_loc (((location_t) 0), PLUS_EXPR, ((contains_struct_check ((decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 508, __FUNCTION__))->decl_common.size), ((tree_class_check ((((contains_struct_check ((str), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 508, __FUNCTION__))->typed.type)), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 508, __FUNCTION__))->type_common.size)); | |||
509 | DECL_SIZE_UNIT (decl)((contains_struct_check ((decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 509, __FUNCTION__))->decl_common.size_unit) | |||
510 | = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),size_binop_loc (((location_t) 0), PLUS_EXPR, ((contains_struct_check ((decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 510, __FUNCTION__))->decl_common.size_unit), ((tree_class_check ((((contains_struct_check ((str), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 511, __FUNCTION__))->typed.type)), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 511, __FUNCTION__))->type_common.size_unit)) | |||
511 | TYPE_SIZE_UNIT (TREE_TYPE (str)))size_binop_loc (((location_t) 0), PLUS_EXPR, ((contains_struct_check ((decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 510, __FUNCTION__))->decl_common.size_unit), ((tree_class_check ((((contains_struct_check ((str), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 511, __FUNCTION__))->typed.type)), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 511, __FUNCTION__))->type_common.size_unit)); | |||
512 | ||||
513 | tree ctor = build_constructor_va (dtype, 3, NULL_TREE(tree) nullptr, | |||
514 | build_int_cst (short_unsigned_type_nodeinteger_types[itk_unsigned_short], | |||
515 | tkind), NULL_TREE(tree) nullptr, | |||
516 | build_int_cst (short_unsigned_type_nodeinteger_types[itk_unsigned_short], | |||
517 | tinfo), NULL_TREE(tree) nullptr, str); | |||
518 | TREE_CONSTANT (ctor)((non_type_check ((ctor), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 518, __FUNCTION__))->base.constant_flag) = 1; | |||
519 | TREE_STATIC (ctor)((ctor)->base.static_flag) = 1; | |||
520 | DECL_INITIAL (decl)((contains_struct_check ((decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 520, __FUNCTION__))->decl_common.initial) = ctor; | |||
521 | varpool_node::finalize_decl (decl); | |||
522 | ||||
523 | /* Save the VAR_DECL into the hash table. */ | |||
524 | decl_for_type_insert (type, decl); | |||
525 | ||||
526 | return build_fold_addr_expr (decl)build_fold_addr_expr_loc (((location_t) 0), (decl)); | |||
527 | } | |||
528 | ||||
529 | /* Create a structure for the ubsan library. NAME is a name of the new | |||
530 | structure. LOCCNT is number of locations, PLOC points to array of | |||
531 | locations. The arguments in ... are of __ubsan_type_descriptor type | |||
532 | and there are at most two of them, followed by NULL_TREE, followed | |||
533 | by optional extra arguments and another NULL_TREE. */ | |||
534 | ||||
535 | tree | |||
536 | ubsan_create_data (const char *name, int loccnt, const location_t *ploc, ...) | |||
537 | { | |||
538 | va_list args; | |||
539 | tree ret, t; | |||
540 | tree fields[6]; | |||
541 | vec<tree, va_gc> *saved_args = NULLnullptr; | |||
542 | size_t i = 0; | |||
543 | int j; | |||
544 | ||||
545 | /* It is possible that PCH zapped table with definitions of sanitizer | |||
546 | builtins. Reinitialize them if needed. */ | |||
547 | initialize_sanitizer_builtins (); | |||
548 | ||||
549 | /* Firstly, create a pointer to type descriptor type. */ | |||
550 | tree td_type = ubsan_get_type_descriptor_type (); | |||
551 | td_type = build_pointer_type (td_type); | |||
552 | ||||
553 | /* Create the structure type. */ | |||
554 | ret = make_node (RECORD_TYPE); | |||
555 | for (j = 0; j < loccnt; j++) | |||
| ||||
556 | { | |||
557 | gcc_checking_assert (i < 2)((void)(!(i < 2) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 557, __FUNCTION__), 0 : 0)); | |||
558 | fields[i] = build_decl (UNKNOWN_LOCATION((location_t) 0), FIELD_DECL, NULL_TREE(tree) nullptr, | |||
559 | ubsan_get_source_location_type ()); | |||
560 | DECL_CONTEXT (fields[i])((contains_struct_check ((fields[i]), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 560, __FUNCTION__))->decl_minimal.context) = ret; | |||
561 | if (i) | |||
562 | DECL_CHAIN (fields[i - 1])(((contains_struct_check (((contains_struct_check ((fields[i - 1]), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 562, __FUNCTION__))), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 562, __FUNCTION__))->common.chain)) = fields[i]; | |||
563 | i++; | |||
564 | } | |||
565 | ||||
566 | va_start (args, ploc)__builtin_va_start(args, ploc); | |||
567 | for (t = va_arg (args, tree)__builtin_va_arg(args, tree); t != NULL_TREE(tree) nullptr; | |||
568 | i++, t = va_arg (args, tree)__builtin_va_arg(args, tree)) | |||
569 | { | |||
570 | gcc_checking_assert (i < 4)((void)(!(i < 4) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 570, __FUNCTION__), 0 : 0)); | |||
571 | /* Save the tree arguments for later use. */ | |||
572 | vec_safe_push (saved_args, t); | |||
573 | fields[i] = build_decl (UNKNOWN_LOCATION((location_t) 0), FIELD_DECL, NULL_TREE(tree) nullptr, | |||
574 | td_type); | |||
575 | DECL_CONTEXT (fields[i])((contains_struct_check ((fields[i]), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 575, __FUNCTION__))->decl_minimal.context) = ret; | |||
576 | if (i) | |||
577 | DECL_CHAIN (fields[i - 1])(((contains_struct_check (((contains_struct_check ((fields[i - 1]), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 577, __FUNCTION__))), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 577, __FUNCTION__))->common.chain)) = fields[i]; | |||
578 | } | |||
579 | ||||
580 | for (t = va_arg (args, tree)__builtin_va_arg(args, tree); t != NULL_TREE(tree) nullptr; | |||
581 | i++, t = va_arg (args, tree)__builtin_va_arg(args, tree)) | |||
582 | { | |||
583 | gcc_checking_assert (i < 6)((void)(!(i < 6) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 583, __FUNCTION__), 0 : 0)); | |||
584 | /* Save the tree arguments for later use. */ | |||
585 | vec_safe_push (saved_args, t); | |||
586 | fields[i] = build_decl (UNKNOWN_LOCATION((location_t) 0), FIELD_DECL, NULL_TREE(tree) nullptr, | |||
587 | TREE_TYPE (t)((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 587, __FUNCTION__))->typed.type)); | |||
588 | DECL_CONTEXT (fields[i])((contains_struct_check ((fields[i]), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 588, __FUNCTION__))->decl_minimal.context) = ret; | |||
589 | if (i) | |||
590 | DECL_CHAIN (fields[i - 1])(((contains_struct_check (((contains_struct_check ((fields[i - 1]), (TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 590, __FUNCTION__))), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 590, __FUNCTION__))->common.chain)) = fields[i]; | |||
591 | } | |||
592 | va_end (args)__builtin_va_end(args); | |||
593 | ||||
594 | tree type_decl = build_decl (input_location, TYPE_DECL, | |||
595 | get_identifier (name)(__builtin_constant_p (name) ? get_identifier_with_length ((name ), strlen (name)) : get_identifier (name)), ret); | |||
596 | DECL_IGNORED_P (type_decl)((contains_struct_check ((type_decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 596, __FUNCTION__))->decl_common.ignored_flag) = 1; | |||
597 | DECL_ARTIFICIAL (type_decl)((contains_struct_check ((type_decl), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 597, __FUNCTION__))->decl_common.artificial_flag) = 1; | |||
598 | TYPE_FIELDS (ret)((tree_check3 ((ret), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 598, __FUNCTION__, (RECORD_TYPE), (UNION_TYPE), (QUAL_UNION_TYPE )))->type_non_common.values) = fields[0]; | |||
| ||||
599 | TYPE_NAME (ret)((tree_class_check ((ret), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 599, __FUNCTION__))->type_common.name) = type_decl; | |||
600 | TYPE_STUB_DECL (ret)(((contains_struct_check (((tree_class_check ((ret), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 600, __FUNCTION__))), (TS_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 600, __FUNCTION__))->common.chain)) = type_decl; | |||
601 | TYPE_ARTIFICIAL (ret)((tree_class_check ((ret), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 601, __FUNCTION__))->base.nowarning_flag) = 1; | |||
602 | layout_type (ret); | |||
603 | ||||
604 | /* Now, fill in the type. */ | |||
605 | char tmp_name[32]; | |||
606 | ASM_GENERATE_INTERNAL_LABEL (tmp_name, "Lubsan_data", ubsan_ids[1]++)do { char *__p; (tmp_name)[0] = '*'; (tmp_name)[1] = '.'; __p = stpcpy (&(tmp_name)[2], "Lubsan_data"); sprint_ul (__p , (unsigned long) (ubsan_ids[1]++)); } while (0); | |||
607 | tree var = build_decl (UNKNOWN_LOCATION((location_t) 0), VAR_DECL, get_identifier (tmp_name)(__builtin_constant_p (tmp_name) ? get_identifier_with_length ((tmp_name), strlen (tmp_name)) : get_identifier (tmp_name)), | |||
608 | ret); | |||
609 | TREE_STATIC (var)((var)->base.static_flag) = 1; | |||
610 | TREE_PUBLIC (var)((var)->base.public_flag) = 0; | |||
611 | DECL_ARTIFICIAL (var)((contains_struct_check ((var), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 611, __FUNCTION__))->decl_common.artificial_flag) = 1; | |||
612 | DECL_IGNORED_P (var)((contains_struct_check ((var), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 612, __FUNCTION__))->decl_common.ignored_flag) = 1; | |||
613 | DECL_EXTERNAL (var)((contains_struct_check ((var), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 613, __FUNCTION__))->decl_common.decl_flag_1) = 0; | |||
614 | ||||
615 | vec<constructor_elt, va_gc> *v; | |||
616 | vec_alloc (v, i); | |||
617 | tree ctor = build_constructor (ret, v); | |||
618 | ||||
619 | /* If desirable, set the __ubsan_source_location element. */ | |||
620 | for (j = 0; j < loccnt; j++) | |||
621 | { | |||
622 | location_t loc = LOCATION_LOCUS (ploc[j])((IS_ADHOC_LOC (ploc[j])) ? get_location_from_adhoc_loc (line_table , ploc[j]) : (ploc[j])); | |||
623 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, ubsan_source_location (loc))do { constructor_elt _ce___ = {(tree) nullptr, ubsan_source_location (loc)}; vec_safe_push ((v), _ce___); } while (0); | |||
624 | } | |||
625 | ||||
626 | size_t nelts = vec_safe_length (saved_args); | |||
627 | for (i = 0; i < nelts; i++) | |||
628 | { | |||
629 | t = (*saved_args)[i]; | |||
630 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t)do { constructor_elt _ce___ = {(tree) nullptr, t}; vec_safe_push ((v), _ce___); } while (0); | |||
631 | } | |||
632 | ||||
633 | TREE_CONSTANT (ctor)((non_type_check ((ctor), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 633, __FUNCTION__))->base.constant_flag) = 1; | |||
634 | TREE_STATIC (ctor)((ctor)->base.static_flag) = 1; | |||
635 | DECL_INITIAL (var)((contains_struct_check ((var), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 635, __FUNCTION__))->decl_common.initial) = ctor; | |||
636 | varpool_node::finalize_decl (var); | |||
637 | ||||
638 | return var; | |||
639 | } | |||
640 | ||||
641 | /* Shared between *build_builtin_unreachable. */ | |||
642 | ||||
643 | tree | |||
644 | sanitize_unreachable_fn (tree *data, location_t loc) | |||
645 | { | |||
646 | tree fn = NULL_TREE(tree) nullptr; | |||
647 | bool san = sanitize_flags_p (SANITIZE_UNREACHABLE); | |||
648 | if (san | |||
649 | ? (flag_sanitize_trapglobal_options.x_flag_sanitize_trap & SANITIZE_UNREACHABLE) | |||
650 | : flag_unreachable_trapsglobal_options.x_flag_unreachable_traps) | |||
651 | { | |||
652 | fn = builtin_decl_explicit (BUILT_IN_UNREACHABLE_TRAP); | |||
653 | *data = NULL_TREE(tree) nullptr; | |||
654 | } | |||
655 | else if (san) | |||
656 | { | |||
657 | /* Call ubsan_create_data first as it initializes SANITIZER built-ins. */ | |||
658 | *data = ubsan_create_data ("__ubsan_unreachable_data", 1, &loc, | |||
659 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
660 | fn = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE); | |||
661 | *data = build_fold_addr_expr_loc (loc, *data); | |||
662 | } | |||
663 | else | |||
664 | { | |||
665 | fn = builtin_decl_explicit (BUILT_IN_UNREACHABLE); | |||
666 | *data = NULL_TREE(tree) nullptr; | |||
667 | } | |||
668 | return fn; | |||
669 | } | |||
670 | ||||
671 | /* Rewrite a gcall to __builtin_unreachable for -fsanitize=unreachable. Called | |||
672 | by the sanopt pass. */ | |||
673 | ||||
674 | bool | |||
675 | ubsan_instrument_unreachable (gimple_stmt_iterator *gsi) | |||
676 | { | |||
677 | location_t loc = gimple_location (gsi_stmt (*gsi)); | |||
678 | gimple *g = gimple_build_builtin_unreachable (loc); | |||
679 | gsi_replace (gsi, g, false); | |||
680 | return false; | |||
681 | } | |||
682 | ||||
683 | /* Return true if T is a call to a libubsan routine. */ | |||
684 | ||||
685 | bool | |||
686 | is_ubsan_builtin_p (tree t) | |||
687 | { | |||
688 | return TREE_CODE (t)((enum tree_code) (t)->base.code) == FUNCTION_DECL | |||
689 | && fndecl_built_in_p (t, BUILT_IN_NORMAL) | |||
690 | && strncmp (IDENTIFIER_POINTER (DECL_NAME (t))((const char *) (tree_check ((((contains_struct_check ((t), ( TS_DECL_MINIMAL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 690, __FUNCTION__))->decl_minimal.name)), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 690, __FUNCTION__, (IDENTIFIER_NODE)))->identifier.id.str ), | |||
691 | "__builtin___ubsan_", 18) == 0; | |||
692 | } | |||
693 | ||||
694 | /* Create a callgraph edge for statement STMT. */ | |||
695 | ||||
696 | static void | |||
697 | ubsan_create_edge (gimple *stmt) | |||
698 | { | |||
699 | gcall *call_stmt = dyn_cast <gcall *> (stmt); | |||
700 | basic_block bb = gimple_bb (stmt); | |||
701 | cgraph_node *node = cgraph_node::get (current_function_decl); | |||
702 | tree decl = gimple_call_fndecl (call_stmt); | |||
703 | if (decl) | |||
704 | node->create_edge (cgraph_node::get_create (decl), call_stmt, bb->count); | |||
705 | } | |||
706 | ||||
707 | /* Expand the UBSAN_BOUNDS special builtin function. */ | |||
708 | ||||
709 | bool | |||
710 | ubsan_expand_bounds_ifn (gimple_stmt_iterator *gsi) | |||
711 | { | |||
712 | gimple *stmt = gsi_stmt (*gsi); | |||
713 | location_t loc = gimple_location (stmt); | |||
714 | gcc_assert (gimple_call_num_args (stmt) == 3)((void)(!(gimple_call_num_args (stmt) == 3) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 714, __FUNCTION__), 0 : 0)); | |||
715 | ||||
716 | /* Pick up the arguments of the UBSAN_BOUNDS call. */ | |||
717 | tree type = TREE_TYPE (TREE_TYPE (gimple_call_arg (stmt, 0)))((contains_struct_check ((((contains_struct_check ((gimple_call_arg (stmt, 0)), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 717, __FUNCTION__))->typed.type)), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 717, __FUNCTION__))->typed.type); | |||
718 | tree index = gimple_call_arg (stmt, 1); | |||
719 | tree orig_index = index; | |||
720 | tree bound = gimple_call_arg (stmt, 2); | |||
721 | ||||
722 | gimple_stmt_iterator gsi_orig = *gsi; | |||
723 | ||||
724 | /* Create condition "if (index >= bound)". */ | |||
725 | basic_block then_bb, fallthru_bb; | |||
726 | gimple_stmt_iterator cond_insert_point | |||
727 | = create_cond_insert_point (gsi, false, false, true, | |||
728 | &then_bb, &fallthru_bb); | |||
729 | index = fold_convert (TREE_TYPE (bound), index)fold_convert_loc (((location_t) 0), ((contains_struct_check ( (bound), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 729, __FUNCTION__))->typed.type), index); | |||
730 | index = force_gimple_operand_gsi (&cond_insert_point, index, | |||
731 | true, NULL_TREE(tree) nullptr, | |||
732 | false, GSI_NEW_STMT); | |||
733 | gimple *g = gimple_build_cond (GE_EXPR, index, bound, NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
734 | gimple_set_location (g, loc); | |||
735 | gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT); | |||
736 | ||||
737 | /* Generate __ubsan_handle_out_of_bounds call. */ | |||
738 | *gsi = gsi_after_labels (then_bb); | |||
739 | if (flag_sanitize_trapglobal_options.x_flag_sanitize_trap & SANITIZE_BOUNDS) | |||
740 | g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0); | |||
741 | else | |||
742 | { | |||
743 | tree data | |||
744 | = ubsan_create_data ("__ubsan_out_of_bounds_data", 1, &loc, | |||
745 | ubsan_type_descriptor (type, UBSAN_PRINT_ARRAY), | |||
746 | ubsan_type_descriptor (TREE_TYPE (orig_index)((contains_struct_check ((orig_index), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 746, __FUNCTION__))->typed.type)), | |||
747 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
748 | data = build_fold_addr_expr_loc (loc, data); | |||
749 | enum built_in_function bcode | |||
750 | = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & SANITIZE_BOUNDS) | |||
751 | ? BUILT_IN_UBSAN_HANDLE_OUT_OF_BOUNDS | |||
752 | : BUILT_IN_UBSAN_HANDLE_OUT_OF_BOUNDS_ABORT; | |||
753 | tree fn = builtin_decl_explicit (bcode); | |||
754 | tree val = ubsan_encode_value (orig_index, UBSAN_ENCODE_VALUE_GIMPLE); | |||
755 | val = force_gimple_operand_gsi (gsi, val, true, NULL_TREE(tree) nullptr, true, | |||
756 | GSI_SAME_STMT); | |||
757 | g = gimple_build_call (fn, 2, data, val); | |||
758 | } | |||
759 | gimple_set_location (g, loc); | |||
760 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
761 | ||||
762 | /* Get rid of the UBSAN_BOUNDS call from the IR. */ | |||
763 | unlink_stmt_vdef (stmt); | |||
764 | gsi_remove (&gsi_orig, true); | |||
765 | ||||
766 | /* Point GSI to next logical statement. */ | |||
767 | *gsi = gsi_start_bb (fallthru_bb); | |||
768 | return true; | |||
769 | } | |||
770 | ||||
771 | /* Expand UBSAN_NULL internal call. The type is kept on the ckind | |||
772 | argument which is a constant, because the middle-end treats pointer | |||
773 | conversions as useless and therefore the type of the first argument | |||
774 | could be changed to any other pointer type. */ | |||
775 | ||||
776 | bool | |||
777 | ubsan_expand_null_ifn (gimple_stmt_iterator *gsip) | |||
778 | { | |||
779 | gimple_stmt_iterator gsi = *gsip; | |||
780 | gimple *stmt = gsi_stmt (gsi); | |||
781 | location_t loc = gimple_location (stmt); | |||
782 | gcc_assert (gimple_call_num_args (stmt) == 3)((void)(!(gimple_call_num_args (stmt) == 3) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 782, __FUNCTION__), 0 : 0)); | |||
783 | tree ptr = gimple_call_arg (stmt, 0); | |||
784 | tree ckind = gimple_call_arg (stmt, 1); | |||
785 | tree align = gimple_call_arg (stmt, 2); | |||
786 | tree check_align = NULL_TREE(tree) nullptr; | |||
787 | bool check_null; | |||
788 | ||||
789 | basic_block cur_bb = gsi_bb (gsi); | |||
790 | ||||
791 | gimple *g; | |||
792 | if (!integer_zerop (align)) | |||
793 | { | |||
794 | unsigned int ptralign = get_pointer_alignment (ptr) / BITS_PER_UNIT(8); | |||
795 | if (compare_tree_int (align, ptralign) == 1) | |||
796 | { | |||
797 | check_align = make_ssa_name (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE]); | |||
798 | g = gimple_build_assign (check_align, NOP_EXPR, ptr); | |||
799 | gimple_set_location (g, loc); | |||
800 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |||
801 | } | |||
802 | } | |||
803 | check_null = sanitize_flags_p (SANITIZE_NULL); | |||
804 | ||||
805 | if (check_align == NULL_TREE(tree) nullptr && !check_null) | |||
806 | { | |||
807 | gsi_remove (gsip, true); | |||
808 | /* Unlink the UBSAN_NULLs vops before replacing it. */ | |||
809 | unlink_stmt_vdef (stmt); | |||
810 | return true; | |||
811 | } | |||
812 | ||||
813 | /* Split the original block holding the pointer dereference. */ | |||
814 | edge e = split_block (cur_bb, stmt); | |||
815 | ||||
816 | /* Get a hold on the 'condition block', the 'then block' and the | |||
817 | 'else block'. */ | |||
818 | basic_block cond_bb = e->src; | |||
819 | basic_block fallthru_bb = e->dest; | |||
820 | basic_block then_bb = create_empty_bb (cond_bb); | |||
821 | add_bb_to_loop (then_bb, cond_bb->loop_father); | |||
822 | loops_state_set (LOOPS_NEED_FIXUP); | |||
823 | ||||
824 | /* Make an edge coming from the 'cond block' into the 'then block'; | |||
825 | this edge is unlikely taken, so set up the probability accordingly. */ | |||
826 | e = make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE); | |||
827 | e->probability = profile_probability::very_unlikely (); | |||
828 | then_bb->count = e->count (); | |||
829 | ||||
830 | /* Connect 'then block' with the 'else block'. This is needed | |||
831 | as the ubsan routines we call in the 'then block' are not noreturn. | |||
832 | The 'then block' only has one outcoming edge. */ | |||
833 | make_single_succ_edge (then_bb, fallthru_bb, EDGE_FALLTHRU); | |||
834 | ||||
835 | /* Set up the fallthrough basic block. */ | |||
836 | e = find_edge (cond_bb, fallthru_bb); | |||
837 | e->flags = EDGE_FALSE_VALUE; | |||
838 | e->probability = profile_probability::very_likely (); | |||
839 | ||||
840 | /* Update dominance info for the newly created then_bb; note that | |||
841 | fallthru_bb's dominance info has already been updated by | |||
842 | split_block. */ | |||
843 | if (dom_info_available_p (CDI_DOMINATORS)) | |||
844 | set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb); | |||
845 | ||||
846 | /* Put the ubsan builtin call into the newly created BB. */ | |||
847 | if (flag_sanitize_trapglobal_options.x_flag_sanitize_trap & ((check_align ? SANITIZE_ALIGNMENT + 0 : 0) | |||
848 | | (check_null ? SANITIZE_NULL + 0 : 0))) | |||
849 | g = gimple_build_call (builtin_decl_implicit (BUILT_IN_TRAP), 0); | |||
850 | else | |||
851 | { | |||
852 | enum built_in_function bcode | |||
853 | = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & ((check_align ? SANITIZE_ALIGNMENT + 0 : 0) | |||
854 | | (check_null ? SANITIZE_NULL + 0 : 0))) | |||
855 | ? BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1 | |||
856 | : BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT; | |||
857 | tree fn = builtin_decl_implicit (bcode); | |||
858 | int align_log = tree_log2 (align); | |||
859 | tree data | |||
860 | = ubsan_create_data ("__ubsan_null_data", 1, &loc, | |||
861 | ubsan_type_descriptor (TREE_TYPE (ckind)((contains_struct_check ((ckind), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 861, __FUNCTION__))->typed.type), | |||
862 | UBSAN_PRINT_POINTER), | |||
863 | NULL_TREE(tree) nullptr, | |||
864 | build_int_cst (unsigned_char_type_nodeinteger_types[itk_unsigned_char], | |||
865 | MAX (align_log, 0)((align_log) > (0) ? (align_log) : (0))), | |||
866 | fold_convert (unsigned_char_type_node, ckind)fold_convert_loc (((location_t) 0), integer_types[itk_unsigned_char ], ckind), | |||
867 | NULL_TREE(tree) nullptr); | |||
868 | data = build_fold_addr_expr_loc (loc, data); | |||
869 | g = gimple_build_call (fn, 2, data, | |||
870 | check_align ? check_align | |||
871 | : build_zero_cst (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE])); | |||
872 | } | |||
873 | gimple_stmt_iterator gsi2 = gsi_start_bb (then_bb); | |||
874 | gimple_set_location (g, loc); | |||
875 | gsi_insert_after (&gsi2, g, GSI_NEW_STMT); | |||
876 | ||||
877 | /* Unlink the UBSAN_NULLs vops before replacing it. */ | |||
878 | unlink_stmt_vdef (stmt); | |||
879 | ||||
880 | if (check_null) | |||
881 | { | |||
882 | g = gimple_build_cond (EQ_EXPR, ptr, build_int_cst (TREE_TYPE (ptr)((contains_struct_check ((ptr), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 882, __FUNCTION__))->typed.type), 0), | |||
883 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
884 | gimple_set_location (g, loc); | |||
885 | ||||
886 | /* Replace the UBSAN_NULL with a GIMPLE_COND stmt. */ | |||
887 | gsi_replace (&gsi, g, false); | |||
888 | stmt = g; | |||
889 | } | |||
890 | ||||
891 | if (check_align) | |||
892 | { | |||
893 | if (check_null) | |||
894 | { | |||
895 | /* Split the block with the condition again. */ | |||
896 | e = split_block (cond_bb, stmt); | |||
897 | basic_block cond1_bb = e->src; | |||
898 | basic_block cond2_bb = e->dest; | |||
899 | ||||
900 | /* Make an edge coming from the 'cond1 block' into the 'then block'; | |||
901 | this edge is unlikely taken, so set up the probability | |||
902 | accordingly. */ | |||
903 | e = make_edge (cond1_bb, then_bb, EDGE_TRUE_VALUE); | |||
904 | e->probability = profile_probability::very_unlikely (); | |||
905 | ||||
906 | /* Set up the fallthrough basic block. */ | |||
907 | e = find_edge (cond1_bb, cond2_bb); | |||
908 | e->flags = EDGE_FALSE_VALUE; | |||
909 | e->probability = profile_probability::very_likely (); | |||
910 | ||||
911 | /* Update dominance info. */ | |||
912 | if (dom_info_available_p (CDI_DOMINATORS)) | |||
913 | { | |||
914 | set_immediate_dominator (CDI_DOMINATORS, fallthru_bb, cond1_bb); | |||
915 | set_immediate_dominator (CDI_DOMINATORS, then_bb, cond1_bb); | |||
916 | } | |||
917 | ||||
918 | gsi2 = gsi_start_bb (cond2_bb); | |||
919 | } | |||
920 | ||||
921 | tree mask = build_int_cst (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE], | |||
922 | tree_to_uhwi (align) - 1); | |||
923 | g = gimple_build_assign (make_ssa_name (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE]), | |||
924 | BIT_AND_EXPR, check_align, mask); | |||
925 | gimple_set_location (g, loc); | |||
926 | if (check_null) | |||
927 | gsi_insert_after (&gsi2, g, GSI_NEW_STMT); | |||
928 | else | |||
929 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |||
930 | ||||
931 | g = gimple_build_cond (NE_EXPR, gimple_assign_lhs (g), | |||
932 | build_int_cst (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE], 0), | |||
933 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
934 | gimple_set_location (g, loc); | |||
935 | if (check_null) | |||
936 | gsi_insert_after (&gsi2, g, GSI_NEW_STMT); | |||
937 | else | |||
938 | /* Replace the UBSAN_NULL with a GIMPLE_COND stmt. */ | |||
939 | gsi_replace (&gsi, g, false); | |||
940 | } | |||
941 | return false; | |||
942 | } | |||
943 | ||||
944 | #define OBJSZ_MAX_OFFSET(1024 * 16) (1024 * 16) | |||
945 | ||||
946 | /* Expand UBSAN_OBJECT_SIZE internal call. */ | |||
947 | ||||
948 | bool | |||
949 | ubsan_expand_objsize_ifn (gimple_stmt_iterator *gsi) | |||
950 | { | |||
951 | gimple *stmt = gsi_stmt (*gsi); | |||
952 | location_t loc = gimple_location (stmt); | |||
953 | gcc_assert (gimple_call_num_args (stmt) == 4)((void)(!(gimple_call_num_args (stmt) == 4) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 953, __FUNCTION__), 0 : 0)); | |||
954 | ||||
955 | tree ptr = gimple_call_arg (stmt, 0); | |||
956 | tree offset = gimple_call_arg (stmt, 1); | |||
957 | tree size = gimple_call_arg (stmt, 2); | |||
958 | tree ckind = gimple_call_arg (stmt, 3); | |||
959 | gimple_stmt_iterator gsi_orig = *gsi; | |||
960 | gimple *g; | |||
961 | ||||
962 | /* See if we can discard the check. */ | |||
963 | if (TREE_CODE (size)((enum tree_code) (size)->base.code) == INTEGER_CST | |||
964 | && integer_all_onesp (size)) | |||
965 | /* Yes, __builtin_object_size couldn't determine the | |||
966 | object size. */; | |||
967 | else if (TREE_CODE (offset)((enum tree_code) (offset)->base.code) == INTEGER_CST | |||
968 | && wi::to_widest (offset) >= -OBJSZ_MAX_OFFSET(1024 * 16) | |||
969 | && wi::to_widest (offset) <= -1) | |||
970 | /* The offset is in range [-16K, -1]. */; | |||
971 | else | |||
972 | { | |||
973 | /* if (offset > objsize) */ | |||
974 | basic_block then_bb, fallthru_bb; | |||
975 | gimple_stmt_iterator cond_insert_point | |||
976 | = create_cond_insert_point (gsi, false, false, true, | |||
977 | &then_bb, &fallthru_bb); | |||
978 | g = gimple_build_cond (GT_EXPR, offset, size, NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
979 | gimple_set_location (g, loc); | |||
980 | gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT); | |||
981 | ||||
982 | /* If the offset is small enough, we don't need the second | |||
983 | run-time check. */ | |||
984 | if (TREE_CODE (offset)((enum tree_code) (offset)->base.code) == INTEGER_CST | |||
985 | && wi::to_widest (offset) >= 0 | |||
986 | && wi::to_widest (offset) <= OBJSZ_MAX_OFFSET(1024 * 16)) | |||
987 | *gsi = gsi_after_labels (then_bb); | |||
988 | else | |||
989 | { | |||
990 | /* Don't issue run-time error if (ptr > ptr + offset). That | |||
991 | may happen when computing a POINTER_PLUS_EXPR. */ | |||
992 | basic_block then2_bb, fallthru2_bb; | |||
993 | ||||
994 | gimple_stmt_iterator gsi2 = gsi_after_labels (then_bb); | |||
995 | cond_insert_point = create_cond_insert_point (&gsi2, false, false, | |||
996 | true, &then2_bb, | |||
997 | &fallthru2_bb); | |||
998 | /* Convert the pointer to an integer type. */ | |||
999 | tree p = make_ssa_name (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE]); | |||
1000 | g = gimple_build_assign (p, NOP_EXPR, ptr); | |||
1001 | gimple_set_location (g, loc); | |||
1002 | gsi_insert_before (&cond_insert_point, g, GSI_NEW_STMT); | |||
1003 | p = gimple_assign_lhs (g); | |||
1004 | /* Compute ptr + offset. */ | |||
1005 | g = gimple_build_assign (make_ssa_name (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE]), | |||
1006 | PLUS_EXPR, p, offset); | |||
1007 | gimple_set_location (g, loc); | |||
1008 | gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT); | |||
1009 | /* Now build the conditional and put it into the IR. */ | |||
1010 | g = gimple_build_cond (LE_EXPR, p, gimple_assign_lhs (g), | |||
1011 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
1012 | gimple_set_location (g, loc); | |||
1013 | gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT); | |||
1014 | *gsi = gsi_after_labels (then2_bb); | |||
1015 | } | |||
1016 | ||||
1017 | /* Generate __ubsan_handle_type_mismatch call. */ | |||
1018 | if (flag_sanitize_trapglobal_options.x_flag_sanitize_trap & SANITIZE_OBJECT_SIZE) | |||
1019 | g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0); | |||
1020 | else | |||
1021 | { | |||
1022 | tree data | |||
1023 | = ubsan_create_data ("__ubsan_objsz_data", 1, &loc, | |||
1024 | ubsan_type_descriptor (TREE_TYPE (ptr)((contains_struct_check ((ptr), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1024, __FUNCTION__))->typed.type), | |||
1025 | UBSAN_PRINT_POINTER), | |||
1026 | NULL_TREE(tree) nullptr, | |||
1027 | build_zero_cst (unsigned_char_type_nodeinteger_types[itk_unsigned_char]), | |||
1028 | ckind, | |||
1029 | NULL_TREE(tree) nullptr); | |||
1030 | data = build_fold_addr_expr_loc (loc, data); | |||
1031 | enum built_in_function bcode | |||
1032 | = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & SANITIZE_OBJECT_SIZE) | |||
1033 | ? BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1 | |||
1034 | : BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT; | |||
1035 | tree p = make_ssa_name (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE]); | |||
1036 | g = gimple_build_assign (p, NOP_EXPR, ptr); | |||
1037 | gimple_set_location (g, loc); | |||
1038 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
1039 | g = gimple_build_call (builtin_decl_explicit (bcode), 2, data, p); | |||
1040 | } | |||
1041 | gimple_set_location (g, loc); | |||
1042 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
1043 | ||||
1044 | /* Point GSI to next logical statement. */ | |||
1045 | *gsi = gsi_start_bb (fallthru_bb); | |||
1046 | ||||
1047 | /* Get rid of the UBSAN_OBJECT_SIZE call from the IR. */ | |||
1048 | unlink_stmt_vdef (stmt); | |||
1049 | gsi_remove (&gsi_orig, true); | |||
1050 | return true; | |||
1051 | } | |||
1052 | ||||
1053 | /* Get rid of the UBSAN_OBJECT_SIZE call from the IR. */ | |||
1054 | unlink_stmt_vdef (stmt); | |||
1055 | gsi_remove (gsi, true); | |||
1056 | return true; | |||
1057 | } | |||
1058 | ||||
1059 | /* Expand UBSAN_PTR internal call. */ | |||
1060 | ||||
1061 | bool | |||
1062 | ubsan_expand_ptr_ifn (gimple_stmt_iterator *gsip) | |||
1063 | { | |||
1064 | gimple_stmt_iterator gsi = *gsip; | |||
1065 | gimple *stmt = gsi_stmt (gsi); | |||
1066 | location_t loc = gimple_location (stmt); | |||
1067 | gcc_assert (gimple_call_num_args (stmt) == 2)((void)(!(gimple_call_num_args (stmt) == 2) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1067, __FUNCTION__), 0 : 0)); | |||
1068 | tree ptr = gimple_call_arg (stmt, 0); | |||
1069 | tree off = gimple_call_arg (stmt, 1); | |||
1070 | ||||
1071 | if (integer_zerop (off)) | |||
1072 | { | |||
1073 | gsi_remove (gsip, true); | |||
1074 | unlink_stmt_vdef (stmt); | |||
1075 | return true; | |||
1076 | } | |||
1077 | ||||
1078 | basic_block cur_bb = gsi_bb (gsi); | |||
1079 | tree ptrplusoff = make_ssa_name (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE]); | |||
1080 | tree ptri = make_ssa_name (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE]); | |||
1081 | int pos_neg = get_range_pos_neg (off); | |||
1082 | ||||
1083 | /* Split the original block holding the pointer dereference. */ | |||
1084 | edge e = split_block (cur_bb, stmt); | |||
1085 | ||||
1086 | /* Get a hold on the 'condition block', the 'then block' and the | |||
1087 | 'else block'. */ | |||
1088 | basic_block cond_bb = e->src; | |||
1089 | basic_block fallthru_bb = e->dest; | |||
1090 | basic_block then_bb = create_empty_bb (cond_bb); | |||
1091 | basic_block cond_pos_bb = NULLnullptr, cond_neg_bb = NULLnullptr; | |||
1092 | add_bb_to_loop (then_bb, cond_bb->loop_father); | |||
1093 | loops_state_set (LOOPS_NEED_FIXUP); | |||
1094 | ||||
1095 | /* Set up the fallthrough basic block. */ | |||
1096 | e->flags = EDGE_FALSE_VALUE; | |||
1097 | if (pos_neg != 3) | |||
1098 | { | |||
1099 | e->probability = profile_probability::very_likely (); | |||
1100 | ||||
1101 | /* Connect 'then block' with the 'else block'. This is needed | |||
1102 | as the ubsan routines we call in the 'then block' are not noreturn. | |||
1103 | The 'then block' only has one outcoming edge. */ | |||
1104 | make_single_succ_edge (then_bb, fallthru_bb, EDGE_FALLTHRU); | |||
1105 | ||||
1106 | /* Make an edge coming from the 'cond block' into the 'then block'; | |||
1107 | this edge is unlikely taken, so set up the probability | |||
1108 | accordingly. */ | |||
1109 | e = make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE); | |||
1110 | e->probability = profile_probability::very_unlikely (); | |||
1111 | then_bb->count = e->count (); | |||
1112 | } | |||
1113 | else | |||
1114 | { | |||
1115 | e->probability = profile_probability::even (); | |||
1116 | ||||
1117 | e = split_block (fallthru_bb, (gimple *) NULLnullptr); | |||
1118 | cond_neg_bb = e->src; | |||
1119 | fallthru_bb = e->dest; | |||
1120 | e->probability = profile_probability::very_likely (); | |||
1121 | e->flags = EDGE_FALSE_VALUE; | |||
1122 | ||||
1123 | e = make_edge (cond_neg_bb, then_bb, EDGE_TRUE_VALUE); | |||
1124 | e->probability = profile_probability::very_unlikely (); | |||
1125 | then_bb->count = e->count (); | |||
1126 | ||||
1127 | cond_pos_bb = create_empty_bb (cond_bb); | |||
1128 | add_bb_to_loop (cond_pos_bb, cond_bb->loop_father); | |||
1129 | ||||
1130 | e = make_edge (cond_bb, cond_pos_bb, EDGE_TRUE_VALUE); | |||
1131 | e->probability = profile_probability::even (); | |||
1132 | cond_pos_bb->count = e->count (); | |||
1133 | ||||
1134 | e = make_edge (cond_pos_bb, then_bb, EDGE_TRUE_VALUE); | |||
1135 | e->probability = profile_probability::very_unlikely (); | |||
1136 | ||||
1137 | e = make_edge (cond_pos_bb, fallthru_bb, EDGE_FALSE_VALUE); | |||
1138 | e->probability = profile_probability::very_likely (); | |||
1139 | ||||
1140 | make_single_succ_edge (then_bb, fallthru_bb, EDGE_FALLTHRU); | |||
1141 | } | |||
1142 | ||||
1143 | gimple *g = gimple_build_assign (ptri, NOP_EXPR, ptr); | |||
1144 | gimple_set_location (g, loc); | |||
1145 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |||
1146 | g = gimple_build_assign (ptrplusoff, PLUS_EXPR, ptri, off); | |||
1147 | gimple_set_location (g, loc); | |||
1148 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |||
1149 | ||||
1150 | /* Update dominance info for the newly created then_bb; note that | |||
1151 | fallthru_bb's dominance info has already been updated by | |||
1152 | split_block. */ | |||
1153 | if (dom_info_available_p (CDI_DOMINATORS)) | |||
1154 | { | |||
1155 | set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb); | |||
1156 | if (pos_neg == 3) | |||
1157 | { | |||
1158 | set_immediate_dominator (CDI_DOMINATORS, cond_pos_bb, cond_bb); | |||
1159 | set_immediate_dominator (CDI_DOMINATORS, fallthru_bb, cond_bb); | |||
1160 | } | |||
1161 | } | |||
1162 | ||||
1163 | /* Put the ubsan builtin call into the newly created BB. */ | |||
1164 | if (flag_sanitize_trapglobal_options.x_flag_sanitize_trap & SANITIZE_POINTER_OVERFLOW) | |||
1165 | g = gimple_build_call (builtin_decl_implicit (BUILT_IN_TRAP), 0); | |||
1166 | else | |||
1167 | { | |||
1168 | enum built_in_function bcode | |||
1169 | = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & SANITIZE_POINTER_OVERFLOW) | |||
1170 | ? BUILT_IN_UBSAN_HANDLE_POINTER_OVERFLOW | |||
1171 | : BUILT_IN_UBSAN_HANDLE_POINTER_OVERFLOW_ABORT; | |||
1172 | tree fn = builtin_decl_implicit (bcode); | |||
1173 | tree data | |||
1174 | = ubsan_create_data ("__ubsan_ptrovf_data", 1, &loc, | |||
1175 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
1176 | data = build_fold_addr_expr_loc (loc, data); | |||
1177 | g = gimple_build_call (fn, 3, data, ptr, ptrplusoff); | |||
1178 | } | |||
1179 | gimple_stmt_iterator gsi2 = gsi_start_bb (then_bb); | |||
1180 | gimple_set_location (g, loc); | |||
1181 | gsi_insert_after (&gsi2, g, GSI_NEW_STMT); | |||
1182 | ||||
1183 | /* Unlink the UBSAN_PTRs vops before replacing it. */ | |||
1184 | unlink_stmt_vdef (stmt); | |||
1185 | ||||
1186 | if (TREE_CODE (off)((enum tree_code) (off)->base.code) == INTEGER_CST) | |||
1187 | g = gimple_build_cond (wi::neg_p (wi::to_wide (off)) ? LT_EXPR : GE_EXPR, | |||
1188 | ptri, fold_build1 (NEGATE_EXPR, sizetype, off)fold_build1_loc (((location_t) 0), NEGATE_EXPR, sizetype_tab[ (int) stk_sizetype], off ), | |||
1189 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
1190 | else if (pos_neg != 3) | |||
1191 | g = gimple_build_cond (pos_neg == 1 ? LT_EXPR : GT_EXPR, | |||
1192 | ptrplusoff, ptri, NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
1193 | else | |||
1194 | { | |||
1195 | gsi2 = gsi_start_bb (cond_pos_bb); | |||
1196 | g = gimple_build_cond (LT_EXPR, ptrplusoff, ptri, NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
1197 | gimple_set_location (g, loc); | |||
1198 | gsi_insert_after (&gsi2, g, GSI_NEW_STMT); | |||
1199 | ||||
1200 | gsi2 = gsi_start_bb (cond_neg_bb); | |||
1201 | g = gimple_build_cond (GT_EXPR, ptrplusoff, ptri, NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
1202 | gimple_set_location (g, loc); | |||
1203 | gsi_insert_after (&gsi2, g, GSI_NEW_STMT); | |||
1204 | ||||
1205 | tree t = gimple_build (&gsi, true, GSI_SAME_STMT, | |||
1206 | loc, NOP_EXPR, ssizetypesizetype_tab[(int) stk_ssizetype], off); | |||
1207 | g = gimple_build_cond (GE_EXPR, t, ssize_int (0)size_int_kind (0, stk_ssizetype), | |||
1208 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
1209 | } | |||
1210 | gimple_set_location (g, loc); | |||
1211 | /* Replace the UBSAN_PTR with a GIMPLE_COND stmt. */ | |||
1212 | gsi_replace (&gsi, g, false); | |||
1213 | return false; | |||
1214 | } | |||
1215 | ||||
1216 | ||||
1217 | /* Cached __ubsan_vptr_type_cache decl. */ | |||
1218 | static GTY(()) tree ubsan_vptr_type_cache_decl; | |||
1219 | ||||
1220 | /* Expand UBSAN_VPTR internal call. The type is kept on the ckind | |||
1221 | argument which is a constant, because the middle-end treats pointer | |||
1222 | conversions as useless and therefore the type of the first argument | |||
1223 | could be changed to any other pointer type. */ | |||
1224 | ||||
1225 | bool | |||
1226 | ubsan_expand_vptr_ifn (gimple_stmt_iterator *gsip) | |||
1227 | { | |||
1228 | gimple_stmt_iterator gsi = *gsip; | |||
1229 | gimple *stmt = gsi_stmt (gsi); | |||
1230 | location_t loc = gimple_location (stmt); | |||
1231 | gcc_assert (gimple_call_num_args (stmt) == 5)((void)(!(gimple_call_num_args (stmt) == 5) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1231, __FUNCTION__), 0 : 0)); | |||
1232 | tree op = gimple_call_arg (stmt, 0); | |||
1233 | tree vptr = gimple_call_arg (stmt, 1); | |||
1234 | tree str_hash = gimple_call_arg (stmt, 2); | |||
1235 | tree ti_decl_addr = gimple_call_arg (stmt, 3); | |||
1236 | tree ckind_tree = gimple_call_arg (stmt, 4); | |||
1237 | ubsan_null_ckind ckind = (ubsan_null_ckind) tree_to_uhwi (ckind_tree); | |||
1238 | tree type = TREE_TYPE (TREE_TYPE (ckind_tree))((contains_struct_check ((((contains_struct_check ((ckind_tree ), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1238, __FUNCTION__))->typed.type)), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1238, __FUNCTION__))->typed.type); | |||
1239 | gimple *g; | |||
1240 | basic_block fallthru_bb = NULLnullptr; | |||
1241 | ||||
1242 | if (ckind == UBSAN_DOWNCAST_POINTER) | |||
1243 | { | |||
1244 | /* Guard everything with if (op != NULL) { ... }. */ | |||
1245 | basic_block then_bb; | |||
1246 | gimple_stmt_iterator cond_insert_point | |||
1247 | = create_cond_insert_point (gsip, false, false, true, | |||
1248 | &then_bb, &fallthru_bb); | |||
1249 | g = gimple_build_cond (NE_EXPR, op, build_zero_cst (TREE_TYPE (op)((contains_struct_check ((op), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1249, __FUNCTION__))->typed.type)), | |||
1250 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
1251 | gimple_set_location (g, loc); | |||
1252 | gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT); | |||
1253 | *gsip = gsi_after_labels (then_bb); | |||
1254 | gsi_remove (&gsi, false); | |||
1255 | gsi_insert_before (gsip, stmt, GSI_NEW_STMT); | |||
1256 | gsi = *gsip; | |||
1257 | } | |||
1258 | ||||
1259 | tree htype = TREE_TYPE (str_hash)((contains_struct_check ((str_hash), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1259, __FUNCTION__))->typed.type); | |||
1260 | tree cst = wide_int_to_tree (htype, | |||
1261 | wi::uhwi (((uint64_t) 0x9ddfea08 << 32) | |||
1262 | | 0xeb382d69, 64)); | |||
1263 | g = gimple_build_assign (make_ssa_name (htype), BIT_XOR_EXPR, | |||
1264 | vptr, str_hash); | |||
1265 | gimple_set_location (g, loc); | |||
1266 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1267 | g = gimple_build_assign (make_ssa_name (htype), MULT_EXPR, | |||
1268 | gimple_assign_lhs (g), cst); | |||
1269 | gimple_set_location (g, loc); | |||
1270 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1271 | tree t1 = gimple_assign_lhs (g); | |||
1272 | g = gimple_build_assign (make_ssa_name (htype), LSHIFT_EXPR, | |||
1273 | t1, build_int_cst (integer_type_nodeinteger_types[itk_int], 47)); | |||
1274 | gimple_set_location (g, loc); | |||
1275 | tree t2 = gimple_assign_lhs (g); | |||
1276 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1277 | g = gimple_build_assign (make_ssa_name (htype), BIT_XOR_EXPR, | |||
1278 | vptr, t1); | |||
1279 | gimple_set_location (g, loc); | |||
1280 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1281 | g = gimple_build_assign (make_ssa_name (htype), BIT_XOR_EXPR, | |||
1282 | t2, gimple_assign_lhs (g)); | |||
1283 | gimple_set_location (g, loc); | |||
1284 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1285 | g = gimple_build_assign (make_ssa_name (htype), MULT_EXPR, | |||
1286 | gimple_assign_lhs (g), cst); | |||
1287 | gimple_set_location (g, loc); | |||
1288 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1289 | tree t3 = gimple_assign_lhs (g); | |||
1290 | g = gimple_build_assign (make_ssa_name (htype), LSHIFT_EXPR, | |||
1291 | t3, build_int_cst (integer_type_nodeinteger_types[itk_int], 47)); | |||
1292 | gimple_set_location (g, loc); | |||
1293 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1294 | g = gimple_build_assign (make_ssa_name (htype), BIT_XOR_EXPR, | |||
1295 | t3, gimple_assign_lhs (g)); | |||
1296 | gimple_set_location (g, loc); | |||
1297 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1298 | g = gimple_build_assign (make_ssa_name (htype), MULT_EXPR, | |||
1299 | gimple_assign_lhs (g), cst); | |||
1300 | gimple_set_location (g, loc); | |||
1301 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1302 | if (!useless_type_conversion_p (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE], htype)) | |||
1303 | { | |||
1304 | g = gimple_build_assign (make_ssa_name (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE]), | |||
1305 | NOP_EXPR, gimple_assign_lhs (g)); | |||
1306 | gimple_set_location (g, loc); | |||
1307 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1308 | } | |||
1309 | tree hash = gimple_assign_lhs (g); | |||
1310 | ||||
1311 | if (ubsan_vptr_type_cache_decl == NULL_TREE(tree) nullptr) | |||
1312 | { | |||
1313 | tree atype = build_array_type_nelts (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE], 128); | |||
1314 | tree array = build_decl (UNKNOWN_LOCATION((location_t) 0), VAR_DECL, | |||
1315 | get_identifier ("__ubsan_vptr_type_cache")(__builtin_constant_p ("__ubsan_vptr_type_cache") ? get_identifier_with_length (("__ubsan_vptr_type_cache"), strlen ("__ubsan_vptr_type_cache" )) : get_identifier ("__ubsan_vptr_type_cache")), | |||
1316 | atype); | |||
1317 | DECL_ARTIFICIAL (array)((contains_struct_check ((array), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1317, __FUNCTION__))->decl_common.artificial_flag) = 1; | |||
1318 | DECL_IGNORED_P (array)((contains_struct_check ((array), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1318, __FUNCTION__))->decl_common.ignored_flag) = 1; | |||
1319 | TREE_PUBLIC (array)((array)->base.public_flag) = 1; | |||
1320 | TREE_STATIC (array)((array)->base.static_flag) = 1; | |||
1321 | DECL_EXTERNAL (array)((contains_struct_check ((array), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1321, __FUNCTION__))->decl_common.decl_flag_1) = 1; | |||
1322 | DECL_VISIBILITY (array)((contains_struct_check ((array), (TS_DECL_WITH_VIS), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1322, __FUNCTION__))->decl_with_vis.visibility) = VISIBILITY_DEFAULT; | |||
1323 | DECL_VISIBILITY_SPECIFIED (array)((contains_struct_check ((array), (TS_DECL_WITH_VIS), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1323, __FUNCTION__))->decl_with_vis.visibility_specified ) = 1; | |||
1324 | varpool_node::finalize_decl (array); | |||
1325 | ubsan_vptr_type_cache_decl = array; | |||
1326 | } | |||
1327 | ||||
1328 | g = gimple_build_assign (make_ssa_name (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE]), | |||
1329 | BIT_AND_EXPR, hash, | |||
1330 | build_int_cst (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE], 127)); | |||
1331 | gimple_set_location (g, loc); | |||
1332 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1333 | ||||
1334 | tree c = build4_loc (loc, ARRAY_REF, pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE], | |||
1335 | ubsan_vptr_type_cache_decl, gimple_assign_lhs (g), | |||
1336 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
1337 | g = gimple_build_assign (make_ssa_name (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE]), | |||
1338 | ARRAY_REF, c); | |||
1339 | gimple_set_location (g, loc); | |||
1340 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1341 | ||||
1342 | basic_block then_bb, fallthru2_bb; | |||
1343 | gimple_stmt_iterator cond_insert_point | |||
1344 | = create_cond_insert_point (gsip, false, false, true, | |||
1345 | &then_bb, &fallthru2_bb); | |||
1346 | g = gimple_build_cond (NE_EXPR, gimple_assign_lhs (g), hash, | |||
1347 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
1348 | gimple_set_location (g, loc); | |||
1349 | gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT); | |||
1350 | *gsip = gsi_after_labels (then_bb); | |||
1351 | if (fallthru_bb == NULLnullptr) | |||
1352 | fallthru_bb = fallthru2_bb; | |||
1353 | ||||
1354 | tree data | |||
1355 | = ubsan_create_data ("__ubsan_vptr_data", 1, &loc, | |||
1356 | ubsan_type_descriptor (type), NULL_TREE(tree) nullptr, ti_decl_addr, | |||
1357 | build_int_cst (unsigned_char_type_nodeinteger_types[itk_unsigned_char], ckind), | |||
1358 | NULL_TREE(tree) nullptr); | |||
1359 | data = build_fold_addr_expr_loc (loc, data); | |||
1360 | enum built_in_function bcode | |||
1361 | = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & SANITIZE_VPTR) | |||
1362 | ? BUILT_IN_UBSAN_HANDLE_DYNAMIC_TYPE_CACHE_MISS | |||
1363 | : BUILT_IN_UBSAN_HANDLE_DYNAMIC_TYPE_CACHE_MISS_ABORT; | |||
1364 | ||||
1365 | g = gimple_build_call (builtin_decl_explicit (bcode), 3, data, op, hash); | |||
1366 | gimple_set_location (g, loc); | |||
1367 | gsi_insert_before (gsip, g, GSI_SAME_STMT); | |||
1368 | ||||
1369 | /* Point GSI to next logical statement. */ | |||
1370 | *gsip = gsi_start_bb (fallthru_bb); | |||
1371 | ||||
1372 | /* Get rid of the UBSAN_VPTR call from the IR. */ | |||
1373 | unlink_stmt_vdef (stmt); | |||
1374 | gsi_remove (&gsi, true); | |||
1375 | return true; | |||
1376 | } | |||
1377 | ||||
1378 | /* Instrument a memory reference. BASE is the base of MEM, IS_LHS says | |||
1379 | whether the pointer is on the left hand side of the assignment. */ | |||
1380 | ||||
1381 | static void | |||
1382 | instrument_mem_ref (tree mem, tree base, gimple_stmt_iterator *iter, | |||
1383 | bool is_lhs) | |||
1384 | { | |||
1385 | enum ubsan_null_ckind ikind = is_lhs ? UBSAN_STORE_OF : UBSAN_LOAD_OF; | |||
1386 | unsigned int align = 0; | |||
1387 | if (sanitize_flags_p (SANITIZE_ALIGNMENT)) | |||
1388 | { | |||
1389 | align = min_align_of_type (TREE_TYPE (base)((contains_struct_check ((base), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1389, __FUNCTION__))->typed.type)); | |||
1390 | if (align <= 1) | |||
1391 | align = 0; | |||
1392 | } | |||
1393 | if (align == 0 && !sanitize_flags_p (SANITIZE_NULL)) | |||
1394 | return; | |||
1395 | tree t = TREE_OPERAND (base, 0)(*((const_cast<tree*> (tree_operand_check ((base), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1395, __FUNCTION__))))); | |||
1396 | if (!POINTER_TYPE_P (TREE_TYPE (t))(((enum tree_code) (((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1396, __FUNCTION__))->typed.type))->base.code) == POINTER_TYPE || ((enum tree_code) (((contains_struct_check ((t), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1396, __FUNCTION__))->typed.type))->base.code) == REFERENCE_TYPE )) | |||
1397 | return; | |||
1398 | if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (base))(((enum tree_code) (((contains_struct_check ((base), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1398, __FUNCTION__))->typed.type))->base.code) == RECORD_TYPE || ((enum tree_code) (((contains_struct_check ((base), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1398, __FUNCTION__))->typed.type))->base.code) == UNION_TYPE || ((enum tree_code) (((contains_struct_check ((base), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1398, __FUNCTION__))->typed.type))->base.code) == QUAL_UNION_TYPE ) && mem != base) | |||
1399 | ikind = UBSAN_MEMBER_ACCESS; | |||
1400 | tree kind = build_int_cst (build_pointer_type (TREE_TYPE (base)((contains_struct_check ((base), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1400, __FUNCTION__))->typed.type)), ikind); | |||
1401 | tree alignt = build_int_cst (pointer_sized_int_nodeglobal_trees[TI_POINTER_SIZED_TYPE], align); | |||
1402 | gcall *g = gimple_build_call_internal (IFN_UBSAN_NULL, 3, t, kind, alignt); | |||
1403 | gimple_set_location (g, gimple_location (gsi_stmt (*iter))); | |||
1404 | gsi_insert_before (iter, g, GSI_SAME_STMT); | |||
1405 | } | |||
1406 | ||||
1407 | /* Perform the pointer instrumentation. */ | |||
1408 | ||||
1409 | static void | |||
1410 | instrument_null (gimple_stmt_iterator gsi, tree t, bool is_lhs) | |||
1411 | { | |||
1412 | /* Handle also e.g. &s->i. */ | |||
1413 | if (TREE_CODE (t)((enum tree_code) (t)->base.code) == ADDR_EXPR) | |||
1414 | t = TREE_OPERAND (t, 0)(*((const_cast<tree*> (tree_operand_check ((t), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1414, __FUNCTION__))))); | |||
1415 | tree base = get_base_address (t); | |||
1416 | if (base != NULL_TREE(tree) nullptr | |||
1417 | && TREE_CODE (base)((enum tree_code) (base)->base.code) == MEM_REF | |||
1418 | && TREE_CODE (TREE_OPERAND (base, 0))((enum tree_code) ((*((const_cast<tree*> (tree_operand_check ((base), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1418, __FUNCTION__))))))->base.code) == SSA_NAME) | |||
1419 | instrument_mem_ref (t, base, &gsi, is_lhs); | |||
1420 | } | |||
1421 | ||||
1422 | /* Instrument pointer arithmetics PTR p+ OFF. */ | |||
1423 | ||||
1424 | static void | |||
1425 | instrument_pointer_overflow (gimple_stmt_iterator *gsi, tree ptr, tree off) | |||
1426 | { | |||
1427 | if (TYPE_PRECISION (sizetype)((tree_class_check ((sizetype_tab[(int) stk_sizetype]), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1427, __FUNCTION__))->type_common.precision) != POINTER_SIZE(((global_options.x_ix86_isa_flags & (1UL << 58)) != 0) ? 32 : ((8) * (((global_options.x_ix86_isa_flags & (1UL << 1)) != 0) ? 8 : 4)))) | |||
1428 | return; | |||
1429 | gcall *g = gimple_build_call_internal (IFN_UBSAN_PTR, 2, ptr, off); | |||
1430 | gimple_set_location (g, gimple_location (gsi_stmt (*gsi))); | |||
1431 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
1432 | } | |||
1433 | ||||
1434 | /* Instrument pointer arithmetics if any. */ | |||
1435 | ||||
1436 | static void | |||
1437 | maybe_instrument_pointer_overflow (gimple_stmt_iterator *gsi, tree t) | |||
1438 | { | |||
1439 | if (TYPE_PRECISION (sizetype)((tree_class_check ((sizetype_tab[(int) stk_sizetype]), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1439, __FUNCTION__))->type_common.precision) != POINTER_SIZE(((global_options.x_ix86_isa_flags & (1UL << 58)) != 0) ? 32 : ((8) * (((global_options.x_ix86_isa_flags & (1UL << 1)) != 0) ? 8 : 4)))) | |||
1440 | return; | |||
1441 | ||||
1442 | /* Handle also e.g. &s->i. */ | |||
1443 | if (TREE_CODE (t)((enum tree_code) (t)->base.code) == ADDR_EXPR) | |||
1444 | t = TREE_OPERAND (t, 0)(*((const_cast<tree*> (tree_operand_check ((t), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1444, __FUNCTION__))))); | |||
1445 | ||||
1446 | if (!handled_component_p (t) && TREE_CODE (t)((enum tree_code) (t)->base.code) != MEM_REF) | |||
1447 | return; | |||
1448 | ||||
1449 | poly_int64 bitsize, bitpos, bytepos; | |||
1450 | tree offset; | |||
1451 | machine_mode mode; | |||
1452 | int volatilep = 0, reversep, unsignedp = 0; | |||
1453 | tree inner = get_inner_reference (t, &bitsize, &bitpos, &offset, &mode, | |||
1454 | &unsignedp, &reversep, &volatilep); | |||
1455 | tree moff = NULL_TREE(tree) nullptr; | |||
1456 | ||||
1457 | bool decl_p = DECL_P (inner)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (inner)->base.code))] == tcc_declaration); | |||
1458 | tree base; | |||
1459 | if (decl_p) | |||
1460 | { | |||
1461 | if ((VAR_P (inner)(((enum tree_code) (inner)->base.code) == VAR_DECL) | |||
1462 | || TREE_CODE (inner)((enum tree_code) (inner)->base.code) == PARM_DECL | |||
1463 | || TREE_CODE (inner)((enum tree_code) (inner)->base.code) == RESULT_DECL) | |||
1464 | && DECL_REGISTER (inner)((contains_struct_check ((inner), (TS_DECL_WRTL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1464, __FUNCTION__))->decl_common.decl_flag_0)) | |||
1465 | return; | |||
1466 | base = inner; | |||
1467 | /* If BASE is a fixed size automatic variable or | |||
1468 | global variable defined in the current TU and bitpos | |||
1469 | fits, don't instrument anything. */ | |||
1470 | poly_int64 base_size; | |||
1471 | if (offset == NULL_TREE(tree) nullptr | |||
1472 | && maybe_ne (bitpos, 0) | |||
1473 | && (VAR_P (base)(((enum tree_code) (base)->base.code) == VAR_DECL) | |||
1474 | || TREE_CODE (base)((enum tree_code) (base)->base.code) == PARM_DECL | |||
1475 | || TREE_CODE (base)((enum tree_code) (base)->base.code) == RESULT_DECL) | |||
1476 | && poly_int_tree_p (DECL_SIZE (base)((contains_struct_check ((base), (TS_DECL_COMMON), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1476, __FUNCTION__))->decl_common.size), &base_size) | |||
1477 | && known_ge (base_size, bitpos)(!maybe_lt (base_size, bitpos)) | |||
1478 | && (!is_global_var (base) || decl_binds_to_current_def_p (base))) | |||
1479 | return; | |||
1480 | } | |||
1481 | else if (TREE_CODE (inner)((enum tree_code) (inner)->base.code) == MEM_REF) | |||
1482 | { | |||
1483 | base = TREE_OPERAND (inner, 0)(*((const_cast<tree*> (tree_operand_check ((inner), (0) , "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1483, __FUNCTION__))))); | |||
1484 | if (TREE_CODE (base)((enum tree_code) (base)->base.code) == ADDR_EXPR | |||
1485 | && DECL_P (TREE_OPERAND (base, 0))(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) ((*((const_cast<tree*> (tree_operand_check ((base), ( 0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1485, __FUNCTION__))))))->base.code))] == tcc_declaration ) | |||
1486 | && !TREE_ADDRESSABLE (TREE_OPERAND (base, 0))(((*((const_cast<tree*> (tree_operand_check ((base), (0 ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1486, __FUNCTION__))))))->base.addressable_flag) | |||
1487 | && !is_global_var (TREE_OPERAND (base, 0)(*((const_cast<tree*> (tree_operand_check ((base), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1487, __FUNCTION__))))))) | |||
1488 | return; | |||
1489 | moff = TREE_OPERAND (inner, 1)(*((const_cast<tree*> (tree_operand_check ((inner), (1) , "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1489, __FUNCTION__))))); | |||
1490 | if (integer_zerop (moff)) | |||
1491 | moff = NULL_TREE(tree) nullptr; | |||
1492 | } | |||
1493 | else | |||
1494 | return; | |||
1495 | ||||
1496 | if (!POINTER_TYPE_P (TREE_TYPE (base))(((enum tree_code) (((contains_struct_check ((base), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1496, __FUNCTION__))->typed.type))->base.code) == POINTER_TYPE || ((enum tree_code) (((contains_struct_check ((base), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1496, __FUNCTION__))->typed.type))->base.code) == REFERENCE_TYPE ) && !DECL_P (base)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (base)->base.code))] == tcc_declaration)) | |||
1497 | return; | |||
1498 | bytepos = bits_to_bytes_round_down (bitpos)force_align_down_and_div (bitpos, (8)); | |||
1499 | if (offset == NULL_TREE(tree) nullptr && known_eq (bytepos, 0)(!maybe_ne (bytepos, 0)) && moff == NULL_TREE(tree) nullptr) | |||
1500 | return; | |||
1501 | ||||
1502 | tree base_addr = base; | |||
1503 | if (decl_p) | |||
1504 | base_addr = build1 (ADDR_EXPR, | |||
1505 | build_pointer_type (TREE_TYPE (base)((contains_struct_check ((base), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1505, __FUNCTION__))->typed.type)), base); | |||
1506 | t = offset; | |||
1507 | if (maybe_ne (bytepos, 0)) | |||
1508 | { | |||
1509 | if (t) | |||
1510 | t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t,fold_build2_loc (((location_t) 0), PLUS_EXPR, ((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1510, __FUNCTION__))->typed.type), t, build_int_cst (((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1511, __FUNCTION__))->typed.type), bytepos) ) | |||
1511 | build_int_cst (TREE_TYPE (t), bytepos))fold_build2_loc (((location_t) 0), PLUS_EXPR, ((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1510, __FUNCTION__))->typed.type), t, build_int_cst (((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1511, __FUNCTION__))->typed.type), bytepos) ); | |||
1512 | else | |||
1513 | t = size_int (bytepos)size_int_kind (bytepos, stk_sizetype); | |||
1514 | } | |||
1515 | if (moff) | |||
1516 | { | |||
1517 | if (t) | |||
1518 | t = fold_build2 (PLUS_EXPR, TREE_TYPE (t), t,fold_build2_loc (((location_t) 0), PLUS_EXPR, ((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1518, __FUNCTION__))->typed.type), t, fold_convert_loc ( ((location_t) 0), ((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1519, __FUNCTION__))->typed.type), moff) ) | |||
1519 | fold_convert (TREE_TYPE (t), moff))fold_build2_loc (((location_t) 0), PLUS_EXPR, ((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1518, __FUNCTION__))->typed.type), t, fold_convert_loc ( ((location_t) 0), ((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1519, __FUNCTION__))->typed.type), moff) ); | |||
1520 | else | |||
1521 | t = fold_convert (sizetype, moff)fold_convert_loc (((location_t) 0), sizetype_tab[(int) stk_sizetype ], moff); | |||
1522 | } | |||
1523 | t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE(tree) nullptr, true, | |||
1524 | GSI_SAME_STMT); | |||
1525 | base_addr = force_gimple_operand_gsi (gsi, base_addr, true, NULL_TREE(tree) nullptr, true, | |||
1526 | GSI_SAME_STMT); | |||
1527 | instrument_pointer_overflow (gsi, base_addr, t); | |||
1528 | } | |||
1529 | ||||
1530 | /* Build an ubsan builtin call for the signed-integer-overflow | |||
1531 | sanitization. CODE says what kind of builtin are we building, | |||
1532 | LOC is a location, LHSTYPE is the type of LHS, OP0 and OP1 | |||
1533 | are operands of the binary operation. */ | |||
1534 | ||||
1535 | tree | |||
1536 | ubsan_build_overflow_builtin (tree_code code, location_t loc, tree lhstype, | |||
1537 | tree op0, tree op1, tree *datap) | |||
1538 | { | |||
1539 | if (flag_sanitize_trapglobal_options.x_flag_sanitize_trap & SANITIZE_SI_OVERFLOW) | |||
1540 | return build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0); | |||
1541 | ||||
1542 | tree data; | |||
1543 | if (datap && *datap) | |||
1544 | data = *datap; | |||
1545 | else | |||
1546 | data = ubsan_create_data ("__ubsan_overflow_data", 1, &loc, | |||
1547 | ubsan_type_descriptor (lhstype), NULL_TREE(tree) nullptr, | |||
1548 | NULL_TREE(tree) nullptr); | |||
1549 | if (datap) | |||
1550 | *datap = data; | |||
1551 | enum built_in_function fn_code; | |||
1552 | ||||
1553 | switch (code) | |||
1554 | { | |||
1555 | case PLUS_EXPR: | |||
1556 | fn_code = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & SANITIZE_SI_OVERFLOW) | |||
1557 | ? BUILT_IN_UBSAN_HANDLE_ADD_OVERFLOW | |||
1558 | : BUILT_IN_UBSAN_HANDLE_ADD_OVERFLOW_ABORT; | |||
1559 | break; | |||
1560 | case MINUS_EXPR: | |||
1561 | fn_code = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & SANITIZE_SI_OVERFLOW) | |||
1562 | ? BUILT_IN_UBSAN_HANDLE_SUB_OVERFLOW | |||
1563 | : BUILT_IN_UBSAN_HANDLE_SUB_OVERFLOW_ABORT; | |||
1564 | break; | |||
1565 | case MULT_EXPR: | |||
1566 | fn_code = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & SANITIZE_SI_OVERFLOW) | |||
1567 | ? BUILT_IN_UBSAN_HANDLE_MUL_OVERFLOW | |||
1568 | : BUILT_IN_UBSAN_HANDLE_MUL_OVERFLOW_ABORT; | |||
1569 | break; | |||
1570 | case NEGATE_EXPR: | |||
1571 | fn_code = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & SANITIZE_SI_OVERFLOW) | |||
1572 | ? BUILT_IN_UBSAN_HANDLE_NEGATE_OVERFLOW | |||
1573 | : BUILT_IN_UBSAN_HANDLE_NEGATE_OVERFLOW_ABORT; | |||
1574 | break; | |||
1575 | default: | |||
1576 | gcc_unreachable ()(fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1576, __FUNCTION__)); | |||
1577 | } | |||
1578 | tree fn = builtin_decl_explicit (fn_code); | |||
1579 | return build_call_expr_loc (loc, fn, 2 + (code != NEGATE_EXPR), | |||
1580 | build_fold_addr_expr_loc (loc, data), | |||
1581 | ubsan_encode_value (op0, UBSAN_ENCODE_VALUE_RTL), | |||
1582 | op1 | |||
1583 | ? ubsan_encode_value (op1, | |||
1584 | UBSAN_ENCODE_VALUE_RTL) | |||
1585 | : NULL_TREE(tree) nullptr); | |||
1586 | } | |||
1587 | ||||
1588 | /* Perform the signed integer instrumentation. GSI is the iterator | |||
1589 | pointing at statement we are trying to instrument. */ | |||
1590 | ||||
1591 | static void | |||
1592 | instrument_si_overflow (gimple_stmt_iterator gsi) | |||
1593 | { | |||
1594 | gimple *stmt = gsi_stmt (gsi); | |||
1595 | tree_code code = gimple_assign_rhs_code (stmt); | |||
1596 | tree lhs = gimple_assign_lhs (stmt); | |||
1597 | tree lhstype = TREE_TYPE (lhs)((contains_struct_check ((lhs), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1597, __FUNCTION__))->typed.type); | |||
1598 | tree lhsinner = VECTOR_TYPE_P (lhstype)(((enum tree_code) (lhstype)->base.code) == VECTOR_TYPE) ? TREE_TYPE (lhstype)((contains_struct_check ((lhstype), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1598, __FUNCTION__))->typed.type) : lhstype; | |||
1599 | tree a, b; | |||
1600 | gimple *g; | |||
1601 | ||||
1602 | /* If this is not a signed operation, don't instrument anything here. | |||
1603 | Also punt on bit-fields. */ | |||
1604 | if (!INTEGRAL_TYPE_P (lhsinner)(((enum tree_code) (lhsinner)->base.code) == ENUMERAL_TYPE || ((enum tree_code) (lhsinner)->base.code) == BOOLEAN_TYPE || ((enum tree_code) (lhsinner)->base.code) == INTEGER_TYPE ) | |||
1605 | || TYPE_OVERFLOW_WRAPS (lhsinner)((((enum tree_code) (lhsinner)->base.code) == POINTER_TYPE || ((enum tree_code) (lhsinner)->base.code) == REFERENCE_TYPE ) ? global_options.x_flag_wrapv_pointer : ((any_integral_type_check ((lhsinner), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1605, __FUNCTION__))->base.u.bits.unsigned_flag || global_options .x_flag_wrapv)) | |||
1606 | || maybe_ne (GET_MODE_BITSIZE (TYPE_MODE (lhsinner)((((enum tree_code) ((tree_class_check ((lhsinner), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1606, __FUNCTION__)))->base.code) == VECTOR_TYPE) ? vector_type_mode (lhsinner) : (lhsinner)->type_common.mode)), | |||
1607 | TYPE_PRECISION (lhsinner)((tree_class_check ((lhsinner), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1607, __FUNCTION__))->type_common.precision))) | |||
1608 | return; | |||
1609 | ||||
1610 | switch (code) | |||
1611 | { | |||
1612 | case MINUS_EXPR: | |||
1613 | case PLUS_EXPR: | |||
1614 | case MULT_EXPR: | |||
1615 | /* Transform | |||
1616 | i = u {+,-,*} 5; | |||
1617 | into | |||
1618 | i = UBSAN_CHECK_{ADD,SUB,MUL} (u, 5); */ | |||
1619 | a = gimple_assign_rhs1 (stmt); | |||
1620 | b = gimple_assign_rhs2 (stmt); | |||
1621 | g = gimple_build_call_internal (code == PLUS_EXPR | |||
1622 | ? IFN_UBSAN_CHECK_ADD | |||
1623 | : code == MINUS_EXPR | |||
1624 | ? IFN_UBSAN_CHECK_SUB | |||
1625 | : IFN_UBSAN_CHECK_MUL, 2, a, b); | |||
1626 | gimple_call_set_lhs (g, lhs); | |||
1627 | gsi_replace (&gsi, g, true); | |||
1628 | break; | |||
1629 | case NEGATE_EXPR: | |||
1630 | /* Represent i = -u; | |||
1631 | as | |||
1632 | i = UBSAN_CHECK_SUB (0, u); */ | |||
1633 | a = build_zero_cst (lhstype); | |||
1634 | b = gimple_assign_rhs1 (stmt); | |||
1635 | g = gimple_build_call_internal (IFN_UBSAN_CHECK_SUB, 2, a, b); | |||
1636 | gimple_call_set_lhs (g, lhs); | |||
1637 | gsi_replace (&gsi, g, true); | |||
1638 | break; | |||
1639 | case ABS_EXPR: | |||
1640 | /* Transform i = ABS_EXPR<u>; | |||
1641 | into | |||
1642 | _N = UBSAN_CHECK_SUB (0, u); | |||
1643 | i = ABS_EXPR<_N>; */ | |||
1644 | a = build_zero_cst (lhstype); | |||
1645 | b = gimple_assign_rhs1 (stmt); | |||
1646 | g = gimple_build_call_internal (IFN_UBSAN_CHECK_SUB, 2, a, b); | |||
1647 | a = make_ssa_name (lhstype); | |||
1648 | gimple_call_set_lhs (g, a); | |||
1649 | gimple_set_location (g, gimple_location (stmt)); | |||
1650 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |||
1651 | gimple_assign_set_rhs1 (stmt, a); | |||
1652 | update_stmt (stmt); | |||
1653 | break; | |||
1654 | default: | |||
1655 | break; | |||
1656 | } | |||
1657 | } | |||
1658 | ||||
1659 | /* Instrument loads from (non-bitfield) bool and C++ enum values | |||
1660 | to check if the memory value is outside of the range of the valid | |||
1661 | type values. */ | |||
1662 | ||||
1663 | static void | |||
1664 | instrument_bool_enum_load (gimple_stmt_iterator *gsi) | |||
1665 | { | |||
1666 | gimple *stmt = gsi_stmt (*gsi); | |||
1667 | tree rhs = gimple_assign_rhs1 (stmt); | |||
1668 | tree type = TREE_TYPE (rhs)((contains_struct_check ((rhs), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1668, __FUNCTION__))->typed.type); | |||
1669 | tree minv = NULL_TREE(tree) nullptr, maxv = NULL_TREE(tree) nullptr; | |||
1670 | ||||
1671 | if (TREE_CODE (type)((enum tree_code) (type)->base.code) == BOOLEAN_TYPE | |||
1672 | && sanitize_flags_p (SANITIZE_BOOL)) | |||
1673 | { | |||
1674 | minv = boolean_false_nodeglobal_trees[TI_BOOLEAN_FALSE]; | |||
1675 | maxv = boolean_true_nodeglobal_trees[TI_BOOLEAN_TRUE]; | |||
1676 | } | |||
1677 | else if (TREE_CODE (type)((enum tree_code) (type)->base.code) == ENUMERAL_TYPE | |||
1678 | && sanitize_flags_p (SANITIZE_ENUM) | |||
1679 | && TREE_TYPE (type)((contains_struct_check ((type), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1679, __FUNCTION__))->typed.type) != NULL_TREE(tree) nullptr | |||
1680 | && TREE_CODE (TREE_TYPE (type))((enum tree_code) (((contains_struct_check ((type), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1680, __FUNCTION__))->typed.type))->base.code) == INTEGER_TYPE | |||
1681 | && (TYPE_PRECISION (TREE_TYPE (type))((tree_class_check ((((contains_struct_check ((type), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1681, __FUNCTION__))->typed.type)), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1681, __FUNCTION__))->type_common.precision) | |||
1682 | < GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (type)(as_a <scalar_int_mode> ((tree_class_check ((type), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1682, __FUNCTION__))->type_common.mode))))) | |||
1683 | { | |||
1684 | minv = TYPE_MIN_VALUE (TREE_TYPE (type))((tree_check5 ((((contains_struct_check ((type), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1684, __FUNCTION__))->typed.type)), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1684, __FUNCTION__, (INTEGER_TYPE), (ENUMERAL_TYPE), (BOOLEAN_TYPE ), (REAL_TYPE), (FIXED_POINT_TYPE)))->type_non_common.minval ); | |||
1685 | maxv = TYPE_MAX_VALUE (TREE_TYPE (type))((tree_check5 ((((contains_struct_check ((type), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1685, __FUNCTION__))->typed.type)), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1685, __FUNCTION__, (INTEGER_TYPE), (ENUMERAL_TYPE), (BOOLEAN_TYPE ), (REAL_TYPE), (FIXED_POINT_TYPE)))->type_non_common.maxval ); | |||
1686 | } | |||
1687 | else | |||
1688 | return; | |||
1689 | ||||
1690 | int modebitsize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type)(as_a <scalar_int_mode> ((tree_class_check ((type), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1690, __FUNCTION__))->type_common.mode))); | |||
1691 | poly_int64 bitsize, bitpos; | |||
1692 | tree offset; | |||
1693 | machine_mode mode; | |||
1694 | int volatilep = 0, reversep, unsignedp = 0; | |||
1695 | tree base = get_inner_reference (rhs, &bitsize, &bitpos, &offset, &mode, | |||
1696 | &unsignedp, &reversep, &volatilep); | |||
1697 | tree utype = build_nonstandard_integer_type (modebitsize, 1); | |||
1698 | ||||
1699 | if ((VAR_P (base)(((enum tree_code) (base)->base.code) == VAR_DECL) && DECL_HARD_REGISTER (base)((tree_check ((base), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1699, __FUNCTION__, (VAR_DECL)))->decl_with_vis.hard_register )) | |||
1700 | || !multiple_p (bitpos, modebitsize) | |||
1701 | || maybe_ne (bitsize, modebitsize) | |||
1702 | || GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (utype)(as_a <scalar_int_mode> ((tree_class_check ((utype), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1702, __FUNCTION__))->type_common.mode))) != modebitsize | |||
1703 | || TREE_CODE (gimple_assign_lhs (stmt))((enum tree_code) (gimple_assign_lhs (stmt))->base.code) != SSA_NAME) | |||
1704 | return; | |||
1705 | ||||
1706 | bool ends_bb = stmt_ends_bb_p (stmt); | |||
1707 | location_t loc = gimple_location (stmt); | |||
1708 | tree lhs = gimple_assign_lhs (stmt); | |||
1709 | tree ptype = build_pointer_type (TREE_TYPE (rhs)((contains_struct_check ((rhs), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1709, __FUNCTION__))->typed.type)); | |||
1710 | tree atype = reference_alias_ptr_type (rhs); | |||
1711 | gimple *g = gimple_build_assign (make_ssa_name (ptype), | |||
1712 | build_fold_addr_expr (rhs)build_fold_addr_expr_loc (((location_t) 0), (rhs))); | |||
1713 | gimple_set_location (g, loc); | |||
1714 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
1715 | tree mem = build2 (MEM_REF, utype, gimple_assign_lhs (g), | |||
1716 | build_int_cst (atype, 0)); | |||
1717 | tree urhs = make_ssa_name (utype); | |||
1718 | if (ends_bb) | |||
1719 | { | |||
1720 | gimple_assign_set_lhs (stmt, urhs); | |||
1721 | g = gimple_build_assign (lhs, NOP_EXPR, urhs); | |||
1722 | gimple_set_location (g, loc); | |||
1723 | edge e = find_fallthru_edge (gimple_bb (stmt)->succs); | |||
1724 | gsi_insert_on_edge_immediate (e, g); | |||
1725 | gimple_assign_set_rhs_from_tree (gsi, mem); | |||
1726 | update_stmt (stmt); | |||
1727 | *gsi = gsi_for_stmt (g); | |||
1728 | g = stmt; | |||
1729 | } | |||
1730 | else | |||
1731 | { | |||
1732 | g = gimple_build_assign (urhs, mem); | |||
1733 | gimple_set_location (g, loc); | |||
1734 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
1735 | } | |||
1736 | minv = fold_convert (utype, minv)fold_convert_loc (((location_t) 0), utype, minv); | |||
1737 | maxv = fold_convert (utype, maxv)fold_convert_loc (((location_t) 0), utype, maxv); | |||
1738 | if (!integer_zerop (minv)) | |||
1739 | { | |||
1740 | g = gimple_build_assign (make_ssa_name (utype), MINUS_EXPR, urhs, minv); | |||
1741 | gimple_set_location (g, loc); | |||
1742 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
1743 | } | |||
1744 | ||||
1745 | gimple_stmt_iterator gsi2 = *gsi; | |||
1746 | basic_block then_bb, fallthru_bb; | |||
1747 | *gsi = create_cond_insert_point (gsi, true, false, true, | |||
1748 | &then_bb, &fallthru_bb); | |||
1749 | g = gimple_build_cond (GT_EXPR, gimple_assign_lhs (g), | |||
1750 | int_const_binop (MINUS_EXPR, maxv, minv), | |||
1751 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
1752 | gimple_set_location (g, loc); | |||
1753 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |||
1754 | ||||
1755 | if (!ends_bb) | |||
1756 | { | |||
1757 | gimple_assign_set_rhs_with_ops (&gsi2, NOP_EXPR, urhs); | |||
1758 | update_stmt (stmt); | |||
1759 | } | |||
1760 | ||||
1761 | gsi2 = gsi_after_labels (then_bb); | |||
1762 | if (flag_sanitize_trapglobal_options.x_flag_sanitize_trap & (TREE_CODE (type)((enum tree_code) (type)->base.code) == BOOLEAN_TYPE | |||
1763 | ? SANITIZE_BOOL : SANITIZE_ENUM)) | |||
1764 | g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0); | |||
1765 | else | |||
1766 | { | |||
1767 | tree data = ubsan_create_data ("__ubsan_invalid_value_data", 1, &loc, | |||
1768 | ubsan_type_descriptor (type), NULL_TREE(tree) nullptr, | |||
1769 | NULL_TREE(tree) nullptr); | |||
1770 | data = build_fold_addr_expr_loc (loc, data); | |||
1771 | enum built_in_function bcode | |||
1772 | = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & (TREE_CODE (type)((enum tree_code) (type)->base.code) == BOOLEAN_TYPE | |||
1773 | ? SANITIZE_BOOL : SANITIZE_ENUM)) | |||
1774 | ? BUILT_IN_UBSAN_HANDLE_LOAD_INVALID_VALUE | |||
1775 | : BUILT_IN_UBSAN_HANDLE_LOAD_INVALID_VALUE_ABORT; | |||
1776 | tree fn = builtin_decl_explicit (bcode); | |||
1777 | ||||
1778 | tree val = ubsan_encode_value (urhs, UBSAN_ENCODE_VALUE_GIMPLE); | |||
1779 | val = force_gimple_operand_gsi (&gsi2, val, true, NULL_TREE(tree) nullptr, true, | |||
1780 | GSI_SAME_STMT); | |||
1781 | g = gimple_build_call (fn, 2, data, val); | |||
1782 | } | |||
1783 | gimple_set_location (g, loc); | |||
1784 | gsi_insert_before (&gsi2, g, GSI_SAME_STMT); | |||
1785 | ubsan_create_edge (g); | |||
1786 | *gsi = gsi_for_stmt (stmt); | |||
1787 | } | |||
1788 | ||||
1789 | /* Determine if we can propagate given LOCATION to ubsan_data descriptor to use | |||
1790 | new style handlers. Libubsan uses heuristics to destinguish between old and | |||
1791 | new styles and relies on these properties for filename: | |||
1792 | ||||
1793 | a) Location's filename must not be NULL. | |||
1794 | b) Location's filename must not be equal to "". | |||
1795 | c) Location's filename must not be equal to "\1". | |||
1796 | d) First two bytes of filename must not contain '\xff' symbol. */ | |||
1797 | ||||
1798 | static bool | |||
1799 | ubsan_use_new_style_p (location_t loc) | |||
1800 | { | |||
1801 | if (loc == UNKNOWN_LOCATION((location_t) 0)) | |||
1802 | return false; | |||
1803 | ||||
1804 | expanded_location xloc = expand_location (loc); | |||
1805 | if (xloc.file == NULLnullptr || startswith (xloc.file, "\1") | |||
1806 | || xloc.file[0] == '\0' || xloc.file[0] == '\xff' | |||
1807 | || xloc.file[1] == '\xff') | |||
1808 | return false; | |||
1809 | ||||
1810 | return true; | |||
1811 | } | |||
1812 | ||||
1813 | /* Instrument float point-to-integer conversion. TYPE is an integer type of | |||
1814 | destination, EXPR is floating-point expression. */ | |||
1815 | ||||
1816 | tree | |||
1817 | ubsan_instrument_float_cast (location_t loc, tree type, tree expr) | |||
1818 | { | |||
1819 | tree expr_type = TREE_TYPE (expr)((contains_struct_check ((expr), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1819, __FUNCTION__))->typed.type); | |||
1820 | tree t, tt, fn, min, max; | |||
1821 | machine_mode mode = TYPE_MODE (expr_type)((((enum tree_code) ((tree_class_check ((expr_type), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1821, __FUNCTION__)))->base.code) == VECTOR_TYPE) ? vector_type_mode (expr_type) : (expr_type)->type_common.mode); | |||
1822 | int prec = TYPE_PRECISION (type)((tree_class_check ((type), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1822, __FUNCTION__))->type_common.precision); | |||
1823 | bool uns_p = TYPE_UNSIGNED (type)((tree_class_check ((type), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1823, __FUNCTION__))->base.u.bits.unsigned_flag); | |||
1824 | if (loc == UNKNOWN_LOCATION((location_t) 0)) | |||
1825 | loc = input_location; | |||
1826 | ||||
1827 | /* Float to integer conversion first truncates toward zero, so | |||
1828 | even signed char c = 127.875f; is not problematic. | |||
1829 | Therefore, we should complain only if EXPR is unordered or smaller | |||
1830 | or equal than TYPE_MIN_VALUE - 1.0 or greater or equal than | |||
1831 | TYPE_MAX_VALUE + 1.0. */ | |||
1832 | if (REAL_MODE_FORMAT (mode)(real_format_for_mode[(((enum mode_class) mode_class[mode]) == MODE_DECIMAL_FLOAT) ? (((mode) - MIN_MODE_DECIMAL_FLOAT) + ( MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1)) : ((enum mode_class) mode_class [mode]) == MODE_FLOAT ? ((mode) - MIN_MODE_FLOAT) : ((fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1832, __FUNCTION__)), 0)])->b == 2) | |||
1833 | { | |||
1834 | /* For maximum, TYPE_MAX_VALUE might not be representable | |||
1835 | in EXPR_TYPE, e.g. if TYPE is 64-bit long long and | |||
1836 | EXPR_TYPE is IEEE single float, but TYPE_MAX_VALUE + 1.0 is | |||
1837 | either representable or infinity. */ | |||
1838 | REAL_VALUE_TYPEstruct real_value maxval = dconst1; | |||
1839 | SET_REAL_EXP (&maxval, REAL_EXP (&maxval) + prec - !uns_p)((&maxval)->uexp = ((unsigned int)(((int)((&maxval )->uexp ^ (unsigned int)(1 << ((32 - 6) - 1))) - (1 << ((32 - 6) - 1))) + prec - !uns_p) & (unsigned int)((1 << (32 - 6)) - 1))); | |||
1840 | real_convert (&maxval, mode, &maxval); | |||
1841 | max = build_real (expr_type, maxval); | |||
1842 | ||||
1843 | /* For unsigned, assume -1.0 is always representable. */ | |||
1844 | if (uns_p) | |||
1845 | min = build_minus_one_cst (expr_type); | |||
1846 | else | |||
1847 | { | |||
1848 | /* TYPE_MIN_VALUE is generally representable (or -inf), | |||
1849 | but TYPE_MIN_VALUE - 1.0 might not be. */ | |||
1850 | REAL_VALUE_TYPEstruct real_value minval = dconstm1, minval2; | |||
1851 | SET_REAL_EXP (&minval, REAL_EXP (&minval) + prec - 1)((&minval)->uexp = ((unsigned int)(((int)((&minval )->uexp ^ (unsigned int)(1 << ((32 - 6) - 1))) - (1 << ((32 - 6) - 1))) + prec - 1) & (unsigned int)((1 << (32 - 6)) - 1))); | |||
1852 | real_convert (&minval, mode, &minval); | |||
1853 | real_arithmetic (&minval2, MINUS_EXPR, &minval, &dconst1); | |||
1854 | real_convert (&minval2, mode, &minval2); | |||
1855 | if (real_compare (EQ_EXPR, &minval, &minval2) | |||
1856 | && !real_isinf (&minval)) | |||
1857 | { | |||
1858 | /* If TYPE_MIN_VALUE - 1.0 is not representable and | |||
1859 | rounds to TYPE_MIN_VALUE, we need to subtract | |||
1860 | more. As REAL_MODE_FORMAT (mode)->p is the number | |||
1861 | of base digits, we want to subtract a number that | |||
1862 | will be 1 << (REAL_MODE_FORMAT (mode)->p - 1) | |||
1863 | times smaller than minval. */ | |||
1864 | minval2 = dconst1; | |||
1865 | gcc_assert (prec > REAL_MODE_FORMAT (mode)->p)((void)(!(prec > (real_format_for_mode[(((enum mode_class) mode_class[mode]) == MODE_DECIMAL_FLOAT) ? (((mode) - MIN_MODE_DECIMAL_FLOAT ) + (MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1)) : ((enum mode_class ) mode_class[mode]) == MODE_FLOAT ? ((mode) - MIN_MODE_FLOAT) : ((fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1865, __FUNCTION__)), 0)])->p) ? fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1865, __FUNCTION__), 0 : 0)); | |||
1866 | SET_REAL_EXP (&minval2,((&minval2)->uexp = ((unsigned int)(((int)((&minval2 )->uexp ^ (unsigned int)(1 << ((32 - 6) - 1))) - (1 << ((32 - 6) - 1))) + prec - 1 - (real_format_for_mode[(((enum mode_class ) mode_class[mode]) == MODE_DECIMAL_FLOAT) ? (((mode) - MIN_MODE_DECIMAL_FLOAT ) + (MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1)) : ((enum mode_class ) mode_class[mode]) == MODE_FLOAT ? ((mode) - MIN_MODE_FLOAT) : ((fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1868, __FUNCTION__)), 0)])->p + 1) & (unsigned int)( (1 << (32 - 6)) - 1))) | |||
1867 | REAL_EXP (&minval2) + prec - 1((&minval2)->uexp = ((unsigned int)(((int)((&minval2 )->uexp ^ (unsigned int)(1 << ((32 - 6) - 1))) - (1 << ((32 - 6) - 1))) + prec - 1 - (real_format_for_mode[(((enum mode_class ) mode_class[mode]) == MODE_DECIMAL_FLOAT) ? (((mode) - MIN_MODE_DECIMAL_FLOAT ) + (MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1)) : ((enum mode_class ) mode_class[mode]) == MODE_FLOAT ? ((mode) - MIN_MODE_FLOAT) : ((fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1868, __FUNCTION__)), 0)])->p + 1) & (unsigned int)( (1 << (32 - 6)) - 1))) | |||
1868 | - REAL_MODE_FORMAT (mode)->p + 1)((&minval2)->uexp = ((unsigned int)(((int)((&minval2 )->uexp ^ (unsigned int)(1 << ((32 - 6) - 1))) - (1 << ((32 - 6) - 1))) + prec - 1 - (real_format_for_mode[(((enum mode_class ) mode_class[mode]) == MODE_DECIMAL_FLOAT) ? (((mode) - MIN_MODE_DECIMAL_FLOAT ) + (MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1)) : ((enum mode_class ) mode_class[mode]) == MODE_FLOAT ? ((mode) - MIN_MODE_FLOAT) : ((fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1868, __FUNCTION__)), 0)])->p + 1) & (unsigned int)( (1 << (32 - 6)) - 1))); | |||
1869 | real_arithmetic (&minval2, MINUS_EXPR, &minval, &minval2); | |||
1870 | real_convert (&minval2, mode, &minval2); | |||
1871 | } | |||
1872 | min = build_real (expr_type, minval2); | |||
1873 | } | |||
1874 | } | |||
1875 | else if (REAL_MODE_FORMAT (mode)(real_format_for_mode[(((enum mode_class) mode_class[mode]) == MODE_DECIMAL_FLOAT) ? (((mode) - MIN_MODE_DECIMAL_FLOAT) + ( MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1)) : ((enum mode_class) mode_class [mode]) == MODE_FLOAT ? ((mode) - MIN_MODE_FLOAT) : ((fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1875, __FUNCTION__)), 0)])->b == 10) | |||
1876 | { | |||
1877 | /* For _Decimal128 up to 34 decimal digits, - sign, | |||
1878 | dot, e, exponent. */ | |||
1879 | char buf[64]; | |||
1880 | mpfr_t m; | |||
1881 | int p = REAL_MODE_FORMAT (mode)(real_format_for_mode[(((enum mode_class) mode_class[mode]) == MODE_DECIMAL_FLOAT) ? (((mode) - MIN_MODE_DECIMAL_FLOAT) + ( MAX_MODE_FLOAT - MIN_MODE_FLOAT + 1)) : ((enum mode_class) mode_class [mode]) == MODE_FLOAT ? ((mode) - MIN_MODE_FLOAT) : ((fancy_abort ("/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1881, __FUNCTION__)), 0)])->p; | |||
1882 | REAL_VALUE_TYPEstruct real_value maxval, minval; | |||
1883 | ||||
1884 | /* Use mpfr_snprintf rounding to compute the smallest | |||
1885 | representable decimal number greater or equal than | |||
1886 | 1 << (prec - !uns_p). */ | |||
1887 | mpfr_init2 (m, prec + 2); | |||
1888 | mpfr_set_ui_2exp (m, 1, prec - !uns_p, MPFR_RNDN); | |||
1889 | mpfr_snprintf (buf, sizeof buf, "%.*RUe", p - 1, m); | |||
1890 | decimal_real_from_string (&maxval, buf); | |||
1891 | max = build_real (expr_type, maxval); | |||
1892 | ||||
1893 | /* For unsigned, assume -1.0 is always representable. */ | |||
1894 | if (uns_p) | |||
1895 | min = build_minus_one_cst (expr_type); | |||
1896 | else | |||
1897 | { | |||
1898 | /* Use mpfr_snprintf rounding to compute the largest | |||
1899 | representable decimal number less or equal than | |||
1900 | (-1 << (prec - 1)) - 1. */ | |||
1901 | mpfr_set_si_2exp (m, -1, prec - 1, MPFR_RNDN); | |||
1902 | mpfr_sub_ui (m, m, 1, MPFR_RNDN); | |||
1903 | mpfr_snprintf (buf, sizeof buf, "%.*RDe", p - 1, m); | |||
1904 | decimal_real_from_string (&minval, buf); | |||
1905 | min = build_real (expr_type, minval); | |||
1906 | } | |||
1907 | mpfr_clear (m); | |||
1908 | } | |||
1909 | else | |||
1910 | return NULL_TREE(tree) nullptr; | |||
1911 | ||||
1912 | if (HONOR_NANS (mode)) | |||
1913 | { | |||
1914 | t = fold_build2 (UNLE_EXPR, boolean_type_node, expr, min)fold_build2_loc (((location_t) 0), UNLE_EXPR, global_trees[TI_BOOLEAN_TYPE ], expr, min ); | |||
1915 | tt = fold_build2 (UNGE_EXPR, boolean_type_node, expr, max)fold_build2_loc (((location_t) 0), UNGE_EXPR, global_trees[TI_BOOLEAN_TYPE ], expr, max ); | |||
1916 | } | |||
1917 | else | |||
1918 | { | |||
1919 | t = fold_build2 (LE_EXPR, boolean_type_node, expr, min)fold_build2_loc (((location_t) 0), LE_EXPR, global_trees[TI_BOOLEAN_TYPE ], expr, min ); | |||
1920 | tt = fold_build2 (GE_EXPR, boolean_type_node, expr, max)fold_build2_loc (((location_t) 0), GE_EXPR, global_trees[TI_BOOLEAN_TYPE ], expr, max ); | |||
1921 | } | |||
1922 | t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, tt)fold_build2_loc (((location_t) 0), TRUTH_OR_EXPR, global_trees [TI_BOOLEAN_TYPE], t, tt ); | |||
1923 | if (integer_zerop (t)) | |||
1924 | return NULL_TREE(tree) nullptr; | |||
1925 | ||||
1926 | if (flag_sanitize_trapglobal_options.x_flag_sanitize_trap & SANITIZE_FLOAT_CAST) | |||
1927 | fn = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0); | |||
1928 | else | |||
1929 | { | |||
1930 | location_t *loc_ptr = NULLnullptr; | |||
1931 | unsigned num_locations = 0; | |||
1932 | /* Figure out if we can propagate location to ubsan_data and use new | |||
1933 | style handlers in libubsan. */ | |||
1934 | if (ubsan_use_new_style_p (loc)) | |||
1935 | { | |||
1936 | loc_ptr = &loc; | |||
1937 | num_locations = 1; | |||
1938 | } | |||
1939 | /* Create the __ubsan_handle_float_cast_overflow fn call. */ | |||
1940 | tree data = ubsan_create_data ("__ubsan_float_cast_overflow_data", | |||
1941 | num_locations, loc_ptr, | |||
1942 | ubsan_type_descriptor (expr_type), | |||
1943 | ubsan_type_descriptor (type), NULL_TREE(tree) nullptr, | |||
1944 | NULL_TREE(tree) nullptr); | |||
1945 | enum built_in_function bcode | |||
1946 | = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & SANITIZE_FLOAT_CAST) | |||
1947 | ? BUILT_IN_UBSAN_HANDLE_FLOAT_CAST_OVERFLOW | |||
1948 | : BUILT_IN_UBSAN_HANDLE_FLOAT_CAST_OVERFLOW_ABORT; | |||
1949 | fn = builtin_decl_explicit (bcode); | |||
1950 | fn = build_call_expr_loc (loc, fn, 2, | |||
1951 | build_fold_addr_expr_loc (loc, data), | |||
1952 | ubsan_encode_value (expr)); | |||
1953 | } | |||
1954 | ||||
1955 | return fold_build3 (COND_EXPR, void_type_node, t, fn, integer_zero_node)fold_build3_loc (((location_t) 0), COND_EXPR, global_trees[TI_VOID_TYPE ], t, fn, global_trees[TI_INTEGER_ZERO] ); | |||
1956 | } | |||
1957 | ||||
1958 | /* Instrument values passed to function arguments with nonnull attribute. */ | |||
1959 | ||||
1960 | static void | |||
1961 | instrument_nonnull_arg (gimple_stmt_iterator *gsi) | |||
1962 | { | |||
1963 | gimple *stmt = gsi_stmt (*gsi); | |||
1964 | location_t loc[2]; | |||
1965 | /* infer_nonnull_range needs flag_delete_null_pointer_checks set, | |||
1966 | while for nonnull sanitization it is clear. */ | |||
1967 | int save_flag_delete_null_pointer_checks = flag_delete_null_pointer_checksglobal_options.x_flag_delete_null_pointer_checks; | |||
1968 | flag_delete_null_pointer_checksglobal_options.x_flag_delete_null_pointer_checks = 1; | |||
1969 | loc[0] = gimple_location (stmt); | |||
1970 | loc[1] = UNKNOWN_LOCATION((location_t) 0); | |||
1971 | for (unsigned int i = 0; i < gimple_call_num_args (stmt); i++) | |||
1972 | { | |||
1973 | tree arg = gimple_call_arg (stmt, i); | |||
1974 | if (POINTER_TYPE_P (TREE_TYPE (arg))(((enum tree_code) (((contains_struct_check ((arg), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1974, __FUNCTION__))->typed.type))->base.code) == POINTER_TYPE || ((enum tree_code) (((contains_struct_check ((arg), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1974, __FUNCTION__))->typed.type))->base.code) == REFERENCE_TYPE ) | |||
1975 | && infer_nonnull_range_by_attribute (stmt, arg)) | |||
1976 | { | |||
1977 | gimple *g; | |||
1978 | if (!is_gimple_val (arg)) | |||
1979 | { | |||
1980 | g = gimple_build_assign (make_ssa_name (TREE_TYPE (arg)((contains_struct_check ((arg), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1980, __FUNCTION__))->typed.type)), arg); | |||
1981 | gimple_set_location (g, loc[0]); | |||
1982 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
1983 | arg = gimple_assign_lhs (g); | |||
1984 | } | |||
1985 | ||||
1986 | basic_block then_bb, fallthru_bb; | |||
1987 | *gsi = create_cond_insert_point (gsi, true, false, true, | |||
1988 | &then_bb, &fallthru_bb); | |||
1989 | g = gimple_build_cond (EQ_EXPR, arg, | |||
1990 | build_zero_cst (TREE_TYPE (arg)((contains_struct_check ((arg), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 1990, __FUNCTION__))->typed.type)), | |||
1991 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
1992 | gimple_set_location (g, loc[0]); | |||
1993 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |||
1994 | ||||
1995 | *gsi = gsi_after_labels (then_bb); | |||
1996 | if (flag_sanitize_trapglobal_options.x_flag_sanitize_trap & SANITIZE_NONNULL_ATTRIBUTE) | |||
1997 | g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0); | |||
1998 | else | |||
1999 | { | |||
2000 | tree data = ubsan_create_data ("__ubsan_nonnull_arg_data", | |||
2001 | 2, loc, NULL_TREE(tree) nullptr, | |||
2002 | build_int_cst (integer_type_nodeinteger_types[itk_int], | |||
2003 | i + 1), | |||
2004 | NULL_TREE(tree) nullptr); | |||
2005 | data = build_fold_addr_expr_loc (loc[0], data); | |||
2006 | enum built_in_function bcode | |||
2007 | = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & SANITIZE_NONNULL_ATTRIBUTE) | |||
2008 | ? BUILT_IN_UBSAN_HANDLE_NONNULL_ARG | |||
2009 | : BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT; | |||
2010 | tree fn = builtin_decl_explicit (bcode); | |||
2011 | ||||
2012 | g = gimple_build_call (fn, 1, data); | |||
2013 | } | |||
2014 | gimple_set_location (g, loc[0]); | |||
2015 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
2016 | ubsan_create_edge (g); | |||
2017 | } | |||
2018 | *gsi = gsi_for_stmt (stmt); | |||
2019 | } | |||
2020 | flag_delete_null_pointer_checksglobal_options.x_flag_delete_null_pointer_checks = save_flag_delete_null_pointer_checks; | |||
2021 | } | |||
2022 | ||||
2023 | /* Instrument returns in functions with returns_nonnull attribute. */ | |||
2024 | ||||
2025 | static void | |||
2026 | instrument_nonnull_return (gimple_stmt_iterator *gsi) | |||
2027 | { | |||
2028 | greturn *stmt = as_a <greturn *> (gsi_stmt (*gsi)); | |||
2029 | location_t loc[2]; | |||
2030 | tree arg = gimple_return_retval (stmt); | |||
2031 | /* infer_nonnull_range needs flag_delete_null_pointer_checks set, | |||
2032 | while for nonnull return sanitization it is clear. */ | |||
2033 | int save_flag_delete_null_pointer_checks = flag_delete_null_pointer_checksglobal_options.x_flag_delete_null_pointer_checks; | |||
2034 | flag_delete_null_pointer_checksglobal_options.x_flag_delete_null_pointer_checks = 1; | |||
2035 | loc[0] = gimple_location (stmt); | |||
2036 | loc[1] = UNKNOWN_LOCATION((location_t) 0); | |||
2037 | if (arg | |||
2038 | && POINTER_TYPE_P (TREE_TYPE (arg))(((enum tree_code) (((contains_struct_check ((arg), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2038, __FUNCTION__))->typed.type))->base.code) == POINTER_TYPE || ((enum tree_code) (((contains_struct_check ((arg), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2038, __FUNCTION__))->typed.type))->base.code) == REFERENCE_TYPE ) | |||
2039 | && is_gimple_val (arg) | |||
2040 | && infer_nonnull_range_by_attribute (stmt, arg)) | |||
2041 | { | |||
2042 | basic_block then_bb, fallthru_bb; | |||
2043 | *gsi = create_cond_insert_point (gsi, true, false, true, | |||
2044 | &then_bb, &fallthru_bb); | |||
2045 | gimple *g = gimple_build_cond (EQ_EXPR, arg, | |||
2046 | build_zero_cst (TREE_TYPE (arg)((contains_struct_check ((arg), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2046, __FUNCTION__))->typed.type)), | |||
2047 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
2048 | gimple_set_location (g, loc[0]); | |||
2049 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |||
2050 | ||||
2051 | *gsi = gsi_after_labels (then_bb); | |||
2052 | if (flag_sanitize_trapglobal_options.x_flag_sanitize_trap & SANITIZE_RETURNS_NONNULL_ATTRIBUTE) | |||
2053 | g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0); | |||
2054 | else | |||
2055 | { | |||
2056 | tree data = ubsan_create_data ("__ubsan_nonnull_return_data", | |||
2057 | 1, &loc[1], NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
2058 | data = build_fold_addr_expr_loc (loc[0], data); | |||
2059 | tree data2 = ubsan_create_data ("__ubsan_nonnull_return_data", | |||
2060 | 1, &loc[0], NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
2061 | data2 = build_fold_addr_expr_loc (loc[0], data2); | |||
2062 | enum built_in_function bcode | |||
2063 | = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & SANITIZE_RETURNS_NONNULL_ATTRIBUTE) | |||
2064 | ? BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1 | |||
2065 | : BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT; | |||
2066 | tree fn = builtin_decl_explicit (bcode); | |||
2067 | ||||
2068 | g = gimple_build_call (fn, 2, data, data2); | |||
2069 | } | |||
2070 | gimple_set_location (g, loc[0]); | |||
2071 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
2072 | ubsan_create_edge (g); | |||
2073 | *gsi = gsi_for_stmt (stmt); | |||
2074 | } | |||
2075 | flag_delete_null_pointer_checksglobal_options.x_flag_delete_null_pointer_checks = save_flag_delete_null_pointer_checks; | |||
2076 | } | |||
2077 | ||||
2078 | /* Instrument memory references. Here we check whether the pointer | |||
2079 | points to an out-of-bounds location. */ | |||
2080 | ||||
2081 | static void | |||
2082 | instrument_object_size (gimple_stmt_iterator *gsi, tree t, bool is_lhs) | |||
2083 | { | |||
2084 | gimple *stmt = gsi_stmt (*gsi); | |||
2085 | location_t loc = gimple_location (stmt); | |||
2086 | tree type; | |||
2087 | tree index = NULL_TREE(tree) nullptr; | |||
2088 | HOST_WIDE_INTlong size_in_bytes; | |||
2089 | ||||
2090 | type = TREE_TYPE (t)((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2090, __FUNCTION__))->typed.type); | |||
2091 | if (VOID_TYPE_P (type)(((enum tree_code) (type)->base.code) == VOID_TYPE)) | |||
2092 | return; | |||
2093 | ||||
2094 | switch (TREE_CODE (t)((enum tree_code) (t)->base.code)) | |||
2095 | { | |||
2096 | case COMPONENT_REF: | |||
2097 | if (TREE_CODE (t)((enum tree_code) (t)->base.code) == COMPONENT_REF | |||
2098 | && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1))((tree_check (((*((const_cast<tree*> (tree_operand_check ((t), (1), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2098, __FUNCTION__)))))), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2098, __FUNCTION__, (FIELD_DECL)))->field_decl.qualifier ) != NULL_TREE(tree) nullptr) | |||
2099 | { | |||
2100 | tree repr = DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1))((tree_check (((*((const_cast<tree*> (tree_operand_check ((t), (1), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2100, __FUNCTION__)))))), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2100, __FUNCTION__, (FIELD_DECL)))->field_decl.qualifier ); | |||
2101 | t = build3 (COMPONENT_REF, TREE_TYPE (repr)((contains_struct_check ((repr), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2101, __FUNCTION__))->typed.type), TREE_OPERAND (t, 0)(*((const_cast<tree*> (tree_operand_check ((t), (0), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2101, __FUNCTION__))))), | |||
2102 | repr, TREE_OPERAND (t, 2)(*((const_cast<tree*> (tree_operand_check ((t), (2), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2102, __FUNCTION__)))))); | |||
2103 | } | |||
2104 | break; | |||
2105 | case ARRAY_REF: | |||
2106 | index = TREE_OPERAND (t, 1)(*((const_cast<tree*> (tree_operand_check ((t), (1), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2106, __FUNCTION__))))); | |||
2107 | break; | |||
2108 | case INDIRECT_REF: | |||
2109 | case MEM_REF: | |||
2110 | case VAR_DECL: | |||
2111 | case PARM_DECL: | |||
2112 | case RESULT_DECL: | |||
2113 | break; | |||
2114 | default: | |||
2115 | return; | |||
2116 | } | |||
2117 | ||||
2118 | size_in_bytes = int_size_in_bytes (type); | |||
2119 | if (size_in_bytes <= 0) | |||
2120 | return; | |||
2121 | ||||
2122 | poly_int64 bitsize, bitpos; | |||
2123 | tree offset; | |||
2124 | machine_mode mode; | |||
2125 | int volatilep = 0, reversep, unsignedp = 0; | |||
2126 | tree inner = get_inner_reference (t, &bitsize, &bitpos, &offset, &mode, | |||
2127 | &unsignedp, &reversep, &volatilep); | |||
2128 | ||||
2129 | if (!multiple_p (bitpos, BITS_PER_UNIT(8)) | |||
2130 | || maybe_ne (bitsize, size_in_bytes * BITS_PER_UNIT(8))) | |||
2131 | return; | |||
2132 | ||||
2133 | bool decl_p = DECL_P (inner)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (inner)->base.code))] == tcc_declaration); | |||
2134 | tree base; | |||
2135 | if (decl_p) | |||
2136 | { | |||
2137 | if ((VAR_P (inner)(((enum tree_code) (inner)->base.code) == VAR_DECL) | |||
2138 | || TREE_CODE (inner)((enum tree_code) (inner)->base.code) == PARM_DECL | |||
2139 | || TREE_CODE (inner)((enum tree_code) (inner)->base.code) == RESULT_DECL) | |||
2140 | && DECL_REGISTER (inner)((contains_struct_check ((inner), (TS_DECL_WRTL), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2140, __FUNCTION__))->decl_common.decl_flag_0)) | |||
2141 | return; | |||
2142 | if (t == inner && !is_global_var (t)) | |||
2143 | return; | |||
2144 | base = inner; | |||
2145 | } | |||
2146 | else if (TREE_CODE (inner)((enum tree_code) (inner)->base.code) == MEM_REF) | |||
2147 | base = TREE_OPERAND (inner, 0)(*((const_cast<tree*> (tree_operand_check ((inner), (0) , "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2147, __FUNCTION__))))); | |||
2148 | else | |||
2149 | return; | |||
2150 | tree ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)((contains_struct_check ((t), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2150, __FUNCTION__))->typed.type)), t); | |||
2151 | ||||
2152 | while (TREE_CODE (base)((enum tree_code) (base)->base.code) == SSA_NAME) | |||
2153 | { | |||
2154 | gimple *def_stmt = SSA_NAME_DEF_STMT (base)(tree_check ((base), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2154, __FUNCTION__, (SSA_NAME)))->ssa_name.def_stmt; | |||
2155 | if (gimple_assign_ssa_name_copy_p (def_stmt) | |||
2156 | || (gimple_assign_cast_p (def_stmt) | |||
2157 | && POINTER_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))(((enum tree_code) (((contains_struct_check ((gimple_assign_rhs1 (def_stmt)), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2157, __FUNCTION__))->typed.type))->base.code) == POINTER_TYPE || ((enum tree_code) (((contains_struct_check ((gimple_assign_rhs1 (def_stmt)), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2157, __FUNCTION__))->typed.type))->base.code) == REFERENCE_TYPE )) | |||
2158 | || (is_gimple_assign (def_stmt) | |||
2159 | && gimple_assign_rhs_code (def_stmt) == POINTER_PLUS_EXPR)) | |||
2160 | { | |||
2161 | tree rhs1 = gimple_assign_rhs1 (def_stmt); | |||
2162 | if (TREE_CODE (rhs1)((enum tree_code) (rhs1)->base.code) == SSA_NAME | |||
2163 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)(tree_check ((rhs1), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2163, __FUNCTION__, (SSA_NAME)))->base.asm_written_flag) | |||
2164 | break; | |||
2165 | else | |||
2166 | base = rhs1; | |||
2167 | } | |||
2168 | else | |||
2169 | break; | |||
2170 | } | |||
2171 | ||||
2172 | if (!POINTER_TYPE_P (TREE_TYPE (base))(((enum tree_code) (((contains_struct_check ((base), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2172, __FUNCTION__))->typed.type))->base.code) == POINTER_TYPE || ((enum tree_code) (((contains_struct_check ((base), (TS_TYPED ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2172, __FUNCTION__))->typed.type))->base.code) == REFERENCE_TYPE ) && !DECL_P (base)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (base)->base.code))] == tcc_declaration)) | |||
2173 | return; | |||
2174 | ||||
2175 | tree sizet; | |||
2176 | tree base_addr = base; | |||
2177 | gimple *bos_stmt = NULLnullptr; | |||
2178 | if (decl_p) | |||
2179 | base_addr = build1 (ADDR_EXPR, | |||
2180 | build_pointer_type (TREE_TYPE (base)((contains_struct_check ((base), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2180, __FUNCTION__))->typed.type)), base); | |||
2181 | if (compute_builtin_object_size (base_addr, OST_DYNAMIC, &sizet)) | |||
2182 | ; | |||
2183 | else if (optimizeglobal_options.x_optimize) | |||
2184 | { | |||
2185 | if (LOCATION_LOCUS (loc)((IS_ADHOC_LOC (loc)) ? get_location_from_adhoc_loc (line_table , loc) : (loc)) == UNKNOWN_LOCATION((location_t) 0)) | |||
2186 | loc = input_location; | |||
2187 | /* Generate __builtin_dynamic_object_size call. */ | |||
2188 | sizet = builtin_decl_explicit (BUILT_IN_DYNAMIC_OBJECT_SIZE); | |||
2189 | sizet = build_call_expr_loc (loc, sizet, 2, base_addr, | |||
2190 | integer_zero_nodeglobal_trees[TI_INTEGER_ZERO]); | |||
2191 | sizet = force_gimple_operand_gsi (gsi, sizet, false, NULL_TREE(tree) nullptr, true, | |||
2192 | GSI_SAME_STMT); | |||
2193 | /* If the call above didn't end up being an integer constant, go one | |||
2194 | statement back and get the __builtin_object_size stmt. Save it, | |||
2195 | we might need it later. */ | |||
2196 | if (SSA_VAR_P (sizet)(((enum tree_code) (sizet)->base.code) == VAR_DECL || ((enum tree_code) (sizet)->base.code) == PARM_DECL || ((enum tree_code ) (sizet)->base.code) == RESULT_DECL || ((enum tree_code) ( sizet)->base.code) == SSA_NAME)) | |||
2197 | { | |||
2198 | gsi_prev (gsi); | |||
2199 | bos_stmt = gsi_stmt (*gsi); | |||
2200 | ||||
2201 | /* Move on to where we were. */ | |||
2202 | gsi_next (gsi); | |||
2203 | } | |||
2204 | } | |||
2205 | else | |||
2206 | return; | |||
2207 | ||||
2208 | /* Generate UBSAN_OBJECT_SIZE (ptr, ptr+sizeof(*ptr)-base, objsize, ckind) | |||
2209 | call. */ | |||
2210 | /* ptr + sizeof (*ptr) - base */ | |||
2211 | t = fold_build2 (MINUS_EXPR, sizetype,fold_build2_loc (((location_t) 0), MINUS_EXPR, sizetype_tab[( int) stk_sizetype], fold_convert_loc (((location_t) 0), global_trees [TI_POINTER_SIZED_TYPE], ptr), fold_convert_loc (((location_t ) 0), global_trees[TI_POINTER_SIZED_TYPE], base_addr) ) | |||
2212 | fold_convert (pointer_sized_int_node, ptr),fold_build2_loc (((location_t) 0), MINUS_EXPR, sizetype_tab[( int) stk_sizetype], fold_convert_loc (((location_t) 0), global_trees [TI_POINTER_SIZED_TYPE], ptr), fold_convert_loc (((location_t ) 0), global_trees[TI_POINTER_SIZED_TYPE], base_addr) ) | |||
2213 | fold_convert (pointer_sized_int_node, base_addr))fold_build2_loc (((location_t) 0), MINUS_EXPR, sizetype_tab[( int) stk_sizetype], fold_convert_loc (((location_t) 0), global_trees [TI_POINTER_SIZED_TYPE], ptr), fold_convert_loc (((location_t ) 0), global_trees[TI_POINTER_SIZED_TYPE], base_addr) ); | |||
2214 | t = fold_build2 (PLUS_EXPR, sizetype, t, TYPE_SIZE_UNIT (type))fold_build2_loc (((location_t) 0), PLUS_EXPR, sizetype_tab[(int ) stk_sizetype], t, ((tree_class_check ((type), (tcc_type), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2214, __FUNCTION__))->type_common.size_unit) ); | |||
2215 | ||||
2216 | /* Perhaps we can omit the check. */ | |||
2217 | if (TREE_CODE (t)((enum tree_code) (t)->base.code) == INTEGER_CST | |||
2218 | && TREE_CODE (sizet)((enum tree_code) (sizet)->base.code) == INTEGER_CST | |||
2219 | && tree_int_cst_le (t, sizet)) | |||
2220 | return; | |||
2221 | ||||
2222 | if (index != NULL_TREE(tree) nullptr | |||
2223 | && TREE_CODE (index)((enum tree_code) (index)->base.code) == SSA_NAME | |||
2224 | && TREE_CODE (sizet)((enum tree_code) (sizet)->base.code) == INTEGER_CST) | |||
2225 | { | |||
2226 | gimple *def = SSA_NAME_DEF_STMT (index)(tree_check ((index), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2226, __FUNCTION__, (SSA_NAME)))->ssa_name.def_stmt; | |||
2227 | if (is_gimple_assign (def) | |||
2228 | && gimple_assign_rhs_code (def) == BIT_AND_EXPR | |||
2229 | && TREE_CODE (gimple_assign_rhs2 (def))((enum tree_code) (gimple_assign_rhs2 (def))->base.code) == INTEGER_CST) | |||
2230 | { | |||
2231 | tree cst = gimple_assign_rhs2 (def); | |||
2232 | tree sz = fold_build2 (EXACT_DIV_EXPR, sizetype, sizet,fold_build2_loc (((location_t) 0), EXACT_DIV_EXPR, sizetype_tab [(int) stk_sizetype], sizet, ((tree_class_check ((type), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2233, __FUNCTION__))->type_common.size_unit) ) | |||
2233 | TYPE_SIZE_UNIT (type))fold_build2_loc (((location_t) 0), EXACT_DIV_EXPR, sizetype_tab [(int) stk_sizetype], sizet, ((tree_class_check ((type), (tcc_type ), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2233, __FUNCTION__))->type_common.size_unit) ); | |||
2234 | if (tree_int_cst_sgn (cst) >= 0 | |||
2235 | && tree_int_cst_lt (cst, sz)) | |||
2236 | return; | |||
2237 | } | |||
2238 | } | |||
2239 | ||||
2240 | if (DECL_P (base)(tree_code_type_tmpl <0>::tree_code_type[(int) (((enum tree_code ) (base)->base.code))] == tcc_declaration) | |||
2241 | && decl_function_context (base) == current_function_decl | |||
2242 | && !TREE_ADDRESSABLE (base)((base)->base.addressable_flag)) | |||
2243 | mark_addressable (base); | |||
2244 | ||||
2245 | if (bos_stmt | |||
2246 | && gimple_call_builtin_p (bos_stmt, BUILT_IN_DYNAMIC_OBJECT_SIZE)) | |||
2247 | ubsan_create_edge (bos_stmt); | |||
2248 | ||||
2249 | /* We have to emit the check. */ | |||
2250 | t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE(tree) nullptr, true, | |||
2251 | GSI_SAME_STMT); | |||
2252 | ptr = force_gimple_operand_gsi (gsi, ptr, true, NULL_TREE(tree) nullptr, true, | |||
2253 | GSI_SAME_STMT); | |||
2254 | tree ckind = build_int_cst (unsigned_char_type_nodeinteger_types[itk_unsigned_char], | |||
2255 | is_lhs ? UBSAN_STORE_OF : UBSAN_LOAD_OF); | |||
2256 | gimple *g = gimple_build_call_internal (IFN_UBSAN_OBJECT_SIZE, 4, | |||
2257 | ptr, t, sizet, ckind); | |||
2258 | gimple_set_location (g, loc); | |||
2259 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
2260 | } | |||
2261 | ||||
2262 | /* Instrument values passed to builtin functions. */ | |||
2263 | ||||
2264 | static void | |||
2265 | instrument_builtin (gimple_stmt_iterator *gsi) | |||
2266 | { | |||
2267 | gimple *stmt = gsi_stmt (*gsi); | |||
2268 | location_t loc = gimple_location (stmt); | |||
2269 | tree arg; | |||
2270 | enum built_in_function fcode | |||
2271 | = DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)); | |||
2272 | int kind = 0; | |||
2273 | switch (fcode) | |||
2274 | { | |||
2275 | CASE_INT_FN (BUILT_IN_CLZ)case BUILT_IN_CLZ: case BUILT_IN_CLZL: case BUILT_IN_CLZLL: case BUILT_IN_CLZIMAX: | |||
2276 | kind = 1; | |||
2277 | gcc_fallthrough (); | |||
2278 | CASE_INT_FN (BUILT_IN_CTZ)case BUILT_IN_CTZ: case BUILT_IN_CTZL: case BUILT_IN_CTZLL: case BUILT_IN_CTZIMAX: | |||
2279 | arg = gimple_call_arg (stmt, 0); | |||
2280 | if (!integer_nonzerop (arg)) | |||
2281 | { | |||
2282 | gimple *g; | |||
2283 | if (!is_gimple_val (arg)) | |||
2284 | { | |||
2285 | g = gimple_build_assign (make_ssa_name (TREE_TYPE (arg)((contains_struct_check ((arg), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2285, __FUNCTION__))->typed.type)), arg); | |||
2286 | gimple_set_location (g, loc); | |||
2287 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
2288 | arg = gimple_assign_lhs (g); | |||
2289 | } | |||
2290 | ||||
2291 | basic_block then_bb, fallthru_bb; | |||
2292 | *gsi = create_cond_insert_point (gsi, true, false, true, | |||
2293 | &then_bb, &fallthru_bb); | |||
2294 | g = gimple_build_cond (EQ_EXPR, arg, | |||
2295 | build_zero_cst (TREE_TYPE (arg)((contains_struct_check ((arg), (TS_TYPED), "/buildworker/marxinbox-gcc-clang-static-analyzer/build/gcc/ubsan.cc" , 2295, __FUNCTION__))->typed.type)), | |||
2296 | NULL_TREE(tree) nullptr, NULL_TREE(tree) nullptr); | |||
2297 | gimple_set_location (g, loc); | |||
2298 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |||
2299 | ||||
2300 | *gsi = gsi_after_labels (then_bb); | |||
2301 | if (flag_sanitize_trapglobal_options.x_flag_sanitize_trap & SANITIZE_BUILTIN) | |||
2302 | g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0); | |||
2303 | else | |||
2304 | { | |||
2305 | tree t = build_int_cst (unsigned_char_type_nodeinteger_types[itk_unsigned_char], kind); | |||
2306 | tree data = ubsan_create_data ("__ubsan_builtin_data", | |||
2307 | 1, &loc, NULL_TREE(tree) nullptr, t, NULL_TREE(tree) nullptr); | |||
2308 | data = build_fold_addr_expr_loc (loc, data); | |||
2309 | enum built_in_function bcode | |||
2310 | = (flag_sanitize_recoverglobal_options.x_flag_sanitize_recover & SANITIZE_BUILTIN) | |||
2311 | ? BUILT_IN_UBSAN_HANDLE_INVALID_BUILTIN | |||
2312 | : BUILT_IN_UBSAN_HANDLE_INVALID_BUILTIN_ABORT; | |||
2313 | tree fn = builtin_decl_explicit (bcode); | |||
2314 | ||||
2315 | g = gimple_build_call (fn, 1, data); | |||
2316 | } | |||
2317 | gimple_set_location (g, loc); | |||
2318 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |||
2319 | ubsan_create_edge (g); | |||
2320 | } | |||
2321 | *gsi = gsi_for_stmt (stmt); | |||
2322 | break; | |||
2323 | default: | |||
2324 | break; | |||
2325 | } | |||
2326 | } | |||
2327 | ||||
2328 | namespace { | |||
2329 | ||||
2330 | const pass_data pass_data_ubsan = | |||
2331 | { | |||
2332 | GIMPLE_PASS, /* type */ | |||
2333 | "ubsan", /* name */ | |||
2334 | OPTGROUP_NONE, /* optinfo_flags */ | |||
2335 | TV_TREE_UBSAN, /* tv_id */ | |||
2336 | ( PROP_cfg(1 << 3) | PROP_ssa(1 << 5) ), /* properties_required */ | |||
2337 | 0, /* properties_provided */ | |||
2338 | 0, /* properties_destroyed */ | |||
2339 | 0, /* todo_flags_start */ | |||
2340 | TODO_update_ssa(1 << 11), /* todo_flags_finish */ | |||
2341 | }; | |||
2342 | ||||
2343 | class pass_ubsan : public gimple_opt_pass | |||
2344 | { | |||
2345 | public: | |||
2346 | pass_ubsan (gcc::context *ctxt) | |||
2347 | : gimple_opt_pass (pass_data_ubsan, ctxt) | |||
2348 | {} | |||
2349 | ||||
2350 | /* opt_pass methods: */ | |||
2351 | bool gate (function *) final override | |||
2352 | { | |||
2353 | return sanitize_flags_p ((SANITIZE_NULL | SANITIZE_SI_OVERFLOW | |||
2354 | | SANITIZE_BOOL | SANITIZE_ENUM | |||
2355 | | SANITIZE_ALIGNMENT | |||
2356 | | SANITIZE_NONNULL_ATTRIBUTE | |||
2357 | | SANITIZE_RETURNS_NONNULL_ATTRIBUTE | |||
2358 | | SANITIZE_OBJECT_SIZE | |||
2359 | | SANITIZE_POINTER_OVERFLOW | |||
2360 | | SANITIZE_BUILTIN)); | |||
2361 | } | |||
2362 | ||||
2363 | unsigned int execute (function *) final override; | |||
2364 | ||||
2365 | }; // class pass_ubsan | |||
2366 | ||||
2367 | unsigned int | |||
2368 | pass_ubsan::execute (function *fun) | |||
2369 | { | |||
2370 | basic_block bb; | |||
2371 | gimple_stmt_iterator gsi; | |||
2372 | unsigned int ret = 0; | |||
2373 | ||||
2374 | initialize_sanitizer_builtins (); | |||
2375 | ||||
2376 | FOR_EACH_BB_FN (bb, fun)for (bb = (fun)->cfg->x_entry_block_ptr->next_bb; bb != (fun)->cfg->x_exit_block_ptr; bb = bb->next_bb) | |||
2377 | { | |||
2378 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);) | |||
2379 | { | |||
2380 | gimple *stmt = gsi_stmt (gsi); | |||
2381 | if (is_gimple_debug (stmt) || gimple_clobber_p (stmt)) | |||
2382 | { | |||
2383 | gsi_next (&gsi); | |||
2384 | continue; | |||
2385 | } | |||
2386 | ||||
2387 | if ((sanitize_flags_p (SANITIZE_SI_OVERFLOW, fun->decl)) | |||
2388 | && is_gimple_assign (stmt)) | |||
2389 | instrument_si_overflow (gsi); | |||
2390 | ||||
2391 | if (sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT, fun->decl)) | |||
2392 | { | |||
2393 | if (gimple_store_p (stmt)) | |||
2394 | instrument_null (gsi, gimple_get_lhs (stmt), true); | |||
2395 | if (gimple_assign_single_p (stmt)) | |||
2396 | instrument_null (gsi, gimple_assign_rhs1 (stmt), false); | |||
2397 | if (is_gimple_call (stmt)) | |||
2398 | { | |||
2399 | unsigned args_num = gimple_call_num_args (stmt); | |||
2400 | for (unsigned i = 0; i < args_num; ++i) | |||
2401 | { | |||
2402 | tree arg = gimple_call_arg (stmt, i); | |||
2403 | if (is_gimple_reg (arg) || is_gimple_min_invariant (arg)) | |||
2404 | continue; | |||
2405 | instrument_null (gsi, arg, false); | |||
2406 | } | |||
2407 | } | |||
2408 | } | |||
2409 | ||||
2410 | if (sanitize_flags_p (SANITIZE_BOOL | SANITIZE_ENUM, fun->decl) | |||
2411 | && gimple_assign_load_p (stmt)) | |||
2412 | { | |||
2413 | instrument_bool_enum_load (&gsi); | |||
2414 | bb = gimple_bb (stmt); | |||
2415 | } | |||
2416 | ||||
2417 | if (sanitize_flags_p (SANITIZE_NONNULL_ATTRIBUTE, fun->decl) | |||
2418 | && is_gimple_call (stmt) | |||
2419 | && !gimple_call_internal_p (stmt)) | |||
2420 | { | |||
2421 | instrument_nonnull_arg (&gsi); | |||
2422 | bb = gimple_bb (stmt); | |||
2423 | } | |||
2424 | ||||
2425 | if (sanitize_flags_p (SANITIZE_BUILTIN, fun->decl) | |||
2426 | && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)) | |||
2427 | { | |||
2428 | instrument_builtin (&gsi); | |||
2429 | bb = gimple_bb (stmt); | |||
2430 | } | |||
2431 | ||||
2432 | if (sanitize_flags_p (SANITIZE_RETURNS_NONNULL_ATTRIBUTE, fun->decl) | |||
2433 | && gimple_code (stmt) == GIMPLE_RETURN) | |||
2434 | { | |||
2435 | instrument_nonnull_return (&gsi); | |||
2436 | bb = gimple_bb (stmt); | |||
2437 | } | |||
2438 | ||||
2439 | if (sanitize_flags_p (SANITIZE_OBJECT_SIZE, fun->decl)) | |||
2440 | { | |||
2441 | if (gimple_store_p (stmt)) | |||
2442 | instrument_object_size (&gsi, gimple_get_lhs (stmt), true); | |||
2443 | if (gimple_assign_load_p (stmt)) | |||
2444 | instrument_object_size (&gsi, gimple_assign_rhs1 (stmt), | |||
2445 | false); | |||
2446 | if (is_gimple_call (stmt)) | |||
2447 | { | |||
2448 | unsigned args_num = gimple_call_num_args (stmt); | |||
2449 | for (unsigned i = 0; i < args_num; ++i) | |||
2450 | { | |||
2451 | tree arg = gimple_call_arg (stmt, i); | |||
2452 | if (is_gimple_reg (arg) || is_gimple_min_invariant (arg)) | |||
2453 | continue; | |||
2454 | instrument_object_size (&gsi, arg, false); | |||
2455 | } | |||
2456 | } | |||
2457 | } | |||
2458 | ||||
2459 | if (sanitize_flags_p (SANITIZE_POINTER_OVERFLOW, fun->decl)) | |||
2460 | { | |||
2461 | if (is_gimple_assign (stmt) | |||
2462 | && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR) | |||
2463 | instrument_pointer_overflow (&gsi, | |||
2464 | gimple_assign_rhs1 (stmt), | |||
2465 | gimple_assign_rhs2 (stmt)); | |||
2466 | if (gimple_store_p (stmt)) | |||
2467 | maybe_instrument_pointer_overflow (&gsi, | |||
2468 | gimple_get_lhs (stmt)); | |||
2469 | if (gimple_assign_single_p (stmt)) | |||
2470 | maybe_instrument_pointer_overflow (&gsi, | |||
2471 | gimple_assign_rhs1 (stmt)); | |||
2472 | if (is_gimple_call (stmt)) | |||
2473 | { | |||
2474 | unsigned args_num = gimple_call_num_args (stmt); | |||
2475 | for (unsigned i = 0; i < args_num; ++i) | |||
2476 | { | |||
2477 | tree arg = gimple_call_arg (stmt, i); | |||
2478 | if (is_gimple_reg (arg)) | |||
2479 | continue; | |||
2480 | maybe_instrument_pointer_overflow (&gsi, arg); | |||
2481 | } | |||
2482 | } | |||
2483 | } | |||
2484 | ||||
2485 | gsi_next (&gsi); | |||
2486 | } | |||
2487 | if (gimple_purge_dead_eh_edges (bb)) | |||
2488 | ret = TODO_cleanup_cfg(1 << 5); | |||
2489 | } | |||
2490 | return ret; | |||
2491 | } | |||
2492 | ||||
2493 | } // anon namespace | |||
2494 | ||||
2495 | gimple_opt_pass * | |||
2496 | make_pass_ubsan (gcc::context *ctxt) | |||
2497 | { | |||
2498 | return new pass_ubsan (ctxt); | |||
2499 | } | |||
2500 | ||||
2501 | #include "gt-ubsan.h" |