Blame SOURCES/00170-gc-assertions.patch

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