Repository navigation
[mypyc] Fix attribute offsets when only a subclass defines __call__ - #22483
Merged
p-sawicki merged 1 commit intoNov 20, 2026
Merged
Conversation
The object struct of a native class with a __call__ method has a vectorcall field. It was placed before the attributes, and since __call__ is inherited, a subclass that defines __call__ when its base class doesn't had all inherited attributes at different offsets than the base class. Code compiled for the base class then read and wrote the wrong fields, e.g. the base class __init__ stored an attribute in the vectorcall field of the subclass instance, and calling the instance crashed. Put the vectorcall field after the attributes instead. It's only accessed through the struct of the class itself and through tp_vectorcall_offset, which is set for each class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The object struct of a native class that has a
__call__method has avectorcallfield (used throughtp_vectorcall_offset). It was placed right aftervtable, before the attributes. A class inherits__call__from its bases, so if only a subclass defines it, the subclass struct has the extra field while the base class struct doesn't. All attributes inherited from the base class were then at different offsets in the subclass, and code compiled for the base class (methods, attribute access through the base type) read and wrote the wrong fields. This is a segfault or memory corruption:Generated struct of
B:A.__init__(compiled for the struct ofA) storesxin thevectorcallfield of theBinstance, so calling it jumps to the address of astrobject. With more attributes, base class methods and subclass code also see each other's attributes in the wrong fields.Now the
vectorcallfield goes after the attributes, so the attributes of a base class are at the same offsets in all subclasses. The field is only accessed through the struct of the class itself (in its setup function, which isn't shared with native subclasses) and throughtp_vectorcall_offset, which is set for each class, so it's fine for its offset to differ between a class and its subclasses. This also works with separate compilation, since each module computes the layout from the class IR in the same way.Classes where the base class also defines
__call__were not affected, and neither were classes without attributes.Tests: a run test with
__call__defined only in a subclass, accessing inherited attributes through base class methods, directly and throughAny;__call__overridden in a sub-subclass; an intermediate class with attributes; a base class with weak reference support; and interpreted subclasses (one inheriting__call__, one defining it). A multi-module test has the base class in another module (another group in separate compilation mode). The new run test fails on master.