gcc/ChangeLog
[official-gcc.git] / gcc / cp / cp-ubsan.c
blob6486218e8da50455a6b3d594d660b9b0f048973b
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 "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 "output.h"
32 #include "toplev.h"
33 #include "ubsan.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 "cgraph.h"
44 /* Test if we should instrument vptr access. */
46 static bool
47 cp_ubsan_instrument_vptr_p (tree type)
49 if (!flag_rtti || flag_sanitize_undefined_trap_on_error)
50 return false;
52 if (current_function_decl
53 && lookup_attribute ("no_sanitize_undefined",
54 DECL_ATTRIBUTES (current_function_decl)))
55 return false;
57 if (type)
59 type = TYPE_MAIN_VARIANT (type);
60 if (!CLASS_TYPE_P (type) || !CLASSTYPE_VTABLES (type))
61 return false;
64 return true;
67 /* Helper function for
68 cp_ubsan_maybe_instrument_{member_{call,access},downcast}.
69 Instrument vptr access. */
71 static tree
72 cp_ubsan_instrument_vptr (location_t loc, tree op, tree type, bool is_addr,
73 enum ubsan_null_ckind ckind)
75 type = TYPE_MAIN_VARIANT (type);
76 const char *mangled = mangle_type_string (type);
77 hashval_t str_hash1 = htab_hash_string (mangled);
78 hashval_t str_hash2 = iterative_hash (mangled, strlen (mangled), 0);
79 tree str_hash = wide_int_to_tree (uint64_type_node,
80 wi::uhwi (((uint64_t) str_hash1 << 32)
81 | str_hash2, 64));
82 if (!is_addr)
83 op = build_fold_addr_expr_loc (loc, op);
84 op = save_expr (op);
85 tree vptr = fold_build3_loc (loc, COMPONENT_REF,
86 TREE_TYPE (TYPE_VFIELD (type)),
87 build_fold_indirect_ref_loc (loc, op),
88 TYPE_VFIELD (type), NULL_TREE);
89 vptr = fold_convert_loc (loc, pointer_sized_int_node, vptr);
90 vptr = fold_convert_loc (loc, uint64_type_node, vptr);
91 if (ckind == UBSAN_DOWNCAST_POINTER)
92 vptr = fold_build3 (COND_EXPR, uint64_type_node,
93 fold_build2 (NE_EXPR, boolean_type_node, op,
94 build_zero_cst (TREE_TYPE (op))),
95 vptr, build_int_cst (uint64_type_node, 0));
96 tree ti_decl = get_tinfo_decl (type);
97 mark_used (ti_decl);
98 tree ptype = build_pointer_type (type);
99 tree call
100 = build_call_expr_internal_loc (loc, IFN_UBSAN_VPTR,
101 void_type_node, 5, op, vptr, str_hash,
102 build_address (ti_decl),
103 build_int_cst (ptype, ckind));
104 TREE_SIDE_EFFECTS (call) = 1;
105 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (op), call, op);
108 /* Helper function for
109 cp_ubsan_maybe_instrument_{member_{call,access},downcast}.
110 Instrument vptr access if it should be instrumented, otherwise return
111 NULL_TREE. */
113 static tree
114 cp_ubsan_maybe_instrument_vptr (location_t loc, tree op, tree type,
115 bool is_addr, enum ubsan_null_ckind ckind)
117 if (!cp_ubsan_instrument_vptr_p (type))
118 return NULL_TREE;
119 return cp_ubsan_instrument_vptr (loc, op, type, is_addr, ckind);
122 /* Instrument a member call (but not constructor call) if needed. */
124 void
125 cp_ubsan_maybe_instrument_member_call (tree stmt)
127 if (call_expr_nargs (stmt) == 0)
128 return;
129 tree *opp = &CALL_EXPR_ARG (stmt, 0);
130 tree op = *opp;
131 if (op == error_mark_node
132 || !POINTER_TYPE_P (TREE_TYPE (op)))
133 return;
134 while (TREE_CODE (op) == COMPOUND_EXPR)
136 opp = &TREE_OPERAND (op, 1);
137 op = *opp;
139 op = cp_ubsan_maybe_instrument_vptr (EXPR_LOCATION (stmt), op,
140 TREE_TYPE (TREE_TYPE (op)),
141 true, UBSAN_MEMBER_CALL);
142 if (op)
143 *opp = op;
146 /* Data passed to cp_ubsan_check_member_access_r. */
148 struct cp_ubsan_check_member_access_data
150 hash_set<tree> *pset;
151 bool is_addr;
154 static tree cp_ubsan_check_member_access_r (tree *, int *, void *);
156 /* Instrument a member access. */
158 static bool
159 cp_ubsan_maybe_instrument_member_access
160 (tree stmt, cp_ubsan_check_member_access_data *ucmd)
162 if (DECL_ARTIFICIAL (TREE_OPERAND (stmt, 1)))
163 return false;
165 tree base = TREE_OPERAND (stmt, 0);
166 if (!cp_ubsan_instrument_vptr_p (TREE_TYPE (base)))
167 return false;
169 cp_walk_tree (&base, cp_ubsan_check_member_access_r, ucmd, ucmd->pset);
171 base = cp_ubsan_instrument_vptr (EXPR_LOCATION (stmt), base,
172 TREE_TYPE (base), false,
173 UBSAN_MEMBER_ACCESS);
174 TREE_OPERAND (stmt, 0)
175 = build_fold_indirect_ref_loc (EXPR_LOCATION (stmt), base);
176 return true;
179 /* Attempt to instrument member accesses inside of the function.
180 cp_ubsan_maybe_instrument_member_access should be called on COMPONENT_REFs
181 in the GENERIC IL, but only when the field is actually accessed, not
182 merely when it's address is taken. Therefore we track in is_addr field
183 whether in the current context we are processing address taken
184 handled components or not. E.g. for &x->y[w->z] we want to call
185 cp_ubsan_maybe_instrument_member_access on *w.z COMPONENT_REF, but
186 not on *x.y. */
188 static tree
189 cp_ubsan_check_member_access_r (tree *stmt_p, int *walk_subtrees, void *data)
191 tree stmt = *stmt_p, t;
192 cp_ubsan_check_member_access_data *ucmd
193 = (cp_ubsan_check_member_access_data *) data;
194 switch (TREE_CODE (stmt))
196 case ADDR_EXPR:
197 t = TREE_OPERAND (stmt, 0);
198 while ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
199 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
200 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
201 if (handled_component_p (t))
203 *walk_subtrees = 0;
204 ucmd->is_addr = true;
205 cp_walk_tree (&t, cp_ubsan_check_member_access_r,
206 data, ucmd->pset);
207 ucmd->is_addr = false;
209 break;
210 case MEM_REF:
211 case INDIRECT_REF:
212 t = TREE_OPERAND (stmt, 0);
213 if (TREE_CODE (t) == ADDR_EXPR)
215 *walk_subtrees = 0;
216 t = TREE_OPERAND (stmt, 0);
217 cp_walk_tree (&t, cp_ubsan_check_member_access_r, data, ucmd->pset);
219 break;
220 case COMPONENT_REF:
221 if (!ucmd->is_addr && cp_ubsan_maybe_instrument_member_access (stmt, ucmd))
223 *walk_subtrees = 0;
224 break;
226 /* FALLTHRU */
227 default:
228 if (ucmd->is_addr && handled_component_p (stmt))
230 int i, len = TREE_OPERAND_LENGTH (stmt);
231 *walk_subtrees = 0;
232 if (!handled_component_p (TREE_OPERAND (stmt, 0)))
233 ucmd->is_addr = false;
234 for (i = 0; i < len; i++)
236 cp_walk_tree (&TREE_OPERAND (stmt, i),
237 cp_ubsan_check_member_access_r, data, ucmd->pset);
238 ucmd->is_addr = false;
240 ucmd->is_addr = true;
242 break;
244 return NULL_TREE;
247 /* Instrument all member accesses inside GENERIC *T_P. */
249 void
250 cp_ubsan_instrument_member_accesses (tree *t_p)
252 if (cp_ubsan_instrument_vptr_p (NULL_TREE))
254 hash_set<tree> pset;
255 cp_ubsan_check_member_access_data ucmd;
256 ucmd.pset = &pset;
257 ucmd.is_addr = false;
258 cp_walk_tree (t_p, cp_ubsan_check_member_access_r, &ucmd, &pset);
262 /* Instrument downcast. */
264 tree
265 cp_ubsan_maybe_instrument_downcast (location_t loc, tree type, tree op)
267 if (!POINTER_TYPE_P (type)
268 || !POINTER_TYPE_P (TREE_TYPE (op))
269 || !CLASS_TYPE_P (TREE_TYPE (type))
270 || !CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
271 || !DERIVED_FROM_P (TREE_TYPE (TREE_TYPE (op)), TREE_TYPE (type)))
272 return NULL_TREE;
274 return cp_ubsan_maybe_instrument_vptr (loc, op, TREE_TYPE (type), true,
275 TREE_CODE (type) == POINTER_TYPE
276 ? UBSAN_DOWNCAST_POINTER
277 : UBSAN_DOWNCAST_REFERENCE);
280 /* Instrument cast to virtual base. */
282 tree
283 cp_ubsan_maybe_instrument_cast_to_vbase (location_t loc, tree type, tree op)
285 return cp_ubsan_maybe_instrument_vptr (loc, op, type, true,
286 UBSAN_CAST_TO_VBASE);