Blame SOURCES/00170-gc-assertions.patch

c8ca20
diff -up Python-2.7.3/Lib/test/test_gc.py.gc-assertions Python-2.7.3/Lib/test/test_gc.py
c8ca20
--- Python-2.7.3/Lib/test/test_gc.py.gc-assertions	2013-02-20 16:28:20.890536607 -0500
c8ca20
+++ Python-2.7.3/Lib/test/test_gc.py	2013-02-20 16:39:52.720489297 -0500
c8ca20
@@ -1,6 +1,7 @@
c8ca20
 import unittest
c8ca20
-from test.test_support import verbose, run_unittest
c8ca20
+from test.test_support import verbose, run_unittest, import_module
c8ca20
 import sys
c8ca20
+import sysconfig
c8ca20
 import time
c8ca20
 import gc
c8ca20
 import weakref
c8ca20
@@ -32,6 +33,8 @@ class GC_Detector(object):
c8ca20
         self.wr = weakref.ref(C1055820(666), it_happened)
c8ca20
 
c8ca20
 
c8ca20
+BUILT_WITH_NDEBUG = ('-DNDEBUG' in sysconfig.get_config_vars()['PY_CFLAGS'])
c8ca20
+
c8ca20
 ### Tests
c8ca20
 ###############################################################################
c8ca20
 
c8ca20
@@ -476,6 +479,49 @@ class GCTests(unittest.TestCase):
c8ca20
             # would be damaged, with an empty __dict__.
c8ca20
             self.assertEqual(x, None)
c8ca20
 
c8ca20
+    @unittest.skipIf(BUILT_WITH_NDEBUG,
c8ca20
+                     'built with -NDEBUG')
c8ca20
+    def test_refcount_errors(self):
c8ca20
+        # Verify the "handling" of objects with broken refcounts
c8ca20
+ 
c8ca20
+        import_module("ctypes") #skip if not supported
c8ca20
+
c8ca20
+        import subprocess
c8ca20
+        code = '''if 1:
c8ca20
+        a = []
c8ca20
+        b = [a]
c8ca20
+
c8ca20
+        # Simulate the refcount of "a" being too low (compared to the
c8ca20
+        # references held on it by live data), but keeping it above zero
c8ca20
+        # (to avoid deallocating it):
c8ca20
+        import ctypes
c8ca20
+        ctypes.pythonapi.Py_DecRef(ctypes.py_object(a))
c8ca20
+
c8ca20
+        # The garbage collector should now have a fatal error when it reaches
c8ca20
+        # the broken object:
c8ca20
+        import gc
c8ca20
+        gc.collect()
c8ca20
+        '''
c8ca20
+        p = subprocess.Popen([sys.executable, "-c", code],
c8ca20
+                             stdout=subprocess.PIPE,
c8ca20
+                             stderr=subprocess.PIPE)
c8ca20
+        stdout, stderr = p.communicate()
c8ca20
+        p.stdout.close()
c8ca20
+        p.stderr.close()
c8ca20
+        # Verify that stderr has a useful error message:
c8ca20
+        self.assertRegexpMatches(stderr,
c8ca20
+            b'Modules/gcmodule.c:[0-9]+: visit_decref: Assertion "gc->gc.gc_refs != 0" failed.')
c8ca20
+        self.assertRegexpMatches(stderr,
c8ca20
+            b'refcount was too small')
c8ca20
+        self.assertRegexpMatches(stderr,
c8ca20
+            b'object  : \[\]')
c8ca20
+        self.assertRegexpMatches(stderr,
c8ca20
+            b'type    : list')
c8ca20
+        self.assertRegexpMatches(stderr,
c8ca20
+            b'refcount: 1')
c8ca20
+        self.assertRegexpMatches(stderr,
c8ca20
+            b'address : 0x[0-9a-f]+')
c8ca20
+
c8ca20
 class GCTogglingTests(unittest.TestCase):
c8ca20
     def setUp(self):
c8ca20
         gc.enable()
