diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fb1f40bb..4c071fc2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -84,7 +84,7 @@ jobs: strategy: matrix: build-type: [ Release, Debug ] - llvm-version: [16, 17, 18] + llvm-version: [20, 21, 22] arch: - name: armhf system-processor: arm @@ -118,7 +118,7 @@ jobs: - name: Install cross-compile toolchain and QEMU run: | sudo apt update - sudo apt install libstdc++-9-dev-${{ matrix.arch.name }}-cross qemu-user ninja-build + sudo apt install libstdc++-11-dev-${{ matrix.arch.name }}-cross qemu-user ninja-build - name: Configure CMake run: | export LDFLAGS="-L/usr/lib/llvm-${{ matrix.llvm-version }}/lib/ -fuse-ld=lld-${{ matrix.llvm-version}} -Wl,--dynamic-linker=/usr/${{ matrix.arch.triple }}/lib/${{ matrix.arch.rtld }},-rpath,/usr/${{ matrix.arch.triple }}/lib" diff --git a/Test/AssociateSetClass.m b/Test/AssociateSetClass.m new file mode 100644 index 00000000..bd114500 --- /dev/null +++ b/Test/AssociateSetClass.m @@ -0,0 +1,52 @@ +#include "Test.h" +#include + +BOOL deallocCalled = NO; +static char key; + +@interface Observed : Test +@end + +@implementation Observed +@end + +@interface Associated : Test +@end + +@implementation Associated +-(void) dealloc +{ + deallocCalled = YES; + [super dealloc]; +} +@end + +int main(int argc, const char * argv[]) { + Class subclass = objc_allocateClassPair([Observed class], "ObservedSubclass", 0); + objc_registerClassPair(subclass); + + @autoreleasepool + { + Observed *object = [Observed new]; + Associated *value = [Associated new]; + objc_setAssociatedObject(object, &key, value, OBJC_ASSOCIATION_RETAIN); + [value release]; + assert(objc_getAssociatedObject(object, &key) == value); + + object_setClass(object, subclass); + assert(object_getClass(object) == subclass); + + 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]); + fprintf(stderr, "Association after object_setClass: %p (expected %p)\n", + objc_getAssociatedObject(object, &key), value); + assert(objc_getAssociatedObject(object, &key) == value); + + [object release]; + } + assert(deallocCalled); + return 0; +} diff --git a/Test/CMakeLists.txt b/Test/CMakeLists.txt index f2c0544c..b1e8580b 100644 --- a/Test/CMakeLists.txt +++ b/Test/CMakeLists.txt @@ -17,6 +17,7 @@ set(TESTS AllocatePair.m AssociatedObject.m AssociatedObject2.m + AssociateSetClass.m BlockCapture_arc.m BlockTest_arc.m ConstantString.m diff --git a/associate.mm b/associate.mm index 7c1a3943..b4f2d547 100644 --- a/associate.mm +++ b/associate.mm @@ -455,3 +455,33 @@ id object_getPrototype_np(id object) { return objc_getAssociatedObject(object, &prototypeKey); } + +Class object_setClass(id object, Class cls) +{ + if (object == nil) + { + return Nil; + } + // If this is a small object, then don't set its class. + if (isSmallObject(object)) + { + return classForObject(object); + } + auto guard = acquire_locks_for_pointers(object); + LOCK_RUNTIME_FOR_SCOPE(); + Class oldClass = object->isa; + // If this object has a hidden class, then we are not going to change the + // isa pointer. Instead, we are going to pivot the superclass of that + // hidden class to the new superclass. This is more complex because it + // requires updating the list is subclasses of the parent and then + // recomputing our dtable. + if (Class hiddenClass = findHiddenClass(object)) + { + oldClass = class_setSuperclass(hiddenClass, cls); + } + else + { + object->isa = cls; + } + return oldClass; +} diff --git a/objc/runtime.h b/objc/runtime.h index c9a0ba63..eaf42f27 100644 --- a/objc/runtime.h +++ b/objc/runtime.h @@ -465,7 +465,10 @@ void class_setIvarLayout(Class cls, const uint8_t *layout); * because modifying the superclass of a class at run time is a very complex * operation and this function is almost always used incorrectly. */ -OBJC_PUBLIC __attribute__((deprecated)) +OBJC_PUBLIC +#ifndef __OBJC_RUNTIME_INTERNAL__ +__attribute__((deprecated)) +#endif Class class_setSuperclass(Class cls, Class newSuper); OBJC_PUBLIC OBJC_GNUSTEP_RUNTIME_UNSUPPORTED("Weak instance variables") diff --git a/runtime.c b/runtime.c index 58282c24..da21fe76 100644 --- a/runtime.c +++ b/runtime.c @@ -509,7 +509,6 @@ void class_setIvarLayout(Class cls, const uint8_t *layout) memcpy(cls->ivars, list, listsize); } -__attribute__((deprecated)) Class class_setSuperclass(Class cls, Class newSuper) { CHECK_ARG(cls); @@ -540,17 +539,23 @@ Class class_setSuperclass(Class cls, Class newSuper) } else { - Class meta = cls->isa, newSuperMeta = newSuper->isa; - // Update the metaclass's superclass. - safe_remove_from_subclass_list(meta); - objc_resolve_class(newSuperMeta); + Class meta = cls->isa; + Class newSuperMeta = newSuper->isa; + // The old and new metaclasses can be the same. If we replace the + if (meta != newSuperMeta) + { + // Update the metaclass's superclass. + safe_remove_from_subclass_list(meta); + objc_resolve_class(newSuperMeta); - meta->super_class = newSuperMeta; - meta->isa = newSuperMeta->isa; + meta->super_class = newSuperMeta; + assert(meta != newSuperMeta); + meta->isa = newSuperMeta->isa; - // The super class's subclass list is used in certain method resolution scenarios. - meta->sibling_class = newSuperMeta->subclass_list; - newSuperMeta->subclass_list = meta; + // The super class's subclass list is used in certain method resolution scenarios. + meta->sibling_class = newSuperMeta->subclass_list; + newSuperMeta->subclass_list = meta; + } } LOCK_FOR_SCOPE(&initialize_lock); @@ -815,16 +820,6 @@ Class object_getClass(id obj) return isa; } -Class object_setClass(id obj, Class cls) -{ - CHECK_ARG(obj); - // If this is a small object, then don't set its class. - if (isSmallObject(obj)) { return classForObject(obj); } - Class oldClass = obj->isa; - obj->isa = cls; - return oldClass; -} - const char *object_getClassName(id obj) { CHECK_ARG(obj);