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