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