Debian Codegraph @

gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.cc

raw
1 /* -fanalyzer plugin for CPython extension modules */
2 /* { dg-options "-g" } */
3
4 #define INCLUDE_MEMORY
5 #define INCLUDE_VECTOR
6 #include "gcc-plugin.h"
7 #include "config.h"
8 #include "system.h"
9 #include "coretypes.h"
10 #include "tree.h"
11 #include "function.h"
12 #include "basic-block.h"
13 #include "gimple.h"
14 #include "gimple-iterator.h"
15 #include "diagnostic-core.h"
16 #include "graphviz.h"
17 #include "options.h"
18 #include "cgraph.h"
19 #include "tree-dfa.h"
20 #include "stringpool.h"
21 #include "convert.h"
22 #include "target.h"
23 #include "fold-const.h"
24 #include "tree-pretty-print.h"
25 #include "diagnostic-color.h"
26 #include "diagnostic-metadata.h"
27 #include "tristate.h"
28 #include "bitmap.h"
29 #include "selftest.h"
30 #include "function.h"
31 #include "json.h"
32 #include "analyzer/analyzer.h"
33 #include "analyzer/analyzer-language.h"
34 #include "analyzer/analyzer-logging.h"
35 #include "ordered-hash-map.h"
36 #include "options.h"
37 #include "cgraph.h"
38 #include "cfg.h"
39 #include "digraph.h"
40 #include "analyzer/supergraph.h"
41 #include "sbitmap.h"
42 #include "analyzer/call-string.h"
43 #include "analyzer/program-point.h"
44 #include "analyzer/store.h"
45 #include "analyzer/region-model.h"
46 #include "analyzer/call-details.h"
47 #include "analyzer/call-info.h"
48 #include "analyzer/exploded-graph.h"
49 #include "make-unique.h"
50
51 int plugin_is_GPL_compatible;
52
53 #if ENABLE_ANALYZER
54 static GTY (()) hash_map<tree, tree> *analyzer_stashed_types;
55 static GTY (()) hash_map<tree, tree> *analyzer_stashed_globals;
56
57 namespace ana
58 {
59 static tree pyobj_record = NULL_TREE;
60 static tree pyobj_ptr_tree = NULL_TREE;
61 static tree pyobj_ptr_ptr = NULL_TREE;
62 static tree varobj_record = NULL_TREE;
63 static tree pylistobj_record = NULL_TREE;
64 static tree pylongobj_record = NULL_TREE;
65 static tree pylongtype_vardecl = NULL_TREE;
66 static tree pylisttype_vardecl = NULL_TREE;
67
68 static tree
69 get_field_by_name (tree type, const char *name)
70 {
71 for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
72 {
73 if (TREE_CODE (field) == FIELD_DECL)
74 {
75 const char *field_name = IDENTIFIER_POINTER (DECL_NAME (field));
76 if (strcmp (field_name, name) == 0)
77 return field;
78 }
79 }
80 return NULL_TREE;
81 }
82
83 static const svalue *
84 get_sizeof_pyobjptr (region_model_manager *mgr)
85 {
86 tree size_tree = TYPE_SIZE_UNIT (pyobj_ptr_tree);
87 const svalue *sizeof_sval = mgr->get_or_create_constant_svalue (size_tree);
88 return sizeof_sval;
89 }
90
91 /* Update MODEL to set OB_BASE_REGION's ob_refcnt to 1. */
92 static void
93 init_ob_refcnt_field (region_model_manager *mgr, region_model *model,
94 const region *ob_base_region, tree pyobj_record,
95 const call_details &cd)
96 {
97 tree ob_refcnt_tree = get_field_by_name (pyobj_record, "ob_refcnt");
98 const region *ob_refcnt_region
99 = mgr->get_field_region (ob_base_region, ob_refcnt_tree);
100 const svalue *refcnt_one_sval
101 = mgr->get_or_create_int_cst (size_type_node, 1);
102 model->set_value (ob_refcnt_region, refcnt_one_sval, cd.get_ctxt ());
103 }
104
105 /* Update MODEL to set OB_BASE_REGION's ob_type to point to
106 PYTYPE_VAR_DECL_PTR. */
107 static void
108 set_ob_type_field (region_model_manager *mgr, region_model *model,
109 const region *ob_base_region, tree pyobj_record,
110 tree pytype_var_decl_ptr, const call_details &cd)
111 {
112 const region *pylist_type_region
113 = mgr->get_region_for_global (pytype_var_decl_ptr);
114 tree pytype_var_decl_ptr_type
115 = build_pointer_type (TREE_TYPE (pytype_var_decl_ptr));
116 const svalue *pylist_type_ptr_sval
117 = mgr->get_ptr_svalue (pytype_var_decl_ptr_type, pylist_type_region);
118 tree ob_type_field = get_field_by_name (pyobj_record, "ob_type");
119 const region *ob_type_region
120 = mgr->get_field_region (ob_base_region, ob_type_field);
121 model->set_value (ob_type_region, pylist_type_ptr_sval, cd.get_ctxt ());
122 }
123
124 /* Retrieve the "ob_base" field's region from OBJECT_RECORD within
125 NEW_OBJECT_REGION and set its value in the MODEL to PYOBJ_SVALUE. */
126 static const region *
127 get_ob_base_region (region_model_manager *mgr, region_model *model,
128 const region *new_object_region, tree object_record,
129 const svalue *pyobj_svalue, const call_details &cd)
130 {
131 tree ob_base_tree = get_field_by_name (object_record, "ob_base");
132 const region *ob_base_region
133 = mgr->get_field_region (new_object_region, ob_base_tree);
134 model->set_value (ob_base_region, pyobj_svalue, cd.get_ctxt ());
135 return ob_base_region;
136 }
137
138 /* Initialize and retrieve a region within the MODEL for a PyObject
139 and set its value to OBJECT_SVALUE. */
140 static const region *
141 init_pyobject_region (region_model_manager *mgr, region_model *model,
142 const svalue *object_svalue, const call_details &cd)
143 {
144 const region *pyobject_region = model->get_or_create_region_for_heap_alloc (
145 NULL, cd.get_ctxt (), true, &cd);
146 model->set_value (pyobject_region, object_svalue, cd.get_ctxt ());
147 return pyobject_region;
148 }
149
150 /* Increment the value of FIELD_REGION in the MODEL by 1. Optionally
151 capture the old and new svalues if OLD_SVAL and NEW_SVAL pointers are
152 provided. */
153 static void
154 inc_field_val (region_model_manager *mgr, region_model *model,
155 const region *field_region, const tree type_node,
156 const call_details &cd, const svalue **old_sval = nullptr,
157 const svalue **new_sval = nullptr)
158 {
159 const svalue *tmp_old_sval
160 = model->get_store_value (field_region, cd.get_ctxt ());
161 const svalue *one_sval = mgr->get_or_create_int_cst (type_node, 1);
162 const svalue *tmp_new_sval = mgr->get_or_create_binop (
163 type_node, PLUS_EXPR, tmp_old_sval, one_sval);
164
165 model->set_value (field_region, tmp_new_sval, cd.get_ctxt ());
166
167 if (old_sval)
168 *old_sval = tmp_old_sval;
169
170 if (new_sval)
171 *new_sval = tmp_new_sval;
172 }
173
174 class pyobj_init_fail : public failed_call_info
175 {
176 public:
177 pyobj_init_fail (const call_details &cd) : failed_call_info (cd) {}
178
179 bool
180 update_model (region_model *model, const exploded_edge *,
181 region_model_context *ctxt) const final override
182 {
183 /* Return NULL; everything else is unchanged. */
184 const call_details cd (get_call_details (model, ctxt));
185 region_model_manager *mgr = cd.get_manager ();
186 if (cd.get_lhs_type ())
187 {
188 const svalue *zero
189 = mgr->get_or_create_int_cst (cd.get_lhs_type (), 0);
190 model->set_value (cd.get_lhs_region (), zero, cd.get_ctxt ());
191 }
192 return true;
193 }
194 };
195
196 /* This is just a copy of leak_stmt_finder for now (subject to change if
197 * necssary) */
198
199 class refcnt_stmt_finder : public stmt_finder
200 {
201 public:
202 refcnt_stmt_finder (const exploded_graph &eg, tree var)
203 : m_eg (eg), m_var (var)
204 {
205 }
206
207 std::unique_ptr<stmt_finder>
208 clone () const final override
209 {
210 return make_unique<refcnt_stmt_finder> (m_eg, m_var);
211 }
212
213 const gimple *
214 find_stmt (const exploded_path &epath) final override
215 {
216 logger *const logger = m_eg.get_logger ();
217 LOG_FUNC (logger);
218
219 if (m_var && TREE_CODE (m_var) == SSA_NAME)
220 {
221 /* Locate the final write to this SSA name in the path. */
222 const gimple *def_stmt = SSA_NAME_DEF_STMT (m_var);
223
224 int idx_of_def_stmt;
225 bool found = epath.find_stmt_backwards (def_stmt, &idx_of_def_stmt);
226 if (!found)
227 goto not_found;
228
229 /* What was the next write to the underlying var
230 after the SSA name was set? (if any). */
231
232 for (unsigned idx = idx_of_def_stmt + 1; idx < epath.m_edges.length ();
233 ++idx)
234 {
235 const exploded_edge *eedge = epath.m_edges[idx];
236 if (logger)
237 logger->log ("eedge[%i]: EN %i -> EN %i", idx,
238 eedge->m_src->m_index,
239 eedge->m_dest->m_index);
240 const exploded_node *dst_node = eedge->m_dest;
241 const program_point &dst_point = dst_node->get_point ();
242 const gimple *stmt = dst_point.get_stmt ();
243 if (!stmt)
244 continue;
245 if (const gassign *assign = dyn_cast<const gassign *> (stmt))
246 {
247 tree lhs = gimple_assign_lhs (assign);
248 if (TREE_CODE (lhs) == SSA_NAME
249 && SSA_NAME_VAR (lhs) == SSA_NAME_VAR (m_var))
250 return assign;
251 }
252 }
253 }
254
255 not_found:
256
257 /* Look backwards for the first statement with a location. */
258 int i;
259 const exploded_edge *eedge;
260 FOR_EACH_VEC_ELT_REVERSE (epath.m_edges, i, eedge)
261 {
262 if (logger)
263 logger->log ("eedge[%i]: EN %i -> EN %i", i, eedge->m_src->m_index,
264 eedge->m_dest->m_index);
265 const exploded_node *dst_node = eedge->m_dest;
266 const program_point &dst_point = dst_node->get_point ();
267 const gimple *stmt = dst_point.get_stmt ();
268 if (stmt)
269 if (get_pure_location (stmt->location) != UNKNOWN_LOCATION)
270 return stmt;
271 }
272
273 gcc_unreachable ();
274 return NULL;
275 }
276
277 void update_event_loc_info (event_loc_info &) final override
278 {
279 /* No-op. */
280 }
281
282 private:
283 const exploded_graph &m_eg;
284 tree m_var;
285 };
286
287 class refcnt_mismatch : public pending_diagnostic_subclass<refcnt_mismatch>
288 {
289 public:
290 refcnt_mismatch (const region *base_region,
291 const svalue *ob_refcnt,
292 const svalue *actual_refcnt,
293 tree reg_tree)
294 : m_base_region (base_region), m_ob_refcnt (ob_refcnt),
295 m_actual_refcnt (actual_refcnt), m_reg_tree(reg_tree)
296 {
297 }
298
299 const char *
300 get_kind () const final override
301 {
302 return "refcnt_mismatch";
303 }
304
305 bool
306 operator== (const refcnt_mismatch &other) const
307 {
308 return (m_base_region == other.m_base_region
309 && m_ob_refcnt == other.m_ob_refcnt
310 && m_actual_refcnt == other.m_actual_refcnt);
311 }
312
313 int get_controlling_option () const final override
314 {
315 return 0;
316 }
317
318 bool
319 emit (diagnostic_emission_context &ctxt) final override
320 {
321 bool warned;
322 // just assuming constants for now
323 auto actual_refcnt
324 = m_actual_refcnt->dyn_cast_constant_svalue ()->get_constant ();
325 auto ob_refcnt = m_ob_refcnt->dyn_cast_constant_svalue ()->get_constant ();
326 warned = ctxt.warn ("expected %qE to have "
327 "reference count: %qE but ob_refcnt field is: %qE",
328 m_reg_tree, actual_refcnt, ob_refcnt);
329
330 // location_t loc = rich_loc->get_loc ();
331 // foo (loc);
332 return warned;
333 }
334
335 void mark_interesting_stuff (interesting_t *interest) final override
336 {
337 if (m_base_region)
338 interest->add_region_creation (m_base_region);
339 }
340
341 private:
342
343 void foo(location_t loc) const
344 {
345 inform(loc, "something is up right here");
346 }
347 const region *m_base_region;
348 const svalue *m_ob_refcnt;
349 const svalue *m_actual_refcnt;
350 tree m_reg_tree;
351 };
352
353 /* Retrieves the svalue associated with the ob_refcnt field of the base region.
354 */
355 static const svalue *
356 retrieve_ob_refcnt_sval (const region *base_reg, const region_model *model,
357 region_model_context *ctxt)
358 {
359 region_model_manager *mgr = model->get_manager ();
360 tree ob_refcnt_tree = get_field_by_name (pyobj_record, "ob_refcnt");
361 const region *ob_refcnt_region
362 = mgr->get_field_region (base_reg, ob_refcnt_tree);
363 const svalue *ob_refcnt_sval
364 = model->get_store_value (ob_refcnt_region, ctxt);
365 return ob_refcnt_sval;
366 }
367
368 static void
369 increment_region_refcnt (hash_map<const region *, int> &map, const region *key)
370 {
371 bool existed;
372 auto &refcnt = map.get_or_insert (key, &existed);
373 refcnt = existed ? refcnt + 1 : 1;
374 }
375
376
377 /* Recursively fills in region_to_refcnt with the references owned by
378 pyobj_ptr_sval. */
379 static void
380 count_pyobj_references (const region_model *model,
381 hash_map<const region *, int> &region_to_refcnt,
382 const svalue *pyobj_ptr_sval,
383 hash_set<const region *> &seen)
384 {
385 if (!pyobj_ptr_sval)
386 return;
387
388 const auto *pyobj_region_sval = pyobj_ptr_sval->dyn_cast_region_svalue ();
389 const auto *pyobj_initial_sval = pyobj_ptr_sval->dyn_cast_initial_svalue ();
390 if (!pyobj_region_sval && !pyobj_initial_sval)
391 return;
392
393 // todo: support initial sval (e.g passed in as parameter)
394 if (pyobj_initial_sval)
395 {
396 // increment_region_refcnt (region_to_refcnt,
397 // pyobj_initial_sval->get_region ());
398 return;
399 }
400
401 const region *pyobj_region = pyobj_region_sval->get_pointee ();
402 if (!pyobj_region || seen.contains (pyobj_region))
403 return;
404
405 seen.add (pyobj_region);
406
407 if (pyobj_ptr_sval->get_type () == pyobj_ptr_tree)
408 increment_region_refcnt (region_to_refcnt, pyobj_region);
409
410 const auto *curr_store = model->get_store ();
411 const auto *retval_cluster = curr_store->get_cluster (pyobj_region);
412 if (!retval_cluster)
413 return;
414
415 const auto &retval_binding_map = retval_cluster->get_map ();
416
417 for (const auto &binding : retval_binding_map)
418 {
419 const svalue *binding_sval = binding.second;
420 const svalue *unwrapped_sval = binding_sval->unwrap_any_unmergeable ();
421 const region *pointee = unwrapped_sval->maybe_get_region ();
422
423 if (pointee && pointee->get_kind () == RK_HEAP_ALLOCATED)
424 count_pyobj_references (model, region_to_refcnt, binding_sval, seen);
425 }
426 }
427
428 /* Compare ob_refcnt field vs the actual reference count of a region */
429 static void
430 check_refcnt (const region_model *model,
431 const region_model *old_model,
432 region_model_context *ctxt,
433 const hash_map<const ana::region *,
434 int>::iterator::reference_pair region_refcnt)
435 {
436 region_model_manager *mgr = model->get_manager ();
437 const auto &curr_region = region_refcnt.first;
438 const auto &actual_refcnt = region_refcnt.second;
439 const svalue *ob_refcnt_sval
440 = retrieve_ob_refcnt_sval (curr_region, model, ctxt);
441 const svalue *actual_refcnt_sval = mgr->get_or_create_int_cst (
442 ob_refcnt_sval->get_type (), actual_refcnt);
443
444 if (ob_refcnt_sval != actual_refcnt_sval)
445 {
446 const svalue *curr_reg_sval
447 = mgr->get_ptr_svalue (pyobj_ptr_tree, curr_region);
448 tree reg_tree = old_model->get_representative_tree (curr_reg_sval);
449 if (!reg_tree)
450 return;
451
452 const auto &eg = ctxt->get_eg ();
453 refcnt_stmt_finder finder (*eg, reg_tree);
454 auto pd = make_unique<refcnt_mismatch> (curr_region, ob_refcnt_sval,
455 actual_refcnt_sval, reg_tree);
456 if (pd && eg)
457 ctxt->warn (std::move (pd), &finder);
458 }
459 }
460
461 static void
462 check_refcnts (const region_model *model,
463 const region_model *old_model,
464 const svalue *retval,
465 region_model_context *ctxt,
466 hash_map<const region *, int> &region_to_refcnt)
467 {
468 for (const auto &region_refcnt : region_to_refcnt)
469 {
470 check_refcnt (model, old_model, ctxt, region_refcnt);
471 }
472 }
473
474 /* Validates the reference count of all Python objects. */
475 void
476 pyobj_refcnt_checker (const region_model *model,
477 const region_model *old_model,
478 const svalue *retval,
479 region_model_context *ctxt)
480 {
481 if (!ctxt)
482 return;
483
484 hash_map<const region *, int> region_to_refcnt;
485 hash_set<const region *> seen_regions;
486
487 count_pyobj_references (model, region_to_refcnt, retval, seen_regions);
488 check_refcnts (model, old_model, retval, ctxt, region_to_refcnt);
489 }
490
491 /* Counts the actual pyobject references from all clusters in the model's
492 * store. */
493 static void
494 count_all_references (const region_model *model,
495 hash_map<const region *, int> &region_to_refcnt)
496 {
497 for (const auto &cluster : *model->get_store ())
498 {
499 auto curr_region = cluster.first;
500 if (curr_region->get_kind () != RK_HEAP_ALLOCATED)
501 continue;
502
503 increment_region_refcnt (region_to_refcnt, curr_region);
504
505 auto binding_cluster = cluster.second;
506 for (const auto &binding : binding_cluster->get_map ())
507 {
508 const svalue *binding_sval = binding.second;
509
510 const svalue *unwrapped_sval
511 = binding_sval->unwrap_any_unmergeable ();
512 // if (unwrapped_sval->get_type () != pyobj_ptr_tree)
513 // continue;
514
515 const region *pointee = unwrapped_sval->maybe_get_region ();
516 if (!pointee || pointee->get_kind () != RK_HEAP_ALLOCATED)
517 continue;
518
519 increment_region_refcnt (region_to_refcnt, pointee);
520 }
521 }
522 }
523
524 static void
525 dump_refcnt_info (const hash_map<const region *, int> &region_to_refcnt,
526 const region_model *model,
527 region_model_context *ctxt)
528 {
529 region_model_manager *mgr = model->get_manager ();
530 pretty_printer pp;
531 pp_format_decoder (&pp) = default_tree_printer;
532 pp_show_color (&pp) = pp_show_color (global_dc->get_reference_printer ());
533 pp.set_output_stream (stderr);
534
535 for (const auto &region_refcnt : region_to_refcnt)
536 {
537 auto region = region_refcnt.first;
538 auto actual_refcnt = region_refcnt.second;
539 const svalue *ob_refcnt_sval
540 = retrieve_ob_refcnt_sval (region, model, ctxt);
541 const svalue *actual_refcnt_sval = mgr->get_or_create_int_cst (
542 ob_refcnt_sval->get_type (), actual_refcnt);
543
544 region->dump_to_pp (&pp, true);
545 pp_string (&pp, " — ob_refcnt: ");
546 ob_refcnt_sval->dump_to_pp (&pp, true);
547 pp_string (&pp, " actual refcnt: ");
548 actual_refcnt_sval->dump_to_pp (&pp, true);
549 pp_newline (&pp);
550 }
551 pp_string (&pp, "~~~~~~~~\n");
552 pp_flush (&pp);
553 }
554
555 class kf_analyzer_cpython_dump_refcounts : public known_function
556 {
557 public:
558 bool matches_call_types_p (const call_details &cd) const final override
559 {
560 return cd.num_args () == 0;
561 }
562 void impl_call_pre (const call_details &cd) const final override
563 {
564 region_model_context *ctxt = cd.get_ctxt ();
565 if (!ctxt)
566 return;
567 region_model *model = cd.get_model ();
568 hash_map<const region *, int> region_to_refcnt;
569 count_all_references(model, region_to_refcnt);
570 dump_refcnt_info(region_to_refcnt, model, ctxt);
571 }
572 };
573
574 /* Some concessions were made to
575 simplify the analysis process when comparing kf_PyList_Append with the
576 real implementation. In particular, PyList_Append performs some
577 optimization internally to try and avoid calls to realloc if
578 possible. For simplicity, we assume that realloc is called every time.
579 Also, we grow the size by just 1 (to ensure enough space for adding a
580 new element) rather than abide by the heuristics that the actual implementation
581 follows. */
582 class kf_PyList_Append : public known_function
583 {
584 public:
585 bool
586 matches_call_types_p (const call_details &cd) const final override
587 {
588 return (cd.num_args () == 2 && cd.arg_is_pointer_p (0)
589 && cd.arg_is_pointer_p (1));
590 }
591 void impl_call_pre (const call_details &cd) const final override;
592 void impl_call_post (const call_details &cd) const final override;
593 };
594
595 void
596 kf_PyList_Append::impl_call_pre (const call_details &cd) const
597 {
598 region_model_manager *mgr = cd.get_manager ();
599 region_model *model = cd.get_model ();
600
601 const svalue *pylist_sval = cd.get_arg_svalue (0);
602 const region *pylist_reg
603 = model->deref_rvalue (pylist_sval, cd.get_arg_tree (0), cd.get_ctxt ());
604
605 const svalue *newitem_sval = cd.get_arg_svalue (1);
606 const region *newitem_reg
607 = model->deref_rvalue (pylist_sval, cd.get_arg_tree (0), cd.get_ctxt ());
608
609 // Skip checks if unknown etc
610 if (pylist_sval->get_kind () != SK_REGION
611 && pylist_sval->get_kind () != SK_CONSTANT)
612 return;
613
614 // PyList_Check
615 tree ob_type_field = get_field_by_name (pyobj_record, "ob_type");
616 const region *ob_type_region
617 = mgr->get_field_region (pylist_reg, ob_type_field);
618 const svalue *stored_sval
619 = model->get_store_value (ob_type_region, cd.get_ctxt ());
620 const region *pylist_type_region
621 = mgr->get_region_for_global (pylisttype_vardecl);
622 tree pylisttype_vardecl_ptr
623 = build_pointer_type (TREE_TYPE (pylisttype_vardecl));
624 const svalue *pylist_type_ptr
625 = mgr->get_ptr_svalue (pylisttype_vardecl_ptr, pylist_type_region);
626
627 if (stored_sval != pylist_type_ptr)
628 {
629 // TODO: emit diagnostic -Wanalyzer-type-error
630 cd.get_ctxt ()->terminate_path ();
631 return;
632 }
633
634 // Check that new_item is not null.
635 {
636 const svalue *null_ptr
637 = mgr->get_or_create_int_cst (newitem_sval->get_type (), 0);
638 if (!model->add_constraint (newitem_sval, NE_EXPR, null_ptr,
639 cd.get_ctxt ()))
640 {
641 // TODO: emit diagnostic here
642 cd.get_ctxt ()->terminate_path ();
643 return;
644 }
645 }
646 }
647
648 void
649 kf_PyList_Append::impl_call_post (const call_details &cd) const
650 {
651 /* Three custom subclasses of custom_edge_info, for handling the various
652 outcomes of "realloc". */
653
654 /* Concrete custom_edge_info: a realloc call that fails, returning NULL.
655 */
656 class realloc_failure : public failed_call_info
657 {
658 public:
659 realloc_failure (const call_details &cd) : failed_call_info (cd) {}
660
661 bool
662 update_model (region_model *model, const exploded_edge *,
663 region_model_context *ctxt) const final override
664 {
665 const call_details cd (get_call_details (model, ctxt));
666 region_model_manager *mgr = cd.get_manager ();
667
668 const svalue *pylist_sval = cd.get_arg_svalue (0);
669 const region *pylist_reg = model->deref_rvalue (
670 pylist_sval, cd.get_arg_tree (0), cd.get_ctxt ());
671
672 /* Identify ob_item field and set it to NULL. */
673 tree ob_item_field = get_field_by_name (pylistobj_record, "ob_item");
674 const region *ob_item_reg
675 = mgr->get_field_region (pylist_reg, ob_item_field);
676 const svalue *old_ptr_sval
677 = model->get_store_value (ob_item_reg, cd.get_ctxt ());
678
679 if (const region_svalue *old_reg
680 = old_ptr_sval->dyn_cast_region_svalue ())
681 {
682 const region *freed_reg = old_reg->get_pointee ();
683 model->unbind_region_and_descendents (freed_reg, POISON_KIND_FREED);
684 model->unset_dynamic_extents (freed_reg);
685 }
686
687 const svalue *null_sval = mgr->get_or_create_null_ptr (pyobj_ptr_ptr);
688 model->set_value (ob_item_reg, null_sval, cd.get_ctxt ());
689
690 if (cd.get_lhs_type ())
691 {
692 const svalue *neg_one
693 = mgr->get_or_create_int_cst (cd.get_lhs_type (), -1);
694 cd.maybe_set_lhs(neg_one);
695 }
696 return true;
697 }
698 };
699
700 class realloc_success_no_move : public call_info
701 {
702 public:
703 realloc_success_no_move (const call_details &cd) : call_info (cd) {}
704
705 void
706 print_desc (pretty_printer &pp) const final override
707 {
708 pp_printf (&pp,
709 "when %qE succeeds, without moving underlying buffer",
710 get_fndecl ());
711 }
712
713 bool
714 update_model (region_model *model, const exploded_edge *,
715 region_model_context *ctxt) const final override
716 {
717 const call_details cd (get_call_details (model, ctxt));
718 region_model_manager *mgr = cd.get_manager ();
719
720 const svalue *pylist_sval = cd.get_arg_svalue (0);
721 const region *pylist_reg = model->deref_rvalue (
722 pylist_sval, cd.get_arg_tree (0), cd.get_ctxt ());
723
724 const svalue *newitem_sval = cd.get_arg_svalue (1);
725 const region *newitem_reg = model->deref_rvalue (
726 newitem_sval, cd.get_arg_tree (1), cd.get_ctxt ());
727
728 tree ob_size_field = get_field_by_name (varobj_record, "ob_size");
729 const region *ob_size_region
730 = mgr->get_field_region (pylist_reg, ob_size_field);
731 const svalue *ob_size_sval = nullptr;
732 const svalue *new_size_sval = nullptr;
733 inc_field_val (mgr, model, ob_size_region, integer_type_node, cd,
734 &ob_size_sval, &new_size_sval);
735
736 const svalue *sizeof_sval = mgr->get_or_create_cast (
737 ob_size_sval->get_type (), get_sizeof_pyobjptr (mgr));
738 const svalue *num_allocated_bytes = mgr->get_or_create_binop (
739 size_type_node, MULT_EXPR, sizeof_sval, new_size_sval);
740
741 tree ob_item_field = get_field_by_name (pylistobj_record, "ob_item");
742 const region *ob_item_region
743 = mgr->get_field_region (pylist_reg, ob_item_field);
744 const svalue *ob_item_ptr_sval
745 = model->get_store_value (ob_item_region, cd.get_ctxt ());
746
747 /* We can only grow in place with a non-NULL pointer and no unknown
748 */
749 {
750 const svalue *null_ptr = mgr->get_or_create_null_ptr (pyobj_ptr_ptr);
751 if (!model->add_constraint (ob_item_ptr_sval, NE_EXPR, null_ptr,
752 cd.get_ctxt ()))
753 {
754 return false;
755 }
756 }
757
758 const unmergeable_svalue *underlying_svalue
759 = ob_item_ptr_sval->dyn_cast_unmergeable_svalue ();
760 const svalue *target_svalue = nullptr;
761 const region_svalue *target_region_svalue = nullptr;
762
763 if (underlying_svalue)
764 {
765 target_svalue = underlying_svalue->get_arg ();
766 if (target_svalue->get_kind () != SK_REGION)
767 {
768 return false;
769 }
770 }
771 else
772 {
773 if (ob_item_ptr_sval->get_kind () != SK_REGION)
774 {
775 return false;
776 }
777 target_svalue = ob_item_ptr_sval;
778 }
779
780 target_region_svalue = target_svalue->dyn_cast_region_svalue ();
781 const region *curr_reg = target_region_svalue->get_pointee ();
782
783 if (compat_types_p (num_allocated_bytes->get_type (), size_type_node))
784 model->set_dynamic_extents (curr_reg, num_allocated_bytes, ctxt);
785
786 model->set_value (ob_size_region, new_size_sval, ctxt);
787
788 const svalue *offset_sval = mgr->get_or_create_binop (
789 size_type_node, MULT_EXPR, sizeof_sval, ob_size_sval);
790 const region *element_region
791 = mgr->get_offset_region (curr_reg, pyobj_ptr_ptr, offset_sval);
792 model->set_value (element_region, newitem_sval, cd.get_ctxt ());
793
794 // Increment ob_refcnt of appended item.
795 tree ob_refcnt_tree = get_field_by_name (pyobj_record, "ob_refcnt");
796 const region *ob_refcnt_region
797 = mgr->get_field_region (newitem_reg, ob_refcnt_tree);
798 inc_field_val (mgr, model, ob_refcnt_region, size_type_node, cd);
799
800 if (cd.get_lhs_type ())
801 {
802 const svalue *zero
803 = mgr->get_or_create_int_cst (cd.get_lhs_type (), 0);
804 cd.maybe_set_lhs(zero);
805 }
806 return true;
807 }
808 };
809
810 class realloc_success_move : public call_info
811 {
812 public:
813 realloc_success_move (const call_details &cd) : call_info (cd) {}
814
815 void
816 print_desc (pretty_printer &pp) const final override
817 {
818 pp_printf (&pp,
819 "when %qE succeeds, moving buffer",
820 get_fndecl ());
821 }
822
823 bool
824 update_model (region_model *model, const exploded_edge *,
825 region_model_context *ctxt) const final override
826 {
827 const call_details cd (get_call_details (model, ctxt));
828 region_model_manager *mgr = cd.get_manager ();
829 const svalue *pylist_sval = cd.get_arg_svalue (0);
830 const region *pylist_reg = model->deref_rvalue (
831 pylist_sval, cd.get_arg_tree (0), cd.get_ctxt ());
832
833 const svalue *newitem_sval = cd.get_arg_svalue (1);
834 const region *newitem_reg = model->deref_rvalue (
835 newitem_sval, cd.get_arg_tree (1), cd.get_ctxt ());
836
837 tree ob_size_field = get_field_by_name (varobj_record, "ob_size");
838 const region *ob_size_region
839 = mgr->get_field_region (pylist_reg, ob_size_field);
840 const svalue *old_ob_size_sval = nullptr;
841 const svalue *new_ob_size_sval = nullptr;
842 inc_field_val (mgr, model, ob_size_region, integer_type_node, cd,
843 &old_ob_size_sval, &new_ob_size_sval);
844
845 const svalue *sizeof_sval = mgr->get_or_create_cast (
846 old_ob_size_sval->get_type (), get_sizeof_pyobjptr (mgr));
847 const svalue *new_size_sval = mgr->get_or_create_binop (
848 size_type_node, MULT_EXPR, sizeof_sval, new_ob_size_sval);
849
850 tree ob_item_field = get_field_by_name (pylistobj_record, "ob_item");
851 const region *ob_item_reg
852 = mgr->get_field_region (pylist_reg, ob_item_field);
853 const svalue *old_ptr_sval
854 = model->get_store_value (ob_item_reg, cd.get_ctxt ());
855
856 /* Create the new region. */
857 const region *new_reg = model->get_or_create_region_for_heap_alloc (
858 new_size_sval, cd.get_ctxt ());
859 const svalue *new_ptr_sval
860 = mgr->get_ptr_svalue (pyobj_ptr_ptr, new_reg);
861 if (!model->add_constraint (new_ptr_sval, NE_EXPR, old_ptr_sval,
862 cd.get_ctxt ()))
863 return false;
864
865 if (const region_svalue *old_reg
866 = old_ptr_sval->dyn_cast_region_svalue ())
867 {
868 const region *freed_reg = old_reg->get_pointee ();
869 const svalue *old_size_sval = model->get_dynamic_extents (freed_reg);
870 if (old_size_sval)
871 {
872 const svalue *copied_size_sval
873 = get_copied_size (model, old_size_sval, new_size_sval);
874 const region *copied_old_reg = mgr->get_sized_region (
875 freed_reg, pyobj_ptr_ptr, copied_size_sval);
876 const svalue *buffer_content_sval
877 = model->get_store_value (copied_old_reg, cd.get_ctxt ());
878 const region *copied_new_reg = mgr->get_sized_region (
879 new_reg, pyobj_ptr_ptr, copied_size_sval);
880 model->set_value (copied_new_reg, buffer_content_sval,
881 cd.get_ctxt ());
882 }
883 else
884 {
885 model->mark_region_as_unknown (freed_reg, cd.get_uncertainty ());
886 }
887
888 model->unbind_region_and_descendents (freed_reg, POISON_KIND_FREED);
889 model->unset_dynamic_extents (freed_reg);
890 }
891
892 const svalue *null_ptr = mgr->get_or_create_null_ptr (pyobj_ptr_ptr);
893 if (!model->add_constraint (new_ptr_sval, NE_EXPR, null_ptr,
894 cd.get_ctxt ()))
895 return false;
896
897 model->set_value (ob_size_region, new_ob_size_sval, ctxt);
898 model->set_value (ob_item_reg, new_ptr_sval, cd.get_ctxt ());
899
900 const svalue *offset_sval = mgr->get_or_create_binop (
901 size_type_node, MULT_EXPR, sizeof_sval, old_ob_size_sval);
902 const region *element_region
903 = mgr->get_offset_region (new_reg, pyobj_ptr_ptr, offset_sval);
904 model->set_value (element_region, newitem_sval, cd.get_ctxt ());
905
906 // Increment ob_refcnt of appended item.
907 tree ob_refcnt_tree = get_field_by_name (pyobj_record, "ob_refcnt");
908 const region *ob_refcnt_region
909 = mgr->get_field_region (newitem_reg, ob_refcnt_tree);
910 inc_field_val (mgr, model, ob_refcnt_region, size_type_node, cd);
911
912 if (cd.get_lhs_type ())
913 {
914 const svalue *zero
915 = mgr->get_or_create_int_cst (cd.get_lhs_type (), 0);
916 cd.maybe_set_lhs(zero);
917 }
918 return true;
919 }
920
921 private:
922 /* Return the lesser of OLD_SIZE_SVAL and NEW_SIZE_SVAL.
923 If unknown, OLD_SIZE_SVAL is returned. */
924 const svalue *
925 get_copied_size (region_model *model, const svalue *old_size_sval,
926 const svalue *new_size_sval) const
927 {
928 tristate res
929 = model->eval_condition (old_size_sval, GT_EXPR, new_size_sval);
930 switch (res.get_value ())
931 {
932 case tristate::TS_TRUE:
933 return new_size_sval;
934 case tristate::TS_FALSE:
935 case tristate::TS_UNKNOWN:
936 return old_size_sval;
937 default:
938 gcc_unreachable ();
939 }
940 }
941 };
942
943 /* Body of kf_PyList_Append::impl_call_post. */
944 if (cd.get_ctxt ())
945 {
946 cd.get_ctxt ()->bifurcate (make_unique<realloc_failure> (cd));
947 cd.get_ctxt ()->bifurcate (make_unique<realloc_success_no_move> (cd));
948 cd.get_ctxt ()->bifurcate (make_unique<realloc_success_move> (cd));
949 cd.get_ctxt ()->terminate_path ();
950 }
951 }
952
953 class kf_PyList_New : public known_function
954 {
955 public:
956 bool
957 matches_call_types_p (const call_details &cd) const final override
958 {
959 return (cd.num_args () == 1 && cd.arg_is_integral_p (0));
960 }
961 void impl_call_post (const call_details &cd) const final override;
962 };
963
964 void
965 kf_PyList_New::impl_call_post (const call_details &cd) const
966 {
967 class success : public success_call_info
968 {
969 public:
970 success (const call_details &cd) : success_call_info (cd) {}
971
972 bool
973 update_model (region_model *model, const exploded_edge *,
974 region_model_context *ctxt) const final override
975 {
976 const call_details cd (get_call_details (model, ctxt));
977 region_model_manager *mgr = cd.get_manager ();
978
979 const svalue *pyobj_svalue
980 = mgr->get_or_create_unknown_svalue (pyobj_record);
981 const svalue *varobj_svalue
982 = mgr->get_or_create_unknown_svalue (varobj_record);
983 const svalue *pylist_svalue
984 = mgr->get_or_create_unknown_svalue (pylistobj_record);
985
986 const svalue *size_sval = cd.get_arg_svalue (0);
987
988 const region *pylist_region
989 = init_pyobject_region (mgr, model, pylist_svalue, cd);
990
991 /*
992 typedef struct
993 {
994 PyObject_VAR_HEAD
995 PyObject **ob_item;
996 Py_ssize_t allocated;
997 } PyListObject;
998 */
999 tree varobj_field = get_field_by_name (pylistobj_record, "ob_base");
1000 const region *varobj_region
1001 = mgr->get_field_region (pylist_region, varobj_field);
1002 model->set_value (varobj_region, varobj_svalue, cd.get_ctxt ());
1003
1004 tree ob_item_field = get_field_by_name (pylistobj_record, "ob_item");
1005 const region *ob_item_region
1006 = mgr->get_field_region (pylist_region, ob_item_field);
1007
1008 const svalue *zero_sval = mgr->get_or_create_int_cst (size_type_node, 0);
1009 const svalue *casted_size_sval
1010 = mgr->get_or_create_cast (size_type_node, size_sval);
1011 const svalue *size_cond_sval = mgr->get_or_create_binop (
1012 size_type_node, LE_EXPR, casted_size_sval, zero_sval);
1013
1014 // if size <= 0, ob_item = NULL
1015
1016 if (tree_int_cst_equal (size_cond_sval->maybe_get_constant (),
1017 integer_one_node))
1018 {
1019 const svalue *null_sval
1020 = mgr->get_or_create_null_ptr (pyobj_ptr_ptr);
1021 model->set_value (ob_item_region, null_sval, cd.get_ctxt ());
1022 }
1023 else // calloc
1024 {
1025 const svalue *sizeof_sval = mgr->get_or_create_cast (
1026 size_sval->get_type (), get_sizeof_pyobjptr (mgr));
1027 const svalue *prod_sval = mgr->get_or_create_binop (
1028 size_type_node, MULT_EXPR, sizeof_sval, size_sval);
1029 const region *ob_item_sized_region
1030 = model->get_or_create_region_for_heap_alloc (prod_sval,
1031 cd.get_ctxt ());
1032 model->zero_fill_region (ob_item_sized_region, cd.get_ctxt ());
1033 const svalue *ob_item_ptr_sval
1034 = mgr->get_ptr_svalue (pyobj_ptr_ptr, ob_item_sized_region);
1035 const svalue *ob_item_unmergeable
1036 = mgr->get_or_create_unmergeable (ob_item_ptr_sval);
1037 model->set_value (ob_item_region, ob_item_unmergeable,
1038 cd.get_ctxt ());
1039 }
1040
1041 /*
1042 typedef struct {
1043 PyObject ob_base;
1044 Py_ssize_t ob_size; // Number of items in variable part
1045 } PyVarObject;
1046 */
1047 const region *ob_base_region = get_ob_base_region (
1048 mgr, model, varobj_region, varobj_record, pyobj_svalue, cd);
1049
1050 tree ob_size_tree = get_field_by_name (varobj_record, "ob_size");
1051 const region *ob_size_region
1052 = mgr->get_field_region (varobj_region, ob_size_tree);
1053 model->set_value (ob_size_region, size_sval, cd.get_ctxt ());
1054
1055 /*
1056 typedef struct _object {
1057 _PyObject_HEAD_EXTRA
1058 Py_ssize_t ob_refcnt;
1059 PyTypeObject *ob_type;
1060 } PyObject;
1061 */
1062
1063 // Initialize ob_refcnt field to 1.
1064 init_ob_refcnt_field(mgr, model, ob_base_region, pyobj_record, cd);
1065
1066 // Get pointer svalue for PyList_Type then assign it to ob_type field.
1067 set_ob_type_field(mgr, model, ob_base_region, pyobj_record, pylisttype_vardecl, cd);
1068
1069 if (cd.get_lhs_type ())
1070 {
1071 const svalue *ptr_sval
1072 = mgr->get_ptr_svalue (cd.get_lhs_type (), pylist_region);
1073 cd.maybe_set_lhs (ptr_sval);
1074 }
1075 return true;
1076 }
1077 };
1078
1079 if (cd.get_ctxt ())
1080 {
1081 cd.get_ctxt ()->bifurcate (make_unique<pyobj_init_fail> (cd));
1082 cd.get_ctxt ()->bifurcate (make_unique<success> (cd));
1083 cd.get_ctxt ()->terminate_path ();
1084 }
1085 }
1086
1087 class kf_PyLong_FromLong : public known_function
1088 {
1089 public:
1090 bool
1091 matches_call_types_p (const call_details &cd) const final override
1092 {
1093 return (cd.num_args () == 1 && cd.arg_is_integral_p (0));
1094 }
1095 void impl_call_post (const call_details &cd) const final override;
1096 };
1097
1098 void
1099 kf_PyLong_FromLong::impl_call_post (const call_details &cd) const
1100 {
1101 class success : public success_call_info
1102 {
1103 public:
1104 success (const call_details &cd) : success_call_info (cd) {}
1105
1106 bool
1107 update_model (region_model *model, const exploded_edge *,
1108 region_model_context *ctxt) const final override
1109 {
1110 const call_details cd (get_call_details (model, ctxt));
1111 region_model_manager *mgr = cd.get_manager ();
1112
1113 const svalue *pyobj_svalue
1114 = mgr->get_or_create_unknown_svalue (pyobj_record);
1115 const svalue *pylongobj_sval
1116 = mgr->get_or_create_unknown_svalue (pylongobj_record);
1117
1118 const region *pylong_region
1119 = init_pyobject_region (mgr, model, pylongobj_sval, cd);
1120
1121 // Create a region for the base PyObject within the PyLongObject.
1122 const region *ob_base_region = get_ob_base_region (
1123 mgr, model, pylong_region, pylongobj_record, pyobj_svalue, cd);
1124
1125 // Initialize ob_refcnt field to 1.
1126 init_ob_refcnt_field(mgr, model, ob_base_region, pyobj_record, cd);
1127
1128 // Get pointer svalue for PyLong_Type then assign it to ob_type field.
1129 set_ob_type_field(mgr, model, ob_base_region, pyobj_record, pylongtype_vardecl, cd);
1130
1131 // Set the PyLongObject value.
1132 tree ob_digit_field = get_field_by_name (pylongobj_record, "ob_digit");
1133 const region *ob_digit_region
1134 = mgr->get_field_region (pylong_region, ob_digit_field);
1135 const svalue *ob_digit_sval = cd.get_arg_svalue (0);
1136 model->set_value (ob_digit_region, ob_digit_sval, cd.get_ctxt ());
1137
1138 if (cd.get_lhs_type ())
1139 {
1140 const svalue *ptr_sval
1141 = mgr->get_ptr_svalue (cd.get_lhs_type (), pylong_region);
1142 cd.maybe_set_lhs (ptr_sval);
1143 }
1144 return true;
1145 }
1146 };
1147
1148 if (cd.get_ctxt ())
1149 {
1150 cd.get_ctxt ()->bifurcate (make_unique<pyobj_init_fail> (cd));
1151 cd.get_ctxt ()->bifurcate (make_unique<success> (cd));
1152 cd.get_ctxt ()->terminate_path ();
1153 }
1154 }
1155
1156 static void
1157 maybe_stash_named_type (logger *logger, const translation_unit &tu,
1158 const char *name)
1159 {
1160 LOG_FUNC_1 (logger, "name: %qs", name);
1161 if (!analyzer_stashed_types)
1162 analyzer_stashed_types = hash_map<tree, tree>::create_ggc ();
1163
1164 tree id = get_identifier (name);
1165 if (tree t = tu.lookup_type_by_id (id))
1166 {
1167 gcc_assert (TREE_CODE (t) == RECORD_TYPE);
1168 analyzer_stashed_types->put (id, t);
1169 if (logger)
1170 logger->log ("found %qs: %qE", name, t);
1171 }
1172 else
1173 {
1174 if (logger)
1175 logger->log ("%qs: not found", name);
1176 }
1177 }
1178
1179 static void
1180 maybe_stash_global_var (logger *logger, const translation_unit &tu,
1181 const char *name)
1182 {
1183 LOG_FUNC_1 (logger, "name: %qs", name);
1184 if (!analyzer_stashed_globals)
1185 analyzer_stashed_globals = hash_map<tree, tree>::create_ggc ();
1186
1187 tree id = get_identifier (name);
1188 if (tree t = tu.lookup_global_var_by_id (id))
1189 {
1190 gcc_assert (TREE_CODE (t) == VAR_DECL);
1191 analyzer_stashed_globals->put (id, t);
1192 if (logger)
1193 logger->log ("found %qs: %qE", name, t);
1194 }
1195 else
1196 {
1197 if (logger)
1198 logger->log ("%qs: not found", name);
1199 }
1200 }
1201
1202 static void
1203 stash_named_types (logger *logger, const translation_unit &tu)
1204 {
1205 LOG_SCOPE (logger);
1206
1207 maybe_stash_named_type (logger, tu, "PyObject");
1208 maybe_stash_named_type (logger, tu, "PyListObject");
1209 maybe_stash_named_type (logger, tu, "PyVarObject");
1210 maybe_stash_named_type (logger, tu, "PyLongObject");
1211 }
1212
1213 static void
1214 stash_global_vars (logger *logger, const translation_unit &tu)
1215 {
1216 LOG_SCOPE (logger);
1217
1218 maybe_stash_global_var (logger, tu, "PyLong_Type");
1219 maybe_stash_global_var (logger, tu, "PyList_Type");
1220 }
1221
1222 static tree
1223 get_stashed_type_by_name (const char *name)
1224 {
1225 if (!analyzer_stashed_types)
1226 return NULL_TREE;
1227 tree id = get_identifier (name);
1228 if (tree *slot = analyzer_stashed_types->get (id))
1229 {
1230 gcc_assert (TREE_CODE (*slot) == RECORD_TYPE);
1231 return *slot;
1232 }
1233 return NULL_TREE;
1234 }
1235
1236 static tree
1237 get_stashed_global_var_by_name (const char *name)
1238 {
1239 if (!analyzer_stashed_globals)
1240 return NULL_TREE;
1241 tree id = get_identifier (name);
1242 if (tree *slot = analyzer_stashed_globals->get (id))
1243 {
1244 gcc_assert (TREE_CODE (*slot) == VAR_DECL);
1245 return *slot;
1246 }
1247 return NULL_TREE;
1248 }
1249
1250 static void
1251 init_py_structs ()
1252 {
1253 pyobj_record = get_stashed_type_by_name ("PyObject");
1254 varobj_record = get_stashed_type_by_name ("PyVarObject");
1255 pylistobj_record = get_stashed_type_by_name ("PyListObject");
1256 pylongobj_record = get_stashed_type_by_name ("PyLongObject");
1257 pylongtype_vardecl = get_stashed_global_var_by_name ("PyLong_Type");
1258 pylisttype_vardecl = get_stashed_global_var_by_name ("PyList_Type");
1259
1260 if (pyobj_record)
1261 {
1262 pyobj_ptr_tree = build_pointer_type (pyobj_record);
1263 pyobj_ptr_ptr = build_pointer_type (pyobj_ptr_tree);
1264 }
1265 }
1266
1267 void
1268 sorry_no_cpython_plugin ()
1269 {
1270 sorry ("%qs definitions not found."
1271 " Please ensure to %qs.)",
1272 "Python/C API", "#include <Python.h>");
1273 }
1274
1275 static void
1276 cpython_analyzer_init_cb (void *gcc_data, void * /*user_data */)
1277 {
1278 ana::plugin_analyzer_init_iface *iface
1279 = (ana::plugin_analyzer_init_iface *)gcc_data;
1280 LOG_SCOPE (iface->get_logger ());
1281 if (0)
1282 inform (input_location, "got here: cpython_analyzer_init_cb");
1283
1284 init_py_structs ();
1285
1286 if (pyobj_record == NULL_TREE)
1287 {
1288 sorry_no_cpython_plugin ();
1289 return;
1290 }
1291
1292 iface->register_known_function ("PyList_Append",
1293 make_unique<kf_PyList_Append> ());
1294 iface->register_known_function ("PyList_New", make_unique<kf_PyList_New> ());
1295 iface->register_known_function ("PyLong_FromLong",
1296 make_unique<kf_PyLong_FromLong> ());
1297
1298 iface->register_known_function (
1299 "__analyzer_cpython_dump_refcounts",
1300 make_unique<kf_analyzer_cpython_dump_refcounts> ());
1301 }
1302 } // namespace ana
1303
1304 #endif /* #if ENABLE_ANALYZER */
1305
1306 int
1307 plugin_init (struct plugin_name_args *plugin_info,
1308 struct plugin_gcc_version *version)
1309 {
1310 #if ENABLE_ANALYZER
1311 const char *plugin_name = plugin_info->base_name;
1312 if (0)
1313 inform (input_location, "got here; %qs", plugin_name);
1314 register_finish_translation_unit_callback (&stash_named_types);
1315 register_finish_translation_unit_callback (&stash_global_vars);
1316 region_model::register_pop_frame_callback(pyobj_refcnt_checker);
1317 register_callback (plugin_info->base_name, PLUGIN_ANALYZER_INIT,
1318 ana::cpython_analyzer_init_cb,
1319 NULL); /* void *user_data */
1320 #else
1321 sorry_no_analyzer ();
1322 #endif
1323 return 0;
1324 }