Sophie

Sophie

distrib > Fedora > 17 > i386 > by-pkgid > 22d19aa2887b575b22775df3fe562f02 > files > 10

gcc-python-plugin-docs-0.9-4.1.fc17.i686.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>0.7 &mdash; gcc-python-plugin 0.9 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.9',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="gcc-python-plugin 0.9 documentation" href="index.html" />
    <link rel="up" title="Release Notes" href="release-notes.html" />
    <link rel="next" title="Appendices" href="appendices.html" />
    <link rel="prev" title="0.8" href="0.8.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="appendices.html" title="Appendices"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="0.8.html" title="0.8"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">gcc-python-plugin 0.9 documentation</a> &raquo;</li>
          <li><a href="release-notes.html" accesskey="U">Release Notes</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="id1">
<h1>0.7<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h1>
<p>This is a major update to the GCC Python plugin.</p>
<p>The main example script, cpychecker, has seen numerous improvements, and has
now detected many reference-counting bugs in real-world CPython extension code.
The usability and signal:noise ratio is greatly improved over previous releases.</p>
<div class="section" id="changes-to-the-gcc-python-plugin">
<h2>Changes to the GCC Python Plugin<a class="headerlink" href="#changes-to-the-gcc-python-plugin" title="Permalink to this headline">¶</a></h2>
<p>It&#8217;s now possible to create custom GCC attributes from Python, allowing you to
add custom high-level annotation to a C API, and to write scripts that will
verify these properties.  It&#8217;s also possible to inject preprocessor macros
from Python.  Taken together, this allows code like this:</p>
<blockquote>
<div><div class="highlight-c"><div class="highlight"><pre><span class="cp">#if defined(WITH_ATTRIBUTE_CLAIMS_MUTEX)</span>
<span class="cp"> #define CLAIMS_MUTEX(x) __attribute__((claims_mutex(x)))</span>
<span class="cp">#else</span>
<span class="cp"> #define CLAIMS_MUTEX(x)</span>
<span class="cp">#endif</span>

<span class="cp">#if defined(WITH_ATTRIBUTE_RELEASES_MUTEX)</span>
<span class="cp"> #define RELEASES_MUTEX(x) __attribute__((releases_mutex(x)))</span>
<span class="cp">#else</span>
<span class="cp"> #define RELEASES_MUTEX(x)</span>
<span class="cp">#endif</span>


<span class="cm">/* Function declarations with custom attributes: */</span>
<span class="k">extern</span> <span class="kt">void</span> <span class="n">some_function</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
    <span class="n">CLAIMS_MUTEX</span><span class="p">(</span><span class="s">&quot;io&quot;</span><span class="p">);</span>

<span class="k">extern</span> <span class="kt">void</span> <span class="n">some_other_function</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
    <span class="n">RELEASES_MUTEX</span><span class="p">(</span><span class="s">&quot;io&quot;</span><span class="p">);</span>

<span class="k">extern</span> <span class="kt">void</span> <span class="n">yet_another_function</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
    <span class="n">CLAIMS_MUTEX</span><span class="p">(</span><span class="s">&quot;db&quot;</span><span class="p">)</span>
    <span class="n">CLAIMS_MUTEX</span><span class="p">(</span><span class="s">&quot;io&quot;</span><span class="p">)</span>
    <span class="n">RELEASES_MUTEX</span><span class="p">(</span><span class="s">&quot;io&quot;</span><span class="p">);</span>
