Blame SOURCES/00170-gc-assertions.patch

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