Blame SOURCES/00170-gc-assertions.patch

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