</pre></div>
</div>
</div></blockquote>
<p>Other improvements:</p>
<blockquote>
<div><ul class="simple">
<li>gcc&#8217;s debug dump facilities are now exposed via a Python API</li>
<li>it&#8217;s now possible to run Python commands in GCC (rather than scripts) using
-fplugin-arg-python-command</li>
<li>improvements to the &#8220;source location&#8221; when reporting on an unhandled
Python exception.  Amongst other tweaks, it&#8217;s now possible for a script to
override this, which the cpychecker uses, so that if it can&#8217;t handle a
particular line of C code, the GCC error report gives that location before
reporting the Python traceback (making debugging much easier).</li>
<li>&#8220;switch&#8221; statements are now properly wrapped at the Python level
(gcc.GimpleSwitch)</li>
<li>C bitfields are now wrapped at the Python level</li>
<li>gcc.Type instances now have a &#8220;sizeof&#8221; attribute, and an &#8220;attributes&#8221;
attribute.</li>
<li>added a gcc.Gimple.walk_tree method, to make it easy to visit all nodes
relating to a statement</li>
<li>added a new example: spell-checking all string literals in code</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="improvements-to-cpychecker">
<h2>Improvements to &#8220;cpychecker&#8221;<a class="headerlink" href="#improvements-to-cpychecker" title="Permalink to this headline">¶</a></h2>
<p>The &#8220;libcpychecker&#8221; Python code is a large example of using the plugin: it
extends GCC with code that tries to detect various bugs in CPython extension
modules.</p>
<p>The cpychecker analyzes the paths that can be followed through a C function,
and verifies various properties, including reference-count handling.</p>
<p>As of this release, the pass has found many reference-counting bugs in
real-world code.  You can see a list of the bugs that it has detected at:</p>
<p><a class="reference external" href="http://gcc-python-plugin.readthedocs.org/en/latest/success.html">http://gcc-python-plugin.readthedocs.org/en/latest/success.html</a></p>
<p>The checker is now <em>almost</em> capable of fully handling the C code within the
gcc python plugin itself.</p>
<p>The checker has also been reorganized to (I hope) make it easy to add checking
for other libraries and APIs.</p>
<div class="section" id="major-rewrite-of-reference-count-tracking">
<h3>Major rewrite of reference-count tracking<a class="headerlink" href="#major-rewrite-of-reference-count-tracking" title="Permalink to this headline">¶</a></h3>
<p>I&#8217;ve rewritten the internals of how reference counts are tracked: the code now
makes a distinction betweeen all of the reference that can be analysed within a
single function, versus all of the other references that may exist in the rest
of the program.</p>
<p>This allows us to know for an object e.g. that the function doesn&#8217;t directly
own any references, but that the reference count is still &gt; 0 (a &#8220;borrowed
reference&#8221;), as compared to the case where the function owns a reference, but
we don&#8217;t know of any in the rest of the program (this is typical when receiving
a &#8220;new reference&#8221; e.g. from a function call to a constructor).</p>
<p>Within the reference-count checker, we now look for memory locations that
store references to objects.   If those locations not on the stack, then the
references they store are now assumed to legally count towards the ob_refcnt
that the function &#8220;owns&#8221;.  This is needed in order to correctly handle e.g.
the PyList_SET_ITEM() macro, which directly writes to the list&#8217;s ob_item field,
&#8220;stealing&#8221; a reference: we can detect these references, and count them towards
the ob_refcnt value.</p>
<p>The checker can now detect types that look like PyObject subclasses at the C
level (by looking at the top-most fields), and uses this information in various
places.</p>
<p>The checker now exposes custom GCC attributes allowing you to mark APIs that
have non-standard reference-handling behavior:</p>
<div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject</span> <span class="o">*</span><span class="n">foo</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
  <span class="n">CPYCHECKER_RETURNS_BORROWED_REF</span><span class="p">;</span>

<span class="k">extern</span> <span class="kt">void</span> <span class="n">bar</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">obj</span><span class="p">,</span> <span class="kt">int</span> <span class="n">j</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">other</span><span class="p">)</span>
  <span class="n">CPYCHECKER_STEALS_REFERENCE_TO_ARG</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
  <span class="n">CPYCHECKER_STEALS_REFERENCE_TO_ARG</span><span class="p">(</span><span class="mi">4</span><span class="p">);</span>
</pre></div>
</div>
<p>It also exposes an attribute allowing you to the run-time and compile-time
type information for a Python extension class:</p>
<div class="highlight-c"><div class="highlight"><pre><span class="cm">/* Define some PyObject subclass, as both a struct and a typedef */</span>
<span class="k">struct</span> <span class="n">OurObjectStruct</span> <span class="p">{</span>
    <span class="n">PyObject_HEAD</span>
    <span class="cm">/* other fields */</span>
<span class="p">};</span>
<span class="k">typedef</span> <span class="k">struct</span> <span class="n">OurObjectStruct</span> <span class="n">OurExtensionObject</span><span class="p">;</span>

