You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make Argument Clinic aware of possible UBSan failures #128180
A huge part of the CPython code base uses Argument Clinic for generating methods for classes or standalone functions. Generally, methods are then used as follows:
Now, in order to fix #111178, we generally convert non-AC clinic methods by changing FooObject * to PyObject * and doing a pointer cast in the function body. See #111178 for a wider discussion. The issue is that we cannot easily do the same for AC-generated code since the prototype is part of the auto-generated block.
We can do it if we specify self: self(type="PyObject *") instead of not specifying self in the AC input, but this is an overkill. Instead, AC should always generate two functions: an implementation function which takes a typed object (i.e., FooObject) and a public dispatcher which takes a PyObject *self and possibly unused arguments. Stated otherwise:
I decided to open a separate issue to track this change orthogonally to the UBSan failures fixes that can be achieved without any AC changes. I hope that this will not deterioriate the performances of CPython too much.
Note: this does not only affect methods as above; this affects any method that is casted to PyCFunction or anything else with incompatible function pointer signatures.
Took a stab at this. For simple cases, all that needs to change is this property to always return PyObject *, and then the functionality to cast it is already implemented:
We'll still need to change some other things though, because some generated clinic code relies on specific structures (e.g. fields in PyTypeObject *). More importantly, we'll have to manually go through and audit where we need to change casts for areas that call clinic funtions manually, typically for locking. For example, I did this with the SSL thread safety fix:
if (_ssl__SSLSocket_owner_set(self, owner, NULL) ==-1) {
Here, self is a PySSLSocket * rather than a PyObject *, so changing that would break this case. We'll probably need to deal with that on an agonizing case-by-case basis.
I'm not afraid of doing the case-by-case, considering this is what I've been doing for the rest. But thanks for digging into this, I haven't looked at clinic a lot!
Well... I forgot about that but Victor completed it in #130682, #131102, #131157, #131159, and I've completed some in #131605. Since I've marked as part of gh-111178, I'll just close this issue.
Activity
picnixz commentedon Dec 26, 2024
cc @ZeroIntensity
ZeroIntensity commentedon Dec 29, 2024
Took a stab at this. For simple cases, all that needs to change is this property to always return
PyObject *, and then the functionality to cast it is already implemented:cpython/Tools/clinic/libclinic/converters.py
Lines 1183 to 1188 in f9a5a3a
We'll still need to change some other things though, because some generated clinic code relies on specific structures (e.g. fields in
PyTypeObject *). More importantly, we'll have to manually go through and audit where we need to change casts for areas that call clinic funtions manually, typically for locking. For example, I did this with the SSL thread safety fix:cpython/Modules/_ssl.c
Line 937 in f9a5a3a
Here,
selfis aPySSLSocket *rather than aPyObject *, so changing that would break this case. We'll probably need to deal with that on an agonizing case-by-case basis.picnixz commentedon Dec 29, 2024
I'm not afraid of doing the case-by-case, considering this is what I've been doing for the rest. But thanks for digging into this, I haven't looked at clinic a lot!
picnixz commentedon Mar 24, 2025
Well... I forgot about that but Victor completed it in #130682, #131102, #131157, #131159, and I've completed some in #131605. Since I've marked as part of gh-111178, I'll just close this issue.