Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *);

#define Py_XSETREF(op, op2) \
do { \
PyObject *_py_tmp = _PyObject_CAST(op); \
PyObject *_py_tmp = _PyObject_XCAST(op);\
(op) = (op2); \
Py_XDECREF(_py_tmp); \
} while (0)
Expand Down
4 changes: 2 additions & 2 deletions Include/cpython/objimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ _PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
}

#define PyObject_INIT(op, typeobj) \
_PyObject_INIT(_PyObject_CAST(op), (typeobj))
_PyObject_INIT(_PyObject_CAST_RAW(op), (typeobj))

static inline PyVarObject*
_PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
Expand All @@ -36,7 +36,7 @@ _PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
}

#define PyObject_INIT_VAR(op, typeobj, size) \
_PyObject_INIT_VAR(_PyVarObject_CAST(op), (typeobj), (size))
_PyObject_INIT_VAR(_PyVarObject_CAST_RAW(op), (typeobj), (size))


/* This function returns the number of allocated memory blocks, regardless of size */
Expand Down
42 changes: 33 additions & 9 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,41 @@ typedef struct _object {
PyTypeObject *ob_type;
} PyObject;

PyAPI_FUNC(PyObject*) _PyObject_Cast(const void *op_raw);
PyAPI_FUNC(PyObject*) _PyObject_XCast(const void *op_raw);

/* Cast argument to PyObject* type. */
#define _PyObject_CAST(op) ((PyObject*)(op))
#define _PyObject_CAST_CONST(op) ((const PyObject*)(op))
#ifdef Py_DEBUG
# define _PyObject_CAST(op) _PyObject_Cast(op)
# define _PyObject_XCAST(op) _PyObject_XCast(op)
#else
# define _PyObject_CAST(op) ((PyObject*)(op))
# define _PyObject_XCAST(op) _PyObject_CAST(op)
#endif
#define _PyObject_CAST_CONST(op) ((const PyObject*)_PyObject_CAST(op))

/* Similar to _PyObject_CAST() but don't check if the argument is valid
PyObject in debug mode. This macro should only be used in low-level
functions like Py_SET_TYPE() or Py_SET_REFCNT() which are used to
initialize newly created objects. */
#define _PyObject_CAST_RAW(op) ((PyObject*)(op))

typedef struct {
PyObject ob_base;
Py_ssize_t ob_size; /* Number of items in variable part */
} PyVarObject;

/* Cast argument to PyVarObject* type. */
#define _PyVarObject_CAST(op) ((PyVarObject*)(op))
#ifdef Py_DEBUG
# define _PyVarObject_CAST(op) ((PyVarObject*)_PyObject_Cast(op))
#else
# define _PyVarObject_CAST(op) ((PyVarObject*)(op))
#endif
/* Similar to _PyVarObject_CAST() but don't check if the argument is valid
PyVarObject in debug mode. This macro should only be used in low-level
functions like Py_SET_SIZE() which is used to initialize newly created
objects. */
#define _PyVarObject_CAST_RAW(op) ((PyVarObject*)(op))

#define Py_REFCNT(ob) (_PyObject_CAST(ob)->ob_refcnt)
#define Py_TYPE(ob) (_PyObject_CAST(ob)->ob_type)
Expand All @@ -132,17 +156,17 @@ static inline int _Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
ob->ob_refcnt = refcnt;
}
#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST_RAW(ob), refcnt)

static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
ob->ob_type = type;
}
#define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
#define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST_RAW(ob), type)

static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
ob->ob_size = size;
}
#define Py_SET_SIZE(ob, size) _Py_SET_SIZE(_PyVarObject_CAST(ob), size)
#define Py_SET_SIZE(ob, size) _Py_SET_SIZE(_PyVarObject_CAST_RAW(ob), size)


/*
Expand Down Expand Up @@ -469,7 +493,7 @@ static inline void _Py_DECREF(
*/
#define Py_CLEAR(op) \
do { \
PyObject *_py_tmp = _PyObject_CAST(op); \
PyObject *_py_tmp = _PyObject_XCAST(op);\
if (_py_tmp != NULL) { \
(op) = NULL; \
Py_DECREF(_py_tmp); \
Expand All @@ -484,7 +508,7 @@ static inline void _Py_XINCREF(PyObject *op)
}
}

#define Py_XINCREF(op) _Py_XINCREF(_PyObject_CAST(op))
#define Py_XINCREF(op) _Py_XINCREF(_PyObject_XCAST(op))

static inline void _Py_XDECREF(PyObject *op)
{
Expand All @@ -493,7 +517,7 @@ static inline void _Py_XDECREF(PyObject *op)
}
}

#define Py_XDECREF(op) _Py_XDECREF(_PyObject_CAST(op))
#define Py_XDECREF(op) _Py_XDECREF(_PyObject_XCAST(op))