<span class="cm">/*</span>
<span class="cm">  Declare the PyTypeObject, using the custom attribute to associate it with</span>
<span class="cm">  the typedef above:</span>
<span class="cm">*/</span>
<span class="k">extern</span> <span class="n">PyTypeObject</span> <span class="n">UserDefinedExtension_Type</span>
  <span class="n">CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF</span><span class="p">(</span><span class="s">&quot;OurExtensionObject&quot;</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="function-calls-with-null-pointer-arguments">
<h3>Function calls with NULL-pointer arguments<a class="headerlink" href="#function-calls-with-null-pointer-arguments" title="Permalink to this headline">¶</a></h3>
<p>The checker knows about various CPython API hooks that will crash on NULL
pointer arguments, and will emit warnings when it can determine a path through
the code that will lead to a definite call with a NULL value.</p>
</div>
<div class="section" id="dereferences-of-uninitialized-pointers">
<h3>Dereferences of uninitialized pointers<a class="headerlink" href="#dereferences-of-uninitialized-pointers" title="Permalink to this headline">¶</a></h3>
<p>The checker will now complain about paths through a function for which it can
prove that an uninitialized pointer will be dereferenced.</p>
</div>
<div class="section" id="error-reporting-improvements">
<h3>Error-reporting improvements<a class="headerlink" href="#error-reporting-improvements" title="Permalink to this headline">¶</a></h3>
<p>The error-reporting machinery can generate HTML reports: see e.g.:
<a class="reference external" href="http://readthedocs.org/docs/gcc-python-plugin/en/latest/cpychecker.html#reference-count-checking">http://readthedocs.org/docs/gcc-python-plugin/en/latest/cpychecker.html#reference-count-checking</a>
and <a class="reference external" href="http://dmalcolm.livejournal.com/6560.html">http://dmalcolm.livejournal.com/6560.html</a></p>
<p>The checker can now annotate its HTML (and textual) reports with information
showing how some pertinent aspect of the program&#8217;s state changes during a
particular path through a function.</p>
<p>For example, when reporting on reference-counting errors, the HTML report
showing the flow through the function will now display all changes to an object&#8217;s
ob_refcnt, together with all changes to what the value ought to be (e.g. due to
pointers being stored to persistent memory locations):</p>
<blockquote>
<div><div class="figure">
<img alt="screenshot of the HTML report" src="_images/sample-html-error-report.png" />
</div>
</div></blockquote>
<p>Similarly, when reporting on exception-handling errors, it now displays the
&#8220;history&#8221; of changes to the thread-local exception state.</p>
<p>There&#8217;s also a debug mode which dumps _everything_ that changes within the
report, which is helpful for debugging the checker itself.</p>
<p>The error report will attempt to use the most representative name for a leaked
object, using a variable name or a C expression fragment as appropriate.</p>
<p>The checker will attempt to combine duplicate error reports, so that it will
only emit one error for all of the various traces of execution that exhibit a
particular reference-counting bug.</p>
<p>Finally, when writing out an HTML report, the path to the HTML is now noted
within gcc&#8217;s regular stderr messages.</p>
</div>
<div class="section" id="signal-noise-ratio-improvements">
<h3>Signal:noise ratio improvements<a class="headerlink" href="#signal-noise-ratio-improvements" title="Permalink to this headline">¶</a></h3>
<p>To suppress various false-positives that I commonly ran into on real code, the
checker now makes certain assumptions:</p>
<blockquote>
<div><ul class="simple">
<li>When encountering an unknown function that returns a PyObject*, the
checker assumes that it will either return a new reference to a sane
object (with a sane ob_type), or return NULL and set the thread-local
exception state.</li>
<li>The checker assumes that a PyObject* argument to a function is non-NULL
and has a &gt;0 refcount, and has a sane ob_type (e.g. with a sane refcount
and tp_dealloc)</li>
<li>When dereferencing a pointer that it has no knowledge about (e.g. a
pointer field in a structure), the checker now assumes that it&#8217;s
non-NULL, unless it knows that NULL is a definite possibility i.e. it
optimistically assumes that you know what you&#8217;re doing (this could be
turned into a command-line option).  Note that for the cases where we
know that the pointer can _definitely_ be NULL, an error will still be
reported (e.g. when considering the various possible return values for a
function known to be able to return NULL).</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="coverage-of-the-cpython-api">
<h3>Coverage of the CPython API<a class="headerlink" href="#coverage-of-the-cpython-api" title="Permalink to this headline">¶</a></h3>
<p>I&#8217;ve gone through much of the CPython API, &#8220;teaching&#8221; the checker about the
reference-count semantics of each API call (and which calls will crash if fed a
NULL pointer).  This involves writing a simple fragment of Python code for
each function, which describes the various different affects that the call can
have on the internal state within the callee.</p>
<dl class="docutils">
<dt>This release adds support for calls to the following:</dt>
<dd><ul class="first last simple">
<li>_PyObject_New</li>
<li>Py_{Initialize|Finalize}</li>
<li>Py_InitModule4</li>
<li>PyArg_ParseTuple[AndKeywords], and the PY_SSIZE_T_CLEAN variants (only
partial coverage so far: &#8220;O&#8221;, &#8220;O!&#8221; should work though)</li>
<li>PyArg_UnpackTuple</li>
<li>PyBool_FromLong</li>
<li>Py_BuildValue and the PY_SSIZE_T_CLEAN variant (only partial coverage so
far)</li>
<li>PyDict_{GetItem,GetItemString,New,SetItem,SetItemString}</li>
<li>PyErr_{Format,NoMemory,Occurred,Print,PrintEx,SetFromErrno[WithFilename],
SetObject,SetString}</li>
<li>PyEval_InitThreads</li>
<li>PyGILState_{Ensure,Release}</li>
<li>PyImport_{AppendInittab,ImportModule}</li>
<li>PyInt_{AsLong,FromLong}</li>
<li>PyList_Append</li>
<li>PyLong_{FromString,FromVoidPtr}</li>
<li>PyMem_{Malloc,Free}</li>
<li>PyModule_Add{IntConstant,Object,StringConstant}</li>
<li>PyObject_{Call,CallMethod,HasAttrString,IsTrue,Repr,Str}</li>
<li>PyRun_{SimpleFileExFlags,SimpleStringFlags}</li>
<li>PySequence_GetItem</li>
<li>PyString_{AsString,FromFormat,FromString,FromStringAndSize}</li>
<li>PyStructSequence_{InitType,New}</li>
<li>PySys_SetObject</li>
<li>PyTuple_{New,SetItem,Size}</li>
<li>PyType_{IsSubtype,Ready}</li>
</ul>
</dd>
</dl>
<p>I&#8217;ve been targetting those API entrypoints that I use myself in the plugin;
this is one area which is particularly amenable to patching, for anyone who
wants to get involved.   I&#8217;ve also added a (disabled) hook that complains
about Python API entrypoints that weren&#8217;t explicitly handled, to make it easy
to find gaps in our coverage of the CPython API.</p>
</div>
<div class="section" id="other-user-visible-improvments">
<h3>Other user-visible improvments<a class="headerlink" href="#other-user-visible-improvments" title="Permalink to this headline">¶</a></h3>
<blockquote>
<div><ul class="simple">
<li>There&#8217;s now a &#8220;gcc-with-cpychecker&#8221; harness, to make it easier to invoke
GCC with the cpychecker code from e.g. Makefiles</li>
<li>The checker now respects <cite>__attribute((nonnull))</cite> on function arguments
when detecting NULL pointers</li>
<li>Handle functions that don&#8217;t return (e.g. &#8220;exit&#8221;)</li>
<li>Number the unknown heap regions, to clarify things when there&#8217;s more than
one</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="internal-improvements">
<h3>Internal improvements<a class="headerlink" href="#internal-improvements" title="Permalink to this headline">¶</a></h3>
<blockquote>
<div><ul class="simple">
<li>The cpychecker now has some protection against combinatorial explosion
for functions that have very large numbers of possible routes through
them.  For such functions, the checker will emit a note on stderr and
not attempt to find reference-counting bugs in the function.</li>
<li>The cpychecker is now done as a custom pass (rather than by wiring up a
callback associated with every pass)</li>
<li>I&#8217;ve tuned the logging within the checker, eliminating some CPU/memory
consumption issues seen when analysing complicated C code.  In particular,
the log message arguments are now only expanded when logging is enabled
(previously this was happening all the time).</li>
<li>Lots of other internal improvements and bug fixes (e.g. handling of arrays
vs pointers, static vs auto local variables, add missing handlers for
various kinds of C expression, lots of work on improving the readability of
error messages)</li>
</ul>
</div></blockquote>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">0.7</a><ul>
<li><a class="reference internal" href="#changes-to-the-gcc-python-plugin">Changes to the GCC Python Plugin</a></li>
<li><a class="reference internal" href="#improvements-to-cpychecker">Improvements to &#8220;cpychecker&#8221;</a><ul>
<li><a class="reference internal" href="#major-rewrite-of-reference-count-tracking">Major rewrite of reference-count tracking</a></li>
<li><a class="reference internal" href="#function-calls-with-null-pointer-arguments">Function calls with NULL-pointer arguments</a></li>
<li><a class="reference internal" href="#dereferences-of-uninitialized-pointers">Dereferences of uninitialized pointers</a></li>
<li><a class="reference internal" href="#error-reporting-improvements">Error-reporting improvements</a></li>
<li><a class="reference internal" href="#signal-noise-ratio-improvements">Signal:noise ratio improvements</a></li>
<li><a class="reference internal" href="#coverage-of-the-cpython-api">Coverage of the CPython API</a></li>
<li><a class="reference internal" href="#other-user-visible-improvments">Other user-visible improvments</a></li>
<li><a class="reference internal" href="#internal-improvements">Internal improvements</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="0.8.html"
                        title="previous chapter">0.8</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="appendices.html"
                        title="next chapter">Appendices</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/0.7.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="appendices.html" title="Appendices"
             >next</a> |</li>
        <li class="right" >
          <a href="0.8.html" title="0.8"
             >previous</a> |</li>
        <li><a href="index.html">gcc-python-plugin 0.9 documentation</a> &raquo;</li>
          <li><a href="release-notes.html" >Release Notes</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2011, David Malcolm.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>