Blame SOURCES/00170-gc-assertions.patch

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