P1236R1 - Signed integers are two's complement
[official-gcc.git] / gcc / c-family / c-ubsan.c
blob4ef2bd80cfedc952382186f3e4b699b3a4ffeb13
1 /* UndefinedBehaviorSanitizer, undefined behavior detector.
2 Copyright (C) 2013-2018 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 "stor-layout.h"
29 #include "builtins.h"
30 #include "gimplify.h"
31 #include "stringpool.h"
32 #include "attribs.h"
33 #include "asan.h"
35 /* Instrument division by zero and INT_MIN / -1. If not instrumenting,
36 return NULL_TREE. */
38 tree
39 ubsan_instrument_division (location_t loc, tree op0, tree op1)
41 tree t, tt;
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));
61 else
62 return NULL_TREE;
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))
69 tree x;
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
79 this expression. */
80 if (integer_zerop (t))
81 return NULL_TREE;
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);
89 else
91 tree data = ubsan_create_data ("__ubsan_overflow_data", 1, &loc,
92 ubsan_type_descriptor (type), NULL_TREE,
93 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);
107 return t;
110 /* Instrument left and right shifts. */
112 tree
113 ubsan_instrument_shift (location_t loc, enum tree_code code,
114 tree op0, tree op1)
116 tree t, tt = NULL_TREE;
117 tree type0 = TREE_TYPE (op0);
118 tree type1 = TREE_TYPE (op1);
119 if (!INTEGRAL_TYPE_P (type0))
120 return NULL_TREE;
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 || maybe_ne (GET_MODE_BITSIZE (TYPE_MODE (type0)),
136 TYPE_PRECISION (type0))
137 || !sanitize_flags_p (SANITIZE_SHIFT_BASE)
138 /* In C++2a and later, shifts are well defined except when
139 the second operand is not within bounds. */
140 || cxx_dialect >= cxx2a)
143 /* For signed x << y, in C99/C11, the following:
144 (unsigned) x >> (uprecm1 - y)
145 if non-zero, is undefined. */
146 else if (code == LSHIFT_EXPR && flag_isoc99 && cxx_dialect < cxx11)
148 tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
149 fold_convert (op1_utype, unshare_expr (op1)));
150 tt = fold_convert_loc (loc, unsigned_type_for (type0), op0);
151 tt = fold_build2 (RSHIFT_EXPR, TREE_TYPE (tt), tt, x);
152 tt = fold_build2 (NE_EXPR, boolean_type_node, tt,
153 build_int_cst (TREE_TYPE (tt), 0));
156 /* For signed x << y, in C++11 and later, the following:
157 x < 0 || ((unsigned) x >> (uprecm1 - y))
158 if > 1, is undefined. */
159 else if (code == LSHIFT_EXPR && cxx_dialect >= cxx11)
161 tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
162 fold_convert (op1_utype, unshare_expr (op1)));
163 tt = fold_convert_loc (loc, unsigned_type_for (type0),
164 unshare_expr (op0));
165 tt = fold_build2 (RSHIFT_EXPR, TREE_TYPE (tt), tt, x);
166 tt = fold_build2 (GT_EXPR, boolean_type_node, tt,
167 build_int_cst (TREE_TYPE (tt), 1));
168 x = fold_build2 (LT_EXPR, boolean_type_node, unshare_expr (op0),
169 build_int_cst (type0, 0));
170 tt = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, x, tt);
173 /* If the condition was folded to 0, no need to instrument
174 this expression. */
175 if (integer_zerop (t) && (tt == NULL_TREE || integer_zerop (tt)))
176 return NULL_TREE;
178 /* In case we have a SAVE_EXPR in a conditional context, we need to
179 make sure it gets evaluated before the condition. */
180 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op0), t);
181 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op1), t);
183 enum sanitize_code recover_kind = SANITIZE_SHIFT_EXPONENT;
184 tree else_t = void_node;
185 if (tt)
187 if (!sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
189 t = fold_build1 (TRUTH_NOT_EXPR, boolean_type_node, t);
190 t = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, t, tt);
191 recover_kind = SANITIZE_SHIFT_BASE;
193 else
195 if (flag_sanitize_undefined_trap_on_error
196 || ((!(flag_sanitize_recover & SANITIZE_SHIFT_EXPONENT))
197 == (!(flag_sanitize_recover & SANITIZE_SHIFT_BASE))))
198 t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, tt);
199 else
200 else_t = tt;
204 if (flag_sanitize_undefined_trap_on_error)
205 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
206 else
208 tree data = ubsan_create_data ("__ubsan_shift_data", 1, &loc,
209 ubsan_type_descriptor (type0),
210 ubsan_type_descriptor (type1), NULL_TREE,
211 NULL_TREE);
212 data = build_fold_addr_expr_loc (loc, data);
214 enum built_in_function bcode
215 = (flag_sanitize_recover & recover_kind)
216 ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
217 : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT;
218 tt = builtin_decl_explicit (bcode);
219 op0 = unshare_expr (op0);
220 op1 = unshare_expr (op1);
221 tt = build_call_expr_loc (loc, tt, 3, data, ubsan_encode_value (op0),
222 ubsan_encode_value (op1));
223 if (else_t != void_node)
225 bcode = (flag_sanitize_recover & SANITIZE_SHIFT_BASE)
226 ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
227 : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT;
228 tree else_tt = builtin_decl_explicit (bcode);
229 op0 = unshare_expr (op0);
230 op1 = unshare_expr (op1);
231 else_tt = build_call_expr_loc (loc, else_tt, 3, data,
232 ubsan_encode_value (op0),
233 ubsan_encode_value (op1));
234 else_t = fold_build3 (COND_EXPR, void_type_node, else_t,
235 else_tt, void_node);
238 t = fold_build3 (COND_EXPR, void_type_node, t, tt, else_t);
240 return t;
243 /* Instrument variable length array bound. */
245 tree
246 ubsan_instrument_vla (location_t loc, tree size)
248 tree type = TREE_TYPE (size);
249 tree t, tt;
251 t = fold_build2 (LE_EXPR, boolean_type_node, size, build_int_cst (type, 0));
252 if (flag_sanitize_undefined_trap_on_error)
253 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
254 else
256 tree data = ubsan_create_data ("__ubsan_vla_data", 1, &loc,
257 ubsan_type_descriptor (type), NULL_TREE,
258 NULL_TREE);
259 data = build_fold_addr_expr_loc (loc, data);
260 enum built_in_function bcode
261 = (flag_sanitize_recover & SANITIZE_VLA)
262 ? BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE
263 : BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE_ABORT;
264 tt = builtin_decl_explicit (bcode);
265 tt = build_call_expr_loc (loc, tt, 2, data, ubsan_encode_value (size));
267 t = fold_build3 (COND_EXPR, void_type_node, t, tt, void_node);
269 return t;
272 /* Instrument missing return in C++ functions returning non-void. */
274 tree
275 ubsan_instrument_return (location_t loc)
277 if (flag_sanitize_undefined_trap_on_error)
278 return build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
280 tree data = ubsan_create_data ("__ubsan_missing_return_data", 1, &loc,
281 NULL_TREE, NULL_TREE);
282 tree t = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_MISSING_RETURN);
283 return build_call_expr_loc (loc, t, 1, build_fold_addr_expr_loc (loc, data));
286 /* Instrument array bounds for ARRAY_REFs. We create special builtin,
287 that gets expanded in the sanopt pass, and make an array dimension
288 of it. ARRAY is the array, *INDEX is an index to the array.
289 Return NULL_TREE if no instrumentation is emitted.
290 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
292 tree
293 ubsan_instrument_bounds (location_t loc, tree array, tree *index,
294 bool ignore_off_by_one)
296 tree type = TREE_TYPE (array);
297 tree domain = TYPE_DOMAIN (type);
299 if (domain == NULL_TREE || TYPE_MAX_VALUE (domain) == NULL_TREE)
300 return NULL_TREE;
302 tree bound = TYPE_MAX_VALUE (domain);
303 if (ignore_off_by_one)
304 bound = fold_build2 (PLUS_EXPR, TREE_TYPE (bound), bound,
305 build_int_cst (TREE_TYPE (bound), 1));
307 /* Detect flexible array members and suchlike, unless
308 -fsanitize=bounds-strict. */
309 tree base = get_base_address (array);
310 if (!sanitize_flags_p (SANITIZE_BOUNDS_STRICT)
311 && TREE_CODE (array) == COMPONENT_REF
312 && base && (INDIRECT_REF_P (base) || TREE_CODE (base) == MEM_REF))
314 tree next = NULL_TREE;
315 tree cref = array;
317 /* Walk all structs/unions. */
318 while (TREE_CODE (cref) == COMPONENT_REF)
320 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
321 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
322 next && TREE_CODE (next) != FIELD_DECL;
323 next = DECL_CHAIN (next))
325 if (next)
326 /* Not a last element. Instrument it. */
327 break;
328 /* Ok, this is the last field of the structure/union. But the
329 aggregate containing the field must be the last field too,
330 recursively. */
331 cref = TREE_OPERAND (cref, 0);
333 if (!next)
334 /* Don't instrument this flexible array member-like array in non-strict
335 -fsanitize=bounds mode. */
336 return NULL_TREE;
339 /* Don't emit instrumentation in the most common cases. */
340 tree idx = NULL_TREE;
341 if (TREE_CODE (*index) == INTEGER_CST)
342 idx = *index;
343 else if (TREE_CODE (*index) == BIT_AND_EXPR
344 && TREE_CODE (TREE_OPERAND (*index, 1)) == INTEGER_CST)
345 idx = TREE_OPERAND (*index, 1);
346 if (idx
347 && TREE_CODE (bound) == INTEGER_CST
348 && tree_int_cst_sgn (idx) >= 0
349 && tree_int_cst_le (idx, bound))
350 return NULL_TREE;
352 *index = save_expr (*index);
353 /* Create a "(T *) 0" tree node to describe the array type. */
354 tree zero_with_type = build_int_cst (build_pointer_type (type), 0);
355 return build_call_expr_internal_loc (loc, IFN_UBSAN_BOUNDS,
356 void_type_node, 3, zero_with_type,
357 *index, bound);
360 /* Return true iff T is an array that was instrumented by SANITIZE_BOUNDS. */
362 bool
363 ubsan_array_ref_instrumented_p (const_tree t)
365 if (TREE_CODE (t) != ARRAY_REF)
366 return false;
368 tree op1 = TREE_OPERAND (t, 1);
369 return TREE_CODE (op1) == COMPOUND_EXPR
370 && TREE_CODE (TREE_OPERAND (op1, 0)) == CALL_EXPR
371 && CALL_EXPR_FN (TREE_OPERAND (op1, 0)) == NULL_TREE
372 && CALL_EXPR_IFN (TREE_OPERAND (op1, 0)) == IFN_UBSAN_BOUNDS;
375 /* Instrument an ARRAY_REF, if it hasn't already been instrumented.
376 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
378 void
379 ubsan_maybe_instrument_array_ref (tree *expr_p, bool ignore_off_by_one)
381 if (!ubsan_array_ref_instrumented_p (*expr_p)
382 && sanitize_flags_p (SANITIZE_BOUNDS | SANITIZE_BOUNDS_STRICT)
383 && current_function_decl != NULL_TREE)
385 tree op0 = TREE_OPERAND (*expr_p, 0);
386 tree op1 = TREE_OPERAND (*expr_p, 1);
387 tree e = ubsan_instrument_bounds (EXPR_LOCATION (*expr_p), op0, &op1,
388 ignore_off_by_one);
389 if (e != NULL_TREE)
391 tree t = copy_node (*expr_p);
392 TREE_OPERAND (t, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (op1),
393 e, op1);
394 *expr_p = t;
399 static tree
400 ubsan_maybe_instrument_reference_or_call (location_t loc, tree op, tree ptype,
401 enum ubsan_null_ckind ckind)
403 if (!sanitize_flags_p (SANITIZE_ALIGNMENT | SANITIZE_NULL)
404 || current_function_decl == NULL_TREE)
405 return NULL_TREE;
407 tree type = TREE_TYPE (ptype);
408 tree orig_op = op;
409 bool instrument = false;
410 unsigned int mina = 0;
412 if (sanitize_flags_p (SANITIZE_ALIGNMENT))
414 mina = min_align_of_type (type);
415 if (mina <= 1)
416 mina = 0;
418 while ((TREE_CODE (op) == NOP_EXPR
419 || TREE_CODE (op) == NON_LVALUE_EXPR)
420 && TREE_CODE (TREE_TYPE (op)) == POINTER_TYPE)
421 op = TREE_OPERAND (op, 0);
422 if (TREE_CODE (op) == NOP_EXPR
423 && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE)
425 if (mina && mina > min_align_of_type (TREE_TYPE (TREE_TYPE (op))))
426 instrument = true;
428 else
430 if (sanitize_flags_p (SANITIZE_NULL) && TREE_CODE (op) == ADDR_EXPR)
432 bool strict_overflow_p = false;
433 /* tree_single_nonzero_warnv_p will not return true for non-weak
434 non-automatic decls with -fno-delete-null-pointer-checks,
435 which is disabled during -fsanitize=null. We don't want to
436 instrument those, just weak vars though. */
437 int save_flag_delete_null_pointer_checks
438 = flag_delete_null_pointer_checks;
439 flag_delete_null_pointer_checks = 1;
440 if (!tree_single_nonzero_warnv_p (op, &strict_overflow_p)
441 || strict_overflow_p)
442 instrument = true;
443 flag_delete_null_pointer_checks
444 = save_flag_delete_null_pointer_checks;
446 else if (sanitize_flags_p (SANITIZE_NULL))
447 instrument = true;
448 if (mina && mina > 1)
450 if (!POINTER_TYPE_P (TREE_TYPE (op))
451 || mina > get_pointer_alignment (op) / BITS_PER_UNIT)
452 instrument = true;
455 if (!instrument)
456 return NULL_TREE;
457 op = save_expr (orig_op);
458 gcc_assert (POINTER_TYPE_P (ptype));
459 if (TREE_CODE (ptype) == REFERENCE_TYPE)
460 ptype = build_pointer_type (TREE_TYPE (ptype));
461 tree kind = build_int_cst (ptype, ckind);
462 tree align = build_int_cst (pointer_sized_int_node, mina);
463 tree call
464 = build_call_expr_internal_loc (loc, IFN_UBSAN_NULL, void_type_node,
465 3, op, kind, align);
466 TREE_SIDE_EFFECTS (call) = 1;
467 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (op), call, op);
470 /* Instrument a NOP_EXPR to REFERENCE_TYPE or INTEGER_CST with REFERENCE_TYPE
471 type if needed. */
473 void
474 ubsan_maybe_instrument_reference (tree *stmt_p)
476 tree stmt = *stmt_p;
477 tree op = stmt;
478 if (TREE_CODE (stmt) == NOP_EXPR)
479 op = TREE_OPERAND (stmt, 0);
480 op = ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt), op,
481 TREE_TYPE (stmt),
482 UBSAN_REF_BINDING);
483 if (op)
485 if (TREE_CODE (stmt) == NOP_EXPR)
486 TREE_OPERAND (stmt, 0) = op;
487 else
488 *stmt_p = op;
492 /* Instrument a CALL_EXPR to a method if needed. */
494 void
495 ubsan_maybe_instrument_member_call (tree stmt, bool is_ctor)
497 if (call_expr_nargs (stmt) == 0)
498 return;
499 tree op = CALL_EXPR_ARG (stmt, 0);
500 if (op == error_mark_node
501 || !POINTER_TYPE_P (TREE_TYPE (op)))
502 return;
503 op = ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt), op,
504 TREE_TYPE (op),
505 is_ctor ? UBSAN_CTOR_CALL
506 : UBSAN_MEMBER_CALL);
507 if (op)
508 CALL_EXPR_ARG (stmt, 0) = op;