Blame SOURCES/00170-gc-assertions.patch

dd3773
diff --git a/Include/object.h b/Include/object.h
dd3773
index 63e37b8..613b26c 100644
dd3773
--- a/Include/object.h
dd3773
+++ b/Include/object.h
dd3773
@@ -1071,6 +1071,49 @@ PyAPI_FUNC(void)
dd3773
 _PyObject_DebugTypeStats(FILE *out);
dd3773
 #endif /* ifndef Py_LIMITED_API */
dd3773
 
dd3773
+/* 
dd3773
+   Define a pair of assertion macros.
dd3773
+
dd3773
+   These work like the regular C assert(), in that they will abort the
dd3773
+   process with a message on stderr if the given condition fails to hold,
dd3773
+   but compile away to nothing if NDEBUG is defined.
dd3773
+
dd3773
+   However, before aborting, Python will also try to call _PyObject_Dump() on
dd3773
+   the given object.  This may be of use when investigating bugs in which a
dd3773
+   particular object is corrupt (e.g. buggy a tp_visit method in an extension
dd3773
+   module breaking the garbage collector), to help locate the broken objects.
dd3773
+
dd3773
+   The WITH_MSG variant allows you to supply an additional message that Python
dd3773
+   will attempt to print to stderr, after the object dump.
dd3773
+*/
dd3773
+#ifdef NDEBUG
dd3773
+/* No debugging: compile away the assertions: */
dd3773
+#define PyObject_ASSERT_WITH_MSG(obj, expr, msg) ((void)0)
dd3773
+#else
dd3773
+/* With debugging: generate checks: */
dd3773
+#define PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
dd3773
+  ((expr)                                           \
dd3773
+   ? (void)(0)                                      \
dd3773
+   : _PyObject_AssertFailed((obj),                  \
dd3773
+                            (msg),                  \
dd3773
+                            (__STRING(expr)),       \
dd3773
+                            (__FILE__),             \
dd3773
+                            (__LINE__),             \
dd3773
+                            (__PRETTY_FUNCTION__)))
dd3773
+#endif
dd3773
+
dd3773
+#define PyObject_ASSERT(obj, expr) \
dd3773
+  PyObject_ASSERT_WITH_MSG(obj, expr, NULL)
dd3773
+
dd3773
+/* 
dd3773
+   Declare and define the entrypoint even when NDEBUG is defined, to avoid
dd3773
+   causing compiler/linker errors when building extensions without NDEBUG
dd3773
+   against a Python built with NDEBUG defined
dd3773
+*/
dd3773
+PyAPI_FUNC(void) _PyObject_AssertFailed(PyObject *,  const char *,
dd3773
+                                        const char *, const char *, int,
dd3773
+                                        const char *);
dd3773
+
dd3773
 #ifdef __cplusplus
dd3773
 }
dd3773
 #endif
dd3773
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
dd3773
index 7e82b24..4497b8f 100644
dd3773
--- a/Lib/test/test_gc.py
dd3773
+++ b/Lib/test/test_gc.py
dd3773
@@ -1,10 +1,12 @@
dd3773
 import unittest
dd3773
 from test.support import (verbose, refcount_test, run_unittest,
dd3773
                           strip_python_stderr, cpython_only, start_threads,
dd3773
-                          temp_dir, requires_type_collecting, TESTFN, unlink)
dd3773
+                          temp_dir, requires_type_collecting, TESTFN, unlink,
dd3773
+                          import_module)
dd3773
 from test.support.script_helper import assert_python_ok, make_script
dd3773
 
dd3773
 import sys
dd3773
+import sysconfig
dd3773
 import time
dd3773
 import gc
dd3773
 import weakref
dd3773
@@ -50,6 +52,8 @@ class GC_Detector(object):
dd3773
         # gc collects it.
dd3773
         self.wr = weakref.ref(C1055820(666), it_happened)
dd3773
 
dd3773
+BUILD_WITH_NDEBUG = ('-DNDEBUG' in sysconfig.get_config_vars()['PY_CFLAGS'])
dd3773
+
dd3773
 @with_tp_del