c8ca20
diff -up Python-2.7.3/Modules/gcmodule.c.gc-assertions Python-2.7.3/Modules/gcmodule.c
c8ca20
--- Python-2.7.3/Modules/gcmodule.c.gc-assertions	2012-04-09 19:07:34.000000000 -0400
c8ca20
+++ Python-2.7.3/Modules/gcmodule.c	2013-02-20 16:28:21.029536600 -0500
c8ca20
@@ -21,6 +21,73 @@
c8ca20
 #include "Python.h"
c8ca20
 #include "frameobject.h"        /* for PyFrame_ClearFreeList */
c8ca20
 
c8ca20
+/* 
c8ca20
+   Define a pair of assertion macros.
c8ca20
+
c8ca20
+   These work like the regular C assert(), in that they will abort the
c8ca20
+   process with a message on stderr if the given condition fails to hold,
c8ca20
+   but compile away to nothing if NDEBUG is defined.
c8ca20
+
c8ca20
+   However, before aborting, Python will also try to call _PyObject_Dump() on
c8ca20
+   the given object.  This may be of use when investigating bugs in which a
c8ca20
+   particular object is corrupt (e.g. buggy a tp_visit method in an extension
c8ca20
+   module breaking the garbage collector), to help locate the broken objects.
c8ca20
+
c8ca20
+   The WITH_MSG variant allows you to supply an additional message that Python
c8ca20
+   will attempt to print to stderr, after the object dump.
c8ca20
+*/
c8ca20
+#ifdef NDEBUG
c8ca20
+/* No debugging: compile away the assertions: */
c8ca20
+#define PyObject_ASSERT_WITH_MSG(obj, expr, msg) ((void)0)
c8ca20
+#else
c8ca20
+/* With debugging: generate checks: */
c8ca20
+#define PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
c8ca20
+  ((expr)                                           \
c8ca20
+   ? (void)(0)                                      \
c8ca20
+   : _PyObject_AssertFailed((obj),                  \
c8ca20
+                            (msg),                  \
c8ca20
+                            (__STRING(expr)),       \
c8ca20
+                            (__FILE__),             \
c8ca20
+                            (__LINE__),             \
c8ca20
+                            (__PRETTY_FUNCTION__)))
c8ca20
+#endif
c8ca20
+
c8ca20
+#define PyObject_ASSERT(obj, expr) \
c8ca20
+  PyObject_ASSERT_WITH_MSG(obj, expr, NULL)
c8ca20
+
c8ca20
+static void _PyObject_AssertFailed(PyObject *,  const char *,
c8ca20
+				   const char *, const char *, int,
c8ca20
+				   const char *);
c8ca20
+
c8ca20
+static void
c8ca20
+_PyObject_AssertFailed(PyObject *obj, const char *msg, const char *expr,
c8ca20
+		       const char *file, int line, const char *function)
c8ca20
+{
c8ca20
+    fprintf(stderr,
c8ca20
+            "%s:%d: %s: Assertion \"%s\" failed.\n",
c8ca20
+            file, line, function, expr);
c8ca20
+    if (msg) {
c8ca20
+        fprintf(stderr, "%s\n", msg);
c8ca20
+    }
c8ca20
+
c8ca20
+    fflush(stderr);
c8ca20
+
c8ca20
+    if (obj) {
c8ca20
+        /* This might succeed or fail, but we're about to abort, so at least
c8ca20
+           try to provide any extra info we can: */
c8ca20
+        _PyObject_Dump(obj);
c8ca20
+    }
c8ca20
+    else {
c8ca20
+        fprintf(stderr, "NULL object\n");
c8ca20
+    }
c8ca20
+
c8ca20
+    fflush(stdout);
c8ca20
+    fflush(stderr);
c8ca20
+
c8ca20
+    /* Terminate the process: */
c8ca20
+    abort();
c8ca20
+}
c8ca20
+
c8ca20
 /* Get an object's GC head */
