2017-03-17 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / c-family / c-ubsan.c
blob36aa919a872f59c15aa062708e02e17ccc9e9d24
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
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 "tm.h"
25 #include "c-family/c-common.h"
26 #include "ubsan.h"
27 #include "c-family/c-ubsan.h"
28 #include "asan.h"
29 #include "stor-layout.h"
30 #include "builtins.h"
31 #include "gimplify.h"
33 /* Instrument division by zero and INT_MIN / -1. If not instrumenting,
34 return NULL_TREE. */
36 tree
37 ubsan_instrument_division (location_t loc, tree op0, tree op1)
39 tree t, tt;
40 tree type = TREE_TYPE (op0);
42 /* At this point both operands should have the same type,
43 because they are already converted to RESULT_TYPE.
44 Use TYPE_MAIN_VARIANT since typedefs can confuse us. */
45 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (op0))
46 == TYPE_MAIN_VARIANT (TREE_TYPE (op1)));
48 op0 = unshare_expr (op0);
49 op1 = unshare_expr (op1);
51 if (TREE_CODE (type) == INTEGER_TYPE
52 && (flag_sanitize & SANITIZE_DIVIDE))
53 t = fold_build2 (EQ_EXPR, boolean_type_node,
54 op1, build_int_cst (type, 0));
55 else if (TREE_CODE (type) == REAL_TYPE
56 && (flag_sanitize & SANITIZE_FLOAT_DIVIDE))
57 t = fold_build2 (EQ_EXPR, boolean_type_node,
58 op1, build_real (type, dconst0));
59 else
60 return NULL_TREE;
62 /* We check INT_MIN / -1 only for signed types. */
63 if (TREE_CODE (type) == INTEGER_TYPE
64 && (flag_sanitize & SANITIZE_DIVIDE)
65 && !TYPE_UNSIGNED (type))
67 tree x;
68 tt = fold_build2 (EQ_EXPR, boolean_type_node, unshare_expr (op1),
69 build_int_cst (type, -1));
70 x = fold_build2 (EQ_EXPR, boolean_type_node, op0,
71 TYPE_MIN_VALUE (type));
72 x = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, x, tt);
73 t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, x);
76 /* If the condition was folded to 0, no need to instrument
77 this expression. */
78 if (integer_zerop (t))
79 return NULL_TREE;
81 /* In case we have a SAVE_EXPR in a conditional context, we need to
82 make sure it gets evaluated before the condition. */
83 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op0), t);
84 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op1), t);
85 if (flag_sanitize_undefined_trap_on_error)
86 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
87 else
89 tree data = ubsan_create_data ("__ubsan_overflow_data", 1, &loc,
90 ubsan_type_descriptor (type), NULL_TREE,
91 NULL_TREE);
92 data = build_fold_addr_expr_loc (loc, data);
93 enum built_in_function bcode
94 = (flag_sanitize_recover & SANITIZE_DIVIDE)
95 ? BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW
96 : BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW_ABORT;
97 tt = builtin_decl_explicit (bcode);
98 op0 = unshare_expr (op0);
99 op1 = unshare_expr (op1);
100 tt = build_call_expr_loc (loc, tt, 3, data, ubsan_encode_value (op0),
101 ubsan_encode_value (op1));
103 t = fold_build3 (COND_EXPR, void_type_node, t, tt, void_node);
105 return t;
108 /* Instrument left and right shifts. */
110 tree
111 ubsan_instrument_shift (location_t loc, enum tree_code code,
112 tree op0, tree op1)
114 tree t, tt = NULL_TREE;
115 tree type0 = TREE_TYPE (op0);
116 tree type1 = TREE_TYPE (op1);
117 if (!INTEGRAL_TYPE_P (type0))
118 return NULL_TREE;
120 tree op1_utype = unsigned_type_for (type1);
121 HOST_WIDE_INT op0_prec = TYPE_PRECISION (type0);
122 tree uprecm1 = build_int_cst (op1_utype, op0_prec - 1);
124 op0 = unshare_expr (op0);
125 op1 = unshare_expr (op1);
127 t = fold_convert_loc (loc, op1_utype, op1);
128 t = fold_build2 (GT_EXPR, boolean_type_node, t, uprecm1);
130 /* If this is not a signed operation, don't perform overflow checks.
131 Also punt on bit-fields. */
132 if (TYPE_OVERFLOW_WRAPS (type0)
133 || GET_MODE_BITSIZE (TYPE_MODE (type0)) != TYPE_PRECISION (type0)
134 || (flag_sanitize & SANITIZE_SHIFT_BASE) == 0)
137 /* For signed x << y, in C99/C11, the following:
138 (unsigned) x >> (uprecm1 - y)
139 if non-zero, is undefined. */
140 else if (code == LSHIFT_EXPR && flag_isoc99 && cxx_dialect < cxx11)
142 tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
143 fold_convert (op1_utype, unshare_expr (op1)));
144 tt = fold_convert_loc (loc, unsigned_type_for (type0), op0);
145 tt = fold_build2 (RSHIFT_EXPR, TREE_TYPE (tt), tt, x);
146 tt = fold_build2 (NE_EXPR, boolean_type_node, tt,
147 build_int_cst (TREE_TYPE (tt), 0));
150 /* For signed x << y, in C++11 and later, the following:
151 x < 0 || ((unsigned) x >> (uprecm1 - y))
152 if > 1, is undefined. */
153 else if (code == LSHIFT_EXPR && cxx_dialect >= cxx11)
155 tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
156 fold_convert (op1_utype, unshare_expr (op1)));
157 tt = fold_convert_loc (loc, unsigned_type_for (type0),
158 unshare_expr (op0));
159 tt = fold_build2 (RSHIFT_EXPR, TREE_TYPE (tt), tt, x);
160 tt = fold_build2 (GT_EXPR, boolean_type_node, tt,
161 build_int_cst (TREE_TYPE (tt), 1));
162 x = fold_build2 (LT_EXPR, boolean_type_node, unshare_expr (op0),
163 build_int_cst (type0, 0));
164 tt = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, x, tt);
167 /* If the condition was folded to 0, no need to instrument
168 this expression. */
169 if (integer_zerop (t) && (tt == NULL_TREE || integer_zerop (tt)))
170 return NULL_TREE;
172 /* In case we have a SAVE_EXPR in a conditional context, we need to
173 make sure it gets evaluated before the condition. */
174 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op0), t);
176 enum sanitize_code recover_kind = SANITIZE_SHIFT_EXPONENT;
177 tree else_t = void_node;
178 if (tt)
180 if ((flag_sanitize & SANITIZE_SHIFT_EXPONENT) == 0)
182 t = fold_build1 (TRUTH_NOT_EXPR, boolean_type_node, t);
183 t = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, t, tt);
184 recover_kind = SANITIZE_SHIFT_BASE;
186 else
188 if (flag_sanitize_undefined_trap_on_error
189 || ((!(flag_sanitize_recover & SANITIZE_SHIFT_EXPONENT))
190 == (!(flag_sanitize_recover & SANITIZE_SHIFT_BASE))))
191 t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, tt);
192 else
193 else_t = tt;
197 if (flag_sanitize_undefined_trap_on_error)
198 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
199 else
201 tree data = ubsan_create_data ("__ubsan_shift_data", 1, &loc,
202 ubsan_type_descriptor (type0),
203 ubsan_type_descriptor (type1), NULL_TREE,
204 NULL_TREE);
205 data = build_fold_addr_expr_loc (loc, data);
207 enum built_in_function bcode
208 = (flag_sanitize_recover & recover_kind)
209 ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
210 : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT;
211 tt = builtin_decl_explicit (bcode);
212 op0 = unshare_expr (op0);
213 op1 = unshare_expr (op1);
214 tt = build_call_expr_loc (loc, tt, 3, data, ubsan_encode_value (op0),
215 ubsan_encode_value (op1));
216 if (else_t != void_node)
218 bcode = (flag_sanitize_recover & SANITIZE_SHIFT_BASE)
219 ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
220 : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT;
221 tree else_tt = builtin_decl_explicit (bcode);
222 op0 = unshare_expr (op0);
223 op1 = unshare_expr (op1);
224 else_tt = build_call_expr_loc (loc, else_tt, 3, data,
225 ubsan_encode_value (op0),
226 ubsan_encode_value (op1));
227 else_t = fold_build3 (COND_EXPR, void_type_node, else_t,
228 else_tt, void_node);
231 t = fold_build3 (COND_EXPR, void_type_node, t, tt, else_t);
233 return t;
236 /* Instrument variable length array bound. */
238 tree
239 ubsan_instrument_vla (location_t loc, tree size)
241 tree type = TREE_TYPE (size);
242 tree t, tt;
244 t = fold_build2 (LE_EXPR, boolean_type_node, size, build_int_cst (type, 0));
245 if (flag_sanitize_undefined_trap_on_error)
246 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
247 else
249 tree data = ubsan_create_data ("__ubsan_vla_data", 1, &loc,
250 ubsan_type_descriptor (type), NULL_TREE,
251 NULL_TREE);
252 data = build_fold_addr_expr_loc (loc, data);
253 enum built_in_function bcode
254 = (flag_sanitize_recover & SANITIZE_VLA)
255 ? BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE
256 : BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE_ABORT;
257 tt = builtin_decl_explicit (bcode);
258 tt = build_call_expr_loc (loc, tt, 2, data, ubsan_encode_value (size));
260 t = fold_build3 (COND_EXPR, void_type_node, t, tt, void_node);
262 return t;
265 /* Instrument missing return in C++ functions returning non-void. */
267 tree
268 ubsan_instrument_return (location_t loc)
270 if (flag_sanitize_undefined_trap_on_error)
271 return build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
273 tree data = ubsan_create_data ("__ubsan_missing_return_data", 1, &loc,
274 NULL_TREE, NULL_TREE);
275 tree t = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_MISSING_RETURN);
276 return build_call_expr_loc (loc, t, 1, build_fold_addr_expr_loc (loc, data));
279 /* Instrument array bounds for ARRAY_REFs. We create special builtin,
280 that gets expanded in the sanopt pass, and make an array dimension
281 of it. ARRAY is the array, *INDEX is an index to the array.
282 Return NULL_TREE if no instrumentation is emitted.
283 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
285 tree
286 ubsan_instrument_bounds (location_t loc, tree array, tree *index,
287 bool ignore_off_by_one)
289 tree type = TREE_TYPE (array);
290 tree domain = TYPE_DOMAIN (type);
292 if (domain == NULL_TREE || TYPE_MAX_VALUE (domain) == NULL_TREE)
293 return NULL_TREE;
295 tree bound = TYPE_MAX_VALUE (domain);
296 if (ignore_off_by_one)
297 bound = fold_build2 (PLUS_EXPR, TREE_TYPE (bound), bound,
298 build_int_cst (TREE_TYPE (bound), 1));
300 /* Detect flexible array members and suchlike, unless
301 -fsanitize=bounds-strict. */
302 tree base = get_base_address (array);
303 if ((flag_sanitize & SANITIZE_BOUNDS_STRICT) == 0
304 && TREE_CODE (array) == COMPONENT_REF
305 && base && (INDIRECT_REF_P (base) || TREE_CODE (base) == MEM_REF))
307 tree next = NULL_TREE;
308 tree cref = array;
310 /* Walk all structs/unions. */
311 while (TREE_CODE (cref) == COMPONENT_REF)
313 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
314 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
315 next && TREE_CODE (next) != FIELD_DECL;
316 next = DECL_CHAIN (next))
318 if (next)
319 /* Not a last element. Instrument it. */
320 break;
321 /* Ok, this is the last field of the structure/union. But the
322 aggregate containing the field must be the last field too,
323 recursively. */
324 cref = TREE_OPERAND (cref, 0);
326 if (!next)
327 /* Don't instrument this flexible array member-like array in non-strict
328 -fsanitize=bounds mode. */
329 return NULL_TREE;
332 /* Don't emit instrumentation in the most common cases. */
333 tree idx = NULL_TREE;
334 if (TREE_CODE (*index) == INTEGER_CST)
335 idx = *index;
336 else if (TREE_CODE (*index) == BIT_AND_EXPR
337 && TREE_CODE (TREE_OPERAND (*index, 1)) == INTEGER_CST)
338 idx = TREE_OPERAND (*index, 1);
339 if (idx
340 && TREE_CODE (bound) == INTEGER_CST
341 && tree_int_cst_sgn (idx) >= 0
342 && tree_int_cst_le (idx, bound))
343 return NULL_TREE;
345 *index = save_expr (*index);
346 /* Create a "(T *) 0" tree node to describe the array type. */
347 tree zero_with_type = build_int_cst (build_pointer_type (type), 0);
348 return build_call_expr_internal_loc (loc, IFN_UBSAN_BOUNDS,
349 void_type_node, 3, zero_with_type,
350 *index, bound);
353 /* Return true iff T is an array that was instrumented by SANITIZE_BOUNDS. */
355 bool
356 ubsan_array_ref_instrumented_p (const_tree t)
358 if (TREE_CODE (t) != ARRAY_REF)
359 return false;
361 tree op1 = TREE_OPERAND (t, 1);
362 return TREE_CODE (op1) == COMPOUND_EXPR
363 && TREE_CODE (TREE_OPERAND (op1, 0)) == CALL_EXPR
364 && CALL_EXPR_FN (TREE_OPERAND (op1, 0)) == NULL_TREE
365 && CALL_EXPR_IFN (TREE_OPERAND (op1, 0)) == IFN_UBSAN_BOUNDS;
368 /* Instrument an ARRAY_REF, if it hasn't already been instrumented.
369 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
371 void
372 ubsan_maybe_instrument_array_ref (tree *expr_p, bool ignore_off_by_one)
374 if (!ubsan_array_ref_instrumented_p (*expr_p)
375 && do_ubsan_in_current_function ())
377 tree op0 = TREE_OPERAND (*expr_p, 0);
378 tree op1 = TREE_OPERAND (*expr_p, 1);
379 tree e = ubsan_instrument_bounds (EXPR_LOCATION (*expr_p), op0, &op1,
380 ignore_off_by_one);
381 if (e != NULL_TREE)
383 tree t = copy_node (*expr_p);
384 TREE_OPERAND (t, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (op1),
385 e, op1);
386 *expr_p = t;
391 static tree
392 ubsan_maybe_instrument_reference_or_call (location_t loc, tree op, tree ptype,
393 enum ubsan_null_ckind ckind)
395 if (!do_ubsan_in_current_function ())
396 return NULL_TREE;
398 tree type = TREE_TYPE (ptype);
399 tree orig_op = op;
400 bool instrument = false;
401 unsigned int mina = 0;
403 if (flag_sanitize & SANITIZE_ALIGNMENT)
405 mina = min_align_of_type (type);
406 if (mina <= 1)
407 mina = 0;
409 while ((TREE_CODE (op) == NOP_EXPR
410 || TREE_CODE (op) == NON_LVALUE_EXPR)
411 && TREE_CODE (TREE_TYPE (op)) == POINTER_TYPE)
412 op = TREE_OPERAND (op, 0);
413 if (TREE_CODE (op) == NOP_EXPR
414 && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE)
416 if (mina && mina > min_align_of_type (TREE_TYPE (TREE_TYPE (op))))
417 instrument = true;
419 else
421 if ((flag_sanitize & SANITIZE_NULL) && TREE_CODE (op) == ADDR_EXPR)
423 bool strict_overflow_p = false;
424 /* tree_single_nonzero_warnv_p will not return true for non-weak
425 non-automatic decls with -fno-delete-null-pointer-checks,
426 which is disabled during -fsanitize=null. We don't want to
427 instrument those, just weak vars though. */
428 int save_flag_delete_null_pointer_checks
429 = flag_delete_null_pointer_checks;
430 flag_delete_null_pointer_checks = 1;
431 if (!tree_single_nonzero_warnv_p (op, &strict_overflow_p)
432 || strict_overflow_p)
433 instrument = true;
434 flag_delete_null_pointer_checks
435 = save_flag_delete_null_pointer_checks;
437 else if (flag_sanitize & SANITIZE_NULL)
438 instrument = true;
439 if (mina && mina > 1)
441 if (!POINTER_TYPE_P (TREE_TYPE (op))
442 || mina > get_pointer_alignment (op) / BITS_PER_UNIT)
443 instrument = true;
446 if (!instrument)
447 return NULL_TREE;
448 op = save_expr (orig_op);
449 gcc_assert (POINTER_TYPE_P (ptype));
450 if (TREE_CODE (ptype) == REFERENCE_TYPE)
451 ptype = build_pointer_type (TREE_TYPE (ptype));
452 tree kind = build_int_cst (ptype, ckind);
453 tree align = build_int_cst (pointer_sized_int_node, mina);
454 tree call
455 = build_call_expr_internal_loc (loc, IFN_UBSAN_NULL, void_type_node,
456 3, op, kind, align);
457 TREE_SIDE_EFFECTS (call) = 1;
458 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (op), call, op);
461 /* Instrument a NOP_EXPR to REFERENCE_TYPE if needed. */
463 void
464 ubsan_maybe_instrument_reference (tree stmt)
466 tree op = TREE_OPERAND (stmt, 0);
467 op = ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt), op,
468 TREE_TYPE (stmt),
469 UBSAN_REF_BINDING);
470 if (op)
471 TREE_OPERAND (stmt, 0) = op;
474 /* Instrument a CALL_EXPR to a method if needed. */
476 void
477 ubsan_maybe_instrument_member_call (tree stmt, bool is_ctor)
479 if (call_expr_nargs (stmt) == 0)
480 return;
481 tree op = CALL_EXPR_ARG (stmt, 0);
482 if (op == error_mark_node
483 || !POINTER_TYPE_P (TREE_TYPE (op)))
484 return;
485 op = ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt), op,
486 TREE_TYPE (op),
487 is_ctor ? UBSAN_CTOR_CALL
488 : UBSAN_MEMBER_CALL);
489 if (op)
490 CALL_EXPR_ARG (stmt, 0) = op;