Skip to content

gh-111178: fix UBSan failures in Python/context.c #128242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 8, 2025
Merged
Changes from 1 commit
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
Next Next commit
fix UBSan failures for PyContext
  • Loading branch information
picnixz committed Dec 23, 2024
commit d9b10424fc156b531a0d5be97e7ea32b990d08ed
63 changes: 34 additions & 29 deletions Python/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,14 @@ class _contextvars.Context "PyContext *" "&PyContext_Type"
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=bdf87f8e0cb580e8]*/


static inline PyContext *
_PyContext_CAST(PyObject *op)
{
assert(PyObject_TypeCheck(op, &PyContext_Type));
return (PyContext *)op;
}


static inline PyContext *
_context_alloc(void)
{
Expand Down Expand Up @@ -513,37 +521,40 @@ context_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}

static int
context_tp_clear(PyContext *self)
context_tp_clear(PyObject *op)
{
PyContext *self = _PyContext_CAST(op);
Py_CLEAR(self->ctx_prev);
Py_CLEAR(self->ctx_vars);
return 0;
}

static int
context_tp_traverse(PyContext *self, visitproc visit, void *arg)
context_tp_traverse(PyObject *op, visitproc visit, void *arg)
{
PyContext *self = _PyContext_CAST(op);
Py_VISIT(self->ctx_prev);
Py_VISIT(self->ctx_vars);
return 0;
}

static void
context_tp_dealloc(PyContext *self)
context_tp_dealloc(PyObject *self)
{
_PyObject_GC_UNTRACK(self);

if (self->ctx_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)self);
PyContext *ctx = _PyContext_CAST(self);
if (ctx->ctx_weakreflist != NULL) {
PyObject_ClearWeakRefs(self);
}
(void)context_tp_clear(self);

_Py_FREELIST_FREE(contexts, self, Py_TYPE(self)->tp_free);
}

static PyObject *
context_tp_iter(PyContext *self)
context_tp_iter(PyObject *op)
{
PyContext *self = _PyContext_CAST(op);
return _PyHamt_NewIterKeys(self->ctx_vars);
}

Expand Down Expand Up @@ -575,18 +586,20 @@ context_tp_richcompare(PyObject *v, PyObject *w, int op)
}

static Py_ssize_t
context_tp_len(PyContext *self)
context_tp_len(PyObject *op)
{
PyContext *self = _PyContext_CAST(op);
return _PyHamt_Len(self->ctx_vars);
}

static PyObject *
context_tp_subscript(PyContext *self, PyObject *key)
context_tp_subscript(PyObject *op, PyObject *key)
{
if (context_check_key_type(key)) {
return NULL;
}
PyObject *val = NULL;
PyContext *self = _PyContext_CAST(op);
int found = _PyHamt_Find(self->ctx_vars, key, &val);
if (found < 0) {
return NULL;
Expand All @@ -599,12 +612,13 @@ context_tp_subscript(PyContext *self, PyObject *key)
}

static int
context_tp_contains(PyContext *self, PyObject *key)
context_tp_contains(PyObject *op, PyObject *key)
{
if (context_check_key_type(key)) {
return -1;
}
PyObject *val = NULL;
PyContext *self = _PyContext_CAST(op);
return _PyHamt_Find(self->ctx_vars, key, &val);
}

Expand Down Expand Up @@ -701,7 +715,7 @@ _contextvars_Context_copy_impl(PyContext *self)


static PyObject *
context_run(PyContext *self, PyObject *const *args,
context_run(PyObject *self, PyObject *const *args,
Py_ssize_t nargs, PyObject *kwnames)
{
PyThreadState *ts = _PyThreadState_GET();
Expand All @@ -712,14 +726,14 @@ context_run(PyContext *self, PyObject *const *args,
return NULL;
}

if (_PyContext_Enter(ts, (PyObject *)self)) {
if (_PyContext_Enter(ts, self)) {
return NULL;
}

PyObject *call_result = _PyObject_VectorcallTstate(
ts, args[0], args + 1, nargs - 1, kwnames);

if (_PyContext_Exit(ts, (PyObject *)self)) {
if (_PyContext_Exit(ts, self)) {
Py_XDECREF(call_result);
return NULL;
}
Expand All @@ -739,21 +753,12 @@ static PyMethodDef PyContext_methods[] = {
};

static PySequenceMethods PyContext_as_sequence = {
0, /* sq_length */
0, /* sq_concat */
0, /* sq_repeat */
0, /* sq_item */
0, /* sq_slice */
0, /* sq_ass_item */
0, /* sq_ass_slice */
(objobjproc)context_tp_contains, /* sq_contains */
0, /* sq_inplace_concat */
0, /* sq_inplace_repeat */
.sq_contains = context_tp_contains
};

static PyMappingMethods PyContext_as_mapping = {
(lenfunc)context_tp_len, /* mp_length */
(binaryfunc)context_tp_subscript, /* mp_subscript */
.mp_length = context_tp_len,
.mp_subscript = context_tp_subscript
};

PyTypeObject PyContext_Type = {
Expand All @@ -763,13 +768,13 @@ PyTypeObject PyContext_Type = {
.tp_methods = PyContext_methods,
.tp_as_mapping = &PyContext_as_mapping,
.tp_as_sequence = &PyContext_as_sequence,
.tp_iter = (getiterfunc)context_tp_iter,
.tp_dealloc = (destructor)context_tp_dealloc,
.tp_iter = context_tp_iter,
.tp_dealloc = context_tp_dealloc,
.tp_getattro = PyObject_GenericGetAttr,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_richcompare = context_tp_richcompare,
.tp_traverse = (traverseproc)context_tp_traverse,
.tp_clear = (inquiry)context_tp_clear,
.tp_traverse = context_tp_traverse,
.tp_clear = context_tp_clear,
.tp_new = context_tp_new,
.tp_weaklistoffset = offsetof(PyContext, ctx_weakreflist),
.tp_hash = PyObject_HashNotImplemented,
Expand Down