1 /* UndefinedBehaviorSanitizer, undefined behavior detector.
2 Copyright (C) 2013-2017 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
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
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/>. */
23 #include "coretypes.h"
25 #include "c-family/c-common.h"
27 #include "c-family/c-ubsan.h"
28 #include "stor-layout.h"
31 #include "stringpool.h"
35 /* Instrument division by zero and INT_MIN / -1. If not instrumenting,
39 ubsan_instrument_division (location_t loc
, tree op0
, tree op1
)
42 tree type
= TREE_TYPE (op0
);
44 /* At this point both operands should have the same type,
45 because they are already converted to RESULT_TYPE.
46 Use TYPE_MAIN_VARIANT since typedefs can confuse us. */
47 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (op0
))
48 == TYPE_MAIN_VARIANT (TREE_TYPE (op1
)));
50 op0
= unshare_expr (op0
);
51 op1
= unshare_expr (op1
);
53 if (TREE_CODE (type
) == INTEGER_TYPE
54 && sanitize_flags_p (SANITIZE_DIVIDE
))
55 t
= fold_build2 (EQ_EXPR
, boolean_type_node
,
56 op1
, build_int_cst (type
, 0));
57 else if (TREE_CODE (type
) == REAL_TYPE
58 && sanitize_flags_p (SANITIZE_FLOAT_DIVIDE
))
59 t
= fold_build2 (EQ_EXPR
, boolean_type_node
,
60 op1
, build_real (type
, dconst0
));
64 /* We check INT_MIN / -1 only for signed types. */
65 if (TREE_CODE (type
) == INTEGER_TYPE
66 && sanitize_flags_p (SANITIZE_DIVIDE
)
67 && !TYPE_UNSIGNED (type
))
70 tt
= fold_build2 (EQ_EXPR
, boolean_type_node
, unshare_expr (op1
),
71 build_int_cst (type
, -1));
72 x
= fold_build2 (EQ_EXPR
, boolean_type_node
, op0
,
73 TYPE_MIN_VALUE (type
));
74 x
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
, x
, tt
);
75 t
= fold_build2 (TRUTH_OR_EXPR
, boolean_type_node
, t
, x
);
78 /* If the condition was folded to 0, no need to instrument
80 if (integer_zerop (t
))
83 /* In case we have a SAVE_EXPR in a conditional context, we need to
84 make sure it gets evaluated before the condition. */
85 t
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (t
), unshare_expr (op0
), t
);
86 t
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (t
), unshare_expr (op1
), t
);
87 if (flag_sanitize_undefined_trap_on_error
)
88 tt
= build_call_expr_loc (loc
, builtin_decl_explicit (BUILT_IN_TRAP
), 0);
91 tree data
= ubsan_create_data ("__ubsan_overflow_data", 1, &loc
,
92 ubsan_type_descriptor (type
), NULL_TREE
,
94 data
= build_fold_addr_expr_loc (loc
, data
);
95 enum built_in_function bcode
96 = (flag_sanitize_recover
& SANITIZE_DIVIDE
)
97 ? BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW
98 : BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW_ABORT
;
99 tt
= builtin_decl_explicit (bcode
);
100 op0
= unshare_expr (op0
);
101 op1
= unshare_expr (op1
);
102 tt
= build_call_expr_loc (loc
, tt
, 3, data
, ubsan_encode_value (op0
),
103 ubsan_encode_value (op1
));
105 t
= fold_build3 (COND_EXPR
, void_type_node
, t
, tt
, void_node
);
110 /* Instrument left and right shifts. */
113 ubsan_instrument_shift (location_t loc
, enum tree_code code
,
116 tree t
, tt
= NULL_TREE
;
117 tree type0
= TREE_TYPE (op0
);
118 tree type1
= TREE_TYPE (op1
);
119 if (!INTEGRAL_TYPE_P (type0
))
122 tree op1_utype
= unsigned_type_for (type1
);
123 HOST_WIDE_INT op0_prec
= TYPE_PRECISION (type0
);
124 tree uprecm1
= build_int_cst (op1_utype
, op0_prec
- 1);
126 op0
= unshare_expr (op0
);
127 op1
= unshare_expr (op1
);
129 t
= fold_convert_loc (loc
, op1_utype
, op1
);
130 t
= fold_build2 (GT_EXPR
, boolean_type_node
, t
, uprecm1
);
132 /* If this is not a signed operation, don't perform overflow checks.
133 Also punt on bit-fields. */
134 if (TYPE_OVERFLOW_WRAPS (type0
)
135 || GET_MODE_BITSIZE (TYPE_MODE (type0
)) != TYPE_PRECISION (type0
)
136 || !sanitize_flags_p (SANITIZE_SHIFT_BASE
))
139 /* For signed x << y, in C99/C11, the following:
140 (unsigned) x >> (uprecm1 - y)
141 if non-zero, is undefined. */
142 else if (code
== LSHIFT_EXPR
&& flag_isoc99
&& cxx_dialect
< cxx11
)
144 tree x
= fold_build2 (MINUS_EXPR
, op1_utype
, uprecm1
,
145 fold_convert (op1_utype
, unshare_expr (op1
)));
146 tt
= fold_convert_loc (loc
, unsigned_type_for (type0
), op0
);
147 tt
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (tt
), tt
, x
);
148 tt
= fold_build2 (NE_EXPR
, boolean_type_node
, tt
,
149 build_int_cst (TREE_TYPE (tt
), 0));
152 /* For signed x << y, in C++11 and later, the following:
153 x < 0 || ((unsigned) x >> (uprecm1 - y))
154 if > 1, is undefined. */
155 else if (code
== LSHIFT_EXPR
&& cxx_dialect
>= cxx11
)
157 tree x
= fold_build2 (MINUS_EXPR
, op1_utype
, uprecm1
,
158 fold_convert (op1_utype
, unshare_expr (op1
)));
159 tt
= fold_convert_loc (loc
, unsigned_type_for (type0
),
161 tt
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (tt
), tt
, x
);
162 tt
= fold_build2 (GT_EXPR
, boolean_type_node
, tt
,
163 build_int_cst (TREE_TYPE (tt
), 1));
164 x
= fold_build2 (LT_EXPR
, boolean_type_node
, unshare_expr (op0
),
165 build_int_cst (type0
, 0));
166 tt
= fold_build2 (TRUTH_OR_EXPR
, boolean_type_node
, x
, tt
);
169 /* If the condition was folded to 0, no need to instrument
171 if (integer_zerop (t
) && (tt
== NULL_TREE
|| integer_zerop (tt
)))
174 /* In case we have a SAVE_EXPR in a conditional context, we need to
175 make sure it gets evaluated before the condition. */
176 t
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (t
), unshare_expr (op0
), t
);
177 t
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (t
), unshare_expr (op1
), t
);
179 enum sanitize_code recover_kind
= SANITIZE_SHIFT_EXPONENT
;
180 tree else_t
= void_node
;
183 if (!sanitize_flags_p (SANITIZE_SHIFT_EXPONENT
))
185 t
= fold_build1 (TRUTH_NOT_EXPR
, boolean_type_node
, t
);
186 t
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
, t
, tt
);
187 recover_kind
= SANITIZE_SHIFT_BASE
;
191 if (flag_sanitize_undefined_trap_on_error
192 || ((!(flag_sanitize_recover
& SANITIZE_SHIFT_EXPONENT
))
193 == (!(flag_sanitize_recover
& SANITIZE_SHIFT_BASE
))))
194 t
= fold_build2 (TRUTH_OR_EXPR
, boolean_type_node
, t
, tt
);
200 if (flag_sanitize_undefined_trap_on_error
)
201 tt
= build_call_expr_loc (loc
, builtin_decl_explicit (BUILT_IN_TRAP
), 0);
204 tree data
= ubsan_create_data ("__ubsan_shift_data", 1, &loc
,
205 ubsan_type_descriptor (type0
),
206 ubsan_type_descriptor (type1
), NULL_TREE
,
208 data
= build_fold_addr_expr_loc (loc
, data
);
210 enum built_in_function bcode
211 = (flag_sanitize_recover
& recover_kind
)
212 ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
213 : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT
;
214 tt
= builtin_decl_explicit (bcode
);
215 op0
= unshare_expr (op0
);
216 op1
= unshare_expr (op1
);
217 tt
= build_call_expr_loc (loc
, tt
, 3, data
, ubsan_encode_value (op0
),
218 ubsan_encode_value (op1
));
219 if (else_t
!= void_node
)
221 bcode
= (flag_sanitize_recover
& SANITIZE_SHIFT_BASE
)
222 ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
223 : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT
;
224 tree else_tt
= builtin_decl_explicit (bcode
);
225 op0
= unshare_expr (op0
);
226 op1
= unshare_expr (op1
);
227 else_tt
= build_call_expr_loc (loc
, else_tt
, 3, data
,
228 ubsan_encode_value (op0
),
229 ubsan_encode_value (op1
));
230 else_t
= fold_build3 (COND_EXPR
, void_type_node
, else_t
,
234 t
= fold_build3 (COND_EXPR
, void_type_node
, t
, tt
, else_t
);
239 /* Instrument variable length array bound. */
242 ubsan_instrument_vla (location_t loc
, tree size
)
244 tree type
= TREE_TYPE (size
);
247 t
= fold_build2 (LE_EXPR
, boolean_type_node
, size
, build_int_cst (type
, 0));
248 if (flag_sanitize_undefined_trap_on_error
)
249 tt
= build_call_expr_loc (loc
, builtin_decl_explicit (BUILT_IN_TRAP
), 0);
252 tree data
= ubsan_create_data ("__ubsan_vla_data", 1, &loc
,
253 ubsan_type_descriptor (type
), NULL_TREE
,
255 data
= build_fold_addr_expr_loc (loc
, data
);
256 enum built_in_function bcode
257 = (flag_sanitize_recover
& SANITIZE_VLA
)
258 ? BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE
259 : BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE_ABORT
;
260 tt
= builtin_decl_explicit (bcode
);
261 tt
= build_call_expr_loc (loc
, tt
, 2, data
, ubsan_encode_value (size
));
263 t
= fold_build3 (COND_EXPR
, void_type_node
, t
, tt
, void_node
);
268 /* Instrument missing return in C++ functions returning non-void. */
271 ubsan_instrument_return (location_t loc
)
273 if (flag_sanitize_undefined_trap_on_error
)
274 return build_call_expr_loc (loc
, builtin_decl_explicit (BUILT_IN_TRAP
), 0);
276 tree data
= ubsan_create_data ("__ubsan_missing_return_data", 1, &loc
,
277 NULL_TREE
, NULL_TREE
);
278 tree t
= builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_MISSING_RETURN
);
279 return build_call_expr_loc (loc
, t
, 1, build_fold_addr_expr_loc (loc
, data
));
282 /* Instrument array bounds for ARRAY_REFs. We create special builtin,
283 that gets expanded in the sanopt pass, and make an array dimension
284 of it. ARRAY is the array, *INDEX is an index to the array.
285 Return NULL_TREE if no instrumentation is emitted.
286 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
289 ubsan_instrument_bounds (location_t loc
, tree array
, tree
*index
,
290 bool ignore_off_by_one
)
292 tree type
= TREE_TYPE (array
);
293 tree domain
= TYPE_DOMAIN (type
);
295 if (domain
== NULL_TREE
|| TYPE_MAX_VALUE (domain
) == NULL_TREE
)
298 tree bound
= TYPE_MAX_VALUE (domain
);
299 if (ignore_off_by_one
)
300 bound
= fold_build2 (PLUS_EXPR
, TREE_TYPE (bound
), bound
,
301 build_int_cst (TREE_TYPE (bound
), 1));
303 /* Detect flexible array members and suchlike, unless
304 -fsanitize=bounds-strict. */
305 tree base
= get_base_address (array
);
306 if (!sanitize_flags_p (SANITIZE_BOUNDS_STRICT
)
307 && TREE_CODE (array
) == COMPONENT_REF
308 && base
&& (INDIRECT_REF_P (base
) || TREE_CODE (base
) == MEM_REF
))
310 tree next
= NULL_TREE
;
313 /* Walk all structs/unions. */
314 while (TREE_CODE (cref
) == COMPONENT_REF
)
316 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
317 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
318 next
&& TREE_CODE (next
) != FIELD_DECL
;
319 next
= DECL_CHAIN (next
))
322 /* Not a last element. Instrument it. */
324 /* Ok, this is the last field of the structure/union. But the
325 aggregate containing the field must be the last field too,
327 cref
= TREE_OPERAND (cref
, 0);
330 /* Don't instrument this flexible array member-like array in non-strict
331 -fsanitize=bounds mode. */
335 /* Don't emit instrumentation in the most common cases. */
336 tree idx
= NULL_TREE
;
337 if (TREE_CODE (*index
) == INTEGER_CST
)
339 else if (TREE_CODE (*index
) == BIT_AND_EXPR
340 && TREE_CODE (TREE_OPERAND (*index
, 1)) == INTEGER_CST
)
341 idx
= TREE_OPERAND (*index
, 1);
343 && TREE_CODE (bound
) == INTEGER_CST
344 && tree_int_cst_sgn (idx
) >= 0
345 && tree_int_cst_le (idx
, bound
))
348 *index
= save_expr (*index
);
349 /* Create a "(T *) 0" tree node to describe the array type. */
350 tree zero_with_type
= build_int_cst (build_pointer_type (type
), 0);
351 return build_call_expr_internal_loc (loc
, IFN_UBSAN_BOUNDS
,
352 void_type_node
, 3, zero_with_type
,
356 /* Return true iff T is an array that was instrumented by SANITIZE_BOUNDS. */
359 ubsan_array_ref_instrumented_p (const_tree t
)
361 if (TREE_CODE (t
) != ARRAY_REF
)
364 tree op1
= TREE_OPERAND (t
, 1);
365 return TREE_CODE (op1
) == COMPOUND_EXPR
366 && TREE_CODE (TREE_OPERAND (op1
, 0)) == CALL_EXPR
367 && CALL_EXPR_FN (TREE_OPERAND (op1
, 0)) == NULL_TREE
368 && CALL_EXPR_IFN (TREE_OPERAND (op1
, 0)) == IFN_UBSAN_BOUNDS
;
371 /* Instrument an ARRAY_REF, if it hasn't already been instrumented.
372 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
375 ubsan_maybe_instrument_array_ref (tree
*expr_p
, bool ignore_off_by_one
)
377 if (!ubsan_array_ref_instrumented_p (*expr_p
)
378 && sanitize_flags_p (SANITIZE_BOUNDS
| SANITIZE_BOUNDS_STRICT
)
379 && current_function_decl
!= NULL_TREE
)
381 tree op0
= TREE_OPERAND (*expr_p
, 0);
382 tree op1
= TREE_OPERAND (*expr_p
, 1);
383 tree e
= ubsan_instrument_bounds (EXPR_LOCATION (*expr_p
), op0
, &op1
,
387 tree t
= copy_node (*expr_p
);
388 TREE_OPERAND (t
, 1) = build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
396 ubsan_maybe_instrument_reference_or_call (location_t loc
, tree op
, tree ptype
,
397 enum ubsan_null_ckind ckind
)
399 if (!sanitize_flags_p (SANITIZE_ALIGNMENT
| SANITIZE_NULL
)
400 || current_function_decl
== NULL_TREE
)
403 tree type
= TREE_TYPE (ptype
);
405 bool instrument
= false;
406 unsigned int mina
= 0;
408 if (sanitize_flags_p (SANITIZE_ALIGNMENT
))
410 mina
= min_align_of_type (type
);
414 while ((TREE_CODE (op
) == NOP_EXPR
415 || TREE_CODE (op
) == NON_LVALUE_EXPR
)
416 && TREE_CODE (TREE_TYPE (op
)) == POINTER_TYPE
)
417 op
= TREE_OPERAND (op
, 0);
418 if (TREE_CODE (op
) == NOP_EXPR
419 && TREE_CODE (TREE_TYPE (op
)) == REFERENCE_TYPE
)
421 if (mina
&& mina
> min_align_of_type (TREE_TYPE (TREE_TYPE (op
))))
426 if (sanitize_flags_p (SANITIZE_NULL
) && TREE_CODE (op
) == ADDR_EXPR
)
428 bool strict_overflow_p
= false;
429 /* tree_single_nonzero_warnv_p will not return true for non-weak
430 non-automatic decls with -fno-delete-null-pointer-checks,
431 which is disabled during -fsanitize=null. We don't want to
432 instrument those, just weak vars though. */
433 int save_flag_delete_null_pointer_checks
434 = flag_delete_null_pointer_checks
;
435 flag_delete_null_pointer_checks
= 1;
436 if (!tree_single_nonzero_warnv_p (op
, &strict_overflow_p
)
437 || strict_overflow_p
)
439 flag_delete_null_pointer_checks
440 = save_flag_delete_null_pointer_checks
;
442 else if (sanitize_flags_p (SANITIZE_NULL
))
444 if (mina
&& mina
> 1)
446 if (!POINTER_TYPE_P (TREE_TYPE (op
))
447 || mina
> get_pointer_alignment (op
) / BITS_PER_UNIT
)
453 op
= save_expr (orig_op
);
454 gcc_assert (POINTER_TYPE_P (ptype
));
455 if (TREE_CODE (ptype
) == REFERENCE_TYPE
)
456 ptype
= build_pointer_type (TREE_TYPE (ptype
));
457 tree kind
= build_int_cst (ptype
, ckind
);
458 tree align
= build_int_cst (pointer_sized_int_node
, mina
);
460 = build_call_expr_internal_loc (loc
, IFN_UBSAN_NULL
, void_type_node
,
462 TREE_SIDE_EFFECTS (call
) = 1;
463 return fold_build2 (COMPOUND_EXPR
, TREE_TYPE (op
), call
, op
);
466 /* Instrument a NOP_EXPR to REFERENCE_TYPE or INTEGER_CST with REFERENCE_TYPE
470 ubsan_maybe_instrument_reference (tree
*stmt_p
)
474 if (TREE_CODE (stmt
) == NOP_EXPR
)
475 op
= TREE_OPERAND (stmt
, 0);
476 op
= ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt
), op
,
481 if (TREE_CODE (stmt
) == NOP_EXPR
)
482 TREE_OPERAND (stmt
, 0) = op
;
488 /* Instrument a CALL_EXPR to a method if needed. */
491 ubsan_maybe_instrument_member_call (tree stmt
, bool is_ctor
)
493 if (call_expr_nargs (stmt
) == 0)
495 tree op
= CALL_EXPR_ARG (stmt
, 0);
496 if (op
== error_mark_node
497 || !POINTER_TYPE_P (TREE_TYPE (op
)))
499 op
= ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt
), op
,
501 is_ctor
? UBSAN_CTOR_CALL
502 : UBSAN_MEMBER_CALL
);
504 CALL_EXPR_ARG (stmt
, 0) = op
;