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