Debian Codegraph @

gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_paths.cc

raw
1 /* { dg-options "-O" } */
2
3 /* This plugin exercises the path-printing code.
4
5 The goal is to unit-test the path-printing code without needing any
6 specific tests within the compiler's IR. We can't use any real
7 diagnostics for this, so we have to fake it, hence this plugin. */
8
9 #include "gcc-plugin.h"
10 #include "config.h"
11 #include "system.h"
12 #include "coretypes.h"
13 #include "tm.h"
14 #include "tree.h"
15 #include "stringpool.h"
16 #include "toplev.h"
17 #include "basic-block.h"
18 #include "hash-table.h"
19 #include "vec.h"
20 #include "ggc.h"
21 #include "basic-block.h"
22 #include "tree-ssa-alias.h"
23 #include "internal-fn.h"
24 #include "gimple.h"
25 #include "gimple-iterator.h"
26 #include "gimple-fold.h"
27 #include "tree-eh.h"
28 #include "gimple-expr.h"
29 #include "is-a.h"
30 #include "tree.h"
31 #include "tree-pass.h"
32 #include "intl.h"
33 #include "plugin-version.h"
34 #include "diagnostic.h"
35 #include "diagnostic-path.h"
36 #include "diagnostic-metadata.h"
37 #include "context.h"
38 #include "print-tree.h"
39 #include "gcc-rich-location.h"
40 #include "cgraph.h"
41 #include "simple-diagnostic-path.h"
42
43 int plugin_is_GPL_compatible;
44
45 const pass_data pass_data_test_show_path =
46 {
47 IPA_PASS, /* type */
48 "test_show_path", /* name */
49 OPTGROUP_NONE, /* optinfo_flags */
50 TV_NONE, /* tv_id */
51 PROP_ssa, /* properties_required */
52 0, /* properties_provided */
53 0, /* properties_destroyed */
54 0, /* todo_flags_start */
55 0, /* todo_flags_finish */
56 };
57
58 class pass_test_show_path : public ipa_opt_pass_d
59 {
60 public:
61 pass_test_show_path(gcc::context *ctxt)
62 : ipa_opt_pass_d (pass_data_test_show_path, ctxt,
63 NULL, /* generate_summary */
64 NULL, /* write_summary */
65 NULL, /* read_summary */
66 NULL, /* write_optimization_summary */
67 NULL, /* read_optimization_summary */
68 NULL, /* stmt_fixup */
69 0, /* function_transform_todo_flags_start */
70 NULL, /* function_transform */
71 NULL) /* variable_transform */
72 {}
73
74 /* opt_pass methods: */
75 bool gate (function *) { return true; }
76 virtual unsigned int execute (function *);
77
78 }; // class pass_test_show_path
79
80 /* Determine if STMT is a call with NUM_ARGS arguments to a function
81 named FUNCNAME.
82 If so, return STMT as a gcall *. Otherwise return NULL. */
83
84 static gcall *
85 check_for_named_call (gimple *stmt,
86 const char *funcname, unsigned int num_args)
87 {
88 gcc_assert (funcname);
89
90 gcall *call = dyn_cast <gcall *> (stmt);
91 if (!call)
92 return NULL;
93
94 tree fndecl = gimple_call_fndecl (call);
95 if (!fndecl)
96 return NULL;
97
98 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), funcname))
99 return NULL;
100
101 if (gimple_call_num_args (call) != num_args)
102 {
103 error_at (stmt->location, "expected number of args: %i (got %i)",
104 num_args, gimple_call_num_args (call));
105 return NULL;
106 }
107
108 return call;
109 }
110
111 /* Example 1: a purely intraprocedural path. */
112
113 static void
114 example_1 ()
115 {
116 gimple_stmt_iterator gsi;
117 basic_block bb;
118
119 gcall *call_to_PyList_Append = NULL;
120 gcall *call_to_PyList_New = NULL;
121 gcond *for_cond = NULL;
122 function *example_a_fun = NULL;
123
124 cgraph_node *node;
125 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
126 {
127 function *fun = node->get_fun ();
128 FOR_EACH_BB_FN (bb, fun)
129 {
130 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
131 {
132 gimple *stmt = gsi_stmt (gsi);
133 if (gcall *call = check_for_named_call (stmt, "PyList_New", 1))
134 {
135 call_to_PyList_New = call;
136 example_a_fun = fun;
137 }
138 if (gcall *call = check_for_named_call (stmt, "PyList_Append", 2))
139 call_to_PyList_Append = call;
140 if (gcond *cond = dyn_cast <gcond *> (stmt))
141 for_cond = cond;
142 }
143 }
144 }
145
146 if (call_to_PyList_New && for_cond && call_to_PyList_Append)
147 {
148 auto_diagnostic_group d;
149 gcc_rich_location richloc (gimple_location (call_to_PyList_Append));
150 simple_diagnostic_path path (global_dc->get_reference_printer ());
151 diagnostic_event_id_t alloc_event_id
152 = path.add_event (gimple_location (call_to_PyList_New),
153 example_a_fun->decl, 0,
154 "when %qs fails, returning NULL",
155 "PyList_New");
156 path.add_event (gimple_location (for_cond),
157 example_a_fun->decl, 0,
158 "when %qs", "i < count");
159 path.add_event (gimple_location (call_to_PyList_Append),
160 example_a_fun->decl, 0,
161 "when calling %qs, passing NULL from %@ as argument %i",
162 "PyList_Append", &alloc_event_id, 1);
163 richloc.set_path (&path);
164 error_at (&richloc,
165 "passing NULL as argument %i to %qs"
166 " which requires a non-NULL parameter",
167 1, "PyList_Append");
168 }
169 }
170
171 /* A (function, location_t) pair. */
172
173 struct event_location_t
174 {
175 event_location_t ()
176 : m_fun (NULL), m_loc (UNKNOWN_LOCATION)
177 {}
178
179 event_location_t (function *fun, location_t loc)
180 : m_fun (fun), m_loc (loc)
181 {}
182
183 void set (const gimple *stmt, function *fun)
184 {
185 m_fun = fun;
186 m_loc = gimple_location (stmt);
187 }
188
189 function *m_fun;
190 location_t m_loc;
191 };
192
193 /* If FUN's name matches FUNCNAME, write the function and its start location
194 into *OUT_ENTRY. */
195
196 static bool
197 check_for_named_function (function *fun, const char *funcname,
198 event_location_t *out_entry)
199 {
200 gcc_assert (fun);
201 gcc_assert (funcname);
202
203 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (fun->decl)), funcname))
204 return false;
205
206 *out_entry = event_location_t (fun, fun->function_start_locus);
207 return true;
208 }
209
210
211 /* Example 2: an interprocedural path. */
212
213 class test_diagnostic_path : public simple_diagnostic_path
214 {
215 public:
216 test_diagnostic_path (pretty_printer *event_pp)
217 : simple_diagnostic_path (event_pp)
218 {
219 }
220 diagnostic_event_id_t
221 add_event_2 (event_location_t evloc, int stack_depth,
222 const char *desc,
223 diagnostic_thread_id_t thread_id = 0)
224 {
225 gcc_assert (evloc.m_fun);
226 return add_thread_event (thread_id, evloc.m_loc, evloc.m_fun->decl,
227 stack_depth, desc);
228 }
229 diagnostic_event_id_t
230 add_event_2_with_event_id (event_location_t evloc, int stack_depth,
231 const char *fmt,
232 diagnostic_thread_id_t thread_id,
233 diagnostic_event_id_t event_id)
234 {
235 gcc_assert (evloc.m_fun);
236 // FMT is assumed to have a single %@ argument
237 return add_thread_event (thread_id, evloc.m_loc, evloc.m_fun->decl,
238 stack_depth, fmt, &event_id);
239 }
240 void add_entry (event_location_t evloc, int stack_depth,
241 const char *funcname,
242 diagnostic_thread_id_t thread_id = 0)
243 {
244 gcc_assert (evloc.m_fun);
245 add_thread_event (thread_id, evloc.m_loc, evloc.m_fun->decl, stack_depth,
246 "entering %qs", funcname);
247 }
248
249 void add_call (event_location_t call_evloc, int caller_stack_depth,
250 event_location_t callee_entry_evloc, const char *callee)
251 {
252 gcc_assert (call_evloc.m_fun);
253 add_event (call_evloc.m_loc, call_evloc.m_fun->decl, caller_stack_depth,
254 "calling %qs", callee);
255 add_entry (callee_entry_evloc, caller_stack_depth + 1, callee);
256 }
257
258 void add_leaf_call (event_location_t call_evloc, int caller_stack_depth,
259 const char *callee)
260 {
261 gcc_assert (call_evloc.m_fun);
262 add_event (call_evloc.m_loc, call_evloc.m_fun->decl, caller_stack_depth,
263 "calling %qs", callee);
264 }
265 };
266
267 static void
268 example_2 ()
269 {
270 gimple_stmt_iterator gsi;
271 basic_block bb;
272
273 event_location_t entry_to_wrapped_malloc;
274 event_location_t call_to_malloc;
275
276 event_location_t entry_to_wrapped_free;
277 event_location_t call_to_free;
278
279 event_location_t entry_to_make_boxed_int;
280 event_location_t call_to_wrapped_malloc;
281
282 event_location_t entry_to_free_boxed_int;
283 event_location_t call_to_wrapped_free;
284
285 event_location_t entry_to_test;
286 event_location_t call_to_make_boxed_int;
287 event_location_t call_to_free_boxed_int;
288
289 event_location_t call_to_missing_location;
290
291 cgraph_node *node;
292 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
293 {
294 function *fun = node->get_fun ();
295 FOR_EACH_BB_FN (bb, fun)
296 {
297 check_for_named_function (fun, "wrapped_malloc",
298 &entry_to_wrapped_malloc);
299 check_for_named_function (fun, "wrapped_free",
300 &entry_to_wrapped_free);
301 check_for_named_function (fun, "make_boxed_int",
302 &entry_to_make_boxed_int);
303 check_for_named_function (fun, "free_boxed_int",
304 &entry_to_free_boxed_int);
305 check_for_named_function (fun, "test",
306 &entry_to_test);
307
308 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
309 {
310 gimple *stmt = gsi_stmt (gsi);
311 if (gcall *call = check_for_named_call (stmt, "malloc", 1))
312 call_to_malloc.set (call, fun);
313 if (gcall *call = check_for_named_call (stmt, "free", 1))
314 call_to_free.set (call, fun);
315 if (gcall *call = check_for_named_call (stmt, "wrapped_malloc", 1))
316 call_to_wrapped_malloc.set (call, fun);
317 if (gcall *call = check_for_named_call (stmt, "wrapped_free", 1))
318 call_to_wrapped_free.set (call, fun);
319 if (gcall *call = check_for_named_call (stmt, "make_boxed_int", 1))
320 call_to_make_boxed_int.set (call, fun);
321 if (gcall *call = check_for_named_call (stmt, "free_boxed_int", 1))
322 call_to_free_boxed_int.set (call, fun);
323 if (gcall *call = check_for_named_call (stmt, "missing_location", 0))
324 {
325 call_to_missing_location.set (call, fun);
326 /* Simulate an event that's missing a useful location_t. */
327 call_to_missing_location.m_loc = UNKNOWN_LOCATION;
328 }
329 }
330 }
331 }
332
333 if (call_to_malloc.m_fun)
334 {
335 auto_diagnostic_group d;
336
337 gcc_rich_location richloc (call_to_free.m_loc);
338 test_diagnostic_path path (global_dc->get_reference_printer ());
339 path.add_entry (entry_to_test, 0, "test");
340 path.add_call (call_to_make_boxed_int, 0,
341 entry_to_make_boxed_int, "make_boxed_int");
342 path.add_call (call_to_wrapped_malloc, 1,
343 entry_to_wrapped_malloc, "wrapped_malloc");
344 path.add_leaf_call (call_to_malloc, 2, "malloc");
345
346 for (int i = 0; i < 2; i++)
347 {
348 path.add_call (call_to_free_boxed_int, 0,
349 entry_to_free_boxed_int, "free_boxed_int");
350 path.add_call (call_to_wrapped_free, 1,
351 entry_to_wrapped_free, "wrapped_free");
352 path.add_leaf_call (call_to_free, 2, "free");
353 if (i == 0 && call_to_missing_location.m_fun)
354 path.add_leaf_call (call_to_missing_location, 0,
355 "missing_location");
356 }
357
358 richloc.set_path (&path);
359
360 diagnostic_metadata m;
361 m.add_cwe (415); /* CWE-415: Double Free. */
362
363 warning_meta (&richloc, m, 0,
364 "double-free of %qs", "ptr");
365 }
366 }
367
368 /* Example 3: an interprocedural path with a callback. */
369
370 static void
371 example_3 ()
372 {
373 gimple_stmt_iterator gsi;
374 basic_block bb;
375
376 event_location_t entry_to_custom_logger;
377 event_location_t call_to_fprintf;
378
379 event_location_t entry_to_int_handler;
380 event_location_t call_to_custom_logger;
381
382 event_location_t entry_to_register_handler;
383 event_location_t call_to_signal;
384
385 event_location_t entry_to_test;
386 event_location_t call_to_register_handler;
387
388 cgraph_node *node;
389 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
390 {
391 function *fun = node->get_fun ();
392 FOR_EACH_BB_FN (bb, fun)
393 {
394 check_for_named_function (fun, "custom_logger",
395 &entry_to_custom_logger);
396 check_for_named_function (fun, "int_handler",
397 &entry_to_int_handler);
398 check_for_named_function (fun, "register_handler",
399 &entry_to_register_handler);
400 check_for_named_function (fun, "test",
401 &entry_to_test);
402 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
403 {
404 gimple *stmt = gsi_stmt (gsi);
405 if (gcall *call = check_for_named_call (stmt, "fprintf", 3))
406 call_to_fprintf.set (call, fun);
407 if (gcall *call = check_for_named_call (stmt, "custom_logger", 1))
408 call_to_custom_logger.set (call, fun);
409 if (gcall *call = check_for_named_call (stmt, "register_handler",
410 0))
411 call_to_register_handler.set (call, fun);
412 if (gcall *call = check_for_named_call (stmt, "signal", 2))
413 call_to_signal.set (call, fun);
414 }
415 }
416 }
417
418 if (call_to_fprintf.m_fun)
419 {
420 auto_diagnostic_group d;
421
422 gcc_rich_location richloc (call_to_fprintf.m_loc);
423 test_diagnostic_path path (global_dc->get_reference_printer ());
424 path.add_entry (entry_to_test, 1, "test");
425 path.add_call (call_to_register_handler, 1,
426 entry_to_register_handler, "register_handler");
427 path.add_event (call_to_signal.m_loc, call_to_signal.m_fun->decl,
428 2, "registering 'int_handler' as signal handler");
429 path.add_event (UNKNOWN_LOCATION, NULL_TREE, 0,
430 "later on, when the signal is delivered to the process");
431 path.add_entry (entry_to_int_handler, 1, "int_handler");
432 path.add_call (call_to_custom_logger, 1,
433 entry_to_custom_logger, "custom_logger");
434 path.add_leaf_call (call_to_fprintf, 2, "fprintf");
435
436 richloc.set_path (&path);
437
438 diagnostic_metadata m;
439 /* CWE-479: Signal Handler Use of a Non-reentrant Function. */
440 m.add_cwe (479);
441
442 warning_meta (&richloc, m, 0,
443 "call to %qs from within signal handler",
444 "fprintf");
445 }
446 }
447
448 /* Example 4: a multithreaded path. */
449
450 static void
451 example_4 ()
452 {
453 gimple_stmt_iterator gsi;
454 basic_block bb;
455
456 event_location_t entry_to_foo;
457 event_location_t entry_to_bar;
458 event_location_t call_to_acquire_lock_a_in_foo;
459 event_location_t call_to_acquire_lock_b_in_foo;
460 event_location_t call_to_acquire_lock_a_in_bar;
461 event_location_t call_to_acquire_lock_b_in_bar;
462
463 cgraph_node *node;
464 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
465 {
466 function *fun = node->get_fun ();
467 FOR_EACH_BB_FN (bb, fun)
468 {
469 bool in_foo = check_for_named_function (fun, "foo",
470 &entry_to_foo);
471 bool in_bar = check_for_named_function (fun, "bar",
472 &entry_to_bar);
473 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
474 {
475 gimple *stmt = gsi_stmt (gsi);
476 event_location_t *evloc = NULL;
477 gcall *call = NULL;
478 if (call = check_for_named_call (stmt, "acquire_lock_a", 0))
479 evloc = (in_foo
480 ? &call_to_acquire_lock_a_in_foo
481 : &call_to_acquire_lock_a_in_bar);
482 else if (call
483 = check_for_named_call (stmt, "acquire_lock_b", 0))
484 evloc = (in_foo
485 ? &call_to_acquire_lock_b_in_foo
486 : &call_to_acquire_lock_b_in_bar);
487 if (evloc)
488 evloc->set (call, fun);
489 }
490 }
491 }
492
493 if (call_to_acquire_lock_a_in_foo.m_fun)
494 {
495 auto_diagnostic_group d;
496
497 gcc_rich_location richloc (call_to_acquire_lock_a_in_bar.m_loc);
498 test_diagnostic_path path (global_dc->get_reference_printer ());
499 diagnostic_thread_id_t thread_1 = path.add_thread ("Thread 1");
500 diagnostic_thread_id_t thread_2 = path.add_thread ("Thread 2");
501 path.add_entry (entry_to_foo, 0, "foo", thread_1);
502 diagnostic_event_id_t event_a_acquired
503 = path.add_event_2 (call_to_acquire_lock_a_in_foo, 1,
504 "lock a is now held by thread 1", thread_1);
505 path.add_entry (entry_to_bar, 0, "bar", thread_2);
506 diagnostic_event_id_t event_b_acquired
507 = path.add_event_2 (call_to_acquire_lock_b_in_bar, 1,
508 "lock b is now held by thread 2", thread_2);
509 path.add_event_2_with_event_id
510 (call_to_acquire_lock_b_in_foo, 1,
511 "deadlocked due to waiting for lock b in thread 1"
512 " (acquired by thread 2 at %@)...",
513 thread_1, event_b_acquired);
514 path.add_event_2_with_event_id
515 (call_to_acquire_lock_a_in_bar, 1,
516 "...whilst waiting for lock a in thread 2"
517 " (acquired by thread 1 at %@)",
518 thread_2, event_a_acquired);
519 richloc.set_path (&path);
520
521 diagnostic_metadata m;
522 warning_meta (&richloc, m, 0,
523 "deadlock due to inconsistent lock acquisition order");
524 }
525 }
526
527 unsigned int
528 pass_test_show_path::execute (function *)
529 {
530 example_1 ();
531 example_2 ();
532 example_3 ();
533 example_4 ();
534
535 return 0;
536 }
537
538 static opt_pass *
539 make_pass_test_show_path (gcc::context *ctxt)
540 {
541 return new pass_test_show_path (ctxt);
542 }
543
544 int
545 plugin_init (struct plugin_name_args *plugin_info,
546 struct plugin_gcc_version *version)
547 {
548 struct register_pass_info pass_info;
549 const char *plugin_name = plugin_info->base_name;
550 int argc = plugin_info->argc;
551 struct plugin_argument *argv = plugin_info->argv;
552
553 if (!plugin_default_version_check (version, &gcc_version))
554 return 1;
555
556 global_dc->m_source_printing.max_width = 80;
557
558 pass_info.pass = make_pass_test_show_path (g);
559 pass_info.reference_pass_name = "whole-program";
560 pass_info.ref_pass_instance_number = 1;
561 pass_info.pos_op = PASS_POS_INSERT_BEFORE;
562 register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
563 &pass_info);
564
565 return 0;
566 }