-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[BUG] Py_TPFLAGS_HEAPTYPE is set on static PyTypeObject #4200
Comments
From the version you report, I think you have the fix in #3603, but it's worth checking that you do? One of the suggestions on that issue (that we didn't take) was to front-pad our non-heap classes to give them space that's safe to overwrite - re-visiting that might be a solution. We could disable |
Thanks for the link to the other related bug and looking into my issue. |
The main thing that Cython would need to know to implement any special-cases is a macro to test (e.g. |
Pyston has version macros
|
In which case, @undingen have a look at https://github.com/da-woods/cython/tree/pyston-tpflags and see if it fixes most of your issues. I still thing longer-term a better solution might be useful though. |
Thanks for your workaround I can confirm that it solves the crashes. It may be a good workaround for pyston 2.2.0 but for upcoming version I would like to be able to support also the case with mixed static and heap allocated classes. I still think that in general setting the flag is wrong. (myenv) marius@antidote:~/tmp/ext$ cat helloworld.pyx
cdef class MyClass:
def __init__(self):
pass
(myenv) marius@antidote:~/tmp/ext$ cat setup.py
from setuptools import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("helloworld.pyx")
) python 3.10b: >>> import helloworld
>>> C = helloworld.MyClass
>>> C
<class 'helloworld.MyClass'>
>>> C.__name__ = "bla"
>>> C
<class 'bla'> python 3.8.5: >>> import helloworld
>>> C = helloworld.MyClass
>>> C
<class 'helloworld.MyClass'>
>>> C.__name__ = "bla"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'helloworld.MyClass' Concerning the pyston special casing I noticed that cython contains support for pyston v1, I will put up PR to remove the support because it's dead and v2 should not require (that much) special casing so it's just a unnecessary maintenance burden on you folks. |
Does Pyston have something like |
Looking at the Cython code I spoted a few areas where it contains special paths for Pyston v1. Pyston v1 only supports python2 and is dead but we did release a new Pyston v2 which is a fork of CPython 3.8. We do not set PYSTON_VERSION in v2 so the current code can't trigger. I hoped we would not need any special casing for v2 but cython#4200 may temporarily need one but I think the current special casing is overkill and if neccessary I can add it back later on.
Looking at the Cython code I spotted a few areas where it contains special paths for Pyston v1. Pyston v1 only supports python2 and is dead but we did release a new Pyston v2 which is a fork of CPython 3.8. As we don't currently set PYSTON_VERSION in v2 the current code can't trigger and can be removed. I hoped we would not need any special casing for v2 because it should be fully compatibile with CPython but cython#4200 may temporarily need one but I think the current code is overkill and if necessary I can add (part of) it back later on.
Looking at the Cython code I spotted a few areas where it contains special paths for Pyston v1. Pyston v1 only supports python2 and is dead but we did release a new Pyston v2 which is a fork of CPython 3.8. As we don't currently set PYSTON_VERSION in v2 the current code can't trigger and can be removed. I hoped we would not need any special casing for v2 because it should be fully compatible with CPython but cython#4200 may temporarily need one but I think the current code is overkill and if necessary I can add (part of) it back later on.
Looking at the Cython code I spotted a few areas where it contains special paths for Pyston v1. Pyston v1 only supports python2 and is dead but we did release a new Pyston v2 which is a fork of CPython 3.8. As we don't currently set PYSTON_VERSION in v2 the current code can't trigger and can be removed. I hoped we would not need any special casing for v2 because it should be fully compatible with CPython but cython#4200 may temporarily need one but I think the current code is overkill and if necessary I can add (part of) it back later on.
There is unfortunately no define which contains all the version numbers :/. I created a new PR which removes the old pyston v1 special casing. |
Yeah, it's clearly a pretty awful hack. However, I'm not sure there's a better way to persuade CPython to allow us to do multiple inheritance. If there was a good way of getting the feature without the flag then we'd do it!
That's possibly something we should fix. Although it's possibly not a huge moral problem whether types have mutable attributes or not - I don't think it's something that Cython has intentionally designed for, and it may well change with |
The check that this is a heap type in CPython in this particular case is overly strict; another workaround would be to relax that in Pyston and elide this hack altogether there. |
Looking at the Cython code I spotted a few areas where it contains special paths for Pyston v1. Pyston v1 only supports python2 and is dead but we did release a new Pyston v2 which is a fork of CPython 3.8. As we don't currently set `PYSTON_VERSION` in v2 the current code can't trigger and can be removed. I hoped we would not need any special casing for v2 because it should be fully compatible with CPython but #4200 may temporarily need one but I think the current code is overkill and if necessary I can add (part of) it back later on.
@da-woods Whats the next step to get your fix for pyston into cython? Should I create a new pull request with your fix or are you going to commit it? |
See #4200 Signed-off-by: Stefan Behnel <stefan_ml@behnel.de>
I committed the change in b19b8d8 |
I think we can close this ticket, right? |
Yes I agree thanks for committing the fix! |
…ranch fixes an issue which pyston triggers cython#4200
I'm working on the pyston python implementation (pyston v2 is mostly a cpython 3.8 fork and pretends to be cpython to cython) and some users notified us that they run into issues using gevent with pyston.
I tracked it down to a memory corruption issue/out of bounds write generated by the cythonized code in connection with ours.
It's caused by cython setting
![image](https://user-images.githubusercontent.com/7397630/120027786-1b8bbc00-bff4-11eb-8f91-69b5e6482c4f.png)
Py_TPFLAGS_HEAPTYPE
temporarily when calling__Pyx_PyType_Ready
:cython/Cython/Utility/ExtensionTypes.c
Line 118 in 6889482
Pyston assumes (because
Py_TPFLAGS_HEAPTYPE
is set) that it got a largerPyHeapTypeObject
and will clear some fields (which overwrite some random memory which causes the gevent bug)https://github.com/pyston/pyston/blob/832ec0d76facb2e1e958324c7c1ce2c3879283ca/Objects/typeobject.c#L281
Maybe we can workaround the issue in pyston but in general I think setting this flag could be dangerous for cpython too it seems to check for
Py_TPFLAGS_HEAPTYPE
several times and the cython comment about it maybe outdated.What do you think?
To Reproduce
Code to reproduce the behaviour:
Expected behavior
Py_TPFLAGS_HEAPTYPE
is only set if thePyTypeObject
is a subclass ofPyHeapTypeObject
.Environment (please complete the following information):
Additional context
Pyston bug report: pyston/pyston#42
The text was updated successfully, but these errors were encountered: