Blame 00170-gc-assertions.patch

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