c8ca20
 #define AS_GC(o) ((PyGC_Head *)(o)-1)
c8ca20
 
c8ca20
@@ -288,7 +355,8 @@ update_refs(PyGC_Head *containers)
c8ca20
 {
c8ca20
     PyGC_Head *gc = containers->gc.gc_next;
c8ca20
     for (; gc != containers; gc = gc->gc.gc_next) {
c8ca20
-        assert(gc->gc.gc_refs == GC_REACHABLE);
c8ca20
+        PyObject_ASSERT(FROM_GC(gc),
c8ca20
+                        gc->gc.gc_refs == GC_REACHABLE);
c8ca20
         gc->gc.gc_refs = Py_REFCNT(FROM_GC(gc));
c8ca20
         /* Python's cyclic gc should never see an incoming refcount
c8ca20
          * of 0:  if something decref'ed to 0, it should have been
c8ca20
@@ -308,7 +376,8 @@ update_refs(PyGC_Head *containers)
c8ca20
          * so serious that maybe this should be a release-build
c8ca20
          * check instead of an assert?
c8ca20
          */
c8ca20
-        assert(gc->gc.gc_refs != 0);
c8ca20
+        PyObject_ASSERT(FROM_GC(gc),
c8ca20
+                        gc->gc.gc_refs != 0);
c8ca20
     }
c8ca20
 }
c8ca20
 
c8ca20
@@ -323,7 +392,9 @@ visit_decref(PyObject *op, void *data)
c8ca20
          * generation being collected, which can be recognized
c8ca20
          * because only they have positive gc_refs.
c8ca20
          */
c8ca20
-        assert(gc->gc.gc_refs != 0); /* else refcount was too small */
c8ca20
+        PyObject_ASSERT_WITH_MSG(FROM_GC(gc),
c8ca20
+                                 gc->gc.gc_refs != 0,
c8ca20
+                                 "refcount was too small");
c8ca20
         if (gc->gc.gc_refs > 0)
c8ca20
             gc->gc.gc_refs--;
c8ca20
     }
c8ca20
@@ -383,9 +454,10 @@ visit_reachable(PyObject *op, PyGC_Head
c8ca20
          * If gc_refs == GC_UNTRACKED, it must be ignored.
c8ca20
          */
c8ca20
          else {
c8ca20
-            assert(gc_refs > 0
c8ca20
-                   || gc_refs == GC_REACHABLE
c8ca20
-                   || gc_refs == GC_UNTRACKED);
c8ca20
+             PyObject_ASSERT(FROM_GC(gc),
c8ca20
+                             gc_refs > 0
c8ca20
+                             || gc_refs == GC_REACHABLE
c8ca20
+                             || gc_refs == GC_UNTRACKED);
c8ca20
          }
c8ca20
     }
c8ca20
     return 0;
