Skip to content

Fix object_setClass with hidden classes. - #442

Merged
davidchisnall merged 2 commits into
masterfrom
fix-set-class-with-hidden-classes
Nov 17, 2026
Merged

davidchisnall merged 2 commits into
masterfrom
fix-set-class-with-hidden-classes

Conversation

@davidchisnall

Copy link
Copy Markdown
Member

The original implementation just pivoted the isa pointer. This causes hidden classes to leak and also causes any state associated with them (in particular, things like associated objects and @synchronise locks) to lose their attachment to the object. The correct thing is to change the superclass of the hidden class to be the new class. This is a bit more annoying because it also requires recomputing dtables and so on.

Fixes #441

The original implementation just pivoted the `isa` pointer.  This causes
hidden classes to leak and also causes any state associated with them
(in particular, things like associated objects and `@synchronise` locks)
to lose their attachment to the object.  The correct thing is to change
the superclass of the hidden class to be the new class.  This is a bit
more annoying because it also requires recomputing dtables and so on.

Fixes #441
Ubuntu got upgraded and embeds version numbers in some of the
cross-compile packages.  The newer images also have newer LLVM versions
(and don't have the older ones).
@davidchisnall
davidchisnall force-pushed the fix-set-class-with-hidden-classes branch from 55708d2 to 959408f Compare November 17, 2026 14:11
@davidchisnall
davidchisnall merged commit 762f9d7 into master Nov 17, 2026
93 checks passed
@davidchisnall
davidchisnall deleted the fix-set-class-with-hidden-classes branch November 17, 2026 19:51
@triplef

triplef commented Nov 18, 2026

Copy link
Copy Markdown
Member

This seems to be causing multiple test failures in libs-base: https://github.com/gnustep/libs-base/actions/runs/44330864867

I think the issue is that hidden classes share their superclass's metaclass, so class_setSuperclass() ends up changing the metaclass of the object's real class. With zombies enabled, once an object that’s KVO-observed or has associated objects gets deallocated, class methods on its class go to NSZombie (-[??? new]: message sent to deallocated instance).

The AssociateSetClass.m test reproduces the issue (stack overflow) if the object is sent a message before object_setClass():

diff --git a/Test/AssociateSetClass.m b/Test/AssociateSetClass.m
index bd11450..acbdf27 100644
--- a/Test/AssociateSetClass.m
+++ b/Test/AssociateSetClass.m
@@ -5,9 +5,13 @@ BOOL deallocCalled = NO;
 static char key;

 @interface Observed : Test
+- (void)ping;
 @end

 @implementation Observed
+- (void)ping
+{
+}
 @end

 @interface Associated : Test
@@ -32,15 +36,20 @@ int main(int argc, const char * argv[]) {
                objc_setAssociatedObject(object, &key, value, OBJC_ASSOCIATION_RETAIN);
                [value release];
                assert(objc_getAssociatedObject(object, &key) == value);
+               [object ping];

                object_setClass(object, subclass);
                assert(object_getClass(object) == subclass);
+               assert(class_getSuperclass(object_getClass([Observed class])) ==
+                      object_getClass([Test class]));

                fprintf(stderr, "Association after object_setClass: %p (expected %p)\n",
                           objc_getAssociatedObject(object, &key), value);
                assert(objc_getAssociatedObject(object, &key) == value);

                object_setClass(object, [Observed class]);
+               assert(class_getSuperclass(object_getClass([Observed class])) ==
+                      object_getClass([Test class]));
                fprintf(stderr, "Association after object_setClass: %p (expected %p)\n",
                           objc_getAssociatedObject(object, &key), value);
                assert(objc_getAssociatedObject(object, &key) == value);

Do we need to set the isa to the new superclass's metaclass when the class is a hidden class in class_setSuperclass(), and don't touch the metaclass it shares with the object's real class or that metaclass's dtable?

@rfm

rfm commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

As a temporary workaround for this object_setClass() issue, I modified base to copy the class pointer directly to the address of the start of the instance. Ugly/fragile, but I guess it will do until object_setClass() is working or we have some other alternative.

@davidchisnall

Copy link
Copy Markdown
Member Author

I'm still recovering from a cold and my head is too full of cotton wool to fix this. Happy to review a PR if someone else wants to try though...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

object_setClass() drops an object's associated objects

3 participants