Blame SOURCES/00170-gc-assertions.patch

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