0.7 — gcc-python-plugin 0.16 documentation (original) (raw)

gcc-python-plugin

This is a major update to the GCC Python plugin.

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.

Changes to the GCC Python Plugin

It’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’s also possible to inject preprocessor macros from Python. Taken together, this allows code like this:

#if defined(WITH_ATTRIBUTE_CLAIMS_MUTEX) #define CLAIMS_MUTEX(x) attribute((claims_mutex(x))) #else #define CLAIMS_MUTEX(x) #endif

#if defined(WITH_ATTRIBUTE_RELEASES_MUTEX) #define RELEASES_MUTEX(x) attribute((releases_mutex(x))) #else #define RELEASES_MUTEX(x) #endif

/* Function declarations with custom attributes: */ extern void some_function(void) CLAIMS_MUTEX("io");

extern void some_other_function(void) RELEASES_MUTEX("io");

extern void yet_another_function(void) CLAIMS_MUTEX("db") CLAIMS_MUTEX("io") RELEASES_MUTEX("io");

Other improvements:

Improvements to “cpychecker”

The “libcpychecker” 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.

The cpychecker analyzes the paths that can be followed through a C function, and verifies various properties, including reference-count handling.

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:

http://gcc-python-plugin.readthedocs.org/en/latest/success.html

The checker is now almost capable of fully handling the C code within the gcc python plugin itself.

The checker has also been reorganized to (I hope) make it easy to add checking for other libraries and APIs.

Major rewrite of reference-count tracking

I’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.

This allows us to know for an object e.g. that the function doesn’t directly own any references, but that the reference count is still > 0 (a “borrowed reference”), as compared to the case where the function owns a reference, but we don’t know of any in the rest of the program (this is typical when receiving a “new reference” e.g. from a function call to a constructor).

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 “owns”. This is needed in order to correctly handle e.g. the PyList_SET_ITEM() macro, which directly writes to the list’s ob_item field, “stealing” a reference: we can detect these references, and count them towards the ob_refcnt value.

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.

The checker now exposes custom GCC attributes allowing you to mark APIs that have non-standard reference-handling behavior:

PyObject *foo(void) CPYCHECKER_RETURNS_BORROWED_REF;

extern void bar(int i, PyObject *obj, int j, PyObject *other) CPYCHECKER_STEALS_REFERENCE_TO_ARG(2) CPYCHECKER_STEALS_REFERENCE_TO_ARG(4);

It also exposes an attribute allowing you to the run-time and compile-time type information for a Python extension class:

/* Define some PyObject subclass, as both a struct and a typedef / struct OurObjectStruct { PyObject_HEAD / other fields */ }; typedef struct OurObjectStruct OurExtensionObject;

/* Declare the PyTypeObject, using the custom attribute to associate it with the typedef above: */ extern PyTypeObject UserDefinedExtension_Type CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF("OurExtensionObject");

Function calls with NULL-pointer arguments

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.

Dereferences of uninitialized pointers

The checker will now complain about paths through a function for which it can prove that an uninitialized pointer will be dereferenced.

Error-reporting improvements

The error-reporting machinery can generate HTML reports: see e.g.:http://readthedocs.org/docs/gcc-python-plugin/en/latest/cpychecker.html#reference-count-checkingand http://dmalcolm.livejournal.com/6560.html

The checker can now annotate its HTML (and textual) reports with information showing how some pertinent aspect of the program’s state changes during a particular path through a function.

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’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):

screenshot of the HTML report

Similarly, when reporting on exception-handling errors, it now displays the “history” of changes to the thread-local exception state.

There’s also a debug mode which dumps _everything_ that changes within the report, which is helpful for debugging the checker itself.

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.

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.

Finally, when writing out an HTML report, the path to the HTML is now noted within gcc’s regular stderr messages.

Signal:noise ratio improvements

To suppress various false-positives that I commonly ran into on real code, the checker now makes certain assumptions:

Coverage of the CPython API

I’ve gone through much of the CPython API, “teaching” 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.

This release adds support for calls to the following:

I’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’ve also added a (disabled) hook that complains about Python API entrypoints that weren’t explicitly handled, to make it easy to find gaps in our coverage of the CPython API.

Other user-visible improvments

Internal improvements