1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
|
/* Detection of infinite loops.
Copyright (C) 2022-2024 Free Software Foundation, Inc.
Contributed by David Malcolm <dmalcolm@redhat.com>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#define INCLUDE_MEMORY
#define INCLUDE_VECTOR
#include "system.h"
#include "coretypes.h"
#include "tree.h"
#include "fold-const.h"
#include "gcc-rich-location.h"
#include "alloc-pool.h"
#include "fibonacci_heap.h"
#include "shortest-paths.h"
#include "diagnostic-core.h"
#include "diagnostic-event-id.h"
#include "diagnostic-path.h"
#include "function.h"
#include "pretty-print.h"
#include "sbitmap.h"
#include "bitmap.h"
#include "tristate.h"
#include "ordered-hash-map.h"
#include "selftest.h"
#include "json.h"
#include "analyzer/analyzer.h"
#include "analyzer/analyzer-logging.h"
#include "analyzer/call-string.h"
#include "analyzer/program-point.h"
#include "analyzer/store.h"
#include "analyzer/region-model.h"
#include "analyzer/constraint-manager.h"
#include "analyzer/sm.h"
#include "analyzer/pending-diagnostic.h"
#include "analyzer/diagnostic-manager.h"
#include "cfg.h"
#include "basic-block.h"
#include "gimple.h"
#include "gimple-iterator.h"
#include "gimple-pretty-print.h"
#include "cgraph.h"
#include "digraph.h"
#include "analyzer/supergraph.h"
#include "analyzer/program-state.h"
#include "analyzer/exploded-graph.h"
#include "analyzer/checker-path.h"
#include "analyzer/feasible-graph.h"
#include "make-unique.h"
#include "diagnostic-format-sarif.h"
/* A bundle of data characterizing a particular infinite loop
identified within the exploded graph. */
struct infinite_loop
{
infinite_loop (const exploded_node &enode,
location_t loc,
std::vector<const exploded_edge *> &&eedges,
logger *logger)
: m_enode (enode),
m_loc (loc),
m_eedge_vec (eedges)
{
LOG_SCOPE (logger);
if (logger)
{
logger->start_log_line ();
logger->log_partial ("infinite loop: EN: %i", m_enode.m_index);
for (auto eedge : m_eedge_vec)
{
logger->log_partial (" ->");
if (const superedge *sedge = eedge->m_sedge)
{
sedge->dump_label_to_pp (logger->get_printer (), false);
}
logger->log_partial (" EN: %i", eedge->m_dest->m_index);
}
logger->end_log_line ();
}
}
bool
operator== (const infinite_loop &other) const
{
/* Compare underlying supernode, rather than enodes, so that
we don't get duplicates in functions that are called from
elsewhere. */
return (m_enode.get_supernode () == other.m_enode.get_supernode ()
&& m_loc == other.m_loc);
}
json::object *
to_json () const
{
json::object *loop_obj = new json::object ();
loop_obj->set_integer ("enode", m_enode.m_index);
json::array *edge_arr = new json::array ();
for (auto eedge : m_eedge_vec)
edge_arr->append (eedge->to_json ());
loop_obj->set ("eedges", edge_arr);
return loop_obj;
}
const exploded_node &m_enode;
location_t m_loc;
std::vector<const exploded_edge *> m_eedge_vec;
};
/* A custom subclass of start_cfg_edge_event that rewords the
message to indicate that the CFG edge is *always* taken on
subsequent iterations, assuming it's been taken once. */
class perpetual_start_cfg_edge_event : public start_cfg_edge_event
{
public:
perpetual_start_cfg_edge_event (const exploded_edge &eedge,
const event_loc_info &loc_info)
: start_cfg_edge_event (eedge, loc_info)
{
}
label_text get_desc (bool can_colorize) const final override
{
bool user_facing = !flag_analyzer_verbose_edges;
label_text edge_desc (m_sedge->get_description (user_facing));
if (user_facing)
{
if (edge_desc.get () && strlen (edge_desc.get ()) > 0)
{
label_text cond_desc = maybe_describe_condition (can_colorize);
label_text result;
if (cond_desc.get ())
return make_label_text
(can_colorize,
"%s: always following %qs branch...",
cond_desc.get (), edge_desc.get ());
else
return make_label_text
(can_colorize,
"if it ever follows %qs branch, it will always do so...",
edge_desc.get ());
}
}
return start_cfg_edge_event::get_desc (can_colorize);
}
};
class looping_back_event : public start_cfg_edge_event
{
public:
looping_back_event (const exploded_edge &eedge,
const event_loc_info &loc_info)
: start_cfg_edge_event (eedge, loc_info)
{
}
label_text get_desc (bool can_colorize) const final override
{
return label_text::borrow ("looping back...");
}
};
/* A subclass of pending_diagnostic for complaining about suspected
infinite loops. */
class infinite_loop_diagnostic
: public pending_diagnostic_subclass<infinite_loop_diagnostic>
{
public:
infinite_loop_diagnostic (std::unique_ptr<infinite_loop> inf_loop)
: m_inf_loop (std::move (inf_loop))
{
gcc_assert (m_inf_loop != nullptr);
}
const char *get_kind () const final override
{
return "infinite_loop_diagnostic";
}
bool operator== (const infinite_loop_diagnostic &other) const
{
return *m_inf_loop == *other.m_inf_loop;
}
int get_controlling_option () const final override
{
return OPT_Wanalyzer_infinite_loop;
}
bool emit (diagnostic_emission_context &ctxt) final override
{
/* "CWE-835: Loop with Unreachable Exit Condition ('Infinite Loop')". */
ctxt.add_cwe (835);
return ctxt.warn ("infinite loop");
}
bool maybe_add_custom_events_for_superedge (const exploded_edge &,
checker_path *)
final override
{
/* Don't add any regular events; instead we add them after pruning as
part of the "final" warning. */
return true;
}
label_text describe_final_event (const evdesc::final_event &ev) final override
{
return ev.formatted_print ("infinite loop here");
}
/* Customize the location where the warning_event appears. */
void add_final_event (const state_machine *,
const exploded_node *enode,
const gimple *,
tree,
state_machine::state_t,
checker_path *emission_path) final override
{
emission_path->add_event
(make_unique<warning_event>
(event_loc_info (m_inf_loop->m_loc,
enode->get_function ()->decl,
enode->get_stack_depth ()),
enode,
NULL, NULL, NULL));
logger *logger = emission_path->get_logger ();
/* EMISSION_PATH has the path to the entry of the infinite loop.
Add extra edges showing the loop itself. */
for (auto eedge : m_inf_loop->m_eedge_vec)
{
if (logger)
logger->log ("EN: %i -> EN: %i",
eedge->m_src->m_index,
eedge->m_dest->m_index);
if (!eedge->m_sedge)
continue;
const cfg_superedge *cfg_sedge
= eedge->m_sedge->dyn_cast_cfg_superedge ();
if (!cfg_sedge)
continue;
const exploded_node *src_node = eedge->m_src;
const program_point &src_point = src_node->get_point ();
const exploded_node *dst_node = eedge->m_dest;
const program_point &dst_point = dst_node->get_point ();
const int src_stack_depth = src_point.get_stack_depth ();
const int dst_stack_depth = dst_point.get_stack_depth ();
const gimple *last_stmt = src_point.get_supernode ()->get_last_stmt ();
event_loc_info loc_info_from
(last_stmt ? last_stmt->location : cfg_sedge->get_goto_locus (),
src_point.get_fndecl (),
src_stack_depth);
event_loc_info loc_info_to
(dst_point.get_supernode ()->get_start_location (),
dst_point.get_fndecl (),
dst_stack_depth);
if (const switch_cfg_superedge *switch_cfg_sedge
= cfg_sedge->dyn_cast_switch_cfg_superedge ())
{
if (switch_cfg_sedge->implicitly_created_default_p ())
{
emission_path->add_event
(make_unique<perpetual_start_cfg_edge_event> (*eedge,
loc_info_from));
emission_path->add_event
(make_unique<end_cfg_edge_event>
(*eedge,
loc_info_to));
}
}
if (cfg_sedge->true_value_p ())
{
emission_path->add_event
(make_unique<perpetual_start_cfg_edge_event> (*eedge,
loc_info_from));
emission_path->add_event
(make_unique<end_cfg_edge_event>
(*eedge,
loc_info_to));
}
else if (cfg_sedge->false_value_p ())
{
emission_path->add_event
(make_unique<perpetual_start_cfg_edge_event> (*eedge,
loc_info_from));
emission_path->add_event
(make_unique<end_cfg_edge_event>
(*eedge,
loc_info_to));
}
else if (cfg_sedge->back_edge_p ())
{
emission_path->add_event
(make_unique<looping_back_event> (*eedge, loc_info_from));
emission_path->add_event
(make_unique<end_cfg_edge_event>
(*eedge,
loc_info_to));
}
}
}
void maybe_add_sarif_properties (sarif_object &result_obj)
const final override
{
sarif_property_bag &props = result_obj.get_or_create_properties ();
#define PROPERTY_PREFIX "gcc/analyzer/infinite_loop_diagnostic/"
props.set (PROPERTY_PREFIX "inf_loop", m_inf_loop->to_json ());
#undef PROPERTY_PREFIX
}
private:
std::unique_ptr<infinite_loop> m_inf_loop;
};
/* If ENODE has an in-edge corresponding to a CFG backedge, return that
exploded in-edge.
Otherwise, return nullptr. */
static const exploded_edge *
get_in_edge_back_edge (const exploded_node &enode)
{
for (auto in_edge : enode.m_preds)
{
const superedge *sedge = in_edge->m_sedge;
if (!sedge)
continue;
const cfg_superedge *cfg_sedge = sedge->dyn_cast_cfg_superedge ();
if (!cfg_sedge)
continue;
if (cfg_sedge->back_edge_p ())
return in_edge;
}
return nullptr;
}
/* Subclass of region_model_context that rejects conditional branches that
aren't known for definite. */
class infinite_loop_checking_context : public noop_region_model_context
{
public:
infinite_loop_checking_context () : m_unusable (false) {}
bool checking_for_infinite_loop_p () const override { return true; }
void on_unusable_in_infinite_loop () override { m_unusable = true; }
bool unusable_p () const { return m_unusable; }
private:
bool m_unusable;
};
/* Determine if an infinite loop starts at ENODE.
Return the loop if it is found, nullptr otherwise.
Look for cycles in the exploded graph in which:
- no externally visible work occurs
- no escape from the cycle
- the program state is "sufficiently concrete" at each step:
- no unknown activity could be occurring
- the worklist was fully drained for each enode in the cycle
i.e. every enode in the cycle is processed. */
static std::unique_ptr<infinite_loop>
starts_infinite_loop_p (const exploded_node &enode,
const exploded_graph &eg,
logger *logger)
{
LOG_FUNC_1 (logger, "considering EN: %i", enode.m_index);
/* Only consider enodes that have a CFG back edge as an in-edge. */
if (const exploded_edge *back_edge = get_in_edge_back_edge (enode))
{
if (logger)
logger->log ("got backedge from EN: %i",
back_edge->m_src->m_index);
}
else
{
if (logger)
logger->log ("rejecting: no backedge in in-edges");
return nullptr;
}
/* Support for dumping an .infinite-loop.dot file visualizing the
traversal for this enode. */
std::unique_ptr<feasible_graph> fg;
feasible_node *curr_fnode = nullptr;
if (flag_dump_analyzer_infinite_loop)
fg = ::make_unique<feasible_graph> ();
location_t first_loc = UNKNOWN_LOCATION;
const exploded_node *iter = &enode;
feasibility_state state (*enode.get_state ().m_region_model,
eg.get_supergraph ());
if (fg)
curr_fnode = fg->add_node (&enode, state, 0);
hash_set<const exploded_node *> visited;
std::vector<const exploded_edge *> eedges;
while (1)
{
if (logger)
logger->log ("iter: EN: %i", iter->m_index);
/* Analysis bailed out before processing this node. */
if (iter->get_status () == exploded_node::STATUS_WORKLIST)
{
if (logger)
logger->log ("rejecting: EN: %i is still in worklist",
iter->m_index);
return nullptr;
}
if (visited.contains (iter))
{
/* We've looped back on ourselves. ENODE is in the loop
itself if ENODE is the first place we looped back,
as opposed to being on a path to a loop. */
if (iter == &enode)
{
if (logger)
logger->log ("accepting: looped back to EN: %i",
iter->m_index);
if (fg)
{
auto_timevar tv (TV_ANALYZER_DUMP);
pretty_printer pp;
pp_printf (&pp, "%s.en%i.infinite-loop.dot",
dump_base_name, enode.m_index);
char *filename = xstrdup (pp_formatted_text (&pp));
feasible_graph::dump_args_t dump_args (eg);
fg->dump_dot (filename, nullptr, dump_args);
free (filename);
}
return ::make_unique<infinite_loop> (enode,
first_loc,
std::move (eedges),
logger);
}
else
{
if (logger)
logger->log ("rejecting: looped back to EN: %i, not to EN: %i",
iter->m_index, enode.m_index);
return nullptr;
}
}
visited.add (iter);
if (first_loc == UNKNOWN_LOCATION)
{
location_t enode_loc = iter->get_point ().get_location ();
if (enode_loc != UNKNOWN_LOCATION)
first_loc = enode_loc;
}
/* Find the out-edges that are feasible, given the
constraints here. */
typedef std::pair<feasibility_state, const exploded_edge *> pair_t;
std::vector<pair_t> succs;
for (auto out_edge : iter->m_succs)
{
log_scope s (logger, "considering out-edge",
"EN:%i -> EN:%i",
out_edge->m_src->m_index,
out_edge->m_dest->m_index);
feasibility_state next_state (state);
/* Use this context to require edge conditions to be known,
rather than be merely "possible". */
infinite_loop_checking_context ctxt;
if (next_state.maybe_update_for_edge (logger,
out_edge,
&ctxt,
nullptr))
succs.push_back (pair_t (next_state, out_edge));
if (ctxt.unusable_p ())
{
/* If we get here, then we have e.g. a gcond where
the condition is UNKNOWN, or a condition
based on a widening_svalue. Reject such paths. */
if (logger)
logger->log ("rejecting: unusable");
return nullptr;
}
}
if (succs.size () != 1)
{
if (logger)
logger->log ("rejecting: %i feasible successors",
(int)succs.size ());
return nullptr;
}
const feasibility_state &next_state = succs[0].first;
const exploded_edge *out_eedge = succs[0].second;
if (out_eedge->could_do_work_p ())
{
if (logger)
logger->log ("rejecting: edge could do work");
return nullptr;
}
if (fg)
{
feasible_node *next_fnode = fg->add_node (out_eedge->m_dest,
next_state,
fg->m_nodes.length ());
fg->add_edge (new feasible_edge (curr_fnode, next_fnode, out_eedge));
curr_fnode = next_fnode;
}
state = next_state;
eedges.push_back (out_eedge);
if (first_loc == UNKNOWN_LOCATION)
{
if (out_eedge->m_sedge)
if (::edge cfg_edge = out_eedge->m_sedge->get_any_cfg_edge ())
if (cfg_edge->goto_locus > BUILTINS_LOCATION)
first_loc = cfg_edge->goto_locus;
}
iter = out_eedge->m_dest;
}
}
/* Implementation of -Wanalyzer-infinite-loop. */
void
exploded_graph::detect_infinite_loops ()
{
LOG_FUNC (get_logger ());
auto_timevar tv (TV_ANALYZER_INFINITE_LOOPS);
/* Track all enodes we've warned for; both the loop entrypoints
and all the enodes within those loops. */
hash_set<const exploded_node *> warned_for;
for (auto enode : m_nodes)
{
if (get_logger ())
get_logger ()->log ("visited: %i out of %i",
(int)warned_for.elements (), m_nodes.length ());
/* Only warn about the first enode we encounter in each cycle. */
if (warned_for.contains(enode))
continue;
if (std::unique_ptr<infinite_loop> inf_loop
= starts_infinite_loop_p (*enode, *this, get_logger ()))
{
const supernode *snode = enode->get_supernode ();
if (get_logger ())
get_logger ()->log ("EN: %i from starts_infinite_loop_p",
enode->m_index);
for (auto iter : inf_loop->m_eedge_vec)
warned_for.add (iter->m_src);
gcc_assert (warned_for.contains(enode));
if (inf_loop->m_loc == UNKNOWN_LOCATION)
{
if (get_logger ())
get_logger ()->log
("no location available for reporting infinite loop");
continue;
}
pending_location ploc (enode, snode, inf_loop->m_loc);
auto d
= ::make_unique<infinite_loop_diagnostic> (std::move (inf_loop));
get_diagnostic_manager ().add_diagnostic (ploc, std::move (d));
}
}
}
|