dd3773
 class Uncollectable(object):
dd3773
     """Create a reference cycle with multiple __del__ methods.
dd3773
@@ -877,6 +881,50 @@ class GCCallbackTests(unittest.TestCase):
dd3773
         self.assertEqual(len(gc.garbage), 0)
dd3773
 
dd3773
 
dd3773
+    @unittest.skipIf(BUILD_WITH_NDEBUG,
dd3773
+                     'built with -NDEBUG')
dd3773
+    def test_refcount_errors(self):
dd3773
+        self.preclean()
dd3773
+        # Verify the "handling" of objects with broken refcounts
dd3773
+        import_module("ctypes") #skip if not supported
dd3773
+
dd3773
+        import subprocess
dd3773
+        code = '''if 1:
dd3773
+        a = []
dd3773
+        b = [a]
dd3773
+
dd3773
+        # Simulate the refcount of "a" being too low (compared to the
dd3773
+        # references held on it by live data), but keeping it above zero
dd3773
+        # (to avoid deallocating it):
dd3773
+        import ctypes
dd3773
+        ctypes.pythonapi.Py_DecRef(ctypes.py_object(a))
dd3773
+
dd3773
+        # The garbage collector should now have a fatal error when it reaches
dd3773
+        # the broken object:
dd3773
+        import gc
dd3773
+        gc.collect()
dd3773
+        '''
dd3773
+        p = subprocess.Popen([sys.executable, "-c", code],
dd3773
+                             stdout=subprocess.PIPE,
dd3773
+                             stderr=subprocess.PIPE)
dd3773
+        stdout, stderr = p.communicate()
dd3773
+        p.stdout.close()
dd3773
+        p.stderr.close()
dd3773
+        # Verify that stderr has a useful error message:
dd3773
+        self.assertRegex(stderr,
dd3773
+            b'Modules/gcmodule.c:[0-9]+: visit_decref: Assertion "\(\(gc\)->gc.gc_refs >> \(1\)\) != 0" failed.')
dd3773
+        self.assertRegex(stderr,
dd3773
+            b'refcount was too small')
dd3773
+        self.assertRegex(stderr,
dd3773
+            b'object  : \[\]')
dd3773
+        self.assertRegex(stderr,
dd3773
+            b'type    : list')
dd3773
+        self.assertRegex(stderr,
dd3773
+            b'refcount: 1')
dd3773
+        self.assertRegex(stderr,
dd3773
+            b'address : 0x[0-9a-f]+')
dd3773
+
dd3773
+
dd3773
 class GCTogglingTests(unittest.TestCase):
dd3773
     def setUp(self):
dd3773
         gc.enable()
dd3773
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
dd3773
index 3bddc40..0cc24f7 100644
dd3773
--- a/Modules/gcmodule.c
dd3773
+++ b/Modules/gcmodule.c
dd3773
@@ -342,7 +342,8 @@ update_refs(PyGC_Head *containers)
dd3773
 {
dd3773
     PyGC_Head *gc = containers->gc.gc_next;
dd3773
     for (; gc != containers; gc = gc->gc.gc_next) {
dd3773
-        assert(_PyGCHead_REFS(gc) == GC_REACHABLE);
dd3773
+        PyObject_ASSERT(FROM_GC(gc),
dd3773
+                        _PyGCHead_REFS(gc) == GC_REACHABLE);
dd3773
         _PyGCHead_SET_REFS(gc, Py_REFCNT(FROM_GC(gc)));
dd3773
         /* Python's cyclic gc should never see an incoming refcount
dd3773
          * of 0:  if something decref'ed to 0, it should have been
dd3773
@@ -362,7 +363,8 @@ update_refs(PyGC_Head *containers)
dd3773
          * so serious that maybe this should be a release-build
dd3773
          * check instead of an assert?
dd3773
          */
dd3773
-        assert(_PyGCHead_REFS(gc) != 0);
dd3773
+        PyObject_ASSERT(FROM_GC(gc),
dd3773
+                        _PyGCHead_REFS(gc) != 0);
dd3773
     }
dd3773
 }
dd3773
 
dd3773
@@ -377,7 +379,9 @@ visit_decref(PyObject *op, void *data)
dd3773
          * generation being collected, which can be recognized
dd3773
          * because only they have positive gc_refs.
dd3773
          */
dd3773
-        assert(_PyGCHead_REFS(gc) != 0); /* else refcount was too small */
dd3773
+        PyObject_ASSERT_WITH_MSG(FROM_GC(gc),
dd3773
+                        _PyGCHead_REFS(gc) != 0,
dd3773
+                        "refcount was too small"); /* else refcount was too small */
dd3773
         if (_PyGCHead_REFS(gc) > 0)
dd3773
             _PyGCHead_DECREF(gc);
dd3773
     }
dd3773
@@ -437,9 +441,10 @@ visit_reachable(PyObject *op, PyGC_Head *reachable)
dd3773
          * If gc_refs == GC_UNTRACKED, it must be ignored.
dd3773
          */
dd3773
          else {
dd3773
-            assert(gc_refs > 0
dd3773
-                   || gc_refs == GC_REACHABLE
dd3773
-                   || gc_refs == GC_UNTRACKED);
dd3773
+             PyObject_ASSERT(FROM_GC(gc),
dd3773
+                             gc_refs > 0
dd3773
+                             || gc_refs == GC_REACHABLE
dd3773
+                             || gc_refs == GC_UNTRACKED);
dd3773
          }
dd3773
     }
dd3773
     return 0;
dd3773
@@ -481,7 +486,7 @@ move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
dd3773
              */
dd3773
             PyObject *op = FROM_GC(gc);
dd3773
             traverseproc traverse = Py_TYPE(op)->tp_traverse;
dd3773
-            assert(_PyGCHead_REFS(gc) > 0);
dd3773
+            PyObject_ASSERT(op, _PyGCHead_REFS(gc) > 0);
dd3773
             _PyGCHead_SET_REFS(gc, GC_REACHABLE);
dd3773
             (void) traverse(op,
dd3773
                             (visitproc)visit_reachable,
dd3773
@@ -544,7 +549,7 @@ move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
dd3773
     for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
dd3773
         PyObject *op = FROM_GC(gc);
dd3773
 
dd3773
-        assert(IS_TENTATIVELY_UNREACHABLE(op));
dd3773
+        PyObject_ASSERT(op, IS_TENTATIVELY_UNREACHABLE(op));
dd3773
         next = gc->gc.gc_next;
dd3773
 
dd3773
         if (has_legacy_finalizer(op)) {
dd3773
@@ -620,7 +625,7 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
dd3773
         PyWeakReference **wrlist;
dd3773
 
dd3773
         op = FROM_GC(gc);
dd3773
-        assert(IS_TENTATIVELY_UNREACHABLE(op));
dd3773
+        PyObject_ASSERT(op, IS_TENTATIVELY_UNREACHABLE(op));
dd3773
         next = gc->gc.gc_next;
dd3773
 
dd3773
         if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
dd3773
@@ -641,9 +646,9 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
dd3773
              * the callback pointer intact.  Obscure:  it also
dd3773
              * changes *wrlist.
dd3773
              */
dd3773
-            assert(wr->wr_object == op);
dd3773
+            PyObject_ASSERT(wr->wr_object, wr->wr_object == op);
dd3773
             _PyWeakref_ClearRef(wr);
dd3773
-            assert(wr->wr_object == Py_None);
dd3773
+            PyObject_ASSERT(wr->wr_object, wr->wr_object == Py_None);
dd3773
             if (wr->wr_callback == NULL)
dd3773
                 continue;                       /* no callback */
dd3773
 
dd3773
@@ -677,7 +682,7 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
dd3773
      */
dd3773
             if (IS_TENTATIVELY_UNREACHABLE(wr))
dd3773
                 continue;
dd3773
-            assert(IS_REACHABLE(wr));
dd3773
+            PyObject_ASSERT(op, IS_REACHABLE(wr));
dd3773
 
dd3773
             /* Create a new reference so that wr can't go away
dd3773
              * before we can process it again.
dd3773
@@ -686,7 +691,8 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
dd3773
 
dd3773
             /* Move wr to wrcb_to_call, for the next pass. */
dd3773
             wrasgc = AS_GC(wr);
dd3773
-            assert(wrasgc != next); /* wrasgc is reachable, but
dd3773
+            PyObject_ASSERT(op, wrasgc != next);
dd3773
+                                    /* wrasgc is reachable, but
dd3773
                                        next isn't, so they can't
dd3773
                                        be the same */
dd3773
             gc_list_move(wrasgc, &wrcb_to_call);
dd3773
@@ -702,11 +708,11 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
dd3773
 
dd3773
         gc = wrcb_to_call.gc.gc_next;
dd3773
         op = FROM_GC(gc);
dd3773
-        assert(IS_REACHABLE(op));
dd3773
-        assert(PyWeakref_Check(op));
dd3773
+        PyObject_ASSERT(op, IS_REACHABLE(op));
dd3773
+        PyObject_ASSERT(op, PyWeakref_Check(op));
dd3773
         wr = (PyWeakReference *)op;
dd3773
         callback = wr->wr_callback;
dd3773
-        assert(callback != NULL);
dd3773
+        PyObject_ASSERT(op, callback != NULL);
dd3773
 
dd3773
         /* copy-paste of weakrefobject.c's handle_callback() */
dd3773
         temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);
dd3773
@@ -820,12 +826,14 @@ check_garbage(PyGC_Head *collectable)
dd3773
     for (gc = collectable->gc.gc_next; gc != collectable;
dd3773
          gc = gc->gc.gc_next) {
dd3773
         _PyGCHead_SET_REFS(gc, Py_REFCNT(FROM_GC(gc)));
dd3773
-        assert(_PyGCHead_REFS(gc) != 0);
dd3773
+        PyObject_ASSERT(FROM_GC(gc),
dd3773
+                        _PyGCHead_REFS(gc) != 0);
dd3773
     }
dd3773
     subtract_refs(collectable);
dd3773
     for (gc = collectable->gc.gc_next; gc != collectable;
dd3773
          gc = gc->gc.gc_next) {
dd3773
-        assert(_PyGCHead_REFS(gc) >= 0);
dd3773
+        PyObject_ASSERT(FROM_GC(gc),
dd3773
+                        _PyGCHead_REFS(gc) >= 0);
dd3773
         if (_PyGCHead_REFS(gc) != 0)
dd3773
             return -1;
dd3773
     }
dd3773
diff --git a/Objects/object.c b/Objects/object.c
dd3773
index defff55..a50697d 100644
dd3773
--- a/Objects/object.c
dd3773
+++ b/Objects/object.c
dd3773
@@ -2030,6 +2030,35 @@ _PyTrash_thread_destroy_chain(void)
dd3773
     }
dd3773
 }
dd3773
 
dd3773
+PyAPI_FUNC(void)
dd3773
+_PyObject_AssertFailed(PyObject *obj, const char *msg, const char *expr,
dd3773
+              const char *file, int line, const char *function)
dd3773
+{
dd3773
+    fprintf(stderr,
dd3773
+            "%s:%d: %s: Assertion \"%s\" failed.\n",
dd3773
+            file, line, function, expr);
dd3773
+    if (msg) {
dd3773
+        fprintf(stderr, "%s\n", msg);
dd3773
+    }
dd3773
+
dd3773
+    fflush(stderr);
dd3773
+
dd3773
+    if (obj) {
dd3773
+        /* This might succeed or fail, but we're about to abort, so at least
dd3773
+           try to provide any extra info we can: */
dd3773
+        _PyObject_Dump(obj);
dd3773
+    }
dd3773
+    else {
dd3773
+        fprintf(stderr, "NULL object\n");
dd3773
+    }
dd3773
+
dd3773
+    fflush(stdout);
dd3773
+    fflush(stderr);
dd3773
+
dd3773
+    /* Terminate the process: */
dd3773
+    abort();
dd3773
+}
dd3773
+
dd3773
 #ifndef Py_TRACE_REFS
dd3773
 /* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
dd3773
    Define this here, so we can undefine the macro. */