Blame SOURCES/00170-gc-assertions.patch

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