8sa1-gcc/gcc/plugin.def
David Malcolm 66dde7bc64 Add analyzer plugin support and CPython GIL example
This patch adds a new GCC plugin event: PLUGIN_ANALYZER_INIT, called
when -fanalyzer is starting, allowing for GCC plugins to register
additional state-machine-based checks within -fanalyzer.  The idea
is that 3rd-party code might want to add domain-specific checks for
its own APIs - with the caveat that the analyzer is itself still
rather experimental.

As an example, the patch adds a proof-of-concept plugin to the testsuite
for checking CPython code: verifying that code that relinquishes
CPython's global interpreter lock doesn't attempt to do anything with
PyObjects in the sections where the lock isn't held.  It also adds a
warning about nested releases of the lock, which is forbidden.
For example:

demo.c: In function 'foo':
demo.c:11:3: warning: use of PyObject '*(obj)' without the GIL
   11 |   Py_INCREF (obj);
      |   ^~~~~~~~~
  'test': events 1-3
    |
    |   15 | void test (PyObject *obj)
    |      |      ^~~~
    |      |      |
    |      |      (1) entry to 'test'
    |   16 | {
    |   17 |   Py_BEGIN_ALLOW_THREADS
    |      |   ~~~~~~~~~~~~~~~~~~~~~~
    |      |   |
    |      |   (2) releasing the GIL here
    |   18 |   foo (obj);
    |      |   ~~~~~~~~~
    |      |   |
    |      |   (3) calling 'foo' from 'test'
    |
    +--> 'foo': events 4-5
           |
           |    9 | foo (PyObject *obj)
           |      | ^~~
           |      | |
           |      | (4) entry to 'foo'
           |   10 | {
           |   11 |   Py_INCREF (obj);
           |      |   ~~~~~~~~~
           |      |   |
           |      |   (5) PyObject '*(obj)' used here without the GIL
           |

Doing so requires adding some logic for ignoring macro expansions in
analyzer diagnostics, since the insides of Py_INCREF and
Py_BEGIN_ALLOW_THREADS are not of interest to the user for these cases.

gcc/analyzer/ChangeLog:
	* analyzer-pass.cc (pass_analyzer::execute): Move sorry call to...
	(sorry_no_analyzer): New.
	* analyzer.h (class state_machine): New forward decl.
	(class logger): New forward decl.
	(class plugin_analyzer_init_iface): New.
	(sorry_no_analyzer): New decl.
	* checker-path.cc (checker_path::fixup_locations): New.
	* checker-path.h (checker_event::set_location): New.
	(checker_path::fixup_locations): New decl.
	* diagnostic-manager.cc
	(diagnostic_manager::emit_saved_diagnostic): Call
	checker_path::fixup_locations, and call fixup_location
	on the primary location.
	* engine.cc: Include "plugin.h".
	(class plugin_analyzer_init_impl): New.
	(impl_run_checkers): Invoke PLUGIN_ANALYZER_INIT callbacks.
	* pending-diagnostic.h (pending_diagnostic::fixup_location): New
	vfunc.

gcc/ChangeLog:
	* doc/plugins.texi (Plugin callbacks): Add PLUGIN_ANALYZER_INIT.
	* plugin.c (register_callback): Likewise.
	(invoke_plugin_callbacks_full): Likewise.
	* plugin.def (PLUGIN_ANALYZER_INIT): New event.

gcc/testsuite/ChangeLog:
	* gcc.dg/plugin/analyzer_gil_plugin.c: New test.
	* gcc.dg/plugin/gil-1.c: New test.
	* gcc.dg/plugin/gil.h: New header.
	* gcc.dg/plugin/plugin.exp (plugin_test_list): Add the new plugin
	and test.
2020-11-30 14:32:28 -05:00

113 lines
3.3 KiB
Modula-2

/* This file contains the definitions for plugin events in GCC.
Copyright (C) 2009-2020 Free Software Foundation, Inc.
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/>. */
/* Called before parsing the body of a function. */
DEFEVENT (PLUGIN_START_PARSE_FUNCTION)
/* After finishing parsing a function. */
DEFEVENT (PLUGIN_FINISH_PARSE_FUNCTION)
/* To hook into pass manager. */
DEFEVENT (PLUGIN_PASS_MANAGER_SETUP)
/* After finishing parsing a type. */
DEFEVENT (PLUGIN_FINISH_TYPE)
/* After finishing parsing a declaration. */
DEFEVENT (PLUGIN_FINISH_DECL)
/* Useful for summary processing. */
DEFEVENT (PLUGIN_FINISH_UNIT)
/* Allows to see low level AST in C and C++ frontends. */
DEFEVENT (PLUGIN_PRE_GENERICIZE)
/* Called before GCC exits. */
DEFEVENT (PLUGIN_FINISH)
/* Information about the plugin. */
DEFEVENT (PLUGIN_INFO)
/* Called at start of GCC Garbage Collection. */
DEFEVENT (PLUGIN_GGC_START)
/* Extend the GGC marking. */
DEFEVENT (PLUGIN_GGC_MARKING)
/* Called at end of GGC. */
DEFEVENT (PLUGIN_GGC_END)
/* Register an extra GGC root table. */
DEFEVENT (PLUGIN_REGISTER_GGC_ROOTS)
/* Called during attribute registration. */
DEFEVENT (PLUGIN_ATTRIBUTES)
/* Called before processing a translation unit. */
DEFEVENT (PLUGIN_START_UNIT)
/* Called during pragma registration. */
DEFEVENT (PLUGIN_PRAGMAS)
/* Called before first pass from all_passes. */
DEFEVENT (PLUGIN_ALL_PASSES_START)
/* Called after last pass from all_passes. */
DEFEVENT (PLUGIN_ALL_PASSES_END)
/* Called before first ipa pass. */
DEFEVENT (PLUGIN_ALL_IPA_PASSES_START)
/* Called after last ipa pass. */
DEFEVENT (PLUGIN_ALL_IPA_PASSES_END)
/* Allows to override pass gate decision for current_pass. */
DEFEVENT (PLUGIN_OVERRIDE_GATE)
/* Called before executing a pass. */
DEFEVENT (PLUGIN_PASS_EXECUTION)
/* Called before executing subpasses of a GIMPLE_PASS in
execute_ipa_pass_list. */
DEFEVENT (PLUGIN_EARLY_GIMPLE_PASSES_START)
/* Called after executing subpasses of a GIMPLE_PASS in
execute_ipa_pass_list. */
DEFEVENT (PLUGIN_EARLY_GIMPLE_PASSES_END)
/* Called when a pass is first instantiated. */
DEFEVENT (PLUGIN_NEW_PASS)
/* Called when a file is #include-d or given via the #line directive.
this could happen many times. The event data is the included file path,
as a const char* pointer. */
DEFEVENT (PLUGIN_INCLUDE_FILE)
/* Called when -fanalyzer starts. The event data is an
ana::plugin_analyzer_init_iface *. */
DEFEVENT (PLUGIN_ANALYZER_INIT)
/* When adding a new hard-coded plugin event, don't forget to edit in
file plugin.c the functions register_callback and
invoke_plugin_callbacks_full accordingly! */
/* After the hard-coded events above, plugins can dynamically allocate events
at run time.
PLUGIN_EVENT_FIRST_DYNAMIC only appears as last enum element. */