annotate gcc/c-family/c-ubsan.c @ 145:1830386684a0

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