annotate gcc/sancov.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Code coverage instrumentation for fuzzing.
kono
parents:
diff changeset
2 Copyright (C) 2015-2017 Free Software Foundation, Inc.
kono
parents:
diff changeset
3 Contributed by Dmitry Vyukov <dvyukov@google.com> and
kono
parents:
diff changeset
4 Wish Wu <wishwu007@gmail.com>
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 This file is part of GCC.
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
9 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
10 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
11 version.
kono
parents:
diff changeset
12
kono
parents:
diff changeset
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
16 for more details.
kono
parents:
diff changeset
17
kono
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
19 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
20 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 #include "config.h"
kono
parents:
diff changeset
23 #include "system.h"
kono
parents:
diff changeset
24 #include "coretypes.h"
kono
parents:
diff changeset
25 #include "backend.h"
kono
parents:
diff changeset
26 #include "tree.h"
kono
parents:
diff changeset
27 #include "gimple.h"
kono
parents:
diff changeset
28 #include "basic-block.h"
kono
parents:
diff changeset
29 #include "options.h"
kono
parents:
diff changeset
30 #include "flags.h"
kono
parents:
diff changeset
31 #include "memmodel.h"
kono
parents:
diff changeset
32 #include "tm_p.h"
kono
parents:
diff changeset
33 #include "stmt.h"
kono
parents:
diff changeset
34 #include "gimple-iterator.h"
kono
parents:
diff changeset
35 #include "gimple-builder.h"
kono
parents:
diff changeset
36 #include "tree-cfg.h"
kono
parents:
diff changeset
37 #include "tree-pass.h"
kono
parents:
diff changeset
38 #include "tree-iterator.h"
kono
parents:
diff changeset
39 #include "fold-const.h"
kono
parents:
diff changeset
40 #include "stringpool.h"
kono
parents:
diff changeset
41 #include "attribs.h"
kono
parents:
diff changeset
42 #include "output.h"
kono
parents:
diff changeset
43 #include "cgraph.h"
kono
parents:
diff changeset
44 #include "asan.h"
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 namespace {
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 /* Instrument one comparison operation, which compares lhs and rhs.
kono
parents:
diff changeset
49 Call the instrumentation function with the comparison operand.
kono
parents:
diff changeset
50 For integral comparisons if exactly one of the comparison operands is
kono
parents:
diff changeset
51 constant, call __sanitizer_cov_trace_const_cmp* instead of
kono
parents:
diff changeset
52 __sanitizer_cov_trace_cmp*. */
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 static void
kono
parents:
diff changeset
55 instrument_comparison (gimple_stmt_iterator *gsi, tree lhs, tree rhs)
kono
parents:
diff changeset
56 {
kono
parents:
diff changeset
57 tree type = TREE_TYPE (lhs);
kono
parents:
diff changeset
58 enum built_in_function fncode = END_BUILTINS;
kono
parents:
diff changeset
59 tree to_type = NULL_TREE;
kono
parents:
diff changeset
60 bool c = false;
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 if (INTEGRAL_TYPE_P (type))
kono
parents:
diff changeset
63 {
kono
parents:
diff changeset
64 c = (is_gimple_min_invariant (lhs)
kono
parents:
diff changeset
65 ^ is_gimple_min_invariant (rhs));
kono
parents:
diff changeset
66 switch (int_size_in_bytes (type))
kono
parents:
diff changeset
67 {
kono
parents:
diff changeset
68 case 1:
kono
parents:
diff changeset
69 fncode = c ? BUILT_IN_SANITIZER_COV_TRACE_CONST_CMP1
kono
parents:
diff changeset
70 : BUILT_IN_SANITIZER_COV_TRACE_CMP1;
kono
parents:
diff changeset
71 to_type = unsigned_char_type_node;
kono
parents:
diff changeset
72 break;
kono
parents:
diff changeset
73 case 2:
kono
parents:
diff changeset
74 fncode = c ? BUILT_IN_SANITIZER_COV_TRACE_CONST_CMP2
kono
parents:
diff changeset
75 : BUILT_IN_SANITIZER_COV_TRACE_CMP2;
kono
parents:
diff changeset
76 to_type = uint16_type_node;
kono
parents:
diff changeset
77 break;
kono
parents:
diff changeset
78 case 4:
kono
parents:
diff changeset
79 fncode = c ? BUILT_IN_SANITIZER_COV_TRACE_CONST_CMP4
kono
parents:
diff changeset
80 : BUILT_IN_SANITIZER_COV_TRACE_CMP4;
kono
parents:
diff changeset
81 to_type = uint32_type_node;
kono
parents:
diff changeset
82 break;
kono
parents:
diff changeset
83 default:
kono
parents:
diff changeset
84 fncode = c ? BUILT_IN_SANITIZER_COV_TRACE_CONST_CMP8
kono
parents:
diff changeset
85 : BUILT_IN_SANITIZER_COV_TRACE_CMP8;
kono
parents:
diff changeset
86 to_type = uint64_type_node;
kono
parents:
diff changeset
87 break;
kono
parents:
diff changeset
88 }
kono
parents:
diff changeset
89 }
kono
parents:
diff changeset
90 else if (SCALAR_FLOAT_TYPE_P (type))
kono
parents:
diff changeset
91 {
kono
parents:
diff changeset
92 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
kono
parents:
diff changeset
93 {
kono
parents:
diff changeset
94 fncode = BUILT_IN_SANITIZER_COV_TRACE_CMPF;
kono
parents:
diff changeset
95 to_type = float_type_node;
kono
parents:
diff changeset
96 }
kono
parents:
diff changeset
97 else if (TYPE_MODE (type) == TYPE_MODE (double_type_node))
kono
parents:
diff changeset
98 {
kono
parents:
diff changeset
99 fncode = BUILT_IN_SANITIZER_COV_TRACE_CMPD;
kono
parents:
diff changeset
100 to_type = double_type_node;
kono
parents:
diff changeset
101 }
kono
parents:
diff changeset
102 }
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 if (to_type != NULL_TREE)
kono
parents:
diff changeset
105 {
kono
parents:
diff changeset
106 gimple_seq seq = NULL;
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 if (!useless_type_conversion_p (to_type, type))
kono
parents:
diff changeset
109 {
kono
parents:
diff changeset
110 if (TREE_CODE (lhs) == INTEGER_CST)
kono
parents:
diff changeset
111 lhs = fold_convert (to_type, lhs);
kono
parents:
diff changeset
112 else
kono
parents:
diff changeset
113 {
kono
parents:
diff changeset
114 gimple_seq_add_stmt (&seq, build_type_cast (to_type, lhs));
kono
parents:
diff changeset
115 lhs = gimple_assign_lhs (gimple_seq_last_stmt (seq));
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 if (TREE_CODE (rhs) == INTEGER_CST)
kono
parents:
diff changeset
119 rhs = fold_convert (to_type, rhs);
kono
parents:
diff changeset
120 else
kono
parents:
diff changeset
121 {
kono
parents:
diff changeset
122 gimple_seq_add_stmt (&seq, build_type_cast (to_type, rhs));
kono
parents:
diff changeset
123 rhs = gimple_assign_lhs (gimple_seq_last_stmt (seq));
kono
parents:
diff changeset
124 }
kono
parents:
diff changeset
125 }
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 if (c && !is_gimple_min_invariant (lhs))
kono
parents:
diff changeset
128 std::swap (lhs, rhs);
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 tree fndecl = builtin_decl_implicit (fncode);
kono
parents:
diff changeset
131 gimple *gcall = gimple_build_call (fndecl, 2, lhs, rhs);
kono
parents:
diff changeset
132 gimple_seq_add_stmt (&seq, gcall);
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 gimple_seq_set_location (seq, gimple_location (gsi_stmt (*gsi)));
kono
parents:
diff changeset
135 gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
kono
parents:
diff changeset
136 }
kono
parents:
diff changeset
137 }
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 /* Instrument switch statement. Call __sanitizer_cov_trace_switch with
kono
parents:
diff changeset
140 the value of the index and array that contains number of case values,
kono
parents:
diff changeset
141 the bitsize of the index and the case values converted to uint64_t. */
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 static void
kono
parents:
diff changeset
144 instrument_switch (gimple_stmt_iterator *gsi, gimple *stmt, function *fun)
kono
parents:
diff changeset
145 {
kono
parents:
diff changeset
146 gswitch *switch_stmt = as_a<gswitch *> (stmt);
kono
parents:
diff changeset
147 tree index = gimple_switch_index (switch_stmt);
kono
parents:
diff changeset
148 HOST_WIDE_INT size_in_bytes = int_size_in_bytes (TREE_TYPE (index));
kono
parents:
diff changeset
149 if (size_in_bytes == -1 || size_in_bytes > 8)
kono
parents:
diff changeset
150 return;
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 location_t loc = gimple_location (stmt);
kono
parents:
diff changeset
153 unsigned i, n = gimple_switch_num_labels (switch_stmt), num = 0;
kono
parents:
diff changeset
154 for (i = 1; i < n; ++i)
kono
parents:
diff changeset
155 {
kono
parents:
diff changeset
156 tree label = gimple_switch_label (switch_stmt, i);
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 tree low_case = CASE_LOW (label);
kono
parents:
diff changeset
159 if (low_case != NULL_TREE)
kono
parents:
diff changeset
160 num++;
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 tree high_case = CASE_HIGH (label);
kono
parents:
diff changeset
163 if (high_case != NULL_TREE)
kono
parents:
diff changeset
164 num++;
kono
parents:
diff changeset
165 }
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 tree case_array_type
kono
parents:
diff changeset
168 = build_array_type (build_type_variant (uint64_type_node, 1, 0),
kono
parents:
diff changeset
169 build_index_type (size_int (num + 2 - 1)));
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 char name[64];
kono
parents:
diff changeset
172 static size_t case_array_count = 0;
kono
parents:
diff changeset
173 ASM_GENERATE_INTERNAL_LABEL (name, "LCASEARRAY", case_array_count++);
kono
parents:
diff changeset
174 tree case_array_var = build_decl (loc, VAR_DECL, get_identifier (name),
kono
parents:
diff changeset
175 case_array_type);
kono
parents:
diff changeset
176 TREE_STATIC (case_array_var) = 1;
kono
parents:
diff changeset
177 TREE_PUBLIC (case_array_var) = 0;
kono
parents:
diff changeset
178 TREE_CONSTANT (case_array_var) = 1;
kono
parents:
diff changeset
179 TREE_READONLY (case_array_var) = 1;
kono
parents:
diff changeset
180 DECL_EXTERNAL (case_array_var) = 0;
kono
parents:
diff changeset
181 DECL_ARTIFICIAL (case_array_var) = 1;
kono
parents:
diff changeset
182 DECL_IGNORED_P (case_array_var) = 1;
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 vec <constructor_elt, va_gc> *v = NULL;
kono
parents:
diff changeset
185 vec_alloc (v, num + 2);
kono
parents:
diff changeset
186 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
187 build_int_cst (uint64_type_node, num));
kono
parents:
diff changeset
188 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
189 build_int_cst (uint64_type_node,
kono
parents:
diff changeset
190 size_in_bytes * BITS_PER_UNIT));
kono
parents:
diff changeset
191 for (i = 1; i < n; ++i)
kono
parents:
diff changeset
192 {
kono
parents:
diff changeset
193 tree label = gimple_switch_label (switch_stmt, i);
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 tree low_case = CASE_LOW (label);
kono
parents:
diff changeset
196 if (low_case != NULL_TREE)
kono
parents:
diff changeset
197 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
198 fold_convert (uint64_type_node, low_case));
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 tree high_case = CASE_HIGH (label);
kono
parents:
diff changeset
201 if (high_case != NULL_TREE)
kono
parents:
diff changeset
202 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
kono
parents:
diff changeset
203 fold_convert (uint64_type_node, high_case));
kono
parents:
diff changeset
204 }
kono
parents:
diff changeset
205 tree ctor = build_constructor (case_array_type, v);
kono
parents:
diff changeset
206 TREE_STATIC (ctor) = 1;
kono
parents:
diff changeset
207 TREE_PUBLIC (ctor) = 0;
kono
parents:
diff changeset
208 TREE_CONSTANT (ctor) = 1;
kono
parents:
diff changeset
209 TREE_READONLY (ctor) = 1;
kono
parents:
diff changeset
210 DECL_INITIAL (case_array_var) = ctor;
kono
parents:
diff changeset
211 varpool_node::finalize_decl (case_array_var);
kono
parents:
diff changeset
212 add_local_decl (fun, case_array_var);
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 gimple_seq seq = NULL;
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 if (!useless_type_conversion_p (uint64_type_node, TREE_TYPE (index)))
kono
parents:
diff changeset
217 {
kono
parents:
diff changeset
218 if (TREE_CODE (index) == INTEGER_CST)
kono
parents:
diff changeset
219 index = fold_convert (uint64_type_node, index);
kono
parents:
diff changeset
220 else
kono
parents:
diff changeset
221 {
kono
parents:
diff changeset
222 gimple_seq_add_stmt (&seq, build_type_cast (uint64_type_node, index));
kono
parents:
diff changeset
223 index = gimple_assign_lhs (gimple_seq_last_stmt (seq));
kono
parents:
diff changeset
224 }
kono
parents:
diff changeset
225 }
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 tree fndecl = builtin_decl_implicit (BUILT_IN_SANITIZER_COV_TRACE_SWITCH);
kono
parents:
diff changeset
228 gimple *gcall = gimple_build_call (fndecl, 2, index,
kono
parents:
diff changeset
229 build_fold_addr_expr (case_array_var));
kono
parents:
diff changeset
230 gimple_seq_add_stmt (&seq, gcall);
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 gimple_seq_set_location (seq, loc);
kono
parents:
diff changeset
233 gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
kono
parents:
diff changeset
234 }
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 unsigned
kono
parents:
diff changeset
237 sancov_pass (function *fun)
kono
parents:
diff changeset
238 {
kono
parents:
diff changeset
239 initialize_sanitizer_builtins ();
kono
parents:
diff changeset
240
kono
parents:
diff changeset
241 /* Insert callback into beginning of every BB. */
kono
parents:
diff changeset
242 if (flag_sanitize_coverage & SANITIZE_COV_TRACE_PC)
kono
parents:
diff changeset
243 {
kono
parents:
diff changeset
244 basic_block bb;
kono
parents:
diff changeset
245 tree fndecl = builtin_decl_implicit (BUILT_IN_SANITIZER_COV_TRACE_PC);
kono
parents:
diff changeset
246 FOR_EACH_BB_FN (bb, fun)
kono
parents:
diff changeset
247 {
kono
parents:
diff changeset
248 gimple_stmt_iterator gsi = gsi_start_nondebug_after_labels_bb (bb);
kono
parents:
diff changeset
249 if (gsi_end_p (gsi))
kono
parents:
diff changeset
250 continue;
kono
parents:
diff changeset
251 gimple *stmt = gsi_stmt (gsi);
kono
parents:
diff changeset
252 gimple *gcall = gimple_build_call (fndecl, 0);
kono
parents:
diff changeset
253 gimple_set_location (gcall, gimple_location (stmt));
kono
parents:
diff changeset
254 gsi_insert_before (&gsi, gcall, GSI_SAME_STMT);
kono
parents:
diff changeset
255 }
kono
parents:
diff changeset
256 }
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 /* Insert callback into every comparison related operation. */
kono
parents:
diff changeset
259 if (flag_sanitize_coverage & SANITIZE_COV_TRACE_CMP)
kono
parents:
diff changeset
260 {
kono
parents:
diff changeset
261 basic_block bb;
kono
parents:
diff changeset
262 FOR_EACH_BB_FN (bb, fun)
kono
parents:
diff changeset
263 {
kono
parents:
diff changeset
264 gimple_stmt_iterator gsi;
kono
parents:
diff changeset
265 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents:
diff changeset
266 {
kono
parents:
diff changeset
267 gimple *stmt = gsi_stmt (gsi);
kono
parents:
diff changeset
268 enum tree_code rhs_code;
kono
parents:
diff changeset
269 switch (gimple_code (stmt))
kono
parents:
diff changeset
270 {
kono
parents:
diff changeset
271 case GIMPLE_ASSIGN:
kono
parents:
diff changeset
272 rhs_code = gimple_assign_rhs_code (stmt);
kono
parents:
diff changeset
273 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
kono
parents:
diff changeset
274 instrument_comparison (&gsi,
kono
parents:
diff changeset
275 gimple_assign_rhs1 (stmt),
kono
parents:
diff changeset
276 gimple_assign_rhs2 (stmt));
kono
parents:
diff changeset
277 else if (rhs_code == COND_EXPR
kono
parents:
diff changeset
278 && COMPARISON_CLASS_P (gimple_assign_rhs1 (stmt)))
kono
parents:
diff changeset
279 {
kono
parents:
diff changeset
280 tree cond = gimple_assign_rhs1 (stmt);
kono
parents:
diff changeset
281 instrument_comparison (&gsi, TREE_OPERAND (cond, 0),
kono
parents:
diff changeset
282 TREE_OPERAND (cond, 1));
kono
parents:
diff changeset
283 }
kono
parents:
diff changeset
284 break;
kono
parents:
diff changeset
285 case GIMPLE_COND:
kono
parents:
diff changeset
286 instrument_comparison (&gsi,
kono
parents:
diff changeset
287 gimple_cond_lhs (stmt),
kono
parents:
diff changeset
288 gimple_cond_rhs (stmt));
kono
parents:
diff changeset
289 break;
kono
parents:
diff changeset
290
kono
parents:
diff changeset
291 case GIMPLE_SWITCH:
kono
parents:
diff changeset
292 instrument_switch (&gsi, stmt, fun);
kono
parents:
diff changeset
293 break;
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 default:
kono
parents:
diff changeset
296 break;
kono
parents:
diff changeset
297 }
kono
parents:
diff changeset
298 }
kono
parents:
diff changeset
299 }
kono
parents:
diff changeset
300 }
kono
parents:
diff changeset
301 return 0;
kono
parents:
diff changeset
302 }
kono
parents:
diff changeset
303
kono
parents:
diff changeset
304 template <bool O0> class pass_sancov : public gimple_opt_pass
kono
parents:
diff changeset
305 {
kono
parents:
diff changeset
306 public:
kono
parents:
diff changeset
307 pass_sancov (gcc::context *ctxt) : gimple_opt_pass (data, ctxt) {}
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 static const pass_data data;
kono
parents:
diff changeset
310 opt_pass *
kono
parents:
diff changeset
311 clone ()
kono
parents:
diff changeset
312 {
kono
parents:
diff changeset
313 return new pass_sancov<O0> (m_ctxt);
kono
parents:
diff changeset
314 }
kono
parents:
diff changeset
315 virtual bool
kono
parents:
diff changeset
316 gate (function *)
kono
parents:
diff changeset
317 {
kono
parents:
diff changeset
318 return flag_sanitize_coverage && (!O0 || !optimize);
kono
parents:
diff changeset
319 }
kono
parents:
diff changeset
320 virtual unsigned int
kono
parents:
diff changeset
321 execute (function *fun)
kono
parents:
diff changeset
322 {
kono
parents:
diff changeset
323 return sancov_pass (fun);
kono
parents:
diff changeset
324 }
kono
parents:
diff changeset
325 }; // class pass_sancov
kono
parents:
diff changeset
326
kono
parents:
diff changeset
327 template <bool O0>
kono
parents:
diff changeset
328 const pass_data pass_sancov<O0>::data = {
kono
parents:
diff changeset
329 GIMPLE_PASS, /* type */
kono
parents:
diff changeset
330 O0 ? "sancov_O0" : "sancov", /* name */
kono
parents:
diff changeset
331 OPTGROUP_NONE, /* optinfo_flags */
kono
parents:
diff changeset
332 TV_NONE, /* tv_id */
kono
parents:
diff changeset
333 (PROP_cfg), /* properties_required */
kono
parents:
diff changeset
334 0, /* properties_provided */
kono
parents:
diff changeset
335 0, /* properties_destroyed */
kono
parents:
diff changeset
336 0, /* todo_flags_start */
kono
parents:
diff changeset
337 TODO_update_ssa, /* todo_flags_finish */
kono
parents:
diff changeset
338 };
kono
parents:
diff changeset
339
kono
parents:
diff changeset
340 } // anon namespace
kono
parents:
diff changeset
341
kono
parents:
diff changeset
342 gimple_opt_pass *
kono
parents:
diff changeset
343 make_pass_sancov (gcc::context *ctxt)
kono
parents:
diff changeset
344 {
kono
parents:
diff changeset
345 return new pass_sancov<false> (ctxt);
kono
parents:
diff changeset
346 }
kono
parents:
diff changeset
347
kono
parents:
diff changeset
348 gimple_opt_pass *
kono
parents:
diff changeset
349 make_pass_sancov_O0 (gcc::context *ctxt)
kono
parents:
diff changeset
350 {
kono
parents:
diff changeset
351 return new pass_sancov<true> (ctxt);
kono
parents:
diff changeset
352 }