Blame SOURCES/00170-gc-assertions.patch

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