c8ca20
@@ -427,7 +499,7 @@ move_unreachable(PyGC_Head *young, PyGC_
c8ca20
              */
c8ca20
             PyObject *op = FROM_GC(gc);
c8ca20
             traverseproc traverse = Py_TYPE(op)->tp_traverse;
c8ca20
-            assert(gc->gc.gc_refs > 0);
c8ca20
+            PyObject_ASSERT(op, gc->gc.gc_refs > 0);
c8ca20
             gc->gc.gc_refs = GC_REACHABLE;
c8ca20
             (void) traverse(op,
c8ca20
                             (visitproc)visit_reachable,
c8ca20
@@ -494,7 +566,8 @@ move_finalizers(PyGC_Head *unreachable,
c8ca20
     for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
c8ca20
         PyObject *op = FROM_GC(gc);
c8ca20
 
c8ca20
-        assert(IS_TENTATIVELY_UNREACHABLE(op));
c8ca20
+        PyObject_ASSERT(op, IS_TENTATIVELY_UNREACHABLE(op));
c8ca20
+
c8ca20
         next = gc->gc.gc_next;
c8ca20
 
c8ca20
         if (has_finalizer(op)) {
c8ca20
@@ -570,7 +643,7 @@ handle_weakrefs(PyGC_Head *unreachable,
c8ca20
         PyWeakReference **wrlist;
c8ca20
 
c8ca20
         op = FROM_GC(gc);
c8ca20
-        assert(IS_TENTATIVELY_UNREACHABLE(op));
c8ca20
+        PyObject_ASSERT(op, IS_TENTATIVELY_UNREACHABLE(op));
c8ca20
         next = gc->gc.gc_next;
c8ca20
 
c8ca20
         if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
c8ca20
@@ -591,9 +664,9 @@ handle_weakrefs(PyGC_Head *unreachable,
c8ca20
              * the callback pointer intact.  Obscure:  it also
c8ca20
              * changes *wrlist.
c8ca20
              */
c8ca20
-            assert(wr->wr_object == op);
c8ca20
+            PyObject_ASSERT(wr->wr_object, wr->wr_object == op);
c8ca20
             _PyWeakref_ClearRef(wr);
c8ca20
-            assert(wr->wr_object == Py_None);
c8ca20
+            PyObject_ASSERT(wr->wr_object, wr->wr_object == Py_None);
c8ca20
             if (wr->wr_callback == NULL)
c8ca20
                 continue;                       /* no callback */
c8ca20
 
c8ca20
@@ -627,7 +700,7 @@ handle_weakrefs(PyGC_Head *unreachable,
c8ca20
      */
c8ca20
             if (IS_TENTATIVELY_UNREACHABLE(wr))
c8ca20
                 continue;
c8ca20
-            assert(IS_REACHABLE(wr));
c8ca20
+            PyObject_ASSERT(op, IS_REACHABLE(wr));
c8ca20
 
c8ca20
             /* Create a new reference so that wr can't go away
c8ca20
              * before we can process it again.
c8ca20
@@ -636,7 +709,8 @@ handle_weakrefs(PyGC_Head *unreachable,
c8ca20
 
c8ca20
             /* Move wr to wrcb_to_call, for the next pass. */
c8ca20
             wrasgc = AS_GC(wr);
c8ca20
-            assert(wrasgc != next); /* wrasgc is reachable, but
c8ca20
+            PyObject_ASSERT(op, wrasgc != next);
c8ca20
+                                    /* wrasgc is reachable, but
c8ca20
                                        next isn't, so they can't
c8ca20
                                        be the same */
c8ca20
             gc_list_move(wrasgc, &wrcb_to_call);
c8ca20
@@ -652,11 +726,11 @@ handle_weakrefs(PyGC_Head *unreachable,
c8ca20
 
c8ca20
         gc = wrcb_to_call.gc.gc_next;
c8ca20
         op = FROM_GC(gc);
c8ca20
-        assert(IS_REACHABLE(op));
c8ca20
-        assert(PyWeakref_Check(op));
c8ca20
+        PyObject_ASSERT(op, IS_REACHABLE(op));
c8ca20
+        PyObject_ASSERT(op, PyWeakref_Check(op));
c8ca20
         wr = (PyWeakReference *)op;
c8ca20
         callback = wr->wr_callback;
c8ca20
-        assert(callback != NULL);
c8ca20
+        PyObject_ASSERT(op, callback != NULL);
c8ca20
 
c8ca20
         /* copy-paste of weakrefobject.c's handle_callback() */
c8ca20
         temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);
c8ca20
@@ -759,7 +833,7 @@ delete_garbage(PyGC_Head *collectable, P
c8ca20
         PyGC_Head *gc = collectable->gc.gc_next;
c8ca20
         PyObject *op = FROM_GC(gc);
c8ca20
 
c8ca20
-        assert(IS_TENTATIVELY_UNREACHABLE(op));
c8ca20
+        PyObject_ASSERT(op, IS_TENTATIVELY_UNREACHABLE(op));
c8ca20
         if (debug & DEBUG_SAVEALL) {
c8ca20
             PyList_Append(garbage, op);
c8ca20
         }