Debian Codegraph @

gcc/testsuite/gcc.dg/plugin/analyzer_gil_plugin.cc

raw
1 /* Proof-of-concept of a -fanalyzer plugin.
2 Detect (some) uses of CPython API outside of the Global Interpreter Lock.
3 https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock
4 */
5 /* { dg-options "-g" } */
6
7 #define INCLUDE_MEMORY
8 #define INCLUDE_VECTOR
9 #include "gcc-plugin.h"
10 #include "config.h"
11 #include "system.h"
12 #include "coretypes.h"
13 #include "make-unique.h"
14 #include "diagnostic.h"
15 #include "tree.h"
16 #include "gimple.h"
17 #include "gimple-iterator.h"
18 #include "gimple-walk.h"
19 #include "diagnostic-event-id.h"
20 #include "analyzer/analyzer.h"
21 #include "analyzer/analyzer-logging.h"
22 #include "json.h"
23 #include "analyzer/sm.h"
24 #include "analyzer/pending-diagnostic.h"
25
26 int plugin_is_GPL_compatible;
27
28 #if ENABLE_ANALYZER
29
30 namespace ana {
31
32 static bool
33 type_based_on_pyobject_p (tree type)
34 {
35 /* Ideally we'd also check for "subclasses" here by iterating up the
36 first field of each struct. */
37 if (TREE_CODE (type) != RECORD_TYPE)
38 return false;
39 tree name = TYPE_IDENTIFIER (type);
40 if (!name)
41 return false;
42 return id_equal (name, "PyObject");
43 }
44
45 /* An experimental state machine, for tracking whether the GIL is held,
46 as global state.. */
47
48 class gil_state_machine : public state_machine
49 {
50 public:
51 gil_state_machine (logger *logger);
52
53 bool inherited_state_p () const final override { return false; }
54
55 bool on_stmt (sm_context &sm_ctxt,
56 const supernode *node,
57 const gimple *stmt) const final override;
58
59 bool can_purge_p (state_t s) const final override;
60
61 void check_for_pyobject_usage_without_gil (sm_context &sm_ctxt,
62 const supernode *node,
63 const gimple *stmt,
64 tree op) const;
65
66 private:
67 void check_for_pyobject_in_call (sm_context &sm_ctxt,
68 const supernode *node,
69 const gcall *call,
70 tree callee_fndecl) const;
71
72 public:
73 /* These states are "global", rather than per-expression. */
74
75 /* State for when we've released the GIL. */
76 state_t m_released_gil;
77
78 /* Stop state. */
79 state_t m_stop;
80 };
81
82 /* Subclass for diagnostics involving the GIL. */
83
84 class gil_diagnostic : public pending_diagnostic
85 {
86 public:
87 /* There isn't a warning ID for us to use. */
88 int get_controlling_option () const final override
89 {
90 return 0;
91 }
92
93 location_t fixup_location (location_t loc,
94 bool) const final override
95 {
96 /* Ideally we'd check for specific macros here, and only
97 resolve certain macros. */
98 if (linemap_location_from_macro_expansion_p (line_table, loc))
99 loc = linemap_resolve_location (line_table, loc,
100 LRK_MACRO_EXPANSION_POINT, NULL);
101 return loc;
102 }
103
104 bool
105 describe_state_change (pretty_printer &pp,
106 const evdesc::state_change &change) final override
107 {
108 if (change.is_global_p ()
109 && change.m_new_state == m_sm.m_released_gil)
110 {
111 pp_string (&pp, "releasing the GIL here");
112 return true;
113 }
114 if (change.is_global_p ()
115 && change.m_new_state == m_sm.get_start_state ())
116 {
117 pp_string (&pp, "acquiring the GIL here");
118 return true;
119 }
120 return false;
121 }
122
123 diagnostic_event::meaning
124 get_meaning_for_state_change (const evdesc::state_change &change)
125 const final override
126 {
127 if (change.is_global_p ())
128 {
129 if (change.m_new_state == m_sm.m_released_gil)
130 return diagnostic_event::meaning (diagnostic_event::VERB_release,
131 diagnostic_event::NOUN_lock);
132 else if (change.m_new_state == m_sm.get_start_state ())
133 return diagnostic_event::meaning (diagnostic_event::VERB_acquire,
134 diagnostic_event::NOUN_lock);
135 }
136 return diagnostic_event::meaning ();
137 }
138 protected:
139 gil_diagnostic (const gil_state_machine &sm) : m_sm (sm)
140 {
141 }
142
143 private:
144 const gil_state_machine &m_sm;
145 };
146
147 class double_save_thread : public gil_diagnostic
148 {
149 public:
150 double_save_thread (const gil_state_machine &sm, const gcall *call)
151 : gil_diagnostic (sm), m_call (call)
152 {}
153
154 const char *get_kind () const final override
155 {
156 return "double_save_thread";
157 }
158
159 bool subclass_equal_p (const pending_diagnostic &base_other) const override
160 {
161 const double_save_thread &sub_other
162 = (const double_save_thread &)base_other;
163 return m_call == sub_other.m_call;
164 }
165
166 bool emit (diagnostic_emission_context &ctxt) final override
167 {
168 return ctxt.warn ("nested usage of %qs", "Py_BEGIN_ALLOW_THREADS");
169 }
170
171 bool
172 describe_final_event (pretty_printer &pp,
173 const evdesc::final_event &ev) final override
174 {
175 pp_printf (&pp,
176 "nested usage of %qs here",
177 "Py_BEGIN_ALLOW_THREADS");
178 return true;
179 }
180
181 private:
182 const gcall *m_call;
183 };
184
185 class fncall_without_gil : public gil_diagnostic
186 {
187 public:
188 fncall_without_gil (const gil_state_machine &sm, const gcall *call,
189 tree callee_fndecl, unsigned arg_idx)
190 : gil_diagnostic (sm), m_call (call), m_callee_fndecl (callee_fndecl),
191 m_arg_idx (arg_idx)
192 {}
193
194 const char *get_kind () const final override
195 {
196 return "fncall_without_gil";
197 }
198
199 bool subclass_equal_p (const pending_diagnostic &base_other) const override
200 {
201 const fncall_without_gil &sub_other
202 = (const fncall_without_gil &)base_other;
203 return (m_call == sub_other.m_call
204 && m_callee_fndecl == sub_other.m_callee_fndecl
205 && m_arg_idx == sub_other.m_arg_idx);
206 }
207
208 bool emit (diagnostic_emission_context &ctxt) final override
209 {
210 if (m_callee_fndecl)
211 return ctxt.warn ("use of PyObject as argument %i of %qE"
212 " without the GIL",
213 m_arg_idx + 1, m_callee_fndecl);
214 else
215 return ctxt.warn ("use of PyObject as argument %i of call"
216 " without the GIL",
217 m_arg_idx + 1, m_callee_fndecl);
218 }
219
220 bool
221 describe_final_event (pretty_printer &pp,
222 const evdesc::final_event &ev) final override
223 {
224 if (m_callee_fndecl)
225 pp_printf (&pp,
226 "use of PyObject as argument %i of %qE here without the GIL",
227 m_arg_idx + 1, m_callee_fndecl);
228 else
229 pp_printf (&pp,
230 "use of PyObject as argument %i of call here without the GIL",
231 m_arg_idx + 1, m_callee_fndecl);
232 return true;
233 }
234
235 private:
236 const gcall *m_call;
237 tree m_callee_fndecl;
238 unsigned m_arg_idx;
239 };
240
241 class pyobject_usage_without_gil : public gil_diagnostic
242 {
243 public:
244 pyobject_usage_without_gil (const gil_state_machine &sm, tree expr)
245 : gil_diagnostic (sm), m_expr (expr)
246 {}
247
248 const char *get_kind () const final override
249 {
250 return "pyobject_usage_without_gil";
251 }
252
253 bool subclass_equal_p (const pending_diagnostic &base_other) const override
254 {
255 return same_tree_p (m_expr,
256 ((const pyobject_usage_without_gil&)base_other).m_expr);
257 }
258
259 bool emit (diagnostic_emission_context &ctxt) final override
260 {
261 return ctxt.warn ("use of PyObject %qE without the GIL", m_expr);
262 }
263
264 bool
265 describe_final_event (pretty_printer &pp,
266 const evdesc::final_event &ev) final override
267 {
268 pp_printf (&pp,
269 "PyObject %qE used here without the GIL",
270 m_expr);
271 return true;
272 }
273
274 private:
275 tree m_expr;
276 };
277
278 /* gil_state_machine's ctor. */
279
280 gil_state_machine::gil_state_machine (logger *logger)
281 : state_machine ("gil", logger)
282 {
283 m_released_gil = add_state ("released_gil");
284 m_stop = add_state ("stop");
285 }
286
287 struct cb_data
288 {
289 cb_data (const gil_state_machine &sm, sm_context &sm_ctxt,
290 const supernode *snode, const gimple *stmt)
291 : m_sm (sm), m_sm_ctxt (sm_ctxt), m_snode (snode), m_stmt (stmt)
292 {
293 }
294
295 const gil_state_machine &m_sm;
296 sm_context &m_sm_ctxt;
297 const supernode *m_snode;
298 const gimple *m_stmt;
299 };
300
301 static bool
302 check_for_pyobject (gimple *, tree op, tree, void *data)
303 {
304 cb_data *d = (cb_data *)data;
305 d->m_sm.check_for_pyobject_usage_without_gil (d->m_sm_ctxt, d->m_snode,
306 d->m_stmt, op);
307 return true;
308 }
309
310 /* Assuming that the GIL has been released, complain about any
311 PyObject * arguments passed to CALL. */
312
313 void
314 gil_state_machine::check_for_pyobject_in_call (sm_context &sm_ctxt,
315 const supernode *node,
316 const gcall *call,
317 tree callee_fndecl) const
318 {
319 for (unsigned i = 0; i < gimple_call_num_args (call); i++)
320 {
321 tree arg = gimple_call_arg (call, i);
322 if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
323 continue;
324 tree type = TREE_TYPE (TREE_TYPE (arg));
325 if (type_based_on_pyobject_p (type))
326 {
327 sm_ctxt.warn (node, call, NULL_TREE,
328 make_unique<fncall_without_gil> (*this, call,
329 callee_fndecl,
330 i));
331 sm_ctxt.set_global_state (m_stop);
332 }
333 }
334 }
335
336 /* Implementation of state_machine::on_stmt vfunc for gil_state_machine. */
337
338 bool
339 gil_state_machine::on_stmt (sm_context &sm_ctxt,
340 const supernode *node,
341 const gimple *stmt) const
342 {
343 const state_t global_state = sm_ctxt.get_global_state ();
344 if (const gcall *call = dyn_cast <const gcall *> (stmt))
345 {
346 if (tree callee_fndecl = sm_ctxt.get_fndecl_for_call (call))
347 {
348 if (is_named_call_p (callee_fndecl, "PyEval_SaveThread", call, 0))
349 {
350 if (0)
351 inform (input_location, "found call to %qs",
352 "PyEval_SaveThread");
353 if (global_state == m_released_gil)
354 {
355 sm_ctxt.warn (node, stmt, NULL_TREE,
356 make_unique<double_save_thread> (*this, call));
357 sm_ctxt.set_global_state (m_stop);
358 }
359 else
360 sm_ctxt.set_global_state (m_released_gil);
361 return true;
362 }
363 else if (is_named_call_p (callee_fndecl, "PyEval_RestoreThread",
364 call, 1))
365 {
366 if (0)
367 inform (input_location, "found call to %qs",
368 "PyEval_SaveThread");
369 if (global_state == m_released_gil)
370 sm_ctxt.set_global_state (m_start);
371 return true;
372 }
373 else if (global_state == m_released_gil)
374 {
375 /* Find PyObject * args of calls to fns with unknown bodies. */
376 if (!fndecl_has_gimple_body_p (callee_fndecl))
377 check_for_pyobject_in_call (sm_ctxt, node, call, callee_fndecl);
378 }
379 }
380 else if (global_state == m_released_gil)
381 check_for_pyobject_in_call (sm_ctxt, node, call, NULL);
382 }
383 else
384 if (global_state == m_released_gil)
385 {
386 /* Walk the stmt, finding uses of PyObject (or "subclasses"). */
387 cb_data d (*this, sm_ctxt, node, stmt);
388 walk_stmt_load_store_addr_ops (const_cast <gimple *> (stmt), &d,
389 check_for_pyobject,
390 check_for_pyobject,
391 check_for_pyobject);
392 }
393 return false;
394 }
395
396 bool
397 gil_state_machine::can_purge_p (state_t s ATTRIBUTE_UNUSED) const
398 {
399 return true;
400 }
401
402 void
403 gil_state_machine::check_for_pyobject_usage_without_gil (sm_context &sm_ctxt,
404 const supernode *node,
405 const gimple *stmt,
406 tree op) const
407 {
408 tree type = TREE_TYPE (op);
409 if (type_based_on_pyobject_p (type))
410 {
411 sm_ctxt.warn (node, stmt, NULL_TREE,
412 make_unique<pyobject_usage_without_gil> (*this, op));
413 sm_ctxt.set_global_state (m_stop);
414 }
415 }
416
417 /* Callback handler for the PLUGIN_ANALYZER_INIT event. */
418
419 static void
420 gil_analyzer_init_cb (void *gcc_data, void */*user_data*/)
421 {
422 ana::plugin_analyzer_init_iface *iface
423 = (ana::plugin_analyzer_init_iface *)gcc_data;
424 LOG_SCOPE (iface->get_logger ());
425 if (0)
426 inform (input_location, "got here: gil_analyzer_init_cb");
427 iface->register_state_machine
428 (make_unique<gil_state_machine> (iface->get_logger ()));
429 }
430
431 } // namespace ana
432
433 #endif /* #if ENABLE_ANALYZER */
434
435 int
436 plugin_init (struct plugin_name_args *plugin_info,
437 struct plugin_gcc_version *version)
438 {
439 #if ENABLE_ANALYZER
440 const char *plugin_name = plugin_info->base_name;
441 if (0)
442 inform (input_location, "got here; %qs", plugin_name);
443 register_callback (plugin_info->base_name,
444 PLUGIN_ANALYZER_INIT,
445 ana::gil_analyzer_init_cb,
446 NULL); /* void *user_data */
447 #else
448 sorry_no_analyzer ();
449 #endif
450 return 0;
451 }