]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-ubsan.c
Daily bump.
[thirdparty/gcc.git] / gcc / c-family / c-ubsan.c
CommitLineData
de5a5fa1 1/* UndefinedBehaviorSanitizer, undefined behavior detector.
cbe34bb5 2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
de5a5fa1
MP
3 Contributed by Marek Polacek <polacek@redhat.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along 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"
c582198b 24#include "tm.h"
2adfab87 25#include "c-family/c-common.h"
de5a5fa1 26#include "ubsan.h"
de5a5fa1 27#include "c-family/c-ubsan.h"
6525783a 28#include "asan.h"
944fa280
JJ
29#include "stor-layout.h"
30#include "builtins.h"
dc891fe7 31#include "gimplify.h"
de5a5fa1
MP
32
33/* Instrument division by zero and INT_MIN / -1. If not instrumenting,
34 return NULL_TREE. */
35
36tree
37ubsan_instrument_division (location_t loc, tree op0, tree op1)
38{
39 tree t, tt;
40 tree type = TREE_TYPE (op0);
41
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)));
47
dc891fe7
MP
48 op0 = unshare_expr (op0);
49 op1 = unshare_expr (op1);
50
f8ed5150 51 if (TREE_CODE (type) == INTEGER_TYPE
45b2222a 52 && sanitize_flags_p (SANITIZE_DIVIDE))
f8ed5150
MP
53 t = fold_build2 (EQ_EXPR, boolean_type_node,
54 op1, build_int_cst (type, 0));
55 else if (TREE_CODE (type) == REAL_TYPE
45b2222a 56 && sanitize_flags_p (SANITIZE_FLOAT_DIVIDE))
f8ed5150
MP
57 t = fold_build2 (EQ_EXPR, boolean_type_node,
58 op1, build_real (type, dconst0));
59 else
de5a5fa1
MP
60 return NULL_TREE;
61
de5a5fa1 62 /* We check INT_MIN / -1 only for signed types. */
f8ed5150 63 if (TREE_CODE (type) == INTEGER_TYPE
45b2222a 64 && sanitize_flags_p (SANITIZE_DIVIDE)
f8ed5150 65 && !TYPE_UNSIGNED (type))
de5a5fa1
MP
66 {
67 tree x;
5a5062b8 68 tt = fold_build2 (EQ_EXPR, boolean_type_node, unshare_expr (op1),
de5a5fa1
MP
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);
74 }
75
b56e9788
MP
76 /* If the condition was folded to 0, no need to instrument
77 this expression. */
78 if (integer_zerop (t))
79 return NULL_TREE;
80
de5a5fa1 81 /* In case we have a SAVE_EXPR in a conditional context, we need to
974348ee 82 make sure it gets evaluated before the condition. */
5a5062b8 83 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op0), t);
15dbc1a6 84 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op1), t);
1c33c9b7
JJ
85 if (flag_sanitize_undefined_trap_on_error)
86 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
87 else
88 {
570a11fe
JJ
89 tree data = ubsan_create_data ("__ubsan_overflow_data", 1, &loc,
90 ubsan_type_descriptor (type), NULL_TREE,
91 NULL_TREE);
1c33c9b7
JJ
92 data = build_fold_addr_expr_loc (loc, data);
93 enum built_in_function bcode
d95a2703 94 = (flag_sanitize_recover & SANITIZE_DIVIDE)
1c33c9b7
JJ
95 ? BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW
96 : BUILT_IN_UBSAN_HANDLE_DIVREM_OVERFLOW_ABORT;
97 tt = builtin_decl_explicit (bcode);
5a5062b8
MP
98 op0 = unshare_expr (op0);
99 op1 = unshare_expr (op1);
1c33c9b7
JJ
100 tt = build_call_expr_loc (loc, tt, 3, data, ubsan_encode_value (op0),
101 ubsan_encode_value (op1));
102 }
632f2871 103 t = fold_build3 (COND_EXPR, void_type_node, t, tt, void_node);
de5a5fa1
MP
104
105 return t;
106}
107
b906f4ca 108/* Instrument left and right shifts. */
de5a5fa1
MP
109
110tree
111ubsan_instrument_shift (location_t loc, enum tree_code code,
112 tree op0, tree op1)
113{
114 tree t, tt = NULL_TREE;
115 tree type0 = TREE_TYPE (op0);
116 tree type1 = TREE_TYPE (op1);
04a32443
JJ
117 if (!INTEGRAL_TYPE_P (type0))
118 return NULL_TREE;
119
de5a5fa1
MP
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);
de5a5fa1 123
dc891fe7
MP
124 op0 = unshare_expr (op0);
125 op1 = unshare_expr (op1);
126
de5a5fa1
MP
127 t = fold_convert_loc (loc, op1_utype, op1);
128 t = fold_build2 (GT_EXPR, boolean_type_node, t, uprecm1);
129
b3007644
PB
130 /* If this is not a signed operation, don't perform overflow checks.
131 Also punt on bit-fields. */
04a32443 132 if (TYPE_OVERFLOW_WRAPS (type0)
2e955d50 133 || GET_MODE_BITSIZE (TYPE_MODE (type0)) != TYPE_PRECISION (type0)
45b2222a 134 || !sanitize_flags_p (SANITIZE_SHIFT_BASE))
b3007644
PB
135 ;
136
de5a5fa1 137 /* For signed x << y, in C99/C11, the following:
59d7607a 138 (unsigned) x >> (uprecm1 - y)
de5a5fa1 139 if non-zero, is undefined. */
b3007644 140 else if (code == LSHIFT_EXPR && flag_isoc99 && cxx_dialect < cxx11)
de5a5fa1 141 {
541e35a6 142 tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
5a5062b8 143 fold_convert (op1_utype, unshare_expr (op1)));
de5a5fa1
MP
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));
148 }
149
e4276ba5 150 /* For signed x << y, in C++11 and later, the following:
59d7607a 151 x < 0 || ((unsigned) x >> (uprecm1 - y))
de5a5fa1 152 if > 1, is undefined. */
b3007644 153 else if (code == LSHIFT_EXPR && cxx_dialect >= cxx11)
de5a5fa1 154 {
6a4da643 155 tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
5a5062b8
MP
156 fold_convert (op1_utype, unshare_expr (op1)));
157 tt = fold_convert_loc (loc, unsigned_type_for (type0),
158 unshare_expr (op0));
de5a5fa1
MP
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));
5a5062b8 162 x = fold_build2 (LT_EXPR, boolean_type_node, unshare_expr (op0),
de5a5fa1
MP
163 build_int_cst (type0, 0));
164 tt = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, x, tt);
165 }
166
b56e9788
MP
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;
171
de5a5fa1 172 /* In case we have a SAVE_EXPR in a conditional context, we need to
974348ee 173 make sure it gets evaluated before the condition. */
5a5062b8 174 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op0), t);
3ee0fb02 175 t = fold_build2 (COMPOUND_EXPR, TREE_TYPE (t), unshare_expr (op1), t);
2e955d50
JJ
176
177 enum sanitize_code recover_kind = SANITIZE_SHIFT_EXPONENT;
178 tree else_t = void_node;
179 if (tt)
180 {
45b2222a 181 if (!sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
2e955d50
JJ
182 {
183 t = fold_build1 (TRUTH_NOT_EXPR, boolean_type_node, t);
184 t = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, t, tt);
185 recover_kind = SANITIZE_SHIFT_BASE;
186 }
187 else
188 {
189 if (flag_sanitize_undefined_trap_on_error
190 || ((!(flag_sanitize_recover & SANITIZE_SHIFT_EXPONENT))
191 == (!(flag_sanitize_recover & SANITIZE_SHIFT_BASE))))
192 t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, tt);
193 else
194 else_t = tt;
195 }
196 }
1c33c9b7
JJ
197
198 if (flag_sanitize_undefined_trap_on_error)
199 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
200 else
201 {
570a11fe 202 tree data = ubsan_create_data ("__ubsan_shift_data", 1, &loc,
0e37a2f3 203 ubsan_type_descriptor (type0),
570a11fe
JJ
204 ubsan_type_descriptor (type1), NULL_TREE,
205 NULL_TREE);
1c33c9b7
JJ
206 data = build_fold_addr_expr_loc (loc, data);
207
208 enum built_in_function bcode
2e955d50 209 = (flag_sanitize_recover & recover_kind)
1c33c9b7
JJ
210 ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
211 : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT;
212 tt = builtin_decl_explicit (bcode);
5a5062b8
MP
213 op0 = unshare_expr (op0);
214 op1 = unshare_expr (op1);
1c33c9b7
JJ
215 tt = build_call_expr_loc (loc, tt, 3, data, ubsan_encode_value (op0),
216 ubsan_encode_value (op1));
2e955d50
JJ
217 if (else_t != void_node)
218 {
219 bcode = (flag_sanitize_recover & SANITIZE_SHIFT_BASE)
220 ? BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS
221 : BUILT_IN_UBSAN_HANDLE_SHIFT_OUT_OF_BOUNDS_ABORT;
222 tree else_tt = builtin_decl_explicit (bcode);
223 op0 = unshare_expr (op0);
224 op1 = unshare_expr (op1);
225 else_tt = build_call_expr_loc (loc, else_tt, 3, data,
226 ubsan_encode_value (op0),
227 ubsan_encode_value (op1));
228 else_t = fold_build3 (COND_EXPR, void_type_node, else_t,
229 else_tt, void_node);
230 }
1c33c9b7 231 }
2e955d50 232 t = fold_build3 (COND_EXPR, void_type_node, t, tt, else_t);
de5a5fa1
MP
233
234 return t;
235}
b906f4ca
MP
236
237/* Instrument variable length array bound. */
238
239tree
240ubsan_instrument_vla (location_t loc, tree size)
241{
242 tree type = TREE_TYPE (size);
243 tree t, tt;
244
245 t = fold_build2 (LE_EXPR, boolean_type_node, size, build_int_cst (type, 0));
1c33c9b7
JJ
246 if (flag_sanitize_undefined_trap_on_error)
247 tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
248 else
249 {
570a11fe
JJ
250 tree data = ubsan_create_data ("__ubsan_vla_data", 1, &loc,
251 ubsan_type_descriptor (type), NULL_TREE,
252 NULL_TREE);
1c33c9b7
JJ
253 data = build_fold_addr_expr_loc (loc, data);
254 enum built_in_function bcode
d95a2703 255 = (flag_sanitize_recover & SANITIZE_VLA)
1c33c9b7
JJ
256 ? BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE
257 : BUILT_IN_UBSAN_HANDLE_VLA_BOUND_NOT_POSITIVE_ABORT;
258 tt = builtin_decl_explicit (bcode);
259 tt = build_call_expr_loc (loc, tt, 2, data, ubsan_encode_value (size));
260 }
632f2871 261 t = fold_build3 (COND_EXPR, void_type_node, t, tt, void_node);
b906f4ca
MP
262
263 return t;
264}
0a508bb6
JJ
265
266/* Instrument missing return in C++ functions returning non-void. */
267
268tree
269ubsan_instrument_return (location_t loc)
270{
1c33c9b7
JJ
271 if (flag_sanitize_undefined_trap_on_error)
272 return build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
6525783a 273
570a11fe
JJ
274 tree data = ubsan_create_data ("__ubsan_missing_return_data", 1, &loc,
275 NULL_TREE, NULL_TREE);
0a508bb6
JJ
276 tree t = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_MISSING_RETURN);
277 return build_call_expr_loc (loc, t, 1, build_fold_addr_expr_loc (loc, data));
278}
0e37a2f3
MP
279
280/* Instrument array bounds for ARRAY_REFs. We create special builtin,
281 that gets expanded in the sanopt pass, and make an array dimension
282 of it. ARRAY is the array, *INDEX is an index to the array.
283 Return NULL_TREE if no instrumentation is emitted.
284 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
285
286tree
287ubsan_instrument_bounds (location_t loc, tree array, tree *index,
288 bool ignore_off_by_one)
289{
290 tree type = TREE_TYPE (array);
291 tree domain = TYPE_DOMAIN (type);
292
4d661eaa 293 if (domain == NULL_TREE || TYPE_MAX_VALUE (domain) == NULL_TREE)
0e37a2f3
MP
294 return NULL_TREE;
295
296 tree bound = TYPE_MAX_VALUE (domain);
297 if (ignore_off_by_one)
298 bound = fold_build2 (PLUS_EXPR, TREE_TYPE (bound), bound,
299 build_int_cst (TREE_TYPE (bound), 1));
300
e0f0d3b9
MP
301 /* Detect flexible array members and suchlike, unless
302 -fsanitize=bounds-strict. */
0e37a2f3 303 tree base = get_base_address (array);
45b2222a 304 if (!sanitize_flags_p (SANITIZE_BOUNDS_STRICT)
e0f0d3b9 305 && TREE_CODE (array) == COMPONENT_REF
22d03525 306 && base && (INDIRECT_REF_P (base) || TREE_CODE (base) == MEM_REF))
0e37a2f3
MP
307 {
308 tree next = NULL_TREE;
309 tree cref = array;
310
311 /* Walk all structs/unions. */
312 while (TREE_CODE (cref) == COMPONENT_REF)
313 {
314 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
315 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
316 next && TREE_CODE (next) != FIELD_DECL;
317 next = DECL_CHAIN (next))
318 ;
319 if (next)
320 /* Not a last element. Instrument it. */
321 break;
322 /* Ok, this is the last field of the structure/union. But the
323 aggregate containing the field must be the last field too,
324 recursively. */
325 cref = TREE_OPERAND (cref, 0);
326 }
327 if (!next)
328 /* Don't instrument this flexible array member-like array in non-strict
329 -fsanitize=bounds mode. */
330 return NULL_TREE;
331 }
332
570a11fe
JJ
333 /* Don't emit instrumentation in the most common cases. */
334 tree idx = NULL_TREE;
335 if (TREE_CODE (*index) == INTEGER_CST)
336 idx = *index;
337 else if (TREE_CODE (*index) == BIT_AND_EXPR
338 && TREE_CODE (TREE_OPERAND (*index, 1)) == INTEGER_CST)
339 idx = TREE_OPERAND (*index, 1);
340 if (idx
341 && TREE_CODE (bound) == INTEGER_CST
342 && tree_int_cst_sgn (idx) >= 0
343 && tree_int_cst_le (idx, bound))
344 return NULL_TREE;
345
0e37a2f3
MP
346 *index = save_expr (*index);
347 /* Create a "(T *) 0" tree node to describe the array type. */
348 tree zero_with_type = build_int_cst (build_pointer_type (type), 0);
349 return build_call_expr_internal_loc (loc, IFN_UBSAN_BOUNDS,
350 void_type_node, 3, zero_with_type,
351 *index, bound);
352}
353
354/* Return true iff T is an array that was instrumented by SANITIZE_BOUNDS. */
355
356bool
357ubsan_array_ref_instrumented_p (const_tree t)
358{
359 if (TREE_CODE (t) != ARRAY_REF)
360 return false;
361
362 tree op1 = TREE_OPERAND (t, 1);
363 return TREE_CODE (op1) == COMPOUND_EXPR
364 && TREE_CODE (TREE_OPERAND (op1, 0)) == CALL_EXPR
365 && CALL_EXPR_FN (TREE_OPERAND (op1, 0)) == NULL_TREE
366 && CALL_EXPR_IFN (TREE_OPERAND (op1, 0)) == IFN_UBSAN_BOUNDS;
367}
368
369/* Instrument an ARRAY_REF, if it hasn't already been instrumented.
370 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
371
372void
373ubsan_maybe_instrument_array_ref (tree *expr_p, bool ignore_off_by_one)
374{
375 if (!ubsan_array_ref_instrumented_p (*expr_p)
f34ebeb2
ML
376 && sanitize_flags_p (SANITIZE_BOUNDS | SANITIZE_BOUNDS_STRICT)
377 && current_function_decl != NULL_TREE)
0e37a2f3
MP
378 {
379 tree op0 = TREE_OPERAND (*expr_p, 0);
380 tree op1 = TREE_OPERAND (*expr_p, 1);
381 tree e = ubsan_instrument_bounds (EXPR_LOCATION (*expr_p), op0, &op1,
382 ignore_off_by_one);
383 if (e != NULL_TREE)
384 {
385 tree t = copy_node (*expr_p);
386 TREE_OPERAND (t, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (op1),
387 e, op1);
388 *expr_p = t;
389 }
390 }
391}
944fa280
JJ
392
393static tree
c39a5e99 394ubsan_maybe_instrument_reference_or_call (location_t loc, tree op, tree ptype,
944fa280
JJ
395 enum ubsan_null_ckind ckind)
396{
f34ebeb2
ML
397 if (!sanitize_flags_p (SANITIZE_ALIGNMENT | SANITIZE_NULL)
398 || current_function_decl == NULL_TREE)
944fa280
JJ
399 return NULL_TREE;
400
c39a5e99
JJ
401 tree type = TREE_TYPE (ptype);
402 tree orig_op = op;
403 bool instrument = false;
404 unsigned int mina = 0;
405
45b2222a 406 if (sanitize_flags_p (SANITIZE_ALIGNMENT))
944fa280
JJ
407 {
408 mina = min_align_of_type (type);
409 if (mina <= 1)
410 mina = 0;
411 }
412 while ((TREE_CODE (op) == NOP_EXPR
413 || TREE_CODE (op) == NON_LVALUE_EXPR)
414 && TREE_CODE (TREE_TYPE (op)) == POINTER_TYPE)
415 op = TREE_OPERAND (op, 0);
416 if (TREE_CODE (op) == NOP_EXPR
417 && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE)
418 {
419 if (mina && mina > min_align_of_type (TREE_TYPE (TREE_TYPE (op))))
420 instrument = true;
421 }
422 else
423 {
45b2222a 424 if (sanitize_flags_p (SANITIZE_NULL) && TREE_CODE (op) == ADDR_EXPR)
944fa280
JJ
425 {
426 bool strict_overflow_p = false;
427 /* tree_single_nonzero_warnv_p will not return true for non-weak
428 non-automatic decls with -fno-delete-null-pointer-checks,
429 which is disabled during -fsanitize=null. We don't want to
430 instrument those, just weak vars though. */
431 int save_flag_delete_null_pointer_checks
432 = flag_delete_null_pointer_checks;
433 flag_delete_null_pointer_checks = 1;
434 if (!tree_single_nonzero_warnv_p (op, &strict_overflow_p)
435 || strict_overflow_p)
436 instrument = true;
437 flag_delete_null_pointer_checks
438 = save_flag_delete_null_pointer_checks;
439 }
45b2222a 440 else if (sanitize_flags_p (SANITIZE_NULL))
944fa280 441 instrument = true;
c39a5e99
JJ
442 if (mina && mina > 1)
443 {
444 if (!POINTER_TYPE_P (TREE_TYPE (op))
445 || mina > get_pointer_alignment (op) / BITS_PER_UNIT)
446 instrument = true;
447 }
944fa280
JJ
448 }
449 if (!instrument)
450 return NULL_TREE;
451 op = save_expr (orig_op);
c39a5e99
JJ
452 gcc_assert (POINTER_TYPE_P (ptype));
453 if (TREE_CODE (ptype) == REFERENCE_TYPE)
454 ptype = build_pointer_type (TREE_TYPE (ptype));
455 tree kind = build_int_cst (ptype, ckind);
944fa280
JJ
456 tree align = build_int_cst (pointer_sized_int_node, mina);
457 tree call
458 = build_call_expr_internal_loc (loc, IFN_UBSAN_NULL, void_type_node,
459 3, op, kind, align);
460 TREE_SIDE_EFFECTS (call) = 1;
461 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (op), call, op);
462}
463
6f3af356
JJ
464/* Instrument a NOP_EXPR to REFERENCE_TYPE or INTEGER_CST with REFERENCE_TYPE
465 type if needed. */
944fa280
JJ
466
467void
6f3af356 468ubsan_maybe_instrument_reference (tree *stmt_p)
944fa280 469{
6f3af356
JJ
470 tree stmt = *stmt_p;
471 tree op = stmt;
472 if (TREE_CODE (stmt) == NOP_EXPR)
473 op = TREE_OPERAND (stmt, 0);
944fa280 474 op = ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt), op,
c39a5e99 475 TREE_TYPE (stmt),
944fa280
JJ
476 UBSAN_REF_BINDING);
477 if (op)
6f3af356
JJ
478 {
479 if (TREE_CODE (stmt) == NOP_EXPR)
480 TREE_OPERAND (stmt, 0) = op;
481 else
482 *stmt_p = op;
483 }
944fa280
JJ
484}
485
486/* Instrument a CALL_EXPR to a method if needed. */
487
488void
489ubsan_maybe_instrument_member_call (tree stmt, bool is_ctor)
490{
491 if (call_expr_nargs (stmt) == 0)
492 return;
493 tree op = CALL_EXPR_ARG (stmt, 0);
494 if (op == error_mark_node
495 || !POINTER_TYPE_P (TREE_TYPE (op)))
496 return;
497 op = ubsan_maybe_instrument_reference_or_call (EXPR_LOCATION (stmt), op,
c39a5e99 498 TREE_TYPE (op),
944fa280
JJ
499 is_ctor ? UBSAN_CTOR_CALL
500 : UBSAN_MEMBER_CALL);
501 if (op)
502 CALL_EXPR_ARG (stmt, 0) = op;
503}