]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/cp-ubsan.c
c0d1ffcfc43b614f92292e55f07b7a06ff2ff19e
[thirdparty/gcc.git] / gcc / cp / cp-ubsan.c
1 /* UndefinedBehaviorSanitizer, undefined behavior detector.
2 Copyright (C) 2014 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <jakub@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 "alias.h"
25 #include "symtab.h"
26 #include "options.h"
27 #include "tree.h"
28 #include "alloc-pool.h"
29 #include "output.h"
30 #include "toplev.h"
31 #include "ubsan.h"
32 #include "cp-tree.h"
33 #include "c-family/c-common.h"
34 #include "c-family/c-ubsan.h"
35 #include "asan.h"
36 #include "internal-fn.h"
37 #include "stor-layout.h"
38 #include "builtins.h"
39 #include "fold-const.h"
40 #include "stringpool.h"
41 #include "predict.h"
42 #include "tree-ssa-alias.h"
43 #include "basic-block.h"
44 #include "gimple-expr.h"
45 #include "gimple.h"
46 #include "lto-streamer.h"
47 #include "cgraph.h"
48
49 /* Test if we should instrument vptr access. */
50
51 static bool
52 cp_ubsan_instrument_vptr_p (tree type)
53 {
54 if (!flag_rtti || flag_sanitize_undefined_trap_on_error)
55 return false;
56
57 if (current_function_decl
58 && lookup_attribute ("no_sanitize_undefined",
59 DECL_ATTRIBUTES (current_function_decl)))
60 return false;
61
62 if (type)
63 {
64 type = TYPE_MAIN_VARIANT (type);
65 if (!CLASS_TYPE_P (type) || !CLASSTYPE_VTABLES (type))
66 return false;
67 }
68
69 return true;
70 }
71
72 /* Helper function for
73 cp_ubsan_maybe_instrument_{member_{call,access},downcast}.
74 Instrument vptr access. */
75
76 static tree
77 cp_ubsan_instrument_vptr (location_t loc, tree op, tree type, bool is_addr,
78 enum ubsan_null_ckind ckind)
79 {
80 type = TYPE_MAIN_VARIANT (type);
81 const char *mangled = mangle_type_string (type);
82 hashval_t str_hash1 = htab_hash_string (mangled);
83 hashval_t str_hash2 = iterative_hash (mangled, strlen (mangled), 0);
84 tree str_hash = wide_int_to_tree (uint64_type_node,
85 wi::uhwi (((uint64_t) str_hash1 << 32)
86 | str_hash2, 64));
87 if (!is_addr)
88 op = build_fold_addr_expr_loc (loc, op);
89 op = save_expr (op);
90 tree vptr = fold_build3_loc (loc, COMPONENT_REF,
91 TREE_TYPE (TYPE_VFIELD (type)),
92 build_fold_indirect_ref_loc (loc, op),
93 TYPE_VFIELD (type), NULL_TREE);
94 vptr = fold_convert_loc (loc, pointer_sized_int_node, vptr);
95 vptr = fold_convert_loc (loc, uint64_type_node, vptr);
96 if (ckind == UBSAN_DOWNCAST_POINTER)
97 vptr = fold_build3 (COND_EXPR, uint64_type_node,
98 fold_build2 (NE_EXPR, boolean_type_node, op,
99 build_zero_cst (TREE_TYPE (op))),
100 vptr, build_int_cst (uint64_type_node, 0));
101 tree ti_decl = get_tinfo_decl (type);
102 mark_used (ti_decl);
103 tree ptype = build_pointer_type (type);
104 tree call
105 = build_call_expr_internal_loc (loc, IFN_UBSAN_VPTR,
106 void_type_node, 5, op, vptr, str_hash,
107 build_address (ti_decl),
108 build_int_cst (ptype, ckind));
109 TREE_SIDE_EFFECTS (call) = 1;
110 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (op), call, op);
111 }
112
113 /* Helper function for
114 cp_ubsan_maybe_instrument_{member_{call,access},downcast}.
115 Instrument vptr access if it should be instrumented, otherwise return
116 NULL_TREE. */
117
118 static tree
119 cp_ubsan_maybe_instrument_vptr (location_t loc, tree op, tree type,
120 bool is_addr, enum ubsan_null_ckind ckind)
121 {
122 if (!cp_ubsan_instrument_vptr_p (type))
123 return NULL_TREE;
124 return cp_ubsan_instrument_vptr (loc, op, type, is_addr, ckind);
125 }
126
127 /* Instrument a member call (but not constructor call) if needed. */
128
129 void
130 cp_ubsan_maybe_instrument_member_call (tree stmt)
131 {
132 if (call_expr_nargs (stmt) == 0)
133 return;
134 tree *opp = &CALL_EXPR_ARG (stmt, 0);
135 tree op = *opp;
136 if (op == error_mark_node
137 || !POINTER_TYPE_P (TREE_TYPE (op)))
138 return;
139 while (TREE_CODE (op) == COMPOUND_EXPR)
140 {
141 opp = &TREE_OPERAND (op, 1);
142 op = *opp;
143 }
144 op = cp_ubsan_maybe_instrument_vptr (EXPR_LOCATION (stmt), op,
145 TREE_TYPE (TREE_TYPE (op)),
146 true, UBSAN_MEMBER_CALL);
147 if (op)
148 *opp = op;
149 }
150
151 /* Data passed to cp_ubsan_check_member_access_r. */
152
153 struct cp_ubsan_check_member_access_data
154 {
155 hash_set<tree> *pset;
156 bool is_addr;
157 };
158
159 static tree cp_ubsan_check_member_access_r (tree *, int *, void *);
160
161 /* Instrument a member access. */
162
163 static bool
164 cp_ubsan_maybe_instrument_member_access
165 (tree stmt, cp_ubsan_check_member_access_data *ucmd)
166 {
167 if (DECL_ARTIFICIAL (TREE_OPERAND (stmt, 1)))
168 return false;
169
170 tree base = TREE_OPERAND (stmt, 0);
171 if (!cp_ubsan_instrument_vptr_p (TREE_TYPE (base)))
172 return false;
173
174 cp_walk_tree (&base, cp_ubsan_check_member_access_r, ucmd, ucmd->pset);
175
176 base = cp_ubsan_instrument_vptr (EXPR_LOCATION (stmt), base,
177 TREE_TYPE (base), false,
178 UBSAN_MEMBER_ACCESS);
179 TREE_OPERAND (stmt, 0)
180 = build_fold_indirect_ref_loc (EXPR_LOCATION (stmt), base);
181 return true;
182 }
183
184 /* Attempt to instrument member accesses inside of the function.
185 cp_ubsan_maybe_instrument_member_access should be called on COMPONENT_REFs
186 in the GENERIC IL, but only when the field is actually accessed, not
187 merely when it's address is taken. Therefore we track in is_addr field
188 whether in the current context we are processing address taken
189 handled components or not. E.g. for &x->y[w->z] we want to call
190 cp_ubsan_maybe_instrument_member_access on *w.z COMPONENT_REF, but
191 not on *x.y. */
192
193 static tree
194 cp_ubsan_check_member_access_r (tree *stmt_p, int *walk_subtrees, void *data)
195 {
196 tree stmt = *stmt_p, t;
197 cp_ubsan_check_member_access_data *ucmd
198 = (cp_ubsan_check_member_access_data *) data;
199 switch (TREE_CODE (stmt))
200 {
201 case ADDR_EXPR:
202 t = TREE_OPERAND (stmt, 0);
203 while ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
204 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
205 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
206 if (handled_component_p (t))
207 {
208 *walk_subtrees = 0;
209 ucmd->is_addr = true;
210 cp_walk_tree (&t, cp_ubsan_check_member_access_r,
211 data, ucmd->pset);
212 ucmd->is_addr = false;
213 }
214 break;
215 case MEM_REF:
216 case INDIRECT_REF:
217 t = TREE_OPERAND (stmt, 0);
218 if (TREE_CODE (t) == ADDR_EXPR)
219 {
220 *walk_subtrees = 0;
221 t = TREE_OPERAND (stmt, 0);
222 cp_walk_tree (&t, cp_ubsan_check_member_access_r, data, ucmd->pset);
223 }
224 break;
225 case COMPONENT_REF:
226 if (!ucmd->is_addr && cp_ubsan_maybe_instrument_member_access (stmt, ucmd))
227 {
228 *walk_subtrees = 0;
229 break;
230 }
231 /* FALLTHRU */
232 default:
233 if (ucmd->is_addr && handled_component_p (stmt))
234 {
235 int i, len = TREE_OPERAND_LENGTH (stmt);
236 *walk_subtrees = 0;
237 if (!handled_component_p (TREE_OPERAND (stmt, 0)))
238 ucmd->is_addr = false;
239 for (i = 0; i < len; i++)
240 {
241 cp_walk_tree (&TREE_OPERAND (stmt, i),
242 cp_ubsan_check_member_access_r, data, ucmd->pset);
243 ucmd->is_addr = false;
244 }
245 ucmd->is_addr = true;
246 }
247 break;
248 }
249 return NULL_TREE;
250 }
251
252 /* Instrument all member accesses inside GENERIC *T_P. */
253
254 void
255 cp_ubsan_instrument_member_accesses (tree *t_p)
256 {
257 if (cp_ubsan_instrument_vptr_p (NULL_TREE))
258 {
259 hash_set<tree> pset;
260 cp_ubsan_check_member_access_data ucmd;
261 ucmd.pset = &pset;
262 ucmd.is_addr = false;
263 cp_walk_tree (t_p, cp_ubsan_check_member_access_r, &ucmd, &pset);
264 }
265 }
266
267 /* Instrument downcast. */
268
269 tree
270 cp_ubsan_maybe_instrument_downcast (location_t loc, tree type, tree op)
271 {
272 if (!POINTER_TYPE_P (type)
273 || !POINTER_TYPE_P (TREE_TYPE (op))
274 || !CLASS_TYPE_P (TREE_TYPE (type))
275 || !CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
276 || !DERIVED_FROM_P (TREE_TYPE (TREE_TYPE (op)), TREE_TYPE (type)))
277 return NULL_TREE;
278
279 return cp_ubsan_maybe_instrument_vptr (loc, op, TREE_TYPE (type), true,
280 TREE_CODE (type) == POINTER_TYPE
281 ? UBSAN_DOWNCAST_POINTER
282 : UBSAN_DOWNCAST_REFERENCE);
283 }
284
285 /* Instrument cast to virtual base. */
286
287 tree
288 cp_ubsan_maybe_instrument_cast_to_vbase (location_t loc, tree type, tree op)
289 {
290 return cp_ubsan_maybe_instrument_vptr (loc, op, type, true,
291 UBSAN_CAST_TO_VBASE);
292 }