Branch data Line data Source code
1 : : /* UndefinedBehaviorSanitizer, undefined behavior detector.
2 : : Copyright (C) 2013-2025 Free Software Foundation, Inc.
3 : : Contributed by Marek Polacek <polacek@redhat.com>
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 : 1365 : ubsan_instrument_division (location_t loc, tree op0, tree op1)
41 : : {
42 : 1365 : tree t, tt, x = NULL_TREE;
43 : 1365 : tree type = TREE_TYPE (op0);
44 : 1365 : 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 : 1365 : tree top0 = TYPE_MAIN_VARIANT (type);
50 : 1365 : tree top1 = TYPE_MAIN_VARIANT (TREE_TYPE (op1));
51 : 1365 : gcc_checking_assert (lang_hooks.types_compatible_p (top0, top1));
52 : :
53 : 1365 : op0 = unshare_expr (op0);
54 : 1365 : op1 = unshare_expr (op1);
55 : :
56 : 1365 : if (INTEGRAL_TYPE_P (type)
57 : 1365 : && sanitize_flags_p (SANITIZE_DIVIDE))
58 : 889 : t = fold_build2 (EQ_EXPR, boolean_type_node,
59 : : op1, build_int_cst (type, 0));
60 : 476 : else if (SCALAR_FLOAT_TYPE_P (type)
61 : 476 : && sanitize_flags_p (SANITIZE_FLOAT_DIVIDE))
62 : : {
63 : 96 : t = fold_build2 (EQ_EXPR, boolean_type_node,
64 : : op1, build_real (type, dconst0));
65 : 96 : flag = SANITIZE_FLOAT_DIVIDE;
66 : : }
67 : : else
68 : : t = NULL_TREE;
69 : :
70 : : /* We check INT_MIN / -1 only for signed types. */
71 : 1365 : if (INTEGRAL_TYPE_P (type)
72 : 1154 : && sanitize_flags_p (SANITIZE_SI_OVERFLOW)
73 : 2244 : && !TYPE_UNSIGNED (type))
74 : : {
75 : 759 : tt = fold_build2 (EQ_EXPR, boolean_type_node, unshare_expr (op1),
76 : : build_int_cst (type, -1));
77 : 759 : x = fold_build2 (EQ_EXPR, boolean_type_node, op0,
78 : : TYPE_MIN_VALUE (type));
79 : 759 : x = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, x, tt);
80 : 759 : if (t == NULL_TREE || integer_zerop (t))
81 : : {
82 : : t = x;
83 : : x = NULL_TREE;
84 : : flag = SANITIZE_SI_OVERFLOW;
85 : : }
86 : 301 : else if ((((flag_sanitize_trap & SANITIZE_DIVIDE) == 0)
87 : 301 : == ((flag_sanitize_trap & SANITIZE_SI_OVERFLOW) == 0))
88 : 301 : && (((flag_sanitize_recover & SANITIZE_DIVIDE) == 0)
89 : 301 : == ((flag_sanitize_recover & SANITIZE_SI_OVERFLOW) == 0)))
90 : : {
91 : 260 : t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, x);
92 : 260 : x = NULL_TREE;
93 : : }
94 : 41 : else if (integer_zerop (x))
95 : 470 : x = NULL_TREE;
96 : : }
97 : 606 : 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 : 1229 : 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 : 783 : t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op0), t);
108 : 783 : t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op1), t);
109 : 783 : if ((flag_sanitize_trap & flag) && x == NULL_TREE)
110 : 10 : tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
111 : : else
112 : : {
113 : 773 : tree data = ubsan_create_data ("__ubsan_overflow_data", 1, &loc,
114 : : ubsan_type_descriptor (type), NULL_TREE,
115 : : NULL_TREE);
116 : 773 : data = build_fold_addr_expr_loc (loc, data);
117 : 773 : if (flag_sanitize_trap & flag)
118 : 0 : tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP),
119 : : 0);
120 : : else
121 : : {
122 : 115 : enum built_in_function bcode
123 : 773 : = (flag_sanitize_recover & flag)
124 : 773 : ? BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW
125 : : : BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW_ABORT;
126 : 773 : tt = builtin_decl_explicit (bcode);
127 : 773 : op0 = unshare_expr (op0);
128 : 773 : op1 = unshare_expr (op1);
129 : 773 : tt = build_call_expr_loc (loc, tt, 3, data, ubsan_encode_value (op0),
130 : : ubsan_encode_value (op1));
131 : : }
132 : 773 : if (x)
133 : : {
134 : 41 : tree xt;
135 : 41 : if (flag_sanitize_trap & SANITIZE_SI_OVERFLOW)
136 : 0 : xt = build_call_expr_loc (loc,
137 : : builtin_decl_explicit (BUILT_IN_TRAP),
138 : : 0);
139 : : else
140 : : {
141 : 16 : enum built_in_function bcode
142 : 41 : = (flag_sanitize_recover & SANITIZE_SI_OVERFLOW)
143 : 41 : ? BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW
144 : : : BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW_ABORT;
145 : 41 : xt = builtin_decl_explicit (bcode);
146 : 41 : op0 = unshare_expr (op0);
147 : 41 : op1 = unshare_expr (op1);
148 : 41 : xt = build_call_expr_loc (loc, xt, 3, data,
149 : : ubsan_encode_value (op0),
150 : : ubsan_encode_value (op1));
151 : : }
152 : 41 : x = fold_build3 (COND_EXPR, void_type_node, x, xt, void_node);
153 : : }
154 : : }
155 : 783 : t = fold_build3 (COND_EXPR, void_type_node, t, tt, x ? x : void_node);
156 : :
157 : 783 : return t;
158 : : }
159 : :
160 : : /* Instrument left and right shifts. */
161 : :
162 : : tree
163 : 2152 : ubsan_instrument_shift (location_t loc, enum tree_code code,
164 : : tree op0, tree op1)
165 : : {
166 : 2152 : tree t, tt = NULL_TREE;
167 : 2152 : tree type0 = TREE_TYPE (op0);
168 : 2152 : tree type1 = TREE_TYPE (op1);
169 : 2152 : if (!INTEGRAL_TYPE_P (type0))
170 : : return NULL_TREE;
171 : :
172 : 2138 : tree op1_utype = unsigned_type_for (type1);
173 : 2138 : HOST_WIDE_INT op0_prec = TYPE_PRECISION (type0);
174 : 2138 : tree uprecm1 = build_int_cst (op1_utype, op0_prec - 1);
175 : :
176 : 2138 : op0 = unshare_expr (op0);
177 : 2138 : op1 = unshare_expr (op1);
178 : :
179 : 2138 : if (code == LROTATE_EXPR || code == RROTATE_EXPR)
180 : : {
181 : : /* For rotates just check for negative op1. */
182 : 28 : if (TYPE_UNSIGNED (type1))
183 : : return NULL_TREE;
184 : 28 : t = fold_build2 (LT_EXPR, boolean_type_node, op1,
185 : : build_int_cst (type1, 0));
186 : : }
187 : : else
188 : : {
189 : 2110 : t = fold_convert_loc (loc, op1_utype, op1);
190 : 2110 : 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 : 2138 : if (TYPE_OVERFLOW_WRAPS (type0)
196 : 2764 : || maybe_ne (GET_MODE_BITSIZE (TYPE_MODE (type0)),
197 : 1382 : TYPE_PRECISION (type0))
198 : 1302 : || !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 : 1251 : || 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 : 1151 : else if (code == LSHIFT_EXPR && flag_isoc99 && cxx_dialect < cxx11)
208 : : {
209 : 766 : tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
210 : : fold_convert (op1_utype, unshare_expr (op1)));
211 : 766 : tt = fold_convert_loc (loc, unsigned_type_for (type0), op0);
212 : 766 : tt = fold_build2 (RSHIFT_EXPR, TREE_TYPE (tt), tt, x);
213 : 766 : tt = fold_build2 (NE_EXPR, boolean_type_node, tt,
214 : : build_int_cst (TREE_TYPE (tt), 0));
215 : 766 : }
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 : 134 : else if (code == LSHIFT_EXPR && cxx_dialect >= cxx11)
221 : : {
222 : 74 : tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
223 : : fold_convert (op1_utype, unshare_expr (op1)));
224 : 74 : tt = fold_convert_loc (loc, unsigned_type_for (type0),
225 : : unshare_expr (op0));
226 : 74 : tt = fold_build2 (RSHIFT_EXPR, TREE_TYPE (tt), tt, x);
227 : 74 : tt = fold_build2 (GT_EXPR, boolean_type_node, tt,
228 : : build_int_cst (TREE_TYPE (tt), 1));
229 : 74 : x = fold_build2 (LT_EXPR, boolean_type_node, unshare_expr (op0),
230 : : build_int_cst (type0, 0));
231 : 74 : 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 : 2138 : if (integer_zerop (t) && (tt == NULL_TREE || integer_zerop (tt)))
237 : 1107 : 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 : 1031 : t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op0), t);
242 : 1031 : t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op1), t);
243 : :
244 : 1031 : enum sanitize_code recover_kind = SANITIZE_SHIFT_EXPONENT;
245 : 1031 : tree else_t = void_node;
246 : 1031 : if (tt)
247 : : {
248 : 530 : if (!sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
249 : : {
250 : 28 : t = fold_build1 (TRUTH_NOT_EXPR, boolean_type_node, t);
251 : 28 : t = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, t, tt);
252 : 28 : recover_kind = SANITIZE_SHIFT_BASE;
253 : : }
254 : : else
255 : : {
256 : 502 : if (((!(flag_sanitize_trap & SANITIZE_SHIFT_EXPONENT))
257 : 502 : == (!(flag_sanitize_trap & SANITIZE_SHIFT_BASE)))
258 : 502 : && ((!(flag_sanitize_recover & SANITIZE_SHIFT_EXPONENT))
259 : 502 : == (!(flag_sanitize_recover & SANITIZE_SHIFT_BASE))))
260 : 435 : t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, tt);
261 : : else
262 : : else_t = tt;
263 : : }
264 : : }
265 : :
266 : 1031 : if ((flag_sanitize_trap & recover_kind) && else_t == void_node)
267 : 0 : tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
268 : : else
269 : : {
270 : 1031 : if (TREE_CODE (type1) == BITINT_TYPE
271 : 1063 : && 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 : 32 : tree type2 = build_nonstandard_integer_type (MAX_FIXED_MODE_SIZE,
278 : 32 : TYPE_UNSIGNED (type1));
279 : 32 : tree op2 = op1;
280 : 32 : if (!TYPE_UNSIGNED (type1))
281 : : {
282 : 32 : op2 = fold_build2 (LT_EXPR, boolean_type_node, unshare_expr (op1),
283 : : fold_convert (type1, TYPE_MIN_VALUE (type2)));
284 : 32 : op2 = fold_build3 (COND_EXPR, type2, op2, TYPE_MIN_VALUE (type2),
285 : : fold_convert (type2, unshare_expr (op1)));
286 : : }
287 : : else
288 : 0 : op2 = fold_convert (type2, op1);
289 : 32 : tree op3
290 : 32 : = fold_build2 (GT_EXPR, boolean_type_node, unshare_expr (op1),
291 : : fold_convert (type1, TYPE_MAX_VALUE (type2)));
292 : 32 : op1 = fold_build3 (COND_EXPR, type2, op3, TYPE_MAX_VALUE (type2),
293 : : op2);
294 : 32 : type1 = type2;
295 : : }
296 : 1031 : tree utd0 = ubsan_type_descriptor (type0, UBSAN_PRINT_FORCE_INT);
297 : 1031 : tree data = ubsan_create_data ("__ubsan_shift_data", 1, &loc, utd0,
298 : : ubsan_type_descriptor (type1), NULL_TREE,
299 : : NULL_TREE);
300 : 1031 : data = build_fold_addr_expr_loc (loc, data);
301 : :
302 : 1031 : if (flag_sanitize_trap & recover_kind)
303 : 0 : tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
304 : : else
305 : : {
306 : 152 : enum built_in_function bcode
307 : 1031 : = (flag_sanitize_recover & recover_kind)
308 : 1031 : ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
309 : : : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT;
310 : 1031 : tt = builtin_decl_explicit (bcode);
311 : 1031 : op0 = unshare_expr (op0);
312 : 1031 : op1 = unshare_expr (op1);
313 : 1031 : tt = build_call_expr_loc (loc, tt, 3, data, ubsan_encode_value (op0),
314 : : ubsan_encode_value (op1));
315 : : }
316 : 1031 : if (else_t != void_node)
317 : : {
318 : 67 : tree else_tt;
319 : 67 : if (flag_sanitize_trap & SANITIZE_SHIFT_BASE)
320 : 0 : else_tt
321 : 0 : = build_call_expr_loc (loc,
322 : : builtin_decl_explicit (BUILT_IN_TRAP), 0);
323 : : else
324 : : {
325 : 32 : enum built_in_function bcode
326 : 67 : = (flag_sanitize_recover & SANITIZE_SHIFT_BASE)
327 : 67 : ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
328 : : : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT;
329 : 67 : else_tt = builtin_decl_explicit (bcode);
330 : 67 : op0 = unshare_expr (op0);
331 : 67 : op1 = unshare_expr (op1);
332 : 67 : else_tt = build_call_expr_loc (loc, else_tt, 3, data,
333 : : ubsan_encode_value (op0),
334 : : ubsan_encode_value (op1));
335 : : }
336 : 67 : else_t = fold_build3 (COND_EXPR, void_type_node, else_t,
337 : : else_tt, void_node);
338 : : }
339 : : }
340 : 1031 : t = fold_build3 (COND_EXPR, void_type_node, t, tt, else_t);
341 : :
342 : 1031 : return t;
343 : : }
344 : :
345 : : /* Instrument variable length array bound. */
346 : :
347 : : tree
348 : 221 : ubsan_instrument_vla (location_t loc, tree size)
349 : : {
350 : 221 : tree type = TREE_TYPE (size);
351 : 221 : tree t, tt;
352 : :
353 : 221 : t = fold_build2 (LE_EXPR, boolean_type_node, size, build_int_cst (type, 0));
354 : 221 : if (flag_sanitize_trap & SANITIZE_VLA)
355 : 0 : tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
356 : : else
357 : : {
358 : 221 : tree data = ubsan_create_data ("__ubsan_vla_data", 1, &loc,
359 : : ubsan_type_descriptor (type), NULL_TREE,
360 : : NULL_TREE);
361 : 221 : data = build_fold_addr_expr_loc (loc, data);
362 : 12 : enum built_in_function bcode
363 : 221 : = (flag_sanitize_recover & SANITIZE_VLA)
364 : 221 : ? BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE
365 : : : BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE_ABORT;
366 : 221 : tt = builtin_decl_explicit (bcode);
367 : 221 : tt = build_call_expr_loc (loc, tt, 2, data, ubsan_encode_value (size));
368 : : }
369 : 221 : t = fold_build3 (COND_EXPR, void_type_node, t, tt, void_node);
370 : :
371 : 221 : return t;
372 : : }
373 : :
374 : : /* Instrument missing return in C++ functions returning non-void. */
375 : :
376 : : tree
377 : 1383 : ubsan_instrument_return (location_t loc)
378 : : {
379 : 1383 : if (flag_sanitize_trap & SANITIZE_RETURN)
380 : : /* pass_warn_function_return checks for BUILTINS_LOCATION. */
381 : 3 : return build_call_expr_loc (BUILTINS_LOCATION,
382 : 3 : builtin_decl_explicit (BUILT_IN_TRAP), 0);
383 : :
384 : 1380 : tree data = ubsan_create_data ("__ubsan_missing_return_data", 1, &loc,
385 : : NULL_TREE, NULL_TREE);
386 : 1380 : tree t = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_MISSING_RETURN);
387 : 1380 : 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 : 37 : get_bound_from_access_with_size (tree call)
395 : : {
396 : 37 : if (!is_access_with_size_p (call))
397 : : return NULL_TREE;
398 : :
399 : 37 : tree ref_to_size = CALL_EXPR_ARG (call, 1);
400 : 37 : tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (call, 2)));
401 : 37 : tree size = fold_build2 (MEM_REF, type, unshare_expr (ref_to_size),
402 : : build_int_cst (ptr_type_node, 0));
403 : : /* If size is negative value, treat it as zero. */
404 : 37 : if (!TYPE_UNSIGNED (type))
405 : : {
406 : 37 : tree cond = fold_build2 (LT_EXPR, boolean_type_node,
407 : : unshare_expr (size), build_zero_cst (type));
408 : 37 : size = fold_build3 (COND_EXPR, type, cond,
409 : : build_zero_cst (type), size);
410 : : }
411 : :
412 : 37 : size = fold_convert (sizetype, size);
413 : :
414 : 37 : return size;
415 : : }
416 : :
417 : :
418 : : /* Instrument array bounds for ARRAY_REFs. We create special builtin,
419 : : that gets expanded in the sanopt pass, and make an array dimension
420 : : of it. ARRAY is the array, *INDEX is an index to the array.
421 : : Return NULL_TREE if no instrumentation is emitted.
422 : : IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside an ADDR_EXPR. */
423 : :
424 : : tree
425 : 4242 : ubsan_instrument_bounds (location_t loc, tree array, tree *index,
426 : : bool ignore_off_by_one)
427 : : {
428 : 4242 : tree type = TREE_TYPE (array);
429 : 4242 : tree domain = TYPE_DOMAIN (type);
430 : :
431 : 4242 : if (domain == NULL_TREE)
432 : : return NULL_TREE;
433 : :
434 : 4230 : tree bound = TYPE_MAX_VALUE (domain);
435 : 4230 : if (!bound)
436 : : {
437 : : /* Handle C [0] arrays, which have TYPE_MAX_VALUE NULL, like
438 : : C++ [0] arrays which have TYPE_MIN_VALUE 0 TYPE_MAX_VALUE -1. */
439 : 228 : if (!c_dialect_cxx ()
440 : 228 : && COMPLETE_TYPE_P (type)
441 : 347 : && integer_zerop (TYPE_SIZE (type)))
442 : 119 : bound = build_int_cst (TREE_TYPE (TYPE_MIN_VALUE (domain)), -1);
443 : 109 : else if (INDIRECT_REF_P (array)
444 : 109 : && is_access_with_size_p ((TREE_OPERAND (array, 0))))
445 : : {
446 : 37 : bound = get_bound_from_access_with_size ((TREE_OPERAND (array, 0)));
447 : 37 : bound = fold_build2 (MINUS_EXPR, TREE_TYPE (bound),
448 : : bound,
449 : : build_int_cst (TREE_TYPE (bound), 1));
450 : : }
451 : : else
452 : 72 : return NULL_TREE;
453 : : }
454 : :
455 : 4158 : bound = fold_build2 (PLUS_EXPR, TREE_TYPE (bound), bound,
456 : : build_int_cst (TREE_TYPE (bound),
457 : : 1 + ignore_off_by_one));
458 : :
459 : : /* Detect flexible array members and suchlike, unless
460 : : -fsanitize=bounds-strict. */
461 : 4158 : tree base = get_base_address (array);
462 : 4158 : if (!sanitize_flags_p (SANITIZE_BOUNDS_STRICT)
463 : 4126 : && TREE_CODE (array) == COMPONENT_REF
464 : 5078 : && base && (INDIRECT_REF_P (base) || TREE_CODE (base) == MEM_REF))
465 : : {
466 : : tree next = NULL_TREE;
467 : : tree cref = array;
468 : :
469 : : /* Walk all structs/unions. */
470 : 834 : while (TREE_CODE (cref) == COMPONENT_REF)
471 : : {
472 : 610 : if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
473 : 603 : for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
474 : 2827 : next && TREE_CODE (next) != FIELD_DECL;
475 : 2224 : next = DECL_CHAIN (next))
476 : : ;
477 : 610 : if (next)
478 : : /* Not a last element. Instrument it. */
479 : : break;
480 : 369 : if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 1))) == ARRAY_TYPE
481 : 369 : && !c_dialect_cxx ())
482 : : {
483 : 268 : unsigned l
484 : 268 : = c_strict_flex_array_level_of (TREE_OPERAND (cref, 1));
485 : 268 : tree type2 = TREE_TYPE (TREE_OPERAND (cref, 1));
486 : 268 : if (TYPE_DOMAIN (type2) != NULL_TREE)
487 : : {
488 : 268 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (type2));
489 : 268 : if (max == NULL_TREE)
490 : : {
491 : : /* C [0] */
492 : 47 : if (COMPLETE_TYPE_P (type2)
493 : 47 : && integer_zerop (TYPE_SIZE (type2))
494 : 94 : && l == 3)
495 : 8 : next = TREE_OPERAND (cref, 1);
496 : : }
497 : 221 : else if (TREE_CODE (max) == INTEGER_CST)
498 : : {
499 : 221 : if (c_dialect_cxx ()
500 : 221 : && integer_all_onesp (max))
501 : : {
502 : : /* C++ [0] */
503 : 0 : if (l == 3)
504 : 0 : next = TREE_OPERAND (cref, 1);
505 : : }
506 : 221 : else if (integer_zerop (max))
507 : : {
508 : : /* C/C++ [1] */
509 : 95 : if (l >= 2)
510 : 16 : next = TREE_OPERAND (cref, 1);
511 : : }
512 : 126 : else if (l >= 1)
513 : 48 : next = TREE_OPERAND (cref, 1);
514 : : }
515 : : }
516 : 268 : if (next)
517 : : break;
518 : : }
519 : : /* Ok, this is the last field of the structure/union. But the
520 : : aggregate containing the field must be the last field too,
521 : : recursively. */
522 : 297 : cref = TREE_OPERAND (cref, 0);
523 : : }
524 : 537 : if (!next)
525 : : /* Don't instrument this flexible array member-like array in non-strict
526 : : -fsanitize=bounds mode. */
527 : : return NULL_TREE;
528 : : }
529 : :
530 : : /* Don't emit instrumentation in the most common cases. */
531 : 3934 : tree idx = NULL_TREE;
532 : 3934 : if (TREE_CODE (*index) == INTEGER_CST)
533 : : idx = *index;
534 : 725 : else if (TREE_CODE (*index) == BIT_AND_EXPR
535 : 725 : && TREE_CODE (TREE_OPERAND (*index, 1)) == INTEGER_CST)
536 : 26 : idx = TREE_OPERAND (*index, 1);
537 : 3235 : if (idx
538 : 3235 : && TREE_CODE (bound) == INTEGER_CST
539 : 2948 : && tree_int_cst_sgn (idx) >= 0
540 : 6161 : && tree_int_cst_lt (idx, bound))
541 : : return NULL_TREE;
542 : :
543 : 1489 : *index = save_expr (*index);
544 : : /* Create a "(T *) 0" tree node to describe the array type. */
545 : 1489 : tree zero_with_type = build_int_cst (build_pointer_type (type), 0);
546 : 1489 : return build_call_expr_internal_loc (loc, IFN_UBSAN_BOUNDS,
547 : : void_type_node, 3, zero_with_type,
548 : 1489 : *index, bound);
549 : : }
550 : :
551 : : /* Return true iff T is an array that was instrumented by SANITIZE_BOUNDS. */
552 : :
553 : : bool
554 : 4249 : ubsan_array_ref_instrumented_p (const_tree t)
555 : : {
556 : 4249 : if (TREE_CODE (t) != ARRAY_REF)
557 : : return false;
558 : :
559 : 4249 : tree op1 = TREE_OPERAND (t, 1);
560 : 4249 : return TREE_CODE (op1) == COMPOUND_EXPR
561 : 0 : && TREE_CODE (TREE_OPERAND (op1, 0)) == CALL_EXPR
562 : 0 : && CALL_EXPR_FN (TREE_OPERAND (op1, 0)) == NULL_TREE
563 : 4249 : && CALL_EXPR_IFN (TREE_OPERAND (op1, 0)) == IFN_UBSAN_BOUNDS;
564 : : }
565 : :
566 : : /* Instrument an ARRAY_REF, if it hasn't already been instrumented.
567 : : IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
568 : :
569 : : void
570 : 4249 : ubsan_maybe_instrument_array_ref (tree *expr_p, bool ignore_off_by_one)
571 : : {
572 : 4249 : if (!ubsan_array_ref_instrumented_p (*expr_p)
573 : 4249 : && sanitize_flags_p (SANITIZE_BOUNDS | SANITIZE_BOUNDS_STRICT)
574 : 8488 : && current_function_decl != NULL_TREE)
575 : : {
576 : 4239 : tree op0 = TREE_OPERAND (*expr_p, 0);
577 : 4239 : tree op1 = TREE_OPERAND (*expr_p, 1);
578 : 4239 : tree e = ubsan_instrument_bounds (EXPR_LOCATION (*expr_p), op0, &op1,
579 : : ignore_off_by_one);
580 : 4239 : if (e != NULL_TREE)
581 : 1486 : TREE_OPERAND (*expr_p, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (op1),
582 : : e, op1);
583 : : }
584 : 4249 : }
585 : :
586 : : static tree
587 : 6816 : ubsan_maybe_instrument_reference_or_call (location_t loc, tree op, tree ptype,
588 : : enum ubsan_null_ckind ckind)
589 : : {
590 : 6816 : if (!sanitize_flags_p (SANITIZE_ALIGNMENT | SANITIZE_NULL)
591 : 6816 : || current_function_decl == NULL_TREE)
592 : : return NULL_TREE;
593 : :
594 : 6816 : tree type = TREE_TYPE (ptype);
595 : 6816 : tree orig_op = op;
596 : 6816 : bool instrument = false;
597 : 6816 : unsigned int mina = 0;
598 : :
599 : 6816 : if (sanitize_flags_p (SANITIZE_ALIGNMENT))
600 : : {
601 : 6586 : mina = min_align_of_type (type);
602 : 6586 : if (mina <= 1)
603 : 1067 : mina = 0;
604 : : }
605 : 9178 : while ((TREE_CODE (op) == NOP_EXPR
606 : 9178 : || TREE_CODE (op) == NON_LVALUE_EXPR)
607 : 9178 : && TREE_CODE (TREE_TYPE (op)) == POINTER_TYPE)
608 : 2362 : op = TREE_OPERAND (op, 0);
609 : 6816 : if (TREE_CODE (op) == NOP_EXPR
610 : 6816 : && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE)
611 : : {
612 : 0 : if (mina && mina > min_align_of_type (TREE_TYPE (TREE_TYPE (op))))
613 : : instrument = true;
614 : : }
615 : : else
616 : : {
617 : 6816 : if (sanitize_flags_p (SANITIZE_NULL) && TREE_CODE (op) == ADDR_EXPR)
618 : : {
619 : 3725 : bool strict_overflow_p = false;
620 : : /* tree_single_nonzero_warnv_p will not return true for non-weak
621 : : non-automatic decls with -fno-delete-null-pointer-checks,
622 : : which is disabled during -fsanitize=null. We don't want to
623 : : instrument those, just weak vars though. */
624 : 3725 : int save_flag_delete_null_pointer_checks
625 : : = flag_delete_null_pointer_checks;
626 : 3725 : flag_delete_null_pointer_checks = 1;
627 : 3725 : if (!tree_single_nonzero_warnv_p (op, &strict_overflow_p)
628 : 3725 : || strict_overflow_p)
629 : : instrument = true;
630 : 3725 : flag_delete_null_pointer_checks
631 : 3725 : = save_flag_delete_null_pointer_checks;
632 : : }
633 : 3091 : else if (sanitize_flags_p (SANITIZE_NULL))
634 : : instrument = true;
635 : 6816 : if (mina && mina > 1)
636 : : {
637 : 6779 : if (!POINTER_TYPE_P (TREE_TYPE (op))
638 : 6779 : || mina > get_pointer_alignment (op) / BITS_PER_UNIT)
639 : : instrument = true;
640 : : }
641 : : }
642 : 2909 : if (!instrument)
643 : 2227 : return NULL_TREE;
644 : 4589 : op = save_expr (orig_op);
645 : 4589 : gcc_assert (POINTER_TYPE_P (ptype));
646 : 4589 : if (TREE_CODE (ptype) == REFERENCE_TYPE)
647 : 1618 : ptype = build_pointer_type (TREE_TYPE (ptype));
648 : 4589 : tree kind = build_int_cst (ptype, ckind);
649 : 4589 : tree align = build_int_cst (pointer_sized_int_node, mina);
650 : 4589 : tree call
651 : 4589 : = build_call_expr_internal_loc (loc, IFN_UBSAN_NULL, void_type_node,
652 : : 3, op, kind, align);
653 : 4589 : TREE_SIDE_EFFECTS (call) = 1;
654 : 4589 : return fold_build2 (COMPOUND_EXPR, TREE_TYPE (op), call, op);
655 : : }
656 : :
657 : : /* Instrument a NOP_EXPR to REFERENCE_TYPE or INTEGER_CST with REFERENCE_TYPE
658 : : type if needed. */
659 : :
660 : : void
661 : 2189 : ubsan_maybe_instrument_reference (tree *stmt_p)
662 : : {
663 : 2189 : tree stmt = *stmt_p;
664 : 2189 : tree op = stmt;
665 : 2189 : if (TREE_CODE (stmt) == NOP_EXPR)
666 : 2179 : op = TREE_OPERAND (stmt, 0);
667 : 2189 : op = ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt), op,
668 : 2189 : TREE_TYPE (stmt),
669 : : UBSAN_REF_BINDING);
670 : 2189 : if (op)
671 : : {
672 : 1618 : if (TREE_CODE (stmt) == NOP_EXPR)
673 : 1608 : TREE_OPERAND (stmt, 0) = op;
674 : : else
675 : 10 : *stmt_p = op;
676 : : }
677 : 2189 : }
678 : :
679 : : /* Instrument a CALL_EXPR to a method if needed. */
680 : :
681 : : void
682 : 4627 : ubsan_maybe_instrument_member_call (tree stmt, bool is_ctor)
683 : : {
684 : 4627 : if (call_expr_nargs (stmt) == 0)
685 : : return;
686 : 4627 : tree op = CALL_EXPR_ARG (stmt, 0);
687 : 4627 : if (op == error_mark_node
688 : 4627 : || !POINTER_TYPE_P (TREE_TYPE (op)))
689 : : return;
690 : 9254 : op = ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt), op,
691 : 4627 : TREE_TYPE (op),
692 : : is_ctor ? UBSAN_CTOR_CALL
693 : : : UBSAN_MEMBER_CALL);
694 : 4627 : if (op)
695 : 2971 : CALL_EXPR_ARG (stmt, 0) = op;
696 : : }
|