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
|
/* Handling for the various __analyzer_* known functions.
Copyright (C) 2020-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 "function.h"
#include "basic-block.h"
#include "gimple.h"
#include "diagnostic-core.h"
#include "analyzer/analyzer.h"
#include "analyzer/analyzer-logging.h"
#include "diagnostic.h"
#include "tree-diagnostic.h" /* for default_tree_printer. */
#include "analyzer/region-model.h"
#include "analyzer/pending-diagnostic.h"
#include "analyzer/call-details.h"
#include "make-unique.h"
#if ENABLE_ANALYZER
namespace ana {
/* Handle calls to "__analyzer_break" by triggering a breakpoint within
the analyzer. */
class kf_analyzer_break : public known_function
{
public:
bool matches_call_types_p (const call_details &cd) const final override
{
return cd.num_args () == 0;
}
void impl_call_pre (const call_details &) const final override
{
/* TODO: is there a good cross-platform way to do this? */
raise (SIGINT);
}
};
/* Handler for calls to "__analyzer_describe".
Emit a warning describing the 2nd argument (which can be of any
type), at the given verbosity level. This is for use when
debugging, and may be of use in DejaGnu tests. */
class kf_analyzer_describe : public known_function
{
public:
bool matches_call_types_p (const call_details &cd) const final override
{
return cd.num_args () == 2;
}
void impl_call_pre (const call_details &cd) const final override
{
if (!cd.get_ctxt ())
return;
tree t_verbosity = cd.get_arg_tree (0);
const svalue *sval = cd.get_arg_svalue (1);
bool simple = zerop (t_verbosity);
label_text desc = sval->get_desc (simple);
warning_at (cd.get_location (), 0, "svalue: %qs", desc.get ());
}
};
/* Handler for calls to "__analyzer_dump_capacity".
Emit a warning describing the capacity of the base region of
the region pointed to by the 1st argument.
This is for use when debugging, and may be of use in DejaGnu tests. */
class kf_analyzer_dump_capacity : public known_function
{
public:
bool matches_call_types_p (const call_details &cd) const final override
{
return (cd.num_args () == 1
&& cd.arg_is_pointer_p (0));
}
void impl_call_pre (const call_details &cd) const final override
{
region_model_context *ctxt = cd.get_ctxt ();
if (!ctxt)
return;
region_model *model = cd.get_model ();
tree t_ptr = cd.get_arg_tree (0);
const svalue *sval_ptr = model->get_rvalue (t_ptr, ctxt);
const region *reg = model->deref_rvalue (sval_ptr, t_ptr, ctxt);
const region *base_reg = reg->get_base_region ();
const svalue *capacity = model->get_capacity (base_reg);
label_text desc = capacity->get_desc (true);
warning_at (cd.get_call_stmt ()->location, 0,
"capacity: %qs", desc.get ());
}
};
/* Compare D1 and D2 using their names, and then IDs to order them. */
static int
cmp_decls (tree d1, tree d2)
{
gcc_assert (DECL_P (d1));
gcc_assert (DECL_P (d2));
if (DECL_NAME (d1) && DECL_NAME (d2))
if (int cmp = strcmp (IDENTIFIER_POINTER (DECL_NAME (d1)),
IDENTIFIER_POINTER (DECL_NAME (d2))))
return cmp;
return (int)DECL_UID (d1) - (int)DECL_UID (d2);
}
/* Comparator for use by vec<tree>::qsort,
using their names, and then IDs to order them. */
static int
cmp_decls_ptr_ptr (const void *p1, const void *p2)
{
tree const *d1 = (tree const *)p1;
tree const *d2 = (tree const *)p2;
return cmp_decls (*d1, *d2);
}
/* Handler for calls to "__analyzer_dump_escaped".
Emit a warning giving the number of decls that have escaped, followed
by a comma-separated list of their names, in alphabetical order.
This is for use when debugging, and may be of use in DejaGnu tests. */
class kf_analyzer_dump_escaped : public known_function
{
public:
bool matches_call_types_p (const call_details &cd) const final override
{
return cd.num_args () == 0;
}
void impl_call_pre (const call_details &cd) const final override
{
region_model_context *ctxt = cd.get_ctxt ();
if (!ctxt)
return;
region_model *model = cd.get_model ();
auto_vec<tree> escaped_decls;
for (auto iter : *model->get_store ())
{
const binding_cluster *c = iter.second;
if (!c->escaped_p ())
continue;
if (tree decl = c->get_base_region ()->maybe_get_decl ())
escaped_decls.safe_push (decl);
}
/* Sort them into deterministic order; alphabetical is
probably most user-friendly. */
escaped_decls.qsort (cmp_decls_ptr_ptr);
pretty_printer pp;
pp_format_decoder (&pp) = default_tree_printer;
pp_show_color (&pp) = pp_show_color (global_dc->printer);
bool first = true;
for (auto iter : escaped_decls)
{
if (first)
first = false;
else
pp_string (&pp, ", ");
pp_printf (&pp, "%qD", iter);
}
/* Print the number to make it easier to write DejaGnu tests for
the "nothing has escaped" case. */
warning_at (cd.get_location (), 0, "escaped: %i: %s",
escaped_decls.length (),
pp_formatted_text (&pp));
}
};
/* Placeholder handler for calls to "__analyzer_dump_exploded_nodes".
This is a no-op; the real implementation happens when the
exploded_graph is postprocessed. */
class kf_analyzer_dump_exploded_nodes : public known_function
{
public:
bool matches_call_types_p (const call_details &cd) const final override
{
return cd.num_args () == 1;
}
};
/* Handler for calls to "__analyzer_dump_named_constant".
Look up the given name, and emit a warning describing the
state of the corresponding stashed value.
This is for use when debugging, and for DejaGnu tests. */
class kf_analyzer_dump_named_constant : public known_function
{
public:
bool matches_call_types_p (const call_details &cd) const final override
{
return cd.num_args () == 1;
}
void impl_call_pre (const call_details &cd) const final override
{
region_model_context *ctxt = cd.get_ctxt ();
if (!ctxt)
return;
const char *name = cd.get_arg_string_literal (0);
if (!name)
{
error_at (cd.get_location (), "cannot determine name");
return;
}
tree value = get_stashed_constant_by_name (name);
if (value)
warning_at (cd.get_location (), 0, "named constant %qs has value %qE",
name, value);
else
warning_at (cd.get_location (), 0, "named constant %qs has unknown value",
name);
}
};
/* A pending_diagnostic subclass for implementing "__analyzer_dump_path". */
class dump_path_diagnostic
: public pending_diagnostic_subclass<dump_path_diagnostic>
{
public:
int get_controlling_option () const final override
{
return 0;
}
bool emit (diagnostic_emission_context &ctxt) final override
{
ctxt.inform ("path");
return true;
}
const char *get_kind () const final override
{
return "dump_path_diagnostic";
}
bool operator== (const dump_path_diagnostic &) const
{
return true;
}
};
/* Handle calls to "__analyzer_dump_path" by queuing a diagnostic at this
exploded_node. */
class kf_analyzer_dump_path : public known_function
{
public:
bool matches_call_types_p (const call_details &cd) const final override
{
return cd.num_args () == 0;
}
void impl_call_pre (const call_details &cd) const final override
{
region_model_context *ctxt = cd.get_ctxt ();
if (!ctxt)
return;
ctxt->warn (make_unique<dump_path_diagnostic> ());
}
};
/* Handle calls to "__analyzer_dump_region_model" by dumping
the region model's state to stderr. */
class kf_analyzer_dump_region_model : public known_function
{
public:
bool matches_call_types_p (const call_details &cd) const final override
{
return cd.num_args () == 0;
}
void impl_call_pre (const call_details &cd) const final override
{
region_model_context *ctxt = cd.get_ctxt ();
if (!ctxt)
return;
region_model *model = cd.get_model ();
model->dump (false);
}
};
/* Handle a call to "__analyzer_eval" by evaluating the input
and dumping as a dummy warning, so that test cases can use
dg-warning to validate the result (and so unexpected warnings will
lead to DejaGnu failures).
Broken out as a subroutine to make it easier to put a breakpoint on it
- though typically this doesn't help, as we have an SSA name as the arg,
and what's more interesting is usually the def stmt for that name. */
class kf_analyzer_eval : public known_function
{
public:
bool matches_call_types_p (const call_details &cd) const final override
{
return cd.num_args () == 1;
}
void impl_call_pre (const call_details &cd) const final override
{
region_model_context *ctxt = cd.get_ctxt ();
if (!ctxt)
return;
region_model *model = cd.get_model ();
tree t_arg = cd.get_arg_tree (0);
tristate t = model->eval_condition (t_arg, NE_EXPR, integer_zero_node,
ctxt);
warning_at (cd.get_location (), 0, "%s", t.as_string ());
}
};
/* Handler for "__analyzer_get_unknown_ptr". */
class kf_analyzer_get_unknown_ptr : public known_function
{
public:
bool matches_call_types_p (const call_details &cd) const final override
{
return cd.num_args () == 0;
}
void impl_call_pre (const call_details &cd) const final override
{
region_model_manager *mgr = cd.get_manager ();
const svalue *ptr_sval
= mgr->get_or_create_unknown_svalue (cd.get_lhs_type ());
cd.maybe_set_lhs (ptr_sval);
}
};
/* Populate KFM with instances of known functions used for debugging the
analyzer and for writing DejaGnu tests, all with a "__analyzer_" prefix. */
void
register_known_analyzer_functions (known_function_manager &kfm)
{
kfm.add ("__analyzer_break", make_unique<kf_analyzer_break> ());
kfm.add ("__analyzer_describe", make_unique<kf_analyzer_describe> ());
kfm.add ("__analyzer_dump_capacity",
make_unique<kf_analyzer_dump_capacity> ());
kfm.add ("__analyzer_dump_escaped", make_unique<kf_analyzer_dump_escaped> ());
kfm.add ("__analyzer_dump_exploded_nodes",
make_unique<kf_analyzer_dump_exploded_nodes> ());
kfm.add ("__analyzer_dump_named_constant",
make_unique<kf_analyzer_dump_named_constant> ());
kfm.add ("__analyzer_dump_path", make_unique<kf_analyzer_dump_path> ());
kfm.add ("__analyzer_dump_region_model",
make_unique<kf_analyzer_dump_region_model> ());
kfm.add ("__analyzer_eval", make_unique<kf_analyzer_eval> ());
kfm.add ("__analyzer_get_unknown_ptr",
make_unique<kf_analyzer_get_unknown_ptr> ());
kfm.add ("__analyzer_get_strlen", make_kf_strlen ());
}
} // namespace ana
#endif /* #if ENABLE_ANALYZER */
|