[3.8] bpo-36389: Backport debug enhancements from master (GH-16796)
* bpo-36389: _PyObject_CheckConsistency() available in release mode (GH-16612)
bpo-36389, bpo-38376: The _PyObject_CheckConsistency() function is
now also available in release mode. For example, it can be used to
debug a crash in the visit_decref() function of the GC.
Modify the following functions to also work in release mode:
* _PyDict_CheckConsistency()
* _PyObject_CheckConsistency()
* _PyType_CheckConsistency()
* _PyUnicode_CheckConsistency()
Other changes:
* _PyMem_IsPtrFreed(ptr) now also returns 1 if ptr is NULL
(equals to 0).
* _PyBytesWriter_CheckConsistency() now returns 1 and is only used
with assert().
* Reorder _PyObject_Dump() to write safe fields first, and only
attempt to render repr() at the end.
(cherry picked from commit 6876257eaabdb30f27ebcbd7d2557278ce2e5705)
* bpo-36389: Fix _PyBytesWriter in release mode (GH-16624)
Fix _PyBytesWriter API when Python is built in release mode with
assertions.
(cherry picked from commit 60ec6efd96d95476fe5e38c491491add04f026e5)
* bpo-38070: Enhance visit_decref() debug trace (GH-16631)
subtract_refs() now pass the parent object to visit_decref() which
pass it to _PyObject_ASSERT(). So if the "is freed" assertion fails,
the parent is used in debug trace, rather than the freed object. The
parent object is more likely to contain useful information. Freed
objects cannot be inspected are are displayed as "<object at xxx is
freed>" with no other detail.
(cherry picked from commit 4d5f94b8cd20f804c7868c5395a15aa6032f874c)
* Fix also a typo in PYMEM_DEADBYTE macro comment
* bpo-36389: Add newline to _PyObject_AssertFailed() (GH-16629)
Add a newline between the verbose object dump and the Py_FatalError()
logs for readability.
(cherry picked from commit 7775349895088a7ae68cecf0c74cf817f15e2c74)
diff --git a/Objects/object.c b/Objects/object.c
index df25313..566593a 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -25,13 +25,14 @@
int
_PyObject_CheckConsistency(PyObject *op, int check_content)
{
- _PyObject_ASSERT(op, op != NULL);
- _PyObject_ASSERT(op, !_PyObject_IsFreed(op));
- _PyObject_ASSERT(op, Py_REFCNT(op) >= 1);
+#define CHECK(expr) \
+ do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
- PyTypeObject *type = op->ob_type;
- _PyObject_ASSERT(op, type != NULL);
- _PyType_CheckConsistency(type);
+ CHECK(!_PyObject_IsFreed(op));
+ CHECK(Py_REFCNT(op) >= 1);
+
+ CHECK(op->ob_type != NULL);
+ _PyType_CheckConsistency(op->ob_type);
if (PyUnicode_Check(op)) {
_PyUnicode_CheckConsistency(op, check_content);
@@ -40,6 +41,8 @@
_PyDict_CheckConsistency(op, check_content);
}
return 1;
+
+#undef CHECK
}
@@ -463,41 +466,41 @@
void
_PyObject_Dump(PyObject* op)
{
- if (op == NULL) {
- fprintf(stderr, "<object at NULL>\n");
- fflush(stderr);
- return;
- }
-
if (_PyObject_IsFreed(op)) {
/* It seems like the object memory has been freed:
don't access it to prevent a segmentation fault. */
fprintf(stderr, "<object at %p is freed>\n", op);
+ fflush(stderr);
return;
}
- PyGILState_STATE gil;
- PyObject *error_type, *error_value, *error_traceback;
-
- fprintf(stderr, "object : ");
- fflush(stderr);
- gil = PyGILState_Ensure();
-
- PyErr_Fetch(&error_type, &error_value, &error_traceback);
- (void)PyObject_Print(op, stderr, 0);
- fflush(stderr);
- PyErr_Restore(error_type, error_value, error_traceback);
-
- PyGILState_Release(gil);
+ /* first, write fields which are the least likely to crash */
+ fprintf(stderr, "object address : %p\n", (void *)op);
/* XXX(twouters) cast refcount to long until %zd is
universally available */
- fprintf(stderr, "\n"
- "type : %s\n"
- "refcount: %ld\n"
- "address : %p\n",
- Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
- (long)op->ob_refcnt,
- (void *)op);
+ fprintf(stderr, "object refcount : %ld\n", (long)op->ob_refcnt);
+ fflush(stderr);
+
+ PyTypeObject *type = Py_TYPE(op);
+ fprintf(stderr, "object type : %p\n", type);
+ fprintf(stderr, "object type name: %s\n",
+ type==NULL ? "NULL" : type->tp_name);
+
+ /* the most dangerous part */
+ fprintf(stderr, "object repr : ");
+ fflush(stderr);
+
+ PyGILState_STATE gil = PyGILState_Ensure();
+ PyObject *error_type, *error_value, *error_traceback;
+ PyErr_Fetch(&error_type, &error_value, &error_traceback);
+
+ (void)PyObject_Print(op, stderr, 0);
+ fflush(stderr);
+
+ PyErr_Restore(error_type, error_value, error_traceback);
+ PyGILState_Release(gil);
+
+ fprintf(stderr, "\n");
fflush(stderr);
}
@@ -2148,6 +2151,7 @@
fprintf(stderr, "%s: ", function);
}
fflush(stderr);
+
if (expr) {
fprintf(stderr, "Assertion \"%s\" failed", expr);
}
@@ -2155,26 +2159,18 @@
fprintf(stderr, "Assertion failed");
}
fflush(stderr);
+
if (msg) {
fprintf(stderr, ": %s", msg);
}
fprintf(stderr, "\n");
fflush(stderr);
- if (obj == NULL) {
- fprintf(stderr, "<object at NULL>\n");
- }
- else if (_PyObject_IsFreed(obj)) {
+ if (_PyObject_IsFreed(obj)) {
/* It seems like the object memory has been freed:
don't access it to prevent a segmentation fault. */
fprintf(stderr, "<object at %p is freed>\n", obj);
- }
- else if (Py_TYPE(obj) == NULL) {
- fprintf(stderr, "<object at %p: ob_type=NULL>\n", obj);
- }
- else if (_PyObject_IsFreed((PyObject *)Py_TYPE(obj))) {
- fprintf(stderr, "<object at %p: type at %p is freed>\n",
- obj, (void *)Py_TYPE(obj));
+ fflush(stderr);
}
else {
/* Display the traceback where the object has been allocated.
@@ -2193,8 +2189,10 @@
/* This might succeed or fail, but we're about to abort, so at least
try to provide any extra info we can: */
_PyObject_Dump(obj);
+
+ fprintf(stderr, "\n");
+ fflush(stderr);
}
- fflush(stderr);
Py_FatalError("_PyObject_AssertFailed");
}