sched: Do not mix prologue and epilogue insns
[official-gcc.git] / gcc / ubsan.c
blob6594dd171837c9e8d724bd02ae8c933110e24b57
1 /* UndefinedBehaviorSanitizer, undefined behavior detector.
2 Copyright (C) 2013-2016 Free Software Foundation, Inc.
3 Contributed by Marek Polacek <polacek@redhat.com>
5 This file is part of GCC.
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.
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.
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/>. */
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 "asan.h"
43 #include "gimplify-me.h"
44 #include "dfp.h"
45 #include "builtins.h"
46 #include "tree-object-size.h"
47 #include "tree-cfg.h"
49 /* Map from a tree to a VAR_DECL tree. */
51 struct GTY((for_user)) tree_type_map {
52 struct tree_map_base type;
53 tree decl;
56 struct tree_type_map_cache_hasher : ggc_cache_ptr_hash<tree_type_map>
58 static inline hashval_t
59 hash (tree_type_map *t)
61 return TYPE_UID (t->type.from);
64 static inline bool
65 equal (tree_type_map *a, tree_type_map *b)
67 return a->type.from == b->type.from;
70 static int
71 keep_cache_entry (tree_type_map *&m)
73 return ggc_marked_p (m->type.from);
77 static GTY ((cache))
78 hash_table<tree_type_map_cache_hasher> *decl_tree_for_type;
80 /* Lookup a VAR_DECL for TYPE, and return it if we find one. */
82 static tree
83 decl_for_type_lookup (tree type)
85 /* If the hash table is not initialized yet, create it now. */
86 if (decl_tree_for_type == NULL)
88 decl_tree_for_type
89 = hash_table<tree_type_map_cache_hasher>::create_ggc (10);
90 /* That also means we don't have to bother with the lookup. */
91 return NULL_TREE;
94 struct tree_type_map *h, in;
95 in.type.from = type;
97 h = decl_tree_for_type->find_with_hash (&in, TYPE_UID (type));
98 return h ? h->decl : NULL_TREE;
101 /* Insert a mapping TYPE->DECL in the VAR_DECL for type hashtable. */
103 static void
104 decl_for_type_insert (tree type, tree decl)
106 struct tree_type_map *h;
108 h = ggc_alloc<tree_type_map> ();
109 h->type.from = type;
110 h->decl = decl;
111 *decl_tree_for_type->find_slot_with_hash (h, TYPE_UID (type), INSERT) = h;
114 /* Helper routine, which encodes a value in the pointer_sized_int_node.
115 Arguments with precision <= POINTER_SIZE are passed directly,
116 the rest is passed by reference. T is a value we are to encode.
117 IN_EXPAND_P is true if this function is called during expansion. */
119 tree
120 ubsan_encode_value (tree t, bool in_expand_p)
122 tree type = TREE_TYPE (t);
123 const unsigned int bitsize = GET_MODE_BITSIZE (TYPE_MODE (type));
124 if (bitsize <= POINTER_SIZE)
125 switch (TREE_CODE (type))
127 case BOOLEAN_TYPE:
128 case ENUMERAL_TYPE:
129 case INTEGER_TYPE:
130 return fold_build1 (NOP_EXPR, pointer_sized_int_node, t);
131 case REAL_TYPE:
133 tree itype = build_nonstandard_integer_type (bitsize, true);
134 t = fold_build1 (VIEW_CONVERT_EXPR, itype, t);
135 return fold_convert (pointer_sized_int_node, t);
137 default:
138 gcc_unreachable ();
140 else
142 if (!DECL_P (t) || !TREE_ADDRESSABLE (t))
144 /* The reason for this is that we don't want to pessimize
145 code by making vars unnecessarily addressable. */
146 tree var = create_tmp_var (type);
147 tree tem = build2 (MODIFY_EXPR, void_type_node, var, t);
148 if (in_expand_p)
150 rtx mem
151 = assign_stack_temp_for_type (TYPE_MODE (type),
152 GET_MODE_SIZE (TYPE_MODE (type)),
153 type);
154 SET_DECL_RTL (var, mem);
155 expand_assignment (var, t, false);
156 return build_fold_addr_expr (var);
158 t = build_fold_addr_expr (var);
159 return build2 (COMPOUND_EXPR, TREE_TYPE (t), tem, t);
161 else
162 return build_fold_addr_expr (t);
166 /* Cached ubsan_get_type_descriptor_type () return value. */
167 static GTY(()) tree ubsan_type_descriptor_type;
169 /* Build
170 struct __ubsan_type_descriptor
172 unsigned short __typekind;
173 unsigned short __typeinfo;
174 char __typename[];
176 type. */
178 static tree
179 ubsan_get_type_descriptor_type (void)
181 static const char *field_names[3]
182 = { "__typekind", "__typeinfo", "__typename" };
183 tree fields[3], ret;
185 if (ubsan_type_descriptor_type)
186 return ubsan_type_descriptor_type;
188 tree itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
189 tree flex_arr_type = build_array_type (char_type_node, itype);
191 ret = make_node (RECORD_TYPE);
192 for (int i = 0; i < 3; i++)
194 fields[i] = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
195 get_identifier (field_names[i]),
196 (i == 2) ? flex_arr_type
197 : short_unsigned_type_node);
198 DECL_CONTEXT (fields[i]) = ret;
199 if (i)
200 DECL_CHAIN (fields[i - 1]) = fields[i];
202 tree type_decl = build_decl (input_location, TYPE_DECL,
203 get_identifier ("__ubsan_type_descriptor"),
204 ret);
205 DECL_IGNORED_P (type_decl) = 1;
206 DECL_ARTIFICIAL (type_decl) = 1;
207 TYPE_FIELDS (ret) = fields[0];
208 TYPE_NAME (ret) = type_decl;
209 TYPE_STUB_DECL (ret) = type_decl;
210 layout_type (ret);
211 ubsan_type_descriptor_type = ret;
212 return ret;
215 /* Cached ubsan_get_source_location_type () return value. */
216 static GTY(()) tree ubsan_source_location_type;
218 /* Build
219 struct __ubsan_source_location
221 const char *__filename;
222 unsigned int __line;
223 unsigned int __column;
225 type. */
227 tree
228 ubsan_get_source_location_type (void)
230 static const char *field_names[3]
231 = { "__filename", "__line", "__column" };
232 tree fields[3], ret;
233 if (ubsan_source_location_type)
234 return ubsan_source_location_type;
236 tree const_char_type = build_qualified_type (char_type_node,
237 TYPE_QUAL_CONST);
239 ret = make_node (RECORD_TYPE);
240 for (int i = 0; i < 3; i++)
242 fields[i] = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
243 get_identifier (field_names[i]),
244 (i == 0) ? build_pointer_type (const_char_type)
245 : unsigned_type_node);
246 DECL_CONTEXT (fields[i]) = ret;
247 if (i)
248 DECL_CHAIN (fields[i - 1]) = fields[i];
250 tree type_decl = build_decl (input_location, TYPE_DECL,
251 get_identifier ("__ubsan_source_location"),
252 ret);
253 DECL_IGNORED_P (type_decl) = 1;
254 DECL_ARTIFICIAL (type_decl) = 1;
255 TYPE_FIELDS (ret) = fields[0];
256 TYPE_NAME (ret) = type_decl;
257 TYPE_STUB_DECL (ret) = type_decl;
258 layout_type (ret);
259 ubsan_source_location_type = ret;
260 return ret;
263 /* Helper routine that returns a CONSTRUCTOR of __ubsan_source_location
264 type with its fields filled from a location_t LOC. */
266 static tree
267 ubsan_source_location (location_t loc)
269 expanded_location xloc;
270 tree type = ubsan_get_source_location_type ();
272 xloc = expand_location (loc);
273 tree str;
274 if (xloc.file == NULL)
276 str = build_int_cst (ptr_type_node, 0);
277 xloc.line = 0;
278 xloc.column = 0;
280 else
282 /* Fill in the values from LOC. */
283 size_t len = strlen (xloc.file) + 1;
284 str = build_string (len, xloc.file);
285 TREE_TYPE (str) = build_array_type_nelts (char_type_node, len);
286 TREE_READONLY (str) = 1;
287 TREE_STATIC (str) = 1;
288 str = build_fold_addr_expr (str);
290 tree ctor = build_constructor_va (type, 3, NULL_TREE, str, NULL_TREE,
291 build_int_cst (unsigned_type_node,
292 xloc.line), NULL_TREE,
293 build_int_cst (unsigned_type_node,
294 xloc.column));
295 TREE_CONSTANT (ctor) = 1;
296 TREE_STATIC (ctor) = 1;
298 return ctor;
301 /* This routine returns a magic number for TYPE. */
303 static unsigned short
304 get_ubsan_type_info_for_type (tree type)
306 if (TREE_CODE (type) == REAL_TYPE)
307 return tree_to_uhwi (TYPE_SIZE (type));
308 else if (INTEGRAL_TYPE_P (type))
310 int prec = exact_log2 (tree_to_uhwi (TYPE_SIZE (type)));
311 gcc_assert (prec != -1);
312 return (prec << 1) | !TYPE_UNSIGNED (type);
314 else
315 return 0;
318 /* Counters for internal labels. ubsan_ids[0] for Lubsan_type,
319 ubsan_ids[1] for Lubsan_data labels. */
320 static GTY(()) unsigned int ubsan_ids[2];
322 /* Helper routine that returns ADDR_EXPR of a VAR_DECL of a type
323 descriptor. It first looks into the hash table; if not found,
324 create the VAR_DECL, put it into the hash table and return the
325 ADDR_EXPR of it. TYPE describes a particular type. PSTYLE is
326 an enum controlling how we want to print the type. */
328 tree
329 ubsan_type_descriptor (tree type, enum ubsan_print_style pstyle)
331 /* See through any typedefs. */
332 type = TYPE_MAIN_VARIANT (type);
334 tree decl = decl_for_type_lookup (type);
335 /* It is possible that some of the earlier created DECLs were found
336 unused, in that case they weren't emitted and varpool_node::get
337 returns NULL node on them. But now we really need them. Thus,
338 renew them here. */
339 if (decl != NULL_TREE && varpool_node::get (decl))
340 return build_fold_addr_expr (decl);
342 tree dtype = ubsan_get_type_descriptor_type ();
343 tree type2 = type;
344 const char *tname = NULL;
345 pretty_printer pretty_name;
346 unsigned char deref_depth = 0;
347 unsigned short tkind, tinfo;
349 /* Get the name of the type, or the name of the pointer type. */
350 if (pstyle == UBSAN_PRINT_POINTER)
352 gcc_assert (POINTER_TYPE_P (type));
353 type2 = TREE_TYPE (type);
355 /* Remove any '*' operators from TYPE. */
356 while (POINTER_TYPE_P (type2))
357 deref_depth++, type2 = TREE_TYPE (type2);
359 if (TREE_CODE (type2) == METHOD_TYPE)
360 type2 = TYPE_METHOD_BASETYPE (type2);
363 /* If an array, get its type. */
364 type2 = strip_array_types (type2);
366 if (pstyle == UBSAN_PRINT_ARRAY)
368 while (POINTER_TYPE_P (type2))
369 deref_depth++, type2 = TREE_TYPE (type2);
372 if (TYPE_NAME (type2) != NULL)
374 if (TREE_CODE (TYPE_NAME (type2)) == IDENTIFIER_NODE)
375 tname = IDENTIFIER_POINTER (TYPE_NAME (type2));
376 else if (DECL_NAME (TYPE_NAME (type2)) != NULL)
377 tname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type2)));
380 if (tname == NULL)
381 /* We weren't able to determine the type name. */
382 tname = "<unknown>";
384 if (pstyle == UBSAN_PRINT_POINTER)
386 pp_printf (&pretty_name, "'%s%s%s%s%s%s%s",
387 TYPE_VOLATILE (type2) ? "volatile " : "",
388 TYPE_READONLY (type2) ? "const " : "",
389 TYPE_RESTRICT (type2) ? "restrict " : "",
390 TYPE_ATOMIC (type2) ? "_Atomic " : "",
391 TREE_CODE (type2) == RECORD_TYPE
392 ? "struct "
393 : TREE_CODE (type2) == UNION_TYPE
394 ? "union " : "", tname,
395 deref_depth == 0 ? "" : " ");
396 while (deref_depth-- > 0)
397 pp_star (&pretty_name);
398 pp_quote (&pretty_name);
400 else if (pstyle == UBSAN_PRINT_ARRAY)
402 /* Pretty print the array dimensions. */
403 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
404 tree t = type;
405 pp_printf (&pretty_name, "'%s ", tname);
406 while (deref_depth-- > 0)
407 pp_star (&pretty_name);
408 while (TREE_CODE (t) == ARRAY_TYPE)
410 pp_left_bracket (&pretty_name);
411 tree dom = TYPE_DOMAIN (t);
412 if (dom && TREE_CODE (TYPE_MAX_VALUE (dom)) == INTEGER_CST)
414 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (dom))
415 && tree_to_uhwi (TYPE_MAX_VALUE (dom)) + 1 != 0)
416 pp_printf (&pretty_name, HOST_WIDE_INT_PRINT_DEC,
417 tree_to_uhwi (TYPE_MAX_VALUE (dom)) + 1);
418 else
419 pp_wide_int (&pretty_name,
420 wi::add (wi::to_widest (TYPE_MAX_VALUE (dom)), 1),
421 TYPE_SIGN (TREE_TYPE (dom)));
423 else
424 /* ??? We can't determine the variable name; print VLA unspec. */
425 pp_star (&pretty_name);
426 pp_right_bracket (&pretty_name);
427 t = TREE_TYPE (t);
429 pp_quote (&pretty_name);
431 /* Save the tree with stripped types. */
432 type = t;
434 else
435 pp_printf (&pretty_name, "'%s'", tname);
437 switch (TREE_CODE (type))
439 case BOOLEAN_TYPE:
440 case ENUMERAL_TYPE:
441 case INTEGER_TYPE:
442 tkind = 0x0000;
443 break;
444 case REAL_TYPE:
445 /* FIXME: libubsan right now only supports float, double and
446 long double type formats. */
447 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
448 || TYPE_MODE (type) == TYPE_MODE (double_type_node)
449 || TYPE_MODE (type) == TYPE_MODE (long_double_type_node))
450 tkind = 0x0001;
451 else
452 tkind = 0xffff;
453 break;
454 default:
455 tkind = 0xffff;
456 break;
458 tinfo = get_ubsan_type_info_for_type (type);
460 /* Create a new VAR_DECL of type descriptor. */
461 const char *tmp = pp_formatted_text (&pretty_name);
462 size_t len = strlen (tmp) + 1;
463 tree str = build_string (len, tmp);
464 TREE_TYPE (str) = build_array_type_nelts (char_type_node, len);
465 TREE_READONLY (str) = 1;
466 TREE_STATIC (str) = 1;
468 char tmp_name[32];
469 ASM_GENERATE_INTERNAL_LABEL (tmp_name, "Lubsan_type", ubsan_ids[0]++);
470 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (tmp_name),
471 dtype);
472 TREE_STATIC (decl) = 1;
473 TREE_PUBLIC (decl) = 0;
474 DECL_ARTIFICIAL (decl) = 1;
475 DECL_IGNORED_P (decl) = 1;
476 DECL_EXTERNAL (decl) = 0;
477 DECL_SIZE (decl)
478 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (TREE_TYPE (str)));
479 DECL_SIZE_UNIT (decl)
480 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
481 TYPE_SIZE_UNIT (TREE_TYPE (str)));
483 tree ctor = build_constructor_va (dtype, 3, NULL_TREE,
484 build_int_cst (short_unsigned_type_node,
485 tkind), NULL_TREE,
486 build_int_cst (short_unsigned_type_node,
487 tinfo), NULL_TREE, str);
488 TREE_CONSTANT (ctor) = 1;
489 TREE_STATIC (ctor) = 1;
490 DECL_INITIAL (decl) = ctor;
491 varpool_node::finalize_decl (decl);
493 /* Save the VAR_DECL into the hash table. */
494 decl_for_type_insert (type, decl);
496 return build_fold_addr_expr (decl);
499 /* Create a structure for the ubsan library. NAME is a name of the new
500 structure. LOCCNT is number of locations, PLOC points to array of
501 locations. The arguments in ... are of __ubsan_type_descriptor type
502 and there are at most two of them, followed by NULL_TREE, followed
503 by optional extra arguments and another NULL_TREE. */
505 tree
506 ubsan_create_data (const char *name, int loccnt, const location_t *ploc, ...)
508 va_list args;
509 tree ret, t;
510 tree fields[6];
511 vec<tree, va_gc> *saved_args = NULL;
512 size_t i = 0;
513 int j;
515 /* It is possible that PCH zapped table with definitions of sanitizer
516 builtins. Reinitialize them if needed. */
517 initialize_sanitizer_builtins ();
519 /* Firstly, create a pointer to type descriptor type. */
520 tree td_type = ubsan_get_type_descriptor_type ();
521 td_type = build_pointer_type (td_type);
523 /* Create the structure type. */
524 ret = make_node (RECORD_TYPE);
525 for (j = 0; j < loccnt; j++)
527 gcc_checking_assert (i < 2);
528 fields[i] = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
529 ubsan_get_source_location_type ());
530 DECL_CONTEXT (fields[i]) = ret;
531 if (i)
532 DECL_CHAIN (fields[i - 1]) = fields[i];
533 i++;
536 va_start (args, ploc);
537 for (t = va_arg (args, tree); t != NULL_TREE;
538 i++, t = va_arg (args, tree))
540 gcc_checking_assert (i < 4);
541 /* Save the tree arguments for later use. */
542 vec_safe_push (saved_args, t);
543 fields[i] = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
544 td_type);
545 DECL_CONTEXT (fields[i]) = ret;
546 if (i)
547 DECL_CHAIN (fields[i - 1]) = fields[i];
550 for (t = va_arg (args, tree); t != NULL_TREE;
551 i++, t = va_arg (args, tree))
553 gcc_checking_assert (i < 6);
554 /* Save the tree arguments for later use. */
555 vec_safe_push (saved_args, t);
556 fields[i] = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
557 TREE_TYPE (t));
558 DECL_CONTEXT (fields[i]) = ret;
559 if (i)
560 DECL_CHAIN (fields[i - 1]) = fields[i];
562 va_end (args);
564 tree type_decl = build_decl (input_location, TYPE_DECL,
565 get_identifier (name), ret);
566 DECL_IGNORED_P (type_decl) = 1;
567 DECL_ARTIFICIAL (type_decl) = 1;
568 TYPE_FIELDS (ret) = fields[0];
569 TYPE_NAME (ret) = type_decl;
570 TYPE_STUB_DECL (ret) = type_decl;
571 layout_type (ret);
573 /* Now, fill in the type. */
574 char tmp_name[32];
575 ASM_GENERATE_INTERNAL_LABEL (tmp_name, "Lubsan_data", ubsan_ids[1]++);
576 tree var = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (tmp_name),
577 ret);
578 TREE_STATIC (var) = 1;
579 TREE_PUBLIC (var) = 0;
580 DECL_ARTIFICIAL (var) = 1;
581 DECL_IGNORED_P (var) = 1;
582 DECL_EXTERNAL (var) = 0;
584 vec<constructor_elt, va_gc> *v;
585 vec_alloc (v, i);
586 tree ctor = build_constructor (ret, v);
588 /* If desirable, set the __ubsan_source_location element. */
589 for (j = 0; j < loccnt; j++)
591 location_t loc = LOCATION_LOCUS (ploc[j]);
592 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, ubsan_source_location (loc));
595 size_t nelts = vec_safe_length (saved_args);
596 for (i = 0; i < nelts; i++)
598 t = (*saved_args)[i];
599 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t);
602 TREE_CONSTANT (ctor) = 1;
603 TREE_STATIC (ctor) = 1;
604 DECL_INITIAL (var) = ctor;
605 varpool_node::finalize_decl (var);
607 return var;
610 /* Instrument the __builtin_unreachable call. We just call the libubsan
611 routine instead. */
613 bool
614 ubsan_instrument_unreachable (gimple_stmt_iterator *gsi)
616 gimple *g;
617 location_t loc = gimple_location (gsi_stmt (*gsi));
619 if (flag_sanitize_undefined_trap_on_error)
620 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
621 else
623 tree data = ubsan_create_data ("__ubsan_unreachable_data", 1, &loc,
624 NULL_TREE, NULL_TREE);
625 data = build_fold_addr_expr_loc (loc, data);
626 tree fn
627 = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE);
628 g = gimple_build_call (fn, 1, data);
630 gimple_set_location (g, loc);
631 gsi_replace (gsi, g, false);
632 return false;
635 /* Return true if T is a call to a libubsan routine. */
637 bool
638 is_ubsan_builtin_p (tree t)
640 return TREE_CODE (t) == FUNCTION_DECL
641 && DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
642 && strncmp (IDENTIFIER_POINTER (DECL_NAME (t)),
643 "__builtin___ubsan_", 18) == 0;
646 /* Create a callgraph edge for statement STMT. */
648 static void
649 ubsan_create_edge (gimple *stmt)
651 gcall *call_stmt = dyn_cast <gcall *> (stmt);
652 basic_block bb = gimple_bb (stmt);
653 int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
654 cgraph_node *node = cgraph_node::get (current_function_decl);
655 tree decl = gimple_call_fndecl (call_stmt);
656 if (decl)
657 node->create_edge (cgraph_node::get_create (decl), call_stmt, bb->count,
658 freq);
661 /* Expand the UBSAN_BOUNDS special builtin function. */
663 bool
664 ubsan_expand_bounds_ifn (gimple_stmt_iterator *gsi)
666 gimple *stmt = gsi_stmt (*gsi);
667 location_t loc = gimple_location (stmt);
668 gcc_assert (gimple_call_num_args (stmt) == 3);
670 /* Pick up the arguments of the UBSAN_BOUNDS call. */
671 tree type = TREE_TYPE (TREE_TYPE (gimple_call_arg (stmt, 0)));
672 tree index = gimple_call_arg (stmt, 1);
673 tree orig_index_type = TREE_TYPE (index);
674 tree bound = gimple_call_arg (stmt, 2);
676 gimple_stmt_iterator gsi_orig = *gsi;
678 /* Create condition "if (index > bound)". */
679 basic_block then_bb, fallthru_bb;
680 gimple_stmt_iterator cond_insert_point
681 = create_cond_insert_point (gsi, false, false, true,
682 &then_bb, &fallthru_bb);
683 index = fold_convert (TREE_TYPE (bound), index);
684 index = force_gimple_operand_gsi (&cond_insert_point, index,
685 true, NULL_TREE,
686 false, GSI_NEW_STMT);
687 gimple *g = gimple_build_cond (GT_EXPR, index, bound, NULL_TREE, NULL_TREE);
688 gimple_set_location (g, loc);
689 gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT);
691 /* Generate __ubsan_handle_out_of_bounds call. */
692 *gsi = gsi_after_labels (then_bb);
693 if (flag_sanitize_undefined_trap_on_error)
694 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
695 else
697 tree data
698 = ubsan_create_data ("__ubsan_out_of_bounds_data", 1, &loc,
699 ubsan_type_descriptor (type, UBSAN_PRINT_ARRAY),
700 ubsan_type_descriptor (orig_index_type),
701 NULL_TREE, NULL_TREE);
702 data = build_fold_addr_expr_loc (loc, data);
703 enum built_in_function bcode
704 = (flag_sanitize_recover & SANITIZE_BOUNDS)
705 ? BUILT_IN_UBSAN_HANDLE_OUT_OF_BOUNDS
706 : BUILT_IN_UBSAN_HANDLE_OUT_OF_BOUNDS_ABORT;
707 tree fn = builtin_decl_explicit (bcode);
708 tree val = force_gimple_operand_gsi (gsi, ubsan_encode_value (index),
709 true, NULL_TREE, true,
710 GSI_SAME_STMT);
711 g = gimple_build_call (fn, 2, data, val);
713 gimple_set_location (g, loc);
714 gsi_insert_before (gsi, g, GSI_SAME_STMT);
716 /* Get rid of the UBSAN_BOUNDS call from the IR. */
717 unlink_stmt_vdef (stmt);
718 gsi_remove (&gsi_orig, true);
720 /* Point GSI to next logical statement. */
721 *gsi = gsi_start_bb (fallthru_bb);
722 return true;
725 /* Expand UBSAN_NULL internal call. The type is kept on the ckind
726 argument which is a constant, because the middle-end treats pointer
727 conversions as useless and therefore the type of the first argument
728 could be changed to any other pointer type. */
730 bool
731 ubsan_expand_null_ifn (gimple_stmt_iterator *gsip)
733 gimple_stmt_iterator gsi = *gsip;
734 gimple *stmt = gsi_stmt (gsi);
735 location_t loc = gimple_location (stmt);
736 gcc_assert (gimple_call_num_args (stmt) == 3);
737 tree ptr = gimple_call_arg (stmt, 0);
738 tree ckind = gimple_call_arg (stmt, 1);
739 tree align = gimple_call_arg (stmt, 2);
740 tree check_align = NULL_TREE;
741 bool check_null;
743 basic_block cur_bb = gsi_bb (gsi);
745 gimple *g;
746 if (!integer_zerop (align))
748 unsigned int ptralign = get_pointer_alignment (ptr) / BITS_PER_UNIT;
749 if (compare_tree_int (align, ptralign) == 1)
751 check_align = make_ssa_name (pointer_sized_int_node);
752 g = gimple_build_assign (check_align, NOP_EXPR, ptr);
753 gimple_set_location (g, loc);
754 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
757 check_null = (flag_sanitize & SANITIZE_NULL) != 0;
759 if (check_align == NULL_TREE && !check_null)
761 gsi_remove (gsip, true);
762 /* Unlink the UBSAN_NULLs vops before replacing it. */
763 unlink_stmt_vdef (stmt);
764 return true;
767 /* Split the original block holding the pointer dereference. */
768 edge e = split_block (cur_bb, stmt);
770 /* Get a hold on the 'condition block', the 'then block' and the
771 'else block'. */
772 basic_block cond_bb = e->src;
773 basic_block fallthru_bb = e->dest;
774 basic_block then_bb = create_empty_bb (cond_bb);
775 add_bb_to_loop (then_bb, cond_bb->loop_father);
776 loops_state_set (LOOPS_NEED_FIXUP);
778 /* Make an edge coming from the 'cond block' into the 'then block';
779 this edge is unlikely taken, so set up the probability accordingly. */
780 e = make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
781 e->probability = PROB_VERY_UNLIKELY;
783 /* Connect 'then block' with the 'else block'. This is needed
784 as the ubsan routines we call in the 'then block' are not noreturn.
785 The 'then block' only has one outcoming edge. */
786 make_single_succ_edge (then_bb, fallthru_bb, EDGE_FALLTHRU);
788 /* Set up the fallthrough basic block. */
789 e = find_edge (cond_bb, fallthru_bb);
790 e->flags = EDGE_FALSE_VALUE;
791 e->count = cond_bb->count;
792 e->probability = REG_BR_PROB_BASE - PROB_VERY_UNLIKELY;
794 /* Update dominance info for the newly created then_bb; note that
795 fallthru_bb's dominance info has already been updated by
796 split_block. */
797 if (dom_info_available_p (CDI_DOMINATORS))
798 set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
800 /* Put the ubsan builtin call into the newly created BB. */
801 if (flag_sanitize_undefined_trap_on_error)
802 g = gimple_build_call (builtin_decl_implicit (BUILT_IN_TRAP), 0);
803 else
805 enum built_in_function bcode
806 = (flag_sanitize_recover & ((check_align ? SANITIZE_ALIGNMENT : 0)
807 | (check_null ? SANITIZE_NULL : 0)))
808 ? BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH
809 : BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT;
810 tree fn = builtin_decl_implicit (bcode);
811 tree data
812 = ubsan_create_data ("__ubsan_null_data", 1, &loc,
813 ubsan_type_descriptor (TREE_TYPE (ckind),
814 UBSAN_PRINT_POINTER),
815 NULL_TREE,
816 align,
817 fold_convert (unsigned_char_type_node, ckind),
818 NULL_TREE);
819 data = build_fold_addr_expr_loc (loc, data);
820 g = gimple_build_call (fn, 2, data,
821 check_align ? check_align
822 : build_zero_cst (pointer_sized_int_node));
824 gimple_stmt_iterator gsi2 = gsi_start_bb (then_bb);
825 gimple_set_location (g, loc);
826 gsi_insert_after (&gsi2, g, GSI_NEW_STMT);
828 /* Unlink the UBSAN_NULLs vops before replacing it. */
829 unlink_stmt_vdef (stmt);
831 if (check_null)
833 g = gimple_build_cond (EQ_EXPR, ptr, build_int_cst (TREE_TYPE (ptr), 0),
834 NULL_TREE, NULL_TREE);
835 gimple_set_location (g, loc);
837 /* Replace the UBSAN_NULL with a GIMPLE_COND stmt. */
838 gsi_replace (&gsi, g, false);
839 stmt = g;
842 if (check_align)
844 if (check_null)
846 /* Split the block with the condition again. */
847 e = split_block (cond_bb, stmt);
848 basic_block cond1_bb = e->src;
849 basic_block cond2_bb = e->dest;
851 /* Make an edge coming from the 'cond1 block' into the 'then block';
852 this edge is unlikely taken, so set up the probability
853 accordingly. */
854 e = make_edge (cond1_bb, then_bb, EDGE_TRUE_VALUE);
855 e->probability = PROB_VERY_UNLIKELY;
857 /* Set up the fallthrough basic block. */
858 e = find_edge (cond1_bb, cond2_bb);
859 e->flags = EDGE_FALSE_VALUE;
860 e->count = cond1_bb->count;
861 e->probability = REG_BR_PROB_BASE - PROB_VERY_UNLIKELY;
863 /* Update dominance info. */
864 if (dom_info_available_p (CDI_DOMINATORS))
866 set_immediate_dominator (CDI_DOMINATORS, fallthru_bb, cond1_bb);
867 set_immediate_dominator (CDI_DOMINATORS, then_bb, cond1_bb);
870 gsi2 = gsi_start_bb (cond2_bb);
873 tree mask = build_int_cst (pointer_sized_int_node,
874 tree_to_uhwi (align) - 1);
875 g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
876 BIT_AND_EXPR, check_align, mask);
877 gimple_set_location (g, loc);
878 if (check_null)
879 gsi_insert_after (&gsi2, g, GSI_NEW_STMT);
880 else
881 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
883 g = gimple_build_cond (NE_EXPR, gimple_assign_lhs (g),
884 build_int_cst (pointer_sized_int_node, 0),
885 NULL_TREE, NULL_TREE);
886 gimple_set_location (g, loc);
887 if (check_null)
888 gsi_insert_after (&gsi2, g, GSI_NEW_STMT);
889 else
890 /* Replace the UBSAN_NULL with a GIMPLE_COND stmt. */
891 gsi_replace (&gsi, g, false);
893 return false;
896 #define OBJSZ_MAX_OFFSET (1024 * 16)
898 /* Expand UBSAN_OBJECT_SIZE internal call. */
900 bool
901 ubsan_expand_objsize_ifn (gimple_stmt_iterator *gsi)
903 gimple *stmt = gsi_stmt (*gsi);
904 location_t loc = gimple_location (stmt);
905 gcc_assert (gimple_call_num_args (stmt) == 4);
907 tree ptr = gimple_call_arg (stmt, 0);
908 tree offset = gimple_call_arg (stmt, 1);
909 tree size = gimple_call_arg (stmt, 2);
910 tree ckind = gimple_call_arg (stmt, 3);
911 gimple_stmt_iterator gsi_orig = *gsi;
912 gimple *g;
914 /* See if we can discard the check. */
915 if (TREE_CODE (size) != INTEGER_CST
916 || integer_all_onesp (size))
917 /* Yes, __builtin_object_size couldn't determine the
918 object size. */;
919 else if (TREE_CODE (offset) == INTEGER_CST
920 && wi::to_widest (offset) >= -OBJSZ_MAX_OFFSET
921 && wi::to_widest (offset) <= -1)
922 /* The offset is in range [-16K, -1]. */;
923 else
925 /* if (offset > objsize) */
926 basic_block then_bb, fallthru_bb;
927 gimple_stmt_iterator cond_insert_point
928 = create_cond_insert_point (gsi, false, false, true,
929 &then_bb, &fallthru_bb);
930 g = gimple_build_cond (GT_EXPR, offset, size, NULL_TREE, NULL_TREE);
931 gimple_set_location (g, loc);
932 gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT);
934 /* If the offset is small enough, we don't need the second
935 run-time check. */
936 if (TREE_CODE (offset) == INTEGER_CST
937 && wi::to_widest (offset) >= 0
938 && wi::to_widest (offset) <= OBJSZ_MAX_OFFSET)
939 *gsi = gsi_after_labels (then_bb);
940 else
942 /* Don't issue run-time error if (ptr > ptr + offset). That
943 may happen when computing a POINTER_PLUS_EXPR. */
944 basic_block then2_bb, fallthru2_bb;
946 gimple_stmt_iterator gsi2 = gsi_after_labels (then_bb);
947 cond_insert_point = create_cond_insert_point (&gsi2, false, false,
948 true, &then2_bb,
949 &fallthru2_bb);
950 /* Convert the pointer to an integer type. */
951 tree p = make_ssa_name (pointer_sized_int_node);
952 g = gimple_build_assign (p, NOP_EXPR, ptr);
953 gimple_set_location (g, loc);
954 gsi_insert_before (&cond_insert_point, g, GSI_NEW_STMT);
955 p = gimple_assign_lhs (g);
956 /* Compute ptr + offset. */
957 g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
958 PLUS_EXPR, p, offset);
959 gimple_set_location (g, loc);
960 gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT);
961 /* Now build the conditional and put it into the IR. */
962 g = gimple_build_cond (LE_EXPR, p, gimple_assign_lhs (g),
963 NULL_TREE, NULL_TREE);
964 gimple_set_location (g, loc);
965 gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT);
966 *gsi = gsi_after_labels (then2_bb);
969 /* Generate __ubsan_handle_type_mismatch call. */
970 if (flag_sanitize_undefined_trap_on_error)
971 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
972 else
974 tree data
975 = ubsan_create_data ("__ubsan_objsz_data", 1, &loc,
976 ubsan_type_descriptor (TREE_TYPE (ptr),
977 UBSAN_PRINT_POINTER),
978 NULL_TREE,
979 build_zero_cst (pointer_sized_int_node),
980 ckind,
981 NULL_TREE);
982 data = build_fold_addr_expr_loc (loc, data);
983 enum built_in_function bcode
984 = (flag_sanitize_recover & SANITIZE_OBJECT_SIZE)
985 ? BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH
986 : BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT;
987 tree p = make_ssa_name (pointer_sized_int_node);
988 g = gimple_build_assign (p, NOP_EXPR, ptr);
989 gimple_set_location (g, loc);
990 gsi_insert_before (gsi, g, GSI_SAME_STMT);
991 g = gimple_build_call (builtin_decl_explicit (bcode), 2, data, p);
993 gimple_set_location (g, loc);
994 gsi_insert_before (gsi, g, GSI_SAME_STMT);
996 /* Point GSI to next logical statement. */
997 *gsi = gsi_start_bb (fallthru_bb);
999 /* Get rid of the UBSAN_OBJECT_SIZE call from the IR. */
1000 unlink_stmt_vdef (stmt);
1001 gsi_remove (&gsi_orig, true);
1002 return true;
1005 /* Get rid of the UBSAN_OBJECT_SIZE call from the IR. */
1006 unlink_stmt_vdef (stmt);
1007 gsi_remove (gsi, true);
1008 return true;
1011 /* Cached __ubsan_vptr_type_cache decl. */
1012 static GTY(()) tree ubsan_vptr_type_cache_decl;
1014 /* Expand UBSAN_VPTR internal call. The type is kept on the ckind
1015 argument which is a constant, because the middle-end treats pointer
1016 conversions as useless and therefore the type of the first argument
1017 could be changed to any other pointer type. */
1019 bool
1020 ubsan_expand_vptr_ifn (gimple_stmt_iterator *gsip)
1022 gimple_stmt_iterator gsi = *gsip;
1023 gimple *stmt = gsi_stmt (gsi);
1024 location_t loc = gimple_location (stmt);
1025 gcc_assert (gimple_call_num_args (stmt) == 5);
1026 tree op = gimple_call_arg (stmt, 0);
1027 tree vptr = gimple_call_arg (stmt, 1);
1028 tree str_hash = gimple_call_arg (stmt, 2);
1029 tree ti_decl_addr = gimple_call_arg (stmt, 3);
1030 tree ckind_tree = gimple_call_arg (stmt, 4);
1031 ubsan_null_ckind ckind = (ubsan_null_ckind) tree_to_uhwi (ckind_tree);
1032 tree type = TREE_TYPE (TREE_TYPE (ckind_tree));
1033 gimple *g;
1034 basic_block fallthru_bb = NULL;
1036 if (ckind == UBSAN_DOWNCAST_POINTER)
1038 /* Guard everything with if (op != NULL) { ... }. */
1039 basic_block then_bb;
1040 gimple_stmt_iterator cond_insert_point
1041 = create_cond_insert_point (gsip, false, false, true,
1042 &then_bb, &fallthru_bb);
1043 g = gimple_build_cond (NE_EXPR, op, build_zero_cst (TREE_TYPE (op)),
1044 NULL_TREE, NULL_TREE);
1045 gimple_set_location (g, loc);
1046 gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT);
1047 *gsip = gsi_after_labels (then_bb);
1048 gsi_remove (&gsi, false);
1049 gsi_insert_before (gsip, stmt, GSI_NEW_STMT);
1050 gsi = *gsip;
1053 tree htype = TREE_TYPE (str_hash);
1054 tree cst = wide_int_to_tree (htype,
1055 wi::uhwi (((uint64_t) 0x9ddfea08 << 32)
1056 | 0xeb382d69, 64));
1057 g = gimple_build_assign (make_ssa_name (htype), BIT_XOR_EXPR,
1058 vptr, str_hash);
1059 gimple_set_location (g, loc);
1060 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1061 g = gimple_build_assign (make_ssa_name (htype), MULT_EXPR,
1062 gimple_assign_lhs (g), cst);
1063 gimple_set_location (g, loc);
1064 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1065 tree t1 = gimple_assign_lhs (g);
1066 g = gimple_build_assign (make_ssa_name (htype), LSHIFT_EXPR,
1067 t1, build_int_cst (integer_type_node, 47));
1068 gimple_set_location (g, loc);
1069 tree t2 = gimple_assign_lhs (g);
1070 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1071 g = gimple_build_assign (make_ssa_name (htype), BIT_XOR_EXPR,
1072 vptr, t1);
1073 gimple_set_location (g, loc);
1074 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1075 g = gimple_build_assign (make_ssa_name (htype), BIT_XOR_EXPR,
1076 t2, gimple_assign_lhs (g));
1077 gimple_set_location (g, loc);
1078 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1079 g = gimple_build_assign (make_ssa_name (htype), MULT_EXPR,
1080 gimple_assign_lhs (g), cst);
1081 gimple_set_location (g, loc);
1082 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1083 tree t3 = gimple_assign_lhs (g);
1084 g = gimple_build_assign (make_ssa_name (htype), LSHIFT_EXPR,
1085 t3, build_int_cst (integer_type_node, 47));
1086 gimple_set_location (g, loc);
1087 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1088 g = gimple_build_assign (make_ssa_name (htype), BIT_XOR_EXPR,
1089 t3, gimple_assign_lhs (g));
1090 gimple_set_location (g, loc);
1091 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1092 g = gimple_build_assign (make_ssa_name (htype), MULT_EXPR,
1093 gimple_assign_lhs (g), cst);
1094 gimple_set_location (g, loc);
1095 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1096 if (!useless_type_conversion_p (pointer_sized_int_node, htype))
1098 g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
1099 NOP_EXPR, gimple_assign_lhs (g));
1100 gimple_set_location (g, loc);
1101 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1103 tree hash = gimple_assign_lhs (g);
1105 if (ubsan_vptr_type_cache_decl == NULL_TREE)
1107 tree atype = build_array_type_nelts (pointer_sized_int_node, 128);
1108 tree array = build_decl (UNKNOWN_LOCATION, VAR_DECL,
1109 get_identifier ("__ubsan_vptr_type_cache"),
1110 atype);
1111 DECL_ARTIFICIAL (array) = 1;
1112 DECL_IGNORED_P (array) = 1;
1113 TREE_PUBLIC (array) = 1;
1114 TREE_STATIC (array) = 1;
1115 DECL_EXTERNAL (array) = 1;
1116 DECL_VISIBILITY (array) = VISIBILITY_DEFAULT;
1117 DECL_VISIBILITY_SPECIFIED (array) = 1;
1118 varpool_node::finalize_decl (array);
1119 ubsan_vptr_type_cache_decl = array;
1122 g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
1123 BIT_AND_EXPR, hash,
1124 build_int_cst (pointer_sized_int_node, 127));
1125 gimple_set_location (g, loc);
1126 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1128 tree c = build4_loc (loc, ARRAY_REF, pointer_sized_int_node,
1129 ubsan_vptr_type_cache_decl, gimple_assign_lhs (g),
1130 NULL_TREE, NULL_TREE);
1131 g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
1132 ARRAY_REF, c);
1133 gimple_set_location (g, loc);
1134 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1136 basic_block then_bb, fallthru2_bb;
1137 gimple_stmt_iterator cond_insert_point
1138 = create_cond_insert_point (gsip, false, false, true,
1139 &then_bb, &fallthru2_bb);
1140 g = gimple_build_cond (NE_EXPR, gimple_assign_lhs (g), hash,
1141 NULL_TREE, NULL_TREE);
1142 gimple_set_location (g, loc);
1143 gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT);
1144 *gsip = gsi_after_labels (then_bb);
1145 if (fallthru_bb == NULL)
1146 fallthru_bb = fallthru2_bb;
1148 tree data
1149 = ubsan_create_data ("__ubsan_vptr_data", 1, &loc,
1150 ubsan_type_descriptor (type), NULL_TREE, ti_decl_addr,
1151 build_int_cst (unsigned_char_type_node, ckind),
1152 NULL_TREE);
1153 data = build_fold_addr_expr_loc (loc, data);
1154 enum built_in_function bcode
1155 = (flag_sanitize_recover & SANITIZE_VPTR)
1156 ? BUILT_IN_UBSAN_HANDLE_DYNAMIC_TYPE_CACHE_MISS
1157 : BUILT_IN_UBSAN_HANDLE_DYNAMIC_TYPE_CACHE_MISS_ABORT;
1159 g = gimple_build_call (builtin_decl_explicit (bcode), 3, data, op, hash);
1160 gimple_set_location (g, loc);
1161 gsi_insert_before (gsip, g, GSI_SAME_STMT);
1163 /* Point GSI to next logical statement. */
1164 *gsip = gsi_start_bb (fallthru_bb);
1166 /* Get rid of the UBSAN_VPTR call from the IR. */
1167 unlink_stmt_vdef (stmt);
1168 gsi_remove (&gsi, true);
1169 return true;
1172 /* Instrument a memory reference. BASE is the base of MEM, IS_LHS says
1173 whether the pointer is on the left hand side of the assignment. */
1175 static void
1176 instrument_mem_ref (tree mem, tree base, gimple_stmt_iterator *iter,
1177 bool is_lhs)
1179 enum ubsan_null_ckind ikind = is_lhs ? UBSAN_STORE_OF : UBSAN_LOAD_OF;
1180 unsigned int align = 0;
1181 if (flag_sanitize & SANITIZE_ALIGNMENT)
1183 align = min_align_of_type (TREE_TYPE (base));
1184 if (align <= 1)
1185 align = 0;
1187 if (align == 0 && (flag_sanitize & SANITIZE_NULL) == 0)
1188 return;
1189 tree t = TREE_OPERAND (base, 0);
1190 if (!POINTER_TYPE_P (TREE_TYPE (t)))
1191 return;
1192 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (base)) && mem != base)
1193 ikind = UBSAN_MEMBER_ACCESS;
1194 tree kind = build_int_cst (build_pointer_type (TREE_TYPE (base)), ikind);
1195 tree alignt = build_int_cst (pointer_sized_int_node, align);
1196 gcall *g = gimple_build_call_internal (IFN_UBSAN_NULL, 3, t, kind, alignt);
1197 gimple_set_location (g, gimple_location (gsi_stmt (*iter)));
1198 gsi_insert_before (iter, g, GSI_SAME_STMT);
1201 /* Perform the pointer instrumentation. */
1203 static void
1204 instrument_null (gimple_stmt_iterator gsi, bool is_lhs)
1206 gimple *stmt = gsi_stmt (gsi);
1207 tree t = is_lhs ? gimple_get_lhs (stmt) : gimple_assign_rhs1 (stmt);
1208 tree base = get_base_address (t);
1209 const enum tree_code code = TREE_CODE (base);
1210 if (code == MEM_REF
1211 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1212 instrument_mem_ref (t, base, &gsi, is_lhs);
1215 /* Build an ubsan builtin call for the signed-integer-overflow
1216 sanitization. CODE says what kind of builtin are we building,
1217 LOC is a location, LHSTYPE is the type of LHS, OP0 and OP1
1218 are operands of the binary operation. */
1220 tree
1221 ubsan_build_overflow_builtin (tree_code code, location_t loc, tree lhstype,
1222 tree op0, tree op1)
1224 if (flag_sanitize_undefined_trap_on_error)
1225 return build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
1227 tree data = ubsan_create_data ("__ubsan_overflow_data", 1, &loc,
1228 ubsan_type_descriptor (lhstype), NULL_TREE,
1229 NULL_TREE);
1230 enum built_in_function fn_code;
1232 switch (code)
1234 case PLUS_EXPR:
1235 fn_code = (flag_sanitize_recover & SANITIZE_SI_OVERFLOW)
1236 ? BUILT_IN_UBSAN_HANDLE_ADD_OVERFLOW
1237 : BUILT_IN_UBSAN_HANDLE_ADD_OVERFLOW_ABORT;
1238 break;
1239 case MINUS_EXPR:
1240 fn_code = (flag_sanitize_recover & SANITIZE_SI_OVERFLOW)
1241 ? BUILT_IN_UBSAN_HANDLE_SUB_OVERFLOW
1242 : BUILT_IN_UBSAN_HANDLE_SUB_OVERFLOW_ABORT;
1243 break;
1244 case MULT_EXPR:
1245 fn_code = (flag_sanitize_recover & SANITIZE_SI_OVERFLOW)
1246 ? BUILT_IN_UBSAN_HANDLE_MUL_OVERFLOW
1247 : BUILT_IN_UBSAN_HANDLE_MUL_OVERFLOW_ABORT;
1248 break;
1249 case NEGATE_EXPR:
1250 fn_code = (flag_sanitize_recover & SANITIZE_SI_OVERFLOW)
1251 ? BUILT_IN_UBSAN_HANDLE_NEGATE_OVERFLOW
1252 : BUILT_IN_UBSAN_HANDLE_NEGATE_OVERFLOW_ABORT;
1253 break;
1254 default:
1255 gcc_unreachable ();
1257 tree fn = builtin_decl_explicit (fn_code);
1258 return build_call_expr_loc (loc, fn, 2 + (code != NEGATE_EXPR),
1259 build_fold_addr_expr_loc (loc, data),
1260 ubsan_encode_value (op0, true),
1261 op1 ? ubsan_encode_value (op1, true)
1262 : NULL_TREE);
1265 /* Perform the signed integer instrumentation. GSI is the iterator
1266 pointing at statement we are trying to instrument. */
1268 static void
1269 instrument_si_overflow (gimple_stmt_iterator gsi)
1271 gimple *stmt = gsi_stmt (gsi);
1272 tree_code code = gimple_assign_rhs_code (stmt);
1273 tree lhs = gimple_assign_lhs (stmt);
1274 tree lhstype = TREE_TYPE (lhs);
1275 tree a, b;
1276 gimple *g;
1278 /* If this is not a signed operation, don't instrument anything here.
1279 Also punt on bit-fields. */
1280 if (!INTEGRAL_TYPE_P (lhstype)
1281 || TYPE_OVERFLOW_WRAPS (lhstype)
1282 || GET_MODE_BITSIZE (TYPE_MODE (lhstype)) != TYPE_PRECISION (lhstype))
1283 return;
1285 switch (code)
1287 case MINUS_EXPR:
1288 case PLUS_EXPR:
1289 case MULT_EXPR:
1290 /* Transform
1291 i = u {+,-,*} 5;
1292 into
1293 i = UBSAN_CHECK_{ADD,SUB,MUL} (u, 5); */
1294 a = gimple_assign_rhs1 (stmt);
1295 b = gimple_assign_rhs2 (stmt);
1296 g = gimple_build_call_internal (code == PLUS_EXPR
1297 ? IFN_UBSAN_CHECK_ADD
1298 : code == MINUS_EXPR
1299 ? IFN_UBSAN_CHECK_SUB
1300 : IFN_UBSAN_CHECK_MUL, 2, a, b);
1301 gimple_call_set_lhs (g, lhs);
1302 gsi_replace (&gsi, g, true);
1303 break;
1304 case NEGATE_EXPR:
1305 /* Represent i = -u;
1307 i = UBSAN_CHECK_SUB (0, u); */
1308 a = build_int_cst (lhstype, 0);
1309 b = gimple_assign_rhs1 (stmt);
1310 g = gimple_build_call_internal (IFN_UBSAN_CHECK_SUB, 2, a, b);
1311 gimple_call_set_lhs (g, lhs);
1312 gsi_replace (&gsi, g, true);
1313 break;
1314 case ABS_EXPR:
1315 /* Transform i = ABS_EXPR<u>;
1316 into
1317 _N = UBSAN_CHECK_SUB (0, u);
1318 i = ABS_EXPR<_N>; */
1319 a = build_int_cst (lhstype, 0);
1320 b = gimple_assign_rhs1 (stmt);
1321 g = gimple_build_call_internal (IFN_UBSAN_CHECK_SUB, 2, a, b);
1322 a = make_ssa_name (lhstype);
1323 gimple_call_set_lhs (g, a);
1324 gimple_set_location (g, gimple_location (stmt));
1325 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
1326 gimple_assign_set_rhs1 (stmt, a);
1327 update_stmt (stmt);
1328 break;
1329 default:
1330 break;
1334 /* Instrument loads from (non-bitfield) bool and C++ enum values
1335 to check if the memory value is outside of the range of the valid
1336 type values. */
1338 static void
1339 instrument_bool_enum_load (gimple_stmt_iterator *gsi)
1341 gimple *stmt = gsi_stmt (*gsi);
1342 tree rhs = gimple_assign_rhs1 (stmt);
1343 tree type = TREE_TYPE (rhs);
1344 tree minv = NULL_TREE, maxv = NULL_TREE;
1346 if (TREE_CODE (type) == BOOLEAN_TYPE && (flag_sanitize & SANITIZE_BOOL))
1348 minv = boolean_false_node;
1349 maxv = boolean_true_node;
1351 else if (TREE_CODE (type) == ENUMERAL_TYPE
1352 && (flag_sanitize & SANITIZE_ENUM)
1353 && TREE_TYPE (type) != NULL_TREE
1354 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
1355 && (TYPE_PRECISION (TREE_TYPE (type))
1356 < GET_MODE_PRECISION (TYPE_MODE (type))))
1358 minv = TYPE_MIN_VALUE (TREE_TYPE (type));
1359 maxv = TYPE_MAX_VALUE (TREE_TYPE (type));
1361 else
1362 return;
1364 int modebitsize = GET_MODE_BITSIZE (TYPE_MODE (type));
1365 HOST_WIDE_INT bitsize, bitpos;
1366 tree offset;
1367 machine_mode mode;
1368 int volatilep = 0, reversep, unsignedp = 0;
1369 tree base = get_inner_reference (rhs, &bitsize, &bitpos, &offset, &mode,
1370 &unsignedp, &reversep, &volatilep);
1371 tree utype = build_nonstandard_integer_type (modebitsize, 1);
1373 if ((VAR_P (base) && DECL_HARD_REGISTER (base))
1374 || (bitpos % modebitsize) != 0
1375 || bitsize != modebitsize
1376 || GET_MODE_BITSIZE (TYPE_MODE (utype)) != modebitsize
1377 || TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
1378 return;
1380 bool ends_bb = stmt_ends_bb_p (stmt);
1381 location_t loc = gimple_location (stmt);
1382 tree lhs = gimple_assign_lhs (stmt);
1383 tree ptype = build_pointer_type (TREE_TYPE (rhs));
1384 tree atype = reference_alias_ptr_type (rhs);
1385 gimple *g = gimple_build_assign (make_ssa_name (ptype),
1386 build_fold_addr_expr (rhs));
1387 gimple_set_location (g, loc);
1388 gsi_insert_before (gsi, g, GSI_SAME_STMT);
1389 tree mem = build2 (MEM_REF, utype, gimple_assign_lhs (g),
1390 build_int_cst (atype, 0));
1391 tree urhs = make_ssa_name (utype);
1392 if (ends_bb)
1394 gimple_assign_set_lhs (stmt, urhs);
1395 g = gimple_build_assign (lhs, NOP_EXPR, urhs);
1396 gimple_set_location (g, loc);
1397 edge e = find_fallthru_edge (gimple_bb (stmt)->succs);
1398 gsi_insert_on_edge_immediate (e, g);
1399 gimple_assign_set_rhs_from_tree (gsi, mem);
1400 update_stmt (stmt);
1401 *gsi = gsi_for_stmt (g);
1402 g = stmt;
1404 else
1406 g = gimple_build_assign (urhs, mem);
1407 gimple_set_location (g, loc);
1408 gsi_insert_before (gsi, g, GSI_SAME_STMT);
1410 minv = fold_convert (utype, minv);
1411 maxv = fold_convert (utype, maxv);
1412 if (!integer_zerop (minv))
1414 g = gimple_build_assign (make_ssa_name (utype), MINUS_EXPR, urhs, minv);
1415 gimple_set_location (g, loc);
1416 gsi_insert_before (gsi, g, GSI_SAME_STMT);
1419 gimple_stmt_iterator gsi2 = *gsi;
1420 basic_block then_bb, fallthru_bb;
1421 *gsi = create_cond_insert_point (gsi, true, false, true,
1422 &then_bb, &fallthru_bb);
1423 g = gimple_build_cond (GT_EXPR, gimple_assign_lhs (g),
1424 int_const_binop (MINUS_EXPR, maxv, minv),
1425 NULL_TREE, NULL_TREE);
1426 gimple_set_location (g, loc);
1427 gsi_insert_after (gsi, g, GSI_NEW_STMT);
1429 if (!ends_bb)
1431 gimple_assign_set_rhs_with_ops (&gsi2, NOP_EXPR, urhs);
1432 update_stmt (stmt);
1435 gsi2 = gsi_after_labels (then_bb);
1436 if (flag_sanitize_undefined_trap_on_error)
1437 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
1438 else
1440 tree data = ubsan_create_data ("__ubsan_invalid_value_data", 1, &loc,
1441 ubsan_type_descriptor (type), NULL_TREE,
1442 NULL_TREE);
1443 data = build_fold_addr_expr_loc (loc, data);
1444 enum built_in_function bcode
1445 = (flag_sanitize_recover & (TREE_CODE (type) == BOOLEAN_TYPE
1446 ? SANITIZE_BOOL : SANITIZE_ENUM))
1447 ? BUILT_IN_UBSAN_HANDLE_LOAD_INVALID_VALUE
1448 : BUILT_IN_UBSAN_HANDLE_LOAD_INVALID_VALUE_ABORT;
1449 tree fn = builtin_decl_explicit (bcode);
1451 tree val = force_gimple_operand_gsi (&gsi2, ubsan_encode_value (urhs),
1452 true, NULL_TREE, true,
1453 GSI_SAME_STMT);
1454 g = gimple_build_call (fn, 2, data, val);
1456 gimple_set_location (g, loc);
1457 gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
1458 ubsan_create_edge (g);
1459 *gsi = gsi_for_stmt (stmt);
1462 /* Determine if we can propagate given LOCATION to ubsan_data descriptor to use
1463 new style handlers. Libubsan uses heuristics to destinguish between old and
1464 new styles and relies on these properties for filename:
1466 a) Location's filename must not be NULL.
1467 b) Location's filename must not be equal to "".
1468 c) Location's filename must not be equal to "\1".
1469 d) First two bytes of filename must not contain '\xff' symbol. */
1471 static bool
1472 ubsan_use_new_style_p (location_t loc)
1474 if (loc == UNKNOWN_LOCATION)
1475 return false;
1477 expanded_location xloc = expand_location (loc);
1478 if (xloc.file == NULL || strncmp (xloc.file, "\1", 2) == 0
1479 || xloc.file[0] == '\0' || xloc.file[0] == '\xff'
1480 || xloc.file[1] == '\xff')
1481 return false;
1483 return true;
1486 /* Instrument float point-to-integer conversion. TYPE is an integer type of
1487 destination, EXPR is floating-point expression. */
1489 tree
1490 ubsan_instrument_float_cast (location_t loc, tree type, tree expr)
1492 tree expr_type = TREE_TYPE (expr);
1493 tree t, tt, fn, min, max;
1494 machine_mode mode = TYPE_MODE (expr_type);
1495 int prec = TYPE_PRECISION (type);
1496 bool uns_p = TYPE_UNSIGNED (type);
1497 if (loc == UNKNOWN_LOCATION)
1498 loc = input_location;
1500 /* Float to integer conversion first truncates toward zero, so
1501 even signed char c = 127.875f; is not problematic.
1502 Therefore, we should complain only if EXPR is unordered or smaller
1503 or equal than TYPE_MIN_VALUE - 1.0 or greater or equal than
1504 TYPE_MAX_VALUE + 1.0. */
1505 if (REAL_MODE_FORMAT (mode)->b == 2)
1507 /* For maximum, TYPE_MAX_VALUE might not be representable
1508 in EXPR_TYPE, e.g. if TYPE is 64-bit long long and
1509 EXPR_TYPE is IEEE single float, but TYPE_MAX_VALUE + 1.0 is
1510 either representable or infinity. */
1511 REAL_VALUE_TYPE maxval = dconst1;
1512 SET_REAL_EXP (&maxval, REAL_EXP (&maxval) + prec - !uns_p);
1513 real_convert (&maxval, mode, &maxval);
1514 max = build_real (expr_type, maxval);
1516 /* For unsigned, assume -1.0 is always representable. */
1517 if (uns_p)
1518 min = build_minus_one_cst (expr_type);
1519 else
1521 /* TYPE_MIN_VALUE is generally representable (or -inf),
1522 but TYPE_MIN_VALUE - 1.0 might not be. */
1523 REAL_VALUE_TYPE minval = dconstm1, minval2;
1524 SET_REAL_EXP (&minval, REAL_EXP (&minval) + prec - 1);
1525 real_convert (&minval, mode, &minval);
1526 real_arithmetic (&minval2, MINUS_EXPR, &minval, &dconst1);
1527 real_convert (&minval2, mode, &minval2);
1528 if (real_compare (EQ_EXPR, &minval, &minval2)
1529 && !real_isinf (&minval))
1531 /* If TYPE_MIN_VALUE - 1.0 is not representable and
1532 rounds to TYPE_MIN_VALUE, we need to subtract
1533 more. As REAL_MODE_FORMAT (mode)->p is the number
1534 of base digits, we want to subtract a number that
1535 will be 1 << (REAL_MODE_FORMAT (mode)->p - 1)
1536 times smaller than minval. */
1537 minval2 = dconst1;
1538 gcc_assert (prec > REAL_MODE_FORMAT (mode)->p);
1539 SET_REAL_EXP (&minval2,
1540 REAL_EXP (&minval2) + prec - 1
1541 - REAL_MODE_FORMAT (mode)->p + 1);
1542 real_arithmetic (&minval2, MINUS_EXPR, &minval, &minval2);
1543 real_convert (&minval2, mode, &minval2);
1545 min = build_real (expr_type, minval2);
1548 else if (REAL_MODE_FORMAT (mode)->b == 10)
1550 /* For _Decimal128 up to 34 decimal digits, - sign,
1551 dot, e, exponent. */
1552 char buf[64];
1553 mpfr_t m;
1554 int p = REAL_MODE_FORMAT (mode)->p;
1555 REAL_VALUE_TYPE maxval, minval;
1557 /* Use mpfr_snprintf rounding to compute the smallest
1558 representable decimal number greater or equal than
1559 1 << (prec - !uns_p). */
1560 mpfr_init2 (m, prec + 2);
1561 mpfr_set_ui_2exp (m, 1, prec - !uns_p, GMP_RNDN);
1562 mpfr_snprintf (buf, sizeof buf, "%.*RUe", p - 1, m);
1563 decimal_real_from_string (&maxval, buf);
1564 max = build_real (expr_type, maxval);
1566 /* For unsigned, assume -1.0 is always representable. */
1567 if (uns_p)
1568 min = build_minus_one_cst (expr_type);
1569 else
1571 /* Use mpfr_snprintf rounding to compute the largest
1572 representable decimal number less or equal than
1573 (-1 << (prec - 1)) - 1. */
1574 mpfr_set_si_2exp (m, -1, prec - 1, GMP_RNDN);
1575 mpfr_sub_ui (m, m, 1, GMP_RNDN);
1576 mpfr_snprintf (buf, sizeof buf, "%.*RDe", p - 1, m);
1577 decimal_real_from_string (&minval, buf);
1578 min = build_real (expr_type, minval);
1580 mpfr_clear (m);
1582 else
1583 return NULL_TREE;
1585 t = fold_build2 (UNLE_EXPR, boolean_type_node, expr, min);
1586 tt = fold_build2 (UNGE_EXPR, boolean_type_node, expr, max);
1587 t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, tt);
1588 if (integer_zerop (t))
1589 return NULL_TREE;
1591 if (flag_sanitize_undefined_trap_on_error)
1592 fn = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
1593 else
1595 location_t *loc_ptr = NULL;
1596 unsigned num_locations = 0;
1597 /* Figure out if we can propagate location to ubsan_data and use new
1598 style handlers in libubsan. */
1599 if (ubsan_use_new_style_p (loc))
1601 loc_ptr = &loc;
1602 num_locations = 1;
1604 /* Create the __ubsan_handle_float_cast_overflow fn call. */
1605 tree data = ubsan_create_data ("__ubsan_float_cast_overflow_data",
1606 num_locations, loc_ptr,
1607 ubsan_type_descriptor (expr_type),
1608 ubsan_type_descriptor (type), NULL_TREE,
1609 NULL_TREE);
1610 enum built_in_function bcode
1611 = (flag_sanitize_recover & SANITIZE_FLOAT_CAST)
1612 ? BUILT_IN_UBSAN_HANDLE_FLOAT_CAST_OVERFLOW
1613 : BUILT_IN_UBSAN_HANDLE_FLOAT_CAST_OVERFLOW_ABORT;
1614 fn = builtin_decl_explicit (bcode);
1615 fn = build_call_expr_loc (loc, fn, 2,
1616 build_fold_addr_expr_loc (loc, data),
1617 ubsan_encode_value (expr, false));
1620 return fold_build3 (COND_EXPR, void_type_node, t, fn, integer_zero_node);
1623 /* Instrument values passed to function arguments with nonnull attribute. */
1625 static void
1626 instrument_nonnull_arg (gimple_stmt_iterator *gsi)
1628 gimple *stmt = gsi_stmt (*gsi);
1629 location_t loc[2];
1630 /* infer_nonnull_range needs flag_delete_null_pointer_checks set,
1631 while for nonnull sanitization it is clear. */
1632 int save_flag_delete_null_pointer_checks = flag_delete_null_pointer_checks;
1633 flag_delete_null_pointer_checks = 1;
1634 loc[0] = gimple_location (stmt);
1635 loc[1] = UNKNOWN_LOCATION;
1636 for (unsigned int i = 0; i < gimple_call_num_args (stmt); i++)
1638 tree arg = gimple_call_arg (stmt, i);
1639 if (POINTER_TYPE_P (TREE_TYPE (arg))
1640 && infer_nonnull_range_by_attribute (stmt, arg))
1642 gimple *g;
1643 if (!is_gimple_val (arg))
1645 g = gimple_build_assign (make_ssa_name (TREE_TYPE (arg)), arg);
1646 gimple_set_location (g, loc[0]);
1647 gsi_insert_before (gsi, g, GSI_SAME_STMT);
1648 arg = gimple_assign_lhs (g);
1651 basic_block then_bb, fallthru_bb;
1652 *gsi = create_cond_insert_point (gsi, true, false, true,
1653 &then_bb, &fallthru_bb);
1654 g = gimple_build_cond (EQ_EXPR, arg,
1655 build_zero_cst (TREE_TYPE (arg)),
1656 NULL_TREE, NULL_TREE);
1657 gimple_set_location (g, loc[0]);
1658 gsi_insert_after (gsi, g, GSI_NEW_STMT);
1660 *gsi = gsi_after_labels (then_bb);
1661 if (flag_sanitize_undefined_trap_on_error)
1662 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
1663 else
1665 tree data = ubsan_create_data ("__ubsan_nonnull_arg_data",
1666 2, loc, NULL_TREE,
1667 build_int_cst (integer_type_node,
1668 i + 1),
1669 NULL_TREE);
1670 data = build_fold_addr_expr_loc (loc[0], data);
1671 enum built_in_function bcode
1672 = (flag_sanitize_recover & SANITIZE_NONNULL_ATTRIBUTE)
1673 ? BUILT_IN_UBSAN_HANDLE_NONNULL_ARG
1674 : BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT;
1675 tree fn = builtin_decl_explicit (bcode);
1677 g = gimple_build_call (fn, 1, data);
1679 gimple_set_location (g, loc[0]);
1680 gsi_insert_before (gsi, g, GSI_SAME_STMT);
1681 ubsan_create_edge (g);
1683 *gsi = gsi_for_stmt (stmt);
1685 flag_delete_null_pointer_checks = save_flag_delete_null_pointer_checks;
1688 /* Instrument returns in functions with returns_nonnull attribute. */
1690 static void
1691 instrument_nonnull_return (gimple_stmt_iterator *gsi)
1693 greturn *stmt = as_a <greturn *> (gsi_stmt (*gsi));
1694 location_t loc[2];
1695 tree arg = gimple_return_retval (stmt);
1696 /* infer_nonnull_range needs flag_delete_null_pointer_checks set,
1697 while for nonnull return sanitization it is clear. */
1698 int save_flag_delete_null_pointer_checks = flag_delete_null_pointer_checks;
1699 flag_delete_null_pointer_checks = 1;
1700 loc[0] = gimple_location (stmt);
1701 loc[1] = UNKNOWN_LOCATION;
1702 if (arg
1703 && POINTER_TYPE_P (TREE_TYPE (arg))
1704 && is_gimple_val (arg)
1705 && infer_nonnull_range_by_attribute (stmt, arg))
1707 basic_block then_bb, fallthru_bb;
1708 *gsi = create_cond_insert_point (gsi, true, false, true,
1709 &then_bb, &fallthru_bb);
1710 gimple *g = gimple_build_cond (EQ_EXPR, arg,
1711 build_zero_cst (TREE_TYPE (arg)),
1712 NULL_TREE, NULL_TREE);
1713 gimple_set_location (g, loc[0]);
1714 gsi_insert_after (gsi, g, GSI_NEW_STMT);
1716 *gsi = gsi_after_labels (then_bb);
1717 if (flag_sanitize_undefined_trap_on_error)
1718 g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
1719 else
1721 tree data = ubsan_create_data ("__ubsan_nonnull_return_data",
1722 2, loc, NULL_TREE, NULL_TREE);
1723 data = build_fold_addr_expr_loc (loc[0], data);
1724 enum built_in_function bcode
1725 = (flag_sanitize_recover & SANITIZE_RETURNS_NONNULL_ATTRIBUTE)
1726 ? BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN
1727 : BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT;
1728 tree fn = builtin_decl_explicit (bcode);
1730 g = gimple_build_call (fn, 1, data);
1732 gimple_set_location (g, loc[0]);
1733 gsi_insert_before (gsi, g, GSI_SAME_STMT);
1734 ubsan_create_edge (g);
1735 *gsi = gsi_for_stmt (stmt);
1737 flag_delete_null_pointer_checks = save_flag_delete_null_pointer_checks;
1740 /* Instrument memory references. Here we check whether the pointer
1741 points to an out-of-bounds location. */
1743 static void
1744 instrument_object_size (gimple_stmt_iterator *gsi, bool is_lhs)
1746 gimple *stmt = gsi_stmt (*gsi);
1747 location_t loc = gimple_location (stmt);
1748 tree t = is_lhs ? gimple_get_lhs (stmt) : gimple_assign_rhs1 (stmt);
1749 tree type;
1750 tree index = NULL_TREE;
1751 HOST_WIDE_INT size_in_bytes;
1753 type = TREE_TYPE (t);
1754 if (VOID_TYPE_P (type))
1755 return;
1757 switch (TREE_CODE (t))
1759 case COMPONENT_REF:
1760 if (TREE_CODE (t) == COMPONENT_REF
1761 && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)) != NULL_TREE)
1763 tree repr = DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1));
1764 t = build3 (COMPONENT_REF, TREE_TYPE (repr), TREE_OPERAND (t, 0),
1765 repr, NULL_TREE);
1767 break;
1768 case ARRAY_REF:
1769 index = TREE_OPERAND (t, 1);
1770 break;
1771 case INDIRECT_REF:
1772 case MEM_REF:
1773 case VAR_DECL:
1774 case PARM_DECL:
1775 case RESULT_DECL:
1776 break;
1777 default:
1778 return;
1781 size_in_bytes = int_size_in_bytes (type);
1782 if (size_in_bytes <= 0)
1783 return;
1785 HOST_WIDE_INT bitsize, bitpos;
1786 tree offset;
1787 machine_mode mode;
1788 int volatilep = 0, reversep, unsignedp = 0;
1789 tree inner = get_inner_reference (t, &bitsize, &bitpos, &offset, &mode,
1790 &unsignedp, &reversep, &volatilep);
1792 if (bitpos % BITS_PER_UNIT != 0
1793 || bitsize != size_in_bytes * BITS_PER_UNIT)
1794 return;
1796 bool decl_p = DECL_P (inner);
1797 tree base;
1798 if (decl_p)
1799 base = inner;
1800 else if (TREE_CODE (inner) == MEM_REF)
1801 base = TREE_OPERAND (inner, 0);
1802 else
1803 return;
1804 tree ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
1806 while (TREE_CODE (base) == SSA_NAME)
1808 gimple *def_stmt = SSA_NAME_DEF_STMT (base);
1809 if (gimple_assign_ssa_name_copy_p (def_stmt)
1810 || (gimple_assign_cast_p (def_stmt)
1811 && POINTER_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
1812 || (is_gimple_assign (def_stmt)
1813 && gimple_assign_rhs_code (def_stmt) == POINTER_PLUS_EXPR))
1815 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1816 if (TREE_CODE (rhs1) == SSA_NAME
1817 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1))
1818 break;
1819 else
1820 base = rhs1;
1822 else
1823 break;
1826 if (!POINTER_TYPE_P (TREE_TYPE (base)) && !DECL_P (base))
1827 return;
1829 tree sizet;
1830 tree base_addr = base;
1831 gimple *bos_stmt = NULL;
1832 if (decl_p)
1833 base_addr = build1 (ADDR_EXPR,
1834 build_pointer_type (TREE_TYPE (base)), base);
1835 unsigned HOST_WIDE_INT size;
1836 if (compute_builtin_object_size (base_addr, 0, &size))
1837 sizet = build_int_cst (sizetype, size);
1838 else if (optimize)
1840 if (LOCATION_LOCUS (loc) == UNKNOWN_LOCATION)
1841 loc = input_location;
1842 /* Generate __builtin_object_size call. */
1843 sizet = builtin_decl_explicit (BUILT_IN_OBJECT_SIZE);
1844 sizet = build_call_expr_loc (loc, sizet, 2, base_addr,
1845 integer_zero_node);
1846 sizet = force_gimple_operand_gsi (gsi, sizet, false, NULL_TREE, true,
1847 GSI_SAME_STMT);
1848 /* If the call above didn't end up being an integer constant, go one
1849 statement back and get the __builtin_object_size stmt. Save it,
1850 we might need it later. */
1851 if (SSA_VAR_P (sizet))
1853 gsi_prev (gsi);
1854 bos_stmt = gsi_stmt (*gsi);
1856 /* Move on to where we were. */
1857 gsi_next (gsi);
1860 else
1861 return;
1863 /* Generate UBSAN_OBJECT_SIZE (ptr, ptr+sizeof(*ptr)-base, objsize, ckind)
1864 call. */
1865 /* ptr + sizeof (*ptr) - base */
1866 t = fold_build2 (MINUS_EXPR, sizetype,
1867 fold_convert (pointer_sized_int_node, ptr),
1868 fold_convert (pointer_sized_int_node, base_addr));
1869 t = fold_build2 (PLUS_EXPR, sizetype, t, TYPE_SIZE_UNIT (type));
1871 /* Perhaps we can omit the check. */
1872 if (TREE_CODE (t) == INTEGER_CST
1873 && TREE_CODE (sizet) == INTEGER_CST
1874 && tree_int_cst_le (t, sizet))
1875 return;
1877 if (index != NULL_TREE
1878 && TREE_CODE (index) == SSA_NAME
1879 && TREE_CODE (sizet) == INTEGER_CST)
1881 gimple *def = SSA_NAME_DEF_STMT (index);
1882 if (is_gimple_assign (def)
1883 && gimple_assign_rhs_code (def) == BIT_AND_EXPR
1884 && TREE_CODE (gimple_assign_rhs2 (def)) == INTEGER_CST)
1886 tree cst = gimple_assign_rhs2 (def);
1887 tree sz = fold_build2 (EXACT_DIV_EXPR, sizetype, sizet,
1888 TYPE_SIZE_UNIT (type));
1889 if (tree_int_cst_sgn (cst) >= 0
1890 && tree_int_cst_lt (cst, sz))
1891 return;
1895 if (bos_stmt && gimple_call_builtin_p (bos_stmt, BUILT_IN_OBJECT_SIZE))
1896 ubsan_create_edge (bos_stmt);
1898 /* We have to emit the check. */
1899 t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, true,
1900 GSI_SAME_STMT);
1901 ptr = force_gimple_operand_gsi (gsi, ptr, true, NULL_TREE, true,
1902 GSI_SAME_STMT);
1903 tree ckind = build_int_cst (unsigned_char_type_node,
1904 is_lhs ? UBSAN_STORE_OF : UBSAN_LOAD_OF);
1905 gimple *g = gimple_build_call_internal (IFN_UBSAN_OBJECT_SIZE, 4,
1906 ptr, t, sizet, ckind);
1907 gimple_set_location (g, loc);
1908 gsi_insert_before (gsi, g, GSI_SAME_STMT);
1911 /* True if we want to play UBSan games in the current function. */
1913 bool
1914 do_ubsan_in_current_function ()
1916 return (current_function_decl != NULL_TREE
1917 && !lookup_attribute ("no_sanitize_undefined",
1918 DECL_ATTRIBUTES (current_function_decl)));
1921 namespace {
1923 const pass_data pass_data_ubsan =
1925 GIMPLE_PASS, /* type */
1926 "ubsan", /* name */
1927 OPTGROUP_NONE, /* optinfo_flags */
1928 TV_TREE_UBSAN, /* tv_id */
1929 ( PROP_cfg | PROP_ssa ), /* properties_required */
1930 0, /* properties_provided */
1931 0, /* properties_destroyed */
1932 0, /* todo_flags_start */
1933 TODO_update_ssa, /* todo_flags_finish */
1936 class pass_ubsan : public gimple_opt_pass
1938 public:
1939 pass_ubsan (gcc::context *ctxt)
1940 : gimple_opt_pass (pass_data_ubsan, ctxt)
1943 /* opt_pass methods: */
1944 virtual bool gate (function *)
1946 return flag_sanitize & (SANITIZE_NULL | SANITIZE_SI_OVERFLOW
1947 | SANITIZE_BOOL | SANITIZE_ENUM
1948 | SANITIZE_ALIGNMENT
1949 | SANITIZE_NONNULL_ATTRIBUTE
1950 | SANITIZE_RETURNS_NONNULL_ATTRIBUTE
1951 | SANITIZE_OBJECT_SIZE)
1952 && do_ubsan_in_current_function ();
1955 virtual unsigned int execute (function *);
1957 }; // class pass_ubsan
1959 unsigned int
1960 pass_ubsan::execute (function *fun)
1962 basic_block bb;
1963 gimple_stmt_iterator gsi;
1964 unsigned int ret = 0;
1966 initialize_sanitizer_builtins ();
1968 FOR_EACH_BB_FN (bb, fun)
1970 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
1972 gimple *stmt = gsi_stmt (gsi);
1973 if (is_gimple_debug (stmt) || gimple_clobber_p (stmt))
1975 gsi_next (&gsi);
1976 continue;
1979 if ((flag_sanitize & SANITIZE_SI_OVERFLOW)
1980 && is_gimple_assign (stmt))
1981 instrument_si_overflow (gsi);
1983 if (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))
1985 if (gimple_store_p (stmt))
1986 instrument_null (gsi, true);
1987 if (gimple_assign_load_p (stmt))
1988 instrument_null (gsi, false);
1991 if (flag_sanitize & (SANITIZE_BOOL | SANITIZE_ENUM)
1992 && gimple_assign_load_p (stmt))
1994 instrument_bool_enum_load (&gsi);
1995 bb = gimple_bb (stmt);
1998 if ((flag_sanitize & SANITIZE_NONNULL_ATTRIBUTE)
1999 && is_gimple_call (stmt)
2000 && !gimple_call_internal_p (stmt))
2002 instrument_nonnull_arg (&gsi);
2003 bb = gimple_bb (stmt);
2006 if ((flag_sanitize & SANITIZE_RETURNS_NONNULL_ATTRIBUTE)
2007 && gimple_code (stmt) == GIMPLE_RETURN)
2009 instrument_nonnull_return (&gsi);
2010 bb = gimple_bb (stmt);
2013 if (flag_sanitize & SANITIZE_OBJECT_SIZE)
2015 if (gimple_store_p (stmt))
2016 instrument_object_size (&gsi, true);
2017 if (gimple_assign_load_p (stmt))
2018 instrument_object_size (&gsi, false);
2021 gsi_next (&gsi);
2023 if (gimple_purge_dead_eh_edges (bb))
2024 ret = TODO_cleanup_cfg;
2026 return ret;
2029 } // anon namespace
2031 gimple_opt_pass *
2032 make_pass_ubsan (gcc::context *ctxt)
2034 return new pass_ubsan (ctxt);
2037 #include "gt-ubsan.h"