/*
These are provided as conveniences to Python runtime embedders, so that
Expand Down
3 changes: 2 additions & 1 deletion Modules/_tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,8 @@ _PyTraceMalloc_NewReference(PyObject *op)
}

uintptr_t ptr;
PyTypeObject *type = Py_TYPE(op);
/* Avoid Py_TYPE() since op is not valid yet */
PyTypeObject *type = op->ob_type;
if (PyType_IS_GC(type)) {
ptr = (uintptr_t)((char *)op - sizeof(PyGC_Head));
}
Expand Down
6 changes: 4 additions & 2 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ PyFloat_FromDouble(double fval)
{
PyFloatObject *op = free_list;
if (op != NULL) {
free_list = (PyFloatObject *) Py_TYPE(op);
/* Avoid Py_TYPE() since op is not valid */
free_list = (PyFloatObject *)((PyObject*)op)->ob_type;
numfree--;
} else {
op = (PyFloatObject*) PyObject_MALLOC(sizeof(PyFloatObject));
Expand Down Expand Up @@ -2004,7 +2005,8 @@ PyFloat_ClearFreeList(void)
PyFloatObject *f = free_list, *next;
int i = numfree;
while (f) {
next = (PyFloatObject*) Py_TYPE(f);
/* Avoid Py_TYPE() since op is not valid */
next = (PyFloatObject*)((PyObject*)f)->ob_type;
PyObject_FREE(f);
f = next;
}
Expand Down
43 changes: 38 additions & 5 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ _Py_IDENTIFIER(__dir__);
_Py_IDENTIFIER(__isabstractmethod__);


PyObject*
_PyObject_Cast(const void *op_raw)
{
#define CHECK(expr) \
do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)

/* Avoid Py_REFCNT() and Py_TYPE() macro which uses _PyObject_CAST()
which calls _PyObject_Cast() */
PyObject *op = (PyObject *)op_raw;
CHECK(!_PyMem_IsPtrFreed(op));
/* Tolerate ob_refcnt==0 to allow to use Py_TYPE() in dealloc methods
like unicode_dealloc(). */
CHECK(op->ob_refcnt >= 0);
CHECK(!_PyMem_IsPtrFreed(op->ob_type));
return op;
#undef CHECK
}


PyObject*
_PyObject_XCast(const void *op_raw)
{
return (op_raw != NULL) ? _PyObject_Cast(op_raw) : NULL;
}


int
_PyObject_CheckConsistency(PyObject *op, int check_content)
{
Expand Down Expand Up @@ -323,7 +349,9 @@ _Py_BreakPoint(void)
int
_PyObject_IsFreed(PyObject *op)
{
if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) {
/* Avoid Py_TYPE() macro which uses _PyObject_CAST() which calls
_PyObject_Cast(). _PyObject_IsFreed() is used by _PyObject_Cast(). */
if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(op->ob_type)) {
return 1;
}
/* ignore op->ob_ref: its value can have be modified
Expand Down Expand Up @@ -354,12 +382,15 @@ _PyObject_Dump(PyObject* op)

/* first, write fields which are the least likely to crash */
fprintf(stderr, "object address : %p\n", (void *)op);
/* Avoid Py_REFCNT() since the object can be invalid */
Py_ssize_t refcnt = op->ob_refcnt;
/* XXX(twouters) cast refcount to long until %zd is
universally available */
fprintf(stderr, "object refcount : %ld\n", (long)Py_REFCNT(op));
fprintf(stderr, "object refcount : %ld\n", (long)refcnt);
fflush(stderr);

PyTypeObject *type = Py_TYPE(op);
/* Avoid Py_TYPE() since the object can be invalid */
PyTypeObject *type = op->ob_type;
fprintf(stderr, "object type : %p\n", type);
fprintf(stderr, "object type name: %s\n",
type==NULL ? "NULL" : type->tp_name);
Expand Down Expand Up @@ -2150,7 +2181,8 @@ _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
Do it before dumping repr(obj), since repr() is more likely
to crash than dumping the traceback. */
void *ptr;
PyTypeObject *type = Py_TYPE(obj);
/* Avoid Py_TYPE() since the object is invalid */
PyTypeObject *type = obj->ob_type;
if (PyType_IS_GC(type)) {
ptr = (void *)((char *)obj - sizeof(PyGC_Head));
}
Expand All @@ -2174,7 +2206,8 @@ _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
void
_Py_Dealloc(PyObject *op)
{
destructor dealloc = Py_TYPE(op)->tp_dealloc;
/* Don't use Py_TYPE() since ob_refcnt=0 */
destructor dealloc = op->ob_type->tp_dealloc;
#ifdef Py_TRACE_REFS
_Py_ForgetReference(op);
#endif
Expand Down
6 changes: 4 additions & 2 deletions Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,10 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
}
#endif

/* PyTypeObject has already been initialized */
if (Py_REFCNT(type) != 0) {
/* PyTypeObject has already been initialized

Avoid Py_REFCNT() since the type is not valid yet. */
if (((PyObject*)type)->ob_refcnt != 0) {
PyErr_BadInternalCall();
return -1;
}
Expand Down
11 changes: 7 additions & 4 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5330,12 +5330,15 @@ PyType_Ready(PyTypeObject *type)

/* Initialize ob_type if NULL. This means extensions that want to be
compilable separately on Windows can call PyType_Ready() instead of
initializing the ob_type field of their type objects. */
/* The test for base != NULL is really unnecessary, since base is only
initializing the ob_type field of their type objects.

The test for base != NULL is really unnecessary, since base is only
NULL when type is &PyBaseObject_Type, and we know its ob_type is
not NULL (it's initialized to &PyType_Type). But coverity doesn't
know that. */
if (Py_IS_TYPE(type, NULL) && base != NULL) {
know that.

Avoid Py_IS_TYPE(type, NULL) since the type is not valid yet. */
if (((PyObject*)type)->ob_type == NULL && base != NULL) {
Py_SET_TYPE(type, Py_TYPE(base));
}

Expand Down
3 changes: 2 additions & 1 deletion Python/importdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
m = NULL;
goto error;
}
if (Py_IS_TYPE(m, NULL)) {
/* Don't use Py_IS_TYPE(m, NULL) which would fail in debug mode */
if (m->ob_type == NULL) {
/* This can happen when a PyModuleDef is returned without calling
* PyModuleDef_Init on it
*/
Expand Down