diff options
author | Victor Stinner <vstinner@python.org> | 2019-10-07 16:42:01 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-07 16:42:01 (GMT) |
commit | 6876257eaabdb30f27ebcbd7d2557278ce2e5705 (patch) | |
tree | 05597a0310d1e330c0c156c97f0fbc8a6386675e /Objects/object.c | |
parent | 321def805abc5b7c92c7e90ca90cb2434fdab855 (diff) | |
download | cpython-6876257eaabdb30f27ebcbd7d2557278ce2e5705.zip cpython-6876257eaabdb30f27ebcbd7d2557278ce2e5705.tar.gz cpython-6876257eaabdb30f27ebcbd7d2557278ce2e5705.tar.bz2 |
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.
Diffstat (limited to 'Objects/object.c')
-rw-r--r-- | Objects/object.c | 71 |
1 files changed, 33 insertions, 38 deletions
diff --git a/Objects/object.c b/Objects/object.c index 43ed4c5..e94480f 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -25,13 +25,14 @@ _Py_IDENTIFIER(__isabstractmethod__); 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 @@ _PyObject_CheckConsistency(PyObject *op, int check_content) _PyDict_CheckConsistency(op, check_content); } return 1; + +#undef CHECK } @@ -463,41 +466,41 @@ _PyObject_IsFreed(PyObject *op) 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; + /* 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, "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); - fprintf(stderr, "object : "); + /* the most dangerous part */ + fprintf(stderr, "object repr : "); fflush(stderr); - gil = PyGILState_Ensure(); + 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); + PyErr_Restore(error_type, error_value, error_traceback); PyGILState_Release(gil); - /* 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, "\n"); fflush(stderr); } @@ -2146,6 +2149,7 @@ _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg, fprintf(stderr, "%s: ", function); } fflush(stderr); + if (expr) { fprintf(stderr, "Assertion \"%s\" failed", expr); } @@ -2153,26 +2157,18 @@ _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg, 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. @@ -2192,7 +2188,6 @@ _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg, try to provide any extra info we can: */ _PyObject_Dump(obj); } - fflush(stderr); Py_FatalError("_PyObject_AssertFailed"); } |