Debian Codegraph @

gcc/c-family/c-ubsan.cc

raw
1 /* UndefinedBehaviorSanitizer, undefined behavior detector.
2 Copyright (C) 2013-2025 Free Software Foundation, Inc.
3 Contributed by Marek Polacek <[email protected]>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "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"
34 #include "langhooks.h"
35
36 /* Instrument division by zero and INT_MIN / -1. If not instrumenting,
37 return NULL_TREE. */
38
39 tree
40 ubsan_instrument_division (location_t loc, tree op0, tree op1)
41 {
42 tree t, tt, x = NULL_TREE;
43 tree type = TREE_TYPE (op0);
44 enum sanitize_code flag = SANITIZE_DIVIDE;
45
46 /* At this point both operands should have the same type,
47 because they are already converted to RESULT_TYPE.
48 Use TYPE_MAIN_VARIANT since typedefs can confuse us. */
49 tree top0 = TYPE_MAIN_VARIANT (type);
50 tree top1 = TYPE_MAIN_VARIANT (TREE_TYPE (op1));
51 gcc_checking_assert (lang_hooks.types_compatible_p (top0, top1));
52
53 op0 = unshare_expr (op0);
54 op1 = unshare_expr (op1);
55
56 if (INTEGRAL_TYPE_P (type)
57 && sanitize_flags_p (SANITIZE_DIVIDE))
58 t = fold_build2 (EQ_EXPR, boolean_type_node,
59 op1, build_int_cst (type, 0));
60 else if (SCALAR_FLOAT_TYPE_P (type)
61 && sanitize_flags_p (SANITIZE_FLOAT_DIVIDE))
62 {
63 t = fold_build2 (EQ_EXPR, boolean_type_node,
64 op1, build_real (type, dconst0));
65 flag = SANITIZE_FLOAT_DIVIDE;
66 }
67 else
68 t = NULL_TREE;
69
70 /* We check INT_MIN / -1 only for signed types. */
71 if (INTEGRAL_TYPE_P (type)
72 && sanitize_flags_p (SANITIZE_SI_OVERFLOW)
73 && !TYPE_UNSIGNED (type))
74 {
75 tt = fold_build2 (EQ_EXPR, boolean_type_node, unshare_expr (op1),
76 build_int_cst (type, -1));
77 x = fold_build2 (EQ_EXPR, boolean_type_node, op0,
78 TYPE_MIN_VALUE (type));
79 x = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, x, tt);
80 if (t == NULL_TREE || integer_zerop (t))
81 {
82 t = x;
83 x = NULL_TREE;
84 flag = SANITIZE_SI_OVERFLOW;
85 }
86 else if ((((flag_sanitize_trap & SANITIZE_DIVIDE) == 0)
87 == ((flag_sanitize_trap & SANITIZE_SI_OVERFLOW) == 0))
88 && (((flag_sanitize_recover & SANITIZE_DIVIDE) == 0)
89 == ((flag_sanitize_recover & SANITIZE_SI_OVERFLOW) == 0)))
90 {
91 t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, x);
92 x = NULL_TREE;
93 }
94 else if (integer_zerop (x))
95 x = NULL_TREE;
96 }
97 else if (t == NULL_TREE)
98 return NULL_TREE;
99
100 /* If the condition was folded to 0, no need to instrument
101 this expression. */
102 if (integer_zerop (t))
103 return NULL_TREE;
104
105 /* In case we have a SAVE_EXPR in a conditional context, we need to
106 make sure it gets evaluated before the condition. */
107 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op0), t);
108 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op1), t);
109 if ((flag_sanitize_trap & flag) && x == NULL_TREE)
110 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
111 else
112 {
113 tree data = ubsan_create_data ("__ubsan_overflow_data", 1, &loc,
114 ubsan_type_descriptor (type), NULL_TREE,
115 NULL_TREE);
116 data = build_fold_addr_expr_loc (loc, data);
117 if (flag_sanitize_trap & flag)
118 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP),
119 0);
120 else
121 {
122 enum built_in_function bcode
123 = (flag_sanitize_recover & flag)
124 ? BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW
125 : BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW_ABORT;
126 tt = builtin_decl_explicit (bcode);
127 op0 = unshare_expr (op0);
128 op1 = unshare_expr (op1);
129 tt = build_call_expr_loc (loc, tt, 3, data, ubsan_encode_value (op0),
130 ubsan_encode_value (op1));
131 }
132 if (x)
133 {
134 tree xt;
135 if (flag_sanitize_trap & SANITIZE_SI_OVERFLOW)
136 xt = build_call_expr_loc (loc,
137 builtin_decl_explicit (BUILT_IN_TRAP),
138 0);
139 else
140 {
141 enum built_in_function bcode
142 = (flag_sanitize_recover & SANITIZE_SI_OVERFLOW)
143 ? BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW
144 : BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW_ABORT;
145 xt = builtin_decl_explicit (bcode);
146 op0 = unshare_expr (op0);
147 op1 = unshare_expr (op1);
148 xt = build_call_expr_loc (loc, xt, 3, data,
149 ubsan_encode_value (op0),
150 ubsan_encode_value (op1));
151 }
152 x = fold_build3 (COND_EXPR, void_type_node, x, xt, void_node);
153 }
154 }
155 t = fold_build3 (COND_EXPR, void_type_node, t, tt, x ? x : void_node);
156
157 return t;
158 }
159
160 /* Instrument left and right shifts. */
161
162 tree
163 ubsan_instrument_shift (location_t loc, enum tree_code code,
164 tree op0, tree op1)
165 {
166 tree t, tt = NULL_TREE;
167 tree type0 = TREE_TYPE (op0);
168 tree type1 = TREE_TYPE (op1);
169 if (!INTEGRAL_TYPE_P (type0))
170 return NULL_TREE;
171
172 tree op1_utype = unsigned_type_for (type1);
173 HOST_WIDE_INT op0_prec = TYPE_PRECISION (type0);
174 tree uprecm1 = build_int_cst (op1_utype, op0_prec - 1);
175
176 op0 = unshare_expr (op0);
177 op1 = unshare_expr (op1);
178
179 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
180 {
181 /* For rotates just check for negative op1. */
182 if (TYPE_UNSIGNED (type1))
183 return NULL_TREE;
184 t = fold_build2 (LT_EXPR, boolean_type_node, op1,
185 build_int_cst (type1, 0));
186 }
187 else
188 {
189 t = fold_convert_loc (loc, op1_utype, op1);
190 t = fold_build2 (GT_EXPR, boolean_type_node, t, uprecm1);
191 }
192
193 /* If this is not a signed operation, don't perform overflow checks.
194 Also punt on bit-fields. */
195 if (TYPE_OVERFLOW_WRAPS (type0)
196 || maybe_ne (GET_MODE_BITSIZE (TYPE_MODE (type0)),
197 TYPE_PRECISION (type0))
198 || !sanitize_flags_p (SANITIZE_SHIFT_BASE)
199 /* In C++20 and later, shifts are well defined except when
200 the second operand is not within bounds. */
201 || cxx_dialect >= cxx20)
202 ;
203
204 /* For signed x << y, in C99 and later, the following:
205 (unsigned) x >> (uprecm1 - y)
206 if non-zero, is undefined. */
207 else if (code == LSHIFT_EXPR && flag_isoc99 && cxx_dialect < cxx11)
208 {
209 tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
210 fold_convert (op1_utype, unshare_expr (op1)));
211 tt = fold_convert_loc (loc, unsigned_type_for (type0), op0);
212 tt = fold_build2 (RSHIFT_EXPR, TREE_TYPE (tt), tt, x);
213 tt = fold_build2 (NE_EXPR, boolean_type_node, tt,
214 build_int_cst (TREE_TYPE (tt), 0));
215 }
216
217 /* For signed x << y, in C++11 to C++17, the following:
218 x < 0 || ((unsigned) x >> (uprecm1 - y))
219 if > 1, is undefined. */
220 else if (code == LSHIFT_EXPR && cxx_dialect >= cxx11)
221 {
222 tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
223 fold_convert (op1_utype, unshare_expr (op1)));
224 tt = fold_convert_loc (loc, unsigned_type_for (type0),
225 unshare_expr (op0));
226 tt = fold_build2 (RSHIFT_EXPR, TREE_TYPE (tt), tt, x);
227 tt = fold_build2 (GT_EXPR, boolean_type_node, tt,
228 build_int_cst (TREE_TYPE (tt), 1));
229 x = fold_build2 (LT_EXPR, boolean_type_node, unshare_expr (op0),
230 build_int_cst (type0, 0));
231 tt = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, x, tt);
232 }
233
234 /* If the condition was folded to 0, no need to instrument
235 this expression. */
236 if (integer_zerop (t) && (tt == NULL_TREE || integer_zerop (tt)))
237 return NULL_TREE;
238
239 /* In case we have a SAVE_EXPR in a conditional context, we need to
240 make sure it gets evaluated before the condition. */
241 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op0), t);
242 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op1), t);
243
244 enum sanitize_code recover_kind = SANITIZE_SHIFT_EXPONENT;
245 tree else_t = void_node;
246 if (tt)
247 {
248 if (!sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
249 {
250 t = fold_build1 (TRUTH_NOT_EXPR, boolean_type_node, t);
251 t = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, t, tt);
252 recover_kind = SANITIZE_SHIFT_BASE;
253 }
254 else
255 {
256 if (((!(flag_sanitize_trap & SANITIZE_SHIFT_EXPONENT))
257 == (!(flag_sanitize_trap & SANITIZE_SHIFT_BASE)))
258 && ((!(flag_sanitize_recover & SANITIZE_SHIFT_EXPONENT))
259 == (!(flag_sanitize_recover & SANITIZE_SHIFT_BASE))))
260 t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, tt);
261 else
262 else_t = tt;
263 }
264 }
265
266 if ((flag_sanitize_trap & recover_kind) && else_t == void_node)
267 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
268 else
269 {
270 if (TREE_CODE (type1) == BITINT_TYPE
271 && TYPE_PRECISION (type1) > MAX_FIXED_MODE_SIZE)
272 {
273 /* Workaround for missing _BitInt support in libsanitizer.
274 Instead of crashing in the library, pretend values above
275 maximum value of normal integral type or below minimum value
276 of that type are those extremes. */
277 tree type2 = build_nonstandard_integer_type (MAX_FIXED_MODE_SIZE,
278 TYPE_UNSIGNED (type1));
279 tree op2 = op1;
280 if (!TYPE_UNSIGNED (type1))
281 {
282 op2 = fold_build2 (LT_EXPR, boolean_type_node, unshare_expr (op1),
283 fold_convert (type1, TYPE_MIN_VALUE (type2)));
284 op2 = fold_build3 (COND_EXPR, type2, op2, TYPE_MIN_VALUE (type2),
285 fold_convert (type2, unshare_expr (op1)));
286 }
287 else
288 op2 = fold_convert (type2, op1);
289 tree op3
290 = fold_build2 (GT_EXPR, boolean_type_node, unshare_expr (op1),
291 fold_convert (type1, TYPE_MAX_VALUE (type2)));
292 op1 = fold_build3 (COND_EXPR, type2, op3, TYPE_MAX_VALUE (type2),
293 op2);
294 type1 = type2;
295 }
296 tree utd0 = ubsan_type_descriptor (type0, UBSAN_PRINT_FORCE_INT);
297 tree data = ubsan_create_data ("__ubsan_shift_data", 1, &loc, utd0,
298 ubsan_type_descriptor (type1), NULL_TREE,
299 NULL_TREE);
300 data = build_fold_addr_expr_loc (loc, data);
301
302 if (flag_sanitize_trap & recover_kind)
303 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
304 else
305 {
306 enum built_in_function bcode
307 = (flag_sanitize_recover & recover_kind)
308 ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
309 : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT;
310 tt = builtin_decl_explicit (bcode);
311 op0 = unshare_expr (op0);
312 op1 = unshare_expr (op1);
313 tt = build_call_expr_loc (loc, tt, 3, data, ubsan_encode_value (op0),
314 ubsan_encode_value (op1));
315 }
316 if (else_t != void_node)
317 {
318 tree else_tt;
319 if (flag_sanitize_trap & SANITIZE_SHIFT_BASE)
320 else_tt
321 = build_call_expr_loc (loc,
322 builtin_decl_explicit (BUILT_IN_TRAP), 0);
323 else
324 {
325 enum built_in_function bcode
326 = (flag_sanitize_recover & SANITIZE_SHIFT_BASE)
327 ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
328 : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT;
329 else_tt = builtin_decl_explicit (bcode);
330 op0 = unshare_expr (op0);
331 op1 = unshare_expr (op1);
332 else_tt = build_call_expr_loc (loc, else_tt, 3, data,
333 ubsan_encode_value (op0),
334 ubsan_encode_value (op1));
335 }
336 else_t = fold_build3 (COND_EXPR, void_type_node, else_t,
337 else_tt, void_node);
338 }
339 }
340 t = fold_build3 (COND_EXPR, void_type_node, t, tt, else_t);
341
342 return t;
343 }
344
345 /* Instrument variable length array bound. */
346
347 tree
348 ubsan_instrument_vla (location_t loc, tree size)
349 {
350 tree type = TREE_TYPE (size);
351 tree t, tt;
352
353 t = fold_build2 (LE_EXPR, boolean_type_node, size, build_int_cst (type, 0));
354 if (flag_sanitize_trap & SANITIZE_VLA)
355 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
356 else
357 {
358 tree data = ubsan_create_data ("__ubsan_vla_data", 1, &loc,
359 ubsan_type_descriptor (type), NULL_TREE,
360 NULL_TREE);
361 data = build_fold_addr_expr_loc (loc, data);
362 enum built_in_function bcode
363 = (flag_sanitize_recover & SANITIZE_VLA)
364 ? BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE
365 : BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE_ABORT;
366 tt = builtin_decl_explicit (bcode);
367 tt = build_call_expr_loc (loc, tt, 2, data, ubsan_encode_value (size));
368 }
369 t = fold_build3 (COND_EXPR, void_type_node, t, tt, void_node);
370
371 return t;
372 }
373
374 /* Instrument missing return in C++ functions returning non-void. */
375
376 tree
377 ubsan_instrument_return (location_t loc)
378 {
379 if (flag_sanitize_trap & SANITIZE_RETURN)
380 /* pass_warn_function_return checks for BUILTINS_LOCATION. */
381 return build_call_expr_loc (BUILTINS_LOCATION,
382 builtin_decl_explicit (BUILT_IN_TRAP), 0);
383
384 tree data = ubsan_create_data ("__ubsan_missing_return_data", 1, &loc,
385 NULL_TREE, NULL_TREE);
386 tree t = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_MISSING_RETURN);
387 return build_call_expr_loc (loc, t, 1, build_fold_addr_expr_loc (loc, data));
388 }
389
390 /* Get the tree that represented the number of counted_by, i.e, the maximum
391 number of the elements of the object that the call to .ACCESS_WITH_SIZE
392 points to, this number will be the bound of the corresponding array. */
393 static tree
394 get_bound_from_access_with_size (tree call)
395 {
396 if (!is_access_with_size_p (call))
397 return NULL_TREE;
398
399 tree ref_to_size = CALL_EXPR_ARG (call, 1);
400 unsigned int class_of_size = TREE_INT_CST_LOW (CALL_EXPR_ARG (call, 2));
401 tree type = TREE_TYPE (CALL_EXPR_ARG (call, 3));
402 tree size = fold_build2 (MEM_REF, type, unshare_expr (ref_to_size),
403 build_int_cst (ptr_type_node, 0));
404 /* If size is negative value, treat it as zero. */
405 if (!TYPE_UNSIGNED (type))
406 {
407 tree cond = fold_build2 (LT_EXPR, boolean_type_node,
408 unshare_expr (size), build_zero_cst (type));
409 size = fold_build3 (COND_EXPR, type, cond,
410 build_zero_cst (type), size);
411 }
412
413 /* Only when class_of_size is 1, i.e, the number of the elements of
414 the object type, return the size. */
415 if (class_of_size != 1)
416 return NULL_TREE;
417 else
418 size = fold_convert (sizetype, size);
419
420 return size;
421 }
422
423
424 /* Instrument array bounds for ARRAY_REFs. We create special builtin,
425 that gets expanded in the sanopt pass, and make an array dimension
426 of it. ARRAY is the array, *INDEX is an index to the array.
427 Return NULL_TREE if no instrumentation is emitted.
428 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside an ADDR_EXPR. */
429
430 tree
431 ubsan_instrument_bounds (location_t loc, tree array, tree *index,
432 bool ignore_off_by_one)
433 {
434 tree type = TREE_TYPE (array);
435 tree domain = TYPE_DOMAIN (type);
436
437 if (domain == NULL_TREE)
438 return NULL_TREE;
439
440 tree bound = TYPE_MAX_VALUE (domain);
441 if (!bound)
442 {
443 /* Handle C [0] arrays, which have TYPE_MAX_VALUE NULL, like
444 C++ [0] arrays which have TYPE_MIN_VALUE 0 TYPE_MAX_VALUE -1. */
445 if (!c_dialect_cxx ()
446 && COMPLETE_TYPE_P (type)
447 && integer_zerop (TYPE_SIZE (type)))
448 bound = build_int_cst (TREE_TYPE (TYPE_MIN_VALUE (domain)), -1);
449 else if (INDIRECT_REF_P (array)
450 && is_access_with_size_p ((TREE_OPERAND (array, 0))))
451 {
452 bound = get_bound_from_access_with_size ((TREE_OPERAND (array, 0)));
453 bound = fold_build2 (MINUS_EXPR, TREE_TYPE (bound),
454 bound,
455 build_int_cst (TREE_TYPE (bound), 1));
456 }
457 else
458 return NULL_TREE;
459 }
460
461 bound = fold_build2 (PLUS_EXPR, TREE_TYPE (bound), bound,
462 build_int_cst (TREE_TYPE (bound),
463 1 + ignore_off_by_one));
464
465 /* Detect flexible array members and suchlike, unless
466 -fsanitize=bounds-strict. */
467 tree base = get_base_address (array);
468 if (!sanitize_flags_p (SANITIZE_BOUNDS_STRICT)
469 && TREE_CODE (array) == COMPONENT_REF
470 && base && (INDIRECT_REF_P (base) || TREE_CODE (base) == MEM_REF))
471 {
472 tree next = NULL_TREE;
473 tree cref = array;
474
475 /* Walk all structs/unions. */
476 while (TREE_CODE (cref) == COMPONENT_REF)
477 {
478 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
479 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
480 next && TREE_CODE (next) != FIELD_DECL;
481 next = DECL_CHAIN (next))
482 ;
483 if (next)
484 /* Not a last element. Instrument it. */
485 break;
486 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 1))) == ARRAY_TYPE
487 && !c_dialect_cxx ())
488 {
489 unsigned l
490 = c_strict_flex_array_level_of (TREE_OPERAND (cref, 1));
491 tree type2 = TREE_TYPE (TREE_OPERAND (cref, 1));
492 if (TYPE_DOMAIN (type2) != NULL_TREE)
493 {
494 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (type2));
495 if (max == NULL_TREE)
496 {
497 /* C [0] */
498 if (COMPLETE_TYPE_P (type2)
499 && integer_zerop (TYPE_SIZE (type2))
500 && l == 3)
501 next = TREE_OPERAND (cref, 1);
502 }
503 else if (TREE_CODE (max) == INTEGER_CST)
504 {
505 if (c_dialect_cxx ()
506 && integer_all_onesp (max))
507 {
508 /* C++ [0] */
509 if (l == 3)
510 next = TREE_OPERAND (cref, 1);
511 }
512 else if (integer_zerop (max))
513 {
514 /* C/C++ [1] */
515 if (l >= 2)
516 next = TREE_OPERAND (cref, 1);
517 }
518 else if (l >= 1)
519 next = TREE_OPERAND (cref, 1);
520 }
521 }
522 if (next)
523 break;
524 }
525 /* Ok, this is the last field of the structure/union. But the
526 aggregate containing the field must be the last field too,
527 recursively. */
528 cref = TREE_OPERAND (cref, 0);
529 }
530 if (!next)
531 /* Don't instrument this flexible array member-like array in non-strict
532 -fsanitize=bounds mode. */
533 return NULL_TREE;
534 }
535
536 /* Don't emit instrumentation in the most common cases. */
537 tree idx = NULL_TREE;
538 if (TREE_CODE (*index) == INTEGER_CST)
539 idx = *index;
540 else if (TREE_CODE (*index) == BIT_AND_EXPR
541 && TREE_CODE (TREE_OPERAND (*index, 1)) == INTEGER_CST)
542 idx = TREE_OPERAND (*index, 1);
543 if (idx
544 && TREE_CODE (bound) == INTEGER_CST
545 && tree_int_cst_sgn (idx) >= 0
546 && tree_int_cst_lt (idx, bound))
547 return NULL_TREE;
548
549 *index = save_expr (*index);
550 /* If TYPE is a VLA, use 1 instead of 0 as the first argument and
551 use just the addend to TYPE_MAX_VALUE (domain) as the third argument
552 temporarily, so that gimplification can use TYPE_MAX_VALUE (domain)
553 after gimplify_type_sizes. See PR120052. */
554 bool is_vla = (TYPE_MAX_VALUE (domain)
555 && TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST);
556 if (is_vla)
557 bound = build_int_cst (TREE_TYPE (bound), 1 + ignore_off_by_one);
558 /* Create a "(T *) 0" (or 1) tree node to describe the array type. */
559 tree zero_with_type = build_int_cst (build_pointer_type (type), is_vla);
560 return build_call_expr_internal_loc (loc, IFN_UBSAN_BOUNDS,
561 void_type_node, 3, zero_with_type,
562 *index, bound);
563 }
564
565 /* Return true iff T is an array that was instrumented by SANITIZE_BOUNDS. */
566
567 bool
568 ubsan_array_ref_instrumented_p (const_tree t)
569 {
570 if (TREE_CODE (t) != ARRAY_REF)
571 return false;
572
573 tree op1 = TREE_OPERAND (t, 1);
574 return TREE_CODE (op1) == COMPOUND_EXPR
575 && TREE_CODE (TREE_OPERAND (op1, 0)) == CALL_EXPR
576 && CALL_EXPR_FN (TREE_OPERAND (op1, 0)) == NULL_TREE
577 && CALL_EXPR_IFN (TREE_OPERAND (op1, 0)) == IFN_UBSAN_BOUNDS;
578 }
579
580 /* Instrument an ARRAY_REF, if it hasn't already been instrumented.
581 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
582
583 void
584 ubsan_maybe_instrument_array_ref (tree *expr_p, bool ignore_off_by_one)
585 {
586 if (!ubsan_array_ref_instrumented_p (*expr_p)
587 && sanitize_flags_p (SANITIZE_BOUNDS | SANITIZE_BOUNDS_STRICT)
588 && current_function_decl != NULL_TREE)
589 {
590 tree op0 = TREE_OPERAND (*expr_p, 0);
591 tree op1 = TREE_OPERAND (*expr_p, 1);
592 tree e = ubsan_instrument_bounds (EXPR_LOCATION (*expr_p), op0, &op1,
593 ignore_off_by_one);
594 if (e != NULL_TREE)
595 TREE_OPERAND (*expr_p, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (op1),
596 e, op1);
597 }
598 }
599
600 static tree
601 ubsan_maybe_instrument_reference_or_call (location_t loc, tree op, tree ptype,
602 enum ubsan_null_ckind ckind)
603 {
604 if (!sanitize_flags_p (SANITIZE_ALIGNMENT | SANITIZE_NULL)
605 || current_function_decl == NULL_TREE)
606 return NULL_TREE;
607
608 tree type = TREE_TYPE (ptype);
609 tree orig_op = op;
610 bool instrument = false;
611 unsigned int mina = 0;
612
613 if (sanitize_flags_p (SANITIZE_ALIGNMENT))
614 {
615 mina = min_align_of_type (type);
616 if (mina <= 1)
617 mina = 0;
618 }
619 while ((TREE_CODE (op) == NOP_EXPR
620 || TREE_CODE (op) == NON_LVALUE_EXPR)
621 && TREE_CODE (TREE_TYPE (op)) == POINTER_TYPE)
622 op = TREE_OPERAND (op, 0);
623 if (TREE_CODE (op) == NOP_EXPR
624 && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE)
625 {
626 if (mina && mina > min_align_of_type (TREE_TYPE (TREE_TYPE (op))))
627 instrument = true;
628 }
629 else
630 {
631 if (sanitize_flags_p (SANITIZE_NULL) && TREE_CODE (op) == ADDR_EXPR)
632 {
633 bool strict_overflow_p = false;
634 /* tree_single_nonzero_warnv_p will not return true for non-weak
635 non-automatic decls with -fno-delete-null-pointer-checks,
636 which is disabled during -fsanitize=null. We don't want to
637 instrument those, just weak vars though. */
638 int save_flag_delete_null_pointer_checks
639 = flag_delete_null_pointer_checks;
640 flag_delete_null_pointer_checks = 1;
641 if (!tree_single_nonzero_warnv_p (op, &strict_overflow_p)
642 || strict_overflow_p)
643 instrument = true;
644 flag_delete_null_pointer_checks
645 = save_flag_delete_null_pointer_checks;
646 }
647 else if (sanitize_flags_p (SANITIZE_NULL))
648 instrument = true;
649 if (mina && mina > 1)
650 {
651 if (!POINTER_TYPE_P (TREE_TYPE (op))
652 || mina > get_pointer_alignment (op) / BITS_PER_UNIT)
653 instrument = true;
654 }
655 }
656 if (!instrument)
657 return NULL_TREE;
658 op = save_expr (orig_op);
659 gcc_assert (POINTER_TYPE_P (ptype));
660 if (TREE_CODE (ptype) == REFERENCE_TYPE)
661 ptype = build_pointer_type (TREE_TYPE (ptype));
662 tree kind = build_int_cst (ptype, ckind);
663 tree align = build_int_cst (pointer_sized_int_node, mina);
664 tree call
665 = build_call_expr_internal_loc (loc, IFN_UBSAN_NULL, void_type_node,
666 3, op, kind, align);
667 TREE_SIDE_EFFECTS (call) = 1;
668 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (op), call, op);
669 }
670
671 /* Instrument a NOP_EXPR to REFERENCE_TYPE or INTEGER_CST with REFERENCE_TYPE
672 type if needed. */
673
674 void
675 ubsan_maybe_instrument_reference (tree *stmt_p)
676 {
677 tree stmt = *stmt_p;
678 tree op = stmt;
679 if (TREE_CODE (stmt) == NOP_EXPR)
680 op = TREE_OPERAND (stmt, 0);
681 op = ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt), op,
682 TREE_TYPE (stmt),
683 UBSAN_REF_BINDING);
684 if (op)
685 {
686 if (TREE_CODE (stmt) == NOP_EXPR)
687 TREE_OPERAND (stmt, 0) = op;
688 else
689 *stmt_p = op;
690 }
691 }
692
693 /* Instrument a CALL_EXPR to a method if needed. */
694
695 void
696 ubsan_maybe_instrument_member_call (tree stmt, bool is_ctor)
697 {
698 if (call_expr_nargs (stmt) == 0)
699 return;
700 tree op = CALL_EXPR_ARG (stmt, 0);
701 if (op == error_mark_node
702 || !POINTER_TYPE_P (TREE_TYPE (op)))
703 return;
704 op = ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt), op,
705 TREE_TYPE (op),
706 is_ctor ? UBSAN_CTOR_CALL
707 : UBSAN_MEMBER_CALL);
708 if (op)
709 CALL_EXPR_ARG (stmt, 0) = op;
710 }