From 4d6225bbe3497657e0a393e6b55fc63b067c1b61 Mon Sep 17 00:00:00 2001 From: Ariel Abreu Date: Tue, 9 Jun 2020 10:23:25 -0400 Subject: [PATCH] objc4-781.2 --- interposable.txt | 1 + libobjc.order | 7 +- markgc.cpp | 22 +- objc.sln | 0 objc.suo | Bin 24576 -> 0 bytes objc.xcodeproj/project.pbxproj | 416 +- objcrt/objcrt.vcproj | 0 prebuild.bat | 0 runtime/DenseMapExtras.h | 86 + runtime/Messengers.subproj/objc-msg-arm.S | 258 +- runtime/Messengers.subproj/objc-msg-arm64.S | 390 +- runtime/Messengers.subproj/objc-msg-i386.S | 117 +- .../objc-msg-simulator-i386.S | 226 +- .../objc-msg-simulator-x86_64.S | 220 +- runtime/Messengers.subproj/objc-msg-win32.m | 15 +- runtime/Messengers.subproj/objc-msg-x86_64.S | 382 +- runtime/Module/ObjectiveC.apinotes | 437 ++ runtime/Module/module.modulemap | 40 + runtime/NSObjCRuntime.h | 2 +- runtime/NSObject-internal.h | 161 + runtime/NSObject.h | 13 +- runtime/NSObject.mm | 756 +-- runtime/Object.h | 3 +- runtime/Object.mm | 8 - runtime/PointerUnion.h | 182 + runtime/Protocol.h | 16 +- runtime/Protocol.mm | 16 +- runtime/a1a2-blocktramps-arm.S | 148 - runtime/a1a2-blocktramps-i386.S | 566 --- runtime/a1a2-blocktramps-x86_64.S | 564 --- runtime/a2a3-blocktramps-arm.S | 148 - runtime/a2a3-blocktramps-i386.S | 566 --- runtime/a2a3-blocktramps-x86_64.S | 565 --- runtime/arm64-asm.h | 174 + runtime/hashtable2.h | 149 +- runtime/hashtable2.mm | 4 +- runtime/include/objc/NSObjCRuntime.h | 1 - runtime/include/objc/NSObject.h | 1 - runtime/include/objc/Object.h | 1 - runtime/include/objc/Protocol.h | 1 - runtime/include/objc/hashtable.h | 1 - runtime/include/objc/hashtable2.h | 1 - runtime/include/objc/maptable.h | 1 - runtime/include/objc/message.h | 1 - runtime/include/objc/objc-api.h | 1 - runtime/include/objc/objc-auto.h | 1 - runtime/include/objc/objc-class.h | 1 - runtime/include/objc/objc-exception.h | 1 - runtime/include/objc/objc-internal.h | 1 - runtime/include/objc/objc-load.h | 1 - runtime/include/objc/objc-runtime.h | 1 - runtime/include/objc/objc-sync.h | 1 - runtime/include/objc/objc.h | 1 - runtime/include/objc/runtime.h | 1 - runtime/isa.h | 134 + runtime/llvm-DenseMap.h | 775 +-- runtime/llvm-DenseMapInfo.h | 16 + runtime/llvm-DenseSet.h | 293 ++ runtime/llvm-type_traits.h | 351 +- runtime/maptable.h | 84 +- runtime/maptable.mm | 102 +- runtime/message.h | 167 +- runtime/objc-abi.h | 339 +- runtime/objc-accessors.mm | 7 +- runtime/objc-api.h | 108 +- runtime/objc-auto.h | 73 +- runtime/objc-auto.mm | 73 +- runtime/objc-block-trampolines.h | 71 + runtime/objc-block-trampolines.mm | 627 ++- runtime/objc-blocktramps-arm.S | 282 ++ ...ramps-arm64.S => objc-blocktramps-arm64.S} | 48 +- runtime/objc-blocktramps-i386.S | 1102 +++++ runtime/objc-blocktramps-x86_64.S | 1097 +++++ runtime/objc-cache-old.h | 1 + runtime/objc-cache-old.mm | 22 +- runtime/objc-cache.h | 2 + runtime/objc-cache.mm | 541 ++- runtime/objc-class-old.mm | 238 +- runtime/objc-class.mm | 211 +- runtime/objc-config.h | 72 +- runtime/objc-env.h | 7 +- runtime/objc-errors.mm | 14 +- runtime/objc-exception.h | 126 +- runtime/objc-exception.mm | 164 +- runtime/objc-file-old.h | 4 +- runtime/objc-file-old.mm | 2 +- runtime/objc-file.h | 68 +- runtime/objc-file.mm | 58 +- runtime/objc-gdb.h | 196 +- runtime/objc-initialize.h | 2 +- runtime/objc-initialize.mm | 262 +- runtime/objc-internal.h | 687 ++- runtime/objc-layout.mm | 4 +- runtime/objc-load.h | 21 +- runtime/objc-loadmethod.mm | 4 +- runtime/objc-lockdebug.h | 71 +- runtime/objc-lockdebug.mm | 499 +- runtime/objc-locks-new.h | 38 + runtime/objc-locks-old.h | 40 + runtime/objc-locks.h | 68 + runtime/objc-object.h | 123 +- runtime/objc-opt.mm | 137 +- runtime/objc-os.h | 534 +- runtime/objc-os.mm | 335 +- runtime/objc-private.h | 461 +- runtime/objc-ptrauth.h | 80 + runtime/objc-references.h | 5 +- runtime/objc-references.mm | 434 +- runtime/objc-runtime-new.h | 1295 +++-- runtime/objc-runtime-new.mm | 4305 +++++++++++------ runtime/objc-runtime-old.h | 29 +- runtime/objc-runtime-old.mm | 142 +- runtime/objc-runtime.mm | 323 +- runtime/objc-sel-old.mm | 62 +- runtime/objc-sel-set.mm | 2 +- runtime/objc-sel-table.S | 36 +- runtime/objc-sel.mm | 130 +- runtime/objc-sync.h | 28 +- runtime/objc-sync.mm | 37 +- runtime/objc-weak.mm | 12 +- runtime/objc-zalloc.h | 125 + runtime/objc-zalloc.mm | 125 + runtime/objc.h | 88 +- runtime/runtime.h | 1024 ++-- test/ARCBase.h | 21 + test/ARCBase.m | 30 + test/ARCLayouts.m | 217 + test/ARCLayoutsWithoutWeak.m | 12 + test/ARCMRC.h | 13 + test/ARCMRC.m | 11 + test/MRCARC.h | 13 + test/MRCARC.m | 11 + test/MRCBase.h | 28 + test/MRCBase.m | 46 + test/accessors.m | 79 + test/accessors2.m | 143 + test/addMethod.m | 121 + test/addMethods.m | 324 ++ test/addProtocol.m | 255 + test/applescriptobjc.m | 13 + test/arr-cast.m | 20 + test/arr-weak.m | 329 ++ test/asm-placeholder.s | 47 + test/association-cf.m | 40 + test/association.m | 127 + test/associationForbidden.h | 50 + test/associationForbidden.m | 16 + test/associationForbidden2.m | 19 + test/associationForbidden3.m | 21 + test/associationForbidden4.m | 18 + test/atomicProperty.mm | 41 + test/badAltHandler.m | 81 + test/badCache.m | 116 + test/badPool.m | 34 + test/badPoolCompat-ios-tvos.m | 14 + test/badPoolCompat-macos.m | 14 + test/badPoolCompat-watchos.m | 14 + test/badSuperclass.m | 37 + test/badSuperclass2.m | 13 + test/badTagClass.m | 48 + test/badTagIndex.m | 33 + test/bigrc.m | 139 + test/blocksAsImps.m | 252 + test/bool.c | 45 + test/cacheflush.h | 7 + test/cacheflush.m | 61 + test/cacheflush0.m | 7 + test/cacheflush2.m | 6 + test/cacheflush3.m | 6 + test/category.m | 229 + test/cdtors.mm | 306 ++ test/classgetclass.m | 17 + test/classname.m | 39 + test/classpair.mm | 319 ++ test/classversion.m | 19 + test/concurrentcat.m | 127 + test/concurrentcat_category.m | 70 + test/copyFixupHandler.mm | 89 + test/copyIvarList.m | 115 + test/copyMethodList.m | 155 + test/copyPropertyList.m | 167 + test/copyProtocolList.m | 69 + test/createInstance.m | 66 + test/customrr-cat1.m | 7 + test/customrr-cat2.m | 7 + test/customrr-nsobject-awz.m | 16 + test/customrr-nsobject-core.m | 17 + test/customrr-nsobject-none.m | 15 + test/customrr-nsobject-rr.m | 16 + test/customrr-nsobject-rrawz.m | 17 + test/customrr-nsobject.m | 231 + test/customrr.m | 915 ++++ test/customrr2.m | 9 + test/definitions.c | 54 + test/designatedinit.m | 26 + test/duplicateClass.m | 150 + test/duplicateProtocols.m | 42 + test/duplicatedClasses.m | 26 + test/evil-class-def.m | 321 ++ test/exc.m | 879 ++++ test/exchangeImp.m | 106 + test/foreach.m | 227 + test/fork.m | 54 + test/forkInitialize.m | 159 + test/forkInitializeDisabled.m | 21 + test/forkInitializeSingleThreaded.m | 8 + test/forward.m | 543 +++ test/forwardDefault.m | 24 + test/forwardDefaultStret.m | 24 + test/future.h | 15 + test/future.m | 82 + test/future0.m | 2 + test/future2.m | 17 + test/gc-main.m | 10 + test/gc.c | 1 + test/gc.m | 8 + test/gcenforcer-app-aso.m | 12 + test/gcenforcer-app-gc.m | 14 + test/gcenforcer-app-gcaso.m | 14 + test/gcenforcer-app-gcaso2.m | 14 + test/gcenforcer-app-gconly.m | 14 + test/gcenforcer-app-nogc.m | 12 + test/gcenforcer-app-noobjc.m | 12 + test/gcenforcer-dylib-nogc.m | 11 + test/gcenforcer-dylib-noobjc.m | 9 + test/gcenforcer-dylib-requiresgc.m | 22 + test/gcenforcer-dylib-supportsgc.m | 9 + test/gcenforcer-preflight.m | 88 + test/gcfiles/evil1 | Bin 0 -> 441 bytes test/gcfiles/i386-aso | Bin 0 -> 12624 bytes test/gcfiles/i386-aso--x86_64-aso | Bin 0 -> 29060 bytes test/gcfiles/i386-broken | Bin 0 -> 12608 bytes test/gcfiles/i386-broken--x86_64-gc | Bin 0 -> 29056 bytes test/gcfiles/i386-broken--x86_64-nogc | Bin 0 -> 29056 bytes test/gcfiles/i386-gc | Bin 0 -> 12608 bytes test/gcfiles/i386-gc--x86_64-broken | Bin 0 -> 29056 bytes test/gcfiles/i386-gc--x86_64-gc | Bin 0 -> 29056 bytes test/gcfiles/i386-gc--x86_64-nogc | Bin 0 -> 29056 bytes test/gcfiles/i386-gcaso | Bin 0 -> 12716 bytes test/gcfiles/i386-gcaso2 | Bin 0 -> 12644 bytes test/gcfiles/i386-gconly | Bin 0 -> 12608 bytes test/gcfiles/i386-nogc | Bin 0 -> 12608 bytes test/gcfiles/i386-nogc--x86_64-broken | Bin 0 -> 29056 bytes test/gcfiles/i386-nogc--x86_64-gc | Bin 0 -> 29056 bytes test/gcfiles/i386-nogc--x86_64-nogc | Bin 0 -> 29056 bytes test/gcfiles/i386-noobjc | Bin 0 -> 4228 bytes test/gcfiles/libnogc.dylib | Bin 0 -> 74696 bytes test/gcfiles/libnoobjc.dylib | Bin 0 -> 41640 bytes test/gcfiles/librequiresgc.dylib | Bin 0 -> 74696 bytes test/gcfiles/librequiresgc.fake.dylib | Bin 0 -> 74696 bytes test/gcfiles/libsupportsgc.dylib | Bin 0 -> 74696 bytes test/gcfiles/x86_64-aso | Bin 0 -> 8580 bytes test/gcfiles/x86_64-broken | Bin 0 -> 8576 bytes test/gcfiles/x86_64-gc | Bin 0 -> 8576 bytes test/gcfiles/x86_64-gcaso | Bin 0 -> 8920 bytes test/gcfiles/x86_64-gcaso2 | Bin 0 -> 8640 bytes test/gcfiles/x86_64-gconly | Bin 0 -> 8576 bytes test/gcfiles/x86_64-nogc | Bin 0 -> 8576 bytes test/gcfiles/x86_64-noobjc | Bin 0 -> 4248 bytes test/gdb.m | 44 + test/getClassHook.m | 128 + test/getImageNameHook.m | 78 + test/getMethod.m | 139 + test/get_task_allow_entitlement.plist | 8 + test/headers.c | 19 + test/headers.sh | 38 + test/imageorder.h | 20 + test/imageorder.m | 41 + test/imageorder1.m | 52 + test/imageorder2.m | 33 + test/imageorder3.m | 33 + test/imports.c | 40 + test/include-warnings.c | 21 + test/includes-objc2.c | 13 + test/includes.c | 47 + test/initialize.m | 323 ++ test/initializeVersusWeak.m | 187 + test/instanceSize.m | 59 + test/isaValidation.m | 267 + test/ismeta.m | 13 + test/ivar.m | 133 + test/ivarSlide.h | 110 + test/ivarSlide.m | 499 ++ test/ivarSlide1.m | 21 + test/literals.m | 55 + test/load-image-notification-dylib.m | 7 + test/load-image-notification.m | 76 + test/load-noobjc.m | 38 + test/load-noobjc2.m | 13 + test/load-noobjc3.m | 9 + test/load-order.m | 18 + test/load-order1.m | 15 + test/load-order2.m | 15 + test/load-order3.m | 12 + test/load-parallel.m | 63 + test/load-parallel0.m | 48 + test/load-parallel00.m | 2 + test/load-reentrant.m | 36 + test/load-reentrant2.m | 23 + test/load.m | 99 + test/methodArgs.m | 180 + test/methodCacheLeaks.m | 65 + test/methodListSize.m | 56 + test/msgSend-performance.m | 176 + test/msgSend.m | 2680 ++++++++++ test/nilAPIArgs.m | 18 + test/nonpointerisa.m | 262 + test/nopool.m | 49 + test/nscdtors.mm | 6 + test/nsexc.m | 7 + test/nsobject.m | 114 + test/nsprotocol.m | 17 + test/objectCopy.m | 38 + test/property.m | 157 + test/propertyDesc.m | 334 ++ test/protocol.m | 281 ++ test/protocol_copyMethodList.m | 154 + test/protocol_copyPropertyList.m | 207 + test/rawisa.m | 30 + test/readClassPair.m | 82 + test/realizedClassGenerationCount.m | 29 + test/release-workaround.m | 34 + test/resolve.m | 298 ++ test/restartableRangesSynchronizeStress.m | 78 + test/rr-autorelease-fast.m | 357 ++ test/rr-autorelease-fastarc.m | 204 + test/rr-autorelease-stacklogging.m | 13 + test/rr-autorelease.m | 9 + test/rr-autorelease2.m | 384 ++ test/rr-nsautorelease.m | 7 + test/rr-sidetable.m | 59 + test/runtime.m | 227 + test/sel.m | 21 + test/setAssociatedObjectHook.m | 47 + test/setSuper.m | 44 + test/subscripting.m | 139 + test/super.m | 21 + test/swift-class-def.m | 201 + test/swiftMetadataInitializer.m | 68 + test/swiftMetadataInitializerRealloc-dylib1.m | 64 + test/swiftMetadataInitializerRealloc-dylib2.m | 40 + test/swiftMetadataInitializerRealloc.m | 176 + test/synchronized-counter.m | 88 + test/synchronized-grid.m | 112 + test/synchronized.m | 114 + test/taggedNSPointers.m | 80 + test/taggedPointers.m | 356 ++ test/taggedPointersAllClasses.m | 86 + test/taggedPointersDisabled.m | 39 + test/taggedPointersTagObfuscationDisabled.m | 21 + test/tbi.c | 14 + test/test.h | 469 ++ test/test.pl | 1738 +++++++ test/testroot.i | 220 + test/timeout.pl | 9 + test/unload.h | 6 + test/unload.m | 180 + test/unload2.m | 42 + test/unload3.c | 10 + test/unload4.m | 7 + test/unwind.m | 81 + test/weak.h | 67 + test/weak.m | 316 ++ test/weak2.m | 82 + test/weakcopy.m | 62 + test/weakframework-missing.m | 14 + test/weakframework-not-missing.m | 11 + test/weakimport-missing.m | 13 + test/weakimport-not-missing.m | 11 + test/weakrace.m | 76 + test/willInitializeClassFunc.m | 109 + test/zone.m | 40 + version.bat | 0 373 files changed, 39788 insertions(+), 9791 deletions(-) create mode 100644 interposable.txt mode change 100755 => 100644 objc.sln delete mode 100755 objc.suo mode change 100755 => 100644 objcrt/objcrt.vcproj mode change 100755 => 100644 prebuild.bat create mode 100644 runtime/DenseMapExtras.h mode change 100755 => 100644 runtime/Messengers.subproj/objc-msg-arm64.S create mode 100644 runtime/Module/ObjectiveC.apinotes create mode 100644 runtime/Module/module.modulemap create mode 100644 runtime/NSObject-internal.h create mode 100644 runtime/PointerUnion.h delete mode 100644 runtime/a1a2-blocktramps-arm.S delete mode 100755 runtime/a1a2-blocktramps-i386.S delete mode 100755 runtime/a1a2-blocktramps-x86_64.S delete mode 100644 runtime/a2a3-blocktramps-arm.S delete mode 100755 runtime/a2a3-blocktramps-i386.S delete mode 100755 runtime/a2a3-blocktramps-x86_64.S create mode 100644 runtime/arm64-asm.h delete mode 120000 runtime/include/objc/NSObjCRuntime.h delete mode 120000 runtime/include/objc/NSObject.h delete mode 120000 runtime/include/objc/Object.h delete mode 120000 runtime/include/objc/Protocol.h delete mode 120000 runtime/include/objc/hashtable.h delete mode 120000 runtime/include/objc/hashtable2.h delete mode 120000 runtime/include/objc/maptable.h delete mode 120000 runtime/include/objc/message.h delete mode 120000 runtime/include/objc/objc-api.h delete mode 120000 runtime/include/objc/objc-auto.h delete mode 120000 runtime/include/objc/objc-class.h delete mode 120000 runtime/include/objc/objc-exception.h delete mode 120000 runtime/include/objc/objc-internal.h delete mode 120000 runtime/include/objc/objc-load.h delete mode 120000 runtime/include/objc/objc-runtime.h delete mode 120000 runtime/include/objc/objc-sync.h delete mode 120000 runtime/include/objc/objc.h delete mode 120000 runtime/include/objc/runtime.h create mode 100644 runtime/isa.h create mode 100644 runtime/llvm-DenseSet.h create mode 100644 runtime/objc-block-trampolines.h create mode 100644 runtime/objc-blocktramps-arm.S rename runtime/{a1a2-blocktramps-arm64.S => objc-blocktramps-arm64.S} (66%) create mode 100644 runtime/objc-blocktramps-i386.S create mode 100644 runtime/objc-blocktramps-x86_64.S create mode 100644 runtime/objc-locks-new.h create mode 100644 runtime/objc-locks-old.h create mode 100644 runtime/objc-locks.h create mode 100644 runtime/objc-ptrauth.h create mode 100644 runtime/objc-zalloc.h create mode 100644 runtime/objc-zalloc.mm create mode 100644 test/ARCBase.h create mode 100644 test/ARCBase.m create mode 100644 test/ARCLayouts.m create mode 100644 test/ARCLayoutsWithoutWeak.m create mode 100644 test/ARCMRC.h create mode 100644 test/ARCMRC.m create mode 100644 test/MRCARC.h create mode 100644 test/MRCARC.m create mode 100644 test/MRCBase.h create mode 100644 test/MRCBase.m create mode 100644 test/accessors.m create mode 100644 test/accessors2.m create mode 100644 test/addMethod.m create mode 100644 test/addMethods.m create mode 100644 test/addProtocol.m create mode 100644 test/applescriptobjc.m create mode 100644 test/arr-cast.m create mode 100644 test/arr-weak.m create mode 100644 test/asm-placeholder.s create mode 100644 test/association-cf.m create mode 100644 test/association.m create mode 100644 test/associationForbidden.h create mode 100644 test/associationForbidden.m create mode 100644 test/associationForbidden2.m create mode 100644 test/associationForbidden3.m create mode 100644 test/associationForbidden4.m create mode 100644 test/atomicProperty.mm create mode 100644 test/badAltHandler.m create mode 100644 test/badCache.m create mode 100644 test/badPool.m create mode 100644 test/badPoolCompat-ios-tvos.m create mode 100644 test/badPoolCompat-macos.m create mode 100644 test/badPoolCompat-watchos.m create mode 100644 test/badSuperclass.m create mode 100644 test/badSuperclass2.m create mode 100644 test/badTagClass.m create mode 100644 test/badTagIndex.m create mode 100644 test/bigrc.m create mode 100644 test/blocksAsImps.m create mode 100644 test/bool.c create mode 100644 test/cacheflush.h create mode 100644 test/cacheflush.m create mode 100644 test/cacheflush0.m create mode 100644 test/cacheflush2.m create mode 100644 test/cacheflush3.m create mode 100644 test/category.m create mode 100644 test/cdtors.mm create mode 100644 test/classgetclass.m create mode 100644 test/classname.m create mode 100644 test/classpair.mm create mode 100644 test/classversion.m create mode 100644 test/concurrentcat.m create mode 100644 test/concurrentcat_category.m create mode 100644 test/copyFixupHandler.mm create mode 100644 test/copyIvarList.m create mode 100644 test/copyMethodList.m create mode 100644 test/copyPropertyList.m create mode 100644 test/copyProtocolList.m create mode 100644 test/createInstance.m create mode 100644 test/customrr-cat1.m create mode 100644 test/customrr-cat2.m create mode 100644 test/customrr-nsobject-awz.m create mode 100644 test/customrr-nsobject-core.m create mode 100644 test/customrr-nsobject-none.m create mode 100644 test/customrr-nsobject-rr.m create mode 100644 test/customrr-nsobject-rrawz.m create mode 100644 test/customrr-nsobject.m create mode 100644 test/customrr.m create mode 100644 test/customrr2.m create mode 100644 test/definitions.c create mode 100644 test/designatedinit.m create mode 100644 test/duplicateClass.m create mode 100644 test/duplicateProtocols.m create mode 100644 test/duplicatedClasses.m create mode 100644 test/evil-class-def.m create mode 100644 test/exc.m create mode 100644 test/exchangeImp.m create mode 100644 test/foreach.m create mode 100644 test/fork.m create mode 100644 test/forkInitialize.m create mode 100644 test/forkInitializeDisabled.m create mode 100644 test/forkInitializeSingleThreaded.m create mode 100644 test/forward.m create mode 100644 test/forwardDefault.m create mode 100644 test/forwardDefaultStret.m create mode 100644 test/future.h create mode 100644 test/future.m create mode 100644 test/future0.m create mode 100644 test/future2.m create mode 100644 test/gc-main.m create mode 100644 test/gc.c create mode 100644 test/gc.m create mode 100644 test/gcenforcer-app-aso.m create mode 100644 test/gcenforcer-app-gc.m create mode 100644 test/gcenforcer-app-gcaso.m create mode 100644 test/gcenforcer-app-gcaso2.m create mode 100644 test/gcenforcer-app-gconly.m create mode 100644 test/gcenforcer-app-nogc.m create mode 100644 test/gcenforcer-app-noobjc.m create mode 100644 test/gcenforcer-dylib-nogc.m create mode 100644 test/gcenforcer-dylib-noobjc.m create mode 100644 test/gcenforcer-dylib-requiresgc.m create mode 100644 test/gcenforcer-dylib-supportsgc.m create mode 100644 test/gcenforcer-preflight.m create mode 100644 test/gcfiles/evil1 create mode 100644 test/gcfiles/i386-aso create mode 100644 test/gcfiles/i386-aso--x86_64-aso create mode 100644 test/gcfiles/i386-broken create mode 100644 test/gcfiles/i386-broken--x86_64-gc create mode 100644 test/gcfiles/i386-broken--x86_64-nogc create mode 100644 test/gcfiles/i386-gc create mode 100644 test/gcfiles/i386-gc--x86_64-broken create mode 100644 test/gcfiles/i386-gc--x86_64-gc create mode 100644 test/gcfiles/i386-gc--x86_64-nogc create mode 100644 test/gcfiles/i386-gcaso create mode 100644 test/gcfiles/i386-gcaso2 create mode 100644 test/gcfiles/i386-gconly create mode 100644 test/gcfiles/i386-nogc create mode 100644 test/gcfiles/i386-nogc--x86_64-broken create mode 100644 test/gcfiles/i386-nogc--x86_64-gc create mode 100644 test/gcfiles/i386-nogc--x86_64-nogc create mode 100644 test/gcfiles/i386-noobjc create mode 100644 test/gcfiles/libnogc.dylib create mode 100644 test/gcfiles/libnoobjc.dylib create mode 100644 test/gcfiles/librequiresgc.dylib create mode 100644 test/gcfiles/librequiresgc.fake.dylib create mode 100644 test/gcfiles/libsupportsgc.dylib create mode 100644 test/gcfiles/x86_64-aso create mode 100644 test/gcfiles/x86_64-broken create mode 100644 test/gcfiles/x86_64-gc create mode 100644 test/gcfiles/x86_64-gcaso create mode 100644 test/gcfiles/x86_64-gcaso2 create mode 100644 test/gcfiles/x86_64-gconly create mode 100644 test/gcfiles/x86_64-nogc create mode 100644 test/gcfiles/x86_64-noobjc create mode 100644 test/gdb.m create mode 100644 test/getClassHook.m create mode 100644 test/getImageNameHook.m create mode 100644 test/getMethod.m create mode 100644 test/get_task_allow_entitlement.plist create mode 100644 test/headers.c create mode 100644 test/headers.sh create mode 100644 test/imageorder.h create mode 100644 test/imageorder.m create mode 100644 test/imageorder1.m create mode 100644 test/imageorder2.m create mode 100644 test/imageorder3.m create mode 100644 test/imports.c create mode 100644 test/include-warnings.c create mode 100644 test/includes-objc2.c create mode 100644 test/includes.c create mode 100644 test/initialize.m create mode 100644 test/initializeVersusWeak.m create mode 100644 test/instanceSize.m create mode 100644 test/isaValidation.m create mode 100644 test/ismeta.m create mode 100644 test/ivar.m create mode 100644 test/ivarSlide.h create mode 100644 test/ivarSlide.m create mode 100644 test/ivarSlide1.m create mode 100644 test/literals.m create mode 100644 test/load-image-notification-dylib.m create mode 100644 test/load-image-notification.m create mode 100644 test/load-noobjc.m create mode 100644 test/load-noobjc2.m create mode 100644 test/load-noobjc3.m create mode 100644 test/load-order.m create mode 100644 test/load-order1.m create mode 100644 test/load-order2.m create mode 100644 test/load-order3.m create mode 100644 test/load-parallel.m create mode 100644 test/load-parallel0.m create mode 100644 test/load-parallel00.m create mode 100644 test/load-reentrant.m create mode 100644 test/load-reentrant2.m create mode 100644 test/load.m create mode 100644 test/methodArgs.m create mode 100644 test/methodCacheLeaks.m create mode 100644 test/methodListSize.m create mode 100644 test/msgSend-performance.m create mode 100644 test/msgSend.m create mode 100644 test/nilAPIArgs.m create mode 100644 test/nonpointerisa.m create mode 100644 test/nopool.m create mode 100644 test/nscdtors.mm create mode 100644 test/nsexc.m create mode 100644 test/nsobject.m create mode 100644 test/nsprotocol.m create mode 100644 test/objectCopy.m create mode 100644 test/property.m create mode 100644 test/propertyDesc.m create mode 100644 test/protocol.m create mode 100644 test/protocol_copyMethodList.m create mode 100644 test/protocol_copyPropertyList.m create mode 100644 test/rawisa.m create mode 100644 test/readClassPair.m create mode 100644 test/realizedClassGenerationCount.m create mode 100644 test/release-workaround.m create mode 100644 test/resolve.m create mode 100644 test/restartableRangesSynchronizeStress.m create mode 100644 test/rr-autorelease-fast.m create mode 100644 test/rr-autorelease-fastarc.m create mode 100644 test/rr-autorelease-stacklogging.m create mode 100644 test/rr-autorelease.m create mode 100644 test/rr-autorelease2.m create mode 100644 test/rr-nsautorelease.m create mode 100644 test/rr-sidetable.m create mode 100644 test/runtime.m create mode 100644 test/sel.m create mode 100644 test/setAssociatedObjectHook.m create mode 100644 test/setSuper.m create mode 100644 test/subscripting.m create mode 100644 test/super.m create mode 100644 test/swift-class-def.m create mode 100644 test/swiftMetadataInitializer.m create mode 100644 test/swiftMetadataInitializerRealloc-dylib1.m create mode 100644 test/swiftMetadataInitializerRealloc-dylib2.m create mode 100644 test/swiftMetadataInitializerRealloc.m create mode 100644 test/synchronized-counter.m create mode 100644 test/synchronized-grid.m create mode 100644 test/synchronized.m create mode 100644 test/taggedNSPointers.m create mode 100644 test/taggedPointers.m create mode 100644 test/taggedPointersAllClasses.m create mode 100644 test/taggedPointersDisabled.m create mode 100644 test/taggedPointersTagObfuscationDisabled.m create mode 100644 test/tbi.c create mode 100644 test/test.h create mode 100644 test/test.pl create mode 100644 test/testroot.i create mode 100644 test/timeout.pl create mode 100644 test/unload.h create mode 100644 test/unload.m create mode 100644 test/unload2.m create mode 100644 test/unload3.c create mode 100644 test/unload4.m create mode 100644 test/unwind.m create mode 100644 test/weak.h create mode 100644 test/weak.m create mode 100644 test/weak2.m create mode 100644 test/weakcopy.m create mode 100644 test/weakframework-missing.m create mode 100644 test/weakframework-not-missing.m create mode 100644 test/weakimport-missing.m create mode 100644 test/weakimport-not-missing.m create mode 100644 test/weakrace.m create mode 100644 test/willInitializeClassFunc.m create mode 100644 test/zone.m mode change 100755 => 100644 version.bat diff --git a/interposable.txt b/interposable.txt new file mode 100644 index 0000000..f5d09f3 --- /dev/null +++ b/interposable.txt @@ -0,0 +1 @@ +_objc_release diff --git a/libobjc.order b/libobjc.order index c2f2c6e..c7415fc 100644 --- a/libobjc.order +++ b/libobjc.order @@ -1,7 +1,7 @@ __objc_init _environ_init _tls_init -_lock_init +_runtime_init _recursive_mutex_init _exception_init _map_images @@ -17,12 +17,10 @@ _rtp_init _gc_fixup_barrier_stubs __objc_update_stubs_in_mach_header _sel_init -_sel_lock ___sel_registerName __objc_search_builtins __ZNK8objc_opt13objc_selopt_t3getEPKc __ZNK8objc_opt13objc_selopt_t4hashEPKc -_sel_unlock _sel_registerName _arr_init __ZN4objc8DenseMapIP11objc_objectmLb1ENS_12DenseMapInfoIS2_EENS3_ImEEE4initEj @@ -98,6 +96,7 @@ __ZNSt3__122__merge_move_constructIRN8method_t16SortBySELAddressEN13method_list_ __ZNSt3__119__merge_move_assignIRN8method_t16SortBySELAddressEPS1_S4_N13method_list_t15method_iteratorEEEvT0_S7_T1_S8_T2_T_ _NXPtrIsEqual __getObjc2CategoryList +__getObjc2CategoryList2 __Z29addUnattachedCategoryForClassP10category_tP7class_tP12_header_info __Z16remethodizeClassP7class_t __Z11flushCachesP7class_t @@ -145,7 +144,6 @@ __objc_insert_tagged_isa _objc_msgSend_fixup __objc_fixupMessageRef _objc_msgSend -__class_lookupMethodAndLoadCache3 _lookUpMethod _prepareForMethodLookup __class_initialize @@ -332,7 +330,6 @@ _objc_atomicCompareAndSwapGlobalBarrier _sel_getUid __ZN12_GLOBAL__N_119AutoreleasePoolPage11tls_deallocEPv __ZN12_GLOBAL__N_119AutoreleasePoolPage4killEv -__objc_constructOrFree _object_cxxConstruct _object_cxxConstructFromClass __class_hasCxxStructors diff --git a/markgc.cpp b/markgc.cpp index 49fd2ba..4543ad6 100644 --- a/markgc.cpp +++ b/markgc.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -476,7 +477,15 @@ bool parse_fat(uint8_t *buffer, size_t size) fat_magic = OSSwapBigToHostInt32(fh->magic); fat_nfat_arch = OSSwapBigToHostInt32(fh->nfat_arch); - if (size < (sizeof(struct fat_header) + fat_nfat_arch * sizeof(struct fat_arch))) { + size_t fat_arch_size; + // fat_nfat_arch * sizeof(struct fat_arch) + sizeof(struct fat_header) + if (os_mul_and_add_overflow(fat_nfat_arch, sizeof(struct fat_arch), + sizeof(struct fat_header), &fat_arch_size)) + { + printf("too many fat archs\n"); + return false; + } + if (size < fat_arch_size) { printf("file is too small\n"); return false; } @@ -484,7 +493,14 @@ bool parse_fat(uint8_t *buffer, size_t size) archs = (struct fat_arch *)(buffer + sizeof(struct fat_header)); /* Special case hidden CPU_TYPE_ARM64 */ - if (size >= (sizeof(struct fat_header) + (fat_nfat_arch + 1) * sizeof(struct fat_arch))) { + size_t fat_arch_plus_one_size; + if (os_add_overflow(fat_arch_size, sizeof(struct fat_arch), + &fat_arch_plus_one_size)) + { + printf("too many fat archs\n"); + return false; + } + if (size >= fat_arch_plus_one_size) { if (fat_nfat_arch > 0 && OSSwapBigToHostInt32(archs[fat_nfat_arch].cputype) == CPU_TYPE_ARM64) { fat_nfat_arch++; @@ -505,7 +521,7 @@ bool parse_fat(uint8_t *buffer, size_t size) arch_cputype, arch_cpusubtype); /* Check that slice data is after all fat headers and archs */ - if (arch_offset < (sizeof(struct fat_header) + fat_nfat_arch * sizeof(struct fat_arch))) { + if (arch_offset < fat_arch_size) { printf("file is badly formed\n"); return false; } diff --git a/objc.sln b/objc.sln old mode 100755 new mode 100644 diff --git a/objc.suo b/objc.suo deleted file mode 100755 index 7f8180c631918658823f234ec88fd2017eac6078..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 24576 zcmeI433wLOy~YPn5cdUaL5o2)5fa%E5FrpiFf0-QEm4FJAZ$SrKm`PC)hn(QDpso2 ziel}>>aAKITd#_(f*aKKwraJlZLM}wYrU;qB=`NzyhBbx^5qLq+WR~ne82Pme&7Gh zIdkT0b7ta>?MMCm&IjAR8Py7>N6n(0RV}0D0dFE3#o@>(YAOTmtg5O?U=+#xB>X?B zfsa+z19b7)N(V|@?}MZk(jihy>0s$l=`iVV=_u(4NlhR+S~^xbMrtLsmOdqYM*6hW zMmkQSjU2DIqiw#|74r0ZUA#7Hm!)6rbf0{$SpyTJa{a4}a-tE@LdA=ti&eAPhhx=h z(j#*HGle5J514q@DKVw=`1fEA^A+N&}?<66psiK1~`doh}WLhD!O;Flo3{AdQglr{+H|jrR959gkH! zP8u&wkj|1ON=4Gy(j;lJG(|c`I#>FvG*v2=&XdlUrb!n_)1?_wiA26%QanqVEzOb2 zq{}3Jo3D6*v`{LS7D*RMi>1q@N@>a?#fih#j5{u)px0^*{bX5 zs^@ue-z*lNp%zx6)|mFAy(~t&j-tH{SF0^n%VJcVtSu|gHSLnB~vPW+;`%W2|k(z zc4}tJQvYmX)g^~z8e$4!BD}w8`=(klS5-A>8Z{|i>xzz|2d1uVvQ^&`(1cdNdRtB+ z^arg=(RbQvr&|0jeScUwN*b${GCyvw^nAuC`u`%2BFuz?TiD+t39wHVb-ZXsQtz5jP?+duav+2+rE4RbYt{$8m(7pvDXdRC9L>B_WJeWEfRK^comYmG<)l|RQL zZ$f;~20m(@f4PpoMCY6wIXveHs)q{IL8g(Yhw^7#mgJsY{?yfv-5Cm$M}7IHYvF=; zPMBWjS>-8zT5@CZKV1HOd^E#xk{mW%ahSx=X_S zM_TvUo`2Z>dTF)XTkDt}%6FuOv0Po5{u&=f$U0TO4A;M0t*ZOUqrWslUc)cTGZt!1 zo%Gn=x(mrue(CCIn6B$g)lp@<3g_%-_0^-@?x_bwL*=nQOFL_~@`l$ef1j6TqS`mB zSlWAOBWjNEs{XWt;(NJ$TW|H}mZ!0)_Ek=5O_R0e*CG47)~ee~_HB(BsqS=x;yd7d zTU&MKRDVsWaJyC_MN!8?KF;rU-BV{g_}YEjkGXrw5rxs=+uF|mN6QZ5Z>+lNqwQNZ zj=;Y#db?Gpm%q`W!>C8zdUIu$84EmtoR!0UcCuP+{M}onW}8ag2c`FOdAcJi(dd=z zNhQ18|GcAfUTvc@;vO+mpNpgLPLE&NE1@lNr&(R$|ca24|7==qU@-Qk@ z>icA^4y(^ES8TD4v{3g~2~Afytb-P*ce3)LB*|#aT$nke^G|EJWjp_LkngQ@7llbG z3%O2LnJIT#o@8FAZ`GHi<#SKw-*EmrL-jN>&U>NGdA>?BQl(++rWUfwzX|JhM)z#b zKg@r8y!M%|QY?%$JE-{r5%zX61=atqz4&bN@1g_t@WutZ7{$tkHo;0O+3!gEL$0n1 zYqN6AZR|#I=eSxaXl+R^>8R0=9prgRne32Ms1;AvN}-3=2YK;lrt+V&UAIbm5ti;n z+vnT~tY5p%8CYCCV{S=q<>Dm^=f!5Rtje+lr3v?!JT~B>=BF)OGJih%h*8v~p_C=l zOXigBo-Q{|SG~u>nm66!Ss=Ui9#4hZ8jC>9E_}udOGk5R3{Vo|;Tr_9;;?kK@$|~nf9=nHw z$G|-~Ojx23x<@-Tz84#yyRv@qT^qYQ1@WC+58b)-kd?2yx?$@7x#Gj+-&ekUswM=DB3gm7}n)g@ti#c@TnqDOqcS&-G8WD~6#^L5nax(nUA zvkr63wAQb^(hu+Fvb!tUUnR}vu4G@9r9eF|d2X;T?@IPZ3G1sV+UuZr?8w(B!di&Y zWw^$y+4_Y$_=c`{&3pgd?+WWWyPCZx9Ir~^If3UeNuOedUAWt7C)3)k=CQ+$7e&`^ z>$HY5&NTnAsohXWpa1Y&mpP~*e|~3IZKfSF<^*;Z&sHDt9D4&!T*|d(Dko;<5f!J#n`UGu{wouSBaQR z*+Yf##D|fHdoehST>zd!!e|XJeCU-pAb#jHeL;CCb8X7>qIulPJ${DfJs^Hp+~Z8@-eGOf$}HoJ2g{ zjPsr-5f_<}u2|wp^m%5K?|g|moMy&(&yXnJEHlbiCK1myBP}C8e)G&I!$Px*%y`>` zw=6ifRc54JEpaWblsI4N?`kPWS|h>myGA1IwGxantd-hJ>m=AQ(l;cI@qh%QEZ>p1 zK95T&d(!MFGwSFuiSV?9ANBElse|;g1ZyR|Byqk!kYJSM=Tdv=mlBLJ|4O3FZ%Z() z`OhTU#ye8#_Z!(KNWV9uEI*aV;}2#W?_H^r^hY!D{a=Z8^=C8u{vvUGKQimC_(O@~ z{WV}!vgr>LgtFk*R0iw-Gx|Vt+3eo5FvG8cb(KvXxq)8~+4PrQX4FA%+4PsbW~3Qxd$?or9c6p0W6u37 z+mjrVc8cw(0Xxt3bjOr=j%?b^Trqi^g|eF}t_b{=*uKayZF__5 zFFR%&I8oBqC}p&vmJ;E1{gCafm!zoux%-ML`@K`{ARFhM{c0^)^KqX4+!->{`eQ)t z%Bti0o~(aZ`(5f|Wy-%_9r@Kptbgc9w3KXHLWO(}ZI&Db62{EHh{WvIQGziNc9Iw!nL%LPq+SxEZf^<3>`0r=k!V|KTCOwc z>2I(Dq|+q=YxR_kiAk~=f)OT3tWu^(DO)DHKw2I!Rv}#L{|MO4vX@Bf%_!4XCBj`2 z$19iamI#|9*iz|U=^p885{wnfMk!>}%Y72%VD*CEV(Gsm!b1}5V(DRt@GS|pLV8pp zuzG=gPWrY)_-_feH_yHr_w1w3sB7W@kzB)&P0OIwbe2d<%gB>HC7mL{Xc@GY94T!r z)LtKnmeE(jkCrh|B5;LMHZms3+8IVTOQK~INhzz4O}b?PyIeLc(hiC6eF;Xx`5%e!BMCNCdPO3lI%uF;?&$U1R$?#kbpL4*P;cYoVpAlfEcX+V#>c(rpsy zHz@wH?ev&m@%OJfmfI&`Ki5F|281p_9MU(=vz>oy?!JWkLN$L$*0Z$dnon%$K~E%E z$Gs2_dg&H3dLfKn`aLsxA&g$S)r?*UqnAE!mi9>Uc*!j7k@V6ZN>sOh+M}p(Z(Hm> zs(yb&3DP$_BG$H;dl0g_SG9kJlU@9CA6OEZ@DT#eUHkLRVK4v z_4|f{{8o2TMr|iB~y%_lIFncxdd)@4< z!0#Qi_X58U%>Ewu?KC@3LqD?&hirHy_-w?CHz;C?S z)WGk2v)O^)T(b)Uze=-}f!}Jgs{+3@W;X|ZUozVm_-!(~Kk$3d>|24~V`f_ezvs(3@`&*}NW=7gJvdOot8EKC* zJKl_Zo69EO&zO<6pX|Ph`qQ4y1;L} z*R=m-Sw43a=*^E3MFnidH@;xh?J(jIzly95ab7maxQQ4I5c{9?!Dm!2C zYi6YViP`IBok~X5?|8J}6&v#c5k>VRn!i$NNBO$fKp1 zqUbaFpnUDjDBlTYIcAjaP}!tyZ>A{fqYv)h`kIlppILu1MbQ|23{`xl8GeOkW6h2i z8z`GF-i$JLG@EF~*BSa4rMSe5#lPWlwa0L>W@nO7;RZ@l7kolAXnX_3@dFU1_2dhKgA z#0in?y}% z%QZsBJ-NsJx2ogXH+6CUn;Eo|*={H0K|5J&wleTrZMG)xyUuKV;CHLpU1qia8ll&m z3)$}9)nC*4Ze!L}@yQZ>B+sma;=U4NRzI`$idoa}8#{>f5Bl0r$N7q3$4ZQ`gC)jp z7<=jCB#t*;O8w4}-Ay{zjP}5;BYkwL8TJ&3aje)(QT!%)GsUH5_|22rOY_YX#s3Ev z{3-*sOg4;N3d+J5vr1|!t(IWa$LDNc=~z+ppAu>Bm+;f@7(abewCfoP_Cr!L9WDH8 zp8fH60H0j{f2Zr8JY}Vw5T1}|J6k0f^Xp3z{c5|E`u##Ses7s+nu^~AQxv^thW);T z-=CzkEq*AQ@HYvjC1b>fP9F&Xqn|aAK_6{qMjn5c2+hrol+C+aghS1+50g#4tpnD< zjQ6qhUmUudp8U2xWve+@57O>H^ z7-@Oesz7o2E*15%NGw-z`u-H-*=!$YnPZ{oV%e1CvVdJ-wkBZLncWbu^=7vOY?Il& z0sE%eLuQ=YW3m}DzimdDzhm~eSr@Sse7(Oeg8n&K9Uh2wCTY|4B@z*?Cd z8?ZKJ#|7+kv;2UKFdG%HF=mAUJKJn>z*d^AGGpEtCYx|W;CG{J=F|-V+a#OwxX+9> z`yJUiiXRWyoA!IhOg~4zH~T}t{${q*jCN2Zn|wPeqqP3I$R^*eX4J^Lg7> z1%6lC?^?4~^1EI(@eO7?W4J{&YlsbIIkMN=zRhuau{&))60mRCe!($q{6*QPDBf;H zUF|S?*-R+uA=+jQft0ZS&W~_)(U=vS~{L0yfa> zw15pZ8yc{Dvk_(-r;lvTXQUZzqnGVbj_Io>%I>c?Wiq2NVw`WG{a_Q!ip&^urpoTD zxY&&R&Xe6;@%d(4!*gw4;P}&GlVo#Wmok}Ai5U5o+7DJ{HqVSQFPB}YctyZg$sR4X zI$&4JW?s71OlEY8Z1UJ(c8u)XW%p2gN8tBW`)xGi8a*VN@R%9r`)#wQ%=rDgvdQBa zGs^s|?dJl&7wq@48G9fE7ltYu%d{YK#TGu!VwW?k?{+uV@RuDIq0*lrcD*0wu2 z=G?l-X1>WaQxxTy^)VBQ&XUc!6`65vlVx{Qd~V=ZY`^o(m~$?$T^jh!wqKbU{bQc( z#R03ZeTieryj(VO=%r?gqE%*>n^CSSY+nm2xXHR~Dp^)VY1_?>PxGVmL1 zHX-mUGMgIsoo`ke_{}j}6!=w`T@v`MG`lMBTVu91@VnXU%Yok=W*Y;)O=g<|zX!}7 z4*VW9dm`}LV)ks{_ng^|!0!iUuLOQSG5bZ}_m5|7J`DW+V)l=~ugdH|?>*2C z4w5Y#9cD&5IMS?5;CH-P$H4EOu>Tg9Wq6x7e@Y Q{U`o^AN>8~o^Hqf9~E!sJ^%m! diff --git a/objc.xcodeproj/project.pbxproj b/objc.xcodeproj/project.pbxproj index 2c6187d..80c47fb 100644 --- a/objc.xcodeproj/project.pbxproj +++ b/objc.xcodeproj/project.pbxproj @@ -7,6 +7,17 @@ objects = { /* Begin PBXAggregateTarget section */ + 834F9B01212E560100F95A54 /* objc4_tests */ = { + isa = PBXAggregateTarget; + buildConfigurationList = 834F9B04212E560200F95A54 /* Build configuration list for PBXAggregateTarget "objc4_tests" */; + buildPhases = ( + 834F9B05212E561400F95A54 /* Run Script (build tests) */, + ); + dependencies = ( + ); + name = objc4_tests; + productName = objc4_tests; + }; 837F67A81A771F63004D34FA /* objc-simulator */ = { isa = PBXAggregateTarget; buildConfigurationList = 837F67A91A771F63004D34FA /* Build configuration list for PBXAggregateTarget "objc-simulator" */; @@ -25,6 +36,21 @@ 393CEAC60DC69E67000B69DE /* objc-references.h in Headers */ = {isa = PBXBuildFile; fileRef = 393CEAC50DC69E67000B69DE /* objc-references.h */; }; 39ABD72312F0B61800D1054C /* objc-weak.h in Headers */ = {isa = PBXBuildFile; fileRef = 39ABD71F12F0B61800D1054C /* objc-weak.h */; }; 39ABD72412F0B61800D1054C /* objc-weak.mm in Sources */ = {isa = PBXBuildFile; fileRef = 39ABD72012F0B61800D1054C /* objc-weak.mm */; }; + 6E1475EA21DFDB1B001357EA /* llvm-AlignOf.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E1475E521DFDB1A001357EA /* llvm-AlignOf.h */; }; + 6E1475EB21DFDB1B001357EA /* llvm-DenseMap.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E1475E621DFDB1B001357EA /* llvm-DenseMap.h */; }; + 6E1475EC21DFDB1B001357EA /* llvm-DenseMapInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E1475E721DFDB1B001357EA /* llvm-DenseMapInfo.h */; }; + 6E1475ED21DFDB1B001357EA /* llvm-type_traits.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E1475E821DFDB1B001357EA /* llvm-type_traits.h */; }; + 6E1475EE21DFDB1B001357EA /* llvm-MathExtras.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E1475E921DFDB1B001357EA /* llvm-MathExtras.h */; }; + 6E7B0862232DE7CA00689009 /* PointerUnion.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E7B0861232DE7CA00689009 /* PointerUnion.h */; }; + 6EACB842232C97A400CE9176 /* objc-zalloc.h in Headers */ = {isa = PBXBuildFile; fileRef = 6EACB841232C97A400CE9176 /* objc-zalloc.h */; }; + 6EACB844232C97B900CE9176 /* objc-zalloc.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6EACB843232C97B900CE9176 /* objc-zalloc.mm */; }; + 6ECD0B1F2244999E00910D88 /* llvm-DenseSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 6ECD0B1E2244999E00910D88 /* llvm-DenseSet.h */; }; + 7213C36321FA7C730090A271 /* NSObject-internal.h in Headers */ = {isa = PBXBuildFile; fileRef = 7213C36221FA7C730090A271 /* NSObject-internal.h */; settings = {ATTRIBUTES = (Private, ); }; }; + 7593EC58202248E50046AB96 /* objc-object.h in Headers */ = {isa = PBXBuildFile; fileRef = 7593EC57202248DF0046AB96 /* objc-object.h */; }; + 75A9504F202BAA0600D7D56F /* objc-locks-new.h in Headers */ = {isa = PBXBuildFile; fileRef = 75A9504E202BAA0300D7D56F /* objc-locks-new.h */; }; + 75A95051202BAA9A00D7D56F /* objc-locks.h in Headers */ = {isa = PBXBuildFile; fileRef = 75A95050202BAA9A00D7D56F /* objc-locks.h */; }; + 75A95053202BAC4100D7D56F /* objc-lockdebug.h in Headers */ = {isa = PBXBuildFile; fileRef = 75A95052202BAC4100D7D56F /* objc-lockdebug.h */; }; + 8306440920D24A5D00E356D2 /* objc-block-trampolines.h in Headers */ = {isa = PBXBuildFile; fileRef = 8306440620D24A3E00E356D2 /* objc-block-trampolines.h */; settings = {ATTRIBUTES = (Private, ); }; }; 830F2A740D737FB800392440 /* objc-msg-arm.s in Sources */ = {isa = PBXBuildFile; fileRef = 830F2A690D737FB800392440 /* objc-msg-arm.s */; }; 830F2A750D737FB900392440 /* objc-msg-i386.s in Sources */ = {isa = PBXBuildFile; fileRef = 830F2A6A0D737FB800392440 /* objc-msg-i386.s */; }; 830F2A7D0D737FBB00392440 /* objc-msg-x86_64.s in Sources */ = {isa = PBXBuildFile; fileRef = 830F2A720D737FB800392440 /* objc-msg-x86_64.s */; }; @@ -37,9 +63,6 @@ 834DF8B715993EE1002F2BC9 /* objc-sel-old.mm in Sources */ = {isa = PBXBuildFile; fileRef = 834DF8B615993EE1002F2BC9 /* objc-sel-old.mm */; }; 834EC0A411614167009B2563 /* objc-abi.h in Headers */ = {isa = PBXBuildFile; fileRef = 834EC0A311614167009B2563 /* objc-abi.h */; settings = {ATTRIBUTES = (Private, ); }; }; 83725F4A14CA5BFA0014370E /* objc-opt.mm in Sources */ = {isa = PBXBuildFile; fileRef = 83725F4914CA5BFA0014370E /* objc-opt.mm */; }; - 8379996E13CBAF6F007C2B5F /* a1a2-blocktramps-arm64.s in Sources */ = {isa = PBXBuildFile; fileRef = 8379996D13CBAF6F007C2B5F /* a1a2-blocktramps-arm64.s */; }; - 8383A3A3122600E9009290B8 /* a1a2-blocktramps-arm.s in Sources */ = {isa = PBXBuildFile; fileRef = 8383A3A1122600E9009290B8 /* a1a2-blocktramps-arm.s */; }; - 8383A3A4122600E9009290B8 /* a2a3-blocktramps-arm.s in Sources */ = {isa = PBXBuildFile; fileRef = 8383A3A2122600E9009290B8 /* a2a3-blocktramps-arm.s */; }; 838485BF0D6D687300CEA253 /* hashtable2.h in Headers */ = {isa = PBXBuildFile; fileRef = 838485B70D6D687300CEA253 /* hashtable2.h */; settings = {ATTRIBUTES = (Public, ); }; }; 838485C00D6D687300CEA253 /* hashtable2.mm in Sources */ = {isa = PBXBuildFile; fileRef = 838485B80D6D687300CEA253 /* hashtable2.mm */; }; 838485C30D6D687300CEA253 /* maptable.h in Headers */ = {isa = PBXBuildFile; fileRef = 838485BB0D6D687300CEA253 /* maptable.h */; settings = {ATTRIBUTES = (Private, ); }; }; @@ -63,7 +86,7 @@ 838485FF0D6D68A200CEA253 /* objc-load.mm in Sources */ = {isa = PBXBuildFile; fileRef = 838485D80D6D68A200CEA253 /* objc-load.mm */; }; 838486000D6D68A200CEA253 /* objc-loadmethod.h in Headers */ = {isa = PBXBuildFile; fileRef = 838485D90D6D68A200CEA253 /* objc-loadmethod.h */; }; 838486010D6D68A200CEA253 /* objc-loadmethod.mm in Sources */ = {isa = PBXBuildFile; fileRef = 838485DA0D6D68A200CEA253 /* objc-loadmethod.mm */; }; - 838486020D6D68A200CEA253 /* objc-lockdebug.mm in Sources */ = {isa = PBXBuildFile; fileRef = 838485DB0D6D68A200CEA253 /* objc-lockdebug.mm */; }; + 838486020D6D68A200CEA253 /* objc-lockdebug.mm in Sources */ = {isa = PBXBuildFile; fileRef = 838485DB0D6D68A200CEA253 /* objc-lockdebug.mm */; settings = {COMPILER_FLAGS = "-Os"; }; }; 838486030D6D68A200CEA253 /* objc-private.h in Headers */ = {isa = PBXBuildFile; fileRef = 838485DC0D6D68A200CEA253 /* objc-private.h */; }; 838486070D6D68A200CEA253 /* objc-runtime-new.h in Headers */ = {isa = PBXBuildFile; fileRef = 838485E00D6D68A200CEA253 /* objc-runtime-new.h */; }; 838486080D6D68A200CEA253 /* objc-runtime-new.mm in Sources */ = {isa = PBXBuildFile; fileRef = 838485E10D6D68A200CEA253 /* objc-runtime-new.mm */; }; @@ -85,6 +108,8 @@ 838486250D6D68F000CEA253 /* List.m in Sources */ = {isa = PBXBuildFile; fileRef = 838486230D6D68F000CEA253 /* List.m */; }; 838486260D6D68F000CEA253 /* List.h in Headers */ = {isa = PBXBuildFile; fileRef = 838486240D6D68F000CEA253 /* List.h */; settings = {ATTRIBUTES = (Public, ); }; }; 838486280D6D6A2400CEA253 /* message.h in Headers */ = {isa = PBXBuildFile; fileRef = 838485BD0D6D687300CEA253 /* message.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 83A4AEDC1EA0840800ACADDE /* module.modulemap in Headers */ = {isa = PBXBuildFile; fileRef = 83A4AED71EA06D9D00ACADDE /* module.modulemap */; settings = {ATTRIBUTES = (Public, ); }; }; + 83A4AEDE1EA08C7200ACADDE /* ObjectiveC.apinotes in Headers */ = {isa = PBXBuildFile; fileRef = 83A4AEDD1EA08C5700ACADDE /* ObjectiveC.apinotes */; settings = {ATTRIBUTES = (Public, ); }; }; 83B1A8BE0FF1AC0D0019EA5B /* objc-msg-simulator-i386.s in Sources */ = {isa = PBXBuildFile; fileRef = 83B1A8BC0FF1AC0D0019EA5B /* objc-msg-simulator-i386.s */; }; 83BE02E40FCCB23400661494 /* objc-file-old.mm in Sources */ = {isa = PBXBuildFile; fileRef = 83BE02E30FCCB23400661494 /* objc-file-old.mm */; }; 83BE02E80FCCB24D00661494 /* objc-file-old.h in Headers */ = {isa = PBXBuildFile; fileRef = 83BE02E50FCCB24D00661494 /* objc-file-old.h */; }; @@ -92,17 +117,20 @@ 83BE02EA0FCCB24D00661494 /* objc-runtime-old.h in Headers */ = {isa = PBXBuildFile; fileRef = 83BE02E70FCCB24D00661494 /* objc-runtime-old.h */; }; 83C9C3391668B50E00F4E544 /* objc-msg-simulator-x86_64.s in Sources */ = {isa = PBXBuildFile; fileRef = 83C9C3381668B50E00F4E544 /* objc-msg-simulator-x86_64.s */; }; 83D49E4F13C7C84F0057F1DD /* objc-msg-arm64.s in Sources */ = {isa = PBXBuildFile; fileRef = 83D49E4E13C7C84F0057F1DD /* objc-msg-arm64.s */; }; + 83D92696212254CF00299F69 /* isa.h in Headers */ = {isa = PBXBuildFile; fileRef = 83D92695212254CF00299F69 /* isa.h */; }; + 83D9269821225A7400299F69 /* arm64-asm.h in Headers */ = {isa = PBXBuildFile; fileRef = 83D9269721225A7400299F69 /* arm64-asm.h */; }; 83EB007B121C9EC200B92C16 /* objc-sel-table.s in Sources */ = {isa = PBXBuildFile; fileRef = 83EB007A121C9EC200B92C16 /* objc-sel-table.s */; }; + 83EF5E9820D2298400F486A4 /* objc-blocktramps-i386.s in Sources */ = {isa = PBXBuildFile; fileRef = E8923D9C116AB2820071B552 /* objc-blocktramps-i386.s */; }; + 83EF5E9920D2298400F486A4 /* objc-blocktramps-x86_64.s in Sources */ = {isa = PBXBuildFile; fileRef = E8923D9D116AB2820071B552 /* objc-blocktramps-x86_64.s */; }; + 83EF5E9C20D2299E00F486A4 /* objc-blocktramps-arm.s in Sources */ = {isa = PBXBuildFile; fileRef = 8383A3A1122600E9009290B8 /* objc-blocktramps-arm.s */; }; 83F4B52815E843B100E0926F /* NSObjCRuntime.h in Headers */ = {isa = PBXBuildFile; fileRef = 83F4B52615E843B100E0926F /* NSObjCRuntime.h */; settings = {ATTRIBUTES = (Public, ); }; }; 83F4B52915E843B100E0926F /* NSObject.h in Headers */ = {isa = PBXBuildFile; fileRef = 83F4B52715E843B100E0926F /* NSObject.h */; settings = {ATTRIBUTES = (Public, ); }; }; 83F550E0155E030800E95D3B /* objc-cache-old.mm in Sources */ = {isa = PBXBuildFile; fileRef = 83F550DF155E030800E95D3B /* objc-cache-old.mm */; }; 87BB4EA70EC39854005D08E1 /* objc-probes.d in Sources */ = {isa = PBXBuildFile; fileRef = 87BB4E900EC39633005D08E1 /* objc-probes.d */; }; 9672F7EE14D5F488007CEC96 /* NSObject.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9672F7ED14D5F488007CEC96 /* NSObject.mm */; }; - E8923DA1116AB2820071B552 /* a1a2-blocktramps-i386.s in Sources */ = {isa = PBXBuildFile; fileRef = E8923D9C116AB2820071B552 /* a1a2-blocktramps-i386.s */; }; - E8923DA2116AB2820071B552 /* a1a2-blocktramps-x86_64.s in Sources */ = {isa = PBXBuildFile; fileRef = E8923D9D116AB2820071B552 /* a1a2-blocktramps-x86_64.s */; }; - E8923DA3116AB2820071B552 /* a2a3-blocktramps-i386.s in Sources */ = {isa = PBXBuildFile; fileRef = E8923D9E116AB2820071B552 /* a2a3-blocktramps-i386.s */; }; - E8923DA4116AB2820071B552 /* a2a3-blocktramps-x86_64.s in Sources */ = {isa = PBXBuildFile; fileRef = E8923D9F116AB2820071B552 /* a2a3-blocktramps-x86_64.s */; }; + C2E6D3FC2225DCF00059DFAA /* DenseMapExtras.h in Headers */ = {isa = PBXBuildFile; fileRef = C2E6D3FB2225DCF00059DFAA /* DenseMapExtras.h */; }; E8923DA5116AB2820071B552 /* objc-block-trampolines.mm in Sources */ = {isa = PBXBuildFile; fileRef = E8923DA0116AB2820071B552 /* objc-block-trampolines.mm */; }; + F9BCC71B205C68E800DD9AFC /* objc-blocktramps-arm64.s in Sources */ = {isa = PBXBuildFile; fileRef = 8379996D13CBAF6F007C2B5F /* objc-blocktramps-arm64.s */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -113,6 +141,13 @@ remoteGlobalIDString = D2AAC0620554660B00DB518D; remoteInfo = objc; }; + F9BCC728205C6A0900DD9AFC /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 08FB7793FE84155DC02AAC07 /* Project object */; + proxyType = 1; + remoteGlobalIDString = F9BCC6CA205C68E800DD9AFC; + remoteInfo = "objc-trampolines"; + }; /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ @@ -120,6 +155,21 @@ 393CEAC50DC69E67000B69DE /* objc-references.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "objc-references.h"; path = "runtime/objc-references.h"; sourceTree = ""; }; 39ABD71F12F0B61800D1054C /* objc-weak.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "objc-weak.h"; path = "runtime/objc-weak.h"; sourceTree = ""; }; 39ABD72012F0B61800D1054C /* objc-weak.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = "objc-weak.mm"; path = "runtime/objc-weak.mm"; sourceTree = ""; }; + 6E1475E521DFDB1A001357EA /* llvm-AlignOf.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.c.h; name = "llvm-AlignOf.h"; path = "runtime/llvm-AlignOf.h"; sourceTree = ""; tabWidth = 2; }; + 6E1475E621DFDB1B001357EA /* llvm-DenseMap.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.c.h; name = "llvm-DenseMap.h"; path = "runtime/llvm-DenseMap.h"; sourceTree = ""; tabWidth = 2; }; + 6E1475E721DFDB1B001357EA /* llvm-DenseMapInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.c.h; name = "llvm-DenseMapInfo.h"; path = "runtime/llvm-DenseMapInfo.h"; sourceTree = ""; tabWidth = 2; }; + 6E1475E821DFDB1B001357EA /* llvm-type_traits.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.c.h; name = "llvm-type_traits.h"; path = "runtime/llvm-type_traits.h"; sourceTree = ""; tabWidth = 2; }; + 6E1475E921DFDB1B001357EA /* llvm-MathExtras.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.c.h; name = "llvm-MathExtras.h"; path = "runtime/llvm-MathExtras.h"; sourceTree = ""; tabWidth = 2; }; + 6E7B0861232DE7CA00689009 /* PointerUnion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PointerUnion.h; path = runtime/PointerUnion.h; sourceTree = ""; }; + 6EACB841232C97A400CE9176 /* objc-zalloc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "objc-zalloc.h"; path = "runtime/objc-zalloc.h"; sourceTree = ""; }; + 6EACB843232C97B900CE9176 /* objc-zalloc.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = "objc-zalloc.mm"; path = "runtime/objc-zalloc.mm"; sourceTree = ""; }; + 6ECD0B1E2244999E00910D88 /* llvm-DenseSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "llvm-DenseSet.h"; path = "runtime/llvm-DenseSet.h"; sourceTree = ""; }; + 7213C36221FA7C730090A271 /* NSObject-internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSObject-internal.h"; path = "runtime/NSObject-internal.h"; sourceTree = ""; }; + 7593EC57202248DF0046AB96 /* objc-object.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "objc-object.h"; path = "runtime/objc-object.h"; sourceTree = ""; }; + 75A9504E202BAA0300D7D56F /* objc-locks-new.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "objc-locks-new.h"; path = "runtime/objc-locks-new.h"; sourceTree = ""; }; + 75A95050202BAA9A00D7D56F /* objc-locks.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "objc-locks.h"; path = "runtime/objc-locks.h"; sourceTree = ""; }; + 75A95052202BAC4100D7D56F /* objc-lockdebug.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "objc-lockdebug.h"; path = "runtime/objc-lockdebug.h"; sourceTree = ""; }; + 8306440620D24A3E00E356D2 /* objc-block-trampolines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "objc-block-trampolines.h"; path = "runtime/objc-block-trampolines.h"; sourceTree = ""; }; 830F2A690D737FB800392440 /* objc-msg-arm.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "objc-msg-arm.s"; path = "runtime/Messengers.subproj/objc-msg-arm.s"; sourceTree = ""; }; 830F2A6A0D737FB800392440 /* objc-msg-i386.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "objc-msg-i386.s"; path = "runtime/Messengers.subproj/objc-msg-i386.s"; sourceTree = ""; }; 830F2A720D737FB800392440 /* objc-msg-x86_64.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "objc-msg-x86_64.s"; path = "runtime/Messengers.subproj/objc-msg-x86_64.s"; sourceTree = ""; tabWidth = 8; usesTabs = 1; }; @@ -133,9 +183,8 @@ 834DF8B615993EE1002F2BC9 /* objc-sel-old.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = "objc-sel-old.mm"; path = "runtime/objc-sel-old.mm"; sourceTree = ""; }; 834EC0A311614167009B2563 /* objc-abi.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "objc-abi.h"; path = "runtime/objc-abi.h"; sourceTree = ""; }; 83725F4914CA5BFA0014370E /* objc-opt.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = "objc-opt.mm"; path = "runtime/objc-opt.mm"; sourceTree = ""; }; - 8379996D13CBAF6F007C2B5F /* a1a2-blocktramps-arm64.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "a1a2-blocktramps-arm64.s"; path = "runtime/a1a2-blocktramps-arm64.s"; sourceTree = ""; }; - 8383A3A1122600E9009290B8 /* a1a2-blocktramps-arm.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "a1a2-blocktramps-arm.s"; path = "runtime/a1a2-blocktramps-arm.s"; sourceTree = ""; }; - 8383A3A2122600E9009290B8 /* a2a3-blocktramps-arm.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "a2a3-blocktramps-arm.s"; path = "runtime/a2a3-blocktramps-arm.s"; sourceTree = ""; }; + 8379996D13CBAF6F007C2B5F /* objc-blocktramps-arm64.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "objc-blocktramps-arm64.s"; path = "runtime/objc-blocktramps-arm64.s"; sourceTree = ""; }; + 8383A3A1122600E9009290B8 /* objc-blocktramps-arm.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "objc-blocktramps-arm.s"; path = "runtime/objc-blocktramps-arm.s"; sourceTree = ""; }; 838485B30D6D682B00CEA253 /* libobjc.order */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = libobjc.order; sourceTree = ""; }; 838485B40D6D683300CEA253 /* APPLE_LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = APPLE_LICENSE; sourceTree = ""; }; 838485B50D6D683300CEA253 /* ReleaseNotes.rtf */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; path = ReleaseNotes.rtf; sourceTree = ""; }; @@ -184,13 +233,18 @@ 8384861A0D6D68A800CEA253 /* runtime.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = runtime.h; path = runtime/runtime.h; sourceTree = ""; }; 838486230D6D68F000CEA253 /* List.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = List.m; path = runtime/OldClasses.subproj/List.m; sourceTree = ""; }; 838486240D6D68F000CEA253 /* List.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = List.h; path = runtime/OldClasses.subproj/List.h; sourceTree = ""; }; + 83A4AED71EA06D9D00ACADDE /* module.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; name = module.modulemap; path = runtime/Module/module.modulemap; sourceTree = ""; }; + 83A4AEDD1EA08C5700ACADDE /* ObjectiveC.apinotes */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = ObjectiveC.apinotes; path = runtime/Module/ObjectiveC.apinotes; sourceTree = ""; }; 83B1A8BC0FF1AC0D0019EA5B /* objc-msg-simulator-i386.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "objc-msg-simulator-i386.s"; path = "runtime/Messengers.subproj/objc-msg-simulator-i386.s"; sourceTree = ""; }; 83BE02E30FCCB23400661494 /* objc-file-old.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = "objc-file-old.mm"; path = "runtime/objc-file-old.mm"; sourceTree = ""; }; 83BE02E50FCCB24D00661494 /* objc-file-old.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "objc-file-old.h"; path = "runtime/objc-file-old.h"; sourceTree = ""; }; 83BE02E60FCCB24D00661494 /* objc-file.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "objc-file.h"; path = "runtime/objc-file.h"; sourceTree = ""; }; 83BE02E70FCCB24D00661494 /* objc-runtime-old.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "objc-runtime-old.h"; path = "runtime/objc-runtime-old.h"; sourceTree = ""; }; 83C9C3381668B50E00F4E544 /* objc-msg-simulator-x86_64.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "objc-msg-simulator-x86_64.s"; path = "runtime/Messengers.subproj/objc-msg-simulator-x86_64.s"; sourceTree = ""; }; + 83CE671D1E6E76B60095A33E /* interposable.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = interposable.txt; sourceTree = ""; }; 83D49E4E13C7C84F0057F1DD /* objc-msg-arm64.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "objc-msg-arm64.s"; path = "runtime/Messengers.subproj/objc-msg-arm64.s"; sourceTree = ""; }; + 83D92695212254CF00299F69 /* isa.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = isa.h; path = runtime/isa.h; sourceTree = ""; }; + 83D9269721225A7400299F69 /* arm64-asm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "arm64-asm.h"; path = "runtime/arm64-asm.h"; sourceTree = ""; }; 83EB007A121C9EC200B92C16 /* objc-sel-table.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "objc-sel-table.s"; path = "runtime/objc-sel-table.s"; sourceTree = ""; }; 83F4B52615E843B100E0926F /* NSObjCRuntime.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSObjCRuntime.h; path = runtime/NSObjCRuntime.h; sourceTree = ""; }; 83F4B52715E843B100E0926F /* NSObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSObject.h; path = runtime/NSObject.h; sourceTree = ""; }; @@ -198,12 +252,12 @@ 87BB4E900EC39633005D08E1 /* objc-probes.d */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.dtrace; name = "objc-probes.d"; path = "runtime/objc-probes.d"; sourceTree = ""; }; 9672F7ED14D5F488007CEC96 /* NSObject.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = NSObject.mm; path = runtime/NSObject.mm; sourceTree = ""; }; BC8B5D1212D3D48100C78A5B /* libauto.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libauto.dylib; path = /usr/lib/libauto.dylib; sourceTree = ""; }; + C2E6D3FB2225DCF00059DFAA /* DenseMapExtras.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DenseMapExtras.h; path = runtime/DenseMapExtras.h; sourceTree = ""; }; D2AAC0630554660B00DB518D /* libobjc.A.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libobjc.A.dylib; sourceTree = BUILT_PRODUCTS_DIR; }; - E8923D9C116AB2820071B552 /* a1a2-blocktramps-i386.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "a1a2-blocktramps-i386.s"; path = "runtime/a1a2-blocktramps-i386.s"; sourceTree = ""; }; - E8923D9D116AB2820071B552 /* a1a2-blocktramps-x86_64.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "a1a2-blocktramps-x86_64.s"; path = "runtime/a1a2-blocktramps-x86_64.s"; sourceTree = ""; }; - E8923D9E116AB2820071B552 /* a2a3-blocktramps-i386.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "a2a3-blocktramps-i386.s"; path = "runtime/a2a3-blocktramps-i386.s"; sourceTree = ""; }; - E8923D9F116AB2820071B552 /* a2a3-blocktramps-x86_64.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "a2a3-blocktramps-x86_64.s"; path = "runtime/a2a3-blocktramps-x86_64.s"; sourceTree = ""; }; + E8923D9C116AB2820071B552 /* objc-blocktramps-i386.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "objc-blocktramps-i386.s"; path = "runtime/objc-blocktramps-i386.s"; sourceTree = ""; }; + E8923D9D116AB2820071B552 /* objc-blocktramps-x86_64.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = "objc-blocktramps-x86_64.s"; path = "runtime/objc-blocktramps-x86_64.s"; sourceTree = ""; }; E8923DA0116AB2820071B552 /* objc-block-trampolines.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = "objc-block-trampolines.mm"; path = "runtime/objc-block-trampolines.mm"; sourceTree = ""; }; + F9BCC727205C68E800DD9AFC /* libobjc-trampolines.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = "libobjc-trampolines.dylib"; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -214,6 +268,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + F9BCC721205C68E800DD9AFC /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ @@ -229,6 +290,7 @@ 08FB7795FE84155DC02AAC07 /* Source */, 838485B20D6D67F900CEA253 /* Other */, 1AB674ADFE9D54B511CA2CBB /* Products */, + F9BCC72A205C6A1600DD9AFC /* Frameworks */, ); name = objc; sourceTree = ""; @@ -236,8 +298,6 @@ 08FB7795FE84155DC02AAC07 /* Source */ = { isa = PBXGroup; children = ( - 8383A3A1122600E9009290B8 /* a1a2-blocktramps-arm.s */, - 8383A3A2122600E9009290B8 /* a2a3-blocktramps-arm.s */, 838485B80D6D687300CEA253 /* hashtable2.mm */, 838485BC0D6D687300CEA253 /* maptable.mm */, 9672F7ED14D5F488007CEC96 /* NSObject.mm */, @@ -245,6 +305,7 @@ 830F2A930D73876100392440 /* objc-accessors.mm */, 838485CA0D6D68A200CEA253 /* objc-auto.mm */, 39ABD72012F0B61800D1054C /* objc-weak.mm */, + 6EACB843232C97B900CE9176 /* objc-zalloc.mm */, E8923DA0116AB2820071B552 /* objc-block-trampolines.mm */, 838485CB0D6D68A200CEA253 /* objc-cache.mm */, 83F550DF155E030800E95D3B /* objc-cache-old.mm */, @@ -271,11 +332,10 @@ 834DF8B615993EE1002F2BC9 /* objc-sel-old.mm */, 838485EA0D6D68A200CEA253 /* objc-sync.mm */, 838485EB0D6D68A200CEA253 /* objc-typeencoding.mm */, - 8379996D13CBAF6F007C2B5F /* a1a2-blocktramps-arm64.s */, - E8923D9C116AB2820071B552 /* a1a2-blocktramps-i386.s */, - E8923D9D116AB2820071B552 /* a1a2-blocktramps-x86_64.s */, - E8923D9E116AB2820071B552 /* a2a3-blocktramps-i386.s */, - E8923D9F116AB2820071B552 /* a2a3-blocktramps-x86_64.s */, + 8383A3A1122600E9009290B8 /* objc-blocktramps-arm.s */, + 8379996D13CBAF6F007C2B5F /* objc-blocktramps-arm64.s */, + E8923D9C116AB2820071B552 /* objc-blocktramps-i386.s */, + E8923D9D116AB2820071B552 /* objc-blocktramps-x86_64.s */, 830F2A690D737FB800392440 /* objc-msg-arm.s */, 83D49E4E13C7C84F0057F1DD /* objc-msg-arm64.s */, 830F2A6A0D737FB800392440 /* objc-msg-i386.s */, @@ -291,6 +351,7 @@ isa = PBXGroup; children = ( D2AAC0630554660B00DB518D /* libobjc.A.dylib */, + F9BCC727205C68E800DD9AFC /* libobjc-trampolines.dylib */, ); name = Products; sourceTree = ""; @@ -301,6 +362,7 @@ 830F2AA50D7394C200392440 /* markgc.cpp */, 838485B40D6D683300CEA253 /* APPLE_LICENSE */, 838485B50D6D683300CEA253 /* ReleaseNotes.rtf */, + 83CE671D1E6E76B60095A33E /* interposable.txt */, 838485B30D6D682B00CEA253 /* libobjc.order */, ); name = Other; @@ -309,6 +371,8 @@ 838485C60D6D687700CEA253 /* Public Headers */ = { isa = PBXGroup; children = ( + 83A4AEDD1EA08C5700ACADDE /* ObjectiveC.apinotes */, + 83A4AED71EA06D9D00ACADDE /* module.modulemap */, 83F4B52615E843B100E0926F /* NSObjCRuntime.h */, 83F4B52715E843B100E0926F /* NSObject.h */, 838485BD0D6D687300CEA253 /* message.h */, @@ -325,10 +389,12 @@ 838485C70D6D688200CEA253 /* Private Headers */ = { isa = PBXGroup; children = ( + 7213C36221FA7C730090A271 /* NSObject-internal.h */, 83112ED30F00599600A5FBAF /* objc-internal.h */, 834EC0A311614167009B2563 /* objc-abi.h */, 838485BB0D6D687300CEA253 /* maptable.h */, 834266D70E665A8B002E4DA2 /* objc-gdb.h */, + 8306440620D24A3E00E356D2 /* objc-block-trampolines.h */, ); name = "Private Headers"; sourceTree = ""; @@ -360,11 +426,25 @@ 8384862A0D6D6ABC00CEA253 /* Project Headers */ = { isa = PBXGroup; children = ( + 6E1475E521DFDB1A001357EA /* llvm-AlignOf.h */, + 6E1475E621DFDB1B001357EA /* llvm-DenseMap.h */, + 6E1475E721DFDB1B001357EA /* llvm-DenseMapInfo.h */, + 6ECD0B1E2244999E00910D88 /* llvm-DenseSet.h */, + 6E1475E921DFDB1B001357EA /* llvm-MathExtras.h */, + 6E1475E821DFDB1B001357EA /* llvm-type_traits.h */, + C2E6D3FB2225DCF00059DFAA /* DenseMapExtras.h */, + 6E7B0861232DE7CA00689009 /* PointerUnion.h */, + 83D9269721225A7400299F69 /* arm64-asm.h */, + 83D92695212254CF00299F69 /* isa.h */, 838485CF0D6D68A200CEA253 /* objc-config.h */, - 83BE02E60FCCB24D00661494 /* objc-file.h */, 83BE02E50FCCB24D00661494 /* objc-file-old.h */, + 83BE02E60FCCB24D00661494 /* objc-file.h */, 838485D40D6D68A200CEA253 /* objc-initialize.h */, 838485D90D6D68A200CEA253 /* objc-loadmethod.h */, + 75A9504E202BAA0300D7D56F /* objc-locks-new.h */, + 75A95052202BAC4100D7D56F /* objc-lockdebug.h */, + 75A95050202BAA9A00D7D56F /* objc-locks.h */, + 7593EC57202248DF0046AB96 /* objc-object.h */, 831C85D30E10CF850066E64C /* objc-os.h */, 838485DC0D6D68A200CEA253 /* objc-private.h */, 393CEAC50DC69E67000B69DE /* objc-references.h */, @@ -372,19 +452,43 @@ 83BE02E70FCCB24D00661494 /* objc-runtime-old.h */, 838485E50D6D68A200CEA253 /* objc-sel-set.h */, 39ABD71F12F0B61800D1054C /* objc-weak.h */, + 6EACB841232C97A400CE9176 /* objc-zalloc.h */, ); name = "Project Headers"; sourceTree = ""; }; + F9BCC72A205C6A1600DD9AFC /* Frameworks */ = { + isa = PBXGroup; + children = ( + ); + name = Frameworks; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ + 8306440820D24A5300E356D2 /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + 8306440920D24A5D00E356D2 /* objc-block-trampolines.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; D2AAC0600554660B00DB518D /* Headers */ = { isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( + 83A4AEDE1EA08C7200ACADDE /* ObjectiveC.apinotes in Headers */, + 75A95051202BAA9A00D7D56F /* objc-locks.h in Headers */, + 6E1475ED21DFDB1B001357EA /* llvm-type_traits.h in Headers */, + 83A4AEDC1EA0840800ACADDE /* module.modulemap in Headers */, 830F2A980D738DC200392440 /* hashtable.h in Headers */, + 6EACB842232C97A400CE9176 /* objc-zalloc.h in Headers */, + 6E1475EA21DFDB1B001357EA /* llvm-AlignOf.h in Headers */, 838485BF0D6D687300CEA253 /* hashtable2.h in Headers */, + 6E1475EC21DFDB1B001357EA /* llvm-DenseMapInfo.h in Headers */, + C2E6D3FC2225DCF00059DFAA /* DenseMapExtras.h in Headers */, 838486260D6D68F000CEA253 /* List.h in Headers */, 838485C30D6D687300CEA253 /* maptable.h in Headers */, 838486280D6D6A2400CEA253 /* message.h in Headers */, @@ -393,28 +497,38 @@ 838485F00D6D68A200CEA253 /* objc-auto.h in Headers */, 838485F40D6D68A200CEA253 /* objc-class.h in Headers */, 838485F60D6D68A200CEA253 /* objc-config.h in Headers */, + 6E1475EE21DFDB1B001357EA /* llvm-MathExtras.h in Headers */, 838485F80D6D68A200CEA253 /* objc-exception.h in Headers */, 83BE02E80FCCB24D00661494 /* objc-file-old.h in Headers */, 83BE02E90FCCB24D00661494 /* objc-file.h in Headers */, + 75A9504F202BAA0600D7D56F /* objc-locks-new.h in Headers */, 834266D80E665A8B002E4DA2 /* objc-gdb.h in Headers */, 838485FB0D6D68A200CEA253 /* objc-initialize.h in Headers */, + 7593EC58202248E50046AB96 /* objc-object.h in Headers */, 83112ED40F00599600A5FBAF /* objc-internal.h in Headers */, 838485FE0D6D68A200CEA253 /* objc-load.h in Headers */, 838486000D6D68A200CEA253 /* objc-loadmethod.h in Headers */, + 75A95053202BAC4100D7D56F /* objc-lockdebug.h in Headers */, 831C85D50E10CF850066E64C /* objc-os.h in Headers */, 838486030D6D68A200CEA253 /* objc-private.h in Headers */, 393CEAC60DC69E67000B69DE /* objc-references.h in Headers */, + 6E1475EB21DFDB1B001357EA /* llvm-DenseMap.h in Headers */, 838486070D6D68A200CEA253 /* objc-runtime-new.h in Headers */, 83BE02EA0FCCB24D00661494 /* objc-runtime-old.h in Headers */, 8384860A0D6D68A200CEA253 /* objc-runtime.h in Headers */, 8384860C0D6D68A200CEA253 /* objc-sel-set.h in Headers */, + 6E7B0862232DE7CA00689009 /* PointerUnion.h in Headers */, + 7213C36321FA7C730090A271 /* NSObject-internal.h in Headers */, 838486100D6D68A200CEA253 /* objc-sync.h in Headers */, + 83D92696212254CF00299F69 /* isa.h in Headers */, 838486130D6D68A200CEA253 /* objc.h in Headers */, + 83D9269821225A7400299F69 /* arm64-asm.h in Headers */, 838486140D6D68A200CEA253 /* Object.h in Headers */, 8384861E0D6D68A800CEA253 /* Protocol.h in Headers */, 838486200D6D68A800CEA253 /* runtime.h in Headers */, 39ABD72312F0B61800D1054C /* objc-weak.h in Headers */, 83F4B52815E843B100E0926F /* NSObjCRuntime.h in Headers */, + 6ECD0B1F2244999E00910D88 /* llvm-DenseSet.h in Headers */, 83F4B52915E843B100E0926F /* NSObject.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; @@ -435,12 +549,30 @@ buildRules = ( ); dependencies = ( + F9BCC729205C6A0900DD9AFC /* PBXTargetDependency */, ); name = objc; productName = objc; productReference = D2AAC0630554660B00DB518D /* libobjc.A.dylib */; productType = "com.apple.product-type.library.dynamic"; }; + F9BCC6CA205C68E800DD9AFC /* objc-trampolines */ = { + isa = PBXNativeTarget; + buildConfigurationList = F9BCC724205C68E800DD9AFC /* Build configuration list for PBXNativeTarget "objc-trampolines" */; + buildPhases = ( + 8306440820D24A5300E356D2 /* Headers */, + F9BCC6EF205C68E800DD9AFC /* Sources */, + F9BCC721205C68E800DD9AFC /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = "objc-trampolines"; + productName = objc; + productReference = F9BCC727205C68E800DD9AFC /* libobjc-trampolines.dylib */; + productType = "com.apple.product-type.library.dynamic"; + }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ @@ -450,6 +582,11 @@ BuildIndependentTargetsInParallel = NO; LastUpgradeCheck = 0440; TargetAttributes = { + 834F9B01212E560100F95A54 = { + CreatedOnToolsVersion = 10.0; + DevelopmentTeam = 59GAB85EFG; + ProvisioningStyle = Automatic; + }; 837F67A81A771F63004D34FA = { CreatedOnToolsVersion = 6.3; }; @@ -471,6 +608,8 @@ targets = ( D2AAC0620554660B00DB518D /* objc */, 837F67A81A771F63004D34FA /* objc-simulator */, + F9BCC6CA205C68E800DD9AFC /* objc-trampolines */, + 834F9B01212E560100F95A54 /* objc4_tests */, ); }; /* End PBXProject section */ @@ -489,7 +628,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "set -x\n/usr/bin/xcrun -sdk macosx clang++ -Wall -mmacosx-version-min=10.9 -arch x86_64 -std=c++11 \"${SRCROOT}/markgc.cpp\" -o \"${BUILT_PRODUCTS_DIR}/markgc\"\n\"${BUILT_PRODUCTS_DIR}/markgc\" \"${BUILT_PRODUCTS_DIR}/libobjc.A.dylib\""; + shellScript = "set -x\n/usr/bin/xcrun -sdk macosx.internal clang++ -Wall -mmacosx-version-min=10.12 -arch x86_64 -std=c++11 \"${SRCROOT}/markgc.cpp\" -o \"${BUILT_PRODUCTS_DIR}/markgc\"\n\"${BUILT_PRODUCTS_DIR}/markgc\" \"${BUILT_PRODUCTS_DIR}/libobjc.A.dylib\""; }; 830F2AFA0D73BC5800392440 /* Run Script (symlink) */ = { isa = PBXShellScriptBuildPhase; @@ -503,7 +642,25 @@ ); runOnlyForDeploymentPostprocessing = 1; shellPath = /bin/sh; - shellScript = "cd \"${INSTALL_DIR}\"\n/bin/ln -s libobjc.A.dylib libobjc.dylib\n"; + shellScript = "cd \"${INSTALL_DIR}\"\n/bin/ln -s libobjc.A.dylib libobjc.dylib\n\nTBD_UPPER=`echo ${GENERATE_TEXT_BASED_STUBS} | tr a-z A-Z`\n\nif [ ${TBD_UPPER} = \"YES\" ] || [ ${TBD_UPPER} = \"TRUE\" ] || [ ${TBD_UPPER} = \"1\" ]; then\nGENERATE_TBD=1\nfi\n\nif [ ${GENERATE_TBD} ]; then\n /bin/ln -s libobjc.A.tbd libobjc.tbd\nfi\n"; + }; + 834F9B05212E561400F95A54 /* Run Script (build tests) */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + ); + name = "Run Script (build tests)"; + outputFileListPaths = ( + ); + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "set -x\nset -e\n\n# Set this to empty, or a space-separated list of tests to run.\ntestfiles=\"\"\n\n# Location inside DSTROOT of our test files and our BATS config file.\ntestdir=/AppleInternal/CoreOS/tests/objc4\nconfigdir=/AppleInternal/CoreOS/BATS/unit_tests\n\nmkdir -p ${DSTROOT}${testdir}\nmkdir -p ${DSTROOT}${configdir}\n\n# Common test.pl args for building and running.\ntestargs=\"ARCH=`echo ${ARCHS} | tr ' ' ','` OS=${PLATFORM_NAME} MEM=mrc,arc LANGUAGE=c,c++,objc,objc++ RUN=0 VERBOSE=1 BATS=1 ${testfiles}\"\n\n# Build the tests and BATS plist into DSTROOT.\nperl ${SRCROOT}/test/test.pl $testargs BUILD=1 RUN=0\n\n# Move the BATS plist where BATS expects it, and convert it to binary format.\nmv ${DSTROOT}${testdir}/objc4.plist ${DSTROOT}${configdir}\nplutil -convert binary1 ${DSTROOT}${configdir}/objc4.plist\n\n# Copy test sources to DSTROOT; running the test requires reading them again.\ncp -R ${SRCROOT}/test ${DSTROOT}${testdir}/test\n\n# Don't copy gcfiles because XBS chokes on them.\n# Don't copy other cruft because verifiers dislike them. (This doesn't matter for submissions but does affect local buildit builds.)\nrm -rf ${DSTROOT}${testdir}/test/gcfiles\nrm -rf ${DSTROOT}${testdir}/test/*~\nrm -rf ${DSTROOT}${testdir}/test/\\#*\\#\nrm -rf ${DSTROOT}${testdir}/test/.\\#*\n"; }; /* End PBXShellScriptBuildPhase section */ @@ -544,26 +701,31 @@ 831C85D60E10CF850066E64C /* objc-os.mm in Sources */, 87BB4EA70EC39854005D08E1 /* objc-probes.d in Sources */, 83BE02E40FCCB23400661494 /* objc-file-old.mm in Sources */, - E8923DA1116AB2820071B552 /* a1a2-blocktramps-i386.s in Sources */, - E8923DA2116AB2820071B552 /* a1a2-blocktramps-x86_64.s in Sources */, - E8923DA3116AB2820071B552 /* a2a3-blocktramps-i386.s in Sources */, - E8923DA4116AB2820071B552 /* a2a3-blocktramps-x86_64.s in Sources */, E8923DA5116AB2820071B552 /* objc-block-trampolines.mm in Sources */, 83B1A8BE0FF1AC0D0019EA5B /* objc-msg-simulator-i386.s in Sources */, 83EB007B121C9EC200B92C16 /* objc-sel-table.s in Sources */, - 8383A3A3122600E9009290B8 /* a1a2-blocktramps-arm.s in Sources */, - 8383A3A4122600E9009290B8 /* a2a3-blocktramps-arm.s in Sources */, 39ABD72412F0B61800D1054C /* objc-weak.mm in Sources */, 83D49E4F13C7C84F0057F1DD /* objc-msg-arm64.s in Sources */, - 8379996E13CBAF6F007C2B5F /* a1a2-blocktramps-arm64.s in Sources */, 9672F7EE14D5F488007CEC96 /* NSObject.mm in Sources */, 83725F4A14CA5BFA0014370E /* objc-opt.mm in Sources */, + 6EACB844232C97B900CE9176 /* objc-zalloc.mm in Sources */, 83F550E0155E030800E95D3B /* objc-cache-old.mm in Sources */, 834DF8B715993EE1002F2BC9 /* objc-sel-old.mm in Sources */, 83C9C3391668B50E00F4E544 /* objc-msg-simulator-x86_64.s in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; + F9BCC6EF205C68E800DD9AFC /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 83EF5E9C20D2299E00F486A4 /* objc-blocktramps-arm.s in Sources */, + 83EF5E9820D2298400F486A4 /* objc-blocktramps-i386.s in Sources */, + 83EF5E9920D2298400F486A4 /* objc-blocktramps-x86_64.s in Sources */, + F9BCC71B205C68E800DD9AFC /* objc-blocktramps-arm64.s in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ @@ -572,14 +734,22 @@ target = D2AAC0620554660B00DB518D /* objc */; targetProxy = 837F67AC1A771F6E004D34FA /* PBXContainerItemProxy */; }; + F9BCC729205C6A0900DD9AFC /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = F9BCC6CA205C68E800DD9AFC /* objc-trampolines */; + targetProxy = F9BCC728205C6A0900DD9AFC /* PBXContainerItemProxy */; + }; /* End PBXTargetDependency section */ /* Begin XCBuildConfiguration section */ 1DEB914B08733D8E0010E9CD /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - ARCHS = "$(ARCHS_STANDARD_32_64_BIT)"; + COPY_HEADERS_RUN_UNIFDEF = YES; + COPY_HEADERS_UNIFDEF_FLAGS = "-UBUILD_FOR_OSX"; + "COPY_HEADERS_UNIFDEF_FLAGS[sdk=macosx*]" = "-DBUILD_FOR_OSX"; COPY_PHASE_STRIP = NO; + DEPLOYMENT_LOCATION = YES; DYLIB_CURRENT_VERSION = 228; EXECUTABLE_PREFIX = lib; GCC_CW_ASM_SYNTAX = NO; @@ -593,10 +763,13 @@ /System/Library/Frameworks/System.framework/PrivateHeaders, ); INSTALL_PATH = /usr/lib; + IS_ZIPPERED = YES; + LLVM_LTO = NO; ORDER_FILE = "$(SDKROOT)/AppleInternal/OrderFiles/libobjc.order"; "ORDER_FILE[sdk=iphonesimulator*]" = ""; OTHER_CFLAGS = ( "-fdollars-in-identifiers", + "-fno-objc-convert-messages-to-runtime-calls", "$(OTHER_CFLAGS)", ); "OTHER_LDFLAGS[sdk=iphoneos*][arch=*]" = ( @@ -610,11 +783,20 @@ __objc_data, "-Xlinker", 0x1000, + "-Xlinker", + "-interposable_list", + "-Xlinker", + interposable.txt, + ); + "OTHER_LDFLAGS[sdk=iphonesimulator*][arch=*]" = ( + "-lc++abi", + "-Xlinker", + "-interposable_list", + "-Xlinker", + interposable.txt, ); - "OTHER_LDFLAGS[sdk=iphonesimulator*][arch=*]" = "-lc++abi"; "OTHER_LDFLAGS[sdk=macosx*]" = ( "-lCrashReporterClient", - "-lauto", "-lc++abi", "-Xlinker", "-sectalign", @@ -624,10 +806,17 @@ __objc_data, "-Xlinker", 0x1000, + "-Xlinker", + "-interposable_list", + "-Xlinker", + interposable.txt, ); + OTHER_TAPI_FLAGS = "-exclude-public-header $(DSTROOT)/usr/include/objc/ObjectiveC.apinotes -exclude-public-header $(DSTROOT)/usr/include/objc/module.modulemap -Xparser -Wno-deprecated-declarations -Xparser -Wno-unavailable-declarations -Xparser -D_OBJC_PRIVATE_H_=1 -DOBJC_DECLARE_SYMBOLS=1"; PRIVATE_HEADERS_FOLDER_PATH = /usr/local/include/objc; PRODUCT_NAME = objc.A; PUBLIC_HEADERS_FOLDER_PATH = /usr/include/objc; + SUPPORTS_TEXT_BASED_API = YES; + TAPI_VERIFY_MODE = Pedantic; UNEXPORTED_SYMBOLS_FILE = unexported_symbols; }; name = Debug; @@ -635,6 +824,10 @@ 1DEB914C08733D8E0010E9CD /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + COPY_HEADERS_RUN_UNIFDEF = YES; + COPY_HEADERS_UNIFDEF_FLAGS = "-UBUILD_FOR_OSX"; + "COPY_HEADERS_UNIFDEF_FLAGS[sdk=macosx*]" = "-DBUILD_FOR_OSX"; + DEPLOYMENT_LOCATION = YES; DYLIB_CURRENT_VERSION = 228; EXECUTABLE_PREFIX = lib; GCC_CW_ASM_SYNTAX = NO; @@ -647,10 +840,12 @@ /System/Library/Frameworks/System.framework/PrivateHeaders, ); INSTALL_PATH = /usr/lib; + IS_ZIPPERED = YES; ORDER_FILE = "$(SDKROOT)/AppleInternal/OrderFiles/libobjc.order"; "ORDER_FILE[sdk=iphonesimulator*]" = ""; OTHER_CFLAGS = ( "-fdollars-in-identifiers", + "-fno-objc-convert-messages-to-runtime-calls", "$(OTHER_CFLAGS)", ); "OTHER_LDFLAGS[sdk=iphoneos*][arch=*]" = ( @@ -664,11 +859,20 @@ __objc_data, "-Xlinker", 0x1000, + "-Xlinker", + "-interposable_list", + "-Xlinker", + interposable.txt, + ); + "OTHER_LDFLAGS[sdk=iphonesimulator*][arch=*]" = ( + "-lc++abi", + "-Xlinker", + "-interposable_list", + "-Xlinker", + interposable.txt, ); - "OTHER_LDFLAGS[sdk=iphonesimulator*][arch=*]" = "-lc++abi"; "OTHER_LDFLAGS[sdk=macosx*]" = ( "-lCrashReporterClient", - "-lauto", "-lc++abi", "-Xlinker", "-sectalign", @@ -678,28 +882,41 @@ __objc_data, "-Xlinker", 0x1000, + "-Xlinker", + "-interposable_list", + "-Xlinker", + interposable.txt, ); + OTHER_TAPI_FLAGS = "-exclude-public-header $(DSTROOT)/usr/include/objc/ObjectiveC.apinotes -exclude-public-header $(DSTROOT)/usr/include/objc/module.modulemap -Xparser -Wno-deprecated-declarations -Xparser -Wno-unavailable-declarations -Xparser -D_OBJC_PRIVATE_H_=1 -DOBJC_DECLARE_SYMBOLS=1"; PRIVATE_HEADERS_FOLDER_PATH = /usr/local/include/objc; PRODUCT_NAME = objc.A; PUBLIC_HEADERS_FOLDER_PATH = /usr/include/objc; + SUPPORTS_TEXT_BASED_API = YES; + TAPI_VERIFY_MODE = Pedantic; UNEXPORTED_SYMBOLS_FILE = unexported_symbols; + WARNING_CFLAGS = ( + "$(inherited)", + "-Wglobal-constructors", + ); }; name = Release; }; 1DEB914F08733D8E0010E9CD /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; CLANG_CXX_LIBRARY = "libc++"; CLANG_LINK_OBJC_RUNTIME = NO; CLANG_OBJC_RUNTIME = NO; DEBUG_INFORMATION_FORMAT = dwarf; - EXCLUDED_INSTALLSRC_SUBDIRECTORY_PATTERNS = "$(inherited) test"; GCC_ENABLE_CPP_EXCEPTIONS = NO; GCC_ENABLE_CPP_RTTI = NO; GCC_INLINES_ARE_PRIVATE_EXTERN = YES; GCC_OPTIMIZATION_LEVEL = 0; - GCC_PREPROCESSOR_DEFINITIONS = "OS_OBJECT_USE_OBJC=0"; + GCC_PREPROCESSOR_DEFINITIONS = ( + "OS_OBJECT_USE_OBJC=0", + "OBJC_IS_DEBUG_BUILD=1", + ); GCC_STRICT_ALIASING = YES; GCC_SYMBOLS_PRIVATE_EXTERN = YES; GCC_VERSION = com.apple.compilers.llvm.clang.1_0; @@ -709,12 +926,15 @@ GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_SHADOW = YES; GCC_WARN_UNUSED_VARIABLE = YES; + LLVM_LTO = YES; OTHER_CFLAGS = ""; "OTHER_CFLAGS[arch=x86_64]" = "-fobjc-legacy-dispatch"; OTHER_CPLUSPLUSFLAGS = ( "$(OTHER_CFLAGS)", "-D_LIBCPP_VISIBLE=\"\"", ); + SDKROOT = macosx.internal; + SUPPORTED_PLATFORMS = "macosx iphoneos appletvos watchos"; WARNING_CFLAGS = ( "-Wall", "-Wextra", @@ -730,12 +950,11 @@ 1DEB915008733D8E0010E9CD /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; CLANG_CXX_LIBRARY = "libc++"; CLANG_LINK_OBJC_RUNTIME = NO; CLANG_OBJC_RUNTIME = NO; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - EXCLUDED_INSTALLSRC_SUBDIRECTORY_PATTERNS = "$(inherited) test"; GCC_ENABLE_CPP_EXCEPTIONS = NO; GCC_ENABLE_CPP_RTTI = NO; GCC_INLINES_ARE_PRIVATE_EXTERN = YES; @@ -752,7 +971,8 @@ GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_SHADOW = YES; GCC_WARN_UNUSED_VARIABLE = YES; - "OTHER_CFLAGS[arch=i386]" = "-momit-leaf-frame-pointer"; + LLVM_LTO = YES; + OTHER_CFLAGS = "-momit-leaf-frame-pointer"; "OTHER_CFLAGS[arch=x86_64]" = ( "-momit-leaf-frame-pointer", "-fobjc-legacy-dispatch", @@ -761,6 +981,8 @@ "$(OTHER_CFLAGS)", "-D_LIBCPP_VISIBLE=\"\"", ); + SDKROOT = macosx.internal; + SUPPORTED_PLATFORMS = "macosx iphoneos appletvos watchos"; WARNING_CFLAGS = ( "-Wall", "-Wextra", @@ -773,6 +995,24 @@ }; name = Release; }; + 834F9B02212E560200F95A54 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 59GAB85EFG; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 834F9B03212E560200F95A54 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 59GAB85EFG; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; 837F67AA1A771F63004D34FA /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -787,6 +1027,78 @@ }; name = Release; }; + F9BCC725205C68E800DD9AFC /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + COPY_HEADERS_RUN_UNIFDEF = YES; + COPY_HEADERS_UNIFDEF_FLAGS = "-UBUILD_FOR_OSX"; + "COPY_HEADERS_UNIFDEF_FLAGS[sdk=macosx*]" = "-DBUILD_FOR_OSX"; + COPY_PHASE_STRIP = NO; + DYLIB_CURRENT_VERSION = 228; + EXECUTABLE_PREFIX = lib; + GCC_CW_ASM_SYNTAX = NO; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS = NO; + HEADER_SEARCH_PATHS = ( + "$(DSTROOT)/usr/include/**", + "$(DSTROOT)/usr/local/include/**", + "$(CONFIGURATION_BUILD_DIR)/usr/include/**", + "$(CONFIGURATION_BUILD_DIR)/usr/local/include/**", + /System/Library/Frameworks/System.framework/PrivateHeaders, + ); + INSTALL_PATH = /usr/lib; + IS_ZIPPERED = YES; + OTHER_CFLAGS = ( + "-fdollars-in-identifiers", + "$(OTHER_CFLAGS)", + ); + OTHER_LDFLAGS = ( + "-Xlinker", + "-not_for_dyld_shared_cache", + ); + PRIVATE_HEADERS_FOLDER_PATH = /usr/local/include/objc; + PRODUCT_NAME = "$(TARGET_NAME)"; + PUBLIC_HEADERS_FOLDER_PATH = /usr/include/objc; + SUPPORTS_TEXT_BASED_API = YES; + TAPI_VERIFY_MODE = Pedantic; + }; + name = Debug; + }; + F9BCC726205C68E800DD9AFC /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + COPY_HEADERS_RUN_UNIFDEF = YES; + COPY_HEADERS_UNIFDEF_FLAGS = "-UBUILD_FOR_OSX"; + "COPY_HEADERS_UNIFDEF_FLAGS[sdk=macosx*]" = "-DBUILD_FOR_OSX"; + DYLIB_CURRENT_VERSION = 228; + EXECUTABLE_PREFIX = lib; + GCC_CW_ASM_SYNTAX = NO; + GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS = NO; + HEADER_SEARCH_PATHS = ( + "$(DSTROOT)/usr/include/**", + "$(DSTROOT)/usr/local/include/**", + "$(CONFIGURATION_BUILD_DIR)/usr/include/**", + "$(CONFIGURATION_BUILD_DIR)/usr/local/include/**", + /System/Library/Frameworks/System.framework/PrivateHeaders, + ); + INSTALL_PATH = /usr/lib; + IS_ZIPPERED = YES; + OTHER_CFLAGS = ( + "-fdollars-in-identifiers", + "$(OTHER_CFLAGS)", + ); + OTHER_LDFLAGS = ( + "-Xlinker", + "-not_for_dyld_shared_cache", + ); + PRIVATE_HEADERS_FOLDER_PATH = /usr/local/include/objc; + PRODUCT_NAME = "$(TARGET_NAME)"; + PUBLIC_HEADERS_FOLDER_PATH = /usr/include/objc; + SUPPORTS_TEXT_BASED_API = YES; + TAPI_VERIFY_MODE = Pedantic; + }; + name = Release; + }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ @@ -808,6 +1120,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 834F9B04212E560200F95A54 /* Build configuration list for PBXAggregateTarget "objc4_tests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 834F9B02212E560200F95A54 /* Debug */, + 834F9B03212E560200F95A54 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 837F67A91A771F63004D34FA /* Build configuration list for PBXAggregateTarget "objc-simulator" */ = { isa = XCConfigurationList; buildConfigurations = ( @@ -817,6 +1138,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + F9BCC724205C68E800DD9AFC /* Build configuration list for PBXNativeTarget "objc-trampolines" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F9BCC725205C68E800DD9AFC /* Debug */, + F9BCC726205C68E800DD9AFC /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; /* End XCConfigurationList section */ }; rootObject = 08FB7793FE84155DC02AAC07 /* Project object */; diff --git a/objcrt/objcrt.vcproj b/objcrt/objcrt.vcproj old mode 100755 new mode 100644 diff --git a/prebuild.bat b/prebuild.bat old mode 100755 new mode 100644 diff --git a/runtime/DenseMapExtras.h b/runtime/DenseMapExtras.h new file mode 100644 index 0000000..19dc4ee --- /dev/null +++ b/runtime/DenseMapExtras.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2019 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +#ifndef DENSEMAPEXTRAS_H +#define DENSEMAPEXTRAS_H + +#include "llvm-DenseMap.h" +#include "llvm-DenseSet.h" + +namespace objc { + +// We cannot use a C++ static initializer to initialize certain globals because +// libc calls us before our C++ initializers run. We also don't want a global +// pointer to some globals because of the extra indirection. +// +// ExplicitInit / LazyInit wrap doing it the hard way. +template +class ExplicitInit { + alignas(Type) uint8_t _storage[sizeof(Type)]; + +public: + template + void init(Ts &&... Args) { + new (_storage) Type(std::forward(Args)...); + } + + Type &get() { + return *reinterpret_cast(_storage); + } +}; + +template +class LazyInit { + alignas(Type) uint8_t _storage[sizeof(Type)]; + bool _didInit; + +public: + template + Type *get(bool allowCreate, Ts &&... Args) { + if (!_didInit) { + if (!allowCreate) { + return nullptr; + } + new (_storage) Type(std::forward(Args)...); + _didInit = true; + } + return reinterpret_cast(_storage); + } +}; + +// Convenience class for Dense Maps & Sets +template +class ExplicitInitDenseMap : public ExplicitInit> { }; + +template +class LazyInitDenseMap : public LazyInit> { }; + +template +class ExplicitInitDenseSet : public ExplicitInit> { }; + +template +class LazyInitDenseSet : public LazyInit> { }; + +} // namespace objc + +#endif /* DENSEMAPEXTRAS_H */ diff --git a/runtime/Messengers.subproj/objc-msg-arm.S b/runtime/Messengers.subproj/objc-msg-arm.S index bd88c6a..b1a9aec 100644 --- a/runtime/Messengers.subproj/objc-msg-arm.S +++ b/runtime/Messengers.subproj/objc-msg-arm.S @@ -29,6 +29,8 @@ #ifdef __arm__ #include +#include "objc-config.h" +#include "isa.h" #ifndef _ARM_ARCH_7 # error requires armv7 @@ -67,25 +69,6 @@ #endif -// Define SUPPORT_INDEXED_ISA for targets which store the class in the ISA as -// an index in to a class table. -// Note, keep this in sync with objc-config.h. -// FIXME: Remove this duplication. We should get this from objc-config.h. -#if __ARM_ARCH_7K__ >= 2 -# define SUPPORT_INDEXED_ISA 1 -#else -# define SUPPORT_INDEXED_ISA 0 -#endif - -// Note, keep these in sync with objc-private.h -#define ISA_INDEX_IS_NPI 1 -#define ISA_INDEX_MASK 0x0001FFFC -#define ISA_INDEX_SHIFT 2 -#define ISA_INDEX_BITS 15 -#define ISA_INDEX_COUNT (1 << ISA_INDEX_BITS) -#define ISA_INDEX_MAGIC_MASK 0x001E0001 -#define ISA_INDEX_MAGIC_VALUE 0x001C0001 - .syntax unified #define MI_EXTERN(var) \ @@ -119,98 +102,35 @@ _objc_indexed_classes: -// _objc_entryPoints and _objc_exitPoints are used by method dispatch +// _objc_restartableRanges is used by method dispatch // caching code to figure out whether any threads are actively // in the cache for dispatching. The labels surround the asm code // that do cache lookups. The tables are zero-terminated. -.align 2 -.private_extern _objc_entryPoints -_objc_entryPoints: - .long _cache_getImp - .long _objc_msgSend - .long _objc_msgSend_stret - .long _objc_msgSendSuper - .long _objc_msgSendSuper_stret - .long _objc_msgSendSuper2 - .long _objc_msgSendSuper2_stret - .long _objc_msgLookup - .long _objc_msgLookup_stret - .long _objc_msgLookupSuper2 - .long _objc_msgLookupSuper2_stret - .long 0 - -.private_extern _objc_exitPoints -_objc_exitPoints: - .long LExit_cache_getImp - .long LExit_objc_msgSend - .long LExit_objc_msgSend_stret - .long LExit_objc_msgSendSuper - .long LExit_objc_msgSendSuper_stret - .long LExit_objc_msgSendSuper2 - .long LExit_objc_msgSendSuper2_stret - .long LExit_objc_msgLookup - .long LExit_objc_msgLookup_stret - .long LExit_objc_msgLookupSuper2 - .long LExit_objc_msgLookupSuper2_stret - .long 0 - - -/******************************************************************** -* List every exit insn from every messenger for debugger use. -* Format: -* ( -* 1 word instruction's address -* 1 word type (ENTER or FAST_EXIT or SLOW_EXIT or NIL_EXIT) -* ) -* 1 word zero -* -* ENTER is the start of a dispatcher -* FAST_EXIT is method dispatch -* SLOW_EXIT is uncached method lookup -* NIL_EXIT is returning zero from a message sent to nil -* These must match objc-gdb.h. -********************************************************************/ - -#define ENTER 1 -#define FAST_EXIT 2 -#define SLOW_EXIT 3 -#define NIL_EXIT 4 - -.section __DATA,__objc_msg_break -.globl _gdb_objc_messenger_breakpoints -_gdb_objc_messenger_breakpoints: -// contents populated by the macros below - -.macro MESSENGER_START -7: - .section __DATA,__objc_msg_break - .long 7b - .long ENTER - .text -.endmacro -.macro MESSENGER_END_FAST -7: - .section __DATA,__objc_msg_break - .long 7b - .long FAST_EXIT - .text -.endmacro -.macro MESSENGER_END_SLOW -7: - .section __DATA,__objc_msg_break - .long 7b - .long SLOW_EXIT - .text -.endmacro -.macro MESSENGER_END_NIL -7: - .section __DATA,__objc_msg_break - .long 7b - .long NIL_EXIT - .text +.macro RestartableEntry + .long LLookupStart$0 + .long 0 + .short LLookupEnd$0 - LLookupStart$0 + .short 0xffff // poor ol' armv7 doesn't support kernel based recovery + .long 0 .endmacro + .align 4 + .private_extern _objc_restartableRanges +_objc_restartableRanges: + RestartableEntry _cache_getImp + RestartableEntry _objc_msgSend + RestartableEntry _objc_msgSend_stret + RestartableEntry _objc_msgSendSuper + RestartableEntry _objc_msgSendSuper_stret + RestartableEntry _objc_msgSendSuper2 + RestartableEntry _objc_msgSendSuper2_stret + RestartableEntry _objc_msgLookup + RestartableEntry _objc_msgLookup_stret + RestartableEntry _objc_msgLookupSuper2 + RestartableEntry _objc_msgLookupSuper2_stret + .fill 16, 1, 0 + /******************************************************************** * Names for relative labels @@ -218,7 +138,7 @@ _gdb_objc_messenger_breakpoints: * Reserved labels: 6: 7: 8: 9: ********************************************************************/ // 6: used by CacheLookup -// 7: used by MI_GET_ADDRESS etc and MESSENGER_START etc +// 7: used by MI_GET_ADDRESS etc // 8: used by CacheLookup #define LNilReceiver 9 #define LNilReceiver_f 9f @@ -249,6 +169,10 @@ _gdb_objc_messenger_breakpoints: #define CACHE 8 #define CACHE_MASK 12 +/* Field offsets in method cache bucket */ +#define CACHED_SEL 0 +#define CACHED_IMP 4 + /* Selected field offsets in method structure */ #define METHOD_NAME 0 #define METHOD_TYPES 4 @@ -300,8 +224,8 @@ LExit$0: ///////////////////////////////////////////////////////////////////// // -// CacheLookup NORMAL|STRET -// CacheLookup2 NORMAL|STRET +// CacheLookup NORMAL|STRET +// CacheLookup2 NORMAL|STRET // // Locate the implementation for a selector in a class's method cache. // @@ -318,7 +242,32 @@ LExit$0: ///////////////////////////////////////////////////////////////////// .macro CacheLookup - + // + // Restart protocol: + // + // As soon as we're past the LLookupStart$1 label we may have loaded + // an invalid cache pointer or mask. + // + // When task_restartable_ranges_synchronize() is called, + // (or when a signal hits us) before we're past LLookupEnd$1, + // then our PC will be reset to LCacheMiss$1 which forcefully + // jumps to the cache-miss codepath. + // + // It is assumed that the CacheMiss codepath starts right at the end + // of CacheLookup2 and will re-setup the registers to meet the cache-miss + // requirements: + // + // GETIMP: + // The cache-miss is just returning NULL (setting r9 to 0) + // + // NORMAL and STRET: + // - r0 or r1 (STRET) contains the receiver + // - r1 or r2 (STRET) contains the selector + // - r9 contains the isa (reloaded from r0/r1) + // - other registers are set as per calling conventions + // +LLookupStart$1: + ldrh r12, [r9, #CACHE_MASK] // r12 = mask ldr r9, [r9, #CACHE] // r9 = buckets .if $0 == STRET @@ -327,7 +276,7 @@ LExit$0: and r12, r12, r1 // r12 = index = SEL & mask .endif add r9, r9, r12, LSL #3 // r9 = bucket = buckets+index*8 - ldr r12, [r9] // r12 = bucket->sel + ldr r12, [r9, #CACHED_SEL] // r12 = bucket->sel 6: .if $0 == STRET teq r12, r2 @@ -335,7 +284,7 @@ LExit$0: teq r12, r1 .endif bne 8f - ldr r12, [r9, #4] // r12 = bucket->imp + ldr r12, [r9, #CACHED_IMP] // r12 = bucket->imp .if $0 == STRET tst r12, r12 // set ne for stret forwarding @@ -346,15 +295,19 @@ LExit$0: .endmacro .macro CacheLookup2 - +#if CACHED_SEL != 0 +# error this code requires that SEL be at offset 0 +#endif 8: cmp r12, #1 - blo 8f // if (bucket->sel == 0) cache miss + blo LCacheMiss$1 // if (bucket->sel == 0) cache miss it eq // if (bucket->sel == 1) cache wrap - ldreq r9, [r9, #4] // bucket->imp is before first bucket + ldreq r9, [r9, #CACHED_IMP] // bucket->imp is before first bucket ldr r12, [r9, #8]! // r12 = (++bucket)->sel b 6b -8: + +LLookupEnd$1: +LCacheMiss$1: .endmacro @@ -377,7 +330,7 @@ LExit$0: // Note: We are doing a little wasted work here to load values we might not // need. Branching turns out to be even worse when performance was measured. MI_GET_ADDRESS(r12, _objc_indexed_classes) - tst.w r9, #ISA_INDEX_IS_NPI + tst.w r9, #ISA_INDEX_IS_NPI_MASK itt ne ubfxne r9, r9, #ISA_INDEX_SHIFT, #ISA_INDEX_BITS ldrne.w r9, [r12, r9, lsl #2] @@ -399,12 +352,12 @@ LExit$0: STATIC_ENTRY _cache_getImp mov r9, r0 - CacheLookup NORMAL + CacheLookup NORMAL, _cache_getImp // cache hit, IMP in r12 mov r0, r12 bx lr // return imp - CacheLookup2 GETIMP + CacheLookup2 GETIMP, _cache_getImp // cache miss, return nil mov r0, #0 bx lr @@ -425,22 +378,19 @@ LExit$0: ********************************************************************/ ENTRY _objc_msgSend - MESSENGER_START cbz r0, LNilReceiver_f ldr r9, [r0] // r9 = self->isa GetClassFromIsa // r9 = class - CacheLookup NORMAL + CacheLookup NORMAL, _objc_msgSend // cache hit, IMP in r12, eq already set for nonstret forwarding - MESSENGER_END_FAST bx r12 // call imp - CacheLookup2 NORMAL + CacheLookup2 NORMAL, _objc_msgSend // cache miss ldr r9, [r0] // r9 = self->isa GetClassFromIsa // r9 = class - MESSENGER_END_SLOW b __objc_msgSend_uncached LNilReceiver: @@ -449,7 +399,6 @@ LNilReceiver: mov r2, #0 mov r3, #0 FP_RETURN_ZERO - MESSENGER_END_NIL bx lr END_ENTRY _objc_msgSend @@ -461,11 +410,11 @@ LNilReceiver: ldr r9, [r0] // r9 = self->isa GetClassFromIsa // r9 = class - CacheLookup NORMAL + CacheLookup NORMAL, _objc_msgLookup // cache hit, IMP in r12, eq already set for nonstret forwarding bx lr - CacheLookup2 NORMAL + CacheLookup2 NORMAL, _objc_msgLookup // cache miss ldr r9, [r0] // r9 = self->isa GetClassFromIsa // r9 = class @@ -504,26 +453,22 @@ LNilReceiver: ********************************************************************/ ENTRY _objc_msgSend_stret - MESSENGER_START cbz r1, LNilReceiver_f ldr r9, [r1] // r9 = self->isa GetClassFromIsa // r9 = class - CacheLookup STRET + CacheLookup STRET, _objc_msgSend_stret // cache hit, IMP in r12, ne already set for stret forwarding - MESSENGER_END_FAST bx r12 - CacheLookup2 STRET + CacheLookup2 STRET, _objc_msgSend_stret // cache miss ldr r9, [r1] // r9 = self->isa GetClassFromIsa // r9 = class - MESSENGER_END_SLOW b __objc_msgSend_stret_uncached LNilReceiver: - MESSENGER_END_NIL bx lr END_ENTRY _objc_msgSend_stret @@ -535,11 +480,11 @@ LNilReceiver: ldr r9, [r1] // r9 = self->isa GetClassFromIsa // r9 = class - CacheLookup STRET + CacheLookup STRET, _objc_msgLookup_stret // cache hit, IMP in r12, ne already set for stret forwarding bx lr - CacheLookup2 STRET + CacheLookup2 STRET, _objc_msgLookup_stret // cache miss ldr r9, [r1] // r9 = self->isa GetClassFromIsa // r9 = class @@ -569,20 +514,17 @@ LNilReceiver: ********************************************************************/ ENTRY _objc_msgSendSuper - MESSENGER_START ldr r9, [r0, #CLASS] // r9 = struct super->class - CacheLookup NORMAL + CacheLookup NORMAL, _objc_msgSendSuper // cache hit, IMP in r12, eq already set for nonstret forwarding ldr r0, [r0, #RECEIVER] // load real receiver - MESSENGER_END_FAST bx r12 // call imp - CacheLookup2 NORMAL + CacheLookup2 NORMAL, _objc_msgSendSuper // cache miss ldr r9, [r0, #CLASS] // r9 = struct super->class ldr r0, [r0, #RECEIVER] // load real receiver - MESSENGER_END_SLOW b __objc_msgSend_uncached END_ENTRY _objc_msgSendSuper @@ -598,22 +540,19 @@ LNilReceiver: ********************************************************************/ ENTRY _objc_msgSendSuper2 - MESSENGER_START ldr r9, [r0, #CLASS] // class = struct super->class ldr r9, [r9, #SUPERCLASS] // class = class->superclass - CacheLookup NORMAL + CacheLookup NORMAL, _objc_msgSendSuper2 // cache hit, IMP in r12, eq already set for nonstret forwarding ldr r0, [r0, #RECEIVER] // load real receiver - MESSENGER_END_FAST bx r12 // call imp - CacheLookup2 NORMAL + CacheLookup2 NORMAL, _objc_msgSendSuper2 // cache miss ldr r9, [r0, #CLASS] // class = struct super->class ldr r9, [r9, #SUPERCLASS] // class = class->superclass ldr r0, [r0, #RECEIVER] // load real receiver - MESSENGER_END_SLOW b __objc_msgSend_uncached END_ENTRY _objc_msgSendSuper2 @@ -623,12 +562,12 @@ LNilReceiver: ldr r9, [r0, #CLASS] // class = struct super->class ldr r9, [r9, #SUPERCLASS] // class = class->superclass - CacheLookup NORMAL + CacheLookup NORMAL, _objc_msgLookupSuper2 // cache hit, IMP in r12, eq already set for nonstret forwarding ldr r0, [r0, #RECEIVER] // load real receiver bx lr - CacheLookup2 NORMAL + CacheLookup2 NORMAL, _objc_msgLookupSuper2 // cache miss ldr r9, [r0, #CLASS] ldr r9, [r9, #SUPERCLASS] // r9 = class to search @@ -651,20 +590,17 @@ LNilReceiver: ********************************************************************/ ENTRY _objc_msgSendSuper_stret - MESSENGER_START ldr r9, [r1, #CLASS] // r9 = struct super->class - CacheLookup STRET + CacheLookup STRET, _objc_msgSendSuper_stret // cache hit, IMP in r12, ne already set for stret forwarding ldr r1, [r1, #RECEIVER] // load real receiver - MESSENGER_END_FAST bx r12 // call imp - CacheLookup2 STRET + CacheLookup2 STRET, _objc_msgSendSuper_stret // cache miss ldr r9, [r1, #CLASS] // r9 = struct super->class ldr r1, [r1, #RECEIVER] // load real receiver - MESSENGER_END_SLOW b __objc_msgSend_stret_uncached END_ENTRY _objc_msgSendSuper_stret @@ -675,22 +611,19 @@ LNilReceiver: ********************************************************************/ ENTRY _objc_msgSendSuper2_stret - MESSENGER_START ldr r9, [r1, #CLASS] // class = struct super->class ldr r9, [r9, #SUPERCLASS] // class = class->superclass - CacheLookup STRET + CacheLookup STRET, _objc_msgSendSuper2_stret // cache hit, IMP in r12, ne already set for stret forwarding ldr r1, [r1, #RECEIVER] // load real receiver - MESSENGER_END_FAST bx r12 // call imp - CacheLookup2 STRET + CacheLookup2 STRET, _objc_msgSendSuper2_stret // cache miss ldr r9, [r1, #CLASS] // class = struct super->class ldr r9, [r9, #SUPERCLASS] // class = class->superclass ldr r1, [r1, #RECEIVER] // load real receiver - MESSENGER_END_SLOW b __objc_msgSend_stret_uncached END_ENTRY _objc_msgSendSuper2_stret @@ -700,12 +633,12 @@ LNilReceiver: ldr r9, [r1, #CLASS] // class = struct super->class ldr r9, [r9, #SUPERCLASS] // class = class->superclass - CacheLookup STRET + CacheLookup STRET, _objc_msgLookupSuper2_stret // cache hit, IMP in r12, ne already set for stret forwarding ldr r1, [r1, #RECEIVER] // load real receiver bx lr - CacheLookup2 STRET + CacheLookup2 STRET, _objc_msgLookupSuper2_stret // cache miss ldr r9, [r1, #CLASS] ldr r9, [r9, #SUPERCLASS] // r9 = class to search @@ -738,6 +671,7 @@ LNilReceiver: sub sp, #8 // align stack FP_SAVE + // lookUpImpOrForward(obj, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER) .if $0 == NORMAL // receiver already in r0 // selector already in r1 @@ -746,8 +680,8 @@ LNilReceiver: mov r1, r2 // selector .endif mov r2, r9 // class to search - - blx __class_lookupMethodAndLoadCache3 + mov r3, #3 // LOOKUP_INITIALIZE | LOOKUP_INITIALIZE + blx _lookUpImpOrForward mov r12, r0 // r12 = IMP .if $0 == NORMAL @@ -837,10 +771,6 @@ LNilReceiver: // THIS IS NOT A CALLABLE C FUNCTION // Out-of-band Z is 0 (EQ) for normal, 1 (NE) for stret - MESSENGER_START - nop - MESSENGER_END_SLOW - beq __objc_msgForward b __objc_msgForward_stret diff --git a/runtime/Messengers.subproj/objc-msg-arm64.S b/runtime/Messengers.subproj/objc-msg-arm64.S old mode 100755 new mode 100644 index c24de97..6bf3f29 --- a/runtime/Messengers.subproj/objc-msg-arm64.S +++ b/runtime/Messengers.subproj/objc-msg-arm64.S @@ -27,110 +27,102 @@ ********************************************************************/ #ifdef __arm64__ - -#include +#include +#include "isa.h" +#include "arm64-asm.h" +#include "objc-config.h" .data -// _objc_entryPoints and _objc_exitPoints are used by method dispatch +// _objc_restartableRanges is used by method dispatch // caching code to figure out whether any threads are actively // in the cache for dispatching. The labels surround the asm code // that do cache lookups. The tables are zero-terminated. -.align 4 -.private_extern _objc_entryPoints -_objc_entryPoints: - .quad _cache_getImp - .quad _objc_msgSend - .quad _objc_msgSendSuper - .quad _objc_msgSendSuper2 - .quad _objc_msgLookup - .quad _objc_msgLookupSuper2 - .quad 0 - -.private_extern _objc_exitPoints -_objc_exitPoints: - .quad LExit_cache_getImp - .quad LExit_objc_msgSend - .quad LExit_objc_msgSendSuper - .quad LExit_objc_msgSendSuper2 - .quad LExit_objc_msgLookup - .quad LExit_objc_msgLookupSuper2 - .quad 0 - - -/******************************************************************** -* List every exit insn from every messenger for debugger use. -* Format: -* ( -* 1 word instruction's address -* 1 word type (ENTER or FAST_EXIT or SLOW_EXIT or NIL_EXIT) -* ) -* 1 word zero -* -* ENTER is the start of a dispatcher -* FAST_EXIT is method dispatch -* SLOW_EXIT is uncached method lookup -* NIL_EXIT is returning zero from a message sent to nil -* These must match objc-gdb.h. -********************************************************************/ - -#define ENTER 1 -#define FAST_EXIT 2 -#define SLOW_EXIT 3 -#define NIL_EXIT 4 - -.section __DATA,__objc_msg_break -.globl _gdb_objc_messenger_breakpoints -_gdb_objc_messenger_breakpoints: -// contents populated by the macros below - -.macro MESSENGER_START -4: - .section __DATA,__objc_msg_break - .quad 4b - .quad ENTER - .text -.endmacro -.macro MESSENGER_END_FAST -4: - .section __DATA,__objc_msg_break - .quad 4b - .quad FAST_EXIT - .text -.endmacro -.macro MESSENGER_END_SLOW -4: - .section __DATA,__objc_msg_break - .quad 4b - .quad SLOW_EXIT - .text -.endmacro -.macro MESSENGER_END_NIL -4: - .section __DATA,__objc_msg_break - .quad 4b - .quad NIL_EXIT - .text +.macro RestartableEntry +#if __LP64__ + .quad LLookupStart$0 +#else + .long LLookupStart$0 + .long 0 +#endif + .short LLookupEnd$0 - LLookupStart$0 + .short LLookupRecover$0 - LLookupStart$0 + .long 0 .endmacro + .align 4 + .private_extern _objc_restartableRanges +_objc_restartableRanges: + RestartableEntry _cache_getImp + RestartableEntry _objc_msgSend + RestartableEntry _objc_msgSendSuper + RestartableEntry _objc_msgSendSuper2 + RestartableEntry _objc_msgLookup + RestartableEntry _objc_msgLookupSuper2 + .fill 16, 1, 0 + /* objc_super parameter to sendSuper */ #define RECEIVER 0 -#define CLASS 8 +#define CLASS __SIZEOF_POINTER__ /* Selected field offsets in class structure */ -#define SUPERCLASS 8 -#define CACHE 16 - -/* Selected field offsets in isa field */ -#define ISA_MASK 0x0000000ffffffff8 +#define SUPERCLASS __SIZEOF_POINTER__ +#define CACHE (2 * __SIZEOF_POINTER__) /* Selected field offsets in method structure */ #define METHOD_NAME 0 -#define METHOD_TYPES 8 -#define METHOD_IMP 16 +#define METHOD_TYPES __SIZEOF_POINTER__ +#define METHOD_IMP (2 * __SIZEOF_POINTER__) + +#define BUCKET_SIZE (2 * __SIZEOF_POINTER__) + + +/******************************************************************** + * GetClassFromIsa_p16 src + * src is a raw isa field. Sets p16 to the corresponding class pointer. + * The raw isa might be an indexed isa to be decoded, or a + * packed isa that needs to be masked. + * + * On exit: + * $0 is unchanged + * p16 is a class pointer + * x10 is clobbered + ********************************************************************/ + +#if SUPPORT_INDEXED_ISA + .align 3 + .globl _objc_indexed_classes +_objc_indexed_classes: + .fill ISA_INDEX_COUNT, PTRSIZE, 0 +#endif + +.macro GetClassFromIsa_p16 /* src */ + +#if SUPPORT_INDEXED_ISA + // Indexed isa + mov p16, $0 // optimistically set dst = src + tbz p16, #ISA_INDEX_IS_NPI_BIT, 1f // done if not non-pointer isa + // isa in p16 is indexed + adrp x10, _objc_indexed_classes@PAGE + add x10, x10, _objc_indexed_classes@PAGEOFF + ubfx p16, p16, #ISA_INDEX_SHIFT, #ISA_INDEX_BITS // extract index + ldr p16, [x10, p16, UXTP #PTRSHIFT] // load class from array +1: + +#elif __LP64__ + // 64-bit packed isa + and p16, $0, #ISA_MASK + +#else + // 32-bit raw isa + mov p16, $0 + +#endif + +.endmacro /******************************************************************** @@ -164,12 +156,12 @@ LExit$0: ********************************************************************/ .macro UNWIND .section __LD,__compact_unwind,regular,debug - .quad $0 + PTR $0 .set LUnwind$0, LExit$0 - $0 .long LUnwind$0 .long $1 - .quad 0 /* no personality */ - .quad 0 /* no LSDA */ + PTR 0 /* no personality */ + PTR 0 /* no LSDA */ .text .endmacro @@ -179,10 +171,15 @@ LExit$0: /******************************************************************** * - * CacheLookup NORMAL|GETIMP|LOOKUP - * + * CacheLookup NORMAL|GETIMP|LOOKUP + * * Locate the implementation for a selector in a class method cache. * + * When this is used in a function that doesn't hold the runtime lock, + * this represents the critical section that may access dead memory. + * If the kernel causes one of these functions to go down the recovery + * path, we pretend the lookup failed by jumping the JumpMiss branch. + * * Takes: * x1 = selector * x16 = class to be searched @@ -200,14 +197,19 @@ LExit$0: #define GETIMP 1 #define LOOKUP 2 +// CacheHit: x17 = cached IMP, x12 = address of cached IMP, x1 = SEL, x16 = isa .macro CacheHit .if $0 == NORMAL - MESSENGER_END_FAST - br x17 // call imp + TailCallCachedImp x17, x12, x1, x16 // authenticate and call imp .elseif $0 == GETIMP - mov x0, x17 // return imp - ret + mov p0, p17 + cbz p0, 9f // don't ptrauth a nil imp + AuthAndResignAsIMP x0, x12, x1, x16 // authenticate imp and re-sign as IMP +9: ret // return IMP .elseif $0 == LOOKUP + // No nil check for ptrauth: the caller would crash anyway when they + // jump to a nil IMP. We don't care if that jump also fails ptrauth. + AuthAndResignAsIMP x17, x12, x1, x16 // authenticate imp and re-sign as IMP ret // return imp via x17 .else .abort oops @@ -217,11 +219,11 @@ LExit$0: .macro CheckMiss // miss if bucket->sel == 0 .if $0 == GETIMP - cbz x9, LGetImpMiss + cbz p9, LGetImpMiss .elseif $0 == NORMAL - cbz x9, __objc_msgSend_uncached + cbz p9, __objc_msgSend_uncached .elseif $0 == LOOKUP - cbz x9, __objc_msgLookup_uncached + cbz p9, __objc_msgLookup_uncached .else .abort oops .endif @@ -240,44 +242,92 @@ LExit$0: .endmacro .macro CacheLookup - // x1 = SEL, x16 = isa - ldp x10, x11, [x16, #CACHE] // x10 = buckets, x11 = occupied|mask - and w12, w1, w11 // x12 = _cmd & mask - add x12, x10, x12, LSL #4 // x12 = buckets + ((_cmd & mask)<<4) + // + // Restart protocol: + // + // As soon as we're past the LLookupStart$1 label we may have loaded + // an invalid cache pointer or mask. + // + // When task_restartable_ranges_synchronize() is called, + // (or when a signal hits us) before we're past LLookupEnd$1, + // then our PC will be reset to LLookupRecover$1 which forcefully + // jumps to the cache-miss codepath which have the following + // requirements: + // + // GETIMP: + // The cache-miss is just returning NULL (setting x0 to 0) + // + // NORMAL and LOOKUP: + // - x0 contains the receiver + // - x1 contains the selector + // - x16 contains the isa + // - other registers are set as per calling conventions + // +LLookupStart$1: - ldp x9, x17, [x12] // {x9, x17} = *bucket -1: cmp x9, x1 // if (bucket->sel != _cmd) + // p1 = SEL, p16 = isa + ldr p11, [x16, #CACHE] // p11 = mask|buckets + +#if CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_HIGH_16 + and p10, p11, #0x0000ffffffffffff // p10 = buckets + and p12, p1, p11, LSR #48 // x12 = _cmd & mask +#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_LOW_4 + and p10, p11, #~0xf // p10 = buckets + and p11, p11, #0xf // p11 = maskShift + mov p12, #0xffff + lsr p11, p12, p11 // p11 = mask = 0xffff >> p11 + and p12, p1, p11 // x12 = _cmd & mask +#else +#error Unsupported cache mask storage for ARM64. +#endif + + + add p12, p10, p12, LSL #(1+PTRSHIFT) + // p12 = buckets + ((_cmd & mask) << (1+PTRSHIFT)) + + ldp p17, p9, [x12] // {imp, sel} = *bucket +1: cmp p9, p1 // if (bucket->sel != _cmd) b.ne 2f // scan more CacheHit $0 // call or return imp -2: // not hit: x12 = not-hit bucket +2: // not hit: p12 = not-hit bucket CheckMiss $0 // miss if bucket->sel == 0 - cmp x12, x10 // wrap if bucket == buckets + cmp p12, p10 // wrap if bucket == buckets b.eq 3f - ldp x9, x17, [x12, #-16]! // {x9, x17} = *--bucket + ldp p17, p9, [x12, #-BUCKET_SIZE]! // {imp, sel} = *--bucket b 1b // loop -3: // wrap: x12 = first bucket, w11 = mask - add x12, x12, w11, UXTW #4 // x12 = buckets+(mask<<4) +3: // wrap: p12 = first bucket, w11 = mask +#if CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_HIGH_16 + add p12, p12, p11, LSR #(48 - (1+PTRSHIFT)) + // p12 = buckets + (mask << 1+PTRSHIFT) +#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_LOW_4 + add p12, p12, p11, LSL #(1+PTRSHIFT) + // p12 = buckets + (mask << 1+PTRSHIFT) +#else +#error Unsupported cache mask storage for ARM64. +#endif // Clone scanning loop to miss instead of hang when cache is corrupt. // The slow path may detect any corruption and halt later. - ldp x9, x17, [x12] // {x9, x17} = *bucket -1: cmp x9, x1 // if (bucket->sel != _cmd) + ldp p17, p9, [x12] // {imp, sel} = *bucket +1: cmp p9, p1 // if (bucket->sel != _cmd) b.ne 2f // scan more CacheHit $0 // call or return imp -2: // not hit: x12 = not-hit bucket +2: // not hit: p12 = not-hit bucket CheckMiss $0 // miss if bucket->sel == 0 - cmp x12, x10 // wrap if bucket == buckets + cmp p12, p10 // wrap if bucket == buckets b.eq 3f - ldp x9, x17, [x12, #-16]! // {x9, x17} = *--bucket + ldp p17, p9, [x12, #-BUCKET_SIZE]! // {imp, sel} = *--bucket b 1b // loop +LLookupEnd$1: +LLookupRecover$1: 3: // double wrap JumpMiss $0 - + .endmacro @@ -292,6 +342,7 @@ LExit$0: * ********************************************************************/ +#if SUPPORT_TAGGED_POINTERS .data .align 3 .globl _objc_debug_taggedpointer_classes @@ -300,39 +351,46 @@ _objc_debug_taggedpointer_classes: .globl _objc_debug_taggedpointer_ext_classes _objc_debug_taggedpointer_ext_classes: .fill 256, 8, 0 +#endif ENTRY _objc_msgSend UNWIND _objc_msgSend, NoFrame - MESSENGER_START - cmp x0, #0 // nil check and tagged pointer check + cmp p0, #0 // nil check and tagged pointer check +#if SUPPORT_TAGGED_POINTERS b.le LNilOrTagged // (MSB tagged pointer looks negative) - ldr x13, [x0] // x13 = isa - and x16, x13, #ISA_MASK // x16 = class +#else + b.eq LReturnZero +#endif + ldr p13, [x0] // p13 = isa + GetClassFromIsa_p16 p13 // p16 = class LGetIsaDone: - CacheLookup NORMAL // calls imp or objc_msgSend_uncached + // calls imp or objc_msgSend_uncached + CacheLookup NORMAL, _objc_msgSend +#if SUPPORT_TAGGED_POINTERS LNilOrTagged: b.eq LReturnZero // nil check // tagged - mov x10, #0xf000000000000000 - cmp x0, x10 - b.hs LExtTag adrp x10, _objc_debug_taggedpointer_classes@PAGE add x10, x10, _objc_debug_taggedpointer_classes@PAGEOFF ubfx x11, x0, #60, #4 ldr x16, [x10, x11, LSL #3] - b LGetIsaDone + adrp x10, _OBJC_CLASS_$___NSUnrecognizedTaggedPointer@PAGE + add x10, x10, _OBJC_CLASS_$___NSUnrecognizedTaggedPointer@PAGEOFF + cmp x10, x16 + b.ne LGetIsaDone -LExtTag: // ext tagged adrp x10, _objc_debug_taggedpointer_ext_classes@PAGE add x10, x10, _objc_debug_taggedpointer_ext_classes@PAGEOFF ubfx x11, x0, #52, #8 ldr x16, [x10, x11, LSL #3] b LGetIsaDone - +// SUPPORT_TAGGED_POINTERS +#endif + LReturnZero: // x0 is already zero mov x1, #0 @@ -340,7 +398,6 @@ LReturnZero: movi d1, #0 movi d2, #0 movi d3, #0 - MESSENGER_END_NIL ret END_ENTRY _objc_msgSend @@ -348,26 +405,31 @@ LReturnZero: ENTRY _objc_msgLookup UNWIND _objc_msgLookup, NoFrame - - cmp x0, #0 // nil check and tagged pointer check + cmp p0, #0 // nil check and tagged pointer check +#if SUPPORT_TAGGED_POINTERS b.le LLookup_NilOrTagged // (MSB tagged pointer looks negative) - ldr x13, [x0] // x13 = isa - and x16, x13, #ISA_MASK // x16 = class +#else + b.eq LLookup_Nil +#endif + ldr p13, [x0] // p13 = isa + GetClassFromIsa_p16 p13 // p16 = class LLookup_GetIsaDone: - CacheLookup LOOKUP // returns imp + // returns imp + CacheLookup LOOKUP, _objc_msgLookup +#if SUPPORT_TAGGED_POINTERS LLookup_NilOrTagged: b.eq LLookup_Nil // nil check // tagged - mov x10, #0xf000000000000000 - cmp x0, x10 - b.hs LLookup_ExtTag adrp x10, _objc_debug_taggedpointer_classes@PAGE add x10, x10, _objc_debug_taggedpointer_classes@PAGEOFF ubfx x11, x0, #60, #4 ldr x16, [x10, x11, LSL #3] - b LLookup_GetIsaDone + adrp x10, _OBJC_CLASS_$___NSUnrecognizedTaggedPointer@PAGE + add x10, x10, _OBJC_CLASS_$___NSUnrecognizedTaggedPointer@PAGEOFF + cmp x10, x16 + b.ne LLookup_GetIsaDone LLookup_ExtTag: adrp x10, _objc_debug_taggedpointer_ext_classes@PAGE @@ -375,6 +437,8 @@ LLookup_ExtTag: ubfx x11, x0, #52, #8 ldr x16, [x10, x11, LSL #3] b LLookup_GetIsaDone +// SUPPORT_TAGGED_POINTERS +#endif LLookup_Nil: adrp x17, __objc_msgNil@PAGE @@ -399,10 +463,10 @@ LLookup_Nil: ENTRY _objc_msgSendSuper UNWIND _objc_msgSendSuper, NoFrame - MESSENGER_START - ldp x0, x16, [x0] // x0 = real receiver, x16 = class - CacheLookup NORMAL // calls imp or objc_msgSend_uncached + ldp p0, p16, [x0] // p0 = real receiver, p16 = class + // calls imp or objc_msgSend_uncached + CacheLookup NORMAL, _objc_msgSendSuper END_ENTRY _objc_msgSendSuper @@ -410,11 +474,10 @@ LLookup_Nil: ENTRY _objc_msgSendSuper2 UNWIND _objc_msgSendSuper2, NoFrame - MESSENGER_START - ldp x0, x16, [x0] // x0 = real receiver, x16 = class - ldr x16, [x16, #SUPERCLASS] // x16 = class->superclass - CacheLookup NORMAL + ldp p0, p16, [x0] // p0 = real receiver, p16 = class + ldr p16, [x16, #SUPERCLASS] // p16 = class->superclass + CacheLookup NORMAL, _objc_msgSendSuper2 END_ENTRY _objc_msgSendSuper2 @@ -422,9 +485,9 @@ LLookup_Nil: ENTRY _objc_msgLookupSuper2 UNWIND _objc_msgLookupSuper2, NoFrame - ldp x0, x16, [x0] // x0 = real receiver, x16 = class - ldr x16, [x16, #SUPERCLASS] // x16 = class->superclass - CacheLookup LOOKUP + ldp p0, p16, [x0] // p0 = real receiver, p16 = class + ldr p16, [x16, #SUPERCLASS] // p16 = class->superclass + CacheLookup LOOKUP, _objc_msgLookupSuper2 END_ENTRY _objc_msgLookupSuper2 @@ -432,6 +495,7 @@ LLookup_Nil: .macro MethodTableLookup // push frame + SignLR stp fp, lr, [sp, #-16]! mov fp, sp @@ -447,11 +511,13 @@ LLookup_Nil: stp x6, x7, [sp, #(8*16+6*8)] str x8, [sp, #(8*16+8*8)] + // lookUpImpOrForward(obj, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER) // receiver and selector already in x0 and x1 mov x2, x16 - bl __class_lookupMethodAndLoadCache3 + mov x3, #3 + bl _lookUpImpOrForward - // imp in x0 + // IMP in x0 mov x17, x0 // restore registers and return @@ -467,6 +533,7 @@ LLookup_Nil: mov sp, fp ldp fp, lr, [sp], #16 + AuthenticateLR .endmacro @@ -474,10 +541,10 @@ LLookup_Nil: UNWIND __objc_msgSend_uncached, FrameWithNoSaves // THIS IS NOT A CALLABLE C FUNCTION - // Out-of-band x16 is the class to search + // Out-of-band p16 is the class to search MethodTableLookup - br x17 + TailCallFunctionPointer x17 END_ENTRY __objc_msgSend_uncached @@ -486,7 +553,7 @@ LLookup_Nil: UNWIND __objc_msgLookup_uncached, FrameWithNoSaves // THIS IS NOT A CALLABLE C FUNCTION - // Out-of-band x16 is the class to search + // Out-of-band p16 is the class to search MethodTableLookup ret @@ -496,11 +563,11 @@ LLookup_Nil: STATIC_ENTRY _cache_getImp - and x16, x0, #ISA_MASK - CacheLookup GETIMP + GetClassFromIsa_p16 p0 + CacheLookup GETIMP, _cache_getImp LGetImpMiss: - mov x0, #0 + mov p0, #0 ret END_ENTRY _cache_getImp @@ -519,10 +586,6 @@ LGetImpMiss: STATIC_ENTRY __objc_msgForward_impcache - MESSENGER_START - nop - MESSENGER_END_SLOW - // No stret specialization. b __objc_msgForward @@ -532,8 +595,8 @@ LGetImpMiss: ENTRY __objc_msgForward adrp x17, __objc_forward_handler@PAGE - ldr x17, [x17, __objc_forward_handler@PAGEOFF] - br x17 + ldr p17, [x17, __objc_forward_handler@PAGEOFF] + TailCallFunctionPointer x17 END_ENTRY __objc_msgForward @@ -553,9 +616,10 @@ LGetImpMiss: ENTRY _method_invoke // x1 is method triplet instead of SEL - ldr x17, [x1, #METHOD_IMP] - ldr x1, [x1, #METHOD_NAME] - br x17 + add p16, p1, #METHOD_IMP + ldr p17, [x16] + ldr p1, [x1, #METHOD_NAME] + TailCallMethodListImp x17, x16 END_ENTRY _method_invoke #endif diff --git a/runtime/Messengers.subproj/objc-msg-i386.S b/runtime/Messengers.subproj/objc-msg-i386.S index 8a64ba0..7473227 100644 --- a/runtime/Messengers.subproj/objc-msg-i386.S +++ b/runtime/Messengers.subproj/objc-msg-i386.S @@ -40,89 +40,30 @@ .data -// _objc_entryPoints and _objc_exitPoints are used by objc +// _objc_restartableRanges is used by method dispatch // to get the critical regions for which method caches // cannot be garbage collected. -.align 2 -.private_extern _objc_entryPoints -_objc_entryPoints: - .long __cache_getImp - .long __cache_getMethod - .long _objc_msgSend - .long _objc_msgSend_fpret - .long _objc_msgSend_stret - .long _objc_msgSendSuper - .long _objc_msgSendSuper_stret +.macro RestartableEntry + .long $0 .long 0 - -.private_extern _objc_exitPoints -_objc_exitPoints: - .long LGetImpExit - .long LGetMethodExit - .long LMsgSendExit - .long LMsgSendFpretExit - .long LMsgSendStretExit - .long LMsgSendSuperExit - .long LMsgSendSuperStretExit + .short $1 - $0 + .short 0xffff // The old runtime doesn't support kernel based recovery .long 0 - - -/******************************************************************** -* List every exit insn from every messenger for debugger use. -* Format: -* ( -* 1 word instruction's address -* 1 word type (ENTER or FAST_EXIT or SLOW_EXIT or NIL_EXIT) -* ) -* 1 word zero -* -* ENTER is the start of a dispatcher -* FAST_EXIT is method dispatch -* SLOW_EXIT is uncached method lookup -* NIL_EXIT is returning zero from a message sent to nil -* These must match objc-gdb.h. -********************************************************************/ - -#define ENTER 1 -#define FAST_EXIT 2 -#define SLOW_EXIT 3 -#define NIL_EXIT 4 - -.section __DATA,__objc_msg_break -.globl _gdb_objc_messenger_breakpoints -_gdb_objc_messenger_breakpoints: -// contents populated by the macros below - -.macro MESSENGER_START -4: - .section __DATA,__objc_msg_break - .long 4b - .long ENTER - .text -.endmacro -.macro MESSENGER_END_FAST -4: - .section __DATA,__objc_msg_break - .long 4b - .long FAST_EXIT - .text -.endmacro -.macro MESSENGER_END_SLOW -4: - .section __DATA,__objc_msg_break - .long 4b - .long SLOW_EXIT - .text -.endmacro -.macro MESSENGER_END_NIL -4: - .section __DATA,__objc_msg_break - .long 4b - .long NIL_EXIT - .text .endmacro + .align 4 + .private_extern _objc_restartableRanges +_objc_restartableRanges: + RestartableEntry __cache_getImp, LGetImpExit + RestartableEntry __cache_getMethod, LGetMethodExit + RestartableEntry _objc_msgSend, LMsgSendExit + RestartableEntry _objc_msgSend_fpret, LMsgSendFpretExit + RestartableEntry _objc_msgSend_stret, LMsgSendStretExit + RestartableEntry _objc_msgSendSuper, LMsgSendSuperExit + RestartableEntry _objc_msgSendSuper_stret, LMsgSendSuperStretExit + .fill 16, 1, 0 + /******************************************************************** * @@ -492,8 +433,6 @@ LMsgSendHitInstrumentDone_$0_$1_$2: ///////////////////////////////////////////////////////////////////// .macro MethodTableLookup - MESSENGER_END_SLOW - // stack has return address and nothing else subl $$(12+5*16), %esp @@ -502,10 +441,12 @@ LMsgSendHitInstrumentDone_$0_$1_$2: movdqa %xmm1, 2*16(%esp) movdqa %xmm0, 1*16(%esp) + // lookUpImpOrForward(obj, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER) + movl $$3, 12(%esp) // LOOKUP_INITIALIZE | LOOKUP_RESOLVER movl %eax, 8(%esp) // class movl %ecx, 4(%esp) // selector movl %edx, 0(%esp) // receiver - call __class_lookupMethodAndLoadCache3 + call _lookUpImpOrForward movdqa 4*16(%esp), %xmm3 movdqa 3*16(%esp), %xmm2 @@ -593,7 +534,6 @@ LGetImpExit: ********************************************************************/ ENTRY _objc_msgSend - MESSENGER_START CALL_MCOUNTER // load receiver and selector @@ -609,7 +549,6 @@ LMsgSendReceiverOk: movl isa(%eax), %edx // class = self->isa CacheLookup WORD_RETURN, MSG_SEND, LMsgSendCacheMiss xor %edx, %edx // set nonstret for msgForward_internal - MESSENGER_END_FAST jmp *%eax // cache miss: go search the method lists @@ -624,7 +563,6 @@ LMsgSendNilSelf: movl $0,%edx xorps %xmm0, %xmm0 LMsgSendDone: - MESSENGER_END_NIL ret // guaranteed non-nil entry point (disabled for now) @@ -647,7 +585,6 @@ LMsgSendExit: ********************************************************************/ ENTRY _objc_msgSendSuper - MESSENGER_START CALL_MCOUNTER // load selector and class to search @@ -658,7 +595,6 @@ LMsgSendExit: // search the cache (class in %edx) CacheLookup WORD_RETURN, MSG_SENDSUPER, LMsgSendSuperCacheMiss xor %edx, %edx // set nonstret for msgForward_internal - MESSENGER_END_FAST jmp *%eax // goto *imp // cache miss: go search the method lists @@ -671,7 +607,6 @@ LMsgSendSuperCacheMiss: LMsgSendSuperIgnored: movl super(%esp), %eax movl receiver(%eax), %eax - MESSENGER_END_NIL ret LMsgSendSuperExit: @@ -736,7 +671,6 @@ LMsgSendvArgsOK: ********************************************************************/ ENTRY _objc_msgSend_fpret - MESSENGER_START CALL_MCOUNTER // load receiver and selector @@ -752,7 +686,6 @@ LMsgSendFpretReceiverOk: movl isa(%eax), %edx // class = self->isa CacheLookup WORD_RETURN, MSG_SEND, LMsgSendFpretCacheMiss xor %edx, %edx // set nonstret for msgForward_internal - MESSENGER_END_FAST jmp *%eax // goto *imp // cache miss: go search the method lists @@ -766,7 +699,6 @@ LMsgSendFpretNilSelf: // %eax is already zero fldz LMsgSendFpretDone: - MESSENGER_END_NIL ret LMsgSendFpretExit: @@ -839,7 +771,6 @@ LMsgSendvFpretArgsOK: ********************************************************************/ ENTRY _objc_msgSend_stret - MESSENGER_START CALL_MCOUNTER // load receiver and selector @@ -855,7 +786,6 @@ LMsgSendStretReceiverOk: movl isa(%eax), %edx // class = self->isa CacheLookup STRUCT_RETURN, MSG_SEND, LMsgSendStretCacheMiss movl $1, %edx // set stret for objc_msgForward - MESSENGER_END_FAST jmp *%eax // goto *imp // cache miss: go search the method lists @@ -866,7 +796,6 @@ LMsgSendStretCacheMiss: // message sent to nil: redirect to nil receiver, if any LMsgSendStretNilSelf: - MESSENGER_END_NIL ret $4 // pop struct return address (#2995932) // guaranteed non-nil entry point (disabled for now) @@ -899,7 +828,6 @@ LMsgSendStretExit: ********************************************************************/ ENTRY _objc_msgSendSuper_stret - MESSENGER_START CALL_MCOUNTER // load selector and class to search @@ -910,7 +838,6 @@ LMsgSendStretExit: // search the cache (class in %edx) CacheLookup STRUCT_RETURN, MSG_SENDSUPER, LMsgSendSuperStretCacheMiss movl $1, %edx // set stret for objc_msgForward - MESSENGER_END_FAST jmp *%eax // goto *imp // cache miss: go search the method lists @@ -1012,10 +939,6 @@ L_forward_stret_handler: // THIS IS NOT A CALLABLE C FUNCTION // Out-of-band register %edx is nonzero for stret, zero otherwise - - MESSENGER_START - nop - MESSENGER_END_SLOW // Check return type (stret or not) testl %edx, %edx diff --git a/runtime/Messengers.subproj/objc-msg-simulator-i386.S b/runtime/Messengers.subproj/objc-msg-simulator-i386.S index beb7ac5..727b983 100644 --- a/runtime/Messengers.subproj/objc-msg-simulator-i386.S +++ b/runtime/Messengers.subproj/objc-msg-simulator-i386.S @@ -28,101 +28,36 @@ .data -// _objc_entryPoints and _objc_exitPoints are used by objc +// _objc_restartableRanges is used by method dispatch // to get the critical regions for which method caches // cannot be garbage collected. -.align 2 -.private_extern _objc_entryPoints -_objc_entryPoints: - .long _cache_getImp - .long _objc_msgSend - .long _objc_msgSend_fpret - .long _objc_msgSend_stret - .long _objc_msgSendSuper - .long _objc_msgSendSuper2 - .long _objc_msgSendSuper_stret - .long _objc_msgSendSuper2_stret - .long _objc_msgLookup - .long _objc_msgLookup_fpret - .long _objc_msgLookup_stret - .long _objc_msgLookupSuper2 - .long _objc_msgLookupSuper2_stret +.macro RestartableEntry + .long $0 .long 0 - -.private_extern _objc_exitPoints -_objc_exitPoints: - .long LExit_cache_getImp - .long LExit_objc_msgSend - .long LExit_objc_msgSend_fpret - .long LExit_objc_msgSend_stret - .long LExit_objc_msgSendSuper - .long LExit_objc_msgSendSuper2 - .long LExit_objc_msgSendSuper_stret - .long LExit_objc_msgSendSuper2_stret - .long LExit_objc_msgLookup - .long LExit_objc_msgLookup_fpret - .long LExit_objc_msgLookup_stret - .long LExit_objc_msgLookupSuper2 - .long LExit_objc_msgLookupSuper2_stret + .short LExit$0 - $0 + .short 0xffff // The simulator doesn't support kernel based recovery .long 0 - - -/******************************************************************** -* List every exit insn from every messenger for debugger use. -* Format: -* ( -* 1 word instruction's address -* 1 word type (ENTER or FAST_EXIT or SLOW_EXIT or NIL_EXIT) -* ) -* 1 word zero -* -* ENTER is the start of a dispatcher -* FAST_EXIT is method dispatch -* SLOW_EXIT is uncached method lookup -* NIL_EXIT is returning zero from a message sent to nil -* These must match objc-gdb.h. -********************************************************************/ - -#define ENTER 1 -#define FAST_EXIT 2 -#define SLOW_EXIT 3 -#define NIL_EXIT 4 - -.section __DATA,__objc_msg_break -.globl _gdb_objc_messenger_breakpoints -_gdb_objc_messenger_breakpoints: -// contents populated by the macros below - -.macro MESSENGER_START -4: - .section __DATA,__objc_msg_break - .long 4b - .long ENTER - .text -.endmacro -.macro MESSENGER_END_FAST -4: - .section __DATA,__objc_msg_break - .long 4b - .long FAST_EXIT - .text -.endmacro -.macro MESSENGER_END_SLOW -4: - .section __DATA,__objc_msg_break - .long 4b - .long SLOW_EXIT - .text -.endmacro -.macro MESSENGER_END_NIL -4: - .section __DATA,__objc_msg_break - .long 4b - .long NIL_EXIT - .text .endmacro + .align 4 + .private_extern _objc_restartableRanges +_objc_restartableRanges: + RestartableEntry _cache_getImp + RestartableEntry _objc_msgSend + RestartableEntry _objc_msgSend_fpret + RestartableEntry _objc_msgSend_stret + RestartableEntry _objc_msgSendSuper + RestartableEntry _objc_msgSendSuper2 + RestartableEntry _objc_msgSendSuper_stret + RestartableEntry _objc_msgSendSuper2_stret + RestartableEntry _objc_msgLookup + RestartableEntry _objc_msgLookup_fpret + RestartableEntry _objc_msgLookup_stret + RestartableEntry _objc_msgLookupSuper2 + RestartableEntry _objc_msgLookupSuper2_stret + .fill 16, 1, 0 + /******************************************************************** * Names for relative labels @@ -188,6 +123,12 @@ _gdb_objc_messenger_breakpoints: // Selected field offsets in class structure #define isa 0 #define superclass 4 +#define cache_buckets 8 +#define cache_mask 12 + +// Method cache +#define cached_sel 0 +#define cached_imp 4 // Method descriptor #define method_name 0 @@ -277,23 +218,26 @@ LExit$0: // eax = found bucket .if $1 == GETIMP - movl 4(%eax), %eax // return imp - ret + movl cached_imp(%eax), %eax // return imp + cmpl $$0, %eax + jz 9f // don't xor a nil imp + xorl %edx, %eax // xor the isa with the imp +9: ret .else -.if $0 != STRET - // eq already set for forwarding by `jne` -.else - test %eax, %eax // set ne for stret forwarding -.endif - .if $1 == CALL - MESSENGER_END_FAST - jmp *4(%eax) // call imp + xorl cached_imp(%eax), %edx // xor imp and isa +.if $0 != STRET + // ne already set for forwarding by `xor` +.else + cmp %eax, %eax // set eq for stret forwarding +.endif + jmp *%edx // call imp .elseif $1 == LOOKUP - movl 4(%eax), %eax // return imp + movl cached_imp(%eax), %eax // return imp + xorl %edx, %eax // xor isa into imp ret .else @@ -307,23 +251,23 @@ LExit$0: .macro CacheLookup - movzwl 12(%edx), %eax // eax = mask + movzwl cache_mask(%edx), %eax // eax = mask andl %ecx, %eax // eax = SEL & mask shll $$3, %eax // eax = offset = (SEL & mask) * 8 - addl 8(%edx), %eax // eax = bucket = cache->buckets+offset - cmpl (%eax), %ecx // if (bucket->sel != SEL) + addl cache_buckets(%edx), %eax // eax = bucket = buckets+offset + cmpl cached_sel(%eax), %ecx // if (bucket->sel != SEL) jne 1f // scan more // The `jne` above sets flags for CacheHit CacheHit $0, $1 // call or return imp 1: // loop - cmpl $$1, (%eax) + cmpl $$1, cached_sel(%eax) jbe 3f // if (bucket->sel <= 1) wrap or miss addl $$8, %eax // bucket++ 2: - cmpl (%eax), %ecx // if (bucket->sel != sel) + cmpl cached_sel(%eax), %ecx // if (bucket->sel != sel) jne 1b // scan more // The `jne` above sets flags for CacheHit CacheHit $0, $1 // call or return imp @@ -332,7 +276,7 @@ LExit$0: // wrap or miss jb LCacheMiss_f // if (bucket->sel < 1) cache miss // wrap - movl 4(%eax), %eax // bucket->imp is really first bucket + movl cached_imp(%eax), %eax // bucket->imp is really first bucket jmp 2f // Clone scanning loop to miss instead of hang when cache is corrupt. @@ -340,12 +284,12 @@ LExit$0: 1: // loop - cmpq $$1, (%eax) + cmpl $$1, cached_sel(%eax) jbe 3f // if (bucket->sel <= 1) wrap or miss addl $$8, %eax // bucket++ 2: - cmpl (%eax), %ecx // if (bucket->sel != sel) + cmpl cached_sel(%eax), %ecx // if (bucket->sel != sel) jne 1b // scan more // The `jne` above sets flags for CacheHit CacheHit $0, $1 // call or return imp @@ -388,10 +332,12 @@ LExit$0: movdqa %xmm1, 2*16(%esp) movdqa %xmm0, 1*16(%esp) + // lookUpImpOrForward(obj, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER) + movl $$3, 12(%esp) // LOOKUP_INITIALIZE | LOOKUP_RESOLVER movl %edx, 8(%esp) // class movl %ecx, 4(%esp) // selector movl %eax, 0(%esp) // receiver - call __class_lookupMethodAndLoadCache3 + call _lookUpImpOrForward // imp in eax @@ -401,9 +347,9 @@ LExit$0: movdqa 1*16(%esp), %xmm0 .if $0 == NORMAL - cmp %eax, %eax // set eq for nonstret forwarding -.else test %eax, %eax // set ne for stret forwarding +.else + cmp %eax, %eax // set eq for nonstret forwarding .endif leave @@ -482,15 +428,12 @@ LNilTestSlow: .if $0 == NORMAL ZeroReturn - MESSENGER_END_NIL ret .elseif $0 == FPRET ZeroReturnFPRET - MESSENGER_END_NIL ret .elseif $0 == STRET ZeroReturnSTRET - MESSENGER_END_NIL ret $$4 .else .abort oops @@ -553,7 +496,6 @@ LCacheMiss: ENTRY _objc_msgSend UNWIND _objc_msgSend, NoFrame - MESSENGER_START movl selector(%esp), %ecx movl self(%esp), %eax @@ -567,7 +509,6 @@ LCacheMiss: LCacheMiss: // isa still in edx - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSend @@ -596,13 +537,11 @@ LCacheMiss: /******************************************************************** * * id objc_msgSendSuper(struct objc_super *super, SEL _cmd, ...); - * IMP objc_msgLookupSuper(struct objc_super *super, SEL _cmd, ...); * ********************************************************************/ ENTRY _objc_msgSendSuper UNWIND _objc_msgSendSuper, NoFrame - MESSENGER_START movl selector(%esp), %ecx movl super(%esp), %eax // struct objc_super @@ -613,30 +552,11 @@ LCacheMiss: LCacheMiss: // class still in edx - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSendSuper - - ENTRY _objc_msgLookupSuper - UNWIND _objc_msgLookupSuper, NoFrame - - movl selector(%esp), %ecx - movl super(%esp), %eax // struct objc_super - movl class(%eax), %edx // struct objc_super->class - movl receiver(%eax), %eax // struct objc_super->receiver - movl %eax, super(%esp) // replace super arg with receiver - CacheLookup NORMAL, LOOKUP // returns IMP on success - -LCacheMiss: - // class still in edx - jmp __objc_msgLookup_uncached - - END_ENTRY _objc_msgLookupSuper - - /******************************************************************** * * id objc_msgSendSuper2(struct objc_super *super, SEL _cmd, ...); @@ -646,7 +566,6 @@ LCacheMiss: ENTRY _objc_msgSendSuper2 UNWIND _objc_msgSendSuper2, NoFrame - MESSENGER_START movl selector(%esp), %ecx movl super(%esp), %eax // struct objc_super @@ -658,7 +577,6 @@ LCacheMiss: LCacheMiss: // class still in edx - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSendSuper2 @@ -691,7 +609,6 @@ LCacheMiss: ENTRY _objc_msgSend_fpret UNWIND _objc_msgSend_fpret, NoFrame - MESSENGER_START movl selector(%esp), %ecx movl self(%esp), %eax @@ -705,7 +622,6 @@ LCacheMiss: LCacheMiss: // class still in edx - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSend_fpret @@ -740,7 +656,6 @@ LCacheMiss: ENTRY _objc_msgSend_stret UNWIND _objc_msgSend_stret, NoFrame - MESSENGER_START movl selector_stret(%esp), %ecx movl self_stret(%esp), %eax @@ -754,7 +669,6 @@ LCacheMiss: LCacheMiss: // class still in edx - MESSENGER_END_SLOW jmp __objc_msgSend_stret_uncached END_ENTRY _objc_msgSend_stret @@ -783,13 +697,11 @@ LCacheMiss: /******************************************************************** * * void objc_msgSendSuper_stret(void *st_addr, struct objc_super *super, SEL _cmd, ...); - * IMP objc_msgLookupSuper_stret(void *st_addr, struct objc_super *super, SEL _cmd, ...); * ********************************************************************/ ENTRY _objc_msgSendSuper_stret UNWIND _objc_msgSendSuper_stret, NoFrame - MESSENGER_START movl selector_stret(%esp), %ecx movl super_stret(%esp), %eax // struct objc_super @@ -800,29 +712,11 @@ LCacheMiss: LCacheMiss: // class still in edx - MESSENGER_END_SLOW jmp __objc_msgSend_stret_uncached END_ENTRY _objc_msgSendSuper_stret - ENTRY _objc_msgLookupSuper_stret - UNWIND _objc_msgLookupSuper_stret, NoFrame - - movl selector_stret(%esp), %ecx - movl super_stret(%esp), %eax // struct objc_super - movl class(%eax), %edx // struct objc_super->class - movl receiver(%eax), %eax // struct objc_super->receiver - movl %eax, super_stret(%esp) // replace super arg with receiver - CacheLookup STRET, LOOKUP // returns IMP on success - -LCacheMiss: - // class still in edx - jmp __objc_msgLookup_stret_uncached - - END_ENTRY _objc_msgLookupSuper_stret - - /******************************************************************** * * void objc_msgSendSuper2_stret(void *st_addr, struct objc_super *super, SEL _cmd, ...); @@ -832,7 +726,6 @@ LCacheMiss: ENTRY _objc_msgSendSuper2_stret UNWIND _objc_msgSendSuper2_stret, NoFrame - MESSENGER_START movl selector_stret(%esp), %ecx movl super_stret(%esp), %eax // struct objc_super @@ -845,7 +738,6 @@ LCacheMiss: // cache miss: go search the method lists LCacheMiss: // class still in edx - MESSENGER_END_SLOW jmp __objc_msgSend_stret_uncached END_ENTRY _objc_msgSendSuper2_stret @@ -958,11 +850,7 @@ L_forward_stret_handler: // THIS IS NOT A CALLABLE C FUNCTION // Out-of-band condition register is NE for stret, EQ otherwise. - MESSENGER_START - nop - MESSENGER_END_SLOW - - jne __objc_msgForward_stret + je __objc_msgForward_stret jmp __objc_msgForward END_ENTRY _objc_msgForward_impcache diff --git a/runtime/Messengers.subproj/objc-msg-simulator-x86_64.S b/runtime/Messengers.subproj/objc-msg-simulator-x86_64.S index 41b5aaa..a5410c4 100644 --- a/runtime/Messengers.subproj/objc-msg-simulator-x86_64.S +++ b/runtime/Messengers.subproj/objc-msg-simulator-x86_64.S @@ -22,7 +22,7 @@ */ #include -#if __x86_64__ && TARGET_OS_SIMULATOR +#if __x86_64__ && TARGET_OS_SIMULATOR && !TARGET_OS_IOSMAC /******************************************************************** ******************************************************************** @@ -34,105 +34,37 @@ .data -// _objc_entryPoints and _objc_exitPoints are used by objc +// _objc_restartableRanges is used by method dispatch // to get the critical regions for which method caches // cannot be garbage collected. -.align 4 -.private_extern _objc_entryPoints -_objc_entryPoints: - .quad _cache_getImp - .quad _objc_msgSend - .quad _objc_msgSend_fpret - .quad _objc_msgSend_fp2ret - .quad _objc_msgSend_stret - .quad _objc_msgSendSuper - .quad _objc_msgSendSuper_stret - .quad _objc_msgSendSuper2 - .quad _objc_msgSendSuper2_stret - .quad _objc_msgLookup - .quad _objc_msgLookup_fpret - .quad _objc_msgLookup_fp2ret - .quad _objc_msgLookup_stret - .quad _objc_msgLookupSuper2 - .quad _objc_msgLookupSuper2_stret - .quad 0 - -.private_extern _objc_exitPoints -_objc_exitPoints: - .quad LExit_cache_getImp - .quad LExit_objc_msgSend - .quad LExit_objc_msgSend_fpret - .quad LExit_objc_msgSend_fp2ret - .quad LExit_objc_msgSend_stret - .quad LExit_objc_msgSendSuper - .quad LExit_objc_msgSendSuper_stret - .quad LExit_objc_msgSendSuper2 - .quad LExit_objc_msgSendSuper2_stret - .quad LExit_objc_msgLookup - .quad LExit_objc_msgLookup_fpret - .quad LExit_objc_msgLookup_fp2ret - .quad LExit_objc_msgLookup_stret - .quad LExit_objc_msgLookupSuper2 - .quad LExit_objc_msgLookupSuper2_stret - .quad 0 - - -/******************************************************************** -* List every exit insn from every messenger for debugger use. -* Format: -* ( -* 1 word instruction's address -* 1 word type (ENTER or FAST_EXIT or SLOW_EXIT or NIL_EXIT) -* ) -* 1 word zero -* -* ENTER is the start of a dispatcher -* FAST_EXIT is method dispatch -* SLOW_EXIT is uncached method lookup -* NIL_EXIT is returning zero from a message sent to nil -* These must match objc-gdb.h. -********************************************************************/ - -#define ENTER 1 -#define FAST_EXIT 2 -#define SLOW_EXIT 3 -#define NIL_EXIT 4 - -.section __DATA,__objc_msg_break -.globl _gdb_objc_messenger_breakpoints -_gdb_objc_messenger_breakpoints: -// contents populated by the macros below - -.macro MESSENGER_START -4: - .section __DATA,__objc_msg_break - .quad 4b - .quad ENTER - .text -.endmacro -.macro MESSENGER_END_FAST -4: - .section __DATA,__objc_msg_break - .quad 4b - .quad FAST_EXIT - .text -.endmacro -.macro MESSENGER_END_SLOW -4: - .section __DATA,__objc_msg_break - .quad 4b - .quad SLOW_EXIT - .text -.endmacro -.macro MESSENGER_END_NIL -4: - .section __DATA,__objc_msg_break - .quad 4b - .quad NIL_EXIT - .text +.macro RestartableEntry + .quad $0 + .short LExit$0 - $0 + .short 0xffff // The simulator doesn't support kernel based recovery + .long 0 .endmacro + .align 4 + .private_extern _objc_restartableRanges +_objc_restartableRanges: + RestartableEntry _cache_getImp + RestartableEntry _objc_msgSend + RestartableEntry _objc_msgSend_fpret + RestartableEntry _objc_msgSend_fp2ret + RestartableEntry _objc_msgSend_stret + RestartableEntry _objc_msgSendSuper + RestartableEntry _objc_msgSendSuper_stret + RestartableEntry _objc_msgSendSuper2 + RestartableEntry _objc_msgSendSuper2_stret + RestartableEntry _objc_msgLookup + RestartableEntry _objc_msgLookup_fpret + RestartableEntry _objc_msgLookup_fp2ret + RestartableEntry _objc_msgLookup_stret + RestartableEntry _objc_msgLookupSuper2 + RestartableEntry _objc_msgLookupSuper2_stret + .fill 16, 1, 0 + /******************************************************************** * Recommended multi-byte NOP instructions @@ -218,6 +150,10 @@ _gdb_objc_messenger_breakpoints: #define method_name 0 #define method_imp 16 +// Method cache +#define cached_sel 0 +#define cached_imp 8 + ////////////////////////////////////////////////////////////////////// // @@ -297,29 +233,30 @@ LExit$0: .macro CacheHit - // CacheHit must always be preceded by a not-taken `jne` instruction - // in order to set the correct flags for _objc_msgForward_impcache. - // r11 = found bucket .if $1 == GETIMP - movq 8(%r11), %rax // return imp - ret + movq cached_imp(%r11), %rax // return imp + cmpq $$0, %rax + jz 9f // don't xor a nil imp + xorq %r10, %rax // xor the isa with the imp +9: ret .else -.if $0 != STRET - // eq already set for forwarding by `jne` -.else - test %r11, %r11 // set ne for stret forwarding -.endif - .if $1 == CALL - MESSENGER_END_FAST - jmp *8(%r11) // call imp - + movq cached_imp(%r11), %r11 // load imp + xorq %r10, %r11 // xor imp and isa +.if $0 != STRET + // ne already set for forwarding by `xor` +.else + cmp %r11, %r11 // set eq for stret forwarding +.endif + jmp *%r11 // call imp + .elseif $1 == LOOKUP - movq 8(%r11), %r11 // return imp + movq cached_imp(%r11), %r11 + xorq %r10, %r11 // return imp ^ isa ret .else @@ -342,35 +279,33 @@ LExit$0: addq 16(%r10), %r11 // r11 = class->cache.buckets + offset .if $0 != STRET - cmpq (%r11), %a2 // if (bucket->sel != _cmd) + cmpq cached_sel(%r11), %a2 // if (bucket->sel != _cmd) .else - cmpq (%r11), %a3 // if (bucket->sel != _cmd) + cmpq cached_sel(%r11), %a3 // if (bucket->sel != _cmd) .endif jne 1f // scan more - // CacheHit must always be preceded by a not-taken `jne` instruction CacheHit $0, $1 // call or return imp 1: // loop - cmpq $$1, (%r11) + cmpq $$1, cached_sel(%r11) jbe 3f // if (bucket->sel <= 1) wrap or miss addq $$16, %r11 // bucket++ 2: .if $0 != STRET - cmpq (%r11), %a2 // if (bucket->sel != _cmd) + cmpq cached_sel(%r11), %a2 // if (bucket->sel != _cmd) .else - cmpq (%r11), %a3 // if (bucket->sel != _cmd) + cmpq cached_sel(%r11), %a3 // if (bucket->sel != _cmd) .endif jne 1b // scan more - // CacheHit must always be preceded by a not-taken `jne` instruction CacheHit $0, $1 // call or return imp 3: // wrap or miss jb LCacheMiss_f // if (bucket->sel < 1) cache miss // wrap - movq 8(%r11), %r11 // bucket->imp is really first bucket + movq cached_imp(%r11), %r11 // bucket->imp is really first bucket jmp 2f // Clone scanning loop to miss instead of hang when cache is corrupt. @@ -378,18 +313,17 @@ LExit$0: 1: // loop - cmpq $$1, (%r11) + cmpq $$1, cached_sel(%r11) jbe 3f // if (bucket->sel <= 1) wrap or miss addq $$16, %r11 // bucket++ 2: .if $0 != STRET - cmpq (%r11), %a2 // if (bucket->sel != _cmd) + cmpq cached_sel(%r11), %a2 // if (bucket->sel != _cmd) .else - cmpq (%r11), %a3 // if (bucket->sel != _cmd) + cmpq cached_sel(%r11), %a3 // if (bucket->sel != _cmd) .endif jne 1b // scan more - // CacheHit must always be preceded by a not-taken `jne` instruction CacheHit $0, $1 // call or return imp 3: @@ -434,8 +368,7 @@ LExit$0: push %a6 movdqa %xmm7, -0x10(%rbp) - // _class_lookupMethodAndLoadCache3(receiver, selector, class) - + // lookUpImpOrForward(obj, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER) .if $0 == NORMAL // receiver already in a1 // selector already in a2 @@ -444,7 +377,8 @@ LExit$0: movq %a3, %a2 .endif movq %r10, %a3 - call __class_lookupMethodAndLoadCache3 + movl $$3, %a4d + call _lookUpImpOrForward // IMP is now in %rax movq %rax, %r11 @@ -466,9 +400,9 @@ LExit$0: movdqa -0x10(%rbp), %xmm7 .if $0 == NORMAL - cmp %r11, %r11 // set eq for nonstret forwarding -.else test %r11, %r11 // set ne for stret forwarding +.else + cmp %r11, %r11 // set eq for nonstret forwarding .endif leave @@ -567,14 +501,13 @@ LNilOrTagged: .else movq %a2, %r11 .endif - shrq $$60, %r11 - cmpl $$0xf, %r11d - je 1f // basic tagged + shrq $$60, %r11 leaq _objc_debug_taggedpointer_classes(%rip), %r10 movq (%r10, %r11, 8), %r10 // read isa from table - jmp LGetIsaDone_b -1: + leaq _OBJC_CLASS_$___NSUnrecognizedTaggedPointer(%rip), %r11 + cmp %r10, %r11 + jne LGetIsaDone_b // ext tagged .if $0 != STRET movq %a1, %r11 @@ -602,7 +535,6 @@ LNil: .else .abort oops .endif - MESSENGER_END_NIL ret .endmacro @@ -671,7 +603,6 @@ _objc_debug_taggedpointer_ext_classes: ENTRY _objc_msgSend UNWIND _objc_msgSend, NoFrame - MESSENGER_START GetIsaCheckNil NORMAL // r10 = self->isa, or return zero CacheLookup NORMAL, CALL // calls IMP on success @@ -682,7 +613,6 @@ _objc_debug_taggedpointer_ext_classes: // cache miss: go search the method lists LCacheMiss: // isa still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSend @@ -728,7 +658,6 @@ LCacheMiss: ENTRY _objc_msgSendSuper UNWIND _objc_msgSendSuper, NoFrame - MESSENGER_START // search the cache (objc_super in %a1) movq class(%a1), %r10 // class = objc_super->class @@ -738,7 +667,6 @@ LCacheMiss: // cache miss: go search the method lists LCacheMiss: // class still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSendSuper @@ -750,7 +678,6 @@ LCacheMiss: ENTRY _objc_msgSendSuper2 UNWIND _objc_msgSendSuper2, NoFrame - MESSENGER_START // objc_super->class is superclass of class to search @@ -763,7 +690,6 @@ LCacheMiss: // cache miss: go search the method lists LCacheMiss: // superclass still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSendSuper2 @@ -808,7 +734,6 @@ LCacheMiss: ENTRY _objc_msgSend_fpret UNWIND _objc_msgSend_fpret, NoFrame - MESSENGER_START GetIsaCheckNil FPRET // r10 = self->isa, or return zero CacheLookup FPRET, CALL // calls IMP on success @@ -819,7 +744,6 @@ LCacheMiss: // cache miss: go search the method lists LCacheMiss: // isa still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSend_fpret @@ -862,7 +786,6 @@ LCacheMiss: ENTRY _objc_msgSend_fp2ret UNWIND _objc_msgSend_fp2ret, NoFrame - MESSENGER_START GetIsaCheckNil FP2RET // r10 = self->isa, or return zero CacheLookup FP2RET, CALL // calls IMP on success @@ -873,7 +796,6 @@ LCacheMiss: // cache miss: go search the method lists LCacheMiss: // isa still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSend_fp2ret @@ -922,7 +844,6 @@ LCacheMiss: ENTRY _objc_msgSend_stret UNWIND _objc_msgSend_stret, NoFrame - MESSENGER_START GetIsaCheckNil STRET // r10 = self->isa, or return zero CacheLookup STRET, CALL // calls IMP on success @@ -933,7 +854,6 @@ LCacheMiss: // cache miss: go search the method lists LCacheMiss: // isa still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_stret_uncached END_ENTRY _objc_msgSend_stret @@ -988,7 +908,6 @@ LCacheMiss: ENTRY _objc_msgSendSuper_stret UNWIND _objc_msgSendSuper_stret, NoFrame - MESSENGER_START // search the cache (objc_super in %a2) movq class(%a2), %r10 // class = objc_super->class @@ -998,7 +917,6 @@ LCacheMiss: // cache miss: go search the method lists LCacheMiss: // class still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_stret_uncached END_ENTRY _objc_msgSendSuper_stret @@ -1010,7 +928,6 @@ LCacheMiss: ENTRY _objc_msgSendSuper2_stret UNWIND _objc_msgSendSuper2_stret, NoFrame - MESSENGER_START // search the cache (objc_super in %a2) movq class(%a2), %r10 // class = objc_super->class @@ -1021,7 +938,6 @@ LCacheMiss: // cache miss: go search the method lists LCacheMiss: // superclass still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_stret_uncached END_ENTRY _objc_msgSendSuper2_stret @@ -1132,11 +1048,7 @@ LCacheMiss: // THIS IS NOT A CALLABLE C FUNCTION // Out-of-band condition register is NE for stret, EQ otherwise. - MESSENGER_START - nop - MESSENGER_END_SLOW - - jne __objc_msgForward_stret + je __objc_msgForward_stret jmp __objc_msgForward END_ENTRY __objc_msgForward_impcache diff --git a/runtime/Messengers.subproj/objc-msg-win32.m b/runtime/Messengers.subproj/objc-msg-win32.m index 8821b0a..432d661 100644 --- a/runtime/Messengers.subproj/objc-msg-win32.m +++ b/runtime/Messengers.subproj/objc-msg-win32.m @@ -175,10 +175,11 @@ MISS: mov eax, isa[edx] // MethodTableLookup WORD_RETURN, MSG_SEND + push $3 push eax push ecx push edx - call _class_lookupMethodAndLoadCache3 + call lookUpImpOrFoward mov edx, kFwdMsgSend leave @@ -244,10 +245,11 @@ MISS: mov eax, isa[edx] // MethodTableLookup WORD_RETURN, MSG_SEND + push $3 push eax push ecx push edx - call _class_lookupMethodAndLoadCache3 + call lookUpImpOrFoward mov edx, kFwdMsgSend leave @@ -313,10 +315,11 @@ MISS: mov eax, super_class[eax] // MethodTableLookup WORD_RETURN, MSG_SENDSUPER + push $3 push eax push ecx push edx - call _class_lookupMethodAndLoadCache3 + call lookUpImpOrFoward mov edx, kFwdMsgSend leave @@ -375,10 +378,11 @@ MISS: mov eax, isa[edx] // MethodTableLookup WORD_RETURN, MSG_SEND + push $3 push eax push ecx push edx - call _class_lookupMethodAndLoadCache3 + call lookUpImpOrFoward mov edx, kFwdMsgSendStret leave @@ -445,10 +449,11 @@ MISS: mov eax, super_class[eax] // MethodTableLookup WORD_RETURN, MSG_SENDSUPER + push $3 push eax push ecx push edx - call _class_lookupMethodAndLoadCache3 + call lookUpImpOrFoward mov edx, kFwdMsgSendStret leave diff --git a/runtime/Messengers.subproj/objc-msg-x86_64.S b/runtime/Messengers.subproj/objc-msg-x86_64.S index 343b300..8fc6d48 100644 --- a/runtime/Messengers.subproj/objc-msg-x86_64.S +++ b/runtime/Messengers.subproj/objc-msg-x86_64.S @@ -22,7 +22,9 @@ */ #include -#if __x86_64__ && !TARGET_OS_SIMULATOR +#if __x86_64__ && !(TARGET_OS_SIMULATOR && !TARGET_OS_IOSMAC) + +#include "isa.h" /******************************************************************** ******************************************************************** @@ -34,105 +36,37 @@ .data -// _objc_entryPoints and _objc_exitPoints are used by objc +// _objc_restartableRanges is used by method dispatch // to get the critical regions for which method caches // cannot be garbage collected. -.align 4 -.private_extern _objc_entryPoints -_objc_entryPoints: - .quad _cache_getImp - .quad _objc_msgSend - .quad _objc_msgSend_fpret - .quad _objc_msgSend_fp2ret - .quad _objc_msgSend_stret - .quad _objc_msgSendSuper - .quad _objc_msgSendSuper_stret - .quad _objc_msgSendSuper2 - .quad _objc_msgSendSuper2_stret - .quad _objc_msgLookup - .quad _objc_msgLookup_fpret - .quad _objc_msgLookup_fp2ret - .quad _objc_msgLookup_stret - .quad _objc_msgLookupSuper2 - .quad _objc_msgLookupSuper2_stret - .quad 0 - -.private_extern _objc_exitPoints -_objc_exitPoints: - .quad LExit_cache_getImp - .quad LExit_objc_msgSend - .quad LExit_objc_msgSend_fpret - .quad LExit_objc_msgSend_fp2ret - .quad LExit_objc_msgSend_stret - .quad LExit_objc_msgSendSuper - .quad LExit_objc_msgSendSuper_stret - .quad LExit_objc_msgSendSuper2 - .quad LExit_objc_msgSendSuper2_stret - .quad LExit_objc_msgLookup - .quad LExit_objc_msgLookup_fpret - .quad LExit_objc_msgLookup_fp2ret - .quad LExit_objc_msgLookup_stret - .quad LExit_objc_msgLookupSuper2 - .quad LExit_objc_msgLookupSuper2_stret - .quad 0 - - -/******************************************************************** -* List every exit insn from every messenger for debugger use. -* Format: -* ( -* 1 word instruction's address -* 1 word type (ENTER or FAST_EXIT or SLOW_EXIT or NIL_EXIT) -* ) -* 1 word zero -* -* ENTER is the start of a dispatcher -* FAST_EXIT is method dispatch -* SLOW_EXIT is uncached method lookup -* NIL_EXIT is returning zero from a message sent to nil -* These must match objc-gdb.h. -********************************************************************/ - -#define ENTER 1 -#define FAST_EXIT 2 -#define SLOW_EXIT 3 -#define NIL_EXIT 4 - -.section __DATA,__objc_msg_break -.globl _gdb_objc_messenger_breakpoints -_gdb_objc_messenger_breakpoints: -// contents populated by the macros below - -.macro MESSENGER_START -4: - .section __DATA,__objc_msg_break - .quad 4b - .quad ENTER - .text -.endmacro -.macro MESSENGER_END_FAST -4: - .section __DATA,__objc_msg_break - .quad 4b - .quad FAST_EXIT - .text -.endmacro -.macro MESSENGER_END_SLOW -4: - .section __DATA,__objc_msg_break - .quad 4b - .quad SLOW_EXIT - .text -.endmacro -.macro MESSENGER_END_NIL -4: - .section __DATA,__objc_msg_break - .quad 4b - .quad NIL_EXIT - .text +.macro RestartableEntry + .quad LLookupStart$0 + .short LLookupEnd$0 - LLookupStart$0 + .short LCacheMiss$0 - LLookupStart$0 + .long 0 .endmacro + .align 4 + .private_extern _objc_restartableRanges +_objc_restartableRanges: + RestartableEntry _cache_getImp + RestartableEntry _objc_msgSend + RestartableEntry _objc_msgSend_fpret + RestartableEntry _objc_msgSend_fp2ret + RestartableEntry _objc_msgSend_stret + RestartableEntry _objc_msgSendSuper + RestartableEntry _objc_msgSendSuper_stret + RestartableEntry _objc_msgSendSuper2 + RestartableEntry _objc_msgSendSuper2_stret + RestartableEntry _objc_msgLookup + RestartableEntry _objc_msgLookup_fpret + RestartableEntry _objc_msgLookup_fp2ret + RestartableEntry _objc_msgLookup_stret + RestartableEntry _objc_msgLookupSuper2 + RestartableEntry _objc_msgLookupSuper2_stret + .fill 16, 1, 0 + /******************************************************************** * Recommended multi-byte NOP instructions @@ -181,9 +115,6 @@ _gdb_objc_messenger_breakpoints: * DO NOT USE THESE LABELS ELSEWHERE * Reserved labels: 6: 7: 8: 9: ********************************************************************/ -#define LCacheMiss 6 -#define LCacheMiss_f 6f -#define LCacheMiss_b 6b #define LNilTestSlow 7 #define LNilTestSlow_f 7f #define LNilTestSlow_b 7b @@ -225,6 +156,10 @@ _gdb_objc_messenger_breakpoints: #define method_name 0 #define method_imp 16 +// Method cache +#define cached_sel 0 +#define cached_imp 8 + ////////////////////////////////////////////////////////////////////// // @@ -285,10 +220,15 @@ LExit$0: ///////////////////////////////////////////////////////////////////// // -// CacheLookup return-type, caller +// CacheLookup return-type, caller, function // // Locate the implementation for a class in a selector's method cache. // +// When this is used in a function that doesn't hold the runtime lock, +// this represents the critical section that may access dead memory. +// If the kernel causes one of these functions to go down the recovery +// path, we pretend the lookup failed by jumping the JumpMiss branch. +// // Takes: // $0 = NORMAL, FPRET, FP2RET, STRET // $1 = CALL, LOOKUP, GETIMP @@ -304,29 +244,30 @@ LExit$0: .macro CacheHit - // CacheHit must always be preceded by a not-taken `jne` instruction - // in order to set the correct flags for _objc_msgForward_impcache. - // r11 = found bucket .if $1 == GETIMP - movq 8(%r11), %rax // return imp - ret + movq cached_imp(%r11), %rax // return imp + cmpq $$0, %rax + jz 9f // don't xor a nil imp + xorq %r10, %rax // xor the isa with the imp +9: ret .else -.if $0 != STRET - // eq already set for forwarding by `jne` -.else - test %r11, %r11 // set ne for stret forwarding -.endif - .if $1 == CALL - MESSENGER_END_FAST - jmp *8(%r11) // call imp - + movq cached_imp(%r11), %r11 // load imp + xorq %r10, %r11 // xor imp and isa +.if $0 != STRET + // ne already set for forwarding by `xor` +.else + cmp %r11, %r11 // set eq for stret forwarding +.endif + jmp *%r11 // call imp + .elseif $1 == LOOKUP - movq 8(%r11), %r11 // return imp + movq cached_imp(%r11), %r11 + xorq %r10, %r11 // return imp ^ isa ret .else @@ -339,6 +280,29 @@ LExit$0: .macro CacheLookup + // + // Restart protocol: + // + // As soon as we're past the LLookupStart$1 label we may have loaded + // an invalid cache pointer or mask. + // + // When task_restartable_ranges_synchronize() is called, + // (or when a signal hits us) before we're past LLookupEnd$1, + // then our PC will be reset to LCacheMiss$1 which forcefully + // jumps to the cache-miss codepath which have the following + // requirements: + // + // GETIMP: + // The cache-miss is just returning NULL (setting %rax to 0) + // + // NORMAL and STRET: + // - a1 or a2 (STRET) contains the receiver + // - a2 or a3 (STRET) contains the selector + // - r10 contains the isa + // - other registers are set as per calling conventions + // +LLookupStart$2: + .if $0 != STRET movq %a2, %r11 // r11 = _cmd .else @@ -349,35 +313,33 @@ LExit$0: addq 16(%r10), %r11 // r11 = class->cache.buckets + offset .if $0 != STRET - cmpq (%r11), %a2 // if (bucket->sel != _cmd) + cmpq cached_sel(%r11), %a2 // if (bucket->sel != _cmd) .else - cmpq (%r11), %a3 // if (bucket->sel != _cmd) + cmpq cached_sel(%r11), %a3 // if (bucket->sel != _cmd) .endif jne 1f // scan more - // CacheHit must always be preceded by a not-taken `jne` instruction CacheHit $0, $1 // call or return imp 1: // loop - cmpq $$1, (%r11) + cmpq $$1, cached_sel(%r11) jbe 3f // if (bucket->sel <= 1) wrap or miss addq $$16, %r11 // bucket++ 2: .if $0 != STRET - cmpq (%r11), %a2 // if (bucket->sel != _cmd) + cmpq cached_sel(%r11), %a2 // if (bucket->sel != _cmd) .else - cmpq (%r11), %a3 // if (bucket->sel != _cmd) + cmpq cached_sel(%r11), %a3 // if (bucket->sel != _cmd) .endif jne 1b // scan more - // CacheHit must always be preceded by a not-taken `jne` instruction CacheHit $0, $1 // call or return imp 3: // wrap or miss - jb LCacheMiss_f // if (bucket->sel < 1) cache miss + jb LCacheMiss$2 // if (bucket->sel < 1) cache miss // wrap - movq 8(%r11), %r11 // bucket->imp is really first bucket + movq cached_imp(%r11), %r11 // bucket->imp is really first bucket jmp 2f // Clone scanning loop to miss instead of hang when cache is corrupt. @@ -385,24 +347,24 @@ LExit$0: 1: // loop - cmpq $$1, (%r11) + cmpq $$1, cached_sel(%r11) jbe 3f // if (bucket->sel <= 1) wrap or miss addq $$16, %r11 // bucket++ 2: .if $0 != STRET - cmpq (%r11), %a2 // if (bucket->sel != _cmd) + cmpq cached_sel(%r11), %a2 // if (bucket->sel != _cmd) .else - cmpq (%r11), %a3 // if (bucket->sel != _cmd) + cmpq cached_sel(%r11), %a3 // if (bucket->sel != _cmd) .endif jne 1b // scan more - // CacheHit must always be preceded by a not-taken `jne` instruction CacheHit $0, $1 // call or return imp 3: // double wrap or miss - jmp LCacheMiss_f + jmp LCacheMiss$2 +LLookupEnd$2: .endmacro @@ -441,8 +403,7 @@ LExit$0: push %a6 movdqa %xmm7, -0x10(%rbp) - // _class_lookupMethodAndLoadCache3(receiver, selector, class) - + // lookUpImpOrForward(obj, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER) .if $0 == NORMAL // receiver already in a1 // selector already in a2 @@ -451,7 +412,8 @@ LExit$0: movq %a3, %a2 .endif movq %r10, %a3 - call __class_lookupMethodAndLoadCache3 + movl $$3, %a4d + call _lookUpImpOrForward // IMP is now in %rax movq %rax, %r11 @@ -473,9 +435,9 @@ LExit$0: movdqa -0x10(%rbp), %xmm7 .if $0 == NORMAL - cmp %r11, %r11 // set eq for nonstret forwarding + test %r11, %r11 // set ne for nonstret forwarding .else - test %r11, %r11 // set ne for stret forwarding + cmp %r11, %r11 // set eq for stret forwarding .endif leave @@ -503,13 +465,13 @@ LExit$0: testb $$1, %a1b PN jnz LGetIsaSlow_f - movq $$0x00007ffffffffff8, %r10 + movq $$ ISA_MASK, %r10 andq (%a1), %r10 .else testb $$1, %a2b PN jnz LGetIsaSlow_f - movq $$0x00007ffffffffff8, %r10 + movq $$ ISA_MASK, %r10 andq (%a2), %r10 .endif LGetIsaDone: @@ -523,13 +485,12 @@ LGetIsaSlow: movl %a2d, %r11d .endif andl $$0xF, %r11d - cmp $$0xF, %r11d - je 1f // basic tagged leaq _objc_debug_taggedpointer_classes(%rip), %r10 movq (%r10, %r11, 8), %r10 // read isa from table - jmp LGetIsaDone_b -1: + leaq _OBJC_CLASS_$___NSUnrecognizedTaggedPointer(%rip), %r11 + cmp %r10, %r11 + jne LGetIsaDone_b // extended tagged .if $0 != STRET movl %a1d, %r11d @@ -642,7 +603,6 @@ LNilTestSlow: .else .abort oops .endif - MESSENGER_END_NIL ret .endmacro @@ -680,9 +640,10 @@ LNilTestSlow: // do lookup movq %a1, %r10 // move class to r10 for CacheLookup - CacheLookup NORMAL, GETIMP // returns IMP on success + // returns IMP on success + CacheLookup NORMAL, GETIMP, _cache_getImp -LCacheMiss: +LCacheMiss_cache_getImp: // cache miss, return nil xorl %eax, %eax ret @@ -713,21 +674,20 @@ _objc_debug_taggedpointer_ext_classes: ENTRY _objc_msgSend UNWIND _objc_msgSend, NoFrame - MESSENGER_START NilTest NORMAL GetIsaFast NORMAL // r10 = self->isa - CacheLookup NORMAL, CALL // calls IMP on success + // calls IMP on success + CacheLookup NORMAL, CALL, _objc_msgSend NilTestReturnZero NORMAL GetIsaSupport NORMAL // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgSend: // isa still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSend @@ -738,14 +698,15 @@ LCacheMiss: NilTest NORMAL GetIsaFast NORMAL // r10 = self->isa - CacheLookup NORMAL, LOOKUP // returns IMP on success + // returns IMP on success + CacheLookup NORMAL, LOOKUP, _objc_msgLookup NilTestReturnIMP NORMAL GetIsaSupport NORMAL // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgLookup: // isa still in r10 jmp __objc_msgLookup_uncached @@ -776,17 +737,16 @@ LCacheMiss: ENTRY _objc_msgSendSuper UNWIND _objc_msgSendSuper, NoFrame - MESSENGER_START // search the cache (objc_super in %a1) movq class(%a1), %r10 // class = objc_super->class movq receiver(%a1), %a1 // load real receiver - CacheLookup NORMAL, CALL // calls IMP on success + // calls IMP on success + CacheLookup NORMAL, CALL, _objc_msgSendSuper // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgSendSuper: // class still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSendSuper @@ -798,7 +758,6 @@ LCacheMiss: ENTRY _objc_msgSendSuper2 UNWIND _objc_msgSendSuper2, NoFrame - MESSENGER_START // objc_super->class is superclass of class to search @@ -806,12 +765,12 @@ LCacheMiss: movq class(%a1), %r10 // cls = objc_super->class movq receiver(%a1), %a1 // load real receiver movq 8(%r10), %r10 // cls = class->superclass - CacheLookup NORMAL, CALL // calls IMP on success + // calls IMP on success + CacheLookup NORMAL, CALL, _objc_msgSendSuper2 // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgSendSuper2: // superclass still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSendSuper2 @@ -825,10 +784,11 @@ LCacheMiss: movq class(%a1), %r10 // cls = objc_super->class movq receiver(%a1), %a1 // load real receiver movq 8(%r10), %r10 // cls = class->superclass - CacheLookup NORMAL, LOOKUP // returns IMP on success + // returns IMP on success + CacheLookup NORMAL, LOOKUP, _objc_msgLookupSuper2 // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgLookupSuper2: // superclass still in r10 jmp __objc_msgLookup_uncached @@ -856,21 +816,20 @@ LCacheMiss: ENTRY _objc_msgSend_fpret UNWIND _objc_msgSend_fpret, NoFrame - MESSENGER_START NilTest FPRET GetIsaFast FPRET // r10 = self->isa - CacheLookup FPRET, CALL // calls IMP on success + // calls IMP on success + CacheLookup FPRET, CALL, _objc_msgSend_fpret NilTestReturnZero FPRET GetIsaSupport FPRET // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgSend_fpret: // isa still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSend_fpret @@ -881,14 +840,15 @@ LCacheMiss: NilTest FPRET GetIsaFast FPRET // r10 = self->isa - CacheLookup FPRET, LOOKUP // returns IMP on success + // returns IMP on success + CacheLookup FPRET, LOOKUP, _objc_msgLookup_fpret NilTestReturnIMP FPRET GetIsaSupport FPRET // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgLookup_fpret: // isa still in r10 jmp __objc_msgLookup_uncached @@ -916,21 +876,20 @@ LCacheMiss: ENTRY _objc_msgSend_fp2ret UNWIND _objc_msgSend_fp2ret, NoFrame - MESSENGER_START NilTest FP2RET GetIsaFast FP2RET // r10 = self->isa - CacheLookup FP2RET, CALL // calls IMP on success + // calls IMP on success + CacheLookup FP2RET, CALL, _objc_msgSend_fp2ret NilTestReturnZero FP2RET GetIsaSupport FP2RET // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgSend_fp2ret: // isa still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_uncached END_ENTRY _objc_msgSend_fp2ret @@ -941,14 +900,15 @@ LCacheMiss: NilTest FP2RET GetIsaFast FP2RET // r10 = self->isa - CacheLookup FP2RET, LOOKUP // returns IMP on success + // returns IMP on success + CacheLookup FP2RET, LOOKUP, _objc_msgLookup_fp2ret NilTestReturnIMP FP2RET GetIsaSupport FP2RET // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgLookup_fp2ret: // isa still in r10 jmp __objc_msgLookup_uncached @@ -982,21 +942,20 @@ LCacheMiss: ENTRY _objc_msgSend_stret UNWIND _objc_msgSend_stret, NoFrame - MESSENGER_START NilTest STRET GetIsaFast STRET // r10 = self->isa - CacheLookup STRET, CALL // calls IMP on success + // calls IMP on success + CacheLookup STRET, CALL, _objc_msgSend_stret NilTestReturnZero STRET GetIsaSupport STRET // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgSend_stret: // isa still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_stret_uncached END_ENTRY _objc_msgSend_stret @@ -1007,14 +966,15 @@ LCacheMiss: NilTest STRET GetIsaFast STRET // r10 = self->isa - CacheLookup STRET, LOOKUP // returns IMP on success + // returns IMP on success + CacheLookup STRET, LOOKUP, _objc_msgLookup_stret NilTestReturnIMP STRET GetIsaSupport STRET // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgLookup_stret: // isa still in r10 jmp __objc_msgLookup_stret_uncached @@ -1054,17 +1014,16 @@ LCacheMiss: ENTRY _objc_msgSendSuper_stret UNWIND _objc_msgSendSuper_stret, NoFrame - MESSENGER_START // search the cache (objc_super in %a2) movq class(%a2), %r10 // class = objc_super->class movq receiver(%a2), %a2 // load real receiver - CacheLookup STRET, CALL // calls IMP on success + // calls IMP on success + CacheLookup STRET, CALL, _objc_msgSendSuper_stret // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgSendSuper_stret: // class still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_stret_uncached END_ENTRY _objc_msgSendSuper_stret @@ -1076,18 +1035,17 @@ LCacheMiss: ENTRY _objc_msgSendSuper2_stret UNWIND _objc_msgSendSuper2_stret, NoFrame - MESSENGER_START // search the cache (objc_super in %a2) movq class(%a2), %r10 // class = objc_super->class movq receiver(%a2), %a2 // load real receiver movq 8(%r10), %r10 // class = class->superclass - CacheLookup STRET, CALL // calls IMP on success + // calls IMP on success + CacheLookup STRET, CALL, _objc_msgSendSuper2_stret // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgSendSuper2_stret: // superclass still in r10 - MESSENGER_END_SLOW jmp __objc_msgSend_stret_uncached END_ENTRY _objc_msgSendSuper2_stret @@ -1099,10 +1057,11 @@ LCacheMiss: movq class(%a2), %r10 // class = objc_super->class movq receiver(%a2), %a2 // load real receiver movq 8(%r10), %r10 // class = class->superclass - CacheLookup STRET, LOOKUP // returns IMP on success + // returns IMP on success + CacheLookup STRET, LOOKUP, _objc_msgLookupSuper2_stret // cache miss: go search the method lists -LCacheMiss: +LCacheMiss_objc_msgLookupSuper2_stret: // superclass still in r10 jmp __objc_msgLookup_stret_uncached @@ -1200,12 +1159,8 @@ LCacheMiss: // THIS IS NOT A CALLABLE C FUNCTION // Out-of-band condition register is NE for stret, EQ otherwise. - - MESSENGER_START - nop - MESSENGER_END_SLOW - jne __objc_msgForward_stret + je __objc_msgForward_stret jmp __objc_msgForward END_ENTRY __objc_msgForward_impcache @@ -1281,49 +1236,4 @@ LCacheMiss: .quad 0 .quad 0 - - // Workaround for Skype evil (rdar://19715989) - - .text - .align 4 - .private_extern _map_images - .private_extern _map_2_images - .private_extern _hax -_hax: - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop -_map_images: - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - nop - jmp _map_2_images - #endif diff --git a/runtime/Module/ObjectiveC.apinotes b/runtime/Module/ObjectiveC.apinotes new file mode 100644 index 0000000..5491c89 --- /dev/null +++ b/runtime/Module/ObjectiveC.apinotes @@ -0,0 +1,437 @@ +--- +Name: ObjectiveC +Classes: +- Name: NSArray + SwiftBridge: 'Swift.Array' +- Name: NSDictionary + SwiftBridge: 'Swift.Dictionary' +- Name: NSSet + SwiftBridge: 'Swift.Set' +- Name: NSString + SwiftBridge: 'Swift.String' +- Name: List + Methods: + - Selector: init + MethodKind: Instance + NullabilityOfRet: N + - Selector: 'isEqual:' + MethodKind: Instance + Nullability: + - O + NullabilityOfRet: S +- Name: NSObject + SwiftName: NSObject + Methods: + - Selector: alloc + MethodKind: Class + NullabilityOfRet: N + - Selector: 'allocWithZone:' + MethodKind: Class + Nullability: + - S + NullabilityOfRet: N + - Selector: class + MethodKind: Class + Availability: nonswift + AvailabilityMsg: use 'self' instead + - Selector: 'conformsToProtocol:' + MethodKind: Class + Nullability: + - N + NullabilityOfRet: S + - Selector: copy + MethodKind: Instance + NullabilityOfRet: N + - Selector: dealloc + MethodKind: Instance + Availability: nonswift + AvailabilityMsg: use 'deinit' to define a de-initializer + - Selector: debugDescription + MethodKind: Class + NullabilityOfRet: N + - Selector: description + MethodKind: Class + NullabilityOfRet: N + - Selector: 'forwardingTargetForSelector:' + MethodKind: Instance + Nullability: + - S + NullabilityOfRet: O + - Selector: 'forwardInvocation:' + MethodKind: Instance + Availability: nonswift + - Selector: init + MethodKind: Instance + NullabilityOfRet: N + DesignatedInit: true + - Selector: 'instanceMethodSignatureForSelector:' + MethodKind: Class + Availability: nonswift + - Selector: 'isSubclassOfClass:' + MethodKind: Class + Nullability: + - N + NullabilityOfRet: S + - Selector: 'methodSignatureForSelector:' + MethodKind: Instance + Availability: nonswift + - Selector: mutableCopy + MethodKind: Instance + NullabilityOfRet: N + - Selector: new + MethodKind: Class + NullabilityOfRet: N + - Selector: superclass + MethodKind: Class + NullabilityOfRet: O +- Name: Object + Methods: + - Selector: init + MethodKind: Instance + NullabilityOfRet: N + - Selector: 'isEqual:' + MethodKind: Instance + Nullability: + - O + NullabilityOfRet: S +Protocols: +- Name: NSObject + SwiftName: NSObjectProtocol + Methods: + - Selector: class + MethodKind: Instance + Availability: nonswift + AvailabilityMsg: use 'type(of:)' instead + - Selector: 'conformsToProtocol:' + MethodKind: Instance + Nullability: + - N + NullabilityOfRet: S + - Selector: 'isEqual:' + MethodKind: Instance + Nullability: + - O + NullabilityOfRet: S + - Selector: 'isKindOfClass:' + MethodKind: Instance + Nullability: + - N + NullabilityOfRet: S + - Selector: 'isMemberOfClass:' + MethodKind: Instance + Nullability: + - N + NullabilityOfRet: S + - Selector: self + MethodKind: Instance + NullabilityOfRet: N + Properties: + - Name: debugDescription + Nullability: N + - Name: description + Nullability: N + - Name: superclass + Nullability: O +Tags: +- Name: _NSZone + SwiftName: _NSZone + + +# Runtime functions did not yet have nullability in Swift 3. + +SwiftVersions: +- Version: 3 + Functions: + # objc.h swift3 + - Name: object_getClassName + NullabilityOfRet: U + Nullability: [U] + - Name: sel_isMapped + Nullability: [U] + - Name: sel_getUid + NullabilityOfRet: U + Nullability: [U] + + # objc-exception.h swift3 + - Name: objc_exception_throw + Nullability: [U] + - Name: objc_begin_catch + NullabilityOfRet: U + Nullability: [U] + - Name: objc_setExceptionPreprocessor + NullabilityOfRet: U + Nullability: [U] + - Name: objc_setExceptionMatcher + NullabilityOfRet: U + Nullability: [U] + - Name: objc_setUncaughtExceptionHandler + NullabilityOfRet: U + Nullability: [U] + - Name: objc_addExceptionHandler + Nullability: [U, U] + + # objc-sync.h swift3 + - Name: objc_sync_enter + Nullability: [U] + - Name: objc_sync_exit + Nullability: [U] + + # runtime.h swift3 + - Name: object_getClass + NullabilityOfRet: U + Nullability: [U] + - Name: object_setClass + NullabilityOfRet: U + Nullability: [U, U] + - Name: object_isClass + Nullability: [U] + - Name: object_getIvar + NullabilityOfRet: U + Nullability: [U, U] + - Name: object_setIvar + Nullability: [U, U, U] + - Name: object_setIvarWithStrongDefault + Nullability: [U, U, U] + - Name: objc_getClass + NullabilityOfRet: U + Nullability: [U] + - Name: objc_getMetaClass + NullabilityOfRet: U + Nullability: [U] + - Name: objc_lookUpClass + NullabilityOfRet: U + Nullability: [U] + - Name: objc_getRequiredClass + NullabilityOfRet: U + Nullability: [U] + - Name: objc_getClassList + Parameters: + - Position: 0 + Type: "Class _Nullable * _Null_unspecified" + - Name: objc_copyClassList + ResultType: "Class _Nullable * _Null_unspecified" + Nullability: [U] + - Name: class_getName + NullabilityOfRet: U + Nullability: [U] + - Name: class_isMetaClass + Nullability: [U] + - Name: class_getSuperclass + NullabilityOfRet: U + Nullability: [U] + - Name: class_getVersion + Nullability: [U] + - Name: class_setVersion + Nullability: [U] + - Name: class_getInstanceSize + Nullability: [U] + - Name: class_getInstanceVariable + NullabilityOfRet: U + Nullability: [U, U] + - Name: class_getClassVariable + NullabilityOfRet: U + Nullability: [U, U] + - Name: class_copyIvarList + ResultType: "Ivar _Nullable * _Null_unspecified" + Nullability: [U, U] + - Name: class_getInstanceMethod + NullabilityOfRet: U + Nullability: [U, U] + - Name: class_getClassMethod + NullabilityOfRet: U + Nullability: [U, U] + - Name: class_getMethodImplementation + NullabilityOfRet: U + Nullability: [U, U] + - Name: class_getMethodImplementation_stret + NullabilityOfRet: U + Nullability: [U, U] + - Name: class_respondsToSelector + Nullability: [U, U] + - Name: class_copyMethodList + Nullability: [U, U] + ResultType: "Method _Nullable * _Null_unspecified" + - Name: class_conformsToProtocol + Nullability: [U, U] + - Name: class_copyProtocolList +# fixme ResultType: + NullabilityOfRet: U + Nullability: [U, U] + - Name: class_getProperty + NullabilityOfRet: U + Nullability: [U, U] + - Name: class_copyPropertyList + ResultType: "objc_property_t _Nullable * _Null_unspecified" + Nullability: [U, U] + - Name: class_getIvarLayout + NullabilityOfRet: U + Nullability: [U] + - Name: class_getWeakIvarLayout + NullabilityOfRet: U + Nullability: [U] + - Name: class_addMethod + Nullability: [U, U, U, U] + - Name: class_replaceMethod + NullabilityOfRet: U + Nullability: [U, U, U, U] + - Name: class_addIvar + Nullability: [U, U, U, U, U] + - Name: class_addProtocol + Nullability: [U, U] + - Name: class_addProperty + Nullability: [U, U, U, U] + - Name: class_replaceProperty + Nullability: [U, U, U, U] + - Name: class_setIvarLayout + Nullability: [U, U] + - Name: class_setWeakIvarLayout + Nullability: [U, U] + - Name: class_createInstance + NullabilityOfRet: U + Nullability: [U, U] + - Name: objc_allocateClassPair + NullabilityOfRet: U + Nullability: [U, U, U] + - Name: objc_registerClassPair + Nullability: [U] + - Name: objc_duplicateClass + NullabilityOfRet: U + Nullability: [U, U, U] + - Name: objc_disposeClassPair + Nullability: [U] + - Name: method_getName + NullabilityOfRet: U + Nullability: [U] + - Name: method_getImplementation + NullabilityOfRet: U + Nullability: [U] + - Name: method_getTypeEncoding + NullabilityOfRet: U + Nullability: [U] + - Name: method_getNumberOfArguments + Nullability: [U] + - Name: method_copyReturnType + NullabilityOfRet: U + Nullability: [U] + - Name: method_copyArgumentType + NullabilityOfRet: U + Nullability: [U, U] + - Name: method_getReturnType + Nullability: [U, U, U] + - Name: method_getArgumentType + Nullability: [U, U, U, U] + - Name: method_getDescription + NullabilityOfRet: U + Nullability: [U] + - Name: method_setImplementation + NullabilityOfRet: U + Nullability: [U, U] + - Name: method_exchangeImplementations + Nullability: [U, U] + - Name: ivar_getName + NullabilityOfRet: U + Nullability: [U] + - Name: ivar_getTypeEncoding + NullabilityOfRet: U + Nullability: [U] + - Name: ivar_getOffset + Nullability: [U] + - Name: property_getName + NullabilityOfRet: U + Nullability: [U] + - Name: property_getAttributes + NullabilityOfRet: U + Nullability: [U] + - Name: property_copyAttributeList + NullabilityOfRet: U + Nullability: [U, U] + - Name: property_copyAttributeValue + NullabilityOfRet: U + Nullability: [U, U] + - Name: objc_getProtocol + NullabilityOfRet: U + Nullability: [U] + - Name: objc_copyProtocolList +# fixme ResultType: + NullabilityOfRet: U + Nullability: [U] + - Name: protocol_conformsToProtocol + Nullability: [U, U] + - Name: protocol_isEqual + Nullability: [U, U] + - Name: protocol_getName + NullabilityOfRet: U + Nullability: [U] + - Name: protocol_getMethodDescription + Nullability: [U, U, U, U] + - Name: protocol_copyMethodDescriptionList + NullabilityOfRet: U + Nullability: [U, U, U, U] + - Name: protocol_getProperty + NullabilityOfRet: U + Nullability: [U, U, U, U] + - Name: protocol_copyPropertyList + ResultType: "objc_property_t _Nullable * _Null_unspecified" + Nullability: [U, U] + - Name: protocol_copyPropertyList2 + ResultType: "objc_property_t _Nullable * _Null_unspecified" + Nullability: [U, U, U, U] + - Name: protocol_copyProtocolList +# fixme ResultType: + NullabilityOfRet: U + Nullability: [U, U] + - Name: objc_allocateProtocol + NullabilityOfRet: U + Nullability: [U] + - Name: objc_registerProtocol + Nullability: [U] + - Name: protocol_addMethodDescription + Nullability: [U, U, U, U, U] + - Name: protocol_addProtocol + Nullability: [U, U] + - Name: protocol_addProperty + Nullability: [U, U, U, U, U, U] + - Name: objc_copyImageNames + ResultType: "const char * _Nullable * _Null_unspecified" + Nullability: [U] + - Name: class_getImageName + NullabilityOfRet: U + Nullability: [U] + - Name: objc_copyClassNamesForImage + ResultType: "const char * _Nullable * _Null_unspecified" + Nullability: [U, U] + - Name: sel_getName + NullabilityOfRet: U + Nullability: [U] + - Name: sel_registerName + NullabilityOfRet: U + Nullability: [U] + - Name: sel_isEqual + Nullability: [U, U] + - Name: objc_enumerationMutation + Nullability: [U] + - Name: objc_setEnumerationMutationHandler + Nullability: [U] + - Name: objc_setForwardHandler + Nullability: [U, U] + - Name: imp_implementationWithBlock + NullabilityOfRet: U + Nullability: [U] + - Name: imp_getBlock + NullabilityOfRet: U + Nullability: [U] + - Name: imp_removeBlock + Nullability: [U] + - Name: objc_loadWeak + NullabilityOfRet: U + Nullability: [U] + - Name: objc_storeWeak + NullabilityOfRet: U + Nullability: [U, U] + - Name: objc_setAssociatedObject + Nullability: [U, U, U, U] + - Name: objc_getAssociatedObject + NullabilityOfRet: U + Nullability: [U, U] + - Name: objc_removeAssociatedObjects + Nullability: [U] diff --git a/runtime/Module/module.modulemap b/runtime/Module/module.modulemap new file mode 100644 index 0000000..74f7d64 --- /dev/null +++ b/runtime/Module/module.modulemap @@ -0,0 +1,40 @@ +module ObjectiveC [system] [extern_c] { + umbrella "." + export * + module * { + export * + } + + module NSObject { + requires objc + header "NSObject.h" + export * + } + +#if defined(BUILD_FOR_OSX) + module List { + // Uses @defs, which does not work in ObjC++ or non-ARC. + requires objc, !objc_arc, !cplusplus + header "List.h" + export * + } + + module Object { + requires objc + header "Object.h" + export * + } + + module Protocol { + requires objc + header "Protocol.h" + export * + } +#endif + +#if !defined(BUILD_FOR_OSX) + // These file are not available outside macOS. + exclude header "hashtable.h" + exclude header "hashtable2.h" +#endif +} diff --git a/runtime/NSObjCRuntime.h b/runtime/NSObjCRuntime.h index d111e0e..1310f03 100644 --- a/runtime/NSObjCRuntime.h +++ b/runtime/NSObjCRuntime.h @@ -8,7 +8,7 @@ #include #include -#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64 +#if __LP64__ || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64 typedef long NSInteger; typedef unsigned long NSUInteger; #else diff --git a/runtime/NSObject-internal.h b/runtime/NSObject-internal.h new file mode 100644 index 0000000..c23fbc2 --- /dev/null +++ b/runtime/NSObject-internal.h @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2019 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +#ifndef _NSOBJECT_INTERNAL_H +#define _NSOBJECT_INTERNAL_H + +/* + * WARNING DANGER HAZARD BEWARE EEK + * + * Everything in this file is for Apple Internal use only. + * These will change in arbitrary OS updates and in unpredictable ways. + * When your program breaks, you get to keep both pieces. + */ + +/* + * NSObject-internal.h: Private SPI for use by other system frameworks. + */ + +/*********************************************************************** + Autorelease pool implementation + + A thread's autorelease pool is a stack of pointers. + Each pointer is either an object to release, or POOL_BOUNDARY which is + an autorelease pool boundary. + A pool token is a pointer to the POOL_BOUNDARY for that pool. When + the pool is popped, every object hotter than the sentinel is released. + The stack is divided into a doubly-linked list of pages. Pages are added + and deleted as necessary. + Thread-local storage points to the hot page, where newly autoreleased + objects are stored. +**********************************************************************/ + +// structure version number. Only bump if ABI compatability is broken +#define AUTORELEASEPOOL_VERSION 1 + +// Set this to 1 to mprotect() autorelease pool contents +#define PROTECT_AUTORELEASEPOOL 0 + +// Set this to 1 to validate the entire autorelease pool header all the time +// (i.e. use check() instead of fastcheck() everywhere) +#define CHECK_AUTORELEASEPOOL (DEBUG) + +#ifdef __cplusplus +#include +#include +#include +#include + + +#ifndef C_ASSERT + #if __has_feature(cxx_static_assert) + #define C_ASSERT(expr) static_assert(expr, "(" #expr ")!") + #elif __has_feature(c_static_assert) + #define C_ASSERT(expr) _Static_assert(expr, "(" #expr ")!") + #else + #define C_ASSERT(expr) + #endif +#endif + +// Make ASSERT work when objc-private.h hasn't been included. +#ifndef ASSERT +#define ASSERT(x) assert(x) +#endif + +struct magic_t { + static const uint32_t M0 = 0xA1A1A1A1; +# define M1 "AUTORELEASE!" + static const size_t M1_len = 12; + uint32_t m[4]; + + magic_t() { + ASSERT(M1_len == strlen(M1)); + ASSERT(M1_len == 3 * sizeof(m[1])); + + m[0] = M0; + strncpy((char *)&m[1], M1, M1_len); + } + + ~magic_t() { + // Clear magic before deallocation. + // This prevents some false positives in memory debugging tools. + // fixme semantically this should be memset_s(), but the + // compiler doesn't optimize that at all (rdar://44856676). + volatile uint64_t *p = (volatile uint64_t *)m; + p[0] = 0; p[1] = 0; + } + + bool check() const { + return (m[0] == M0 && 0 == strncmp((char *)&m[1], M1, M1_len)); + } + + bool fastcheck() const { +#if CHECK_AUTORELEASEPOOL + return check(); +#else + return (m[0] == M0); +#endif + } + +# undef M1 +}; + +class AutoreleasePoolPage; +struct AutoreleasePoolPageData +{ + magic_t const magic; + __unsafe_unretained id *next; + pthread_t const thread; + AutoreleasePoolPage * const parent; + AutoreleasePoolPage *child; + uint32_t const depth; + uint32_t hiwat; + + AutoreleasePoolPageData(__unsafe_unretained id* _next, pthread_t _thread, AutoreleasePoolPage* _parent, uint32_t _depth, uint32_t _hiwat) + : magic(), next(_next), thread(_thread), + parent(_parent), child(nil), + depth(_depth), hiwat(_hiwat) + { + } +}; + + +struct thread_data_t +{ +#ifdef __LP64__ + pthread_t const thread; + uint32_t const hiwat; + uint32_t const depth; +#else + pthread_t const thread; + uint32_t const hiwat; + uint32_t const depth; + uint32_t padding; +#endif +}; +C_ASSERT(sizeof(thread_data_t) == 16); + +#undef C_ASSERT + +#endif +#endif diff --git a/runtime/NSObject.h b/runtime/NSObject.h index f42b446..2172aba 100644 --- a/runtime/NSObject.h +++ b/runtime/NSObject.h @@ -18,7 +18,7 @@ @property (readonly) NSUInteger hash; @property (readonly) Class superclass; -- (Class)class OBJC_SWIFT_UNAVAILABLE("use 'anObject.dynamicType' instead"); +- (Class)class OBJC_SWIFT_UNAVAILABLE("use 'type(of: anObject)' instead"); - (instancetype)self; - (id)performSelector:(SEL)aSelector; @@ -47,11 +47,14 @@ @end -OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) +OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0) OBJC_ROOT_CLASS OBJC_EXPORT @interface NSObject { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wobjc-interface-ivars" Class isa OBJC_ISA_AVAILABILITY; +#pragma clang diagnostic pop } + (void)load; @@ -82,7 +85,7 @@ OBJC_EXPORT + (IMP)instanceMethodForSelector:(SEL)aSelector; - (void)doesNotRecognizeSelector:(SEL)aSelector; -- (id)forwardingTargetForSelector:(SEL)aSelector OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +- (id)forwardingTargetForSelector:(SEL)aSelector OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); - (void)forwardInvocation:(NSInvocation *)anInvocation OBJC_SWIFT_UNAVAILABLE(""); - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector OBJC_SWIFT_UNAVAILABLE(""); @@ -93,8 +96,8 @@ OBJC_EXPORT + (BOOL)isSubclassOfClass:(Class)aClass; -+ (BOOL)resolveClassMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); -+ (BOOL)resolveInstanceMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); ++ (BOOL)resolveClassMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); ++ (BOOL)resolveInstanceMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); + (NSUInteger)hash; + (Class)superclass; diff --git a/runtime/NSObject.mm b/runtime/NSObject.mm index c7e7f43..f672f4c 100644 --- a/runtime/NSObject.mm +++ b/runtime/NSObject.mm @@ -25,8 +25,7 @@ #include "NSObject.h" #include "objc-weak.h" -#include "llvm-DenseMap.h" -#include "NSObject.h" +#include "DenseMapExtras.h" #include #include @@ -36,67 +35,26 @@ #include #include #include -#include #include #include #include +#include "NSObject-internal.h" @interface NSInvocation - (SEL)selector; @end - -#if TARGET_OS_MAC - -// NSObject used to be in Foundation/CoreFoundation. - -#define SYMBOL_ELSEWHERE_IN_3(sym, vers, n) \ - OBJC_EXPORT const char elsewhere_ ##n __asm__("$ld$hide$os" #vers "$" #sym); const char elsewhere_ ##n = 0 -#define SYMBOL_ELSEWHERE_IN_2(sym, vers, n) \ - SYMBOL_ELSEWHERE_IN_3(sym, vers, n) -#define SYMBOL_ELSEWHERE_IN(sym, vers) \ - SYMBOL_ELSEWHERE_IN_2(sym, vers, __COUNTER__) - +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_magic_offset = __builtin_offsetof(AutoreleasePoolPageData, magic); +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_next_offset = __builtin_offsetof(AutoreleasePoolPageData, next); +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_thread_offset = __builtin_offsetof(AutoreleasePoolPageData, thread); +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_parent_offset = __builtin_offsetof(AutoreleasePoolPageData, parent); +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_child_offset = __builtin_offsetof(AutoreleasePoolPageData, child); +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_depth_offset = __builtin_offsetof(AutoreleasePoolPageData, depth); +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_hiwat_offset = __builtin_offsetof(AutoreleasePoolPageData, hiwat); #if __OBJC2__ -# define NSOBJECT_ELSEWHERE_IN(vers) \ - SYMBOL_ELSEWHERE_IN(_OBJC_CLASS_$_NSObject, vers); \ - SYMBOL_ELSEWHERE_IN(_OBJC_METACLASS_$_NSObject, vers); \ - SYMBOL_ELSEWHERE_IN(_OBJC_IVAR_$_NSObject.isa, vers) -#else -# define NSOBJECT_ELSEWHERE_IN(vers) \ - SYMBOL_ELSEWHERE_IN(.objc_class_name_NSObject, vers) +OBJC_EXTERN const uint32_t objc_class_abi_version = OBJC_CLASS_ABI_VERSION_MAX; #endif -#if TARGET_OS_IOS - NSOBJECT_ELSEWHERE_IN(5.1); - NSOBJECT_ELSEWHERE_IN(5.0); - NSOBJECT_ELSEWHERE_IN(4.3); - NSOBJECT_ELSEWHERE_IN(4.2); - NSOBJECT_ELSEWHERE_IN(4.1); - NSOBJECT_ELSEWHERE_IN(4.0); - NSOBJECT_ELSEWHERE_IN(3.2); - NSOBJECT_ELSEWHERE_IN(3.1); - NSOBJECT_ELSEWHERE_IN(3.0); - NSOBJECT_ELSEWHERE_IN(2.2); - NSOBJECT_ELSEWHERE_IN(2.1); - NSOBJECT_ELSEWHERE_IN(2.0); -#elif TARGET_OS_OSX - NSOBJECT_ELSEWHERE_IN(10.7); - NSOBJECT_ELSEWHERE_IN(10.6); - NSOBJECT_ELSEWHERE_IN(10.5); - NSOBJECT_ELSEWHERE_IN(10.4); - NSOBJECT_ELSEWHERE_IN(10.3); - NSOBJECT_ELSEWHERE_IN(10.2); - NSOBJECT_ELSEWHERE_IN(10.1); - NSOBJECT_ELSEWHERE_IN(10.0); -#else - // NSObject has always been in libobjc on these platforms. -#endif - -// TARGET_OS_MAC -#endif - - /*********************************************************************** * Weak ivar support **********************************************************************/ @@ -107,9 +65,9 @@ static id defaultBadAllocHandler(Class cls) cls->nameForLogging()); } -static id(*badAllocHandler)(Class) = &defaultBadAllocHandler; +id(*badAllocHandler)(Class) = &defaultBadAllocHandler; -static id callBadAllocHandler(Class cls) +id _objc_callBadAllocHandler(Class cls) { // fixme add re-entrancy protection in case allocation fails inside handler return (*badAllocHandler)(cls); @@ -132,9 +90,19 @@ namespace { #define SIDE_TABLE_RC_SHIFT 2 #define SIDE_TABLE_FLAG_MASK (SIDE_TABLE_RC_ONE-1) +struct RefcountMapValuePurgeable { + static inline bool isPurgeable(size_t x) { + return x == 0; + } +}; + // RefcountMap disguises its pointers because we // don't want the table to act as a root for `leaks`. -typedef objc::DenseMap,size_t,true> RefcountMap; +typedef objc::DenseMap,size_t,RefcountMapValuePurgeable> RefcountMap; + +// Template parameters. +enum HaveOld { DontHaveOld = false, DoHaveOld = true }; +enum HaveNew { DontHaveNew = false, DoHaveNew = true }; struct SideTable { spinlock_t slock; @@ -151,66 +119,107 @@ struct SideTable { void lock() { slock.lock(); } void unlock() { slock.unlock(); } + void forceReset() { slock.forceReset(); } // Address-ordered lock discipline for a pair of side tables. - template + template static void lockTwo(SideTable *lock1, SideTable *lock2); - template + template static void unlockTwo(SideTable *lock1, SideTable *lock2); }; template<> -void SideTable::lockTwo(SideTable *lock1, SideTable *lock2) { +void SideTable::lockTwo + (SideTable *lock1, SideTable *lock2) +{ spinlock_t::lockTwo(&lock1->slock, &lock2->slock); } template<> -void SideTable::lockTwo(SideTable *lock1, SideTable *) { +void SideTable::lockTwo + (SideTable *lock1, SideTable *) +{ lock1->lock(); } template<> -void SideTable::lockTwo(SideTable *, SideTable *lock2) { +void SideTable::lockTwo + (SideTable *, SideTable *lock2) +{ lock2->lock(); } template<> -void SideTable::unlockTwo(SideTable *lock1, SideTable *lock2) { +void SideTable::unlockTwo + (SideTable *lock1, SideTable *lock2) +{ spinlock_t::unlockTwo(&lock1->slock, &lock2->slock); } template<> -void SideTable::unlockTwo(SideTable *lock1, SideTable *) { +void SideTable::unlockTwo + (SideTable *lock1, SideTable *) +{ lock1->unlock(); } template<> -void SideTable::unlockTwo(SideTable *, SideTable *lock2) { +void SideTable::unlockTwo + (SideTable *, SideTable *lock2) +{ lock2->unlock(); } - - -// We cannot use a C++ static initializer to initialize SideTables because -// libc calls us before our C++ initializers run. We also don't want a global -// pointer to this struct because of the extra indirection. -// Do it the hard way. -alignas(StripedMap) static uint8_t - SideTableBuf[sizeof(StripedMap)]; - -static void SideTableInit() { - new (SideTableBuf) StripedMap(); -} +static objc::ExplicitInit> SideTablesMap; static StripedMap& SideTables() { - return *reinterpret_cast*>(SideTableBuf); + return SideTablesMap.get(); } // anonymous namespace }; +void SideTableLockAll() { + SideTables().lockAll(); +} + +void SideTableUnlockAll() { + SideTables().unlockAll(); +} + +void SideTableForceResetAll() { + SideTables().forceResetAll(); +} + +void SideTableDefineLockOrder() { + SideTables().defineLockOrder(); +} + +void SideTableLocksPrecedeLock(const void *newlock) { + SideTables().precedeLock(newlock); +} + +void SideTableLocksSucceedLock(const void *oldlock) { + SideTables().succeedLock(oldlock); +} + +void SideTableLocksPrecedeLocks(StripedMap& newlocks) { + int i = 0; + const void *newlock; + while ((newlock = newlocks.getLock(i++))) { + SideTables().precedeLock(newlock); + } +} + +void SideTableLocksSucceedLocks(StripedMap& oldlocks) { + int i = 0; + const void *oldlock; + while ((oldlock = oldlocks.getLock(i++))) { + SideTables().succeedLock(oldlock); + } +} // // The -fobjc-arc flag causes the compiler to issue calls to objc_{retain/release/autorelease/retain_block} @@ -256,12 +265,16 @@ objc_storeStrong(id *location, id obj) // If CrashIfDeallocating is true, the process is halted if newObj is // deallocating or newObj's class does not support weak references. // If CrashIfDeallocating is false, nil is stored instead. -template +enum CrashIfDeallocating { + DontCrashIfDeallocating = false, DoCrashIfDeallocating = true +}; +template static id storeWeak(id *location, objc_object *newObj) { - assert(HaveOld || HaveNew); - if (!HaveNew) assert(newObj == nil); + ASSERT(haveOld || haveNew); + if (!haveNew) ASSERT(newObj == nil); Class previouslyInitializedClass = nil; id oldObj; @@ -272,35 +285,35 @@ storeWeak(id *location, objc_object *newObj) // Order by lock address to prevent lock ordering problems. // Retry if the old value changes underneath us. retry: - if (HaveOld) { + if (haveOld) { oldObj = *location; oldTable = &SideTables()[oldObj]; } else { oldTable = nil; } - if (HaveNew) { + if (haveNew) { newTable = &SideTables()[newObj]; } else { newTable = nil; } - SideTable::lockTwo(oldTable, newTable); + SideTable::lockTwo(oldTable, newTable); - if (HaveOld && *location != oldObj) { - SideTable::unlockTwo(oldTable, newTable); + if (haveOld && *location != oldObj) { + SideTable::unlockTwo(oldTable, newTable); goto retry; } // Prevent a deadlock between the weak reference machinery // and the +initialize machinery by ensuring that no // weakly-referenced object has an un-+initialized isa. - if (HaveNew && newObj) { + if (haveNew && newObj) { Class cls = newObj->getIsa(); if (cls != previouslyInitializedClass && !((objc_class *)cls)->isInitialized()) { - SideTable::unlockTwo(oldTable, newTable); - _class_initialize(_class_getNonMetaClass(cls, (id)newObj)); + SideTable::unlockTwo(oldTable, newTable); + class_initialize(cls, (id)newObj); // If this class is finished with +initialize then we're good. // If this class is still running +initialize on this thread @@ -315,15 +328,15 @@ storeWeak(id *location, objc_object *newObj) } // Clean up old value, if any. - if (HaveOld) { + if (haveOld) { weak_unregister_no_lock(&oldTable->weak_table, oldObj, location); } // Assign new value, if any. - if (HaveNew) { - newObj = (objc_object *)weak_register_no_lock(&newTable->weak_table, - (id)newObj, location, - CrashIfDeallocating); + if (haveNew) { + newObj = (objc_object *) + weak_register_no_lock(&newTable->weak_table, (id)newObj, location, + crashIfDeallocating); // weak_register_no_lock returns nil if weak store should be rejected // Set is-weakly-referenced bit in refcount table. @@ -338,7 +351,7 @@ storeWeak(id *location, objc_object *newObj) // No new value. The storage is not changed. } - SideTable::unlockTwo(oldTable, newTable); + SideTable::unlockTwo(oldTable, newTable); return (id)newObj; } @@ -356,7 +369,7 @@ storeWeak(id *location, objc_object *newObj) id objc_storeWeak(id *location, id newObj) { - return storeWeak + return storeWeak (location, (objc_object *)newObj); } @@ -374,7 +387,7 @@ objc_storeWeak(id *location, id newObj) id objc_storeWeakOrNil(id *location, id newObj) { - return storeWeak + return storeWeak (location, (objc_object *)newObj); } @@ -403,7 +416,7 @@ objc_initWeak(id *location, id newObj) return nil; } - return storeWeak + return storeWeak (location, (objc_object*)newObj); } @@ -415,7 +428,7 @@ objc_initWeakOrNil(id *location, id newObj) return nil; } - return storeWeak + return storeWeak (location, (objc_object*)newObj); } @@ -434,7 +447,7 @@ objc_initWeakOrNil(id *location, id newObj) void objc_destroyWeak(id *location) { - (void)storeWeak + (void)storeWeak (location, nil); } @@ -478,7 +491,7 @@ objc_loadWeakRetained(id *location) if (! cls->hasCustomRR()) { // Fast case. We know +initialize is complete because // default-RR can never be set before then. - assert(cls->isInitialized()); + ASSERT(cls->isInitialized()); if (! obj->rootTryRetain()) { result = nil; } @@ -488,17 +501,17 @@ objc_loadWeakRetained(id *location) // the lock if necessary in order to avoid deadlocks. if (cls->isInitialized() || _thisThreadIsInitializingClass(cls)) { BOOL (*tryRetain)(id, SEL) = (BOOL(*)(id, SEL)) - class_getMethodImplementation(cls, SEL_retainWeakReference); + class_getMethodImplementation(cls, @selector(retainWeakReference)); if ((IMP)tryRetain == _objc_msgForward) { result = nil; } - else if (! (*tryRetain)(obj, SEL_retainWeakReference)) { + else if (! (*tryRetain)(obj, @selector(retainWeakReference))) { result = nil; } } else { table->unlock(); - _class_initialize(cls); + class_initialize(cls, obj); goto retry; } } @@ -579,54 +592,26 @@ objc_moveWeak(id *dst, id *src) objects are stored. **********************************************************************/ -// Set this to 1 to mprotect() autorelease pool contents -#define PROTECT_AUTORELEASEPOOL 0 - -// Set this to 1 to validate the entire autorelease pool header all the time -// (i.e. use check() instead of fastcheck() everywhere) -#define CHECK_AUTORELEASEPOOL (DEBUG) - BREAKPOINT_FUNCTION(void objc_autoreleaseNoPool(id obj)); BREAKPOINT_FUNCTION(void objc_autoreleasePoolInvalid(const void *token)); -namespace { - -struct magic_t { - static const uint32_t M0 = 0xA1A1A1A1; -# define M1 "AUTORELEASE!" - static const size_t M1_len = 12; - uint32_t m[4]; - - magic_t() { - assert(M1_len == strlen(M1)); - assert(M1_len == 3 * sizeof(m[1])); - - m[0] = M0; - strncpy((char *)&m[1], M1, M1_len); - } - - ~magic_t() { - m[0] = m[1] = m[2] = m[3] = 0; - } - - bool check() const { - return (m[0] == M0 && 0 == strncmp((char *)&m[1], M1, M1_len)); - } - - bool fastcheck() const { -#if CHECK_AUTORELEASEPOOL - return check(); -#else - return (m[0] == M0); -#endif - } - -# undef M1 -}; - - -class AutoreleasePoolPage +class AutoreleasePoolPage : private AutoreleasePoolPageData { + friend struct thread_data_t; + +public: + static size_t const SIZE = +#if PROTECT_AUTORELEASEPOOL + PAGE_MAX_SIZE; // must be multiple of vm page size +#else + PAGE_MIN_SIZE; // size and alignment, power of 2 +#endif + +private: + static pthread_key_t const key = AUTORELEASE_POOL_KEY; + static uint8_t const SCRIBBLE = 0xA3; // 0xA3A3A3A3 after releasing + static size_t const COUNT = SIZE / sizeof(id); + // EMPTY_POOL_PLACEHOLDER is stored in TLS when exactly one pool is // pushed and it has never contained any objects. This saves memory // when the top level (i.e. libdispatch) pushes and pops pools but @@ -634,23 +619,6 @@ class AutoreleasePoolPage # define EMPTY_POOL_PLACEHOLDER ((id*)1) # define POOL_BOUNDARY nil - static pthread_key_t const key = AUTORELEASE_POOL_KEY; - static uint8_t const SCRIBBLE = 0xA3; // 0xA3A3A3A3 after releasing - static size_t const SIZE = -#if PROTECT_AUTORELEASEPOOL - PAGE_MAX_SIZE; // must be multiple of vm page size -#else - PAGE_MAX_SIZE; // size and alignment, power of 2 -#endif - static size_t const COUNT = SIZE / sizeof(id); - - magic_t const magic; - id *next; - pthread_t const thread; - AutoreleasePoolPage * const parent; - AutoreleasePoolPage *child; - uint32_t const depth; - uint32_t hiwat; // SIZE-sizeof(*this) bytes of contents follow @@ -675,15 +643,16 @@ class AutoreleasePoolPage #endif } - AutoreleasePoolPage(AutoreleasePoolPage *newParent) - : magic(), next(begin()), thread(pthread_self()), - parent(newParent), child(nil), - depth(parent ? 1+parent->depth : 0), - hiwat(parent ? parent->hiwat : 0) + AutoreleasePoolPage(AutoreleasePoolPage *newParent) : + AutoreleasePoolPageData(begin(), + objc_thread_self(), + newParent, + newParent ? 1+newParent->depth : 0, + newParent ? newParent->hiwat : 0) { if (parent) { parent->check(); - assert(!parent->child); + ASSERT(!parent->child); parent->unprotect(); parent->child = this; parent->protect(); @@ -695,19 +664,19 @@ class AutoreleasePoolPage { check(); unprotect(); - assert(empty()); + ASSERT(empty()); // Not recursive: we don't want to blow out the stack // if a thread accumulates a stupendous amount of garbage - assert(!child); + ASSERT(!child); } - - void busted(bool die = true) + template + void + busted(Fn log) const { magic_t right; - (die ? _objc_fatal : _objc_inform) - ("autorelease pool page %p corrupted\n" + log("autorelease pool page %p corrupted\n" " magic 0x%08x 0x%08x 0x%08x 0x%08x\n" " should be 0x%08x 0x%08x 0x%08x 0x%08x\n" " pthread %p\n" @@ -715,23 +684,37 @@ class AutoreleasePoolPage this, magic.m[0], magic.m[1], magic.m[2], magic.m[3], right.m[0], right.m[1], right.m[2], right.m[3], - this->thread, pthread_self()); + this->thread, objc_thread_self()); } - void check(bool die = true) + __attribute__((noinline, cold, noreturn)) + void + busted_die() const { - if (!magic.check() || !pthread_equal(thread, pthread_self())) { - busted(die); + busted(_objc_fatal); + __builtin_unreachable(); + } + + inline void + check(bool die = true) const + { + if (!magic.check() || thread != objc_thread_self()) { + if (die) { + busted_die(); + } else { + busted(_objc_inform); + } } } - void fastcheck(bool die = true) + inline void + fastcheck() const { #if CHECK_AUTORELEASEPOOL - check(die); + check(); #else if (! magic.fastcheck()) { - busted(die); + busted_die(); } #endif } @@ -759,7 +742,7 @@ class AutoreleasePoolPage id *add(id obj) { - assert(!full()); + ASSERT(!full()); unprotect(); id *ret = next; // faster than `return next-1` because of aliasing *next++ = obj; @@ -803,7 +786,7 @@ class AutoreleasePoolPage #if DEBUG // we expect any children to be completely empty for (AutoreleasePoolPage *page = child; page; page = page->child) { - assert(page->empty()); + ASSERT(page->empty()); } #endif } @@ -839,8 +822,8 @@ class AutoreleasePoolPage setHotPage((AutoreleasePoolPage *)p); if (AutoreleasePoolPage *page = coldPage()) { - if (!page->empty()) pop(page->begin()); // pop all of the pools - if (DebugMissingPools || DebugPoolAllocation) { + if (!page->empty()) objc_autoreleasePoolPop(page->begin()); // pop all of the pools + if (slowpath(DebugMissingPools || DebugPoolAllocation)) { // pop() killed the pages already } else { page->kill(); // free all of the pages @@ -861,7 +844,7 @@ class AutoreleasePoolPage AutoreleasePoolPage *result; uintptr_t offset = p % SIZE; - assert(offset >= sizeof(AutoreleasePoolPage)); + ASSERT(offset >= sizeof(AutoreleasePoolPage)); result = (AutoreleasePoolPage *)(p - offset); result->fastcheck(); @@ -878,7 +861,7 @@ class AutoreleasePoolPage static inline id* setEmptyPoolPlaceholder() { - assert(tls_get_direct(key) == nil); + ASSERT(tls_get_direct(key) == nil); tls_set_direct(key, (void *)EMPTY_POOL_PLACEHOLDER); return EMPTY_POOL_PLACEHOLDER; } @@ -929,8 +912,8 @@ class AutoreleasePoolPage // The hot page is full. // Step to the next non-full page, adding a new page if necessary. // Then add the object to that page. - assert(page == hotPage()); - assert(page->full() || DebugPoolAllocation); + ASSERT(page == hotPage()); + ASSERT(page->full() || DebugPoolAllocation); do { if (page->child) page = page->child; @@ -946,7 +929,7 @@ class AutoreleasePoolPage { // "No page" could mean no pool has been pushed // or an empty placeholder pool has been pushed and has no contents yet - assert(!hotPage()); + ASSERT(!hotPage()); bool pushExtraBoundary = false; if (haveEmptyPoolPlaceholder()) { @@ -963,7 +946,7 @@ class AutoreleasePoolPage "autoreleased with no pool in place - " "just leaking - break on " "objc_autoreleaseNoPool() to debug", - pthread_self(), (void*)obj, object_getClassName(obj)); + objc_thread_self(), (void*)obj, object_getClassName(obj)); objc_autoreleaseNoPool(obj); return nil; } @@ -1001,10 +984,10 @@ class AutoreleasePoolPage public: static inline id autorelease(id obj) { - assert(obj); - assert(!obj->isTaggedPointer()); + ASSERT(obj); + ASSERT(!obj->isTaggedPointer()); id *dest __unused = autoreleaseFast(obj); - assert(!dest || dest == EMPTY_POOL_PLACEHOLDER || *dest == obj); + ASSERT(!dest || dest == EMPTY_POOL_PLACEHOLDER || *dest == obj); return obj; } @@ -1012,22 +995,23 @@ public: static inline void *push() { id *dest; - if (DebugPoolAllocation) { + if (slowpath(DebugPoolAllocation)) { // Each autorelease pool starts on a new pool page. dest = autoreleaseNewPage(POOL_BOUNDARY); } else { dest = autoreleaseFast(POOL_BOUNDARY); } - assert(dest == EMPTY_POOL_PLACEHOLDER || *dest == POOL_BOUNDARY); + ASSERT(dest == EMPTY_POOL_PLACEHOLDER || *dest == POOL_BOUNDARY); return dest; } + __attribute__((noinline, cold)) static void badPop(void *token) { // Error. For bincompat purposes this is not // fatal in executables built with old SDKs. - if (DebugPoolAllocation || sdkIsAtLeast(10_12, 10_0, 10_0, 3_0)) { + if (DebugPoolAllocation || sdkIsAtLeast(10_12, 10_0, 10_0, 3_0, 2_0)) { // OBJC_DEBUG_POOL_ALLOCATION or new SDK. Bad pop is fatal. _objc_fatal ("Invalid or prematurely-freed autorelease pool %p.", token); @@ -1046,26 +1030,64 @@ public: } objc_autoreleasePoolInvalid(token); } - - static inline void pop(void *token) + + template + static void + popPage(void *token, AutoreleasePoolPage *page, id *stop) + { + if (allowDebug && PrintPoolHiwat) printHiwat(); + + page->releaseUntil(stop); + + // memory: delete empty children + if (allowDebug && DebugPoolAllocation && page->empty()) { + // special case: delete everything during page-per-pool debugging + AutoreleasePoolPage *parent = page->parent; + page->kill(); + setHotPage(parent); + } else if (allowDebug && DebugMissingPools && page->empty() && !page->parent) { + // special case: delete everything for pop(top) + // when debugging missing autorelease pools + page->kill(); + setHotPage(nil); + } else if (page->child) { + // hysteresis: keep one empty child if page is more than half full + if (page->lessThanHalfFull()) { + page->child->kill(); + } + else if (page->child->child) { + page->child->child->kill(); + } + } + } + + __attribute__((noinline, cold)) + static void + popPageDebug(void *token, AutoreleasePoolPage *page, id *stop) + { + popPage(token, page, stop); + } + + static inline void + pop(void *token) { AutoreleasePoolPage *page; id *stop; - if (token == (void*)EMPTY_POOL_PLACEHOLDER) { // Popping the top-level placeholder pool. - if (hotPage()) { - // Pool was used. Pop its contents normally. - // Pool pages remain allocated for re-use as usual. - pop(coldPage()->begin()); - } else { + page = hotPage(); + if (!page) { // Pool was never used. Clear the placeholder. - setHotPage(nil); + return setHotPage(nil); } - return; + // Pool was used. Pop its contents normally. + // Pool pages remain allocated for re-use as usual. + page = coldPage(); + token = page->begin(); + } else { + page = pageForPointer(token); } - page = pageForPointer(token); stop = (id *)token; if (*stop != POOL_BOUNDARY) { if (stop == page->begin() && !page->parent) { @@ -1079,41 +1101,22 @@ public: } } - if (PrintPoolHiwat) printHiwat(); - - page->releaseUntil(stop); - - // memory: delete empty children - if (DebugPoolAllocation && page->empty()) { - // special case: delete everything during page-per-pool debugging - AutoreleasePoolPage *parent = page->parent; - page->kill(); - setHotPage(parent); - } else if (DebugMissingPools && page->empty() && !page->parent) { - // special case: delete everything for pop(top) - // when debugging missing autorelease pools - page->kill(); - setHotPage(nil); - } - else if (page->child) { - // hysteresis: keep one empty child if page is more than half full - if (page->lessThanHalfFull()) { - page->child->kill(); - } - else if (page->child->child) { - page->child->child->kill(); - } + if (slowpath(PrintPoolHiwat || DebugPoolAllocation || DebugMissingPools)) { + return popPageDebug(token, page, stop); } + + return popPage(token, page, stop); } static void init() { int r __unused = pthread_key_init_np(AutoreleasePoolPage::key, AutoreleasePoolPage::tls_dealloc); - assert(r == 0); + ASSERT(r == 0); } - void print() + __attribute__((noinline, cold)) + void print() { _objc_inform("[%p] ................ PAGE %s %s %s", this, full() ? "(full)" : "", @@ -1130,10 +1133,11 @@ public: } } + __attribute__((noinline, cold)) static void printAll() - { + { _objc_inform("##############"); - _objc_inform("AUTORELEASE POOLS for thread %p", pthread_self()); + _objc_inform("AUTORELEASE POOLS for thread %p", objc_thread_self()); AutoreleasePoolPage *page; ptrdiff_t objects = 0; @@ -1157,6 +1161,7 @@ public: _objc_inform("##############"); } + __attribute__((noinline, cold)) static void printHiwat() { // Check and propagate high water mark @@ -1169,11 +1174,11 @@ public: p->hiwat = mark; p->protect(); } - + _objc_inform("POOL HIGHWATER: new high water mark of %u " - "pending releases for thread %p:", - mark, pthread_self()); - + "pending releases for thread %p:", + mark, objc_thread_self()); + void *stack[128]; int count = backtrace(stack, sizeof(stack)/sizeof(stack[0])); char **sym = backtrace_symbols(stack, count); @@ -1187,10 +1192,6 @@ public: #undef POOL_BOUNDARY }; -// anonymous namespace -}; - - /*********************************************************************** * Slow paths for inline control **********************************************************************/ @@ -1204,7 +1205,7 @@ objc_object::rootRetain_overflow(bool tryRetain) } -NEVER_INLINE bool +NEVER_INLINE uintptr_t objc_object::rootRelease_underflow(bool performDealloc) { return rootRelease(performDealloc, true); @@ -1218,7 +1219,7 @@ objc_object::rootRelease_underflow(bool performDealloc) NEVER_INLINE void objc_object::clearDeallocating_slow() { - assert(isa.nonpointer && (isa.weakly_referenced || isa.has_sidetable_rc)); + ASSERT(isa.nonpointer && (isa.weakly_referenced || isa.has_sidetable_rc)); SideTable& table = SideTables()[this]; table.lock(); @@ -1237,7 +1238,7 @@ __attribute__((noinline,used)) id objc_object::rootAutorelease2() { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); return AutoreleasePoolPage::autorelease((id)this); } @@ -1247,13 +1248,12 @@ BREAKPOINT_FUNCTION( ); -NEVER_INLINE -bool +NEVER_INLINE uintptr_t objc_object::overrelease_error() { _objc_inform_now_and_on_crash("%s object %p overreleased while already deallocating; break on objc_overrelease_during_dealloc_error to debug", object_getClassName((id)this), this); objc_overrelease_during_dealloc_error(); - return false; // allow rootRelease() to tail-call this + return 0; // allow rootRelease() to tail-call this } @@ -1307,14 +1307,14 @@ objc_object::sidetable_moveExtraRC_nolock(size_t extra_rc, bool isDeallocating, bool weaklyReferenced) { - assert(!isa.nonpointer); // should already be changed to raw pointer + ASSERT(!isa.nonpointer); // should already be changed to raw pointer SideTable& table = SideTables()[this]; size_t& refcntStorage = table.refcnts[this]; size_t oldRefcnt = refcntStorage; // not deallocating - that was in the isa - assert((oldRefcnt & SIDE_TABLE_DEALLOCATING) == 0); - assert((oldRefcnt & SIDE_TABLE_WEAKLY_REFERENCED) == 0); + ASSERT((oldRefcnt & SIDE_TABLE_DEALLOCATING) == 0); + ASSERT((oldRefcnt & SIDE_TABLE_WEAKLY_REFERENCED) == 0); uintptr_t carry; size_t refcnt = addc(oldRefcnt, extra_rc << SIDE_TABLE_RC_SHIFT, 0, &carry); @@ -1331,14 +1331,14 @@ objc_object::sidetable_moveExtraRC_nolock(size_t extra_rc, bool objc_object::sidetable_addExtraRC_nolock(size_t delta_rc) { - assert(isa.nonpointer); + ASSERT(isa.nonpointer); SideTable& table = SideTables()[this]; size_t& refcntStorage = table.refcnts[this]; size_t oldRefcnt = refcntStorage; // isa-side bits should not be set here - assert((oldRefcnt & SIDE_TABLE_DEALLOCATING) == 0); - assert((oldRefcnt & SIDE_TABLE_WEAKLY_REFERENCED) == 0); + ASSERT((oldRefcnt & SIDE_TABLE_DEALLOCATING) == 0); + ASSERT((oldRefcnt & SIDE_TABLE_WEAKLY_REFERENCED) == 0); if (oldRefcnt & SIDE_TABLE_RC_PINNED) return true; @@ -1362,7 +1362,7 @@ objc_object::sidetable_addExtraRC_nolock(size_t delta_rc) size_t objc_object::sidetable_subExtraRC_nolock(size_t delta_rc) { - assert(isa.nonpointer); + ASSERT(isa.nonpointer); SideTable& table = SideTables()[this]; RefcountMap::iterator it = table.refcnts.find(this); @@ -1373,11 +1373,11 @@ objc_object::sidetable_subExtraRC_nolock(size_t delta_rc) size_t oldRefcnt = it->second; // isa-side bits should not be set here - assert((oldRefcnt & SIDE_TABLE_DEALLOCATING) == 0); - assert((oldRefcnt & SIDE_TABLE_WEAKLY_REFERENCED) == 0); + ASSERT((oldRefcnt & SIDE_TABLE_DEALLOCATING) == 0); + ASSERT((oldRefcnt & SIDE_TABLE_WEAKLY_REFERENCED) == 0); size_t newRefcnt = oldRefcnt - (delta_rc << SIDE_TABLE_RC_SHIFT); - assert(oldRefcnt > newRefcnt); // shouldn't underflow + ASSERT(oldRefcnt > newRefcnt); // shouldn't underflow it->second = newRefcnt; return delta_rc; } @@ -1386,7 +1386,7 @@ objc_object::sidetable_subExtraRC_nolock(size_t delta_rc) size_t objc_object::sidetable_getExtraRC_nolock() { - assert(isa.nonpointer); + ASSERT(isa.nonpointer); SideTable& table = SideTables()[this]; RefcountMap::iterator it = table.refcnts.find(this); if (it == table.refcnts.end()) return 0; @@ -1402,7 +1402,7 @@ id objc_object::sidetable_retain() { #if SUPPORT_NONPOINTER_ISA - assert(!isa.nonpointer); + ASSERT(!isa.nonpointer); #endif SideTable& table = SideTables()[this]; @@ -1421,7 +1421,7 @@ bool objc_object::sidetable_tryRetain() { #if SUPPORT_NONPOINTER_ISA - assert(!isa.nonpointer); + ASSERT(!isa.nonpointer); #endif SideTable& table = SideTables()[this]; @@ -1435,13 +1435,14 @@ objc_object::sidetable_tryRetain() // } bool result = true; - RefcountMap::iterator it = table.refcnts.find(this); - if (it == table.refcnts.end()) { - table.refcnts[this] = SIDE_TABLE_RC_ONE; - } else if (it->second & SIDE_TABLE_DEALLOCATING) { + auto it = table.refcnts.try_emplace(this, SIDE_TABLE_RC_ONE); + auto &refcnt = it.first->second; + if (it.second) { + // there was no entry + } else if (refcnt & SIDE_TABLE_DEALLOCATING) { result = false; - } else if (! (it->second & SIDE_TABLE_RC_PINNED)) { - it->second += SIDE_TABLE_RC_ONE; + } else if (! (refcnt & SIDE_TABLE_RC_PINNED)) { + refcnt += SIDE_TABLE_RC_ONE; } return result; @@ -1509,7 +1510,7 @@ void objc_object::sidetable_setWeaklyReferenced_nolock() { #if SUPPORT_NONPOINTER_ISA - assert(!isa.nonpointer); + ASSERT(!isa.nonpointer); #endif SideTable& table = SideTables()[this]; @@ -1525,27 +1526,27 @@ uintptr_t objc_object::sidetable_release(bool performDealloc) { #if SUPPORT_NONPOINTER_ISA - assert(!isa.nonpointer); + ASSERT(!isa.nonpointer); #endif SideTable& table = SideTables()[this]; bool do_dealloc = false; table.lock(); - RefcountMap::iterator it = table.refcnts.find(this); - if (it == table.refcnts.end()) { + auto it = table.refcnts.try_emplace(this, SIDE_TABLE_DEALLOCATING); + auto &refcnt = it.first->second; + if (it.second) { do_dealloc = true; - table.refcnts[this] = SIDE_TABLE_DEALLOCATING; - } else if (it->second < SIDE_TABLE_DEALLOCATING) { + } else if (refcnt < SIDE_TABLE_DEALLOCATING) { // SIDE_TABLE_WEAKLY_REFERENCED may be set. Don't change it. do_dealloc = true; - it->second |= SIDE_TABLE_DEALLOCATING; - } else if (! (it->second & SIDE_TABLE_RC_PINNED)) { - it->second -= SIDE_TABLE_RC_ONE; + refcnt |= SIDE_TABLE_DEALLOCATING; + } else if (! (refcnt & SIDE_TABLE_RC_PINNED)) { + refcnt -= SIDE_TABLE_RC_ONE; } table.unlock(); if (do_dealloc && performDealloc) { - ((void(*)(objc_object *, SEL))objc_msgSend)(this, SEL_dealloc); + ((void(*)(objc_object *, SEL))objc_msgSend)(this, @selector(dealloc)); } return do_dealloc; } @@ -1578,7 +1579,7 @@ objc_object::sidetable_clearDeallocating() #if __OBJC2__ -__attribute__((aligned(16))) +__attribute__((aligned(16), flatten, noinline)) id objc_retain(id obj) { @@ -1588,7 +1589,7 @@ objc_retain(id obj) } -__attribute__((aligned(16))) +__attribute__((aligned(16), flatten, noinline)) void objc_release(id obj) { @@ -1598,7 +1599,7 @@ objc_release(id obj) } -__attribute__((aligned(16))) +__attribute__((aligned(16), flatten, noinline)) id objc_autorelease(id obj) { @@ -1628,7 +1629,7 @@ id objc_autorelease(id obj) { return [obj autorelease]; } bool _objc_rootTryRetain(id obj) { - assert(obj); + ASSERT(obj); return obj->rootTryRetain(); } @@ -1636,7 +1637,7 @@ _objc_rootTryRetain(id obj) bool _objc_rootIsDeallocating(id obj) { - assert(obj); + ASSERT(obj); return obj->rootIsDeallocating(); } @@ -1645,7 +1646,7 @@ _objc_rootIsDeallocating(id obj) void objc_clear_deallocating(id obj) { - assert(obj); + ASSERT(obj); if (obj->isTaggedPointer()) return; obj->clearDeallocating(); @@ -1655,100 +1656,62 @@ objc_clear_deallocating(id obj) bool _objc_rootReleaseWasZero(id obj) { - assert(obj); + ASSERT(obj); return obj->rootReleaseShouldDealloc(); } -id +NEVER_INLINE id _objc_rootAutorelease(id obj) { - assert(obj); + ASSERT(obj); return obj->rootAutorelease(); } uintptr_t _objc_rootRetainCount(id obj) { - assert(obj); + ASSERT(obj); return obj->rootRetainCount(); } -id +NEVER_INLINE id _objc_rootRetain(id obj) { - assert(obj); + ASSERT(obj); return obj->rootRetain(); } -void +NEVER_INLINE void _objc_rootRelease(id obj) { - assert(obj); + ASSERT(obj); obj->rootRelease(); } -id -_objc_rootAllocWithZone(Class cls, malloc_zone_t *zone) -{ - id obj; - -#if __OBJC2__ - // allocWithZone under __OBJC2__ ignores the zone parameter - (void)zone; - obj = class_createInstance(cls, 0); -#else - if (!zone) { - obj = class_createInstance(cls, 0); - } - else { - obj = class_createInstanceFromZone(cls, 0, zone); - } -#endif - - if (slowpath(!obj)) obj = callBadAllocHandler(cls); - return obj; -} - - // Call [cls alloc] or [cls allocWithZone:nil], with appropriate // shortcutting optimizations. static ALWAYS_INLINE id callAlloc(Class cls, bool checkNil, bool allocWithZone=false) { - if (slowpath(checkNil && !cls)) return nil; - #if __OBJC2__ + if (slowpath(checkNil && !cls)) return nil; if (fastpath(!cls->ISA()->hasCustomAWZ())) { - // No alloc/allocWithZone implementation. Go straight to the allocator. - // fixme store hasCustomAWZ in the non-meta class and - // add it to canAllocFast's summary - if (fastpath(cls->canAllocFast())) { - // No ctors, raw isa, etc. Go straight to the metal. - bool dtor = cls->hasCxxDtor(); - id obj = (id)calloc(1, cls->bits.fastInstanceSize()); - if (slowpath(!obj)) return callBadAllocHandler(cls); - obj->initInstanceIsa(cls, dtor); - return obj; - } - else { - // Has ctor or raw isa or something. Use the slower path. - id obj = class_createInstance(cls, 0); - if (slowpath(!obj)) return callBadAllocHandler(cls); - return obj; - } + return _objc_rootAllocWithZone(cls, nil); } #endif // No shortcuts available. - if (allocWithZone) return [cls allocWithZone:nil]; - return [cls alloc]; + if (allocWithZone) { + return ((id(*)(id, SEL, struct _NSZone *))objc_msgSend)(cls, @selector(allocWithZone:), nil); + } + return ((id(*)(id, SEL))objc_msgSend)(cls, @selector(alloc)); } @@ -1774,11 +1737,86 @@ objc_allocWithZone(Class cls) return callAlloc(cls, true/*checkNil*/, true/*allocWithZone*/); } +// Calls [[cls alloc] init]. +id +objc_alloc_init(Class cls) +{ + return [callAlloc(cls, true/*checkNil*/, false/*allocWithZone*/) init]; +} + +// Calls [cls new] +id +objc_opt_new(Class cls) +{ +#if __OBJC2__ + if (fastpath(cls && !cls->ISA()->hasCustomCore())) { + return [callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/) init]; + } +#endif + return ((id(*)(id, SEL))objc_msgSend)(cls, @selector(new)); +} + +// Calls [obj self] +id +objc_opt_self(id obj) +{ +#if __OBJC2__ + if (fastpath(!obj || obj->isTaggedPointer() || !obj->ISA()->hasCustomCore())) { + return obj; + } +#endif + return ((id(*)(id, SEL))objc_msgSend)(obj, @selector(self)); +} + +// Calls [obj class] +Class +objc_opt_class(id obj) +{ +#if __OBJC2__ + if (slowpath(!obj)) return nil; + Class cls = obj->getIsa(); + if (fastpath(!cls->hasCustomCore())) { + return cls->isMetaClass() ? obj : cls; + } +#endif + return ((Class(*)(id, SEL))objc_msgSend)(obj, @selector(class)); +} + +// Calls [obj isKindOfClass] +BOOL +objc_opt_isKindOfClass(id obj, Class otherClass) +{ +#if __OBJC2__ + if (slowpath(!obj)) return NO; + Class cls = obj->getIsa(); + if (fastpath(!cls->hasCustomCore())) { + for (Class tcls = cls; tcls; tcls = tcls->superclass) { + if (tcls == otherClass) return YES; + } + return NO; + } +#endif + return ((BOOL(*)(id, SEL, Class))objc_msgSend)(obj, @selector(isKindOfClass:), otherClass); +} + +// Calls [obj respondsToSelector] +BOOL +objc_opt_respondsToSelector(id obj, SEL sel) +{ +#if __OBJC2__ + if (slowpath(!obj)) return NO; + Class cls = obj->getIsa(); + if (fastpath(!cls->hasCustomCore())) { + return class_respondsToSelector_inst(obj, sel, cls); + } +#endif + return ((BOOL(*)(id, SEL, SEL))objc_msgSend)(obj, @selector(respondsToSelector:), sel); +} void _objc_rootDealloc(id obj) { - assert(obj); + ASSERT(obj); obj->rootDealloc(); } @@ -1786,7 +1824,7 @@ _objc_rootDealloc(id obj) void _objc_rootFinalize(id obj __unused) { - assert(obj); + ASSERT(obj); _objc_fatal("_objc_rootFinalize called with garbage collection off"); } @@ -1825,6 +1863,7 @@ objc_autoreleasePoolPush(void) return AutoreleasePoolPage::push(); } +NEVER_INLINE void objc_autoreleasePoolPop(void *ctxt) { @@ -1935,7 +1974,8 @@ objc_objectptr_t objc_unretainedPointer(id object) { return object; } void arr_init(void) { AutoreleasePoolPage::init(); - SideTableInit(); + SideTablesMap.init(); + _objc_associations_init(); } @@ -1947,8 +1987,8 @@ void arr_init(void) @interface __NSUnrecognizedTaggedPointer : NSObject @end +__attribute__((objc_nonlazy_class)) @implementation __NSUnrecognizedTaggedPointer -+(void) load { } -(id) retain { return self; } -(oneway void) release { } -(id) autorelease { return self; } @@ -1956,12 +1996,9 @@ void arr_init(void) #endif - +__attribute__((objc_nonlazy_class)) @implementation NSObject -+ (void)load { -} - + (void)initialize { } @@ -1990,7 +2027,7 @@ void arr_init(void) } + (BOOL)isMemberOfClass:(Class)cls { - return object_getClass((id)self) == cls; + return self->ISA() == cls; } - (BOOL)isMemberOfClass:(Class)cls { @@ -1998,7 +2035,7 @@ void arr_init(void) } + (BOOL)isKindOfClass:(Class)cls { - for (Class tcls = object_getClass((id)self); tcls; tcls = tcls->superclass) { + for (Class tcls = self->ISA(); tcls; tcls = tcls->superclass) { if (tcls == cls) return YES; } return NO; @@ -2026,18 +2063,15 @@ void arr_init(void) } + (BOOL)instancesRespondToSelector:(SEL)sel { - if (!sel) return NO; - return class_respondsToSelector(self, sel); + return class_respondsToSelector_inst(nil, sel, self); } + (BOOL)respondsToSelector:(SEL)sel { - if (!sel) return NO; - return class_respondsToSelector_inst(object_getClass(self), sel, self); + return class_respondsToSelector_inst(self, sel, self->ISA()); } - (BOOL)respondsToSelector:(SEL)sel { - if (!sel) return NO; - return class_respondsToSelector_inst([self class], sel, self); + return class_respondsToSelector_inst(self, sel, [self class]); } + (BOOL)conformsToProtocol:(Protocol *)protocol { @@ -2221,7 +2255,7 @@ void arr_init(void) // Replaced by ObjectAlloc - (id)retain { - return ((id)self)->rootRetain(); + return _objc_rootRetain(self); } @@ -2231,7 +2265,7 @@ void arr_init(void) // Replaced by ObjectAlloc - (BOOL)_tryRetain { - return ((id)self)->rootTryRetain(); + return _objc_rootTryRetain(self); } + (BOOL)_isDeallocating { @@ -2239,14 +2273,14 @@ void arr_init(void) } - (BOOL)_isDeallocating { - return ((id)self)->rootIsDeallocating(); + return _objc_rootIsDeallocating(self); } + (BOOL)allowsWeakReference { return YES; } -+ (BOOL)retainWeakReference { ++ (BOOL)retainWeakReference { return YES; } @@ -2263,7 +2297,7 @@ void arr_init(void) // Replaced by ObjectAlloc - (oneway void)release { - ((id)self)->rootRelease(); + _objc_rootRelease(self); } + (id)autorelease { @@ -2272,7 +2306,7 @@ void arr_init(void) // Replaced by ObjectAlloc - (id)autorelease { - return ((id)self)->rootAutorelease(); + return _objc_rootAutorelease(self); } + (NSUInteger)retainCount { @@ -2280,7 +2314,7 @@ void arr_init(void) } - (NSUInteger)retainCount { - return ((id)self)->rootRetainCount(); + return _objc_rootRetainCount(self); } + (id)alloc { diff --git a/runtime/Object.h b/runtime/Object.h index 5c857ac..86ce416 100644 --- a/runtime/Object.h +++ b/runtime/Object.h @@ -38,7 +38,8 @@ #if __OBJC__ && !__OBJC2__ __OSX_AVAILABLE(10.0) -__IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE +__IOS_UNAVAILABLE __TVOS_UNAVAILABLE +__WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ROOT_CLASS @interface Object { diff --git a/runtime/Object.mm b/runtime/Object.mm index 3ec14be..e03b274 100644 --- a/runtime/Object.mm +++ b/runtime/Object.mm @@ -35,14 +35,6 @@ typedef struct objc_object *id; #if __OBJC2__ -__OSX_AVAILABLE(10.0) -__IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE -OBJC_ROOT_CLASS -@interface Object { - Class isa; -} -@end - @implementation Object + (id)initialize diff --git a/runtime/PointerUnion.h b/runtime/PointerUnion.h new file mode 100644 index 0000000..85b7846 --- /dev/null +++ b/runtime/PointerUnion.h @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2019 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +#ifndef POINTERUNION_H +#define POINTERUNION_H + +#include +#include + +namespace objc { + +template struct PointerUnionTypeSelectorReturn { + using Return = T; +}; + +/// Get a type based on whether two types are the same or not. +/// +/// For: +/// +/// \code +/// using Ret = typename PointerUnionTypeSelector::Return; +/// \endcode +/// +/// Ret will be EQ type if T1 is same as T2 or NE type otherwise. +template +struct PointerUnionTypeSelector { + using Return = typename PointerUnionTypeSelectorReturn::Return; +}; + +template +struct PointerUnionTypeSelector { + using Return = typename PointerUnionTypeSelectorReturn::Return; +}; + +template +struct PointerUnionTypeSelectorReturn< + PointerUnionTypeSelector> { + using Return = + typename PointerUnionTypeSelector::Return; +}; + +template +class PointerUnion { + uintptr_t _value; + + static_assert(alignof(PT1) >= 2, "alignment requirement"); + static_assert(alignof(PT2) >= 2, "alignment requirement"); + + struct IsPT1 { + static const uintptr_t Num = 0; + }; + struct IsPT2 { + static const uintptr_t Num = 1; + }; + template struct UNION_DOESNT_CONTAIN_TYPE {}; + + uintptr_t getPointer() const { + return _value & ~1; + } + uintptr_t getTag() const { + return _value & 1; + } + +public: + explicit PointerUnion(const std::atomic &raw) + : _value(raw.load(std::memory_order_relaxed)) + { } + PointerUnion(PT1 t) : _value((uintptr_t)t) { } + PointerUnion(PT2 t) : _value((uintptr_t)t | 1) { } + + void storeAt(std::atomic &raw, std::memory_order order) const { + raw.store(_value, order); + } + + template + bool is() const { + using Ty = typename PointerUnionTypeSelector>>::Return; + return getTag() == Ty::Num; + } + + template T get() const { + ASSERT(is() && "Invalid accessor called"); + return reinterpret_cast(getPointer()); + } + + template T dyn_cast() const { + if (is()) + return get(); + return T(); + } +}; + +template +class PointerUnion4 { + uintptr_t _value; + + static_assert(alignof(PT1) >= 4, "alignment requirement"); + static_assert(alignof(PT2) >= 4, "alignment requirement"); + static_assert(alignof(PT3) >= 4, "alignment requirement"); + static_assert(alignof(PT4) >= 4, "alignment requirement"); + + struct IsPT1 { + static const uintptr_t Num = 0; + }; + struct IsPT2 { + static const uintptr_t Num = 1; + }; + struct IsPT3 { + static const uintptr_t Num = 2; + }; + struct IsPT4 { + static const uintptr_t Num = 3; + }; + template struct UNION_DOESNT_CONTAIN_TYPE {}; + + uintptr_t getPointer() const { + return _value & ~3; + } + uintptr_t getTag() const { + return _value & 3; + } + +public: + explicit PointerUnion4(const std::atomic &raw) + : _value(raw.load(std::memory_order_relaxed)) + { } + PointerUnion4(PT1 t) : _value((uintptr_t)t) { } + PointerUnion4(PT2 t) : _value((uintptr_t)t | 1) { } + PointerUnion4(PT3 t) : _value((uintptr_t)t | 2) { } + PointerUnion4(PT4 t) : _value((uintptr_t)t | 3) { } + + void storeAt(std::atomic &raw, std::memory_order order) const { + raw.store(_value, order); + } + + template + bool is() const { + using Ty = typename PointerUnionTypeSelector>>>>::Return; + return getTag() == Ty::Num; + } + + template T get() const { + ASSERT(is() && "Invalid accessor called"); + return reinterpret_cast(getPointer()); + } + + template T dyn_cast() const { + if (is()) + return get(); + return T(); + } +}; + +} // namespace objc + +#endif /* DENSEMAPEXTRAS_H */ diff --git a/runtime/Protocol.h b/runtime/Protocol.h index 1f2a7b5..953b366 100644 --- a/runtime/Protocol.h +++ b/runtime/Protocol.h @@ -41,7 +41,7 @@ // All methods of class Protocol are unavailable. // Use the functions in objc/runtime.h instead. -OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) +OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0) @interface Protocol : NSObject @end @@ -50,7 +50,7 @@ OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) #include -OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) +OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0) @interface Protocol : Object { @private @@ -74,12 +74,20 @@ OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) __OSX_DEPRECATED(10.0, 10.5, "use protocol_getMethodDescription instead") __IOS_DEPRECATED(2.0, 2.0, "use protocol_getMethodDescription instead") __TVOS_DEPRECATED(9.0, 9.0, "use protocol_getMethodDescription instead") - __WATCHOS_DEPRECATED(1.0, 1.0, "use protocol_getMethodDescription instead"); + __WATCHOS_DEPRECATED(1.0, 1.0, "use protocol_getMethodDescription instead") +#ifndef __APPLE_BLEACH_SDK__ + __BRIDGEOS_DEPRECATED(2.0, 2.0, "use protocol_getMethodDescription instead") +#endif +; - (struct objc_method_description *) descriptionForClassMethod:(SEL)aSel __OSX_DEPRECATED(10.0, 10.5, "use protocol_getMethodDescription instead") __IOS_DEPRECATED(2.0, 2.0, "use protocol_getMethodDescription instead") __TVOS_DEPRECATED(9.0, 9.0, "use protocol_getMethodDescription instead") - __WATCHOS_DEPRECATED(1.0, 1.0, "use protocol_getMethodDescription instead"); + __WATCHOS_DEPRECATED(1.0, 1.0, "use protocol_getMethodDescription instead") +#ifndef __APPLE_BLEACH_SDK__ + __BRIDGEOS_DEPRECATED(2.0, 2.0, "use protocol_getMethodDescription instead") +#endif +; @end diff --git a/runtime/Protocol.mm b/runtime/Protocol.mm index 2a2b3da..9e97419 100644 --- a/runtime/Protocol.mm +++ b/runtime/Protocol.mm @@ -45,22 +45,20 @@ // by CF, so __IncompleteProtocol would be left without an R/R implementation // otherwise, which would break ARC. -@interface __IncompleteProtocol : NSObject @end -@implementation __IncompleteProtocol +@interface __IncompleteProtocol : NSObject +@end + #if __OBJC2__ -// fixme hack - make __IncompleteProtocol a non-lazy class -+ (void) load { } +__attribute__((objc_nonlazy_class)) #endif +@implementation __IncompleteProtocol @end -@implementation Protocol - #if __OBJC2__ -// fixme hack - make Protocol a non-lazy class -+ (void) load { } +__attribute__((objc_nonlazy_class)) #endif - +@implementation Protocol - (BOOL) conformsTo: (Protocol *)aProtocolObj { diff --git a/runtime/a1a2-blocktramps-arm.S b/runtime/a1a2-blocktramps-arm.S deleted file mode 100644 index 9e54078..0000000 --- a/runtime/a1a2-blocktramps-arm.S +++ /dev/null @@ -1,148 +0,0 @@ -#if __arm__ - -#include -#include - -.syntax unified - -.text - - .private_extern __a1a2_tramphead - .private_extern __a1a2_firsttramp - .private_extern __a1a2_trampend - -// Trampoline machinery assumes the trampolines are Thumb function pointers -#if !__thumb2__ -# error sorry -#endif - -.thumb -.thumb_func __a1a2_tramphead -.thumb_func __a1a2_firsttramp -.thumb_func __a1a2_trampend - -.align PAGE_MAX_SHIFT -__a1a2_tramphead: - /* - r0 == self - r12 == pc of trampoline's first instruction + PC bias - lr == original return address - */ - - mov r1, r0 // _cmd = self - - // Trampoline's data is one page before the trampoline text. - // Also correct PC bias of 4 bytes. - sub r12, #PAGE_MAX_SIZE - ldr r0, [r12, #-4] // self = block object - ldr pc, [r0, #12] // tail call block->invoke - // not reached - - // Align trampolines to 8 bytes -.align 3 - -.macro TrampolineEntry - mov r12, pc - b __a1a2_tramphead -.align 3 -.endmacro - -.macro TrampolineEntryX16 - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry -.endmacro - -.macro TrampolineEntryX256 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 -.endmacro - -.private_extern __a1a2_firsttramp -__a1a2_firsttramp: - // 2048-2 trampolines to fill 16K page - TrampolineEntryX256 - TrampolineEntryX256 - TrampolineEntryX256 - TrampolineEntryX256 - - TrampolineEntryX256 - TrampolineEntryX256 - TrampolineEntryX256 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - - TrampolineEntry - TrampolineEntry - // TrampolineEntry - // TrampolineEntry - -.private_extern __a1a2_trampend -__a1a2_trampend: - -#endif diff --git a/runtime/a1a2-blocktramps-i386.S b/runtime/a1a2-blocktramps-i386.S deleted file mode 100755 index e4579c0..0000000 --- a/runtime/a1a2-blocktramps-i386.S +++ /dev/null @@ -1,566 +0,0 @@ -/* - * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved. - * - * @APPLE_LICENSE_HEADER_START@ - * - * This file contains Original Code and/or Modifications of Original Code - * as defined in and that are subject to the Apple Public Source License - * Version 2.0 (the 'License'). You may not use this file except in - * compliance with the License. Please obtain a copy of the License at - * http://www.opensource.apple.com/apsl/ and read it before using this - * file. - * - * The Original Code and all software distributed under the License are - * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER - * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, - * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. - * Please see the License for the specific language governing rights and - * limitations under the License. - * - * @APPLE_LICENSE_HEADER_END@ - */ - -#ifdef __i386__ - -#include - -.text - .private_extern __a1a2_tramphead - .private_extern __a1a2_firsttramp - .private_extern __a1a2_nexttramp - .private_extern __a1a2_trampend - -.align PAGE_SHIFT -__a1a2_tramphead: - popl %eax - andl $0xFFFFFFF8, %eax - subl $ PAGE_SIZE, %eax - movl 4(%esp), %ecx // self -> ecx - movl %ecx, 8(%esp) // ecx -> _cmd - movl (%eax), %ecx // blockPtr -> ecx - movl %ecx, 4(%esp) // ecx -> self - jmp *12(%ecx) // tail to block->invoke - -.macro TrampolineEntry - call __a1a2_tramphead - nop - nop - nop -.endmacro - -.align 5 -__a1a2_firsttramp: - TrampolineEntry -__a1a2_nexttramp: // used to calculate size of each trampoline - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - -__a1a2_trampend: - -#endif diff --git a/runtime/a1a2-blocktramps-x86_64.S b/runtime/a1a2-blocktramps-x86_64.S deleted file mode 100755 index 696eb59..0000000 --- a/runtime/a1a2-blocktramps-x86_64.S +++ /dev/null @@ -1,564 +0,0 @@ -/* - * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved. - * - * @APPLE_LICENSE_HEADER_START@ - * - * This file contains Original Code and/or Modifications of Original Code - * as defined in and that are subject to the Apple Public Source License - * Version 2.0 (the 'License'). You may not use this file except in - * compliance with the License. Please obtain a copy of the License at - * http://www.opensource.apple.com/apsl/ and read it before using this - * file. - * - * The Original Code and all software distributed under the License are - * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER - * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, - * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. - * Please see the License for the specific language governing rights and - * limitations under the License. - * - * @APPLE_LICENSE_HEADER_END@ - */ - -#ifdef __x86_64__ - -#include - - .text - .private_extern __a1a2_tramphead - .private_extern __a1a2_firsttramp - .private_extern __a1a2_nexttramp - .private_extern __a1a2_trampend - -.align PAGE_SHIFT -__a1a2_tramphead: - popq %r10 - andq $0xFFFFFFFFFFFFFFF8, %r10 - subq $ PAGE_SIZE, %r10 - movq %rdi, %rsi // arg1 -> arg2 - movq (%r10), %rdi // block -> arg1 - jmp *16(%rdi) - -.macro TrampolineEntry - callq __a1a2_tramphead - nop - nop - nop -.endmacro - -.align 5 -__a1a2_firsttramp: - TrampolineEntry -__a1a2_nexttramp: - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - -__a1a2_trampend: - -#endif diff --git a/runtime/a2a3-blocktramps-arm.S b/runtime/a2a3-blocktramps-arm.S deleted file mode 100644 index ac0ce72..0000000 --- a/runtime/a2a3-blocktramps-arm.S +++ /dev/null @@ -1,148 +0,0 @@ -#if __arm__ - -#include -#include - -.syntax unified - -.text - - .private_extern __a2a3_tramphead - .private_extern __a2a3_firsttramp - .private_extern __a2a3_trampend - -// Trampoline machinery assumes the trampolines are Thumb function pointers -#if !__thumb2__ -# error sorry -#endif - -.thumb -.thumb_func __a2a3_tramphead -.thumb_func __a2a3_firsttramp -.thumb_func __a2a3_trampend - -.align PAGE_MAX_SHIFT -__a2a3_tramphead: - /* - r1 == self - r12 == pc of trampoline's first instruction + PC bias - lr == original return address - */ - - mov r2, r1 // _cmd = self - - // Trampoline's data is one page before the trampoline text. - // Also correct PC bias of 4 bytes. - sub r12, #PAGE_MAX_SIZE - ldr r1, [r12, #-4] // self = block object - ldr pc, [r1, #12] // tail call block->invoke - // not reached - - // Align trampolines to 8 bytes -.align 3 - -.macro TrampolineEntry - mov r12, pc - b __a2a3_tramphead -.align 3 -.endmacro - -.macro TrampolineEntryX16 - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry -.endmacro - -.macro TrampolineEntryX256 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 -.endmacro - -.private_extern __a2a3_firsttramp -__a2a3_firsttramp: - // 2048-2 trampolines to fill 16K page - TrampolineEntryX256 - TrampolineEntryX256 - TrampolineEntryX256 - TrampolineEntryX256 - - TrampolineEntryX256 - TrampolineEntryX256 - TrampolineEntryX256 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntryX16 - TrampolineEntryX16 - TrampolineEntryX16 - - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - - TrampolineEntry - TrampolineEntry - // TrampolineEntry - // TrampolineEntry - -.private_extern __a2a3_trampend -__a2a3_trampend: - -#endif diff --git a/runtime/a2a3-blocktramps-i386.S b/runtime/a2a3-blocktramps-i386.S deleted file mode 100755 index d9932f6..0000000 --- a/runtime/a2a3-blocktramps-i386.S +++ /dev/null @@ -1,566 +0,0 @@ -/* - * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved. - * - * @APPLE_LICENSE_HEADER_START@ - * - * This file contains Original Code and/or Modifications of Original Code - * as defined in and that are subject to the Apple Public Source License - * Version 2.0 (the 'License'). You may not use this file except in - * compliance with the License. Please obtain a copy of the License at - * http://www.opensource.apple.com/apsl/ and read it before using this - * file. - * - * The Original Code and all software distributed under the License are - * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER - * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, - * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. - * Please see the License for the specific language governing rights and - * limitations under the License. - * - * @APPLE_LICENSE_HEADER_END@ - */ - -#ifdef __i386__ - -#include - -.text - .private_extern __a2a3_tramphead - .private_extern __a2a3_firsttramp - .private_extern __a2a3_nexttramp - .private_extern __a2a3_trampend - -.align PAGE_SHIFT -__a2a3_tramphead: - popl %eax - andl $0xFFFFFFF8, %eax - subl $ PAGE_SIZE, %eax - movl 8(%esp), %ecx // self -> ecx - movl %ecx, 12(%esp) // ecx -> _cmd - movl (%eax), %ecx // blockPtr -> ecx - movl %ecx, 8(%esp) // ecx -> self - jmp *12(%ecx) // tail to block->invoke - -.macro TrampolineEntry - call __a2a3_tramphead - nop - nop - nop -.endmacro - -.align 5 -__a2a3_firsttramp: - TrampolineEntry -__a2a3_nexttramp: - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - -__a2a3_trampend: - -#endif diff --git a/runtime/a2a3-blocktramps-x86_64.S b/runtime/a2a3-blocktramps-x86_64.S deleted file mode 100755 index 4904ac4..0000000 --- a/runtime/a2a3-blocktramps-x86_64.S +++ /dev/null @@ -1,565 +0,0 @@ -/* - * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved. - * - * @APPLE_LICENSE_HEADER_START@ - * - * This file contains Original Code and/or Modifications of Original Code - * as defined in and that are subject to the Apple Public Source License - * Version 2.0 (the 'License'). You may not use this file except in - * compliance with the License. Please obtain a copy of the License at - * http://www.opensource.apple.com/apsl/ and read it before using this - * file. - * - * The Original Code and all software distributed under the License are - * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER - * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, - * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. - * Please see the License for the specific language governing rights and - * limitations under the License. - * - * @APPLE_LICENSE_HEADER_END@ - */ - -#ifdef __x86_64__ - -#include - - .text - .private_extern __a2a3_tramphead - .private_extern __a2a3_firsttramp - .private_extern __a2a3_nexttramp - .private_extern __a2a3_trampend - -.align PAGE_SHIFT -__a2a3_tramphead: - popq %r10 - andq $0xFFFFFFFFFFFFFFF8, %r10 - subq $ PAGE_SIZE, %r10 - // %rdi -- first arg -- is address of return value's space. Don't mess with it. - movq %rsi, %rdx // arg2 -> arg3 - movq (%r10), %rsi // block -> arg2 - jmp *16(%rsi) - -.macro TrampolineEntry - callq __a2a3_tramphead - nop - nop - nop -.endmacro - -.align 5 -__a2a3_firsttramp: - TrampolineEntry -__a2a3_nexttramp: - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - TrampolineEntry - -__a2a3_trampend: - -#endif diff --git a/runtime/arm64-asm.h b/runtime/arm64-asm.h new file mode 100644 index 0000000..fb15e5e --- /dev/null +++ b/runtime/arm64-asm.h @@ -0,0 +1,174 @@ +/* + * @APPLE_LICENSE_HEADER_START@ + * + * Copyright (c) 2018 Apple Inc. All Rights Reserved. + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ +/******************************************************************** + * + * arm64-asm.h - asm tools for arm64/arm64_32 and ROP/JOP + * + ********************************************************************/ + +#if __arm64__ + +#if __LP64__ +// true arm64 + +#define SUPPORT_TAGGED_POINTERS 1 +#define PTR .quad +#define PTRSIZE 8 +#define PTRSHIFT 3 // 1<= 2 || (__arm64__ && !__LP64__) + // armv7k or arm64_32 + +# define ISA_INDEX_IS_NPI_BIT 0 +# define ISA_INDEX_IS_NPI_MASK 0x00000001 +# define ISA_INDEX_MASK 0x0001FFFC +# define ISA_INDEX_SHIFT 2 +# define ISA_INDEX_BITS 15 +# define ISA_INDEX_COUNT (1 << ISA_INDEX_BITS) +# define ISA_INDEX_MAGIC_MASK 0x001E0001 +# define ISA_INDEX_MAGIC_VALUE 0x001C0001 +# define ISA_BITFIELD \ + uintptr_t nonpointer : 1; \ + uintptr_t has_assoc : 1; \ + uintptr_t indexcls : 15; \ + uintptr_t magic : 4; \ + uintptr_t has_cxx_dtor : 1; \ + uintptr_t weakly_referenced : 1; \ + uintptr_t deallocating : 1; \ + uintptr_t has_sidetable_rc : 1; \ + uintptr_t extra_rc : 7 +# define RC_ONE (1ULL<<25) +# define RC_HALF (1ULL<<6) + +# else +# error unknown architecture for indexed isa +# endif + +// SUPPORT_INDEXED_ISA +#endif + + +// _OBJC_ISA_H_ +#endif diff --git a/runtime/llvm-DenseMap.h b/runtime/llvm-DenseMap.h index 6fe1382..893e84e 100644 --- a/runtime/llvm-DenseMap.h +++ b/runtime/llvm-DenseMap.h @@ -11,7 +11,7 @@ // //===----------------------------------------------------------------------===// -// Taken from llvmCore-3425.0.31. +// Taken from clang-1100.247.11.10.9 #ifndef LLVM_ADT_DENSEMAP_H #define LLVM_ADT_DENSEMAP_H @@ -21,119 +21,179 @@ #include "llvm-AlignOf.h" #include "llvm-DenseMapInfo.h" #include -#include -#include -#include #include -#include #include #include +#include +#include +#include +#include #include #include "objc-private.h" -// From llvm/Support/Compiler.h -#define LLVM_USE_RVALUE_REFERENCES 1 -#define llvm_move(value) (::std::move(value)) - #define MIN_BUCKETS 4 #define MIN_COMPACT 1024 +#define LLVM_UNLIKELY slowpath +#define LLVM_LIKELY fastpath namespace objc { -template, - bool IsConst = false> +namespace detail { + +// We extend a pair to allow users to override the bucket type with their own +// implementation without requiring two members. +template +struct DenseMapPair : public std::pair { + + // FIXME: Switch to inheriting constructors when we drop support for older + // clang versions. + // NOTE: This default constructor is declared with '{}' rather than + // '= default' to work around a separate bug in clang-3.8. This can + // also go when we switch to inheriting constructors. + DenseMapPair() {} + + DenseMapPair(const KeyT &Key, const ValueT &Value) + : std::pair(Key, Value) {} + + DenseMapPair(KeyT &&Key, ValueT &&Value) + : std::pair(std::move(Key), std::move(Value)) {} + + template + DenseMapPair(AltKeyT &&AltKey, AltValueT &&AltValue, + typename std::enable_if< + std::is_convertible::value && + std::is_convertible::value>::type * = 0) + : std::pair(std::forward(AltKey), + std::forward(AltValue)) {} + + template + DenseMapPair(AltPairT &&AltPair, + typename std::enable_if>::value>::type * = 0) + : std::pair(std::forward(AltPair)) {} + + KeyT &getFirst() { return std::pair::first; } + const KeyT &getFirst() const { return std::pair::first; } + ValueT &getSecond() { return std::pair::second; } + const ValueT &getSecond() const { return std::pair::second; } +}; + +} // end namespace detail + +template < + typename KeyT, typename ValueT, + typename ValueInfoT = DenseMapValueInfo, + typename KeyInfoT = DenseMapInfo, + typename Bucket = detail::DenseMapPair, + bool IsConst = false> class DenseMapIterator; -// ZeroValuesArePurgeable=true is used by the refcount table. +// ValueInfoT is used by the refcount table. // A key/value pair with value==0 is not required to be stored // in the refcount table; it could correctly be erased instead. // For performance, we do keep zero values in the table when the // true refcount decreases to 1: this makes any future retain faster. // For memory size, we allow rehashes and table insertions to // remove a zero value as if it were a tombstone. - -template +template class DenseMapBase { -protected: - typedef std::pair BucketT; + template + using const_arg_type_t = typename const_pointer_or_const_ref::type; public: - typedef KeyT key_type; - typedef ValueT mapped_type; - typedef BucketT value_type; + using size_type = unsigned; + using key_type = KeyT; + using mapped_type = ValueT; + using value_type = BucketT; + + using iterator = DenseMapIterator; + using const_iterator = + DenseMapIterator; - typedef DenseMapIterator iterator; - typedef DenseMapIterator const_iterator; inline iterator begin() { - // When the map is empty, avoid the overhead of AdvancePastEmptyBuckets(). - return empty() ? end() : iterator(getBuckets(), getBucketsEnd()); + // When the map is empty, avoid the overhead of advancing/retreating past + // empty buckets. + if (empty()) + return end(); + return makeIterator(getBuckets(), getBucketsEnd()); } inline iterator end() { - return iterator(getBucketsEnd(), getBucketsEnd(), true); + return makeIterator(getBucketsEnd(), getBucketsEnd(), true); } inline const_iterator begin() const { - return empty() ? end() : const_iterator(getBuckets(), getBucketsEnd()); + if (empty()) + return end(); + return makeConstIterator(getBuckets(), getBucketsEnd()); } inline const_iterator end() const { - return const_iterator(getBucketsEnd(), getBucketsEnd(), true); + return makeConstIterator(getBucketsEnd(), getBucketsEnd(), true); } - bool empty() const { return getNumEntries() == 0; } + bool empty() const { + return getNumEntries() == 0; + } unsigned size() const { return getNumEntries(); } - /// Grow the densemap so that it has at least Size buckets. Does not shrink - void resize(size_t Size) { - if (Size > getNumBuckets()) - grow(Size); + /// Grow the densemap so that it can contain at least \p NumEntries items + /// before resizing again. + void reserve(size_type NumEntries) { + auto NumBuckets = getMinBucketToReserveForEntries(NumEntries); + if (NumBuckets > getNumBuckets()) + grow(NumBuckets); } void clear() { if (getNumEntries() == 0 && getNumTombstones() == 0) return; - + // If the capacity of the array is huge, and the # elements used is small, // shrink the array. - if (getNumEntries() * 4 < getNumBuckets() && - getNumBuckets() > MIN_BUCKETS) { + if (getNumEntries() * 4 < getNumBuckets() && getNumBuckets() > MIN_BUCKETS) { shrink_and_clear(); return; } const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey(); + if (is_trivially_copyable::value && + is_trivially_copyable::value) { + // Use a simpler loop when these are trivial types. + for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) + P->getFirst() = EmptyKey; + } else { + unsigned NumEntries = getNumEntries(); for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) { - if (!KeyInfoT::isEqual(P->first, EmptyKey)) { - if (!KeyInfoT::isEqual(P->first, TombstoneKey)) { - P->second.~ValueT(); - decrementNumEntries(); + if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey)) { + if (!KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) { + P->getSecond().~ValueT(); + --NumEntries; + } + P->getFirst() = EmptyKey; } - P->first = EmptyKey; } + ASSERT(NumEntries == 0 && "Node count imbalance!"); } - assert(getNumEntries() == 0 && "Node count imbalance!"); + setNumEntries(0); setNumTombstones(0); } - /// count - Return true if the specified key is in the map. - bool count(const KeyT &Val) const { + /// Return 1 if the specified key is in the map, 0 otherwise. + size_type count(const_arg_type_t Val) const { const BucketT *TheBucket; - return LookupBucketFor(Val, TheBucket); + return LookupBucketFor(Val, TheBucket) ? 1 : 0; } - iterator find(const KeyT &Val) { + iterator find(const_arg_type_t Val) { BucketT *TheBucket; if (LookupBucketFor(Val, TheBucket)) - return iterator(TheBucket, getBucketsEnd(), true); + return makeIterator(TheBucket, getBucketsEnd(), true); return end(); } - const_iterator find(const KeyT &Val) const { + const_iterator find(const_arg_type_t Val) const { const BucketT *TheBucket; if (LookupBucketFor(Val, TheBucket)) - return const_iterator(TheBucket, getBucketsEnd(), true); + return makeConstIterator(TheBucket, getBucketsEnd(), true); return end(); } @@ -146,23 +206,23 @@ public: iterator find_as(const LookupKeyT &Val) { BucketT *TheBucket; if (LookupBucketFor(Val, TheBucket)) - return iterator(TheBucket, getBucketsEnd(), true); + return makeIterator(TheBucket, getBucketsEnd(), true); return end(); } template const_iterator find_as(const LookupKeyT &Val) const { const BucketT *TheBucket; if (LookupBucketFor(Val, TheBucket)) - return const_iterator(TheBucket, getBucketsEnd(), true); + return makeConstIterator(TheBucket, getBucketsEnd(), true); return end(); } /// lookup - Return the entry for the specified key, or a default /// constructed value if no such entry exists. - ValueT lookup(const KeyT &Val) const { + ValueT lookup(const_arg_type_t Val) const { const BucketT *TheBucket; if (LookupBucketFor(Val, TheBucket)) - return TheBucket->second; + return TheBucket->getSecond(); return ValueT(); } @@ -170,14 +230,73 @@ public: // If the key is already in the map, it returns false and doesn't update the // value. std::pair insert(const std::pair &KV) { + return try_emplace(KV.first, KV.second); + } + + // Inserts key,value pair into the map if the key isn't already in the map. + // If the key is already in the map, it returns false and doesn't update the + // value. + std::pair insert(std::pair &&KV) { + return try_emplace(std::move(KV.first), std::move(KV.second)); + } + + // Inserts key,value pair into the map if the key isn't already in the map. + // The value is constructed in-place if the key is not in the map, otherwise + // it is not moved. + template + std::pair try_emplace(KeyT &&Key, Ts &&... Args) { BucketT *TheBucket; - if (LookupBucketFor(KV.first, TheBucket)) - return std::make_pair(iterator(TheBucket, getBucketsEnd(), true), - false); // Already in map. + if (LookupBucketFor(Key, TheBucket)) + return std::make_pair( + makeIterator(TheBucket, getBucketsEnd(), true), + false); // Already in map. // Otherwise, insert the new element. - TheBucket = InsertIntoBucket(KV.first, KV.second, TheBucket); - return std::make_pair(iterator(TheBucket, getBucketsEnd(), true), true); + TheBucket = + InsertIntoBucket(TheBucket, std::move(Key), std::forward(Args)...); + return std::make_pair( + makeIterator(TheBucket, getBucketsEnd(), true), + true); + } + + // Inserts key,value pair into the map if the key isn't already in the map. + // The value is constructed in-place if the key is not in the map, otherwise + // it is not moved. + template + std::pair try_emplace(const KeyT &Key, Ts &&... Args) { + BucketT *TheBucket; + if (LookupBucketFor(Key, TheBucket)) + return std::make_pair( + makeIterator(TheBucket, getBucketsEnd(), true), + false); // Already in map. + + // Otherwise, insert the new element. + TheBucket = InsertIntoBucket(TheBucket, Key, std::forward(Args)...); + return std::make_pair( + makeIterator(TheBucket, getBucketsEnd(), true), + true); + } + + /// Alternate version of insert() which allows a different, and possibly + /// less expensive, key type. + /// The DenseMapInfo is responsible for supplying methods + /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key + /// type used. + template + std::pair insert_as(std::pair &&KV, + const LookupKeyT &Val) { + BucketT *TheBucket; + if (LookupBucketFor(Val, TheBucket)) + return std::make_pair( + makeIterator(TheBucket, getBucketsEnd(), *this, true), + false); // Already in map. + + // Otherwise, insert the new element. + TheBucket = InsertIntoBucketWithLookup(TheBucket, std::move(KV.first), + std::move(KV.second), Val); + return std::make_pair( + makeIterator(TheBucket, getBucketsEnd(), *this, true), + true); } /// insert - Range insertion of pairs. @@ -192,9 +311,9 @@ public: void compact() { if (getNumEntries() == 0) { shrink_and_clear(); - } - else if (getNumBuckets() / 16 > getNumEntries() && - getNumBuckets() > MIN_COMPACT) + } + else if (getNumBuckets() / 16 > getNumEntries() && + getNumBuckets() > MIN_COMPACT) { grow(getNumEntries() * 2); } @@ -205,8 +324,8 @@ public: if (!LookupBucketFor(Val, TheBucket)) return false; // not in map. - TheBucket->second.~ValueT(); - TheBucket->first = getTombstoneKey(); + TheBucket->getSecond().~ValueT(); + TheBucket->getFirst() = getTombstoneKey(); decrementNumEntries(); incrementNumTombstones(); compact(); @@ -214,8 +333,8 @@ public: } void erase(iterator I) { BucketT *TheBucket = &*I; - TheBucket->second.~ValueT(); - TheBucket->first = getTombstoneKey(); + TheBucket->getSecond().~ValueT(); + TheBucket->getFirst() = getTombstoneKey(); decrementNumEntries(); incrementNumTombstones(); compact(); @@ -226,26 +345,24 @@ public: if (LookupBucketFor(Key, TheBucket)) return *TheBucket; - return *InsertIntoBucket(Key, ValueT(), TheBucket); + return *InsertIntoBucket(TheBucket, Key); } ValueT &operator[](const KeyT &Key) { return FindAndConstruct(Key).second; } -#if LLVM_USE_RVALUE_REFERENCES value_type& FindAndConstruct(KeyT &&Key) { BucketT *TheBucket; if (LookupBucketFor(Key, TheBucket)) return *TheBucket; - return *InsertIntoBucket(Key, ValueT(), TheBucket); + return *InsertIntoBucket(TheBucket, std::move(Key)); } ValueT &operator[](KeyT &&Key) { - return FindAndConstruct(Key).second; + return FindAndConstruct(std::move(Key)).second; } -#endif /// isPointerIntoBucketsArray - Return true if the specified pointer points /// somewhere into the DenseMap's array of buckets (i.e. either to a key or @@ -260,7 +377,7 @@ public: const void *getPointerIntoBucketsArray() const { return getBuckets(); } protected: - DenseMapBase() {} + DenseMapBase() = default; void destroyAll() { if (getNumBuckets() == 0) // Nothing to do. @@ -268,26 +385,33 @@ protected: const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey(); for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) { - if (!KeyInfoT::isEqual(P->first, EmptyKey) && - !KeyInfoT::isEqual(P->first, TombstoneKey)) - P->second.~ValueT(); - P->first.~KeyT(); - } - -#ifndef NDEBUG - memset((void*)getBuckets(), 0x5a, sizeof(BucketT)*getNumBuckets()); -#endif + if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) && + !KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) + P->getSecond().~ValueT(); + P->getFirst().~KeyT(); } + } void initEmpty() { setNumEntries(0); setNumTombstones(0); - assert((getNumBuckets() & (getNumBuckets()-1)) == 0 && + ASSERT((getNumBuckets() & (getNumBuckets()-1)) == 0 && "# initial buckets must be a power of two!"); const KeyT EmptyKey = getEmptyKey(); for (BucketT *B = getBuckets(), *E = getBucketsEnd(); B != E; ++B) - new (&B->first) KeyT(EmptyKey); + ::new (&B->getFirst()) KeyT(EmptyKey); + } + + /// Returns the number of buckets to allocate to ensure that the DenseMap can + /// accommodate \p NumEntries without need to grow(). + unsigned getMinBucketToReserveForEntries(unsigned NumEntries) { + // Ensure that "NumEntries * 4 < NumBuckets * 3" + if (NumEntries == 0) + return 0; + // +1 is required because of the strict equality. + // For example if NumEntries is 48, we need to return 401. + return NextPowerOf2(NumEntries * 4 / 3 + 1); } void moveFromOldBuckets(BucketT *OldBucketsBegin, BucketT *OldBucketsEnd) { @@ -297,106 +421,129 @@ protected: const KeyT EmptyKey = getEmptyKey(); const KeyT TombstoneKey = getTombstoneKey(); for (BucketT *B = OldBucketsBegin, *E = OldBucketsEnd; B != E; ++B) { - if (!KeyInfoT::isEqual(B->first, EmptyKey) && - !KeyInfoT::isEqual(B->first, TombstoneKey) && - !(ZeroValuesArePurgeable && B->second == 0)) { + if (ValueInfoT::isPurgeable(B->getSecond())) { + // Free the value. + B->getSecond().~ValueT(); + } else if (!KeyInfoT::isEqual(B->getFirst(), EmptyKey) && + !KeyInfoT::isEqual(B->getFirst(), TombstoneKey)) { // Insert the key/value into the new table. BucketT *DestBucket; - bool FoundVal = LookupBucketFor(B->first, DestBucket); + bool FoundVal = LookupBucketFor(B->getFirst(), DestBucket); (void)FoundVal; // silence warning. - assert(!FoundVal && "Key already in new map?"); - DestBucket->first = llvm_move(B->first); - new (&DestBucket->second) ValueT(llvm_move(B->second)); + ASSERT(!FoundVal && "Key already in new map?"); + DestBucket->getFirst() = std::move(B->getFirst()); + ::new (&DestBucket->getSecond()) ValueT(std::move(B->getSecond())); incrementNumEntries(); - - // Free the value. - B->second.~ValueT(); - } - B->first.~KeyT(); - } -#ifndef NDEBUG - if (OldBucketsBegin != OldBucketsEnd) - memset((void*)OldBucketsBegin, 0x5a, - sizeof(BucketT) * (OldBucketsEnd - OldBucketsBegin)); -#endif + // Free the value. + B->getSecond().~ValueT(); + } + B->getFirst().~KeyT(); + } } template - void copyFrom(const DenseMapBase& other) { - assert(getNumBuckets() == other.getNumBuckets()); + void copyFrom( + const DenseMapBase &other) { + ASSERT(&other != this); + ASSERT(getNumBuckets() == other.getNumBuckets()); setNumEntries(other.getNumEntries()); setNumTombstones(other.getNumTombstones()); - if (isPodLike::value && isPodLike::value) - memcpy(getBuckets(), other.getBuckets(), + if (is_trivially_copyable::value && + is_trivially_copyable::value) + memcpy(reinterpret_cast(getBuckets()), other.getBuckets(), getNumBuckets() * sizeof(BucketT)); else for (size_t i = 0; i < getNumBuckets(); ++i) { - new (&getBuckets()[i].first) KeyT(other.getBuckets()[i].first); - if (!KeyInfoT::isEqual(getBuckets()[i].first, getEmptyKey()) && - !KeyInfoT::isEqual(getBuckets()[i].first, getTombstoneKey())) - new (&getBuckets()[i].second) ValueT(other.getBuckets()[i].second); + ::new (&getBuckets()[i].getFirst()) + KeyT(other.getBuckets()[i].getFirst()); + if (!KeyInfoT::isEqual(getBuckets()[i].getFirst(), getEmptyKey()) && + !KeyInfoT::isEqual(getBuckets()[i].getFirst(), getTombstoneKey())) + ::new (&getBuckets()[i].getSecond()) + ValueT(other.getBuckets()[i].getSecond()); } } - void swap(DenseMapBase& RHS) { - std::swap(getNumEntries(), RHS.getNumEntries()); - std::swap(getNumTombstones(), RHS.getNumTombstones()); - } - static unsigned getHashValue(const KeyT &Val) { return KeyInfoT::getHashValue(Val); } + template static unsigned getHashValue(const LookupKeyT &Val) { return KeyInfoT::getHashValue(Val); } + static const KeyT getEmptyKey() { + static_assert(std::is_base_of::value, + "Must pass the derived type to this template!"); return KeyInfoT::getEmptyKey(); } + static const KeyT getTombstoneKey() { return KeyInfoT::getTombstoneKey(); } private: + iterator makeIterator(BucketT *P, BucketT *E, + bool NoAdvance=false) { + return iterator(P, E, NoAdvance); + } + + const_iterator makeConstIterator(const BucketT *P, const BucketT *E, + const bool NoAdvance=false) const { + return const_iterator(P, E, NoAdvance); + } + unsigned getNumEntries() const { return static_cast(this)->getNumEntries(); } + void setNumEntries(unsigned Num) { static_cast(this)->setNumEntries(Num); } + void incrementNumEntries() { setNumEntries(getNumEntries() + 1); } + void decrementNumEntries() { setNumEntries(getNumEntries() - 1); } + unsigned getNumTombstones() const { return static_cast(this)->getNumTombstones(); } + void setNumTombstones(unsigned Num) { static_cast(this)->setNumTombstones(Num); } + void incrementNumTombstones() { setNumTombstones(getNumTombstones() + 1); } + void decrementNumTombstones() { setNumTombstones(getNumTombstones() - 1); } + const BucketT *getBuckets() const { return static_cast(this)->getBuckets(); } + BucketT *getBuckets() { return static_cast(this)->getBuckets(); } + unsigned getNumBuckets() const { return static_cast(this)->getNumBuckets(); } + BucketT *getBucketsEnd() { return getBuckets() + getNumBuckets(); } + const BucketT *getBucketsEnd() const { return getBuckets() + getNumBuckets(); } @@ -409,39 +556,32 @@ private: static_cast(this)->shrink_and_clear(); } + template + BucketT *InsertIntoBucket(BucketT *TheBucket, KeyArg &&Key, + ValueArgs &&... Values) { + TheBucket = InsertIntoBucketImpl(Key, Key, TheBucket); - BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value, - BucketT *TheBucket) { - TheBucket = InsertIntoBucketImpl(Key, TheBucket); - - TheBucket->first = Key; - new (&TheBucket->second) ValueT(Value); + TheBucket->getFirst() = std::forward(Key); + ::new (&TheBucket->getSecond()) ValueT(std::forward(Values)...); return TheBucket; } -#if LLVM_USE_RVALUE_REFERENCES - BucketT *InsertIntoBucket(const KeyT &Key, ValueT &&Value, - BucketT *TheBucket) { - TheBucket = InsertIntoBucketImpl(Key, TheBucket); + template + BucketT *InsertIntoBucketWithLookup(BucketT *TheBucket, KeyT &&Key, + ValueT &&Value, LookupKeyT &Lookup) { + TheBucket = InsertIntoBucketImpl(Key, Lookup, TheBucket); - TheBucket->first = Key; - new (&TheBucket->second) ValueT(std::move(Value)); + TheBucket->getFirst() = std::move(Key); + ::new (&TheBucket->getSecond()) ValueT(std::move(Value)); return TheBucket; } - BucketT *InsertIntoBucket(KeyT &&Key, ValueT &&Value, BucketT *TheBucket) { - TheBucket = InsertIntoBucketImpl(Key, TheBucket); - - TheBucket->first = std::move(Key); - new (&TheBucket->second) ValueT(std::move(Value)); - return TheBucket; - } -#endif - - BucketT *InsertIntoBucketImpl(const KeyT &Key, BucketT *TheBucket) { - // If the load of the hash table is more than 3/4, grow the table. - // If fewer than 1/8 of the buckets are empty (meaning that many are - // filled with tombstones), rehash the table without growing. + template + BucketT *InsertIntoBucketImpl(const KeyT &Key, const LookupKeyT &Lookup, + BucketT *TheBucket) { + // If the load of the hash table is more than 3/4, or if fewer than 1/8 of + // the buckets are empty (meaning that many are filled with tombstones), + // grow the table. // // The later case is tricky. For example, if we had one empty bucket with // tons of tombstones, failing lookups (e.g. for insertion) would have to @@ -450,43 +590,53 @@ private: // causing infinite loops in lookup. unsigned NewNumEntries = getNumEntries() + 1; unsigned NumBuckets = getNumBuckets(); - if (NewNumEntries*4 >= NumBuckets*3) { + if (LLVM_UNLIKELY(NewNumEntries * 4 >= NumBuckets * 3)) { this->grow(NumBuckets * 2); - LookupBucketFor(Key, TheBucket); + LookupBucketFor(Lookup, TheBucket); NumBuckets = getNumBuckets(); - } - if (NumBuckets-(NewNumEntries+getNumTombstones()) <= NumBuckets/8) { + } else if (LLVM_UNLIKELY(NumBuckets-(NewNumEntries+getNumTombstones()) <= + NumBuckets/8)) { this->grow(NumBuckets); - LookupBucketFor(Key, TheBucket); + LookupBucketFor(Lookup, TheBucket); } - assert(TheBucket); + ASSERT(TheBucket); // Only update the state after we've grown our bucket space appropriately // so that when growing buckets we have self-consistent entry count. // If we are writing over a tombstone or zero value, remember this. - if (KeyInfoT::isEqual(TheBucket->first, getEmptyKey())) { + if (KeyInfoT::isEqual(TheBucket->getFirst(), getEmptyKey())) { // Replacing an empty bucket. - incrementNumEntries(); - } - else if (KeyInfoT::isEqual(TheBucket->first, getTombstoneKey())) { + incrementNumEntries(); + } else if (KeyInfoT::isEqual(TheBucket->getFirst(), getTombstoneKey())) { // Replacing a tombstone. incrementNumEntries(); decrementNumTombstones(); - } - else if (ZeroValuesArePurgeable && TheBucket->second == 0) { - // Purging a zero. No accounting changes. - TheBucket->second.~ValueT(); } else { - // Updating an existing entry. No accounting changes. + // we should be purging a zero. No accounting changes. + ASSERT(ValueInfoT::isPurgeable(TheBucket->getSecond())); + TheBucket->getSecond().~ValueT(); } return TheBucket; } + __attribute__((noinline, noreturn, cold)) + void FatalCorruptHashTables(const BucketT *BucketsPtr, unsigned NumBuckets) const + { + _objc_fatal("Hash table corrupted. This is probably a memory error " + "somewhere. (table at %p, buckets at %p (%zu bytes), " + "%u buckets, %u entries, %u tombstones, " + "data %p %p %p %p)", + this, BucketsPtr, malloc_size(BucketsPtr), + NumBuckets, getNumEntries(), getNumTombstones(), + ((void**)BucketsPtr)[0], ((void**)BucketsPtr)[1], + ((void**)BucketsPtr)[2], ((void**)BucketsPtr)[3]); + } + /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in /// FoundBucket. If the bucket contains the key and a value, this returns - /// true, otherwise it returns a bucket with an empty marker or tombstone - /// or zero value and returns false. + /// true, otherwise it returns a bucket with an empty marker or tombstone and + /// returns false. template bool LookupBucketFor(const LookupKeyT &Val, const BucketT *&FoundBucket) const { @@ -494,12 +644,12 @@ private: const unsigned NumBuckets = getNumBuckets(); if (NumBuckets == 0) { - FoundBucket = 0; + FoundBucket = nullptr; return false; } - // FoundTombstone - Keep track of whether we find a tombstone or zero value while probing. - const BucketT *FoundTombstone = 0; + // FoundTombstone - Keep track of whether we find a tombstone while probing. + const BucketT *FoundTombstone = nullptr; const KeyT EmptyKey = getEmptyKey(); const KeyT TombstoneKey = getTombstoneKey(); assert(!KeyInfoT::isEqual(Val, EmptyKey) && @@ -508,20 +658,19 @@ private: unsigned BucketNo = getHashValue(Val) & (NumBuckets-1); unsigned ProbeAmt = 1; - while (1) { + while (true) { const BucketT *ThisBucket = BucketsPtr + BucketNo; // Found Val's bucket? If so, return it. - if (KeyInfoT::isEqual(Val, ThisBucket->first)) { + if (LLVM_LIKELY(KeyInfoT::isEqual(Val, ThisBucket->getFirst()))) { FoundBucket = ThisBucket; return true; } // If we found an empty bucket, the key doesn't exist in the set. // Insert it and return the default value. - if (KeyInfoT::isEqual(ThisBucket->first, EmptyKey)) { + if (LLVM_LIKELY(KeyInfoT::isEqual(ThisBucket->getFirst(), EmptyKey))) { // If we've already seen a tombstone while probing, fill it in instead // of the empty bucket we eventually probed to. - if (FoundTombstone) ThisBucket = FoundTombstone; FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket; return false; } @@ -529,27 +678,19 @@ private: // If this is a tombstone, remember it. If Val ends up not in the map, we // prefer to return it than something that would require more probing. // Ditto for zero values. - if (KeyInfoT::isEqual(ThisBucket->first, TombstoneKey) && !FoundTombstone) + if (KeyInfoT::isEqual(ThisBucket->getFirst(), TombstoneKey) && + !FoundTombstone) FoundTombstone = ThisBucket; // Remember the first tombstone found. - if (ZeroValuesArePurgeable && - ThisBucket->second == 0 && !FoundTombstone) + if (ValueInfoT::isPurgeable(ThisBucket->getSecond()) && !FoundTombstone) FoundTombstone = ThisBucket; // Otherwise, it's a hash collision or a tombstone, continue quadratic // probing. if (ProbeAmt > NumBuckets) { - // No empty buckets in table. Die. - _objc_fatal("Hash table corrupted. This is probably a memory error " - "somewhere. (table at %p, buckets at %p (%zu bytes), " - "%u buckets, %u entries, %u tombstones, " - "data %p %p %p %p)", - this, BucketsPtr, malloc_size(BucketsPtr), - NumBuckets, getNumEntries(), getNumTombstones(), - ((void**)BucketsPtr)[0], ((void**)BucketsPtr)[1], - ((void**)BucketsPtr)[2], ((void**)BucketsPtr)[3]); + FatalCorruptHashTables(BucketsPtr, NumBuckets); } BucketNo += ProbeAmt++; - BucketNo&= (NumBuckets-1); + BucketNo &= (NumBuckets-1); } } @@ -572,17 +713,51 @@ public: } }; -template > -class DenseMap - : public DenseMapBase, - KeyT, ValueT, KeyInfoT, ZeroValuesArePurgeable> { +/// Equality comparison for DenseMap. +/// +/// Iterates over elements of LHS confirming that each (key, value) pair in LHS +/// is also in RHS, and that no additional pairs are in RHS. +/// Equivalent to N calls to RHS.find and N value comparisons. Amortized +/// complexity is linear, worst case is O(N^2) (if every hash collides). +template +bool operator==( + const DenseMapBase &LHS, + const DenseMapBase &RHS) { + if (LHS.size() != RHS.size()) + return false; + + for (auto &KV : LHS) { + auto I = RHS.find(KV.first); + if (I == RHS.end() || I->second != KV.second) + return false; + } + + return true; +} + +/// Inequality comparison for DenseMap. +/// +/// Equivalent to !(LHS == RHS). See operator== for performance notes. +template +bool operator!=( + const DenseMapBase &LHS, + const DenseMapBase &RHS) { + return !(LHS == RHS); +} + +template , + typename KeyInfoT = DenseMapInfo, + typename BucketT = detail::DenseMapPair> +class DenseMap : public DenseMapBase, + KeyT, ValueT, ValueInfoT, KeyInfoT, BucketT> { + friend class DenseMapBase; + // Lift some types from the dependent base class into this class for // simplicity of referring to them. - typedef DenseMapBase BaseT; - typedef typename BaseT::BucketT BucketT; - friend class DenseMapBase; + using BaseT = DenseMapBase; BucketT *Buckets; unsigned NumEntries; @@ -590,28 +765,31 @@ class DenseMap unsigned NumBuckets; public: - explicit DenseMap(unsigned NumInitBuckets = 0) { - init(NumInitBuckets); - } + /// Create a DenseMap wth an optional \p InitialReserve that guarantee that + /// this number of elements can be inserted in the map without grow() + explicit DenseMap(unsigned InitialReserve = 0) { init(InitialReserve); } - DenseMap(const DenseMap &other) { + DenseMap(const DenseMap &other) : BaseT() { init(0); copyFrom(other); } -#if LLVM_USE_RVALUE_REFERENCES - DenseMap(DenseMap &&other) { + DenseMap(DenseMap &&other) : BaseT() { init(0); swap(other); } -#endif template DenseMap(const InputIt &I, const InputIt &E) { - init(NextPowerOf2(std::distance(I, E))); + init(std::distance(I, E)); this->insert(I, E); } + DenseMap(std::initializer_list Vals) { + init(Vals.size()); + this->insert(Vals.begin(), Vals.end()); + } + ~DenseMap() { this->destroyAll(); operator delete(Buckets); @@ -625,11 +803,11 @@ public: } DenseMap& operator=(const DenseMap& other) { - copyFrom(other); + if (&other != this) + copyFrom(other); return *this; } -#if LLVM_USE_RVALUE_REFERENCES DenseMap& operator=(DenseMap &&other) { this->destroyAll(); operator delete(Buckets); @@ -637,7 +815,6 @@ public: swap(other); return *this; } -#endif void copyFrom(const DenseMap& other) { this->destroyAll(); @@ -650,7 +827,8 @@ public: } } - void init(unsigned InitBuckets) { + void init(unsigned InitNumEntries) { + auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries); if (allocateBuckets(InitBuckets)) { this->BaseT::initEmpty(); } else { @@ -663,8 +841,8 @@ public: unsigned OldNumBuckets = NumBuckets; BucketT *OldBuckets = Buckets; - allocateBuckets(std::max(MIN_BUCKETS, NextPowerOf2(AtLeast))); - assert(Buckets); + allocateBuckets(std::max(MIN_BUCKETS, static_cast(NextPowerOf2(AtLeast-1)))); + ASSERT(Buckets); if (!OldBuckets) { this->BaseT::initEmpty(); return; @@ -697,6 +875,7 @@ private: unsigned getNumEntries() const { return NumEntries; } + void setNumEntries(unsigned Num) { NumEntries = Num; } @@ -704,6 +883,7 @@ private: unsigned getNumTombstones() const { return NumTombstones; } + void setNumTombstones(unsigned Num) { NumTombstones = Num; } @@ -719,27 +899,31 @@ private: bool allocateBuckets(unsigned Num) { NumBuckets = Num; if (NumBuckets == 0) { - Buckets = 0; + Buckets = nullptr; return false; } - Buckets = static_cast(operator new(sizeof(BucketT)*NumBuckets)); + Buckets = static_cast(operator new(sizeof(BucketT) * NumBuckets)); return true; } }; -template > +template , + typename KeyInfoT = DenseMapInfo, + typename BucketT = detail::DenseMapPair> class SmallDenseMap - : public DenseMapBase, - KeyT, ValueT, KeyInfoT, ZeroValuesArePurgeable> { + : public DenseMapBase< + SmallDenseMap, KeyT, + ValueT, ValueInfoT, KeyInfoT, BucketT> { + friend class DenseMapBase; + // Lift some types from the dependent base class into this class for // simplicity of referring to them. - typedef DenseMapBase BaseT; - typedef typename BaseT::BucketT BucketT; - friend class DenseMapBase; + using BaseT = DenseMapBase; + + static_assert(powerof2(InlineBuckets), + "InlineBuckets must be a power of 2."); unsigned Small : 1; unsigned NumEntries : 31; @@ -759,17 +943,15 @@ public: init(NumInitBuckets); } - SmallDenseMap(const SmallDenseMap &other) { + SmallDenseMap(const SmallDenseMap &other) : BaseT() { init(0); copyFrom(other); } -#if LLVM_USE_RVALUE_REFERENCES - SmallDenseMap(SmallDenseMap &&other) { + SmallDenseMap(SmallDenseMap &&other) : BaseT() { init(0); swap(other); } -#endif template SmallDenseMap(const InputIt &I, const InputIt &E) { @@ -798,23 +980,23 @@ public: for (unsigned i = 0, e = InlineBuckets; i != e; ++i) { BucketT *LHSB = &getInlineBuckets()[i], *RHSB = &RHS.getInlineBuckets()[i]; - bool hasLHSValue = (!KeyInfoT::isEqual(LHSB->first, EmptyKey) && - !KeyInfoT::isEqual(LHSB->first, TombstoneKey)); - bool hasRHSValue = (!KeyInfoT::isEqual(RHSB->first, EmptyKey) && - !KeyInfoT::isEqual(RHSB->first, TombstoneKey)); + bool hasLHSValue = (!KeyInfoT::isEqual(LHSB->getFirst(), EmptyKey) && + !KeyInfoT::isEqual(LHSB->getFirst(), TombstoneKey)); + bool hasRHSValue = (!KeyInfoT::isEqual(RHSB->getFirst(), EmptyKey) && + !KeyInfoT::isEqual(RHSB->getFirst(), TombstoneKey)); if (hasLHSValue && hasRHSValue) { // Swap together if we can... std::swap(*LHSB, *RHSB); continue; } // Swap separately and handle any assymetry. - std::swap(LHSB->first, RHSB->first); + std::swap(LHSB->getFirst(), RHSB->getFirst()); if (hasLHSValue) { - new (&RHSB->second) ValueT(llvm_move(LHSB->second)); - LHSB->second.~ValueT(); + ::new (&RHSB->getSecond()) ValueT(std::move(LHSB->getSecond())); + LHSB->getSecond().~ValueT(); } else if (hasRHSValue) { - new (&LHSB->second) ValueT(llvm_move(RHSB->second)); - RHSB->second.~ValueT(); + ::new (&LHSB->getSecond()) ValueT(std::move(RHSB->getSecond())); + RHSB->getSecond().~ValueT(); } } return; @@ -829,7 +1011,7 @@ public: SmallDenseMap &LargeSide = Small ? RHS : *this; // First stash the large side's rep and move the small side across. - LargeRep TmpRep = llvm_move(*LargeSide.getLargeRep()); + LargeRep TmpRep = std::move(*LargeSide.getLargeRep()); LargeSide.getLargeRep()->~LargeRep(); LargeSide.Small = true; // This is similar to the standard move-from-old-buckets, but the bucket @@ -839,27 +1021,27 @@ public: for (unsigned i = 0, e = InlineBuckets; i != e; ++i) { BucketT *NewB = &LargeSide.getInlineBuckets()[i], *OldB = &SmallSide.getInlineBuckets()[i]; - new (&NewB->first) KeyT(llvm_move(OldB->first)); - OldB->first.~KeyT(); - if (!KeyInfoT::isEqual(NewB->first, EmptyKey) && - !KeyInfoT::isEqual(NewB->first, TombstoneKey)) { - new (&NewB->second) ValueT(llvm_move(OldB->second)); - OldB->second.~ValueT(); + ::new (&NewB->getFirst()) KeyT(std::move(OldB->getFirst())); + OldB->getFirst().~KeyT(); + if (!KeyInfoT::isEqual(NewB->getFirst(), EmptyKey) && + !KeyInfoT::isEqual(NewB->getFirst(), TombstoneKey)) { + ::new (&NewB->getSecond()) ValueT(std::move(OldB->getSecond())); + OldB->getSecond().~ValueT(); } } // The hard part of moving the small buckets across is done, just move // the TmpRep into its new home. SmallSide.Small = false; - new (SmallSide.getLargeRep()) LargeRep(llvm_move(TmpRep)); + new (SmallSide.getLargeRep()) LargeRep(std::move(TmpRep)); } SmallDenseMap& operator=(const SmallDenseMap& other) { - copyFrom(other); + if (&other != this) + copyFrom(other); return *this; } -#if LLVM_USE_RVALUE_REFERENCES SmallDenseMap& operator=(SmallDenseMap &&other) { this->destroyAll(); deallocateBuckets(); @@ -867,7 +1049,6 @@ public: swap(other); return *this; } -#endif void copyFrom(const SmallDenseMap& other) { this->destroyAll(); @@ -875,7 +1056,7 @@ public: Small = true; if (other.getNumBuckets() > InlineBuckets) { Small = false; - allocateBuckets(other.getNumBuckets()); + new (getLargeRep()) LargeRep(allocateBuckets(other.getNumBuckets())); } this->BaseT::copyFrom(other); } @@ -890,11 +1071,11 @@ public: } void grow(unsigned AtLeast) { - if (AtLeast > InlineBuckets) + if (AtLeast >= InlineBuckets) AtLeast = std::max(MIN_BUCKETS, NextPowerOf2(AtLeast)); if (Small) { - if (AtLeast <= InlineBuckets) + if (AtLeast < InlineBuckets) return; // Nothing to do. // First move the inline buckets into a temporary storage. @@ -907,16 +1088,16 @@ public: const KeyT EmptyKey = this->getEmptyKey(); const KeyT TombstoneKey = this->getTombstoneKey(); for (BucketT *P = getBuckets(), *E = P + InlineBuckets; P != E; ++P) { - if (!KeyInfoT::isEqual(P->first, EmptyKey) && - !KeyInfoT::isEqual(P->first, TombstoneKey)) { + if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) && + !KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) { assert(size_t(TmpEnd - TmpBegin) < InlineBuckets && "Too many inline buckets!"); - new (&TmpEnd->first) KeyT(llvm_move(P->first)); - new (&TmpEnd->second) ValueT(llvm_move(P->second)); + ::new (&TmpEnd->getFirst()) KeyT(std::move(P->getFirst())); + ::new (&TmpEnd->getSecond()) ValueT(std::move(P->getSecond())); ++TmpEnd; - P->second.~ValueT(); + P->getSecond().~ValueT(); } - P->first.~KeyT(); + P->getFirst().~KeyT(); } // Now make this map use the large rep, and move all the entries back @@ -927,7 +1108,7 @@ public: return; } - LargeRep OldRep = llvm_move(*getLargeRep()); + LargeRep OldRep = std::move(*getLargeRep()); getLargeRep()->~LargeRep(); if (AtLeast <= InlineBuckets) { Small = true; @@ -966,34 +1147,40 @@ private: unsigned getNumEntries() const { return NumEntries; } + void setNumEntries(unsigned Num) { - assert(Num < INT_MAX && "Cannot support more than INT_MAX entries"); + // NumEntries is hardcoded to be 31 bits wide. + ASSERT(Num < (1U << 31) && "Cannot support more than 1<<31 entries"); NumEntries = Num; } unsigned getNumTombstones() const { return NumTombstones; } + void setNumTombstones(unsigned Num) { NumTombstones = Num; } const BucketT *getInlineBuckets() const { - assert(Small); + ASSERT(Small); // Note that this cast does not violate aliasing rules as we assert that // the memory's dynamic type is the small, inline bucket buffer, and the // 'storage.buffer' static type is 'char *'. return reinterpret_cast(storage.buffer); } + BucketT *getInlineBuckets() { return const_cast( const_cast(this)->getInlineBuckets()); } + const LargeRep *getLargeRep() const { - assert(!Small); + ASSERT(!Small); // Note, same rule about aliasing as with getInlineBuckets. return reinterpret_cast(storage.buffer); } + LargeRep *getLargeRep() { return const_cast( const_cast(this)->getLargeRep()); @@ -1002,10 +1189,12 @@ private: const BucketT *getBuckets() const { return Small ? getInlineBuckets() : getLargeRep()->Buckets; } + BucketT *getBuckets() { return const_cast( const_cast(this)->getBuckets()); } + unsigned getNumBuckets() const { return Small ? InlineBuckets : getLargeRep()->NumBuckets; } @@ -1019,43 +1208,52 @@ private: } LargeRep allocateBuckets(unsigned Num) { - assert(Num > InlineBuckets && "Must allocate more buckets than are inline"); + ASSERT(Num > InlineBuckets && "Must allocate more buckets than are inline"); LargeRep Rep = { static_cast(operator new(sizeof(BucketT) * Num)), Num -}; + }; return Rep; } }; -template +template class DenseMapIterator { - typedef std::pair Bucket; - typedef DenseMapIterator ConstIterator; - friend class DenseMapIterator; -public: - typedef ptrdiff_t difference_type; - typedef typename conditional::type value_type; - typedef value_type *pointer; - typedef value_type &reference; - typedef std::forward_iterator_tag iterator_category; -private: - pointer Ptr, End; -public: - DenseMapIterator() : Ptr(0), End(0) {} + friend class DenseMapIterator; + friend class DenseMapIterator; - DenseMapIterator(pointer Pos, pointer E, bool NoAdvance = false) - : Ptr(Pos), End(E) { - if (!NoAdvance) AdvancePastEmptyBuckets(); + using ConstIterator = DenseMapIterator; + +public: + using difference_type = ptrdiff_t; + using value_type = + typename std::conditional::type; + using pointer = value_type *; + using reference = value_type &; + using iterator_category = std::forward_iterator_tag; + +private: + pointer Ptr = nullptr; + pointer End = nullptr; + +public: + DenseMapIterator() = default; + + DenseMapIterator(pointer Pos, pointer E, + bool NoAdvance = false) + : Ptr(Pos), End(E) { + if (NoAdvance) return; + AdvancePastEmptyBuckets(); } - // If IsConst is true this is a converting constructor from iterator to - // const_iterator and the default copy constructor is used. - // Otherwise this is a copy constructor for iterator. - DenseMapIterator(const DenseMapIterator& I) - : Ptr(I.Ptr), End(I.End) {} + // Converting ctor from non-const iterators to const iterators. SFINAE'd out + // for const iterator destinations so it doesn't end up as a user defined copy + // constructor. + template ::type> + DenseMapIterator( + const DenseMapIterator &I) + : Ptr(I.Ptr), End(I.End) {} reference operator*() const { return *Ptr; @@ -1065,10 +1263,10 @@ public: } bool operator==(const ConstIterator &RHS) const { - return Ptr == RHS.operator->(); + return Ptr == RHS.Ptr; } bool operator!=(const ConstIterator &RHS) const { - return Ptr != RHS.operator->(); + return Ptr != RHS.Ptr; } inline DenseMapIterator& operator++() { // Preincrement @@ -1082,16 +1280,31 @@ public: private: void AdvancePastEmptyBuckets() { + ASSERT(Ptr <= End); const KeyT Empty = KeyInfoT::getEmptyKey(); const KeyT Tombstone = KeyInfoT::getTombstoneKey(); - while (Ptr != End && - (KeyInfoT::isEqual(Ptr->first, Empty) || - KeyInfoT::isEqual(Ptr->first, Tombstone))) + while (Ptr != End && (KeyInfoT::isEqual(Ptr->getFirst(), Empty) || + KeyInfoT::isEqual(Ptr->getFirst(), Tombstone))) ++Ptr; } + + void RetreatPastEmptyBuckets() { + ASSERT(Ptr >= End); + const KeyT Empty = KeyInfoT::getEmptyKey(); + const KeyT Tombstone = KeyInfoT::getTombstoneKey(); + + while (Ptr != End && (KeyInfoT::isEqual(Ptr[-1].getFirst(), Empty) || + KeyInfoT::isEqual(Ptr[-1].getFirst(), Tombstone))) + --Ptr; + } }; +template +inline size_t capacity_in_bytes(const DenseMap &X) { + return X.getMemorySize(); +} + } // end namespace objc -#endif +#endif // LLVM_ADT_DENSEMAP_H diff --git a/runtime/llvm-DenseMapInfo.h b/runtime/llvm-DenseMapInfo.h index 4b7869f..a82c701 100644 --- a/runtime/llvm-DenseMapInfo.h +++ b/runtime/llvm-DenseMapInfo.h @@ -75,6 +75,15 @@ template<> struct DenseMapInfo { return _objc_strhash(Val); } static bool isEqual(const char* const &LHS, const char* const &RHS) { + if (LHS == RHS) { + return true; + } + if (LHS == getEmptyKey() || RHS == getEmptyKey()) { + return false; + } + if (LHS == getTombstoneKey() || RHS == getTombstoneKey()) { + return false; + } return 0 == strcmp(LHS, RHS); } }; @@ -195,6 +204,13 @@ struct DenseMapInfo > { } }; +template +struct DenseMapValueInfo { + static inline bool isPurgeable(const T &value) { + return false; + } +}; + } // end namespace objc #endif diff --git a/runtime/llvm-DenseSet.h b/runtime/llvm-DenseSet.h new file mode 100644 index 0000000..45ea107 --- /dev/null +++ b/runtime/llvm-DenseSet.h @@ -0,0 +1,293 @@ +//===- llvm/ADT/DenseSet.h - Dense probed hash table ------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the DenseSet and SmallDenseSet classes. +// +//===----------------------------------------------------------------------===// + +// Taken from clang-1100.247.11.10.9 + +#ifndef LLVM_ADT_DENSESET_H +#define LLVM_ADT_DENSESET_H + +#include "llvm-DenseMap.h" +#include "llvm-DenseMapInfo.h" +#include "llvm-type_traits.h" +#include +#include +#include +#include +#include +#include + +#include "objc-private.h" + +namespace objc { + +namespace detail { + +struct DenseSetEmpty {}; + +// Use the empty base class trick so we can create a DenseMap where the buckets +// contain only a single item. +template class DenseSetPair : public DenseSetEmpty { + KeyT key; + +public: + KeyT &getFirst() { return key; } + const KeyT &getFirst() const { return key; } + DenseSetEmpty &getSecond() { return *this; } + const DenseSetEmpty &getSecond() const { return *this; } +}; + +/// Base class for DenseSet and DenseSmallSet. +/// +/// MapTy should be either +/// +/// DenseMap, +/// ValueInfoT, detail::DenseSetPair> +/// +/// or the equivalent SmallDenseMap type. ValueInfoT must implement the +/// DenseMapInfo "concept". +template +class DenseSetImpl { + static_assert(sizeof(typename MapTy::value_type) == sizeof(ValueT), + "DenseMap buckets unexpectedly large!"); + MapTy TheMap; + + template + using const_arg_type_t = typename const_pointer_or_const_ref::type; + +public: + using key_type = ValueT; + using value_type = ValueT; + using size_type = unsigned; + + explicit DenseSetImpl(unsigned InitialReserve = 0) : TheMap(InitialReserve) {} + + DenseSetImpl(std::initializer_list Elems) + : DenseSetImpl(PowerOf2Ceil(Elems.size())) { + insert(Elems.begin(), Elems.end()); + } + + bool empty() const { return TheMap.empty(); } + size_type size() const { return TheMap.size(); } + size_t getMemorySize() const { return TheMap.getMemorySize(); } + + /// Grow the DenseSet so that it has at least Size buckets. Will not shrink + /// the Size of the set. + void resize(size_t Size) { TheMap.resize(Size); } + + /// Grow the DenseSet so that it can contain at least \p NumEntries items + /// before resizing again. + void reserve(size_t Size) { TheMap.reserve(Size); } + + void clear() { + TheMap.clear(); + } + + /// Return 1 if the specified key is in the set, 0 otherwise. + size_type count(const_arg_type_t V) const { + return TheMap.count(V); + } + + bool erase(const ValueT &V) { + return TheMap.erase(V); + } + + void swap(DenseSetImpl &RHS) { TheMap.swap(RHS.TheMap); } + + // Iterators. + + class ConstIterator; + + class Iterator { + typename MapTy::iterator I; + friend class DenseSetImpl; + friend class ConstIterator; + + public: + using difference_type = typename MapTy::iterator::difference_type; + using value_type = ValueT; + using pointer = value_type *; + using reference = value_type &; + using iterator_category = std::forward_iterator_tag; + + Iterator() = default; + Iterator(const typename MapTy::iterator &i) : I(i) {} + + ValueT &operator*() { return I->getFirst(); } + const ValueT &operator*() const { return I->getFirst(); } + ValueT *operator->() { return &I->getFirst(); } + const ValueT *operator->() const { return &I->getFirst(); } + + Iterator& operator++() { ++I; return *this; } + Iterator operator++(int) { auto T = *this; ++I; return T; } + bool operator==(const ConstIterator& X) const { return I == X.I; } + bool operator!=(const ConstIterator& X) const { return I != X.I; } + }; + + class ConstIterator { + typename MapTy::const_iterator I; + friend class DenseSet; + friend class Iterator; + + public: + using difference_type = typename MapTy::const_iterator::difference_type; + using value_type = ValueT; + using pointer = const value_type *; + using reference = const value_type &; + using iterator_category = std::forward_iterator_tag; + + ConstIterator() = default; + ConstIterator(const Iterator &B) : I(B.I) {} + ConstIterator(const typename MapTy::const_iterator &i) : I(i) {} + + const ValueT &operator*() const { return I->getFirst(); } + const ValueT *operator->() const { return &I->getFirst(); } + + ConstIterator& operator++() { ++I; return *this; } + ConstIterator operator++(int) { auto T = *this; ++I; return T; } + bool operator==(const ConstIterator& X) const { return I == X.I; } + bool operator!=(const ConstIterator& X) const { return I != X.I; } + }; + + using iterator = Iterator; + using const_iterator = ConstIterator; + + iterator begin() { return Iterator(TheMap.begin()); } + iterator end() { return Iterator(TheMap.end()); } + + const_iterator begin() const { return ConstIterator(TheMap.begin()); } + const_iterator end() const { return ConstIterator(TheMap.end()); } + + iterator find(const_arg_type_t V) { return Iterator(TheMap.find(V)); } + const_iterator find(const_arg_type_t V) const { + return ConstIterator(TheMap.find(V)); + } + + /// Alternative version of find() which allows a different, and possibly less + /// expensive, key type. + /// The DenseMapInfo is responsible for supplying methods + /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key type + /// used. + template + iterator find_as(const LookupKeyT &Val) { + return Iterator(TheMap.find_as(Val)); + } + template + const_iterator find_as(const LookupKeyT &Val) const { + return ConstIterator(TheMap.find_as(Val)); + } + + void erase(Iterator I) { return TheMap.erase(I.I); } + void erase(ConstIterator CI) { return TheMap.erase(CI.I); } + + std::pair insert(const ValueT &V) { + detail::DenseSetEmpty Empty; + return TheMap.try_emplace(V, Empty); + } + + std::pair insert(ValueT &&V) { + detail::DenseSetEmpty Empty; + return TheMap.try_emplace(std::move(V), Empty); + } + + /// Alternative version of insert that uses a different (and possibly less + /// expensive) key type. + template + std::pair insert_as(const ValueT &V, + const LookupKeyT &LookupKey) { + return TheMap.insert_as({V, detail::DenseSetEmpty()}, LookupKey); + } + template + std::pair insert_as(ValueT &&V, const LookupKeyT &LookupKey) { + return TheMap.insert_as({std::move(V), detail::DenseSetEmpty()}, LookupKey); + } + + // Range insertion of values. + template + void insert(InputIt I, InputIt E) { + for (; I != E; ++I) + insert(*I); + } +}; + +/// Equality comparison for DenseSet. +/// +/// Iterates over elements of LHS confirming that each element is also a member +/// of RHS, and that RHS contains no additional values. +/// Equivalent to N calls to RHS.count. Amortized complexity is linear, worst +/// case is O(N^2) (if every hash collides). +template +bool operator==(const DenseSetImpl &LHS, + const DenseSetImpl &RHS) { + if (LHS.size() != RHS.size()) + return false; + + for (auto &E : LHS) + if (!RHS.count(E)) + return false; + + return true; +} + +/// Inequality comparison for DenseSet. +/// +/// Equivalent to !(LHS == RHS). See operator== for performance notes. +template +bool operator!=(const DenseSetImpl &LHS, + const DenseSetImpl &RHS) { + return !(LHS == RHS); +} + +} // end namespace detail + +/// Implements a dense probed hash-table based set. +template > +class DenseSet : public detail::DenseSetImpl< + ValueT, DenseMap, + ValueInfoT, detail::DenseSetPair>, + ValueInfoT> { + using BaseT = + detail::DenseSetImpl, + ValueInfoT, detail::DenseSetPair>, + ValueInfoT>; + +public: + using BaseT::BaseT; +}; + +/// Implements a dense probed hash-table based set with some number of buckets +/// stored inline. +template > +class SmallDenseSet + : public detail::DenseSetImpl< + ValueT, SmallDenseMap, + ValueInfoT, detail::DenseSetPair>, + ValueInfoT> { + using BaseT = detail::DenseSetImpl< + ValueT, SmallDenseMap, + ValueInfoT, detail::DenseSetPair>, + ValueInfoT>; + +public: + using BaseT::BaseT; +}; + +} // end namespace objc + +#endif // LLVM_ADT_DENSESET_H diff --git a/runtime/llvm-type_traits.h b/runtime/llvm-type_traits.h index bbe38d4..abcce7e 100644 --- a/runtime/llvm-type_traits.h +++ b/runtime/llvm-type_traits.h @@ -1,24 +1,22 @@ //===- llvm/Support/type_traits.h - Simplfied type traits -------*- C++ -*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // -// This file provides a template class that determines if a type is a class or -// not. The basic mechanism, based on using the pointer to member function of -// a zero argument to a function was "boosted" from the boost type_traits -// library. See http://www.boost.org/ for all the gory details. +// This file provides useful additions to the standard type_traits library. // //===----------------------------------------------------------------------===// -// Taken from llvmCore-3425.0.31. +// Taken from clang-1100.247.11.10.9 #ifndef LLVM_SUPPORT_TYPE_TRAITS_H #define LLVM_SUPPORT_TYPE_TRAITS_H +#define HAVE_STD_IS_TRIVIALLY_COPYABLE 1 + #include #include @@ -27,195 +25,180 @@ #define __has_feature(x) 0 #endif -// This is actually the conforming implementation which works with abstract -// classes. However, enough compilers have trouble with it that most will use -// the one in boost/type_traits/object_traits.hpp. This implementation actually -// works with VC7.0, but other interactions seem to fail when we use it. - namespace objc { - -namespace dont_use -{ - // These two functions should never be used. They are helpers to - // the is_class template below. They cannot be located inside - // is_class because doing so causes at least GCC to think that - // the value of the "value" enumerator is not constant. Placing - // them out here (for some strange reason) allows the sizeof - // operator against them to magically be constant. This is - // important to make the is_class::value idiom zero cost. it - // evaluates to a constant 1 or 0 depending on whether the - // parameter T is a class or not (respectively). - template char is_class_helper(void(T::*)()); - template double is_class_helper(...); -} -template -struct is_class -{ - // is_class<> metafunction due to Paul Mensonides (leavings@attbi.com). For - // more details: - // http://groups.google.com/groups?hl=en&selm=000001c1cc83%24e154d5e0%247772e50c%40c161550a&rnum=1 + +/// Metafunction that determines whether the given type is either an +/// integral type or an enumeration type, including enum classes. +/// +/// Note that this accepts potentially more integral types than is_integral +/// because it is based on being implicitly convertible to an integral type. +/// Also note that enum classes aren't implicitly convertible to integral types, +/// the value may therefore need to be explicitly converted before being used. +template class is_integral_or_enum { + using UnderlyingT = typename std::remove_reference::type; + public: static const bool value = - sizeof(char) == sizeof(dont_use::is_class_helper(0)); + !std::is_class::value && // Filter conversion operators. + !std::is_pointer::value && + !std::is_floating_point::value && + (std::is_enum::value || + std::is_convertible::value); }; - - -/// isPodLike - This is a type trait that is used to determine whether a given -/// type can be copied around with memcpy instead of running ctors etc. + +/// If T is a pointer, just return it. If it is not, return T&. +template +struct add_lvalue_reference_if_not_pointer { using type = T &; }; + template -struct isPodLike { -#if __has_feature(is_trivially_copyable) - // If the compiler supports the is_trivially_copyable trait use it, as it - // matches the definition of isPodLike closely. - static const bool value = __is_trivially_copyable(T); -#else - // If we don't know anything else, we can (at least) assume that all non-class - // types are PODs. - static const bool value = !is_class::value; +struct add_lvalue_reference_if_not_pointer< + T, typename std::enable_if::value>::type> { + using type = T; +}; + +/// If T is a pointer to X, return a pointer to const X. If it is not, +/// return const T. +template +struct add_const_past_pointer { using type = const T; }; + +template +struct add_const_past_pointer< + T, typename std::enable_if::value>::type> { + using type = const typename std::remove_pointer::type *; +}; + +template +struct const_pointer_or_const_ref { + using type = const T &; +}; +template +struct const_pointer_or_const_ref< + T, typename std::enable_if::value>::type> { + using type = typename add_const_past_pointer::type; +}; + +namespace detail { +/// Internal utility to detect trivial copy construction. +template union copy_construction_triviality_helper { + T t; + copy_construction_triviality_helper() = default; + copy_construction_triviality_helper(const copy_construction_triviality_helper&) = default; + ~copy_construction_triviality_helper() = default; +}; +/// Internal utility to detect trivial move construction. +template union move_construction_triviality_helper { + T t; + move_construction_triviality_helper() = default; + move_construction_triviality_helper(move_construction_triviality_helper&&) = default; + ~move_construction_triviality_helper() = default; +}; + +template +union trivial_helper { + T t; +}; + +} // end namespace detail + +/// An implementation of `std::is_trivially_copy_constructible` since we have +/// users with STLs that don't yet include it. +template +struct is_trivially_copy_constructible + : std::is_copy_constructible< + ::objc::detail::copy_construction_triviality_helper> {}; +template +struct is_trivially_copy_constructible : std::true_type {}; +template +struct is_trivially_copy_constructible : std::false_type {}; + +/// An implementation of `std::is_trivially_move_constructible` since we have +/// users with STLs that don't yet include it. +template +struct is_trivially_move_constructible + : std::is_move_constructible< + ::objc::detail::move_construction_triviality_helper> {}; +template +struct is_trivially_move_constructible : std::true_type {}; +template +struct is_trivially_move_constructible : std::true_type {}; + + +template +struct is_copy_assignable { + template + static auto get(F*) -> decltype(std::declval() = std::declval(), std::true_type{}); + static std::false_type get(...); + static constexpr bool value = decltype(get((T*)nullptr))::value; +}; + +template +struct is_move_assignable { + template + static auto get(F*) -> decltype(std::declval() = std::declval(), std::true_type{}); + static std::false_type get(...); + static constexpr bool value = decltype(get((T*)nullptr))::value; +}; + + +// An implementation of `std::is_trivially_copyable` since STL version +// is not equally supported by all compilers, especially GCC 4.9. +// Uniform implementation of this trait is important for ABI compatibility +// as it has an impact on SmallVector's ABI (among others). +template +class is_trivially_copyable { + + // copy constructors + static constexpr bool has_trivial_copy_constructor = + std::is_copy_constructible>::value; + static constexpr bool has_deleted_copy_constructor = + !std::is_copy_constructible::value; + + // move constructors + static constexpr bool has_trivial_move_constructor = + std::is_move_constructible>::value; + static constexpr bool has_deleted_move_constructor = + !std::is_move_constructible::value; + + // copy assign + static constexpr bool has_trivial_copy_assign = + is_copy_assignable>::value; + static constexpr bool has_deleted_copy_assign = + !is_copy_assignable::value; + + // move assign + static constexpr bool has_trivial_move_assign = + is_move_assignable>::value; + static constexpr bool has_deleted_move_assign = + !is_move_assignable::value; + + // destructor + static constexpr bool has_trivial_destructor = + std::is_destructible>::value; + + public: + + static constexpr bool value = + has_trivial_destructor && + (has_deleted_move_assign || has_trivial_move_assign) && + (has_deleted_move_constructor || has_trivial_move_constructor) && + (has_deleted_copy_assign || has_trivial_copy_assign) && + (has_deleted_copy_constructor || has_trivial_copy_constructor); + +#ifdef HAVE_STD_IS_TRIVIALLY_COPYABLE + static_assert(value == std::is_trivially_copyable::value, + "inconsistent behavior between llvm:: and std:: implementation of is_trivially_copyable"); #endif }; - -// std::pair's are pod-like if their elements are. -template -struct isPodLike > { - static const bool value = isPodLike::value && isPodLike::value; -}; - - -template -struct integral_constant { - typedef T value_type; - static const value_type value = v; - typedef integral_constant type; - operator value_type() { return value; } -}; - -typedef integral_constant true_type; -typedef integral_constant false_type; - -/// \brief Metafunction that determines whether the two given types are -/// equivalent. -template struct is_same : public false_type {}; -template struct is_same : public true_type {}; - -/// \brief Metafunction that removes const qualification from a type. -template struct remove_const { typedef T type; }; -template struct remove_const { typedef T type; }; - -/// \brief Metafunction that removes volatile qualification from a type. -template struct remove_volatile { typedef T type; }; -template struct remove_volatile { typedef T type; }; - -/// \brief Metafunction that removes both const and volatile qualification from -/// a type. -template struct remove_cv { - typedef typename remove_const::type>::type type; -}; - -/// \brief Helper to implement is_integral metafunction. -template struct is_integral_impl : false_type {}; -template <> struct is_integral_impl< bool> : true_type {}; -template <> struct is_integral_impl< char> : true_type {}; -template <> struct is_integral_impl< signed char> : true_type {}; -template <> struct is_integral_impl : true_type {}; -template <> struct is_integral_impl< wchar_t> : true_type {}; -template <> struct is_integral_impl< short> : true_type {}; -template <> struct is_integral_impl : true_type {}; -template <> struct is_integral_impl< int> : true_type {}; -template <> struct is_integral_impl : true_type {}; -template <> struct is_integral_impl< long> : true_type {}; -template <> struct is_integral_impl : true_type {}; -template <> struct is_integral_impl< long long> : true_type {}; -template <> struct is_integral_impl : true_type {}; - -/// \brief Metafunction that determines whether the given type is an integral -/// type. template -struct is_integral : is_integral_impl {}; - -/// \brief Metafunction to remove reference from a type. -template struct remove_reference { typedef T type; }; -template struct remove_reference { typedef T type; }; - -/// \brief Metafunction that determines whether the given type is a pointer -/// type. -template struct is_pointer : false_type {}; -template struct is_pointer : true_type {}; -template struct is_pointer : true_type {}; -template struct is_pointer : true_type {}; -template struct is_pointer : true_type {}; - -/// \brief Metafunction that determines whether the given type is either an -/// integral type or an enumeration type. -/// -/// Note that this accepts potentially more integral types than we whitelist -/// above for is_integral because it is based on merely being convertible -/// implicitly to an integral type. -template class is_integral_or_enum { - // Provide an overload which can be called with anything implicitly - // convertible to an unsigned long long. This should catch integer types and - // enumeration types at least. We blacklist classes with conversion operators - // below. - static double check_int_convertible(unsigned long long); - static char check_int_convertible(...); - - typedef typename remove_reference::type UnderlyingT; - static UnderlyingT &nonce_instance; - -public: - static const bool - value = (!is_class::value && !is_pointer::value && - !is_same::value && - !is_same::value && - sizeof(char) != sizeof(check_int_convertible(nonce_instance))); +class is_trivially_copyable : public std::true_type { }; -// enable_if_c - Enable/disable a template based on a metafunction -template -struct enable_if_c { - typedef T type; -}; -template struct enable_if_c { }; - -// enable_if - Enable/disable a template based on a metafunction -template -struct enable_if : public enable_if_c { }; - -namespace dont_use { - template char base_of_helper(const volatile Base*); - template double base_of_helper(...); -} - -/// is_base_of - Metafunction to determine whether one type is a base class of -/// (or identical to) another type. -template -struct is_base_of { - static const bool value - = is_class::value && is_class::value && - sizeof(char) == sizeof(dont_use::base_of_helper((Derived*)0)); -}; - -// remove_pointer - Metafunction to turn Foo* into Foo. Defined in -// C++0x [meta.trans.ptr]. -template struct remove_pointer { typedef T type; }; -template struct remove_pointer { typedef T type; }; -template struct remove_pointer { typedef T type; }; -template struct remove_pointer { typedef T type; }; -template struct remove_pointer { - typedef T type; }; - -template -struct conditional { typedef T type; }; - -template -struct conditional { typedef F type; }; - -} +} // end namespace llvm #ifdef LLVM_DEFINED_HAS_FEATURE #undef __has_feature #endif -#endif +#endif // LLVM_SUPPORT_TYPE_TRAITS_H diff --git a/runtime/maptable.h b/runtime/maptable.h index e43da78..e0f2af9 100644 --- a/runtime/maptable.h +++ b/runtime/maptable.h @@ -30,9 +30,8 @@ #define _OBJC_MAPTABLE_H_ #ifndef _OBJC_PRIVATE_H_ -# define OBJC_MAP_AVAILABILITY \ - __OSX_DEPRECATED(10.0, 10.1, "NXMapTable is deprecated") \ - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE +# define OBJC_MAP_AVAILABILITY \ + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.0, 10.1, "NXMapTable is deprecated") #else # define OBJC_MAP_AVAILABILITY #endif @@ -49,16 +48,21 @@ __BEGIN_DECLS typedef struct _NXMapTable { /* private data structure; may change */ - const struct _NXMapTablePrototype *prototype; + const struct _NXMapTablePrototype * _Nonnull prototype; unsigned count; unsigned nbBucketsMinusOne; - void *buckets; + void * _Nullable buckets; } NXMapTable OBJC_MAP_AVAILABILITY; typedef struct _NXMapTablePrototype { - unsigned (*hash)(NXMapTable *, const void *key); - int (*isEqual)(NXMapTable *, const void *key1, const void *key2); - void (*free)(NXMapTable *, void *key, void *value); + unsigned (* _Nonnull hash)(NXMapTable * _Nonnull, + const void * _Nullable key); + int (* _Nonnull isEqual)(NXMapTable * _Nonnull, + const void * _Nullable key1, + const void * _Nullable key2); + void (* _Nonnull free)(NXMapTable * _Nonnull, + void * _Nullable key, + void * _Nullable value); int style; /* reserved for future expansion; currently 0 */ } NXMapTablePrototype OBJC_MAP_AVAILABILITY; @@ -70,36 +74,59 @@ typedef struct _NXMapTablePrototype { C - isEqual(key1, key2) => key1 == key2 */ -#define NX_MAPNOTAKEY ((void *)(-1)) +#define NX_MAPNOTAKEY ((void * _Nonnull)(-1)) /*************** Functions ***************/ -OBJC_EXPORT NXMapTable *NXCreateMapTableFromZone(NXMapTablePrototype prototype, unsigned capacity, void *z) OBJC_MAP_AVAILABILITY; -OBJC_EXPORT NXMapTable *NXCreateMapTable(NXMapTablePrototype prototype, unsigned capacity) OBJC_MAP_AVAILABILITY; +OBJC_EXPORT NXMapTable * _Nonnull +NXCreateMapTableFromZone(NXMapTablePrototype prototype, + unsigned capacity, void * _Nullable z) + OBJC_MAP_AVAILABILITY; + +OBJC_EXPORT NXMapTable * _Nonnull +NXCreateMapTable(NXMapTablePrototype prototype, unsigned capacity) + OBJC_MAP_AVAILABILITY; /* capacity is only a hint; 0 creates a small table */ -OBJC_EXPORT void NXFreeMapTable(NXMapTable *table) OBJC_MAP_AVAILABILITY; +OBJC_EXPORT void +NXFreeMapTable(NXMapTable * _Nonnull table) + OBJC_MAP_AVAILABILITY; /* call free for each pair, and recovers table */ -OBJC_EXPORT void NXResetMapTable(NXMapTable *table) OBJC_MAP_AVAILABILITY; +OBJC_EXPORT void +NXResetMapTable(NXMapTable * _Nonnull table) + OBJC_MAP_AVAILABILITY; /* free each pair; keep current capacity */ -OBJC_EXPORT BOOL NXCompareMapTables(NXMapTable *table1, NXMapTable *table2) OBJC_MAP_AVAILABILITY; +OBJC_EXPORT BOOL +NXCompareMapTables(NXMapTable * _Nonnull table1, NXMapTable * _Nonnull table2) + OBJC_MAP_AVAILABILITY; /* Returns YES if the two sets are equal (each member of table1 in table2, and table have same size) */ -OBJC_EXPORT unsigned NXCountMapTable(NXMapTable *table) OBJC_MAP_AVAILABILITY; +OBJC_EXPORT unsigned +NXCountMapTable(NXMapTable * _Nonnull table) + OBJC_MAP_AVAILABILITY; /* current number of data in table */ -OBJC_EXPORT void *NXMapMember(NXMapTable *table, const void *key, void **value) OBJC_MAP_AVAILABILITY; +OBJC_EXPORT void * _Nullable +NXMapMember(NXMapTable * _Nonnull table, const void * _Nullable key, + void * _Nullable * _Nonnull value) OBJC_MAP_AVAILABILITY; /* return original table key or NX_MAPNOTAKEY. If key is found, value is set */ -OBJC_EXPORT void *NXMapGet(NXMapTable *table, const void *key) OBJC_MAP_AVAILABILITY; +OBJC_EXPORT void * _Nullable +NXMapGet(NXMapTable * _Nonnull table, const void * _Nullable key) + OBJC_MAP_AVAILABILITY; /* return original corresponding value or NULL. When NULL need be stored as value, NXMapMember can be used to test for presence */ -OBJC_EXPORT void *NXMapInsert(NXMapTable *table, const void *key, const void *value) OBJC_MAP_AVAILABILITY; +OBJC_EXPORT void * _Nullable +NXMapInsert(NXMapTable * _Nonnull table, const void * _Nullable key, + const void * _Nullable value) + OBJC_MAP_AVAILABILITY; /* override preexisting pair; Return previous value or NULL. */ -OBJC_EXPORT void *NXMapRemove(NXMapTable *table, const void *key) OBJC_MAP_AVAILABILITY; +OBJC_EXPORT void * _Nullable +NXMapRemove(NXMapTable * _Nonnull table, const void * _Nullable key) + OBJC_MAP_AVAILABILITY; /* previous value or NULL is returned */ /* Iteration over all elements of a table consists in setting up an iteration state and then to progress until all entries have been visited. An example of use for counting elements in a table is: @@ -115,22 +142,31 @@ OBJC_EXPORT void *NXMapRemove(NXMapTable *table, const void *key) OBJC_MAP_AVAIL typedef struct {int index;} NXMapState OBJC_MAP_AVAILABILITY; /* callers should not rely on actual contents of the struct */ -OBJC_EXPORT NXMapState NXInitMapState(NXMapTable *table) OBJC_MAP_AVAILABILITY; +OBJC_EXPORT NXMapState +NXInitMapState(NXMapTable * _Nonnull table) + OBJC_MAP_AVAILABILITY; -OBJC_EXPORT int NXNextMapState(NXMapTable *table, NXMapState *state, const void **key, const void **value) OBJC_MAP_AVAILABILITY; +OBJC_EXPORT int +NXNextMapState(NXMapTable * _Nonnull table, NXMapState * _Nonnull state, + const void * _Nullable * _Nonnull key, + const void * _Nullable * _Nonnull value) + OBJC_MAP_AVAILABILITY; /* returns 0 when all elements have been visited */ /*************** Conveniences ***************/ -OBJC_EXPORT const NXMapTablePrototype NXPtrValueMapPrototype OBJC_MAP_AVAILABILITY; +OBJC_EXPORT const NXMapTablePrototype NXPtrValueMapPrototype + OBJC_MAP_AVAILABILITY; /* hashing is pointer/integer hashing; isEqual is identity; free is no-op. */ -OBJC_EXPORT const NXMapTablePrototype NXStrValueMapPrototype OBJC_MAP_AVAILABILITY; +OBJC_EXPORT const NXMapTablePrototype NXStrValueMapPrototype + OBJC_MAP_AVAILABILITY; /* hashing is string hashing; isEqual is strcmp; free is no-op. */ -OBJC_EXPORT const NXMapTablePrototype NXObjectMapPrototype OBJC2_UNAVAILABLE; +OBJC_EXPORT const NXMapTablePrototype NXObjectMapPrototype + OBJC2_UNAVAILABLE; /* for objects; uses methods: hash, isEqual:, free, all for key. */ __END_DECLS diff --git a/runtime/maptable.mm b/runtime/maptable.mm index 0413ecd..25594a0 100644 --- a/runtime/maptable.mm +++ b/runtime/maptable.mm @@ -161,11 +161,107 @@ BOOL NXCompareMapTables(NXMapTable *table1, NXMapTable *table2) { unsigned NXCountMapTable(NXMapTable *table) { return table->count; } +#if __x86_64__ +extern "C" void __NXMAPTABLE_CORRUPTED__ +(const void *table, const void *buckets, uint64_t count, + uint64_t nbBucketsMinusOne, uint64_t badkeys, uint64_t index, + uint64_t index2, uint64_t pairIndexes, const void *key1, + const void *value1, const void *key2, const void *value2, + const void *key3, const void *value3); + +static int _mapStrIsEqual(NXMapTable *table, const void *key1, const void *key2); + +asm("\n .text" + "\n .private_extern ___NXMAPTABLE_CORRUPTED__" + "\n ___NXMAPTABLE_CORRUPTED__:" + // push a frame for the unwinder to see + "\n pushq %rbp" + "\n mov %rsp, %rbp" + // push register parameters to the stack in reverse order + "\n pushq %r9" + "\n pushq %r8" + "\n pushq %rcx" + "\n pushq %rdx" + "\n pushq %rsi" + "\n pushq %rdi" + // pop the pushed register parameters into their destinations + "\n popq %rax" // table + "\n popq %rbx" // buckets + "\n popq %rcx" // count + "\n popq %rdx" // nbBucketsMinusOne + "\n popq %rdi" // badkeys + "\n popq %rsi" // index + // read stack parameters into their destinations + "\n mov 0*8+16(%rbp), %r8" // index2 + "\n mov 1*8+16(%rbp), %r9" // pairIndexes + "\n mov 2*8+16(%rbp), %r10" // key1 + "\n mov 3*8+16(%rbp), %r11" // value1 + "\n mov 4*8+16(%rbp), %r12" // key2 + "\n mov 5*8+16(%rbp), %r13" // value2 + "\n mov 6*8+16(%rbp), %r14" // key3 + "\n mov 7*8+16(%rbp), %r15" // value3 + "\n ud2"); +#endif + +// Look for a particular case of data corruption (rdar://36373000) +// and investigate it further before crashing. +static void validateKey(NXMapTable *table, MapPair *pair, + unsigned index, unsigned index2) +{ +#if __x86_64__ +# define BADKEY ((void * _Nonnull)(0xfffffffffffffffeULL)) + if (pair->key != BADKEY || + table->prototype->isEqual != _mapStrIsEqual) + { + return; + } + + _objc_inform_now_and_on_crash + ("NXMapTable %p (%p) has invalid key/value pair %p->%p (%p)", + table, table->buckets, pair->key, pair->value, pair); + _objc_inform_now_and_on_crash + ("table %p, buckets %p, count %u, nbNucketsMinusOne %u, " + "prototype %p (hash %p, isEqual %p, free %p)", + table, table->buckets, table->count, table->nbBucketsMinusOne, + table->prototype, table->prototype->hash, table->prototype->isEqual, + table->prototype->free); + + // Count the number of bad keys in the table. + MapPair *pairs = (MapPair *)table->buckets; + unsigned badKeys = 0; + for (unsigned i = 0; i < table->nbBucketsMinusOne+1; i++) { + if (pairs[i].key == BADKEY) badKeys++; + } + + _objc_inform_now_and_on_crash("%u invalid keys in table", badKeys); + + // Record some additional key pairs for posterity. + unsigned pair2Index = nextIndex(table, index); + unsigned pair3Index = nextIndex(table, pair2Index); + MapPair *pair2 = pairs + pair2Index; + MapPair *pair3 = pairs + pair3Index; + uint64_t pairIndexes = ((uint64_t)pair2Index << 32) | pair3Index; + + // Save a bunch of values to registers so we can see them in the crash log. + __NXMAPTABLE_CORRUPTED__ + (// rax, rbx, rcx, rdx + table, table->buckets, table->count, table->nbBucketsMinusOne, + // rdi, rsi, skip rbp, skip rsp + badKeys, index, + // r8, r9, r10, r11 + index2, pairIndexes, pair->key, pair->value, + // r12, r13, r14, r15 + pair2->key, pair2->value, pair3->key, pair3->value); +#endif +} + static INLINE void *_NXMapMember(NXMapTable *table, const void *key, void **value) { MapPair *pairs = (MapPair *)table->buckets; unsigned index = bucketOf(table, key); MapPair *pair = pairs + index; if (pair->key == NX_MAPNOTAKEY) return NX_MAPNOTAKEY; + validateKey(table, pair, index, index); + if (isEqual(table, pair->key, key)) { *value = (void *)pair->value; return (void *)pair->key; @@ -174,6 +270,7 @@ static INLINE void *_NXMapMember(NXMapTable *table, const void *key, void **valu while ((index2 = nextIndex(table, index2)) != index) { pair = pairs + index2; if (pair->key == NX_MAPNOTAKEY) return NX_MAPNOTAKEY; + validateKey(table, pair, index, index2); if (isEqual(table, pair->key, key)) { *value = (void *)pair->value; return (void *)pair->key; @@ -244,7 +341,7 @@ void *NXMapInsert(NXMapTable *table, const void *key, const void *value) { while ((index2 = nextIndex(table, index2)) != index) { pair = pairs + index2; if (pair->key == NX_MAPNOTAKEY) { - pair->key = key; pair->value = value; + pair->key = key; pair->value = value; table->count++; if (table->count * 4 > numBuckets * 3) _NXMapRehash(table); return NULL; @@ -261,8 +358,6 @@ void *NXMapInsert(NXMapTable *table, const void *key, const void *value) { } } -static int mapRemove = 0; - void *NXMapRemove(NXMapTable *table, const void *key) { MapPair *pairs = (MapPair *)table->buckets; unsigned index = bucketOf(table, key); @@ -271,7 +366,6 @@ void *NXMapRemove(NXMapTable *table, const void *key) { int found = 0; const void *old = NULL; if (pair->key == NX_MAPNOTAKEY) return NULL; - mapRemove ++; /* compute chain */ { unsigned index2 = index; diff --git a/runtime/message.h b/runtime/message.h index 725e912..31bd544 100644 --- a/runtime/message.h +++ b/runtime/message.h @@ -24,27 +24,23 @@ #ifndef _OBJC_MESSAGE_H #define _OBJC_MESSAGE_H -#pragma GCC system_header - #include #include -#pragma GCC system_header - #ifndef OBJC_SUPER #define OBJC_SUPER /// Specifies the superclass of an instance. struct objc_super { /// Specifies an instance of a class. - __unsafe_unretained id receiver; + __unsafe_unretained _Nonnull id receiver; /// Specifies the particular superclass of the instance to message. #if !defined(__cplusplus) && !__OBJC2__ /* For compatibility with old objc-runtime.h header */ - __unsafe_unretained Class class; + __unsafe_unretained _Nonnull Class class; #else - __unsafe_unretained Class super_class; + __unsafe_unretained _Nonnull Class super_class; #endif /* super_class is the first class to search */ }; @@ -61,10 +57,16 @@ struct objc_super { * before being called. */ #if !OBJC_OLD_DISPATCH_PROTOTYPES -OBJC_EXPORT void objc_msgSend(void /* id self, SEL op, ... */ ) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); -OBJC_EXPORT void objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */ ) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wincompatible-library-redeclaration" +OBJC_EXPORT void +objc_msgSend(void /* id self, SEL op, ... */ ) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */ ) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); +#pragma clang diagnostic pop #else /** * Sends a message with a simple return value to an instance of a class. @@ -82,8 +84,9 @@ OBJC_EXPORT void objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... * other messages are sent using \c objc_msgSend. Methods that have data structures as return values * are sent using \c objc_msgSendSuper_stret and \c objc_msgSend_stret. */ -OBJC_EXPORT id objc_msgSend(id self, SEL op, ...) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_msgSend(id _Nullable self, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Sends a message with a simple return value to the superclass of an instance of a class. * @@ -98,8 +101,9 @@ OBJC_EXPORT id objc_msgSend(id self, SEL op, ...) * * @see objc_msgSend */ -OBJC_EXPORT id objc_msgSendSuper(struct objc_super *super, SEL op, ...) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_msgSendSuper(struct objc_super * _Nonnull super, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); #endif @@ -113,20 +117,27 @@ OBJC_EXPORT id objc_msgSendSuper(struct objc_super *super, SEL op, ...) * before being called. */ #if !OBJC_OLD_DISPATCH_PROTOTYPES -OBJC_EXPORT void objc_msgSend_stret(void /* id self, SEL op, ... */ ) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wincompatible-library-redeclaration" +OBJC_EXPORT void +objc_msgSend_stret(void /* id self, SEL op, ... */ ) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; -OBJC_EXPORT void objc_msgSendSuper_stret(void /* struct objc_super *super, SEL op, ... */ ) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) + +OBJC_EXPORT void +objc_msgSendSuper_stret(void /* struct objc_super *super, SEL op, ... */ ) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; +#pragma clang diagnostic pop #else /** * Sends a message with a data-structure return value to an instance of a class. * * @see objc_msgSend */ -OBJC_EXPORT void objc_msgSend_stret(id self, SEL op, ...) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) +OBJC_EXPORT void +objc_msgSend_stret(id _Nullable self, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; /** @@ -134,8 +145,10 @@ OBJC_EXPORT void objc_msgSend_stret(id self, SEL op, ...) * * @see objc_msgSendSuper */ -OBJC_EXPORT void objc_msgSendSuper_stret(struct objc_super *super, SEL op, ...) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) +OBJC_EXPORT void +objc_msgSendSuper_stret(struct objc_super * _Nonnull super, + SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; #endif @@ -158,19 +171,26 @@ OBJC_EXPORT void objc_msgSendSuper_stret(struct objc_super *super, SEL op, ...) * before being called. */ #if !OBJC_OLD_DISPATCH_PROTOTYPES +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wincompatible-library-redeclaration" # if defined(__i386__) -OBJC_EXPORT void objc_msgSend_fpret(void /* id self, SEL op, ... */ ) - OBJC_AVAILABLE(10.4, 2.0, 9.0, 1.0); +OBJC_EXPORT void +objc_msgSend_fpret(void /* id self, SEL op, ... */ ) + OBJC_AVAILABLE(10.4, 2.0, 9.0, 1.0, 2.0); # elif defined(__x86_64__) -OBJC_EXPORT void objc_msgSend_fpret(void /* id self, SEL op, ... */ ) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); -OBJC_EXPORT void objc_msgSend_fp2ret(void /* id self, SEL op, ... */ ) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +objc_msgSend_fpret(void /* id self, SEL op, ... */ ) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); +OBJC_EXPORT void +objc_msgSend_fp2ret(void /* id self, SEL op, ... */ ) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); + +#pragma clang diagnostic pop # endif // !OBJC_OLD_DISPATCH_PROTOTYPES @@ -187,8 +207,12 @@ OBJC_EXPORT void objc_msgSend_fp2ret(void /* id self, SEL op, ... */ ) * you must use \c objc_msgSend_fpret for functions returning non-integral type. For \c float or * \c long \c double return types, cast the function to an appropriate function pointer type first. */ -OBJC_EXPORT double objc_msgSend_fpret(id self, SEL op, ...) - OBJC_AVAILABLE(10.4, 2.0, 9.0, 1.0); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wincompatible-library-redeclaration" +OBJC_EXPORT double +objc_msgSend_fpret(id _Nullable self, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.4, 2.0, 9.0, 1.0, 2.0); +#pragma clang diagnostic pop /* Use objc_msgSendSuper() for fp-returning messages to super. */ /* See also objc_msgSendv_fpret() below. */ @@ -199,15 +223,17 @@ OBJC_EXPORT double objc_msgSend_fpret(id self, SEL op, ...) * * @see objc_msgSend */ -OBJC_EXPORT long double objc_msgSend_fpret(id self, SEL op, ...) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT long double +objc_msgSend_fpret(id _Nullable self, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); # if __STDC_VERSION__ >= 199901L -OBJC_EXPORT _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT _Complex long double +objc_msgSend_fp2ret(id _Nullable self, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); # else -OBJC_EXPORT void objc_msgSend_fp2ret(id self, SEL op, ...) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void objc_msgSend_fp2ret(id _Nullable self, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); # endif /* Use objc_msgSendSuper() for fp-returning messages to super. */ @@ -229,16 +255,25 @@ OBJC_EXPORT void objc_msgSend_fp2ret(id self, SEL op, ...) * before being called. */ #if !OBJC_OLD_DISPATCH_PROTOTYPES -OBJC_EXPORT void method_invoke(void /* id receiver, Method m, ... */ ) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); -OBJC_EXPORT void method_invoke_stret(void /* id receiver, Method m, ... */ ) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wincompatible-library-redeclaration" +OBJC_EXPORT void +method_invoke(void /* id receiver, Method m, ... */ ) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +method_invoke_stret(void /* id receiver, Method m, ... */ ) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; +#pragma clang diagnostic pop #else -OBJC_EXPORT id method_invoke(id receiver, Method m, ...) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); -OBJC_EXPORT void method_invoke_stret(id receiver, Method m, ...) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0) +OBJC_EXPORT id _Nullable +method_invoke(id _Nullable receiver, Method _Nonnull m, ...) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +method_invoke_stret(id _Nullable receiver, Method _Nonnull m, ...) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; #endif @@ -259,16 +294,25 @@ OBJC_EXPORT void method_invoke_stret(id receiver, Method m, ...) * but may be compared to other IMP values. */ #if !OBJC_OLD_DISPATCH_PROTOTYPES -OBJC_EXPORT void _objc_msgForward(void /* id receiver, SEL sel, ... */ ) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); -OBJC_EXPORT void _objc_msgForward_stret(void /* id receiver, SEL sel, ... */ ) - OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wincompatible-library-redeclaration" +OBJC_EXPORT void +_objc_msgForward(void /* id receiver, SEL sel, ... */ ) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +_objc_msgForward_stret(void /* id receiver, SEL sel, ... */ ) + OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; +#pragma clang diagnostic pop #else -OBJC_EXPORT id _objc_msgForward(id receiver, SEL sel, ...) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); -OBJC_EXPORT void _objc_msgForward_stret(id receiver, SEL sel, ...) - OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0) +OBJC_EXPORT id _Nullable +_objc_msgForward(id _Nonnull receiver, SEL _Nonnull sel, ...) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +_objc_msgForward_stret(id _Nonnull receiver, SEL _Nonnull sel, ...) + OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; #endif @@ -288,14 +332,25 @@ OBJC_EXPORT void _objc_msgForward_stret(id receiver, SEL sel, ...) typedef void* marg_list; -OBJC_EXPORT id objc_msgSendv(id self, SEL op, size_t arg_size, marg_list arg_frame) OBJC2_UNAVAILABLE; -OBJC_EXPORT void objc_msgSendv_stret(void *stretAddr, id self, SEL op, size_t arg_size, marg_list arg_frame) OBJC2_UNAVAILABLE; +OBJC_EXPORT id _Nullable +objc_msgSendv(id _Nullable self, SEL _Nonnull op, size_t arg_size, + marg_list _Nonnull arg_frame) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT void +objc_msgSendv_stret(void * _Nonnull stretAddr, id _Nullable self, + SEL _Nonnull op, size_t arg_size, + marg_list _Nullable arg_frame) + OBJC2_UNAVAILABLE; /* Note that objc_msgSendv_stret() does not return a structure type, * and should not be cast to do so. This is unlike objc_msgSend_stret() * and objc_msgSendSuper_stret(). */ #if defined(__i386__) -OBJC_EXPORT double objc_msgSendv_fpret(id self, SEL op, unsigned arg_size, marg_list arg_frame) OBJC2_UNAVAILABLE; +OBJC_EXPORT double +objc_msgSendv_fpret(id _Nullable self, SEL _Nonnull op, + unsigned arg_size, marg_list _Nullable arg_frame) + OBJC2_UNAVAILABLE; #endif diff --git a/runtime/objc-abi.h b/runtime/objc-abi.h index 7d9e89e..18430df 100644 --- a/runtime/objc-abi.h +++ b/runtime/objc-abi.h @@ -43,11 +43,51 @@ #include #include +/* Linker metadata symbols */ + +// NSObject was in Foundation/CF on macOS < 10.8. +#if TARGET_OS_OSX +#if __OBJC2__ + +OBJC_EXPORT const char __objc_nsobject_class_10_5 + __asm__("$ld$hide$os10.5$_OBJC_CLASS_$_NSObject"); +OBJC_EXPORT const char __objc_nsobject_class_10_6 + __asm__("$ld$hide$os10.6$_OBJC_CLASS_$_NSObject"); +OBJC_EXPORT const char __objc_nsobject_class_10_7 + __asm__("$ld$hide$os10.7$_OBJC_CLASS_$_NSObject"); + +OBJC_EXPORT const char __objc_nsobject_metaclass_10_5 + __asm__("$ld$hide$os10.5$_OBJC_METACLASS_$_NSObject"); +OBJC_EXPORT const char __objc_nsobject_metaclass_10_6 + __asm__("$ld$hide$os10.6$_OBJC_METACLASS_$_NSObject"); +OBJC_EXPORT const char __objc_nsobject_metaclass_10_7 + __asm__("$ld$hide$os10.7$_OBJC_METACLASS_$_NSObject"); + +OBJC_EXPORT const char __objc_nsobject_isa_10_5 + __asm__("$ld$hide$os10.5$_OBJC_IVAR_$_NSObject.isa"); +OBJC_EXPORT const char __objc_nsobject_isa_10_6 + __asm__("$ld$hide$os10.6$_OBJC_IVAR_$_NSObject.isa"); +OBJC_EXPORT const char __objc_nsobject_isa_10_7 + __asm__("$ld$hide$os10.7$_OBJC_IVAR_$_NSObject.isa"); + +#else + +OBJC_EXPORT const char __objc_nsobject_class_10_5 + __asm__("$ld$hide$os10.5$.objc_class_name_NSObject"); +OBJC_EXPORT const char __objc_nsobject_class_10_6 + __asm__("$ld$hide$os10.6$.objc_class_name_NSObject"); +OBJC_EXPORT const char __objc_nsobject_class_10_7 + __asm__("$ld$hide$os10.7$.objc_class_name_NSObject"); + +#endif +#endif + /* Runtime startup. */ // Old static initializer. Used by old crt1.o and old bug workarounds. -OBJC_EXPORT void _objcInit(void) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT void +_objcInit(void) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /* Images */ @@ -60,6 +100,7 @@ typedef struct objc_image_info { #if __cplusplus >= 201103L private: enum : uint32_t { + // 1 byte assorted flags IsReplacement = 1<<0, // used for Fix&Continue, now ignored SupportsGC = 1<<1, // image supports GC RequiresGC = 1<<2, // image requires GC @@ -67,17 +108,28 @@ typedef struct objc_image_info { CorrectedSynthesize = 1<<4, // used for an old workaround, now ignored IsSimulated = 1<<5, // image compiled for a simulator platform HasCategoryClassProperties = 1<<6, // class properties in category_t + OptimizedByDyldClosure = 1 << 7, // dyld (not the shared cache) optimized this. - SwiftVersionMaskShift = 8, - SwiftVersionMask = 0xff << SwiftVersionMaskShift // Swift ABI version + // 1 byte Swift unstable ABI version number + SwiftUnstableVersionMaskShift = 8, + SwiftUnstableVersionMask = 0xff << SwiftUnstableVersionMaskShift, + // 2 byte Swift stable ABI version number + SwiftStableVersionMaskShift = 16, + SwiftStableVersionMask = 0xffffUL << SwiftStableVersionMaskShift }; - public: + public: enum : uint32_t { + // Values for SwiftUnstableVersion + // All stable ABIs store SwiftVersion5 here. SwiftVersion1 = 1, SwiftVersion1_2 = 2, SwiftVersion2 = 3, - SwiftVersion3 = 4 + SwiftVersion3 = 4, + SwiftVersion4 = 5, + SwiftVersion4_1 = 6, + SwiftVersion4_2 = 6, // [sic] + SwiftVersion5 = 7 }; public: @@ -86,8 +138,9 @@ typedef struct objc_image_info { bool requiresGC() const { return flags & RequiresGC; } bool optimizedByDyld() const { return flags & OptimizedByDyld; } bool hasCategoryClassProperties() const { return flags & HasCategoryClassProperties; } - bool containsSwift() const { return (flags & SwiftVersionMask) != 0; } - uint32_t swiftVersion() const { return (flags & SwiftVersionMask) >> SwiftVersionMaskShift; } + bool optimizedByDyldClosure() const { return flags & OptimizedByDyldClosure; } + bool containsSwift() const { return (flags & SwiftUnstableVersionMask) != 0; } + uint32_t swiftUnstableVersion() const { return (flags & SwiftUnstableVersionMask) >> SwiftUnstableVersionMaskShift; } #endif } objc_image_info; @@ -122,52 +175,78 @@ HasClassProperties: /* Properties */ // Read or write an object property. Not all object properties use these. -OBJC_EXPORT id objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); -OBJC_EXPORT void objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, id newValue, BOOL atomic, signed char shouldCopy) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_getProperty(id _Nullable self, SEL _Nonnull _cmd, + ptrdiff_t offset, BOOL atomic) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); -OBJC_EXPORT void objc_setProperty_atomic(id self, SEL _cmd, id newValue, ptrdiff_t offset) - OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0); -OBJC_EXPORT void objc_setProperty_nonatomic(id self, SEL _cmd, id newValue, ptrdiff_t offset) - OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0); -OBJC_EXPORT void objc_setProperty_atomic_copy(id self, SEL _cmd, id newValue, ptrdiff_t offset) - OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0); -OBJC_EXPORT void objc_setProperty_nonatomic_copy(id self, SEL _cmd, id newValue, ptrdiff_t offset) - OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0); +OBJC_EXPORT void +objc_setProperty(id _Nullable self, SEL _Nonnull _cmd, ptrdiff_t offset, + id _Nullable newValue, BOOL atomic, signed char shouldCopy) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +objc_setProperty_atomic(id _Nullable self, SEL _Nonnull _cmd, + id _Nullable newValue, ptrdiff_t offset) + OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +objc_setProperty_nonatomic(id _Nullable self, SEL _Nonnull _cmd, + id _Nullable newValue, ptrdiff_t offset) + OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +objc_setProperty_atomic_copy(id _Nullable self, SEL _Nonnull _cmd, + id _Nullable newValue, ptrdiff_t offset) + OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +objc_setProperty_nonatomic_copy(id _Nullable self, SEL _Nonnull _cmd, + id _Nullable newValue, ptrdiff_t offset) + OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0); // Read or write a non-object property. Not all uses are C structs, // and not all C struct properties use this. -OBJC_EXPORT void objc_copyStruct(void *dest, const void *src, ptrdiff_t size, BOOL atomic, BOOL hasStrong) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +objc_copyStruct(void * _Nonnull dest, const void * _Nonnull src, + ptrdiff_t size, BOOL atomic, BOOL hasStrong) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); // Perform a copy of a C++ object using striped locks. Used by non-POD C++ typed atomic properties. -OBJC_EXPORT void objc_copyCppObjectAtomic(void *dest, const void *src, void (*copyHelper) (void *dest, const void *source)) - OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0); +OBJC_EXPORT void +objc_copyCppObjectAtomic(void * _Nonnull dest, const void * _Nonnull src, + void (* _Nonnull copyHelper) + (void * _Nonnull dest, const void * _Nonnull source)) + OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0); /* Classes. */ #if __OBJC2__ -OBJC_EXPORT IMP _objc_empty_vtable - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT IMP _Nonnull _objc_empty_vtable + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); #endif OBJC_EXPORT struct objc_cache _objc_empty_cache - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /* Messages */ #if __OBJC2__ // objc_msgSendSuper2() takes the current search class, not its superclass. -OBJC_EXPORT id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) - OBJC_AVAILABLE(10.6, 2.0, 9.0, 1.0); -OBJC_EXPORT void objc_msgSendSuper2_stret(struct objc_super *super, SEL op,...) - OBJC_AVAILABLE(10.6, 2.0, 9.0, 1.0) +OBJC_EXPORT id _Nullable +objc_msgSendSuper2(struct objc_super * _Nonnull super, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.6, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +objc_msgSendSuper2_stret(struct objc_super * _Nonnull super, + SEL _Nonnull op,...) + OBJC_AVAILABLE(10.6, 2.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; // objc_msgSend_noarg() may be faster for methods with no additional arguments. -OBJC_EXPORT id objc_msgSend_noarg(id self, SEL _cmd) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_msgSend_noarg(id _Nullable self, SEL _Nonnull _cmd) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); #endif #if __OBJC2__ @@ -175,29 +254,42 @@ OBJC_EXPORT id objc_msgSend_noarg(id self, SEL _cmd) // may perform extra sanity checking. // Old objc_msgSendSuper() does not have a debug version; this is OBJC2 only. // *_fixup() do not have debug versions; use non-fixup only for debug mode. -OBJC_EXPORT id objc_msgSend_debug(id self, SEL op, ...) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); -OBJC_EXPORT id objc_msgSendSuper2_debug(struct objc_super *super, SEL op, ...) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); -OBJC_EXPORT void objc_msgSend_stret_debug(id self, SEL op, ...) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0) +OBJC_EXPORT id _Nullable +objc_msgSend_debug(id _Nullable self, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT id _Nullable +objc_msgSendSuper2_debug(struct objc_super * _Nonnull super, + SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +objc_msgSend_stret_debug(id _Nullable self, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; -OBJC_EXPORT void objc_msgSendSuper2_stret_debug(struct objc_super *super, SEL op,...) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0) + +OBJC_EXPORT void +objc_msgSendSuper2_stret_debug(struct objc_super * _Nonnull super, + SEL _Nonnull op,...) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; # if defined(__i386__) -OBJC_EXPORT double objc_msgSend_fpret_debug(id self, SEL op, ...) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT double +objc_msgSend_fpret_debug(id _Nullable self, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); # elif defined(__x86_64__) -OBJC_EXPORT long double objc_msgSend_fpret_debug(id self, SEL op, ...) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT long double +objc_msgSend_fpret_debug(id _Nullable self, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); # if __STDC_VERSION__ >= 199901L -OBJC_EXPORT _Complex long double objc_msgSend_fp2ret_debug(id self, SEL op, ...) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT _Complex long double +objc_msgSend_fp2ret_debug(id _Nullable self, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); # else -OBJC_EXPORT void objc_msgSend_fp2ret_debug(id self, SEL op, ...) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT void +objc_msgSend_fp2ret_debug(id _Nullable self, SEL _Nonnull op, ...) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); # endif # endif @@ -219,66 +311,80 @@ OBJC_EXPORT void objc_msgSend_fp2ret_debug(id self, SEL op, ...) // - Red zone is not preserved. // See each architecture's implementation for details. -OBJC_EXPORT void objc_msgLookup(void) - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); -OBJC_EXPORT void objc_msgLookupSuper2(void) - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); -OBJC_EXPORT void objc_msgLookup_stret(void) - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0) +OBJC_EXPORT void +objc_msgLookup(void) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); + +OBJC_EXPORT void +objc_msgLookupSuper2(void) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); + +OBJC_EXPORT void +objc_msgLookup_stret(void) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0) OBJC_ARM64_UNAVAILABLE; -OBJC_EXPORT void objc_msgLookupSuper2_stret(void) - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0) + +OBJC_EXPORT void +objc_msgLookupSuper2_stret(void) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0) OBJC_ARM64_UNAVAILABLE; # if defined(__i386__) -OBJC_EXPORT void objc_msgLookup_fpret(void) - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); +OBJC_EXPORT void +objc_msgLookup_fpret(void) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); + # elif defined(__x86_64__) -OBJC_EXPORT void objc_msgLookup_fpret(void) - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); -OBJC_EXPORT void objc_msgLookup_fp2ret(void) - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); +OBJC_EXPORT void +objc_msgLookup_fpret(void) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); + +OBJC_EXPORT void +objc_msgLookup_fp2ret(void) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); # endif #endif -#if TARGET_OS_OSX && defined(__x86_64__) -// objc_msgSend_fixup() is used for vtable-dispatchable call sites. -OBJC_EXPORT void objc_msgSend_fixup(void) - __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized") - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; -OBJC_EXPORT void objc_msgSend_stret_fixup(void) - __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized") - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; -OBJC_EXPORT void objc_msgSendSuper2_fixup(void) - __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized") - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; -OBJC_EXPORT void objc_msgSendSuper2_stret_fixup(void) - __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized") - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; -OBJC_EXPORT void objc_msgSend_fpret_fixup(void) - __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized") - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; -OBJC_EXPORT void objc_msgSend_fp2ret_fixup(void) - __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized") - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; +#if (TARGET_OS_OSX || TARGET_OS_SIMULATOR) && defined(__x86_64__) +// objc_msgSend_fixup() was used for vtable-dispatchable call sites. +// The symbols remain exported on macOS for binary compatibility. +// The symbols can probably be removed from iOS simulator but we haven't tried. +OBJC_EXPORT void +objc_msgSend_fixup(void) + __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized"); + +OBJC_EXPORT void +objc_msgSend_stret_fixup(void) + __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized"); + +OBJC_EXPORT void +objc_msgSendSuper2_fixup(void) + __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized"); + +OBJC_EXPORT void +objc_msgSendSuper2_stret_fixup(void) + __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized"); + +OBJC_EXPORT void +objc_msgSend_fpret_fixup(void) + __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized"); + +OBJC_EXPORT void +objc_msgSend_fp2ret_fixup(void) + __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized"); #endif /* C++-compatible exception handling. */ #if __OBJC2__ -// fixme these conflict with C++ compiler's internal definitions -#if !defined(__cplusplus) - // Vtable for C++ exception typeinfo for Objective-C types. -OBJC_EXPORT const void *objc_ehtype_vtable[] - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const void * _Nullable objc_ehtype_vtable[] + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); // C++ exception typeinfo for type `id`. OBJC_EXPORT struct objc_typeinfo OBJC_EHTYPE_id - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); - -#endif + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); // Exception personality function for Objective-C and Objective-C++ code. struct _Unwind_Exception; @@ -287,16 +393,17 @@ OBJC_EXPORT int __objc_personality_v0(int version, int actions, uint64_t exceptionClass, - struct _Unwind_Exception *exceptionObject, - struct _Unwind_Context *context) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); + struct _Unwind_Exception * _Nonnull exceptionObject, + struct _Unwind_Context * _Nonnull context) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); #endif /* ARC */ -OBJC_EXPORT id objc_retainBlock(id) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_retainBlock(id _Nullable) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); /* Non-pointer isa */ @@ -305,7 +412,7 @@ OBJC_EXPORT id objc_retainBlock(id) // Extract class pointer from an isa field. -#if TARGET_OS_SIMULATOR +#if TARGET_OS_SIMULATOR && !TARGET_OS_IOSMAC // No simulators use nonpointer isa yet. #elif __LP64__ @@ -315,9 +422,9 @@ OBJC_EXPORT id objc_retainBlock(id) // Packed-isa version. This one is used directly by Swift code. // (Class)(isa & (uintptr_t)&objc_absolute_packed_isa_class_mask) == class ptr OBJC_EXPORT const struct { char c; } objc_absolute_packed_isa_class_mask - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); -#elif __ARM_ARCH_7K__ >= 2 +#elif (__ARM_ARCH_7K__ >= 2 || (__arm64__ && !__LP64__)) # define OBJC_HAVE_NONPOINTER_ISA 1 # define OBJC_HAVE_INDEXED_NONPOINTER_ISA 1 @@ -329,18 +436,42 @@ OBJC_EXPORT const struct { char c; } objc_absolute_packed_isa_class_mask // cls = (Class)isa; // } OBJC_EXPORT const struct { char c; } objc_absolute_indexed_isa_magic_mask - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); OBJC_EXPORT const struct { char c; } objc_absolute_indexed_isa_magic_value - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); OBJC_EXPORT const struct { char c; } objc_absolute_indexed_isa_index_mask - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); OBJC_EXPORT const struct { char c; } objc_absolute_indexed_isa_index_shift - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); #endif -// OBJC2 #endif + +/* Object class */ + +// This symbol might be required for binary compatibility, so we +// declare it here where TAPI will see it. +#if __OBJC__ && __OBJC2__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wobjc-interface-ivars" +#if !defined(OBJC_DECLARE_SYMBOLS) +__OSX_AVAILABLE(10.0) +__IOS_UNAVAILABLE __TVOS_UNAVAILABLE +__WATCHOS_UNAVAILABLE +# ifndef __APPLE_BLEACH_SDK__ +__BRIDGEOS_UNAVAILABLE +# endif +#endif +OBJC_ROOT_CLASS +@interface Object { + Class isa; +} +@end +#pragma clang diagnostic pop +#endif + + // _OBJC_ABI_H #endif diff --git a/runtime/objc-accessors.mm b/runtime/objc-accessors.mm index 25ec71b..612abea 100644 --- a/runtime/objc-accessors.mm +++ b/runtime/objc-accessors.mm @@ -39,10 +39,9 @@ - (id)mutableCopyWithZone:(void *)zone; @end -// These locks must not be at function scope. -static StripedMap PropertyLocks; -static StripedMap StructLocks; -static StripedMap CppObjectLocks; +StripedMap PropertyLocks; +StripedMap StructLocks; +StripedMap CppObjectLocks; #define MUTABLE_COPY 2 diff --git a/runtime/objc-api.h b/runtime/objc-api.h index 42f88e7..284f24f 100644 --- a/runtime/objc-api.h +++ b/runtime/objc-api.h @@ -28,6 +28,7 @@ #include #include #include +#include #ifndef __has_feature # define __has_feature(x) 0 @@ -41,6 +42,41 @@ # define __has_attribute(x) 0 #endif +#if !__has_feature(nullability) +# ifndef _Nullable +# define _Nullable +# endif +# ifndef _Nonnull +# define _Nonnull +# endif +# ifndef _Null_unspecified +# define _Null_unspecified +# endif +#endif + +#ifndef __APPLE_BLEACH_SDK__ +# if __has_feature(attribute_availability_bridgeos) +# ifndef __BRIDGEOS_AVAILABLE +# define __BRIDGEOS_AVAILABLE(_vers) __OS_AVAILABILITY(bridgeos,introduced=_vers) +# endif +# ifndef __BRIDGEOS_DEPRECATED +# define __BRIDGEOS_DEPRECATED(_start, _dep, _msg) __BRIDGEOS_AVAILABLE(_start) __OS_AVAILABILITY_MSG(bridgeos,deprecated=_dep,_msg) +# endif +# ifndef __BRIDGEOS_UNAVAILABLE +# define __BRIDGEOS_UNAVAILABLE __OS_AVAILABILITY(bridgeos,unavailable) +# endif +# else +# ifndef __BRIDGEOS_AVAILABLE +# define __BRIDGEOS_AVAILABLE(_vers) +# endif +# ifndef __BRIDGEOS_DEPRECATED +# define __BRIDGEOS_DEPRECATED(_start, _dep, _msg) +# endif +# ifndef __BRIDGEOS_UNAVAILABLE +# define __BRIDGEOS_UNAVAILABLE +# endif +# endif +#endif /* * OBJC_API_VERSION 0 or undef: Tiger and earlier API only @@ -86,15 +122,67 @@ /* OBJC_OLD_DISPATCH_PROTOTYPES == 0 enforces the rule that the dispatch * functions must be cast to an appropriate function pointer type. */ #if !defined(OBJC_OLD_DISPATCH_PROTOTYPES) -# define OBJC_OLD_DISPATCH_PROTOTYPES 1 +# if __swift__ + // Existing Swift code expects IMP to be Comparable. + // Variadic IMP is comparable via OpaquePointer; non-variadic IMP isn't. +# define OBJC_OLD_DISPATCH_PROTOTYPES 1 +# else +# define OBJC_OLD_DISPATCH_PROTOTYPES 0 +# endif #endif /* OBJC_AVAILABLE: shorthand for all-OS availability */ -#if !defined(OBJC_AVAILABLE) -# define OBJC_AVAILABLE(x, i, t, w) \ - __OSX_AVAILABLE(x) __IOS_AVAILABLE(i) \ - __TVOS_AVAILABLE(t) __WATCHOS_AVAILABLE(w) +#ifndef __APPLE_BLEACH_SDK__ +# if !defined(OBJC_AVAILABLE) +# define OBJC_AVAILABLE(x, i, t, w, b) \ + __OSX_AVAILABLE(x) __IOS_AVAILABLE(i) __TVOS_AVAILABLE(t) \ + __WATCHOS_AVAILABLE(w) __BRIDGEOS_AVAILABLE(b) +# endif +#else +# if !defined(OBJC_AVAILABLE) +# define OBJC_AVAILABLE(x, i, t, w, b) \ + __OSX_AVAILABLE(x) __IOS_AVAILABLE(i) __TVOS_AVAILABLE(t) \ + __WATCHOS_AVAILABLE(w) +# endif +#endif + + +/* OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE: Deprecated on OS X, + * unavailable everywhere else. */ +#ifndef __APPLE_BLEACH_SDK__ +# if !defined(OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE) +# define OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(_start, _dep, _msg) \ + __OSX_DEPRECATED(_start, _dep, _msg) \ + __IOS_UNAVAILABLE __TVOS_UNAVAILABLE \ + __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE +# endif +#else +# if !defined(OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE) +# define OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(_start, _dep, _msg) \ + __OSX_DEPRECATED(_start, _dep, _msg) \ + __IOS_UNAVAILABLE __TVOS_UNAVAILABLE \ + __WATCHOS_UNAVAILABLE +# endif +#endif + + +/* OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE: Available on OS X, + * unavailable everywhere else. */ +#ifndef __APPLE_BLEACH_SDK__ +# if !defined(OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE) +# define OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE(vers) \ + __OSX_AVAILABLE(vers) \ + __IOS_UNAVAILABLE __TVOS_UNAVAILABLE \ + __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE +# endif +#else +# if !defined(OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE) +# define OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE(vers) \ + __OSX_AVAILABLE(vers) \ + __IOS_UNAVAILABLE __TVOS_UNAVAILABLE \ + __WATCHOS_UNAVAILABLE +# endif #endif @@ -118,7 +206,7 @@ # define OBJC2_UNAVAILABLE \ __OSX_DEPRECATED(10.5, 10.5, "not available in __OBJC2__") \ __IOS_DEPRECATED(2.0, 2.0, "not available in __OBJC2__") \ - __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE + __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE # endif #endif @@ -229,4 +317,12 @@ #define OBJC_OPTIONS(_type, _name) _type _name; enum #endif +#if !defined(OBJC_RETURNS_RETAINED) +# if __OBJC__ && __has_attribute(ns_returns_retained) +# define OBJC_RETURNS_RETAINED __attribute__((ns_returns_retained)) +# else +# define OBJC_RETURNS_RETAINED +# endif +#endif + #endif diff --git a/runtime/objc-auto.h b/runtime/objc-auto.h index 3624af5..364590f 100644 --- a/runtime/objc-auto.h +++ b/runtime/objc-auto.h @@ -24,8 +24,6 @@ #ifndef _OBJC_AUTO_H_ #define _OBJC_AUTO_H_ -#pragma GCC system_header - #include #include #include @@ -57,7 +55,7 @@ enum { OBJC_EXHAUSTIVE_COLLECTION = (3 << 0), OBJC_COLLECT_IF_NEEDED = (1 << 3), - OBJC_WAIT_UNTIL_DONE = (1 << 4), + OBJC_WAIT_UNTIL_DONE = (1 << 4) }; enum { @@ -65,71 +63,72 @@ enum { }; -#ifndef OBJC_NO_GC +#if !defined(OBJC_NO_GC) || \ + (OBJC_DECLARE_SYMBOLS && !defined(OBJC_NO_GC_API)) /* Out-of-line declarations */ OBJC_EXPORT void objc_collect(unsigned long options) - __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.6, 10.8, "it does nothing"); OBJC_EXPORT BOOL objc_collectingEnabled(void) - __OSX_DEPRECATED(10.5, 10.8, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.5, 10.8, "it always returns NO"); OBJC_EXPORT malloc_zone_t *objc_collectableZone(void) - __OSX_DEPRECATED(10.7, 10.8, "it always returns nil") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.7, 10.8, "it always returns nil"); OBJC_EXPORT void objc_setCollectionThreshold(size_t threshold) - __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.5, 10.8, "it does nothing"); OBJC_EXPORT void objc_setCollectionRatio(size_t ratio) - __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.5, 10.8, "it does nothing"); OBJC_EXPORT BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation) - __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead"); OBJC_EXPORT BOOL objc_atomicCompareAndSwapPtrBarrier(id predicate, id replacement, volatile id *objectLocation) - __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead"); OBJC_EXPORT BOOL objc_atomicCompareAndSwapGlobal(id predicate, id replacement, volatile id *objectLocation) - __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead"); OBJC_EXPORT BOOL objc_atomicCompareAndSwapGlobalBarrier(id predicate, id replacement, volatile id *objectLocation) - __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead"); OBJC_EXPORT BOOL objc_atomicCompareAndSwapInstanceVariable(id predicate, id replacement, volatile id *objectLocation) - __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead"); OBJC_EXPORT BOOL objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate, id replacement, volatile id *objectLocation) - __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead"); OBJC_EXPORT id objc_assign_strongCast(id val, id *dest) - __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.8, "use a simple assignment instead"); OBJC_EXPORT id objc_assign_global(id val, id *dest) - __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.8, "use a simple assignment instead"); OBJC_EXPORT id objc_assign_threadlocal(id val, id *dest) - __OSX_DEPRECATED(10.7, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.7, 10.8, "use a simple assignment instead"); OBJC_EXPORT id objc_assign_ivar(id value, id dest, ptrdiff_t offset) - __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.8, "use a simple assignment instead"); OBJC_EXPORT void *objc_memmove_collectable(void *dst, const void *src, size_t size) - __OSX_DEPRECATED(10.4, 10.8, "use memmove instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.8, "use memmove instead"); OBJC_EXPORT id objc_read_weak(id *location) - __OSX_DEPRECATED(10.5, 10.8, "use a simple read instead, or convert to zeroing __weak") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.5, 10.8, "use a simple read instead, or convert to zeroing __weak"); OBJC_EXPORT id objc_assign_weak(id value, id *location) - __OSX_DEPRECATED(10.5, 10.8, "use a simple assignment instead, or convert to zeroing __weak") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.5, 10.8, "use a simple assignment instead, or convert to zeroing __weak"); OBJC_EXPORT void objc_registerThreadWithCollector(void) - __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.6, 10.8, "it does nothing"); OBJC_EXPORT void objc_unregisterThreadWithCollector(void) - __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.6, 10.8, "it does nothing"); OBJC_EXPORT void objc_assertRegisteredThreadWithCollector(void) - __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.6, 10.8, "it does nothing"); OBJC_EXPORT void objc_clear_stack(unsigned long options) - __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.5, 10.8, "it does nothing"); OBJC_EXPORT BOOL objc_is_finalized(void *ptr) - __OSX_DEPRECATED(10.4, 10.8, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.8, "it always returns NO"); OBJC_EXPORT void objc_finalizeOnMainThread(Class cls) - __OSX_DEPRECATED(10.5, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.5, 10.5, "it does nothing"); OBJC_EXPORT BOOL objc_collecting_enabled(void) - __OSX_DEPRECATED(10.4, 10.5, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.5, "it always returns NO"); OBJC_EXPORT void objc_set_collection_threshold(size_t threshold) - __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.5, "it does nothing"); OBJC_EXPORT void objc_set_collection_ratio(size_t ratio) - __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.5, "it does nothing"); OBJC_EXPORT void objc_start_collector_thread(void) - __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.5, "it does nothing"); OBJC_EXPORT void objc_startCollectorThread(void) - __OSX_DEPRECATED(10.5, 10.7, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.5, 10.7, "it does nothing"); OBJC_EXPORT id objc_allocate_object(Class cls, int extra) - __OSX_DEPRECATED(10.4, 10.4, "use class_createInstance instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.4, "use class_createInstance instead"); /* !defined(OBJC_NO_GC) */ @@ -199,7 +198,7 @@ static OBJC_INLINE id objc_assign_threadlocal(id val, id *dest) OBJC_GC_DEPRECATED("use a simple assignment instead") static OBJC_INLINE id objc_assign_ivar(id val, id dest, ptrdiff_t offset) - { return (*(id*)((char *)dest+offset) = val); } + { return (*(id*)((intptr_t)(char *)dest+offset) = val); } OBJC_GC_DEPRECATED("use a simple read instead, or convert to zeroing __weak") static OBJC_INLINE id objc_read_weak(id *location) @@ -235,10 +234,10 @@ static OBJC_INLINE void objc_start_collector_thread(void) { } extern id objc_allocate_object(Class cls, int extra) UNAVAILABLE_ATTRIBUTE; #else OBJC_EXPORT id class_createInstance(Class cls, size_t extraBytes) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); OBJC_GC_DEPRECATED("use class_createInstance instead") static OBJC_INLINE id objc_allocate_object(Class cls, int extra) - { return class_createInstance(cls, extra); } + { return class_createInstance(cls, (size_t)extra); } #endif OBJC_GC_DEPRECATED("it does nothing") diff --git a/runtime/objc-auto.mm b/runtime/objc-auto.mm index bf7dceb..e8e704b 100644 --- a/runtime/objc-auto.mm +++ b/runtime/objc-auto.mm @@ -21,7 +21,9 @@ * @APPLE_LICENSE_HEADER_END@ */ +#define OBJC_DECLARE_SYMBOLS 1 #include "objc-private.h" +#include "objc-auto.h" // GC is no longer supported. @@ -35,86 +37,85 @@ #else // No GC but we do need to export GC symbols. -// These are mostly the same as the OBJC_NO_GC inline versions in objc-auto.h. # if !SUPPORT_GC_COMPAT # error inconsistent config settings # endif -OBJC_EXPORT void objc_collect(unsigned long options __unused) { } -OBJC_EXPORT BOOL objc_collectingEnabled(void) { return NO; } -OBJC_EXPORT void objc_setCollectionThreshold(size_t threshold __unused) { } -OBJC_EXPORT void objc_setCollectionRatio(size_t ratio __unused) { } -OBJC_EXPORT void objc_startCollectorThread(void) { } +void objc_collect(unsigned long options __unused) { } +BOOL objc_collectingEnabled(void) { return NO; } +void objc_setCollectionThreshold(size_t threshold __unused) { } +void objc_setCollectionRatio(size_t ratio __unused) { } +void objc_startCollectorThread(void) { } #if TARGET_OS_WIN32 -OBJC_EXPORT BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation) +BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation) { void *original = InterlockedCompareExchangePointer((void * volatile *)objectLocation, (void *)replacement, (void *)predicate); return (original == predicate); } -OBJC_EXPORT BOOL objc_atomicCompareAndSwapPtrBarrier(id predicate, id replacement, volatile id *objectLocation) +BOOL objc_atomicCompareAndSwapPtrBarrier(id predicate, id replacement, volatile id *objectLocation) { void *original = InterlockedCompareExchangePointer((void * volatile *)objectLocation, (void *)replacement, (void *)predicate); return (original == predicate); } #else -OBJC_EXPORT BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation) +BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation) { return OSAtomicCompareAndSwapPtr((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); } -OBJC_EXPORT BOOL objc_atomicCompareAndSwapPtrBarrier(id predicate, id replacement, volatile id *objectLocation) +BOOL objc_atomicCompareAndSwapPtrBarrier(id predicate, id replacement, volatile id *objectLocation) { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); } #endif -OBJC_EXPORT BOOL objc_atomicCompareAndSwapGlobal(id predicate, id replacement, volatile id *objectLocation) +BOOL objc_atomicCompareAndSwapGlobal(id predicate, id replacement, volatile id *objectLocation) { return objc_atomicCompareAndSwapPtr(predicate, replacement, objectLocation); } -OBJC_EXPORT BOOL objc_atomicCompareAndSwapGlobalBarrier(id predicate, id replacement, volatile id *objectLocation) +BOOL objc_atomicCompareAndSwapGlobalBarrier(id predicate, id replacement, volatile id *objectLocation) { return objc_atomicCompareAndSwapPtrBarrier(predicate, replacement, objectLocation); } -OBJC_EXPORT BOOL objc_atomicCompareAndSwapInstanceVariable(id predicate, id replacement, volatile id *objectLocation) +BOOL objc_atomicCompareAndSwapInstanceVariable(id predicate, id replacement, volatile id *objectLocation) { return objc_atomicCompareAndSwapPtr(predicate, replacement, objectLocation); } -OBJC_EXPORT BOOL objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate, id replacement, volatile id *objectLocation) +BOOL objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate, id replacement, volatile id *objectLocation) { return objc_atomicCompareAndSwapPtrBarrier(predicate, replacement, objectLocation); } -OBJC_EXPORT id objc_assign_strongCast(id val, id *dest) +id objc_assign_strongCast(id val, id *dest) { return (*dest = val); } -OBJC_EXPORT id objc_assign_global(id val, id *dest) +id objc_assign_global(id val, id *dest) { return (*dest = val); } -OBJC_EXPORT id objc_assign_threadlocal(id val, id *dest) +id objc_assign_threadlocal(id val, id *dest) { return (*dest = val); } -OBJC_EXPORT id objc_assign_ivar(id val, id dest, ptrdiff_t offset) +id objc_assign_ivar(id val, id dest, ptrdiff_t offset) { return (*(id*)((char *)dest+offset) = val); } -OBJC_EXPORT id objc_read_weak(id *location) +id objc_read_weak(id *location) { return *location; } -OBJC_EXPORT id objc_assign_weak(id value, id *location) +id objc_assign_weak(id value, id *location) { return (*location = value); } -OBJC_EXPORT void *objc_memmove_collectable(void *dst, const void *src, size_t size) +void *objc_memmove_collectable(void *dst, const void *src, size_t size) { return memmove(dst, src, size); } -OBJC_EXPORT void objc_finalizeOnMainThread(Class cls __unused) { } -OBJC_EXPORT BOOL objc_is_finalized(void *ptr __unused) { return NO; } -OBJC_EXPORT void objc_clear_stack(unsigned long options __unused) { } +void objc_finalizeOnMainThread(Class cls __unused) { } +BOOL objc_is_finalized(void *ptr __unused) { return NO; } +void objc_clear_stack(unsigned long options __unused) { } -OBJC_EXPORT BOOL objc_collecting_enabled(void) { return NO; } -OBJC_EXPORT void objc_set_collection_threshold(size_t threshold __unused) { } -OBJC_EXPORT void objc_set_collection_ratio(size_t ratio __unused) { } -OBJC_EXPORT void objc_start_collector_thread(void) { } +BOOL objc_collecting_enabled(void) { return NO; } +void objc_set_collection_threshold(size_t threshold __unused) { } +void objc_set_collection_ratio(size_t ratio __unused) { } +void objc_start_collector_thread(void) { } -OBJC_EXPORT id objc_allocate_object(Class cls, int extra) +id objc_allocate_object(Class cls, int extra) { return class_createInstance(cls, extra); } -OBJC_EXPORT void objc_registerThreadWithCollector() { } -OBJC_EXPORT void objc_unregisterThreadWithCollector() { } -OBJC_EXPORT void objc_assertRegisteredThreadWithCollector() { } +void objc_registerThreadWithCollector() { } +void objc_unregisterThreadWithCollector() { } +void objc_assertRegisteredThreadWithCollector() { } -OBJC_EXPORT malloc_zone_t* objc_collect_init(int(*callback)() __unused) { return nil; } -OBJC_EXPORT void* objc_collectableZone() { return nil; } +malloc_zone_t* objc_collect_init(int(*callback)() __unused) { return nil; } +malloc_zone_t* objc_collectableZone() { return nil; } -OBJC_EXPORT BOOL objc_isAuto(id object __unused) { return NO; } -OBJC_EXPORT BOOL objc_dumpHeap(char *filename __unused, unsigned long length __unused) +BOOL objc_isAuto(id object __unused) { return NO; } +BOOL objc_dumpHeap(char *filename __unused, unsigned long length __unused) { return NO; } // not OBJC_NO_GC_API diff --git a/runtime/objc-block-trampolines.h b/runtime/objc-block-trampolines.h new file mode 100644 index 0000000..c65eeef --- /dev/null +++ b/runtime/objc-block-trampolines.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2018 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + + +#ifndef _OBJC_TRAMPOLINES_H +#define _OBJC_TRAMPOLINES_H + +/* + * WARNING DANGER HAZARD BEWARE EEK + * + * Everything in this file is for Apple Internal use only. + * These will change in arbitrary OS updates and in unpredictable ways. + * When your program breaks, you get to keep both pieces. + */ + +/* + * objc-block-trampolines.h: Symbols for IMP block trampolines + */ + +// WARNING: remapped code and dtrace do not play well together. Dtrace +// will place trap instructions to instrument the code, which then get +// remapped along with everything else. The remapped traps are not +// recognized by dtrace and the process crashes. To avoid this, dtrace +// blacklists this library by name. Do not change the name of this +// library. rdar://problem/42627391 + +#include + +OBJC_EXPORT const char _objc_blockTrampolineImpl + OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0); + +OBJC_EXPORT const char _objc_blockTrampolineStart + OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0); + +OBJC_EXPORT const char _objc_blockTrampolineLast + OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0); + + +OBJC_EXPORT const char _objc_blockTrampolineImpl_stret +OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0) + OBJC_ARM64_UNAVAILABLE; + +OBJC_EXPORT const char _objc_blockTrampolineStart_stret +OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0) + OBJC_ARM64_UNAVAILABLE; + +OBJC_EXPORT const char _objc_blockTrampolineLast_stret +OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0) + OBJC_ARM64_UNAVAILABLE; + +#endif diff --git a/runtime/objc-block-trampolines.mm b/runtime/objc-block-trampolines.mm index ccde02a..9dea652 100644 --- a/runtime/objc-block-trampolines.mm +++ b/runtime/objc-block-trampolines.mm @@ -35,27 +35,161 @@ #include #include #include +#include -// symbols defined in assembly files -// Don't use the symbols directly; they're thumb-biased on some ARM archs. -#define TRAMP(tramp) \ - static inline __unused uintptr_t tramp(void) { \ - extern void *_##tramp; \ - return ((uintptr_t)&_##tramp) & ~1UL; \ - } -// Scalar return -TRAMP(a1a2_tramphead); // trampoline header code -TRAMP(a1a2_firsttramp); // first trampoline -TRAMP(a1a2_trampend); // after the last trampoline +// fixme C++ compilers don't implemement memory_order_consume efficiently. +// Use memory_order_relaxed and cross our fingers. +#define MEMORY_ORDER_CONSUME std::memory_order_relaxed -#if SUPPORT_STRET -// Struct return -TRAMP(a2a3_tramphead); -TRAMP(a2a3_firsttramp); -TRAMP(a2a3_trampend); +// 8 bytes of text and data per trampoline on all architectures. +#define SLOT_SIZE 8 + +// The trampolines are defined in assembly files in libobjc-trampolines.dylib. +// We can't link to libobjc-trampolines.dylib directly because +// for security reasons it isn't in the dyld shared cache. + +// Trampoline addresses are lazily looked up. +// All of them are hidden behind a single atomic pointer for lock-free init. + +#ifdef __PTRAUTH_INTRINSICS__ +# define TrampolinePtrauth __ptrauth(ptrauth_key_function_pointer, 1, 0x3af1) +#else +# define TrampolinePtrauth #endif +class TrampolinePointerWrapper { + struct TrampolinePointers { + class TrampolineAddress { + const void * TrampolinePtrauth storage; + + public: + TrampolineAddress(void *dylib, const char *name) { +#define PREFIX "_objc_blockTrampoline" + char symbol[strlen(PREFIX) + strlen(name) + 1]; + strcpy(symbol, PREFIX); + strcat(symbol, name); + // dlsym() from a text segment returns a signed pointer + // Authenticate it manually and let the compiler re-sign it. + storage = ptrauth_auth_data(dlsym(dylib, symbol), + ptrauth_key_function_pointer, 0); + if (!storage) { + _objc_fatal("couldn't dlsym %s", symbol); + } + } + + uintptr_t address() { + return (uintptr_t)(void*)storage; + } + }; + + TrampolineAddress impl; // trampoline header code + TrampolineAddress start; // first trampoline +#if DEBUG + // These symbols are only used in assertions. + // fixme might be able to move the assertions to libobjc-trampolines itself + TrampolineAddress last; // start of the last trampoline + // We don't use the address after the last trampoline because that + // address might be in a different section, and then dlsym() would not + // sign it as a function pointer. +# if SUPPORT_STRET + TrampolineAddress impl_stret; + TrampolineAddress start_stret; + TrampolineAddress last_stret; +# endif +#endif + + uintptr_t textSegment; + uintptr_t textSegmentSize; + + void check() { +#if DEBUG + ASSERT(impl.address() == textSegment + PAGE_MAX_SIZE); + ASSERT(impl.address() % PAGE_SIZE == 0); // not PAGE_MAX_SIZE + assert(impl.address() + PAGE_MAX_SIZE == + last.address() + SLOT_SIZE); + ASSERT(last.address()+8 < textSegment + textSegmentSize); + ASSERT((last.address() - start.address()) % SLOT_SIZE == 0); +# if SUPPORT_STRET + ASSERT(impl_stret.address() == textSegment + 2*PAGE_MAX_SIZE); + ASSERT(impl_stret.address() % PAGE_SIZE == 0); // not PAGE_MAX_SIZE + assert(impl_stret.address() + PAGE_MAX_SIZE == + last_stret.address() + SLOT_SIZE); + assert(start.address() - impl.address() == + start_stret.address() - impl_stret.address()); + assert(last_stret.address() + SLOT_SIZE < + textSegment + textSegmentSize); + assert((last_stret.address() - start_stret.address()) + % SLOT_SIZE == 0); +# endif +#endif + } + + + TrampolinePointers(void *dylib) + : impl(dylib, "Impl") + , start(dylib, "Start") +#if DEBUG + , last(dylib, "Last") +# if SUPPORT_STRET + , impl_stret(dylib, "Impl_stret") + , start_stret(dylib, "Start_stret") + , last_stret(dylib, "Last_stret") +# endif +#endif + { + const auto *mh = + dyld_image_header_containing_address((void *)impl.address()); + unsigned long size = 0; + textSegment = (uintptr_t) + getsegmentdata((headerType *)mh, "__TEXT", &size); + textSegmentSize = size; + + check(); + } + }; + + std::atomic trampolines{nil}; + + TrampolinePointers *get() { + return trampolines.load(MEMORY_ORDER_CONSUME); + } + +public: + void Initialize() { + if (get()) return; + + // This code may be called concurrently. + // In the worst case we perform extra dyld operations. + void *dylib = dlopen("/usr/lib/libobjc-trampolines.dylib", + RTLD_NOW | RTLD_LOCAL | RTLD_FIRST); + if (!dylib) { + _objc_fatal("couldn't dlopen libobjc-trampolines.dylib: %s", + dlerror()); + } + + auto t = new TrampolinePointers(dylib); + TrampolinePointers *old = nil; + if (! trampolines.compare_exchange_strong(old, t, memory_order_release)) + { + delete t; // Lost an initialization race. + } + } + + uintptr_t textSegment() { return get()->textSegment; } + uintptr_t textSegmentSize() { return get()->textSegmentSize; } + + // See comments below about PAGE_SIZE and PAGE_MAX_SIZE. + uintptr_t dataSize() { return PAGE_MAX_SIZE; } + + uintptr_t impl() { return get()->impl.address(); } + uintptr_t start() { return get()->start.address(); } +}; + +static TrampolinePointerWrapper Trampolines; + // argument mode identifier +// Some calculations assume that these modes are sequential starting from 0. +// This order must match the order of the trampoline's assembly code. typedef enum { ReturnValueInRegisterArgumentMode, #if SUPPORT_STRET @@ -65,34 +199,46 @@ typedef enum { ArgumentModeCount } ArgumentMode; - // We must take care with our data layout on architectures that support // multiple page sizes. // // The trampoline template in __TEXT is sized and aligned with PAGE_MAX_SIZE. // On some platforms this requires additional linker flags. // -// When we allocate a page pair, we use PAGE_MAX_SIZE size. +// When we allocate a page group, we use PAGE_MAX_SIZE size. // This allows trampoline code to find its data by subtracting PAGE_MAX_SIZE. // -// When we allocate a page pair, we use the process's page alignment. +// When we allocate a page group, we use the process's page alignment. // This simplifies allocation because we don't need to force greater than // default alignment when running with small pages, but it also means // the trampoline code MUST NOT look for its data by masking with PAGE_MAX_MASK. -struct TrampolineBlockPagePair +struct TrampolineBlockPageGroup { - TrampolineBlockPagePair *nextPagePair; // linked list of all pages - TrampolineBlockPagePair *nextAvailablePage; // linked list of pages with available slots - + TrampolineBlockPageGroup *nextPageGroup; // linked list of all pages + TrampolineBlockPageGroup *nextAvailablePage; // linked list of pages with available slots + uintptr_t nextAvailable; // index of next available slot, endIndex() if no more available + + const void * TrampolinePtrauth const text; // text VM region; stored only for the benefit of the leaks tool + + TrampolineBlockPageGroup() + : nextPageGroup(nil) + , nextAvailablePage(nil) + , nextAvailable(startIndex()) + , text((const void *)((uintptr_t)this + Trampolines.dataSize())) + { } // Payload data: block pointers and free list. // Bytes parallel with trampoline header code are the fields above or unused - // uint8_t blocks[ PAGE_MAX_SIZE - sizeof(TrampolineBlockPagePair) ] - - // Code: trampoline header followed by trampolines. - // uint8_t trampolines[PAGE_MAX_SIZE]; + // uint8_t payloads[PAGE_MAX_SIZE - sizeof(TrampolineBlockPageGroup)] + + // Code: Mach-O header, then trampoline header followed by trampolines. + // On platforms with struct return we have non-stret trampolines and + // stret trampolines. The stret and non-stret trampolines at a given + // index share the same data page. + // uint8_t macho[PAGE_MAX_SIZE]; + // uint8_t trampolines[ArgumentModeCount][PAGE_MAX_SIZE]; // Per-trampoline block data format: // initial value is 0 while page data is filled sequentially @@ -105,11 +251,11 @@ struct TrampolineBlockPagePair }; static uintptr_t headerSize() { - return (uintptr_t) (a1a2_firsttramp() - a1a2_tramphead()); + return (uintptr_t) (Trampolines.start() - Trampolines.impl()); } static uintptr_t slotSize() { - return 8; + return SLOT_SIZE; } static uintptr_t startIndex() { @@ -118,7 +264,7 @@ struct TrampolineBlockPagePair } static uintptr_t endIndex() { - return (uintptr_t)PAGE_MAX_SIZE / slotSize(); + return (uintptr_t)Trampolines.dataSize() / slotSize(); } static bool validIndex(uintptr_t index) { @@ -126,208 +272,161 @@ struct TrampolineBlockPagePair } Payload *payload(uintptr_t index) { - assert(validIndex(index)); + ASSERT(validIndex(index)); return (Payload *)((char *)this + index*slotSize()); } - IMP trampoline(uintptr_t index) { - assert(validIndex(index)); - char *imp = (char *)this + index*slotSize() + PAGE_MAX_SIZE; + uintptr_t trampolinesForMode(int aMode) { + // Skip over the data area, one page of Mach-O headers, + // and one text page for each mode before this one. + return (uintptr_t)this + Trampolines.dataSize() + + PAGE_MAX_SIZE * (1 + aMode); + } + + IMP trampoline(int aMode, uintptr_t index) { + ASSERT(validIndex(index)); + char *base = (char *)trampolinesForMode(aMode); + char *imp = base + index*slotSize(); #if __arm__ imp++; // trampoline is Thumb instructions +#endif +#if __has_feature(ptrauth_calls) + imp = ptrauth_sign_unauthenticated(imp, + ptrauth_key_function_pointer, 0); #endif return (IMP)imp; } - uintptr_t indexForTrampoline(IMP tramp) { - uintptr_t tramp0 = (uintptr_t)this + PAGE_MAX_SIZE; - uintptr_t start = tramp0 + headerSize(); - uintptr_t end = tramp0 + PAGE_MAX_SIZE; - uintptr_t address = (uintptr_t)tramp; - if (address >= start && address < end) { - return (uintptr_t)(address - tramp0) / slotSize(); + uintptr_t indexForTrampoline(uintptr_t tramp) { + for (int aMode = 0; aMode < ArgumentModeCount; aMode++) { + uintptr_t base = trampolinesForMode(aMode); + uintptr_t start = base + startIndex() * slotSize(); + uintptr_t end = base + endIndex() * slotSize(); + if (tramp >= start && tramp < end) { + return (uintptr_t)(tramp - base) / slotSize(); + } } return 0; } static void check() { - assert(TrampolineBlockPagePair::slotSize() == 8); - assert(TrampolineBlockPagePair::headerSize() >= sizeof(TrampolineBlockPagePair)); - assert(TrampolineBlockPagePair::headerSize() % TrampolineBlockPagePair::slotSize() == 0); - - // _objc_inform("%p %p %p", a1a2_tramphead(), a1a2_firsttramp(), - // a1a2_trampend()); - assert(a1a2_tramphead() % PAGE_SIZE == 0); // not PAGE_MAX_SIZE - assert(a1a2_tramphead() + PAGE_MAX_SIZE == a1a2_trampend()); -#if SUPPORT_STRET - // _objc_inform("%p %p %p", a2a3_tramphead(), a2a3_firsttramp(), - // a2a3_trampend()); - assert(a2a3_tramphead() % PAGE_SIZE == 0); // not PAGE_MAX_SIZE - assert(a2a3_tramphead() + PAGE_MAX_SIZE == a2a3_trampend()); -#endif - -#if __arm__ - // make sure trampolines are Thumb - extern void *_a1a2_firsttramp; - extern void *_a2a3_firsttramp; - assert(((uintptr_t)&_a1a2_firsttramp) % 2 == 1); - assert(((uintptr_t)&_a2a3_firsttramp) % 2 == 1); -#endif + ASSERT(TrampolineBlockPageGroup::headerSize() >= sizeof(TrampolineBlockPageGroup)); + ASSERT(TrampolineBlockPageGroup::headerSize() % TrampolineBlockPageGroup::slotSize() == 0); } }; -// two sets of trampoline pages; one for stack returns and one for register returns -static TrampolineBlockPagePair *headPagePairs[ArgumentModeCount]; +static TrampolineBlockPageGroup *HeadPageGroup; #pragma mark Utility Functions -static inline void _lock() { -#if __OBJC2__ - runtimeLock.write(); -#else - classLock.lock(); +#if !__OBJC2__ +#define runtimeLock classLock #endif -} - -static inline void _unlock() { -#if __OBJC2__ - runtimeLock.unlockWrite(); -#else - classLock.unlock(); -#endif -} - -static inline void _assert_locked() { -#if __OBJC2__ - runtimeLock.assertWriting(); -#else - classLock.assertLocked(); -#endif -} #pragma mark Trampoline Management Functions -static TrampolineBlockPagePair *_allocateTrampolinesAndData(ArgumentMode aMode) +static TrampolineBlockPageGroup *_allocateTrampolinesAndData() { - _assert_locked(); + runtimeLock.assertLocked(); vm_address_t dataAddress; - TrampolineBlockPagePair::check(); + TrampolineBlockPageGroup::check(); - TrampolineBlockPagePair *headPagePair = headPagePairs[aMode]; - - if (headPagePair) { - assert(headPagePair->nextAvailablePage == nil); - } - + // Our final mapping will look roughly like this: + // r/w data + // r/o text mapped from libobjc-trampolines.dylib + // with fixed offsets from the text to the data embedded in the text. + // + // More precisely it will look like this: + // 1 page r/w data + // 1 page libobjc-trampolines.dylib Mach-O header + // N pages trampoline code, one for each ArgumentMode + // M pages for the rest of libobjc-trampolines' TEXT segment. + // The kernel requires that we remap the entire TEXT segment every time. + // We assume that our code begins on the second TEXT page, but are robust + // against other additions to the end of the TEXT segment. + + ASSERT(HeadPageGroup == nil || HeadPageGroup->nextAvailablePage == nil); + + auto textSource = Trampolines.textSegment(); + auto textSourceSize = Trampolines.textSegmentSize(); + auto dataSize = Trampolines.dataSize(); + + // Allocate a single contiguous region big enough to hold data+text. kern_return_t result; - for (int i = 0; i < 5; i++) { - result = vm_allocate(mach_task_self(), &dataAddress, - PAGE_MAX_SIZE * 2, - TRUE | VM_MAKE_TAG(VM_MEMORY_FOUNDATION)); - if (result != KERN_SUCCESS) { - mach_error("vm_allocate failed", result); - return nil; - } - - vm_address_t codeAddress = dataAddress + PAGE_MAX_SIZE; - result = vm_deallocate(mach_task_self(), codeAddress, PAGE_MAX_SIZE); - if (result != KERN_SUCCESS) { - mach_error("vm_deallocate failed", result); - return nil; - } - - uintptr_t codePage; - switch(aMode) { - case ReturnValueInRegisterArgumentMode: - codePage = a1a2_tramphead(); - break; -#if SUPPORT_STRET - case ReturnValueOnStackArgumentMode: - codePage = a2a3_tramphead(); - break; -#endif - default: - _objc_fatal("unknown return mode %d", (int)aMode); - break; - } - vm_prot_t currentProtection, maxProtection; - result = vm_remap(mach_task_self(), &codeAddress, PAGE_MAX_SIZE, - 0, FALSE, mach_task_self(), codePage, TRUE, - ¤tProtection, &maxProtection, VM_INHERIT_SHARE); - if (result != KERN_SUCCESS) { - result = vm_deallocate(mach_task_self(), - dataAddress, PAGE_MAX_SIZE); - if (result != KERN_SUCCESS) { - mach_error("vm_deallocate for retry failed.", result); - return nil; - } - } else { - break; - } - } - + result = vm_allocate(mach_task_self(), &dataAddress, + dataSize + textSourceSize, + VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_MEMORY_FOUNDATION)); if (result != KERN_SUCCESS) { - return nil; + _objc_fatal("vm_allocate trampolines failed (%d)", result); } + + // Remap libobjc-trampolines' TEXT segment atop all + // but the first of the pages we just allocated: + vm_address_t textDest = dataAddress + dataSize; + vm_prot_t currentProtection, maxProtection; + result = vm_remap(mach_task_self(), &textDest, + textSourceSize, + 0, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, + mach_task_self(), textSource, TRUE, + ¤tProtection, &maxProtection, VM_INHERIT_SHARE); + if (result != KERN_SUCCESS) { + _objc_fatal("vm_remap trampolines failed (%d)", result); + } + + auto *pageGroup = new ((void*)dataAddress) TrampolineBlockPageGroup; - TrampolineBlockPagePair *pagePair = (TrampolineBlockPagePair *) dataAddress; - pagePair->nextAvailable = pagePair->startIndex(); - pagePair->nextPagePair = nil; - pagePair->nextAvailablePage = nil; - - if (headPagePair) { - TrampolineBlockPagePair *lastPagePair = headPagePair; - while(lastPagePair->nextPagePair) - lastPagePair = lastPagePair->nextPagePair; - - lastPagePair->nextPagePair = pagePair; - headPagePairs[aMode]->nextAvailablePage = pagePair; + if (HeadPageGroup) { + TrampolineBlockPageGroup *lastPageGroup = HeadPageGroup; + while(lastPageGroup->nextPageGroup) { + lastPageGroup = lastPageGroup->nextPageGroup; + } + lastPageGroup->nextPageGroup = pageGroup; + HeadPageGroup->nextAvailablePage = pageGroup; } else { - headPagePairs[aMode] = pagePair; + HeadPageGroup = pageGroup; } - return pagePair; + return pageGroup; } -static TrampolineBlockPagePair * -_getOrAllocatePagePairWithNextAvailable(ArgumentMode aMode) +static TrampolineBlockPageGroup * +getOrAllocatePageGroupWithNextAvailable() { - _assert_locked(); + runtimeLock.assertLocked(); - TrampolineBlockPagePair *headPagePair = headPagePairs[aMode]; - - if (!headPagePair) - return _allocateTrampolinesAndData(aMode); + if (!HeadPageGroup) + return _allocateTrampolinesAndData(); // make sure head page is filled first - if (headPagePair->nextAvailable != headPagePair->endIndex()) - return headPagePair; + if (HeadPageGroup->nextAvailable != HeadPageGroup->endIndex()) + return HeadPageGroup; - if (headPagePair->nextAvailablePage) // check if there is a page w/a hole - return headPagePair->nextAvailablePage; + if (HeadPageGroup->nextAvailablePage) // check if there is a page w/a hole + return HeadPageGroup->nextAvailablePage; - return _allocateTrampolinesAndData(aMode); // tack on a new one + return _allocateTrampolinesAndData(); // tack on a new one } -static TrampolineBlockPagePair * -_pageAndIndexContainingIMP(IMP anImp, uintptr_t *outIndex, - TrampolineBlockPagePair **outHeadPagePair) +static TrampolineBlockPageGroup * +pageAndIndexContainingIMP(IMP anImp, uintptr_t *outIndex) { - _assert_locked(); + runtimeLock.assertLocked(); - for (int arg = 0; arg < ArgumentModeCount; arg++) { - for (TrampolineBlockPagePair *pagePair = headPagePairs[arg]; - pagePair; - pagePair = pagePair->nextPagePair) - { - uintptr_t index = pagePair->indexForTrampoline(anImp); - if (index) { - if (outIndex) *outIndex = index; - if (outHeadPagePair) *outHeadPagePair = headPagePairs[arg]; - return pagePair; - } + // Authenticate as a function pointer, returning an un-signed address. + uintptr_t trampAddress = + (uintptr_t)ptrauth_auth_data((const char *)anImp, + ptrauth_key_function_pointer, 0); + + for (TrampolineBlockPageGroup *pageGroup = HeadPageGroup; + pageGroup; + pageGroup = pageGroup->nextPageGroup) + { + uintptr_t index = pageGroup->indexForTrampoline(trampAddress); + if (index) { + if (outIndex) *outIndex = index; + return pageGroup; } } @@ -336,7 +435,7 @@ _pageAndIndexContainingIMP(IMP anImp, uintptr_t *outIndex, static ArgumentMode -_argumentModeForBlock(id block) +argumentModeForBlock(id block) { ArgumentMode aMode = ReturnValueInRegisterArgumentMode; @@ -344,29 +443,50 @@ _argumentModeForBlock(id block) if (_Block_has_signature(block) && _Block_use_stret(block)) aMode = ReturnValueOnStackArgumentMode; #else - assert(! (_Block_has_signature(block) && _Block_use_stret(block))); + ASSERT(! (_Block_has_signature(block) && _Block_use_stret(block))); #endif return aMode; } +/// Initialize the trampoline machinery. Normally this does nothing, as +/// everything is initialized lazily, but for certain processes we eagerly load +/// the trampolines dylib. +void +_imp_implementationWithBlock_init(void) +{ +#if TARGET_OS_OSX + // Eagerly load libobjc-trampolines.dylib in certain processes. Some + // programs (most notably QtWebEngineProcess used by older versions of + // embedded Chromium) enable a highly restrictive sandbox profile which + // blocks access to that dylib. If anything calls + // imp_implementationWithBlock (as AppKit has started doing) then we'll + // crash trying to load it. Loading it here sets it up before the sandbox + // profile is enabled and blocks it. + // + // This fixes EA Origin (rdar://problem/50813789) + // and Steam (rdar://problem/55286131) + if (__progname && + (strcmp(__progname, "QtWebEngineProcess") == 0 || + strcmp(__progname, "Steam Helper") == 0)) { + Trampolines.Initialize(); + } +#endif +} + // `block` must already have been copied IMP _imp_implementationWithBlockNoCopy(id block) { - _assert_locked(); + runtimeLock.assertLocked(); - ArgumentMode aMode = _argumentModeForBlock(block); + TrampolineBlockPageGroup *pageGroup = + getOrAllocatePageGroupWithNextAvailable(); - TrampolineBlockPagePair *pagePair = - _getOrAllocatePagePairWithNextAvailable(aMode); - if (!headPagePairs[aMode]) - headPagePairs[aMode] = pagePair; - - uintptr_t index = pagePair->nextAvailable; - assert(index >= pagePair->startIndex() && index < pagePair->endIndex()); - TrampolineBlockPagePair::Payload *payload = pagePair->payload(index); + uintptr_t index = pageGroup->nextAvailable; + ASSERT(index >= pageGroup->startIndex() && index < pageGroup->endIndex()); + TrampolineBlockPageGroup::Payload *payload = pageGroup->payload(index); uintptr_t nextAvailableIndex = payload->nextAvailable; if (nextAvailableIndex == 0) { @@ -374,104 +494,109 @@ _imp_implementationWithBlockNoCopy(id block) // If the page is now full this will now be endIndex(), handled below. nextAvailableIndex = index + 1; } - pagePair->nextAvailable = nextAvailableIndex; - if (nextAvailableIndex == pagePair->endIndex()) { - // PagePair is now full (free list or wilderness exhausted) + pageGroup->nextAvailable = nextAvailableIndex; + if (nextAvailableIndex == pageGroup->endIndex()) { + // PageGroup is now full (free list or wilderness exhausted) // Remove from available page linked list - TrampolineBlockPagePair *iterator = headPagePairs[aMode]; - while(iterator && (iterator->nextAvailablePage != pagePair)) { + TrampolineBlockPageGroup *iterator = HeadPageGroup; + while(iterator && (iterator->nextAvailablePage != pageGroup)) { iterator = iterator->nextAvailablePage; } if (iterator) { - iterator->nextAvailablePage = pagePair->nextAvailablePage; - pagePair->nextAvailablePage = nil; + iterator->nextAvailablePage = pageGroup->nextAvailablePage; + pageGroup->nextAvailablePage = nil; } } payload->block = block; - return pagePair->trampoline(index); + return pageGroup->trampoline(argumentModeForBlock(block), index); } #pragma mark Public API IMP imp_implementationWithBlock(id block) { + // Block object must be copied outside runtimeLock + // because it performs arbitrary work. block = Block_copy(block); - _lock(); - IMP returnIMP = _imp_implementationWithBlockNoCopy(block); - _unlock(); - return returnIMP; + + // Trampolines must be initialized outside runtimeLock + // because it calls dlopen(). + Trampolines.Initialize(); + + mutex_locker_t lock(runtimeLock); + + return _imp_implementationWithBlockNoCopy(block); } id imp_getBlock(IMP anImp) { uintptr_t index; - TrampolineBlockPagePair *pagePair; + TrampolineBlockPageGroup *pageGroup; if (!anImp) return nil; - _lock(); + mutex_locker_t lock(runtimeLock); - pagePair = _pageAndIndexContainingIMP(anImp, &index, nil); + pageGroup = pageAndIndexContainingIMP(anImp, &index); - if (!pagePair) { - _unlock(); + if (!pageGroup) { return nil; } - TrampolineBlockPagePair::Payload *payload = pagePair->payload(index); + TrampolineBlockPageGroup::Payload *payload = pageGroup->payload(index); - if (payload->nextAvailable <= TrampolineBlockPagePair::endIndex()) { + if (payload->nextAvailable <= TrampolineBlockPageGroup::endIndex()) { // unallocated - _unlock(); return nil; } - _unlock(); - return payload->block; } BOOL imp_removeBlock(IMP anImp) { - TrampolineBlockPagePair *pagePair; - TrampolineBlockPagePair *headPagePair; - uintptr_t index; if (!anImp) return NO; + + id block; - _lock(); - pagePair = _pageAndIndexContainingIMP(anImp, &index, &headPagePair); + { + mutex_locker_t lock(runtimeLock); - if (!pagePair) { - _unlock(); - return NO; + uintptr_t index; + TrampolineBlockPageGroup *pageGroup = + pageAndIndexContainingIMP(anImp, &index); + + if (!pageGroup) { + return NO; + } + + TrampolineBlockPageGroup::Payload *payload = pageGroup->payload(index); + block = payload->block; + // block is released below, outside the lock + + payload->nextAvailable = pageGroup->nextAvailable; + pageGroup->nextAvailable = index; + + // make sure this page is on available linked list + TrampolineBlockPageGroup *pageGroupIterator = HeadPageGroup; + + // see if page is the next available page for any existing pages + while (pageGroupIterator->nextAvailablePage && + pageGroupIterator->nextAvailablePage != pageGroup) + { + pageGroupIterator = pageGroupIterator->nextAvailablePage; + } + + if (! pageGroupIterator->nextAvailablePage) { + // if iteration stopped because nextAvail was nil + // add to end of list. + pageGroupIterator->nextAvailablePage = pageGroup; + pageGroup->nextAvailablePage = nil; + } } - TrampolineBlockPagePair::Payload *payload = pagePair->payload(index); - id block = payload->block; - // block is released below - - payload->nextAvailable = pagePair->nextAvailable; - pagePair->nextAvailable = index; - - // make sure this page is on available linked list - TrampolineBlockPagePair *pagePairIterator = headPagePair; - - // see if page is the next available page for any existing pages - while (pagePairIterator->nextAvailablePage && - pagePairIterator->nextAvailablePage != pagePair) - { - pagePairIterator = pagePairIterator->nextAvailablePage; - } - - if (! pagePairIterator->nextAvailablePage) { - // if iteration stopped because nextAvail was nil - // add to end of list. - pagePairIterator->nextAvailablePage = pagePair; - pagePair->nextAvailablePage = nil; - } - - _unlock(); + // do this AFTER dropping the lock Block_release(block); return YES; } diff --git a/runtime/objc-blocktramps-arm.S b/runtime/objc-blocktramps-arm.S new file mode 100644 index 0000000..de80a43 --- /dev/null +++ b/runtime/objc-blocktramps-arm.S @@ -0,0 +1,282 @@ +#if __arm__ + +#include +#include + +.syntax unified + +.text +.globl __objc_blockTrampolineImpl +.globl __objc_blockTrampolineStart +.globl __objc_blockTrampolineLast + +// Trampoline machinery assumes the trampolines are Thumb function pointers +#if !__thumb2__ +# error sorry +#endif + +.thumb + +// Exported symbols are not marked as functions. +// The trampoline construction code assumes that the Thumb bit is not set. +.thumb_func L__objc_blockTrampolineImpl_func + +.align PAGE_MAX_SHIFT +__objc_blockTrampolineImpl: +L__objc_blockTrampolineImpl_func: + /* + r0 == self + r12 == pc of trampoline's first instruction + PC bias + lr == original return address + */ + + mov r1, r0 // _cmd = self + + // Trampoline's data is two pages before the trampoline text. + // Also correct PC bias of 4 bytes. + sub r12, # 2*PAGE_MAX_SIZE + ldr r0, [r12, #-4] // self = block object + ldr pc, [r0, #12] // tail call block->invoke + // not reached + +.macro TrampolineEntry + mov r12, pc + b L__objc_blockTrampolineImpl_func +.align 3 +.endmacro + +.macro TrampolineEntryX16 + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry +.endmacro + +.macro TrampolineEntryX256 + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 +.endmacro + +.align 5 +__objc_blockTrampolineStart: + // 2048-4 trampolines to fill 16K page + TrampolineEntryX256 + TrampolineEntryX256 + TrampolineEntryX256 + TrampolineEntryX256 + + TrampolineEntryX256 + TrampolineEntryX256 + TrampolineEntryX256 + + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + + TrampolineEntryX16 + TrampolineEntryX16 + TrampolineEntryX16 + + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + + TrampolineEntry + TrampolineEntry + TrampolineEntry +__objc_blockTrampolineLast: + TrampolineEntry + + // TrampolineEntry + // TrampolineEntry + // TrampolineEntry + // TrampolineEntry + + + +.text +.globl __objc_blockTrampolineImpl_stret +.globl __objc_blockTrampolineStart_stret +.globl __objc_blockTrampolineLast_stret + +// Trampoline machinery assumes the trampolines are Thumb function pointers +#if !__thumb2__ +# error sorry +#endif + +.thumb + +// Exported symbols are not marked as functions. +// The trampoline construction code assumes that the Thumb bit is not set. +.thumb_func L__objc_blockTrampolineImpl_stret_func + +.align PAGE_MAX_SHIFT +__objc_blockTrampolineImpl_stret: +L__objc_blockTrampolineImpl_stret_func: + /* + r1 == self + r12 == pc of trampoline's first instruction + PC bias + lr == original return address + */ + + mov r2, r1 // _cmd = self + + // Trampoline's data is three pages before the trampoline text. + // Also correct PC bias of 4 bytes. + sub r12, # 3*PAGE_MAX_SIZE + ldr r1, [r12, #-4] // self = block object + ldr pc, [r1, #12] // tail call block->invoke + // not reached + +.macro TrampolineEntry_stret + mov r12, pc + b L__objc_blockTrampolineImpl_stret_func +.align 3 +.endmacro + +.macro TrampolineEntryX16_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret +.endmacro + +.macro TrampolineEntryX256_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret +.endmacro + +.align 5 +__objc_blockTrampolineStart_stret: + // 2048-4 trampolines to fill 16K page + TrampolineEntryX256_stret + TrampolineEntryX256_stret + TrampolineEntryX256_stret + TrampolineEntryX256_stret + + TrampolineEntryX256_stret + TrampolineEntryX256_stret + TrampolineEntryX256_stret + + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + + TrampolineEntryX16_stret + TrampolineEntryX16_stret + TrampolineEntryX16_stret + + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret +__objc_blockTrampolineLast_stret: + TrampolineEntry_stret + + // TrampolineEntry_stret + // TrampolineEntry_stret + // TrampolineEntry_stret + // TrampolineEntry_stret + +#endif diff --git a/runtime/a1a2-blocktramps-arm64.S b/runtime/objc-blocktramps-arm64.S similarity index 66% rename from runtime/a1a2-blocktramps-arm64.S rename to runtime/objc-blocktramps-arm64.S index 139df99..d7e32a5 100644 --- a/runtime/a1a2-blocktramps-arm64.S +++ b/runtime/objc-blocktramps-arm64.S @@ -1,35 +1,46 @@ #if __arm64__ #include +#include "arm64-asm.h" + +// Offset of block->invoke field. +#if __LP64__ + // true arm64 +# define BLOCK_INVOKE 16 +#else + // arm64_32 +# define BLOCK_INVOKE 12 +#endif .text - - .private_extern __a1a2_tramphead - .private_extern __a1a2_firsttramp - .private_extern __a1a2_trampend +.globl __objc_blockTrampolineImpl +.globl __objc_blockTrampolineStart +.globl __objc_blockTrampolineLast .align PAGE_MAX_SHIFT -__a1a2_tramphead: -L_a1a2_tramphead: +__objc_blockTrampolineImpl: +L_objc_blockTrampolineImpl: /* x0 == self - x17 == address of called trampoline's data (1 page before its code) + x17 == address of called trampoline's data (2 pages before its code) lr == original return address */ mov x1, x0 // _cmd = self - ldr x0, [x17] // self = block object - ldr x16, [x0, #16] // tail call block->invoke - br x16 + ldr p0, [x17] // self = block object + add p15, p0, #BLOCK_INVOKE // x15 = &block->invoke + ldr p16, [x15] // x16 = block->invoke + TailCallBlockInvoke x16, x15 // pad up to TrampolineBlockPagePair header size nop nop + nop .macro TrampolineEntry - // load address of trampoline data (one page before this instruction) - adr x17, -PAGE_MAX_SIZE - b L_a1a2_tramphead + // load address of trampoline data (two pages before this instruction) + adr x17, -2*PAGE_MAX_SIZE + b L_objc_blockTrampolineImpl .endmacro .macro TrampolineEntryX16 @@ -77,9 +88,8 @@ L_a1a2_tramphead: .endmacro .align 3 -.private_extern __a1a2_firsttramp -__a1a2_firsttramp: - // 2048-3 trampolines to fill 16K page +__objc_blockTrampolineStart: + // 2048-4 trampolines to fill 16K page TrampolineEntryX256 TrampolineEntryX256 TrampolineEntryX256 @@ -121,14 +131,12 @@ __a1a2_firsttramp: TrampolineEntry TrampolineEntry TrampolineEntry +__objc_blockTrampolineLast: TrampolineEntry - TrampolineEntry // TrampolineEntry // TrampolineEntry // TrampolineEntry - -.private_extern __a1a2_trampend -__a1a2_trampend: + // TrampolineEntry #endif diff --git a/runtime/objc-blocktramps-i386.S b/runtime/objc-blocktramps-i386.S new file mode 100644 index 0000000..d4f1eb8 --- /dev/null +++ b/runtime/objc-blocktramps-i386.S @@ -0,0 +1,1102 @@ +/* + * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +#ifdef __i386__ + +#include + +.text +.globl __objc_blockTrampolineImpl +.globl __objc_blockTrampolineStart +.globl __objc_blockTrampolineLast + +.align PAGE_SHIFT +__objc_blockTrampolineImpl: + movl (%esp), %eax // return address pushed by trampoline + // 4(%esp) is return address pushed by the call site + movl 8(%esp), %ecx // self -> ecx + movl %ecx, 12(%esp) // ecx -> _cmd + movl -2*PAGE_SIZE-5(%eax), %ecx // block object pointer -> ecx + // trampoline is -5 bytes from the return address + // data is -2 pages from the trampoline + movl %ecx, 8(%esp) // ecx -> self + ret // back to TrampolineEntry to preserve CPU's return stack + +.macro TrampolineEntry + // This trampoline is 8 bytes long. + // This callq is 5 bytes long. + calll __objc_blockTrampolineImpl + jmp *12(%ecx) // tail call block->invoke +.endmacro + +.align 5 +__objc_blockTrampolineStart: + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry +__objc_blockTrampolineLast: + TrampolineEntry + + +.text +.globl __objc_blockTrampolineImpl_stret +.globl __objc_blockTrampolineStart_stret +.globl __objc_blockTrampolineLast_stret + +.align PAGE_SHIFT +__objc_blockTrampolineImpl_stret: + movl (%esp), %eax // return address pushed by trampoline + // 4(%esp) is return address pushed by the call site + // 8(%esp) is struct-return address + movl 12(%esp), %ecx // self -> ecx + movl %ecx, 16(%esp) // ecx -> _cmd + movl -3*PAGE_SIZE-5(%eax), %ecx // block object pointer -> ecx + // trampoline is -5 bytes from the return address + // data is -3 pages from the trampoline + movl %ecx, 12(%esp) // ecx -> self + ret + +.macro TrampolineEntry_stret + // This trampoline is 8 bytes long. + // This callq is 5 bytes long. + call __objc_blockTrampolineImpl_stret + jmp *12(%ecx) // tail to block->invoke +.endmacro + +.align 5 +__objc_blockTrampolineStart_stret: + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret +__objc_blockTrampolineLast_stret: + TrampolineEntry_stret + +#endif diff --git a/runtime/objc-blocktramps-x86_64.S b/runtime/objc-blocktramps-x86_64.S new file mode 100644 index 0000000..5f377f0 --- /dev/null +++ b/runtime/objc-blocktramps-x86_64.S @@ -0,0 +1,1097 @@ +/* + * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +#ifdef __x86_64__ + +#include + +.text +.globl __objc_blockTrampolineImpl +.globl __objc_blockTrampolineStart +.globl __objc_blockTrampolineLast + +.align PAGE_SHIFT +__objc_blockTrampolineImpl: + movq (%rsp), %r10 // read return address pushed by TrampolineEntry's callq + movq %rdi, %rsi // arg1 -> arg2 + movq -2*PAGE_SIZE-5(%r10), %rdi // block object pointer -> arg1 + // trampoline is -5 bytes from the return address + // data is -2 pages from the trampoline + ret // back to TrampolineEntry to preserve CPU's return stack + +.macro TrampolineEntry + // This trampoline is 8 bytes long. + // This callq is 5 bytes long. + callq __objc_blockTrampolineImpl + jmp *16(%rdi) +.endmacro + +.align 5 +__objc_blockTrampolineStart: + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry + TrampolineEntry +__objc_blockTrampolineLast: + TrampolineEntry + + +.text +.globl __objc_blockTrampolineImpl_stret +.globl __objc_blockTrampolineStart_stret +.globl __objc_blockTrampolineLast_stret + +.align PAGE_SHIFT +__objc_blockTrampolineImpl_stret: + + // %rdi -- arg1 -- is address of return value's space. Don't mess with it. + movq (%rsp), %r10 // read return address pushed by TrampolineEntry's callq + movq %rsi, %rdx // arg2 -> arg3 + movq -3*PAGE_SIZE-5(%r10), %rsi // block object pointer -> arg2 + // trampoline is -5 bytes from the return address + // data is -3 pages from the trampoline + ret // back to TrampolineEntry to preserve CPU's return stack + +.macro TrampolineEntry_stret + // This trampoline is 8 bytes long. + // This callq is 5 bytes long. + callq __objc_blockTrampolineImpl_stret + jmp *16(%rsi) +.endmacro + +.align 5 +__objc_blockTrampolineStart_stret: + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret + TrampolineEntry_stret +__objc_blockTrampolineLast_stret: + TrampolineEntry_stret + +#endif diff --git a/runtime/objc-cache-old.h b/runtime/objc-cache-old.h index 7f0245e..995e2e3 100644 --- a/runtime/objc-cache-old.h +++ b/runtime/objc-cache-old.h @@ -31,6 +31,7 @@ __BEGIN_DECLS extern IMP _cache_getImp(Class cls, SEL sel); extern Method _cache_getMethod(Class cls, SEL sel, IMP objc_msgForward_internal_imp); +extern void cache_init(void); extern void flush_cache(Class cls); extern bool _cache_fill(Class cls, Method meth, SEL sel); extern void _cache_addForwardEntry(Class cls, SEL sel); diff --git a/runtime/objc-cache-old.mm b/runtime/objc-cache-old.mm index 12baf36..fed884a 100644 --- a/runtime/objc-cache-old.mm +++ b/runtime/objc-cache-old.mm @@ -693,8 +693,14 @@ static uintptr_t _get_pc_for_thread(thread_t thread) * reading function is in progress because it might still be using * the garbage memory. **********************************************************************/ -OBJC_EXPORT uintptr_t objc_entryPoints[]; -OBJC_EXPORT uintptr_t objc_exitPoints[]; +typedef struct { + uint64_t location; + unsigned short length; + unsigned short recovery_offs; + unsigned int flags; +} task_restartable_range_t; + +extern "C" task_restartable_range_t objc_restartableRanges[]; static int _collecting_in_critical(void) { @@ -707,7 +713,7 @@ static int _collecting_in_critical(void) kern_return_t ret; int result; - mach_port_t mythread = pthread_mach_thread_np(pthread_self()); + mach_port_t mythread = pthread_mach_thread_np(objc_thread_self()); // Get a list of all the threads in the current task ret = task_threads (mach_task_self (), &threads, &number); @@ -738,10 +744,11 @@ static int _collecting_in_critical(void) } // Check whether it is in the cache lookup code - for (region = 0; objc_entryPoints[region] != 0; region++) + for (region = 0; objc_restartableRanges[region].location != 0; region++) { - if ((pc >= objc_entryPoints[region]) && - (pc <= objc_exitPoints[region])) + uint32_t loc = (uint32_t)objc_restartableRanges[region].location; + if ((pc > loc) && + (pc - loc) < objc_restartableRanges[region].length) { result = TRUE; goto done; @@ -1788,6 +1795,9 @@ void _class_printMethodCacheStatistics(void) #endif +void cache_init() +{ +} // !__OBJC2__ #endif diff --git a/runtime/objc-cache.h b/runtime/objc-cache.h index de348a0..e0448e7 100644 --- a/runtime/objc-cache.h +++ b/runtime/objc-cache.h @@ -6,6 +6,8 @@ __BEGIN_DECLS +extern void cache_init(void); + extern IMP cache_getImp(Class cls, SEL sel); extern void cache_fill(Class cls, SEL sel, IMP imp, id receiver); diff --git a/runtime/objc-cache.mm b/runtime/objc-cache.mm index a0c6545..4e6ca11 100644 --- a/runtime/objc-cache.mm +++ b/runtime/objc-cache.mm @@ -90,7 +90,9 @@ /* Initial cache bucket count. INIT_CACHE_SIZE must be a power of two. */ enum { INIT_CACHE_SIZE_LOG2 = 2, - INIT_CACHE_SIZE = (1 << INIT_CACHE_SIZE_LOG2) + INIT_CACHE_SIZE = (1 << INIT_CACHE_SIZE_LOG2), + MAX_CACHE_SIZE_LOG2 = 16, + MAX_CACHE_SIZE = (1 << MAX_CACHE_SIZE_LOG2), }; static void cache_collect_free(struct bucket_t *data, mask_t capacity); @@ -147,8 +149,14 @@ asm("\n .section __TEXT,__const" "\n .globl __objc_empty_vtable" "\n .set __objc_empty_vtable, 0" "\n .globl __objc_empty_cache" +#if CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_LOW_4 + "\n .align 4" + "\n L__objc_empty_cache: .space " stringize2(EMPTY_BYTES) + "\n .set __objc_empty_cache, L__objc_empty_cache + 0xf" +#else "\n .align 3" "\n __objc_empty_cache: .space " stringize2(EMPTY_BYTES) +#endif ); @@ -173,111 +181,133 @@ static inline mask_t cache_next(mask_t i, mask_t mask) { #endif -// copied from dispatch_atomic_maximally_synchronizing_barrier -// fixme verify that this barrier hack does in fact work here -#if __x86_64__ -#define mega_barrier() \ - do { unsigned long _clbr; __asm__ __volatile__( \ - "cpuid" \ - : "=a" (_clbr) : "0" (0) : "rbx", "rcx", "rdx", "cc", "memory" \ - ); } while(0) - -#elif __i386__ -#define mega_barrier() \ - do { unsigned long _clbr; __asm__ __volatile__( \ - "cpuid" \ - : "=a" (_clbr) : "0" (0) : "ebx", "ecx", "edx", "cc", "memory" \ - ); } while(0) - -#elif __arm__ || __arm64__ +// mega_barrier doesn't really work, but it works enough on ARM that +// we leave well enough alone and keep using it there. +#if __arm__ #define mega_barrier() \ __asm__ __volatile__( \ "dsb ish" \ : : : "memory") -#else -#error unknown architecture #endif #if __arm64__ +// Pointer-size register prefix for inline asm +# if __LP64__ +# define p "x" // true arm64 +# else +# define p "w" // arm64_32 +# endif + // Use atomic double-word instructions to update cache entries. // This requires cache buckets not cross cache line boundaries. -#define stp(onep, twop, destp) \ - __asm__ ("stp %[one], %[two], [%[dest]]" \ - : "=m" (((uint64_t *)(destp))[0]), \ - "=m" (((uint64_t *)(destp))[1]) \ - : [one] "r" (onep), \ - [two] "r" (twop), \ - [dest] "r" (destp) \ - : /* no clobbers */ \ - ) -#define ldp(onep, twop, srcp) \ - __asm__ ("ldp %[one], %[two], [%[src]]" \ - : [one] "=r" (onep), \ - [two] "=r" (twop) \ - : "m" (((uint64_t *)(srcp))[0]), \ - "m" (((uint64_t *)(srcp))[1]), \ - [src] "r" (srcp) \ - : /* no clobbers */ \ - ) +static ALWAYS_INLINE void +stp(uintptr_t onep, uintptr_t twop, void *destp) +{ + __asm__ ("stp %" p "[one], %" p "[two], [%x[dest]]" + : "=m" (((uintptr_t *)(destp))[0]), + "=m" (((uintptr_t *)(destp))[1]) + : [one] "r" (onep), + [two] "r" (twop), + [dest] "r" (destp) + : /* no clobbers */ + ); +} +static ALWAYS_INLINE void __unused +ldp(uintptr_t& onep, uintptr_t& twop, const void *srcp) +{ + __asm__ ("ldp %" p "[one], %" p "[two], [%x[src]]" + : [one] "=r" (onep), + [two] "=r" (twop) + : "m" (((const uintptr_t *)(srcp))[0]), + "m" (((const uintptr_t *)(srcp))[1]), + [src] "r" (srcp) + : /* no clobbers */ + ); +} + +#undef p #endif // Class points to cache. SEL is key. Cache buckets store SEL+IMP. // Caches are never built in the dyld shared cache. -static inline mask_t cache_hash(cache_key_t key, mask_t mask) +static inline mask_t cache_hash(SEL sel, mask_t mask) { - return (mask_t)(key & mask); + return (mask_t)(uintptr_t)sel & mask; } cache_t *getCache(Class cls) { - assert(cls); + ASSERT(cls); return &cls->cache; } -cache_key_t getKey(SEL sel) -{ - assert(sel); - return (cache_key_t)sel; -} - #if __arm64__ -void bucket_t::set(cache_key_t newKey, IMP newImp) +template +void bucket_t::set(SEL newSel, IMP newImp, Class cls) { - assert(_key == 0 || _key == newKey); + ASSERT(_sel.load(memory_order::memory_order_relaxed) == 0 || + _sel.load(memory_order::memory_order_relaxed) == newSel); - // LDP/STP guarantees that all observers get - // either key/imp or newKey/newImp - stp(newKey, newImp, this); + static_assert(offsetof(bucket_t,_imp) == 0 && + offsetof(bucket_t,_sel) == sizeof(void *), + "bucket_t layout doesn't match arm64 bucket_t::set()"); + + uintptr_t encodedImp = (impEncoding == Encoded + ? encodeImp(newImp, newSel, cls) + : (uintptr_t)newImp); + + // LDP/STP guarantees that all observers get + // either imp/sel or newImp/newSel + stp(encodedImp, (uintptr_t)newSel, this); } #else -void bucket_t::set(cache_key_t newKey, IMP newImp) +template +void bucket_t::set(SEL newSel, IMP newImp, Class cls) { - assert(_key == 0 || _key == newKey); + ASSERT(_sel.load(memory_order::memory_order_relaxed) == 0 || + _sel.load(memory_order::memory_order_relaxed) == newSel); - // objc_msgSend uses key and imp with no locks. - // It is safe for objc_msgSend to see new imp but NULL key + // objc_msgSend uses sel and imp with no locks. + // It is safe for objc_msgSend to see new imp but NULL sel // (It will get a cache miss but not dispatch to the wrong place.) - // It is unsafe for objc_msgSend to see old imp and new key. - // Therefore we write new imp, wait a lot, then write new key. + // It is unsafe for objc_msgSend to see old imp and new sel. + // Therefore we write new imp, wait a lot, then write new sel. - _imp = newImp; - - if (_key != newKey) { - mega_barrier(); - _key = newKey; + uintptr_t newIMP = (impEncoding == Encoded + ? encodeImp(newImp, newSel, cls) + : (uintptr_t)newImp); + + if (atomicity == Atomic) { + _imp.store(newIMP, memory_order::memory_order_relaxed); + + if (_sel.load(memory_order::memory_order_relaxed) != newSel) { +#ifdef __arm__ + mega_barrier(); + _sel.store(newSel, memory_order::memory_order_relaxed); +#elif __x86_64__ || __i386__ + _sel.store(newSel, memory_order::memory_order_release); +#else +#error Don't know how to do bucket_t::set on this architecture. +#endif + } + } else { + _imp.store(newIMP, memory_order::memory_order_relaxed); + _sel.store(newSel, memory_order::memory_order_relaxed); } } #endif +#if CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_OUTLINED + void cache_t::setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask) { // objc_msgSend uses mask and buckets with no locks. @@ -287,29 +317,138 @@ void cache_t::setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask) // Therefore we write new buckets, wait a lot, then write new mask. // objc_msgSend reads mask first, then buckets. +#ifdef __arm__ // ensure other threads see buckets contents before buckets pointer mega_barrier(); - _buckets = newBuckets; + _buckets.store(newBuckets, memory_order::memory_order_relaxed); // ensure other threads see new buckets before new mask mega_barrier(); - _mask = newMask; + _mask.store(newMask, memory_order::memory_order_relaxed); _occupied = 0; +#elif __x86_64__ || i386 + // ensure other threads see buckets contents before buckets pointer + _buckets.store(newBuckets, memory_order::memory_order_release); + + // ensure other threads see new buckets before new mask + _mask.store(newMask, memory_order::memory_order_release); + _occupied = 0; +#else +#error Don't know how to do setBucketsAndMask on this architecture. +#endif } +struct bucket_t *cache_t::emptyBuckets() +{ + return (bucket_t *)&_objc_empty_cache; +} struct bucket_t *cache_t::buckets() { - return _buckets; + return _buckets.load(memory_order::memory_order_relaxed); } mask_t cache_t::mask() { - return _mask; + return _mask.load(memory_order::memory_order_relaxed); } +void cache_t::initializeToEmpty() +{ + bzero(this, sizeof(*this)); + _buckets.store((bucket_t *)&_objc_empty_cache, memory_order::memory_order_relaxed); +} + +#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_HIGH_16 + +void cache_t::setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask) +{ + uintptr_t buckets = (uintptr_t)newBuckets; + uintptr_t mask = (uintptr_t)newMask; + + ASSERT(buckets <= bucketsMask); + ASSERT(mask <= maxMask); + + _maskAndBuckets.store(((uintptr_t)newMask << maskShift) | (uintptr_t)newBuckets, std::memory_order_relaxed); + _occupied = 0; +} + +struct bucket_t *cache_t::emptyBuckets() +{ + return (bucket_t *)&_objc_empty_cache; +} + +struct bucket_t *cache_t::buckets() +{ + uintptr_t maskAndBuckets = _maskAndBuckets.load(memory_order::memory_order_relaxed); + return (bucket_t *)(maskAndBuckets & bucketsMask); +} + +mask_t cache_t::mask() +{ + uintptr_t maskAndBuckets = _maskAndBuckets.load(memory_order::memory_order_relaxed); + return maskAndBuckets >> maskShift; +} + +void cache_t::initializeToEmpty() +{ + bzero(this, sizeof(*this)); + _maskAndBuckets.store((uintptr_t)&_objc_empty_cache, std::memory_order_relaxed); +} + +#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_LOW_4 + +void cache_t::setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask) +{ + uintptr_t buckets = (uintptr_t)newBuckets; + unsigned mask = (unsigned)newMask; + + ASSERT(buckets == (buckets & bucketsMask)); + ASSERT(mask <= 0xffff); + + // The shift amount is equal to the number of leading zeroes in + // the last 16 bits of mask. Count all the leading zeroes, then + // subtract to ignore the top half. + uintptr_t maskShift = __builtin_clz(mask) - (sizeof(mask) * CHAR_BIT - 16); + ASSERT(mask == (0xffff >> maskShift)); + + _maskAndBuckets.store(buckets | maskShift, memory_order::memory_order_relaxed); + _occupied = 0; + + ASSERT(this->buckets() == newBuckets); + ASSERT(this->mask() == newMask); +} + +struct bucket_t *cache_t::emptyBuckets() +{ + return (bucket_t *)((uintptr_t)&_objc_empty_cache & bucketsMask); +} + +struct bucket_t *cache_t::buckets() +{ + uintptr_t maskAndBuckets = _maskAndBuckets.load(memory_order::memory_order_relaxed); + return (bucket_t *)(maskAndBuckets & bucketsMask); +} + +mask_t cache_t::mask() +{ + uintptr_t maskAndBuckets = _maskAndBuckets.load(memory_order::memory_order_relaxed); + uintptr_t maskShift = (maskAndBuckets & maskMask); + return 0xffff >> maskShift; +} + +void cache_t::initializeToEmpty() +{ + bzero(this, sizeof(*this)); + _maskAndBuckets.store((uintptr_t)&_objc_empty_cache, std::memory_order_relaxed); +} + +#else +#error Unknown cache mask storage type. +#endif + mask_t cache_t::occupied() { return _occupied; @@ -320,30 +459,21 @@ void cache_t::incrementOccupied() _occupied++; } -void cache_t::initializeToEmpty() -{ - bzero(this, sizeof(*this)); - _buckets = (bucket_t *)&_objc_empty_cache; -} - - -mask_t cache_t::capacity() +unsigned cache_t::capacity() { return mask() ? mask()+1 : 0; } -#if CACHE_END_MARKER - -size_t cache_t::bytesForCapacity(uint32_t cap) +size_t cache_t::bytesForCapacity(uint32_t cap) { - // fixme put end marker inline when capacity+1 malloc is inefficient - return sizeof(bucket_t) * (cap + 1); + return sizeof(bucket_t) * cap; } -bucket_t *cache_t::endMarker(struct bucket_t *b, uint32_t cap) +#if CACHE_END_MARKER + +bucket_t *cache_t::endMarker(struct bucket_t *b, uint32_t cap) { - // bytesForCapacity() chooses whether the end marker is inline or not return (bucket_t *)((uintptr_t)b + bytesForCapacity(cap)) - 1; } @@ -351,21 +481,18 @@ bucket_t *allocateBuckets(mask_t newCapacity) { // Allocate one extra bucket to mark the end of the list. // This can't overflow mask_t because newCapacity is a power of 2. - // fixme instead put the end mark inline when +1 is malloc-inefficient bucket_t *newBuckets = (bucket_t *) calloc(cache_t::bytesForCapacity(newCapacity), 1); bucket_t *end = cache_t::endMarker(newBuckets, newCapacity); #if __arm__ - // End marker's key is 1 and imp points BEFORE the first bucket. + // End marker's sel is 1 and imp points BEFORE the first bucket. // This saves an instruction in objc_msgSend. - end->setKey((cache_key_t)(uintptr_t)1); - end->setImp((IMP)(newBuckets - 1)); + end->set((SEL)(uintptr_t)1, (IMP)(newBuckets - 1), nil); #else - // End marker's key is 1 and imp points to the first bucket. - end->setKey((cache_key_t)(uintptr_t)1); - end->setImp((IMP)newBuckets); + // End marker's sel is 1 and imp points to the first bucket. + end->set((SEL)(uintptr_t)1, (IMP)newBuckets, nil); #endif if (PrintCaches) recordNewCache(newCapacity); @@ -375,11 +502,6 @@ bucket_t *allocateBuckets(mask_t newCapacity) #else -size_t cache_t::bytesForCapacity(uint32_t cap) -{ - return sizeof(bucket_t) * cap; -} - bucket_t *allocateBuckets(mask_t newCapacity) { if (PrintCaches) recordNewCache(newCapacity); @@ -392,13 +514,17 @@ bucket_t *allocateBuckets(mask_t newCapacity) bucket_t *emptyBucketsForCapacity(mask_t capacity, bool allocate = true) { +#if CONFIG_USE_CACHE_LOCK cacheUpdateLock.assertLocked(); +#else + runtimeLock.assertLocked(); +#endif size_t bytes = cache_t::bytesForCapacity(capacity); // Use _objc_empty_cache if the buckets is small enough. if (bytes <= EMPTY_BYTES) { - return (bucket_t *)&_objc_empty_cache; + return cache_t::emptyBuckets(); } // Use shared empty buckets allocated on the heap. @@ -443,11 +569,9 @@ bool cache_t::canBeFreed() return !isConstantEmptyCache(); } - -void cache_t::reallocate(mask_t oldCapacity, mask_t newCapacity) +ALWAYS_INLINE +void cache_t::reallocate(mask_t oldCapacity, mask_t newCapacity, bool freeOld) { - bool freeOld = canBeFreed(); - bucket_t *oldBuckets = buckets(); bucket_t *newBuckets = allocateBuckets(newCapacity); @@ -455,14 +579,13 @@ void cache_t::reallocate(mask_t oldCapacity, mask_t newCapacity) // This is thought to save cache memory at the cost of extra cache fills. // fixme re-measure this - assert(newCapacity > 0); - assert((uintptr_t)(mask_t)(newCapacity-1) == newCapacity-1); + ASSERT(newCapacity > 0); + ASSERT((uintptr_t)(mask_t)(newCapacity-1) == newCapacity-1); setBucketsAndMask(newBuckets, newCapacity - 1); if (freeOld) { cache_collect_free(oldBuckets, oldCapacity); - cache_collect(false); } } @@ -474,16 +597,35 @@ void cache_t::bad_cache(id receiver, SEL sel, Class isa) ("Method cache corrupted. This may be a message to an " "invalid object, or a memory error somewhere else."); cache_t *cache = &isa->cache; +#if CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_OUTLINED + bucket_t *buckets = cache->_buckets.load(memory_order::memory_order_relaxed); _objc_inform_now_and_on_crash ("%s %p, SEL %p, isa %p, cache %p, buckets %p, " "mask 0x%x, occupied 0x%x", receiver ? "receiver" : "unused", receiver, - sel, isa, cache, cache->_buckets, - cache->_mask, cache->_occupied); + sel, isa, cache, buckets, + cache->_mask.load(memory_order::memory_order_relaxed), + cache->_occupied); _objc_inform_now_and_on_crash ("%s %zu bytes, buckets %zu bytes", receiver ? "receiver" : "unused", malloc_size(receiver), - malloc_size(cache->_buckets)); + malloc_size(buckets)); +#elif (CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_HIGH_16 || \ + CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_LOW_4) + uintptr_t maskAndBuckets = cache->_maskAndBuckets.load(memory_order::memory_order_relaxed); + _objc_inform_now_and_on_crash + ("%s %p, SEL %p, isa %p, cache %p, buckets and mask 0x%lx, " + "occupied 0x%x", + receiver ? "receiver" : "unused", receiver, + sel, isa, cache, maskAndBuckets, + cache->_occupied); + _objc_inform_now_and_on_crash + ("%s %zu bytes, buckets %zu bytes", + receiver ? "receiver" : "unused", malloc_size(receiver), + malloc_size(cache->buckets())); +#else +#error Unknown cache mask storage type. +#endif _objc_inform_now_and_on_crash ("selector '%s'", sel_getName(sel)); _objc_inform_now_and_on_crash @@ -493,89 +635,75 @@ void cache_t::bad_cache(id receiver, SEL sel, Class isa) "invalid object, or a memory error somewhere else."); } - -bucket_t * cache_t::find(cache_key_t k, id receiver) -{ - assert(k != 0); - - bucket_t *b = buckets(); - mask_t m = mask(); - mask_t begin = cache_hash(k, m); - mask_t i = begin; - do { - if (b[i].key() == 0 || b[i].key() == k) { - return &b[i]; - } - } while ((i = cache_next(i, m)) != begin); - - // hack - Class cls = (Class)((uintptr_t)this - offsetof(objc_class, cache)); - cache_t::bad_cache(receiver, (SEL)k, cls); -} - - -void cache_t::expand() +ALWAYS_INLINE +void cache_t::insert(Class cls, SEL sel, IMP imp, id receiver) { +#if CONFIG_USE_CACHE_LOCK cacheUpdateLock.assertLocked(); - - uint32_t oldCapacity = capacity(); - uint32_t newCapacity = oldCapacity ? oldCapacity*2 : INIT_CACHE_SIZE; +#else + runtimeLock.assertLocked(); +#endif - if ((uint32_t)(mask_t)newCapacity != newCapacity) { - // mask overflow - can't grow further - // fixme this wastes one bit of mask - newCapacity = oldCapacity; - } - - reallocate(oldCapacity, newCapacity); -} - - -static void cache_fill_nolock(Class cls, SEL sel, IMP imp, id receiver) -{ - cacheUpdateLock.assertLocked(); - - // Never cache before +initialize is done - if (!cls->isInitialized()) return; - - // Make sure the entry wasn't added to the cache by some other thread - // before we grabbed the cacheUpdateLock. - if (cache_getImp(cls, sel)) return; - - cache_t *cache = getCache(cls); - cache_key_t key = getKey(sel); + ASSERT(sel != 0 && cls->isInitialized()); // Use the cache as-is if it is less than 3/4 full - mask_t newOccupied = cache->occupied() + 1; - mask_t capacity = cache->capacity(); - if (cache->isConstantEmptyCache()) { + mask_t newOccupied = occupied() + 1; + unsigned oldCapacity = capacity(), capacity = oldCapacity; + if (slowpath(isConstantEmptyCache())) { // Cache is read-only. Replace it. - cache->reallocate(capacity, capacity ?: INIT_CACHE_SIZE); + if (!capacity) capacity = INIT_CACHE_SIZE; + reallocate(oldCapacity, capacity, /* freeOld */false); } - else if (newOccupied <= capacity / 4 * 3) { + else if (fastpath(newOccupied + CACHE_END_MARKER <= capacity / 4 * 3)) { // Cache is less than 3/4 full. Use it as-is. } else { - // Cache is too full. Expand it. - cache->expand(); + capacity = capacity ? capacity * 2 : INIT_CACHE_SIZE; + if (capacity > MAX_CACHE_SIZE) { + capacity = MAX_CACHE_SIZE; + } + reallocate(oldCapacity, capacity, true); } + bucket_t *b = buckets(); + mask_t m = capacity - 1; + mask_t begin = cache_hash(sel, m); + mask_t i = begin; + // Scan for the first unused slot and insert there. - // There is guaranteed to be an empty slot because the + // There is guaranteed to be an empty slot because the // minimum size is 4 and we resized at 3/4 full. - bucket_t *bucket = cache->find(key, receiver); - if (bucket->key() == 0) cache->incrementOccupied(); - bucket->set(key, imp); + do { + if (fastpath(b[i].sel() == 0)) { + incrementOccupied(); + b[i].set(sel, imp, cls); + return; + } + if (b[i].sel() == sel) { + // The entry was added to the cache by some other thread + // before we grabbed the cacheUpdateLock. + return; + } + } while (fastpath((i = cache_next(i, m)) != begin)); + + cache_t::bad_cache(receiver, (SEL)sel, cls); } void cache_fill(Class cls, SEL sel, IMP imp, id receiver) { + runtimeLock.assertLocked(); + #if !DEBUG_TASK_THREADS - mutex_locker_t lock(cacheUpdateLock); - cache_fill_nolock(cls, sel, imp, receiver); + // Never cache before +initialize is done + if (cls->isInitialized()) { + cache_t *cache = getCache(cls); +#if CONFIG_USE_CACHE_LOCK + mutex_locker_t lock(cacheUpdateLock); +#endif + cache->insert(cls, sel, imp, receiver); + } #else _collecting_in_critical(); - return; #endif } @@ -584,7 +712,11 @@ void cache_fill(Class cls, SEL sel, IMP imp, id receiver) // This must not shrink the cache - that breaks the lock-free scheme. void cache_erase_nolock(Class cls) { +#if CONFIG_USE_CACHE_LOCK cacheUpdateLock.assertLocked(); +#else + runtimeLock.assertLocked(); +#endif cache_t *cache = getCache(cls); @@ -595,14 +727,17 @@ void cache_erase_nolock(Class cls) cache->setBucketsAndMask(buckets, capacity - 1); // also clears occupied cache_collect_free(oldBuckets, capacity); - cache_collect(false); } } void cache_delete(Class cls) { +#if CONFIG_USE_CACHE_LOCK mutex_locker_t lock(cacheUpdateLock); +#else + runtimeLock.assertLocked(); +#endif if (cls->cache.canBeFreed()) { if (PrintCaches) recordDeadCache(cls->cache.capacity()); free(cls->cache.buckets()); @@ -648,7 +783,7 @@ static uintptr_t _get_pc_for_thread(thread_t thread) arm_thread_state64_t state; unsigned int count = ARM_THREAD_STATE64_COUNT; kern_return_t okay = thread_get_state (thread, ARM_THREAD_STATE64, (thread_state_t)&state, &count); - return (okay == KERN_SUCCESS) ? state.__pc : PC_SENTINEL; + return (okay == KERN_SUCCESS) ? (uintptr_t)arm_thread_state64_get_pc(state) : PC_SENTINEL; } #else { @@ -665,21 +800,64 @@ static uintptr_t _get_pc_for_thread(thread_t thread) * reading function is in progress because it might still be using * the garbage memory. **********************************************************************/ -OBJC_EXPORT uintptr_t objc_entryPoints[]; -OBJC_EXPORT uintptr_t objc_exitPoints[]; +#if HAVE_TASK_RESTARTABLE_RANGES +#include +#else +typedef struct { + uint64_t location; + unsigned short length; + unsigned short recovery_offs; + unsigned int flags; +} task_restartable_range_t; +#endif + +extern "C" task_restartable_range_t objc_restartableRanges[]; + +#if HAVE_TASK_RESTARTABLE_RANGES +static bool shouldUseRestartableRanges = true; +#endif + +void cache_init() +{ +#if HAVE_TASK_RESTARTABLE_RANGES + mach_msg_type_number_t count = 0; + kern_return_t kr; + + while (objc_restartableRanges[count].location) { + count++; + } + + kr = task_restartable_ranges_register(mach_task_self(), + objc_restartableRanges, count); + if (kr == KERN_SUCCESS) return; + _objc_fatal("task_restartable_ranges_register failed (result 0x%x: %s)", + kr, mach_error_string(kr)); +#endif // HAVE_TASK_RESTARTABLE_RANGES +} static int _collecting_in_critical(void) { #if TARGET_OS_WIN32 return TRUE; -#else +#elif HAVE_TASK_RESTARTABLE_RANGES + // Only use restartable ranges if we registered them earlier. + if (shouldUseRestartableRanges) { + kern_return_t kr = task_restartable_ranges_synchronize(mach_task_self()); + if (kr == KERN_SUCCESS) return FALSE; + _objc_fatal("task_restartable_ranges_synchronize failed (result 0x%x: %s)", + kr, mach_error_string(kr)); + } +#endif // !HAVE_TASK_RESTARTABLE_RANGES + + // Fallthrough if we didn't use restartable ranges. + thread_act_port_array_t threads; unsigned number; unsigned count; kern_return_t ret; int result; - mach_port_t mythread = pthread_mach_thread_np(pthread_self()); + mach_port_t mythread = pthread_mach_thread_np(objc_thread_self()); // Get a list of all the threads in the current task #if !DEBUG_TASK_THREADS @@ -715,10 +893,11 @@ static int _collecting_in_critical(void) } // Check whether it is in the cache lookup code - for (region = 0; objc_entryPoints[region] != 0; region++) + for (region = 0; objc_restartableRanges[region].location != 0; region++) { - if ((pc >= objc_entryPoints[region]) && - (pc <= objc_exitPoints[region])) + uint64_t loc = objc_restartableRanges[region].location; + if ((pc > loc) && + (pc - loc < (uint64_t)objc_restartableRanges[region].length)) { result = TRUE; goto done; @@ -737,7 +916,6 @@ static int _collecting_in_critical(void) // Return our finding return result; -#endif } @@ -798,13 +976,18 @@ static void _garbage_make_room(void) **********************************************************************/ static void cache_collect_free(bucket_t *data, mask_t capacity) { +#if CONFIG_USE_CACHE_LOCK cacheUpdateLock.assertLocked(); +#else + runtimeLock.assertLocked(); +#endif if (PrintCaches) recordDeadCache(capacity); _garbage_make_room (); garbage_byte_size += cache_t::bytesForCapacity(capacity); garbage_refs[garbage_count++] = data; + cache_collect(false); } @@ -815,7 +998,11 @@ static void cache_collect_free(bucket_t *data, mask_t capacity) **********************************************************************/ void cache_collect(bool collectALot) { +#if CONFIG_USE_CACHE_LOCK cacheUpdateLock.assertLocked(); +#else + runtimeLock.assertLocked(); +#endif // Done if the garbage is not full if (garbage_byte_size < garbage_threshold && !collectALot) { diff --git a/runtime/objc-class-old.mm b/runtime/objc-class-old.mm index f62bdee..acc269e 100644 --- a/runtime/objc-class-old.mm +++ b/runtime/objc-class-old.mm @@ -36,6 +36,7 @@ static Method _class_getMethod(Class cls, SEL sel); static Method _class_getMethodNoSuper(Class cls, SEL sel); static Method _class_getMethodNoSuper_nolock(Class cls, SEL sel); +static Class _class_getNonMetaClass(Class cls, id obj); static void flush_caches(Class cls, bool flush_meta); @@ -163,13 +164,12 @@ static old_method_list *fixupSelectorsInMethodList(Class cls, old_method_list *m // Mach-O bundles are fixed up in place. // This prevents leaks when a bundle is unloaded. } - sel_lock(); + mutex_locker_t lock(selLock); for ( i = 0; i < mlist->method_count; i += 1 ) { method = &mlist->method_list[i]; method->method_name = sel_registerNameNoLock((const char *)method->method_name, isBundle); // Always copy selector data from bundles. } - sel_unlock(); mlist->obsolete = fixed_up_method_list; } return mlist; @@ -325,6 +325,112 @@ static void _freedHandler(id obj, SEL sel) } +/*********************************************************************** +* _class_resolveClassMethod +* Call +resolveClassMethod, looking for a method to be added to class cls. +* cls should be a metaclass. +* Does not check if the method already exists. +**********************************************************************/ +static void _class_resolveClassMethod(id inst, SEL sel, Class cls) +{ + ASSERT(cls->isMetaClass()); + SEL resolve_sel = @selector(resolveClassMethod:); + + if (!lookUpImpOrNil(inst, resolve_sel, cls)) { + // Resolver not implemented. + return; + } + + BOOL (*msg)(Class, SEL, SEL) = (typeof(msg))objc_msgSend; + bool resolved = msg(_class_getNonMetaClass(cls, inst), resolve_sel, sel); + + // Cache the result (good or bad) so the resolver doesn't fire next time. + // +resolveClassMethod adds to self->ISA() a.k.a. cls + IMP imp = lookUpImpOrNil(inst, sel, cls); + if (resolved && PrintResolving) { + if (imp) { + _objc_inform("RESOLVE: method %c[%s %s] " + "dynamically resolved to %p", + cls->isMetaClass() ? '+' : '-', + cls->nameForLogging(), sel_getName(sel), imp); + } + else { + // Method resolver didn't add anything? + _objc_inform("RESOLVE: +[%s resolveClassMethod:%s] returned YES" + ", but no new implementation of %c[%s %s] was found", + cls->nameForLogging(), sel_getName(sel), + cls->isMetaClass() ? '+' : '-', + cls->nameForLogging(), sel_getName(sel)); + } + } +} + + +/*********************************************************************** +* _class_resolveInstanceMethod +* Call +resolveInstanceMethod, looking for a method to be added to class cls. +* cls may be a metaclass or a non-meta class. +* Does not check if the method already exists. +**********************************************************************/ +static void _class_resolveInstanceMethod(id inst, SEL sel, Class cls) +{ + SEL resolve_sel = @selector(resolveInstanceMethod:); + + if (! lookUpImpOrNil(cls, resolve_sel, cls->ISA())) { + // Resolver not implemented. + return; + } + + BOOL (*msg)(Class, SEL, SEL) = (typeof(msg))objc_msgSend; + bool resolved = msg(cls, resolve_sel, sel); + + // Cache the result (good or bad) so the resolver doesn't fire next time. + // +resolveInstanceMethod adds to self a.k.a. cls + IMP imp = lookUpImpOrNil(inst, sel, cls); + + if (resolved && PrintResolving) { + if (imp) { + _objc_inform("RESOLVE: method %c[%s %s] " + "dynamically resolved to %p", + cls->isMetaClass() ? '+' : '-', + cls->nameForLogging(), sel_getName(sel), imp); + } + else { + // Method resolver didn't add anything? + _objc_inform("RESOLVE: +[%s resolveInstanceMethod:%s] returned YES" + ", but no new implementation of %c[%s %s] was found", + cls->nameForLogging(), sel_getName(sel), + cls->isMetaClass() ? '+' : '-', + cls->nameForLogging(), sel_getName(sel)); + } + } +} + + +/*********************************************************************** +* _class_resolveMethod +* Call +resolveClassMethod or +resolveInstanceMethod. +* Returns nothing; any result would be potentially out-of-date already. +* Does not check if the method already exists. +**********************************************************************/ +static void +_class_resolveMethod(id inst, SEL sel, Class cls) +{ + if (! cls->isMetaClass()) { + // try [cls resolveInstanceMethod:sel] + _class_resolveInstanceMethod(inst, sel, cls); + } + else { + // try [nonMetaClass resolveClassMethod:sel] + // and [cls resolveInstanceMethod:sel] + _class_resolveClassMethod(inst, sel, cls); + if (!lookUpImpOrNil(inst, sel, cls)) { + _class_resolveInstanceMethod(inst, sel, cls); + } + } +} + + /*********************************************************************** * log_and_fill_cache * Log this method call. If the logger permits it, fill the method cache. @@ -347,33 +453,19 @@ log_and_fill_cache(Class cls, Class implementer, Method meth, SEL sel) } -/*********************************************************************** -* _class_lookupMethodAndLoadCache. -* Method lookup for dispatchers ONLY. OTHER CODE SHOULD USE lookUpImp(). -* This lookup avoids optimistic cache scan because the dispatcher -* already tried that. -**********************************************************************/ -IMP _class_lookupMethodAndLoadCache3(id obj, SEL sel, Class cls) -{ - return lookUpImpOrForward(cls, sel, obj, - YES/*initialize*/, NO/*cache*/, YES/*resolver*/); -} - - /*********************************************************************** * lookUpImpOrForward. * The standard IMP lookup. -* initialize==NO tries to avoid +initialize (but sometimes fails) -* cache==NO skips optimistic unlocked lookup (but uses cache elsewhere) -* Most callers should use initialize==YES and cache==YES. -* inst is an instance of cls or a subclass thereof, or nil if none is known. +* Without LOOKUP_INITIALIZE: tries to avoid +initialize (but sometimes fails) +* Without LOOKUP_CACHE: skips optimistic unlocked lookup (but uses cache elsewhere) +* Most callers should use LOOKUP_INITIALIZE and LOOKUP_CACHE +* inst is an instance of cls or a subclass thereof, or nil if none is known. * If cls is an un-initialized metaclass then a non-nil inst is faster. * May return _objc_msgForward_impcache. IMPs destined for external use * must be converted to _objc_msgForward or _objc_msgForward_stret. -* If you don't want forwarding at all, use lookUpImpOrNil() instead. +* If you don't want forwarding at all, use LOOKUP_NIL. **********************************************************************/ -IMP lookUpImpOrForward(Class cls, SEL sel, id inst, - bool initialize, bool cache, bool resolver) +IMP lookUpImpOrForward(id inst, SEL sel, Class cls, int behavior) { Class curClass; IMP methodPC = nil; @@ -383,9 +475,9 @@ IMP lookUpImpOrForward(Class cls, SEL sel, id inst, methodListLock.assertUnlocked(); // Optimistic cache lookup - if (cache) { + if (behavior & LOOKUP_CACHE) { methodPC = _cache_getImp(cls, sel); - if (methodPC) return methodPC; + if (methodPC) goto out_nolock; } // Check for freed class @@ -393,10 +485,10 @@ IMP lookUpImpOrForward(Class cls, SEL sel, id inst, return (IMP) _freedHandler; // Check for +initialize - if (initialize && !cls->isInitialized()) { - _class_initialize (_class_getNonMetaClass(cls, inst)); - // If sel == initialize, _class_initialize will send +initialize and - // then the messenger will send +initialize again after this + if ((behavior & LOOKUP_INITIALIZE) && !cls->isInitialized()) { + initializeNonMetaClass (_class_getNonMetaClass(cls, inst)); + // If sel == initialize, initializeNonMetaClass will send +initialize + // and then the messenger will send +initialize again after this // procedure finishes. Of course, if this is not being called // from the messenger then it won't happen. 2778172 } @@ -454,7 +546,7 @@ IMP lookUpImpOrForward(Class cls, SEL sel, id inst, // No implementation found. Try method resolver once. - if (resolver && !triedResolver) { + if ((behavior & LOOKUP_RESOLVER) && !triedResolver) { methodListLock.unlock(); _class_resolveMethod(cls, sel, inst); triedResolver = YES; @@ -470,23 +562,14 @@ IMP lookUpImpOrForward(Class cls, SEL sel, id inst, done: methodListLock.unlock(); + out_nolock: + if ((behavior & LOOKUP_NIL) && methodPC == (IMP)_objc_msgForward_impcache) { + return nil; + } return methodPC; } -/*********************************************************************** -* lookUpImpOrNil. -* Like lookUpImpOrForward, but returns nil instead of _objc_msgForward_impcache -**********************************************************************/ -IMP lookUpImpOrNil(Class cls, SEL sel, id inst, - bool initialize, bool cache, bool resolver) -{ - IMP imp = lookUpImpOrForward(cls, sel, inst, initialize, cache, resolver); - if (imp == _objc_msgForward_impcache) return nil; - else return imp; -} - - /*********************************************************************** * lookupMethodInClassAndLoadCache. * Like _class_lookupMethodAndLoadCache, but does not search superclasses. @@ -683,33 +766,6 @@ void class_setIvarLayout(Class cls, const uint8_t *layout) cls->ivar_layout = ustrdupMaybeNil(layout); } -// SPI: Instance-specific object layout. - -void _class_setIvarLayoutAccessor(Class cls, const uint8_t* (*accessor) (id object)) { - if (!cls) return; - - if (! (cls->info & CLS_EXT)) { - _objc_inform("class '%s' needs to be recompiled", cls->name); - return; - } - - // fixme leak - cls->ivar_layout = (const uint8_t *)accessor; - cls->setInfo(CLS_HAS_INSTANCE_SPECIFIC_LAYOUT); -} - -const uint8_t *_object_getIvarLayout(Class cls, id object) { - if (cls && (cls->info & CLS_EXT)) { - const uint8_t* layout = cls->ivar_layout; - if (cls->info & CLS_HAS_INSTANCE_SPECIFIC_LAYOUT) { - const uint8_t* (*accessor) (id object) = (const uint8_t* (*)(id))layout; - layout = accessor(object); - } - return layout; - } else { - return nil; - } -} /*********************************************************************** * class_setWeakIvarLayout @@ -762,7 +818,7 @@ const char *class_getName(Class cls) * Return the ordinary class for this class or metaclass. * Used by +initialize. **********************************************************************/ -Class _class_getNonMetaClass(Class cls, id obj) +static Class _class_getNonMetaClass(Class cls, id obj) { // fixme ick if (cls->isMetaClass()) { @@ -780,13 +836,21 @@ Class _class_getNonMetaClass(Class cls, id obj) else { cls = objc_getClass(cls->name); } - assert(cls); + ASSERT(cls); } return cls; } +Class class_initialize(Class cls, id inst) { + if (!cls->isInitialized()) { + initializeNonMetaClass (_class_getNonMetaClass(cls, inst)); + } + return cls; +} + + Cache _class_getCache(Class cls) { return cls->cache; @@ -837,7 +901,7 @@ IMP _category_getLoadMethod(Category cat) * If methods are removed between calls to class_nextMethodList(), it may * omit surviving method lists or simply crash. **********************************************************************/ -OBJC_EXPORT struct objc_method_list *class_nextMethodList(Class cls, void **it) +struct objc_method_list *class_nextMethodList(Class cls, void **it) { OBJC_WARN_DEPRECATED; @@ -851,7 +915,7 @@ OBJC_EXPORT struct objc_method_list *class_nextMethodList(Class cls, void **it) * * Formerly class_addInstanceMethods () **********************************************************************/ -OBJC_EXPORT void class_addMethods(Class cls, struct objc_method_list *meths) +void class_addMethods(Class cls, struct objc_method_list *meths) { OBJC_WARN_DEPRECATED; @@ -871,7 +935,7 @@ OBJC_EXPORT void class_addMethods(Class cls, struct objc_method_list *meths) /*********************************************************************** * class_removeMethods. **********************************************************************/ -OBJC_EXPORT void class_removeMethods(Class cls, struct objc_method_list *meths) +void class_removeMethods(Class cls, struct objc_method_list *meths) { OBJC_WARN_DEPRECATED; @@ -942,8 +1006,7 @@ Method class_getInstanceMethod(Class cls, SEL sel) } // Search method lists, try method resolver, etc. - lookUpImpOrNil(cls, sel, nil, - NO/*initialize*/, NO/*cache*/, YES/*resolver*/); + lookUpImpOrForward(nil, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER); meth = _cache_getMethod(cls, sel, _objc_msgForward_impcache); if (meth == (Method)1) { @@ -1494,6 +1557,9 @@ unsigned int method_getSizeOfArguments(Method m) return encoding_getSizeOfArguments(method_getTypeEncoding(m)); } +// This function was accidentally un-exported beginning in macOS 10.9. +// As of macOS 10.13 nobody had complained. +/* unsigned int method_getArgumentInfo(Method m, int arg, const char **type, int *offset) { @@ -1502,9 +1568,10 @@ unsigned int method_getArgumentInfo(Method m, int arg, return encoding_getArgumentInfo(method_getTypeEncoding(m), arg, type, offset); } +*/ -static spinlock_t impLock; +spinlock_t impLock; IMP method_setImplementation(Method m_gen, IMP imp) { @@ -2289,7 +2356,7 @@ objc_constructInstance(Class cls, void *bytes) obj->initIsa(cls); if (cls->hasCxxCtor()) { - return object_cxxConstructFromClass(obj, cls); + return object_cxxConstructFromClass(obj, cls, OBJECT_CONSTRUCT_NONE); } else { return obj; } @@ -2464,6 +2531,21 @@ id class_createInstanceFromZone(Class cls, size_t extraBytes, void *z) return (*_zoneAlloc)(cls, extraBytes, z); } +id +_objc_rootAllocWithZone(Class cls, malloc_zone_t *zone) +{ + id obj; + + if (fastpath(!zone)) { + obj = class_createInstance(cls, 0); + } else { + obj = class_createInstanceFromZone(cls, 0, zone); + } + + if (slowpath(!obj)) obj = _objc_callBadAllocHandler(cls); + return obj; +} + unsigned class_createInstances(Class cls, size_t extraBytes, id *results, unsigned num_requested) { diff --git a/runtime/objc-class.mm b/runtime/objc-class.mm index d16ebc2..776f3fa 100644 --- a/runtime/objc-class.mm +++ b/runtime/objc-class.mm @@ -160,18 +160,6 @@ #include "objc-abi.h" #include - -/* overriding the default object allocation and error handling routines */ - -OBJC_EXPORT id (*_alloc)(Class, size_t); -OBJC_EXPORT id (*_copy)(id, size_t); -OBJC_EXPORT id (*_realloc)(id, size_t); -OBJC_EXPORT id (*_dealloc)(id); -OBJC_EXPORT id (*_zoneAlloc)(Class, size_t, void *); -OBJC_EXPORT id (*_zoneRealloc)(id, size_t, void *); -OBJC_EXPORT id (*_zoneCopy)(id, size_t, void *); - - /*********************************************************************** * Information about multi-thread support: * @@ -207,7 +195,9 @@ Class object_setClass(id obj, Class cls) // weakly-referenced object has an un-+initialized isa. // Unresolved future classes are not so protected. if (!cls->isFuture() && !cls->isInitialized()) { - _class_initialize(_class_getNonMetaClass(cls, nil)); + // use lookUpImpOrNil to indirectly provoke +initialize + // to avoid duplicating the code to actually send +initialize + lookUpImpOrNil(nil, @selector(initialize), cls, LOOKUP_INITIALIZE); } return obj->changeIsa(cls); @@ -494,9 +484,9 @@ void object_cxxDestruct(id obj) * return nil: construction failed because a C++ constructor threw an exception **********************************************************************/ id -object_cxxConstructFromClass(id obj, Class cls) +object_cxxConstructFromClass(id obj, Class cls, int flags) { - assert(cls->hasCxxCtor()); // required for performance, not correctness + ASSERT(cls->hasCxxCtor()); // required for performance, not correctness id (*ctor)(id); Class supercls; @@ -505,8 +495,8 @@ object_cxxConstructFromClass(id obj, Class cls) // Call superclasses' ctors first, if any. if (supercls && supercls->hasCxxCtor()) { - bool ok = object_cxxConstructFromClass(obj, supercls); - if (!ok) return nil; // some superclass's ctor failed - give up + bool ok = object_cxxConstructFromClass(obj, supercls, flags); + if (slowpath(!ok)) return nil; // some superclass's ctor failed - give up } // Find this class's ctor, if any. @@ -518,11 +508,17 @@ object_cxxConstructFromClass(id obj, Class cls) _objc_inform("CXX: calling C++ constructors for class %s", cls->nameForLogging()); } - if ((*ctor)(obj)) return obj; // ctor called and succeeded - ok + if (fastpath((*ctor)(obj))) return obj; // ctor called and succeeded - ok - // This class's ctor was called and failed. + supercls = cls->superclass; // this reload avoids a spill on the stack + + // This class's ctor was called and failed. // Call superclasses's dtors to clean up. if (supercls) object_cxxDestructFromClass(obj, supercls); + if (flags & OBJECT_CONSTRUCT_FREE_ONFAILURE) free(obj); + if (flags & OBJECT_CONSTRUCT_CALL_BADALLOC) { + return _objc_callBadAllocHandler(cls); + } return nil; } @@ -576,117 +572,6 @@ void fixupCopiedIvars(id newObject, id oldObject) } -/*********************************************************************** -* _class_resolveClassMethod -* Call +resolveClassMethod, looking for a method to be added to class cls. -* cls should be a metaclass. -* Does not check if the method already exists. -**********************************************************************/ -static void _class_resolveClassMethod(Class cls, SEL sel, id inst) -{ - assert(cls->isMetaClass()); - - if (! lookUpImpOrNil(cls, SEL_resolveClassMethod, inst, - NO/*initialize*/, YES/*cache*/, NO/*resolver*/)) - { - // Resolver not implemented. - return; - } - - BOOL (*msg)(Class, SEL, SEL) = (typeof(msg))objc_msgSend; - bool resolved = msg(_class_getNonMetaClass(cls, inst), - SEL_resolveClassMethod, sel); - - // Cache the result (good or bad) so the resolver doesn't fire next time. - // +resolveClassMethod adds to self->ISA() a.k.a. cls - IMP imp = lookUpImpOrNil(cls, sel, inst, - NO/*initialize*/, YES/*cache*/, NO/*resolver*/); - - if (resolved && PrintResolving) { - if (imp) { - _objc_inform("RESOLVE: method %c[%s %s] " - "dynamically resolved to %p", - cls->isMetaClass() ? '+' : '-', - cls->nameForLogging(), sel_getName(sel), imp); - } - else { - // Method resolver didn't add anything? - _objc_inform("RESOLVE: +[%s resolveClassMethod:%s] returned YES" - ", but no new implementation of %c[%s %s] was found", - cls->nameForLogging(), sel_getName(sel), - cls->isMetaClass() ? '+' : '-', - cls->nameForLogging(), sel_getName(sel)); - } - } -} - - -/*********************************************************************** -* _class_resolveInstanceMethod -* Call +resolveInstanceMethod, looking for a method to be added to class cls. -* cls may be a metaclass or a non-meta class. -* Does not check if the method already exists. -**********************************************************************/ -static void _class_resolveInstanceMethod(Class cls, SEL sel, id inst) -{ - if (! lookUpImpOrNil(cls->ISA(), SEL_resolveInstanceMethod, cls, - NO/*initialize*/, YES/*cache*/, NO/*resolver*/)) - { - // Resolver not implemented. - return; - } - - BOOL (*msg)(Class, SEL, SEL) = (typeof(msg))objc_msgSend; - bool resolved = msg(cls, SEL_resolveInstanceMethod, sel); - - // Cache the result (good or bad) so the resolver doesn't fire next time. - // +resolveInstanceMethod adds to self a.k.a. cls - IMP imp = lookUpImpOrNil(cls, sel, inst, - NO/*initialize*/, YES/*cache*/, NO/*resolver*/); - - if (resolved && PrintResolving) { - if (imp) { - _objc_inform("RESOLVE: method %c[%s %s] " - "dynamically resolved to %p", - cls->isMetaClass() ? '+' : '-', - cls->nameForLogging(), sel_getName(sel), imp); - } - else { - // Method resolver didn't add anything? - _objc_inform("RESOLVE: +[%s resolveInstanceMethod:%s] returned YES" - ", but no new implementation of %c[%s %s] was found", - cls->nameForLogging(), sel_getName(sel), - cls->isMetaClass() ? '+' : '-', - cls->nameForLogging(), sel_getName(sel)); - } - } -} - - -/*********************************************************************** -* _class_resolveMethod -* Call +resolveClassMethod or +resolveInstanceMethod. -* Returns nothing; any result would be potentially out-of-date already. -* Does not check if the method already exists. -**********************************************************************/ -void _class_resolveMethod(Class cls, SEL sel, id inst) -{ - if (! cls->isMetaClass()) { - // try [cls resolveInstanceMethod:sel] - _class_resolveInstanceMethod(cls, sel, inst); - } - else { - // try [nonMetaClass resolveClassMethod:sel] - // and [cls resolveInstanceMethod:sel] - _class_resolveClassMethod(cls, sel, inst); - if (!lookUpImpOrNil(cls, sel, inst, - NO/*initialize*/, YES/*cache*/, NO/*resolver*/)) - { - _class_resolveInstanceMethod(cls, sel, inst); - } - } -} - /*********************************************************************** * class_getClassMethod. Return the class method for the specified @@ -745,23 +630,18 @@ BOOL class_respondsToMethod(Class cls, SEL sel) BOOL class_respondsToSelector(Class cls, SEL sel) { - return class_respondsToSelector_inst(cls, sel, nil); + return class_respondsToSelector_inst(nil, sel, cls); } // inst is an instance of cls or a subclass thereof, or nil if none is known. // Non-nil inst is faster in some cases. See lookUpImpOrForward() for details. -bool class_respondsToSelector_inst(Class cls, SEL sel, id inst) +NEVER_INLINE BOOL +class_respondsToSelector_inst(id inst, SEL sel, Class cls) { - IMP imp; - - if (!sel || !cls) return NO; - // Avoids +initialize because it historically did so. // We're not returning a callable IMP anyway. - imp = lookUpImpOrNil(cls, sel, inst, - NO/*initialize*/, YES/*cache*/, YES/*resolver*/); - return bool(imp); + return sel && cls && lookUpImpOrNil(inst, sel, cls, LOOKUP_RESOLVER); } @@ -788,8 +668,7 @@ IMP class_getMethodImplementation(Class cls, SEL sel) if (!cls || !sel) return nil; - imp = lookUpImpOrNil(cls, sel, nil, - YES/*initialize*/, YES/*cache*/, YES/*resolver*/); + imp = lookUpImpOrNil(nil, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER); // Translate forwarding function to C-callable external version if (!imp) { @@ -816,6 +695,9 @@ IMP class_getMethodImplementation_stret(Class cls, SEL sel) /*********************************************************************** * instrumentObjcMessageSends **********************************************************************/ +// Define this everywhere even if it isn't used to simplify fork() safety code. +spinlock_t objcMsgLogLock; + #if !SUPPORT_MESSAGE_LOGGING void instrumentObjcMessageSends(BOOL flag) @@ -826,7 +708,6 @@ void instrumentObjcMessageSends(BOOL flag) bool objcMsgLogEnabled = false; static int objcMsgLogFD = -1; -static spinlock_t objcMsgLogLock; bool logMessageSend(bool isClassMethod, const char *objectsClass, @@ -946,26 +827,6 @@ char * method_copyArgumentType(Method m, unsigned int index) return encoding_copyArgumentType(method_getTypeEncoding(m), index); } - -/*********************************************************************** -* _objc_constructOrFree -* Call C++ constructors, and free() if they fail. -* bytes->isa must already be set. -* cls must have cxx constructors. -* Returns the object, or nil. -**********************************************************************/ -id -_objc_constructOrFree(id bytes, Class cls) -{ - assert(cls->hasCxxCtor()); // for performance, not correctness - - id obj = object_cxxConstructFromClass(bytes, cls); - if (!obj) free(bytes); - - return obj; -} - - /*********************************************************************** * _class_createInstancesFromZone * Batch-allocating version of _class_createInstanceFromZone. @@ -996,8 +857,10 @@ _class_createInstancesFromZone(Class cls, size_t extraBytes, void *zone, for (unsigned i = 0; i < num_allocated; i++) { id obj = results[i]; obj->initIsa(cls); // fixme allow nonpointer - if (ctor) obj = _objc_constructOrFree(obj, cls); - + if (ctor) { + obj = object_cxxConstructFromClass(obj, cls, + OBJECT_CONSTRUCT_FREE_ONFAILURE); + } if (obj) { results[i-shift] = obj; } else { @@ -1043,11 +906,11 @@ copyPropertyAttributeString(const objc_property_attribute_t *attrs, #if DEBUG // debug build: sanitize input for (i = 0; i < count; i++) { - assert(attrs[i].name); - assert(strlen(attrs[i].name) > 0); - assert(! strchr(attrs[i].name, ',')); - assert(! strchr(attrs[i].name, '"')); - if (attrs[i].value) assert(! strchr(attrs[i].value, ',')); + ASSERT(attrs[i].name); + ASSERT(strlen(attrs[i].name) > 0); + ASSERT(! strchr(attrs[i].name, ',')); + ASSERT(! strchr(attrs[i].name, '"')); + if (attrs[i].value) ASSERT(! strchr(attrs[i].value, ',')); } #endif @@ -1133,8 +996,8 @@ iteratePropertyAttributes(const char *attrs, const char *nameStart; const char *nameEnd; - assert(start < end); - assert(*start); + ASSERT(start < end); + ASSERT(*start); if (*start != '\"') { // single-char short name nameStart = start; @@ -1154,7 +1017,7 @@ iteratePropertyAttributes(const char *attrs, const char *valueStart; const char *valueEnd; - assert(start <= end); + ASSERT(start <= end); valueStart = start; valueEnd = end; @@ -1231,8 +1094,8 @@ copyPropertyAttributeList(const char *attrs, unsigned int *outCount) attrcount = iteratePropertyAttributes(attrs, copyOneAttribute, &ra, &rs); - assert((uint8_t *)(ra+1) <= (uint8_t *)result+size); - assert((uint8_t *)rs <= (uint8_t *)result+size); + ASSERT((uint8_t *)(ra+1) <= (uint8_t *)result+size); + ASSERT((uint8_t *)rs <= (uint8_t *)result+size); if (attrcount == 0) { free(result); diff --git a/runtime/objc-config.h b/runtime/objc-config.h index 91a458f..9bc9fc1 100644 --- a/runtime/objc-config.h +++ b/runtime/objc-config.h @@ -26,6 +26,15 @@ #include +// Define __OBJC2__ for the benefit of our asm files. +#ifndef __OBJC2__ +# if TARGET_OS_OSX && !TARGET_OS_IOSMAC && __i386__ + // old ABI +# else +# define __OBJC2__ 1 +# endif +#endif + // Avoid the !NDEBUG double negative. #if !NDEBUG # define DEBUG 1 @@ -42,7 +51,7 @@ #endif // Define SUPPORT_ZONES=1 to enable malloc zone support in NXHashTable. -#if !TARGET_OS_OSX +#if !(TARGET_OS_OSX || TARGET_OS_IOSMAC) # define SUPPORT_ZONES 0 #else # define SUPPORT_ZONES 1 @@ -56,7 +65,7 @@ #endif // Define SUPPORT_PREOPT=1 to enable dyld shared cache optimizations -#if TARGET_OS_WIN32 || TARGET_OS_SIMULATOR +#if TARGET_OS_WIN32 # define SUPPORT_PREOPT 0 #else # define SUPPORT_PREOPT 1 @@ -73,7 +82,7 @@ // Define SUPPORT_MSB_TAGGED_POINTERS to use the MSB // as the tagged pointer marker instead of the LSB. // Be sure to edit tagged pointer SPI in objc-internal.h as well. -#if !SUPPORT_TAGGED_POINTERS || !TARGET_OS_IPHONE +#if !SUPPORT_TAGGED_POINTERS || (TARGET_OS_OSX || TARGET_OS_IOSMAC) # define SUPPORT_MSB_TAGGED_POINTERS 0 #else # define SUPPORT_MSB_TAGGED_POINTERS 1 @@ -83,7 +92,7 @@ // field as an index into a class table. // Note, keep this in sync with any .s files which also define it. // Be sure to edit objc-abi.h as well. -#if __ARM_ARCH_7K__ >= 2 +#if __ARM_ARCH_7K__ >= 2 || (__arm64__ && !__LP64__) # define SUPPORT_INDEXED_ISA 1 #else # define SUPPORT_INDEXED_ISA 0 @@ -91,7 +100,8 @@ // Define SUPPORT_PACKED_ISA=1 on platforms that store the class in the isa // field as a maskable pointer with other data around it. -#if (!__LP64__ || TARGET_OS_WIN32 || TARGET_OS_SIMULATOR) +#if (!__LP64__ || TARGET_OS_WIN32 || \ + (TARGET_OS_SIMULATOR && !TARGET_OS_IOSMAC)) # define SUPPORT_PACKED_ISA 0 #else # define SUPPORT_PACKED_ISA 1 @@ -125,7 +135,7 @@ // Define SUPPORT_ALT_HANDLERS if you're using zero-cost exceptions // but also need to support AppKit's alt-handler scheme // Be sure to edit objc-exception.h as well (objc_add/removeExceptionHandler) -#if !SUPPORT_ZEROCOST_EXCEPTIONS || TARGET_OS_IPHONE || TARGET_OS_EMBEDDED +#if !SUPPORT_ZEROCOST_EXCEPTIONS || !TARGET_OS_OSX # define SUPPORT_ALT_HANDLERS 0 #else # define SUPPORT_ALT_HANDLERS 1 @@ -146,17 +156,18 @@ #endif // Define SUPPORT_MESSAGE_LOGGING to enable NSObjCMessageLoggingEnabled -#if TARGET_OS_WIN32 || TARGET_OS_EMBEDDED +#if !TARGET_OS_OSX # define SUPPORT_MESSAGE_LOGGING 0 #else # define SUPPORT_MESSAGE_LOGGING 1 #endif -// Define SUPPORT_QOS_HACK to work around deadlocks due to QoS bugs. -#if !__OBJC2__ || TARGET_OS_WIN32 || DARLING -# define SUPPORT_QOS_HACK 0 +// Define HAVE_TASK_RESTARTABLE_RANGES to enable usage of +// task_restartable_ranges_synchronize() +#if TARGET_OS_SIMULATOR || defined(__i386__) || defined(__arm__) || !TARGET_OS_MAC +# define HAVE_TASK_RESTARTABLE_RANGES 0 #else -# define SUPPORT_QOS_HACK 1 +# define HAVE_TASK_RESTARTABLE_RANGES 1 #endif // OBJC_INSTRUMENTED controls whether message dispatching is dynamically @@ -167,4 +178,43 @@ // because objc-class.h is public and objc-config.h is not. //#define OBJC_INSTRUMENTED +// In __OBJC2__, the runtimeLock is a mutex always held +// hence the cache lock is redundant and can be elided. +// +// If the runtime lock ever becomes a rwlock again, +// the cache lock would need to be used again +#if __OBJC2__ +#define CONFIG_USE_CACHE_LOCK 0 +#else +#define CONFIG_USE_CACHE_LOCK 1 +#endif + +// Determine how the method cache stores IMPs. +#define CACHE_IMP_ENCODING_NONE 1 // Method cache contains raw IMP. +#define CACHE_IMP_ENCODING_ISA_XOR 2 // Method cache contains ISA ^ IMP. +#define CACHE_IMP_ENCODING_PTRAUTH 3 // Method cache contains ptrauth'd IMP. + +#if __PTRAUTH_INTRINSICS__ +// Always use ptrauth when it's supported. +#define CACHE_IMP_ENCODING CACHE_IMP_ENCODING_PTRAUTH +#elif defined(__arm__) +// 32-bit ARM uses no encoding. +#define CACHE_IMP_ENCODING CACHE_IMP_ENCODING_NONE +#else +// Everything else uses ISA ^ IMP. +#define CACHE_IMP_ENCODING CACHE_IMP_ENCODING_ISA_XOR +#endif + +#define CACHE_MASK_STORAGE_OUTLINED 1 +#define CACHE_MASK_STORAGE_HIGH_16 2 +#define CACHE_MASK_STORAGE_LOW_4 3 + +#if defined(__arm64__) && __LP64__ +#define CACHE_MASK_STORAGE CACHE_MASK_STORAGE_HIGH_16 +#elif defined(__arm64__) && !__LP64__ +#define CACHE_MASK_STORAGE CACHE_MASK_STORAGE_LOW_4 +#else +#define CACHE_MASK_STORAGE CACHE_MASK_STORAGE_OUTLINED +#endif + #endif diff --git a/runtime/objc-env.h b/runtime/objc-env.h index 7eb0afa..ccdceb6 100644 --- a/runtime/objc-env.h +++ b/runtime/objc-env.h @@ -22,8 +22,9 @@ OPTION( PrintAltHandlers, OBJC_PRINT_ALT_HANDLERS, "log processi OPTION( PrintReplacedMethods, OBJC_PRINT_REPLACED_METHODS, "log methods replaced by category implementations") OPTION( PrintDeprecation, OBJC_PRINT_DEPRECATION_WARNINGS, "warn about calls to deprecated runtime functions") OPTION( PrintPoolHiwat, OBJC_PRINT_POOL_HIGHWATER, "log high-water marks for autorelease pools") -OPTION( PrintCustomRR, OBJC_PRINT_CUSTOM_RR, "log classes with un-optimized custom retain/release methods") -OPTION( PrintCustomAWZ, OBJC_PRINT_CUSTOM_AWZ, "log classes with un-optimized custom allocWithZone methods") +OPTION( PrintCustomCore, OBJC_PRINT_CUSTOM_CORE, "log classes with custom core methods") +OPTION( PrintCustomRR, OBJC_PRINT_CUSTOM_RR, "log classes with custom retain/release methods") +OPTION( PrintCustomAWZ, OBJC_PRINT_CUSTOM_AWZ, "log classes with custom allocWithZone methods") OPTION( PrintRawIsa, OBJC_PRINT_RAW_ISA, "log classes that require raw pointer isa fields") OPTION( DebugUnload, OBJC_DEBUG_UNLOAD, "warn about poorly-behaving bundles when unloaded") @@ -39,4 +40,6 @@ OPTION( DebugDontCrash, OBJC_DEBUG_DONT_CRASH, "halt the pro OPTION( DisableVtables, OBJC_DISABLE_VTABLES, "disable vtable dispatch") OPTION( DisablePreopt, OBJC_DISABLE_PREOPTIMIZATION, "disable preoptimization courtesy of dyld shared cache") OPTION( DisableTaggedPointers, OBJC_DISABLE_TAGGED_POINTERS, "disable tagged pointer optimization of NSNumber et al.") +OPTION( DisableTaggedPointerObfuscation, OBJC_DISABLE_TAG_OBFUSCATION, "disable obfuscation of tagged pointers") OPTION( DisableNonpointerIsa, OBJC_DISABLE_NONPOINTER_ISA, "disable non-pointer isa fields") +OPTION( DisableInitializeForkSafety, OBJC_DISABLE_INITIALIZE_FORK_SAFETY, "disable safety checks for +initialize after fork") diff --git a/runtime/objc-errors.mm b/runtime/objc-errors.mm index 6797f38..17fdda2 100644 --- a/runtime/objc-errors.mm +++ b/runtime/objc-errors.mm @@ -76,8 +76,6 @@ void _objc_error(id rcv, const char *fmt, va_list args) #include <_simple.h> -OBJC_EXPORT void (*_error)(id, const char *, va_list); - // Return true if c is a UTF8 continuation byte static bool isUTF8Continuation(char c) { @@ -85,7 +83,7 @@ static bool isUTF8Continuation(char c) } // Add "message" to any forthcoming crash log. -static mutex_t crashlog_lock; +mutex_t crashlog_lock; static void _objc_crashlog(const char *message) { char *newmsg; @@ -160,7 +158,7 @@ static void _objc_syslog(const char *message) #if !__OBJC2__ // used by ExceptionHandling.framework #endif -__attribute__((noreturn)) +__attribute__((noreturn, cold)) void _objc_error(id self, const char *fmt, va_list ap) { char *buf; @@ -183,7 +181,7 @@ void __objc_error(id rcv, const char *fmt, ...) va_end(vp); } -static __attribute__((noreturn)) +static __attribute__((noreturn, cold)) void _objc_fatalv(uint64_t reason, uint64_t flags, const char *fmt, va_list ap) { char *buf1; @@ -200,8 +198,8 @@ void _objc_fatalv(uint64_t reason, uint64_t flags, const char *fmt, va_list ap) _Exit(1); } else { - // abort_with_reason(OS_REASON_OBJC, reason, buf1, flags); - abort(); + _objc_crashlog(buf1); + abort_with_reason(OS_REASON_OBJC, reason, buf1, flags); } } @@ -218,7 +216,7 @@ void _objc_fatal(const char *fmt, ...) va_list ap; va_start(ap,fmt); _objc_fatalv(OBJC_EXIT_REASON_UNSPECIFIED, - /*OS_REASON_FLAG_ONE_TIME_FAILURE*/ 0, + OS_REASON_FLAG_ONE_TIME_FAILURE, fmt, ap); } diff --git a/runtime/objc-exception.h b/runtime/objc-exception.h index c67b92a..c93b47b 100644 --- a/runtime/objc-exception.h +++ b/runtime/objc-exception.h @@ -31,51 +31,57 @@ // compiler reserves a setjmp buffer + 4 words as localExceptionData -OBJC_EXPORT void objc_exception_throw(id exception) - __OSX_AVAILABLE(10.3) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; -OBJC_EXPORT void objc_exception_try_enter(void *localExceptionData) - __OSX_AVAILABLE(10.3) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; -OBJC_EXPORT void objc_exception_try_exit(void *localExceptionData) - __OSX_AVAILABLE(10.3) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; -OBJC_EXPORT id objc_exception_extract(void *localExceptionData) - __OSX_AVAILABLE(10.3) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; -OBJC_EXPORT int objc_exception_match(Class exceptionClass, id exception) - __OSX_AVAILABLE(10.3) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; +OBJC_EXPORT void +objc_exception_throw(id _Nonnull exception) + OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE(10.3); + +OBJC_EXPORT void +objc_exception_try_enter(void * _Nonnull localExceptionData) + OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE(10.3); + +OBJC_EXPORT void +objc_exception_try_exit(void * _Nonnull localExceptionData) + OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE(10.3); + +OBJC_EXPORT id _Nonnull +objc_exception_extract(void * _Nonnull localExceptionData) + OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE(10.3); + +OBJC_EXPORT int objc_exception_match(Class _Nonnull exceptionClass, + id _Nonnull exception) + OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE(10.3); typedef struct { int version; - void (*throw_exc)(id); // version 0 - void (*try_enter)(void *); // version 0 - void (*try_exit)(void *); // version 0 - id (*extract)(void *); // version 0 - int (*match)(Class, id); // version 0 + void (* _Nonnull throw_exc)(id _Nonnull); // version 0 + void (* _Nonnull try_enter)(void * _Nonnull); // version 0 + void (* _Nonnull try_exit)(void * _Nonnull); // version 0 + id _Nonnull (* _Nonnull extract)(void * _Nonnull); // version 0 + int (* _Nonnull match)(Class _Nonnull, id _Nonnull); // version 0 } objc_exception_functions_t; // get table; version tells how many -OBJC_EXPORT void objc_exception_get_functions(objc_exception_functions_t *table) - __OSX_AVAILABLE(10.3) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; +OBJC_EXPORT void +objc_exception_get_functions(objc_exception_functions_t * _Nullable table) + OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE(10.3); // set table -OBJC_EXPORT void objc_exception_set_functions(objc_exception_functions_t *table) - __OSX_AVAILABLE(10.3) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; +OBJC_EXPORT void +objc_exception_set_functions(objc_exception_functions_t * _Nullable table) + OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE(10.3); // !__OBJC2__ #else // __OBJC2__ -typedef id (*objc_exception_preprocessor)(id exception); -typedef int (*objc_exception_matcher)(Class catch_type, id exception); -typedef void (*objc_uncaught_exception_handler)(id exception); -typedef void (*objc_exception_handler)(id unused, void *context); +typedef id _Nonnull (*objc_exception_preprocessor)(id _Nonnull exception); +typedef int (*objc_exception_matcher)(Class _Nonnull catch_type, + id _Nonnull exception); +typedef void (*objc_uncaught_exception_handler)(id _Null_unspecified /* _Nonnull */ exception); +typedef void (*objc_exception_handler)(id _Nullable unused, + void * _Nullable context); /** * Throw a runtime exception. This function is inserted by the compiler @@ -83,31 +89,47 @@ typedef void (*objc_exception_handler)(id unused, void *context); * * @param exception The exception to be thrown. */ -OBJC_EXPORT void objc_exception_throw(id exception) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); -OBJC_EXPORT void objc_exception_rethrow(void) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); -OBJC_EXPORT id objc_begin_catch(void *exc_buf) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); -OBJC_EXPORT void objc_end_catch(void) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); -OBJC_EXPORT void objc_terminate(void) - OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0); +OBJC_EXPORT void +objc_exception_throw(id _Nonnull exception) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); -OBJC_EXPORT objc_exception_preprocessor objc_setExceptionPreprocessor(objc_exception_preprocessor fn) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); -OBJC_EXPORT objc_exception_matcher objc_setExceptionMatcher(objc_exception_matcher fn) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); -OBJC_EXPORT objc_uncaught_exception_handler objc_setUncaughtExceptionHandler(objc_uncaught_exception_handler fn) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +objc_exception_rethrow(void) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT id _Nonnull +objc_begin_catch(void * _Nonnull exc_buf) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +objc_end_catch(void) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT void +objc_terminate(void) + OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT objc_exception_preprocessor _Nonnull +objc_setExceptionPreprocessor(objc_exception_preprocessor _Nonnull fn) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT objc_exception_matcher _Nonnull +objc_setExceptionMatcher(objc_exception_matcher _Nonnull fn) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT objc_uncaught_exception_handler _Nonnull +objc_setUncaughtExceptionHandler(objc_uncaught_exception_handler _Nonnull fn) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); // Not for iOS. -OBJC_EXPORT uintptr_t objc_addExceptionHandler(objc_exception_handler fn, void *context) - __OSX_AVAILABLE(10.5) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; -OBJC_EXPORT void objc_removeExceptionHandler(uintptr_t token) - __OSX_AVAILABLE(10.5) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; +OBJC_EXPORT uintptr_t +objc_addExceptionHandler(objc_exception_handler _Nonnull fn, + void * _Nullable context) + OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE(10.5); + +OBJC_EXPORT void +objc_removeExceptionHandler(uintptr_t token) + OBJC_OSX_AVAILABLE_OTHERS_UNAVAILABLE(10.5); // __OBJC2__ #endif diff --git a/runtime/objc-exception.mm b/runtime/objc-exception.mm index d6b1d83..6c318c6 100644 --- a/runtime/objc-exception.mm +++ b/runtime/objc-exception.mm @@ -133,8 +133,8 @@ typedef struct _threadChain { static ThreadChainLink_t ThreadChainLink; static ThreadChainLink_t *getChainLink() { - // follow links until thread_self() found (someday) XXX - objc_thread_t self = thread_self(); + // follow links until objc_thread_self() found (someday) XXX + objc_thread_t self = objc_thread_self(); ThreadChainLink_t *walker = &ThreadChainLink; while (walker->perThreadID != self) { if (walker->next != nil) { @@ -238,6 +238,7 @@ void _destroyAltHandlerList(struct alt_handler_list *list) **********************************************************************/ #include "objc-private.h" +#include #include #include #include @@ -310,7 +311,7 @@ CXX_PERSONALITY(int version, struct objc_typeinfo { // Position of vtable and name fields must match C++ typeinfo object - const void **vtable; // always objc_ehtype_vtable+2 + const void ** __ptrauth_cxx_vtable_pointer vtable; // objc_ehtype_vtable+2 const char *name; // c++ typeinfo string Class cls_unremapped; @@ -321,54 +322,97 @@ struct objc_exception { struct objc_typeinfo tinfo; }; +extern "C" { -static void _objc_exception_noop(void) { } -static bool _objc_exception_false(void) { return 0; } -// static bool _objc_exception_true(void) { return 1; } -static void _objc_exception_abort1(void) { - _objc_fatal("unexpected call into objc exception typeinfo vtable %d", 1); -} -static void _objc_exception_abort2(void) { - _objc_fatal("unexpected call into objc exception typeinfo vtable %d", 2); -} -static void _objc_exception_abort3(void) { - _objc_fatal("unexpected call into objc exception typeinfo vtable %d", 3); -} -static void _objc_exception_abort4(void) { - _objc_fatal("unexpected call into objc exception typeinfo vtable %d", 4); -} +__attribute__((used)) +void _objc_exception_noop(void) { } +__attribute__((used)) +bool _objc_exception_false(void) { return 0; } +// bool _objc_exception_true(void) { return 1; } +__attribute__((used)) +void _objc_exception_abort1(void) { + _objc_fatal("unexpected call into objc exception typeinfo vtable %d", 1); +} +__attribute__((used)) +void _objc_exception_abort2(void) { + _objc_fatal("unexpected call into objc exception typeinfo vtable %d", 2); +} +__attribute__((used)) +void _objc_exception_abort3(void) { + _objc_fatal("unexpected call into objc exception typeinfo vtable %d", 3); +} +__attribute__((used)) +void _objc_exception_abort4(void) { + _objc_fatal("unexpected call into objc exception typeinfo vtable %d", 4); +} +__attribute__((used)) +bool _objc_exception_do_catch(struct objc_typeinfo *catch_tinfo, + struct objc_typeinfo *throw_tinfo, + void **throw_obj_p, + unsigned outer); +} -static bool _objc_exception_do_catch(struct objc_typeinfo *catch_tinfo, - struct objc_typeinfo *throw_tinfo, - void **throw_obj_p, - unsigned outer); +// C++ pointers to vtables are signed with no extra data. +// C++ vtable entries are signed with a number derived from the function name. +// For this fake vtable, we hardcode number as deciphered from the +// assembly output during libc++abi's build. +#if __has_feature(ptrauth_calls) +# define VTABLE_PTR_AUTH "@AUTH(da, 0)" +# define VTABLE_ENTRY_AUTH(x) "@AUTH(ia," #x ",addr)" +#else +# define VTABLE_PTR_AUTH "" +# define VTABLE_ENTRY_AUTH(x) "" +#endif -// forward declaration -OBJC_EXPORT struct objc_typeinfo OBJC_EHTYPE_id; +#if __LP64__ +# define PTR ".quad " +# define TWOPTRSIZE "16" +#else +# define PTR ".long " +# define TWOPTRSIZE "8" +#endif -OBJC_EXPORT -const void *objc_ehtype_vtable[] = { - nil, // typeinfo's vtable? - fixme - (void*)&OBJC_EHTYPE_id, // typeinfo's typeinfo - hack - (void*)_objc_exception_noop, // in-place destructor? - (void*)_objc_exception_noop, // destructor? - (void*)_objc_exception_false, // OLD __is_pointer_p - (void*)_objc_exception_false, // OLD __is_function_p - (void*)_objc_exception_do_catch, // OLD __do_catch, NEW can_catch - (void*)_objc_exception_false, // OLD __do_upcast, NEW search_above_dst - (void*)_objc_exception_false, // NEW search_below_dst - (void*)_objc_exception_abort1, // paranoia: blow up if libc++abi - (void*)_objc_exception_abort2, // adds something new - (void*)_objc_exception_abort3, - (void*)_objc_exception_abort4, -}; +// Hand-built vtable for objc exception typeinfo. +// "OLD" is GNU libcpp, "NEW" is libc++abi. -OBJC_EXPORT -struct objc_typeinfo OBJC_EHTYPE_id = { - objc_ehtype_vtable+2, - "id", - nil -}; +asm( + "\n .cstring" + "\n l_.id_str: .asciz \"id\"" + + "\n .section __DATA,__const" + "\n .globl _OBJC_EHTYPE_id" + "\n .globl _objc_ehtype_vtable" + "\n .p2align 4" + + "\n _OBJC_EHTYPE_id:" + "\n " PTR "(_objc_ehtype_vtable+" TWOPTRSIZE ") " VTABLE_PTR_AUTH + "\n " PTR "l_.id_str" + "\n " PTR "0" + + "\n _objc_ehtype_vtable:" + "\n " PTR "0" + // typeinfo's typeinfo - fixme hack + "\n " PTR "_OBJC_EHTYPE_id" + // destructor and in-place destructor + "\n " PTR "__objc_exception_noop" VTABLE_ENTRY_AUTH(52634) + "\n " PTR "__objc_exception_noop" VTABLE_ENTRY_AUTH(10344) + // OLD __is_pointer_p + "\n " PTR "__objc_exception_noop" VTABLE_ENTRY_AUTH(6889) + // OLD __is_function_p + "\n " PTR "__objc_exception_noop" VTABLE_ENTRY_AUTH(23080) + // OLD __do_catch, NEW can_catch + "\n " PTR "__objc_exception_do_catch" VTABLE_ENTRY_AUTH(27434) + // OLD __do_upcast, NEW search_above_dst + "\n " PTR "__objc_exception_false" VTABLE_ENTRY_AUTH(48481) + // NEW search_below_dst + "\n " PTR "__objc_exception_false" VTABLE_ENTRY_AUTH(41165) + // NEW has_unambiguous_public_base (fixme need this?) + "\n " PTR "__objc_exception_abort1" VTABLE_ENTRY_AUTH(14357) + // paranoia: die if libcxxabi adds anything else + "\n " PTR "__objc_exception_abort2" + "\n " PTR "__objc_exception_abort3" + "\n " PTR "__objc_exception_abort4" + ); @@ -583,10 +627,10 @@ void objc_end_catch(void) // `outer` is not passed by the new libcxxabi -static bool _objc_exception_do_catch(struct objc_typeinfo *catch_tinfo, - struct objc_typeinfo *throw_tinfo, - void **throw_obj_p, - unsigned outer UNAVAILABLE_ATTRIBUTE) +bool _objc_exception_do_catch(struct objc_typeinfo *catch_tinfo, + struct objc_typeinfo *throw_tinfo, + void **throw_obj_p, + unsigned outer UNAVAILABLE_ATTRIBUTE) { id exception; @@ -1059,7 +1103,6 @@ struct alt_handler_list { struct alt_handler_list *next_DEBUGONLY; }; -static mutex_t DebugLock; static struct alt_handler_list *DebugLists; static uintptr_t DebugCounter; @@ -1080,7 +1123,7 @@ fetch_handler_list(bool create) if (DebugAltHandlers) { // Save this list so the debug code can find it from other threads - mutex_locker_t lock(DebugLock); + mutex_locker_t lock(AltHandlerDebugLock); list->next_DEBUGONLY = DebugLists; DebugLists = list; } @@ -1095,7 +1138,7 @@ void _destroyAltHandlerList(struct alt_handler_list *list) if (list) { if (DebugAltHandlers) { // Detach from the list-of-lists. - mutex_locker_t lock(DebugLock); + mutex_locker_t lock(AltHandlerDebugLock); struct alt_handler_list **listp = &DebugLists; while (*listp && *listp != list) listp = &(*listp)->next_DEBUGONLY; if (*listp) *listp = (*listp)->next_DEBUGONLY; @@ -1160,7 +1203,7 @@ uintptr_t objc_addExceptionHandler(objc_exception_handler fn, void *context) if (DebugAltHandlers) { // Record backtrace in case this handler is misused later. - mutex_locker_t lock(DebugLock); + mutex_locker_t lock(AltHandlerDebugLock); token = DebugCounter++; if (token == 0) token = DebugCounter++; @@ -1172,9 +1215,9 @@ uintptr_t objc_addExceptionHandler(objc_exception_handler fn, void *context) bzero(data->debug, sizeof(*data->debug)); } - pthread_getname_np(pthread_self(), data->debug->thread, THREADNAME_COUNT); - strlcpy(data->debug->queue, - dispatch_queue_get_label(dispatch_get_current_queue()), + pthread_getname_np(objc_thread_self(), data->debug->thread, THREADNAME_COUNT); + strlcpy(data->debug->queue, + dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), THREADNAME_COUNT); data->debug->backtraceSize = backtrace(data->debug->backtrace, BACKTRACE_COUNT); @@ -1274,7 +1317,7 @@ void alt_handler_error(uintptr_t token) "or break in objc_alt_handler_error() to debug."); if (DebugAltHandlers) { - DebugLock.lock(); + AltHandlerDebugLock.lock(); // Search other threads' alt handler lists for this handler. struct alt_handler_list *list; @@ -1314,7 +1357,7 @@ void alt_handler_error(uintptr_t token) } } done: - DebugLock.unlock(); + AltHandlerDebugLock.unlock(); } @@ -1394,3 +1437,6 @@ void exception_init(void) // __OBJC2__ #endif + +// Define this everywhere even if it isn't used, to simplify fork() safety code +mutex_t AltHandlerDebugLock; diff --git a/runtime/objc-file-old.h b/runtime/objc-file-old.h index 3feb82b..9799c5e 100644 --- a/runtime/objc-file-old.h +++ b/runtime/objc-file-old.h @@ -40,8 +40,8 @@ extern struct old_protocol **_getObjcProtocols(const header_info *hi, size_t *np extern Class *_getObjcClassRefs(const header_info *hi, size_t *nclasses); extern const char *_getObjcClassNames(const header_info *hi, size_t *size); -using Initializer = void(*)(void); -extern Initializer* getLibobjcInitializers(const headerType *mhdr, size_t *count); +using UnsignedInitializer = void(*)(void); +extern UnsignedInitializer* getLibobjcInitializers(const headerType *mhdr, size_t *count); __END_DECLS diff --git a/runtime/objc-file-old.mm b/runtime/objc-file-old.mm index 8e170d4..67889ec 100644 --- a/runtime/objc-file-old.mm +++ b/runtime/objc-file-old.mm @@ -88,7 +88,7 @@ GETSECT(_getObjcSelectorRefs, SEL, "__OBJC", "__message_refs"); GETSECT(_getObjcClassRefs, Class, "__OBJC", "__cls_refs"); GETSECT(_getObjcClassNames, const char, "__OBJC", "__class_names"); // __OBJC,__class_names section only emitted by CodeWarrior rdar://4951638 -GETSECT(getLibobjcInitializers, Initializer, "__DATA", "__objc_init_func"); +GETSECT(getLibobjcInitializers, UnsignedInitializer, "__DATA", "__objc_init_func"); objc_image_info * diff --git a/runtime/objc-file.h b/runtime/objc-file.h index 5e78b61..23c0da1 100644 --- a/runtime/objc-file.h +++ b/runtime/objc-file.h @@ -30,22 +30,70 @@ // classref_t is not fixed up at launch; use remapClass() to convert +// classlist and catlist and protolist sections are marked const here +// because those sections may be in read-only __DATA_CONST segments. + extern SEL *_getObjc2SelectorRefs(const header_info *hi, size_t *count); extern message_ref_t *_getObjc2MessageRefs(const header_info *hi, size_t *count); extern Class*_getObjc2ClassRefs(const header_info *hi, size_t *count); extern Class*_getObjc2SuperRefs(const header_info *hi, size_t *count); -extern classref_t *_getObjc2ClassList(const header_info *hi, size_t *count); -extern classref_t *_getObjc2NonlazyClassList(const header_info *hi, size_t *count); -extern category_t **_getObjc2CategoryList(const header_info *hi, size_t *count); -extern category_t **_getObjc2NonlazyCategoryList(const header_info *hi, size_t *count); -extern protocol_t **_getObjc2ProtocolList(const header_info *hi, size_t *count); +extern classref_t const *_getObjc2ClassList(const header_info *hi, size_t *count); +extern classref_t const *_getObjc2NonlazyClassList(const header_info *hi, size_t *count); +extern category_t * const *_getObjc2CategoryList(const header_info *hi, size_t *count); +extern category_t * const *_getObjc2CategoryList2(const header_info *hi, size_t *count); +extern category_t * const *_getObjc2NonlazyCategoryList(const header_info *hi, size_t *count); +extern protocol_t * const *_getObjc2ProtocolList(const header_info *hi, size_t *count); extern protocol_t **_getObjc2ProtocolRefs(const header_info *hi, size_t *count); -using Initializer = void(*)(void); -extern Initializer* getLibobjcInitializers(const header_info *hi, size_t *count); -extern classref_t *_getObjc2NonlazyClassList(const headerType *mhdr, size_t *count); -extern category_t **_getObjc2NonlazyCategoryList(const headerType *mhdr, size_t *count); -extern Initializer* getLibobjcInitializers(const headerType *mhdr, size_t *count); +// FIXME: rdar://29241917&33734254 clang doesn't sign static initializers. +struct UnsignedInitializer { +private: + uintptr_t storage; +public: + void operator () () const { + using Initializer = void(*)(); + Initializer init = + ptrauth_sign_unauthenticated((Initializer)storage, + ptrauth_key_function_pointer, 0); + init(); + } +}; + +extern UnsignedInitializer *getLibobjcInitializers(const header_info *hi, size_t *count); + +extern classref_t const *_getObjc2NonlazyClassList(const headerType *mhdr, size_t *count); +extern category_t * const *_getObjc2NonlazyCategoryList(const headerType *mhdr, size_t *count); +extern UnsignedInitializer *getLibobjcInitializers(const headerType *mhdr, size_t *count); + +static inline void +foreach_data_segment(const headerType *mhdr, + std::function code) +{ + intptr_t slide = 0; + + // compute VM slide + const segmentType *seg = (const segmentType *) (mhdr + 1); + for (unsigned long i = 0; i < mhdr->ncmds; i++) { + if (seg->cmd == SEGMENT_CMD && + segnameEquals(seg->segname, "__TEXT")) + { + slide = (char *)mhdr - (char *)seg->vmaddr; + break; + } + seg = (const segmentType *)((char *)seg + seg->cmdsize); + } + + // enumerate __DATA* segments + seg = (const segmentType *) (mhdr + 1); + for (unsigned long i = 0; i < mhdr->ncmds; i++) { + if (seg->cmd == SEGMENT_CMD && + segnameStartsWith(seg->segname, "__DATA")) + { + code(seg, slide); + } + seg = (const segmentType *)((char *)seg + seg->cmdsize); + } +} #endif diff --git a/runtime/objc-file.mm b/runtime/objc-file.mm index c9ec260..ffde2fd 100644 --- a/runtime/objc-file.mm +++ b/runtime/objc-file.mm @@ -59,13 +59,14 @@ GETSECT(_getObjc2SelectorRefs, SEL, "__objc_selrefs"); GETSECT(_getObjc2MessageRefs, message_ref_t, "__objc_msgrefs"); GETSECT(_getObjc2ClassRefs, Class, "__objc_classrefs"); GETSECT(_getObjc2SuperRefs, Class, "__objc_superrefs"); -GETSECT(_getObjc2ClassList, classref_t, "__objc_classlist"); -GETSECT(_getObjc2NonlazyClassList, classref_t, "__objc_nlclslist"); -GETSECT(_getObjc2CategoryList, category_t *, "__objc_catlist"); -GETSECT(_getObjc2NonlazyCategoryList, category_t *, "__objc_nlcatlist"); -GETSECT(_getObjc2ProtocolList, protocol_t *, "__objc_protolist"); +GETSECT(_getObjc2ClassList, classref_t const, "__objc_classlist"); +GETSECT(_getObjc2NonlazyClassList, classref_t const, "__objc_nlclslist"); +GETSECT(_getObjc2CategoryList, category_t * const, "__objc_catlist"); +GETSECT(_getObjc2CategoryList2, category_t * const, "__objc_catlist2"); +GETSECT(_getObjc2NonlazyCategoryList, category_t * const, "__objc_nlcatlist"); +GETSECT(_getObjc2ProtocolList, protocol_t * const, "__objc_protolist"); GETSECT(_getObjc2ProtocolRefs, protocol_t *, "__objc_protorefs"); -GETSECT(getLibobjcInitializers, Initializer, "__objc_init_func"); +GETSECT(getLibobjcInitializers, UnsignedInitializer, "__objc_init_func"); objc_image_info * @@ -75,31 +76,15 @@ _getObjcImageInfo(const headerType *mhdr, size_t *outBytes) outBytes, nil); } - -static const segmentType * -getsegbynamefromheader(const headerType *mhdr, const char *segname) -{ - const segmentType *seg = (const segmentType *) (mhdr + 1); - for (unsigned long i = 0; i < mhdr->ncmds; i++){ - if (seg->cmd == SEGMENT_CMD && segnameEquals(seg->segname, segname)) { - return seg; - } - seg = (const segmentType *)((char *)seg + seg->cmdsize); - } - return nil; -} - // Look for an __objc* section other than __objc_imageinfo static bool segmentHasObjcContents(const segmentType *seg) { - if (seg) { - for (uint32_t i = 0; i < seg->nsects; i++) { - const sectionType *sect = ((const sectionType *)(seg+1))+i; - if (sectnameStartsWith(sect->sectname, "__objc_") && - !sectnameEquals(sect->sectname, "__objc_imageinfo")) - { - return true; - } + for (uint32_t i = 0; i < seg->nsects; i++) { + const sectionType *sect = ((const sectionType *)(seg+1))+i; + if (sectnameStartsWith(sect->sectname, "__objc_") && + !sectnameEquals(sect->sectname, "__objc_imageinfo")) + { + return true; } } @@ -110,16 +95,15 @@ static bool segmentHasObjcContents(const segmentType *seg) bool _hasObjcContents(const header_info *hi) { - const segmentType *data = - getsegbynamefromheader(hi->mhdr(), "__DATA"); - const segmentType *data_const = - getsegbynamefromheader(hi->mhdr(), "__DATA_CONST"); - const segmentType *data_dirty = - getsegbynamefromheader(hi->mhdr(), "__DATA_DIRTY"); + bool foundObjC = false; + + foreach_data_segment(hi->mhdr(), [&](const segmentType *seg, intptr_t slide) + { + if (segmentHasObjcContents(seg)) foundObjC = true; + }); + + return foundObjC; - return segmentHasObjcContents(data) - || segmentHasObjcContents(data_const) - || segmentHasObjcContents(data_dirty); } diff --git a/runtime/objc-gdb.h b/runtime/objc-gdb.h index 0c01590..9cab4a3 100644 --- a/runtime/objc-gdb.h +++ b/runtime/objc-gdb.h @@ -34,7 +34,9 @@ #ifdef __APPLE_API_PRIVATE -#define _OBJC_PRIVATE_H_ +#ifndef _OBJC_PRIVATE_H_ +# define _OBJC_PRIVATE_H_ +#endif #include #include #include @@ -47,17 +49,30 @@ __BEGIN_DECLS **********************************************************************/ // Return cls if it's a valid class, or crash. -OBJC_EXPORT Class gdb_class_getClass(Class cls) +OBJC_EXPORT Class _Nonnull +gdb_class_getClass(Class _Nonnull cls) #if __OBJC2__ - OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0); + OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0); #else - OBJC_AVAILABLE(10.7, 3.1, 9.0, 1.0); + OBJC_AVAILABLE(10.7, 3.1, 9.0, 1.0, 2.0); #endif // Same as gdb_class_getClass(object_getClass(cls)). -OBJC_EXPORT Class gdb_object_getClass(id obj) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT Class _Nonnull gdb_object_getClass(id _Nullable obj) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); +/*********************************************************************** +* Class inspection +**********************************************************************/ + +#if __OBJC2__ + +// Return the raw, mangled name of cls. +OBJC_EXPORT const char * _Nonnull +objc_debug_class_getNameRaw(Class _Nullable cls) +OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); + +#endif /*********************************************************************** * Class lists for heap. @@ -66,15 +81,25 @@ OBJC_EXPORT Class gdb_object_getClass(id obj) #if __OBJC2__ // Maps class name to Class, for in-use classes only. NXStrValueMapPrototype. -OBJC_EXPORT NXMapTable *gdb_objc_realized_classes - OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0); +OBJC_EXPORT NXMapTable * _Nullable gdb_objc_realized_classes + OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0); + +// A generation count of realized classes. Increments when new classes +// are realized. This is NOT an exact count of the number of classes. +// It's not guaranteed by how much it increases when classes are +// realized, only that it increases by something. When classes are +// removed (unloading bundles or destroying dynamically allocated +// classes) the number will also increase to signal that there has +// been a change. +OBJC_EXPORT uintptr_t objc_debug_realized_class_generation_count; #else // Hashes Classes, for all known classes. Custom prototype. -OBJC_EXPORT NXHashTable *_objc_debug_class_hash +OBJC_EXPORT NXHashTable * _Nullable _objc_debug_class_hash __OSX_AVAILABLE(10.2) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + __IOS_UNAVAILABLE __TVOS_UNAVAILABLE + __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE; #endif @@ -88,14 +113,14 @@ OBJC_EXPORT NXHashTable *_objc_debug_class_hash // Extract isa pointer from an isa field. // (Class)(isa & mask) == class pointer OBJC_EXPORT const uintptr_t objc_debug_isa_class_mask - OBJC_AVAILABLE(10.10, 7.0, 9.0, 1.0); + OBJC_AVAILABLE(10.10, 7.0, 9.0, 1.0, 2.0); // Extract magic cookie from an isa field. // (isa & magic_mask) == magic_value OBJC_EXPORT const uintptr_t objc_debug_isa_magic_mask - OBJC_AVAILABLE(10.10, 7.0, 9.0, 1.0); + OBJC_AVAILABLE(10.10, 7.0, 9.0, 1.0, 2.0); OBJC_EXPORT const uintptr_t objc_debug_isa_magic_value - OBJC_AVAILABLE(10.10, 7.0, 9.0, 1.0); + OBJC_AVAILABLE(10.10, 7.0, 9.0, 1.0, 2.0); // Use indexed ISAs for targets which store index of the class in the ISA. // This index can be used to index the array of classes. @@ -109,7 +134,7 @@ OBJC_EXPORT const uintptr_t objc_debug_indexed_isa_index_shift; // And then we can use that index to get the class from this array. Note // the size is provided so that clients can ensure the index they get is in // bounds and not read off the end of the array. -OBJC_EXPORT Class objc_indexed_classes[]; +OBJC_EXPORT Class _Nullable objc_indexed_classes[]; // When we don't have enough bits to store a class*, we can instead store an // index in to this array. Classes are added here when they are realized. @@ -121,6 +146,63 @@ OBJC_EXPORT uintptr_t objc_indexed_classes_count; #endif +/*********************************************************************** +* Class structure decoding +**********************************************************************/ +#if __OBJC2__ + +// Mask for the pointer from class struct to class rw data. +// Other bits may be used for flags. +// Use 0x00007ffffffffff8UL or 0xfffffffcUL when this variable is unavailable. +OBJC_EXPORT const uintptr_t objc_debug_class_rw_data_mask + OBJC_AVAILABLE(10.13, 11.0, 11.0, 4.0, 2.0); + +// The ABI version for the internal runtime representations +// lldb, CoreSymbolication and debugging tools need to know +OBJC_EXTERN const uint32_t objc_class_abi_version + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); + +// the maximum ABI version in existence for now +#define OBJC_CLASS_ABI_VERSION_MAX 1 + +// Used when objc_class_abi_version is absent or 0 +struct class_rw_v0_t { + uint32_t flags; + uint32_t version; + uintptr_t ro; // class_ro_t + uintptr_t methods; // method_array_t + uintptr_t properties; // property_array_t + uintptr_t protocols; // protocol_array_t + Class _Nullable firstSubclass; + Class _Nullable nextSiblingClass; + char *_Nullable demangledName; + + // uint32_t index; // only when indexed-isa is used +}; + +// Used when objc_class_abi_version is 1 +struct class_rw_v1_t { + uint32_t flags; + uint16_t witness; + uint16_t index; // only when indexed-isa is used + uintptr_t ro_or_rw_ext; // tagged union based on the low bit: + // 0: class_ro_t 1: class_rw_ext_t + Class _Nullable firstSubclass; + Class _Nullable nextSiblingClass; +}; + +struct class_rw_ext_v1_t { + uintptr_t ro; // class_ro_t + uintptr_t methods; // method_array_t + uintptr_t properties; // property_array_t + uintptr_t protocols; // protocol_array_t + char *_Nullable demangledName; + uint32_t version; +}; + +#endif + + /*********************************************************************** * Tagged pointer decoding **********************************************************************/ @@ -130,24 +212,30 @@ OBJC_EXPORT uintptr_t objc_indexed_classes_count; // if (obj & mask) obj is a tagged pointer object OBJC_EXPORT uintptr_t objc_debug_taggedpointer_mask - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0); + +// tagged pointers are obfuscated by XORing with a random value +// decoded_obj = (obj ^ obfuscator) +OBJC_EXPORT uintptr_t objc_debug_taggedpointer_obfuscator + OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0); + // tag_slot = (obj >> slot_shift) & slot_mask OBJC_EXPORT unsigned int objc_debug_taggedpointer_slot_shift - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0); OBJC_EXPORT uintptr_t objc_debug_taggedpointer_slot_mask - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0); // class = classes[tag_slot] -OBJC_EXPORT Class objc_debug_taggedpointer_classes[] - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); +OBJC_EXPORT Class _Nullable objc_debug_taggedpointer_classes[] + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0); -// payload = (obj << payload_lshift) >> payload_rshift +// payload = (decoded_obj << payload_lshift) >> payload_rshift // Payload signedness is determined by the signedness of the right-shift. OBJC_EXPORT unsigned int objc_debug_taggedpointer_payload_lshift - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0); OBJC_EXPORT unsigned int objc_debug_taggedpointer_payload_rshift - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0); // Extended tagged pointers (255 classes, 52-bit payload). @@ -156,68 +244,56 @@ OBJC_EXPORT unsigned int objc_debug_taggedpointer_payload_rshift // tagged pointer scheme alone, it will appear to have an isa // that is either nil or class __NSUnrecognizedTaggedPointer. -// if (ext_mask != 0 && (obj & ext_mask) == ext_mask) +// if (ext_mask != 0 && (decoded_obj & ext_mask) == ext_mask) // obj is a ext tagged pointer object OBJC_EXPORT uintptr_t objc_debug_taggedpointer_ext_mask - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); // ext_tag_slot = (obj >> ext_slot_shift) & ext_slot_mask OBJC_EXPORT unsigned int objc_debug_taggedpointer_ext_slot_shift - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); OBJC_EXPORT uintptr_t objc_debug_taggedpointer_ext_slot_mask - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); // class = ext_classes[ext_tag_slot] -OBJC_EXPORT Class objc_debug_taggedpointer_ext_classes[] - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); +OBJC_EXPORT Class _Nullable objc_debug_taggedpointer_ext_classes[] + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); -// payload = (obj << ext_payload_lshift) >> ext_payload_rshift +// payload = (decoded_obj << ext_payload_lshift) >> ext_payload_rshift // Payload signedness is determined by the signedness of the right-shift. OBJC_EXPORT unsigned int objc_debug_taggedpointer_ext_payload_lshift - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); OBJC_EXPORT unsigned int objc_debug_taggedpointer_ext_payload_rshift - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); #endif /*********************************************************************** -* Breakpoints in objc_msgSend for debugger stepping. -* The array is a {0,0} terminated list of addresses. -* Each address is one of the following: -* OBJC_MESSENGER_START: Address is the start of a messenger function. -* OBJC_MESSENGER_END_FAST: Address is a jump insn that calls an IMP. -* OBJC_MESSENGER_END_SLOW: Address is some insn in the slow lookup path. -* OBJC_MESSENGER_END_NIL: Address is a return insn for messages to nil. -* -* Every path from OBJC_MESSENGER_START should reach some OBJC_MESSENGER_END. -* At all ENDs, the stack and parameter register state is the same as START. -* -* In some cases, the END_FAST case jumps to something other than the -* method's implementation. In those cases the jump's destination will -* be another function that is marked OBJC_MESSENGER_START. +* Swift marker bits **********************************************************************/ #if __OBJC2__ - -#define OBJC_MESSENGER_START 1 -#define OBJC_MESSENGER_END_FAST 2 -#define OBJC_MESSENGER_END_SLOW 3 -#define OBJC_MESSENGER_END_NIL 4 - -struct objc_messenger_breakpoint { - uintptr_t address; - uintptr_t kind; -}; - -OBJC_EXPORT struct objc_messenger_breakpoint -gdb_objc_messenger_breakpoints[] - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); - +OBJC_EXPORT const uintptr_t objc_debug_swift_stable_abi_bit +OBJC_AVAILABLE(10.14.4, 12.2, 12.2, 5.2, 3.2); #endif + +/*********************************************************************** +* AutoreleasePoolPage +**********************************************************************/ +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_magic_offset OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_next_offset OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_thread_offset OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_parent_offset OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_child_offset OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_depth_offset OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); +OBJC_EXTERN const uint32_t objc_debug_autoreleasepoolpage_hiwat_offset OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); + __END_DECLS +// APPLE_API_PRIVATE #endif +// _OBJC_GDB_H #endif diff --git a/runtime/objc-initialize.h b/runtime/objc-initialize.h index 9ec99b5..c4695d5 100644 --- a/runtime/objc-initialize.h +++ b/runtime/objc-initialize.h @@ -30,7 +30,7 @@ __BEGIN_DECLS struct _objc_initializing_classes; -extern void _class_initialize(Class cls); +extern void initializeNonMetaClass(Class cls); extern void _destroyInitializingClassList(struct _objc_initializing_classes *list); diff --git a/runtime/objc-initialize.mm b/runtime/objc-initialize.mm index 0857305..4713325 100644 --- a/runtime/objc-initialize.mm +++ b/runtime/objc-initialize.mm @@ -95,11 +95,19 @@ #include "objc-private.h" #include "message.h" #include "objc-initialize.h" +#include "DenseMapExtras.h" /* classInitLock protects CLS_INITIALIZED and CLS_INITIALIZING, and * is signalled when any class is done initializing. * Threads that are waiting for a class to finish initializing wait on this. */ -static monitor_t classInitLock; +monitor_t classInitLock; + + +struct _objc_willInitializeClassCallback { + _objc_func_willInitializeClass f; + void *context; +}; +static GlobalSmallVector<_objc_willInitializeClassCallback, 1> willInitializeFuncs; /*********************************************************************** @@ -262,9 +270,12 @@ static void _setThisThreadIsNotInitializingClass(Class cls) typedef struct PendingInitialize { Class subclass; struct PendingInitialize *next; + + PendingInitialize(Class cls) : subclass(cls), next(nullptr) { } } PendingInitialize; -static NXMapTable *pendingInitializeMap; +typedef objc::DenseMap PendingInitializeMap; +static PendingInitializeMap *pendingInitializeMap; /*********************************************************************** * _finishInitializing @@ -277,11 +288,11 @@ static void _finishInitializing(Class cls, Class supercls) PendingInitialize *pending; classInitLock.assertLocked(); - assert(!supercls || supercls->isInitialized()); + ASSERT(!supercls || supercls->isInitialized()); if (PrintInitializing) { - _objc_inform("INITIALIZE: %s is fully +initialized", - cls->nameForLogging()); + _objc_inform("INITIALIZE: thread %p: %s is fully +initialized", + objc_thread_self(), cls->nameForLogging()); } // mark this class as fully +initialized @@ -291,21 +302,23 @@ static void _finishInitializing(Class cls, Class supercls) // mark any subclasses that were merely waiting for this class if (!pendingInitializeMap) return; - pending = (PendingInitialize *)NXMapGet(pendingInitializeMap, cls); - if (!pending) return; - NXMapRemove(pendingInitializeMap, cls); - + auto it = pendingInitializeMap->find(cls); + if (it == pendingInitializeMap->end()) return; + + pending = it->second; + pendingInitializeMap->erase(it); + // Destroy the pending table if it's now empty, to save memory. - if (NXCountMapTable(pendingInitializeMap) == 0) { - NXFreeMapTable(pendingInitializeMap); + if (pendingInitializeMap->size() == 0) { + delete pendingInitializeMap; pendingInitializeMap = nil; } while (pending) { PendingInitialize *next = pending->next; if (pending->subclass) _finishInitializing(pending->subclass, cls); - free(pending); + delete pending; pending = next; } } @@ -318,26 +331,27 @@ static void _finishInitializing(Class cls, Class supercls) **********************************************************************/ static void _finishInitializingAfter(Class cls, Class supercls) { - PendingInitialize *pending; classInitLock.assertLocked(); if (PrintInitializing) { - _objc_inform("INITIALIZE: %s waiting for superclass +[%s initialize]", - cls->nameForLogging(), supercls->nameForLogging()); + _objc_inform("INITIALIZE: thread %p: class %s will be marked as fully " + "+initialized after superclass +[%s initialize] completes", + objc_thread_self(), cls->nameForLogging(), + supercls->nameForLogging()); } if (!pendingInitializeMap) { - pendingInitializeMap = - NXCreateMapTable(NXPtrValueMapPrototype, 10); + pendingInitializeMap = new PendingInitializeMap{10}; // fixme pre-size this table for CF/NSObject +initialize } - pending = (PendingInitialize *)malloc(sizeof(*pending)); - pending->subclass = cls; - pending->next = (PendingInitialize *) - NXMapGet(pendingInitializeMap, supercls); - NXMapInsert(pendingInitializeMap, supercls, pending); + PendingInitialize *pending = new PendingInitialize{cls}; + auto result = pendingInitializeMap->try_emplace(supercls, pending); + if (!result.second) { + pending->next = result.first->second; + result.first->second = pending; + } } @@ -352,6 +366,11 @@ void callInitialize(Class cls) void waitForInitializeToComplete(Class cls) { + if (PrintInitializing) { + _objc_inform("INITIALIZE: thread %p: blocking until +[%s initialize] " + "completes", objc_thread_self(), cls->nameForLogging()); + } + monitor_locker_t lock(classInitLock); while (!cls->isInitialized()) { classInitLock.wait(); @@ -362,18 +381,119 @@ void waitForInitializeToComplete(Class cls) void callInitialize(Class cls) { - ((void(*)(Class, SEL))objc_msgSend)(cls, SEL_initialize); + ((void(*)(Class, SEL))objc_msgSend)(cls, @selector(initialize)); asm(""); } +/*********************************************************************** +* classHasTrivialInitialize +* Returns true if the class has no +initialize implementation or +* has a +initialize implementation that looks empty. +* Any root class +initialize implemetation is assumed to be trivial. +**********************************************************************/ +static bool classHasTrivialInitialize(Class cls) +{ + if (cls->isRootClass() || cls->isRootMetaclass()) return true; + + Class rootCls = cls->ISA()->ISA()->superclass; + + IMP rootImp = lookUpImpOrNil(rootCls, @selector(initialize), rootCls->ISA()); + IMP imp = lookUpImpOrNil(cls, @selector(initialize), cls->ISA()); + return (imp == nil || imp == (IMP)&objc_noop_imp || imp == rootImp); +} + + +/*********************************************************************** +* lockAndFinishInitializing +* Mark a class as finished initializing and notify waiters, or queue for later. +* If the superclass is also done initializing, then update +* the info bits and notify waiting threads. +* If not, update them later. (This can happen if this +initialize +* was itself triggered from inside a superclass +initialize.) +**********************************************************************/ +static void lockAndFinishInitializing(Class cls, Class supercls) +{ + monitor_locker_t lock(classInitLock); + if (!supercls || supercls->isInitialized()) { + _finishInitializing(cls, supercls); + } else { + _finishInitializingAfter(cls, supercls); + } +} + + +/*********************************************************************** +* performForkChildInitialize +* +initialize after fork() is problematic. It's possible for the +* fork child process to call some +initialize that would deadlock waiting +* for another +initialize in the parent process. +* We wouldn't know how much progress it made therein, so we can't +* act as if +initialize completed nor can we restart +initialize +* from scratch. +* +* Instead we proceed introspectively. If the class has some +* +initialize implementation, we halt. If the class has no +* +initialize implementation of its own, we continue. Root +* class +initialize is assumed to be empty if it exists. +* +* We apply this rule even if the child's +initialize does not appear +* to be blocked by anything. This prevents races wherein the +initialize +* deadlock only rarely hits. Instead we disallow it even when we "won" +* the race. +* +* Exception: processes that are single-threaded when fork() is called +* have no restrictions on +initialize in the child. Examples: sshd and httpd. +* +* Classes that wish to implement +initialize and be callable after +* fork() must use an atfork() handler to provoke +initialize in fork prepare. +**********************************************************************/ + +// Called before halting when some +initialize +// method can't be called after fork(). +BREAKPOINT_FUNCTION( + void objc_initializeAfterForkError(Class cls) +); + +void performForkChildInitialize(Class cls, Class supercls) +{ + if (classHasTrivialInitialize(cls)) { + if (PrintInitializing) { + _objc_inform("INITIALIZE: thread %p: skipping trivial +[%s " + "initialize] in fork() child process", + objc_thread_self(), cls->nameForLogging()); + } + lockAndFinishInitializing(cls, supercls); + } + else { + if (PrintInitializing) { + _objc_inform("INITIALIZE: thread %p: refusing to call +[%s " + "initialize] in fork() child process because " + "it may have been in progress when fork() was called", + objc_thread_self(), cls->nameForLogging()); + } + _objc_inform_now_and_on_crash + ("+[%s initialize] may have been in progress in another thread " + "when fork() was called.", + cls->nameForLogging()); + objc_initializeAfterForkError(cls); + _objc_fatal + ("+[%s initialize] may have been in progress in another thread " + "when fork() was called. We cannot safely call it or " + "ignore it in the fork() child process. Crashing instead. " + "Set a breakpoint on objc_initializeAfterForkError to debug.", + cls->nameForLogging()); + } +} + + /*********************************************************************** * class_initialize. Send the '+initialize' message on demand to any * uninitialized class. Force initialization of superclasses first. **********************************************************************/ -void _class_initialize(Class cls) +void initializeNonMetaClass(Class cls) { - assert(!cls->isMetaClass()); + ASSERT(!cls->isMetaClass()); Class supercls; bool reallyInitialize = NO; @@ -382,15 +502,19 @@ void _class_initialize(Class cls) // See note about deadlock above. supercls = cls->superclass; if (supercls && !supercls->isInitialized()) { - _class_initialize(supercls); + initializeNonMetaClass(supercls); } // Try to atomically set CLS_INITIALIZING. + SmallVector<_objc_willInitializeClassCallback, 1> localWillInitializeFuncs; { monitor_locker_t lock(classInitLock); if (!cls->isInitialized() && !cls->isInitializing()) { cls->setInitializing(); reallyInitialize = YES; + + // Grab a copy of the will-initialize funcs with the lock held. + localWillInitializeFuncs.initFrom(willInitializeFuncs); } } @@ -399,44 +523,55 @@ void _class_initialize(Class cls) // Record that we're initializing this class so we can message it. _setThisThreadIsInitializingClass(cls); + + if (MultithreadedForkChild) { + // LOL JK we don't really call +initialize methods after fork(). + performForkChildInitialize(cls, supercls); + return; + } + for (auto callback : localWillInitializeFuncs) + callback.f(callback.context, cls); + // Send the +initialize message. // Note that +initialize is sent to the superclass (again) if // this class doesn't implement +initialize. 2157218 if (PrintInitializing) { - _objc_inform("INITIALIZE: calling +[%s initialize]", - cls->nameForLogging()); + _objc_inform("INITIALIZE: thread %p: calling +[%s initialize]", + objc_thread_self(), cls->nameForLogging()); } // Exceptions: A +initialize call that throws an exception // is deemed to be a complete and successful +initialize. - @try { + // + // Only __OBJC2__ adds these handlers. !__OBJC2__ has a + // bootstrapping problem of this versus CF's call to + // objc_exception_set_functions(). +#if __OBJC2__ + @try +#endif + { callInitialize(cls); if (PrintInitializing) { - _objc_inform("INITIALIZE: finished +[%s initialize]", - cls->nameForLogging()); + _objc_inform("INITIALIZE: thread %p: finished +[%s initialize]", + objc_thread_self(), cls->nameForLogging()); } } +#if __OBJC2__ @catch (...) { if (PrintInitializing) { - _objc_inform("INITIALIZE: +[%s initialize] threw an exception", - cls->nameForLogging()); + _objc_inform("INITIALIZE: thread %p: +[%s initialize] " + "threw an exception", + objc_thread_self(), cls->nameForLogging()); } @throw; } - @finally { - // Done initializing. - // If the superclass is also done initializing, then update - // the info bits and notify waiting threads. - // If not, update them later. (This can happen if this +initialize - // was itself triggered from inside a superclass +initialize.) - monitor_locker_t lock(classInitLock); - if (!supercls || supercls->isInitialized()) { - _finishInitializing(cls, supercls); - } else { - _finishInitializingAfter(cls, supercls); - } + @finally +#endif + { + // Done initializing. + lockAndFinishInitializing(cls, supercls); } return; } @@ -450,9 +585,14 @@ void _class_initialize(Class cls) // before blocking. if (_thisThreadIsInitializingClass(cls)) { return; - } else { + } else if (!MultithreadedForkChild) { waitForInitializeToComplete(cls); return; + } else { + // We're on the child side of fork(), facing a class that + // was initializing by some other thread when fork() was called. + _setThisThreadIsInitializingClass(cls); + performForkChildInitialize(cls, supercls); } } @@ -472,3 +612,33 @@ void _class_initialize(Class cls) _objc_fatal("thread-safe class init in objc runtime is buggy!"); } } + +void _objc_addWillInitializeClassFunc(_objc_func_willInitializeClass _Nonnull func, void * _Nullable context) { +#if __OBJC2__ + unsigned count; + Class *realizedClasses; + + // Fetch all currently initialized classes. Do this with classInitLock held + // so we don't race with setting those flags. + { + monitor_locker_t initLock(classInitLock); + realizedClasses = objc_copyRealizedClassList(&count); + for (unsigned i = 0; i < count; i++) { + // Remove uninitialized classes from the array. + if (!realizedClasses[i]->isInitializing() && !realizedClasses[i]->isInitialized()) + realizedClasses[i] = Nil; + } + + willInitializeFuncs.append({func, context}); + } + + // Invoke the callback for all realized classes that weren't cleared out. + for (unsigned i = 0; i < count; i++) { + if (Class cls = realizedClasses[i]) { + func(context, cls); + } + } + + free(realizedClasses); +#endif +} diff --git a/runtime/objc-internal.h b/runtime/objc-internal.h index 5bcb28c..afc82f5 100644 --- a/runtime/objc-internal.h +++ b/runtime/objc-internal.h @@ -41,9 +41,9 @@ #include #include #include +#include #include -__BEGIN_DECLS // Termination reasons in the OS_REASON_OBJC namespace. #define OBJC_EXIT_REASON_UNSPECIFIED 1 @@ -54,13 +54,23 @@ __BEGIN_DECLS // The runtime's class structure will never grow beyond this. #define OBJC_MAX_CLASS_SIZE (32*sizeof(void*)) + +__BEGIN_DECLS + +// This symbol is exported only from debug builds of libobjc itself. +#if defined(OBJC_IS_DEBUG_BUILD) +OBJC_EXPORT void _objc_isDebugBuild(void); +#endif + // In-place construction of an Objective-C class. // cls and metacls must each be OBJC_MAX_CLASS_SIZE bytes. // Returns nil if a class with the same name already exists. // Returns nil if the superclass is under construction. // Call objc_registerClassPair() when you are done. -OBJC_EXPORT Class objc_initializeClassPair(Class superclass, const char *name, Class cls, Class metacls) - OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0); +OBJC_EXPORT Class _Nullable +objc_initializeClassPair(Class _Nullable superclass, const char * _Nonnull name, + Class _Nonnull cls, Class _Nonnull metacls) + OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0); // Class and metaclass construction from a compiler-generated memory image. // cls and cls->isa must each be OBJC_MAX_CLASS_SIZE bytes. @@ -72,86 +82,164 @@ OBJC_EXPORT Class objc_initializeClassPair(Class superclass, const char *name, C // Do not call objc_registerClassPair(). #if __OBJC2__ struct objc_image_info; -OBJC_EXPORT Class objc_readClassPair(Class cls, - const struct objc_image_info *info) - OBJC_AVAILABLE(10.10, 8.0, 9.0, 1.0); +OBJC_EXPORT Class _Nullable +objc_readClassPair(Class _Nonnull cls, + const struct objc_image_info * _Nonnull info) + OBJC_AVAILABLE(10.10, 8.0, 9.0, 1.0, 2.0); #endif // Batch object allocation using malloc_zone_batch_malloc(). -OBJC_EXPORT unsigned class_createInstances(Class cls, size_t extraBytes, - id *results, unsigned num_requested) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0) +OBJC_EXPORT unsigned +class_createInstances(Class _Nullable cls, size_t extraBytes, + id _Nonnull * _Nonnull results, unsigned num_requested) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0) OBJC_ARC_UNAVAILABLE; // Get the isa pointer written into objects just before being freed. -OBJC_EXPORT Class _objc_getFreedObjectClass(void) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT Class _Nonnull +_objc_getFreedObjectClass(void) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); // env NSObjCMessageLoggingEnabled -OBJC_EXPORT void instrumentObjcMessageSends(BOOL flag) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT void +instrumentObjcMessageSends(BOOL flag) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); // Initializer called by libSystem -OBJC_EXPORT void _objc_init(void) +OBJC_EXPORT void +_objc_init(void) #if __OBJC2__ - OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0); + OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0); #else - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); #endif +// fork() safety called by libSystem +OBJC_EXPORT void +_objc_atfork_prepare(void) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); + +OBJC_EXPORT void +_objc_atfork_parent(void) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); + +OBJC_EXPORT void +_objc_atfork_child(void) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); + // Return YES if GC is on and `object` is a GC allocation. -OBJC_EXPORT BOOL objc_isAuto(id object) - __OSX_DEPRECATED(10.4, 10.8, "it always returns NO") - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; +OBJC_EXPORT BOOL +objc_isAuto(id _Nullable object) + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.8, "it always returns NO"); + +// GC debugging +OBJC_EXPORT BOOL +objc_dumpHeap(char * _Nonnull filename, unsigned long length) + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.8, "it always returns NO"); // GC startup callback from Foundation -OBJC_EXPORT malloc_zone_t *objc_collect_init(int (*callback)(void)) - __OSX_DEPRECATED(10.4, 10.8, "it does nothing") - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; +OBJC_EXPORT malloc_zone_t * _Nullable +objc_collect_init(int (* _Nonnull callback)(void)) + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.4, 10.8, "it does nothing"); + +#if __OBJC2__ +// Copies the list of currently realized classes +// intended for introspection only +// most users will want objc_copyClassList instead. +OBJC_EXPORT +Class _Nonnull * _Nullable +objc_copyRealizedClassList(unsigned int *_Nullable outCount) + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); + +typedef struct objc_imp_cache_entry { + SEL _Nonnull sel; + IMP _Nonnull imp; +} objc_imp_cache_entry; + +OBJC_EXPORT +objc_imp_cache_entry *_Nullable +class_copyImpCache(Class _Nonnull cls, int * _Nullable outCount) + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); +#endif // Plainly-implemented GC barriers. Rosetta used to use these. -OBJC_EXPORT id objc_assign_strongCast_generic(id value, id *dest) +OBJC_EXPORT id _Nullable +objc_assign_strongCast_generic(id _Nullable value, id _Nullable * _Nonnull dest) UNAVAILABLE_ATTRIBUTE; -OBJC_EXPORT id objc_assign_global_generic(id value, id *dest) + +OBJC_EXPORT id _Nullable +objc_assign_global_generic(id _Nullable value, id _Nullable * _Nonnull dest) UNAVAILABLE_ATTRIBUTE; -OBJC_EXPORT id objc_assign_threadlocal_generic(id value, id *dest) + +OBJC_EXPORT id _Nullable +objc_assign_threadlocal_generic(id _Nullable value, + id _Nullable * _Nonnull dest) UNAVAILABLE_ATTRIBUTE; -OBJC_EXPORT id objc_assign_ivar_generic(id value, id dest, ptrdiff_t offset) + +OBJC_EXPORT id _Nullable +objc_assign_ivar_generic(id _Nullable value, id _Nonnull dest, ptrdiff_t offset) UNAVAILABLE_ATTRIBUTE; // GC preflight for an app executable. // 1: some slice requires GC // 0: no slice requires GC // -1: I/O or file format error -OBJC_EXPORT int objc_appRequiresGC(int fd) +OBJC_EXPORT int +objc_appRequiresGC(int fd) __OSX_AVAILABLE(10.11) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; + __IOS_UNAVAILABLE __TVOS_UNAVAILABLE + __WATCHOS_UNAVAILABLE +#ifndef __APPLE_BLEACH_SDK__ + __BRIDGEOS_UNAVAILABLE +#endif +; // Install missing-class callback. Used by the late unlamented ZeroLink. -OBJC_EXPORT void _objc_setClassLoader(BOOL (*newClassLoader)(const char *)) OBJC2_UNAVAILABLE; +OBJC_EXPORT void +_objc_setClassLoader(BOOL (* _Nonnull newClassLoader)(const char * _Nonnull)) + OBJC2_UNAVAILABLE; + +#if !(TARGET_OS_OSX && !TARGET_OS_IOSMAC && __i386__) +// Add a class copy fixup handler. The name is a misnomer, as +// multiple calls will install multiple handlers. Older versions +// of the Swift runtime call it by name, and it's only used by Swift +// so it's not worth deprecating this name in favor of a better one. +OBJC_EXPORT void +_objc_setClassCopyFixupHandler(void (* _Nonnull newFixupHandler) + (Class _Nonnull oldClass, Class _Nonnull newClass)) + OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0); +#endif // Install handler for allocation failures. // Handler may abort, or throw, or provide an object to return. -OBJC_EXPORT void _objc_setBadAllocHandler(id (*newHandler)(Class isa)) - OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0); - -// This can go away when AppKit stops calling it (rdar://7811851) -#if __OBJC2__ -OBJC_EXPORT void objc_setMultithreaded (BOOL flag) - __OSX_DEPRECATED(10.0, 10.5, "multithreading is always available") - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; -#endif +OBJC_EXPORT void +_objc_setBadAllocHandler(id _Nullable (* _Nonnull newHandler) + (Class _Nullable isa)) + OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0); // Used by ExceptionHandling.framework #if !__OBJC2__ -OBJC_EXPORT void _objc_error(id rcv, const char *fmt, va_list args) - __attribute__((noreturn)) - __OSX_DEPRECATED(10.0, 10.5, "use other logging facilities instead") - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; +OBJC_EXPORT void +_objc_error(id _Nullable rcv, const char * _Nonnull fmt, va_list args) + __attribute__((noreturn, cold)) + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.0, 10.5, "use other logging facilities instead"); #endif +/** + * Returns the names of all the classes within a library. + * + * @param image The mach header for library or framework you are inquiring about. + * @param outCount The number of class names returned. + * + * @return An array of C strings representing the class names. + */ +OBJC_EXPORT const char * _Nonnull * _Nullable +objc_copyClassNamesForImageHeader(const struct mach_header * _Nonnull mh, + unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0); + // Tagged pointer objects. #if __LP64__ @@ -174,15 +262,32 @@ typedef uint16_t objc_tag_index_t; enum #endif { + // 60-bit payloads OBJC_TAG_NSAtom = 0, OBJC_TAG_1 = 1, OBJC_TAG_NSString = 2, OBJC_TAG_NSNumber = 3, OBJC_TAG_NSIndexPath = 4, OBJC_TAG_NSManagedObjectID = 5, - OBJC_TAG_NSDate = 6, + OBJC_TAG_NSDate = 6, + + // 60-bit reserved OBJC_TAG_RESERVED_7 = 7, + // 52-bit payloads + OBJC_TAG_Photos_1 = 8, + OBJC_TAG_Photos_2 = 9, + OBJC_TAG_Photos_3 = 10, + OBJC_TAG_Photos_4 = 11, + OBJC_TAG_XPC_1 = 12, + OBJC_TAG_XPC_2 = 13, + OBJC_TAG_XPC_3 = 14, + OBJC_TAG_XPC_4 = 15, + OBJC_TAG_NSColor = 16, + OBJC_TAG_UIColor = 17, + OBJC_TAG_CGColor = 18, + OBJC_TAG_NSIndexSet = 19, + OBJC_TAG_First60BitPayload = 0, OBJC_TAG_Last60BitPayload = 6, OBJC_TAG_First52BitPayload = 8, @@ -202,48 +307,50 @@ _objc_taggedPointersEnabled(void); // Register a class for a tagged pointer tag. // Aborts if the tag is invalid or already in use. -OBJC_EXPORT void _objc_registerTaggedPointerClass(objc_tag_index_t tag, Class cls) - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); +OBJC_EXPORT void +_objc_registerTaggedPointerClass(objc_tag_index_t tag, Class _Nonnull cls) + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0); // Returns the registered class for the given tag. // Returns nil if the tag is valid but has no registered class. // Aborts if the tag is invalid. -OBJC_EXPORT Class _objc_getClassForTag(objc_tag_index_t tag) - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); +OBJC_EXPORT Class _Nullable +_objc_getClassForTag(objc_tag_index_t tag) + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0); // Create a tagged pointer object with the given tag and payload. // Assumes the tag is valid. // Assumes tagged pointers are enabled. // The payload will be silently truncated to fit. -static inline void * +static inline void * _Nonnull _objc_makeTaggedPointer(objc_tag_index_t tag, uintptr_t payload); // Return true if ptr is a tagged pointer object. // Does not check the validity of ptr's class. static inline bool -_objc_isTaggedPointer(const void *ptr); +_objc_isTaggedPointer(const void * _Nullable ptr); // Extract the tag value from the given tagged pointer object. // Assumes ptr is a valid tagged pointer object. // Does not check the validity of ptr's tag. static inline objc_tag_index_t -_objc_getTaggedPointerTag(const void *ptr); +_objc_getTaggedPointerTag(const void * _Nullable ptr); // Extract the payload from the given tagged pointer object. // Assumes ptr is a valid tagged pointer object. // The payload value is zero-extended. static inline uintptr_t -_objc_getTaggedPointerValue(const void *ptr); +_objc_getTaggedPointerValue(const void * _Nullable ptr); // Extract the payload from the given tagged pointer object. // Assumes ptr is a valid tagged pointer object. // The payload value is sign-extended. static inline intptr_t -_objc_getTaggedPointerSignedValue(const void *ptr); +_objc_getTaggedPointerSignedValue(const void * _Nullable ptr); // Don't use the values below. Use the declarations above. -#if TARGET_OS_OSX && __x86_64__ +#if (TARGET_OS_OSX || TARGET_OS_IOSMAC) && __x86_64__ // 64-bit Mac - tag bit is LSB # define OBJC_MSB_TAGGED_POINTERS 0 #else @@ -262,29 +369,43 @@ _objc_getTaggedPointerSignedValue(const void *ptr); #define _OBJC_TAG_EXT_SLOT_MASK 0xff #if OBJC_MSB_TAGGED_POINTERS -# define _OBJC_TAG_MASK (1ULL<<63) +# define _OBJC_TAG_MASK (1UL<<63) # define _OBJC_TAG_INDEX_SHIFT 60 # define _OBJC_TAG_SLOT_SHIFT 60 # define _OBJC_TAG_PAYLOAD_LSHIFT 4 # define _OBJC_TAG_PAYLOAD_RSHIFT 4 -# define _OBJC_TAG_EXT_MASK (0xfULL<<60) +# define _OBJC_TAG_EXT_MASK (0xfUL<<60) # define _OBJC_TAG_EXT_INDEX_SHIFT 52 # define _OBJC_TAG_EXT_SLOT_SHIFT 52 # define _OBJC_TAG_EXT_PAYLOAD_LSHIFT 12 # define _OBJC_TAG_EXT_PAYLOAD_RSHIFT 12 #else -# define _OBJC_TAG_MASK 1 +# define _OBJC_TAG_MASK 1UL # define _OBJC_TAG_INDEX_SHIFT 1 # define _OBJC_TAG_SLOT_SHIFT 0 # define _OBJC_TAG_PAYLOAD_LSHIFT 0 # define _OBJC_TAG_PAYLOAD_RSHIFT 4 -# define _OBJC_TAG_EXT_MASK 0xfULL +# define _OBJC_TAG_EXT_MASK 0xfUL # define _OBJC_TAG_EXT_INDEX_SHIFT 4 # define _OBJC_TAG_EXT_SLOT_SHIFT 4 # define _OBJC_TAG_EXT_PAYLOAD_LSHIFT 0 # define _OBJC_TAG_EXT_PAYLOAD_RSHIFT 12 #endif +extern uintptr_t objc_debug_taggedpointer_obfuscator; + +static inline void * _Nonnull +_objc_encodeTaggedPointer(uintptr_t ptr) +{ + return (void *)(objc_debug_taggedpointer_obfuscator ^ ptr); +} + +static inline uintptr_t +_objc_decodeTaggedPointer(const void * _Nullable ptr) +{ + return (uintptr_t)ptr ^ objc_debug_taggedpointer_obfuscator; +} + static inline bool _objc_taggedPointersEnabled(void) { @@ -292,42 +413,45 @@ _objc_taggedPointersEnabled(void) return (objc_debug_taggedpointer_mask != 0); } -static inline void * +static inline void * _Nonnull _objc_makeTaggedPointer(objc_tag_index_t tag, uintptr_t value) { // PAYLOAD_LSHIFT and PAYLOAD_RSHIFT are the payload extraction shifts. // They are reversed here for payload insertion. - // assert(_objc_taggedPointersEnabled()); + // ASSERT(_objc_taggedPointersEnabled()); if (tag <= OBJC_TAG_Last60BitPayload) { - // assert(((value << _OBJC_TAG_PAYLOAD_RSHIFT) >> _OBJC_TAG_PAYLOAD_LSHIFT) == value); - return (void*) + // ASSERT(((value << _OBJC_TAG_PAYLOAD_RSHIFT) >> _OBJC_TAG_PAYLOAD_LSHIFT) == value); + uintptr_t result = (_OBJC_TAG_MASK | ((uintptr_t)tag << _OBJC_TAG_INDEX_SHIFT) | ((value << _OBJC_TAG_PAYLOAD_RSHIFT) >> _OBJC_TAG_PAYLOAD_LSHIFT)); + return _objc_encodeTaggedPointer(result); } else { - // assert(tag >= OBJC_TAG_First52BitPayload); - // assert(tag <= OBJC_TAG_Last52BitPayload); - // assert(((value << _OBJC_TAG_EXT_PAYLOAD_RSHIFT) >> _OBJC_TAG_EXT_PAYLOAD_LSHIFT) == value); - return (void*) + // ASSERT(tag >= OBJC_TAG_First52BitPayload); + // ASSERT(tag <= OBJC_TAG_Last52BitPayload); + // ASSERT(((value << _OBJC_TAG_EXT_PAYLOAD_RSHIFT) >> _OBJC_TAG_EXT_PAYLOAD_LSHIFT) == value); + uintptr_t result = (_OBJC_TAG_EXT_MASK | ((uintptr_t)(tag - OBJC_TAG_First52BitPayload) << _OBJC_TAG_EXT_INDEX_SHIFT) | ((value << _OBJC_TAG_EXT_PAYLOAD_RSHIFT) >> _OBJC_TAG_EXT_PAYLOAD_LSHIFT)); + return _objc_encodeTaggedPointer(result); } } static inline bool -_objc_isTaggedPointer(const void *ptr) +_objc_isTaggedPointer(const void * _Nullable ptr) { - return ((intptr_t)ptr & _OBJC_TAG_MASK) == _OBJC_TAG_MASK; + return ((uintptr_t)ptr & _OBJC_TAG_MASK) == _OBJC_TAG_MASK; } static inline objc_tag_index_t -_objc_getTaggedPointerTag(const void *ptr) +_objc_getTaggedPointerTag(const void * _Nullable ptr) { - // assert(_objc_isTaggedPointer(ptr)); - uintptr_t basicTag = ((uintptr_t)ptr >> _OBJC_TAG_INDEX_SHIFT) & _OBJC_TAG_INDEX_MASK; - uintptr_t extTag = ((uintptr_t)ptr >> _OBJC_TAG_EXT_INDEX_SHIFT) & _OBJC_TAG_EXT_INDEX_MASK; + // ASSERT(_objc_isTaggedPointer(ptr)); + uintptr_t value = _objc_decodeTaggedPointer(ptr); + uintptr_t basicTag = (value >> _OBJC_TAG_INDEX_SHIFT) & _OBJC_TAG_INDEX_MASK; + uintptr_t extTag = (value >> _OBJC_TAG_EXT_INDEX_SHIFT) & _OBJC_TAG_EXT_INDEX_MASK; if (basicTag == _OBJC_TAG_INDEX_MASK) { return (objc_tag_index_t)(extTag + OBJC_TAG_First52BitPayload); } else { @@ -336,26 +460,28 @@ _objc_getTaggedPointerTag(const void *ptr) } static inline uintptr_t -_objc_getTaggedPointerValue(const void *ptr) +_objc_getTaggedPointerValue(const void * _Nullable ptr) { - // assert(_objc_isTaggedPointer(ptr)); - uintptr_t basicTag = ((uintptr_t)ptr >> _OBJC_TAG_INDEX_SHIFT) & _OBJC_TAG_INDEX_MASK; + // ASSERT(_objc_isTaggedPointer(ptr)); + uintptr_t value = _objc_decodeTaggedPointer(ptr); + uintptr_t basicTag = (value >> _OBJC_TAG_INDEX_SHIFT) & _OBJC_TAG_INDEX_MASK; if (basicTag == _OBJC_TAG_INDEX_MASK) { - return ((uintptr_t)ptr << _OBJC_TAG_EXT_PAYLOAD_LSHIFT) >> _OBJC_TAG_EXT_PAYLOAD_RSHIFT; + return (value << _OBJC_TAG_EXT_PAYLOAD_LSHIFT) >> _OBJC_TAG_EXT_PAYLOAD_RSHIFT; } else { - return ((uintptr_t)ptr << _OBJC_TAG_PAYLOAD_LSHIFT) >> _OBJC_TAG_PAYLOAD_RSHIFT; + return (value << _OBJC_TAG_PAYLOAD_LSHIFT) >> _OBJC_TAG_PAYLOAD_RSHIFT; } } static inline intptr_t -_objc_getTaggedPointerSignedValue(const void *ptr) +_objc_getTaggedPointerSignedValue(const void * _Nullable ptr) { - // assert(_objc_isTaggedPointer(ptr)); - uintptr_t basicTag = ((uintptr_t)ptr >> _OBJC_TAG_INDEX_SHIFT) & _OBJC_TAG_INDEX_MASK; + // ASSERT(_objc_isTaggedPointer(ptr)); + uintptr_t value = _objc_decodeTaggedPointer(ptr); + uintptr_t basicTag = (value >> _OBJC_TAG_INDEX_SHIFT) & _OBJC_TAG_INDEX_MASK; if (basicTag == _OBJC_TAG_INDEX_MASK) { - return ((intptr_t)ptr << _OBJC_TAG_EXT_PAYLOAD_LSHIFT) >> _OBJC_TAG_EXT_PAYLOAD_RSHIFT; + return ((intptr_t)value << _OBJC_TAG_EXT_PAYLOAD_LSHIFT) >> _OBJC_TAG_EXT_PAYLOAD_RSHIFT; } else { - return ((intptr_t)ptr << _OBJC_TAG_PAYLOAD_LSHIFT) >> _OBJC_TAG_PAYLOAD_RSHIFT; + return ((intptr_t)value << _OBJC_TAG_PAYLOAD_LSHIFT) >> _OBJC_TAG_PAYLOAD_RSHIFT; } } @@ -376,22 +502,78 @@ _objc_getTaggedPointerSignedValue(const void *ptr) * * class_getMethodImplementation(object_getClass(obj), name); */ -OBJC_EXPORT IMP object_getMethodImplementation(id obj, SEL name) - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); +OBJC_EXPORT IMP _Nonnull +object_getMethodImplementation(id _Nullable obj, SEL _Nonnull name) + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0); -OBJC_EXPORT IMP object_getMethodImplementation_stret(id obj, SEL name) - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0) +OBJC_EXPORT IMP _Nonnull +object_getMethodImplementation_stret(id _Nullable obj, SEL _Nonnull name) + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; -// Instance-specific instance variable layout. +/** + * Adds multiple methods to a class in bulk. This amortizes overhead that can be + * expensive when adding methods one by one with class_addMethod. + * + * @param cls The class to which to add the methods. + * @param names An array of selectors for the methods to add. + * @param imps An array of functions which implement the new methods. + * @param types An array of strings that describe the types of each method's + * arguments. + * @param count The number of items in the names, imps, and types arrays. + * @param outFiledCount Upon return, contains the number of failed selectors in + * the returned array. + * + * @return A NULL-terminated C array of selectors which could not be added. A + * method cannot be added when a method of that name already exists on that + * class. When no failures occur, the return value is \c NULL. When a non-NULL + * value is returned, the caller must free the array with \c free(). + * + */ +#if __OBJC2__ +OBJC_EXPORT _Nullable SEL * _Nullable +class_addMethodsBulk(_Nullable Class cls, _Nonnull const SEL * _Nonnull names, + _Nonnull const IMP * _Nonnull imps, + const char * _Nonnull * _Nonnull types, uint32_t count, + uint32_t * _Nullable outFailedCount) + OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0); +#endif + +/** + * Replaces multiple methods in a class in bulk. This amortizes overhead that + * can be expensive when adding methods one by one with class_replaceMethod. + * + * @param cls The class to modify. + * @param names An array of selectors for the methods to replace. + * @param imps An array of functions will be the new method implementantations. + * @param types An array of strings that describe the types of each method's + * arguments. + * @param count The number of items in the names, imps, and types arrays. + */ +#if __OBJC2__ +OBJC_EXPORT void +class_replaceMethodsBulk(_Nullable Class cls, + _Nonnull const SEL * _Nonnull names, + _Nonnull const IMP * _Nonnull imps, + const char * _Nonnull * _Nonnull types, + uint32_t count) + OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0); +#endif + + +// Instance-specific instance variable layout. This is no longer implemented. + +OBJC_EXPORT void +_class_setIvarLayoutAccessor(Class _Nullable cls, + const uint8_t* _Nullable (* _Nonnull accessor) + (id _Nullable object)) + UNAVAILABLE_ATTRIBUTE; + +OBJC_EXPORT const uint8_t * _Nullable +_object_getIvarLayout(Class _Nullable cls, id _Nullable object) + UNAVAILABLE_ATTRIBUTE; -OBJC_EXPORT void _class_setIvarLayoutAccessor(Class cls_gen, const uint8_t* (*accessor) (id object)) - __OSX_AVAILABLE(10.7) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; -OBJC_EXPORT const uint8_t *_object_getIvarLayout(Class cls_gen, id object) - __OSX_AVAILABLE(10.7) - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE; /* "Unknown" includes non-object ivars and non-ARC non-__weak ivars @@ -406,230 +588,299 @@ typedef enum { objc_ivar_memoryUnretained // direct access / direct access } objc_ivar_memory_management_t; -OBJC_EXPORT objc_ivar_memory_management_t _class_getIvarMemoryManagement(Class cls, Ivar ivar) - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); +OBJC_EXPORT objc_ivar_memory_management_t +_class_getIvarMemoryManagement(Class _Nullable cls, Ivar _Nonnull ivar) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); -OBJC_EXPORT BOOL _class_isFutureClass(Class cls) - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); +OBJC_EXPORT BOOL _class_isFutureClass(Class _Nullable cls) + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0); // API to only be called by root classes like NSObject or NSProxy OBJC_EXPORT -id -_objc_rootRetain(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +id _Nonnull +_objc_rootRetain(id _Nonnull obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT void -_objc_rootRelease(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +_objc_rootRelease(id _Nonnull obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT bool -_objc_rootReleaseWasZero(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +_objc_rootReleaseWasZero(id _Nonnull obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT bool -_objc_rootTryRetain(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +_objc_rootTryRetain(id _Nonnull obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT bool -_objc_rootIsDeallocating(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +_objc_rootIsDeallocating(id _Nonnull obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT -id -_objc_rootAutorelease(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +id _Nonnull +_objc_rootAutorelease(id _Nonnull obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT uintptr_t -_objc_rootRetainCount(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +_objc_rootRetainCount(id _Nonnull obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT -id -_objc_rootInit(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +id _Nonnull +_objc_rootInit(id _Nonnull obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT -id -_objc_rootAllocWithZone(Class cls, malloc_zone_t *zone) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +id _Nullable +_objc_rootAllocWithZone(Class _Nonnull cls, malloc_zone_t * _Nullable zone) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT -id -_objc_rootAlloc(Class cls) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +id _Nullable +_objc_rootAlloc(Class _Nonnull cls) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT void -_objc_rootDealloc(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +_objc_rootDealloc(id _Nonnull obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT void -_objc_rootFinalize(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +_objc_rootFinalize(id _Nonnull obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT -malloc_zone_t * -_objc_rootZone(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +malloc_zone_t * _Nonnull +_objc_rootZone(id _Nonnull obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT uintptr_t -_objc_rootHash(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +_objc_rootHash(id _Nonnull obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT -void * +void * _Nonnull objc_autoreleasePoolPush(void) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); OBJC_EXPORT void -objc_autoreleasePoolPop(void *context) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +objc_autoreleasePoolPop(void * _Nonnull context) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); -OBJC_EXPORT id objc_alloc(Class cls) - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_alloc(Class _Nullable cls) + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0); -OBJC_EXPORT id objc_allocWithZone(Class cls) - OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_allocWithZone(Class _Nullable cls) + OBJC_AVAILABLE(10.9, 7.0, 9.0, 1.0, 2.0); -OBJC_EXPORT id objc_retain(id obj) +OBJC_EXPORT id _Nullable +objc_alloc_init(Class _Nullable cls) + OBJC_AVAILABLE(10.14.4, 12.2, 12.2, 5.2, 3.2); + +OBJC_EXPORT id _Nullable +objc_opt_new(Class _Nullable cls) + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); + +OBJC_EXPORT id _Nullable +objc_opt_self(id _Nullable obj) + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); + +OBJC_EXPORT Class _Nullable +objc_opt_class(id _Nullable obj) + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); + +OBJC_EXPORT BOOL +objc_opt_respondsToSelector(id _Nullable obj, SEL _Nullable sel) + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); + +OBJC_EXPORT BOOL +objc_opt_isKindOfClass(id _Nullable obj, Class _Nullable cls) + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); + + +OBJC_EXPORT BOOL +objc_sync_try_enter(id _Nonnull obj) + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); + +OBJC_EXPORT id _Nullable +objc_retain(id _Nullable obj) __asm__("_objc_retain") - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); -OBJC_EXPORT void objc_release(id obj) +OBJC_EXPORT void +objc_release(id _Nullable obj) __asm__("_objc_release") - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); -OBJC_EXPORT id objc_autorelease(id obj) +OBJC_EXPORT id _Nullable +objc_autorelease(id _Nullable obj) __asm__("_objc_autorelease") - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); // Prepare a value at +1 for return through a +0 autoreleasing convention. -OBJC_EXPORT -id -objc_autoreleaseReturnValue(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_autoreleaseReturnValue(id _Nullable obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); // Prepare a value at +0 for return through a +0 autoreleasing convention. -OBJC_EXPORT -id -objc_retainAutoreleaseReturnValue(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_retainAutoreleaseReturnValue(id _Nullable obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); // Accept a value returned through a +0 autoreleasing convention for use at +1. -OBJC_EXPORT -id -objc_retainAutoreleasedReturnValue(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_retainAutoreleasedReturnValue(id _Nullable obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); // Accept a value returned through a +0 autoreleasing convention for use at +0. -OBJC_EXPORT -id -objc_unsafeClaimAutoreleasedReturnValue(id obj) - OBJC_AVAILABLE(10.11, 9.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_unsafeClaimAutoreleasedReturnValue(id _Nullable obj) + OBJC_AVAILABLE(10.11, 9.0, 9.0, 1.0, 2.0); -OBJC_EXPORT -void -objc_storeStrong(id *location, id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT void +objc_storeStrong(id _Nullable * _Nonnull location, id _Nullable obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); -OBJC_EXPORT -id -objc_retainAutorelease(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_retainAutorelease(id _Nullable obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); // obsolete. -OBJC_EXPORT id objc_retain_autorelease(id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_retain_autorelease(id _Nullable obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); -OBJC_EXPORT -id -objc_loadWeakRetained(id *location) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_loadWeakRetained(id _Nullable * _Nonnull location) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); -OBJC_EXPORT -id -objc_initWeak(id *location, id val) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_initWeak(id _Nullable * _Nonnull location, id _Nullable val) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); // Like objc_storeWeak, but stores nil if the new object is deallocating // or the new object's class does not support weak references. // Returns the value stored (either the new object or nil). -OBJC_EXPORT -id -objc_storeWeakOrNil(id *location, id obj) - OBJC_AVAILABLE(10.11, 9.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_storeWeakOrNil(id _Nullable * _Nonnull location, id _Nullable obj) + OBJC_AVAILABLE(10.11, 9.0, 9.0, 1.0, 2.0); // Like objc_initWeak, but stores nil if the new object is deallocating // or the new object's class does not support weak references. // Returns the value stored (either the new object or nil). -OBJC_EXPORT -id -objc_initWeakOrNil(id *location, id val) - OBJC_AVAILABLE(10.11, 9.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_initWeakOrNil(id _Nullable * _Nonnull location, id _Nullable val) + OBJC_AVAILABLE(10.11, 9.0, 9.0, 1.0, 2.0); -OBJC_EXPORT -void -objc_destroyWeak(id *location) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT void +objc_destroyWeak(id _Nullable * _Nonnull location) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); -OBJC_EXPORT -void -objc_copyWeak(id *to, id *from) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT void +objc_copyWeak(id _Nullable * _Nonnull to, id _Nullable * _Nonnull from) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); -OBJC_EXPORT -void -objc_moveWeak(id *to, id *from) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT void +objc_moveWeak(id _Nullable * _Nonnull to, id _Nullable * _Nonnull from) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); -OBJC_EXPORT -void +OBJC_EXPORT void _objc_autoreleasePoolPrint(void) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); -OBJC_EXPORT BOOL objc_should_deallocate(id object) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT BOOL +objc_should_deallocate(id _Nonnull object) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); -OBJC_EXPORT void objc_clear_deallocating(id object) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT void +objc_clear_deallocating(id _Nonnull object) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); // to make CF link for now -OBJC_EXPORT -void * +OBJC_EXPORT void * _Nonnull _objc_autoreleasePoolPush(void) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); -OBJC_EXPORT -void -_objc_autoreleasePoolPop(void *context) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT void +_objc_autoreleasePoolPop(void * _Nonnull context) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); + + +/** + * Load a classref, which is a chunk of data containing a class + * pointer. May perform initialization and rewrite the classref to + * point to a new object, if needed. Returns the loaded Class. + * + * In particular, if the classref points to a stub class (indicated + * by setting the bottom bit of the class pointer to 1), then this + * will call the stub's initializer and then replace the classref + * value with the value returned by the initializer. + * + * @param clsref The classref to load. + * @return The loaded Class pointer. + */ +#if __OBJC2__ +OBJC_EXPORT _Nullable Class +objc_loadClassref(_Nullable Class * _Nonnull clsref) + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 5.0); +#endif // Extra @encode data for XPC, or NULL -OBJC_EXPORT const char *_protocol_getMethodTypeEncoding(Protocol *p, SEL sel, BOOL isRequiredMethod, BOOL isInstanceMethod) - OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nullable +_protocol_getMethodTypeEncoding(Protocol * _Nonnull proto, SEL _Nonnull sel, + BOOL isRequiredMethod, BOOL isInstanceMethod) + OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0); +/** + * Function type for a function that is called when a realized class + * is about to be initialized. + * + * @param context The context pointer the function was registered with. + * @param cls The class that's about to be initialized. + */ +struct mach_header; +typedef void (*_objc_func_willInitializeClass)(void * _Nullable context, Class _Nonnull cls); + +/** + * Add a function to be called when a realized class is about to be + * initialized. The class can be queried and manipulated using runtime + * functions. Don't message it. + * + * When adding a new function, that function is immediately called with all + * realized classes that are already initialized or are in the process + * of initialization. + * + * @param func The function to add. + * @param context A context pointer that will be passed to the function when called. + */ +#define OBJC_WILLINITIALIZECLASSFUNC_DEFINED 1 +OBJC_EXPORT void _objc_addWillInitializeClassFunc(_objc_func_willInitializeClass _Nonnull func, void * _Nullable context) + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 4.0); + // API to only be called by classes that provide their own reference count storage -OBJC_EXPORT -void -_objc_deallocOnMainThreadHelper(void *context) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT void +_objc_deallocOnMainThreadHelper(void * _Nullable context) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); // On async versus sync deallocation and the _dealloc2main flag // diff --git a/runtime/objc-layout.mm b/runtime/objc-layout.mm index d125f08..47993ea 100644 --- a/runtime/objc-layout.mm +++ b/runtime/objc-layout.mm @@ -338,8 +338,8 @@ layout_bitmap_grow(layout_bitmap *bits, size_t newCount) realloc(bits->bits, (newAllocated+7) / 8); bits->bitsAllocated = newAllocated; } - assert(bits->bitsAllocated >= bits->bitCount); - assert(bits->bitsAllocated >= newCount); + ASSERT(bits->bitsAllocated >= bits->bitCount); + ASSERT(bits->bitsAllocated >= newCount); } diff --git a/runtime/objc-load.h b/runtime/objc-load.h index 8ec1fa2..0e334a2 100644 --- a/runtime/objc-load.h +++ b/runtime/objc-load.h @@ -35,20 +35,21 @@ /* dynamically loading Mach-O object files that contain Objective-C code */ OBJC_EXPORT long objc_loadModules ( - char *modlist[], - void *errStream, - void (*class_callback) (Class, Category), - /*headerType*/ struct mach_header **hdr_addr, - char *debug_file + char * _Nullable modlist[_Nullable], + void * _Nullable errStream, + void (* _Nullable class_callback) (Class _Nullable, Category _Nullable), + /*headerType*/ struct mach_header * _Nullable * _Nullable hdr_addr, + char * _Nullable debug_file ) OBJC2_UNAVAILABLE; + OBJC_EXPORT int objc_loadModule ( - char * moduleName, - void (*class_callback) (Class, Category), - int * errorCode + char * _Nullable moduleName, + void (* _Nullable class_callback) (Class _Nullable, Category _Nullable), + int * _Nullable errorCode ) OBJC2_UNAVAILABLE; OBJC_EXPORT long objc_unloadModules( - void *errorStream, /* input (optional) */ - void (*unloadCallback)(Class, Category) /* input (optional) */ + void * _Nullable errorStream, /* input (optional) */ + void (* _Nullable unloadCallback)(Class _Nullable, Category _Nullable) /* input (optional) */ ) OBJC2_UNAVAILABLE; #endif /* _OBJC_LOAD_H_ */ diff --git a/runtime/objc-loadmethod.mm b/runtime/objc-loadmethod.mm index 55ef00b..d095cee 100644 --- a/runtime/objc-loadmethod.mm +++ b/runtime/objc-loadmethod.mm @@ -201,7 +201,7 @@ static void call_class_loads(void) if (PrintLoading) { _objc_inform("LOAD: +[%s load]\n", cls->nameForLogging()); } - (*load_method)(cls, SEL_load); + (*load_method)(cls, @selector(load)); } // Destroy the detached list. @@ -248,7 +248,7 @@ static bool call_category_loads(void) cls->nameForLogging(), _category_getName(cat)); } - (*load_method)(cls, SEL_load); + (*load_method)(cls, @selector(load)); cats[i].cat = nil; } } diff --git a/runtime/objc-lockdebug.h b/runtime/objc-lockdebug.h index 071064d..a3048b1 100644 --- a/runtime/objc-lockdebug.h +++ b/runtime/objc-lockdebug.h @@ -21,32 +21,50 @@ * @APPLE_LICENSE_HEADER_END@ */ +#if LOCKDEBUG +extern void lockdebug_assert_all_locks_locked(); +extern void lockdebug_assert_no_locks_locked(); +extern void lockdebug_setInForkPrepare(bool); +extern void lockdebug_lock_precedes_lock(const void *oldlock, const void *newlock); +#else +static constexpr inline void lockdebug_assert_all_locks_locked() { } +static constexpr inline void lockdebug_assert_no_locks_locked() { } +static constexpr inline void lockdebug_setInForkPrepare(bool) { } +static constexpr inline void lockdebug_lock_precedes_lock(const void *, const void *) { } +#endif + +extern void lockdebug_remember_mutex(mutex_tt *lock); extern void lockdebug_mutex_lock(mutex_tt *lock); extern void lockdebug_mutex_try_lock(mutex_tt *lock); extern void lockdebug_mutex_unlock(mutex_tt *lock); extern void lockdebug_mutex_assert_locked(mutex_tt *lock); extern void lockdebug_mutex_assert_unlocked(mutex_tt *lock); -static inline void lockdebug_mutex_lock(mutex_tt *lock) { } -static inline void lockdebug_mutex_try_lock(mutex_tt *lock) { } -static inline void lockdebug_mutex_unlock(mutex_tt *lock) { } -static inline void lockdebug_mutex_assert_locked(mutex_tt *lock) { } -static inline void lockdebug_mutex_assert_unlocked(mutex_tt *lock) { } +static constexpr inline void lockdebug_remember_mutex(mutex_tt *lock) { } +static constexpr inline void lockdebug_mutex_lock(mutex_tt *lock) { } +static constexpr inline void lockdebug_mutex_try_lock(mutex_tt *lock) { } +static constexpr inline void lockdebug_mutex_unlock(mutex_tt *lock) { } +static constexpr inline void lockdebug_mutex_assert_locked(mutex_tt *lock) { } +static constexpr inline void lockdebug_mutex_assert_unlocked(mutex_tt *lock) { } +extern void lockdebug_remember_monitor(monitor_tt *lock); extern void lockdebug_monitor_enter(monitor_tt *lock); extern void lockdebug_monitor_leave(monitor_tt *lock); extern void lockdebug_monitor_wait(monitor_tt *lock); extern void lockdebug_monitor_assert_locked(monitor_tt *lock); extern void lockdebug_monitor_assert_unlocked(monitor_tt *lock); -static inline void lockdebug_monitor_enter(monitor_tt *lock) { } -static inline void lockdebug_monitor_leave(monitor_tt *lock) { } -static inline void lockdebug_monitor_wait(monitor_tt *lock) { } -static inline void lockdebug_monitor_assert_locked(monitor_tt *lock) { } -static inline void lockdebug_monitor_assert_unlocked(monitor_tt *lock) {} +static constexpr inline void lockdebug_remember_monitor(monitor_tt *lock) { } +static constexpr inline void lockdebug_monitor_enter(monitor_tt *lock) { } +static constexpr inline void lockdebug_monitor_leave(monitor_tt *lock) { } +static constexpr inline void lockdebug_monitor_wait(monitor_tt *lock) { } +static constexpr inline void lockdebug_monitor_assert_locked(monitor_tt *lock) { } +static constexpr inline void lockdebug_monitor_assert_unlocked(monitor_tt *lock) {} +extern void +lockdebug_remember_recursive_mutex(recursive_mutex_tt *lock); extern void lockdebug_recursive_mutex_lock(recursive_mutex_tt *lock); extern void @@ -56,34 +74,13 @@ lockdebug_recursive_mutex_assert_locked(recursive_mutex_tt *lock); extern void lockdebug_recursive_mutex_assert_unlocked(recursive_mutex_tt *lock); -static inline void +static constexpr inline void +lockdebug_remember_recursive_mutex(recursive_mutex_tt *lock) { } +static constexpr inline void lockdebug_recursive_mutex_lock(recursive_mutex_tt *lock) { } -static inline void +static constexpr inline void lockdebug_recursive_mutex_unlock(recursive_mutex_tt *lock) { } -static inline void +static constexpr inline void lockdebug_recursive_mutex_assert_locked(recursive_mutex_tt *lock) { } -static inline void +static constexpr inline void lockdebug_recursive_mutex_assert_unlocked(recursive_mutex_tt *lock) { } - - -extern void lockdebug_rwlock_read(rwlock_tt *lock); -extern void lockdebug_rwlock_try_read_success(rwlock_tt *lock); -extern void lockdebug_rwlock_unlock_read(rwlock_tt *lock); -extern void lockdebug_rwlock_write(rwlock_tt *lock); -extern void lockdebug_rwlock_try_write_success(rwlock_tt *lock); -extern void lockdebug_rwlock_unlock_write(rwlock_tt *lock); -extern void lockdebug_rwlock_assert_reading(rwlock_tt *lock); -extern void lockdebug_rwlock_assert_writing(rwlock_tt *lock); -extern void lockdebug_rwlock_assert_locked(rwlock_tt *lock); -extern void lockdebug_rwlock_assert_unlocked(rwlock_tt *lock); - -static inline void lockdebug_rwlock_read(rwlock_tt *) { } -static inline void lockdebug_rwlock_try_read_success(rwlock_tt *) { } -static inline void lockdebug_rwlock_unlock_read(rwlock_tt *) { } -static inline void lockdebug_rwlock_write(rwlock_tt *) { } -static inline void lockdebug_rwlock_try_write_success(rwlock_tt *) { } -static inline void lockdebug_rwlock_unlock_write(rwlock_tt *) { } -static inline void lockdebug_rwlock_assert_reading(rwlock_tt *) { } -static inline void lockdebug_rwlock_assert_writing(rwlock_tt *) { } -static inline void lockdebug_rwlock_assert_locked(rwlock_tt *) { } -static inline void lockdebug_rwlock_assert_unlocked(rwlock_tt *) { } diff --git a/runtime/objc-lockdebug.mm b/runtime/objc-lockdebug.mm index ed94011..f182a27 100644 --- a/runtime/objc-lockdebug.mm +++ b/runtime/objc-lockdebug.mm @@ -28,117 +28,253 @@ #include "objc-private.h" -#if DEBUG && !TARGET_OS_WIN32 +#if LOCKDEBUG && !TARGET_OS_WIN32 + +#include + + +/*********************************************************************** +* Thread-local bool set during _objc_atfork_prepare(). +* That function is allowed to break some lock ordering rules. +**********************************************************************/ + +static tls_key_t fork_prepare_tls; + +void +lockdebug_setInForkPrepare(bool inForkPrepare) +{ + INIT_ONCE_PTR(fork_prepare_tls, tls_create(nil), (void)0); + tls_set(fork_prepare_tls, (void*)inForkPrepare); +} + +static bool +inForkPrepare() +{ + INIT_ONCE_PTR(fork_prepare_tls, tls_create(nil), (void)0); + return (bool)tls_get(fork_prepare_tls); +} + + + +/*********************************************************************** +* Lock order graph. +* "lock X precedes lock Y" means that X must be acquired first. +* This property is transitive. +**********************************************************************/ + +struct lockorder { + const void *l; + std::vector predecessors; + + mutable std::unordered_map memo; + + lockorder(const void *newl) : l(newl) { } +}; + +static std::unordered_map lockOrderList; +// not mutex_t because we don't want lock debugging on this lock +static mutex_tt lockOrderLock; + +static bool +lockPrecedesLock(const lockorder *oldlock, const lockorder *newlock) +{ + auto memoed = newlock->memo.find(oldlock); + if (memoed != newlock->memo.end()) { + return memoed->second; + } + + bool result = false; + for (const auto *pre : newlock->predecessors) { + if (oldlock == pre || lockPrecedesLock(oldlock, pre)) { + result = true; + break; + } + } + + newlock->memo[oldlock] = result; + return result; +} + +static bool +lockPrecedesLock(const void *oldlock, const void *newlock) +{ + mutex_tt::locker lock(lockOrderLock); + + auto oldorder = lockOrderList.find(oldlock); + auto neworder = lockOrderList.find(newlock); + if (neworder == lockOrderList.end() || oldorder == lockOrderList.end()) { + return false; + } + return lockPrecedesLock(oldorder->second, neworder->second); +} + +static bool +lockUnorderedWithLock(const void *oldlock, const void *newlock) +{ + mutex_tt::locker lock(lockOrderLock); + + auto oldorder = lockOrderList.find(oldlock); + auto neworder = lockOrderList.find(newlock); + if (neworder == lockOrderList.end() || oldorder == lockOrderList.end()) { + return true; + } + + if (lockPrecedesLock(oldorder->second, neworder->second) || + lockPrecedesLock(neworder->second, oldorder->second)) + { + return false; + } + + return true; +} + +void lockdebug_lock_precedes_lock(const void *oldlock, const void *newlock) +{ + if (lockPrecedesLock(newlock, oldlock)) { + _objc_fatal("contradiction in lock order declaration"); + } + + mutex_tt::locker lock(lockOrderLock); + + auto oldorder = lockOrderList.find(oldlock); + auto neworder = lockOrderList.find(newlock); + if (oldorder == lockOrderList.end()) { + lockOrderList[oldlock] = new lockorder(oldlock); + oldorder = lockOrderList.find(oldlock); + } + if (neworder == lockOrderList.end()) { + lockOrderList[newlock] = new lockorder(newlock); + neworder = lockOrderList.find(newlock); + } + + neworder->second->predecessors.push_back(oldorder->second); +} + /*********************************************************************** * Recording - per-thread list of mutexes and monitors held **********************************************************************/ -typedef struct { - void *l; // the lock itself - int k; // the kind of lock it is (MUTEX, MONITOR, etc) - int i; // the lock's nest count -} lockcount; +enum class lockkind { + MUTEX = 1, MONITOR = 2, RDLOCK = 3, WRLOCK = 4, RECURSIVE = 5 +}; -#define MUTEX 1 -#define MONITOR 2 -#define RDLOCK 3 -#define WRLOCK 4 -#define RECURSIVE 5 +#define MUTEX lockkind::MUTEX +#define MONITOR lockkind::MONITOR +#define RDLOCK lockkind::RDLOCK +#define WRLOCK lockkind::WRLOCK +#define RECURSIVE lockkind::RECURSIVE -typedef struct _objc_lock_list { - int allocated; - int used; - lockcount list[0]; -} _objc_lock_list; +struct lockcount { + lockkind k; // the kind of lock it is (MUTEX, MONITOR, etc) + int i; // the lock's nest count +}; +using objc_lock_list = std::unordered_map; + + +// Thread-local list of locks owned by a thread. +// Used by lock ownership checks. static tls_key_t lock_tls; +// Global list of all locks. +// Used by fork() safety check. +// This can't be a static struct because of C++ initialization order problems. +static objc_lock_list& AllLocks() { + static objc_lock_list *locks; + INIT_ONCE_PTR(locks, new objc_lock_list, (void)0); + return *locks; +} + + static void destroyLocks(void *value) { - _objc_lock_list *locks = (_objc_lock_list *)value; + auto locks = (objc_lock_list *)value; // fixme complain about any still-held locks? - if (locks) free(locks); + if (locks) delete locks; } -static struct _objc_lock_list * -getLocks(BOOL create) +static objc_lock_list& +ownedLocks() { - _objc_lock_list *locks; - // Use a dedicated tls key to prevent differences vs non-debug in // usage of objc's other tls keys (required for some unit tests). INIT_ONCE_PTR(lock_tls, tls_create(&destroyLocks), (void)0); - locks = (_objc_lock_list *)tls_get(lock_tls); + auto locks = (objc_lock_list *)tls_get(lock_tls); if (!locks) { - if (!create) { - return NULL; - } else { - locks = (_objc_lock_list *)calloc(1, sizeof(_objc_lock_list) + sizeof(lockcount) * 16); - locks->allocated = 16; - locks->used = 0; - tls_set(lock_tls, locks); - } + locks = new objc_lock_list; + tls_set(lock_tls, locks); } - if (locks->allocated == locks->used) { - if (!create) { - return locks; - } else { - _objc_lock_list *oldlocks = locks; - locks = (_objc_lock_list *)calloc(1, sizeof(_objc_lock_list) + 2 * oldlocks->used * sizeof(lockcount)); - locks->used = oldlocks->used; - locks->allocated = oldlocks->used * 2; - memcpy(locks->list, oldlocks->list, locks->used * sizeof(lockcount)); - tls_set(lock_tls, locks); - free(oldlocks); - } - } - - return locks; + return *locks; } -static BOOL -hasLock(_objc_lock_list *locks, void *lock, int kind) +static bool +hasLock(objc_lock_list& locks, const void *lock, lockkind kind) { - int i; - if (!locks) return NO; - - for (i = 0; i < locks->used; i++) { - if (locks->list[i].l == lock && locks->list[i].k == kind) return YES; - } - return NO; + auto iter = locks.find(lock); + if (iter != locks.end() && iter->second.k == kind) return true; + return false; } -static void -setLock(_objc_lock_list *locks, void *lock, int kind) +static const char *sym(const void *lock) { - int i; - for (i = 0; i < locks->used; i++) { - if (locks->list[i].l == lock && locks->list[i].k == kind) { - locks->list[i].i++; - return; - } - } - - locks->list[locks->used].l = lock; - locks->list[locks->used].i = 1; - locks->list[locks->used].k = kind; - locks->used++; + Dl_info info; + int ok = dladdr(lock, &info); + if (ok && info.dli_sname && info.dli_sname[0]) return info.dli_sname; + else return "??"; } static void -clearLock(_objc_lock_list *locks, void *lock, int kind) +setLock(objc_lock_list& locks, const void *lock, lockkind kind) { - int i; - for (i = 0; i < locks->used; i++) { - if (locks->list[i].l == lock && locks->list[i].k == kind) { - if (--locks->list[i].i == 0) { - locks->list[i].l = NULL; - locks->list[i] = locks->list[--locks->used]; + // Check if we already own this lock. + auto iter = locks.find(lock); + if (iter != locks.end() && iter->second.k == kind) { + iter->second.i++; + return; + } + + // Newly-acquired lock. Verify lock ordering. + // Locks not in AllLocks are exempt (i.e. @synchronize locks) + if (&locks != &AllLocks() && AllLocks().find(lock) != AllLocks().end()) { + for (auto& oldlock : locks) { + if (AllLocks().find(oldlock.first) == AllLocks().end()) { + // oldlock is exempt + continue; + } + + if (lockPrecedesLock(lock, oldlock.first)) { + _objc_fatal("lock %p (%s) incorrectly acquired before %p (%s)", + oldlock.first, sym(oldlock.first), lock, sym(lock)); + } + if (!inForkPrepare() && + lockUnorderedWithLock(lock, oldlock.first)) + { + // _objc_atfork_prepare is allowed to acquire + // otherwise-unordered locks, but nothing else may. + _objc_fatal("lock %p (%s) acquired before %p (%s) " + "with no defined lock order", + oldlock.first, sym(oldlock.first), lock, sym(lock)); + } + } + } + + locks[lock] = lockcount{kind, 1}; +} + +static void +clearLock(objc_lock_list& locks, const void *lock, lockkind kind) +{ + auto iter = locks.find(lock); + if (iter != locks.end()) { + auto& l = iter->second; + if (l.k == kind) { + if (--l.i == 0) { + locks.erase(iter); } return; } @@ -149,49 +285,61 @@ clearLock(_objc_lock_list *locks, void *lock, int kind) /*********************************************************************** -* Mutex checking +* fork() safety checking **********************************************************************/ -#if !TARGET_OS_SIMULATOR -// Non-simulator platforms have lock debugging built into os_unfair_lock. - - void -lockdebug_mutex_lock(mutex_t *lock) +lockdebug_remember_mutex(mutex_t *lock) { - // empty + setLock(AllLocks(), lock, MUTEX); } void -lockdebug_mutex_unlock(mutex_t *lock) +lockdebug_remember_recursive_mutex(recursive_mutex_t *lock) { - // empty + setLock(AllLocks(), lock, RECURSIVE); } void -lockdebug_mutex_assert_locked(mutex_t *lock) +lockdebug_remember_monitor(monitor_t *lock) { - os_unfair_lock_assert_owner((os_unfair_lock *)lock); + setLock(AllLocks(), lock, MONITOR); } void -lockdebug_mutex_assert_unlocked(mutex_t *lock) +lockdebug_assert_all_locks_locked() { - os_unfair_lock_assert_not_owner((os_unfair_lock *)lock); + auto& owned = ownedLocks(); + + for (const auto& l : AllLocks()) { + if (!hasLock(owned, l.first, l.second.k)) { + _objc_fatal("lock %p:%d is incorrectly not owned", + l.first, l.second.k); + } + } +} + +void +lockdebug_assert_no_locks_locked() +{ + auto& owned = ownedLocks(); + + for (const auto& l : AllLocks()) { + if (hasLock(owned, l.first, l.second.k)) { + _objc_fatal("lock %p:%d is incorrectly owned", l.first, l.second.k); + } + } } -// !TARGET_OS_SIMULATOR -#else -// TARGET_OS_SIMULATOR - -// Simulator platforms have no built-in lock debugging in os_unfair_lock. - +/*********************************************************************** +* Mutex checking +**********************************************************************/ void lockdebug_mutex_lock(mutex_t *lock) { - _objc_lock_list *locks = getLocks(YES); + auto& locks = ownedLocks(); if (hasLock(locks, lock, MUTEX)) { _objc_fatal("deadlock: relocking mutex"); @@ -205,14 +353,14 @@ lockdebug_mutex_lock(mutex_t *lock) void lockdebug_mutex_try_lock_success(mutex_t *lock) { - _objc_lock_list *locks = getLocks(YES); + auto& locks = ownedLocks(); setLock(locks, lock, MUTEX); } void lockdebug_mutex_unlock(mutex_t *lock) { - _objc_lock_list *locks = getLocks(NO); + auto& locks = ownedLocks(); if (!hasLock(locks, lock, MUTEX)) { _objc_fatal("unlocking unowned mutex"); @@ -224,7 +372,7 @@ lockdebug_mutex_unlock(mutex_t *lock) void lockdebug_mutex_assert_locked(mutex_t *lock) { - _objc_lock_list *locks = getLocks(NO); + auto& locks = ownedLocks(); if (!hasLock(locks, lock, MUTEX)) { _objc_fatal("mutex incorrectly not locked"); @@ -234,7 +382,7 @@ lockdebug_mutex_assert_locked(mutex_t *lock) void lockdebug_mutex_assert_unlocked(mutex_t *lock) { - _objc_lock_list *locks = getLocks(NO); + auto& locks = ownedLocks(); if (hasLock(locks, lock, MUTEX)) { _objc_fatal("mutex incorrectly locked"); @@ -242,24 +390,21 @@ lockdebug_mutex_assert_unlocked(mutex_t *lock) } -// TARGET_OS_SIMULATOR -#endif - /*********************************************************************** * Recursive mutex checking **********************************************************************/ void -lockdebug_recursive_mutex_lock(recursive_mutex_tt *lock) +lockdebug_recursive_mutex_lock(recursive_mutex_t *lock) { - _objc_lock_list *locks = getLocks(YES); + auto& locks = ownedLocks(); setLock(locks, lock, RECURSIVE); } void -lockdebug_recursive_mutex_unlock(recursive_mutex_tt *lock) +lockdebug_recursive_mutex_unlock(recursive_mutex_t *lock) { - _objc_lock_list *locks = getLocks(NO); + auto& locks = ownedLocks(); if (!hasLock(locks, lock, RECURSIVE)) { _objc_fatal("unlocking unowned recursive mutex"); @@ -269,9 +414,9 @@ lockdebug_recursive_mutex_unlock(recursive_mutex_tt *lock) void -lockdebug_recursive_mutex_assert_locked(recursive_mutex_tt *lock) +lockdebug_recursive_mutex_assert_locked(recursive_mutex_t *lock) { - _objc_lock_list *locks = getLocks(NO); + auto& locks = ownedLocks(); if (!hasLock(locks, lock, RECURSIVE)) { _objc_fatal("recursive mutex incorrectly not locked"); @@ -279,9 +424,9 @@ lockdebug_recursive_mutex_assert_locked(recursive_mutex_tt *lock) } void -lockdebug_recursive_mutex_assert_unlocked(recursive_mutex_tt *lock) +lockdebug_recursive_mutex_assert_unlocked(recursive_mutex_t *lock) { - _objc_lock_list *locks = getLocks(NO); + auto& locks = ownedLocks(); if (hasLock(locks, lock, RECURSIVE)) { _objc_fatal("recursive mutex incorrectly locked"); @@ -296,7 +441,7 @@ lockdebug_recursive_mutex_assert_unlocked(recursive_mutex_tt *lock) void lockdebug_monitor_enter(monitor_t *lock) { - _objc_lock_list *locks = getLocks(YES); + auto& locks = ownedLocks(); if (hasLock(locks, lock, MONITOR)) { _objc_fatal("deadlock: relocking monitor"); @@ -307,7 +452,7 @@ lockdebug_monitor_enter(monitor_t *lock) void lockdebug_monitor_leave(monitor_t *lock) { - _objc_lock_list *locks = getLocks(NO); + auto& locks = ownedLocks(); if (!hasLock(locks, lock, MONITOR)) { _objc_fatal("unlocking unowned monitor"); @@ -318,7 +463,7 @@ lockdebug_monitor_leave(monitor_t *lock) void lockdebug_monitor_wait(monitor_t *lock) { - _objc_lock_list *locks = getLocks(NO); + auto& locks = ownedLocks(); if (!hasLock(locks, lock, MONITOR)) { _objc_fatal("waiting in unowned monitor"); @@ -329,7 +474,7 @@ lockdebug_monitor_wait(monitor_t *lock) void lockdebug_monitor_assert_locked(monitor_t *lock) { - _objc_lock_list *locks = getLocks(NO); + auto& locks = ownedLocks(); if (!hasLock(locks, lock, MONITOR)) { _objc_fatal("monitor incorrectly not locked"); @@ -339,133 +484,11 @@ lockdebug_monitor_assert_locked(monitor_t *lock) void lockdebug_monitor_assert_unlocked(monitor_t *lock) { - _objc_lock_list *locks = getLocks(NO); + auto& locks = ownedLocks(); if (hasLock(locks, lock, MONITOR)) { _objc_fatal("monitor incorrectly held"); } } - -/*********************************************************************** -* rwlock checking -**********************************************************************/ - -void -lockdebug_rwlock_read(rwlock_tt *lock) -{ - _objc_lock_list *locks = getLocks(YES); - - if (hasLock(locks, lock, RDLOCK)) { - // Recursive rwlock read is bad (may deadlock vs pending writer) - _objc_fatal("recursive rwlock read"); - } - if (hasLock(locks, lock, WRLOCK)) { - _objc_fatal("deadlock: read after write for rwlock"); - } - setLock(locks, lock, RDLOCK); -} - -// try-read success is the only case with lockdebug effects. -// try-read when already reading is OK (won't deadlock) -// try-read when already writing is OK (will fail) -// try-read failure does nothing. -void -lockdebug_rwlock_try_read_success(rwlock_tt *lock) -{ - _objc_lock_list *locks = getLocks(YES); - setLock(locks, lock, RDLOCK); -} - -void -lockdebug_rwlock_unlock_read(rwlock_tt *lock) -{ - _objc_lock_list *locks = getLocks(NO); - - if (!hasLock(locks, lock, RDLOCK)) { - _objc_fatal("un-reading unowned rwlock"); - } - clearLock(locks, lock, RDLOCK); -} - - -void -lockdebug_rwlock_write(rwlock_tt *lock) -{ - _objc_lock_list *locks = getLocks(YES); - - if (hasLock(locks, lock, RDLOCK)) { - // Lock promotion not allowed (may deadlock) - _objc_fatal("deadlock: write after read for rwlock"); - } - if (hasLock(locks, lock, WRLOCK)) { - _objc_fatal("recursive rwlock write"); - } - setLock(locks, lock, WRLOCK); -} - -// try-write success is the only case with lockdebug effects. -// try-write when already reading is OK (will fail) -// try-write when already writing is OK (will fail) -// try-write failure does nothing. -void -lockdebug_rwlock_try_write_success(rwlock_tt *lock) -{ - _objc_lock_list *locks = getLocks(YES); - setLock(locks, lock, WRLOCK); -} - -void -lockdebug_rwlock_unlock_write(rwlock_tt *lock) -{ - _objc_lock_list *locks = getLocks(NO); - - if (!hasLock(locks, lock, WRLOCK)) { - _objc_fatal("un-writing unowned rwlock"); - } - clearLock(locks, lock, WRLOCK); -} - - -void -lockdebug_rwlock_assert_reading(rwlock_tt *lock) -{ - _objc_lock_list *locks = getLocks(NO); - - if (!hasLock(locks, lock, RDLOCK)) { - _objc_fatal("rwlock incorrectly not reading"); - } -} - -void -lockdebug_rwlock_assert_writing(rwlock_tt *lock) -{ - _objc_lock_list *locks = getLocks(NO); - - if (!hasLock(locks, lock, WRLOCK)) { - _objc_fatal("rwlock incorrectly not writing"); - } -} - -void -lockdebug_rwlock_assert_locked(rwlock_tt *lock) -{ - _objc_lock_list *locks = getLocks(NO); - - if (!hasLock(locks, lock, RDLOCK) && !hasLock(locks, lock, WRLOCK)) { - _objc_fatal("rwlock incorrectly neither reading nor writing"); - } -} - -void -lockdebug_rwlock_assert_unlocked(rwlock_tt *lock) -{ - _objc_lock_list *locks = getLocks(NO); - - if (hasLock(locks, lock, RDLOCK) || hasLock(locks, lock, WRLOCK)) { - _objc_fatal("rwlock incorrectly not unlocked"); - } -} - - #endif diff --git a/runtime/objc-locks-new.h b/runtime/objc-locks-new.h new file mode 100644 index 0000000..cd3ace3 --- /dev/null +++ b/runtime/objc-locks-new.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2017 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +/*********************************************************************** +* objc-locks-new.h +* Declarations of all locks used in the runtime. +**********************************************************************/ + +#ifndef _OBJC_LOCKS_NEW_H +#define _OBJC_LOCKS_NEW_H + +// fork() safety requires careful tracking of all locks used in the runtime. +// Thou shalt not declare any locks outside this file. + +extern mutex_t runtimeLock; +extern mutex_t DemangleCacheLock; + +#endif diff --git a/runtime/objc-locks-old.h b/runtime/objc-locks-old.h new file mode 100644 index 0000000..1926136 --- /dev/null +++ b/runtime/objc-locks-old.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +/*********************************************************************** +* objc-locks-old.h +* Declarations of all locks used in the runtime. +**********************************************************************/ + +#ifndef _OBJC_LOCKS_OLD_H +#define _OBJC_LOCKS_OLD_H + +// fork() safety requires careful tracking of all locks used in the runtime. +// Thou shalt not declare any locks outside this file. + +extern mutex_t classLock; +extern mutex_t methodListLock; +extern mutex_t NXUniqueStringLock; +extern spinlock_t impLock; + +#endif diff --git a/runtime/objc-locks.h b/runtime/objc-locks.h new file mode 100644 index 0000000..cd4a419 --- /dev/null +++ b/runtime/objc-locks.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2017 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +/*********************************************************************** +* objc-locks.h +* Declarations of all locks used in the runtime. +**********************************************************************/ + +#ifndef _OBJC_LOCKS_H +#define _OBJC_LOCKS_H + +// fork() safety requires careful tracking of all locks used in the runtime. +// Thou shalt not declare any locks outside this file. + +// Lock ordering is declared in _objc_fork_prepare() +// and is enforced by lockdebug. + +extern monitor_t classInitLock; +extern mutex_t selLock; +#if CONFIG_USE_CACHE_LOCK +extern mutex_t cacheUpdateLock; +#endif +extern recursive_mutex_t loadMethodLock; +extern mutex_t crashlog_lock; +extern spinlock_t objcMsgLogLock; +extern mutex_t AltHandlerDebugLock; +extern mutex_t AssociationsManagerLock; +extern StripedMap PropertyLocks; +extern StripedMap StructLocks; +extern StripedMap CppObjectLocks; + +// SideTable lock is buried awkwardly. Call a function to manipulate it. +extern void SideTableLockAll(); +extern void SideTableUnlockAll(); +extern void SideTableForceResetAll(); +extern void SideTableDefineLockOrder(); +extern void SideTableLocksPrecedeLock(const void *newlock); +extern void SideTableLocksSucceedLock(const void *oldlock); +extern void SideTableLocksPrecedeLocks(StripedMap& newlocks); +extern void SideTableLocksSucceedLocks(StripedMap& oldlocks); + +#if __OBJC2__ +#include "objc-locks-new.h" +#else +#include "objc-locks-old.h" +#endif + +#endif diff --git a/runtime/objc-object.h b/runtime/objc-object.h index 821b0a7..2c17c94 100644 --- a/runtime/objc-object.h +++ b/runtime/objc-object.h @@ -43,7 +43,7 @@ bool prepareOptimizedReturn(ReturnDisposition disposition); #if SUPPORT_TAGGED_POINTERS extern "C" { - extern Class objc_debug_taggedpointer_classes[_OBJC_TAG_SLOT_COUNT*2]; + extern Class objc_debug_taggedpointer_classes[_OBJC_TAG_SLOT_COUNT]; extern Class objc_debug_taggedpointer_ext_classes[_OBJC_TAG_EXT_SLOT_COUNT]; } #define objc_tag_classes objc_debug_taggedpointer_classes @@ -55,8 +55,8 @@ extern "C" { ALWAYS_INLINE Class & classForIndex(uintptr_t index) { - assert(index > 0); - assert(index < (uintptr_t)objc_indexed_classes_count); + ASSERT(index > 0); + ASSERT(index < (uintptr_t)objc_indexed_classes_count); return objc_indexed_classes[index]; } @@ -76,20 +76,26 @@ objc_object::isClass() inline Class objc_object::getIsa() { - if (!isTaggedPointer()) return ISA(); + if (fastpath(!isTaggedPointer())) return ISA(); - uintptr_t ptr = (uintptr_t)this; - if (isExtTaggedPointer()) { - uintptr_t slot = - (ptr >> _OBJC_TAG_EXT_SLOT_SHIFT) & _OBJC_TAG_EXT_SLOT_MASK; - return objc_tag_ext_classes[slot]; - } else { - uintptr_t slot = - (ptr >> _OBJC_TAG_SLOT_SHIFT) & _OBJC_TAG_SLOT_MASK; - return objc_tag_classes[slot]; + extern objc_class OBJC_CLASS_$___NSUnrecognizedTaggedPointer; + uintptr_t slot, ptr = (uintptr_t)this; + Class cls; + + slot = (ptr >> _OBJC_TAG_SLOT_SHIFT) & _OBJC_TAG_SLOT_MASK; + cls = objc_tag_classes[slot]; + if (slowpath(cls == (Class)&OBJC_CLASS_$___NSUnrecognizedTaggedPointer)) { + slot = (ptr >> _OBJC_TAG_EXT_SLOT_SHIFT) & _OBJC_TAG_EXT_SLOT_MASK; + cls = objc_tag_ext_classes[slot]; } + return cls; } +inline uintptr_t +objc_object::isaBits() const +{ + return isa.bits; +} inline bool objc_object::isTaggedPointer() @@ -106,7 +112,8 @@ objc_object::isBasicTaggedPointer() inline bool objc_object::isExtTaggedPointer() { - return ((uintptr_t)this & _OBJC_TAG_EXT_MASK) == _OBJC_TAG_EXT_MASK; + uintptr_t ptr = _objc_decodeTaggedPointer(this); + return (ptr & _OBJC_TAG_EXT_MASK) == _OBJC_TAG_EXT_MASK; } @@ -121,6 +128,12 @@ objc_object::getIsa() return ISA(); } +inline uintptr_t +objc_object::isaBits() const +{ + return isa.bits; +} + inline bool objc_object::isTaggedPointer() @@ -150,7 +163,7 @@ objc_object::isExtTaggedPointer() inline Class objc_object::ISA() { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); #if SUPPORT_INDEXED_ISA if (isa.nonpointer) { uintptr_t slot = isa.indexcls; @@ -162,6 +175,12 @@ objc_object::ISA() #endif } +inline Class +objc_object::rawISA() +{ + ASSERT(!isTaggedPointer() && !isa.nonpointer); + return (Class)isa.bits; +} inline bool objc_object::hasNonpointerIsa() @@ -195,8 +214,8 @@ objc_object::initProtocolIsa(Class cls) inline void objc_object::initInstanceIsa(Class cls, bool hasCxxDtor) { - assert(!cls->instancesRequireRawIsa()); - assert(hasCxxDtor == cls->hasCxxDtor()); + ASSERT(!cls->instancesRequireRawIsa()); + ASSERT(hasCxxDtor == cls->hasCxxDtor()); initIsa(cls, true, hasCxxDtor); } @@ -204,18 +223,18 @@ objc_object::initInstanceIsa(Class cls, bool hasCxxDtor) inline void objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor) { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); if (!nonpointer) { - isa.cls = cls; + isa = isa_t((uintptr_t)cls); } else { - assert(!DisableNonpointerIsa); - assert(!cls->instancesRequireRawIsa()); + ASSERT(!DisableNonpointerIsa); + ASSERT(!cls->instancesRequireRawIsa()); isa_t newisa(0); #if SUPPORT_INDEXED_ISA - assert(cls->classArrayIndex() > 0); + ASSERT(cls->classArrayIndex() > 0); newisa.bits = ISA_INDEX_MAGIC_VALUE; // isa.magic is part of ISA_MAGIC_VALUE // isa.nonpointer is part of ISA_MAGIC_VALUE @@ -248,7 +267,7 @@ objc_object::changeIsa(Class newCls) // assert(newCls->isFuture() || // newCls->isInitializing() || newCls->isInitialized()); - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); isa_t oldisa; isa_t newisa; @@ -270,7 +289,7 @@ objc_object::changeIsa(Class newCls) // isa.magic is part of ISA_MAGIC_VALUE // isa.nonpointer is part of ISA_MAGIC_VALUE newisa.has_cxx_dtor = newCls->hasCxxDtor(); - assert(newCls->classArrayIndex() > 0); + ASSERT(newCls->classArrayIndex() > 0); newisa.indexcls = (uintptr_t)newCls->classArrayIndex(); #else if (oldisa.bits == 0) newisa.bits = ISA_MAGIC_VALUE; @@ -350,7 +369,7 @@ objc_object::setHasAssociatedObjects() inline bool objc_object::isWeaklyReferenced() { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); if (isa.nonpointer) return isa.weakly_referenced; else return sidetable_isWeaklyReferenced(); } @@ -379,7 +398,7 @@ objc_object::setWeaklyReferenced_nolock() inline bool objc_object::hasCxxDtor() { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); if (isa.nonpointer) return isa.has_cxx_dtor; else return isa.cls->hasCxxDtor(); } @@ -435,13 +454,13 @@ objc_object::rootDealloc() inline id objc_object::retain() { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); if (fastpath(!ISA()->hasCustomRR())) { return rootRetain(); } - return ((id(*)(objc_object *, SEL))objc_msgSend)(this, SEL_retain); + return ((id(*)(objc_object *, SEL))objc_msgSend)(this, @selector(retain)); } @@ -483,6 +502,7 @@ objc_object::rootRetain(bool tryRetain, bool handleOverflow) newisa = oldisa; if (slowpath(!newisa.nonpointer)) { ClearExclusive(&isa.bits); + if (rawISA()->isMetaClass()) return (id)this; if (!tryRetain && sideTableLocked) sidetable_unlock(); if (tryRetain) return sidetable_tryRetain() ? (id)this : nil; else return sidetable_retain(); @@ -526,14 +546,14 @@ objc_object::rootRetain(bool tryRetain, bool handleOverflow) inline void objc_object::release() { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); if (fastpath(!ISA()->hasCustomRR())) { rootRelease(); return; } - ((void(*)(objc_object *, SEL))objc_msgSend)(this, SEL_release); + ((void(*)(objc_object *, SEL))objc_msgSend)(this, @selector(release)); } @@ -575,6 +595,7 @@ objc_object::rootRelease(bool performDealloc, bool handleUnderflow) newisa = oldisa; if (slowpath(!newisa.nonpointer)) { ClearExclusive(&isa.bits); + if (rawISA()->isMetaClass()) return false; if (sideTableLocked) sidetable_unlock(); return sidetable_release(performDealloc); } @@ -676,9 +697,10 @@ objc_object::rootRelease(bool performDealloc, bool handleUnderflow) if (slowpath(sideTableLocked)) sidetable_unlock(); - __sync_synchronize(); + __c11_atomic_thread_fence(__ATOMIC_ACQUIRE); + if (performDealloc) { - ((void(*)(objc_object *, SEL))objc_msgSend)(this, SEL_dealloc); + ((void(*)(objc_object *, SEL))objc_msgSend)(this, @selector(dealloc)); } return true; } @@ -688,10 +710,12 @@ objc_object::rootRelease(bool performDealloc, bool handleUnderflow) inline id objc_object::autorelease() { - if (isTaggedPointer()) return (id)this; - if (fastpath(!ISA()->hasCustomRR())) return rootAutorelease(); + ASSERT(!isTaggedPointer()); + if (fastpath(!ISA()->hasCustomRR())) { + return rootAutorelease(); + } - return ((id(*)(objc_object *, SEL))objc_msgSend)(this, SEL_autorelease); + return ((id(*)(objc_object *, SEL))objc_msgSend)(this, @selector(autorelease)); } @@ -736,10 +760,15 @@ objc_object::rootRetainCount() inline Class objc_object::ISA() { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); return isa.cls; } +inline Class +objc_object::rawISA() +{ + return ISA(); +} inline bool objc_object::hasNonpointerIsa() @@ -751,7 +780,7 @@ objc_object::hasNonpointerIsa() inline void objc_object::initIsa(Class cls) { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); isa = (uintptr_t)cls; } @@ -792,7 +821,7 @@ objc_object::changeIsa(Class cls) // assert(cls->isFuture() || // cls->isInitializing() || cls->isInitialized()); - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); isa_t oldisa, newisa; newisa.cls = cls; @@ -825,7 +854,7 @@ objc_object::setHasAssociatedObjects() inline bool objc_object::isWeaklyReferenced() { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); return sidetable_isWeaklyReferenced(); } @@ -834,7 +863,7 @@ objc_object::isWeaklyReferenced() inline void objc_object::setWeaklyReferenced_nolock() { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); sidetable_setWeaklyReferenced_nolock(); } @@ -843,7 +872,7 @@ objc_object::setWeaklyReferenced_nolock() inline bool objc_object::hasCxxDtor() { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); return isa.cls->hasCxxDtor(); } @@ -875,13 +904,13 @@ objc_object::rootDealloc() inline id objc_object::retain() { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); if (fastpath(!ISA()->hasCustomRR())) { return sidetable_retain(); } - return ((id(*)(objc_object *, SEL))objc_msgSend)(this, SEL_retain); + return ((id(*)(objc_object *, SEL))objc_msgSend)(this, @selector(retain)); } @@ -900,14 +929,14 @@ objc_object::rootRetain() inline void objc_object::release() { - assert(!isTaggedPointer()); + ASSERT(!isTaggedPointer()); if (fastpath(!ISA()->hasCustomRR())) { sidetable_release(); return; } - ((void(*)(objc_object *, SEL))objc_msgSend)(this, SEL_release); + ((void(*)(objc_object *, SEL))objc_msgSend)(this, @selector(release)); } @@ -938,7 +967,7 @@ objc_object::autorelease() if (isTaggedPointer()) return (id)this; if (fastpath(!ISA()->hasCustomRR())) return rootAutorelease(); - return ((id(*)(objc_object *, SEL))objc_msgSend)(this, SEL_autorelease); + return ((id(*)(objc_object *, SEL))objc_msgSend)(this, @selector(autorelease)); } @@ -1167,7 +1196,7 @@ setReturnDisposition(ReturnDisposition disposition) static ALWAYS_INLINE bool prepareOptimizedReturn(ReturnDisposition disposition) { - assert(getReturnDisposition() == ReturnAtPlus0); + ASSERT(getReturnDisposition() == ReturnAtPlus0); if (callerAcceptsOptimizedReturn(__builtin_return_address(0))) { if (disposition) setReturnDisposition(disposition); diff --git a/runtime/objc-opt.mm b/runtime/objc-opt.mm index 19533df..f4afb20 100644 --- a/runtime/objc-opt.mm +++ b/runtime/objc-opt.mm @@ -49,16 +49,41 @@ bool header_info::isPreoptimized() const return false; } +bool header_info::hasPreoptimizedSelectors() const +{ + return false; +} + +bool header_info::hasPreoptimizedClasses() const +{ + return false; +} + +bool header_info::hasPreoptimizedProtocols() const +{ + return false; +} + objc_selopt_t *preoptimizedSelectors(void) { return nil; } +bool sharedCacheSupportsProtocolRoots(void) +{ + return false; +} + Protocol *getPreoptimizedProtocol(const char *name) { return nil; } +unsigned int getPreoptimizedClassUnreasonableCount() +{ + return 0; +} + Class getPreoptimizedClass(const char *name) { return nil; @@ -99,6 +124,7 @@ void preopt_init(void) using objc_opt::objc_stringhash_offset_t; using objc_opt::objc_protocolopt_t; +using objc_opt::objc_protocolopt2_t; using objc_opt::objc_clsopt_t; using objc_opt::objc_headeropt_ro_t; using objc_opt::objc_headeropt_rw_t; @@ -149,15 +175,53 @@ bool header_info::isPreoptimized() const return YES; } +bool header_info::hasPreoptimizedSelectors() const +{ + // preoptimization disabled for some reason + if (!preoptimized) return NO; + + return info()->optimizedByDyld() || info()->optimizedByDyldClosure(); +} + +bool header_info::hasPreoptimizedClasses() const +{ + // preoptimization disabled for some reason + if (!preoptimized) return NO; + + return info()->optimizedByDyld() || info()->optimizedByDyldClosure(); +} + +bool header_info::hasPreoptimizedProtocols() const +{ + // preoptimization disabled for some reason + if (!preoptimized) return NO; + + return info()->optimizedByDyld() || info()->optimizedByDyldClosure(); +} + objc_selopt_t *preoptimizedSelectors(void) { return opt ? opt->selopt() : nil; } - -Protocol *getPreoptimizedProtocol(const char *name) +bool sharedCacheSupportsProtocolRoots(void) { + return (opt != nil) && (opt->protocolopt2() != nil); +} + + +Protocol *getSharedCachePreoptimizedProtocol(const char *name) +{ + // Look in the new table if we have it + if (objc_protocolopt2_t *protocols2 = opt ? opt->protocolopt2() : nil) { + // Note, we have to pass the lambda directly here as otherwise we would try + // message copy and autorelease. + return (Protocol *)protocols2->getProtocol(name, [](const void* hi) -> bool { + return ((header_info *)hi)->isLoaded(); + }); + } + objc_protocolopt_t *protocols = opt ? opt->protocolopt() : nil; if (!protocols) return nil; @@ -165,11 +229,66 @@ Protocol *getPreoptimizedProtocol(const char *name) } +Protocol *getPreoptimizedProtocol(const char *name) +{ + // Try table from dyld closure first. It was built to ignore the dupes it + // knows will come from the cache, so anything left in here was there when + // we launched + Protocol *result = nil; + // Note, we have to pass the lambda directly here as otherwise we would try + // message copy and autorelease. + _dyld_for_each_objc_protocol(name, [&result](void* protocolPtr, bool isLoaded, bool* stop) { + // Skip images which aren't loaded. This supports the case where dyld + // might soft link an image from the main binary so its possibly not + // loaded yet. + if (!isLoaded) + return; + + // Found a loaded image with this class name, so stop the search + result = (Protocol *)protocolPtr; + *stop = true; + }); + if (result) return result; + + return getSharedCachePreoptimizedProtocol(name); +} + + +unsigned int getPreoptimizedClassUnreasonableCount() +{ + objc_clsopt_t *classes = opt ? opt->clsopt() : nil; + if (!classes) return 0; + + // This is an overestimate: each set of duplicates + // gets double-counted in `capacity` as well. + return classes->capacity + classes->duplicateCount(); +} + + Class getPreoptimizedClass(const char *name) { objc_clsopt_t *classes = opt ? opt->clsopt() : nil; if (!classes) return nil; + // Try table from dyld closure first. It was built to ignore the dupes it + // knows will come from the cache, so anything left in here was there when + // we launched + Class result = nil; + // Note, we have to pass the lambda directly here as otherwise we would try + // message copy and autorelease. + _dyld_for_each_objc_class(name, [&result](void* classPtr, bool isLoaded, bool* stop) { + // Skip images which aren't loaded. This supports the case where dyld + // might soft link an image from the main binary so its possibly not + // loaded yet. + if (!isLoaded) + return; + + // Found a loaded image with this class name, so stop the search + result = (Class)classPtr; + *stop = true; + }); + if (result) return result; + void *cls; void *hi; uint32_t count = classes->getClassAndHeader(name, cls, hi); @@ -243,7 +362,7 @@ struct objc_headeropt_ro_t { header_info *get(const headerType *mhdr) { - assert(entsize == sizeof(header_info)); + ASSERT(entsize == sizeof(header_info)); int32_t start = 0; int32_t end = count; @@ -304,13 +423,21 @@ header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr) hdr->fname(), hdr, hinfoRO, hinfoRW); } int32_t index = (int32_t)(hdr - hinfoRO->headers); - assert(hinfoRW->entsize == sizeof(header_info_rw)); + ASSERT(hinfoRW->entsize == sizeof(header_info_rw)); return &hinfoRW->headers[index]; } void preopt_init(void) { + // Get the memory region occupied by the shared cache. + size_t length; + const uintptr_t start = (uintptr_t)_dyld_get_shared_cache_range(&length); + + if (start) { + objc::dataSegmentsRanges.add(start, start + length); + } + // `opt` not set at compile time in order to detect too-early usage const char *failure = nil; opt = &_objc_opt_data; @@ -331,7 +458,7 @@ void preopt_init(void) // One of the tables is missing. failure = "(dyld shared cache is absent or out of date)"; } - + if (failure) { // All preoptimized selector references are invalid. preoptimized = NO; diff --git a/runtime/objc-os.h b/runtime/objc-os.h index b34abe6..c28ba05 100644 --- a/runtime/objc-os.h +++ b/runtime/objc-os.h @@ -29,8 +29,10 @@ #ifndef _OBJC_OS_H #define _OBJC_OS_H +#include #include #include "objc-config.h" +#include "objc-private.h" #ifdef __LP64__ # define WORD_SHIFT 3UL @@ -48,7 +50,9 @@ static inline uint32_t word_align(uint32_t x) { static inline size_t word_align(size_t x) { return (x + WORD_MASK) & ~WORD_MASK; } - +static inline size_t align16(size_t x) { + return (x + size_t(15)) & ~size_t(15); +} // Mix-in for classes that must not be copied. class nocopy_t { @@ -56,10 +60,38 @@ class nocopy_t { nocopy_t(const nocopy_t&) = delete; const nocopy_t& operator=(const nocopy_t&) = delete; protected: - nocopy_t() { } - ~nocopy_t() { } + constexpr nocopy_t() = default; + ~nocopy_t() = default; }; +// Version of std::atomic that does not allow implicit conversions +// to/from the wrapped type, and requires an explicit memory order +// be passed to load() and store(). +template +struct explicit_atomic : public std::atomic { + explicit explicit_atomic(T initial) noexcept : std::atomic(std::move(initial)) {} + operator T() const = delete; + + T load(std::memory_order order) const noexcept { + return std::atomic::load(order); + } + void store(T desired, std::memory_order order) noexcept { + std::atomic::store(desired, order); + } + + // Convert a normal pointer to an atomic pointer. This is a + // somewhat dodgy thing to do, but if the atomic type is lock + // free and the same size as the non-atomic type, we know the + // representations are the same, and the compiler generates good + // code. + static explicit_atomic *from_pointer(T *ptr) { + static_assert(sizeof(explicit_atomic *) == sizeof(T *), + "Size of atomic must match size of original"); + explicit_atomic *atomic = (explicit_atomic *)ptr; + ASSERT(atomic->is_lock_free()); + return atomic; + } +}; #if TARGET_OS_MAC @@ -91,7 +123,7 @@ class nocopy_t { # include # include # include -//# include +# include # include # include # include @@ -104,7 +136,7 @@ class nocopy_t { # include # include # include -//# include +# include # include "objc-probes.h" // generated dtrace probe definitions. // Some libc functions call objc_msgSend() @@ -114,7 +146,7 @@ void vsyslog(int, const char *, va_list) UNAVAILABLE_ATTRIBUTE; #define ALWAYS_INLINE inline __attribute__((always_inline)) -#define NEVER_INLINE inline __attribute__((noinline)) +#define NEVER_INLINE __attribute__((noinline)) #define fastpath(x) (__builtin_expect(bool(x), 1)) #define slowpath(x) (__builtin_expect(bool(x), 0)) @@ -132,8 +164,7 @@ subc(uintptr_t lhs, uintptr_t rhs, uintptr_t carryin, uintptr_t *carryout) return __builtin_subcl(lhs, rhs, carryin, carryout); } - -#if __arm64__ +#if __arm64__ && !__arm64e__ static ALWAYS_INLINE uintptr_t @@ -164,72 +195,36 @@ ClearExclusive(uintptr_t *dst __unused) __builtin_arm_clrex(); } - -#elif __arm__ +#else static ALWAYS_INLINE -uintptr_t +uintptr_t LoadExclusive(uintptr_t *src) { - return *src; + return __c11_atomic_load((_Atomic(uintptr_t) *)src, __ATOMIC_RELAXED); } static ALWAYS_INLINE -bool +bool StoreExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value) { - return OSAtomicCompareAndSwapPtr((void *)oldvalue, (void *)value, - (void **)dst); + return __c11_atomic_compare_exchange_weak((_Atomic(uintptr_t) *)dst, &oldvalue, value, __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + static ALWAYS_INLINE -bool +bool StoreReleaseExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value) { - return OSAtomicCompareAndSwapPtrBarrier((void *)oldvalue, (void *)value, - (void **)dst); + return __c11_atomic_compare_exchange_weak((_Atomic(uintptr_t) *)dst, &oldvalue, value, __ATOMIC_RELEASE, __ATOMIC_RELAXED); } static ALWAYS_INLINE -void +void ClearExclusive(uintptr_t *dst __unused) { } - -#elif __x86_64__ || __i386__ - -static ALWAYS_INLINE -uintptr_t -LoadExclusive(uintptr_t *src) -{ - return *src; -} - -static ALWAYS_INLINE -bool -StoreExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value) -{ - - return __sync_bool_compare_and_swap((void **)dst, (void *)oldvalue, (void *)value); -} - -static ALWAYS_INLINE -bool -StoreReleaseExclusive(uintptr_t *dst, uintptr_t oldvalue, uintptr_t value) -{ - return StoreExclusive(dst, oldvalue, value); -} - -static ALWAYS_INLINE -void -ClearExclusive(uintptr_t *dst __unused) -{ -} - - -#else -# error unknown architecture #endif @@ -317,10 +312,10 @@ ClearExclusive(uintptr_t *dst __unused) #include extern void _objc_fatal(const char *fmt, ...) - __attribute__((noreturn, format (printf, 1, 2))); + __attribute__((noreturn, cold, format (printf, 1, 2))); extern void _objc_fatal_with_reason(uint64_t reason, uint64_t flags, const char *fmt, ...) - __attribute__((noreturn, format (printf, 3, 4))); + __attribute__((noreturn, cold, format (printf, 3, 4))); #define INIT_ONCE_PTR(var, create, delete) \ do { \ @@ -359,9 +354,6 @@ extern void _objc_fatal_with_reason(uint64_t reason, uint64_t flags, # if SUPPORT_RETURN_AUTORELEASE # define RETURN_DISPOSITION_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY4) # endif -# if SUPPORT_QOS_HACK -# define QOS_KEY ((tls_key_t)__PTK_FRAMEWORK_OBJC_KEY5) -# endif #else # define SUPPORT_DIRECT_THREAD_KEYS 0 #endif @@ -433,7 +425,7 @@ typedef DWORD objc_thread_t; // thread ID static __inline int thread_equal(objc_thread_t t1, objc_thread_t t2) { return t1 == t2; } -static __inline objc_thread_t thread_self(void) { +static __inline objc_thread_t objc_thread_self(void) { return GetCurrentThreadId(); } @@ -495,15 +487,15 @@ typedef struct { #define RECURSIVE_MUTEX_NOT_LOCKED 1 extern void recursive_mutex_init(recursive_mutex_t *m); static __inline int _recursive_mutex_lock_nodebug(recursive_mutex_t *m) { - assert(m->mutex); + ASSERT(m->mutex); return WaitForSingleObject(m->mutex, INFINITE); } static __inline bool _recursive_mutex_try_lock_nodebug(recursive_mutex_t *m) { - assert(m->mutex); + ASSERT(m->mutex); return (WAIT_OBJECT_0 == WaitForSingleObject(m->mutex, 0)); } static __inline int _recursive_mutex_unlock_nodebug(recursive_mutex_t *m) { - assert(m->mutex); + ASSERT(m->mutex); return ReleaseMutex(m->mutex) ? 0 : RECURSIVE_MUTEX_NOT_LOCKED; } @@ -598,9 +590,6 @@ static inline int monitor_notifyAll(monitor_t *c) { } -// fixme no rwlock yet - - typedef IMAGE_DOS_HEADER headerType; // fixme YES bundle? NO bundle? sometimes? #define headerIsBundle(hi) YES @@ -631,7 +620,7 @@ OBJC_EXTERN IMAGE_DOS_HEADER __ImageBase; // OS compatibility static inline uint64_t nanoseconds() { - return mach_absolute_time(); + return clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW); } // Internal data types @@ -641,10 +630,6 @@ typedef pthread_t objc_thread_t; static __inline int thread_equal(objc_thread_t t1, objc_thread_t t2) { return pthread_equal(t1, t2); } -static __inline objc_thread_t thread_self(void) { - return pthread_self(); -} - typedef pthread_key_t tls_key_t; @@ -654,7 +639,7 @@ static inline tls_key_t tls_create(void (*dtor)(void*)) { return k; } static inline void *tls_get(tls_key_t k) { - return pthread_getspecific(k); + return pthread_getspecific(k); } static inline void tls_set(tls_key_t k, void *value) { pthread_setspecific(k, value); @@ -662,68 +647,20 @@ static inline void tls_set(tls_key_t k, void *value) { #if SUPPORT_DIRECT_THREAD_KEYS -#if DEBUG -static bool is_valid_direct_key(tls_key_t k) { +static inline bool is_valid_direct_key(tls_key_t k) { return ( k == SYNC_DATA_DIRECT_KEY || k == SYNC_COUNT_DIRECT_KEY || k == AUTORELEASE_POOL_KEY + || k == _PTHREAD_TSD_SLOT_PTHREAD_SELF # if SUPPORT_RETURN_AUTORELEASE || k == RETURN_DISPOSITION_KEY -# endif -# if SUPPORT_QOS_HACK - || k == QOS_KEY # endif ); } -#endif -#if __arm__ - -// rdar://9162780 _pthread_get/setspecific_direct are inefficient -// copied from libdispatch - -__attribute__((const)) -static ALWAYS_INLINE void** -tls_base(void) -{ - uintptr_t p; -#if defined(__arm__) && defined(_ARM_ARCH_6) - __asm__("mrc p15, 0, %[p], c13, c0, 3" : [p] "=&r" (p)); - return (void**)(p & ~0x3ul); -#else -#error tls_base not implemented -#endif -} - - -static ALWAYS_INLINE void -tls_set_direct(void **tsdb, tls_key_t k, void *v) -{ - assert(is_valid_direct_key(k)); - - tsdb[k] = v; -} -#define tls_set_direct(k, v) \ - tls_set_direct(tls_base(), (k), (v)) - - -static ALWAYS_INLINE void * -tls_get_direct(void **tsdb, tls_key_t k) -{ - assert(is_valid_direct_key(k)); - - return tsdb[k]; -} -#define tls_get_direct(k) \ - tls_get_direct(tls_base(), (k)) - -// arm -#else -// not arm - -static inline void *tls_get_direct(tls_key_t k) +static inline void *tls_get_direct(tls_key_t k) { - assert(is_valid_direct_key(k)); + ASSERT(is_valid_direct_key(k)); if (_pthread_has_direct_tsd()) { return _pthread_getspecific_direct(k); @@ -733,7 +670,7 @@ static inline void *tls_get_direct(tls_key_t k) } static inline void tls_set_direct(tls_key_t k, void *value) { - assert(is_valid_direct_key(k)); + ASSERT(is_valid_direct_key(k)); if (_pthread_has_direct_tsd()) { _pthread_setspecific_direct(k, value); @@ -742,45 +679,42 @@ static inline void tls_set_direct(tls_key_t k, void *value) } } -// not arm -#endif - -// SUPPORT_DIRECT_THREAD_KEYS -#endif - - -static inline pthread_t pthread_self_direct() +__attribute__((const)) +static inline pthread_t objc_thread_self() { - return (pthread_t) - _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_SELF); + return (pthread_t)tls_get_direct(_PTHREAD_TSD_SLOT_PTHREAD_SELF); } - -static inline mach_port_t mach_thread_self_direct() +#else +__attribute__((const)) +static inline pthread_t objc_thread_self() { - return (mach_port_t)(uintptr_t) - _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MACH_THREAD_SELF); + return pthread_self(); } - -#if SUPPORT_QOS_HACK -static inline pthread_priority_t pthread_self_priority_direct() -{ - pthread_priority_t pri = (pthread_priority_t) - _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS); - return pri & ~_PTHREAD_PRIORITY_FLAGS_MASK; -} -#endif +#endif // SUPPORT_DIRECT_THREAD_KEYS template class mutex_tt; template class monitor_tt; -template class rwlock_tt; template class recursive_mutex_tt; -using spinlock_t = mutex_tt; -using mutex_t = mutex_tt; -using monitor_t = monitor_tt; -using rwlock_t = rwlock_tt; -using recursive_mutex_t = recursive_mutex_tt; +#if DEBUG +# define LOCKDEBUG 1 +#else +# define LOCKDEBUG 0 +#endif + +using spinlock_t = mutex_tt; +using mutex_t = mutex_tt; +using monitor_t = monitor_tt; +using recursive_mutex_t = recursive_mutex_tt; + +// Use fork_unsafe_lock to get a lock that isn't +// acquired and released around fork(). +// All fork-safe locks are checked in debug builds. +struct fork_unsafe_lock_t { + constexpr fork_unsafe_lock_t() = default; +}; +extern const fork_unsafe_lock_t fork_unsafe_lock; #include "objc-lockdebug.h" @@ -788,13 +722,19 @@ template class mutex_tt : nocopy_t { os_unfair_lock mLock; public: - mutex_tt() : mLock(OS_UNFAIR_LOCK_INIT) { } + constexpr mutex_tt() : mLock(OS_UNFAIR_LOCK_INIT) { + lockdebug_remember_mutex(this); + } + + constexpr mutex_tt(const fork_unsafe_lock_t unsafe) : mLock(OS_UNFAIR_LOCK_INIT) { } void lock() { lockdebug_mutex_lock(this); + // + uint32_t opts = OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION | OS_UNFAIR_LOCK_ADAPTIVE_SPIN; os_unfair_lock_lock_with_options_inline - (&mLock, OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION); + (&mLock, (os_unfair_lock_options_t)opts); } void unlock() { @@ -803,6 +743,13 @@ class mutex_tt : nocopy_t { os_unfair_lock_unlock_inline(&mLock); } + void forceReset() { + lockdebug_mutex_unlock(this); + + bzero(&mLock, sizeof(mLock)); + mLock = os_unfair_lock OS_UNFAIR_LOCK_INIT; + } + void assertLocked() { lockdebug_mutex_assert_locked(this); } @@ -815,7 +762,7 @@ class mutex_tt : nocopy_t { // Address-ordered lock discipline for a pair of locks. static void lockTwo(mutex_tt *lock1, mutex_tt *lock2) { - if (lock1 > lock2) { + if (lock1 < lock2) { lock1->lock(); lock2->lock(); } else { @@ -828,46 +775,86 @@ class mutex_tt : nocopy_t { lock1->unlock(); if (lock2 != lock1) lock2->unlock(); } + + // Scoped lock and unlock + class locker : nocopy_t { + mutex_tt& lock; + public: + locker(mutex_tt& newLock) + : lock(newLock) { lock.lock(); } + ~locker() { lock.unlock(); } + }; + + // Either scoped lock and unlock, or NOP. + class conditional_locker : nocopy_t { + mutex_tt& lock; + bool didLock; + public: + conditional_locker(mutex_tt& newLock, bool shouldLock) + : lock(newLock), didLock(shouldLock) + { + if (shouldLock) lock.lock(); + } + ~conditional_locker() { if (didLock) lock.unlock(); } + }; }; +using mutex_locker_t = mutex_tt::locker; +using conditional_mutex_locker_t = mutex_tt::conditional_locker; + template class recursive_mutex_tt : nocopy_t { - pthread_mutex_t mLock; + os_unfair_recursive_lock mLock; public: - recursive_mutex_tt() : mLock(PTHREAD_RECURSIVE_MUTEX_INITIALIZER) { } + constexpr recursive_mutex_tt() : mLock(OS_UNFAIR_RECURSIVE_LOCK_INIT) { + lockdebug_remember_recursive_mutex(this); + } + + constexpr recursive_mutex_tt(const fork_unsafe_lock_t unsafe) + : mLock(OS_UNFAIR_RECURSIVE_LOCK_INIT) + { } void lock() { lockdebug_recursive_mutex_lock(this); - - int err = pthread_mutex_lock(&mLock); - if (err) _objc_fatal("pthread_mutex_lock failed (%d)", err); + os_unfair_recursive_lock_lock(&mLock); } void unlock() { lockdebug_recursive_mutex_unlock(this); - int err = pthread_mutex_unlock(&mLock); - if (err) _objc_fatal("pthread_mutex_unlock failed (%d)", err); + os_unfair_recursive_lock_unlock(&mLock); + } + + void forceReset() + { + lockdebug_recursive_mutex_unlock(this); + + bzero(&mLock, sizeof(mLock)); + mLock = os_unfair_recursive_lock OS_UNFAIR_RECURSIVE_LOCK_INIT; + } + + bool tryLock() + { + if (os_unfair_recursive_lock_trylock(&mLock)) { + lockdebug_recursive_mutex_lock(this); + return true; + } + return false; } bool tryUnlock() { - int err = pthread_mutex_unlock(&mLock); - if (err == 0) { + if (os_unfair_recursive_lock_tryunlock4objc(&mLock)) { lockdebug_recursive_mutex_unlock(this); return true; - } else if (err == EPERM) { - return false; - } else { - _objc_fatal("pthread_mutex_unlock failed (%d)", err); } + return false; } - void assertLocked() { lockdebug_recursive_mutex_assert_locked(this); } @@ -884,8 +871,15 @@ class monitor_tt { pthread_cond_t cond; public: - monitor_tt() - : mutex(PTHREAD_MUTEX_INITIALIZER), cond(PTHREAD_COND_INITIALIZER) { } + constexpr monitor_tt() + : mutex(PTHREAD_MUTEX_INITIALIZER), cond(PTHREAD_COND_INITIALIZER) + { + lockdebug_remember_monitor(this); + } + + monitor_tt(const fork_unsafe_lock_t unsafe) + : mutex(PTHREAD_MUTEX_INITIALIZER), cond(PTHREAD_COND_INITIALIZER) + { } void enter() { @@ -923,6 +917,16 @@ class monitor_tt { if (err) _objc_fatal("pthread_cond_broadcast failed (%d)", err); } + void forceReset() + { + lockdebug_monitor_leave(this); + + bzero(&mutex, sizeof(mutex)); + bzero(&cond, sizeof(cond)); + mutex = pthread_mutex_t PTHREAD_MUTEX_INITIALIZER; + cond = pthread_cond_t PTHREAD_COND_INITIALIZER; + } + void assertLocked() { lockdebug_monitor_assert_locked(this); @@ -946,152 +950,6 @@ static inline semaphore_t create_semaphore(void) } -#if SUPPORT_QOS_HACK -// Override QOS class to avoid priority inversion in rwlocks -// do a qos override before taking rw lock in objc - -#include -extern pthread_priority_t BackgroundPriority; -extern pthread_priority_t MainPriority; - -static inline void qosStartOverride() -{ - uintptr_t overrideRefCount = (uintptr_t)tls_get_direct(QOS_KEY); - if (overrideRefCount > 0) { - // If there is a qos override, increment the refcount and continue - tls_set_direct(QOS_KEY, (void *)(overrideRefCount + 1)); - } - else { - pthread_priority_t currentPriority = pthread_self_priority_direct(); - // Check if override is needed. Only override if we are background qos - if (currentPriority != 0 && currentPriority <= BackgroundPriority) { - int res __unused = _pthread_override_qos_class_start_direct(mach_thread_self_direct(), MainPriority); - assert(res == 0); - // Once we override, we set the reference count in the tsd - // to know when to end the override - tls_set_direct(QOS_KEY, (void *)1); - } - } -} - -static inline void qosEndOverride() -{ - uintptr_t overrideRefCount = (uintptr_t)tls_get_direct(QOS_KEY); - if (overrideRefCount == 0) return; - - if (overrideRefCount == 1) { - // end the override - int res __unused = _pthread_override_qos_class_end_direct(mach_thread_self_direct()); - assert(res == 0); - } - - // decrement refcount - tls_set_direct(QOS_KEY, (void *)(overrideRefCount - 1)); -} - -// SUPPORT_QOS_HACK -#else -// not SUPPORT_QOS_HACK - -static inline void qosStartOverride() { } -static inline void qosEndOverride() { } - -// not SUPPORT_QOS_HACK -#endif - - -template -class rwlock_tt : nocopy_t { - pthread_rwlock_t mLock; - - public: - rwlock_tt() : mLock(PTHREAD_RWLOCK_INITIALIZER) { } - - void read() - { - lockdebug_rwlock_read(this); - - qosStartOverride(); - int err = pthread_rwlock_rdlock(&mLock); - if (err) _objc_fatal("pthread_rwlock_rdlock failed (%d)", err); - } - - void unlockRead() - { - lockdebug_rwlock_unlock_read(this); - - int err = pthread_rwlock_unlock(&mLock); - if (err) _objc_fatal("pthread_rwlock_unlock failed (%d)", err); - qosEndOverride(); - } - - bool tryRead() - { - qosStartOverride(); - int err = pthread_rwlock_tryrdlock(&mLock); - if (err == 0) { - lockdebug_rwlock_try_read_success(this); - return true; - } else if (err == EBUSY) { - qosEndOverride(); - return false; - } else { - _objc_fatal("pthread_rwlock_tryrdlock failed (%d)", err); - } - } - - void write() - { - lockdebug_rwlock_write(this); - - qosStartOverride(); - int err = pthread_rwlock_wrlock(&mLock); - if (err) _objc_fatal("pthread_rwlock_wrlock failed (%d)", err); - } - - void unlockWrite() - { - lockdebug_rwlock_unlock_write(this); - - int err = pthread_rwlock_unlock(&mLock); - if (err) _objc_fatal("pthread_rwlock_unlock failed (%d)", err); - qosEndOverride(); - } - - bool tryWrite() - { - qosStartOverride(); - int err = pthread_rwlock_trywrlock(&mLock); - if (err == 0) { - lockdebug_rwlock_try_write_success(this); - return true; - } else if (err == EBUSY) { - qosEndOverride(); - return false; - } else { - _objc_fatal("pthread_rwlock_trywrlock failed (%d)", err); - } - } - - - void assertReading() { - lockdebug_rwlock_assert_reading(this); - } - - void assertWriting() { - lockdebug_rwlock_assert_writing(this); - } - - void assertLocked() { - lockdebug_rwlock_assert_locked(this); - } - - void assertUnlocked() { - lockdebug_rwlock_assert_unlocked(this); - } -}; - - #ifndef __LP64__ typedef struct mach_header headerType; typedef struct segment_command segmentType; @@ -1162,27 +1020,35 @@ ustrdupMaybeNil(const uint8_t *str) // OS version checking: // // sdkVersion() -// DYLD_OS_VERSION(mac, ios, tv, watch) -// sdkIsOlderThan(mac, ios, tv, watch) -// sdkIsAtLeast(mac, ios, tv, watch) +// DYLD_OS_VERSION(mac, ios, tv, watch, bridge) +// sdkIsOlderThan(mac, ios, tv, watch, bridge) +// sdkIsAtLeast(mac, ios, tv, watch, bridge) // // This version order matches OBJC_AVAILABLE. #if TARGET_OS_OSX -# define DYLD_OS_VERSION(x, i, t, w) DYLD_MACOSX_VERSION_##x +# define DYLD_OS_VERSION(x, i, t, w, b) DYLD_MACOSX_VERSION_##x # define sdkVersion() dyld_get_program_sdk_version() #elif TARGET_OS_IOS -# define DYLD_OS_VERSION(x, i, t, w) DYLD_IOS_VERSION_##i +# define DYLD_OS_VERSION(x, i, t, w, b) DYLD_IOS_VERSION_##i # define sdkVersion() dyld_get_program_sdk_version() #elif TARGET_OS_TV // dyld does not currently have distinct constants for tvOS -# define DYLD_OS_VERSION(x, i, t, w) DYLD_IOS_VERSION_##t +# define DYLD_OS_VERSION(x, i, t, w, b) DYLD_IOS_VERSION_##t # define sdkVersion() dyld_get_program_sdk_version() +#elif TARGET_OS_BRIDGE +# if TARGET_OS_WATCH +# error bridgeOS 1.0 not supported +# endif + // fixme don't need bridgeOS versioning yet +# define DYLD_OS_VERSION(x, i, t, w, b) DYLD_IOS_VERSION_##t +# define sdkVersion() dyld_get_program_sdk_bridge_os_version() + #elif TARGET_OS_WATCH -# define DYLD_OS_VERSION(x, i, t, w) DYLD_WATCHOS_VERSION_##w +# define DYLD_OS_VERSION(x, i, t, w, b) DYLD_WATCHOS_VERSION_##w // watchOS has its own API for compatibility reasons # define sdkVersion() dyld_get_program_sdk_watch_os_version() @@ -1191,16 +1057,17 @@ ustrdupMaybeNil(const uint8_t *str) #endif -#define sdkIsOlderThan(x, i, t, w) \ - (sdkVersion() < DYLD_OS_VERSION(x, i, t, w)) -#define sdkIsAtLeast(x, i, t, w) \ - (sdkVersion() >= DYLD_OS_VERSION(x, i, t, w)) +#define sdkIsOlderThan(x, i, t, w, b) \ + (sdkVersion() < DYLD_OS_VERSION(x, i, t, w, b)) +#define sdkIsAtLeast(x, i, t, w, b) \ + (sdkVersion() >= DYLD_OS_VERSION(x, i, t, w, b)) // Allow bare 0 to be used in DYLD_OS_VERSION() and sdkIsOlderThan() #define DYLD_MACOSX_VERSION_0 0 #define DYLD_IOS_VERSION_0 0 #define DYLD_TVOS_VERSION_0 0 #define DYLD_WATCHOS_VERSION_0 0 +#define DYLD_BRIDGEOS_VERSION_0 0 // Pretty-print a DYLD_*_VERSION_* constant. #define SDK_FORMAT "%hu.%hhu.%hhu" @@ -1209,4 +1076,13 @@ ustrdupMaybeNil(const uint8_t *str) (unsigned char)(((uint32_t)(v))>>8), \ (unsigned char)(((uint32_t)(v))>>0) +// fork() safety requires careful tracking of all locks. +// Our custom lock types check this in debug builds. +// Disallow direct use of all other lock types. +typedef __darwin_pthread_mutex_t pthread_mutex_t UNAVAILABLE_ATTRIBUTE; +typedef __darwin_pthread_rwlock_t pthread_rwlock_t UNAVAILABLE_ATTRIBUTE; +typedef int32_t OSSpinLock UNAVAILABLE_ATTRIBUTE; +typedef struct os_unfair_lock_s os_unfair_lock UNAVAILABLE_ATTRIBUTE; + + #endif diff --git a/runtime/objc-os.mm b/runtime/objc-os.mm index 187651d..7d600ef 100644 --- a/runtime/objc-os.mm +++ b/runtime/objc-os.mm @@ -28,12 +28,15 @@ #include "objc-private.h" #include "objc-loadmethod.h" +#include "objc-cache.h" #if TARGET_OS_WIN32 #include "objc-runtime-old.h" #include "objcrt.h" +const fork_unsafe_lock_t fork_unsafe_lock; + int monitor_init(monitor_t *c) { // fixme error checking @@ -100,7 +103,7 @@ WINBOOL APIENTRY DllMain( HMODULE hModule, case DLL_PROCESS_ATTACH: environ_init(); tls_init(); - lock_init(); + runtime_init(); sel_init(3500); // old selector heuristic exception_init(); break; @@ -251,18 +254,13 @@ static header_info * addHeader(const headerType *mhdr, const char *path, int &to // Verify image_info size_t info_size = 0; const objc_image_info *image_info = _getObjcImageInfo(mhdr,&info_size); - assert(image_info == hi->info()); + ASSERT(image_info == hi->info()); #endif } else { // Didn't find an hinfo in the dyld shared cache. - // Weed out duplicates - for (hi = FirstHeader; hi; hi = hi->getNext()) { - if (mhdr == hi->mhdr()) return NULL; - } - // Locate the __OBJC segment size_t info_size = 0; unsigned long seg_size; @@ -340,7 +338,7 @@ linksToLibrary(const header_info *hi, const char *name) **********************************************************************/ static bool shouldRejectGCApp(const header_info *hi) { - assert(hi->mhdr()->filetype == MH_EXECUTE); + ASSERT(hi->mhdr()->filetype == MH_EXECUTE); if (!hi->info()->supportsGC()) { // App does not use GC. Don't reject it. @@ -384,7 +382,7 @@ static bool shouldRejectGCApp(const header_info *hi) **********************************************************************/ static bool shouldRejectGCImage(const headerType *mhdr) { - assert(mhdr->filetype != MH_EXECUTE); + ASSERT(mhdr->filetype != MH_EXECUTE); objc_image_info *image_info; size_t size; @@ -417,6 +415,25 @@ static bool shouldRejectGCImage(const headerType *mhdr) #endif +// Swift currently adds 4 callbacks. +static GlobalSmallVector loadImageFuncs; + +void objc_addLoadImageFunc(objc_func_loadImage _Nonnull func) { + // Not supported on the old runtime. Not that the old runtime is supported anyway. +#if __OBJC2__ + mutex_locker_t lock(runtimeLock); + + // Call it with all the existing images first. + for (auto header = FirstHeader; header; header = header->getNext()) { + func((struct mach_header *)header->mhdr()); + } + + // Add it to the vector for future loads. + loadImageFuncs.append(func); +#endif +} + + /*********************************************************************** * map_images_nolock * Process the given images which are being mapped in by dyld. @@ -489,7 +506,7 @@ map_images_nolock(unsigned mhCount, const char * const mhPaths[], if (shouldRejectGCApp(hi)) { _objc_fatal_with_reason (OBJC_EXIT_REASON_GC_NOT_SUPPORTED, - /*OS_REASON_FLAG_CONSISTENT_FAILURE*/ 0, + OS_REASON_FLAG_CONSISTENT_FAILURE, "Objective-C garbage collection " "is no longer supported."); } @@ -530,12 +547,45 @@ map_images_nolock(unsigned mhCount, const char * const mhPaths[], if (mh->filetype != MH_EXECUTE && shouldRejectGCImage(mh)) { _objc_fatal_with_reason (OBJC_EXIT_REASON_GC_NOT_SUPPORTED, - /*OS_REASON_FLAG_CONSISTENT_FAILURE*/ 0, + OS_REASON_FLAG_CONSISTENT_FAILURE, "%s requires Objective-C garbage collection " "which is no longer supported.", hi->fname()); } } #endif + +#if TARGET_OS_OSX + // Disable +initialize fork safety if the app is too old (< 10.13). + // Disable +initialize fork safety if the app has a + // __DATA,__objc_fork_ok section. + + if (dyld_get_program_sdk_version() < DYLD_MACOSX_VERSION_10_13) { + DisableInitializeForkSafety = true; + if (PrintInitializing) { + _objc_inform("INITIALIZE: disabling +initialize fork " + "safety enforcement because the app is " + "too old (SDK version " SDK_FORMAT ")", + FORMAT_SDK(dyld_get_program_sdk_version())); + } + } + + for (uint32_t i = 0; i < hCount; i++) { + auto hi = hList[i]; + auto mh = hi->mhdr(); + if (mh->filetype != MH_EXECUTE) continue; + unsigned long size; + if (getsectiondata(hi->mhdr(), "__DATA", "__objc_fork_ok", &size)) { + DisableInitializeForkSafety = true; + if (PrintInitializing) { + _objc_inform("INITIALIZE: disabling +initialize fork " + "safety enforcement because the app has " + "a __DATA,__objc_fork_ok section"); + } + } + break; // assume only one MH_EXECUTE image + } +#endif + } if (hCount > 0) { @@ -543,6 +593,13 @@ map_images_nolock(unsigned mhCount, const char * const mhPaths[], } firstTime = NO; + + // Call image load funcs after everything is set up. + for (auto func : loadImageFuncs) { + for (uint32_t i = 0; i < mhCount; i++) { + func(mhdrs[i]); + } + } } @@ -596,13 +653,257 @@ unmap_image_nolock(const struct mach_header *mh) static void static_init() { size_t count; - Initializer *inits = getLibobjcInitializers(&_mh_dylib_header, &count); + auto inits = getLibobjcInitializers(&_mh_dylib_header, &count); for (size_t i = 0; i < count; i++) { inits[i](); } } +/*********************************************************************** +* _objc_atfork_prepare +* _objc_atfork_parent +* _objc_atfork_child +* Allow ObjC to be used between fork() and exec(). +* libc requires this because it has fork-safe functions that use os_objects. +* +* _objc_atfork_prepare() acquires all locks. +* _objc_atfork_parent() releases the locks again. +* _objc_atfork_child() forcibly resets the locks. +**********************************************************************/ + +// Declare lock ordering. +#if LOCKDEBUG +__attribute__((constructor)) +static void defineLockOrder() +{ + // Every lock precedes crashlog_lock + // on the assumption that fatal errors could be anywhere. + lockdebug_lock_precedes_lock(&loadMethodLock, &crashlog_lock); + lockdebug_lock_precedes_lock(&classInitLock, &crashlog_lock); +#if __OBJC2__ + lockdebug_lock_precedes_lock(&runtimeLock, &crashlog_lock); + lockdebug_lock_precedes_lock(&DemangleCacheLock, &crashlog_lock); +#else + lockdebug_lock_precedes_lock(&classLock, &crashlog_lock); + lockdebug_lock_precedes_lock(&methodListLock, &crashlog_lock); + lockdebug_lock_precedes_lock(&NXUniqueStringLock, &crashlog_lock); + lockdebug_lock_precedes_lock(&impLock, &crashlog_lock); +#endif + lockdebug_lock_precedes_lock(&selLock, &crashlog_lock); +#if CONFIG_USE_CACHE_LOCK + lockdebug_lock_precedes_lock(&cacheUpdateLock, &crashlog_lock); +#endif + lockdebug_lock_precedes_lock(&objcMsgLogLock, &crashlog_lock); + lockdebug_lock_precedes_lock(&AltHandlerDebugLock, &crashlog_lock); + lockdebug_lock_precedes_lock(&AssociationsManagerLock, &crashlog_lock); + SideTableLocksPrecedeLock(&crashlog_lock); + PropertyLocks.precedeLock(&crashlog_lock); + StructLocks.precedeLock(&crashlog_lock); + CppObjectLocks.precedeLock(&crashlog_lock); + + // loadMethodLock precedes everything + // because it is held while +load methods run + lockdebug_lock_precedes_lock(&loadMethodLock, &classInitLock); +#if __OBJC2__ + lockdebug_lock_precedes_lock(&loadMethodLock, &runtimeLock); + lockdebug_lock_precedes_lock(&loadMethodLock, &DemangleCacheLock); +#else + lockdebug_lock_precedes_lock(&loadMethodLock, &methodListLock); + lockdebug_lock_precedes_lock(&loadMethodLock, &classLock); + lockdebug_lock_precedes_lock(&loadMethodLock, &NXUniqueStringLock); + lockdebug_lock_precedes_lock(&loadMethodLock, &impLock); +#endif + lockdebug_lock_precedes_lock(&loadMethodLock, &selLock); +#if CONFIG_USE_CACHE_LOCK + lockdebug_lock_precedes_lock(&loadMethodLock, &cacheUpdateLock); +#endif + lockdebug_lock_precedes_lock(&loadMethodLock, &objcMsgLogLock); + lockdebug_lock_precedes_lock(&loadMethodLock, &AltHandlerDebugLock); + lockdebug_lock_precedes_lock(&loadMethodLock, &AssociationsManagerLock); + SideTableLocksSucceedLock(&loadMethodLock); + PropertyLocks.succeedLock(&loadMethodLock); + StructLocks.succeedLock(&loadMethodLock); + CppObjectLocks.succeedLock(&loadMethodLock); + + // PropertyLocks and CppObjectLocks and AssociationManagerLock + // precede everything because they are held while objc_retain() + // or C++ copy are called. + // (StructLocks do not precede everything because it calls memmove only.) + auto PropertyAndCppObjectAndAssocLocksPrecedeLock = [&](const void *lock) { + PropertyLocks.precedeLock(lock); + CppObjectLocks.precedeLock(lock); + lockdebug_lock_precedes_lock(&AssociationsManagerLock, lock); + }; +#if __OBJC2__ + PropertyAndCppObjectAndAssocLocksPrecedeLock(&runtimeLock); + PropertyAndCppObjectAndAssocLocksPrecedeLock(&DemangleCacheLock); +#else + PropertyAndCppObjectAndAssocLocksPrecedeLock(&methodListLock); + PropertyAndCppObjectAndAssocLocksPrecedeLock(&classLock); + PropertyAndCppObjectAndAssocLocksPrecedeLock(&NXUniqueStringLock); + PropertyAndCppObjectAndAssocLocksPrecedeLock(&impLock); +#endif + PropertyAndCppObjectAndAssocLocksPrecedeLock(&classInitLock); + PropertyAndCppObjectAndAssocLocksPrecedeLock(&selLock); +#if CONFIG_USE_CACHE_LOCK + PropertyAndCppObjectAndAssocLocksPrecedeLock(&cacheUpdateLock); +#endif + PropertyAndCppObjectAndAssocLocksPrecedeLock(&objcMsgLogLock); + PropertyAndCppObjectAndAssocLocksPrecedeLock(&AltHandlerDebugLock); + + SideTableLocksSucceedLocks(PropertyLocks); + SideTableLocksSucceedLocks(CppObjectLocks); + SideTableLocksSucceedLock(&AssociationsManagerLock); + + PropertyLocks.precedeLock(&AssociationsManagerLock); + CppObjectLocks.precedeLock(&AssociationsManagerLock); + +#if __OBJC2__ + lockdebug_lock_precedes_lock(&classInitLock, &runtimeLock); +#endif + +#if __OBJC2__ + // Runtime operations may occur inside SideTable locks + // (such as storeWeak calling getMethodImplementation) + SideTableLocksPrecedeLock(&runtimeLock); + SideTableLocksPrecedeLock(&classInitLock); + // Some operations may occur inside runtimeLock. + lockdebug_lock_precedes_lock(&runtimeLock, &selLock); +#if CONFIG_USE_CACHE_LOCK + lockdebug_lock_precedes_lock(&runtimeLock, &cacheUpdateLock); +#endif + lockdebug_lock_precedes_lock(&runtimeLock, &DemangleCacheLock); +#else + // Runtime operations may occur inside SideTable locks + // (such as storeWeak calling getMethodImplementation) + SideTableLocksPrecedeLock(&methodListLock); + SideTableLocksPrecedeLock(&classInitLock); + // Method lookup and fixup. + lockdebug_lock_precedes_lock(&methodListLock, &classLock); + lockdebug_lock_precedes_lock(&methodListLock, &selLock); +#if CONFIG_USE_CACHE_LOCK + lockdebug_lock_precedes_lock(&methodListLock, &cacheUpdateLock); +#endif + lockdebug_lock_precedes_lock(&methodListLock, &impLock); + lockdebug_lock_precedes_lock(&classLock, &selLock); + lockdebug_lock_precedes_lock(&classLock, &cacheUpdateLock); +#endif + + // Striped locks use address order internally. + SideTableDefineLockOrder(); + PropertyLocks.defineLockOrder(); + StructLocks.defineLockOrder(); + CppObjectLocks.defineLockOrder(); +} +// LOCKDEBUG +#endif + +static bool ForkIsMultithreaded; +void _objc_atfork_prepare() +{ + // Save threaded-ness for the child's use. + ForkIsMultithreaded = pthread_is_threaded_np(); + + lockdebug_assert_no_locks_locked(); + lockdebug_setInForkPrepare(true); + + loadMethodLock.lock(); + PropertyLocks.lockAll(); + CppObjectLocks.lockAll(); + AssociationsManagerLock.lock(); + SideTableLockAll(); + classInitLock.enter(); +#if __OBJC2__ + runtimeLock.lock(); + DemangleCacheLock.lock(); +#else + methodListLock.lock(); + classLock.lock(); + NXUniqueStringLock.lock(); + impLock.lock(); +#endif + selLock.lock(); +#if CONFIG_USE_CACHE_LOCK + cacheUpdateLock.lock(); +#endif + objcMsgLogLock.lock(); + AltHandlerDebugLock.lock(); + StructLocks.lockAll(); + crashlog_lock.lock(); + + lockdebug_assert_all_locks_locked(); + lockdebug_setInForkPrepare(false); +} + +void _objc_atfork_parent() +{ + lockdebug_assert_all_locks_locked(); + + CppObjectLocks.unlockAll(); + StructLocks.unlockAll(); + PropertyLocks.unlockAll(); + AssociationsManagerLock.unlock(); + AltHandlerDebugLock.unlock(); + objcMsgLogLock.unlock(); + crashlog_lock.unlock(); + loadMethodLock.unlock(); +#if CONFIG_USE_CACHE_LOCK + cacheUpdateLock.unlock(); +#endif + selLock.unlock(); + SideTableUnlockAll(); +#if __OBJC2__ + DemangleCacheLock.unlock(); + runtimeLock.unlock(); +#else + impLock.unlock(); + NXUniqueStringLock.unlock(); + methodListLock.unlock(); + classLock.unlock(); +#endif + classInitLock.leave(); + + lockdebug_assert_no_locks_locked(); +} + +void _objc_atfork_child() +{ + // Turn on +initialize fork safety enforcement if applicable. + if (ForkIsMultithreaded && !DisableInitializeForkSafety) { + MultithreadedForkChild = true; + } + + lockdebug_assert_all_locks_locked(); + + CppObjectLocks.forceResetAll(); + StructLocks.forceResetAll(); + PropertyLocks.forceResetAll(); + AssociationsManagerLock.forceReset(); + AltHandlerDebugLock.forceReset(); + objcMsgLogLock.forceReset(); + crashlog_lock.forceReset(); + loadMethodLock.forceReset(); +#if CONFIG_USE_CACHE_LOCK + cacheUpdateLock.forceReset(); +#endif + selLock.forceReset(); + SideTableForceResetAll(); +#if __OBJC2__ + DemangleCacheLock.forceReset(); + runtimeLock.forceReset(); +#else + impLock.forceReset(); + NXUniqueStringLock.forceReset(); + methodListLock.forceReset(); + classLock.forceReset(); +#endif + classInitLock.forceReset(); + + lockdebug_assert_no_locks_locked(); +} + + /*********************************************************************** * _objc_init * Bootstrap initialization. Registers our image notifier with dyld. @@ -619,10 +920,16 @@ void _objc_init(void) environ_init(); tls_init(); static_init(); - lock_init(); + runtime_init(); exception_init(); + cache_init(); + _imp_implementationWithBlock_init(); - _dyld_objc_notify_register(&map_2_images, load_images, unmap_image); + _dyld_objc_notify_register(&map_images, load_images, unmap_image); + +#if __OBJC2__ + didCallDyldNotifyRegister = true; +#endif } diff --git a/runtime/objc-private.h b/runtime/objc-private.h index 07bf78c..4d7aab2 100644 --- a/runtime/objc-private.h +++ b/runtime/objc-private.h @@ -21,8 +21,8 @@ * @APPLE_LICENSE_HEADER_END@ */ /* - * objc-private.h - * Copyright 1988-1996, NeXT Software, Inc. + * objc-private.h + * Copyright 1988-1996, NeXT Software, Inc. */ #ifndef _OBJC_PRIVATE_H_ @@ -39,12 +39,20 @@ #endif #define OBJC_TYPES_DEFINED 1 +#undef OBJC_OLD_DISPATCH_PROTOTYPES #define OBJC_OLD_DISPATCH_PROTOTYPES 0 #include // for nullptr_t #include #include +// An assert that's disabled for release builds but still ensures the expression compiles. +#ifdef NDEBUG +#define ASSERT(x) (void)sizeof(!(x)) +#else +#define ASSERT(x) assert(x) +#endif + struct objc_class; struct objc_object; @@ -55,113 +63,19 @@ namespace { struct SideTable; }; +#include "isa.h" -#if (!SUPPORT_NONPOINTER_ISA && !SUPPORT_PACKED_ISA && !SUPPORT_INDEXED_ISA) ||\ - ( SUPPORT_NONPOINTER_ISA && SUPPORT_PACKED_ISA && !SUPPORT_INDEXED_ISA) ||\ - ( SUPPORT_NONPOINTER_ISA && !SUPPORT_PACKED_ISA && SUPPORT_INDEXED_ISA) - // good config -#else -# error bad config -#endif - - -union isa_t -{ +union isa_t { isa_t() { } isa_t(uintptr_t value) : bits(value) { } Class cls; uintptr_t bits; - -#if SUPPORT_PACKED_ISA - - // extra_rc must be the MSB-most field (so it matches carry/overflow flags) - // nonpointer must be the LSB (fixme or get rid of it) - // shiftcls must occupy the same bits that a real class pointer would - // bits + RC_ONE is equivalent to extra_rc + 1 - // RC_HALF is the high bit of extra_rc (i.e. half of its range) - - // future expansion: - // uintptr_t fast_rr : 1; // no r/r overrides - // uintptr_t lock : 2; // lock for atomic property, @synch - // uintptr_t extraBytes : 1; // allocated with extra bytes - -# if __arm64__ -# define ISA_MASK 0x0000000ffffffff8ULL -# define ISA_MAGIC_MASK 0x000003f000000001ULL -# define ISA_MAGIC_VALUE 0x000001a000000001ULL +#if defined(ISA_BITFIELD) struct { - uintptr_t nonpointer : 1; - uintptr_t has_assoc : 1; - uintptr_t has_cxx_dtor : 1; - uintptr_t shiftcls : 33; // MACH_VM_MAX_ADDRESS 0x1000000000 - uintptr_t magic : 6; - uintptr_t weakly_referenced : 1; - uintptr_t deallocating : 1; - uintptr_t has_sidetable_rc : 1; - uintptr_t extra_rc : 19; -# define RC_ONE (1ULL<<45) -# define RC_HALF (1ULL<<18) + ISA_BITFIELD; // defined in isa.h }; - -# elif __x86_64__ -# define ISA_MASK 0x00007ffffffffff8ULL -# define ISA_MAGIC_MASK 0x001f800000000001ULL -# define ISA_MAGIC_VALUE 0x001d800000000001ULL - struct { - uintptr_t nonpointer : 1; - uintptr_t has_assoc : 1; - uintptr_t has_cxx_dtor : 1; - uintptr_t shiftcls : 44; // MACH_VM_MAX_ADDRESS 0x7fffffe00000 - uintptr_t magic : 6; - uintptr_t weakly_referenced : 1; - uintptr_t deallocating : 1; - uintptr_t has_sidetable_rc : 1; - uintptr_t extra_rc : 8; -# define RC_ONE (1ULL<<56) -# define RC_HALF (1ULL<<7) - }; - -# else -# error unknown architecture for packed isa -# endif - -// SUPPORT_PACKED_ISA #endif - - -#if SUPPORT_INDEXED_ISA - -# if __ARM_ARCH_7K__ >= 2 - -# define ISA_INDEX_IS_NPI 1 -# define ISA_INDEX_MASK 0x0001FFFC -# define ISA_INDEX_SHIFT 2 -# define ISA_INDEX_BITS 15 -# define ISA_INDEX_COUNT (1 << ISA_INDEX_BITS) -# define ISA_INDEX_MAGIC_MASK 0x001E0001 -# define ISA_INDEX_MAGIC_VALUE 0x001C0001 - struct { - uintptr_t nonpointer : 1; - uintptr_t has_assoc : 1; - uintptr_t indexcls : 15; - uintptr_t magic : 4; - uintptr_t has_cxx_dtor : 1; - uintptr_t weakly_referenced : 1; - uintptr_t deallocating : 1; - uintptr_t has_sidetable_rc : 1; - uintptr_t extra_rc : 7; -# define RC_ONE (1ULL<<25) -# define RC_HALF (1ULL<<6) - }; - -# else -# error unknown architecture for indexed isa -# endif - -// SUPPORT_INDEXED_ISA -#endif - }; @@ -174,8 +88,13 @@ public: // ISA() assumes this is NOT a tagged pointer object Class ISA(); + // rawISA() assumes this is NOT a tagged pointer object or a non pointer ISA + Class rawISA(); + // getIsa() allows this to be a tagged pointer object Class getIsa(); + + uintptr_t isaBits() const; // initIsa() should be used to init the isa of new objects only. // If this object already has an isa, use changeIsa() for correctness. @@ -232,14 +151,14 @@ private: // Slow paths for inline control id rootAutorelease2(); - bool overrelease_error(); + uintptr_t overrelease_error(); #if SUPPORT_NONPOINTER_ISA // Unified retain count manipulation for nonpointer isa id rootRetain(bool tryRetain, bool handleOverflow); bool rootRelease(bool performDealloc, bool handleUnderflow); id rootRetain_overflow(bool tryRetain); - bool rootRelease_underflow(bool performDealloc); + uintptr_t rootRelease_underflow(bool performDealloc); void clearDeallocating_slow(); @@ -309,6 +228,8 @@ typedef struct old_property *objc_property_t; // Private headers +#include "objc-ptrauth.h" + #if __OBJC2__ #include "objc-runtime-new.h" #else @@ -333,6 +254,55 @@ struct objc_selopt_t; __BEGIN_DECLS +namespace objc { + +struct SafeRanges { +private: + struct Range { + uintptr_t start; + uintptr_t end; + + inline bool contains(uintptr_t ptr) const { + uintptr_t m_start, m_end; +#if __arm64__ + // Force the compiler to use ldp + // we really don't want 2 loads and 2 jumps. + __asm__( +# if __LP64__ + "ldp %x[one], %x[two], [%x[src]]" +# else + "ldp %w[one], %w[two], [%x[src]]" +# endif + : [one] "=r" (m_start), [two] "=r" (m_end) + : [src] "r" (this) + ); +#else + m_start = start; + m_end = end; +#endif + return m_start <= ptr && ptr < m_end; + } + }; + + struct Range *ranges; + uint32_t count; + uint32_t size : 31; + uint32_t sorted : 1; + +public: + inline bool contains(uint16_t witness, uintptr_t ptr) const { + return witness < count && ranges[witness].contains(ptr); + } + + bool find(uintptr_t ptr, uint32_t &pos); + void add(uintptr_t start, uintptr_t end); + void remove(uintptr_t start, uintptr_t end); +}; + +extern struct SafeRanges dataSegmentsRanges; + +} // objc + struct header_info; // Split out the rw data from header info. For now put it in a huge array @@ -448,6 +418,12 @@ public: bool isPreoptimized() const; + bool hasPreoptimizedSelectors() const; + + bool hasPreoptimizedClasses() const; + + bool hasPreoptimizedProtocols() const; + #if !__OBJC2__ struct old_protocol **proto_refs; struct objc_module *mod_ptr; @@ -475,7 +451,6 @@ private: extern header_info *FirstHeader; extern header_info *LastHeader; -extern int HeaderCount; extern void appendHeader(header_info *hi); extern void removeHeader(header_info *hi); @@ -503,32 +478,17 @@ static inline bool sectnameStartsWith(const char *sectname, const char *prefix){ } +#if __OBJC2__ +extern bool didCallDyldNotifyRegister; +#endif + + /* selectors */ extern void sel_init(size_t selrefCount); extern SEL sel_registerNameNoLock(const char *str, bool copy); -extern void sel_lock(void); -extern void sel_unlock(void); -extern SEL SEL_load; -extern SEL SEL_initialize; -extern SEL SEL_resolveClassMethod; -extern SEL SEL_resolveInstanceMethod; extern SEL SEL_cxx_construct; extern SEL SEL_cxx_destruct; -extern SEL SEL_retain; -extern SEL SEL_release; -extern SEL SEL_autorelease; -extern SEL SEL_retainCount; -extern SEL SEL_alloc; -extern SEL SEL_allocWithZone; -extern SEL SEL_dealloc; -extern SEL SEL_copy; -extern SEL SEL_new; -extern SEL SEL_forwardInvocation; -extern SEL SEL_tryRetain; -extern SEL SEL_isDeallocating; -extern SEL SEL_retainWeakReference; -extern SEL SEL_allowsWeakReference; /* preoptimization */ extern void preopt_init(void); @@ -539,19 +499,34 @@ extern header_info *preoptimizedHinfoForHeader(const headerType *mhdr); extern objc_selopt_t *preoptimizedSelectors(void); +extern bool sharedCacheSupportsProtocolRoots(void); extern Protocol *getPreoptimizedProtocol(const char *name); +extern Protocol *getSharedCachePreoptimizedProtocol(const char *name); +extern unsigned getPreoptimizedClassUnreasonableCount(); extern Class getPreoptimizedClass(const char *name); extern Class* copyPreoptimizedClasses(const char *name, int *outCount); extern Class _calloc_class(size_t size); /* method lookup */ -extern IMP lookUpImpOrNil(Class, SEL, id obj, bool initialize, bool cache, bool resolver); -extern IMP lookUpImpOrForward(Class, SEL, id obj, bool initialize, bool cache, bool resolver); +enum { + LOOKUP_INITIALIZE = 1, + LOOKUP_RESOLVER = 2, + LOOKUP_CACHE = 4, + LOOKUP_NIL = 8, +}; +extern IMP lookUpImpOrForward(id obj, SEL, Class cls, int behavior); + +static inline IMP +lookUpImpOrNil(id obj, SEL sel, Class cls, int behavior = 0) +{ + return lookUpImpOrForward(obj, sel, cls, behavior | LOOKUP_CACHE | LOOKUP_NIL); +} extern IMP lookupMethodInClassAndLoadCache(Class cls, SEL sel); -extern bool class_respondsToSelector_inst(Class cls, SEL sel, id inst); +extern BOOL class_respondsToSelector_inst(id inst, SEL sel, Class cls); +extern Class class_initialize(Class cls, id inst); extern bool objcMsgLogEnabled; extern bool logMessageSend(bool isClassMethod, @@ -560,7 +535,6 @@ extern bool logMessageSend(bool isClassMethod, SEL selector); /* message dispatcher */ -extern IMP _class_lookupMethodAndLoadCache3(id, SEL, Class); #if !OBJC_OLD_DISPATCH_PROTOTYPES extern void _objc_msgForward_impcache(void); @@ -569,11 +543,13 @@ extern id _objc_msgForward_impcache(id, SEL, ...); #endif /* errors */ -extern void __objc_error(id, const char *, ...) __attribute__((format (printf, 2, 3), noreturn)); -extern void _objc_inform(const char *fmt, ...) __attribute__((format (printf, 1, 2))); -extern void _objc_inform_on_crash(const char *fmt, ...) __attribute__((format (printf, 1, 2))); -extern void _objc_inform_now_and_on_crash(const char *fmt, ...) __attribute__((format (printf, 1, 2))); -extern void _objc_inform_deprecated(const char *oldname, const char *newname) __attribute__((noinline)); +extern id(*badAllocHandler)(Class); +extern id _objc_callBadAllocHandler(Class cls) __attribute__((cold, noinline)); +extern void __objc_error(id, const char *, ...) __attribute__((cold, format (printf, 2, 3), noreturn)); +extern void _objc_inform(const char *fmt, ...) __attribute__((cold, format(printf, 1, 2))); +extern void _objc_inform_on_crash(const char *fmt, ...) __attribute__((cold, format (printf, 1, 2))); +extern void _objc_inform_now_and_on_crash(const char *fmt, ...) __attribute__((cold, format (printf, 1, 2))); +extern void _objc_inform_deprecated(const char *oldname, const char *newname) __attribute__((cold, noinline)); extern void inform_duplicate(const char *name, Class oldCls, Class cls); /* magic */ @@ -593,16 +569,6 @@ extern objc_property_attribute_t *copyPropertyAttributeList(const char *attrs, u extern char *copyPropertyAttributeValue(const char *attrs, const char *name); /* locking */ -extern void lock_init(void); -extern rwlock_t selLock; -extern mutex_t cacheUpdateLock; -extern recursive_mutex_t loadMethodLock; -#if __OBJC2__ -extern rwlock_t runtimeLock; -#else -extern mutex_t classLock; -extern mutex_t methodListLock; -#endif class monitor_locker_t : nocopy_t { monitor_t& lock; @@ -611,14 +577,6 @@ class monitor_locker_t : nocopy_t { ~monitor_locker_t() { lock.leave(); } }; -class mutex_locker_t : nocopy_t { - mutex_t& lock; - public: - mutex_locker_t(mutex_t& newLock) - : lock(newLock) { lock.lock(); } - ~mutex_locker_t() { lock.unlock(); } -}; - class recursive_mutex_locker_t : nocopy_t { recursive_mutex_t& lock; public: @@ -627,20 +585,6 @@ class recursive_mutex_locker_t : nocopy_t { ~recursive_mutex_locker_t() { lock.unlock(); } }; -class rwlock_reader_t : nocopy_t { - rwlock_t& lock; - public: - rwlock_reader_t(rwlock_t& newLock) : lock(newLock) { lock.read(); } - ~rwlock_reader_t() { lock.unlockRead(); } -}; - -class rwlock_writer_t : nocopy_t { - rwlock_t& lock; - public: - rwlock_writer_t(rwlock_t& newLock) : lock(newLock) { lock.write(); } - ~rwlock_writer_t() { lock.unlockWrite(); } -}; - /* Exceptions */ struct alt_handler_list; @@ -662,6 +606,7 @@ extern void gdb_objc_class_changed(Class cls, unsigned long changes, const char #undef OPTION extern void environ_init(void); +extern void runtime_init(void); extern void logReplacedMethod(const char *className, SEL s, bool isMeta, const char *catName, IMP oldImp, IMP newImp); @@ -672,6 +617,9 @@ typedef struct { struct SyncCache *syncCache; // for @synchronize struct alt_handler_list *handlerList; // for exception alt handlers char *printableNames[4]; // temporary demangled names for logging + const char **classNameLookups; // for objc_getClass() hooks + unsigned classNameLookupsAllocated; + unsigned classNameLookupsUsed; // If you add new fields here, don't forget to update // _objc_pthread_destroyspecific() @@ -698,6 +646,7 @@ extern void arr_init(void); extern id objc_autoreleaseReturnValue(id obj); // block trampolines +extern void _imp_implementationWithBlock_init(void); extern IMP _imp_implementationWithBlockNoCopy(id block); // layout.h @@ -723,9 +672,11 @@ extern void layout_bitmap_print(layout_bitmap bits); // fixme runtime +extern bool MultithreadedForkChild; +extern id objc_noop_imp(id self, SEL _cmd); extern Class look_up_class(const char *aClassName, bool includeUnconnected, bool includeClassHandler); -extern "C" void map_2_images(unsigned count, const char * const paths[], - const struct mach_header * const mhdrs[]); +extern "C" void map_images(unsigned count, const char * const paths[], + const struct mach_header * const mhdrs[]); extern void map_images_nolock(unsigned count, const char * const paths[], const struct mach_header * const mhdrs[]); extern void load_images(const char *path, const struct mach_header *mh); @@ -733,28 +684,27 @@ extern void unmap_image(const char *path, const struct mach_header *mh); extern void unmap_image_nolock(const struct mach_header *mh); extern void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int unoptimizedTotalClass); extern void _unload_image(header_info *hi); -extern const char ** _objc_copyClassNamesForImage(header_info *hi, unsigned int *outCount); - extern const header_info *_headerForClass(Class cls); extern Class _class_remap(Class cls); -extern Class _class_getNonMetaClass(Class cls, id obj); extern Ivar _class_getVariable(Class cls, const char *name); extern unsigned _class_createInstancesFromZone(Class cls, size_t extraBytes, void *zone, id *results, unsigned num_requested); -extern id _objc_constructOrFree(id bytes, Class cls); extern const char *_category_getName(Category cat); extern const char *_category_getClassName(Category cat); extern Class _category_getClass(Category cat); extern IMP _category_getLoadMethod(Category cat); -extern id object_cxxConstructFromClass(id obj, Class cls); +enum { + OBJECT_CONSTRUCT_NONE = 0, + OBJECT_CONSTRUCT_FREE_ONFAILURE = 1, + OBJECT_CONSTRUCT_CALL_BADALLOC = 2, +}; +extern id object_cxxConstructFromClass(id obj, Class cls, int flags); extern void object_cxxDestruct(id obj); -extern void _class_resolveMethod(Class cls, SEL sel, id inst); - extern void fixupCopiedIvars(id newObject, id oldObject); extern Class _class_getClassForIvar(Class cls, Ivar ivar); @@ -786,9 +736,9 @@ __END_DECLS static __inline uint32_t _objc_strhash(const char *s) { uint32_t hash = 0; for (;;) { - int a = *s++; - if (0 == a) break; - hash += (hash << 8) + a; + int a = *s++; + if (0 == a) break; + hash += (hash << 8) + a; } return hash; } @@ -859,6 +809,7 @@ class TimeLogger { } }; +enum { CacheLineSize = 64 }; // StripedMap is a map of void* -> T, sized appropriately // for cache-friendly lock striping. @@ -866,10 +817,7 @@ class TimeLogger { // or as StripedMap where SomeStruct stores a spin lock. template class StripedMap { - - enum { CacheLineSize = 64 }; - -#if TARGET_OS_EMBEDDED +#if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR enum { StripeCount = 8 }; #else enum { StripeCount = 64 }; @@ -894,14 +842,56 @@ class StripedMap { return const_cast>(this)[p]; } + // Shortcuts for StripedMaps of locks. + void lockAll() { + for (unsigned int i = 0; i < StripeCount; i++) { + array[i].value.lock(); + } + } + + void unlockAll() { + for (unsigned int i = 0; i < StripeCount; i++) { + array[i].value.unlock(); + } + } + + void forceResetAll() { + for (unsigned int i = 0; i < StripeCount; i++) { + array[i].value.forceReset(); + } + } + + void defineLockOrder() { + for (unsigned int i = 1; i < StripeCount; i++) { + lockdebug_lock_precedes_lock(&array[i-1].value, &array[i].value); + } + } + + void precedeLock(const void *newlock) { + // assumes defineLockOrder is also called + lockdebug_lock_precedes_lock(&array[StripeCount-1].value, newlock); + } + + void succeedLock(const void *oldlock) { + // assumes defineLockOrder is also called + lockdebug_lock_precedes_lock(oldlock, &array[0].value); + } + + const void *getLock(int i) { + if (i < StripeCount) return &array[i].value; + else return nil; + } + #if DEBUG StripedMap() { // Verify alignment expectations. uintptr_t base = (uintptr_t)&array[0].value; uintptr_t delta = (uintptr_t)&array[1].value - base; - assert(delta % CacheLineSize == 0); - assert(base % CacheLineSize == 0); + ASSERT(delta % CacheLineSize == 0); + ASSERT(base % CacheLineSize == 0); } +#else + constexpr StripedMap() {} #endif }; @@ -965,6 +955,109 @@ static inline bool operator != (DisguisedPtr lhs, id rhs) { } +// Storage for a thread-safe chained hook function. +// get() returns the value for calling. +// set() installs a new function and returns the old one for chaining. +// More precisely, set() writes the old value to a variable supplied by +// the caller. get() and set() use appropriate barriers so that the +// old value is safely written to the variable before the new value is +// called to use it. +// +// T1: store to old variable; store-release to hook variable +// T2: load-acquire from hook variable; call it; called hook loads old variable + +template +class ChainedHookFunction { + std::atomic hook{nil}; + +public: + ChainedHookFunction(Fn f) : hook{f} { }; + + Fn get() { + return hook.load(std::memory_order_acquire); + } + + void set(Fn newValue, Fn *oldVariable) + { + Fn oldValue = hook.load(std::memory_order_relaxed); + do { + *oldVariable = oldValue; + } while (!hook.compare_exchange_weak(oldValue, newValue, + std::memory_order_release, + std::memory_order_relaxed)); + } +}; + + +// A small vector for use as a global variable. Only supports appending and +// iteration. Stores a single element inline, and multiple elements in a heap +// allocation. There is no attempt to amortize reallocation cost; this is +// intended to be used in situation where zero or one element is common, two +// might happen, and three or more is very rare. +// +// This does not clean up its allocation, and thus cannot be used as a local +// variable or member of something with limited lifetime. + +template +class GlobalSmallVector { + static_assert(std::is_pod::value, "SmallVector requires POD types"); + +protected: + unsigned count{0}; + union { + T inlineElements[InlineCount]; + T *elements; + }; + +public: + void append(const T &val) { + if (count < InlineCount) { + // We have space. Store the new value inline. + inlineElements[count] = val; + } else if (count == InlineCount) { + // Inline storage is full. Switch to a heap allocation. + T *newElements = (T *)malloc((count + 1) * sizeof(T)); + memcpy(newElements, inlineElements, count * sizeof(T)); + newElements[count] = val; + elements = newElements; + } else { + // Resize the heap allocation and append. + elements = (T *)realloc(elements, (count + 1) * sizeof(T)); + elements[count] = val; + } + count++; + } + + const T *begin() const { + return count <= InlineCount ? inlineElements : elements; + } + + const T *end() const { + return begin() + count; + } +}; + +// A small vector that cleans up its internal memory allocation when destroyed. +template +class SmallVector: public GlobalSmallVector { +public: + ~SmallVector() { + if (this->count > InlineCount) + free(this->elements); + } + + template + void initFrom(const GlobalSmallVector &other) { + ASSERT(this->count == 0); + this->count = (unsigned)(other.end() - other.begin()); + if (this->count > InlineCount) { + this->elements = (T *)memdup(other.begin(), this->count * sizeof(T)); + } else { + memcpy(this->inlineElements, other.begin(), this->count * sizeof(T)); + } + } +}; + // Pointer hash function. // This is not a terrific hash, but it is fast // and not outrageously flawed for our purposes. @@ -1015,6 +1108,10 @@ static uint32_t ptr_hash(uint32_t key) */ + +// Lock declarations +#include "objc-locks.h" + // Inlined parts of objc_object's implementation #include "objc-object.h" diff --git a/runtime/objc-ptrauth.h b/runtime/objc-ptrauth.h new file mode 100644 index 0000000..e275dca --- /dev/null +++ b/runtime/objc-ptrauth.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2017 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +#ifndef _OBJC_PTRAUTH_H_ +#define _OBJC_PTRAUTH_H_ + +#include + +// On some architectures, method lists and method caches store signed IMPs. + +// fixme simply include ptrauth.h once all build trains have it +#if __has_include () +#include +#else +#define ptrauth_strip(__value, __key) __value +#define ptrauth_blend_discriminator(__pointer, __integer) ((uintptr_t)0) +#define ptrauth_sign_constant(__value, __key, __data) __value +#define ptrauth_sign_unauthenticated(__value, __key, __data) __value +#define ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key, __new_data) __value +#define ptrauth_auth_function(__value, __old_key, __old_data) __value +#define ptrauth_auth_data(__value, __old_key, __old_data) __value +#define ptrauth_string_discriminator(__string) ((int)0) +#define ptrauth_sign_generic_data(__value, __data) ((ptrauth_generic_signature_t)0) + +#define __ptrauth_function_pointer +#define __ptrauth_return_address +#define __ptrauth_block_invocation_pointer +#define __ptrauth_block_copy_helper +#define __ptrauth_block_destroy_helper +#define __ptrauth_block_byref_copy_helper +#define __ptrauth_block_byref_destroy_helper +#define __ptrauth_objc_method_list_imp +#define __ptrauth_cxx_vtable_pointer +#define __ptrauth_cxx_vtt_vtable_pointer +#define __ptrauth_swift_heap_object_destructor +#define __ptrauth_cxx_virtual_function_pointer(__declkey) +#define __ptrauth_swift_function_pointer(__typekey) +#define __ptrauth_swift_class_method_pointer(__declkey) +#define __ptrauth_swift_protocol_witness_function_pointer(__declkey) +#define __ptrauth_swift_value_witness_function_pointer(__key) +#endif + + +#if __has_feature(ptrauth_calls) + +#if !__arm64__ +#error ptrauth other than arm64e is unimplemented +#endif + +// Method lists use process-independent signature for compatibility. +using MethodListIMP = IMP __ptrauth_objc_method_list_imp; + +#else + +using MethodListIMP = IMP; + +#endif + +// _OBJC_PTRAUTH_H_ +#endif diff --git a/runtime/objc-references.h b/runtime/objc-references.h index 1e49f7c..8c79405 100644 --- a/runtime/objc-references.h +++ b/runtime/objc-references.h @@ -32,8 +32,9 @@ __BEGIN_DECLS -extern void _object_set_associative_reference(id object, void *key, id value, uintptr_t policy); -extern id _object_get_associative_reference(id object, void *key); +extern void _objc_associations_init(); +extern void _object_set_associative_reference(id object, const void *key, id value, uintptr_t policy); +extern id _object_get_associative_reference(id object, const void *key); extern void _object_remove_assocations(id object); __END_DECLS diff --git a/runtime/objc-references.mm b/runtime/objc-references.mm index 712b88e..caa8910 100644 --- a/runtime/objc-references.mm +++ b/runtime/objc-references.mm @@ -28,307 +28,209 @@ #include "objc-private.h" #include #include - -#if _LIBCPP_VERSION -# include -#else -# include - using namespace tr1; -#endif - - -// wrap all the murky C++ details in a namespace to get them out of the way. - -namespace objc_references_support { - struct DisguisedPointerEqual { - bool operator()(uintptr_t p1, uintptr_t p2) const { - return p1 == p2; - } - }; - - struct DisguisedPointerHash { - uintptr_t operator()(uintptr_t k) const { - // borrowed from CFSet.c -#if __LP64__ - uintptr_t a = 0x4368726973746F70ULL; - uintptr_t b = 0x686572204B616E65ULL; -#else - uintptr_t a = 0x4B616E65UL; - uintptr_t b = 0x4B616E65UL; -#endif - uintptr_t c = 1; - a += k; -#if __LP64__ - a -= b; a -= c; a ^= (c >> 43); - b -= c; b -= a; b ^= (a << 9); - c -= a; c -= b; c ^= (b >> 8); - a -= b; a -= c; a ^= (c >> 38); - b -= c; b -= a; b ^= (a << 23); - c -= a; c -= b; c ^= (b >> 5); - a -= b; a -= c; a ^= (c >> 35); - b -= c; b -= a; b ^= (a << 49); - c -= a; c -= b; c ^= (b >> 11); - a -= b; a -= c; a ^= (c >> 12); - b -= c; b -= a; b ^= (a << 18); - c -= a; c -= b; c ^= (b >> 22); -#else - a -= b; a -= c; a ^= (c >> 13); - b -= c; b -= a; b ^= (a << 8); - c -= a; c -= b; c ^= (b >> 13); - a -= b; a -= c; a ^= (c >> 12); - b -= c; b -= a; b ^= (a << 16); - c -= a; c -= b; c ^= (b >> 5); - a -= b; a -= c; a ^= (c >> 3); - b -= c; b -= a; b ^= (a << 10); - c -= a; c -= b; c ^= (b >> 15); -#endif - return c; - } - }; - - struct ObjectPointerLess { - bool operator()(const void *p1, const void *p2) const { - return p1 < p2; - } - }; - - struct ObjcPointerHash { - uintptr_t operator()(void *p) const { - return DisguisedPointerHash()(uintptr_t(p)); - } - }; - - // STL allocator that uses the runtime's internal allocator. - - template struct ObjcAllocator { - typedef T value_type; - typedef value_type* pointer; - typedef const value_type *const_pointer; - typedef value_type& reference; - typedef const value_type& const_reference; - typedef size_t size_type; - typedef ptrdiff_t difference_type; - - template struct rebind { typedef ObjcAllocator other; }; - - template ObjcAllocator(const ObjcAllocator&) {} - ObjcAllocator() {} - ObjcAllocator(const ObjcAllocator&) {} - ~ObjcAllocator() {} - - pointer address(reference x) const { return &x; } - const_pointer address(const_reference x) const { - return x; - } - - pointer allocate(size_type n, const_pointer = 0) { - return static_cast(::malloc(n * sizeof(T))); - } - - void deallocate(pointer p, size_type) { ::free(p); } - - size_type max_size() const { - return static_cast(-1) / sizeof(T); - } - - void construct(pointer p, const value_type& x) { - new(p) value_type(x); - } - - void destroy(pointer p) { p->~value_type(); } - - void operator=(const ObjcAllocator&); - - }; - - template<> struct ObjcAllocator { - typedef void value_type; - typedef void* pointer; - typedef const void *const_pointer; - template struct rebind { typedef ObjcAllocator other; }; - }; - - typedef uintptr_t disguised_ptr_t; - inline disguised_ptr_t DISGUISE(id value) { return ~uintptr_t(value); } - inline id UNDISGUISE(disguised_ptr_t dptr) { return id(~dptr); } - - class ObjcAssociation { - uintptr_t _policy; - id _value; - public: - ObjcAssociation(uintptr_t policy, id value) : _policy(policy), _value(value) {} - ObjcAssociation() : _policy(0), _value(nil) {} - - uintptr_t policy() const { return _policy; } - id value() const { return _value; } - - bool hasValue() { return _value != nil; } - }; - -#if TARGET_OS_WIN32 - typedef hash_map ObjectAssociationMap; - typedef hash_map AssociationsHashMap; -#else - typedef ObjcAllocator > ObjectAssociationMapAllocator; - class ObjectAssociationMap : public std::map { - public: - void *operator new(size_t n) { return ::malloc(n); } - void operator delete(void *ptr) { ::free(ptr); } - }; - typedef ObjcAllocator > AssociationsHashMapAllocator; - class AssociationsHashMap : public unordered_map { - public: - void *operator new(size_t n) { return ::malloc(n); } - void operator delete(void *ptr) { ::free(ptr); } - }; -#endif -} - -using namespace objc_references_support; - -// class AssociationsManager manages a lock / hash table singleton pair. -// Allocating an instance acquires the lock, and calling its assocations() method -// lazily allocates it. - -class AssociationsManager { - static spinlock_t _lock; - static AssociationsHashMap *_map; // associative references: object pointer -> PtrPtrHashMap. -public: - AssociationsManager() { _lock.lock(); } - ~AssociationsManager() { _lock.unlock(); } - - AssociationsHashMap &associations() { - if (_map == NULL) - _map = new AssociationsHashMap(); - return *_map; - } -}; - -spinlock_t AssociationsManager::_lock; -AssociationsHashMap *AssociationsManager::_map = NULL; +#include "DenseMapExtras.h" // expanded policy bits. -enum { +enum { OBJC_ASSOCIATION_SETTER_ASSIGN = 0, OBJC_ASSOCIATION_SETTER_RETAIN = 1, OBJC_ASSOCIATION_SETTER_COPY = 3, // NOTE: both bits are set, so we can simply test 1 bit in releaseValue below. - OBJC_ASSOCIATION_GETTER_READ = (0 << 8), - OBJC_ASSOCIATION_GETTER_RETAIN = (1 << 8), + OBJC_ASSOCIATION_GETTER_READ = (0 << 8), + OBJC_ASSOCIATION_GETTER_RETAIN = (1 << 8), OBJC_ASSOCIATION_GETTER_AUTORELEASE = (2 << 8) -}; +}; -id _object_get_associative_reference(id object, void *key) { - id value = nil; - uintptr_t policy = OBJC_ASSOCIATION_ASSIGN; - { - AssociationsManager manager; - AssociationsHashMap &associations(manager.associations()); - disguised_ptr_t disguised_object = DISGUISE(object); - AssociationsHashMap::iterator i = associations.find(disguised_object); - if (i != associations.end()) { - ObjectAssociationMap *refs = i->second; - ObjectAssociationMap::iterator j = refs->find(key); - if (j != refs->end()) { - ObjcAssociation &entry = j->second; - value = entry.value(); - policy = entry.policy(); - if (policy & OBJC_ASSOCIATION_GETTER_RETAIN) ((id(*)(id, SEL))objc_msgSend)(value, SEL_retain); +spinlock_t AssociationsManagerLock; + +namespace objc { + +class ObjcAssociation { + uintptr_t _policy; + id _value; +public: + ObjcAssociation(uintptr_t policy, id value) : _policy(policy), _value(value) {} + ObjcAssociation() : _policy(0), _value(nil) {} + ObjcAssociation(const ObjcAssociation &other) = default; + ObjcAssociation &operator=(const ObjcAssociation &other) = default; + ObjcAssociation(ObjcAssociation &&other) : ObjcAssociation() { + swap(other); + } + + inline void swap(ObjcAssociation &other) { + std::swap(_policy, other._policy); + std::swap(_value, other._value); + } + + inline uintptr_t policy() const { return _policy; } + inline id value() const { return _value; } + + inline void acquireValue() { + if (_value) { + switch (_policy & 0xFF) { + case OBJC_ASSOCIATION_SETTER_RETAIN: + _value = objc_retain(_value); + break; + case OBJC_ASSOCIATION_SETTER_COPY: + _value = ((id(*)(id, SEL))objc_msgSend)(_value, @selector(copy)); + break; } } } - if (value && (policy & OBJC_ASSOCIATION_GETTER_AUTORELEASE)) { - ((id(*)(id, SEL))objc_msgSend)(value, SEL_autorelease); - } - return value; -} -static id acquireValue(id value, uintptr_t policy) { - switch (policy & 0xFF) { - case OBJC_ASSOCIATION_SETTER_RETAIN: - return ((id(*)(id, SEL))objc_msgSend)(value, SEL_retain); - case OBJC_ASSOCIATION_SETTER_COPY: - return ((id(*)(id, SEL))objc_msgSend)(value, SEL_copy); + inline void releaseHeldValue() { + if (_value && (_policy & OBJC_ASSOCIATION_SETTER_RETAIN)) { + objc_release(_value); + } } - return value; -} -static void releaseValue(id value, uintptr_t policy) { - if (policy & OBJC_ASSOCIATION_SETTER_RETAIN) { - ((id(*)(id, SEL))objc_msgSend)(value, SEL_release); + inline void retainReturnedValue() { + if (_value && (_policy & OBJC_ASSOCIATION_GETTER_RETAIN)) { + objc_retain(_value); + } } -} -struct ReleaseValue { - void operator() (ObjcAssociation &association) { - releaseValue(association.value(), association.policy()); + inline id autoreleaseReturnedValue() { + if (slowpath(_value && (_policy & OBJC_ASSOCIATION_GETTER_AUTORELEASE))) { + return objc_autorelease(_value); + } + return _value; } }; -void _object_set_associative_reference(id object, void *key, id value, uintptr_t policy) { - // retain the new value (if any) outside the lock. - ObjcAssociation old_association(0, nil); - id new_value = value ? acquireValue(value, policy) : nil; +typedef DenseMap ObjectAssociationMap; +typedef DenseMap, ObjectAssociationMap> AssociationsHashMap; + +// class AssociationsManager manages a lock / hash table singleton pair. +// Allocating an instance acquires the lock + +class AssociationsManager { + using Storage = ExplicitInitDenseMap, ObjectAssociationMap>; + static Storage _mapStorage; + +public: + AssociationsManager() { AssociationsManagerLock.lock(); } + ~AssociationsManager() { AssociationsManagerLock.unlock(); } + + AssociationsHashMap &get() { + return _mapStorage.get(); + } + + static void init() { + _mapStorage.init(); + } +}; + +AssociationsManager::Storage AssociationsManager::_mapStorage; + +} // namespace objc + +using namespace objc; + +void +_objc_associations_init() +{ + AssociationsManager::init(); +} + +id +_object_get_associative_reference(id object, const void *key) +{ + ObjcAssociation association{}; + { AssociationsManager manager; - AssociationsHashMap &associations(manager.associations()); - disguised_ptr_t disguised_object = DISGUISE(object); - if (new_value) { - // break any existing association. - AssociationsHashMap::iterator i = associations.find(disguised_object); - if (i != associations.end()) { - // secondary table exists - ObjectAssociationMap *refs = i->second; - ObjectAssociationMap::iterator j = refs->find(key); - if (j != refs->end()) { - old_association = j->second; - j->second = ObjcAssociation(policy, new_value); - } else { - (*refs)[key] = ObjcAssociation(policy, new_value); - } - } else { - // create the new association (first time). - ObjectAssociationMap *refs = new ObjectAssociationMap; - associations[disguised_object] = refs; - (*refs)[key] = ObjcAssociation(policy, new_value); + AssociationsHashMap &associations(manager.get()); + AssociationsHashMap::iterator i = associations.find((objc_object *)object); + if (i != associations.end()) { + ObjectAssociationMap &refs = i->second; + ObjectAssociationMap::iterator j = refs.find(key); + if (j != refs.end()) { + association = j->second; + association.retainReturnedValue(); + } + } + } + + return association.autoreleaseReturnedValue(); +} + +void +_object_set_associative_reference(id object, const void *key, id value, uintptr_t policy) +{ + // This code used to work when nil was passed for object and key. Some code + // probably relies on that to not crash. Check and handle it explicitly. + // rdar://problem/44094390 + if (!object && !value) return; + + if (object->getIsa()->forbidsAssociatedObjects()) + _objc_fatal("objc_setAssociatedObject called on instance (%p) of class %s which does not allow associated objects", object, object_getClassName(object)); + + DisguisedPtr disguised{(objc_object *)object}; + ObjcAssociation association{policy, value}; + + // retain the new value (if any) outside the lock. + association.acquireValue(); + + { + AssociationsManager manager; + AssociationsHashMap &associations(manager.get()); + + if (value) { + auto refs_result = associations.try_emplace(disguised, ObjectAssociationMap{}); + if (refs_result.second) { + /* it's the first association we make */ object->setHasAssociatedObjects(); } + + /* establish or replace the association */ + auto &refs = refs_result.first->second; + auto result = refs.try_emplace(key, std::move(association)); + if (!result.second) { + association.swap(result.first->second); + } } else { - // setting the association to nil breaks the association. - AssociationsHashMap::iterator i = associations.find(disguised_object); - if (i != associations.end()) { - ObjectAssociationMap *refs = i->second; - ObjectAssociationMap::iterator j = refs->find(key); - if (j != refs->end()) { - old_association = j->second; - refs->erase(j); + auto refs_it = associations.find(disguised); + if (refs_it != associations.end()) { + auto &refs = refs_it->second; + auto it = refs.find(key); + if (it != refs.end()) { + association.swap(it->second); + refs.erase(it); + if (refs.size() == 0) { + associations.erase(refs_it); + + } } } } } + // release the old value (outside of the lock). - if (old_association.hasValue()) ReleaseValue()(old_association); + association.releaseHeldValue(); } -void _object_remove_assocations(id object) { - vector< ObjcAssociation,ObjcAllocator > elements; +// Unlike setting/getting an associated reference, +// this function is performance sensitive because of +// raw isa objects (such as OS Objects) that can't track +// whether they have associated objects. +void +_object_remove_assocations(id object) +{ + ObjectAssociationMap refs{}; + { AssociationsManager manager; - AssociationsHashMap &associations(manager.associations()); - if (associations.size() == 0) return; - disguised_ptr_t disguised_object = DISGUISE(object); - AssociationsHashMap::iterator i = associations.find(disguised_object); + AssociationsHashMap &associations(manager.get()); + AssociationsHashMap::iterator i = associations.find((objc_object *)object); if (i != associations.end()) { - // copy all of the associations that need to be removed. - ObjectAssociationMap *refs = i->second; - for (ObjectAssociationMap::iterator j = refs->begin(), end = refs->end(); j != end; ++j) { - elements.push_back(j->second); - } - // remove the secondary table. - delete refs; + refs.swap(i->second); associations.erase(i); } } - // the calls to releaseValue() happen outside of the lock. - for_each(elements.begin(), elements.end(), ReleaseValue()); + + // release everything (outside of the lock). + for (auto &i: refs) { + i.second.releaseHeldValue(); + } } diff --git a/runtime/objc-runtime-new.h b/runtime/objc-runtime-new.h index 7b7dbac..d3541cf 100644 --- a/runtime/objc-runtime-new.h +++ b/runtime/objc-runtime-new.h @@ -24,37 +24,293 @@ #ifndef _OBJC_RUNTIME_NEW_H #define _OBJC_RUNTIME_NEW_H +#include "PointerUnion.h" + +// class_data_bits_t is the class_t->data field (class_rw_t pointer plus flags) +// The extra bits are optimized for the retain/release and alloc/dealloc paths. + +// Values for class_ro_t->flags +// These are emitted by the compiler and are part of the ABI. +// Note: See CGObjCNonFragileABIMac::BuildClassRoTInitializer in clang +// class is a metaclass +#define RO_META (1<<0) +// class is a root class +#define RO_ROOT (1<<1) +// class has .cxx_construct/destruct implementations +#define RO_HAS_CXX_STRUCTORS (1<<2) +// class has +load implementation +// #define RO_HAS_LOAD_METHOD (1<<3) +// class has visibility=hidden set +#define RO_HIDDEN (1<<4) +// class has attribute(objc_exception): OBJC_EHTYPE_$_ThisClass is non-weak +#define RO_EXCEPTION (1<<5) +// class has ro field for Swift metadata initializer callback +#define RO_HAS_SWIFT_INITIALIZER (1<<6) +// class compiled with ARC +#define RO_IS_ARC (1<<7) +// class has .cxx_destruct but no .cxx_construct (with RO_HAS_CXX_STRUCTORS) +#define RO_HAS_CXX_DTOR_ONLY (1<<8) +// class is not ARC but has ARC-style weak ivar layout +#define RO_HAS_WEAK_WITHOUT_ARC (1<<9) +// class does not allow associated objects on instances +#define RO_FORBIDS_ASSOCIATED_OBJECTS (1<<10) + +// class is in an unloadable bundle - must never be set by compiler +#define RO_FROM_BUNDLE (1<<29) +// class is unrealized future class - must never be set by compiler +#define RO_FUTURE (1<<30) +// class is realized - must never be set by compiler +#define RO_REALIZED (1<<31) + +// Values for class_rw_t->flags +// These are not emitted by the compiler and are never used in class_ro_t. +// Their presence should be considered in future ABI versions. +// class_t->data is class_rw_t, not class_ro_t +#define RW_REALIZED (1<<31) +// class is unresolved future class +#define RW_FUTURE (1<<30) +// class is initialized +#define RW_INITIALIZED (1<<29) +// class is initializing +#define RW_INITIALIZING (1<<28) +// class_rw_t->ro is heap copy of class_ro_t +#define RW_COPIED_RO (1<<27) +// class allocated but not yet registered +#define RW_CONSTRUCTING (1<<26) +// class allocated and registered +#define RW_CONSTRUCTED (1<<25) +// available for use; was RW_FINALIZE_ON_MAIN_THREAD +// #define RW_24 (1<<24) +// class +load has been called +#define RW_LOADED (1<<23) +#if !SUPPORT_NONPOINTER_ISA +// class instances may have associative references +#define RW_INSTANCES_HAVE_ASSOCIATED_OBJECTS (1<<22) +#endif +// class has instance-specific GC layout +#define RW_HAS_INSTANCE_SPECIFIC_LAYOUT (1 << 21) +// class does not allow associated objects on its instances +#define RW_FORBIDS_ASSOCIATED_OBJECTS (1<<20) +// class has started realizing but not yet completed it +#define RW_REALIZING (1<<19) + +// class is a metaclass (copied from ro) +#define RW_META RO_META // (1<<0) + + +// NOTE: MORE RW_ FLAGS DEFINED BELOW + + +// Values for class_rw_t->flags (RW_*), cache_t->_flags (FAST_CACHE_*), +// or class_t->bits (FAST_*). +// +// FAST_* and FAST_CACHE_* are stored on the class, reducing pointer indirection. + +#if __LP64__ + +// class is a Swift class from the pre-stable Swift ABI +#define FAST_IS_SWIFT_LEGACY (1UL<<0) +// class is a Swift class from the stable Swift ABI +#define FAST_IS_SWIFT_STABLE (1UL<<1) +// class or superclass has default retain/release/autorelease/retainCount/ +// _tryRetain/_isDeallocating/retainWeakReference/allowsWeakReference +#define FAST_HAS_DEFAULT_RR (1UL<<2) +// data pointer +#define FAST_DATA_MASK 0x00007ffffffffff8UL + +#if __arm64__ +// class or superclass has .cxx_construct/.cxx_destruct implementation +// FAST_CACHE_HAS_CXX_DTOR is the first bit so that setting it in +// isa_t::has_cxx_dtor is a single bfi +#define FAST_CACHE_HAS_CXX_DTOR (1<<0) +#define FAST_CACHE_HAS_CXX_CTOR (1<<1) +// Denormalized RO_META to avoid an indirection +#define FAST_CACHE_META (1<<2) +#else +// Denormalized RO_META to avoid an indirection +#define FAST_CACHE_META (1<<0) +// class or superclass has .cxx_construct/.cxx_destruct implementation +// FAST_CACHE_HAS_CXX_DTOR is chosen to alias with isa_t::has_cxx_dtor +#define FAST_CACHE_HAS_CXX_CTOR (1<<1) +#define FAST_CACHE_HAS_CXX_DTOR (1<<2) +#endif + +// Fast Alloc fields: +// This stores the word-aligned size of instances + "ALLOC_DELTA16", +// or 0 if the instance size doesn't fit. +// +// These bits occupy the same bits than in the instance size, so that +// the size can be extracted with a simple mask operation. +// +// FAST_CACHE_ALLOC_MASK16 allows to extract the instance size rounded +// rounded up to the next 16 byte boundary, which is a fastpath for +// _objc_rootAllocWithZone() +#define FAST_CACHE_ALLOC_MASK 0x1ff8 +#define FAST_CACHE_ALLOC_MASK16 0x1ff0 +#define FAST_CACHE_ALLOC_DELTA16 0x0008 + +// class's instances requires raw isa +#define FAST_CACHE_REQUIRES_RAW_ISA (1<<13) +// class or superclass has default alloc/allocWithZone: implementation +// Note this is is stored in the metaclass. +#define FAST_CACHE_HAS_DEFAULT_AWZ (1<<14) +// class or superclass has default new/self/class/respondsToSelector/isKindOfClass +#define FAST_CACHE_HAS_DEFAULT_CORE (1<<15) + +#else + +// class or superclass has .cxx_construct implementation +#define RW_HAS_CXX_CTOR (1<<18) +// class or superclass has .cxx_destruct implementation +#define RW_HAS_CXX_DTOR (1<<17) +// class or superclass has default alloc/allocWithZone: implementation +// Note this is is stored in the metaclass. +#define RW_HAS_DEFAULT_AWZ (1<<16) +// class's instances requires raw isa +#if SUPPORT_NONPOINTER_ISA +#define RW_REQUIRES_RAW_ISA (1<<15) +#endif +// class or superclass has default retain/release/autorelease/retainCount/ +// _tryRetain/_isDeallocating/retainWeakReference/allowsWeakReference +#define RW_HAS_DEFAULT_RR (1<<14) +// class or superclass has default new/self/class/respondsToSelector/isKindOfClass +#define RW_HAS_DEFAULT_CORE (1<<13) + +// class is a Swift class from the pre-stable Swift ABI +#define FAST_IS_SWIFT_LEGACY (1UL<<0) +// class is a Swift class from the stable Swift ABI +#define FAST_IS_SWIFT_STABLE (1UL<<1) +// data pointer +#define FAST_DATA_MASK 0xfffffffcUL + +#endif // __LP64__ + +// The Swift ABI requires that these bits be defined like this on all platforms. +static_assert(FAST_IS_SWIFT_LEGACY == 1, "resistance is futile"); +static_assert(FAST_IS_SWIFT_STABLE == 2, "resistance is futile"); + + #if __LP64__ typedef uint32_t mask_t; // x86_64 & arm64 asm are less efficient with 16-bits #else typedef uint16_t mask_t; #endif -typedef uintptr_t cache_key_t; +typedef uintptr_t SEL; struct swift_class_t; +enum Atomicity { Atomic = true, NotAtomic = false }; +enum IMPEncoding { Encoded = true, Raw = false }; struct bucket_t { private: - cache_key_t _key; - IMP _imp; + // IMP-first is better for arm64e ptrauth and no worse for arm64. + // SEL-first is better for armv7* and i386 and x86_64. +#if __arm64__ + explicit_atomic _imp; + explicit_atomic _sel; +#else + explicit_atomic _sel; + explicit_atomic _imp; +#endif + + // Compute the ptrauth signing modifier from &_imp, newSel, and cls. + uintptr_t modifierForSEL(SEL newSel, Class cls) const { + return (uintptr_t)&_imp ^ (uintptr_t)newSel ^ (uintptr_t)cls; + } + + // Sign newImp, with &_imp, newSel, and cls as modifiers. + uintptr_t encodeImp(IMP newImp, SEL newSel, Class cls) const { + if (!newImp) return 0; +#if CACHE_IMP_ENCODING == CACHE_IMP_ENCODING_PTRAUTH + return (uintptr_t) + ptrauth_auth_and_resign(newImp, + ptrauth_key_function_pointer, 0, + ptrauth_key_process_dependent_code, + modifierForSEL(newSel, cls)); +#elif CACHE_IMP_ENCODING == CACHE_IMP_ENCODING_ISA_XOR + return (uintptr_t)newImp ^ (uintptr_t)cls; +#elif CACHE_IMP_ENCODING == CACHE_IMP_ENCODING_NONE + return (uintptr_t)newImp; +#else +#error Unknown method cache IMP encoding. +#endif + } public: - inline cache_key_t key() const { return _key; } - inline IMP imp() const { return (IMP)_imp; } - inline void setKey(cache_key_t newKey) { _key = newKey; } - inline void setImp(IMP newImp) { _imp = newImp; } + inline SEL sel() const { return _sel.load(memory_order::memory_order_relaxed); } - void set(cache_key_t newKey, IMP newImp); + inline IMP imp(Class cls) const { + uintptr_t imp = _imp.load(memory_order::memory_order_relaxed); + if (!imp) return nil; +#if CACHE_IMP_ENCODING == CACHE_IMP_ENCODING_PTRAUTH + SEL sel = _sel.load(memory_order::memory_order_relaxed); + return (IMP) + ptrauth_auth_and_resign((const void *)imp, + ptrauth_key_process_dependent_code, + modifierForSEL(sel, cls), + ptrauth_key_function_pointer, 0); +#elif CACHE_IMP_ENCODING == CACHE_IMP_ENCODING_ISA_XOR + return (IMP)(imp ^ (uintptr_t)cls); +#elif CACHE_IMP_ENCODING == CACHE_IMP_ENCODING_NONE + return (IMP)imp; +#else +#error Unknown method cache IMP encoding. +#endif + } + + template + void set(SEL newSel, IMP newImp, Class cls); }; struct cache_t { - struct bucket_t *_buckets; - mask_t _mask; - mask_t _occupied; +#if CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_OUTLINED + explicit_atomic _buckets; + explicit_atomic _mask; +#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_HIGH_16 + explicit_atomic _maskAndBuckets; + mask_t _mask_unused; + + // How much the mask is shifted by. + static constexpr uintptr_t maskShift = 48; + + // Additional bits after the mask which must be zero. msgSend + // takes advantage of these additional bits to construct the value + // `mask << 4` from `_maskAndBuckets` in a single instruction. + static constexpr uintptr_t maskZeroBits = 4; + + // The largest mask value we can store. + static constexpr uintptr_t maxMask = ((uintptr_t)1 << (64 - maskShift)) - 1; + + // The mask applied to `_maskAndBuckets` to retrieve the buckets pointer. + static constexpr uintptr_t bucketsMask = ((uintptr_t)1 << (maskShift - maskZeroBits)) - 1; + + // Ensure we have enough bits for the buckets pointer. + static_assert(bucketsMask >= MACH_VM_MAX_ADDRESS, "Bucket field doesn't have enough bits for arbitrary pointers."); +#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_LOW_4 + // _maskAndBuckets stores the mask shift in the low 4 bits, and + // the buckets pointer in the remainder of the value. The mask + // shift is the value where (0xffff >> shift) produces the correct + // mask. This is equal to 16 - log2(cache_size). + explicit_atomic _maskAndBuckets; + mask_t _mask_unused; + + static constexpr uintptr_t maskBits = 4; + static constexpr uintptr_t maskMask = (1 << maskBits) - 1; + static constexpr uintptr_t bucketsMask = ~maskMask; +#else +#error Unknown cache mask storage type. +#endif + +#if __LP64__ + uint16_t _flags; +#endif + uint16_t _occupied; public: + static bucket_t *emptyBuckets(); + struct bucket_t *buckets(); mask_t mask(); mask_t occupied(); @@ -62,24 +318,96 @@ public: void setBucketsAndMask(struct bucket_t *newBuckets, mask_t newMask); void initializeToEmpty(); - mask_t capacity(); + unsigned capacity(); bool isConstantEmptyCache(); bool canBeFreed(); +#if __LP64__ + bool getBit(uint16_t flags) const { + return _flags & flags; + } + void setBit(uint16_t set) { + __c11_atomic_fetch_or((_Atomic(uint16_t) *)&_flags, set, __ATOMIC_RELAXED); + } + void clearBit(uint16_t clear) { + __c11_atomic_fetch_and((_Atomic(uint16_t) *)&_flags, ~clear, __ATOMIC_RELAXED); + } +#endif + +#if FAST_CACHE_ALLOC_MASK + bool hasFastInstanceSize(size_t extra) const + { + if (__builtin_constant_p(extra) && extra == 0) { + return _flags & FAST_CACHE_ALLOC_MASK16; + } + return _flags & FAST_CACHE_ALLOC_MASK; + } + + size_t fastInstanceSize(size_t extra) const + { + ASSERT(hasFastInstanceSize(extra)); + + if (__builtin_constant_p(extra) && extra == 0) { + return _flags & FAST_CACHE_ALLOC_MASK16; + } else { + size_t size = _flags & FAST_CACHE_ALLOC_MASK; + // remove the FAST_CACHE_ALLOC_DELTA16 that was added + // by setFastInstanceSize + return align16(size + extra - FAST_CACHE_ALLOC_DELTA16); + } + } + + void setFastInstanceSize(size_t newSize) + { + // Set during realization or construction only. No locking needed. + uint16_t newBits = _flags & ~FAST_CACHE_ALLOC_MASK; + uint16_t sizeBits; + + // Adding FAST_CACHE_ALLOC_DELTA16 allows for FAST_CACHE_ALLOC_MASK16 + // to yield the proper 16byte aligned allocation size with a single mask + sizeBits = word_align(newSize) + FAST_CACHE_ALLOC_DELTA16; + sizeBits &= FAST_CACHE_ALLOC_MASK; + if (newSize <= sizeBits) { + newBits |= sizeBits; + } + _flags = newBits; + } +#else + bool hasFastInstanceSize(size_t extra) const { + return false; + } + size_t fastInstanceSize(size_t extra) const { + abort(); + } + void setFastInstanceSize(size_t extra) { + // nothing + } +#endif + static size_t bytesForCapacity(uint32_t cap); static struct bucket_t * endMarker(struct bucket_t *b, uint32_t cap); - void expand(); - void reallocate(mask_t oldCapacity, mask_t newCapacity); - struct bucket_t * find(cache_key_t key, id receiver); + void reallocate(mask_t oldCapacity, mask_t newCapacity, bool freeOld); + void insert(Class cls, SEL sel, IMP imp, id receiver); - static void bad_cache(id receiver, SEL sel, Class isa) __attribute__((noreturn)); + static void bad_cache(id receiver, SEL sel, Class isa) __attribute__((noreturn, cold)); }; // classref_t is unremapped class_t* typedef struct classref * classref_t; + +#ifdef __PTRAUTH_INTRINSICS__ +# define StubClassInitializerPtrauth __ptrauth(ptrauth_key_function_pointer, 1, 0xc671) +#else +# define StubClassInitializerPtrauth +#endif +struct stub_class_t { + uintptr_t isa; + _objc_swiftMetadataInitializer StubClassInitializerPtrauth initializer; +}; + /*********************************************************************** * entsize_list_tt * Generic implementation of an array of non-fragile structs. @@ -103,20 +431,28 @@ struct entsize_list_tt { } Element& getOrEnd(uint32_t i) const { - assert(i <= count); + ASSERT(i <= count); return *(Element *)((uint8_t *)&first + i*entsize()); } Element& get(uint32_t i) const { - assert(i < count); + ASSERT(i < count); return getOrEnd(i); } size_t byteSize() const { - return sizeof(*this) + (count-1)*entsize(); + return byteSize(entsize(), count); + } + + static size_t byteSize(uint32_t entsize, uint32_t count) { + return sizeof(entsize_list_tt) + (count-1)*entsize; } List *duplicate() const { - return (List *)memdup(this, this->byteSize()); + auto *dup = (List *)calloc(this->byteSize(), 1); + dup->entsizeAndFlags = this->entsizeAndFlags; + dup->count = this->count; + std::copy(begin(), end(), dup->begin()); + return dup; } struct iterator; @@ -207,7 +543,7 @@ struct entsize_list_tt { struct method_t { SEL name; const char *types; - IMP imp; + MethodListIMP imp; struct SortBySELAddress : public std::binary_function { + bool isUniqued() const; bool isFixedUp() const; void setFixedUp(); uint32_t indexOfMethod(const method_t *meth) const { uint32_t i = (uint32_t)(((uintptr_t)meth - (uintptr_t)this) / entsize()); - assert(i < count); + ASSERT(i < count); return i; } }; @@ -272,8 +609,9 @@ struct property_list_t : entsize_list_tt { typedef uintptr_t protocol_ref_t; // protocol_t *, but unremapped // Values for protocol_t->flags -#define PROTOCOL_FIXED_UP_2 (1<<31) // must never be set by compiler -#define PROTOCOL_FIXED_UP_1 (1<<30) // must never be set by compiler +#define PROTOCOL_FIXED_UP_2 (1<<31) // must never be set by compiler +#define PROTOCOL_FIXED_UP_1 (1<<30) // must never be set by compiler +#define PROTOCOL_IS_CANONICAL (1<<29) // must never be set by compiler // Bits 0..15 are reserved for Swift's use. #define PROTOCOL_FIXED_UP_MASK (PROTOCOL_FIXED_UP_1 | PROTOCOL_FIXED_UP_2) @@ -302,6 +640,9 @@ struct protocol_t : objc_object { bool isFixedUp() const; void setFixedUp(); + bool isCanonical() const; + void clearIsCanonical(); + # define HAS_FIELD(f) (size >= offsetof(protocol_t, f) + sizeof(f)) bool hasExtendedMethodTypesField() const { @@ -326,7 +667,7 @@ struct protocol_t : objc_object { }; struct protocol_list_t { - // count is 64-bit by accident. + // count is pointer-sized by accident. uintptr_t count; protocol_ref_t list[0]; // variable-size @@ -355,176 +696,6 @@ struct protocol_list_t { } }; -struct locstamped_category_t { - category_t *cat; - struct header_info *hi; -}; - -struct locstamped_category_list_t { - uint32_t count; -#if __LP64__ - uint32_t reserved; -#endif - locstamped_category_t list[0]; -}; - - -// class_data_bits_t is the class_t->data field (class_rw_t pointer plus flags) -// The extra bits are optimized for the retain/release and alloc/dealloc paths. - -// Values for class_ro_t->flags -// These are emitted by the compiler and are part of the ABI. -// Note: See CGObjCNonFragileABIMac::BuildClassRoTInitializer in clang -// class is a metaclass -#define RO_META (1<<0) -// class is a root class -#define RO_ROOT (1<<1) -// class has .cxx_construct/destruct implementations -#define RO_HAS_CXX_STRUCTORS (1<<2) -// class has +load implementation -// #define RO_HAS_LOAD_METHOD (1<<3) -// class has visibility=hidden set -#define RO_HIDDEN (1<<4) -// class has attribute(objc_exception): OBJC_EHTYPE_$_ThisClass is non-weak -#define RO_EXCEPTION (1<<5) -// this bit is available for reassignment -// #define RO_REUSE_ME (1<<6) -// class compiled with ARC -#define RO_IS_ARC (1<<7) -// class has .cxx_destruct but no .cxx_construct (with RO_HAS_CXX_STRUCTORS) -#define RO_HAS_CXX_DTOR_ONLY (1<<8) -// class is not ARC but has ARC-style weak ivar layout -#define RO_HAS_WEAK_WITHOUT_ARC (1<<9) - -// class is in an unloadable bundle - must never be set by compiler -#define RO_FROM_BUNDLE (1<<29) -// class is unrealized future class - must never be set by compiler -#define RO_FUTURE (1<<30) -// class is realized - must never be set by compiler -#define RO_REALIZED (1<<31) - -// Values for class_rw_t->flags -// These are not emitted by the compiler and are never used in class_ro_t. -// Their presence should be considered in future ABI versions. -// class_t->data is class_rw_t, not class_ro_t -#define RW_REALIZED (1<<31) -// class is unresolved future class -#define RW_FUTURE (1<<30) -// class is initialized -#define RW_INITIALIZED (1<<29) -// class is initializing -#define RW_INITIALIZING (1<<28) -// class_rw_t->ro is heap copy of class_ro_t -#define RW_COPIED_RO (1<<27) -// class allocated but not yet registered -#define RW_CONSTRUCTING (1<<26) -// class allocated and registered -#define RW_CONSTRUCTED (1<<25) -// available for use; was RW_FINALIZE_ON_MAIN_THREAD -// #define RW_24 (1<<24) -// class +load has been called -#define RW_LOADED (1<<23) -#if !SUPPORT_NONPOINTER_ISA -// class instances may have associative references -#define RW_INSTANCES_HAVE_ASSOCIATED_OBJECTS (1<<22) -#endif -// class has instance-specific GC layout -#define RW_HAS_INSTANCE_SPECIFIC_LAYOUT (1 << 21) -// available for use -// #define RW_20 (1<<20) -// class has started realizing but not yet completed it -#define RW_REALIZING (1<<19) - -// NOTE: MORE RW_ FLAGS DEFINED BELOW - - -// Values for class_rw_t->flags or class_t->bits -// These flags are optimized for retain/release and alloc/dealloc -// 64-bit stores more of them in class_t->bits to reduce pointer indirection. - -#if !__LP64__ - -// class or superclass has .cxx_construct implementation -#define RW_HAS_CXX_CTOR (1<<18) -// class or superclass has .cxx_destruct implementation -#define RW_HAS_CXX_DTOR (1<<17) -// class or superclass has default alloc/allocWithZone: implementation -// Note this is is stored in the metaclass. -#define RW_HAS_DEFAULT_AWZ (1<<16) -// class's instances requires raw isa -#if SUPPORT_NONPOINTER_ISA -#define RW_REQUIRES_RAW_ISA (1<<15) -#endif - -// class is a Swift class -#define FAST_IS_SWIFT (1UL<<0) -// class or superclass has default retain/release/autorelease/retainCount/ -// _tryRetain/_isDeallocating/retainWeakReference/allowsWeakReference -#define FAST_HAS_DEFAULT_RR (1UL<<1) -// data pointer -#define FAST_DATA_MASK 0xfffffffcUL - -#elif 1 -// Leaks-compatible version that steals low bits only. - -// class or superclass has .cxx_construct implementation -#define RW_HAS_CXX_CTOR (1<<18) -// class or superclass has .cxx_destruct implementation -#define RW_HAS_CXX_DTOR (1<<17) -// class or superclass has default alloc/allocWithZone: implementation -// Note this is is stored in the metaclass. -#define RW_HAS_DEFAULT_AWZ (1<<16) - -// class is a Swift class -#define FAST_IS_SWIFT (1UL<<0) -// class or superclass has default retain/release/autorelease/retainCount/ -// _tryRetain/_isDeallocating/retainWeakReference/allowsWeakReference -#define FAST_HAS_DEFAULT_RR (1UL<<1) -// class's instances requires raw isa -#define FAST_REQUIRES_RAW_ISA (1UL<<2) -// data pointer -#define FAST_DATA_MASK 0x00007ffffffffff8UL - -#else -// Leaks-incompatible version that steals lots of bits. - -// class is a Swift class -#define FAST_IS_SWIFT (1UL<<0) -// class's instances requires raw isa -#define FAST_REQUIRES_RAW_ISA (1UL<<1) -// class or superclass has .cxx_destruct implementation -// This bit is aligned with isa_t->hasCxxDtor to save an instruction. -#define FAST_HAS_CXX_DTOR (1UL<<2) -// data pointer -#define FAST_DATA_MASK 0x00007ffffffffff8UL -// class or superclass has .cxx_construct implementation -#define FAST_HAS_CXX_CTOR (1UL<<47) -// class or superclass has default alloc/allocWithZone: implementation -// Note this is is stored in the metaclass. -#define FAST_HAS_DEFAULT_AWZ (1UL<<48) -// class or superclass has default retain/release/autorelease/retainCount/ -// _tryRetain/_isDeallocating/retainWeakReference/allowsWeakReference -#define FAST_HAS_DEFAULT_RR (1UL<<49) -// summary bit for fast alloc path: !hasCxxCtor and -// !instancesRequireRawIsa and instanceSize fits into shiftedSize -#define FAST_ALLOC (1UL<<50) -// instance size in units of 16 bytes -// or 0 if the instance size is too big in this field -// This field must be LAST -#define FAST_SHIFTED_SIZE_SHIFT 51 - -// FAST_ALLOC means -// FAST_HAS_CXX_CTOR is set -// FAST_REQUIRES_RAW_ISA is not set -// FAST_SHIFTED_SIZE is not zero -// FAST_ALLOC does NOT check FAST_HAS_DEFAULT_AWZ because that -// bit is stored on the metaclass. -#define FAST_ALLOC_MASK (FAST_HAS_CXX_CTOR | FAST_REQUIRES_RAW_ISA) -#define FAST_ALLOC_VALUE (0) - -#endif - - struct class_ro_t { uint32_t flags; uint32_t instanceStart; @@ -543,9 +714,33 @@ struct class_ro_t { const uint8_t * weakIvarLayout; property_list_t *baseProperties; + // This field exists only when RO_HAS_SWIFT_INITIALIZER is set. + _objc_swiftMetadataInitializer __ptrauth_objc_method_list_imp _swiftMetadataInitializer_NEVER_USE[0]; + + _objc_swiftMetadataInitializer swiftMetadataInitializer() const { + if (flags & RO_HAS_SWIFT_INITIALIZER) { + return _swiftMetadataInitializer_NEVER_USE[0]; + } else { + return nil; + } + } + method_list_t *baseMethods() const { return baseMethodList; } + + class_ro_t *duplicate() const { + if (flags & RO_HAS_SWIFT_INITIALIZER) { + size_t size = sizeof(*this) + sizeof(_swiftMetadataInitializer_NEVER_USE[0]); + class_ro_t *ro = (class_ro_t *)memdup(this, size); + ro->_swiftMetadataInitializer_NEVER_USE[0] = this->_swiftMetadataInitializer_NEVER_USE[0]; + return ro; + } else { + size_t size = sizeof(*this); + class_ro_t *ro = (class_ro_t *)memdup(this, size); + return ro; + } + } }; @@ -580,12 +775,12 @@ class list_array_tt { protected: class iterator { - List **lists; - List **listsEnd; + List * const *lists; + List * const *listsEnd; typename List::iterator m, mEnd; public: - iterator(List **begin, List **end) + iterator(List *const *begin, List *const *end) : lists(begin), listsEnd(end) { if (begin != end) { @@ -609,10 +804,10 @@ class list_array_tt { } const iterator& operator ++ () { - assert(m != mEnd); + ASSERT(m != mEnd); m++; if (m == mEnd) { - assert(lists != listsEnd); + ASSERT(lists != listsEnd); lists++; if (lists != listsEnd) { m = (*lists)->begin(); @@ -633,7 +828,7 @@ class list_array_tt { return arrayAndFlag & 1; } - array_t *array() { + array_t *array() const { return (array_t *)(arrayAndFlag & ~1); } @@ -642,8 +837,10 @@ class list_array_tt { } public: + list_array_tt() : list(nullptr) { } + list_array_tt(List *l) : list(l) { } - uint32_t count() { + uint32_t count() const { uint32_t result = 0; for (auto lists = beginLists(), end = endLists(); lists != end; @@ -654,12 +851,12 @@ class list_array_tt { return result; } - iterator begin() { + iterator begin() const { return iterator(beginLists(), endLists()); } - iterator end() { - List **e = endLists(); + iterator end() const { + List * const *e = endLists(); return iterator(e, e); } @@ -674,7 +871,7 @@ class list_array_tt { } } - List** beginLists() { + List* const * beginLists() const { if (hasArray()) { return array()->lists; } else { @@ -682,7 +879,7 @@ class list_array_tt { } } - List** endLists() { + List* const * endLists() const { if (hasArray()) { return array()->lists + array()->count; } else if (list) { @@ -762,11 +959,14 @@ class method_array_t : typedef list_array_tt Super; public: - method_list_t **beginCategoryMethodLists() { + method_array_t() : Super() { } + method_array_t(method_list_t *l) : Super(l) { } + + method_list_t * const *beginCategoryMethodLists() const { return beginLists(); } - method_list_t **endCategoryMethodLists(Class cls); + method_list_t * const *endCategoryMethodLists(Class cls) const; method_array_t duplicate() { return Super::duplicate(); @@ -780,6 +980,9 @@ class property_array_t : typedef list_array_tt Super; public: + property_array_t() : Super() { } + property_array_t(property_list_t *l) : Super(l) { } + property_array_t duplicate() { return Super::duplicate(); } @@ -792,46 +995,71 @@ class protocol_array_t : typedef list_array_tt Super; public: + protocol_array_t() : Super() { } + protocol_array_t(protocol_list_t *l) : Super(l) { } + protocol_array_t duplicate() { return Super::duplicate(); } }; +struct class_rw_ext_t { + const class_ro_t *ro; + method_array_t methods; + property_array_t properties; + protocol_array_t protocols; + char *demangledName; + uint32_t version; +}; struct class_rw_t { // Be warned that Symbolication knows the layout of this structure. uint32_t flags; - uint32_t version; + uint16_t witness; +#if SUPPORT_INDEXED_ISA + uint16_t index; +#endif - const class_ro_t *ro; - - method_array_t methods; - property_array_t properties; - protocol_array_t protocols; + explicit_atomic ro_or_rw_ext; Class firstSubclass; Class nextSiblingClass; - char *demangledName; +private: + using ro_or_rw_ext_t = objc::PointerUnion; -#if SUPPORT_INDEXED_ISA - uint32_t index; -#endif + const ro_or_rw_ext_t get_ro_or_rwe() const { + return ro_or_rw_ext_t{ro_or_rw_ext}; + } - void setFlags(uint32_t set) + void set_ro_or_rwe(const class_ro_t *ro) { + ro_or_rw_ext_t{ro}.storeAt(ro_or_rw_ext, memory_order_relaxed); + } + + void set_ro_or_rwe(class_rw_ext_t *rwe, const class_ro_t *ro) { + // the release barrier is so that the class_rw_ext_t::ro initialization + // is visible to lockless readers + rwe->ro = ro; + ro_or_rw_ext_t{rwe}.storeAt(ro_or_rw_ext, memory_order_release); + } + + class_rw_ext_t *extAlloc(const class_ro_t *ro, bool deep = false); + +public: + void setFlags(uint32_t set) { - OSAtomicOr32Barrier(set, &flags); + __c11_atomic_fetch_or((_Atomic(uint32_t) *)&flags, set, __ATOMIC_RELAXED); } void clearFlags(uint32_t clear) { - OSAtomicXor32Barrier(clear, &flags); + __c11_atomic_fetch_and((_Atomic(uint32_t) *)&flags, ~clear, __ATOMIC_RELAXED); } // set and clear must not overlap void changeFlags(uint32_t set, uint32_t clear) { - assert((set & clear) == 0); + ASSERT((set & clear) == 0); uint32_t oldf, newf; do { @@ -839,202 +1067,136 @@ struct class_rw_t { newf = (oldf | set) & ~clear; } while (!OSAtomicCompareAndSwap32Barrier(oldf, newf, (volatile int32_t *)&flags)); } + + class_rw_ext_t *ext() const { + return get_ro_or_rwe().dyn_cast(); + } + + class_rw_ext_t *extAllocIfNeeded() { + auto v = get_ro_or_rwe(); + if (fastpath(v.is())) { + return v.get(); + } else { + return extAlloc(v.get()); + } + } + + class_rw_ext_t *deepCopy(const class_ro_t *ro) { + return extAlloc(ro, true); + } + + const class_ro_t *ro() const { + auto v = get_ro_or_rwe(); + if (slowpath(v.is())) { + return v.get()->ro; + } + return v.get(); + } + + void set_ro(const class_ro_t *ro) { + auto v = get_ro_or_rwe(); + if (v.is()) { + v.get()->ro = ro; + } else { + set_ro_or_rwe(ro); + } + } + + const method_array_t methods() const { + auto v = get_ro_or_rwe(); + if (v.is()) { + return v.get()->methods; + } else { + return method_array_t{v.get()->baseMethods()}; + } + } + + const property_array_t properties() const { + auto v = get_ro_or_rwe(); + if (v.is()) { + return v.get()->properties; + } else { + return property_array_t{v.get()->baseProperties}; + } + } + + const protocol_array_t protocols() const { + auto v = get_ro_or_rwe(); + if (v.is()) { + return v.get()->protocols; + } else { + return protocol_array_t{v.get()->baseProtocols}; + } + } }; struct class_data_bits_t { + friend objc_class; // Values are the FAST_ flags above. uintptr_t bits; private: - bool getBit(uintptr_t bit) + bool getBit(uintptr_t bit) const { return bits & bit; } -#if FAST_ALLOC - static uintptr_t updateFastAlloc(uintptr_t oldBits, uintptr_t change) - { - if (change & FAST_ALLOC_MASK) { - if (((oldBits & FAST_ALLOC_MASK) == FAST_ALLOC_VALUE) && - ((oldBits >> FAST_SHIFTED_SIZE_SHIFT) != 0)) - { - oldBits |= FAST_ALLOC; - } else { - oldBits &= ~FAST_ALLOC; - } - } - return oldBits; - } -#else - static uintptr_t updateFastAlloc(uintptr_t oldBits, uintptr_t change) { - return oldBits; - } -#endif - - void setBits(uintptr_t set) + // Atomically set the bits in `set` and clear the bits in `clear`. + // set and clear must not overlap. + void setAndClearBits(uintptr_t set, uintptr_t clear) { + ASSERT((set & clear) == 0); uintptr_t oldBits; uintptr_t newBits; do { oldBits = LoadExclusive(&bits); - newBits = updateFastAlloc(oldBits | set, set); + newBits = (oldBits | set) & ~clear; } while (!StoreReleaseExclusive(&bits, oldBits, newBits)); } - void clearBits(uintptr_t clear) - { - uintptr_t oldBits; - uintptr_t newBits; - do { - oldBits = LoadExclusive(&bits); - newBits = updateFastAlloc(oldBits & ~clear, clear); - } while (!StoreReleaseExclusive(&bits, oldBits, newBits)); + void setBits(uintptr_t set) { + __c11_atomic_fetch_or((_Atomic(uintptr_t) *)&bits, set, __ATOMIC_RELAXED); + } + + void clearBits(uintptr_t clear) { + __c11_atomic_fetch_and((_Atomic(uintptr_t) *)&bits, ~clear, __ATOMIC_RELAXED); } public: - class_rw_t* data() { + class_rw_t* data() const { return (class_rw_t *)(bits & FAST_DATA_MASK); } void setData(class_rw_t *newData) { - assert(!data() || (newData->flags & (RW_REALIZING | RW_FUTURE))); + ASSERT(!data() || (newData->flags & (RW_REALIZING | RW_FUTURE))); // Set during realization or construction only. No locking needed. - bits = (bits & ~FAST_DATA_MASK) | (uintptr_t)newData; + // Use a store-release fence because there may be concurrent + // readers of data and data's contents. + uintptr_t newBits = (bits & ~FAST_DATA_MASK) | (uintptr_t)newData; + atomic_thread_fence(memory_order_release); + bits = newBits; } - bool hasDefaultRR() { - return getBit(FAST_HAS_DEFAULT_RR); - } - void setHasDefaultRR() { - setBits(FAST_HAS_DEFAULT_RR); - } - void setHasCustomRR() { - clearBits(FAST_HAS_DEFAULT_RR); - } - -#if FAST_HAS_DEFAULT_AWZ - bool hasDefaultAWZ() { - return getBit(FAST_HAS_DEFAULT_AWZ); - } - void setHasDefaultAWZ() { - setBits(FAST_HAS_DEFAULT_AWZ); - } - void setHasCustomAWZ() { - clearBits(FAST_HAS_DEFAULT_AWZ); - } -#else - bool hasDefaultAWZ() { - return data()->flags & RW_HAS_DEFAULT_AWZ; - } - void setHasDefaultAWZ() { - data()->setFlags(RW_HAS_DEFAULT_AWZ); - } - void setHasCustomAWZ() { - data()->clearFlags(RW_HAS_DEFAULT_AWZ); - } -#endif - -#if FAST_HAS_CXX_CTOR - bool hasCxxCtor() { - return getBit(FAST_HAS_CXX_CTOR); - } - void setHasCxxCtor() { - setBits(FAST_HAS_CXX_CTOR); - } -#else - bool hasCxxCtor() { - return data()->flags & RW_HAS_CXX_CTOR; - } - void setHasCxxCtor() { - data()->setFlags(RW_HAS_CXX_CTOR); - } -#endif - -#if FAST_HAS_CXX_DTOR - bool hasCxxDtor() { - return getBit(FAST_HAS_CXX_DTOR); - } - void setHasCxxDtor() { - setBits(FAST_HAS_CXX_DTOR); - } -#else - bool hasCxxDtor() { - return data()->flags & RW_HAS_CXX_DTOR; - } - void setHasCxxDtor() { - data()->setFlags(RW_HAS_CXX_DTOR); - } -#endif - -#if FAST_REQUIRES_RAW_ISA - bool instancesRequireRawIsa() { - return getBit(FAST_REQUIRES_RAW_ISA); - } - void setInstancesRequireRawIsa() { - setBits(FAST_REQUIRES_RAW_ISA); - } -#elif SUPPORT_NONPOINTER_ISA - bool instancesRequireRawIsa() { - return data()->flags & RW_REQUIRES_RAW_ISA; - } - void setInstancesRequireRawIsa() { - data()->setFlags(RW_REQUIRES_RAW_ISA); - } -#else - bool instancesRequireRawIsa() { - return true; - } - void setInstancesRequireRawIsa() { - // nothing - } -#endif - -#if FAST_ALLOC - size_t fastInstanceSize() - { - assert(bits & FAST_ALLOC); - return (bits >> FAST_SHIFTED_SIZE_SHIFT) * 16; - } - void setFastInstanceSize(size_t newSize) - { - // Set during realization or construction only. No locking needed. - assert(data()->flags & RW_REALIZING); - - // Round up to 16-byte boundary, then divide to get 16-byte units - newSize = ((newSize + 15) & ~15) / 16; - - uintptr_t newBits = newSize << FAST_SHIFTED_SIZE_SHIFT; - if ((newBits >> FAST_SHIFTED_SIZE_SHIFT) == newSize) { - int shift = WORD_BITS - FAST_SHIFTED_SIZE_SHIFT; - uintptr_t oldBits = (bits << shift) >> shift; - if ((oldBits & FAST_ALLOC_MASK) == FAST_ALLOC_VALUE) { - newBits |= FAST_ALLOC; - } - bits = oldBits | newBits; + // Get the class's ro data, even in the presence of concurrent realization. + // fixme this isn't really safe without a compiler barrier at least + // and probably a memory barrier when realizeClass changes the data field + const class_ro_t *safe_ro() { + class_rw_t *maybe_rw = data(); + if (maybe_rw->flags & RW_REALIZED) { + // maybe_rw is rw + return maybe_rw->ro(); + } else { + // maybe_rw is actually ro + return (class_ro_t *)maybe_rw; } } - bool canAllocFast() { - return bits & FAST_ALLOC; - } -#else - size_t fastInstanceSize() { - abort(); - } - void setFastInstanceSize(size_t) { - // nothing - } - bool canAllocFast() { - return false; - } -#endif - void setClassArrayIndex(unsigned Idx) { #if SUPPORT_INDEXED_ISA // 0 is unused as then we can rely on zero-initialisation from calloc. - assert(Idx > 0); + ASSERT(Idx > 0); data()->index = Idx; #endif } @@ -1047,12 +1209,34 @@ public: #endif } - bool isSwift() { - return getBit(FAST_IS_SWIFT); + bool isAnySwift() { + return isSwiftStable() || isSwiftLegacy(); } - void setIsSwift() { - setBits(FAST_IS_SWIFT); + bool isSwiftStable() { + return getBit(FAST_IS_SWIFT_STABLE); + } + void setIsSwiftStable() { + setAndClearBits(FAST_IS_SWIFT_STABLE, FAST_IS_SWIFT_LEGACY); + } + + bool isSwiftLegacy() { + return getBit(FAST_IS_SWIFT_LEGACY); + } + void setIsSwiftLegacy() { + setAndClearBits(FAST_IS_SWIFT_LEGACY, FAST_IS_SWIFT_STABLE); + } + + // fixme remove this once the Swift runtime uses the stable bits + bool isSwiftStable_ButAllowLegacyForNow() { + return isAnySwift(); + } + + _objc_swiftMetadataInitializer swiftMetadataInitializer() { + // This function is called on un-realized classes without + // holding any locks. + // Beware of races with other realizers. + return safe_ro()->swiftMetadataInitializer(); } }; @@ -1063,7 +1247,7 @@ struct objc_class : objc_object { cache_t cache; // formerly cache pointer and vtable class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags - class_rw_t *data() { + class_rw_t *data() const { return bits.data(); } void setData(class_rw_t *newData) { @@ -1071,106 +1255,234 @@ struct objc_class : objc_object { } void setInfo(uint32_t set) { - assert(isFuture() || isRealized()); + ASSERT(isFuture() || isRealized()); data()->setFlags(set); } void clearInfo(uint32_t clear) { - assert(isFuture() || isRealized()); + ASSERT(isFuture() || isRealized()); data()->clearFlags(clear); } // set and clear must not overlap void changeInfo(uint32_t set, uint32_t clear) { - assert(isFuture() || isRealized()); - assert((set & clear) == 0); + ASSERT(isFuture() || isRealized()); + ASSERT((set & clear) == 0); data()->changeFlags(set, clear); } - bool hasCustomRR() { - return ! bits.hasDefaultRR(); +#if FAST_HAS_DEFAULT_RR + bool hasCustomRR() const { + return !bits.getBit(FAST_HAS_DEFAULT_RR); } void setHasDefaultRR() { - assert(isInitializing()); - bits.setHasDefaultRR(); + bits.setBits(FAST_HAS_DEFAULT_RR); } - void setHasCustomRR(bool inherited = false); - void printCustomRR(bool inherited); + void setHasCustomRR() { + bits.clearBits(FAST_HAS_DEFAULT_RR); + } +#else + bool hasCustomRR() const { + return !(bits.data()->flags & RW_HAS_DEFAULT_RR); + } + void setHasDefaultRR() { + bits.data()->setFlags(RW_HAS_DEFAULT_RR); + } + void setHasCustomRR() { + bits.data()->clearFlags(RW_HAS_DEFAULT_RR); + } +#endif - bool hasCustomAWZ() { - return ! bits.hasDefaultAWZ(); +#if FAST_CACHE_HAS_DEFAULT_AWZ + bool hasCustomAWZ() const { + return !cache.getBit(FAST_CACHE_HAS_DEFAULT_AWZ); } void setHasDefaultAWZ() { - assert(isInitializing()); - bits.setHasDefaultAWZ(); + cache.setBit(FAST_CACHE_HAS_DEFAULT_AWZ); } - void setHasCustomAWZ(bool inherited = false); - void printCustomAWZ(bool inherited); + void setHasCustomAWZ() { + cache.clearBit(FAST_CACHE_HAS_DEFAULT_AWZ); + } +#else + bool hasCustomAWZ() const { + return !(bits.data()->flags & RW_HAS_DEFAULT_AWZ); + } + void setHasDefaultAWZ() { + bits.data()->setFlags(RW_HAS_DEFAULT_AWZ); + } + void setHasCustomAWZ() { + bits.data()->clearFlags(RW_HAS_DEFAULT_AWZ); + } +#endif - bool instancesRequireRawIsa() { - return bits.instancesRequireRawIsa(); +#if FAST_CACHE_HAS_DEFAULT_CORE + bool hasCustomCore() const { + return !cache.getBit(FAST_CACHE_HAS_DEFAULT_CORE); } - void setInstancesRequireRawIsa(bool inherited = false); + void setHasDefaultCore() { + return cache.setBit(FAST_CACHE_HAS_DEFAULT_CORE); + } + void setHasCustomCore() { + return cache.clearBit(FAST_CACHE_HAS_DEFAULT_CORE); + } +#else + bool hasCustomCore() const { + return !(bits.data()->flags & RW_HAS_DEFAULT_CORE); + } + void setHasDefaultCore() { + bits.data()->setFlags(RW_HAS_DEFAULT_CORE); + } + void setHasCustomCore() { + bits.data()->clearFlags(RW_HAS_DEFAULT_CORE); + } +#endif + +#if FAST_CACHE_HAS_CXX_CTOR + bool hasCxxCtor() { + ASSERT(isRealized()); + return cache.getBit(FAST_CACHE_HAS_CXX_CTOR); + } + void setHasCxxCtor() { + cache.setBit(FAST_CACHE_HAS_CXX_CTOR); + } +#else + bool hasCxxCtor() { + ASSERT(isRealized()); + return bits.data()->flags & RW_HAS_CXX_CTOR; + } + void setHasCxxCtor() { + bits.data()->setFlags(RW_HAS_CXX_CTOR); + } +#endif + +#if FAST_CACHE_HAS_CXX_DTOR + bool hasCxxDtor() { + ASSERT(isRealized()); + return cache.getBit(FAST_CACHE_HAS_CXX_DTOR); + } + void setHasCxxDtor() { + cache.setBit(FAST_CACHE_HAS_CXX_DTOR); + } +#else + bool hasCxxDtor() { + ASSERT(isRealized()); + return bits.data()->flags & RW_HAS_CXX_DTOR; + } + void setHasCxxDtor() { + bits.data()->setFlags(RW_HAS_CXX_DTOR); + } +#endif + +#if FAST_CACHE_REQUIRES_RAW_ISA + bool instancesRequireRawIsa() { + return cache.getBit(FAST_CACHE_REQUIRES_RAW_ISA); + } + void setInstancesRequireRawIsa() { + cache.setBit(FAST_CACHE_REQUIRES_RAW_ISA); + } +#elif SUPPORT_NONPOINTER_ISA + bool instancesRequireRawIsa() { + return bits.data()->flags & RW_REQUIRES_RAW_ISA; + } + void setInstancesRequireRawIsa() { + bits.data()->setFlags(RW_REQUIRES_RAW_ISA); + } +#else + bool instancesRequireRawIsa() { + return true; + } + void setInstancesRequireRawIsa() { + // nothing + } +#endif + void setInstancesRequireRawIsaRecursively(bool inherited = false); void printInstancesRequireRawIsa(bool inherited); bool canAllocNonpointer() { - assert(!isFuture()); + ASSERT(!isFuture()); return !instancesRequireRawIsa(); } - bool canAllocFast() { - assert(!isFuture()); - return bits.canAllocFast(); + + bool isSwiftStable() { + return bits.isSwiftStable(); } - - bool hasCxxCtor() { - // addSubclass() propagates this flag from the superclass. - assert(isRealized()); - return bits.hasCxxCtor(); - } - void setHasCxxCtor() { - bits.setHasCxxCtor(); + bool isSwiftLegacy() { + return bits.isSwiftLegacy(); } - bool hasCxxDtor() { - // addSubclass() propagates this flag from the superclass. - assert(isRealized()); - return bits.hasCxxDtor(); - } - void setHasCxxDtor() { - bits.setHasCxxDtor(); + bool isAnySwift() { + return bits.isAnySwift(); } - - bool isSwift() { - return bits.isSwift(); + bool isSwiftStable_ButAllowLegacyForNow() { + return bits.isSwiftStable_ButAllowLegacyForNow(); } + bool isStubClass() const { + uintptr_t isa = (uintptr_t)isaBits(); + return 1 <= isa && isa < 16; + } + + // Swift stable ABI built for old deployment targets looks weird. + // The is-legacy bit is set for compatibility with old libobjc. + // We are on a "new" deployment target so we need to rewrite that bit. + // These stable-with-legacy-bit classes are distinguished from real + // legacy classes using another bit in the Swift data + // (ClassFlags::IsSwiftPreStableABI) + + bool isUnfixedBackwardDeployingStableSwift() { + // Only classes marked as Swift legacy need apply. + if (!bits.isSwiftLegacy()) return false; + + // Check the true legacy vs stable distinguisher. + // The low bit of Swift's ClassFlags is SET for true legacy + // and UNSET for stable pretending to be legacy. + uint32_t swiftClassFlags = *(uint32_t *)(&bits + 1); + bool isActuallySwiftLegacy = bool(swiftClassFlags & 1); + return !isActuallySwiftLegacy; + } + + void fixupBackwardDeployingStableSwift() { + if (isUnfixedBackwardDeployingStableSwift()) { + // Class really is stable Swift, pretending to be pre-stable. + // Fix its lie. + bits.setIsSwiftStable(); + } + } + + _objc_swiftMetadataInitializer swiftMetadataInitializer() { + return bits.swiftMetadataInitializer(); + } // Return YES if the class's ivars are managed by ARC, // or the class is MRC but has ARC-style weak ivars. bool hasAutomaticIvars() { - return data()->ro->flags & (RO_IS_ARC | RO_HAS_WEAK_WITHOUT_ARC); + return data()->ro()->flags & (RO_IS_ARC | RO_HAS_WEAK_WITHOUT_ARC); } // Return YES if the class's ivars are managed by ARC. bool isARC() { - return data()->ro->flags & RO_IS_ARC; + return data()->ro()->flags & RO_IS_ARC; } + bool forbidsAssociatedObjects() { + return (data()->flags & RW_FORBIDS_ASSOCIATED_OBJECTS); + } + #if SUPPORT_NONPOINTER_ISA // Tracked in non-pointer isas; not tracked otherwise #else bool instancesHaveAssociatedObjects() { // this may be an unrealized future class in the CF-bridged case - assert(isFuture() || isRealized()); + ASSERT(isFuture() || isRealized()); return data()->flags & RW_INSTANCES_HAVE_ASSOCIATED_OBJECTS; } void setInstancesHaveAssociatedObjects() { // this may be an unrealized future class in the CF-bridged case - assert(isFuture() || isRealized()); + ASSERT(isFuture() || isRealized()); setInfo(RW_INSTANCES_HAVE_ASSOCIATED_OBJECTS); } #endif @@ -1188,7 +1500,7 @@ struct objc_class : objc_object { } void setInitializing() { - assert(!isMetaClass()); + ASSERT(!isMetaClass()); ISA()->setInfo(RW_INITIALIZING); } @@ -1199,27 +1511,38 @@ struct objc_class : objc_object { void setInitialized(); bool isLoadable() { - assert(isRealized()); + ASSERT(isRealized()); return true; // any class registered for +load is definitely loadable } IMP getLoadMethod(); // Locking: To prevent concurrent realization, hold runtimeLock. - bool isRealized() { - return data()->flags & RW_REALIZED; + bool isRealized() const { + return !isStubClass() && (data()->flags & RW_REALIZED); } // Returns true if this is an unrealized future class. // Locking: To prevent concurrent realization, hold runtimeLock. - bool isFuture() { + bool isFuture() const { return data()->flags & RW_FUTURE; } bool isMetaClass() { - assert(this); - assert(isRealized()); - return data()->ro->flags & RO_META; + ASSERT(this); + ASSERT(isRealized()); +#if FAST_CACHE_META + return cache.getBit(FAST_CACHE_META); +#else + return data()->flags & RW_META; +#endif + } + + // Like isMetaClass, but also valid on un-realized classes + bool isMetaClassMaybeUnrealized() { + static_assert(offsetof(class_rw_t, flags) == offsetof(class_ro_t, flags), "flags alias"); + static_assert(RO_META == RW_META, "flags alias"); + return data()->flags & RW_META; } // NOT identical to this->ISA when this is a metaclass @@ -1237,42 +1560,46 @@ struct objc_class : objc_object { const char *mangledName() { // fixme can't assert locks here - assert(this); + ASSERT(this); if (isRealized() || isFuture()) { - return data()->ro->name; + return data()->ro()->name; } else { return ((const class_ro_t *)data())->name; } } - const char *demangledName(bool realize = false); + const char *demangledName(bool needsLock); const char *nameForLogging(); // May be unaligned depending on class's ivars. - uint32_t unalignedInstanceStart() { - assert(isRealized()); - return data()->ro->instanceStart; + uint32_t unalignedInstanceStart() const { + ASSERT(isRealized()); + return data()->ro()->instanceStart; } // Class's instance start rounded up to a pointer-size boundary. // This is used for ARC layout bitmaps. - uint32_t alignedInstanceStart() { + uint32_t alignedInstanceStart() const { return word_align(unalignedInstanceStart()); } // May be unaligned depending on class's ivars. - uint32_t unalignedInstanceSize() { - assert(isRealized()); - return data()->ro->instanceSize; + uint32_t unalignedInstanceSize() const { + ASSERT(isRealized()); + return data()->ro()->instanceSize; } // Class's ivar size rounded up to a pointer-size boundary. - uint32_t alignedInstanceSize() { + uint32_t alignedInstanceSize() const { return word_align(unalignedInstanceSize()); } - size_t instanceSize(size_t extraBytes) { + size_t instanceSize(size_t extraBytes) const { + if (fastpath(cache.hasFastInstanceSize(extraBytes))) { + return cache.fastInstanceSize(extraBytes); + } + size_t size = alignedInstanceSize() + extraBytes; // CF requires all objects be at least 16 bytes. if (size < 16) size = 16; @@ -1280,12 +1607,14 @@ struct objc_class : objc_object { } void setInstanceSize(uint32_t newSize) { - assert(isRealized()); - if (newSize != data()->ro->instanceSize) { - assert(data()->flags & RW_COPIED_RO); - *const_cast(&data()->ro->instanceSize) = newSize; + ASSERT(isRealized()); + ASSERT(data()->flags & RW_REALIZING); + auto ro = data()->ro(); + if (newSize != ro->instanceSize) { + ASSERT(data()->flags & RW_COPIED_RO); + *const_cast(&ro->instanceSize) = newSize; } - bits.setFastInstanceSize(newSize); + cache.setFastInstanceSize(newSize); } void chooseClassArrayIndex(); @@ -1297,7 +1626,6 @@ struct objc_class : objc_object { unsigned classArrayIndex() { return bits.classArrayIndex(); } - }; @@ -1335,6 +1663,11 @@ struct category_t { } property_list_t *propertiesForMeta(bool isMeta, struct header_info *hi); + + protocol_list_t *protocolsForMeta(bool isMeta) { + if (isMeta) return nullptr; + else return protocols; + } }; struct objc_super2 { @@ -1350,50 +1683,4 @@ struct message_ref_t { extern Method protocol_getMethod(protocol_t *p, SEL sel, bool isRequiredMethod, bool isInstanceMethod, bool recursive); -static inline void -foreach_realized_class_and_subclass_2(Class top, bool (^code)(Class)) -{ - // runtimeLock.assertWriting(); - assert(top); - Class cls = top; - while (1) { - if (!code(cls)) break; - - if (cls->data()->firstSubclass) { - cls = cls->data()->firstSubclass; - } else { - while (!cls->data()->nextSiblingClass && cls != top) { - cls = cls->superclass; - } - if (cls == top) break; - cls = cls->data()->nextSiblingClass; - } - } -} - -// Enumerates a class and all of its realized subclasses. -static inline void -foreach_realized_class_and_subclass(Class top, void (^code)(Class)) -{ - foreach_realized_class_and_subclass_2(top, ^bool(Class cls) { - code(cls); return true; - }); -} - -// Enumerates all realized classes and metaclasses. -extern Class firstRealizedClass(); -static inline void -foreach_realized_class_and_metaclass(void (^code)(Class)) -{ - for (Class top = firstRealizedClass(); - top != nil; - top = top->data()->nextSiblingClass) - { - foreach_realized_class_and_subclass_2(top, ^bool(Class cls) { - code(cls); return true; - }); - } - -} - #endif diff --git a/runtime/objc-runtime-new.mm b/runtime/objc-runtime-new.mm index b366c19..e158375 100644 --- a/runtime/objc-runtime-new.mm +++ b/runtime/objc-runtime-new.mm @@ -28,10 +28,12 @@ #if __OBJC2__ +#include "DenseMapExtras.h" #include "objc-private.h" #include "objc-runtime-new.h" #include "objc-file.h" #include "objc-cache.h" +#include "objc-zalloc.h" #include #include #include @@ -41,59 +43,47 @@ static void disableTaggedPointers(); static void detach_class(Class cls, bool isMeta); static void free_class(Class cls); -static Class setSuperclass(Class cls, Class newSuper); -static Class realizeClass(Class cls); -static method_t *getMethodNoSuper_nolock(Class cls, SEL sel); -static method_t *getMethod_nolock(Class cls, SEL sel); static IMP addMethod(Class cls, SEL name, IMP imp, const char *types, bool replace); -static bool isRRSelector(SEL sel); -static bool isAWZSelector(SEL sel); -static bool methodListImplementsRR(const method_list_t *mlist); -static bool methodListImplementsAWZ(const method_list_t *mlist); -static void updateCustomRR_AWZ(Class cls, method_t *meth); +static void adjustCustomFlagsForMethodChange(Class cls, method_t *meth); static method_t *search_method_list(const method_list_t *mlist, SEL sel); +static bool method_lists_contains_any(method_list_t * const *mlists, method_list_t * const *end, + SEL sels[], size_t selcount); static void flushCaches(Class cls); +static void initializeTaggedPointerObfuscator(void); #if SUPPORT_FIXUP static void fixupMessageRef(message_ref_t *msg); #endif +static Class realizeClassMaybeSwiftAndUnlock(Class cls, mutex_t& lock); +static Class readClass(Class cls, bool headerIsBundle, bool headerIsPreoptimized); -static bool MetaclassNSObjectAWZSwizzled; -static bool ClassNSObjectRRSwizzled; - -id objc_noop_imp(id self, SEL _cmd __unused) { - return self; -} +struct locstamped_category_t { + category_t *cat; + struct header_info *hi; +}; +enum { + ATTACH_CLASS = 1 << 0, + ATTACH_METACLASS = 1 << 1, + ATTACH_CLASS_AND_METACLASS = 1 << 2, + ATTACH_EXISTING = 1 << 3, +}; +static void attachCategories(Class cls, const struct locstamped_category_t *cats_list, uint32_t cats_count, int flags); /*********************************************************************** * Lock management **********************************************************************/ -rwlock_t runtimeLock; -rwlock_t selLock; +mutex_t runtimeLock; +mutex_t selLock; +#if CONFIG_USE_CACHE_LOCK mutex_t cacheUpdateLock; +#endif recursive_mutex_t loadMethodLock; -#if SUPPORT_QOS_HACK -pthread_priority_t BackgroundPriority = 0; -pthread_priority_t MainPriority = 0; -# if DEBUG -static __unused void destroyQOSKey(void *arg) { - _objc_fatal("QoS override level at thread exit is %zu instead of zero", - (size_t)(uintptr_t)arg); -} -# endif -#endif +/*********************************************************************** +* Class structure decoding +**********************************************************************/ -void lock_init(void) -{ -#if SUPPORT_QOS_HACK - BackgroundPriority = _pthread_qos_class_encode(QOS_CLASS_BACKGROUND, 0, 0); - MainPriority = _pthread_qos_class_encode(qos_class_main(), 0, 0); -# if DEBUG - pthread_key_init_np(QOS_KEY, &destroyQOSKey); -# endif -#endif -} +const uintptr_t objc_debug_class_rw_data_mask = FAST_DATA_MASK; /*********************************************************************** @@ -188,9 +178,40 @@ const uintptr_t objc_debug_isa_magic_value = 0; #endif -typedef locstamped_category_list_t category_list; +/*********************************************************************** +* Swift marker bits +**********************************************************************/ +const uintptr_t objc_debug_swift_stable_abi_bit = FAST_IS_SWIFT_STABLE; +/*********************************************************************** +* allocatedClasses +* A table of all classes (and metaclasses) which have been allocated +* with objc_allocateClassPair. +**********************************************************************/ +namespace objc { +static ExplicitInitDenseSet allocatedClasses; +} + +/*********************************************************************** +* _firstRealizedClass +* The root of all realized classes +**********************************************************************/ +static Class _firstRealizedClass = nil; + +/*********************************************************************** +* didInitialAttachCategories +* Whether the initial attachment of categories present at startup has +* been done. +**********************************************************************/ +static bool didInitialAttachCategories = false; + +/*********************************************************************** +* didCallDyldNotifyRegister +* Whether the call to _dyld_objc_notify_register has completed. +**********************************************************************/ +bool didCallDyldNotifyRegister = false; + /* Low two bits of mlist->entsize is used as the fixed-up marker. PREOPTIMIZED VERSION: @@ -214,13 +235,26 @@ typedef locstamped_category_list_t category_list; */ static uint32_t fixed_up_method_list = 3; +static uint32_t uniqued_method_list = 1; static uint32_t fixed_up_protocol = PROTOCOL_FIXED_UP_1; +static uint32_t canonical_protocol = PROTOCOL_IS_CANONICAL; void disableSharedCacheOptimizations(void) { fixed_up_method_list = 2; + // It is safe to set uniqued method lists to 0 as we'll never call it unless + // the method list was already in need of being fixed up + uniqued_method_list = 0; fixed_up_protocol = PROTOCOL_FIXED_UP_1 | PROTOCOL_FIXED_UP_2; + // Its safe to just set canonical protocol to 0 as we'll never call + // clearIsCanonical() unless isCanonical() returned true, which can't happen + // with a 0 mask + canonical_protocol = 0; +} + +bool method_list_t::isUniqued() const { + return (flags() & uniqued_method_list) != 0; } bool method_list_t::isFixedUp() const { @@ -228,8 +262,8 @@ bool method_list_t::isFixedUp() const { } void method_list_t::setFixedUp() { - runtimeLock.assertWriting(); - assert(!isFixedUp()); + runtimeLock.assertLocked(); + ASSERT(!isFixedUp()); entsizeAndFlags = entsize() | fixed_up_method_list; } @@ -238,18 +272,28 @@ bool protocol_t::isFixedUp() const { } void protocol_t::setFixedUp() { - runtimeLock.assertWriting(); - assert(!isFixedUp()); + runtimeLock.assertLocked(); + ASSERT(!isFixedUp()); flags = (flags & ~PROTOCOL_FIXED_UP_MASK) | fixed_up_protocol; } +bool protocol_t::isCanonical() const { + return (flags & canonical_protocol) != 0; +} -method_list_t **method_array_t::endCategoryMethodLists(Class cls) +void protocol_t::clearIsCanonical() { + runtimeLock.assertLocked(); + ASSERT(isCanonical()); + flags = flags & ~canonical_protocol; +} + + +method_list_t * const *method_array_t::endCategoryMethodLists(Class cls) const { - method_list_t **mlists = beginLists(); - method_list_t **mlistsEnd = endLists(); + auto mlists = beginLists(); + auto mlistsEnd = endLists(); - if (mlists == mlistsEnd || !cls->data()->ro->baseMethods()) + if (mlists == mlistsEnd || !cls->data()->ro()->baseMethods()) { // No methods, or no base methods. // Everything here is a category method. @@ -279,10 +323,22 @@ static void try_free(const void *p) } +using ClassCopyFixupHandler = void (*)(Class _Nonnull oldClass, + Class _Nonnull newClass); +// Normally there's only one handler registered. +static GlobalSmallVector classCopyFixupHandlers; + +void _objc_setClassCopyFixupHandler(void (* _Nonnull newFixupHandler) + (Class _Nonnull oldClass, Class _Nonnull newClass)) { + mutex_locker_t lock(runtimeLock); + + classCopyFixupHandlers.append(newFixupHandler); +} + static Class alloc_class_for_subclass(Class supercls, size_t extraBytes) { - if (!supercls || !supercls->isSwift()) { + if (!supercls || !supercls->isAnySwift()) { return _calloc_class(sizeof(objc_class) + extraBytes); } @@ -304,8 +360,17 @@ alloc_class_for_subclass(Class supercls, size_t extraBytes) bzero(swcls, sizeof(objc_class)); swcls->description = nil; + for (auto handler : classCopyFixupHandlers) { + handler(supercls, (Class)swcls); + } + // Mark this class as Swift-enhanced. - swcls->bits.setIsSwift(); + if (supercls->isSwiftStable()) { + swcls->bits.setIsSwiftStable(); + } + if (supercls->isSwiftLegacy()) { + swcls->bits.setIsSwiftLegacy(); + } return (Class)swcls; } @@ -324,7 +389,7 @@ void *object_getIndexedIvars(id obj) if (!obj->isClass()) return base + obj->ISA()->alignedInstanceSize(); Class cls = (Class)obj; - if (!cls->isSwift()) return base + sizeof(objc_class); + if (!cls->isAnySwift()) return base + sizeof(objc_class); swift_class_t *swcls = (swift_class_t *)cls; return base - swcls->classAddressOffset + word_align(swcls->classSize); @@ -338,125 +403,100 @@ void *object_getIndexedIvars(id obj) **********************************************************************/ static class_ro_t *make_ro_writeable(class_rw_t *rw) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); if (rw->flags & RW_COPIED_RO) { // already writeable, do nothing } else { - class_ro_t *ro = (class_ro_t *) - memdup(rw->ro, sizeof(*rw->ro)); - rw->ro = ro; + rw->set_ro(rw->ro()->duplicate()); rw->flags |= RW_COPIED_RO; } - return (class_ro_t *)rw->ro; + return const_cast(rw->ro()); } /*********************************************************************** -* unattachedCategories -* Returns the class => categories map of unattached categories. -* Locking: runtimeLock must be held by the caller. +* dataSegmentsContain +* Returns true if the given address lies within a data segment in any +* loaded image. **********************************************************************/ -static NXMapTable *unattachedCategories(void) +NEVER_INLINE +static bool +dataSegmentsContain(Class cls) { - runtimeLock.assertWriting(); - - static NXMapTable *category_map = nil; - - if (category_map) return category_map; - - // fixme initial map size - category_map = NXCreateMapTable(NXPtrValueMapPrototype, 16); - - return category_map; -} - - -/*********************************************************************** -* addUnattachedCategoryForClass -* Records an unattached category. -* Locking: runtimeLock must be held by the caller. -**********************************************************************/ -static void addUnattachedCategoryForClass(category_t *cat, Class cls, - header_info *catHeader) -{ - runtimeLock.assertWriting(); - - // DO NOT use cat->cls! cls may be cat->cls->isa instead - NXMapTable *cats = unattachedCategories(); - category_list *list; - - list = (category_list *)NXMapGet(cats, cls); - if (!list) { - list = (category_list *) - calloc(sizeof(*list) + sizeof(list->list[0]), 1); - } else { - list = (category_list *) - realloc(list, sizeof(*list) + sizeof(list->list[0]) * (list->count + 1)); - } - list->list[list->count++] = (locstamped_category_t){cat, catHeader}; - NXMapInsert(cats, cls, list); -} - - -/*********************************************************************** -* removeUnattachedCategoryForClass -* Removes an unattached category. -* Locking: runtimeLock must be held by the caller. -**********************************************************************/ -static void removeUnattachedCategoryForClass(category_t *cat, Class cls) -{ - runtimeLock.assertWriting(); - - // DO NOT use cat->cls! cls may be cat->cls->isa instead - NXMapTable *cats = unattachedCategories(); - category_list *list; - - list = (category_list *)NXMapGet(cats, cls); - if (!list) return; - - uint32_t i; - for (i = 0; i < list->count; i++) { - if (list->list[i].cat == cat) { - // shift entries to preserve list order - memmove(&list->list[i], &list->list[i+1], - (list->count-i-1) * sizeof(list->list[i])); - list->count--; - return; + uint32_t index; + if (objc::dataSegmentsRanges.find((uintptr_t)cls, index)) { + // if the class is realized (hence has a class_rw_t), + // memorize where we found the range + if (cls->isRealized()) { + cls->data()->witness = (uint16_t)index; } + return true; + } + return false; +} + + +/*********************************************************************** +* isKnownClass +* Return true if the class is known to the runtime (located within the +* shared cache, within the data segment of a loaded image, or has been +* allocated with obj_allocateClassPair). +* +* The result of this operation is cached on the class in a "witness" +* value that is cheaply checked in the fastpath. +**********************************************************************/ +ALWAYS_INLINE +static bool +isKnownClass(Class cls) +{ + if (fastpath(objc::dataSegmentsRanges.contains(cls->data()->witness, (uintptr_t)cls))) { + return true; + } + auto &set = objc::allocatedClasses.get(); + return set.find(cls) != set.end() || dataSegmentsContain(cls); +} + + +/*********************************************************************** +* addClassTableEntry +* Add a class to the table of all classes. If addMeta is true, +* automatically adds the metaclass of the class as well. +* Locking: runtimeLock must be held by the caller. +**********************************************************************/ +static void +addClassTableEntry(Class cls, bool addMeta = true) +{ + runtimeLock.assertLocked(); + + // This class is allowed to be a known class via the shared cache or via + // data segments, but it is not allowed to be in the dynamic table already. + auto &set = objc::allocatedClasses.get(); + + ASSERT(set.find(cls) == set.end()); + + if (!isKnownClass(cls)) + set.insert(cls); + if (addMeta) + addClassTableEntry(cls->ISA(), false); +} + + +/*********************************************************************** +* checkIsKnownClass +* Checks the given class against the list of all known classes. Dies +* with a fatal error if the class is not known. +* Locking: runtimeLock must be held by the caller. +**********************************************************************/ +ALWAYS_INLINE +static void +checkIsKnownClass(Class cls) +{ + if (slowpath(!isKnownClass(cls))) { + _objc_fatal("Attempt to use unknown class %p.", cls); } } - -/*********************************************************************** -* unattachedCategoriesForClass -* Returns the list of unattached categories for a class, and -* deletes them from the list. -* The result must be freed by the caller. -* Locking: runtimeLock must be held by the caller. -**********************************************************************/ -static category_list * -unattachedCategoriesForClass(Class cls, bool realizing) -{ - runtimeLock.assertWriting(); - return (category_list *)NXMapRemove(unattachedCategories(), cls); -} - - -/*********************************************************************** -* removeAllUnattachedCategoriesForClass -* Deletes all unattached categories (loaded or not) for a class. -* Locking: runtimeLock must be held by the caller. -**********************************************************************/ -static void removeAllUnattachedCategoriesForClass(Class cls) -{ - runtimeLock.assertWriting(); - - void *list = NXMapRemove(unattachedCategories(), cls); - if (list) free(list); -} - - /*********************************************************************** * classNSObject * Returns class NSObject. @@ -468,6 +508,11 @@ static Class classNSObject(void) return (Class)&OBJC_CLASS_$_NSObject; } +static Class metaclassNSObject(void) +{ + extern objc_class OBJC_METACLASS_$_NSObject; + return (Class)&OBJC_METACLASS_$_NSObject; +} /*********************************************************************** * printReplacements @@ -475,17 +520,17 @@ static Class classNSObject(void) * Warn about methods from cats that override other methods in cats or cls. * Assumes no methods from cats have been added to cls yet. **********************************************************************/ -static void printReplacements(Class cls, category_list *cats) +__attribute__((cold, noinline)) +static void +printReplacements(Class cls, const locstamped_category_t *cats_list, uint32_t cats_count) { uint32_t c; bool isMeta = cls->isMetaClass(); - if (!cats) return; - // Newest categories are LAST in cats // Later categories override earlier ones. - for (c = 0; c < cats->count; c++) { - category_t *cat = cats->list[c].cat; + for (c = 0; c < cats_count; c++) { + category_t *cat = cats_list[c].cat; method_list_t *mlist = cat->methodsForMeta(isMeta); if (!mlist) continue; @@ -498,7 +543,7 @@ static void printReplacements(Class cls, category_list *cats) // Look for method in earlier categories for (uint32_t c2 = 0; c2 < c; c2++) { - category_t *cat2 = cats->list[c2].cat; + category_t *cat2 = cats_list[c2].cat; const method_list_t *mlist2 = cat2->methodsForMeta(isMeta); if (!mlist2) continue; @@ -515,7 +560,7 @@ static void printReplacements(Class cls, category_list *cats) } // Look for method in cls - for (const auto& meth2 : cls->data()->methods) { + for (const auto& meth2 : cls->data()->methods()) { SEL s2 = sel_registerName(sel_cname(meth2.name)); if (s == s2) { logReplacedMethod(cls->nameForLogging(), s, @@ -532,28 +577,626 @@ static void printReplacements(Class cls, category_list *cats) } +/*********************************************************************** +* unreasonableClassCount +* Provides an upper bound for any iteration of classes, +* to prevent spins when runtime metadata is corrupted. +**********************************************************************/ +static unsigned unreasonableClassCount() +{ + runtimeLock.assertLocked(); + + int base = NXCountMapTable(gdb_objc_realized_classes) + + getPreoptimizedClassUnreasonableCount(); + + // Provide lots of slack here. Some iterations touch metaclasses too. + // Some iterations backtrack (like realized class iteration). + // We don't need an efficient bound, merely one that prevents spins. + return (base + 1) * 16; +} + + +/*********************************************************************** +* Class enumerators +* The passed in block returns `false` if subclasses can be skipped +* Locking: runtimeLock must be held by the caller. +**********************************************************************/ +static inline void +foreach_realized_class_and_subclass_2(Class top, unsigned &count, + bool skip_metaclass, + bool (^code)(Class) __attribute((noescape))) +{ + Class cls = top; + + runtimeLock.assertLocked(); + ASSERT(top); + + while (1) { + if (--count == 0) { + _objc_fatal("Memory corruption in class list."); + } + + bool skip_subclasses; + + if (skip_metaclass && cls->isMetaClass()) { + skip_subclasses = true; + } else { + skip_subclasses = !code(cls); + } + + if (!skip_subclasses && cls->data()->firstSubclass) { + cls = cls->data()->firstSubclass; + } else { + while (!cls->data()->nextSiblingClass && cls != top) { + cls = cls->superclass; + if (--count == 0) { + _objc_fatal("Memory corruption in class list."); + } + } + if (cls == top) break; + cls = cls->data()->nextSiblingClass; + } + } +} + +// Enumerates a class and all of its realized subclasses. +static void +foreach_realized_class_and_subclass(Class top, bool (^code)(Class) __attribute((noescape))) +{ + unsigned int count = unreasonableClassCount(); + + foreach_realized_class_and_subclass_2(top, count, false, code); +} + +// Enumerates all realized classes and metaclasses. +static void +foreach_realized_class_and_metaclass(bool (^code)(Class) __attribute((noescape))) +{ + unsigned int count = unreasonableClassCount(); + + for (Class top = _firstRealizedClass; + top != nil; + top = top->data()->nextSiblingClass) + { + foreach_realized_class_and_subclass_2(top, count, false, code); + } +} + +// Enumerates all realized classes (ignoring metaclasses). +static void +foreach_realized_class(bool (^code)(Class) __attribute((noescape))) +{ + unsigned int count = unreasonableClassCount(); + + for (Class top = _firstRealizedClass; + top != nil; + top = top->data()->nextSiblingClass) + { + foreach_realized_class_and_subclass_2(top, count, true, code); + } +} + + +/*********************************************************************** + * Method Scanners / Optimization tracking + * Implementation of scanning for various implementations of methods. + **********************************************************************/ + +namespace objc { + +enum SelectorBundle { + AWZ, + RR, + Core, +}; + +namespace scanner { + +// The current state of NSObject swizzling for every scanner +// +// It allows for cheap checks of global swizzles, and also lets +// things like IMP Swizzling before NSObject has been initialized +// to be remembered, as setInitialized() would miss these. +// +// Every pair of bits describes a SelectorBundle. +// even bits: is NSObject class swizzled for this bundle +// odd bits: is NSObject meta class swizzled for this bundle +static uintptr_t NSObjectSwizzledMask; + +static ALWAYS_INLINE uintptr_t +swizzlingBit(SelectorBundle bundle, bool isMeta) +{ + return 1UL << (2 * bundle + isMeta); +} + +static void __attribute__((cold, noinline)) +printCustom(Class cls, SelectorBundle bundle, bool inherited) +{ + static char const * const SelectorBundleName[] = { + [AWZ] = "CUSTOM AWZ", + [RR] = "CUSTOM RR", + [Core] = "CUSTOM Core", + }; + + _objc_inform("%s: %s%s%s", SelectorBundleName[bundle], + cls->nameForLogging(), + cls->isMetaClass() ? " (meta)" : "", + inherited ? " (inherited)" : ""); +} + +enum class Scope { Instances, Classes, Both }; + +template +class Mixin { + + // work around compiler being broken with templates using Class/objc_class, + // probably some weird confusion with Class being builtin + ALWAYS_INLINE static objc_class *as_objc_class(Class cls) { + return (objc_class *)cls; + } + + static void + setCustomRecursively(Class cls, bool inherited = false) + { + foreach_realized_class_and_subclass(cls, [=](Class c){ + if (c != cls && !as_objc_class(c)->isInitialized()) { + // Subclass not yet initialized. Wait for setInitialized() to do it + return false; + } + if (Traits::isCustom(c)) { + return false; + } + Traits::setCustom(c); + if (ShouldPrint) { + printCustom(cls, Bundle, inherited || c != cls); + } + return true; + }); + } + + static bool + isNSObjectSwizzled(bool isMeta) + { + return NSObjectSwizzledMask & swizzlingBit(Bundle, isMeta); + } + + static void + setNSObjectSwizzled(Class NSOClass, bool isMeta) + { + NSObjectSwizzledMask |= swizzlingBit(Bundle, isMeta); + if (as_objc_class(NSOClass)->isInitialized()) { + setCustomRecursively(NSOClass); + } + } + + static void + scanChangedMethodForUnknownClass(const method_t *meth) + { + Class cls; + + cls = classNSObject(); + if (Domain != Scope::Classes && !isNSObjectSwizzled(NO)) { + for (const auto &meth2: as_objc_class(cls)->data()->methods()) { + if (meth == &meth2) { + setNSObjectSwizzled(cls, NO); + break; + } + } + } + + cls = metaclassNSObject(); + if (Domain != Scope::Instances && !isNSObjectSwizzled(YES)) { + for (const auto &meth2: as_objc_class(cls)->data()->methods()) { + if (meth == &meth2) { + setNSObjectSwizzled(cls, YES); + break; + } + } + } + } + + static void + scanAddedClassImpl(Class cls, bool isMeta) + { + Class NSOClass = (isMeta ? metaclassNSObject() : classNSObject()); + bool setCustom = NO, inherited = NO; + + if (isNSObjectSwizzled(isMeta)) { + setCustom = YES; + } else if (cls == NSOClass) { + // NSObject is default but we need to check categories + auto &methods = as_objc_class(cls)->data()->methods(); + setCustom = Traits::scanMethodLists(methods.beginCategoryMethodLists(), + methods.endCategoryMethodLists(cls)); + } else if (!isMeta && !as_objc_class(cls)->superclass) { + // Custom Root class + setCustom = YES; + } else if (Traits::isCustom(as_objc_class(cls)->superclass)) { + // Superclass is custom, therefore we are too. + setCustom = YES; + inherited = YES; + } else { + // Not NSObject. + auto &methods = as_objc_class(cls)->data()->methods(); + setCustom = Traits::scanMethodLists(methods.beginLists(), + methods.endLists()); + } + if (slowpath(setCustom)) { + if (ShouldPrint) printCustom(cls, Bundle, inherited); + } else { + Traits::setDefault(cls); + } + } + +public: + // Scan a class that is about to be marked Initialized for particular + // bundles of selectors, and mark the class and its children + // accordingly. + // + // This also handles inheriting properties from its superclass. + // + // Caller: objc_class::setInitialized() + static void + scanInitializedClass(Class cls, Class metacls) + { + if (Domain != Scope::Classes) { + scanAddedClassImpl(cls, false); + } + if (Domain != Scope::Instances) { + scanAddedClassImpl(metacls, true); + } + } + + // Inherit various properties from the superclass when a class + // is being added to the graph. + // + // Caller: addSubclass() + static void + scanAddedSubClass(Class subcls, Class supercls) + { + if (slowpath(Traits::isCustom(supercls) && !Traits::isCustom(subcls))) { + setCustomRecursively(subcls, true); + } + } + + // Scan Method lists for selectors that would override things + // in a Bundle. + // + // This is used to detect when categories override problematic selectors + // are injected in a class after it has been initialized. + // + // Caller: prepareMethodLists() + static void + scanAddedMethodLists(Class cls, method_list_t **mlists, int count) + { + if (slowpath(Traits::isCustom(cls))) { + return; + } + if (slowpath(Traits::scanMethodLists(mlists, mlists + count))) { + setCustomRecursively(cls); + } + } + + // Handle IMP Swizzling (the IMP for an exisiting method being changed). + // + // In almost all cases, IMP swizzling does not affect custom bits. + // Custom search will already find the method whether or not + // it is swizzled, so it does not transition from non-custom to custom. + // + // The only cases where IMP swizzling can affect the custom bits is + // if the swizzled method is one of the methods that is assumed to be + // non-custom. These special cases are listed in setInitialized(). + // We look for such cases here. + // + // Caller: Swizzling methods via adjustCustomFlagsForMethodChange() + static void + scanChangedMethod(Class cls, const method_t *meth) + { + if (fastpath(!Traits::isInterestingSelector(meth->name))) { + return; + } + + if (cls) { + bool isMeta = as_objc_class(cls)->isMetaClass(); + if (isMeta && Domain != Scope::Instances) { + if (cls == metaclassNSObject() && !isNSObjectSwizzled(isMeta)) { + setNSObjectSwizzled(cls, isMeta); + } + } + if (!isMeta && Domain != Scope::Classes) { + if (cls == classNSObject() && !isNSObjectSwizzled(isMeta)) { + setNSObjectSwizzled(cls, isMeta); + } + } + } else { + // We're called from method_exchangeImplementations, only NSObject + // class and metaclass may be problematic (exchanging the default + // builtin IMP of an interesting seleector, is a swizzling that, + // may flip our scanned property. For other classes, the previous + // value had already flipped the property). + // + // However, as we don't know the class, we need to scan all of + // NSObject class and metaclass methods (this is SLOW). + scanChangedMethodForUnknownClass(meth); + } + } +}; + +} // namespace scanner + +// AWZ methods: +alloc / +allocWithZone: +struct AWZScanner : scanner::Mixin { + static bool isCustom(Class cls) { + return cls->hasCustomAWZ(); + } + static void setCustom(Class cls) { + cls->setHasCustomAWZ(); + } + static void setDefault(Class cls) { + cls->setHasDefaultAWZ(); + } + static bool isInterestingSelector(SEL sel) { + return sel == @selector(alloc) || sel == @selector(allocWithZone:); + } + static bool scanMethodLists(method_list_t * const *mlists, method_list_t * const *end) { + SEL sels[2] = { @selector(alloc), @selector(allocWithZone:), }; + return method_lists_contains_any(mlists, end, sels, 2); + } +}; + +// Retain/Release methods that are extremely rarely overridden +// +// retain/release/autorelease/retainCount/ +// _tryRetain/_isDeallocating/retainWeakReference/allowsWeakReference +struct RRScanner : scanner::Mixin { + static bool isCustom(Class cls) { + return cls->hasCustomRR(); + } + static void setCustom(Class cls) { + cls->setHasCustomRR(); + } + static void setDefault(Class cls) { + cls->setHasDefaultRR(); + } + static bool isInterestingSelector(SEL sel) { + return sel == @selector(retain) || + sel == @selector(release) || + sel == @selector(autorelease) || + sel == @selector(_tryRetain) || + sel == @selector(_isDeallocating) || + sel == @selector(retainCount) || + sel == @selector(allowsWeakReference) || + sel == @selector(retainWeakReference); + } + static bool scanMethodLists(method_list_t * const *mlists, method_list_t * const *end) { + SEL sels[8] = { + @selector(retain), + @selector(release), + @selector(autorelease), + @selector(_tryRetain), + @selector(_isDeallocating), + @selector(retainCount), + @selector(allowsWeakReference), + @selector(retainWeakReference), + }; + return method_lists_contains_any(mlists, end, sels, 8); + } +}; + +// Core NSObject methods that are extremely rarely overridden +// +// +new, ±class, ±self, ±isKindOfClass:, ±respondsToSelector +struct CoreScanner : scanner::Mixin { + static bool isCustom(Class cls) { + return cls->hasCustomCore(); + } + static void setCustom(Class cls) { + cls->setHasCustomCore(); + } + static void setDefault(Class cls) { + cls->setHasDefaultCore(); + } + static bool isInterestingSelector(SEL sel) { + return sel == @selector(new) || + sel == @selector(self) || + sel == @selector(class) || + sel == @selector(isKindOfClass:) || + sel == @selector(respondsToSelector:); + } + static bool scanMethodLists(method_list_t * const *mlists, method_list_t * const *end) { + SEL sels[5] = { + @selector(new), + @selector(self), + @selector(class), + @selector(isKindOfClass:), + @selector(respondsToSelector:) + }; + return method_lists_contains_any(mlists, end, sels, 5); + } +}; + +class category_list : nocopy_t { + union { + locstamped_category_t lc; + struct { + locstamped_category_t *array; + // this aliases with locstamped_category_t::hi + // which is an aliased pointer + uint32_t is_array : 1; + uint32_t count : 31; + uint32_t size : 32; + }; + } _u; + +public: + category_list() : _u{{nullptr, nullptr}} { } + category_list(locstamped_category_t lc) : _u{{lc}} { } + category_list(category_list &&other) : category_list() { + std::swap(_u, other._u); + } + ~category_list() + { + if (_u.is_array) { + free(_u.array); + } + } + + uint32_t count() const + { + if (_u.is_array) return _u.count; + return _u.lc.cat ? 1 : 0; + } + + uint32_t arrayByteSize(uint32_t size) const + { + return sizeof(locstamped_category_t) * size; + } + + const locstamped_category_t *array() const + { + return _u.is_array ? _u.array : &_u.lc; + } + + void append(locstamped_category_t lc) + { + if (_u.is_array) { + if (_u.count == _u.size) { + // Have a typical malloc growth: + // - size <= 8: grow by 2 + // - size <= 16: grow by 4 + // - size <= 32: grow by 8 + // ... etc + _u.size += _u.size < 8 ? 2 : 1 << (fls(_u.size) - 2); + _u.array = (locstamped_category_t *)reallocf(_u.array, arrayByteSize(_u.size)); + } + _u.array[_u.count++] = lc; + } else if (_u.lc.cat == NULL) { + _u.lc = lc; + } else { + locstamped_category_t *arr = (locstamped_category_t *)malloc(arrayByteSize(2)); + arr[0] = _u.lc; + arr[1] = lc; + + _u.array = arr; + _u.is_array = true; + _u.count = 2; + _u.size = 2; + } + } + + void erase(category_t *cat) + { + if (_u.is_array) { + for (int i = 0; i < _u.count; i++) { + if (_u.array[i].cat == cat) { + // shift entries to preserve list order + memmove(&_u.array[i], &_u.array[i+1], arrayByteSize(_u.count - i - 1)); + return; + } + } + } else if (_u.lc.cat == cat) { + _u.lc.cat = NULL; + _u.lc.hi = NULL; + } + } +}; + +class UnattachedCategories : public ExplicitInitDenseMap +{ +public: + void addForClass(locstamped_category_t lc, Class cls) + { + runtimeLock.assertLocked(); + + if (slowpath(PrintConnecting)) { + _objc_inform("CLASS: found category %c%s(%s)", + cls->isMetaClass() ? '+' : '-', + cls->nameForLogging(), lc.cat->name); + } + + auto result = get().try_emplace(cls, lc); + if (!result.second) { + result.first->second.append(lc); + } + } + + void attachToClass(Class cls, Class previously, int flags) + { + runtimeLock.assertLocked(); + ASSERT((flags & ATTACH_CLASS) || + (flags & ATTACH_METACLASS) || + (flags & ATTACH_CLASS_AND_METACLASS)); + + auto &map = get(); + auto it = map.find(previously); + + if (it != map.end()) { + category_list &list = it->second; + if (flags & ATTACH_CLASS_AND_METACLASS) { + int otherFlags = flags & ~ATTACH_CLASS_AND_METACLASS; + attachCategories(cls, list.array(), list.count(), otherFlags | ATTACH_CLASS); + attachCategories(cls->ISA(), list.array(), list.count(), otherFlags | ATTACH_METACLASS); + } else { + attachCategories(cls, list.array(), list.count(), flags); + } + map.erase(it); + } + } + + void eraseCategoryForClass(category_t *cat, Class cls) + { + runtimeLock.assertLocked(); + + auto &map = get(); + auto it = map.find(cls); + if (it != map.end()) { + category_list &list = it->second; + list.erase(cat); + if (list.count() == 0) { + map.erase(it); + } + } + } + + void eraseClass(Class cls) + { + runtimeLock.assertLocked(); + + get().erase(cls); + } +}; + +static UnattachedCategories unattachedCategories; + +} // namespace objc + static bool isBundleClass(Class cls) { - return cls->data()->ro->flags & RO_FROM_BUNDLE; + return cls->data()->ro()->flags & RO_FROM_BUNDLE; } static void fixupMethodList(method_list_t *mlist, bool bundleCopy, bool sort) { - runtimeLock.assertWriting(); - assert(!mlist->isFixedUp()); + runtimeLock.assertLocked(); + ASSERT(!mlist->isFixedUp()); // fixme lock less in attachMethodLists ? - sel_lock(); + // dyld3 may have already uniqued, but not sorted, the list + if (!mlist->isUniqued()) { + mutex_locker_t lock(selLock); - // Unique selectors in list. - for (auto& meth : *mlist) { - const char *name = sel_cname(meth.name); - meth.name = sel_registerNameNoLock(name, bundleCopy); + // Unique selectors in list. + for (auto& meth : *mlist) { + const char *name = sel_cname(meth.name); + meth.name = sel_registerNameNoLock(name, bundleCopy); + } } - - sel_unlock(); // Sort by selector address. if (sort) { @@ -567,23 +1210,19 @@ fixupMethodList(method_list_t *mlist, bool bundleCopy, bool sort) static void -prepareMethodLists(Class cls, method_list_t **addedLists, int addedCount, +prepareMethodLists(Class cls, method_list_t **addedLists, int addedCount, bool baseMethods, bool methodsFromBundle) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); if (addedCount == 0) return; - // Don't scan redundantly - bool scanForCustomRR = !cls->hasCustomRR(); - bool scanForCustomAWZ = !cls->hasCustomAWZ(); - - // There exist RR/AWZ special cases for some class's base methods. - // But this code should never need to scan base methods for RR/AWZ: - // default RR/AWZ cannot be set before setInitialized(). + // There exist RR/AWZ/Core special cases for some class's base methods. + // But this code should never need to scan base methods for RR/AWZ/Core: + // default RR/AWZ/Core cannot be set before setInitialized(). // Therefore we need not handle any special cases here. if (baseMethods) { - assert(!scanForCustomRR && !scanForCustomAWZ); + ASSERT(cls->hasCustomAWZ() && cls->hasCustomRR() && cls->hasCustomCore()); } // Add method lists to array. @@ -592,84 +1231,139 @@ prepareMethodLists(Class cls, method_list_t **addedLists, int addedCount, for (int i = 0; i < addedCount; i++) { method_list_t *mlist = addedLists[i]; - assert(mlist); + ASSERT(mlist); // Fixup selectors if necessary if (!mlist->isFixedUp()) { fixupMethodList(mlist, methodsFromBundle, true/*sort*/); } + } - // Scan for method implementations tracked by the class's flags - if (scanForCustomRR && methodListImplementsRR(mlist)) { - cls->setHasCustomRR(); - scanForCustomRR = false; - } - if (scanForCustomAWZ && methodListImplementsAWZ(mlist)) { - cls->setHasCustomAWZ(); - scanForCustomAWZ = false; - } + // If the class is initialized, then scan for method implementations + // tracked by the class's flags. If it's not initialized yet, + // then objc_class::setInitialized() will take care of it. + if (cls->isInitialized()) { + objc::AWZScanner::scanAddedMethodLists(cls, addedLists, addedCount); + objc::RRScanner::scanAddedMethodLists(cls, addedLists, addedCount); + objc::CoreScanner::scanAddedMethodLists(cls, addedLists, addedCount); } } +class_rw_ext_t * +class_rw_t::extAlloc(const class_ro_t *ro, bool deepCopy) +{ + runtimeLock.assertLocked(); + + auto rwe = objc::zalloc(); + + rwe->version = (ro->flags & RO_META) ? 7 : 0; + + method_list_t *list = ro->baseMethods(); + if (list) { + if (deepCopy) list = list->duplicate(); + rwe->methods.attachLists(&list, 1); + } + + // See comments in objc_duplicateClass + // property lists and protocol lists historically + // have not been deep-copied + // + // This is probably wrong and ought to be fixed some day + property_list_t *proplist = ro->baseProperties; + if (proplist) { + rwe->properties.attachLists(&proplist, 1); + } + + protocol_list_t *protolist = ro->baseProtocols; + if (protolist) { + rwe->protocols.attachLists(&protolist, 1); + } + + set_ro_or_rwe(rwe, ro); + return rwe; +} // Attach method lists and properties and protocols from categories to a class. // Assumes the categories in cats are all loaded and sorted by load order, // oldest categories first. -static void -attachCategories(Class cls, category_list *cats, bool flush_caches) +static void +attachCategories(Class cls, const locstamped_category_t *cats_list, uint32_t cats_count, + int flags) { - if (!cats) return; - if (PrintReplacedMethods) printReplacements(cls, cats); + if (slowpath(PrintReplacedMethods)) { + printReplacements(cls, cats_list, cats_count); + } + if (slowpath(PrintConnecting)) { + _objc_inform("CLASS: attaching %d categories to%s class '%s'%s", + cats_count, (flags & ATTACH_EXISTING) ? " existing" : "", + cls->nameForLogging(), (flags & ATTACH_METACLASS) ? " (meta)" : ""); + } - bool isMeta = cls->isMetaClass(); + /* + * Only a few classes have more than 64 categories during launch. + * This uses a little stack, and avoids malloc. + * + * Categories must be added in the proper order, which is back + * to front. To do that with the chunking, we iterate cats_list + * from front to back, build up the local buffers backwards, + * and call attachLists on the chunks. attachLists prepends the + * lists, so the final result is in the expected order. + */ + constexpr uint32_t ATTACH_BUFSIZ = 64; + method_list_t *mlists[ATTACH_BUFSIZ]; + property_list_t *proplists[ATTACH_BUFSIZ]; + protocol_list_t *protolists[ATTACH_BUFSIZ]; - // fixme rearrange to remove these intermediate allocations - method_list_t **mlists = (method_list_t **) - malloc(cats->count * sizeof(*mlists)); - property_list_t **proplists = (property_list_t **) - malloc(cats->count * sizeof(*proplists)); - protocol_list_t **protolists = (protocol_list_t **) - malloc(cats->count * sizeof(*protolists)); - - // Count backwards through cats to get newest categories first - int mcount = 0; - int propcount = 0; - int protocount = 0; - int i = cats->count; + uint32_t mcount = 0; + uint32_t propcount = 0; + uint32_t protocount = 0; bool fromBundle = NO; - while (i--) { - auto& entry = cats->list[i]; + bool isMeta = (flags & ATTACH_METACLASS); + auto rwe = cls->data()->extAllocIfNeeded(); + + for (uint32_t i = 0; i < cats_count; i++) { + auto& entry = cats_list[i]; method_list_t *mlist = entry.cat->methodsForMeta(isMeta); if (mlist) { - mlists[mcount++] = mlist; + if (mcount == ATTACH_BUFSIZ) { + prepareMethodLists(cls, mlists, mcount, NO, fromBundle); + rwe->methods.attachLists(mlists, mcount); + mcount = 0; + } + mlists[ATTACH_BUFSIZ - ++mcount] = mlist; fromBundle |= entry.hi->isBundle(); } - property_list_t *proplist = + property_list_t *proplist = entry.cat->propertiesForMeta(isMeta, entry.hi); if (proplist) { - proplists[propcount++] = proplist; + if (propcount == ATTACH_BUFSIZ) { + rwe->properties.attachLists(proplists, propcount); + propcount = 0; + } + proplists[ATTACH_BUFSIZ - ++propcount] = proplist; } - protocol_list_t *protolist = entry.cat->protocols; + protocol_list_t *protolist = entry.cat->protocolsForMeta(isMeta); if (protolist) { - protolists[protocount++] = protolist; + if (protocount == ATTACH_BUFSIZ) { + rwe->protocols.attachLists(protolists, protocount); + protocount = 0; + } + protolists[ATTACH_BUFSIZ - ++protocount] = protolist; } } - auto rw = cls->data(); + if (mcount > 0) { + prepareMethodLists(cls, mlists + ATTACH_BUFSIZ - mcount, mcount, NO, fromBundle); + rwe->methods.attachLists(mlists + ATTACH_BUFSIZ - mcount, mcount); + if (flags & ATTACH_EXISTING) flushCaches(cls); + } - prepareMethodLists(cls, mlists, mcount, NO, fromBundle); - rw->methods.attachLists(mlists, mcount); - free(mlists); - if (flush_caches && mcount > 0) flushCaches(cls); + rwe->properties.attachLists(proplists + ATTACH_BUFSIZ - propcount, propcount); - rw->properties.attachLists(proplists, propcount); - free(proplists); - - rw->protocols.attachLists(protolists, protocount); - free(protolists); + rwe->protocols.attachLists(protolists + ATTACH_BUFSIZ - protocount, protocount); } @@ -679,13 +1373,14 @@ attachCategories(Class cls, category_list *cats, bool flush_caches) * Attaches any outstanding categories. * Locking: runtimeLock must be held by the caller **********************************************************************/ -static void methodizeClass(Class cls) +static void methodizeClass(Class cls, Class previously) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); bool isMeta = cls->isMetaClass(); auto rw = cls->data(); - auto ro = rw->ro; + auto ro = rw->ro(); + auto rwe = rw->ext(); // Methodizing for the first time if (PrintConnecting) { @@ -697,84 +1392,55 @@ static void methodizeClass(Class cls) method_list_t *list = ro->baseMethods(); if (list) { prepareMethodLists(cls, &list, 1, YES, isBundleClass(cls)); - rw->methods.attachLists(&list, 1); + if (rwe) rwe->methods.attachLists(&list, 1); } property_list_t *proplist = ro->baseProperties; - if (proplist) { - rw->properties.attachLists(&proplist, 1); + if (rwe && proplist) { + rwe->properties.attachLists(&proplist, 1); } protocol_list_t *protolist = ro->baseProtocols; - if (protolist) { - rw->protocols.attachLists(&protolist, 1); + if (rwe && protolist) { + rwe->protocols.attachLists(&protolist, 1); } // Root classes get bonus method implementations if they don't have // them already. These apply before category replacements. if (cls->isRootMetaclass()) { // root metaclass - addMethod(cls, SEL_initialize, (IMP)&objc_noop_imp, "", NO); + addMethod(cls, @selector(initialize), (IMP)&objc_noop_imp, "", NO); } // Attach categories. - category_list *cats = unattachedCategoriesForClass(cls, true /*realizing*/); - attachCategories(cls, cats, false /*don't flush caches*/); - - if (PrintConnecting) { - if (cats) { - for (uint32_t i = 0; i < cats->count; i++) { - _objc_inform("CLASS: attached category %c%s(%s)", - isMeta ? '+' : '-', - cls->nameForLogging(), cats->list[i].cat->name); - } + if (previously) { + if (isMeta) { + objc::unattachedCategories.attachToClass(cls, previously, + ATTACH_METACLASS); + } else { + // When a class relocates, categories with class methods + // may be registered on the class itself rather than on + // the metaclass. Tell attachToClass to look for those. + objc::unattachedCategories.attachToClass(cls, previously, + ATTACH_CLASS_AND_METACLASS); } } - - if (cats) free(cats); + objc::unattachedCategories.attachToClass(cls, cls, + isMeta ? ATTACH_METACLASS : ATTACH_CLASS); #if DEBUG // Debug: sanity-check all SELs; log method list contents - for (const auto& meth : rw->methods) { + for (const auto& meth : rw->methods()) { if (PrintConnecting) { _objc_inform("METHOD %c[%s %s]", isMeta ? '+' : '-', cls->nameForLogging(), sel_getName(meth.name)); } - assert(sel_registerName(sel_getName(meth.name)) == meth.name); + ASSERT(sel_registerName(sel_getName(meth.name)) == meth.name); } #endif } -/*********************************************************************** -* remethodizeClass -* Attach outstanding categories to an existing class. -* Fixes up cls's method list, protocol list, and property list. -* Updates method caches for cls and its subclasses. -* Locking: runtimeLock must be held by the caller -**********************************************************************/ -static void remethodizeClass(Class cls) -{ - category_list *cats; - bool isMeta; - - runtimeLock.assertWriting(); - - isMeta = cls->isMetaClass(); - - // Re-methodizing: check for more categories - if ((cats = unattachedCategoriesForClass(cls, false/*not realizing*/))) { - if (PrintConnecting) { - _objc_inform("CLASS: attaching categories to class '%s' %s", - cls->nameForLogging(), isMeta ? "(meta)" : ""); - } - - attachCategories(cls, cats, true /*flush caches*/); - free(cats); - } -} - - /*********************************************************************** * nonMetaClasses * Returns the secondary metaclass => class map @@ -786,7 +1452,8 @@ static void remethodizeClass(Class cls) * Classes with no duplicates are not included. * Classes in the preoptimized named-class table are not included. * Classes whose duplicates are in the preoptimized table are not included. -* Most code should use getNonMetaClass() instead of reading this table. +* Most code should use getMaybeUnrealizedNonMetaClass() +* instead of reading this table. * Locking: runtimeLock must be read- or write-locked by the caller **********************************************************************/ static NXMapTable *nonmeta_class_map = nil; @@ -812,19 +1479,19 @@ static NXMapTable *nonMetaClasses(void) **********************************************************************/ static void addNonMetaClass(Class cls) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); void *old; old = NXMapInsert(nonMetaClasses(), cls->ISA(), cls); - assert(!cls->isMetaClass()); - assert(cls->ISA()->isMetaClass()); - assert(!old); + ASSERT(!cls->isMetaClassMaybeUnrealized()); + ASSERT(cls->ISA()->isMetaClassMaybeUnrealized()); + ASSERT(!old); } static void removeNonMetaClass(Class cls) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); NXMapRemove(nonMetaClasses(), cls->ISA()); } @@ -946,32 +1613,41 @@ static char *copySwiftV1MangledName(const char *string, bool isProtocol = false) /*********************************************************************** -* getClass +* getClassExceptSomeSwift * Looks up a class by name. The class MIGHT NOT be realized. * Demangled Swift names are recognized. +* Classes known to the Swift runtime but not yet used are NOT recognized. +* (such as subclasses of un-instantiated generics) +* Use look_up_class() to find them as well. * Locking: runtimeLock must be read- or write-locked by the caller. **********************************************************************/ // This is a misnomer: gdb_objc_realized_classes is actually a list of // named classes not in the dyld shared cache, whether realized or not. NXMapTable *gdb_objc_realized_classes; // exported for debuggers in objc-gdb.h +uintptr_t objc_debug_realized_class_generation_count; static Class getClass_impl(const char *name) { runtimeLock.assertLocked(); // allocated in _read_images - assert(gdb_objc_realized_classes); + ASSERT(gdb_objc_realized_classes); // Try runtime-allocated table Class result = (Class)NXMapGet(gdb_objc_realized_classes, name); if (result) return result; - // Try table from dyld shared cache + // Try table from dyld shared cache. + // Note we do this last to handle the case where we dlopen'ed a shared cache + // dylib with duplicates of classes already present in the main executable. + // In that case, we put the class from the main executable in + // gdb_objc_realized_classes and want to check that before considering any + // newly loaded shared cache binaries. return getPreoptimizedClass(name); } -static Class getClass(const char *name) +static Class getClassExceptSomeSwift(const char *name) { runtimeLock.assertLocked(); @@ -998,21 +1674,22 @@ static Class getClass(const char *name) **********************************************************************/ static void addNamedClass(Class cls, const char *name, Class replacing = nil) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); Class old; - if ((old = getClass(name)) && old != replacing) { + if ((old = getClassExceptSomeSwift(name)) && old != replacing) { inform_duplicate(name, old, cls); - // getNonMetaClass uses name lookups. Classes not found by name - // lookup must be in the secondary meta->nonmeta table. + // getMaybeUnrealizedNonMetaClass uses name lookups. + // Classes not found by name lookup must be in the + // secondary meta->nonmeta table. addNonMetaClass(cls); } else { NXMapInsert(gdb_objc_realized_classes, name, cls); } - assert(!(cls->data()->flags & RO_META)); + ASSERT(!(cls->data()->flags & RO_META)); // wrong: constructed classes are already realized when they get here - // assert(!cls->isRealized()); + // ASSERT(!cls->isRealized()); } @@ -1023,8 +1700,8 @@ static void addNamedClass(Class cls, const char *name, Class replacing = nil) **********************************************************************/ static void removeNamedClass(Class cls, const char *name) { - runtimeLock.assertWriting(); - assert(!(cls->data()->flags & RO_META)); + runtimeLock.assertLocked(); + ASSERT(!(cls->data()->flags & RO_META)); if (cls == NXMapGet(gdb_objc_realized_classes, name)) { NXMapRemove(gdb_objc_realized_classes, name); } else { @@ -1043,7 +1720,7 @@ static void removeNamedClass(Class cls, const char *name) static NXMapTable *future_named_class_map = nil; static NXMapTable *futureNamedClasses() { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); if (future_named_class_map) return future_named_class_map; @@ -1069,21 +1746,21 @@ static void addFutureNamedClass(const char *name, Class cls) { void *old; - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); if (PrintFuture) { _objc_inform("FUTURE: reserving %p for %s", (void*)cls, name); } - class_rw_t *rw = (class_rw_t *)calloc(sizeof(class_rw_t), 1); + class_rw_t *rw = objc::zalloc(); class_ro_t *ro = (class_ro_t *)calloc(sizeof(class_ro_t), 1); ro->name = strdupIfMutable(name); - rw->ro = ro; + rw->set_ro(ro); cls->setData(rw); cls->data()->flags = RO_FUTURE; old = NXMapKeyCopyingInsert(futureNamedClasses(), name, cls); - assert(!old); + ASSERT(!old); } @@ -1096,7 +1773,7 @@ static void addFutureNamedClass(const char *name, Class cls) **********************************************************************/ static Class popFutureNamedClass(const char *name) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); Class cls = nil; @@ -1118,21 +1795,14 @@ static Class popFutureNamedClass(const char *name) * Returns the oldClass => nil map for ignored weak-linked classes. * Locking: runtimeLock must be read- or write-locked by the caller **********************************************************************/ -static NXMapTable *remappedClasses(bool create) +static objc::DenseMap *remappedClasses(bool create) { - static NXMapTable *remapped_class_map = nil; + static objc::LazyInitDenseMap remapped_class_map; runtimeLock.assertLocked(); - if (remapped_class_map) return remapped_class_map; - if (!create) return nil; - - // remapped_class_map is big enough to hold CF's classes and a few others - INIT_ONCE_PTR(remapped_class_map, - NXCreateMapTable(NXPtrValueMapPrototype, 32), - NXFreeMapTable(v)); - - return remapped_class_map; + // start big enough to hold CF's classes and a few others + return remapped_class_map.get(create, 32); } @@ -1148,8 +1818,8 @@ static bool noClassesRemapped(void) bool result = (remappedClasses(NO) == nil); #if DEBUG // Catch construction of an empty table, which defeats optimization. - NXMapTable *map = remappedClasses(NO); - if (map) assert(NXCountMapTable(map) > 0); + auto *map = remappedClasses(NO); + if (map) ASSERT(map->size() > 0); #endif return result; } @@ -1163,16 +1833,25 @@ static bool noClassesRemapped(void) **********************************************************************/ static void addRemappedClass(Class oldcls, Class newcls) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); if (PrintFuture) { _objc_inform("FUTURE: using %p instead of %p for %s", (void*)newcls, (void*)oldcls, oldcls->nameForLogging()); } - void *old; - old = NXMapInsert(remappedClasses(YES), oldcls, newcls); - assert(!old); + auto result = remappedClasses(YES)->insert({ oldcls, newcls }); +#if DEBUG + if (!std::get<1>(result)) { + // An existing mapping was overwritten. This is not allowed + // unless it was to nil. + auto iterator = std::get<0>(result); + auto value = std::get<1>(*iterator); + ASSERT(value == nil); + } +#else + (void)result; +#endif } @@ -1187,16 +1866,16 @@ static Class remapClass(Class cls) { runtimeLock.assertLocked(); - Class c2; - if (!cls) return nil; - NXMapTable *map = remappedClasses(NO); - if (!map || NXMapMember(map, cls, (void**)&c2) == NX_MAPNOTAKEY) { + auto *map = remappedClasses(NO); + if (!map) return cls; - } else { - return c2; - } + + auto iterator = map->find(cls); + if (iterator == map->end()) + return cls; + return std::get<1>(*iterator); } static Class remapClass(classref_t cls) @@ -1206,7 +1885,7 @@ static Class remapClass(classref_t cls) Class _class_remap(Class cls) { - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return remapClass(cls); } @@ -1225,20 +1904,36 @@ static void remapClassRef(Class *clsref) } +_Nullable Class +objc_loadClassref(_Nullable Class * _Nonnull clsref) +{ + auto *atomicClsref = explicit_atomic::from_pointer((uintptr_t *)clsref); + + uintptr_t cls = atomicClsref->load(std::memory_order_relaxed); + if (fastpath((cls & 1) == 0)) + return (Class)cls; + + auto stub = (stub_class_t *)(cls & ~1ULL); + Class initialized = stub->initializer((Class)stub, nil); + atomicClsref->store((uintptr_t)initialized, std::memory_order_relaxed); + return initialized; +} + + /*********************************************************************** -* getNonMetaClass +* getMaybeUnrealizedNonMetaClass * Return the ordinary class for this class or metaclass. * `inst` is an instance of `cls` or a subclass thereof, or nil. * Non-nil inst is faster. +* The result may be unrealized. * Used by +initialize. * Locking: runtimeLock must be read- or write-locked by the caller **********************************************************************/ -static Class getNonMetaClass(Class metacls, id inst) +static Class getMaybeUnrealizedNonMetaClass(Class metacls, id inst) { - static int total, named, secondary, sharedcache; + static int total, named, secondary, sharedcache, dyld3; runtimeLock.assertLocked(); - - realizeClass(metacls); + ASSERT(metacls->isRealized()); total++; @@ -1246,30 +1941,30 @@ static Class getNonMetaClass(Class metacls, id inst) if (!metacls->isMetaClass()) return metacls; // metacls really is a metaclass + // which means inst (if any) is a class // special case for root metaclass // where inst == inst->ISA() == metacls is possible if (metacls->ISA() == metacls) { Class cls = metacls->superclass; - assert(cls->isRealized()); - assert(!cls->isMetaClass()); - assert(cls->ISA() == metacls); + ASSERT(cls->isRealized()); + ASSERT(!cls->isMetaClass()); + ASSERT(cls->ISA() == metacls); if (cls->ISA() == metacls) return cls; } // use inst if available if (inst) { - Class cls = (Class)inst; - realizeClass(cls); + Class cls = remapClass((Class)inst); // cls may be a subclass - find the real class for metacls - while (cls && cls->ISA() != metacls) { + // fixme this probably stops working once Swift starts + // reallocating classes if cls is unrealized. + while (cls) { + if (cls->ISA() == metacls) { + ASSERT(!cls->isMetaClassMaybeUnrealized()); + return cls; + } cls = cls->superclass; - realizeClass(cls); - } - if (cls) { - assert(!cls->isMetaClass()); - assert(cls->ISA() == metacls); - return cls; } #if DEBUG _objc_fatal("cls is not an instance of metacls"); @@ -1280,16 +1975,14 @@ static Class getNonMetaClass(Class metacls, id inst) // try name lookup { - Class cls = getClass(metacls->mangledName()); - if (cls->ISA() == metacls) { + Class cls = getClassExceptSomeSwift(metacls->mangledName()); + if (cls && cls->ISA() == metacls) { named++; if (PrintInitializing) { _objc_inform("INITIALIZE: %d/%d (%g%%) " "successful by-name metaclass lookups", named, total, named*100.0/total); } - - realizeClass(cls); return cls; } } @@ -1305,8 +1998,44 @@ static Class getNonMetaClass(Class metacls, id inst) secondary, total, secondary*100.0/total); } - assert(cls->ISA() == metacls); - realizeClass(cls); + ASSERT(cls->ISA() == metacls); + return cls; + } + } + + // try the dyld closure table + if (isPreoptimized()) + { + // Try table from dyld closure first. It was built to ignore the dupes it + // knows will come from the cache, so anything left in here was there when + // we launched + Class cls = nil; + // Note, we have to pass the lambda directly here as otherwise we would try + // message copy and autorelease. + _dyld_for_each_objc_class(metacls->mangledName(), + [&cls, metacls](void* classPtr, bool isLoaded, bool* stop) { + // Skip images which aren't loaded. This supports the case where dyld + // might soft link an image from the main binary so its possibly not + // loaded yet. + if (!isLoaded) + return; + + // Found a loaded image with this class name, so check if its the right one + Class result = (Class)classPtr; + if (result->ISA() == metacls) { + cls = result; + *stop = true; + } + }); + + if (cls) { + dyld3++; + if (PrintInitializing) { + _objc_inform("INITIALIZE: %d/%d (%g%%) " + "successful dyld closure metaclass lookups", + dyld3, total, dyld3*100.0/total); + } + return cls; } } @@ -1335,7 +2064,6 @@ static Class getNonMetaClass(Class metacls, id inst) sharedcache, total, sharedcache*100.0/total); } - realizeClass(cls); return cls; } } @@ -1345,45 +2073,90 @@ static Class getNonMetaClass(Class metacls, id inst) /*********************************************************************** -* _class_getNonMetaClass -* Return the ordinary class for this class or metaclass. -* Used by +initialize. -* Locking: acquires runtimeLock +* class_initialize. Send the '+initialize' message on demand to any +* uninitialized class. Force initialization of superclasses first. +* inst is an instance of cls, or nil. Non-nil is better for performance. +* Returns the class pointer. If the class was unrealized then +* it may be reallocated. +* Locking: +* runtimeLock must be held by the caller +* This function may drop the lock. +* On exit the lock is re-acquired or dropped as requested by leaveLocked. **********************************************************************/ -Class _class_getNonMetaClass(Class cls, id obj) +static Class initializeAndMaybeRelock(Class cls, id inst, + mutex_t& lock, bool leaveLocked) { - rwlock_writer_t lock(runtimeLock); - cls = getNonMetaClass(cls, obj); - assert(cls->isRealized()); + lock.assertLocked(); + ASSERT(cls->isRealized()); + + if (cls->isInitialized()) { + if (!leaveLocked) lock.unlock(); + return cls; + } + + // Find the non-meta class for cls, if it is not already one. + // The +initialize message is sent to the non-meta class object. + Class nonmeta = getMaybeUnrealizedNonMetaClass(cls, inst); + + // Realize the non-meta class if necessary. + if (nonmeta->isRealized()) { + // nonmeta is cls, which was already realized + // OR nonmeta is distinct, but is already realized + // - nothing else to do + lock.unlock(); + } else { + nonmeta = realizeClassMaybeSwiftAndUnlock(nonmeta, lock); + // runtimeLock is now unlocked + // fixme Swift can't relocate the class today, + // but someday it will: + cls = object_getClass(nonmeta); + } + + // runtimeLock is now unlocked, for +initialize dispatch + ASSERT(nonmeta->isRealized()); + initializeNonMetaClass(nonmeta); + + if (leaveLocked) runtimeLock.lock(); return cls; } +// Locking: acquires runtimeLock +Class class_initialize(Class cls, id obj) +{ + runtimeLock.lock(); + return initializeAndMaybeRelock(cls, obj, runtimeLock, false); +} + +// Locking: caller must hold runtimeLock; this may drop and re-acquire it +static Class initializeAndLeaveLocked(Class cls, id obj, mutex_t& lock) +{ + return initializeAndMaybeRelock(cls, obj, lock, true); +} + /*********************************************************************** * addRootClass * Adds cls as a new realized root class. * Locking: runtimeLock must be held by the caller. **********************************************************************/ -static Class _firstRealizedClass = nil; -Class firstRealizedClass() -{ - runtimeLock.assertLocked(); - return _firstRealizedClass; -} - static void addRootClass(Class cls) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); - assert(cls->isRealized()); + ASSERT(cls->isRealized()); + + objc_debug_realized_class_generation_count++; + cls->data()->nextSiblingClass = _firstRealizedClass; _firstRealizedClass = cls; } static void removeRootClass(Class cls) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); + objc_debug_realized_class_generation_count++; + Class *classp; for (classp = &_firstRealizedClass; *classp != cls; @@ -1401,11 +2174,14 @@ static void removeRootClass(Class cls) **********************************************************************/ static void addSubclass(Class supercls, Class subcls) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); if (supercls && subcls) { - assert(supercls->isRealized()); - assert(subcls->isRealized()); + ASSERT(supercls->isRealized()); + ASSERT(subcls->isRealized()); + + objc_debug_realized_class_generation_count++; + subcls->data()->nextSiblingClass = supercls->data()->firstSubclass; supercls->data()->firstSubclass = subcls; @@ -1417,18 +2193,14 @@ static void addSubclass(Class supercls, Class subcls) subcls->setHasCxxDtor(); } - if (supercls->hasCustomRR()) { - subcls->setHasCustomRR(true); - } + objc::AWZScanner::scanAddedSubClass(subcls, supercls); + objc::RRScanner::scanAddedSubClass(subcls, supercls); + objc::CoreScanner::scanAddedSubClass(subcls, supercls); - if (supercls->hasCustomAWZ()) { - subcls->setHasCustomAWZ(true); - } - - // Special case: instancesRequireRawIsa does not propagate + // Special case: instancesRequireRawIsa does not propagate // from root class to root metaclass if (supercls->instancesRequireRawIsa() && supercls->superclass) { - subcls->setInstancesRequireRawIsa(true); + subcls->setInstancesRequireRawIsaRecursively(true); } } } @@ -1441,17 +2213,19 @@ static void addSubclass(Class supercls, Class subcls) **********************************************************************/ static void removeSubclass(Class supercls, Class subcls) { - runtimeLock.assertWriting(); - assert(supercls->isRealized()); - assert(subcls->isRealized()); - assert(subcls->superclass == supercls); + runtimeLock.assertLocked(); + ASSERT(supercls->isRealized()); + ASSERT(subcls->isRealized()); + ASSERT(subcls->superclass == supercls); + objc_debug_realized_class_generation_count++; + Class *cp; for (cp = &supercls->data()->firstSubclass; *cp && *cp != subcls; cp = &(*cp)->data()->nextSiblingClass) ; - assert(*cp == subcls); + ASSERT(*cp == subcls); *cp = subcls->data()->nextSiblingClass; } @@ -1481,7 +2255,7 @@ static NXMapTable *protocols(void) * Looks up a protocol by name. Demangled Swift names are recognized. * Locking: runtimeLock must be read- or write-locked by the caller. **********************************************************************/ -static Protocol *getProtocol(const char *name) +static NEVER_INLINE Protocol *getProtocol(const char *name) { runtimeLock.assertLocked(); @@ -1493,7 +2267,16 @@ static Protocol *getProtocol(const char *name) if (char *swName = copySwiftV1MangledName(name, true/*isProtocol*/)) { result = (Protocol *)NXMapGet(protocols(), swName); free(swName); - return result; + if (result) return result; + } + + // Try table from dyld shared cache + // Temporarily check that we are using the new table. Eventually this check + // will always be true. + // FIXME: Remove this check when we can + if (sharedCacheSupportsProtocolRoots()) { + result = getPreoptimizedProtocol(name); + if (result) return result; } return nil; @@ -1506,10 +2289,15 @@ static Protocol *getProtocol(const char *name) * a protocol struct that has been reallocated. * Locking: runtimeLock must be read- or write-locked by the caller **********************************************************************/ -static protocol_t *remapProtocol(protocol_ref_t proto) +static ALWAYS_INLINE protocol_t *remapProtocol(protocol_ref_t proto) { runtimeLock.assertLocked(); + // Protocols in shared cache images have a canonical bit to mark that they + // are the definition we should use + if (((protocol_t *)proto)->isCanonical()) + return (protocol_t *)proto; + protocol_t *newproto = (protocol_t *) getProtocol(((protocol_t *)proto)->mangledName); return newproto ? newproto : (protocol_t *)proto; @@ -1542,11 +2330,11 @@ static void remapProtocolRef(protocol_t **protoref) **********************************************************************/ static void moveIvars(class_ro_t *ro, uint32_t superSize) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); uint32_t diff; - assert(superSize > ro->instanceStart); + ASSERT(superSize > ro->instanceStart); diff = superSize - ro->instanceStart; if (ro->ivars) { @@ -1589,8 +2377,8 @@ static void reconcileInstanceVariables(Class cls, Class supercls, const class_ro { class_rw_t *rw = cls->data(); - assert(supercls); - assert(!cls->isMetaClass()); + ASSERT(supercls); + ASSERT(!cls->isMetaClass()); /* debug: print them all before sliding if (ro->ivars) { @@ -1605,7 +2393,7 @@ static void reconcileInstanceVariables(Class cls, Class supercls, const class_ro */ // Non-fragile ivars - reconcile this class with its superclass - const class_ro_t *super_ro = supercls->data()->ro; + const class_ro_t *super_ro = supercls->data()->ro(); if (DebugNonFragileIvars) { // Debugging: Force non-fragile ivars to slide. @@ -1627,7 +2415,7 @@ static void reconcileInstanceVariables(Class cls, Class supercls, const class_ro { uint32_t oldStart = ro->instanceStart; class_ro_t *ro_w = make_ro_writeable(rw); - ro = rw->ro; + ro = rw->ro(); // Find max ivar alignment in class. // default to word size to simplify ivar update @@ -1678,7 +2466,7 @@ static void reconcileInstanceVariables(Class cls, Class supercls, const class_ro super_ro->instanceSize); } class_ro_t *ro_w = make_ro_writeable(rw); - ro = rw->ro; + ro = rw->ro(); moveIvars(ro_w, super_ro->instanceSize); gdb_objc_class_changed(cls, OBJC_CLASS_IVARS_CHANGED, ro->name); } @@ -1686,94 +2474,105 @@ static void reconcileInstanceVariables(Class cls, Class supercls, const class_ro /*********************************************************************** -* realizeClass +* realizeClassWithoutSwift * Performs first-time initialization on class cls, * including allocating its read-write data. +* Does not perform any Swift-side initialization. * Returns the real class structure for the class. * Locking: runtimeLock must be write-locked by the caller **********************************************************************/ -static Class realizeClass(Class cls) +static Class realizeClassWithoutSwift(Class cls, Class previously) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); - const class_ro_t *ro; class_rw_t *rw; Class supercls; Class metacls; - bool isMeta; if (!cls) return nil; if (cls->isRealized()) return cls; - assert(cls == remapClass(cls)); + ASSERT(cls == remapClass(cls)); // fixme verify class is not in an un-dlopened part of the shared cache? - ro = (const class_ro_t *)cls->data(); + auto ro = (const class_ro_t *)cls->data(); + auto isMeta = ro->flags & RO_META; if (ro->flags & RO_FUTURE) { // This was a future class. rw data is already allocated. rw = cls->data(); - ro = cls->data()->ro; + ro = cls->data()->ro(); + ASSERT(!isMeta); cls->changeInfo(RW_REALIZED|RW_REALIZING, RW_FUTURE); } else { // Normal class. Allocate writeable class data. - rw = (class_rw_t *)calloc(sizeof(class_rw_t), 1); - rw->ro = ro; - rw->flags = RW_REALIZED|RW_REALIZING; + rw = objc::zalloc(); + rw->set_ro(ro); + rw->flags = RW_REALIZED|RW_REALIZING|isMeta; cls->setData(rw); } - isMeta = ro->flags & RO_META; - - rw->version = isMeta ? 7 : 0; // old runtime went up to 6 - +#if FAST_CACHE_META + if (isMeta) cls->cache.setBit(FAST_CACHE_META); +#endif // Choose an index for this class. // Sets cls->instancesRequireRawIsa if indexes no more indexes are available cls->chooseClassArrayIndex(); if (PrintConnecting) { - _objc_inform("CLASS: realizing class '%s'%s %p %p #%u", + _objc_inform("CLASS: realizing class '%s'%s %p %p #%u %s%s", cls->nameForLogging(), isMeta ? " (meta)" : "", - (void*)cls, ro, cls->classArrayIndex()); + (void*)cls, ro, cls->classArrayIndex(), + cls->isSwiftStable() ? "(swift)" : "", + cls->isSwiftLegacy() ? "(pre-stable swift)" : ""); } // Realize superclass and metaclass, if they aren't already. // This needs to be done after RW_REALIZED is set above, for root classes. // This needs to be done after class index is chosen, for root metaclasses. - supercls = realizeClass(remapClass(cls->superclass)); - metacls = realizeClass(remapClass(cls->ISA())); + // This assumes that none of those classes have Swift contents, + // or that Swift's initializers have already been called. + // fixme that assumption will be wrong if we add support + // for ObjC subclasses of Swift classes. + supercls = realizeClassWithoutSwift(remapClass(cls->superclass), nil); + metacls = realizeClassWithoutSwift(remapClass(cls->ISA()), nil); #if SUPPORT_NONPOINTER_ISA - // Disable non-pointer isa for some classes and/or platforms. - // Set instancesRequireRawIsa. - bool instancesRequireRawIsa = cls->instancesRequireRawIsa(); - bool rawIsaIsInherited = false; - static bool hackedDispatch = false; + if (isMeta) { + // Metaclasses do not need any features from non pointer ISA + // This allows for a faspath for classes in objc_retain/objc_release. + cls->setInstancesRequireRawIsa(); + } else { + // Disable non-pointer isa for some classes and/or platforms. + // Set instancesRequireRawIsa. + bool instancesRequireRawIsa = cls->instancesRequireRawIsa(); + bool rawIsaIsInherited = false; + static bool hackedDispatch = false; - if (DisableNonpointerIsa) { - // Non-pointer isa disabled by environment or app SDK version - instancesRequireRawIsa = true; - } - else if (!hackedDispatch && !(ro->flags & RO_META) && - 0 == strcmp(ro->name, "OS_object")) - { - // hack for libdispatch et al - isa also acts as vtable pointer - hackedDispatch = true; - instancesRequireRawIsa = true; - } - else if (supercls && supercls->superclass && - supercls->instancesRequireRawIsa()) - { - // This is also propagated by addSubclass() - // but nonpointer isa setup needs it earlier. - // Special case: instancesRequireRawIsa does not propagate - // from root class to root metaclass - instancesRequireRawIsa = true; - rawIsaIsInherited = true; - } - - if (instancesRequireRawIsa) { - cls->setInstancesRequireRawIsa(rawIsaIsInherited); + if (DisableNonpointerIsa) { + // Non-pointer isa disabled by environment or app SDK version + instancesRequireRawIsa = true; + } + else if (!hackedDispatch && 0 == strcmp(ro->name, "OS_object")) + { + // hack for libdispatch et al - isa also acts as vtable pointer + hackedDispatch = true; + instancesRequireRawIsa = true; + } + else if (supercls && supercls->superclass && + supercls->instancesRequireRawIsa()) + { + // This is also propagated by addSubclass() + // but nonpointer isa setup needs it earlier. + // Special case: instancesRequireRawIsa does not propagate + // from root class to root metaclass + instancesRequireRawIsa = true; + rawIsaIsInherited = true; + } + + if (instancesRequireRawIsa) { + cls->setInstancesRequireRawIsaRecursively(rawIsaIsInherited); + } } // SUPPORT_NONPOINTER_ISA #endif @@ -1796,6 +2595,14 @@ static Class realizeClass(Class cls) cls->setHasCxxCtor(); } } + + // Propagate the associated objects forbidden flag from ro or from + // the superclass. + if ((ro->flags & RO_FORBIDS_ASSOCIATED_OBJECTS) || + (supercls && supercls->forbidsAssociatedObjects())) + { + rw->flags |= RW_FORBIDS_ASSOCIATED_OBJECTS; + } // Connect this class to its superclass's subclass lists if (supercls) { @@ -1805,12 +2612,177 @@ static Class realizeClass(Class cls) } // Attach categories - methodizeClass(cls); + methodizeClass(cls, previously); return cls; } +/*********************************************************************** +* _objc_realizeClassFromSwift +* Called by Swift when it needs the ObjC part of a class to be realized. +* There are four cases: +* 1. cls != nil; previously == cls +* Class cls is being realized in place +* 2. cls != nil; previously == nil +* Class cls is being constructed at runtime +* 3. cls != nil; previously != cls +* The class that was at previously has been reallocated to cls +* 4. cls == nil, previously != nil +* The class at previously is hereby disavowed +* +* Only variants #1 and #2 are supported today. +* +* Locking: acquires runtimeLock +**********************************************************************/ +Class _objc_realizeClassFromSwift(Class cls, void *previously) +{ + if (cls) { + if (previously && previously != (void*)cls) { + // #3: relocation + mutex_locker_t lock(runtimeLock); + addRemappedClass((Class)previously, cls); + addClassTableEntry(cls); + addNamedClass(cls, cls->mangledName(), /*replacing*/nil); + return realizeClassWithoutSwift(cls, (Class)previously); + } else { + // #1 and #2: realization in place, or new class + mutex_locker_t lock(runtimeLock); + + if (!previously) { + // #2: new class + cls = readClass(cls, false/*bundle*/, false/*shared cache*/); + } + + // #1 and #2: realization in place, or new class + // We ignore the Swift metadata initializer callback. + // We assume that's all handled since we're being called from Swift. + return realizeClassWithoutSwift(cls, nil); + } + } + else { + // #4: disavowal + // In the future this will mean remapping the old address to nil + // and if necessary removing the old address from any other tables. + _objc_fatal("Swift requested that class %p be ignored, " + "but libobjc does not support that.", previously); + } +} + +/*********************************************************************** +* realizeSwiftClass +* Performs first-time initialization on class cls, +* including allocating its read-write data, +* and any Swift-side initialization. +* Returns the real class structure for the class. +* Locking: acquires runtimeLock indirectly +**********************************************************************/ +static Class realizeSwiftClass(Class cls) +{ + runtimeLock.assertUnlocked(); + + // Some assumptions: + // * Metaclasses never have a Swift initializer. + // * Root classes never have a Swift initializer. + // (These two together avoid initialization order problems at the root.) + // * Unrealized non-Swift classes have no Swift ancestry. + // * Unrealized Swift classes with no initializer have no ancestry that + // does have the initializer. + // (These two together mean we don't need to scan superclasses here + // and we don't need to worry about Swift superclasses inside + // realizeClassWithoutSwift()). + + // fixme some of these assumptions will be wrong + // if we add support for ObjC sublasses of Swift classes. + +#if DEBUG + runtimeLock.lock(); + ASSERT(remapClass(cls) == cls); + ASSERT(cls->isSwiftStable_ButAllowLegacyForNow()); + ASSERT(!cls->isMetaClassMaybeUnrealized()); + ASSERT(cls->superclass); + runtimeLock.unlock(); +#endif + + // Look for a Swift metadata initialization function + // installed on the class. If it is present we call it. + // That function in turn initializes the Swift metadata, + // prepares the "compiler-generated" ObjC metadata if not + // already present, and calls _objc_realizeSwiftClass() to finish + // our own initialization. + + if (auto init = cls->swiftMetadataInitializer()) { + if (PrintConnecting) { + _objc_inform("CLASS: calling Swift metadata initializer " + "for class '%s' (%p)", cls->nameForLogging(), cls); + } + + Class newcls = init(cls, nil); + + // fixme someday Swift will need to relocate classes at this point, + // but we don't accept that yet. + if (cls != newcls) { + mutex_locker_t lock(runtimeLock); + addRemappedClass(cls, newcls); + } + + return newcls; + } + else { + // No Swift-side initialization callback. + // Perform our own realization directly. + mutex_locker_t lock(runtimeLock); + return realizeClassWithoutSwift(cls, nil); + } +} + + +/*********************************************************************** +* realizeClassMaybeSwift (MaybeRelock / AndUnlock / AndLeaveLocked) +* Realize a class that might be a Swift class. +* Returns the real class structure for the class. +* Locking: +* runtimeLock must be held on entry +* runtimeLock may be dropped during execution +* ...AndUnlock function leaves runtimeLock unlocked on exit +* ...AndLeaveLocked re-acquires runtimeLock if it was dropped +* This complication avoids repeated lock transitions in some cases. +**********************************************************************/ +static Class +realizeClassMaybeSwiftMaybeRelock(Class cls, mutex_t& lock, bool leaveLocked) +{ + lock.assertLocked(); + + if (!cls->isSwiftStable_ButAllowLegacyForNow()) { + // Non-Swift class. Realize it now with the lock still held. + // fixme wrong in the future for objc subclasses of swift classes + realizeClassWithoutSwift(cls, nil); + if (!leaveLocked) lock.unlock(); + } else { + // Swift class. We need to drop locks and call the Swift + // runtime to initialize it. + lock.unlock(); + cls = realizeSwiftClass(cls); + ASSERT(cls->isRealized()); // callback must have provoked realization + if (leaveLocked) lock.lock(); + } + + return cls; +} + +static Class +realizeClassMaybeSwiftAndUnlock(Class cls, mutex_t& lock) +{ + return realizeClassMaybeSwiftMaybeRelock(cls, lock, false); +} + +static Class +realizeClassMaybeSwiftAndLeaveLocked(Class cls, mutex_t& lock) +{ + return realizeClassMaybeSwiftMaybeRelock(cls, lock, true); +} + + /*********************************************************************** * missingWeakSuperclass * Return YES if some superclass of cls was weak-linked and is missing. @@ -1818,7 +2790,7 @@ static Class realizeClass(Class cls) static bool missingWeakSuperclass(Class cls) { - assert(!cls->isRealized()); + ASSERT(!cls->isRealized()); if (!cls->superclass) { // superclass nil. This is normal for root classes only. @@ -1826,8 +2798,8 @@ missingWeakSuperclass(Class cls) } else { // superclass not nil. Check if a higher superclass is missing. Class supercls = remapClass(cls->superclass); - assert(cls != cls->superclass); - assert(cls != supercls); + ASSERT(cls != cls->superclass); + ASSERT(cls != supercls); if (!supercls) return YES; if (supercls->isRealized()) return NO; return missingWeakSuperclass(supercls); @@ -1839,20 +2811,24 @@ missingWeakSuperclass(Class cls) * realizeAllClassesInImage * Non-lazily realizes all unrealized classes in the given image. * Locking: runtimeLock must be held by the caller. +* Locking: this function may drop and re-acquire the lock. **********************************************************************/ static void realizeAllClassesInImage(header_info *hi) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); size_t count, i; - classref_t *classlist; + classref_t const *classlist; if (hi->areAllClassesRealized()) return; classlist = _getObjc2ClassList(hi, &count); for (i = 0; i < count; i++) { - realizeClass(remapClass(classlist[i])); + Class cls = remapClass(classlist[i]); + if (cls) { + realizeClassMaybeSwiftAndLeaveLocked(cls, runtimeLock); + } } hi->setAllClassesRealized(YES); @@ -1863,14 +2839,19 @@ static void realizeAllClassesInImage(header_info *hi) * realizeAllClasses * Non-lazily realizes all unrealized classes in all known images. * Locking: runtimeLock must be held by the caller. +* Locking: this function may drop and re-acquire the lock. +* Dropping the lock makes this function thread-unsafe with respect +* to concurrent image unload, but the callers of this function +* already ultimately do something that is also thread-unsafe with +* respect to image unload (such as using the list of all classes). **********************************************************************/ static void realizeAllClasses(void) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); header_info *hi; for (hi = FirstHeader; hi; hi = hi->getNext()) { - realizeAllClassesInImage(hi); + realizeAllClassesInImage(hi); // may drop and re-acquire runtimeLock } } @@ -1884,7 +2865,7 @@ static void realizeAllClasses(void) **********************************************************************/ Class _objc_allocateFutureClass(const char *name) { - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); Class cls; NXMapTable *map = futureNamedClasses(); @@ -1946,18 +2927,21 @@ BOOL _class_isFutureClass(Class cls) **********************************************************************/ static void flushCaches(Class cls) { - runtimeLock.assertWriting(); - + runtimeLock.assertLocked(); +#if CONFIG_USE_CACHE_LOCK mutex_locker_t lock(cacheUpdateLock); +#endif if (cls) { - foreach_realized_class_and_subclass(cls, ^(Class c){ + foreach_realized_class_and_subclass(cls, [](Class c){ cache_erase_nolock(c); + return true; }); } else { - foreach_realized_class_and_metaclass(^(Class c){ + foreach_realized_class_and_metaclass([](Class c){ cache_erase_nolock(c); + return true; }); } } @@ -1966,7 +2950,7 @@ static void flushCaches(Class cls) void _objc_flush_caches(Class cls) { { - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); flushCaches(cls); if (cls && cls->superclass && cls != cls->getIsa()) { flushCaches(cls->getIsa()); @@ -1978,7 +2962,11 @@ void _objc_flush_caches(Class cls) if (!cls) { // collectALot if cls==nil +#if CONFIG_USE_CACHE_LOCK mutex_locker_t lock(cacheUpdateLock); +#else + mutex_locker_t lock(runtimeLock); +#endif cache_collect(true); } } @@ -1992,14 +2980,91 @@ void _objc_flush_caches(Class cls) * Locking: write-locks runtimeLock **********************************************************************/ void -map_2_images(unsigned count, const char * const paths[], - const struct mach_header * const mhdrs[]) +map_images(unsigned count, const char * const paths[], + const struct mach_header * const mhdrs[]) { - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return map_images_nolock(count, paths, mhdrs); } +static void load_categories_nolock(header_info *hi) { + bool hasClassProperties = hi->info()->hasCategoryClassProperties(); + + size_t count; + auto processCatlist = [&](category_t * const *catlist) { + for (unsigned i = 0; i < count; i++) { + category_t *cat = catlist[i]; + Class cls = remapClass(cat->cls); + locstamped_category_t lc{cat, hi}; + + if (!cls) { + // Category's target class is missing (probably weak-linked). + // Ignore the category. + if (PrintConnecting) { + _objc_inform("CLASS: IGNORING category \?\?\?(%s) %p with " + "missing weak-linked target class", + cat->name, cat); + } + continue; + } + + // Process this category. + if (cls->isStubClass()) { + // Stub classes are never realized. Stub classes + // don't know their metaclass until they're + // initialized, so we have to add categories with + // class methods or properties to the stub itself. + // methodizeClass() will find them and add them to + // the metaclass as appropriate. + if (cat->instanceMethods || + cat->protocols || + cat->instanceProperties || + cat->classMethods || + cat->protocols || + (hasClassProperties && cat->_classProperties)) + { + objc::unattachedCategories.addForClass(lc, cls); + } + } else { + // First, register the category with its target class. + // Then, rebuild the class's method lists (etc) if + // the class is realized. + if (cat->instanceMethods || cat->protocols + || cat->instanceProperties) + { + if (cls->isRealized()) { + attachCategories(cls, &lc, 1, ATTACH_EXISTING); + } else { + objc::unattachedCategories.addForClass(lc, cls); + } + } + + if (cat->classMethods || cat->protocols + || (hasClassProperties && cat->_classProperties)) + { + if (cls->ISA()->isRealized()) { + attachCategories(cls->ISA(), &lc, 1, ATTACH_EXISTING | ATTACH_METACLASS); + } else { + objc::unattachedCategories.addForClass(lc, cls->ISA()); + } + } + } + } + }; + + processCatlist(_getObjc2CategoryList(hi, &count)); + processCatlist(_getObjc2CategoryList2(hi, &count)); +} + +static void loadAllCategories() { + mutex_locker_t lock(runtimeLock); + + for (auto *hi = FirstHeader; hi != NULL; hi = hi->getNext()) { + load_categories_nolock(hi); + } +} + /*********************************************************************** * load_images * Process +load in the given images which are being mapped in by dyld. @@ -2012,6 +3077,11 @@ extern void prepare_load_methods(const headerType *mhdr); void load_images(const char *path __unused, const struct mach_header *mh) { + if (!didInitialAttachCategories && didCallDyldNotifyRegister) { + didInitialAttachCategories = true; + loadAllCategories(); + } + // Return without taking locks if there are no +load methods here. if (!hasLoadMethods((const headerType *)mh)) return; @@ -2019,7 +3089,7 @@ load_images(const char *path __unused, const struct mach_header *mh) // Discover load methods { - rwlock_writer_t lock2(runtimeLock); + mutex_locker_t lock2(runtimeLock); prepare_load_methods((const headerType *)mh); } @@ -2038,23 +3108,21 @@ void unmap_image(const char *path __unused, const struct mach_header *mh) { recursive_mutex_locker_t lock(loadMethodLock); - rwlock_writer_t lock2(runtimeLock); + mutex_locker_t lock2(runtimeLock); unmap_image_nolock(mh); } - - /*********************************************************************** * mustReadClasses * Preflight check in advance of readClass() from an image. **********************************************************************/ -bool mustReadClasses(header_info *hi) +bool mustReadClasses(header_info *hi, bool hasDyldRoots) { const char *reason; // If the image is not preoptimized then we must read classes. - if (!hi->isPreoptimized()) { + if (!hi->hasPreoptimizedClasses()) { reason = nil; // Don't log this one because it is noisy. goto readthem; } @@ -2065,10 +3133,10 @@ bool mustReadClasses(header_info *hi) goto readthem; #endif - assert(!hi->isBundle()); // no MH_BUNDLE in shared cache + ASSERT(!hi->isBundle()); // no MH_BUNDLE in shared cache // If the image may have missing weak superclasses then we must read classes - if (!noMissingWeakSuperclasses()) { + if (!noMissingWeakSuperclasses() || hasDyldRoots) { reason = "the image may contain classes with missing weak superclasses"; goto readthem; } @@ -2079,6 +3147,20 @@ bool mustReadClasses(header_info *hi) goto readthem; } + // readClass() rewrites bits in backward-deploying Swift stable ABI code. + // The assumption here is there there are no such classes + // in the dyld shared cache. +#if DEBUG + { + size_t count; + classref_t const *classlist = _getObjc2ClassList(hi, &count); + for (size_t i = 0; i < count; i++) { + Class cls = remapClass(classlist[i]); + ASSERT(!cls->isUnfixedBackwardDeployingStableSwift()); + } + } +#endif + // readClass() does not need to do anything. return NO; @@ -2121,21 +3203,7 @@ Class readClass(Class cls, bool headerIsBundle, bool headerIsPreoptimized) return nil; } - // Note: Class __ARCLite__'s hack does not go through here. - // Class structure fixups that apply to it also need to be - // performed in non-lazy realization below. - - // These fields should be set to zero because of the - // binding of _objc_empty_vtable, but OS X 10.8's dyld - // does not bind shared cache absolute symbols as expected. - // This (and the __ARCLite__ hack below) can be removed - // once the simulator drops 10.8 support. -#if TARGET_OS_SIMULATOR - if (cls->cache._mask) cls->cache._mask = 0; - if (cls->cache._occupied) cls->cache._occupied = 0; - if (cls->ISA()->cache._mask) cls->ISA()->cache._mask = 0; - if (cls->ISA()->cache._occupied) cls->ISA()->cache._occupied = 0; -#endif + cls->fixupBackwardDeployingStableSwift(); Class replacing = nil; if (Class newCls = popFutureNamedClass(mangledName)) { @@ -2143,16 +3211,16 @@ Class readClass(Class cls, bool headerIsBundle, bool headerIsPreoptimized) // Copy objc_class to future class's struct. // Preserve future's rw data block. - if (newCls->isSwift()) { + if (newCls->isAnySwift()) { _objc_fatal("Can't complete future class request for '%s' " "because the real class is too big.", cls->nameForLogging()); } class_rw_t *rw = newCls->data(); - const class_ro_t *old_ro = rw->ro; + const class_ro_t *old_ro = rw->ro(); memcpy(newCls, cls, sizeof(objc_class)); - rw->ro = (class_ro_t *)newCls->data(); + rw->set_ro((class_ro_t *)newCls->data()); newCls->setData(rw); freeIfMutable((char *)old_ro->name); free((void *)old_ro); @@ -2166,12 +3234,13 @@ Class readClass(Class cls, bool headerIsBundle, bool headerIsPreoptimized) if (headerIsPreoptimized && !replacing) { // class list built in shared cache // fixme strict assert doesn't work because of duplicates - // assert(cls == getClass(name)); - assert(getClass(mangledName)); + // ASSERT(cls == getClass(name)); + ASSERT(getClassExceptSomeSwift(mangledName)); } else { addNamedClass(cls, mangledName, replacing); + addClassTableEntry(cls); } - + // for future reference: shared cache never contains MH_BUNDLEs if (headerIsBundle) { cls->data()->flags |= RO_FROM_BUNDLE; @@ -2198,11 +3267,29 @@ readProtocol(protocol_t *newproto, Class protocol_class, protocol_t *oldproto = (protocol_t *)getProtocol(newproto->mangledName); if (oldproto) { - // Some other definition already won. - if (PrintProtocols) { - _objc_inform("PROTOCOLS: protocol at %p is %s " - "(duplicate of %p)", - newproto, oldproto->nameForLogging(), oldproto); + if (oldproto != newproto) { + // Some other definition already won. + if (PrintProtocols) { + _objc_inform("PROTOCOLS: protocol at %p is %s " + "(duplicate of %p)", + newproto, oldproto->nameForLogging(), oldproto); + } + + // If we are a shared cache binary then we have a definition of this + // protocol, but if another one was chosen then we need to clear our + // isCanonical bit so that no-one trusts it. + // Note, if getProtocol returned a shared cache protocol then the + // canonical definition is already in the shared cache and we don't + // need to do anything. + if (headerIsPreoptimized && !oldproto->isCanonical()) { + // Note newproto is an entry in our __objc_protolist section which + // for shared cache binaries points to the original protocol in + // that binary, not the shared cache uniqued one. + auto cacheproto = (protocol_t *) + getSharedCachePreoptimizedProtocol(newproto->mangledName); + if (cacheproto && cacheproto->isCanonical()) + cacheproto->clearIsCanonical(); + } } } else if (headerIsPreoptimized) { @@ -2223,8 +3310,8 @@ readProtocol(protocol_t *newproto, Class protocol_class, installedproto = newproto; } - assert(installedproto->getIsa() == protocol_class); - assert(installedproto->size >= sizeof(protocol_t)); + ASSERT(installedproto->getIsa() == protocol_class); + ASSERT(installedproto->size >= sizeof(protocol_t)); insertFn(protocol_map, installedproto->mangledName, installedproto); @@ -2290,9 +3377,10 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un Class *resolvedFutureClasses = nil; size_t resolvedFutureClassCount = 0; static bool doneOnce; + bool launchTime = NO; TimeLogger ts(PrintImageTimes); - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); #define EACH_HEADER \ hIndex = 0; \ @@ -2301,6 +3389,7 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un if (!doneOnce) { doneOnce = YES; + launchTime = YES; #if SUPPORT_NONPOINTER_ISA // Disable non-pointer isa under some conditions. @@ -2309,7 +3398,7 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un // Disable nonpointer isa if any image contains old Swift code for (EACH_HEADER) { if (hi->info()->containsSwift() && - hi->info()->swiftVersion() < objc_image_info::SwiftVersion3) + hi->info()->swiftUnstableVersion() < objc_image_info::SwiftVersion3) { DisableNonpointerIsa = true; if (PrintRawIsa) { @@ -2356,6 +3445,8 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un disableTaggedPointers(); } + initializeTaggedPointerObfuscator(); + if (PrintConnecting) { _objc_inform("CLASS: found %d classes during launch", totalClasses); } @@ -2371,19 +3462,42 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un ts.log("IMAGE TIMES: first time tasks"); } + // Fix up @selector references + static size_t UnfixedSelectors; + { + mutex_locker_t lock(selLock); + for (EACH_HEADER) { + if (hi->hasPreoptimizedSelectors()) continue; + + bool isBundle = hi->isBundle(); + SEL *sels = _getObjc2SelectorRefs(hi, &count); + UnfixedSelectors += count; + for (i = 0; i < count; i++) { + const char *name = sel_cname(sels[i]); + SEL sel = sel_registerNameNoLock(name, isBundle); + if (sels[i] != sel) { + sels[i] = sel; + } + } + } + } + + ts.log("IMAGE TIMES: fix up selector references"); // Discover classes. Fix up unresolved future classes. Mark bundle classes. + bool hasDyldRoots = dyld_shared_cache_some_image_overridden(); for (EACH_HEADER) { - if (! mustReadClasses(hi)) { + if (! mustReadClasses(hi, hasDyldRoots)) { // Image is sufficiently optimized that we need not call readClass() continue; } - bool headerIsBundle = hi->isBundle(); - bool headerIsPreoptimized = hi->isPreoptimized(); + classref_t const *classlist = _getObjc2ClassList(hi, &count); + + bool headerIsBundle = hi->isBundle(); + bool headerIsPreoptimized = hi->hasPreoptimizedClasses(); - classref_t *classlist = _getObjc2ClassList(hi, &count); for (i = 0; i < count; i++) { Class cls = (Class)classlist[i]; Class newCls = readClass(cls, headerIsBundle, headerIsPreoptimized); @@ -2422,24 +3536,6 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un ts.log("IMAGE TIMES: remap classes"); - // Fix up @selector references - static size_t UnfixedSelectors; - sel_lock(); - for (EACH_HEADER) { - if (hi->isPreoptimized()) continue; - - bool isBundle = hi->isBundle(); - SEL *sels = _getObjc2SelectorRefs(hi, &count); - UnfixedSelectors += count; - for (i = 0; i < count; i++) { - const char *name = sel_cname(sels[i]); - sels[i] = sel_registerNameNoLock(name, isBundle); - } - } - sel_unlock(); - - ts.log("IMAGE TIMES: fix up selector references"); - #if SUPPORT_FIXUP // Fix up old objc_msgSend_fixup call sites for (EACH_HEADER) { @@ -2458,16 +3554,33 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un ts.log("IMAGE TIMES: fix up objc_msgSend_fixup"); #endif + bool cacheSupportsProtocolRoots = sharedCacheSupportsProtocolRoots(); + // Discover protocols. Fix up protocol refs. for (EACH_HEADER) { extern objc_class OBJC_CLASS_$_Protocol; Class cls = (Class)&OBJC_CLASS_$_Protocol; - assert(cls); + ASSERT(cls); NXMapTable *protocol_map = protocols(); - bool isPreoptimized = hi->isPreoptimized(); + bool isPreoptimized = hi->hasPreoptimizedProtocols(); + + // Skip reading protocols if this is an image from the shared cache + // and we support roots + // Note, after launch we do need to walk the protocol as the protocol + // in the shared cache is marked with isCanonical() and that may not + // be true if some non-shared cache binary was chosen as the canonical + // definition + if (launchTime && isPreoptimized && cacheSupportsProtocolRoots) { + if (PrintProtocols) { + _objc_inform("PROTOCOLS: Skipping reading protocols in image: %s", + hi->fname()); + } + continue; + } + bool isBundle = hi->isBundle(); - protocol_t **protolist = _getObjc2ProtocolList(hi, &count); + protocol_t * const *protolist = _getObjc2ProtocolList(hi, &count); for (i = 0; i < count; i++) { readProtocol(protolist[i], cls, protocol_map, isPreoptimized, isBundle); @@ -2480,6 +3593,12 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un // Preoptimized images may have the right // answer already but we don't know for sure. for (EACH_HEADER) { + // At launch time, we know preoptimized image refs are pointing at the + // shared cache definition of a protocol. We can skip the check on + // launch, but have to visit @protocol refs for shared cache images + // loaded later. + if (launchTime && cacheSupportsProtocolRoots && hi->isPreoptimized()) + continue; protocol_t **protolist = _getObjc2ProtocolRefs(hi, &count); for (i = 0; i < count; i++) { remapProtocolRef(&protolist[i]); @@ -2488,31 +3607,45 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un ts.log("IMAGE TIMES: fix up @protocol references"); + // Discover categories. Only do this after the initial category + // attachment has been done. For categories present at startup, + // discovery is deferred until the first load_images call after + // the call to _dyld_objc_notify_register completes. rdar://problem/53119145 + if (didInitialAttachCategories) { + for (EACH_HEADER) { + load_categories_nolock(hi); + } + } + + ts.log("IMAGE TIMES: discover categories"); + + // Category discovery MUST BE Late to avoid potential races + // when other threads call the new category code before + // this thread finishes its fixups. + + // +load handled by prepare_load_methods() + // Realize non-lazy classes (for +load methods and static instances) for (EACH_HEADER) { - classref_t *classlist = + classref_t const *classlist = _getObjc2NonlazyClassList(hi, &count); for (i = 0; i < count; i++) { Class cls = remapClass(classlist[i]); if (!cls) continue; - // hack for class __ARCLite__, which didn't get this above -#if TARGET_OS_SIMULATOR - if (cls->cache._buckets == (void*)&_objc_empty_cache && - (cls->cache._mask || cls->cache._occupied)) - { - cls->cache._mask = 0; - cls->cache._occupied = 0; - } - if (cls->ISA()->cache._buckets == (void*)&_objc_empty_cache && - (cls->ISA()->cache._mask || cls->ISA()->cache._occupied)) - { - cls->ISA()->cache._mask = 0; - cls->ISA()->cache._occupied = 0; - } -#endif + addClassTableEntry(cls); - realizeClass(cls); + if (cls->isSwiftStable()) { + if (cls->swiftMetadataInitializer()) { + _objc_fatal("Swift class %s with a metadata initializer " + "is not allowed to be non-lazy", + cls->nameForLogging()); + } + // fixme also disallow relocatable classes + // We can't disallow all Swift classes because of + // classes like Swift.__EmptyArrayStorage + } + realizeClassWithoutSwift(cls, nil); } } @@ -2521,78 +3654,17 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un // Realize newly-resolved future classes, in case CF manipulates them if (resolvedFutureClasses) { for (i = 0; i < resolvedFutureClassCount; i++) { - realizeClass(resolvedFutureClasses[i]); - resolvedFutureClasses[i]->setInstancesRequireRawIsa(false/*inherited*/); + Class cls = resolvedFutureClasses[i]; + if (cls->isSwiftStable()) { + _objc_fatal("Swift class is not allowed to be future"); + } + realizeClassWithoutSwift(cls, nil); + cls->setInstancesRequireRawIsaRecursively(false/*inherited*/); } free(resolvedFutureClasses); - } - - ts.log("IMAGE TIMES: realize future classes"); - - // Discover categories. - for (EACH_HEADER) { - category_t **catlist = - _getObjc2CategoryList(hi, &count); - bool hasClassProperties = hi->info()->hasCategoryClassProperties(); - - for (i = 0; i < count; i++) { - category_t *cat = catlist[i]; - Class cls = remapClass(cat->cls); - - if (!cls) { - // Category's target class is missing (probably weak-linked). - // Disavow any knowledge of this category. - catlist[i] = nil; - if (PrintConnecting) { - _objc_inform("CLASS: IGNORING category \?\?\?(%s) %p with " - "missing weak-linked target class", - cat->name, cat); - } - continue; - } - - // Process this category. - // First, register the category with its target class. - // Then, rebuild the class's method lists (etc) if - // the class is realized. - bool classExists = NO; - if (cat->instanceMethods || cat->protocols - || cat->instanceProperties) - { - addUnattachedCategoryForClass(cat, cls, hi); - if (cls->isRealized()) { - remethodizeClass(cls); - classExists = YES; - } - if (PrintConnecting) { - _objc_inform("CLASS: found category -%s(%s) %s", - cls->nameForLogging(), cat->name, - classExists ? "on existing class" : ""); - } - } - - if (cat->classMethods || cat->protocols - || (hasClassProperties && cat->_classProperties)) - { - addUnattachedCategoryForClass(cat, cls->ISA(), hi); - if (cls->ISA()->isRealized()) { - remethodizeClass(cls->ISA()); - } - if (PrintConnecting) { - _objc_inform("CLASS: found category +%s(%s)", - cls->nameForLogging(), cat->name); - } - } - } } - ts.log("IMAGE TIMES: discover categories"); - - // Category discovery MUST BE LAST to avoid potential races - // when other threads call the new category code before - // this thread finishes its fixups. - - // +load handled by prepare_load_methods() + ts.log("IMAGE TIMES: realize future classes"); if (DebugNonFragileIvars) { realizeAllClasses(); @@ -2607,7 +3679,7 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un static unsigned int PreoptOptimizedClasses; for (EACH_HEADER) { - if (hi->isPreoptimized()) { + if (hi->hasPreoptimizedSelectors()) { _objc_inform("PREOPTIMIZATION: honoring preoptimized selectors " "in %s", hi->fname()); } @@ -2616,13 +3688,13 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un "in %s", hi->fname()); } - classref_t *classlist = _getObjc2ClassList(hi, &count); + classref_t const *classlist = _getObjc2ClassList(hi, &count); for (i = 0; i < count; i++) { Class cls = remapClass(classlist[i]); if (!cls) continue; PreoptTotalClasses++; - if (hi->isPreoptimized()) { + if (hi->hasPreoptimizedClasses()) { PreoptOptimizedClasses++; } @@ -2672,7 +3744,7 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un static void schedule_class_load(Class cls) { if (!cls) return; - assert(cls->isRealized()); // _read_images should realize + ASSERT(cls->isRealized()); // _read_images should realize if (cls->data()->flags & RW_LOADED) return; @@ -2696,21 +3768,25 @@ void prepare_load_methods(const headerType *mhdr) { size_t count, i; - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); - classref_t *classlist = + classref_t const *classlist = _getObjc2NonlazyClassList(mhdr, &count); for (i = 0; i < count; i++) { schedule_class_load(remapClass(classlist[i])); } - category_t **categorylist = _getObjc2NonlazyCategoryList(mhdr, &count); + category_t * const *categorylist = _getObjc2NonlazyCategoryList(mhdr, &count); for (i = 0; i < count; i++) { category_t *cat = categorylist[i]; Class cls = remapClass(cat->cls); if (!cls) continue; // category for ignored weak-linked class - realizeClass(cls); - assert(cls->ISA()->isRealized()); + if (cls->isSwiftStable()) { + _objc_fatal("Swift class extensions and categories on Swift " + "classes are not allowed to have +load methods"); + } + realizeClassWithoutSwift(cls, nil); + ASSERT(cls->ISA()->isRealized()); add_category_to_loadable_list(cat); } } @@ -2726,21 +3802,22 @@ void _unload_image(header_info *hi) size_t count, i; loadMethodLock.assertLocked(); - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); // Unload unattached categories and categories waiting for +load. - category_t **catlist = _getObjc2CategoryList(hi, &count); + // Ignore __objc_catlist2. We don't support unloading Swift + // and we never will. + category_t * const *catlist = _getObjc2CategoryList(hi, &count); for (i = 0; i < count; i++) { category_t *cat = catlist[i]; - if (!cat) continue; // category for ignored weak-linked class Class cls = remapClass(cat->cls); - assert(cls); // shouldn't have live category for dead class + if (!cls) continue; // category for ignored weak-linked class // fixme for MH_DYLIB cat's class may have been unloaded already // unattached list - removeUnattachedCategoryForClass(cat, cls); + objc::unattachedCategories.eraseCategoryForClass(cat, cls); // +load queue remove_category_from_loadable_list(cat); @@ -2752,41 +3829,34 @@ void _unload_image(header_info *hi) // and __DATA,__objc_nlclslist. arclite's hack puts a class in the latter // only, and we need to unload that class if we unload an arclite image. - NXHashTable *classes = NXCreateHashTable(NXPtrPrototype, 0, nil); - classref_t *classlist; + objc::DenseSet classes{}; + classref_t const *classlist; classlist = _getObjc2ClassList(hi, &count); for (i = 0; i < count; i++) { Class cls = remapClass(classlist[i]); - if (cls) NXHashInsert(classes, cls); + if (cls) classes.insert(cls); } classlist = _getObjc2NonlazyClassList(hi, &count); for (i = 0; i < count; i++) { Class cls = remapClass(classlist[i]); - if (cls) NXHashInsert(classes, cls); + if (cls) classes.insert(cls); } // First detach classes from each other. Then free each class. // This avoid bugs where this loop unloads a subclass before its superclass - NXHashState hs; - Class cls; - - hs = NXInitHashState(classes); - while (NXNextHashState(classes, &hs, (void**)&cls)) { + for (Class cls: classes) { remove_class_from_loadable_list(cls); detach_class(cls->ISA(), YES); detach_class(cls, NO); } - hs = NXInitHashState(classes); - while (NXNextHashState(classes, &hs, (void**)&cls)) { + for (Class cls: classes) { free_class(cls->ISA()); free_class(cls); } - NXFreeHashTable(classes); - // XXX FIXME -- Clean up protocols: // Support unloading protocols at dylib/image unload time @@ -2826,7 +3896,7 @@ method_getName(Method m) { if (!m) return nil; - assert(m->name == sel_registerName(sel_getName(m->name))); + ASSERT(m->name == sel_registerName(sel_getName(m->name))); return m->name; } @@ -2853,7 +3923,7 @@ method_getTypeEncoding(Method m) static IMP _method_setImplementation(Class cls, method_t *m, IMP imp) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); if (!m) return nil; if (!imp) return nil; @@ -2867,7 +3937,7 @@ _method_setImplementation(Class cls, method_t *m, IMP imp) flushCaches(cls); - updateCustomRR_AWZ(cls, m); + adjustCustomFlagsForMethodChange(cls, m); return old; } @@ -2877,7 +3947,7 @@ method_setImplementation(Method m, IMP imp) { // Don't know the class - will be slow if RR/AWZ are affected // fixme build list of classes whose Methods are known externally? - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return _method_setImplementation(Nil, m, imp); } @@ -2886,7 +3956,7 @@ void method_exchangeImplementations(Method m1, Method m2) { if (!m1 || !m2) return; - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); IMP m1_imp = m1->imp; m1->imp = m2->imp; @@ -2899,8 +3969,8 @@ void method_exchangeImplementations(Method m1, Method m2) flushCaches(nil); - updateCustomRR_AWZ(nil, m1); - updateCustomRR_AWZ(nil, m2); + adjustCustomFlagsForMethodChange(nil, m1); + adjustCustomFlagsForMethodChange(nil, m2); } @@ -2962,7 +4032,7 @@ objc_property_attribute_t *property_copyAttributeList(objc_property_t prop, return nil; } - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return copyPropertyAttributeList(prop->attributes,outCount); } @@ -2970,7 +4040,7 @@ char * property_copyAttributeValue(objc_property_t prop, const char *name) { if (!prop || !name || *name == '\0') return nil; - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return copyPropertyAttributeValue(prop->attributes, name); } @@ -3042,7 +4112,7 @@ static void fixupProtocolMethodList(protocol_t *proto, method_list_t *mlist, bool required, bool instance) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); if (!mlist) return; if (mlist->isFixedUp()) return; @@ -3081,7 +4151,7 @@ fixupProtocolMethodList(protocol_t *proto, method_list_t *mlist, static void fixupProtocol(protocol_t *proto) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); if (proto->protocols) { for (uintptr_t i = 0; i < proto->protocols->count; i++) { @@ -3109,10 +4179,10 @@ static void fixupProtocolIfNeeded(protocol_t *proto) { runtimeLock.assertUnlocked(); - assert(proto); + ASSERT(proto); if (!proto->isFixedUp()) { - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); fixupProtocol(proto); } } @@ -3153,7 +4223,7 @@ protocol_getMethod_nolock(protocol_t *proto, SEL sel, if (!proto || !sel) return nil; - assert(proto->isFixedUp()); + ASSERT(proto->isFixedUp()); method_list_t *mlist = getProtocolMethodList(proto, isRequiredMethod, isInstanceMethod); @@ -3188,7 +4258,7 @@ protocol_getMethod(protocol_t *proto, SEL sel, bool isRequiredMethod, bool isIns if (!proto) return nil; fixupProtocolIfNeeded(proto); - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return protocol_getMethod_nolock(proto, sel, isRequiredMethod, isInstanceMethod, recursive); } @@ -3198,7 +4268,7 @@ protocol_getMethod(protocol_t *proto, SEL sel, bool isRequiredMethod, bool isIns * protocol_getMethodTypeEncoding_nolock * Return the @encode string for the requested protocol method. * Returns nil if the compiler did not emit any extended @encode data. -* Locking: runtimeLock must be held for writing by the caller +* Locking: runtimeLock must be held by the caller **********************************************************************/ const char * protocol_getMethodTypeEncoding_nolock(protocol_t *proto, SEL sel, @@ -3210,7 +4280,7 @@ protocol_getMethodTypeEncoding_nolock(protocol_t *proto, SEL sel, if (!proto) return nil; if (!proto->extendedMethodTypes()) return nil; - assert(proto->isFixedUp()); + ASSERT(proto->isFixedUp()); method_t *m = protocol_getMethod_nolock(proto, sel, @@ -3249,7 +4319,7 @@ _protocol_getMethodTypeEncoding(Protocol *proto_gen, SEL sel, if (!proto) return nil; fixupProtocolIfNeeded(proto); - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return protocol_getMethodTypeEncoding_nolock(proto, sel, isRequiredMethod, isInstanceMethod); @@ -3264,7 +4334,7 @@ _protocol_getMethodTypeEncoding(Protocol *proto_gen, SEL sel, const char * protocol_t::demangledName() { - assert(hasDemangledNameField()); + ASSERT(hasDemangledNameField()); if (! _demangledName) { char *de = copySwiftV1DemangledName(mangledName, true/*isProtocol*/); @@ -3331,6 +4401,9 @@ protocol_conformsToProtocol_nolock(protocol_t *self, protocol_t *other) uintptr_t i; for (i = 0; i < self->protocols->count; i++) { protocol_t *proto = remapProtocol(self->protocols->list[i]); + if (other == proto) { + return YES; + } if (0 == strcmp(other->mangledName, proto->mangledName)) { return YES; } @@ -3351,7 +4424,7 @@ protocol_conformsToProtocol_nolock(protocol_t *self, protocol_t *other) **********************************************************************/ BOOL protocol_conformsToProtocol(Protocol *self, Protocol *other) { - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return protocol_conformsToProtocol_nolock(newprotocol(self), newprotocol(other)); } @@ -3395,7 +4468,7 @@ protocol_copyMethodDescriptionList(Protocol *p, fixupProtocolIfNeeded(proto); - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); method_list_t *mlist = getProtocolMethodList(proto, isRequiredMethod, isInstanceMethod); @@ -3461,7 +4534,7 @@ objc_property_t protocol_getProperty(Protocol *p, const char *name, { if (!p || !name) return nil; - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return (objc_property_t) protocol_getProperty_nolock(newprotocol(p), name, isRequiredProperty, isInstanceProperty); @@ -3508,7 +4581,7 @@ protocol_copyPropertyList2(Protocol *proto, unsigned int *outCount, return nil; } - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); property_list_t *plist = isInstanceProperty ? newprotocol(proto)->instanceProperties @@ -3542,7 +4615,7 @@ protocol_copyProtocolList(Protocol *p, unsigned int *outCount) return nil; } - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); if (proto->protocols) { count = (unsigned int)proto->protocols->count; @@ -3572,7 +4645,7 @@ protocol_copyProtocolList(Protocol *p, unsigned int *outCount) Protocol * objc_allocateProtocol(const char *name) { - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); if (getProtocol(name)) { return nil; @@ -3603,7 +4676,7 @@ void objc_registerProtocol(Protocol *proto_gen) { protocol_t *proto = newprotocol(proto_gen); - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); extern objc_class OBJC_CLASS_$___IncompleteProtocol; Class oldcls = (Class)&OBJC_CLASS_$___IncompleteProtocol; @@ -3625,7 +4698,11 @@ void objc_registerProtocol(Protocol *proto_gen) // have been retained and we must preserve that count. proto->changeIsa(cls); - NXMapKeyCopyingInsert(protocols(), proto->mangledName, proto); + // Don't add this protocol if we already have it. + // Should we warn on duplicates? + if (getProtocol(proto->mangledName) == nil) { + NXMapKeyCopyingInsert(protocols(), proto->mangledName, proto); + } } @@ -3648,7 +4725,7 @@ protocol_addProtocol(Protocol *proto_gen, Protocol *addition_gen) if (!proto_gen) return; if (!addition_gen) return; - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); if (proto->ISA() != cls) { _objc_inform("protocol_addProtocol: modified protocol '%s' is not " @@ -3711,7 +4788,7 @@ protocol_addMethodDescription(Protocol *proto_gen, SEL name, const char *types, if (!proto_gen) return; - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); if (proto->ISA() != cls) { _objc_inform("protocol_addMethodDescription: protocol '%s' is not " @@ -3769,7 +4846,7 @@ protocol_addProperty(Protocol *proto_gen, const char *name, if (!proto) return; if (!name) return; - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); if (proto->ISA() != cls) { _objc_inform("protocol_addProperty: protocol '%s' is not " @@ -3791,37 +4868,107 @@ protocol_addProperty(Protocol *proto_gen, const char *name, //} } - -/*********************************************************************** -* objc_getClassList -* Returns pointers to all classes. -* This requires all classes be realized, which is regretfully non-lazy. -* Locking: acquires runtimeLock -**********************************************************************/ -int -objc_getClassList(Class *buffer, int bufferLen) +static int +objc_getRealizedClassList_nolock(Class *buffer, int bufferLen) { - rwlock_writer_t lock(runtimeLock); - - realizeAllClasses(); - - __block int count = 0; - foreach_realized_class_and_metaclass(^(Class cls) { - if (!cls->isMetaClass()) count++; - }); + int count = 0; if (buffer) { - __block int c = 0; - foreach_realized_class_and_metaclass(^(Class cls) { - if (c < bufferLen && !cls->isMetaClass()) { - buffer[c++] = cls; + int c = 0; + foreach_realized_class([=, &count, &c](Class cls) { + count++; + if (c < bufferLen) { + buffer[c++] = cls; } + return true; + }); + } else { + foreach_realized_class([&count](Class cls) { + count++; + return true; }); } return count; } +static Class * +objc_copyRealizedClassList_nolock(unsigned int *outCount) +{ + Class *result = nil; + unsigned int count = 0; + + foreach_realized_class([&count](Class cls) { + count++; + return true; + }); + + if (count > 0) { + unsigned int c = 0; + + result = (Class *)malloc((1+count) * sizeof(Class)); + foreach_realized_class([=, &c](Class cls) { + result[c++] = cls; + return true; + }); + result[c] = nil; + } + + if (outCount) *outCount = count; + return result; +} + +static void +class_getImpCache_nolock(Class cls, cache_t &cache, objc_imp_cache_entry *buffer, int len) +{ + bucket_t *buckets = cache.buckets(); + + uintptr_t count = cache.capacity(); + uintptr_t index; + int wpos = 0; + + for (index = 0; index < count && wpos < len; index += 1) { + if (buckets[index].sel()) { + buffer[wpos].imp = buckets[index].imp(cls); + buffer[wpos].sel = buckets[index].sel(); + wpos++; + } + } +} + +/*********************************************************************** + * objc_getClassList + * Returns pointers to all classes. + * This requires all classes be realized, which is regretfully non-lazy. + * Locking: acquires runtimeLock + **********************************************************************/ +int +objc_getClassList(Class *buffer, int bufferLen) +{ + mutex_locker_t lock(runtimeLock); + + realizeAllClasses(); + + return objc_getRealizedClassList_nolock(buffer, bufferLen); +} + +/*********************************************************************** + * objc_copyClassList + * Returns pointers to Realized classes. + * + * outCount may be nil. *outCount is the number of classes returned. + * If the returned array is not nil, it is nil-terminated and must be + * freed with free(). + * Locking: write-locks runtimeLock + **********************************************************************/ +Class * +objc_copyRealizedClassList(unsigned int *outCount) +{ + mutex_locker_t lock(runtimeLock); + + return objc_copyRealizedClassList_nolock(outCount); +} + /*********************************************************************** * objc_copyClassList @@ -3836,30 +4983,43 @@ objc_getClassList(Class *buffer, int bufferLen) Class * objc_copyClassList(unsigned int *outCount) { - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); realizeAllClasses(); - Class *result = nil; + return objc_copyRealizedClassList_nolock(outCount); +} - __block unsigned int count = 0; - foreach_realized_class_and_metaclass(^(Class cls) { - if (!cls->isMetaClass()) count++; - }); +/*********************************************************************** + * class_copyImpCache + * Returns the current content of the Class IMP Cache + * + * outCount may be nil. *outCount is the number of entries returned. + * If the returned array is not nil, it is nil-terminated and must be + * freed with free(). + * Locking: write-locks cacheUpdateLock + **********************************************************************/ +objc_imp_cache_entry * +class_copyImpCache(Class cls, int *outCount) +{ + objc_imp_cache_entry *buffer = nullptr; - if (count > 0) { - result = (Class *)malloc((1+count) * sizeof(Class)); - __block unsigned int c = 0; - foreach_realized_class_and_metaclass(^(Class cls) { - if (!cls->isMetaClass()) { - result[c++] = cls; - } - }); - result[c] = nil; +#if CONFIG_USE_CACHE_LOCK + mutex_locker_t lock(cacheUpdateLock); +#else + mutex_locker_t lock(runtimeLock); +#endif + + cache_t &cache = cls->cache; + int count = (int)cache.occupied(); + + if (count) { + buffer = (objc_imp_cache_entry *)calloc(1+count, sizeof(objc_imp_cache_entry)); + class_getImpCache_nolock(cls, cache, buffer, count); } if (outCount) *outCount = count; - return result; + return buffer; } @@ -3871,11 +5031,40 @@ objc_copyClassList(unsigned int *outCount) Protocol * __unsafe_unretained * objc_copyProtocolList(unsigned int *outCount) { - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); NXMapTable *protocol_map = protocols(); - unsigned int count = NXCountMapTable(protocol_map); + // Find all the protocols from the pre-optimized images. These protocols + // won't be in the protocol map. + objc::DenseMap preoptimizedProtocols; + if (sharedCacheSupportsProtocolRoots()) { + header_info *hi; + for (hi = FirstHeader; hi; hi = hi->getNext()) { + if (!hi->hasPreoptimizedProtocols()) + continue; + + size_t count, i; + const protocol_t * const *protolist = _getObjc2ProtocolList(hi, &count); + for (i = 0; i < count; i++) { + const protocol_t* protocol = protolist[i]; + + // Skip protocols we have in the run time map. These likely + // correspond to protocols added dynamically which have the same + // name as a protocol found later in a dlopen'ed shared cache image. + if (NXMapGet(protocol_map, protocol->mangledName) != nil) + continue; + + // The protocols in the shared cache protolist point to their + // original on-disk object, not the optimized one. We can use the name + // to find the optimized one. + Protocol* optimizedProto = getPreoptimizedProtocol(protocol->mangledName); + preoptimizedProtocols.insert({ protocol->mangledName, optimizedProto }); + } + } + } + + unsigned int count = NXCountMapTable(protocol_map) + (unsigned int)preoptimizedProtocols.size(); if (count == 0) { if (outCount) *outCount = 0; return nil; @@ -3892,9 +5081,14 @@ objc_copyProtocolList(unsigned int *outCount) { result[i++] = proto; } + + // Add any protocols found in the pre-optimized table + for (auto it : preoptimizedProtocols) { + result[i++] = it.second; + } result[i++] = nil; - assert(i == count+1); + ASSERT(i == count+1); if (outCount) *outCount = count; return result; @@ -3908,7 +5102,7 @@ objc_copyProtocolList(unsigned int *outCount) **********************************************************************/ Protocol *objc_getProtocol(const char *name) { - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return getProtocol(name); } @@ -3929,17 +5123,18 @@ class_copyMethodList(Class cls, unsigned int *outCount) return nil; } - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); + const auto methods = cls->data()->methods(); - assert(cls->isRealized()); + ASSERT(cls->isRealized()); - count = cls->data()->methods.count(); + count = methods.count(); if (count > 0) { result = (Method *)malloc((count + 1) * sizeof(Method)); count = 0; - for (auto& meth : cls->data()->methods) { + for (auto& meth : methods) { result[count++] = &meth; } result[count] = nil; @@ -3967,11 +5162,11 @@ class_copyIvarList(Class cls, unsigned int *outCount) return nil; } - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); - assert(cls->isRealized()); + ASSERT(cls->isRealized()); - if ((ivars = cls->data()->ro->ivars) && ivars->count) { + if ((ivars = cls->data()->ro()->ivars) && ivars->count) { result = (Ivar *)malloc((ivars->count+1) * sizeof(Ivar)); for (auto& ivar : *ivars) { @@ -4001,18 +5196,21 @@ class_copyPropertyList(Class cls, unsigned int *outCount) return nil; } - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); - assert(cls->isRealized()); + checkIsKnownClass(cls); + ASSERT(cls->isRealized()); + auto rw = cls->data(); property_t **result = nil; - unsigned int count = rw->properties.count(); + auto const properties = rw->properties(); + unsigned int count = properties.count(); if (count > 0) { result = (property_t **)malloc((count + 1) * sizeof(property_t *)); count = 0; - for (auto& prop : rw->properties) { + for (auto& prop : properties) { result[count++] = ∝ } result[count] = nil; @@ -4036,12 +5234,12 @@ objc_class::getLoadMethod() const method_list_t *mlist; - assert(isRealized()); - assert(ISA()->isRealized()); - assert(!isMetaClass()); - assert(ISA()->isMetaClass()); + ASSERT(isRealized()); + ASSERT(ISA()->isRealized()); + ASSERT(!isMetaClass()); + ASSERT(ISA()->isMetaClass()); - mlist = ISA()->data()->ro->baseMethods(); + mlist = ISA()->data()->ro()->baseMethods(); if (mlist) { for (const auto& meth : *mlist) { const char *name = sel_cname(meth.name); @@ -4091,9 +5289,9 @@ _category_getClassName(Category cat) Class _category_getClass(Category cat) { - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); Class result = remapClass(cat->cls); - assert(result->isRealized()); // ok for call_category_loads' usage + ASSERT(result->isRealized()); // ok for call_category_loads' usage return result; } @@ -4155,17 +5353,20 @@ class_copyProtocolList(Class cls, unsigned int *outCount) return nil; } - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); + const auto protocols = cls->data()->protocols(); - assert(cls->isRealized()); + checkIsKnownClass(cls); + + ASSERT(cls->isRealized()); - count = cls->data()->protocols.count(); + count = protocols.count(); if (count > 0) { result = (Protocol **)malloc((count+1) * sizeof(Protocol *)); count = 0; - for (const auto& proto : cls->data()->protocols) { + for (const auto& proto : protocols) { result[count++] = (Protocol *)remapProtocol(proto); } result[count] = nil; @@ -4177,28 +5378,78 @@ class_copyProtocolList(Class cls, unsigned int *outCount) /*********************************************************************** -* _objc_copyClassNamesForImage -* fixme -* Locking: write-locks runtimeLock +* objc_copyImageNames +* Copies names of loaded images with ObjC contents. +* +* Locking: acquires runtimeLock +**********************************************************************/ +const char **objc_copyImageNames(unsigned int *outCount) +{ + mutex_locker_t lock(runtimeLock); + + int HeaderCount = 0; + for (header_info *hi = FirstHeader; hi != nil; hi = hi->getNext()) { + HeaderCount++; + } + +#if TARGET_OS_WIN32 + const TCHAR **names = (const TCHAR **) + malloc((HeaderCount+1) * sizeof(TCHAR *)); +#else + const char **names = (const char **) + malloc((HeaderCount+1) * sizeof(char *)); +#endif + + unsigned int count = 0; + for (header_info *hi = FirstHeader; hi != nil; hi = hi->getNext()) { +#if TARGET_OS_WIN32 + if (hi->moduleName) { + names[count++] = hi->moduleName; + } +#else + const char *fname = hi->fname(); + if (fname) { + names[count++] = fname; + } +#endif + } + names[count] = nil; + + if (count == 0) { + // Return nil instead of empty list if there are no images + free((void *)names); + names = nil; + } + + if (outCount) *outCount = count; + return names; +} + + +/*********************************************************************** +* copyClassNamesForImage_nolock +* Copies class names from the given image. +* Missing weak-import classes are omitted. +* Swift class names are demangled. +* +* Locking: runtimeLock must be held by the caller **********************************************************************/ const char ** -_objc_copyClassNamesForImage(header_info *hi, unsigned int *outCount) +copyClassNamesForImage_nolock(header_info *hi, unsigned int *outCount) { - size_t count, i, shift; - classref_t *classlist; - const char **names; - - // Need to write-lock in case demangledName() needs to realize a class. - rwlock_writer_t lock(runtimeLock); - - classlist = _getObjc2ClassList(hi, &count); - names = (const char **)malloc((count+1) * sizeof(const char *)); - - shift = 0; - for (i = 0; i < count; i++) { + runtimeLock.assertLocked(); + ASSERT(hi); + + size_t count; + classref_t const *classlist = _getObjc2ClassList(hi, &count); + const char **names = (const char **) + malloc((count+1) * sizeof(const char *)); + + size_t shift = 0; + for (size_t i = 0; i < count; i++) { Class cls = remapClass(classlist[i]); if (cls) { - names[i-shift] = cls->demangledName(true/*realize*/); + names[i-shift] = cls->demangledName(/* needs lock */false); } else { shift++; // ignored weak-linked class } @@ -4211,6 +5462,78 @@ _objc_copyClassNamesForImage(header_info *hi, unsigned int *outCount) } + +/*********************************************************************** +* objc_copyClassNamesForImage +* Copies class names from the named image. +* The image name must be identical to dladdr's dli_fname value. +* Missing weak-import classes are omitted. +* Swift class names are demangled. +* +* Locking: acquires runtimeLock +**********************************************************************/ +const char ** +objc_copyClassNamesForImage(const char *image, unsigned int *outCount) +{ + if (!image) { + if (outCount) *outCount = 0; + return nil; + } + + mutex_locker_t lock(runtimeLock); + + // Find the image. + header_info *hi; + for (hi = FirstHeader; hi != nil; hi = hi->getNext()) { +#if TARGET_OS_WIN32 + if (0 == wcscmp((TCHAR *)image, hi->moduleName)) break; +#else + if (0 == strcmp(image, hi->fname())) break; +#endif + } + + if (!hi) { + if (outCount) *outCount = 0; + return nil; + } + + return copyClassNamesForImage_nolock(hi, outCount); +} + + +/*********************************************************************** +* objc_copyClassNamesForImageHeader +* Copies class names from the given image. +* Missing weak-import classes are omitted. +* Swift class names are demangled. +* +* Locking: acquires runtimeLock +**********************************************************************/ +const char ** +objc_copyClassNamesForImageHeader(const struct mach_header *mh, unsigned int *outCount) +{ + if (!mh) { + if (outCount) *outCount = 0; + return nil; + } + + mutex_locker_t lock(runtimeLock); + + // Find the image. + header_info *hi; + for (hi = FirstHeader; hi != nil; hi = hi->getNext()) { + if (hi->mhdr() == (const headerType *)mh) break; + } + + if (!hi) { + if (outCount) *outCount = 0; + return nil; + } + + return copyClassNamesForImage_nolock(hi, outCount); +} + + /*********************************************************************** * saveTemporaryString * Save a string in a thread-local FIFO buffer. @@ -4244,7 +5567,13 @@ objc_class::nameForLogging() { // Handle the easy case directly. if (isRealized() || isFuture()) { - if (data()->demangledName) return data()->demangledName; + if (!isAnySwift()) { + return data()->ro()->name; + } + auto rwe = data()->ext(); + if (rwe && rwe->demangledName) { + return rwe->demangledName; + } } char *result; @@ -4262,31 +5591,51 @@ objc_class::nameForLogging() /*********************************************************************** * objc_class::demangledName * If realize=false, the class must already be realized or future. -* Locking: If realize=true, runtimeLock must be held for writing by the caller. +* Locking: runtimeLock may or may not be held by the caller. **********************************************************************/ -static mutex_t DemangleCacheLock; -static NXHashTable *DemangleCache; +mutex_t DemangleCacheLock; +static objc::DenseSet *DemangleCache; const char * -objc_class::demangledName(bool realize) +objc_class::demangledName(bool needsLock) { + if (!needsLock) { + runtimeLock.assertLocked(); + } + // Return previously demangled name if available. if (isRealized() || isFuture()) { - if (data()->demangledName) return data()->demangledName; + // Swift metaclasses don't have the is-Swift bit. + // We can't take this shortcut for them. + if (!isMetaClass() && !isAnySwift()) { + return data()->ro()->name; + } + auto rwe = data()->ext(); + if (rwe && rwe->demangledName) { + return rwe->demangledName; + } } // Try demangling the mangled name. const char *mangled = mangledName(); char *de = copySwiftV1DemangledName(mangled); + class_rw_ext_t *rwe; + if (isRealized() || isFuture()) { - // Class is already realized or future. + if (needsLock) { + mutex_locker_t lock(runtimeLock); + rwe = data()->extAllocIfNeeded(); + } else { + rwe = data()->extAllocIfNeeded(); + } + // Class is already realized or future. // Save demangling result in rw data. - // We may not own rwlock for writing so use an atomic operation instead. + // We may not own runtimeLock so use an atomic operation instead. if (! OSAtomicCompareAndSwapPtrBarrier(nil, (void*)(de ?: mangled), - (void**)&data()->demangledName)) + (void**)&rwe->demangledName)) { if (de) free(de); } - return data()->demangledName; + return rwe->demangledName; } // Class is not yet realized. @@ -4295,47 +5644,54 @@ objc_class::demangledName(bool realize) return mangled; } - // Class is not yet realized and name is mangled. Realize the class. + // Class is not yet realized and name is mangled. + // Allocate the name but don't save it in the class. + // Save the name in a side cache instead to prevent leaks. + // When the class is actually realized we may allocate a second + // copy of the name, but we don't care. + // (Previously we would try to realize the class now and save the + // name there, but realization is more complicated for Swift classes.) + // Only objc_copyClassNamesForImage() should get here. - // fixme lldb's calls to class_getName() can also get here when // interrogating the dyld shared cache. (rdar://27258517) - // fixme runtimeLock.assertWriting(); - // fixme assert(realize); - - if (realize) { - runtimeLock.assertWriting(); - realizeClass((Class)this); - data()->demangledName = de; - return de; - } - else { - // Save the string to avoid leaks. - char *cached; - { - mutex_locker_t lock(DemangleCacheLock); - if (!DemangleCache) { - DemangleCache = NXCreateHashTable(NXStrPrototype, 0, nil); - } - cached = (char *)NXHashInsertIfAbsent(DemangleCache, de); + // fixme ASSERT(realize); + + const char *cached; + { + mutex_locker_t lock(DemangleCacheLock); + if (!DemangleCache) { + DemangleCache = new objc::DenseSet{}; } - if (cached != de) free(de); - return cached; + cached = *DemangleCache->insert(de).first; } + if (cached != de) free(de); + return cached; } /*********************************************************************** * class_getName * fixme -* Locking: acquires runtimeLock +* Locking: may acquire DemangleCacheLock **********************************************************************/ const char *class_getName(Class cls) { if (!cls) return "nil"; // fixme lldb calls class_getName() on unrealized classes (rdar://27258517) - // assert(cls->isRealized() || cls->isFuture()); - return cls->demangledName(); + // ASSERT(cls->isRealized() || cls->isFuture()); + return cls->demangledName(/* needs lock */true); +} + +/*********************************************************************** +* objc_debug_class_getNameRaw +* fixme +* Locking: none +**********************************************************************/ +const char *objc_debug_class_getNameRaw(Class cls) +{ + if (!cls) return "nil"; + return cls->mangledName(); } @@ -4348,8 +5704,12 @@ int class_getVersion(Class cls) { if (!cls) return 0; - assert(cls->isRealized()); - return cls->data()->version; + ASSERT(cls->isRealized()); + auto rwe = cls->data()->ext(); + if (rwe) { + return rwe->version; + } + return cls->isMetaClass() ? 7 : 0; } @@ -4362,14 +5722,23 @@ void class_setVersion(Class cls, int version) { if (!cls) return; - assert(cls->isRealized()); - cls->data()->version = version; + ASSERT(cls->isRealized()); + auto rwe = cls->data()->ext(); + if (!rwe) { + mutex_locker_t lock(runtimeLock); + rwe = cls->data()->extAllocIfNeeded(); + } + + rwe->version = version; } - -static method_t *findMethodInSortedMethodList(SEL key, const method_list_t *list) +/*********************************************************************** + * search_method_list_inline + **********************************************************************/ +ALWAYS_INLINE static method_t * +findMethodInSortedMethodList(SEL key, const method_list_t *list) { - assert(list); + ASSERT(list); const method_t * const first = &list->first; const method_t *base = first; @@ -4401,17 +5770,13 @@ static method_t *findMethodInSortedMethodList(SEL key, const method_list_t *list return nil; } -/*********************************************************************** -* getMethodNoSuper_nolock -* fixme -* Locking: runtimeLock must be read- or write-locked by the caller -**********************************************************************/ -static method_t *search_method_list(const method_list_t *mlist, SEL sel) +ALWAYS_INLINE static method_t * +search_method_list_inline(const method_list_t *mlist, SEL sel) { int methodListIsFixedUp = mlist->isFixedUp(); int methodListHasExpectedSize = mlist->entsize() == sizeof(method_t); - if (__builtin_expect(methodListIsFixedUp && methodListHasExpectedSize, 1)) { + if (fastpath(methodListIsFixedUp && methodListHasExpectedSize)) { return findMethodInSortedMethodList(sel, mlist); } else { // Linear search of unsorted method list @@ -4434,21 +5799,68 @@ static method_t *search_method_list(const method_list_t *mlist, SEL sel) return nil; } +NEVER_INLINE static method_t * +search_method_list(const method_list_t *mlist, SEL sel) +{ + return search_method_list_inline(mlist, sel); +} + +/*********************************************************************** + * method_lists_contains_any + **********************************************************************/ +static NEVER_INLINE bool +method_lists_contains_any(method_list_t * const *mlists, method_list_t * const *end, + SEL sels[], size_t selcount) +{ + while (mlists < end) { + const method_list_t *mlist = *mlists++; + int methodListIsFixedUp = mlist->isFixedUp(); + int methodListHasExpectedSize = mlist->entsize() == sizeof(method_t); + + if (fastpath(methodListIsFixedUp && methodListHasExpectedSize)) { + for (size_t i = 0; i < selcount; i++) { + if (findMethodInSortedMethodList(sels[i], mlist)) { + return true; + } + } + } else { + for (auto& meth : *mlist) { + for (size_t i = 0; i < selcount; i++) { + if (meth.name == sels[i]) { + return true; + } + } + } + } + } + return false; +} + +/*********************************************************************** + * getMethodNoSuper_nolock + * fixme + * Locking: runtimeLock must be read- or write-locked by the caller + **********************************************************************/ static method_t * getMethodNoSuper_nolock(Class cls, SEL sel) { runtimeLock.assertLocked(); - assert(cls->isRealized()); + ASSERT(cls->isRealized()); // fixme nil cls? // fixme nil sel? - for (auto mlists = cls->data()->methods.beginLists(), - end = cls->data()->methods.endLists(); + auto const methods = cls->data()->methods(); + for (auto mlists = methods.beginLists(), + end = methods.endLists(); mlists != end; ++mlists) { - method_t *m = search_method_list(*mlists, sel); + // getMethodNoSuper_nolock is the hottest + // caller of search_method_list, inlining it turns + // getMethodNoSuper_nolock into a frame-less function and eliminates + // any store from this codepath. + method_t *m = search_method_list_inline(*mlists, sel); if (m) return m; } @@ -4471,7 +5883,7 @@ getMethod_nolock(Class cls, SEL sel) // fixme nil cls? // fixme nil sel? - assert(cls->isRealized()); + ASSERT(cls->isRealized()); while (cls && ((m = getMethodNoSuper_nolock(cls, sel))) == nil) { cls = cls->superclass; @@ -4488,7 +5900,7 @@ getMethod_nolock(Class cls, SEL sel) **********************************************************************/ static Method _class_getMethod(Class cls, SEL sel) { - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return getMethod_nolock(cls, sel); } @@ -4509,8 +5921,7 @@ Method class_getInstanceMethod(Class cls, SEL sel) #warning fixme build and search caches // Search method lists, try method resolver, etc. - lookUpImpOrNil(cls, sel, nil, - NO/*initialize*/, NO/*cache*/, YES/*resolver*/); + lookUpImpOrForward(nil, sel, cls, LOOKUP_RESOLVER); #warning fixme build and search caches @@ -4518,6 +5929,136 @@ Method class_getInstanceMethod(Class cls, SEL sel) } +/*********************************************************************** +* resolveClassMethod +* Call +resolveClassMethod, looking for a method to be added to class cls. +* cls should be a metaclass. +* Does not check if the method already exists. +**********************************************************************/ +static void resolveClassMethod(id inst, SEL sel, Class cls) +{ + runtimeLock.assertUnlocked(); + ASSERT(cls->isRealized()); + ASSERT(cls->isMetaClass()); + + if (!lookUpImpOrNil(inst, @selector(resolveClassMethod:), cls)) { + // Resolver not implemented. + return; + } + + Class nonmeta; + { + mutex_locker_t lock(runtimeLock); + nonmeta = getMaybeUnrealizedNonMetaClass(cls, inst); + // +initialize path should have realized nonmeta already + if (!nonmeta->isRealized()) { + _objc_fatal("nonmeta class %s (%p) unexpectedly not realized", + nonmeta->nameForLogging(), nonmeta); + } + } + BOOL (*msg)(Class, SEL, SEL) = (typeof(msg))objc_msgSend; + bool resolved = msg(nonmeta, @selector(resolveClassMethod:), sel); + + // Cache the result (good or bad) so the resolver doesn't fire next time. + // +resolveClassMethod adds to self->ISA() a.k.a. cls + IMP imp = lookUpImpOrNil(inst, sel, cls); + + if (resolved && PrintResolving) { + if (imp) { + _objc_inform("RESOLVE: method %c[%s %s] " + "dynamically resolved to %p", + cls->isMetaClass() ? '+' : '-', + cls->nameForLogging(), sel_getName(sel), imp); + } + else { + // Method resolver didn't add anything? + _objc_inform("RESOLVE: +[%s resolveClassMethod:%s] returned YES" + ", but no new implementation of %c[%s %s] was found", + cls->nameForLogging(), sel_getName(sel), + cls->isMetaClass() ? '+' : '-', + cls->nameForLogging(), sel_getName(sel)); + } + } +} + + +/*********************************************************************** +* resolveInstanceMethod +* Call +resolveInstanceMethod, looking for a method to be added to class cls. +* cls may be a metaclass or a non-meta class. +* Does not check if the method already exists. +**********************************************************************/ +static void resolveInstanceMethod(id inst, SEL sel, Class cls) +{ + runtimeLock.assertUnlocked(); + ASSERT(cls->isRealized()); + SEL resolve_sel = @selector(resolveInstanceMethod:); + + if (!lookUpImpOrNil(cls, resolve_sel, cls->ISA())) { + // Resolver not implemented. + return; + } + + BOOL (*msg)(Class, SEL, SEL) = (typeof(msg))objc_msgSend; + bool resolved = msg(cls, resolve_sel, sel); + + // Cache the result (good or bad) so the resolver doesn't fire next time. + // +resolveInstanceMethod adds to self a.k.a. cls + IMP imp = lookUpImpOrNil(inst, sel, cls); + + if (resolved && PrintResolving) { + if (imp) { + _objc_inform("RESOLVE: method %c[%s %s] " + "dynamically resolved to %p", + cls->isMetaClass() ? '+' : '-', + cls->nameForLogging(), sel_getName(sel), imp); + } + else { + // Method resolver didn't add anything? + _objc_inform("RESOLVE: +[%s resolveInstanceMethod:%s] returned YES" + ", but no new implementation of %c[%s %s] was found", + cls->nameForLogging(), sel_getName(sel), + cls->isMetaClass() ? '+' : '-', + cls->nameForLogging(), sel_getName(sel)); + } + } +} + + +/*********************************************************************** +* resolveMethod_locked +* Call +resolveClassMethod or +resolveInstanceMethod. +* +* Called with the runtimeLock held to avoid pressure in the caller +* Tail calls into lookUpImpOrForward, also to avoid pressure in the callerb +**********************************************************************/ +static NEVER_INLINE IMP +resolveMethod_locked(id inst, SEL sel, Class cls, int behavior) +{ + runtimeLock.assertLocked(); + ASSERT(cls->isRealized()); + + runtimeLock.unlock(); + + if (! cls->isMetaClass()) { + // try [cls resolveInstanceMethod:sel] + resolveInstanceMethod(inst, sel, cls); + } + else { + // try [nonMetaClass resolveClassMethod:sel] + // and [cls resolveInstanceMethod:sel] + resolveClassMethod(inst, sel, cls); + if (!lookUpImpOrNil(inst, sel, cls)) { + resolveInstanceMethod(inst, sel, cls); + } + } + + // chances are that calling the resolver have populated the cache + // so attempt using it + return lookUpImpOrForward(inst, sel, cls, behavior | LOOKUP_CACHE); +} + + /*********************************************************************** * log_and_fill_cache * Log this method call. If the logger permits it, fill the method cache. @@ -4528,7 +6069,7 @@ static void log_and_fill_cache(Class cls, IMP imp, SEL sel, id receiver, Class implementer) { #if SUPPORT_MESSAGE_LOGGING - if (objcMsgLogEnabled) { + if (slowpath(objcMsgLogEnabled && implementer)) { bool cacheIt = logMessageSend(implementer->isMetaClass(), cls->nameForLogging(), implementer->nameForLogging(), @@ -4536,154 +6077,136 @@ log_and_fill_cache(Class cls, IMP imp, SEL sel, id receiver, Class implementer) if (!cacheIt) return; } #endif - cache_fill (cls, sel, imp, receiver); -} - - -/*********************************************************************** -* _class_lookupMethodAndLoadCache. -* Method lookup for dispatchers ONLY. OTHER CODE SHOULD USE lookUpImp(). -* This lookup avoids optimistic cache scan because the dispatcher -* already tried that. -**********************************************************************/ -IMP _class_lookupMethodAndLoadCache3(id obj, SEL sel, Class cls) -{ - return lookUpImpOrForward(cls, sel, obj, - YES/*initialize*/, NO/*cache*/, YES/*resolver*/); + cache_fill(cls, sel, imp, receiver); } /*********************************************************************** * lookUpImpOrForward. * The standard IMP lookup. -* initialize==NO tries to avoid +initialize (but sometimes fails) -* cache==NO skips optimistic unlocked lookup (but uses cache elsewhere) -* Most callers should use initialize==YES and cache==YES. +* Without LOOKUP_INITIALIZE: tries to avoid +initialize (but sometimes fails) +* Without LOOKUP_CACHE: skips optimistic unlocked lookup (but uses cache elsewhere) +* Most callers should use LOOKUP_INITIALIZE and LOOKUP_CACHE * inst is an instance of cls or a subclass thereof, or nil if none is known. * If cls is an un-initialized metaclass then a non-nil inst is faster. * May return _objc_msgForward_impcache. IMPs destined for external use * must be converted to _objc_msgForward or _objc_msgForward_stret. -* If you don't want forwarding at all, use lookUpImpOrNil() instead. +* If you don't want forwarding at all, use LOOKUP_NIL. **********************************************************************/ -IMP lookUpImpOrForward(Class cls, SEL sel, id inst, - bool initialize, bool cache, bool resolver) +IMP lookUpImpOrForward(id inst, SEL sel, Class cls, int behavior) { - Class curClass; + const IMP forward_imp = (IMP)_objc_msgForward_impcache; IMP imp = nil; - Method meth; - bool triedResolver = NO; + Class curClass; runtimeLock.assertUnlocked(); // Optimistic cache lookup - if (cache) { + if (fastpath(behavior & LOOKUP_CACHE)) { imp = cache_getImp(cls, sel); - if (imp) return imp; + if (imp) goto done_nolock; } - if (!cls->isRealized()) { - rwlock_writer_t lock(runtimeLock); - realizeClass(cls); + // runtimeLock is held during isRealized and isInitialized checking + // to prevent races against concurrent realization. + + // runtimeLock is held during method search to make + // method-lookup + cache-fill atomic with respect to method addition. + // Otherwise, a category could be added but ignored indefinitely because + // the cache was re-filled with the old value after the cache flush on + // behalf of the category. + + runtimeLock.lock(); + + // We don't want people to be able to craft a binary blob that looks like + // a class but really isn't one and do a CFI attack. + // + // To make these harder we want to make sure this is a class that was + // either built into the binary or legitimately registered through + // objc_duplicateClass, objc_initializeClassPair or objc_allocateClassPair. + // + // TODO: this check is quite costly during process startup. + checkIsKnownClass(cls); + + if (slowpath(!cls->isRealized())) { + cls = realizeClassMaybeSwiftAndLeaveLocked(cls, runtimeLock); + // runtimeLock may have been dropped but is now locked again } - if (initialize && !cls->isInitialized()) { - _class_initialize (_class_getNonMetaClass(cls, inst)); - // If sel == initialize, _class_initialize will send +initialize and + if (slowpath((behavior & LOOKUP_INITIALIZE) && !cls->isInitialized())) { + cls = initializeAndLeaveLocked(cls, inst, runtimeLock); + // runtimeLock may have been dropped but is now locked again + + // If sel == initialize, class_initialize will send +initialize and // then the messenger will send +initialize again after this // procedure finishes. Of course, if this is not being called // from the messenger then it won't happen. 2778172 } - // The lock is held to make method-lookup + cache-fill atomic - // with respect to method addition. Otherwise, a category could - // be added but ignored indefinitely because the cache was re-filled - // with the old value after the cache flush on behalf of the category. - retry: - runtimeLock.read(); - - // Try this class's cache. - - imp = cache_getImp(cls, sel); - if (imp) goto done; - - // Try this class's method lists. - - meth = getMethodNoSuper_nolock(cls, sel); - if (meth) { - log_and_fill_cache(cls, meth->imp, sel, inst, cls); - imp = meth->imp; - goto done; - } - - // Try superclass caches and method lists. - + runtimeLock.assertLocked(); curClass = cls; - while ((curClass = curClass->superclass)) { - // Superclass cache. - imp = cache_getImp(curClass, sel); - if (imp) { - if (imp != (IMP)_objc_msgForward_impcache) { - // Found the method in a superclass. Cache it in this class. - log_and_fill_cache(cls, imp, sel, inst, curClass); - goto done; - } - else { - // Found a forward:: entry in a superclass. - // Stop searching, but don't cache yet; call method - // resolver for this class first. - break; - } + + // The code used to lookpu the class's cache again right after + // we take the lock but for the vast majority of the cases + // evidence shows this is a miss most of the time, hence a time loss. + // + // The only codepath calling into this without having performed some + // kind of cache lookup is class_getInstanceMethod(). + + for (unsigned attempts = unreasonableClassCount();;) { + // curClass method list. + Method meth = getMethodNoSuper_nolock(curClass, sel); + if (meth) { + imp = meth->imp; + goto done; } - // Superclass method list. - meth = getMethodNoSuper_nolock(curClass, sel); - if (meth) { - log_and_fill_cache(cls, meth->imp, sel, inst, curClass); - imp = meth->imp; + if (slowpath((curClass = curClass->superclass) == nil)) { + // No implementation found, and method resolver didn't help. + // Use forwarding. + imp = forward_imp; + break; + } + + // Halt if there is a cycle in the superclass chain. + if (slowpath(--attempts == 0)) { + _objc_fatal("Memory corruption in class list."); + } + + // Superclass cache. + imp = cache_getImp(curClass, sel); + if (slowpath(imp == forward_imp)) { + // Found a forward:: entry in a superclass. + // Stop searching, but don't cache yet; call method + // resolver for this class first. + break; + } + if (fastpath(imp)) { + // Found the method in a superclass. Cache it in this class. goto done; } } // No implementation found. Try method resolver once. - if (resolver && !triedResolver) { - runtimeLock.unlockRead(); - _class_resolveMethod(cls, sel, inst); - // Don't cache the result; we don't hold the lock so it may have - // changed already. Re-do the search from scratch instead. - triedResolver = YES; - goto retry; + if (slowpath(behavior & LOOKUP_RESOLVER)) { + behavior ^= LOOKUP_RESOLVER; + return resolveMethod_locked(inst, sel, cls, behavior); } - // No implementation found, and method resolver didn't help. - // Use forwarding. - - imp = (IMP)_objc_msgForward_impcache; - cache_fill(cls, sel, imp, inst); - done: - runtimeLock.unlockRead(); - + log_and_fill_cache(cls, imp, sel, inst, curClass); + runtimeLock.unlock(); + done_nolock: + if (slowpath((behavior & LOOKUP_NIL) && imp == forward_imp)) { + return nil; + } return imp; } - -/*********************************************************************** -* lookUpImpOrNil. -* Like lookUpImpOrForward, but returns nil instead of _objc_msgForward_impcache -**********************************************************************/ -IMP lookUpImpOrNil(Class cls, SEL sel, id inst, - bool initialize, bool cache, bool resolver) -{ - IMP imp = lookUpImpOrForward(cls, sel, inst, initialize, cache, resolver); - if (imp == _objc_msgForward_impcache) return nil; - else return imp; -} - - /*********************************************************************** * lookupMethodInClassAndLoadCache. -* Like _class_lookupMethodAndLoadCache, but does not search superclasses. +* Like lookUpImpOrForward, but does not search superclasses. * Caches and returns objc_msgForward if the method is not found in the class. **********************************************************************/ IMP lookupMethodInClassAndLoadCache(Class cls, SEL sel) @@ -4693,7 +6216,7 @@ IMP lookupMethodInClassAndLoadCache(Class cls, SEL sel) // fixme this is incomplete - no resolver, +initialize - // but it's only used for .cxx_construct/destruct so we don't care - assert(sel == SEL_cxx_construct || sel == SEL_cxx_destruct); + ASSERT(sel == SEL_cxx_construct || sel == SEL_cxx_destruct); // Search cache first. imp = cache_getImp(cls, sel); @@ -4701,7 +6224,7 @@ IMP lookupMethodInClassAndLoadCache(Class cls, SEL sel) // Cache miss. Search method list. - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); meth = getMethodNoSuper_nolock(cls, sel); @@ -4726,12 +6249,14 @@ objc_property_t class_getProperty(Class cls, const char *name) { if (!cls || !name) return nil; - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); - assert(cls->isRealized()); + checkIsKnownClass(cls); + + ASSERT(cls->isRealized()); for ( ; cls; cls = cls->superclass) { - for (auto& prop : cls->data()->properties) { + for (auto& prop : cls->data()->properties()) { if (0 == strcmp(name, prop.name)) { return (objc_property_t)∝ } @@ -4770,122 +6295,23 @@ objc_class::setInitialized() Class metacls; Class cls; - assert(!isMetaClass()); + ASSERT(!isMetaClass()); cls = (Class)this; metacls = cls->ISA(); - rwlock_reader_t lock(runtimeLock); - - // Scan metaclass for custom AWZ. - // Scan metaclass for custom RR. - // Scan class for custom RR. - // Also print custom RR/AWZ because we probably haven't done it yet. + mutex_locker_t lock(runtimeLock); // Special cases: - // NSObject AWZ class methods are default. - // NSObject RR instance methods are default. - // updateCustomRR_AWZ() also knows these special cases. + // - NSObject AWZ class methods are default. + // - NSObject RR class and instance methods are default. + // - NSObject Core class and instance methods are default. + // adjustCustomFlagsForMethodChange() also knows these special cases. // attachMethodLists() also knows these special cases. - bool inherited; - bool metaCustomAWZ = NO; - if (MetaclassNSObjectAWZSwizzled) { - // Somebody already swizzled NSObject's methods - metaCustomAWZ = YES; - inherited = NO; - } - else if (metacls == classNSObject()->ISA()) { - // NSObject's metaclass AWZ is default, but we still need to check cats - auto& methods = metacls->data()->methods; - for (auto mlists = methods.beginCategoryMethodLists(), - end = methods.endCategoryMethodLists(metacls); - mlists != end; - ++mlists) - { - if (methodListImplementsAWZ(*mlists)) { - metaCustomAWZ = YES; - inherited = NO; - break; - } - } - } - else if (metacls->superclass->hasCustomAWZ()) { - // Superclass is custom AWZ, therefore we are too. - metaCustomAWZ = YES; - inherited = YES; - } - else { - // Not metaclass NSObject. - auto& methods = metacls->data()->methods; - for (auto mlists = methods.beginLists(), - end = methods.endLists(); - mlists != end; - ++mlists) - { - if (methodListImplementsAWZ(*mlists)) { - metaCustomAWZ = YES; - inherited = NO; - break; - } - } - } - if (!metaCustomAWZ) metacls->setHasDefaultAWZ(); - - if (PrintCustomAWZ && metaCustomAWZ) metacls->printCustomAWZ(inherited); - // metacls->printCustomRR(); - - - bool clsCustomRR = NO; - if (ClassNSObjectRRSwizzled) { - // Somebody already swizzled NSObject's methods - clsCustomRR = YES; - inherited = NO; - } - if (cls == classNSObject()) { - // NSObject's RR is default, but we still need to check categories - auto& methods = cls->data()->methods; - for (auto mlists = methods.beginCategoryMethodLists(), - end = methods.endCategoryMethodLists(cls); - mlists != end; - ++mlists) - { - if (methodListImplementsRR(*mlists)) { - clsCustomRR = YES; - inherited = NO; - break; - } - } - } - else if (!cls->superclass) { - // Custom root class - clsCustomRR = YES; - inherited = NO; - } - else if (cls->superclass->hasCustomRR()) { - // Superclass is custom RR, therefore we are too. - clsCustomRR = YES; - inherited = YES; - } - else { - // Not class NSObject. - auto& methods = cls->data()->methods; - for (auto mlists = methods.beginLists(), - end = methods.endLists(); - mlists != end; - ++mlists) - { - if (methodListImplementsRR(*mlists)) { - clsCustomRR = YES; - inherited = NO; - break; - } - } - } - if (!clsCustomRR) cls->setHasDefaultRR(); - - // cls->printCustomAWZ(); - if (PrintCustomRR && clsCustomRR) cls->printCustomRR(inherited); + objc::AWZScanner::scanInitializedClass(cls, metacls); + objc::RRScanner::scanInitializedClass(cls, metacls); + objc::CoreScanner::scanInitializedClass(cls, metacls); // Update the +initialize flags. // Do this last. @@ -4893,164 +6319,35 @@ objc_class::setInitialized() } -/*********************************************************************** -* Return YES if sel is used by retain/release implementors -**********************************************************************/ -static bool -isRRSelector(SEL sel) -{ - return (sel == SEL_retain || sel == SEL_release || - sel == SEL_autorelease || sel == SEL_retainCount || - sel == SEL_tryRetain || sel == SEL_retainWeakReference || - sel == SEL_isDeallocating || sel == SEL_allowsWeakReference); -} - - -/*********************************************************************** -* Return YES if mlist implements one of the isRRSelector() methods -**********************************************************************/ -static bool -methodListImplementsRR(const method_list_t *mlist) -{ - return (search_method_list(mlist, SEL_retain) || - search_method_list(mlist, SEL_release) || - search_method_list(mlist, SEL_autorelease) || - search_method_list(mlist, SEL_retainCount) || - search_method_list(mlist, SEL_tryRetain) || - search_method_list(mlist, SEL_isDeallocating) || - search_method_list(mlist, SEL_retainWeakReference) || - search_method_list(mlist, SEL_allowsWeakReference)); -} - - -/*********************************************************************** -* Return YES if sel is used by alloc or allocWithZone implementors -**********************************************************************/ -static bool -isAWZSelector(SEL sel) -{ - return (sel == SEL_allocWithZone || sel == SEL_alloc); -} - - -/*********************************************************************** -* Return YES if mlist implements one of the isAWZSelector() methods -**********************************************************************/ -static bool -methodListImplementsAWZ(const method_list_t *mlist) -{ - return (search_method_list(mlist, SEL_allocWithZone) || - search_method_list(mlist, SEL_alloc)); -} - - -void -objc_class::printCustomRR(bool inherited) -{ - assert(PrintCustomRR); - assert(hasCustomRR()); - _objc_inform("CUSTOM RR: %s%s%s", nameForLogging(), - isMetaClass() ? " (meta)" : "", - inherited ? " (inherited)" : ""); -} - -void -objc_class::printCustomAWZ(bool inherited) -{ - assert(PrintCustomAWZ); - assert(hasCustomAWZ()); - _objc_inform("CUSTOM AWZ: %s%s%s", nameForLogging(), - isMetaClass() ? " (meta)" : "", - inherited ? " (inherited)" : ""); -} - -void +void objc_class::printInstancesRequireRawIsa(bool inherited) { - assert(PrintRawIsa); - assert(instancesRequireRawIsa()); + ASSERT(PrintRawIsa); + ASSERT(instancesRequireRawIsa()); _objc_inform("RAW ISA: %s%s%s", nameForLogging(), isMetaClass() ? " (meta)" : "", inherited ? " (inherited)" : ""); } - -/*********************************************************************** -* Mark this class and all of its subclasses as implementors or -* inheritors of custom RR (retain/release/autorelease/retainCount) -**********************************************************************/ -void objc_class::setHasCustomRR(bool inherited) -{ - Class cls = (Class)this; - runtimeLock.assertWriting(); - - if (hasCustomRR()) return; - - foreach_realized_class_and_subclass(cls, ^(Class c){ - if (c != cls && !c->isInitialized()) { - // Subclass not yet initialized. Wait for setInitialized() to do it - // fixme short circuit recursion? - return; - } - if (c->hasCustomRR()) { - // fixme short circuit recursion? - return; - } - - c->bits.setHasCustomRR(); - - if (PrintCustomRR) c->printCustomRR(inherited || c != cls); - }); -} - -/*********************************************************************** -* Mark this class and all of its subclasses as implementors or -* inheritors of custom alloc/allocWithZone: -**********************************************************************/ -void objc_class::setHasCustomAWZ(bool inherited) -{ - Class cls = (Class)this; - runtimeLock.assertWriting(); - - if (hasCustomAWZ()) return; - - foreach_realized_class_and_subclass(cls, ^(Class c){ - if (c != cls && !c->isInitialized()) { - // Subclass not yet initialized. Wait for setInitialized() to do it - // fixme short circuit recursion? - return; - } - if (c->hasCustomAWZ()) { - // fixme short circuit recursion? - return; - } - - c->bits.setHasCustomAWZ(); - - if (PrintCustomAWZ) c->printCustomAWZ(inherited || c != cls); - }); -} - - /*********************************************************************** * Mark this class and all of its subclasses as requiring raw isa pointers **********************************************************************/ -void objc_class::setInstancesRequireRawIsa(bool inherited) +void objc_class::setInstancesRequireRawIsaRecursively(bool inherited) { Class cls = (Class)this; - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); if (instancesRequireRawIsa()) return; - foreach_realized_class_and_subclass(cls, ^(Class c){ + foreach_realized_class_and_subclass(cls, [=](Class c){ if (c->instancesRequireRawIsa()) { - // fixme short circuit recursion? - return; + return false; } - c->bits.setInstancesRequireRawIsa(); + c->setInstancesRequireRawIsa(); if (PrintRawIsa) c->printInstancesRequireRawIsa(inherited || c != cls); + return true; }); } @@ -5063,12 +6360,12 @@ void objc_class::chooseClassArrayIndex() { #if SUPPORT_INDEXED_ISA Class cls = (Class)this; - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); if (objc_indexed_classes_count >= ISA_INDEX_COUNT) { // No more indexes available. - assert(cls->classArrayIndex() == 0); - cls->setInstancesRequireRawIsa(false/*not inherited*/); + ASSERT(cls->classArrayIndex() == 0); + cls->setInstancesRequireRawIsaRecursively(false/*not inherited*/); return; } @@ -5084,88 +6381,11 @@ void objc_class::chooseClassArrayIndex() * Update custom RR and AWZ when a method changes its IMP **********************************************************************/ static void -updateCustomRR_AWZ(Class cls, method_t *meth) +adjustCustomFlagsForMethodChange(Class cls, method_t *meth) { - // In almost all cases, IMP swizzling does not affect custom RR/AWZ bits. - // Custom RR/AWZ search will already find the method whether or not - // it is swizzled, so it does not transition from non-custom to custom. - // - // The only cases where IMP swizzling can affect the RR/AWZ bits is - // if the swizzled method is one of the methods that is assumed to be - // non-custom. These special cases are listed in setInitialized(). - // We look for such cases here. - - if (isRRSelector(meth->name)) { - - if ((classNSObject()->isInitialized() && - classNSObject()->hasCustomRR()) - || - ClassNSObjectRRSwizzled) - { - // already custom, nothing would change - return; - } - - bool swizzlingNSObject = NO; - if (cls == classNSObject()) { - swizzlingNSObject = YES; - } else { - // Don't know the class. - // The only special case is class NSObject. - for (const auto& meth2 : classNSObject()->data()->methods) { - if (meth == &meth2) { - swizzlingNSObject = YES; - break; - } - } - } - if (swizzlingNSObject) { - if (classNSObject()->isInitialized()) { - classNSObject()->setHasCustomRR(); - } else { - // NSObject not yet +initialized, so custom RR has not yet - // been checked, and setInitialized() will not notice the - // swizzle. - ClassNSObjectRRSwizzled = YES; - } - } - } - else if (isAWZSelector(meth->name)) { - Class metaclassNSObject = classNSObject()->ISA(); - - if ((metaclassNSObject->isInitialized() && - metaclassNSObject->hasCustomAWZ()) - || - MetaclassNSObjectAWZSwizzled) - { - // already custom, nothing would change - return; - } - - bool swizzlingNSObject = NO; - if (cls == metaclassNSObject) { - swizzlingNSObject = YES; - } else { - // Don't know the class. - // The only special case is metaclass NSObject. - for (const auto& meth2 : metaclassNSObject->data()->methods) { - if (meth == &meth2) { - swizzlingNSObject = YES; - break; - } - } - } - if (swizzlingNSObject) { - if (metaclassNSObject->isInitialized()) { - metaclassNSObject->setHasCustomAWZ(); - } else { - // NSObject not yet +initialized, so custom RR has not yet - // been checked, and setInitialized() will not notice the - // swizzle. - MetaclassNSObjectAWZSwizzled = YES; - } - } - } + objc::AWZScanner::scanChangedMethod(cls, meth); + objc::RRScanner::scanChangedMethod(cls, meth); + objc::CoreScanner::scanChangedMethod(cls, meth); } @@ -5178,7 +6398,7 @@ updateCustomRR_AWZ(Class cls, method_t *meth) const uint8_t * class_getIvarLayout(Class cls) { - if (cls) return cls->data()->ro->ivarLayout; + if (cls) return cls->data()->ro()->ivarLayout; else return nil; } @@ -5192,7 +6412,7 @@ class_getIvarLayout(Class cls) const uint8_t * class_getWeakIvarLayout(Class cls) { - if (cls) return cls->data()->ro->weakIvarLayout; + if (cls) return cls->data()->ro()->weakIvarLayout; else return nil; } @@ -5211,8 +6431,10 @@ class_setIvarLayout(Class cls, const uint8_t *layout) { if (!cls) return; - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); + checkIsKnownClass(cls); + // Can only change layout of in-construction classes. // note: if modifications to post-construction classes were // allowed, there would be a race below (us vs. concurrent object_setIvar) @@ -5228,35 +6450,6 @@ class_setIvarLayout(Class cls, const uint8_t *layout) ro_w->ivarLayout = ustrdupMaybeNil(layout); } -// SPI: Instance-specific object layout. - -void -_class_setIvarLayoutAccessor(Class cls, const uint8_t* (*accessor) (id object)) { - if (!cls) return; - - rwlock_writer_t lock(runtimeLock); - - class_ro_t *ro_w = make_ro_writeable(cls->data()); - - // FIXME: this really isn't safe to free if there are instances of this class already. - if (!(cls->data()->flags & RW_HAS_INSTANCE_SPECIFIC_LAYOUT)) try_free(ro_w->ivarLayout); - ro_w->ivarLayout = (uint8_t *)accessor; - cls->setInfo(RW_HAS_INSTANCE_SPECIFIC_LAYOUT); -} - -const uint8_t * -_object_getIvarLayout(Class cls, id object) -{ - if (cls) { - const uint8_t* layout = cls->data()->ro->ivarLayout; - if (cls->data()->flags & RW_HAS_INSTANCE_SPECIFIC_LAYOUT) { - const uint8_t* (*accessor) (id object) = (const uint8_t* (*)(id))layout; - layout = accessor(object); - } - return layout; - } - return nil; -} /*********************************************************************** * class_setWeakIvarLayout @@ -5272,8 +6465,10 @@ class_setWeakIvarLayout(Class cls, const uint8_t *layout) { if (!cls) return; - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); + checkIsKnownClass(cls); + // Can only change layout of in-construction classes. // note: if modifications to post-construction classes were // allowed, there would be a race below (us vs. concurrent object_setIvar) @@ -5300,8 +6495,8 @@ static ivar_t *getIvar(Class cls, const char *name) runtimeLock.assertLocked(); const ivar_list_t *ivars; - assert(cls->isRealized()); - if ((ivars = cls->data()->ro->ivars)) { + ASSERT(cls->isRealized()); + if ((ivars = cls->data()->ro()->ivars)) { for (auto& ivar : *ivars) { if (!ivar.offset) continue; // anonymous bitfield @@ -5323,10 +6518,10 @@ static ivar_t *getIvar(Class cls, const char *name) **********************************************************************/ Class _class_getClassForIvar(Class cls, Ivar ivar) { - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); for ( ; cls; cls = cls->superclass) { - if (auto ivars = cls->data()->ro->ivars) { + if (auto ivars = cls->data()->ro()->ivars) { if (ivars->containsIvar(ivar)) { return cls; } @@ -5345,7 +6540,7 @@ Class _class_getClassForIvar(Class cls, Ivar ivar) Ivar _class_getVariable(Class cls, const char *name) { - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); for ( ; cls; cls = cls->superclass) { ivar_t *ivar = getIvar(cls, name); @@ -5370,11 +6565,13 @@ BOOL class_conformsToProtocol(Class cls, Protocol *proto_gen) if (!cls) return NO; if (!proto_gen) return NO; - rwlock_reader_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); - assert(cls->isRealized()); - - for (const auto& proto_ref : cls->data()->protocols) { + checkIsKnownClass(cls); + + ASSERT(cls->isRealized()); + + for (const auto& proto_ref : cls->data()->protocols()) { protocol_t *p = remapProtocol(proto_ref); if (p == proto || protocol_conformsToProtocol_nolock(p, proto)) { return YES; @@ -5395,10 +6592,12 @@ addMethod(Class cls, SEL name, IMP imp, const char *types, bool replace) { IMP result = nil; - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); - assert(types); - assert(cls->isRealized()); + checkIsKnownClass(cls); + + ASSERT(types); + ASSERT(cls->isRealized()); method_t *m; if ((m = getMethodNoSuper_nolock(cls, name))) { @@ -5409,6 +6608,8 @@ addMethod(Class cls, SEL name, IMP imp, const char *types, bool replace) result = _method_setImplementation(cls, m, imp); } } else { + auto rwe = cls->data()->extAllocIfNeeded(); + // fixme optimize method_list_t *newlist; newlist = (method_list_t *)calloc(sizeof(*newlist), 1); @@ -5420,7 +6621,7 @@ addMethod(Class cls, SEL name, IMP imp, const char *types, bool replace) newlist->first.imp = imp; prepareMethodLists(cls, &newlist, 1, NO, NO); - cls->data()->methods.attachLists(&newlist, 1); + rwe->methods.attachLists(&newlist, 1); flushCaches(cls); result = nil; @@ -5429,13 +6630,93 @@ addMethod(Class cls, SEL name, IMP imp, const char *types, bool replace) return result; } +/********************************************************************** +* addMethods +* Add the given methods to a class in bulk. +* Returns the selectors which could not be added, when replace == NO and a +* method already exists. The returned selectors are NULL terminated and must be +* freed by the caller. They are NULL if no failures occurred. +* Locking: runtimeLock must be held by the caller +**********************************************************************/ +static SEL * +addMethods(Class cls, const SEL *names, const IMP *imps, const char **types, + uint32_t count, bool replace, uint32_t *outFailedCount) +{ + runtimeLock.assertLocked(); + + ASSERT(names); + ASSERT(imps); + ASSERT(types); + ASSERT(cls->isRealized()); + + method_list_t *newlist; + size_t newlistSize = method_list_t::byteSize(sizeof(method_t), count); + newlist = (method_list_t *)calloc(newlistSize, 1); + newlist->entsizeAndFlags = + (uint32_t)sizeof(method_t) | fixed_up_method_list; + newlist->count = 0; + + method_t *newlistMethods = &newlist->first; + + SEL *failedNames = nil; + uint32_t failedCount = 0; + + for (uint32_t i = 0; i < count; i++) { + method_t *m; + if ((m = getMethodNoSuper_nolock(cls, names[i]))) { + // already exists + if (!replace) { + // report failure + if (failedNames == nil) { + // allocate an extra entry for a trailing NULL in case + // every method fails + failedNames = (SEL *)calloc(sizeof(*failedNames), + count + 1); + } + failedNames[failedCount] = m->name; + failedCount++; + } else { + _method_setImplementation(cls, m, imps[i]); + } + } else { + method_t *newmethod = &newlistMethods[newlist->count]; + newmethod->name = names[i]; + newmethod->types = strdupIfMutable(types[i]); + newmethod->imp = imps[i]; + newlist->count++; + } + } + + if (newlist->count > 0) { + auto rwe = cls->data()->extAllocIfNeeded(); + + // fixme resize newlist because it may have been over-allocated above. + // Note that realloc() alone doesn't work due to ptrauth. + + method_t::SortBySELAddress sorter; + std::stable_sort(newlist->begin(), newlist->end(), sorter); + + prepareMethodLists(cls, &newlist, 1, NO, NO); + rwe->methods.attachLists(&newlist, 1); + flushCaches(cls); + } else { + // Attaching the method list to the class consumes it. If we don't + // do that, we have to free the memory ourselves. + free(newlist); + } + + if (outFailedCount) *outFailedCount = failedCount; + + return failedNames; +} + BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types) { if (!cls) return NO; - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return ! addMethod(cls, name, imp, types ?: "", NO); } @@ -5445,11 +6726,36 @@ class_replaceMethod(Class cls, SEL name, IMP imp, const char *types) { if (!cls) return nil; - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return addMethod(cls, name, imp, types ?: "", YES); } +SEL * +class_addMethodsBulk(Class cls, const SEL *names, const IMP *imps, + const char **types, uint32_t count, + uint32_t *outFailedCount) +{ + if (!cls) { + if (outFailedCount) *outFailedCount = count; + return (SEL *)memdup(names, count * sizeof(*names)); + } + + mutex_locker_t lock(runtimeLock); + return addMethods(cls, names, imps, types, count, NO, outFailedCount); +} + +void +class_replaceMethodsBulk(Class cls, const SEL *names, const IMP *imps, + const char **types, uint32_t count) +{ + if (!cls) return; + + mutex_locker_t lock(runtimeLock); + addMethods(cls, names, imps, types, count, YES, nil); +} + + /*********************************************************************** * class_addIvar * Adds an ivar to a class. @@ -5464,9 +6770,10 @@ class_addIvar(Class cls, const char *name, size_t size, if (!type) type = ""; if (name && 0 == strcmp(name, "")) name = nil; - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); - assert(cls->isRealized()); + checkIsKnownClass(cls); + ASSERT(cls->isRealized()); // No class variables if (cls->isMetaClass()) { @@ -5490,7 +6797,7 @@ class_addIvar(Class cls, const char *name, size_t size, // fixme allocate less memory here ivar_list_t *oldlist, *newlist; - if ((oldlist = (ivar_list_t *)cls->data()->ro->ivars)) { + if ((oldlist = (ivar_list_t *)cls->data()->ro()->ivars)) { size_t oldsize = oldlist->byteSize(); newlist = (ivar_list_t *)calloc(oldsize + oldlist->entsize(), 1); memcpy(newlist, oldlist, oldsize); @@ -5539,9 +6846,10 @@ BOOL class_addProtocol(Class cls, Protocol *protocol_gen) if (!cls) return NO; if (class_conformsToProtocol(cls, protocol_gen)) return NO; - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); + auto rwe = cls->data()->extAllocIfNeeded(); - assert(cls->isRealized()); + ASSERT(cls->isRealized()); // fixme optimize protocol_list_t *protolist = (protocol_list_t *) @@ -5549,7 +6857,7 @@ BOOL class_addProtocol(Class cls, Protocol *protocol_gen) protolist->count = 1; protolist->list[0] = (protocol_ref_t)protocol; - cls->data()->protocols.attachLists(&protolist, 1); + rwe->protocols.attachLists(&protolist, 1); // fixme metaclass? @@ -5577,15 +6885,16 @@ _class_addProperty(Class cls, const char *name, } else if (prop) { // replace existing - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); try_free(prop->attributes); prop->attributes = copyPropertyAttributeString(attrs, count); return YES; } else { - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); + auto rwe = cls->data()->extAllocIfNeeded(); - assert(cls->isRealized()); + ASSERT(cls->isRealized()); property_list_t *proplist = (property_list_t *) malloc(sizeof(*proplist)); @@ -5594,7 +6903,7 @@ _class_addProperty(Class cls, const char *name, proplist->first.name = strdupIfMutable(name); proplist->first.attributes = copyPropertyAttributeString(attrs, count); - cls->data()->properties.attachLists(&proplist, 1); + rwe->properties.attachLists(&proplist, 1); return YES; } @@ -5620,6 +6929,20 @@ class_replaceProperty(Class cls, const char *name, * Look up a class by name, and realize it. * Locking: acquires runtimeLock **********************************************************************/ +static BOOL empty_getClass(const char *name, Class *outClass) +{ + *outClass = nil; + return NO; +} + +static ChainedHookFunction GetClassHook{empty_getClass}; + +void objc_setHook_getClass(objc_hook_getClass newValue, + objc_hook_getClass *outOldValue) +{ + GetClassHook.set(newValue, outOldValue); +} + Class look_up_class(const char *name, bool includeUnconnected __attribute__((unused)), @@ -5630,14 +6953,58 @@ look_up_class(const char *name, Class result; bool unrealized; { - rwlock_reader_t lock(runtimeLock); - result = getClass(name); + runtimeLock.lock(); + result = getClassExceptSomeSwift(name); unrealized = result && !result->isRealized(); + if (unrealized) { + result = realizeClassMaybeSwiftAndUnlock(result, runtimeLock); + // runtimeLock is now unlocked + } else { + runtimeLock.unlock(); + } } - if (unrealized) { - rwlock_writer_t lock(runtimeLock); - realizeClass(result); + + if (!result) { + // Ask Swift about its un-instantiated classes. + + // We use thread-local storage to prevent infinite recursion + // if the hook function provokes another lookup of the same name + // (for example, if the hook calls objc_allocateClassPair) + + auto *tls = _objc_fetch_pthread_data(true); + + // Stop if this thread is already looking up this name. + for (unsigned i = 0; i < tls->classNameLookupsUsed; i++) { + if (0 == strcmp(name, tls->classNameLookups[i])) { + return nil; + } + } + + // Save this lookup in tls. + if (tls->classNameLookupsUsed == tls->classNameLookupsAllocated) { + tls->classNameLookupsAllocated = + (tls->classNameLookupsAllocated * 2 ?: 1); + size_t size = tls->classNameLookupsAllocated * + sizeof(tls->classNameLookups[0]); + tls->classNameLookups = (const char **) + realloc(tls->classNameLookups, size); + } + tls->classNameLookups[tls->classNameLookupsUsed++] = name; + + // Call the hook. + Class swiftcls = nil; + if (GetClassHook.get()(name, &swiftcls)) { + ASSERT(swiftcls->isRealized()); + result = swiftcls; + } + + // Erase the name from tls. + unsigned slot = --tls->classNameLookupsUsed; + ASSERT(slot >= 0 && slot < tls->classNameLookupsAllocated); + ASSERT(name == tls->classNameLookups[slot]); + tls->classNameLookups[slot] = nil; } + return result; } @@ -5653,10 +7020,16 @@ objc_duplicateClass(Class original, const char *name, { Class duplicate; - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); - assert(original->isRealized()); - assert(!original->isMetaClass()); + checkIsKnownClass(original); + + auto orig_rw = original->data(); + auto orig_rwe = orig_rw->ext(); + auto orig_ro = orig_rw->ro(); + + ASSERT(original->isRealized()); + ASSERT(!original->isMetaClass()); duplicate = alloc_class_for_subclass(original, extraBytes); @@ -5665,24 +7038,31 @@ objc_duplicateClass(Class original, const char *name, duplicate->cache.initializeToEmpty(); - class_rw_t *rw = (class_rw_t *)calloc(sizeof(*original->data()), 1); - rw->flags = (original->data()->flags | RW_COPIED_RO | RW_REALIZING); - rw->version = original->data()->version; + class_rw_t *rw = objc::zalloc(); + rw->flags = (orig_rw->flags | RW_COPIED_RO | RW_REALIZING); rw->firstSubclass = nil; rw->nextSiblingClass = nil; duplicate->bits = original->bits; duplicate->setData(rw); - rw->ro = (class_ro_t *) - memdup(original->data()->ro, sizeof(*original->data()->ro)); - *(char **)&rw->ro->name = strdupIfMutable(name); + auto ro = orig_ro->duplicate(); + *(char **)&ro->name = strdupIfMutable(name); + rw->set_ro(ro); - rw->methods = original->data()->methods.duplicate(); + if (orig_rwe) { + auto rwe = rw->extAllocIfNeeded(); + rwe->version = orig_rwe->version; + rwe->methods = orig_rwe->methods.duplicate(); - // fixme dies when categories are added to the base - rw->properties = original->data()->properties; - rw->protocols = original->data()->protocols; + // fixme dies when categories are added to the base + rwe->properties = orig_rwe->properties; + rwe->protocols = orig_rwe->protocols; + } else if (ro->baseMethods()) { + // if we have base methods, we need to make a deep copy + // which requires a class_rw_ext_t to be allocated + rw->deepCopy(ro); + } duplicate->chooseClassArrayIndex(); @@ -5695,12 +7075,12 @@ objc_duplicateClass(Class original, const char *name, // Don't methodize class - construction above is correct - addNamedClass(duplicate, duplicate->data()->ro->name); - + addNamedClass(duplicate, ro->name); + addClassTableEntry(duplicate, /*addMeta=*/false); + if (PrintConnecting) { _objc_inform("CLASS: realizing class '%s' (duplicate of %s) %p %p", - name, original->nameForLogging(), - (void*)duplicate, duplicate->data()->ro); + name, original->nameForLogging(), (void*)duplicate, ro); } duplicate->clearInfo(RW_REALIZING); @@ -5718,36 +7098,38 @@ static const uint8_t UnsetLayout = 0; static void objc_initializeClassPair_internal(Class superclass, const char *name, Class cls, Class meta) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); class_ro_t *cls_ro_w, *meta_ro_w; + class_rw_t *cls_rw_w, *meta_rw_w; - cls->setData((class_rw_t *)calloc(sizeof(class_rw_t), 1)); - meta->setData((class_rw_t *)calloc(sizeof(class_rw_t), 1)); + cls_rw_w = objc::zalloc(); + meta_rw_w = objc::zalloc(); cls_ro_w = (class_ro_t *)calloc(sizeof(class_ro_t), 1); meta_ro_w = (class_ro_t *)calloc(sizeof(class_ro_t), 1); - cls->data()->ro = cls_ro_w; - meta->data()->ro = meta_ro_w; + + cls->setData(cls_rw_w); + cls_rw_w->set_ro(cls_ro_w); + meta->setData(meta_rw_w); + meta_rw_w->set_ro(meta_ro_w); // Set basic info - cls->data()->flags = RW_CONSTRUCTING | RW_COPIED_RO | RW_REALIZED | RW_REALIZING; - meta->data()->flags = RW_CONSTRUCTING | RW_COPIED_RO | RW_REALIZED | RW_REALIZING; - cls->data()->version = 0; - meta->data()->version = 7; + cls_rw_w->flags = RW_CONSTRUCTING | RW_COPIED_RO | RW_REALIZED | RW_REALIZING; + meta_rw_w->flags = RW_CONSTRUCTING | RW_COPIED_RO | RW_REALIZED | RW_REALIZING | RW_META; cls_ro_w->flags = 0; meta_ro_w->flags = RO_META; - if (!superclass) { - cls_ro_w->flags |= RO_ROOT; - meta_ro_w->flags |= RO_ROOT; - } if (superclass) { + uint32_t flagsToCopy = RW_FORBIDS_ASSOCIATED_OBJECTS; + cls_rw_w->flags |= superclass->data()->flags & flagsToCopy; cls_ro_w->instanceStart = superclass->unalignedInstanceSize(); meta_ro_w->instanceStart = superclass->ISA()->unalignedInstanceSize(); cls->setInstanceSize(cls_ro_w->instanceStart); meta->setInstanceSize(meta_ro_w->instanceStart); } else { + cls_ro_w->flags |= RO_ROOT; + meta_ro_w->flags |= RO_ROOT; cls_ro_w->instanceStart = 0; meta_ro_w->instanceStart = (uint32_t)sizeof(objc_class); cls->setInstanceSize((uint32_t)sizeof(id)); // just an isa @@ -5763,8 +7145,19 @@ static void objc_initializeClassPair_internal(Class superclass, const char *name meta->chooseClassArrayIndex(); cls->chooseClassArrayIndex(); + // This absolutely needs to be done before addSubclass + // as initializeToEmpty() clobbers the FAST_CACHE bits + cls->cache.initializeToEmpty(); + meta->cache.initializeToEmpty(); + +#if FAST_CACHE_META + meta->cache.setBit(FAST_CACHE_META); +#endif + meta->setInstancesRequireRawIsa(); + // Connect to superclasses and metaclasses cls->initClassIsa(meta); + if (superclass) { meta->initClassIsa(superclass->ISA()->ISA()); cls->superclass = superclass; @@ -5779,8 +7172,7 @@ static void objc_initializeClassPair_internal(Class superclass, const char *name addSubclass(cls, meta); } - cls->cache.initializeToEmpty(); - meta->cache.initializeToEmpty(); + addClassTableEntry(cls); } @@ -5814,11 +7206,16 @@ verifySuperclass(Class superclass, bool rootOK) **********************************************************************/ Class objc_initializeClassPair(Class superclass, const char *name, Class cls, Class meta) { - rwlock_writer_t lock(runtimeLock); + // Fail if the class name is in use. + if (look_up_class(name, NO, NO)) return nil; + + mutex_locker_t lock(runtimeLock); // Fail if the class name is in use. // Fail if the superclass isn't kosher. - if (getClass(name) || !verifySuperclass(superclass, true/*rootOK*/)) { + if (getClassExceptSomeSwift(name) || + !verifySuperclass(superclass, true/*rootOK*/)) + { return nil; } @@ -5838,11 +7235,16 @@ Class objc_allocateClassPair(Class superclass, const char *name, { Class cls, meta; - rwlock_writer_t lock(runtimeLock); + // Fail if the class name is in use. + if (look_up_class(name, NO, NO)) return nil; + + mutex_locker_t lock(runtimeLock); // Fail if the class name is in use. // Fail if the superclass isn't kosher. - if (getClass(name) || !verifySuperclass(superclass, true/*rootOK*/)) { + if (getClassExceptSomeSwift(name) || + !verifySuperclass(superclass, true/*rootOK*/)) + { return nil; } @@ -5864,13 +7266,15 @@ Class objc_allocateClassPair(Class superclass, const char *name, **********************************************************************/ void objc_registerClassPair(Class cls) { - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); - if ((cls->data()->flags & RW_CONSTRUCTED) || + checkIsKnownClass(cls); + + if ((cls->data()->flags & RW_CONSTRUCTED) || (cls->ISA()->data()->flags & RW_CONSTRUCTED)) { _objc_inform("objc_registerClassPair: class '%s' was already " - "registered!", cls->data()->ro->name); + "registered!", cls->data()->ro()->name); return; } @@ -5879,7 +7283,7 @@ void objc_registerClassPair(Class cls) { _objc_inform("objc_registerClassPair: class '%s' was not " "allocated with objc_allocateClassPair!", - cls->data()->ro->name); + cls->data()->ro()->name); return; } @@ -5888,7 +7292,7 @@ void objc_registerClassPair(Class cls) cls->changeInfo(RW_CONSTRUCTED, RW_CONSTRUCTING | RW_REALIZING); // Add to named class table. - addNamedClass(cls, cls->data()->ro->name); + addNamedClass(cls, cls->data()->ro()->name); } @@ -5904,26 +7308,31 @@ void objc_registerClassPair(Class cls) **********************************************************************/ Class objc_readClassPair(Class bits, const struct objc_image_info *info) { - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); // No info bits are significant yet. (void)info; - // Fail if the class name is in use. // Fail if the superclass isn't kosher. - const char *name = bits->mangledName(); bool rootOK = bits->data()->flags & RO_ROOT; - if (getClass(name) || !verifySuperclass(bits->superclass, rootOK)){ + if (!verifySuperclass(bits->superclass, rootOK)){ return nil; } + // Duplicate classes are allowed, just like they are for image loading. + // readClass will complain about the duplicate. + Class cls = readClass(bits, false/*bundle*/, false/*shared cache*/); if (cls != bits) { // This function isn't allowed to remap anything. _objc_fatal("objc_readClassPair for class %s changed %p to %p", cls->nameForLogging(), bits, cls); } - realizeClass(cls); + + // The only client of this function is old Swift. + // Stable Swift won't use it. + // fixme once Swift in the OS settles we can assert(!cls->isSwiftStable()). + cls = realizeClassWithoutSwift(cls, nil); return cls; } @@ -5938,10 +7347,10 @@ Class objc_readClassPair(Class bits, const struct objc_image_info *info) **********************************************************************/ static void detach_class(Class cls, bool isMeta) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); // categories not yet attached to this class - removeAllUnattachedCategoriesForClass(cls); + objc::unattachedCategories.eraseClass(cls); // superclass's subclass list if (cls->isRealized()) { @@ -5957,6 +7366,7 @@ static void detach_class(Class cls, bool isMeta) if (!isMeta) { removeNamedClass(cls, cls->mangledName()); } + objc::allocatedClasses.get().erase(cls); } @@ -5968,19 +7378,22 @@ static void detach_class(Class cls, bool isMeta) **********************************************************************/ static void free_class(Class cls) { - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); if (! cls->isRealized()) return; auto rw = cls->data(); - auto ro = rw->ro; + auto rwe = rw->ext(); + auto ro = rw->ro(); cache_delete(cls); - - for (auto& meth : rw->methods) { - try_free(meth.types); + + if (rwe) { + for (auto& meth : rwe->methods) { + try_free(meth.types); + } + rwe->methods.tryFree(); } - rw->methods.tryFree(); const ivar_list_t *ivars = ro->ivars; if (ivars) { @@ -5992,53 +7405,58 @@ static void free_class(Class cls) try_free(ivars); } - for (auto& prop : rw->properties) { - try_free(prop.name); - try_free(prop.attributes); - } - rw->properties.tryFree(); + if (rwe) { + for (auto& prop : rwe->properties) { + try_free(prop.name); + try_free(prop.attributes); + } + rwe->properties.tryFree(); - rw->protocols.tryFree(); + rwe->protocols.tryFree(); + } try_free(ro->ivarLayout); try_free(ro->weakIvarLayout); try_free(ro->name); try_free(ro); - try_free(rw); + objc::zfree(rwe); + objc::zfree(rw); try_free(cls); } void objc_disposeClassPair(Class cls) { - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); - if (!(cls->data()->flags & (RW_CONSTRUCTED|RW_CONSTRUCTING)) || + checkIsKnownClass(cls); + + if (!(cls->data()->flags & (RW_CONSTRUCTED|RW_CONSTRUCTING)) || !(cls->ISA()->data()->flags & (RW_CONSTRUCTED|RW_CONSTRUCTING))) { // class not allocated with objc_allocateClassPair // disposing still-unregistered class is OK! _objc_inform("objc_disposeClassPair: class '%s' was not " "allocated with objc_allocateClassPair!", - cls->data()->ro->name); + cls->data()->ro()->name); return; } if (cls->isMetaClass()) { _objc_inform("objc_disposeClassPair: class '%s' is a metaclass, " - "not a class!", cls->data()->ro->name); + "not a class!", cls->data()->ro()->name); return; } // Shouldn't have any live subclasses. if (cls->data()->firstSubclass) { _objc_inform("objc_disposeClassPair: class '%s' still has subclasses, " - "including '%s'!", cls->data()->ro->name, + "including '%s'!", cls->data()->ro()->name, cls->data()->firstSubclass->nameForLogging()); } if (cls->ISA()->data()->firstSubclass) { _objc_inform("objc_disposeClassPair: class '%s' still has subclasses, " - "including '%s'!", cls->data()->ro->name, + "including '%s'!", cls->data()->ro()->name, cls->ISA()->data()->firstSubclass->nameForLogging()); } @@ -6062,7 +7480,7 @@ void objc_disposeClassPair(Class cls) * Note: class_createInstance() and class_createInstances() preflight this. **********************************************************************/ id -objc_constructInstance(Class cls, void *bytes) +objc_constructInstance(Class cls, void *bytes) { if (!cls || !bytes) return nil; @@ -6080,7 +7498,7 @@ objc_constructInstance(Class cls, void *bytes) } if (hasCxxCtor) { - return object_cxxConstructFromClass(obj, cls); + return object_cxxConstructFromClass(obj, cls, OBJECT_CONSTRUCT_NONE); } else { return obj; } @@ -6091,59 +7509,71 @@ objc_constructInstance(Class cls, void *bytes) * class_createInstance * fixme * Locking: none +* +* Note: this function has been carefully written so that the fastpath +* takes no branch. **********************************************************************/ - -static __attribute__((always_inline)) -id -_class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone, - bool cxxConstruct = true, +static ALWAYS_INLINE id +_class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone, + int construct_flags = OBJECT_CONSTRUCT_NONE, + bool cxxConstruct = true, size_t *outAllocatedSize = nil) { - if (!cls) return nil; - - assert(cls->isRealized()); + ASSERT(cls->isRealized()); // Read class's info bits all at once for performance - bool hasCxxCtor = cls->hasCxxCtor(); + bool hasCxxCtor = cxxConstruct && cls->hasCxxCtor(); bool hasCxxDtor = cls->hasCxxDtor(); bool fast = cls->canAllocNonpointer(); + size_t size; - size_t size = cls->instanceSize(extraBytes); + size = cls->instanceSize(extraBytes); if (outAllocatedSize) *outAllocatedSize = size; id obj; - if (!zone && fast) { + if (zone) { + obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size); + } else { obj = (id)calloc(1, size); - if (!obj) return nil; - obj->initInstanceIsa(cls, hasCxxDtor); - } - else { - if (zone) { - obj = (id)malloc_zone_calloc ((malloc_zone_t *)zone, 1, size); - } else { - obj = (id)calloc(1, size); + } + if (slowpath(!obj)) { + if (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) { + return _objc_callBadAllocHandler(cls); } - if (!obj) return nil; + return nil; + } - // Use raw pointer isa on the assumption that they might be + if (!zone && fast) { + obj->initInstanceIsa(cls, hasCxxDtor); + } else { + // Use raw pointer isa on the assumption that they might be // doing something weird with the zone or RR. obj->initIsa(cls); } - if (cxxConstruct && hasCxxCtor) { - obj = _objc_constructOrFree(obj, cls); + if (fastpath(!hasCxxCtor)) { + return obj; } - return obj; + construct_flags |= OBJECT_CONSTRUCT_FREE_ONFAILURE; + return object_cxxConstructFromClass(obj, cls, construct_flags); } - -id +id class_createInstance(Class cls, size_t extraBytes) { + if (!cls) return nil; return _class_createInstanceFromZone(cls, extraBytes, nil); } +NEVER_INLINE +id +_objc_rootAllocWithZone(Class cls, malloc_zone_t *zone __unused) +{ + // allocWithZone under __OBJC2__ ignores the zone parameter + return _class_createInstanceFromZone(cls, 0, nil, + OBJECT_CONSTRUCT_CALL_BADALLOC); +} /*********************************************************************** * class_createInstances @@ -6176,7 +7606,8 @@ _object_copyFromZone(id oldObj, size_t extraBytes, void *zone) Class cls = oldObj->ISA(); size_t size; - id obj = _class_createInstanceFromZone(cls, extraBytes, zone, false, &size); + id obj = _class_createInstanceFromZone(cls, extraBytes, zone, + OBJECT_CONSTRUCT_NONE, false, &size); if (!obj) return nil; // Copy everything except the isa, which was already set above. @@ -6203,7 +7634,7 @@ object_copy(id oldObj, size_t extraBytes) } -#if !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE) +#if SUPPORT_ZONES /*********************************************************************** * class_createInstanceFromZone @@ -6213,6 +7644,7 @@ object_copy(id oldObj, size_t extraBytes) id class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone) { + if (!cls) return nil; return _class_createInstanceFromZone(cls, extraBytes, zone); } @@ -6317,6 +7749,7 @@ Class _objc_getFreedObjectClass (void) #if !SUPPORT_TAGGED_POINTERS // These variables are always provided for debuggers. +uintptr_t objc_debug_taggedpointer_obfuscator = 0; uintptr_t objc_debug_taggedpointer_mask = 0; unsigned objc_debug_taggedpointer_slot_shift = 0; uintptr_t objc_debug_taggedpointer_slot_mask = 0; @@ -6334,12 +7767,16 @@ Class objc_debug_taggedpointer_ext_classes[1] = { nil }; static void disableTaggedPointers() { } +static void +initializeTaggedPointerObfuscator(void) { } + #else // The "slot" used in the class table and given to the debugger // includes the is-tagged bit. This makes objc_msgSend faster. // The "ext" representation doesn't do that. +uintptr_t objc_debug_taggedpointer_obfuscator; uintptr_t objc_debug_taggedpointer_mask = _OBJC_TAG_MASK; unsigned objc_debug_taggedpointer_slot_shift = _OBJC_TAG_SLOT_SHIFT; uintptr_t objc_debug_taggedpointer_slot_mask = _OBJC_TAG_SLOT_MASK; @@ -6376,11 +7813,15 @@ disableTaggedPointers() static Class * classSlotForBasicTagIndex(objc_tag_index_t tag) { + uintptr_t tagObfuscator = ((objc_debug_taggedpointer_obfuscator + >> _OBJC_TAG_INDEX_SHIFT) + & _OBJC_TAG_INDEX_MASK); + uintptr_t obfuscatedTag = tag ^ tagObfuscator; // Array index in objc_tag_classes includes the tagged bit itself #if SUPPORT_MSB_TAGGED_POINTERS - return &objc_tag_classes[0x8 | tag]; + return &objc_tag_classes[0x8 | obfuscatedTag]; #else - return &objc_tag_classes[(tag << 1) | 1]; + return &objc_tag_classes[(obfuscatedTag << 1) | 1]; #endif } @@ -6395,12 +7836,43 @@ classSlotForTagIndex(objc_tag_index_t tag) } if (tag >= OBJC_TAG_First52BitPayload && tag <= OBJC_TAG_Last52BitPayload) { - return &objc_tag_ext_classes[tag - OBJC_TAG_First52BitPayload]; + int index = tag - OBJC_TAG_First52BitPayload; + uintptr_t tagObfuscator = ((objc_debug_taggedpointer_obfuscator + >> _OBJC_TAG_EXT_INDEX_SHIFT) + & _OBJC_TAG_EXT_INDEX_MASK); + return &objc_tag_ext_classes[index ^ tagObfuscator]; } return nil; } +/*********************************************************************** +* initializeTaggedPointerObfuscator +* Initialize objc_debug_taggedpointer_obfuscator with randomness. +* +* The tagged pointer obfuscator is intended to make it more difficult +* for an attacker to construct a particular object as a tagged pointer, +* in the presence of a buffer overflow or other write control over some +* memory. The obfuscator is XORed with the tagged pointers when setting +* or retrieving payload values. They are filled with randomness on first +* use. +**********************************************************************/ +static void +initializeTaggedPointerObfuscator(void) +{ + if (sdkIsOlderThan(10_14, 12_0, 12_0, 5_0, 3_0) || + // Set the obfuscator to zero for apps linked against older SDKs, + // in case they're relying on the tagged pointer representation. + DisableTaggedPointerObfuscation) { + objc_debug_taggedpointer_obfuscator = 0; + } else { + // Pull random data into the variable, then shift away all non-payload bits. + arc4random_buf(&objc_debug_taggedpointer_obfuscator, + sizeof(objc_debug_taggedpointer_obfuscator)); + objc_debug_taggedpointer_obfuscator &= ~_OBJC_TAG_MASK; + } +} + /*********************************************************************** * _objc_registerTaggedPointerClass @@ -6496,15 +7968,15 @@ fixupMessageRef(message_ref_t *msg) msg->sel = sel_registerName((const char *)msg->sel); if (msg->imp == &objc_msgSend_fixup) { - if (msg->sel == SEL_alloc) { + if (msg->sel == @selector(alloc)) { msg->imp = (IMP)&objc_alloc; - } else if (msg->sel == SEL_allocWithZone) { + } else if (msg->sel == @selector(allocWithZone:)) { msg->imp = (IMP)&objc_allocWithZone; - } else if (msg->sel == SEL_retain) { + } else if (msg->sel == @selector(retain)) { msg->imp = (IMP)&objc_retain; - } else if (msg->sel == SEL_release) { + } else if (msg->sel == @selector(release)) { msg->imp = (IMP)&objc_release; - } else if (msg->sel == SEL_autorelease) { + } else if (msg->sel == @selector(autorelease)) { msg->imp = (IMP)&objc_autorelease; } else { msg->imp = &objc_msgSend_fixedup; @@ -6540,10 +8012,10 @@ static Class setSuperclass(Class cls, Class newSuper) { Class oldSuper; - runtimeLock.assertWriting(); + runtimeLock.assertLocked(); - assert(cls->isRealized()); - assert(newSuper->isRealized()); + ASSERT(cls->isRealized()); + ASSERT(newSuper->isRealized()); oldSuper = cls->superclass; removeSubclass(oldSuper, cls); @@ -6564,10 +8036,15 @@ static Class setSuperclass(Class cls, Class newSuper) Class class_setSuperclass(Class cls, Class newSuper) { - rwlock_writer_t lock(runtimeLock); + mutex_locker_t lock(runtimeLock); return setSuperclass(cls, newSuper); } +void runtime_init(void) +{ + objc::unattachedCategories.init(32); + objc::allocatedClasses.init(); +} // __OBJC2__ #endif diff --git a/runtime/objc-runtime-old.h b/runtime/objc-runtime-old.h index 5f0ff02..1467210 100644 --- a/runtime/objc-runtime-old.h +++ b/runtime/objc-runtime-old.h @@ -65,8 +65,8 @@ #define CLS_LEAF 0x800000 // class instances may have associative references #define CLS_INSTANCES_HAVE_ASSOCIATED_OBJECTS 0x1000000 -// class has instance-specific GC layout -#define CLS_HAS_INSTANCE_SPECIFIC_LAYOUT 0x2000000 +// available for use; was CLS_HAS_INSTANCE_SPECIFIC_LAYOUT +#define CLS_2000000 0x2000000 // class compiled with ARC #define CLS_IS_ARC 0x4000000 // class is not ARC but has ARC-style weak ivar layout @@ -205,7 +205,7 @@ struct objc_class : objc_object { // set and clear must not overlap void changeInfo(uint32_t set, uint32_t clear) { - assert((set & clear) == 0); + ASSERT((set & clear) == 0); uint32_t oldf, newf; do { @@ -234,20 +234,19 @@ struct objc_class : objc_object { return info & CLS_IS_ARC; } - bool hasCustomRR() { + bool hasCustomRR() { return true; } - void setHasCustomRR(bool = false) { } - void setHasDefaultRR() { } - void printCustomRR(bool) { } - bool hasCustomAWZ() { + bool hasCustomAWZ() { return true; } - void setHasCustomAWZ(bool = false) { } - void setHasDefaultAWZ() { } - void printCustomAWZ(bool) { } + bool forbidsAssociatedObjects() { + // Old runtime doesn't support forbidding associated objects. + return false; + } + bool instancesHaveAssociatedObjects() { return info & CLS_INSTANCES_HAVE_ASSOCIATED_OBJECTS; } @@ -300,6 +299,14 @@ struct objc_class : objc_object { const char *mangledName() { return name; } const char *demangledName() { return name; } const char *nameForLogging() { return name; } + + bool isRootClass() { + return superclass == nil; + } + + bool isRootMetaclass() { + return ISA() == (Class)this; + } bool isMetaClass() { return info & CLS_META; diff --git a/runtime/objc-runtime-old.mm b/runtime/objc-runtime-old.mm index d6b393e..3d8c0b2 100644 --- a/runtime/objc-runtime-old.mm +++ b/runtime/objc-runtime-old.mm @@ -365,7 +365,7 @@ objc_copyProtocolList(unsigned int *outCount) } result[i++] = nil; - assert(i == count+1); + ASSERT(i == count+1); if (outCount) *outCount = count; return result; @@ -879,7 +879,7 @@ static void really_connect_class(Class cls, // No duplicate classes allowed. // Duplicates should have been rejected by _objc_read_classes_from_image - assert(!oldCls); + ASSERT(!oldCls); } // Fix up pended class refs to this class, if any @@ -1337,7 +1337,7 @@ static inline void map_selrefs(SEL *sels, size_t count, bool copy) if (!sels) return; - sel_lock(); + mutex_locker_t lock(selLock); // Process each selector for (index = 0; index < count; index += 1) @@ -1353,8 +1353,6 @@ static inline void map_selrefs(SEL *sels, size_t count, bool copy) sels[index] = sel; } } - - sel_unlock(); } @@ -1371,7 +1369,7 @@ static void map_method_descs (struct objc_method_description_list * methods, bo if (!methods) return; - sel_lock(); + mutex_locker_t lock(selLock); // Process each method for (index = 0; index < methods->count; index += 1) @@ -1390,8 +1388,6 @@ static void map_method_descs (struct objc_method_description_list * methods, bo if (method->name != sel) method->name = sel; } - - sel_unlock(); } @@ -1784,7 +1780,7 @@ Protocol * objc_allocateProtocol(const char *name) { Class cls = objc_getClass("__IncompleteProtocol"); - assert(cls); + ASSERT(cls); mutex_locker_t lock(classLock); @@ -2157,8 +2153,8 @@ unmap_image(const char *path __unused, const struct mach_header *mh) * Calls ABI-agnostic code after taking ABI-specific locks. **********************************************************************/ void -map_2_images(unsigned count, const char * const paths[], - const struct mach_header * const mhdrs[]) +map_images(unsigned count, const char * const paths[], + const struct mach_header * const mhdrs[]) { recursive_mutex_locker_t lock(loadMethodLock); map_images_nolock(count, paths, mhdrs); @@ -2200,6 +2196,11 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un // Parts of this order are important for correctness or performance. + // Fix up selector refs from all images. + for (i = 0; i < hCount; i++) { + _objc_fixup_selector_refs(hList[i]); + } + // Read classes from all images. for (i = 0; i < hCount; i++) { _objc_read_classes_from_image(hList[i]); @@ -2222,10 +2223,9 @@ void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int un _objc_connect_classes_from_image(hList[i]); } - // Fix up class refs, selector refs, and protocol objects from all images. + // Fix up class refs, and protocol objects from all images. for (i = 0; i < hCount; i++) { _objc_map_class_refs_for_image(hList[i]); - _objc_fixup_selector_refs(hList[i]); _objc_fixup_protocol_objects_for_image(hList[i]); } @@ -3153,7 +3153,46 @@ static bool _objc_register_category(old_category *cat, int version) } -const char ** +const char **objc_copyImageNames(unsigned int *outCount) +{ + header_info *hi; + int count = 0; + int max = 0; + for (hi = FirstHeader; hi != nil; hi = hi->getNext()) { + max++; + } +#if TARGET_OS_WIN32 + const TCHAR **names = (const TCHAR **)calloc(max+1, sizeof(TCHAR *)); +#else + const char **names = (const char **)calloc(max+1, sizeof(char *)); +#endif + + for (hi = FirstHeader; hi != NULL && count < max; hi = hi->getNext()) { +#if TARGET_OS_WIN32 + if (hi->moduleName) { + names[count++] = hi->moduleName; + } +#else + const char *fname = hi->fname(); + if (fname) { + names[count++] = fname; + } +#endif + } + names[count] = NULL; + + if (count == 0) { + // Return NULL instead of empty list if there are no images + free((void *)names); + names = NULL; + } + + if (outCount) *outCount = count; + return names; +} + + +static const char ** _objc_copyClassNamesForImage(header_info *hi, unsigned int *outCount) { Module mods; @@ -3200,6 +3239,66 @@ _objc_copyClassNamesForImage(header_info *hi, unsigned int *outCount) return list; } + +/********************************************************************** +* +**********************************************************************/ +const char ** +objc_copyClassNamesForImage(const char *image, unsigned int *outCount) +{ + header_info *hi; + + if (!image) { + if (outCount) *outCount = 0; + return NULL; + } + + // Find the image. + for (hi = FirstHeader; hi != NULL; hi = hi->getNext()) { +#if TARGET_OS_WIN32 + if (0 == wcscmp((TCHAR *)image, hi->moduleName)) break; +#else + if (0 == strcmp(image, hi->fname())) break; +#endif + } + + if (!hi) { + if (outCount) *outCount = 0; + return NULL; + } + + return _objc_copyClassNamesForImage(hi, outCount); +} + + + +/********************************************************************** +* +**********************************************************************/ +const char ** +objc_copyClassNamesForImageHeader(const struct mach_header *mh, unsigned int *outCount) +{ + header_info *hi; + + if (!mh) { + if (outCount) *outCount = 0; + return NULL; + } + + // Find the image. + for (hi = FirstHeader; hi != NULL; hi = hi->getNext()) { + if (hi->mhdr() == (const headerType *)mh) break; + } + + if (!hi) { + if (outCount) *outCount = 0; + return NULL; + } + + return _objc_copyClassNamesForImage(hi, outCount); +} + + Class gdb_class_getClass(Class cls) { const char *className = cls->name; @@ -3216,16 +3315,27 @@ Class gdb_object_getClass(id obj) } +/*********************************************************************** +* objc_setMultithreaded. +**********************************************************************/ +void objc_setMultithreaded (BOOL flag) +{ + OBJC_WARN_DEPRECATED; + + // Nothing here. Thread synchronization in the runtime is always active. +} + + /*********************************************************************** * Lock management **********************************************************************/ -rwlock_t selLock; +mutex_t selLock; mutex_t classLock; mutex_t methodListLock; mutex_t cacheUpdateLock; recursive_mutex_t loadMethodLock; -void lock_init(void) +void runtime_init(void) { } diff --git a/runtime/objc-runtime.mm b/runtime/objc-runtime.mm index e9efe55..08a1b77 100644 --- a/runtime/objc-runtime.mm +++ b/runtime/objc-runtime.mm @@ -35,15 +35,40 @@ #include "objc-private.h" #include "objc-loadmethod.h" +#include "objc-file.h" #include "message.h" -OBJC_EXPORT Class getOriginalClassForPosingClass(Class); - - /*********************************************************************** * Exports. **********************************************************************/ +/* Linker metadata symbols */ + +// NSObject was in Foundation/CF on macOS < 10.8. +#if TARGET_OS_OSX +#if __OBJC2__ + +const char __objc_nsobject_class_10_5 = 0; +const char __objc_nsobject_class_10_6 = 0; +const char __objc_nsobject_class_10_7 = 0; + +const char __objc_nsobject_metaclass_10_5 = 0; +const char __objc_nsobject_metaclass_10_6 = 0; +const char __objc_nsobject_metaclass_10_7 = 0; + +const char __objc_nsobject_isa_10_5 = 0; +const char __objc_nsobject_isa_10_6 = 0; +const char __objc_nsobject_isa_10_7 = 0; + +#else + +const char __objc_nsobject_class_10_5 = 0; +const char __objc_nsobject_class_10_6 = 0; +const char __objc_nsobject_class_10_7 = 0; + +#endif +#endif + // Settings from environment variables #define OPTION(var, env, help) bool var = false; #include "objc-env.h" @@ -64,34 +89,47 @@ const option_t Settings[] = { // objc's key for pthread_getspecific +#if SUPPORT_DIRECT_THREAD_KEYS +#define _objc_pthread_key TLS_DIRECT_KEY +#else static tls_key_t _objc_pthread_key; +#endif // Selectors -SEL SEL_load = NULL; -SEL SEL_initialize = NULL; -SEL SEL_resolveInstanceMethod = NULL; -SEL SEL_resolveClassMethod = NULL; SEL SEL_cxx_construct = NULL; SEL SEL_cxx_destruct = NULL; -SEL SEL_retain = NULL; -SEL SEL_release = NULL; -SEL SEL_autorelease = NULL; -SEL SEL_retainCount = NULL; -SEL SEL_alloc = NULL; -SEL SEL_allocWithZone = NULL; -SEL SEL_dealloc = NULL; -SEL SEL_copy = NULL; -SEL SEL_new = NULL; -SEL SEL_forwardInvocation = NULL; -SEL SEL_tryRetain = NULL; -SEL SEL_isDeallocating = NULL; -SEL SEL_retainWeakReference = NULL; -SEL SEL_allowsWeakReference = NULL; - +struct objc::SafeRanges objc::dataSegmentsRanges; header_info *FirstHeader = 0; // NULL means empty list header_info *LastHeader = 0; // NULL means invalid; recompute it -int HeaderCount = 0; + +// Set to true on the child side of fork() +// if the parent process was multithreaded when fork() was called. +bool MultithreadedForkChild = false; + + +/*********************************************************************** +* objc_noop_imp. Used when we need to install a do-nothing method somewhere. +**********************************************************************/ +id objc_noop_imp(id self, SEL _cmd __unused) { + return self; +} + + +/*********************************************************************** +* _objc_isDebugBuild. Defined in debug builds only. +* Some test code looks for the presence of this symbol. +**********************************************************************/ +#if DEBUG != OBJC_IS_DEBUG_BUILD +#error mismatch in debug-ness macros +// DEBUG is used in our code. OBJC_IS_DEBUG_BUILD is used in the +// header declaration of _objc_isDebugBuild() because that header +// is visible to other clients who might have their own DEBUG macro. +#endif + +#if OBJC_IS_DEBUG_BUILD +void _objc_isDebugBuild(void) { } +#endif /*********************************************************************** @@ -159,6 +197,72 @@ Class objc_getMetaClass(const char *aClassName) return cls->ISA(); } +/*********************************************************************** + * objc::SafeRanges::find. Find an image data segment that contains address + **********************************************************************/ +bool +objc::SafeRanges::find(uintptr_t ptr, uint32_t &pos) +{ + if (!sorted) { + std::sort(ranges, ranges + count, [](const Range &s1, const Range &s2){ + return s1.start < s2.start; + }); + sorted = true; + } + + uint32_t l = 0, r = count; + while (l < r) { + uint32_t i = (l + r) / 2; + + if (ptr < ranges[i].start) { + r = i; + } else if (ptr >= ranges[i].end) { + l = i + 1; + } else { + pos = i; + return true; + } + } + + pos = UINT32_MAX; + return false; +} + +/*********************************************************************** + * objc::SafeRanges::add. Register a new well known data segment. + **********************************************************************/ +void +objc::SafeRanges::add(uintptr_t start, uintptr_t end) +{ + if (count == size) { + // Have a typical malloc growth: + // - size <= 32: grow by 4 + // - size <= 64: grow by 8 + // - size <= 128: grow by 16 + // ... etc + size += size < 16 ? 4 : 1 << (fls(size) - 3); + ranges = (Range *)realloc(ranges, sizeof(Range) * size); + } + ranges[count++] = Range{ start, end }; + sorted = false; +} + +/*********************************************************************** + * objc::SafeRanges::remove. Remove a previously known data segment. + **********************************************************************/ +void +objc::SafeRanges::remove(uintptr_t start, uintptr_t end) +{ + uint32_t pos; + + if (!find(start, pos) || ranges[pos].end != end) { + _objc_fatal("Cannot find range %#lx..%#lx", start, end); + } + if (pos < --count) { + ranges[pos] = ranges[count]; + sorted = false; + } +} /*********************************************************************** * appendHeader. Add a newly-constructed header_info to the list. @@ -167,7 +271,6 @@ void appendHeader(header_info *hi) { // Add the header to the header list. // The header is appended to the list, to preserve the bottom-up order. - HeaderCount++; hi->setNext(NULL); if (!FirstHeader) { // list is empty @@ -182,6 +285,15 @@ void appendHeader(header_info *hi) LastHeader->setNext(hi); LastHeader = hi; } + +#if __OBJC2__ + if ((hi->mhdr()->flags & MH_DYLIB_IN_CACHE) == 0) { + foreach_data_segment(hi->mhdr(), [](const segmentType *seg, intptr_t slide) { + uintptr_t start = (uintptr_t)seg->vmaddr + slide; + objc::dataSegmentsRanges.add(start, start + seg->vmsize); + }); + } +#endif } @@ -211,12 +323,19 @@ void removeHeader(header_info *hi) if (LastHeader == deadHead) { LastHeader = NULL; // will be recomputed next time it's used } - - HeaderCount--; break; } prev = current; } + +#if __OBJC2__ + if ((hi->mhdr()->flags & MH_DYLIB_IN_CACHE) == 0) { + foreach_data_segment(hi->mhdr(), [](const segmentType *seg, intptr_t slide) { + uintptr_t start = (uintptr_t)seg->vmaddr + slide; + objc::dataSegmentsRanges.remove(start, start + seg->vmsize); + }); + } +#endif } @@ -326,7 +445,7 @@ logReplacedMethod(const char *className, SEL s, const char *newImage = "??"; // Silently ignore +load replacement because category +load is special - if (s == SEL_load) return; + if (s == @selector(load)) return; #if TARGET_OS_WIN32 // don't know dladdr()/dli_fname equivalent @@ -344,18 +463,6 @@ logReplacedMethod(const char *className, SEL s, } - -/*********************************************************************** -* objc_setMultithreaded. -**********************************************************************/ -void objc_setMultithreaded (BOOL flag) -{ - OBJC_WARN_DEPRECATED; - - // Nothing here. Thread synchronization in the runtime is always active. -} - - /*********************************************************************** * _objc_fetch_pthread_data * Fetch objc's pthread data for this thread. @@ -395,6 +502,7 @@ void _objc_pthread_destroyspecific(void *arg) free(data->printableNames[i]); } } + free(data->classNameLookups); // add further cleanup here... @@ -406,7 +514,6 @@ void _objc_pthread_destroyspecific(void *arg) void tls_init(void) { #if SUPPORT_DIRECT_THREAD_KEYS - _objc_pthread_key = TLS_DIRECT_KEY; pthread_key_init_np(TLS_DIRECT_KEY, &_objc_pthread_destroyspecific); #else _objc_pthread_key = tls_create(&_objc_pthread_destroyspecific); @@ -439,7 +546,7 @@ void *_objc_forward_stret_handler = nil; #else // Default forward handler halts the process. -__attribute__((noreturn)) void +__attribute__((noreturn, cold)) void objc_defaultForwardHandler(id self, SEL sel) { _objc_fatal("%c[%s %s]: unrecognized selector sent to instance %p " @@ -451,7 +558,7 @@ void *_objc_forward_handler = (void*)objc_defaultForwardHandler; #if SUPPORT_STRET struct stret { int i[100]; }; -__attribute__((noreturn)) struct stret +__attribute__((noreturn, cold)) struct stret objc_defaultForwardStretHandler(id self, SEL sel) { objc_defaultForwardHandler(self, sel); @@ -474,109 +581,36 @@ void objc_setForwardHandler(void *fwd, void *fwd_stret) // GrP fixme extern "C" Class _objc_getOrigClass(const char *name); #endif -const char *class_getImageName(Class cls) -{ -#if TARGET_OS_WIN32 - TCHAR *szFileName; - DWORD charactersCopied; - Class origCls; - HMODULE classModule; - bool res; -#endif - if (!cls) return NULL; +static BOOL internal_class_getImageName(Class cls, const char **outName) +{ #if !__OBJC2__ cls = _objc_getOrigClass(cls->demangledName()); #endif -#if TARGET_OS_WIN32 - charactersCopied = 0; - szFileName = malloc(MAX_PATH * sizeof(TCHAR)); - - origCls = objc_getOrigClass(cls->demangledName()); - classModule = NULL; - res = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)origCls, &classModule); - if (res && classModule) { - charactersCopied = GetModuleFileName(classModule, szFileName, MAX_PATH * sizeof(TCHAR)); - } - if (classModule) FreeLibrary(classModule); - if (charactersCopied) { - return (const char *)szFileName; - } else { - free(szFileName); - } - return NULL; -#else - return dyld_image_path_containing_address(cls); -#endif + auto result = dyld_image_path_containing_address(cls); + *outName = result; + return (result != nil); } -const char **objc_copyImageNames(unsigned int *outCount) +static ChainedHookFunction +GetImageNameHook{internal_class_getImageName}; + +void objc_setHook_getImageName(objc_hook_getImageName newValue, + objc_hook_getImageName *outOldValue) { - header_info *hi; - int count = 0; - int max = HeaderCount; -#if TARGET_OS_WIN32 - const TCHAR **names = (const TCHAR **)calloc(max+1, sizeof(TCHAR *)); -#else - const char **names = (const char **)calloc(max+1, sizeof(char *)); -#endif - - for (hi = FirstHeader; hi != NULL && count < max; hi = hi->getNext()) { -#if TARGET_OS_WIN32 - if (hi->moduleName) { - names[count++] = hi->moduleName; - } -#else - const char *fname = hi->fname(); - if (fname) { - names[count++] = fname; - } -#endif - } - names[count] = NULL; - - if (count == 0) { - // Return NULL instead of empty list if there are no images - free((void *)names); - names = NULL; - } - - if (outCount) *outCount = count; - return names; + GetImageNameHook.set(newValue, outOldValue); } - -/********************************************************************** -* -**********************************************************************/ -const char ** -objc_copyClassNamesForImage(const char *image, unsigned int *outCount) +const char *class_getImageName(Class cls) { - header_info *hi; + if (!cls) return nil; - if (!image) { - if (outCount) *outCount = 0; - return NULL; - } - - // Find the image. - for (hi = FirstHeader; hi != NULL; hi = hi->getNext()) { -#if TARGET_OS_WIN32 - if (0 == wcscmp((TCHAR *)image, hi->moduleName)) break; -#else - if (0 == strcmp(image, hi->fname())) break; -#endif - } - - if (!hi) { - if (outCount) *outCount = 0; - return NULL; - } - - return _objc_copyClassNamesForImage(hi, outCount); + const char *name; + if (GetImageNameHook.get()(cls, &name)) return name; + else return nil; } - + /********************************************************************** * Fast Enumeration Support @@ -609,13 +643,30 @@ void objc_setEnumerationMutationHandler(void (*handler)(id)) { * Associative Reference Support **********************************************************************/ -id objc_getAssociatedObject(id object, const void *key) { - return _object_get_associative_reference(object, (void *)key); +id +objc_getAssociatedObject(id object, const void *key) +{ + return _object_get_associative_reference(object, key); } +static void +_base_objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) +{ + _object_set_associative_reference(object, key, value, policy); +} -void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) { - _object_set_associative_reference(object, (void *)key, value, policy); +static ChainedHookFunction SetAssocHook{_base_objc_setAssociatedObject}; + +void +objc_setHook_setAssociatedObject(objc_hook_setAssociatedObject _Nonnull newValue, + objc_hook_setAssociatedObject _Nullable * _Nonnull outOldValue) { + SetAssocHook.set(newValue, outOldValue); +} + +void +objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) +{ + SetAssocHook.get()(object, key, value, policy); } diff --git a/runtime/objc-sel-old.mm b/runtime/objc-sel-old.mm index c956494..2a3a242 100644 --- a/runtime/objc-sel-old.mm +++ b/runtime/objc-sel-old.mm @@ -77,42 +77,35 @@ BOOL sel_isMapped(SEL name) sel = _objc_search_builtins((const char *)name); if (sel) return YES; - rwlock_reader_t lock(selLock); + mutex_locker_t lock(selLock); if (_objc_selectors) { sel = __objc_sel_set_get(_objc_selectors, name); } return bool(sel); } -static SEL __sel_registerName(const char *name, int lock, int copy) +static SEL __sel_registerName(const char *name, bool shouldLock, bool copy) { SEL result = 0; - if (lock) selLock.assertUnlocked(); - else selLock.assertWriting(); + if (shouldLock) selLock.assertUnlocked(); + else selLock.assertLocked(); if (!name) return (SEL)0; result = _objc_search_builtins(name); if (result) return result; - - if (lock) selLock.read(); + + conditional_mutex_locker_t lock(selLock, shouldLock); if (_objc_selectors) { result = __objc_sel_set_get(_objc_selectors, (SEL)name); } - if (lock) selLock.unlockRead(); if (result) return result; // No match. Insert. - if (lock) selLock.write(); - if (!_objc_selectors) { _objc_selectors = __objc_sel_set_create(SelrefCount); } - if (lock) { - // Rescan in case it was added while we dropped the lock - result = __objc_sel_set_get(_objc_selectors, (SEL)name); - } if (!result) { result = (SEL)(copy ? strdup(name) : name); __objc_sel_set_add(_objc_selectors, result); @@ -121,7 +114,6 @@ static SEL __sel_registerName(const char *name, int lock, int copy) #endif } - if (lock) selLock.unlockWrite(); return result; } @@ -134,16 +126,6 @@ SEL sel_registerNameNoLock(const char *name, bool copy) { return __sel_registerName(name, 0, copy); // NO lock, maybe copy } -void sel_lock(void) -{ - selLock.write(); -} - -void sel_unlock(void) -{ - selLock.unlockWrite(); -} - // 2001/1/24 // the majority of uses of this function (which used to return NULL if not found) @@ -175,39 +157,13 @@ void sel_init(size_t selrefCount) // Register selectors used by libobjc -#define s(x) SEL_##x = sel_registerNameNoLock(#x, NO) -#define t(x,y) SEL_##y = sel_registerNameNoLock(#x, NO) + mutex_locker_t lock(selLock); - sel_lock(); - - s(load); - s(initialize); - t(resolveInstanceMethod:, resolveInstanceMethod); - t(resolveClassMethod:, resolveClassMethod); - t(.cxx_construct, cxx_construct); - t(.cxx_destruct, cxx_destruct); - s(retain); - s(release); - s(autorelease); - s(retainCount); - s(alloc); - t(allocWithZone:, allocWithZone); - s(dealloc); - s(copy); - s(new); - t(forwardInvocation:, forwardInvocation); - t(_tryRetain, tryRetain); - t(_isDeallocating, isDeallocating); - s(retainWeakReference); - s(allowsWeakReference); + SEL_cxx_construct = sel_registerNameNoLock(".cxx_construct", NO); + SEL_cxx_destruct = sel_registerNameNoLock(".cxx_destruct", NO); extern SEL FwdSel; FwdSel = sel_registerNameNoLock("forward::", NO); - - sel_unlock(); - -#undef s -#undef t } __END_DECLS diff --git a/runtime/objc-sel-set.mm b/runtime/objc-sel-set.mm index 79ca591..ab21b00 100644 --- a/runtime/objc-sel-set.mm +++ b/runtime/objc-sel-set.mm @@ -120,7 +120,7 @@ struct __objc_sel_set *__objc_sel_set_create(size_t selrefs) { sset->_count = 0; // heuristic to convert executable's selrefs count to table size -#if TARGET_OS_IPHONE +#if TARGET_OS_IPHONE && !TARGET_OS_IOSMAC for (idx = 0; __objc_sel_set_capacities[idx] < selrefs; idx++); if (idx > 0 && selrefs < 1536) idx--; #else diff --git a/runtime/objc-sel-table.S b/runtime/objc-sel-table.S index 359df9b..6d9710d 100644 --- a/runtime/objc-sel-table.S +++ b/runtime/objc-sel-table.S @@ -7,6 +7,9 @@ # define PTR(x) .long x #endif +// These offsets are populated by the dyld shared cache builder. +// They point to memory allocatd elsewhere in the shared cache. + .section __TEXT,__objc_opt_ro .align 3 .private_extern __objc_opt_data @@ -20,39 +23,6 @@ __objc_opt_data: .long 0 /* table.headeropt_rw_offset */ .space PAGE_MAX_SIZE-28 -/* space for selopt, smax/capacity=524288, blen/mask=262143+1 */ -.space 262144 /* mask tab */ -.space 524288 /* checkbytes */ -.space 524288*4 /* offsets */ - -/* space for clsopt, smax/capacity=65536, blen/mask=16383+1 */ -.space 16384 /* mask tab */ -.space 65536 /* checkbytes */ -.space 65536*12 /* offsets to name and class and header_info */ -.space PAGE_MAX_SIZE /* some duplicate classes */ - -/* space for protocolopt, smax/capacity=8192, blen/mask=4095+1 */ -.space 4096 /* mask tab */ -.space 8192 /* checkbytes */ -.space 8192*4 /* offsets */ - -/* space for header_info (RO) structures */ -.space 16384 - -.section __DATA,__objc_opt_rw -.align 3 -.private_extern __objc_opt_rw_data -__objc_opt_rw_data: -/* space for header_info (RW) structures */ -.space 16384 - -/* space for 8192 protocols */ -#if __LP64__ -.space 8192 * 11 * 8 -#else -.space 8192 * 11 * 4 -#endif - /* section of pointers that the shared cache optimizer wants to know about */ .section __DATA,__objc_opt_ptrs diff --git a/runtime/objc-sel.mm b/runtime/objc-sel.mm index 4c7fd29..27ee356 100644 --- a/runtime/objc-sel.mm +++ b/runtime/objc-sel.mm @@ -25,16 +25,15 @@ #include "objc-private.h" #include "objc-cache.h" +#include "DenseMapExtras.h" #if SUPPORT_PREOPT static const objc_selopt_t *builtins = NULL; +static bool useDyldSelectorLookup = false; #endif -static size_t SelrefCount = 0; - -static NXMapTable *namedSelectors; - +static objc::ExplicitInitDenseSet namedSelectors; static SEL search_builtins(const char *key); @@ -44,11 +43,17 @@ static SEL search_builtins(const char *key); **********************************************************************/ void sel_init(size_t selrefCount) { - // save this value for later - SelrefCount = selrefCount; - #if SUPPORT_PREOPT - builtins = preoptimizedSelectors(); + // If dyld finds a known shared cache selector, then it must be also looking + // in the shared cache table. + if (_dyld_get_objc_selector("retain") != nil) + useDyldSelectorLookup = true; + else + builtins = preoptimizedSelectors(); + + if (PrintPreopt && useDyldSelectorLookup) { + _objc_inform("PREOPTIMIZATION: using dyld selector opt"); + } if (PrintPreopt && builtins) { uint32_t occupied = builtins->occupied; @@ -59,47 +64,24 @@ void sel_init(size_t selrefCount) _objc_inform("PREOPTIMIZATION: %u/%u (%u%%) hash table occupancy", occupied, capacity, (unsigned)(occupied/(double)capacity*100)); - } + } + namedSelectors.init(useDyldSelectorLookup ? 0 : (unsigned)selrefCount); +#else + namedSelectors.init((unsigned)selrefCount); #endif // Register selectors used by libobjc -#define s(x) SEL_##x = sel_registerNameNoLock(#x, NO) -#define t(x,y) SEL_##y = sel_registerNameNoLock(#x, NO) + mutex_locker_t lock(selLock); - sel_lock(); - - s(load); - s(initialize); - t(resolveInstanceMethod:, resolveInstanceMethod); - t(resolveClassMethod:, resolveClassMethod); - t(.cxx_construct, cxx_construct); - t(.cxx_destruct, cxx_destruct); - s(retain); - s(release); - s(autorelease); - s(retainCount); - s(alloc); - t(allocWithZone:, allocWithZone); - s(dealloc); - s(copy); - s(new); - t(forwardInvocation:, forwardInvocation); - t(_tryRetain, tryRetain); - t(_isDeallocating, isDeallocating); - s(retainWeakReference); - s(allowsWeakReference); - - sel_unlock(); - -#undef s -#undef t + SEL_cxx_construct = sel_registerNameNoLock(".cxx_construct", NO); + SEL_cxx_destruct = sel_registerNameNoLock(".cxx_destruct", NO); } static SEL sel_alloc(const char *name, bool copy) { - selLock.assertWriting(); + selLock.assertLocked(); return (SEL)(copy ? strdupIfMutable(name) : name); } @@ -119,62 +101,50 @@ BOOL sel_isMapped(SEL sel) if (sel == search_builtins(name)) return YES; - rwlock_reader_t lock(selLock); - if (namedSelectors) { - return (sel == (SEL)NXMapGet(namedSelectors, name)); - } - return false; + mutex_locker_t lock(selLock); + auto it = namedSelectors.get().find(name); + return it != namedSelectors.get().end() && (SEL)*it == sel; } static SEL search_builtins(const char *name) { #if SUPPORT_PREOPT - if (builtins) return (SEL)builtins->get(name); + if (builtins) { + SEL result = 0; + if ((result = (SEL)builtins->get(name))) + return result; + + if ((result = (SEL)_dyld_get_objc_selector(name))) + return result; + } else if (useDyldSelectorLookup) { + if (SEL result = (SEL)_dyld_get_objc_selector(name)) + return result; + } #endif return nil; } -static SEL __sel_registerName(const char *name, int lock, int copy) +static SEL __sel_registerName(const char *name, bool shouldLock, bool copy) { SEL result = 0; - if (lock) selLock.assertUnlocked(); - else selLock.assertWriting(); + if (shouldLock) selLock.assertUnlocked(); + else selLock.assertLocked(); if (!name) return (SEL)0; result = search_builtins(name); if (result) return result; - if (lock) selLock.read(); - if (namedSelectors) { - result = (SEL)NXMapGet(namedSelectors, name); - } - if (lock) selLock.unlockRead(); - if (result) return result; - - // No match. Insert. - - if (lock) selLock.write(); - - if (!namedSelectors) { - namedSelectors = NXCreateMapTable(NXStrValueMapPrototype, - (unsigned)SelrefCount); - } - if (lock) { - // Rescan in case it was added while we dropped the lock - result = (SEL)NXMapGet(namedSelectors, name); - } - if (!result) { - result = sel_alloc(name, copy); - // fixme choose a better container (hash not map for starters) - NXMapInsert(namedSelectors, sel_getName(result), result); - } - - if (lock) selLock.unlockWrite(); - return result; + conditional_mutex_locker_t lock(selLock, shouldLock); + auto it = namedSelectors.get().insert(name); + if (it.second) { + // No match. Insert. + *it.first = (const char *)sel_alloc(name, copy); + } + return (SEL)*it.first; } @@ -186,16 +156,6 @@ SEL sel_registerNameNoLock(const char *name, bool copy) { return __sel_registerName(name, 0, copy); // NO lock, maybe copy } -void sel_lock(void) -{ - selLock.write(); -} - -void sel_unlock(void) -{ - selLock.unlockWrite(); -} - // 2001/1/24 // the majority of uses of this function (which used to return NULL if not found) diff --git a/runtime/objc-sync.h b/runtime/objc-sync.h index 93a96fc..e9ab64f 100644 --- a/runtime/objc-sync.h +++ b/runtime/objc-sync.h @@ -35,33 +35,25 @@ * * @return OBJC_SYNC_SUCCESS once lock is acquired. */ -OBJC_EXPORT int objc_sync_enter(id obj) - OBJC_AVAILABLE(10.3, 2.0, 9.0, 1.0); +OBJC_EXPORT int +objc_sync_enter(id _Nonnull obj) + OBJC_AVAILABLE(10.3, 2.0, 9.0, 1.0, 2.0); /** * End synchronizing on 'obj'. * - * @param obj The objet to end synchronizing on. + * @param obj The object to end synchronizing on. * * @return OBJC_SYNC_SUCCESS or OBJC_SYNC_NOT_OWNING_THREAD_ERROR */ -OBJC_EXPORT int objc_sync_exit(id obj) - OBJC_AVAILABLE(10.3, 2.0, 9.0, 1.0); - -// The wait/notify functions have never worked correctly and no longer exist. -OBJC_EXPORT int objc_sync_wait(id obj, long long milliSecondsMaxWait) - UNAVAILABLE_ATTRIBUTE; -OBJC_EXPORT int objc_sync_notify(id obj) - UNAVAILABLE_ATTRIBUTE; -OBJC_EXPORT int objc_sync_notifyAll(id obj) - UNAVAILABLE_ATTRIBUTE; +OBJC_EXPORT int +objc_sync_exit(id _Nonnull obj) + OBJC_AVAILABLE(10.3, 2.0, 9.0, 1.0, 2.0); enum { - OBJC_SYNC_SUCCESS = 0, - OBJC_SYNC_NOT_OWNING_THREAD_ERROR = -1, - OBJC_SYNC_TIMED_OUT = -2, - OBJC_SYNC_NOT_INITIALIZED = -3 + OBJC_SYNC_SUCCESS = 0, + OBJC_SYNC_NOT_OWNING_THREAD_ERROR = -1 }; -#endif // __OBJC_SNYC_H_ +#endif // __OBJC_SYNC_H_ diff --git a/runtime/objc-sync.mm b/runtime/objc-sync.mm index cb69981..d29a822 100644 --- a/runtime/objc-sync.mm +++ b/runtime/objc-sync.mm @@ -30,7 +30,7 @@ // -typedef struct SyncData { +typedef struct alignas(CacheLineSize) SyncData { struct SyncData* nextData; DisguisedPtr object; int32_t threadCount; // number of THREADS using this block @@ -60,7 +60,7 @@ struct SyncList { SyncData *data; spinlock_t lock; - SyncList() : data(nil) { } + constexpr SyncList() : data(nil), lock(fork_unsafe_lock) { } }; // Use multiple parallel lists to decrease contention among unrelated objects. @@ -228,14 +228,14 @@ static SyncData* id2data(id object, enum usage why) } } - // malloc a new SyncData and add to list. - // XXX calling malloc with a global lock held is bad practice, - // might be worth releasing the lock, mallocing, and searching again. - // But since we never free these guys we won't be stuck in malloc very often. - result = (SyncData*)calloc(sizeof(SyncData), 1); + // Allocate a new SyncData and add to list. + // XXX allocating memory with a global lock held is bad practice, + // might be worth releasing the lock, allocating, and searching again. + // But since we never free these guys we won't be stuck in allocation very often. + posix_memalign((void **)&result, alignof(SyncData), sizeof(SyncData)); result->object = (objc_object *)object; result->threadCount = 1; - new (&result->mutex) recursive_mutex_t(); + new (&result->mutex) recursive_mutex_t(fork_unsafe_lock); result->nextData = *listp; *listp = result; @@ -287,7 +287,7 @@ int objc_sync_enter(id obj) if (obj) { SyncData* data = id2data(obj, ACQUIRE); - assert(data); + ASSERT(data); data->mutex.lock(); } else { // @synchronized(nil) does nothing @@ -300,6 +300,25 @@ int objc_sync_enter(id obj) return result; } +BOOL objc_sync_try_enter(id obj) +{ + BOOL result = YES; + + if (obj) { + SyncData* data = id2data(obj, ACQUIRE); + ASSERT(data); + result = data->mutex.tryLock(); + } else { + // @synchronized(nil) does nothing + if (DebugNilSync) { + _objc_inform("NIL SYNC DEBUG: @synchronized(nil); set a breakpoint on objc_sync_nil to debug"); + } + objc_sync_nil(); + } + + return result; +} + // End synchronizing on 'obj'. // Returns OBJC_SYNC_SUCCESS or OBJC_SYNC_NOT_OWNING_THREAD_ERROR diff --git a/runtime/objc-weak.mm b/runtime/objc-weak.mm index 3dd6d0a..4d9c43d 100644 --- a/runtime/objc-weak.mm +++ b/runtime/objc-weak.mm @@ -76,7 +76,7 @@ __attribute__((noinline, used)) static void grow_refs_and_insert(weak_entry_t *entry, objc_object **new_referrer) { - assert(entry->out_of_line()); + ASSERT(entry->out_of_line()); size_t old_size = TABLE_SIZE(entry); size_t new_size = old_size ? old_size * 2 : 8; @@ -135,7 +135,7 @@ static void append_referrer(weak_entry_t *entry, objc_object **new_referrer) entry->max_hash_displacement = 0; } - assert(entry->out_of_line()); + ASSERT(entry->out_of_line()); if (entry->num_refs >= TABLE_SIZE(entry) * 3/4) { return grow_refs_and_insert(entry, new_referrer); @@ -211,7 +211,7 @@ static void remove_referrer(weak_entry_t *entry, objc_object **old_referrer) static void weak_entry_insert(weak_table_t *weak_table, weak_entry_t *new_entry) { weak_entry_t *weak_entries = weak_table->weak_entries; - assert(weak_entries != nil); + ASSERT(weak_entries != nil); size_t begin = hash_pointer(new_entry->referent) & (weak_table->mask); size_t index = begin; @@ -308,7 +308,7 @@ static void weak_entry_remove(weak_table_t *weak_table, weak_entry_t *entry) static weak_entry_t * weak_entry_for_referent(weak_table_t *weak_table, objc_object *referent) { - assert(referent); + ASSERT(referent); weak_entry_t *weak_entries = weak_table->weak_entries; @@ -405,12 +405,12 @@ weak_register_no_lock(weak_table_t *weak_table, id referent_id, BOOL (*allowsWeakReference)(objc_object *, SEL) = (BOOL(*)(objc_object *, SEL)) object_getMethodImplementation((id)referent, - SEL_allowsWeakReference); + @selector(allowsWeakReference)); if ((IMP)allowsWeakReference == _objc_msgForward) { return nil; } deallocating = - ! (*allowsWeakReference)(referent, SEL_allowsWeakReference); + ! (*allowsWeakReference)(referent, @selector(allowsWeakReference)); } if (deallocating) { diff --git a/runtime/objc-zalloc.h b/runtime/objc-zalloc.h new file mode 100644 index 0000000..ef8203c --- /dev/null +++ b/runtime/objc-zalloc.h @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2019 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +/** + * @file objc-zalloc.h + * + * "zone allocator" for objc. + * + * Provides packed allocation for data structures the runtime + * almost never frees. + */ + +#ifndef _OBJC_ZALLOC_H +#define _OBJC_ZALLOC_H + +#include +#include +#include + +namespace objc { + +// Darwin malloc always aligns to 16 bytes +#define MALLOC_ALIGNMENT 16 + +class AtomicQueue { +#if __LP64__ + using pair_t = __int128_t; +#else + using pair_t = uint64_t; +#endif + static constexpr auto relaxed = std::memory_order_relaxed; + static constexpr auto release = std::memory_order_release; + + struct Entry { + struct Entry *next; + }; + + union { + struct { + Entry *head; + unsigned long gen; + }; + std::atomic atomic_pair; + pair_t pair; + }; + +public: + void *pop(); + void push_list(void *_head, void *_tail); + inline void push(void *head) + { + push_list(head, head); + } +}; + +template +class Zone { +}; + +template +class Zone { + struct Element { + Element *next; + char buf[sizeof(T) - sizeof(void *)]; + } __attribute__((packed)); + + static AtomicQueue _freelist; + static T *alloc_slow(); + +public: + static T *alloc(); + static void free(T *); +}; + +template +class Zone { +public: + static inline T *alloc() { + return reinterpret_cast(::calloc(sizeof(T), 1)); + } + static inline void free(T *ptr) { + ::free(ptr); + } +}; + +/* + * This allocator returns always zeroed memory, + * and the template needs to be instantiated in objc-zalloc.mm + */ + +template +T *zalloc() +{ + return Zone::alloc(); +} + +template +void zfree(T *e) +{ + Zone::free(e); +} + +}; + +#endif diff --git a/runtime/objc-zalloc.mm b/runtime/objc-zalloc.mm new file mode 100644 index 0000000..cdfd043 --- /dev/null +++ b/runtime/objc-zalloc.mm @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2007 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +/** + * @file objc-zalloc.h + * + * "zone allocator" for objc. + * + * Provides packed allocation for data structures the runtime + * almost never frees. + */ + +#include "objc-private.h" +#include "objc-zalloc.h" + +namespace objc { + +void *AtomicQueue::pop() +{ + AtomicQueue l1, l2; + + l1.pair = pair; // non atomic on purpose + + do { + if (l1.head == nullptr) { + return nullptr; + } + l2.head = l1.head->next; + l2.gen = l1.gen + 1; + } while (!atomic_pair.compare_exchange_weak(l1.pair, l2.pair, relaxed, relaxed)); + + return reinterpret_cast(l1.head); +} + +void AtomicQueue::push_list(void *_head, void *_tail) +{ + Entry *head = reinterpret_cast(_head); + Entry *tail = reinterpret_cast(_tail); + AtomicQueue l1, l2; + + l1.pair = pair; // non atomic load on purpose + do { + tail->next = l1.head; + l2.head = head; + l2.gen = l1.gen + 1; + } while (!atomic_pair.compare_exchange_weak(l1.pair, l2.pair, release, relaxed)); +} + +template +constexpr inline +T gcd(T a, T b) +{ + return b == 0 ? a : gcd(b, a % b); +} + +template +AtomicQueue Zone::_freelist; + +template +T *Zone::alloc_slow() +{ + // our malloc aligns to 16 bytes and this code should be used for sizes + // small enough that this should always be an actual malloc bucket. + // + // The point of this code is *NOT* speed but optimal density + constexpr size_t n_elem = MALLOC_ALIGNMENT / gcd(sizeof(T), size_t{MALLOC_ALIGNMENT}); + Element *slab = reinterpret_cast(::calloc(n_elem, sizeof(T))); + for (size_t i = 1; i < n_elem - 1; i++) { + slab[i].next = &slab[i + 1]; + } + _freelist.push_list(reinterpret_cast(&slab[1]), + reinterpret_cast(&slab[n_elem - 1])); + return reinterpret_cast(&slab[0]); +} + +template +T *Zone::alloc() +{ + void *e = _freelist.pop(); + if (e) { + __builtin_bzero(e, sizeof(void *)); + return reinterpret_cast(e); + } + return alloc_slow(); +} + +template +void Zone::free(T *ptr) +{ + if (ptr) { + Element *e = reinterpret_cast(ptr); + __builtin_bzero(e->buf, sizeof(e->buf)); + _freelist.push(e); + } +} + +#if __OBJC2__ +#define ZoneInstantiate(type) \ + template class Zone + +ZoneInstantiate(class_rw_t); +ZoneInstantiate(class_rw_ext_t); +#endif + +} diff --git a/runtime/objc.h b/runtime/objc.h index 7417ebc..6a73568 100644 --- a/runtime/objc.h +++ b/runtime/objc.h @@ -39,7 +39,7 @@ typedef struct objc_class *Class; /// Represents an instance of a class. struct objc_object { - Class isa OBJC_ISA_AVAILABILITY; + Class _Nonnull isa OBJC_ISA_AVAILABILITY; }; /// A pointer to an instance of a class. @@ -53,22 +53,38 @@ typedef struct objc_selector *SEL; #if !OBJC_OLD_DISPATCH_PROTOTYPES typedef void (*IMP)(void /* id, SEL, ... */ ); #else -typedef id (*IMP)(id, SEL, ...); +typedef id _Nullable (*IMP)(id _Nonnull, SEL _Nonnull, ...); +#endif + +/// Type to represent a boolean value. + +#if defined(__OBJC_BOOL_IS_BOOL) + // Honor __OBJC_BOOL_IS_BOOL when available. +# if __OBJC_BOOL_IS_BOOL +# define OBJC_BOOL_IS_BOOL 1 +# else +# define OBJC_BOOL_IS_BOOL 0 +# endif +#else + // __OBJC_BOOL_IS_BOOL not set. +# if TARGET_OS_OSX || TARGET_OS_IOSMAC || ((TARGET_OS_IOS || TARGET_OS_BRIDGE) && !__LP64__ && !__ARM_ARCH_7K) +# define OBJC_BOOL_IS_BOOL 0 +# else +# define OBJC_BOOL_IS_BOOL 1 +# endif +#endif + +#if OBJC_BOOL_IS_BOOL + typedef bool BOOL; +#else +# define OBJC_BOOL_IS_CHAR 1 + typedef signed char BOOL; + // BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" + // even if -funsigned-char is used. #endif #define OBJC_BOOL_DEFINED -/// Type to represent a boolean value. -#if (TARGET_OS_IPHONE && __LP64__) || TARGET_OS_WATCH -#define OBJC_BOOL_IS_BOOL 1 -typedef bool BOOL; -#else -#define OBJC_BOOL_IS_CHAR 1 -typedef signed char BOOL; -// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" -// even if -funsigned-char is used. -#endif - #if __has_feature(objc_bool) #define YES __objc_yes #define NO __objc_no @@ -119,8 +135,8 @@ typedef signed char BOOL; * * @return A C string indicating the name of the selector. */ -OBJC_EXPORT const char *sel_getName(SEL sel) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nonnull sel_getName(SEL _Nonnull sel) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Registers a method with the Objective-C runtime system, maps the method @@ -134,8 +150,8 @@ OBJC_EXPORT const char *sel_getName(SEL sel) * method’s selector before you can add the method to a class definition. If the method name * has already been registered, this function simply returns the selector. */ -OBJC_EXPORT SEL sel_registerName(const char *str) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT SEL _Nonnull sel_registerName(const char * _Nonnull str) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Returns the class name of a given object. @@ -144,8 +160,8 @@ OBJC_EXPORT SEL sel_registerName(const char *str) * * @return The name of the class of which \e obj is an instance. */ -OBJC_EXPORT const char *object_getClassName(id obj) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nonnull object_getClassName(id _Nullable obj) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Returns a pointer to any extra bytes allocated with an instance given object. @@ -163,8 +179,9 @@ OBJC_EXPORT const char *object_getClassName(id obj) * guaranteed, even if the area following the object's last ivar is more aligned than that. * @note In a garbage-collected environment, the memory is scanned conservatively. */ -OBJC_EXPORT void *object_getIndexedIvars(id obj) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT void * _Nullable object_getIndexedIvars(id _Nullable obj) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0) + OBJC_ARC_UNAVAILABLE; /** * Identifies a selector as being valid or invalid. @@ -176,8 +193,8 @@ OBJC_EXPORT void *object_getIndexedIvars(id obj) * @warning On some platforms, an invalid reference (to invalid memory addresses) can cause * a crash. */ -OBJC_EXPORT BOOL sel_isMapped(SEL sel) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT BOOL sel_isMapped(SEL _Nonnull sel) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Registers a method name with the Objective-C runtime system. @@ -191,20 +208,29 @@ OBJC_EXPORT BOOL sel_isMapped(SEL sel) * and returned \c NULL if the selector was not found. This was changed for safety, because it was * observed that many of the callers of this function did not check the return value for \c NULL. */ -OBJC_EXPORT SEL sel_getUid(const char *str) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT SEL _Nonnull sel_getUid(const char * _Nonnull str) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); typedef const void* objc_objectptr_t; // Obsolete ARC conversions. -OBJC_EXPORT id objc_retainedObject(objc_objectptr_t obj) - OBJC_UNAVAILABLE("use CFBridgingRelease() or a (__bridge_transfer id) cast instead"); -OBJC_EXPORT id objc_unretainedObject(objc_objectptr_t obj) - OBJC_UNAVAILABLE("use a (__bridge id) cast instead"); -OBJC_EXPORT objc_objectptr_t objc_unretainedPointer(id obj) - OBJC_UNAVAILABLE("use a __bridge cast instead"); +OBJC_EXPORT id _Nullable objc_retainedObject(objc_objectptr_t _Nullable obj) +#if !OBJC_DECLARE_SYMBOLS + OBJC_UNAVAILABLE("use CFBridgingRelease() or a (__bridge_transfer id) cast instead") +#endif + ; +OBJC_EXPORT id _Nullable objc_unretainedObject(objc_objectptr_t _Nullable obj) +#if !OBJC_DECLARE_SYMBOLS + OBJC_UNAVAILABLE("use a (__bridge id) cast instead") +#endif + ; +OBJC_EXPORT objc_objectptr_t _Nullable objc_unretainedPointer(id _Nullable obj) +#if !OBJC_DECLARE_SYMBOLS + OBJC_UNAVAILABLE("use a __bridge cast instead") +#endif + ; #if !__OBJC2__ diff --git a/runtime/runtime.h b/runtime/runtime.h index 38e74f1..c97129b 100644 --- a/runtime/runtime.h +++ b/runtime/runtime.h @@ -53,18 +53,18 @@ typedef struct objc_category *Category; typedef struct objc_property *objc_property_t; struct objc_class { - Class isa OBJC_ISA_AVAILABILITY; + Class _Nonnull isa OBJC_ISA_AVAILABILITY; #if !__OBJC2__ - Class super_class OBJC2_UNAVAILABLE; - const char *name OBJC2_UNAVAILABLE; + Class _Nullable super_class OBJC2_UNAVAILABLE; + const char * _Nonnull name OBJC2_UNAVAILABLE; long version OBJC2_UNAVAILABLE; long info OBJC2_UNAVAILABLE; long instance_size OBJC2_UNAVAILABLE; - struct objc_ivar_list *ivars OBJC2_UNAVAILABLE; - struct objc_method_list **methodLists OBJC2_UNAVAILABLE; - struct objc_cache *cache OBJC2_UNAVAILABLE; - struct objc_protocol_list *protocols OBJC2_UNAVAILABLE; + struct objc_ivar_list * _Nullable ivars OBJC2_UNAVAILABLE; + struct objc_method_list * _Nullable * _Nullable methodLists OBJC2_UNAVAILABLE; + struct objc_cache * _Nonnull cache OBJC2_UNAVAILABLE; + struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE; #endif } OBJC2_UNAVAILABLE; @@ -80,14 +80,14 @@ typedef struct objc_object Protocol; /// Defines a method struct objc_method_description { - SEL name; /**< The name of the method */ - char *types; /**< The types of the method arguments */ + SEL _Nullable name; /**< The name of the method */ + char * _Nullable types; /**< The types of the method arguments */ }; /// Defines a property attribute typedef struct { - const char *name; /**< The name of the attribute */ - const char *value; /**< The value of the attribute (usually empty) */ + const char * _Nonnull name; /**< The name of the attribute */ + const char * _Nonnull value; /**< The value of the attribute (usually empty) */ } objc_property_attribute_t; @@ -103,8 +103,8 @@ typedef struct { * * @return A copy of \e obj. */ -OBJC_EXPORT id object_copy(id obj, size_t size) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) +OBJC_EXPORT id _Nullable object_copy(id _Nullable obj, size_t size) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0) OBJC_ARC_UNAVAILABLE; /** @@ -114,8 +114,9 @@ OBJC_EXPORT id object_copy(id obj, size_t size) * * @return nil */ -OBJC_EXPORT id object_dispose(id obj) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) +OBJC_EXPORT id _Nullable +object_dispose(id _Nullable obj) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0) OBJC_ARC_UNAVAILABLE; /** @@ -126,8 +127,9 @@ OBJC_EXPORT id object_dispose(id obj) * @return The class object of which \e object is an instance, * or \c Nil if \e object is \c nil. */ -OBJC_EXPORT Class object_getClass(id obj) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT Class _Nullable +object_getClass(id _Nullable obj) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Sets the class of an object. @@ -137,8 +139,9 @@ OBJC_EXPORT Class object_getClass(id obj) * * @return The previous value of \e object's class, or \c Nil if \e object is \c nil. */ -OBJC_EXPORT Class object_setClass(id obj, Class cls) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT Class _Nullable +object_setClass(id _Nullable obj, Class _Nonnull cls) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** @@ -148,40 +151,11 @@ OBJC_EXPORT Class object_setClass(id obj, Class cls) * * @return true if the object is a class or metaclass, false otherwise. */ -OBJC_EXPORT BOOL object_isClass(id obj) - OBJC_AVAILABLE(10.10, 8.0, 9.0, 1.0); +OBJC_EXPORT BOOL +object_isClass(id _Nullable obj) + OBJC_AVAILABLE(10.10, 8.0, 9.0, 1.0, 2.0); -/** - * Returns the class name of a given object. - * - * @param obj An Objective-C object. - * - * @return The name of the class of which \e obj is an instance. - */ -OBJC_EXPORT const char *object_getClassName(id obj) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); - -/** - * Returns a pointer to any extra bytes allocated with an instance given object. - * - * @param obj An Objective-C object. - * - * @return A pointer to any extra bytes allocated with \e obj. If \e obj was - * not allocated with any extra bytes, then dereferencing the returned pointer is undefined. - * - * @note This function returns a pointer to any extra bytes allocated with the instance - * (as specified by \c class_createInstance with extraBytes>0). This memory follows the - * object's ordinary ivars, but may not be adjacent to the last ivar. - * @note The returned pointer is guaranteed to be pointer-size aligned, even if the area following - * the object's last ivar is less aligned than that. Alignment greater than pointer-size is never - * guaranteed, even if the area following the object's last ivar is more aligned than that. - * @note In a garbage-collected environment, the memory is scanned conservatively. - */ -OBJC_EXPORT void *object_getIndexedIvars(id obj) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) - OBJC_ARC_UNAVAILABLE; - /** * Reads the value of an instance variable in an object. * @@ -193,8 +167,9 @@ OBJC_EXPORT void *object_getIndexedIvars(id obj) * @note \c object_getIvar is faster than \c object_getInstanceVariable if the Ivar * for the instance variable is already known. */ -OBJC_EXPORT id object_getIvar(id obj, Ivar ivar) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +object_getIvar(id _Nullable obj, Ivar _Nonnull ivar) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Sets the value of an instance variable in an object. @@ -209,8 +184,9 @@ OBJC_EXPORT id object_getIvar(id obj, Ivar ivar) * @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar * for the instance variable is already known. */ -OBJC_EXPORT void object_setIvar(id obj, Ivar ivar, id value) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +object_setIvar(id _Nullable obj, Ivar _Nonnull ivar, id _Nullable value) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Sets the value of an instance variable in an object. @@ -225,8 +201,10 @@ OBJC_EXPORT void object_setIvar(id obj, Ivar ivar, id value) * @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar * for the instance variable is already known. */ -OBJC_EXPORT void object_setIvarWithStrongDefault(id obj, Ivar ivar, id value) - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); +OBJC_EXPORT void +object_setIvarWithStrongDefault(id _Nullable obj, Ivar _Nonnull ivar, + id _Nullable value) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); /** * Changes the value of an instance variable of a class instance. @@ -243,8 +221,10 @@ OBJC_EXPORT void object_setIvarWithStrongDefault(id obj, Ivar ivar, id value) * use that memory management. Instance variables with unknown memory management * are assigned as if they were unsafe_unretained. */ -OBJC_EXPORT Ivar object_setInstanceVariable(id obj, const char *name, void *value) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) +OBJC_EXPORT Ivar _Nullable +object_setInstanceVariable(id _Nullable obj, const char * _Nonnull name, + void * _Nullable value) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0) OBJC_ARC_UNAVAILABLE; /** @@ -262,8 +242,11 @@ OBJC_EXPORT Ivar object_setInstanceVariable(id obj, const char *name, void *valu * use that memory management. Instance variables with unknown memory management * are assigned as if they were strong. */ -OBJC_EXPORT Ivar object_setInstanceVariableWithStrongDefault(id obj, const char *name, void *value) - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0) +OBJC_EXPORT Ivar _Nullable +object_setInstanceVariableWithStrongDefault(id _Nullable obj, + const char * _Nonnull name, + void * _Nullable value) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0) OBJC_ARC_UNAVAILABLE; /** @@ -277,8 +260,10 @@ OBJC_EXPORT Ivar object_setInstanceVariableWithStrongDefault(id obj, const char * @return A pointer to the \c Ivar data structure that defines the type and name of * the instance variable specified by \e name. */ -OBJC_EXPORT Ivar object_getInstanceVariable(id obj, const char *name, void **outValue) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) +OBJC_EXPORT Ivar _Nullable +object_getInstanceVariable(id _Nullable obj, const char * _Nonnull name, + void * _Nullable * _Nullable outValue) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0) OBJC_ARC_UNAVAILABLE; @@ -300,8 +285,9 @@ OBJC_EXPORT Ivar object_getInstanceVariable(id obj, const char *name, void **out * @warning Earlier implementations of this function (prior to OS X v10.0) * terminate the program if the class does not exist. */ -OBJC_EXPORT Class objc_getClass(const char *name) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT Class _Nullable +objc_getClass(const char * _Nonnull name) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Returns the metaclass definition of a specified class. @@ -316,8 +302,9 @@ OBJC_EXPORT Class objc_getClass(const char *name) * definition must have a valid metaclass definition, and so the metaclass definition is always returned, * whether it’s valid or not. */ -OBJC_EXPORT Class objc_getMetaClass(const char *name) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT Class _Nullable +objc_getMetaClass(const char * _Nonnull name) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Returns the class definition of a specified class. @@ -331,8 +318,9 @@ OBJC_EXPORT Class objc_getMetaClass(const char *name) * registered, \c objc_getClass calls the class handler callback and then checks a second * time to see whether the class is registered. This function does not call the class handler callback. */ -OBJC_EXPORT Class objc_lookUpClass(const char *name) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT Class _Nullable +objc_lookUpClass(const char * _Nonnull name) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Returns the class definition of a specified class. @@ -344,8 +332,9 @@ OBJC_EXPORT Class objc_lookUpClass(const char *name) * @note This function is the same as \c objc_getClass, but kills the process if the class is not found. * @note This function is used by ZeroLink, where failing to find a class would be a compile-time link error without ZeroLink. */ -OBJC_EXPORT Class objc_getRequiredClass(const char *name) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT Class _Nonnull +objc_getRequiredClass(const char * _Nonnull name) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Obtains the list of registered class definitions. @@ -366,8 +355,9 @@ OBJC_EXPORT Class objc_getRequiredClass(const char *name) * @warning You cannot assume that class objects you get from this function are classes that inherit from \c NSObject, * so you cannot safely call any methods on such classes without detecting that the method is implemented first. */ -OBJC_EXPORT int objc_getClassList(Class *buffer, int bufferCount) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT int +objc_getClassList(Class _Nonnull * _Nullable buffer, int bufferCount) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Creates and returns a list of pointers to all registered class definitions. @@ -379,8 +369,9 @@ OBJC_EXPORT int objc_getClassList(Class *buffer, int bufferCount) * * @see objc_getClassList */ -OBJC_EXPORT Class *objc_copyClassList(unsigned int *outCount) - OBJC_AVAILABLE(10.7, 3.1, 9.0, 1.0); +OBJC_EXPORT Class _Nonnull * _Nullable +objc_copyClassList(unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.7, 3.1, 9.0, 1.0, 2.0); /* Working with Classes */ @@ -392,8 +383,9 @@ OBJC_EXPORT Class *objc_copyClassList(unsigned int *outCount) * * @return The name of the class, or the empty string if \e cls is \c Nil. */ -OBJC_EXPORT const char *class_getName(Class cls) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nonnull +class_getName(Class _Nullable cls) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns a Boolean value that indicates whether a class object is a metaclass. @@ -403,8 +395,9 @@ OBJC_EXPORT const char *class_getName(Class cls) * @return \c YES if \e cls is a metaclass, \c NO if \e cls is a non-meta class, * \c NO if \e cls is \c Nil. */ -OBJC_EXPORT BOOL class_isMetaClass(Class cls) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT BOOL +class_isMetaClass(Class _Nullable cls) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns the superclass of a class. @@ -416,8 +409,9 @@ OBJC_EXPORT BOOL class_isMetaClass(Class cls) * * @note You should usually use \c NSObject's \c superclass method instead of this function. */ -OBJC_EXPORT Class class_getSuperclass(Class cls) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT Class _Nullable +class_getSuperclass(Class _Nullable cls) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Sets the superclass of a given class. @@ -429,11 +423,16 @@ OBJC_EXPORT Class class_getSuperclass(Class cls) * * @warning You should not use this function. */ -OBJC_EXPORT Class class_setSuperclass(Class cls, Class newSuper) - __OSX_DEPRECATED(10.5, 10.5, "not recommended") - __IOS_DEPRECATED(2.0, 2.0, "not recommended") - __TVOS_DEPRECATED(9.0, 9.0, "not recommended") - __WATCHOS_DEPRECATED(1.0, 1.0, "not recommended"); +OBJC_EXPORT Class _Nonnull +class_setSuperclass(Class _Nonnull cls, Class _Nonnull newSuper) + __OSX_DEPRECATED(10.5, 10.5, "not recommended") + __IOS_DEPRECATED(2.0, 2.0, "not recommended") + __TVOS_DEPRECATED(9.0, 9.0, "not recommended") + __WATCHOS_DEPRECATED(1.0, 1.0, "not recommended") +#ifndef __APPLE_BLEACH_SDK__ + __BRIDGEOS_DEPRECATED(2.0, 2.0, "not recommended") +#endif +; /** * Returns the version number of a class definition. @@ -445,8 +444,9 @@ OBJC_EXPORT Class class_setSuperclass(Class cls, Class newSuper) * * @see class_setVersion */ -OBJC_EXPORT int class_getVersion(Class cls) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT int +class_getVersion(Class _Nullable cls) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Sets the version number of a class definition. @@ -462,8 +462,9 @@ OBJC_EXPORT int class_getVersion(Class cls) * @note Classes derived from the Foundation framework \c NSObject class can set the class-definition * version number using the \c setVersion: class method, which is implemented using the \c class_setVersion function. */ -OBJC_EXPORT void class_setVersion(Class cls, int version) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT void +class_setVersion(Class _Nullable cls, int version) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Returns the size of instances of a class. @@ -472,8 +473,9 @@ OBJC_EXPORT void class_setVersion(Class cls, int version) * * @return The size in bytes of instances of the class \e cls, or \c 0 if \e cls is \c Nil. */ -OBJC_EXPORT size_t class_getInstanceSize(Class cls) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT size_t +class_getInstanceSize(Class _Nullable cls) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns the \c Ivar for a specified instance variable of a given class. @@ -484,8 +486,9 @@ OBJC_EXPORT size_t class_getInstanceSize(Class cls) * @return A pointer to an \c Ivar data structure containing information about * the instance variable specified by \e name. */ -OBJC_EXPORT Ivar class_getInstanceVariable(Class cls, const char *name) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT Ivar _Nullable +class_getInstanceVariable(Class _Nullable cls, const char * _Nonnull name) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Returns the Ivar for a specified class variable of a given class. @@ -495,8 +498,9 @@ OBJC_EXPORT Ivar class_getInstanceVariable(Class cls, const char *name) * * @return A pointer to an \c Ivar data structure containing information about the class variable specified by \e name. */ -OBJC_EXPORT Ivar class_getClassVariable(Class cls, const char *name) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT Ivar _Nullable +class_getClassVariable(Class _Nullable cls, const char * _Nonnull name) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Describes the instance variables declared by a class. @@ -511,8 +515,9 @@ OBJC_EXPORT Ivar class_getClassVariable(Class cls, const char *name) * * If the class declares no instance variables, or cls is Nil, NULL is returned and *outCount is 0. */ -OBJC_EXPORT Ivar *class_copyIvarList(Class cls, unsigned int *outCount) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT Ivar _Nonnull * _Nullable +class_copyIvarList(Class _Nullable cls, unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns a specified instance method for a given class. @@ -526,8 +531,9 @@ OBJC_EXPORT Ivar *class_copyIvarList(Class cls, unsigned int *outCount) * * @note This function searches superclasses for implementations, whereas \c class_copyMethodList does not. */ -OBJC_EXPORT Method class_getInstanceMethod(Class cls, SEL name) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT Method _Nullable +class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Returns a pointer to the data structure describing a given class method for a given class. @@ -542,8 +548,9 @@ OBJC_EXPORT Method class_getInstanceMethod(Class cls, SEL name) * @note Note that this function searches superclasses for implementations, * whereas \c class_copyMethodList does not. */ -OBJC_EXPORT Method class_getClassMethod(Class cls, SEL name) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT Method _Nullable +class_getClassMethod(Class _Nullable cls, SEL _Nonnull name) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Returns the function pointer that would be called if a @@ -560,8 +567,9 @@ OBJC_EXPORT Method class_getClassMethod(Class cls, SEL name) * an actual method implementation. For example, if instances of the class do not respond to * the selector, the function pointer returned will be part of the runtime's message forwarding machinery. */ -OBJC_EXPORT IMP class_getMethodImplementation(Class cls, SEL name) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT IMP _Nullable +class_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns the function pointer that would be called if a particular @@ -573,8 +581,9 @@ OBJC_EXPORT IMP class_getMethodImplementation(Class cls, SEL name) * @return The function pointer that would be called if \c [object name] were called * with an instance of the class, or \c NULL if \e cls is \c Nil. */ -OBJC_EXPORT IMP class_getMethodImplementation_stret(Class cls, SEL name) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0) +OBJC_EXPORT IMP _Nullable +class_getMethodImplementation_stret(Class _Nullable cls, SEL _Nonnull name) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0) OBJC_ARM64_UNAVAILABLE; /** @@ -588,8 +597,9 @@ OBJC_EXPORT IMP class_getMethodImplementation_stret(Class cls, SEL name) * @note You should usually use \c NSObject's \c respondsToSelector: or \c instancesRespondToSelector: * methods instead of this function. */ -OBJC_EXPORT BOOL class_respondsToSelector(Class cls, SEL sel) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT BOOL +class_respondsToSelector(Class _Nullable cls, SEL _Nonnull sel) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Describes the instance methods implemented by a class. @@ -608,8 +618,9 @@ OBJC_EXPORT BOOL class_respondsToSelector(Class cls, SEL sel) * @note To get the implementations of methods that may be implemented by superclasses, * use \c class_getInstanceMethod or \c class_getClassMethod. */ -OBJC_EXPORT Method *class_copyMethodList(Class cls, unsigned int *outCount) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT Method _Nonnull * _Nullable +class_copyMethodList(Class _Nullable cls, unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns a Boolean value that indicates whether a class conforms to a given protocol. @@ -621,8 +632,9 @@ OBJC_EXPORT Method *class_copyMethodList(Class cls, unsigned int *outCount) * * @note You should usually use NSObject's conformsToProtocol: method instead of this function. */ -OBJC_EXPORT BOOL class_conformsToProtocol(Class cls, Protocol *protocol) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT BOOL +class_conformsToProtocol(Class _Nullable cls, Protocol * _Nullable protocol) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Describes the protocols adopted by a class. @@ -637,8 +649,9 @@ OBJC_EXPORT BOOL class_conformsToProtocol(Class cls, Protocol *protocol) * * If cls adopts no protocols, or cls is Nil, returns NULL and *outCount is 0. */ -OBJC_EXPORT Protocol * __unsafe_unretained *class_copyProtocolList(Class cls, unsigned int *outCount) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable +class_copyProtocolList(Class _Nullable cls, unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns a property with a given name of a given class. @@ -650,8 +663,9 @@ OBJC_EXPORT Protocol * __unsafe_unretained *class_copyProtocolList(Class cls, un * \c NULL if the class does not declare a property with that name, * or \c NULL if \e cls is \c Nil. */ -OBJC_EXPORT objc_property_t class_getProperty(Class cls, const char *name) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT objc_property_t _Nullable +class_getProperty(Class _Nullable cls, const char * _Nonnull name) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Describes the properties declared by a class. @@ -666,8 +680,9 @@ OBJC_EXPORT objc_property_t class_getProperty(Class cls, const char *name) * * If \e cls declares no properties, or \e cls is \c Nil, returns \c NULL and \c *outCount is \c 0. */ -OBJC_EXPORT objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT objc_property_t _Nonnull * _Nullable +class_copyPropertyList(Class _Nullable cls, unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns a description of the \c Ivar layout for a given class. @@ -676,8 +691,9 @@ OBJC_EXPORT objc_property_t *class_copyPropertyList(Class cls, unsigned int *out * * @return A description of the \c Ivar layout for \e cls. */ -OBJC_EXPORT const uint8_t *class_getIvarLayout(Class cls) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const uint8_t * _Nullable +class_getIvarLayout(Class _Nullable cls) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns a description of the layout of weak Ivars for a given class. @@ -686,8 +702,9 @@ OBJC_EXPORT const uint8_t *class_getIvarLayout(Class cls) * * @return A description of the layout of the weak \c Ivars for \e cls. */ -OBJC_EXPORT const uint8_t *class_getWeakIvarLayout(Class cls) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const uint8_t * _Nullable +class_getWeakIvarLayout(Class _Nullable cls) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Adds a new method to a class with a given name and implementation. @@ -704,9 +721,10 @@ OBJC_EXPORT const uint8_t *class_getWeakIvarLayout(Class cls) * but will not replace an existing implementation in this class. * To change an existing implementation, use method_setImplementation. */ -OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp, - const char *types) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT BOOL +class_addMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, + const char * _Nullable types) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Replaces the implementation of a method for a given class. @@ -726,9 +744,10 @@ OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp, * - If the method identified by \e name does exist, its \c IMP is replaced as if \c method_setImplementation were called. * The type encoding specified by \e types is ignored. */ -OBJC_EXPORT IMP class_replaceMethod(Class cls, SEL name, IMP imp, - const char *types) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT IMP _Nullable +class_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, + const char * _Nullable types) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Adds a new instance variable to a class. @@ -743,9 +762,10 @@ OBJC_EXPORT IMP class_replaceMethod(Class cls, SEL name, IMP imp, * variable depends on the ivar's type and the machine architecture. * For variables of any pointer type, pass log2(sizeof(pointer_type)). */ -OBJC_EXPORT BOOL class_addIvar(Class cls, const char *name, size_t size, - uint8_t alignment, const char *types) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT BOOL +class_addIvar(Class _Nullable cls, const char * _Nonnull name, size_t size, + uint8_t alignment, const char * _Nullable types) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Adds a protocol to a class. @@ -756,8 +776,9 @@ OBJC_EXPORT BOOL class_addIvar(Class cls, const char *name, size_t size, * @return \c YES if the method was added successfully, otherwise \c NO * (for example, the class already conforms to that protocol). */ -OBJC_EXPORT BOOL class_addProtocol(Class cls, Protocol *protocol) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT BOOL +class_addProtocol(Class _Nullable cls, Protocol * _Nonnull protocol) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Adds a property to a class. @@ -770,8 +791,11 @@ OBJC_EXPORT BOOL class_addProtocol(Class cls, Protocol *protocol) * @return \c YES if the property was added successfully, otherwise \c NO * (for example, the class already has that property). */ -OBJC_EXPORT BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT BOOL +class_addProperty(Class _Nullable cls, const char * _Nonnull name, + const objc_property_attribute_t * _Nullable attributes, + unsigned int attributeCount) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); /** * Replace a property of a class. @@ -781,8 +805,11 @@ OBJC_EXPORT BOOL class_addProperty(Class cls, const char *name, const objc_prope * @param attributes An array of property attributes. * @param attributeCount The number of attributes in \e attributes. */ -OBJC_EXPORT void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT void +class_replaceProperty(Class _Nullable cls, const char * _Nonnull name, + const objc_property_attribute_t * _Nullable attributes, + unsigned int attributeCount) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); /** * Sets the Ivar layout for a given class. @@ -790,8 +817,9 @@ OBJC_EXPORT void class_replaceProperty(Class cls, const char *name, const objc_p * @param cls The class to modify. * @param layout The layout of the \c Ivars for \e cls. */ -OBJC_EXPORT void class_setIvarLayout(Class cls, const uint8_t *layout) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +class_setIvarLayout(Class _Nullable cls, const uint8_t * _Nullable layout) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Sets the layout for weak Ivars for a given class. @@ -799,8 +827,9 @@ OBJC_EXPORT void class_setIvarLayout(Class cls, const uint8_t *layout) * @param cls The class to modify. * @param layout The layout of the weak Ivars for \e cls. */ -OBJC_EXPORT void class_setWeakIvarLayout(Class cls, const uint8_t *layout) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +class_setWeakIvarLayout(Class _Nullable cls, const uint8_t * _Nullable layout) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Used by CoreFoundation's toll-free bridging. @@ -812,8 +841,9 @@ OBJC_EXPORT void class_setWeakIvarLayout(Class cls, const uint8_t *layout) * * @warning Do not call this function yourself. */ -OBJC_EXPORT Class objc_getFutureClass(const char *name) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0) +OBJC_EXPORT Class _Nonnull +objc_getFutureClass(const char * _Nonnull name) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0) OBJC_ARC_UNAVAILABLE; @@ -830,9 +860,10 @@ OBJC_EXPORT Class objc_getFutureClass(const char *name) * * @return An instance of the class \e cls. */ -OBJC_EXPORT id class_createInstance(Class cls, size_t extraBytes) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0) - OBJC_ARC_UNAVAILABLE; +OBJC_EXPORT id _Nullable +class_createInstance(Class _Nullable cls, size_t extraBytes) + OBJC_RETURNS_RETAINED + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Creates an instance of a class at the specific location provided. @@ -847,8 +878,9 @@ OBJC_EXPORT id class_createInstance(Class cls, size_t extraBytes) * * @see class_createInstance */ -OBJC_EXPORT id objc_constructInstance(Class cls, void *bytes) - OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0) +OBJC_EXPORT id _Nullable +objc_constructInstance(Class _Nullable cls, void * _Nullable bytes) + OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0) OBJC_ARC_UNAVAILABLE; /** @@ -861,8 +893,8 @@ OBJC_EXPORT id objc_constructInstance(Class cls, void *bytes) * * @note CF and other clients do call this under GC. */ -OBJC_EXPORT void *objc_destructInstance(id obj) - OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0) +OBJC_EXPORT void * _Nullable objc_destructInstance(id _Nullable obj) + OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0) OBJC_ARC_UNAVAILABLE; @@ -885,25 +917,29 @@ OBJC_EXPORT void *objc_destructInstance(id obj) * @note Instance methods and instance variables should be added to the class itself. * Class methods should be added to the metaclass. */ -OBJC_EXPORT Class objc_allocateClassPair(Class superclass, const char *name, - size_t extraBytes) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT Class _Nullable +objc_allocateClassPair(Class _Nullable superclass, const char * _Nonnull name, + size_t extraBytes) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Registers a class that was allocated using \c objc_allocateClassPair. * * @param cls The class you want to register. */ -OBJC_EXPORT void objc_registerClassPair(Class cls) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +objc_registerClassPair(Class _Nonnull cls) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Used by Foundation's Key-Value Observing. * * @warning Do not call this function yourself. */ -OBJC_EXPORT Class objc_duplicateClass(Class original, const char *name, size_t extraBytes) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT Class _Nonnull +objc_duplicateClass(Class _Nonnull original, const char * _Nonnull name, + size_t extraBytes) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Destroy a class and its associated metaclass. @@ -913,8 +949,9 @@ OBJC_EXPORT Class objc_duplicateClass(Class original, const char *name, size_t e * * @warning Do not call if instances of this class or a subclass exist. */ -OBJC_EXPORT void objc_disposeClassPair(Class cls) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +objc_disposeClassPair(Class _Nonnull cls) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /* Working with Methods */ @@ -928,8 +965,9 @@ OBJC_EXPORT void objc_disposeClassPair(Class cls) * * @note To get the method name as a C string, call \c sel_getName(method_getName(method)). */ -OBJC_EXPORT SEL method_getName(Method m) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT SEL _Nonnull +method_getName(Method _Nonnull m) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns the implementation of a method. @@ -938,8 +976,9 @@ OBJC_EXPORT SEL method_getName(Method m) * * @return A function pointer of type IMP. */ -OBJC_EXPORT IMP method_getImplementation(Method m) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT IMP _Nonnull +method_getImplementation(Method _Nonnull m) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns a string describing a method's parameter and return types. @@ -948,8 +987,9 @@ OBJC_EXPORT IMP method_getImplementation(Method m) * * @return A C string. The string may be \c NULL. */ -OBJC_EXPORT const char *method_getTypeEncoding(Method m) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nullable +method_getTypeEncoding(Method _Nonnull m) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns the number of arguments accepted by a method. @@ -958,8 +998,9 @@ OBJC_EXPORT const char *method_getTypeEncoding(Method m) * * @return An integer containing the number of arguments accepted by the given method. */ -OBJC_EXPORT unsigned int method_getNumberOfArguments(Method m) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT unsigned int +method_getNumberOfArguments(Method _Nonnull m) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Returns a string describing a method's return type. @@ -968,8 +1009,9 @@ OBJC_EXPORT unsigned int method_getNumberOfArguments(Method m) * * @return A C string describing the return type. You must free the string with \c free(). */ -OBJC_EXPORT char *method_copyReturnType(Method m) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT char * _Nonnull +method_copyReturnType(Method _Nonnull m) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns a string describing a single parameter type of a method. @@ -980,8 +1022,9 @@ OBJC_EXPORT char *method_copyReturnType(Method m) * @return A C string describing the type of the parameter at index \e index, or \c NULL * if method has no parameter index \e index. You must free the string with \c free(). */ -OBJC_EXPORT char *method_copyArgumentType(Method m, unsigned int index) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT char * _Nullable +method_copyArgumentType(Method _Nonnull m, unsigned int index) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns by reference a string describing a method's return type. @@ -993,8 +1036,9 @@ OBJC_EXPORT char *method_copyArgumentType(Method m, unsigned int index) * @note The method's return type string is copied to \e dst. * \e dst is filled as if \c strncpy(dst, parameter_type, dst_len) were called. */ -OBJC_EXPORT void method_getReturnType(Method m, char *dst, size_t dst_len) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +method_getReturnType(Method _Nonnull m, char * _Nonnull dst, size_t dst_len) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns by reference a string describing a single parameter type of a method. @@ -1008,11 +1052,14 @@ OBJC_EXPORT void method_getReturnType(Method m, char *dst, size_t dst_len) * were called. If the method contains no parameter with that index, \e dst is filled as * if \c strncpy(dst, "", dst_len) were called. */ -OBJC_EXPORT void method_getArgumentType(Method m, unsigned int index, - char *dst, size_t dst_len) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); -OBJC_EXPORT struct objc_method_description *method_getDescription(Method m) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +method_getArgumentType(Method _Nonnull m, unsigned int index, + char * _Nullable dst, size_t dst_len) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); + +OBJC_EXPORT struct objc_method_description * _Nonnull +method_getDescription(Method _Nonnull m) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Sets the implementation of a method. @@ -1022,8 +1069,9 @@ OBJC_EXPORT struct objc_method_description *method_getDescription(Method m) * * @return The previous implementation of the method. */ -OBJC_EXPORT IMP method_setImplementation(Method m, IMP imp) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT IMP _Nonnull +method_setImplementation(Method _Nonnull m, IMP _Nonnull imp) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Exchanges the implementations of two methods. @@ -1039,8 +1087,9 @@ OBJC_EXPORT IMP method_setImplementation(Method m, IMP imp) * method_setImplementation(m2, imp1); * \endcode */ -OBJC_EXPORT void method_exchangeImplementations(Method m1, Method m2) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /* Working with Instance Variables */ @@ -1052,8 +1101,9 @@ OBJC_EXPORT void method_exchangeImplementations(Method m1, Method m2) * * @return A C string containing the instance variable's name. */ -OBJC_EXPORT const char *ivar_getName(Ivar v) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nullable +ivar_getName(Ivar _Nonnull v) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns the type string of an instance variable. @@ -1064,8 +1114,9 @@ OBJC_EXPORT const char *ivar_getName(Ivar v) * * @note For possible values, see Objective-C Runtime Programming Guide > Type Encodings. */ -OBJC_EXPORT const char *ivar_getTypeEncoding(Ivar v) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nullable +ivar_getTypeEncoding(Ivar _Nonnull v) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns the offset of an instance variable. @@ -1077,8 +1128,9 @@ OBJC_EXPORT const char *ivar_getTypeEncoding(Ivar v) * @note For instance variables of type \c id or other object types, call \c object_getIvar * and \c object_setIvar instead of using this offset to access the instance variable data directly. */ -OBJC_EXPORT ptrdiff_t ivar_getOffset(Ivar v) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT ptrdiff_t +ivar_getOffset(Ivar _Nonnull v) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /* Working with Properties */ @@ -1090,8 +1142,9 @@ OBJC_EXPORT ptrdiff_t ivar_getOffset(Ivar v) * * @return A C string containing the property's name. */ -OBJC_EXPORT const char *property_getName(objc_property_t property) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nonnull +property_getName(objc_property_t _Nonnull property) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns the attribute string of a property. @@ -1102,8 +1155,9 @@ OBJC_EXPORT const char *property_getName(objc_property_t property) * * @note The format of the attribute string is described in Declared Properties in Objective-C Runtime Programming Guide. */ -OBJC_EXPORT const char *property_getAttributes(objc_property_t property) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nullable +property_getAttributes(objc_property_t _Nonnull property) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns an array of property attributes for a property. @@ -1113,8 +1167,10 @@ OBJC_EXPORT const char *property_getAttributes(objc_property_t property) * * @return An array of property attributes; must be free'd() by the caller. */ -OBJC_EXPORT objc_property_attribute_t *property_copyAttributeList(objc_property_t property, unsigned int *outCount) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT objc_property_attribute_t * _Nullable +property_copyAttributeList(objc_property_t _Nonnull property, + unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); /** * Returns the value of a property attribute given the attribute name. @@ -1125,8 +1181,10 @@ OBJC_EXPORT objc_property_attribute_t *property_copyAttributeList(objc_property_ * @return The value string of the attribute \e attributeName if it exists in * \e property, \c nil otherwise. */ -OBJC_EXPORT char *property_copyAttributeValue(objc_property_t property, const char *attributeName) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT char * _Nullable +property_copyAttributeValue(objc_property_t _Nonnull property, + const char * _Nonnull attributeName) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); /* Working with Protocols */ @@ -1140,8 +1198,9 @@ OBJC_EXPORT char *property_copyAttributeValue(objc_property_t property, const ch * * @note This function acquires the runtime lock. */ -OBJC_EXPORT Protocol *objc_getProtocol(const char *name) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT Protocol * _Nullable +objc_getProtocol(const char * _Nonnull name) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns an array of all the protocols known to the runtime. @@ -1153,8 +1212,9 @@ OBJC_EXPORT Protocol *objc_getProtocol(const char *name) * * @note This function acquires the runtime lock. */ -OBJC_EXPORT Protocol * __unsafe_unretained *objc_copyProtocolList(unsigned int *outCount) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable +objc_copyProtocolList(unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns a Boolean value that indicates whether one protocol conforms to another protocol. @@ -1171,8 +1231,10 @@ OBJC_EXPORT Protocol * __unsafe_unretained *objc_copyProtocolList(unsigned int * * \endcode * All the protocols listed between angle brackets are considered part of the ProtocolName protocol. */ -OBJC_EXPORT BOOL protocol_conformsToProtocol(Protocol *proto, Protocol *other) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT BOOL +protocol_conformsToProtocol(Protocol * _Nullable proto, + Protocol * _Nullable other) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns a Boolean value that indicates whether two protocols are equal. @@ -1182,23 +1244,25 @@ OBJC_EXPORT BOOL protocol_conformsToProtocol(Protocol *proto, Protocol *other) * * @return \c YES if \e proto is the same as \e other, otherwise \c NO. */ -OBJC_EXPORT BOOL protocol_isEqual(Protocol *proto, Protocol *other) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT BOOL +protocol_isEqual(Protocol * _Nullable proto, Protocol * _Nullable other) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns the name of a protocol. * - * @param p A protocol. + * @param proto A protocol. * * @return The name of the protocol \e p as a C string. */ -OBJC_EXPORT const char *protocol_getName(Protocol *p) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nonnull +protocol_getName(Protocol * _Nonnull proto) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns a method description structure for a specified method of a given protocol. * - * @param p A protocol. + * @param proto A protocol. * @param aSel A selector. * @param isRequiredMethod A Boolean value that indicates whether aSel is a required method. * @param isInstanceMethod A Boolean value that indicates whether aSel is an instance method. @@ -1210,13 +1274,15 @@ OBJC_EXPORT const char *protocol_getName(Protocol *p) * * @note This function recursively searches any protocols that this protocol conforms to. */ -OBJC_EXPORT struct objc_method_description protocol_getMethodDescription(Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT struct objc_method_description +protocol_getMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull aSel, + BOOL isRequiredMethod, BOOL isInstanceMethod) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns an array of method descriptions of methods meeting a given specification for a given protocol. * - * @param p A protocol. + * @param proto A protocol. * @param isRequiredMethod A Boolean value that indicates whether returned methods should * be required methods (pass YES to specify required methods). * @param isInstanceMethod A Boolean value that indicates whether returned methods should @@ -1230,8 +1296,12 @@ OBJC_EXPORT struct objc_method_description protocol_getMethodDescription(Protoco * * @note Methods in other protocols adopted by this protocol are not included. */ -OBJC_EXPORT struct objc_method_description *protocol_copyMethodDescriptionList(Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT struct objc_method_description * _Nullable +protocol_copyMethodDescriptionList(Protocol * _Nonnull proto, + BOOL isRequiredMethod, + BOOL isInstanceMethod, + unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns the specified property of a given protocol. @@ -1244,8 +1314,11 @@ OBJC_EXPORT struct objc_method_description *protocol_copyMethodDescriptionList(P * @return The property specified by \e name, \e isRequiredProperty, and \e isInstanceProperty for \e proto, * or \c NULL if none of \e proto's properties meets the specification. */ -OBJC_EXPORT objc_property_t protocol_getProperty(Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT objc_property_t _Nullable +protocol_getProperty(Protocol * _Nonnull proto, + const char * _Nonnull name, + BOOL isRequiredProperty, BOOL isInstanceProperty) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns an array of the required instance properties declared by a protocol. @@ -1255,8 +1328,10 @@ OBJC_EXPORT objc_property_t protocol_getProperty(Protocol *proto, const char *na * protocol_copyPropertyList2(proto, outCount, YES, YES); * \endcode */ -OBJC_EXPORT objc_property_t *protocol_copyPropertyList(Protocol *proto, unsigned int *outCount) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT objc_property_t _Nonnull * _Nullable +protocol_copyPropertyList(Protocol * _Nonnull proto, + unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns an array of properties declared by a protocol. @@ -1271,8 +1346,11 @@ OBJC_EXPORT objc_property_t *protocol_copyPropertyList(Protocol *proto, unsigned * \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free(). * If the protocol declares no matching properties, \c NULL is returned and \c *outCount is \c 0. */ -OBJC_EXPORT objc_property_t *protocol_copyPropertyList2(Protocol *proto, unsigned int *outCount, BOOL isRequiredProperty, BOOL isInstanceProperty) - OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0); +OBJC_EXPORT objc_property_t _Nonnull * _Nullable +protocol_copyPropertyList2(Protocol * _Nonnull proto, + unsigned int * _Nullable outCount, + BOOL isRequiredProperty, BOOL isInstanceProperty) + OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0); /** * Returns an array of the protocols adopted by a protocol. @@ -1282,10 +1360,12 @@ OBJC_EXPORT objc_property_t *protocol_copyPropertyList2(Protocol *proto, unsigne * * @return A C array of protocols adopted by \e proto. The array contains \e *outCount pointers * followed by a \c NULL terminator. You must free the array with \c free(). - * If the protocol declares no properties, \c NULL is returned and \c *outCount is \c 0. + * If the protocol adopts no other protocols, \c NULL is returned and \c *outCount is \c 0. */ -OBJC_EXPORT Protocol * __unsafe_unretained *protocol_copyProtocolList(Protocol *proto, unsigned int *outCount) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable +protocol_copyProtocolList(Protocol * _Nonnull proto, + unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Creates a new protocol instance that cannot be used until registered with @@ -1297,8 +1377,9 @@ OBJC_EXPORT Protocol * __unsafe_unretained *protocol_copyProtocolList(Protocol * * with the same name already exists. * @note There is no dispose method for this. */ -OBJC_EXPORT Protocol *objc_allocateProtocol(const char *name) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT Protocol * _Nullable +objc_allocateProtocol(const char * _Nonnull name) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); /** * Registers a newly constructed protocol with the runtime. The protocol @@ -1306,8 +1387,9 @@ OBJC_EXPORT Protocol *objc_allocateProtocol(const char *name) * * @param proto The protocol you want to register. */ -OBJC_EXPORT void objc_registerProtocol(Protocol *proto) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT void +objc_registerProtocol(Protocol * _Nonnull proto) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); /** * Adds a method to a protocol. The protocol must be under construction. @@ -1318,8 +1400,11 @@ OBJC_EXPORT void objc_registerProtocol(Protocol *proto) * @param isRequiredMethod YES if the method is not an optional method. * @param isInstanceMethod YES if the method is an instance method. */ -OBJC_EXPORT void protocol_addMethodDescription(Protocol *proto, SEL name, const char *types, BOOL isRequiredMethod, BOOL isInstanceMethod) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT void +protocol_addMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull name, + const char * _Nullable types, + BOOL isRequiredMethod, BOOL isInstanceMethod) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); /** * Adds an incorporated protocol to another protocol. The protocol being @@ -1329,8 +1414,9 @@ OBJC_EXPORT void protocol_addMethodDescription(Protocol *proto, SEL name, const * @param proto The protocol you want to add to, it must be under construction. * @param addition The protocol you want to incorporate into \e proto, it must be registered. */ -OBJC_EXPORT void protocol_addProtocol(Protocol *proto, Protocol *addition) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT void +protocol_addProtocol(Protocol * _Nonnull proto, Protocol * _Nonnull addition) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); /** * Adds a property to a protocol. The protocol must be under construction. @@ -1344,8 +1430,12 @@ OBJC_EXPORT void protocol_addProtocol(Protocol *proto, Protocol *addition) * This is the only case allowed fo a property, as a result, setting this to NO will * not add the property to the protocol at all. */ -OBJC_EXPORT void protocol_addProperty(Protocol *proto, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT void +protocol_addProperty(Protocol * _Nonnull proto, const char * _Nonnull name, + const objc_property_attribute_t * _Nullable attributes, + unsigned int attributeCount, + BOOL isRequiredProperty, BOOL isInstanceProperty) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); /* Working with Libraries */ @@ -1358,8 +1448,9 @@ OBJC_EXPORT void protocol_addProperty(Protocol *proto, const char *name, const o * * @return An array of C strings of names. Must be free()'d by caller. */ -OBJC_EXPORT const char **objc_copyImageNames(unsigned int *outCount) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nonnull * _Nonnull +objc_copyImageNames(unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns the dynamic library name a class originated from. @@ -1368,8 +1459,9 @@ OBJC_EXPORT const char **objc_copyImageNames(unsigned int *outCount) * * @return The name of the library containing this class. */ -OBJC_EXPORT const char *class_getImageName(Class cls) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nullable +class_getImageName(Class _Nullable cls) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Returns the names of all the classes within a library. @@ -1379,9 +1471,10 @@ OBJC_EXPORT const char *class_getImageName(Class cls) * * @return An array of C strings representing the class names. */ -OBJC_EXPORT const char **objc_copyClassNamesForImage(const char *image, - unsigned int *outCount) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nonnull * _Nullable +objc_copyClassNamesForImage(const char * _Nonnull image, + unsigned int * _Nullable outCount) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /* Working with Selectors */ @@ -1393,23 +1486,10 @@ OBJC_EXPORT const char **objc_copyClassNamesForImage(const char *image, * * @return A C string indicating the name of the selector. */ -OBJC_EXPORT const char *sel_getName(SEL sel) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT const char * _Nonnull +sel_getName(SEL _Nonnull sel) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); -/** - * Registers a method name with the Objective-C runtime system. - * - * @param str A pointer to a C string. Pass the name of the method you wish to register. - * - * @return A pointer of type SEL specifying the selector for the named method. - * - * @note The implementation of this method is identical to the implementation of \c sel_registerName. - * @note Prior to OS X version 10.0, this method tried to find the selector mapped to the given name - * and returned \c NULL if the selector was not found. This was changed for safety, because it was - * observed that many of the callers of this function did not check the return value for \c NULL. - */ -OBJC_EXPORT SEL sel_getUid(const char *str) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); /** * Registers a method with the Objective-C runtime system, maps the method @@ -1423,8 +1503,9 @@ OBJC_EXPORT SEL sel_getUid(const char *str) * method’s selector before you can add the method to a class definition. If the method name * has already been registered, this function simply returns the selector. */ -OBJC_EXPORT SEL sel_registerName(const char *str) - OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0); +OBJC_EXPORT SEL _Nonnull +sel_registerName(const char * _Nonnull str) + OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0); /** * Returns a Boolean value that indicates whether two selectors are equal. @@ -1436,8 +1517,9 @@ OBJC_EXPORT SEL sel_registerName(const char *str) * * @note sel_isEqual is equivalent to ==. */ -OBJC_EXPORT BOOL sel_isEqual(SEL lhs, SEL rhs) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT BOOL +sel_isEqual(SEL _Nonnull lhs, SEL _Nonnull rhs) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /* Objective-C Language Features */ @@ -1451,16 +1533,18 @@ OBJC_EXPORT BOOL sel_isEqual(SEL lhs, SEL rhs) * @param obj The object being mutated. * */ -OBJC_EXPORT void objc_enumerationMutation(id obj) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +objc_enumerationMutation(id _Nonnull obj) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Sets the current mutation handler. * * @param handler Function pointer to the new mutation handler. */ -OBJC_EXPORT void objc_setEnumerationMutationHandler(void (*handler)(id)) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +objc_setEnumerationMutationHandler(void (*_Nullable handler)(id _Nonnull )) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Set the function to be called by objc_msgForward. @@ -1470,8 +1554,9 @@ OBJC_EXPORT void objc_setEnumerationMutationHandler(void (*handler)(id)) * * @see message.h::_objc_msgForward */ -OBJC_EXPORT void objc_setForwardHandler(void *fwd, void *fwd_stret) - OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0); +OBJC_EXPORT void +objc_setForwardHandler(void * _Nonnull fwd, void * _Nonnull fwd_stret) + OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); /** * Creates a pointer to a function that will call the block @@ -1485,8 +1570,9 @@ OBJC_EXPORT void objc_setForwardHandler(void *fwd, void *fwd_stret) * @return The IMP that calls this block. Must be disposed of with * \c imp_removeBlock. */ -OBJC_EXPORT IMP imp_implementationWithBlock(id block) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT IMP _Nonnull +imp_implementationWithBlock(id _Nonnull block) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); /** * Return the block associated with an IMP that was created using @@ -1496,8 +1582,9 @@ OBJC_EXPORT IMP imp_implementationWithBlock(id block) * * @return The block called by \e anImp. */ -OBJC_EXPORT id imp_getBlock(IMP anImp) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT id _Nullable +imp_getBlock(IMP _Nonnull anImp) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); /** * Disassociates a block from an IMP that was created using @@ -1509,8 +1596,9 @@ OBJC_EXPORT id imp_getBlock(IMP anImp) * @return YES if the block was released successfully, NO otherwise. * (For example, the block might not have been used to create an IMP previously). */ -OBJC_EXPORT BOOL imp_removeBlock(IMP anImp) - OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0); +OBJC_EXPORT BOOL +imp_removeBlock(IMP _Nonnull anImp) + OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0); /** * This loads the object referenced by a weak pointer and returns it, after @@ -1520,10 +1608,11 @@ OBJC_EXPORT BOOL imp_removeBlock(IMP anImp) * * @param location The weak pointer address * - * @return The object pointed to by \e location, or \c nil if \e location is \c nil. + * @return The object pointed to by \e location, or \c nil if \e *location is \c nil. */ -OBJC_EXPORT id objc_loadWeak(id *location) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_loadWeak(id _Nullable * _Nonnull location) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); /** * This function stores a new value into a __weak variable. It would @@ -1534,8 +1623,9 @@ OBJC_EXPORT id objc_loadWeak(id *location) * * @return The value stored into \e location, i.e. \e obj */ -OBJC_EXPORT id objc_storeWeak(id *location, id obj) - OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_storeWeak(id _Nullable * _Nonnull location, id _Nullable obj) + OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0); /* Associative References */ @@ -1567,8 +1657,10 @@ typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) { * @see objc_setAssociatedObject * @see objc_removeAssociatedObjects */ -OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) - OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0); +OBJC_EXPORT void +objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key, + id _Nullable value, objc_AssociationPolicy policy) + OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0); /** * Returns the value associated with a given object for a given key. @@ -1580,8 +1672,9 @@ OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, * * @see objc_setAssociatedObject */ -OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key) - OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0); +OBJC_EXPORT id _Nullable +objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key) + OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0); /** * Removes all associations for a given object. @@ -1597,8 +1690,167 @@ OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key) * @see objc_setAssociatedObject * @see objc_getAssociatedObject */ -OBJC_EXPORT void objc_removeAssociatedObjects(id object) - OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0); +OBJC_EXPORT void +objc_removeAssociatedObjects(id _Nonnull object) + OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0); + + +/* Hooks for Swift */ + +/** + * Function type for a hook that intercepts class_getImageName(). + * + * @param cls The class whose image name is being looked up. + * @param outImageName On return, the result of the image name lookup. + * @return YES if an image name for this class was found, NO otherwise. + * + * @see class_getImageName + * @see objc_setHook_getImageName + */ +typedef BOOL (*objc_hook_getImageName)(Class _Nonnull cls, const char * _Nullable * _Nonnull outImageName); + +/** + * Install a hook for class_getImageName(). + * + * @param newValue The hook function to install. + * @param outOldValue The address of a function pointer variable. On return, + * the old hook function is stored in the variable. + * + * @note The store to *outOldValue is thread-safe: the variable will be + * updated before class_getImageName() calls your new hook to read it, + * even if your new hook is called from another thread before this + * setter completes. + * @note The first hook in the chain is the native implementation of + * class_getImageName(). Your hook should call the previous hook for + * classes that you do not recognize. + * + * @see class_getImageName + * @see objc_hook_getImageName + */ +OBJC_EXPORT void objc_setHook_getImageName(objc_hook_getImageName _Nonnull newValue, + objc_hook_getImageName _Nullable * _Nonnull outOldValue) + OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0); + +/** + * Function type for a hook that assists objc_getClass() and related functions. + * + * @param name The class name to look up. + * @param outClass On return, the result of the class lookup. + * @return YES if a class with this name was found, NO otherwise. + * + * @see objc_getClass + * @see objc_setHook_getClass + */ +typedef BOOL (*objc_hook_getClass)(const char * _Nonnull name, Class _Nullable * _Nonnull outClass); + +/** + * Install a hook for objc_getClass() and related functions. + * + * @param newValue The hook function to install. + * @param outOldValue The address of a function pointer variable. On return, + * the old hook function is stored in the variable. + * + * @note The store to *outOldValue is thread-safe: the variable will be + * updated before objc_getClass() calls your new hook to read it, + * even if your new hook is called from another thread before this + * setter completes. + * @note Your hook should call the previous hook for class names + * that you do not recognize. + * + * @see objc_getClass + * @see objc_hook_getClass + */ +#if !(TARGET_OS_OSX && __i386__) +#define OBJC_GETCLASSHOOK_DEFINED 1 +OBJC_EXPORT void objc_setHook_getClass(objc_hook_getClass _Nonnull newValue, + objc_hook_getClass _Nullable * _Nonnull outOldValue) + OBJC_AVAILABLE(10.14.4, 12.2, 12.2, 5.2, 3.2); +#endif + +/** + * Function type for a hook that assists objc_setAssociatedObject(). + * + * @param object The source object for the association. + * @param key The key for the association. + * @param value The value to associate with the key key for object. Pass nil to clear an existing association. + * @param policy The policy for the association. For possible values, see “Associative Object Behaviors.” + * + * @see objc_setAssociatedObject + * @see objc_setHook_setAssociatedObject + */ +typedef void (*objc_hook_setAssociatedObject)(id _Nonnull object, const void * _Nonnull key, + id _Nullable value, objc_AssociationPolicy policy); + +/** + * Install a hook for objc_setAssociatedObject(). + * + * @param newValue The hook function to install. + * @param outOldValue The address of a function pointer variable. On return, + * the old hook function is stored in the variable. + * + * @note The store to *outOldValue is thread-safe: the variable will be + * updated before objc_setAssociatedObject() calls your new hook to read it, + * even if your new hook is called from another thread before this + * setter completes. + * @note Your hook should always call the previous hook. + * + * @see objc_setAssociatedObject + * @see objc_hook_setAssociatedObject + */ +#if !(TARGET_OS_OSX && __i386__) +#define OBJC_SETASSOCIATEDOBJECTHOOK_DEFINED 1 +OBJC_EXPORT void objc_setHook_setAssociatedObject(objc_hook_setAssociatedObject _Nonnull newValue, + objc_hook_setAssociatedObject _Nullable * _Nonnull outOldValue) + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 4.0); +#endif + +/** + * Function type for a function that is called when an image is loaded. + * + * @param header The newly loaded header. + */ +struct mach_header; +typedef void (*objc_func_loadImage)(const struct mach_header * _Nonnull header); + +/** + * Add a function to be called when a new image is loaded. The function is + * called after ObjC has scanned and fixed up the image. It is called + * BEFORE +load methods are invoked. + * + * When adding a new function, that function is immediately called with all + * images that are currently loaded. It is then called as needed for images + * that are loaded afterwards. + * + * Note: the function is called with ObjC's internal runtime lock held. + * Be VERY careful with what the function does to avoid deadlocks or + * poor performance. + * + * @param func The function to add. + */ +#define OBJC_ADDLOADIMAGEFUNC_DEFINED 1 +OBJC_EXPORT void objc_addLoadImageFunc(objc_func_loadImage _Nonnull func) + OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 4.0); + +/** + * Callback from Objective-C to Swift to perform Swift class initialization. + */ +#if !(TARGET_OS_OSX && __i386__) +typedef Class _Nullable +(*_objc_swiftMetadataInitializer)(Class _Nonnull cls, void * _Nullable arg); +#endif + + +/** + * Perform Objective-C initialization of a Swift class. + * Do not call this function. It is provided for the Swift runtime's use only + * and will change without notice or mercy. + */ +#if !(TARGET_OS_OSX && __i386__) +#define OBJC_REALIZECLASSFROMSWIFT_DEFINED 1 +OBJC_EXPORT Class _Nullable +_objc_realizeClassFromSwift(Class _Nullable cls, void * _Nullable previously) + OBJC_AVAILABLE(10.14.4, 12.2, 12.2, 5.2, 3.2); +#endif #define _C_ID '@' @@ -1678,30 +1930,30 @@ OBJC_EXPORT void objc_removeAssociatedObjects(id object) struct objc_method_description_list { - int count; - struct objc_method_description list[1]; + int count; + struct objc_method_description list[1]; }; struct objc_protocol_list { - struct objc_protocol_list *next; + struct objc_protocol_list * _Nullable next; long count; - __unsafe_unretained Protocol *list[1]; + __unsafe_unretained Protocol * _Nullable list[1]; }; struct objc_category { - char *category_name OBJC2_UNAVAILABLE; - char *class_name OBJC2_UNAVAILABLE; - struct objc_method_list *instance_methods OBJC2_UNAVAILABLE; - struct objc_method_list *class_methods OBJC2_UNAVAILABLE; - struct objc_protocol_list *protocols OBJC2_UNAVAILABLE; + char * _Nonnull category_name OBJC2_UNAVAILABLE; + char * _Nonnull class_name OBJC2_UNAVAILABLE; + struct objc_method_list * _Nullable instance_methods OBJC2_UNAVAILABLE; + struct objc_method_list * _Nullable class_methods OBJC2_UNAVAILABLE; + struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE; } OBJC2_UNAVAILABLE; struct objc_ivar { - char *ivar_name OBJC2_UNAVAILABLE; - char *ivar_type OBJC2_UNAVAILABLE; + char * _Nullable ivar_name OBJC2_UNAVAILABLE; + char * _Nullable ivar_type OBJC2_UNAVAILABLE; int ivar_offset OBJC2_UNAVAILABLE; #ifdef __LP64__ int space OBJC2_UNAVAILABLE; @@ -1719,13 +1971,13 @@ struct objc_ivar_list { struct objc_method { - SEL method_name OBJC2_UNAVAILABLE; - char *method_types OBJC2_UNAVAILABLE; - IMP method_imp OBJC2_UNAVAILABLE; + SEL _Nonnull method_name OBJC2_UNAVAILABLE; + char * _Nullable method_types OBJC2_UNAVAILABLE; + IMP _Nonnull method_imp OBJC2_UNAVAILABLE; } OBJC2_UNAVAILABLE; struct objc_method_list { - struct objc_method_list *obsolete OBJC2_UNAVAILABLE; + struct objc_method_list * _Nullable obsolete OBJC2_UNAVAILABLE; int method_count OBJC2_UNAVAILABLE; #ifdef __LP64__ @@ -1740,10 +1992,10 @@ typedef struct objc_symtab *Symtab OBJC2_UNAVAILABLE; struct objc_symtab { unsigned long sel_ref_cnt OBJC2_UNAVAILABLE; - SEL *refs OBJC2_UNAVAILABLE; + SEL _Nonnull * _Nullable refs OBJC2_UNAVAILABLE; unsigned short cls_def_cnt OBJC2_UNAVAILABLE; unsigned short cat_def_cnt OBJC2_UNAVAILABLE; - void *defs[1] /* variable size */ OBJC2_UNAVAILABLE; + void * _Nullable defs[1] /* variable size */ OBJC2_UNAVAILABLE; } OBJC2_UNAVAILABLE; @@ -1760,7 +2012,7 @@ typedef struct objc_cache *Cache OBJC2_UNAVAILABLE; struct objc_cache { unsigned int mask /* total = mask + 1 */ OBJC2_UNAVAILABLE; unsigned int occupied OBJC2_UNAVAILABLE; - Method buckets[1] OBJC2_UNAVAILABLE; + Method _Nullable buckets[1] OBJC2_UNAVAILABLE; }; @@ -1769,8 +2021,8 @@ typedef struct objc_module *Module OBJC2_UNAVAILABLE; struct objc_module { unsigned long version OBJC2_UNAVAILABLE; unsigned long size OBJC2_UNAVAILABLE; - const char *name OBJC2_UNAVAILABLE; - Symtab symtab OBJC2_UNAVAILABLE; + const char * _Nullable name OBJC2_UNAVAILABLE; + Symtab _Nullable symtab OBJC2_UNAVAILABLE; } OBJC2_UNAVAILABLE; #else @@ -1782,52 +2034,107 @@ struct objc_method_list; /* Obsolete functions */ -OBJC_EXPORT IMP class_lookupMethod(Class cls, SEL sel) - __OSX_DEPRECATED(10.0, 10.5, "use class_getMethodImplementation instead") - __IOS_DEPRECATED(2.0, 2.0, "use class_getMethodImplementation instead") - __TVOS_DEPRECATED(9.0, 9.0, "use class_getMethodImplementation instead") - __WATCHOS_DEPRECATED(1.0, 1.0, "use class_getMethodImplementation instead"); -OBJC_EXPORT BOOL class_respondsToMethod(Class cls, SEL sel) - __OSX_DEPRECATED(10.0, 10.5, "use class_respondsToSelector instead") - __IOS_DEPRECATED(2.0, 2.0, "use class_respondsToSelector instead") - __TVOS_DEPRECATED(9.0, 9.0, "use class_respondsToSelector instead") - __WATCHOS_DEPRECATED(1.0, 1.0, "use class_respondsToSelector instead"); -OBJC_EXPORT void _objc_flush_caches(Class cls) - __OSX_DEPRECATED(10.0, 10.5, "not recommended") - __IOS_DEPRECATED(2.0, 2.0, "not recommended") - __TVOS_DEPRECATED(9.0, 9.0, "not recommended") - __WATCHOS_DEPRECATED(1.0, 1.0, "not recommended"); +OBJC_EXPORT IMP _Nullable +class_lookupMethod(Class _Nullable cls, SEL _Nonnull sel) + __OSX_DEPRECATED(10.0, 10.5, "use class_getMethodImplementation instead") + __IOS_DEPRECATED(2.0, 2.0, "use class_getMethodImplementation instead") + __TVOS_DEPRECATED(9.0, 9.0, "use class_getMethodImplementation instead") + __WATCHOS_DEPRECATED(1.0, 1.0, "use class_getMethodImplementation instead") +#ifndef __APPLE_BLEACH_SDK__ + __BRIDGEOS_DEPRECATED(2.0, 2.0, "use class_getMethodImplementation instead") +#endif +; +OBJC_EXPORT BOOL +class_respondsToMethod(Class _Nullable cls, SEL _Nonnull sel) + __OSX_DEPRECATED(10.0, 10.5, "use class_respondsToSelector instead") + __IOS_DEPRECATED(2.0, 2.0, "use class_respondsToSelector instead") + __TVOS_DEPRECATED(9.0, 9.0, "use class_respondsToSelector instead") + __WATCHOS_DEPRECATED(1.0, 1.0, "use class_respondsToSelector instead") +#ifndef __APPLE_BLEACH_SDK__ + __BRIDGEOS_DEPRECATED(2.0, 2.0, "use class_respondsToSelector instead") +#endif +; -OBJC_EXPORT id object_copyFromZone(id anObject, size_t nBytes, void *z) - __OSX_DEPRECATED(10.0, 10.5, "use object_copy instead") - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE - OBJC_ARC_UNAVAILABLE; -OBJC_EXPORT id object_realloc(id anObject, size_t nBytes) OBJC2_UNAVAILABLE; -OBJC_EXPORT id object_reallocFromZone(id anObject, size_t nBytes, void *z) OBJC2_UNAVAILABLE; +OBJC_EXPORT void +_objc_flush_caches(Class _Nullable cls) + __OSX_DEPRECATED(10.0, 10.5, "not recommended") + __IOS_DEPRECATED(2.0, 2.0, "not recommended") + __TVOS_DEPRECATED(9.0, 9.0, "not recommended") + __WATCHOS_DEPRECATED(1.0, 1.0, "not recommended") +#ifndef __APPLE_BLEACH_SDK__ + __BRIDGEOS_DEPRECATED(2.0, 2.0, "not recommended") +#endif +; + +OBJC_EXPORT id _Nullable +object_copyFromZone(id _Nullable anObject, size_t nBytes, void * _Nullable z) + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.0, 10.5, "use object_copy instead"); + +OBJC_EXPORT id _Nullable +object_realloc(id _Nullable anObject, size_t nBytes) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT id _Nullable +object_reallocFromZone(id _Nullable anObject, size_t nBytes, void * _Nullable z) + OBJC2_UNAVAILABLE; #define OBSOLETE_OBJC_GETCLASSES 1 -OBJC_EXPORT void *objc_getClasses(void) OBJC2_UNAVAILABLE; -OBJC_EXPORT void objc_addClass(Class myClass) OBJC2_UNAVAILABLE; -OBJC_EXPORT void objc_setClassHandler(int (*)(const char *)) OBJC2_UNAVAILABLE; -OBJC_EXPORT void objc_setMultithreaded (BOOL flag) OBJC2_UNAVAILABLE; +OBJC_EXPORT void * _Nonnull +objc_getClasses(void) + OBJC2_UNAVAILABLE; -OBJC_EXPORT id class_createInstanceFromZone(Class, size_t idxIvars, void *z) - __OSX_DEPRECATED(10.0, 10.5, "use class_createInstance instead") - __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE - OBJC_ARC_UNAVAILABLE; +OBJC_EXPORT void +objc_addClass(Class _Nonnull myClass) + OBJC2_UNAVAILABLE; -OBJC_EXPORT void class_addMethods(Class, struct objc_method_list *) OBJC2_UNAVAILABLE; -OBJC_EXPORT void class_removeMethods(Class, struct objc_method_list *) OBJC2_UNAVAILABLE; -OBJC_EXPORT void _objc_resolve_categories_for_class(Class cls) OBJC2_UNAVAILABLE; +OBJC_EXPORT void +objc_setClassHandler(int (* _Nullable )(const char * _Nonnull)) + OBJC2_UNAVAILABLE; -OBJC_EXPORT Class class_poseAs(Class imposter, Class original) OBJC2_UNAVAILABLE; +OBJC_EXPORT void +objc_setMultithreaded(BOOL flag) + OBJC2_UNAVAILABLE; -OBJC_EXPORT unsigned int method_getSizeOfArguments(Method m) OBJC2_UNAVAILABLE; -OBJC_EXPORT unsigned method_getArgumentInfo(struct objc_method *m, int arg, const char **type, int *offset) OBJC2_UNAVAILABLE; +OBJC_EXPORT id _Nullable +class_createInstanceFromZone(Class _Nullable, size_t idxIvars, + void * _Nullable z) + OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.0, 10.5, "use class_createInstance instead"); + +OBJC_EXPORT void +class_addMethods(Class _Nullable, struct objc_method_list * _Nonnull) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT void +class_removeMethods(Class _Nullable, struct objc_method_list * _Nonnull) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT void +_objc_resolve_categories_for_class(Class _Nonnull cls) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT Class _Nonnull +class_poseAs(Class _Nonnull imposter, Class _Nonnull original) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT unsigned int +method_getSizeOfArguments(Method _Nonnull m) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT unsigned +method_getArgumentInfo(struct objc_method * _Nonnull m, int arg, + const char * _Nullable * _Nonnull type, + int * _Nonnull offset) + UNAVAILABLE_ATTRIBUTE // This function was accidentally deleted in 10.9. + OBJC2_UNAVAILABLE; + +OBJC_EXPORT Class _Nullable +objc_getOrigClass(const char * _Nonnull name) + OBJC2_UNAVAILABLE; -OBJC_EXPORT Class objc_getOrigClass(const char *name) OBJC2_UNAVAILABLE; #define OBJC_NEXT_METHOD_LIST 1 -OBJC_EXPORT struct objc_method_list *class_nextMethodList(Class, void **) OBJC2_UNAVAILABLE; +OBJC_EXPORT struct objc_method_list * _Nullable +class_nextMethodList(Class _Nullable, void * _Nullable * _Nullable) + OBJC2_UNAVAILABLE; // usage for nextMethodList // // void *iterator = 0; @@ -1835,13 +2142,36 @@ OBJC_EXPORT struct objc_method_list *class_nextMethodList(Class, void **) OBJC2_ // while ( mlist = class_nextMethodList( cls, &iterator ) ) // ; -OBJC_EXPORT id (*_alloc)(Class, size_t) OBJC2_UNAVAILABLE; -OBJC_EXPORT id (*_copy)(id, size_t) OBJC2_UNAVAILABLE; -OBJC_EXPORT id (*_realloc)(id, size_t) OBJC2_UNAVAILABLE; -OBJC_EXPORT id (*_dealloc)(id) OBJC2_UNAVAILABLE; -OBJC_EXPORT id (*_zoneAlloc)(Class, size_t, void *) OBJC2_UNAVAILABLE; -OBJC_EXPORT id (*_zoneRealloc)(id, size_t, void *) OBJC2_UNAVAILABLE; -OBJC_EXPORT id (*_zoneCopy)(id, size_t, void *) OBJC2_UNAVAILABLE; -OBJC_EXPORT void (*_error)(id, const char *, va_list) OBJC2_UNAVAILABLE; +OBJC_EXPORT id _Nullable +(* _Nonnull _alloc)(Class _Nullable, size_t) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT id _Nullable +(* _Nonnull _copy)(id _Nullable, size_t) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT id _Nullable +(* _Nonnull _realloc)(id _Nullable, size_t) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT id _Nullable +(* _Nonnull _dealloc)(id _Nullable) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT id _Nullable +(* _Nonnull _zoneAlloc)(Class _Nullable, size_t, void * _Nullable) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT id _Nullable +(* _Nonnull _zoneRealloc)(id _Nullable, size_t, void * _Nullable) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT id _Nullable +(* _Nonnull _zoneCopy)(id _Nullable, size_t, void * _Nullable) + OBJC2_UNAVAILABLE; + +OBJC_EXPORT void +(* _Nonnull _error)(id _Nullable, const char * _Nonnull, va_list) + OBJC2_UNAVAILABLE; #endif diff --git a/test/ARCBase.h b/test/ARCBase.h new file mode 100644 index 0000000..55722cf --- /dev/null +++ b/test/ARCBase.h @@ -0,0 +1,21 @@ +// +// ARCBase.h +// TestARCLayouts +// +// Created by Patrick Beard on 3/8/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import + +@interface ARCMisalign : NSObject { + char misalign1; +} +@end + +@interface ARCBase : ARCMisalign +@property long number; +@property(retain) id object; +@property void *pointer; +@property(weak) __weak id delegate; +@end diff --git a/test/ARCBase.m b/test/ARCBase.m new file mode 100644 index 0000000..37f91c5 --- /dev/null +++ b/test/ARCBase.m @@ -0,0 +1,30 @@ +// +// ARCBase.m +// TestARCLayouts +// +// Created by Patrick Beard on 3/8/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "ARCBase.h" + +// ARCMisalign->misalign1 and ARCBase->misalign2 together cause +// ARCBase's instanceStart to be misaligned, which exercises handling +// of storage that is not represented in the class's ivar layout bitmaps. + +@implementation ARCMisalign +@end + +@interface ARCBase () { +@private + char misalign2; + long number; + id object; + void *pointer; + __weak id delegate; +} +@end + +@implementation ARCBase +@synthesize number, object, pointer, delegate; +@end diff --git a/test/ARCLayouts.m b/test/ARCLayouts.m new file mode 100644 index 0000000..516c837 --- /dev/null +++ b/test/ARCLayouts.m @@ -0,0 +1,217 @@ +// Note that test ARCLayoutsWithoutWeak uses the same files +// with different build options. +/* +TEST_CONFIG MEM=arc +TEST_BUILD + mkdir -p $T{OBJDIR} + $C{COMPILE_NOLINK_NOMEM} -c $DIR/MRCBase.m -o $T{OBJDIR}/MRCBase.o + $C{COMPILE_NOLINK_NOMEM} -c $DIR/MRCARC.m -o $T{OBJDIR}/MRCARC.o + $C{COMPILE_NOLINK} -c $DIR/ARCBase.m -o $T{OBJDIR}/ARCBase.o + $C{COMPILE_NOLINK} -c $DIR/ARCMRC.m -o $T{OBJDIR}/ARCMRC.o + $C{COMPILE} '-DNAME=\"ARCLayouts.m\"' -fobjc-arc $DIR/ARCLayouts.m -x none $T{OBJDIR}/MRCBase.o $T{OBJDIR}/MRCARC.o $T{OBJDIR}/ARCBase.o $T{OBJDIR}/ARCMRC.o -framework Foundation -o ARCLayouts.exe +END +*/ + +#include "test.h" +#import +#import +#import + +#import "ARCMRC.h" +#import "MRCARC.h" + +@interface NSObject (Layouts) ++ (const char *)strongLayout; ++ (const char *)weakLayout; +@end + +void printlayout(const char *name, const uint8_t *layout) +{ + if (!testverbose()) return; + + testprintf("%s: ", name); + + // these use fprintf() to avoid repeated VERBOSE: in the middle of the line + if (!layout) { + fprintf(stderr, "NULL\n"); + return; + } + + const uint8_t *c; + for (c = layout; *c; c++) { + fprintf(stderr, "%02x ", *c); + } + + fprintf(stderr, "00\n"); +} + +@implementation NSObject (Layouts) + ++ (const char *)strongLayout { + const uint8_t *layout = class_getIvarLayout(self); + printlayout("strong", layout); + return (const char *)layout; +} + ++ (const char *)weakLayout { + const uint8_t *weakLayout = class_getWeakIvarLayout(self); + printlayout("weak", weakLayout); + return (const char *)weakLayout; +} + ++ (Ivar)instanceVariable:(const char *)name { + return class_getInstanceVariable(self, name); +} + +@end + +void checkMM(Class cls, const char *ivarName, + objc_ivar_memory_management_t mmExpected) +{ + Ivar ivar = [cls instanceVariable:ivarName]; + objc_ivar_memory_management_t mm = _class_getIvarMemoryManagement(cls,ivar); + testprintf("%s->%s want %d, got %d\n", + class_getName(cls), ivarName, mmExpected, mm); + testassert(mmExpected == mm); +} + +int main (int argc __unused, const char * argv[] __unused) { + + testprintf("ARCMRC\n"); + testassert(strcmp([ARCMRC strongLayout], "\x01") == 0); + testassert([ARCMRC weakLayout] == NULL); + + + // Verify that ARCBase->misalign and MRCBase->alignment did their thing. + testassert(ivar_getOffset(class_getInstanceVariable([ARCBase class], "misalign2")) & 1); + testassert(ivar_getOffset(class_getInstanceVariable([MRCBase class], "alignment")) > (ptrdiff_t)sizeof(void*)); + + testprintf("ARCMisalign\n"); + testassert([ARCMisalign strongLayout] == NULL); + testassert([ARCMisalign weakLayout] == NULL); + + testprintf("ARCBase\n"); + if (strcmp([ARCBase strongLayout], "\x11\x30") == 0) { + testwarn("1130 layout is a compiler flaw but doesn't fail"); + } else { + testassert(strcmp([ARCBase strongLayout], "\x11") == 0); + } + testassert(strcmp([ARCBase weakLayout], "\x31") == 0); + + testprintf("MRCARC\n"); + testassert([MRCARC strongLayout] == NULL); + testassert([MRCARC weakLayout] == NULL); + + testprintf("MRCBase\n"); + // MRC marks __weak only. + testassert([MRCBase strongLayout] == NULL); + if (supportsMRCWeak) { + testassert(strcmp([MRCBase weakLayout], "\x71") == 0); + } else { + testassert([MRCBase weakLayout] == nil); + } + + // now check consistency between dynamic accessors and KVC, etc. + ARCMRC *am = [ARCMRC new]; + MRCARC *ma = [MRCARC new]; + + NSString *amValue = [[NSString alloc] initWithFormat:@"%s %p", "ARCMRC", am]; + NSString *amValue2 = [[NSString alloc] initWithFormat:@"%s %p", "ARCMRC", am]; + NSString *maValue = [[NSString alloc] initWithFormat:@"%s %p", "MRCARC", ma]; + NSString *maValue2 = [[NSString alloc] initWithFormat:@"%s %p", "MRCARC", ma]; + + am.number = M_PI; + + object_setIvar(am, [ARCMRC instanceVariable:"object"], amValue); + testassert(CFGetRetainCount((__bridge CFTypeRef)amValue) == 1); + testassert(am.object == amValue); + + object_setIvarWithStrongDefault(am, [ARCMRC instanceVariable:"object"], amValue2); + testassert(CFGetRetainCount((__bridge CFTypeRef)amValue2) == 2); + testassert(am.object == amValue2); + + am.pointer = @selector(ARCMRC); + + object_setIvar(am, [ARCMRC instanceVariable:"delegate"], ma); + testassert(CFGetRetainCount((__bridge CFTypeRef)ma) == 1); + testassert(am.delegate == ma); + + object_setIvarWithStrongDefault(am, [ARCMRC instanceVariable:"delegate"], ma); + testassert(CFGetRetainCount((__bridge CFTypeRef)ma) == 1); + testassert(am.delegate == ma); + + + ma.number = M_E; + + object_setIvar(ma, [MRCARC instanceVariable:"object"], maValue); + testassert(CFGetRetainCount((__bridge CFTypeRef)maValue) == 2); + @autoreleasepool { + testassert(ma.object == maValue); + } + + object_setIvarWithStrongDefault(ma, [MRCARC instanceVariable:"object"], maValue2); + testassert(CFGetRetainCount((__bridge CFTypeRef)maValue2) == 2); + @autoreleasepool { + testassert(ma.object == maValue2); + } + + ma.pointer = @selector(MRCARC); + + ma.delegate = am; + object_setIvar(ma, [MRCARC instanceVariable:"delegate"], am); + testassert(CFGetRetainCount((__bridge CFTypeRef)am) == 1); + @autoreleasepool { + testassert(ma.delegate == am); + } + + object_setIvarWithStrongDefault(ma, [MRCARC instanceVariable:"delegate"], am); + testassert(CFGetRetainCount((__bridge CFTypeRef)am) == 1); + @autoreleasepool { + testassert(ma.delegate == am); + } + + + // Verify that object_copy() handles ARC variables correctly. + + MRCARC *ma2 = docopy(ma); + testassert(ma2); + testassert(ma2 != ma); + testassert(CFGetRetainCount((__bridge CFTypeRef)maValue2) == 3); + testassert(CFGetRetainCount((__bridge CFTypeRef)am) == 1); + testassert(ma2.number == ma.number); + testassert(ma2.object == ma.object); + @autoreleasepool { + testassert(ma2.delegate == ma.delegate); + } + testassert(ma2.pointer == ma.pointer); + + + // Test _class_getIvarMemoryManagement() SPI + + objc_ivar_memory_management_t memoryMRCWeak = + supportsMRCWeak ? objc_ivar_memoryWeak : objc_ivar_memoryUnknown; + checkMM([ARCMRC class], "number", objc_ivar_memoryUnknown); + checkMM([ARCMRC class], "object", objc_ivar_memoryUnknown); + checkMM([ARCMRC class], "pointer", objc_ivar_memoryUnknown); + checkMM([ARCMRC class], "delegate", memoryMRCWeak); + checkMM([ARCMRC class], "dataSource", objc_ivar_memoryStrong); + + checkMM([MRCARC class], "number", objc_ivar_memoryUnretained); + checkMM([MRCARC class], "object", objc_ivar_memoryStrong); + checkMM([MRCARC class], "pointer", objc_ivar_memoryUnretained); + checkMM([MRCARC class], "delegate", objc_ivar_memoryWeak); + checkMM([MRCARC class], "dataSource", objc_ivar_memoryUnknown); + + checkMM([ARCBase class], "number", objc_ivar_memoryUnretained); + checkMM([ARCBase class], "object", objc_ivar_memoryStrong); + checkMM([ARCBase class], "pointer", objc_ivar_memoryUnretained); + checkMM([ARCBase class], "delegate", objc_ivar_memoryWeak); + + checkMM([MRCBase class], "number", objc_ivar_memoryUnknown); + checkMM([MRCBase class], "object", objc_ivar_memoryUnknown); + checkMM([MRCBase class], "pointer", objc_ivar_memoryUnknown); + checkMM([MRCBase class], "delegate", memoryMRCWeak); + + succeed(NAME); + return 0; +} diff --git a/test/ARCLayoutsWithoutWeak.m b/test/ARCLayoutsWithoutWeak.m new file mode 100644 index 0000000..6e81ac6 --- /dev/null +++ b/test/ARCLayoutsWithoutWeak.m @@ -0,0 +1,12 @@ +// Same as test ARCLayouts but with MRC __weak support disabled. +/* +TEST_CONFIG MEM=arc +TEST_BUILD + mkdir -p $T{OBJDIR} + $C{COMPILE_NOLINK_NOMEM} -c $DIR/MRCBase.m -o $T{OBJDIR}/MRCBase.o -fno-objc-weak + $C{COMPILE_NOLINK_NOMEM} -c $DIR/MRCARC.m -o $T{OBJDIR}/MRCARC.o -fno-objc-weak + $C{COMPILE_NOLINK} -c $DIR/ARCBase.m -o $T{OBJDIR}/ARCBase.o + $C{COMPILE_NOLINK} -c $DIR/ARCMRC.m -o $T{OBJDIR}/ARCMRC.o + $C{COMPILE} '-DNAME=\"ARCLayoutsWithoutWeak.m\"' -fobjc-arc $DIR/ARCLayouts.m -x none $T{OBJDIR}/MRCBase.o $T{OBJDIR}/MRCARC.o $T{OBJDIR}/ARCBase.o $T{OBJDIR}/ARCMRC.o -framework Foundation -o ARCLayoutsWithoutWeak.exe +END +*/ diff --git a/test/ARCMRC.h b/test/ARCMRC.h new file mode 100644 index 0000000..a37422e --- /dev/null +++ b/test/ARCMRC.h @@ -0,0 +1,13 @@ +// +// ARCMRC.h +// TestARCLayouts +// +// Created by Patrick Beard on 3/8/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "MRCBase.h" + +@interface ARCMRC : MRCBase +@property(retain) id dataSource; +@end diff --git a/test/ARCMRC.m b/test/ARCMRC.m new file mode 100644 index 0000000..eed9fce --- /dev/null +++ b/test/ARCMRC.m @@ -0,0 +1,11 @@ +// +// ARCMRC.m +// + +#import "ARCMRC.h" + +@implementation ARCMRC + +@synthesize dataSource; + +@end diff --git a/test/MRCARC.h b/test/MRCARC.h new file mode 100644 index 0000000..9443ea9 --- /dev/null +++ b/test/MRCARC.h @@ -0,0 +1,13 @@ +// +// MRCARC.h +// TestARCLayouts +// +// Created by Patrick Beard on 3/8/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "ARCBase.h" + +@interface MRCARC : ARCBase +@property(retain) id dataSource; +@end diff --git a/test/MRCARC.m b/test/MRCARC.m new file mode 100644 index 0000000..c2882f3 --- /dev/null +++ b/test/MRCARC.m @@ -0,0 +1,11 @@ +// +// MRCARC.m +// + +#import "MRCARC.h" + +@implementation MRCARC + +@synthesize dataSource; + +@end diff --git a/test/MRCBase.h b/test/MRCBase.h new file mode 100644 index 0000000..050f43f --- /dev/null +++ b/test/MRCBase.h @@ -0,0 +1,28 @@ +// +// MRCBase.h +// TestARCLayouts +// +// Created by Patrick Beard on 3/8/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import + +// YES if MRC compiler supports ARC-style weak +extern bool supportsMRCWeak; + +#if __LP64__ +#define DOUBLEWORD_ALIGNED __attribute__((aligned(16))) +#else +#define DOUBLEWORD_ALIGNED __attribute__((aligned(8))) +#endif + +@interface MRCBase : NSObject +@property double number; +@property(retain) id object; +@property void *pointer; +@property(weak) __weak id delegate; +@end + +// Call object_copy from MRC. +extern id __attribute__((ns_returns_retained)) docopy(id obj); diff --git a/test/MRCBase.m b/test/MRCBase.m new file mode 100644 index 0000000..e95cc2e --- /dev/null +++ b/test/MRCBase.m @@ -0,0 +1,46 @@ +// +// MRCBase.m +// TestARCLayouts +// +// Created by Patrick Beard on 3/8/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#include "MRCBase.h" +#include "test.h" + +// MRCBase->alignment ensures that there is a gap between the end of +// NSObject's ivars and the start of MRCBase's ivars, which exercises +// handling of storage that is not represented in any class's ivar +// layout bitmaps. + +#if __has_feature(objc_arc_weak) +bool supportsMRCWeak = true; +#else +bool supportsMRCWeak = false; +#endif + +@interface MRCBase () { +@private + double DOUBLEWORD_ALIGNED alignment; + uintptr_t pad[3]; // historically this made OBJC2 layout bitmaps match OBJC1 + double number; + id object; + void *pointer; +#if __has_feature(objc_arc_weak) + __weak +#endif + id delegate; +} +@end + +@implementation MRCBase +@synthesize number, object, pointer, delegate; +@end + +// Call object_copy from MRC. +extern id __attribute__((ns_returns_retained)) +docopy(id obj) +{ + return object_copy(obj, 0); +} diff --git a/test/accessors.m b/test/accessors.m new file mode 100644 index 0000000..9111e64 --- /dev/null +++ b/test/accessors.m @@ -0,0 +1,79 @@ +// TEST_CFLAGS -framework Foundation + +#import +#import +#import +#include "test.h" + +@interface Test : NSObject { + NSString *_value; + // _object is at the last optimized property offset + id _object __attribute__((aligned(64))); +} +@property(readonly) Class cls; +@property(copy) NSString *value; +@property(assign) id object; +@end + +typedef struct { + void *isa; + void *_value; + // _object is at the last optimized property offset + void *_object __attribute__((aligned(64))); +} TestDefs; + +@implementation Test + +// Question: why can't this code be automatically generated? + +#if !__has_feature(objc_arc) +- (void)dealloc { + self.value = nil; + self.object = nil; + [super dealloc]; +} +#endif + +- (Class)cls { return objc_getProperty(self, _cmd, 0, YES); } + +- (NSString*)value { return (NSString*) objc_getProperty(self, _cmd, offsetof(TestDefs, _value), YES); } +- (void)setValue:(NSString*)inValue { objc_setProperty(self, _cmd, offsetof(TestDefs, _value), inValue, YES, YES); } + +- (id)object { return objc_getProperty(self, _cmd, offsetof(TestDefs, _object), YES); } +- (void)setObject:(id)inObject { objc_setProperty(self, _cmd, offsetof(TestDefs, _object), inObject, YES, NO); } + +- (NSString *)description { + return [NSString stringWithFormat:@"value = %@, object = %@", self.value, self.object]; +} + +@end + +int main() { + PUSH_POOL { + + NSMutableString *value = [NSMutableString stringWithUTF8String:"test"]; + id object = [NSNumber numberWithInt:11]; + Test *t = AUTORELEASE([Test new]); + t.value = value; + [value setString:@"yuck"]; // mutate the string. + testassert(t.value != value); // must copy, since it was mutable. + testassert([t.value isEqualToString:@"test"]); + + Class testClass = [Test class]; + Class cls = t.cls; + testassert(testClass == cls); + cls = t.cls; + testassert(testClass == cls); + + t.object = object; + t.object = object; + + // NSLog(@"t.object = %@, t.value = %@", t.object, t.value); + // NSLog(@"t.object = %@, t.value = %@", t.object, t.value); // second call will optimized getters. + + } POP_POOL; + + succeed(__FILE__); + + return 0; +} diff --git a/test/accessors2.m b/test/accessors2.m new file mode 100644 index 0000000..4b3db6d --- /dev/null +++ b/test/accessors2.m @@ -0,0 +1,143 @@ +// TEST_CONFIG + +#include "test.h" +#include +#include + +#include "testroot.i" + +@interface Base : TestRoot { + @public + id ivar; +} +@end +@implementation Base @end + +int main() +{ + SEL _cmd = @selector(foo); + Base *o = [Base new]; + ptrdiff_t offset = ivar_getOffset(class_getInstanceVariable([Base class], "ivar")); + testassert(offset == sizeof(id)); + + TestRoot *value = [TestRoot new]; + + // fixme test atomicity + + // Original setter API + + testprintf("original nonatomic retain\n"); + o->ivar = nil; + TestRootRetain = 0; + TestRootCopyWithZone = 0; + TestRootMutableCopyWithZone = 0; + objc_setProperty(o, _cmd, offset, value, NO/*atomic*/, NO/*copy*/); + testassert(TestRootRetain == 1); + testassert(TestRootCopyWithZone == 0); + testassert(TestRootMutableCopyWithZone == 0); + testassert(o->ivar == value); + + testprintf("original atomic retain\n"); + o->ivar = nil; + TestRootRetain = 0; + TestRootCopyWithZone = 0; + TestRootMutableCopyWithZone = 0; + objc_setProperty(o, _cmd, offset, value, YES/*atomic*/, NO/*copy*/); + testassert(TestRootRetain == 1); + testassert(TestRootCopyWithZone == 0); + testassert(TestRootMutableCopyWithZone == 0); + testassert(o->ivar == value); + + testprintf("original nonatomic copy\n"); + o->ivar = nil; + TestRootRetain = 0; + TestRootCopyWithZone = 0; + TestRootMutableCopyWithZone = 0; + objc_setProperty(o, _cmd, offset, value, NO/*atomic*/, YES/*copy*/); + testassert(TestRootRetain == 0); + testassert(TestRootCopyWithZone == 1); + testassert(TestRootMutableCopyWithZone == 0); + testassert(o->ivar && o->ivar != value); + + testprintf("original atomic copy\n"); + o->ivar = nil; + TestRootRetain = 0; + TestRootCopyWithZone = 0; + TestRootMutableCopyWithZone = 0; + objc_setProperty(o, _cmd, offset, value, YES/*atomic*/, YES/*copy*/); + testassert(TestRootRetain == 0); + testassert(TestRootCopyWithZone == 1); + testassert(TestRootMutableCopyWithZone == 0); + testassert(o->ivar && o->ivar != value); + + testprintf("original nonatomic mutablecopy\n"); + o->ivar = nil; + TestRootRetain = 0; + TestRootCopyWithZone = 0; + TestRootMutableCopyWithZone = 0; + objc_setProperty(o, _cmd, offset, value, NO/*atomic*/, 2/*copy*/); + testassert(TestRootRetain == 0); + testassert(TestRootCopyWithZone == 0); + testassert(TestRootMutableCopyWithZone == 1); + testassert(o->ivar && o->ivar != value); + + testprintf("original atomic mutablecopy\n"); + o->ivar = nil; + TestRootRetain = 0; + TestRootCopyWithZone = 0; + TestRootMutableCopyWithZone = 0; + objc_setProperty(o, _cmd, offset, value, YES/*atomic*/, 2/*copy*/); + testassert(TestRootRetain == 0); + testassert(TestRootCopyWithZone == 0); + testassert(TestRootMutableCopyWithZone == 1); + testassert(o->ivar && o->ivar != value); + + + // Optimized setter API + + testprintf("optimized nonatomic retain\n"); + o->ivar = nil; + TestRootRetain = 0; + TestRootCopyWithZone = 0; + TestRootMutableCopyWithZone = 0; + objc_setProperty_nonatomic(o, _cmd, value, offset); + testassert(TestRootRetain == 1); + testassert(TestRootCopyWithZone == 0); + testassert(TestRootMutableCopyWithZone == 0); + testassert(o->ivar == value); + + testprintf("optimized atomic retain\n"); + o->ivar = nil; + TestRootRetain = 0; + TestRootCopyWithZone = 0; + TestRootMutableCopyWithZone = 0; + objc_setProperty_atomic(o, _cmd, value, offset); + testassert(TestRootRetain == 1); + testassert(TestRootCopyWithZone == 0); + testassert(TestRootMutableCopyWithZone == 0); + testassert(o->ivar == value); + + testprintf("optimized nonatomic copy\n"); + o->ivar = nil; + TestRootRetain = 0; + TestRootCopyWithZone = 0; + TestRootMutableCopyWithZone = 0; + objc_setProperty_nonatomic_copy(o, _cmd, value, offset); + testassert(TestRootRetain == 0); + testassert(TestRootCopyWithZone == 1); + testassert(TestRootMutableCopyWithZone == 0); + testassert(o->ivar && o->ivar != value); + + testprintf("optimized atomic copy\n"); + o->ivar = nil; + TestRootRetain = 0; + TestRootCopyWithZone = 0; + TestRootMutableCopyWithZone = 0; + objc_setProperty_atomic_copy(o, _cmd, value, offset); + testassert(TestRootRetain == 0); + testassert(TestRootCopyWithZone == 1); + testassert(TestRootMutableCopyWithZone == 0); + testassert(o->ivar && o->ivar != value); + + succeed(__FILE__); +} diff --git a/test/addMethod.m b/test/addMethod.m new file mode 100644 index 0000000..8e693d9 --- /dev/null +++ b/test/addMethod.m @@ -0,0 +1,121 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" +#include + +@interface Super : TestRoot @end +@implementation Super +-(int)superMethod { return 0; } +-(int)superMethod2 { return 0; } +-(int)bothMethod { return 0; } +@end + +@interface Sub : Super @end +@implementation Sub +-(int)subMethod { return 0; } +-(int)bothMethod { return 0; } +@end + +@interface Sub2 : Super @end +@implementation Sub2 +-(int)subMethod { return 0; } +-(int)bothMethod { return 0; } +@end + + +id fn(id self __attribute__((unused)), SEL cmd __attribute__((unused)), ...) { return nil; } + +#define TWICE(x) x; x + +int main() +{ + IMP superMethodFromSuper = class_getMethodImplementation([Super class], @selector(superMethod)); + IMP superMethod2FromSuper = class_getMethodImplementation([Super class], @selector(superMethod2)); + IMP bothMethodFromSuper = class_getMethodImplementation([Super class], @selector(bothMethod)); + IMP subMethodFromSub = class_getMethodImplementation([Sub class], @selector(subMethod)); + IMP bothMethodFromSub = class_getMethodImplementation([Sub class], @selector(bothMethod)); + IMP subMethodFromSub2 = class_getMethodImplementation([Sub2 class], @selector(subMethod)); + IMP bothMethodFromSub2 = class_getMethodImplementation([Sub2 class], @selector(bothMethod)); + + testassert(superMethodFromSuper); + testassert(superMethod2FromSuper); + testassert(bothMethodFromSuper); + testassert(subMethodFromSub); + testassert(bothMethodFromSub); + testassert(subMethodFromSub2); + testassert(bothMethodFromSub2); + + BOOL ok; + IMP imp; + + // Each method lookup below is performed twice, with the intent + // that at least one of them will be a cache lookup. + + // class_addMethod doesn't replace existing implementations + ok = class_addMethod([Super class], @selector(superMethod), (IMP)fn, NULL); + testassert(!ok); + TWICE(testassert(class_getMethodImplementation([Super class], @selector(superMethod)) == superMethodFromSuper)); + + // class_addMethod does override superclass implementations + ok = class_addMethod([Sub class], @selector(superMethod), (IMP)fn, NULL); + testassert(ok); + TWICE(testassert(class_getMethodImplementation([Sub class], @selector(superMethod)) == (IMP)fn)); + + // class_addMethod does add root implementations + ok = class_addMethod([Super class], @selector(superMethodNew2), (IMP)fn, NULL); + testassert(ok); + TWICE(testassert(class_getMethodImplementation([Super class], @selector(superMethodNew2)) == (IMP)fn)); + TWICE(testassert(class_getMethodImplementation([Sub class], @selector(superMethodNew2)) == (IMP)fn)); + + // bincompat: some apps call class_addMethod() and class_replaceMethod() with a nil IMP + // This has the effect of covering the superclass implementation. + // fixme change this, possibly behind a linked-on-or-after check + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wnonnull" + ok = class_addMethod([Sub class], @selector(superMethod2), nil, NULL); + #pragma clang diagnostic pop + testassert(ok); + TWICE(testassert(class_getMethodImplementation([Sub class], @selector(superMethod2)) == (IMP)_objc_msgForward)); + + // class_replaceMethod does add new implementations, + // returning NULL if super has an implementation + imp = class_replaceMethod([Sub2 class], @selector(superMethod), (IMP)fn, NULL); + testassert(imp == NULL); + TWICE(testassert(class_getMethodImplementation([Sub2 class], @selector(superMethod)) == (IMP)fn)); + + // class_replaceMethod does add new implementations, + // returning NULL if super has no implementation + imp = class_replaceMethod([Sub2 class], @selector(subMethodNew), (IMP)fn, NULL); + testassert(imp == NULL); + TWICE(testassert(class_getMethodImplementation([Sub2 class], @selector(subMethodNew)) == (IMP)fn)); + + // class_replaceMethod does add new implemetations + // returning NULL if there is no super class + imp = class_replaceMethod([Super class], @selector(superMethodNew), (IMP)fn, NULL); + testassert(imp == NULL); + TWICE(testassert(class_getMethodImplementation([Super class], @selector(superMethodNew)) == (IMP)fn)); + + + // class_replaceMethod does replace existing implementations, + // returning existing implementation (regardless of super) + imp = class_replaceMethod([Sub2 class], @selector(subMethod), (IMP)fn, NULL); + testassert(imp == subMethodFromSub2); + TWICE(testassert(class_getMethodImplementation([Sub2 class], @selector(subMethod)) == (IMP)fn)); + + // class_replaceMethod does replace existing implemetations, + // returning existing implementation (regardless of super) + imp = class_replaceMethod([Sub2 class], @selector(bothMethod), (IMP)fn, NULL); + testassert(imp == bothMethodFromSub2); + TWICE(testassert(class_getMethodImplementation([Sub2 class], @selector(bothMethod)) == (IMP)fn)); + + // class_replaceMethod does replace existing implemetations, + // returning existing implementation (regardless of super) + imp = class_replaceMethod([Super class], @selector(superMethod), (IMP)fn, NULL); + testassert(imp == superMethodFromSuper); + TWICE(testassert(class_getMethodImplementation([Super class], @selector(superMethod)) == (IMP)fn)); + + // fixme actually try calling them + + succeed(__FILE__); +} diff --git a/test/addMethods.m b/test/addMethods.m new file mode 100644 index 0000000..022ee95 --- /dev/null +++ b/test/addMethods.m @@ -0,0 +1,324 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" +#include +#include + +// Macros for array construction. +// ten IMPs +#define IMPS10 (IMP)fn0, (IMP)fn1, (IMP)fn2, (IMP)fn3, (IMP)fn4, \ + (IMP)fn5, (IMP)fn6, (IMP)fn7, (IMP)fn8, (IMP)fn9 +// ten method types +#define TYPES10 "", "", "", "", "", "", "", "", "", "" +// ten selectors of the form name0..name9 +#define SELS10(name) \ + @selector(name##0), @selector(name##1), @selector(name##2), \ + @selector(name##3), @selector(name##4), @selector(name##5), \ + @selector(name##6), @selector(name##7), @selector(name##8), \ + @selector(name##9) + + +@interface Super : TestRoot @end +@implementation Super +-(int)superMethod0 { return 0; } +-(int)superMethod1 { return 0; } +-(int)superMethod2 { return 0; } +-(int)superMethod3 { return 0; } +-(int)superMethod4 { return 0; } +-(int)superMethod5 { return 0; } +-(int)superMethod6 { return 0; } +-(int)superMethod7 { return 0; } +-(int)superMethod8 { return 0; } +-(int)superMethod9 { return 0; } +-(int)bothMethod0 { return 0; } +-(int)bothMethod1 { return 0; } +-(int)bothMethod2 { return 0; } +-(int)bothMethod3 { return 0; } +-(int)bothMethod4 { return 0; } +-(int)bothMethod5 { return 0; } +-(int)bothMethod6 { return 0; } +-(int)bothMethod7 { return 0; } +-(int)bothMethod8 { return 0; } +-(int)bothMethod9 { return 0; } +@end + +@interface Sub : Super @end +@implementation Sub +-(int)subMethod0 { return 0; } +-(int)subMethod1 { return 0; } +-(int)subMethod2 { return 0; } +-(int)subMethod3 { return 0; } +-(int)subMethod4 { return 0; } +-(int)subMethod5 { return 0; } +-(int)subMethod6 { return 0; } +-(int)subMethod7 { return 0; } +-(int)subMethod8 { return 0; } +-(int)subMethod9 { return 0; } +-(int)bothMethod0 { return 0; } +-(int)bothMethod1 { return 0; } +-(int)bothMethod2 { return 0; } +-(int)bothMethod3 { return 0; } +-(int)bothMethod4 { return 0; } +-(int)bothMethod5 { return 0; } +-(int)bothMethod6 { return 0; } +-(int)bothMethod7 { return 0; } +-(int)bothMethod8 { return 0; } +-(int)bothMethod9 { return 0; } +@end + +@interface Sub2 : Super @end +@implementation Sub2 +-(int)subMethod0 { return 0; } +-(int)subMethod1 { return 0; } +-(int)subMethod2 { return 0; } +-(int)subMethod3 { return 0; } +-(int)subMethod4 { return 0; } +-(int)subMethod5 { return 0; } +-(int)subMethod6 { return 0; } +-(int)subMethod7 { return 0; } +-(int)subMethod8 { return 0; } +-(int)subMethod9 { return 0; } +-(int)bothMethod0 { return 0; } +-(int)bothMethod1 { return 0; } +-(int)bothMethod2 { return 0; } +-(int)bothMethod3 { return 0; } +-(int)bothMethod4 { return 0; } +-(int)bothMethod5 { return 0; } +-(int)bothMethod6 { return 0; } +-(int)bothMethod7 { return 0; } +-(int)bothMethod8 { return 0; } +-(int)bothMethod9 { return 0; } +@end + + +id fn0(id self __attribute__((unused)), SEL cmd __attribute__((unused)), ...) { + return nil; +} +id fn1(id self __attribute__((unused)), SEL cmd __attribute__((unused)), ...) { + return nil; +} +id fn2(id self __attribute__((unused)), SEL cmd __attribute__((unused)), ...) { + return nil; +} +id fn3(id self __attribute__((unused)), SEL cmd __attribute__((unused)), ...) { + return nil; +} +id fn4(id self __attribute__((unused)), SEL cmd __attribute__((unused)), ...) { + return nil; +} +id fn5(id self __attribute__((unused)), SEL cmd __attribute__((unused)), ...) { + return nil; +} +id fn6(id self __attribute__((unused)), SEL cmd __attribute__((unused)), ...) { + return nil; +} +id fn7(id self __attribute__((unused)), SEL cmd __attribute__((unused)), ...) { + return nil; +} +id fn8(id self __attribute__((unused)), SEL cmd __attribute__((unused)), ...) { + return nil; +} +id fn9(id self __attribute__((unused)), SEL cmd __attribute__((unused)), ...) { + return nil; +} + +void testBulkMemoryOnce(void) +{ + Class c = objc_allocateClassPair([TestRoot class], "c", 0); + objc_registerClassPair(c); + + SEL sels[10] = { + SELS10(method) + }; + IMP imps[10] = { + IMPS10 + }; + const char *types[10] = { + TYPES10 + }; + + uint32_t failureCount = 0; + SEL *failed; + + // Test all successes. + failed = class_addMethodsBulk(c, sels, imps, types, 4, &failureCount); + testassert(failed == NULL); + testassert(failureCount == 0); + + // Test mixed success and failure (this overlaps the previous one, so there + // will be one of each). + failed = class_addMethodsBulk(c, sels + 3, imps + 3, types + 3, 2, + &failureCount); + testassert(failed != NULL); + testassert(failureCount == 1); + testassert(failed[0] == sels[3]); + free(failed); + + // Test total failure. + failed = class_addMethodsBulk(c, sels, imps, types, 5, &failureCount); + testassert(failed != NULL); + testassert(failureCount == 5); + for(int i = 0; i < 5; i++) { + testassert(failed[i] == sels[i]); + } + free(failed); + + class_replaceMethodsBulk(c, sels, imps, types, 10); + + for(int i = 0; i < 10; i++) { + testassert(class_getMethodImplementation(c, sels[i]) == imps[i]); + } + + objc_disposeClassPair(c); +} + +int main() +{ + IMP dummyIMPs[130] = { + IMPS10, IMPS10, IMPS10, IMPS10, IMPS10, + IMPS10, IMPS10, IMPS10, IMPS10, IMPS10, + IMPS10, IMPS10, IMPS10, + }; + + // similar to dummyIMPs but with different values in each slot + IMP dummyIMPs2[130] = { + (IMP)fn5, (IMP)fn6, (IMP)fn7, (IMP)fn8, (IMP)fn9, + IMPS10, IMPS10, IMPS10, IMPS10, IMPS10, + IMPS10, IMPS10, IMPS10, IMPS10, IMPS10, + IMPS10, IMPS10, + (IMP)fn0, (IMP)fn1, (IMP)fn2, (IMP)fn3, (IMP)fn4, + }; + + const char *dummyTypes[130] = { + TYPES10, TYPES10, TYPES10, TYPES10, TYPES10, + TYPES10, TYPES10, TYPES10, TYPES10, TYPES10, + TYPES10, TYPES10, TYPES10, + }; + + SEL addSELs[20] = { + SELS10(superMethod), + SELS10(superMethodAddNew) + }; + + uint32_t failedCount = 0; + SEL *failed; + + failed = class_addMethodsBulk([Super class], addSELs, dummyIMPs, dummyTypes, + 20, &failedCount); + + // class_addMethodsBulk reports failures for all methods that already exist + testassert(failed != NULL); + testassert(failedCount == 10); + + // class_addMethodsBulk failed for existing implementations + for(int i = 0; i < 10; i++) { + testassert(failed[i] == addSELs[i]); + testassert(class_getMethodImplementation([Super class], addSELs[i]) + != dummyIMPs[i]); + } + + free(failed); + + // class_addMethodsBulk does add root implementations + for(int i = 10; i < 20; i++) { + testassert(class_getMethodImplementation([Super class], addSELs[i]) + == dummyIMPs[i]); + } + + // class_addMethod does override superclass implementations + failed = class_addMethodsBulk([Sub class], addSELs, dummyIMPs, dummyTypes, + 10, &failedCount); + testassert(failedCount == 0); + testassert(failed == NULL); + for(int i = 0; i < 10; i++) { + testassert(class_getMethodImplementation([Sub class], addSELs[i]) + == dummyIMPs[i]); + } + + SEL subReplaceSELs[40] = { + SELS10(superMethod), + SELS10(subMethodNew), + SELS10(subMethod), + SELS10(bothMethod), + }; + + // class_replaceMethodsBulk adds new implementations or replaces existing + // ones for methods that exist on the superclass, the subclass, both, or + // neither + class_replaceMethodsBulk([Sub2 class], subReplaceSELs, dummyIMPs, + dummyTypes, 40); + for(int i = 0; i < 40; i++) { + IMP newIMP = class_getMethodImplementation([Sub2 class], + subReplaceSELs[i]); + testassert(newIMP == dummyIMPs[i]); + } + + SEL superReplaceSELs[20] = { + SELS10(superMethod), + SELS10(superMethodNew), + }; + + // class_replaceMethodsBulk adds new implementations or replaces existing + // ones in the superclass + class_replaceMethodsBulk([Super class], superReplaceSELs, dummyIMPs, + dummyTypes, 20); + for(int i = 0; i < 20; i++) { + IMP newIMP = class_getMethodImplementation([Super class], + superReplaceSELs[i]); + testassert(newIMP == dummyIMPs[i]); + } + + + // class_addMethodsBulk, where almost all of the requested additions + // already exist and thus can't be added. (They were already added + // above by class_replaceMethodsBulk([Sub2 class], subReplaceSELs, ...).) + + // This list is large in the hope of provoking any realloc() of the + // new method list inside addMethods(). + // The runtime doesn't care that the list contains lots of duplicates. + SEL subAddMostlyExistingSELs[130] = { + SELS10(superMethod), SELS10(subMethodNew), SELS10(subMethod), + SELS10(superMethod), SELS10(subMethodNew), SELS10(subMethod), + SELS10(superMethod), SELS10(subMethodNew), SELS10(subMethod), + SELS10(superMethod), SELS10(subMethodNew), SELS10(subMethod), + SELS10(bothMethod), + }; + subAddMostlyExistingSELs[16] = @selector(INDEX_16_IS_DIFFERENT); + + failed = class_addMethodsBulk([Sub2 class], subAddMostlyExistingSELs, + dummyIMPs2, dummyTypes, 130, &failedCount); + testassert(failedCount == 129); + testassert(failed != NULL); + + for(int i = 0; i < 130; i++) { + IMP newIMP = class_getMethodImplementation([Sub2 class], + subAddMostlyExistingSELs[i]); + if (i == 16) { + // the only one that was actually added + testassert(newIMP != dummyIMPs[i]); + testassert(newIMP == dummyIMPs2[i]); + } else { + // the others should all have failed + testassert(newIMP == dummyIMPs[i]); + testassert(newIMP != dummyIMPs2[i]); + } + } + for (uint32_t i = 0; i < failedCount; i++) { + testassert(failed[i] != NULL); + testassert(failed[i] != subAddMostlyExistingSELs[16]); + } + + + // fixme actually try calling them + + // make sure the Bulk functions aren't leaking + testBulkMemoryOnce(); + leak_mark(); + for(int i = 0; i < 10; i++) { + testBulkMemoryOnce(); + } + leak_check(0); + + succeed(__FILE__); +} diff --git a/test/addProtocol.m b/test/addProtocol.m new file mode 100644 index 0000000..7f639b1 --- /dev/null +++ b/test/addProtocol.m @@ -0,0 +1,255 @@ +/* +TEST_CFLAGS -framework Foundation +TEST_BUILD_OUTPUT +.*addProtocol.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*addProtocol.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*addProtocol.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*addProtocol.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*addProtocol.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +END +TEST_RUN_OUTPUT +objc\[\d+\]: protocol_addProtocol: added protocol 'EmptyProto' is still under construction! +objc\[\d+\]: objc_registerProtocol: protocol 'Proto1' was already registered! +objc\[\d+\]: protocol_addProtocol: modified protocol 'Proto1' is not under construction! +objc\[\d+\]: protocol_addMethodDescription: protocol 'Proto1' is not under construction! +objc\[\d+\]: objc_registerProtocol: protocol 'SuperProto' was already registered! +objc\[\d+\]: protocol_addProtocol: modified protocol 'SuperProto' is not under construction! +objc\[\d+\]: protocol_addMethodDescription: protocol 'SuperProto' is not under construction! +OK: addProtocol.m +END +*/ + +#include "test.h" + +#include +#include + +@protocol SuperProto @end +@protocol SuperProto2 @end +@protocol UnrelatedProto @end + + +void Crash(id self, SEL _cmd) +{ + fail("%c[%s %s] called unexpectedly", + class_isMetaClass(object_getClass(self)) ? '+' : '-', + object_getClassName(self), sel_getName(_cmd)); +} + + +int main() +{ + // old-ABI implementation of [Protocol retain] + // is added by +[NSObject(NSObject) load] in CF. + [NSObject class]; + + Protocol *proto, *proto2; + Protocol * __unsafe_unretained *protolist; + struct objc_method_description *desclist; + objc_property_t *proplist; + unsigned int count; + + // If objc_registerProtocol() fails to preserve the retain count + // then ARC will deallocate Protocol objects too early. + class_replaceMethod(objc_getClass("Protocol"), + sel_registerName("dealloc"), (IMP)Crash, "v@:"); + class_replaceMethod(objc_getClass("__IncompleteProtocol"), + sel_registerName("dealloc"), (IMP)Crash, "v@:"); + + // make sure binary contains hard copies of these protocols + proto = @protocol(SuperProto); + proto = @protocol(SuperProto2); + + // Adding a protocol + + char *name = strdup("Proto1"); + proto = objc_allocateProtocol(name); + testassert(proto); + testassert(!objc_getProtocol(name)); + + protocol_addProtocol(proto, @protocol(SuperProto)); + protocol_addProtocol(proto, @protocol(SuperProto2)); + // no inheritance cycles + proto2 = objc_allocateProtocol("EmptyProto"); + protocol_addProtocol(proto, proto2); // fails + objc_registerProtocol(proto2); + protocol_addProtocol(proto, proto2); // succeeds + + char *types = strdup("@:"); + protocol_addMethodDescription(proto, @selector(ReqInst0), types, YES, YES); + protocol_addMethodDescription(proto, @selector(ReqInst1), types, YES, YES); + protocol_addMethodDescription(proto, @selector(ReqInst2), types, YES, YES); + protocol_addMethodDescription(proto, @selector(ReqInst3), types, YES, YES); + + protocol_addMethodDescription(proto, @selector(ReqClas0), types, YES, NO); + protocol_addMethodDescription(proto, @selector(ReqClas1), types, YES, NO); + protocol_addMethodDescription(proto, @selector(ReqClas2), types, YES, NO); + protocol_addMethodDescription(proto, @selector(ReqClas3), types, YES, NO); + + protocol_addMethodDescription(proto, @selector(OptInst0), types, NO, YES); + protocol_addMethodDescription(proto, @selector(OptInst1), types, NO, YES); + protocol_addMethodDescription(proto, @selector(OptInst2), types, NO, YES); + protocol_addMethodDescription(proto, @selector(OptInst3), types, NO, YES); + + protocol_addMethodDescription(proto, @selector(OptClas0), types, NO, NO); + protocol_addMethodDescription(proto, @selector(OptClas1), types, NO, NO); + protocol_addMethodDescription(proto, @selector(OptClas2), types, NO, NO); + protocol_addMethodDescription(proto, @selector(OptClas3), types, NO, NO); + + char *name0 = strdup("ReqInst0"); + char *name1 = strdup("ReqInst1"); + char *name2 = strdup("ReqInst2"); + char *name3 = strdup("ReqInst3"); + char *name4 = strdup("ReqClass0"); + char *name5 = strdup("ReqClass1"); + char *name6 = strdup("ReqClass2"); + char *name7 = strdup("ReqClass3"); + char *attrname = strdup("T"); + char *attrvalue = strdup("i"); + objc_property_attribute_t attrs[] = {{attrname, attrvalue}}; + int attrcount = sizeof(attrs) / sizeof(attrs[0]); + protocol_addProperty(proto, name0, attrs, attrcount, YES, YES); + protocol_addProperty(proto, name1, attrs, attrcount, YES, YES); + protocol_addProperty(proto, name2, attrs, attrcount, YES, YES); + protocol_addProperty(proto, name3, attrs, attrcount, YES, YES); + protocol_addProperty(proto, name4, attrs, attrcount, YES, NO); + protocol_addProperty(proto, name5, attrs, attrcount, YES, NO); + protocol_addProperty(proto, name6, attrs, attrcount, YES, NO); + protocol_addProperty(proto, name7, attrs, attrcount, YES, NO); + + objc_registerProtocol(proto); + testassert(0 == strcmp(protocol_getName(proto), "Proto1")); + + // Use of added protocols + + testassert(proto == objc_getProtocol("Proto1")); + strcpy(name, "XXXXXX"); // name is copied + testassert(0 == strcmp(protocol_getName(proto), "Proto1")); + + protolist = protocol_copyProtocolList(proto, &count); + testassert(protolist); + testassert(count == 3); + // note this order is not required + testassert(protolist[0] == @protocol(SuperProto) && + protolist[1] == @protocol(SuperProto2) && + protolist[2] == proto2); + free(protolist); + + testassert(protocol_conformsToProtocol(proto, proto2)); + testassert(protocol_conformsToProtocol(proto, @protocol(SuperProto))); + testassert(!protocol_conformsToProtocol(proto, @protocol(UnrelatedProto))); + + strcpy(types, "XX"); // types is copied + desclist = protocol_copyMethodDescriptionList(proto, YES, YES, &count); + testassert(desclist && count == 4); + testprintf("%p %p\n", desclist[0].name, @selector(ReqInst0)); + // testassert(desclist[0].name == @selector(ReqInst0)); + testassert(0 == strcmp(desclist[0].types, "@:")); + free(desclist); + desclist = protocol_copyMethodDescriptionList(proto, YES, NO, &count); + testassert(desclist && count == 4); + testassert(desclist[1].name == @selector(ReqClas1)); + testassert(0 == strcmp(desclist[1].types, "@:")); + free(desclist); + desclist = protocol_copyMethodDescriptionList(proto, NO, YES, &count); + testassert(desclist && count == 4); + testassert(desclist[2].name == @selector(OptInst2)); + testassert(0 == strcmp(desclist[2].types, "@:")); + free(desclist); + desclist = protocol_copyMethodDescriptionList(proto, NO, NO, &count); + testassert(desclist && count == 4); + testassert(desclist[3].name == @selector(OptClas3)); + testassert(0 == strcmp(desclist[3].types, "@:")); + free(desclist); + + strcpy(name0, "XXXXXXXX"); // name is copied + strcpy(name1, "XXXXXXXX"); // name is copied + strcpy(name2, "XXXXXXXX"); // name is copied + strcpy(name3, "XXXXXXXX"); // name is copied + strcpy(name4, "XXXXXXXXX"); // name is copied + strcpy(name5, "XXXXXXXXX"); // name is copied + strcpy(name6, "XXXXXXXXX"); // name is copied + strcpy(name7, "XXXXXXXXX"); // name is copied + strcpy(attrname, "X"); // description is copied + strcpy(attrvalue, "X"); // description is copied + memset(attrs, 'X', sizeof(attrs)); // description is copied + + // instance properties + count = 100; + proplist = protocol_copyPropertyList(proto, &count); + testassert(proplist); + testassert(count == 4); + // note this order is not required + testassert(0 == strcmp(property_getName(proplist[0]), "ReqInst0")); + testassert(0 == strcmp(property_getName(proplist[1]), "ReqInst1")); + testassert(0 == strcmp(property_getName(proplist[2]), "ReqInst2")); + testassert(0 == strcmp(property_getName(proplist[3]), "ReqInst3")); + testassert(0 == strcmp(property_getAttributes(proplist[0]), "Ti")); + testassert(0 == strcmp(property_getAttributes(proplist[1]), "Ti")); + testassert(0 == strcmp(property_getAttributes(proplist[2]), "Ti")); + testassert(0 == strcmp(property_getAttributes(proplist[3]), "Ti")); + free(proplist); + + // class properties + count = 100; + proplist = protocol_copyPropertyList2(proto, &count, YES, NO); + testassert(proplist); + testassert(count == 4); + // note this order is not required + testassert(0 == strcmp(property_getName(proplist[0]), "ReqClass0")); + testassert(0 == strcmp(property_getName(proplist[1]), "ReqClass1")); + testassert(0 == strcmp(property_getName(proplist[2]), "ReqClass2")); + testassert(0 == strcmp(property_getName(proplist[3]), "ReqClass3")); + testassert(0 == strcmp(property_getAttributes(proplist[0]), "Ti")); + testassert(0 == strcmp(property_getAttributes(proplist[1]), "Ti")); + testassert(0 == strcmp(property_getAttributes(proplist[2]), "Ti")); + testassert(0 == strcmp(property_getAttributes(proplist[3]), "Ti")); + free(proplist); + + + testassert(proto2 == objc_getProtocol("EmptyProto")); + testassert(0 == strcmp(protocol_getName(proto2), "EmptyProto")); + + protolist = protocol_copyProtocolList(proto2, &count); + testassert(!protolist); + testassert(count == 0); + + testassert(!protocol_conformsToProtocol(proto2, proto)); + testassert(!protocol_conformsToProtocol(proto2,@protocol(SuperProto))); + testassert(!protocol_conformsToProtocol(proto2,@protocol(UnrelatedProto))); + + desclist = protocol_copyMethodDescriptionList(proto2, YES, YES, &count); + testassert(!desclist && count == 0); + desclist = protocol_copyMethodDescriptionList(proto2, YES, NO, &count); + testassert(!desclist && count == 0); + desclist = protocol_copyMethodDescriptionList(proto2, NO, YES, &count); + testassert(!desclist && count == 0); + desclist = protocol_copyMethodDescriptionList(proto2, NO, NO, &count); + testassert(!desclist && count == 0); + + // Immutability of existing protocols + + objc_registerProtocol(proto); + protocol_addProtocol(proto, @protocol(SuperProto2)); + protocol_addMethodDescription(proto, @selector(foo), "", YES, YES); + + objc_registerProtocol(@protocol(SuperProto)); + protocol_addProtocol(@protocol(SuperProto), @protocol(SuperProto2)); + protocol_addMethodDescription(@protocol(SuperProto), @selector(foo), "", YES, YES); + + // No duplicates + + proto = objc_allocateProtocol("SuperProto"); + testassert(!proto); + proto = objc_allocateProtocol("Proto1"); + testassert(!proto); + + // NULL protocols ignored + + protocol_addProtocol((__bridge Protocol *)((void*)1), NULL); + protocol_addProtocol(NULL, (__bridge Protocol *)((void*)1)); + protocol_addProtocol(NULL, NULL); + protocol_addMethodDescription(NULL, @selector(foo), "", YES, YES); + + succeed(__FILE__); +} diff --git a/test/applescriptobjc.m b/test/applescriptobjc.m new file mode 100644 index 0000000..dd4370e --- /dev/null +++ b/test/applescriptobjc.m @@ -0,0 +1,13 @@ +// TEST_CONFIG OS=macosx +// TEST_CFLAGS -framework AppleScriptObjC -framework Foundation + +// Verify that trivial AppleScriptObjC apps run with GC off. + +#include +#include "test.h" + +int main() +{ + [NSBundle class]; + succeed(__FILE__); +} diff --git a/test/arr-cast.m b/test/arr-cast.m new file mode 100644 index 0000000..ddb6e21 --- /dev/null +++ b/test/arr-cast.m @@ -0,0 +1,20 @@ +// TEST_CONFIG + +#include "test.h" + +// objc.h redefines these calls into bridge casts. +// This test verifies that the function implementations are exported. +__BEGIN_DECLS +extern void *retainedObject(void *arg) __asm__("_objc_retainedObject"); +extern void *unretainedObject(void *arg) __asm__("_objc_unretainedObject"); +extern void *unretainedPointer(void *arg) __asm__("_objc_unretainedPointer"); +__END_DECLS + +int main() +{ + void *p = (void*)&main; + testassert(p == retainedObject(p)); + testassert(p == unretainedObject(p)); + testassert(p == unretainedPointer(p)); + succeed(__FILE__); +} diff --git a/test/arr-weak.m b/test/arr-weak.m new file mode 100644 index 0000000..a38077f --- /dev/null +++ b/test/arr-weak.m @@ -0,0 +1,329 @@ +// TEST_CONFIG MEM=mrc +// TEST_CRASHES +/* +TEST_RUN_OUTPUT +objc\[\d+\]: Cannot form weak reference to instance \(0x[0-9a-f]+\) of class Crash. It is possible that this object was over-released, or is in the process of deallocation. +objc\[\d+\]: HALTED +END +*/ + +#include "test.h" + +#include + +static id weak; +static id weak2; +static id weak3; +static id weak4; +static bool did_dealloc; + +static int state; + +@interface NSObject (WeakInternals) +-(BOOL)_tryRetain; +-(BOOL)_isDeallocating; +@end + +@interface Test : NSObject @end +@implementation Test +-(void)dealloc { + testprintf("Weak storeOrNil does not crash while deallocating\n"); + weak4 = (id)0x100; // old value must not be used + id result = objc_initWeakOrNil(&weak4, self); + testassert(result == nil); + testassert(weak4 == nil); + result = objc_storeWeakOrNil(&weak4, self); + testassert(result == nil); + testassert(weak4 == nil); + + // The value returned by objc_loadWeak() is now nil, + // but the storage is not yet cleared. + testassert(weak == self); + testassert(weak2 == self); + + // objc_loadWeak() does not eagerly clear the storage. + testassert(objc_loadWeakRetained(&weak) == nil); + testassert(weak != nil); + + // dealloc clears the storage. + testprintf("Weak references clear during super dealloc\n"); + testassert(weak2 != nil); + [super dealloc]; + testassert(weak == nil); + testassert(weak2 == nil); + + did_dealloc = true; +} +@end + +@interface CustomTryRetain : Test @end +@implementation CustomTryRetain +-(BOOL)_tryRetain { state++; return [super _tryRetain]; } +@end + +@interface CustomIsDeallocating : Test @end +@implementation CustomIsDeallocating +-(BOOL)_isDeallocating { state++; return [super _isDeallocating]; } +@end + +@interface CustomAllowsWeakReference : Test @end +@implementation CustomAllowsWeakReference +-(BOOL)allowsWeakReference { state++; return [super allowsWeakReference]; } +@end + +@interface CustomRetainWeakReference : Test @end +@implementation CustomRetainWeakReference +-(BOOL)retainWeakReference { state++; return [super retainWeakReference]; } +@end + +@interface Crash : NSObject @end +@implementation Crash +-(void)dealloc { + testassert(weak == self); + testassert(weak2 == self); + testassert(objc_loadWeakRetained(&weak) == nil); + testassert(objc_loadWeakRetained(&weak2) == nil); + + testprintf("Weak storeOrNil does not crash while deallocating\n"); + id result = objc_storeWeakOrNil(&weak, self); + testassert(result == nil); + + testprintf("Weak store crashes while deallocating\n"); + objc_storeWeak(&weak, self); + fail("objc_storeWeak of deallocating value should have crashed"); + [super dealloc]; +} +@end + + +void cycle(Class cls, Test *obj, Test *obj2, bool storeOrNil) +{ + testprintf("Cycling class %s\n", class_getName(cls)); + + id result; + + id (*storeWeak)(id *location, id obj); + id (*initWeak)(id *location, id obj); + if (storeOrNil) { + testprintf("Using objc_storeWeakOrNil\n"); + storeWeak = objc_storeWeakOrNil; + initWeak = objc_initWeakOrNil; + } else { + testprintf("Using objc_storeWeak\n"); + storeWeak = objc_storeWeak; + initWeak = objc_initWeak; + } + + // state counts calls to custom weak methods + // Difference test classes have different expected values. + int storeTarget; + int loadTarget; + if (cls == [Test class]) { + storeTarget = 0; + loadTarget = 0; + } + else if (cls == [CustomTryRetain class] || + cls == [CustomRetainWeakReference class]) + { + storeTarget = 0; + loadTarget = 1; + } + else if (cls == [CustomIsDeallocating class] || + cls == [CustomAllowsWeakReference class]) + { + storeTarget = 1; + loadTarget = 0; + } + else fail("wut"); + + testprintf("Weak assignment\n"); + state = 0; + result = storeWeak(&weak, obj); + testassert(state == storeTarget); + testassert(result == obj); + testassert(weak == obj); + + testprintf("Weak assignment to the same value\n"); + state = 0; + result = storeWeak(&weak, obj); + testassert(state == storeTarget); + testassert(result == obj); + testassert(weak == obj); + + testprintf("Weak load\n"); + state = 0; + result = objc_loadWeakRetained(&weak); + if (state != loadTarget) testprintf("state %d target %d\n", state, loadTarget); + testassert(state == loadTarget); + testassert(result == obj); + testassert(result == weak); + [result release]; + + testprintf("Weak assignment to different value\n"); + state = 0; + result = storeWeak(&weak, obj2); + testassert(state == storeTarget); + testassert(result == obj2); + testassert(weak == obj2); + + testprintf("Weak assignment to NULL\n"); + state = 0; + result = storeWeak(&weak, NULL); + testassert(state == 0); + testassert(result == NULL); + testassert(weak == NULL); + + testprintf("Weak re-assignment to NULL\n"); + state = 0; + result = storeWeak(&weak, NULL); + testassert(state == 0); + testassert(result == NULL); + testassert(weak == NULL); + + testprintf("Weak move\n"); + state = 0; + result = storeWeak(&weak, obj); + testassert(state == storeTarget); + testassert(result == obj); + testassert(weak == obj); + weak2 = (id)(PAGE_MAX_SIZE-16); + objc_moveWeak(&weak2, &weak); + testassert(weak == nil); + testassert(weak2 == obj); + storeWeak(&weak2, NULL); + + testprintf("Weak copy\n"); + state = 0; + result = storeWeak(&weak, obj); + testassert(state == storeTarget); + testassert(result == obj); + testassert(weak == obj); + weak2 = (id)(PAGE_MAX_SIZE-16); + objc_copyWeak(&weak2, &weak); + testassert(weak == obj); + testassert(weak2 == obj); + storeWeak(&weak, NULL); + storeWeak(&weak2, NULL); + + testprintf("Weak clear\n"); + + id obj3 = [cls new]; + + state = 0; + result = storeWeak(&weak, obj3); + testassert(state == storeTarget); + testassert(result == obj3); + testassert(weak == obj3); + + state = 0; + result = storeWeak(&weak2, obj3); + testassert(state == storeTarget); + testassert(result == obj3); + testassert(weak2 == obj3); + + did_dealloc = false; + [obj3 release]; + testassert(did_dealloc); + testassert(weak == NULL); + testassert(weak2 == NULL); + + + testprintf("Weak init and destroy\n"); + + id obj4 = [cls new]; + + state = 0; + weak = (id)0x100; // old value must not be used + result = initWeak(&weak, obj4); + testassert(state == storeTarget); + testassert(result == obj4); + testassert(weak == obj4); + + state = 0; + weak2 = (id)0x100; // old value must not be used + result = initWeak(&weak2, obj4); + testassert(state == storeTarget); + testassert(result == obj4); + testassert(weak2 == obj4); + + state = 0; + weak3 = (id)0x100; // old value must not be used + result = initWeak(&weak3, obj4); + testassert(state == storeTarget); + testassert(result == obj4); + testassert(weak3 == obj4); + + state = 0; + objc_destroyWeak(&weak3); + testassert(state == 0); + testassert(weak3 == obj4); // storage is unchanged + + did_dealloc = false; + [obj4 release]; + testassert(did_dealloc); + testassert(weak == NULL); // not destroyed earlier so cleared now + testassert(weak2 == NULL); // not destroyed earlier so cleared now + testassert(weak3 == obj4); // destroyed earlier so not cleared now + + objc_destroyWeak(&weak); + objc_destroyWeak(&weak2); +} + + +void test_class(Class cls) +{ + // prime strong and weak side tables before leak checking + Test *prime[256] = {nil}; + for (size_t i = 0; i < sizeof(prime)/sizeof(prime[0]); i++) { + objc_storeWeak(&prime[i], [cls new]); + } + + Test *obj = [cls new]; + Test *obj2 = [cls new]; + + for (int i = 0; i < 100000; i++) { + cycle(cls, obj, obj2, false); + cycle(cls, obj, obj2, true); + } + leak_mark(); + for (int i = 0; i < 100000; i++) { + cycle(cls, obj, obj2, false); + cycle(cls, obj, obj2, true); + } + // allow some slop for side table expansion + // 5120 is common with this configuration + leak_check(6000); + + // rdar://14105994 + id weaks[8]; + for (size_t i = 0; i < sizeof(weaks)/sizeof(weaks[0]); i++) { + objc_storeWeak(&weaks[i], obj); + } + for (size_t i = 0; i < sizeof(weaks)/sizeof(weaks[0]); i++) { + objc_storeWeak(&weaks[i], nil); + } +} + +int main() +{ + test_class([Test class]); + test_class([CustomTryRetain class]); + test_class([CustomIsDeallocating class]); + test_class([CustomAllowsWeakReference class]); + test_class([CustomRetainWeakReference class]); + + + id result; + + Crash *obj3 = [Crash new]; + result = objc_storeWeak(&weak, obj3); + testassert(result == obj3); + testassert(weak == obj3); + + result = objc_storeWeak(&weak2, obj3); + testassert(result == obj3); + testassert(weak2 == obj3); + + [obj3 release]; + fail("should have crashed in -[Crash dealloc]"); +} diff --git a/test/asm-placeholder.s b/test/asm-placeholder.s new file mode 100644 index 0000000..f222644 --- /dev/null +++ b/test/asm-placeholder.s @@ -0,0 +1,47 @@ +.macro NOP16 + nop + nop + nop + nop + nop + nop + nop + nop + nop + nop + nop + nop + nop + nop + nop + nop +.endmacro + +.macro NOP256 + NOP16 + NOP16 + NOP16 + NOP16 + NOP16 + NOP16 + NOP16 + NOP16 + NOP16 + NOP16 + NOP16 + NOP16 + NOP16 + NOP16 + NOP16 + NOP16 +.endmacro + +.text + .globl _main + .align 14 +_main: + // at least 1024 instruction bytes on all architectures + NOP256 + NOP256 + NOP256 + NOP256 diff --git a/test/association-cf.m b/test/association-cf.m new file mode 100644 index 0000000..4ffa2f4 --- /dev/null +++ b/test/association-cf.m @@ -0,0 +1,40 @@ +// TEST_CFLAGS -framework CoreFoundation +// TEST_CONFIG MEM=mrc +// not for ARC because ARC memory management doesn't +// work on CF types whose ObjC side is not yet loaded + +#include +#include + +#include "test.h" + +#if __has_feature(objc_arc) + +int main() +{ + testwarn("rdar://11368528 confused by Foundation"); + succeed(__FILE__); +} + +#else + +int main() +{ + // rdar://6164781 setAssociatedObject on unresolved future class crashes + + id mp = (id)CFMachPortCreate(0, 0, 0, 0); + testassert(mp); + + testassert(! objc_getClass("NSMachPort")); + + objc_setAssociatedObject(mp, (void*)1, mp, OBJC_ASSOCIATION_ASSIGN); + + id obj = objc_getAssociatedObject(mp, (void*)1); + testassert(obj == mp); + + CFRelease((CFTypeRef)mp); + + succeed(__FILE__); +} + +#endif diff --git a/test/association.m b/test/association.m new file mode 100644 index 0000000..e148fc5 --- /dev/null +++ b/test/association.m @@ -0,0 +1,127 @@ +// TEST_CONFIG + +#include "test.h" +#include +#include + +static int values; +static int supers; +static int subs; + +static const char *key = "key"; + + +@interface Value : NSObject @end +@interface Super : NSObject @end +@interface Sub : NSObject @end + +@interface Super2 : NSObject @end +@interface Sub2 : NSObject @end + +@implementation Super +-(id) init +{ + // rdar://8270243 don't lose associations after isa swizzling + + id value = [Value new]; + objc_setAssociatedObject(self, &key, value, OBJC_ASSOCIATION_RETAIN); + RELEASE_VAR(value); + + object_setClass(self, [Sub class]); + + return self; +} + +-(void) dealloc +{ + supers++; + SUPER_DEALLOC(); +} + +@end + +@implementation Sub +-(void) dealloc +{ + subs++; + SUPER_DEALLOC(); +} +@end + +@implementation Super2 +-(id) init +{ + // rdar://9617109 don't lose associations after isa swizzling + + id value = [Value new]; + object_setClass(self, [Sub2 class]); + objc_setAssociatedObject(self, &key, value, OBJC_ASSOCIATION_RETAIN); + RELEASE_VAR(value); + object_setClass(self, [Super2 class]); + + return self; +} + +-(void) dealloc +{ + supers++; + SUPER_DEALLOC(); +} + +@end + +@implementation Sub2 +-(void) dealloc +{ + subs++; + SUPER_DEALLOC(); +} +@end + +@implementation Value +-(void) dealloc { + values++; + SUPER_DEALLOC(); +} +@end + + +int main() +{ + testonthread(^{ + int i; + for (i = 0; i < 100; i++) { + RELEASE_VALUE([[Super alloc] init]); + } + }); + testcollect(); + + testassert(supers == 0); + testassert(subs > 0); + testassert(subs == values); + + + supers = 0; + subs = 0; + values = 0; + + testonthread(^{ + int i; + for (i = 0; i < 100; i++) { + RELEASE_VALUE([[Super2 alloc] init]); + } + }); + testcollect(); + + testassert(supers > 0); + testassert(subs == 0); + testassert(supers == values); + + // rdar://44094390 tolerate nil object and nil value +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wnonnull" + objc_setAssociatedObject(nil, &key, nil, OBJC_ASSOCIATION_ASSIGN); +#pragma clang diagnostic pop + + succeed(__FILE__); +} diff --git a/test/associationForbidden.h b/test/associationForbidden.h new file mode 100644 index 0000000..2ec4de0 --- /dev/null +++ b/test/associationForbidden.h @@ -0,0 +1,50 @@ +#include "testroot.i" + +@interface Normal : TestRoot +@end +@implementation Normal +@end + +@interface Forbidden : TestRoot +@end +@implementation Forbidden +@end + +struct minimal_unrealized_class { + void *isa; + void *superclass; + void *cachePtr; + uintptr_t maskAndOccupied; + struct minimal_class_ro *ro; +}; + +struct minimal_class_ro { + uint32_t flags; +}; + +extern struct minimal_unrealized_class OBJC_CLASS_$_Forbidden; + +#define RO_FORBIDS_ASSOCIATED_OBJECTS (1<<10) + +static void *key = &key; + +static void test(void); + +int main() +{ + struct minimal_unrealized_class *localForbidden = &OBJC_CLASS_$_Forbidden; + localForbidden->ro->flags |= RO_FORBIDS_ASSOCIATED_OBJECTS; + test(); +} + +static inline void ShouldSucceed(id obj) { + objc_setAssociatedObject(obj, key, obj, OBJC_ASSOCIATION_ASSIGN); + id assoc = objc_getAssociatedObject(obj, key); + fprintf(stderr, "Associated object is %p\n", assoc); + testassert(obj == assoc); +} + +static inline void ShouldFail(id obj) { + objc_setAssociatedObject(obj, key, obj, OBJC_ASSOCIATION_ASSIGN); + fail("should have crashed trying to set the associated object"); +} diff --git a/test/associationForbidden.m b/test/associationForbidden.m new file mode 100644 index 0000000..1afac35 --- /dev/null +++ b/test/associationForbidden.m @@ -0,0 +1,16 @@ +// TEST_CRASHES +/* +TEST_RUN_OUTPUT +Associated object is 0x[0-9a-fA-F]+ +objc\[\d+\]: objc_setAssociatedObject called on instance \(0x[0-9a-fA-F]+\) of class Forbidden which does not allow associated objects +objc\[\d+\]: HALTED +END +*/ + +#include "associationForbidden.h" + +void test(void) +{ + ShouldSucceed([Normal alloc]); + ShouldFail([Forbidden alloc]); +} diff --git a/test/associationForbidden2.m b/test/associationForbidden2.m new file mode 100644 index 0000000..9d71ee0 --- /dev/null +++ b/test/associationForbidden2.m @@ -0,0 +1,19 @@ +// TEST_CRASHES +/* +TEST_RUN_OUTPUT +Associated object is 0x[0-9a-fA-F]+ +objc\[\d+\]: objc_setAssociatedObject called on instance \(0x[0-9a-fA-F]+\) of class ForbiddenSubclass which does not allow associated objects +objc\[\d+\]: HALTED +END +*/ + +#include "associationForbidden.h" + +void test(void) +{ + ShouldSucceed([Normal alloc]); + Class ForbiddenSubclass = objc_allocateClassPair([Forbidden class], + "ForbiddenSubclass", 0); + objc_registerClassPair(ForbiddenSubclass); + ShouldFail([ForbiddenSubclass alloc]); +} diff --git a/test/associationForbidden3.m b/test/associationForbidden3.m new file mode 100644 index 0000000..f96ba90 --- /dev/null +++ b/test/associationForbidden3.m @@ -0,0 +1,21 @@ +// TEST_CRASHES +/* +TEST_RUN_OUTPUT +Associated object is 0x[0-9a-fA-F]+ +objc\[\d+\]: objc_setAssociatedObject called on instance \(0x[0-9a-fA-F]+\) of class ForbiddenSubclass which does not allow associated objects +objc\[\d+\]: HALTED +END +*/ + +#include "associationForbidden.h" + +@interface ForbiddenSubclass : Forbidden +@end +@implementation ForbiddenSubclass +@end + +void test(void) +{ + ShouldSucceed([Normal alloc]); + ShouldSucceed([ForbiddenSubclass alloc]); +} diff --git a/test/associationForbidden4.m b/test/associationForbidden4.m new file mode 100644 index 0000000..05370fe --- /dev/null +++ b/test/associationForbidden4.m @@ -0,0 +1,18 @@ +// TEST_CRASHES +/* +TEST_RUN_OUTPUT +Associated object is 0x[0-9a-fA-F]+ +objc\[\d+\]: objc_setAssociatedObject called on instance \(0x[0-9a-fA-F]+\) of class ForbiddenDuplicate which does not allow associated objects +objc\[\d+\]: HALTED +END +*/ + +#include "associationForbidden.h" + +void test(void) +{ + ShouldSucceed([Normal alloc]); + Class ForbiddenDuplicate = objc_duplicateClass([Forbidden class], + "ForbiddenDuplicate", 0); + ShouldFail([ForbiddenDuplicate alloc]); +} diff --git a/test/atomicProperty.mm b/test/atomicProperty.mm new file mode 100644 index 0000000..2625e76 --- /dev/null +++ b/test/atomicProperty.mm @@ -0,0 +1,41 @@ +// TEST_CONFIG + +#include "test.h" +#include +#include +#import + +class SerialNumber { + size_t _number; +public: + SerialNumber() : _number(42) {} + SerialNumber(const SerialNumber &number) : _number(number._number + 1) {} + SerialNumber &operator=(const SerialNumber &number) { _number = number._number + 1; return *this; } + + int operator==(const SerialNumber &number) { return _number == number._number; } + int operator!=(const SerialNumber &number) { return _number != number._number; } +}; + +@interface TestAtomicProperty : NSObject { + SerialNumber number; +} +@property(atomic) SerialNumber number; +@end + +@implementation TestAtomicProperty + +@synthesize number; + +@end + +int main() +{ + PUSH_POOL { + SerialNumber number; + TestAtomicProperty *test = [TestAtomicProperty new]; + test.number = number; + testassert(test.number != number); + } POP_POOL; + + succeed(__FILE__); +} diff --git a/test/badAltHandler.m b/test/badAltHandler.m new file mode 100644 index 0000000..6ea4c76 --- /dev/null +++ b/test/badAltHandler.m @@ -0,0 +1,81 @@ +/* TEST_CONFIG OS=macosx + TEST_CRASHES + +TEST_RUN_OUTPUT +objc\[\d+\]: objc_removeExceptionHandler\(\) called with unknown alt handler; this is probably a bug in multithreaded AppKit use. Set environment variable OBJC_DEBUG_ALT_HANDLERS=YES or break in objc_alt_handler_error\(\) to debug. +objc\[\d+\]: objc_removeExceptionHandler\(\) called with unknown alt handler; this is probably a bug in multithreaded AppKit use. +objc\[\d+\]: HALTED +END +*/ + +#include "test.h" + +#include + +/* + rdar://6888838 + Mail installs an alt handler on one thread and deletes it on another. + This confuses the alt handler machinery, which halts the process. +*/ + +uintptr_t Token; + +void handler(id unused __unused, void *context __unused) +{ +} + +int main() +{ +#if __clang__ && __cplusplus + // alt handlers need the objc personality + // catch (id) workaround forces the objc personality + @try { + testwarn("rdar://9183014 clang uses wrong exception personality"); + } @catch (id e __unused) { + } +#endif + + @try { + // Install 4 alt handlers + uintptr_t t1, t2, t3, t4; + t1 = objc_addExceptionHandler(&handler, NULL); + t2 = objc_addExceptionHandler(&handler, NULL); + t3 = objc_addExceptionHandler(&handler, NULL); + t4 = objc_addExceptionHandler(&handler, NULL); + + // Remove 3 of them. + objc_removeExceptionHandler(t1); + objc_removeExceptionHandler(t2); + objc_removeExceptionHandler(t3); + + // Create an alt handler on another thread + // that collides with one of the removed handlers + testonthread(^{ + @try { + Token = objc_addExceptionHandler(&handler, NULL); + } @catch (...) { + } + }); + + // Incorrectly remove the other thread's handler + objc_removeExceptionHandler(Token); + // Remove the 4th handler + objc_removeExceptionHandler(t4); + + // Install 8 more handlers. + // If the other thread's handler was not ignored, + // this will fail. + objc_addExceptionHandler(&handler, NULL); + objc_addExceptionHandler(&handler, NULL); + objc_addExceptionHandler(&handler, NULL); + objc_addExceptionHandler(&handler, NULL); + objc_addExceptionHandler(&handler, NULL); + objc_addExceptionHandler(&handler, NULL); + objc_addExceptionHandler(&handler, NULL); + objc_addExceptionHandler(&handler, NULL); + } @catch (...) { + } + + // This should have crashed earlier. + fail(__FILE__); +} diff --git a/test/badCache.m b/test/badCache.m new file mode 100644 index 0000000..8b7aa2a --- /dev/null +++ b/test/badCache.m @@ -0,0 +1,116 @@ +/* +TEST_CRASHES +TEST_RUN_OUTPUT +arm +OK: badCache.m +OR +crash now +objc\[\d+\]: Method cache corrupted.* +objc\[\d+\]: .* +objc\[\d+\]: .* +objc\[\d+\]: .* +objc\[\d+\]: .* +objc\[\d+\]: Method cache corrupted.* +objc\[\d+\]: HALTED +END +*/ + + +#include "test.h" + +// Test objc_msgSend's detection of infinite loops during cache scan. + +#if __arm__ + +int main() +{ + testwarn("objc_msgSend on arm doesn't detect infinite loops"); + fprintf(stderr, "arm\n"); + succeed(__FILE__); +} + +#else + +#include "testroot.i" + +#if __LP64__ +typedef uint32_t mask_t; +#else +typedef uint16_t mask_t; +#endif + +struct bucket_t { + uintptr_t sel; + uintptr_t imp; +}; + +struct cache_t { + uintptr_t buckets; + mask_t mask; + mask_t occupied; +}; + +struct class_t { + void *isa; + void *supercls; + struct cache_t cache; +}; + +@interface Subclass : TestRoot @end +@implementation Subclass @end + +int main() +{ + Class cls = [TestRoot class]; + id obj = [cls new]; + [obj self]; + + struct cache_t *cache = &((__bridge struct class_t *)cls)->cache; + + // Figure out which cache mask scheme is in use by examining the existing bits. + int low4 = 0; +#if __LP64__ + int top16 = 0; +#endif + int outlined = 0; + + if (cache->buckets & 0xf) { + low4 = 1; +#if __LP64__ + } else if ((cache->buckets & (0xffffULL << 48))) { + top16 = 1; +#endif + } else { + outlined = 1; + } + +# define COUNT 4 +# define COUNTSHIFT 14 + struct bucket_t *buckets = (struct bucket_t *)calloc(sizeof(struct bucket_t), COUNT+1); + for (int i = 0; i < COUNT; i++) { + buckets[i].sel = ~0; + buckets[i].imp = ~0; + } + buckets[COUNT].sel = 1; + buckets[COUNT].imp = (uintptr_t)buckets; + + if (low4) { + cache->buckets = (uintptr_t)buckets | COUNTSHIFT; +#if __LP64__ + } else if (top16) { + cache->buckets = ((uintptr_t)(COUNT - 1) << 48) | (uintptr_t)buckets; +#endif + } else if (outlined) { + cache->mask = COUNT-1; + cache->buckets = (uintptr_t)buckets; + } + + cache->occupied = 0; + + fprintf(stderr, "crash now\n"); + [obj self]; + + fail("should have crashed"); +} + +#endif diff --git a/test/badPool.m b/test/badPool.m new file mode 100644 index 0000000..fa47e8e --- /dev/null +++ b/test/badPool.m @@ -0,0 +1,34 @@ +// TEST_CONFIG MEM=mrc +// TEST_CRASHES + +// Test badPoolCompat also uses this file. + +/* +TEST_RUN_OUTPUT +objc\[\d+\]: [Ii]nvalid or prematurely-freed autorelease pool 0x[0-9a-fA-F]+\.? +objc\[\d+\]: HALTED +END +*/ + +#include "test.h" + +int main() +{ + void *outer = objc_autoreleasePoolPush(); + void *inner = objc_autoreleasePoolPush(); + objc_autoreleasePoolPop(outer); + objc_autoreleasePoolPop(inner); + +#if !OLD + fail("should have crashed already with new SDK"); +#else + // should only warn once + outer = objc_autoreleasePoolPush(); + inner = objc_autoreleasePoolPush(); + objc_autoreleasePoolPop(outer); + objc_autoreleasePoolPop(inner); + + succeed(__FILE__); +#endif +} + diff --git a/test/badPoolCompat-ios-tvos.m b/test/badPoolCompat-ios-tvos.m new file mode 100644 index 0000000..5f1b92c --- /dev/null +++ b/test/badPoolCompat-ios-tvos.m @@ -0,0 +1,14 @@ +// Run test badPool as if it were built with an old SDK. + +// TEST_CONFIG MEM=mrc OS=iphoneos,iphonesimulator,appletvos,appletvsimulator +// TEST_CRASHES +// TEST_CFLAGS -DOLD=1 -Xlinker -sdk_version -Xlinker 9.0 + +/* +TEST_RUN_OUTPUT +objc\[\d+\]: Invalid or prematurely-freed autorelease pool 0x[0-9a-fA-f]+\. Set a breakpoint .* Proceeding anyway .* +OK: badPool.m +END +*/ + +#include "badPool.m" diff --git a/test/badPoolCompat-macos.m b/test/badPoolCompat-macos.m new file mode 100644 index 0000000..afd2117 --- /dev/null +++ b/test/badPoolCompat-macos.m @@ -0,0 +1,14 @@ +// Run test badPool as if it were built with an old SDK. + +// TEST_CONFIG MEM=mrc OS=macosx +// TEST_CRASHES +// TEST_CFLAGS -DOLD=1 -Xlinker -sdk_version -Xlinker 10.11 + +/* +TEST_RUN_OUTPUT +objc\[\d+\]: Invalid or prematurely-freed autorelease pool 0x[0-9a-fA-f]+\. Set a breakpoint .* Proceeding anyway .* +OK: badPool.m +END +*/ + +#include "badPool.m" diff --git a/test/badPoolCompat-watchos.m b/test/badPoolCompat-watchos.m new file mode 100644 index 0000000..6e89e44 --- /dev/null +++ b/test/badPoolCompat-watchos.m @@ -0,0 +1,14 @@ +// Run test badPool as if it were built with an old SDK. + +// TEST_CONFIG MEM=mrc OS=watchos,watchsimulator +// TEST_CRASHES +// TEST_CFLAGS -DOLD=1 -Xlinker -sdk_version -Xlinker 2.0 + +/* +TEST_RUN_OUTPUT +objc\[\d+\]: Invalid or prematurely-freed autorelease pool 0x[0-9a-fA-f]+\. Set a breakpoint .* Proceeding anyway .* +OK: badPool.m +END +*/ + +#include "badPool.m" diff --git a/test/badSuperclass.m b/test/badSuperclass.m new file mode 100644 index 0000000..2fa0bc7 --- /dev/null +++ b/test/badSuperclass.m @@ -0,0 +1,37 @@ +// TEST_CRASHES +/* +TEST_RUN_OUTPUT +objc\[\d+\]: Memory corruption in class list\. +objc\[\d+\]: HALTED +END +*/ + +#include "test.h" +#include "testroot.i" + +@interface Super : TestRoot @end +@implementation Super @end + +@interface Sub : Super @end +@implementation Sub @end + +int main() +{ + alarm(10); + + Class supercls = [Super class]; + Class subcls = [Sub class]; + id subobj __unused = [Sub alloc]; + + // Create a cycle in a superclass chain (Sub->supercls == Sub) + // then attempt to walk that chain. Runtime should halt eventually. + _objc_flush_caches(supercls); + ((Class *)(__bridge void *)subcls)[1] = subcls; +#ifdef CACHE_FLUSH + _objc_flush_caches(supercls); +#else + [subobj class]; +#endif + + fail("should have crashed"); +} diff --git a/test/badSuperclass2.m b/test/badSuperclass2.m new file mode 100644 index 0000000..a75f053 --- /dev/null +++ b/test/badSuperclass2.m @@ -0,0 +1,13 @@ +// TEST_CRASHES +/* +TEST_RUN_OUTPUT +objc\[\d+\]: Memory corruption in class list\. +objc\[\d+\]: HALTED +OR +old abi +OK: badSuperclass\.m +END +*/ + +#define CACHE_FLUSH +#include "badSuperclass.m" diff --git a/test/badTagClass.m b/test/badTagClass.m new file mode 100644 index 0000000..2a08ebd --- /dev/null +++ b/test/badTagClass.m @@ -0,0 +1,48 @@ +/* +TEST_CRASHES +TEST_BUILD_OUTPUT +.*badTagClass.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +END +TEST_RUN_OUTPUT +objc\[\d+\]: tag index 1 used for two different classes \(was 0x[0-9a-fA-F]+ NSObject, now 0x[0-9a-fA-F]+ TestRoot\) +objc\[\d+\]: HALTED +OR +no tagged pointers +OK: badTagClass.m +END +*/ + +#include "test.h" +#include "testroot.i" + +#include +#include + +#if OBJC_HAVE_TAGGED_POINTERS + +int main() +{ + // re-registration and nil registration allowed + _objc_registerTaggedPointerClass(OBJC_TAG_1, [NSObject class]); + _objc_registerTaggedPointerClass(OBJC_TAG_1, [NSObject class]); + _objc_registerTaggedPointerClass(OBJC_TAG_1, nil); + _objc_registerTaggedPointerClass(OBJC_TAG_1, [NSObject class]); + + // colliding registration disallowed + _objc_registerTaggedPointerClass(OBJC_TAG_1, [TestRoot class]); + + fail(__FILE__); +} + +#else + +int main() +{ + // provoke the same nullability warning as the real test + objc_getClass(nil); + + fprintf(stderr, "no tagged pointers\n"); + succeed(__FILE__); +} + +#endif diff --git a/test/badTagIndex.m b/test/badTagIndex.m new file mode 100644 index 0000000..c4bfd33 --- /dev/null +++ b/test/badTagIndex.m @@ -0,0 +1,33 @@ +/* +TEST_CRASHES +TEST_RUN_OUTPUT +objc\[\d+\]: tag index 264 is invalid +objc\[\d+\]: HALTED +OR +no tagged pointers +OK: badTagIndex.m +END +*/ + +#include "test.h" + +#include +#include + +#if OBJC_HAVE_TAGGED_POINTERS + +int main() +{ + _objc_registerTaggedPointerClass((objc_tag_index_t)(OBJC_TAG_Last52BitPayload+1), [NSObject class]); + fail(__FILE__); +} + +#else + +int main() +{ + fprintf(stderr, "no tagged pointers\n"); + succeed(__FILE__); +} + +#endif diff --git a/test/bigrc.m b/test/bigrc.m new file mode 100644 index 0000000..419bbb6 --- /dev/null +++ b/test/bigrc.m @@ -0,0 +1,139 @@ +// TEST_CONFIG MEM=mrc +/* +TEST_RUN_OUTPUT +objc\[\d+\]: Deallocator object 0x[0-9a-fA-F]+ overreleased while already deallocating; break on objc_overrelease_during_dealloc_error to debug +OK: bigrc.m +OR +no overrelease enforcement +OK: bigrc.m +END + */ + +#include "test.h" +#include "testroot.i" + +static size_t LOTS; + +@interface Deallocator : TestRoot @end +@implementation Deallocator + +-(void)dealloc +{ + id o = self; + size_t rc = 1; + + + testprintf("Retain a lot during dealloc\n"); + + testassert(rc == 1); + testassert([o retainCount] == rc); + do { + [o retain]; + if (rc % 0x100000 == 0) testprintf("%zx/%zx ++\n", rc, LOTS); + } while (++rc < LOTS); + + testassert([o retainCount] == rc); + + do { + [o release]; + if (rc % 0x100000 == 0) testprintf("%zx/%zx --\n", rc, LOTS); + } while (--rc > 1); + + testassert(rc == 1); + testassert([o retainCount] == rc); + + + testprintf("Overrelease during dealloc\n"); + + // Not all architectures enforce this. +#if !SUPPORT_NONPOINTER_ISA + testwarn("no overrelease enforcement"); + fprintf(stderr, "no overrelease enforcement\n"); +#endif + [o release]; + + [super dealloc]; +} + +@end + +size_t clz(uintptr_t isa) { + if (sizeof(uintptr_t) == 4) + return __builtin_clzl(isa); + testassert(sizeof(uintptr_t) == 8); + return __builtin_clzll(isa); +} + +int main() +{ + Deallocator *o = [Deallocator new]; + size_t rc = 1; + + [o retain]; + + uintptr_t isa = *(uintptr_t *)o; + if (isa & 1) { + // Assume refcount in high bits. + LOTS = 1 << (4 + clz(isa)); + testprintf("LOTS %zu via cntlzw\n", LOTS); + } else { + LOTS = 0x1000000; + testprintf("LOTS %zu via guess\n", LOTS); + } + + [o release]; + + + testprintf("Retain a lot\n"); + + testassert(rc == 1); + testassert([o retainCount] == rc); + do { + [o retain]; + if (rc % 0x100000 == 0) testprintf("%zx/%zx ++\n", rc, LOTS); + } while (++rc < LOTS); + + testassert([o retainCount] == rc); + + do { + [o release]; + if (rc % 0x100000 == 0) testprintf("%zx/%zx --\n", rc, LOTS); + } while (--rc > 1); + + testassert(rc == 1); + testassert([o retainCount] == rc); + + + testprintf("tryRetain a lot\n"); + + id w; + objc_storeWeak(&w, o); + testassert(w == o); + + testassert(rc == 1); + testassert([o retainCount] == rc); + do { + objc_loadWeakRetained(&w); + if (rc % 0x100000 == 0) testprintf("%zx/%zx ++\n", rc, LOTS); + } while (++rc < LOTS); + + testassert([o retainCount] == rc); + + do { + [o release]; + if (rc % 0x100000 == 0) testprintf("%zx/%zx --\n", rc, LOTS); + } while (--rc > 1); + + testassert(rc == 1); + testassert([o retainCount] == rc); + + testprintf("dealloc\n"); + + testassert(TestRootDealloc == 0); + testassert(w != nil); + [o release]; + testassert(TestRootDealloc == 1); + testassert(w == nil); + + succeed(__FILE__); +} diff --git a/test/blocksAsImps.m b/test/blocksAsImps.m new file mode 100644 index 0000000..2ba2580 --- /dev/null +++ b/test/blocksAsImps.m @@ -0,0 +1,252 @@ +// TEST_CFLAGS -framework Foundation + +#include "test.h" +#include +#import + +#include + +#if !__has_feature(objc_arc) +# define __bridge +#endif + +#if __arm64__ + // stret supported, but is identical to non-stret +# define STRET_SPECIAL 0 +#else + // stret supported and distinct from non-stret +# define STRET_SPECIAL 1 +#endif + +typedef struct BigStruct { + uintptr_t datums[200]; +} BigStruct; + +@interface Foo:NSObject +@end +@implementation Foo +- (BigStruct) methodThatReturnsBigStruct: (BigStruct) b +{ + return b; +} +@end + +@interface Foo(bar) +- (int) boo: (int) a; +- (BigStruct) structThatIsBig: (BigStruct) b; +- (BigStruct) methodThatReturnsBigStruct: (BigStruct) b; +- (float) methodThatReturnsFloat: (float) aFloat; +@end + +// This is void* instead of id to prevent interference from ARC. +typedef uintptr_t (*FuncPtr)(void *, SEL); +typedef BigStruct (*BigStructFuncPtr)(id, SEL, BigStruct); +typedef float (*FloatFuncPtr)(id, SEL, float); + +BigStruct bigfunc(BigStruct a) { + return a; +} + +@interface Deallocator : NSObject @end +@implementation Deallocator +-(void) methodThatNobodyElseCalls1 { } +-(void) methodThatNobodyElseCalls2 { } +id retain_imp(Deallocator *self, SEL _cmd) { + _objc_flush_caches([Deallocator class]); + [self methodThatNobodyElseCalls1]; + struct objc_super sup = { self, [[Deallocator class] superclass] }; + return ((id(*)(struct objc_super *, SEL))objc_msgSendSuper)(&sup, _cmd); +} +void dealloc_imp(Deallocator *self, SEL _cmd) { + _objc_flush_caches([Deallocator class]); + [self methodThatNobodyElseCalls2]; + struct objc_super sup = { self, [[Deallocator class] superclass] }; + ((void(*)(struct objc_super *, SEL))objc_msgSendSuper)(&sup, _cmd); +} ++(void) load { + class_addMethod(self, sel_registerName("retain"), (IMP)retain_imp, ""); + class_addMethod(self, sel_registerName("dealloc"), (IMP)dealloc_imp, ""); +} +@end + +/* Code copied from objc-block-trampolines.m to test Block innards */ +typedef enum { + ReturnValueInRegisterArgumentMode, +#if STRET_SPECIAL + ReturnValueOnStackArgumentMode, +#endif + + ArgumentModeMax +} ArgumentMode; + +static ArgumentMode _argumentModeForBlock(id block) { + ArgumentMode aMode = ReturnValueInRegisterArgumentMode; +#if STRET_SPECIAL + if ( _Block_use_stret((__bridge void *)block) ) + aMode = ReturnValueOnStackArgumentMode; +#else + testassert(!_Block_use_stret((__bridge void *)block)); +#endif + + return aMode; +} +/* End copied code */ + +int main () { + // make sure the bits are in place + int (^registerReturn)() = ^(){ return 42; }; + ArgumentMode aMode; + + aMode = _argumentModeForBlock(registerReturn); + testassert(aMode == ReturnValueInRegisterArgumentMode); + + BigStruct (^stackReturn)() = ^() { BigStruct k = {{0}}; return k; }; + aMode = _argumentModeForBlock(stackReturn); +#if STRET_SPECIAL + testassert(aMode == ReturnValueOnStackArgumentMode); +#else + testassert(aMode == ReturnValueInRegisterArgumentMode); +#endif + + uintptr_t TEST_QUANTITY = is_guardmalloc() ? 1000 : 100000; + FuncPtr funcArray[TEST_QUANTITY]; + + for(uintptr_t i = 0; i < TEST_QUANTITY; i++) { + uintptr_t (^block)(void *self) = ^uintptr_t(void *self) { + testassert(i == (uintptr_t)self); + return i; + }; + block = (__bridge id)_Block_copy((__bridge void *)block); + + funcArray[i] = (FuncPtr) imp_implementationWithBlock(block); + + testassert(block((void *)i) == i); + + id blockFromIMPResult = imp_getBlock((IMP)funcArray[i]); + testassert(blockFromIMPResult == (id)block); + + _Block_release((__bridge void *)block); + } + + for(uintptr_t i = 0; i < TEST_QUANTITY; i++) { + uintptr_t result = funcArray[i]((void *)i, 0); + testassert(i == result); + } + + for(uintptr_t i = 0; i < TEST_QUANTITY; i = i + 3) { + imp_removeBlock((IMP)funcArray[i]); + id shouldBeNull = imp_getBlock((IMP)funcArray[i]); + testassert(shouldBeNull == NULL); + } + + for(uintptr_t i = 0; i < TEST_QUANTITY; i = i + 3) { + uintptr_t j = i * i; + + uintptr_t (^block)(void *) = ^uintptr_t(void *self) { + testassert(j == (uintptr_t)self); + return j; + }; + funcArray[i] = (FuncPtr) imp_implementationWithBlock(block); + + testassert(block((void *)j) == j); + testassert(funcArray[i]((void *)j, 0) == j); + } + + for(uintptr_t i = 0; i < TEST_QUANTITY; i = i + 3) { + uintptr_t j = i * i; + uintptr_t result = funcArray[i]((void *)j, 0); + testassert(j == result); + } + + int (^implBlock)(id, int); + + implBlock = ^(id self __attribute__((unused)), int a){ + return -1 * a; + }; + + PUSH_POOL { + + IMP methodImp = imp_implementationWithBlock(implBlock); + + BOOL success = class_addMethod([Foo class], @selector(boo:), methodImp, "i@:i"); + testassert(success); + + Foo *f = [Foo new]; + int (*impF)(id self, SEL _cmd, int x) = (int(*)(id, SEL, int)) [Foo instanceMethodForSelector: @selector(boo:)]; + + int x = impF(f, @selector(boo:), -42); + + testassert(x == 42); + testassert([f boo: -42] == 42); + + BigStruct a; + for (uintptr_t i = 0; i < 200; i++) { + a.datums[i] = i; + } + + // slightly more straightforward here + __block unsigned int state = 0; + BigStruct (^structBlock)(id, BigStruct) = ^BigStruct(id self __attribute__((unused)), BigStruct c) { + state++; + return c; + }; + BigStruct blockDirect = structBlock(nil, a); + testassert(!memcmp(&a, &blockDirect, sizeof(BigStruct))); + testassert(state==1); + + IMP bigStructIMP = imp_implementationWithBlock(structBlock); + + class_addMethod([Foo class], @selector(structThatIsBig:), bigStructIMP, "oh, type strings, how I hate thee. Fortunately, the runtime doesn't generally care."); + + BigStruct b; + + BigStructFuncPtr bFunc; + + b = bigfunc(a); + testassert(!memcmp(&a, &b, sizeof(BigStruct))); + b = bigfunc(a); + testassert(!memcmp(&a, &b, sizeof(BigStruct))); + + bFunc = (BigStructFuncPtr) [Foo instanceMethodForSelector: @selector(methodThatReturnsBigStruct:)]; + + b = bFunc(f, @selector(methodThatReturnsBigStruct:), a); + testassert(!memcmp(&a, &b, sizeof(BigStruct))); + + b = [f methodThatReturnsBigStruct: a]; + testassert(!memcmp(&a, &b, sizeof(BigStruct))); + + bFunc = (BigStructFuncPtr) [Foo instanceMethodForSelector: @selector(structThatIsBig:)]; + + b = bFunc(f, @selector(structThatIsBig:), a); + testassert(!memcmp(&a, &b, sizeof(BigStruct))); + testassert(state==2); + + b = [f structThatIsBig: a]; + testassert(!memcmp(&a, &b, sizeof(BigStruct))); + testassert(state==3); + + + IMP floatIMP = imp_implementationWithBlock(^float (id self __attribute__((unused)), float aFloat ) { + return aFloat; + }); + class_addMethod([Foo class], @selector(methodThatReturnsFloat:), floatIMP, "ooh.. type string unspecified again... oh noe... runtime might punish. not."); + + float e = (float)0.001; + float retF = (float)[f methodThatReturnsFloat: 37.1212f]; + testassert( ((retF - e) < 37.1212) && ((retF + e) > 37.1212) ); + + +#if !__has_feature(objc_arc) + // Make sure imp_implementationWithBlock() and imp_removeBlock() + // don't deadlock while calling Block_copy() and Block_release() + Deallocator *dead = [[Deallocator alloc] init]; + IMP deadlockImp = imp_implementationWithBlock(^{ [dead self]; }); + [dead release]; + imp_removeBlock(deadlockImp); +#endif + + } POP_POOL; + + succeed(__FILE__); +} + diff --git a/test/bool.c b/test/bool.c new file mode 100644 index 0000000..c12cc32 --- /dev/null +++ b/test/bool.c @@ -0,0 +1,45 @@ +// TEST_CFLAGS -funsigned-char +// (verify -funsigned-char doesn't change the definition of BOOL) + +#include "test.h" +#include + +#if TARGET_OS_OSX +# define RealBool 0 +#elif TARGET_OS_IOS || TARGET_OS_BRIDGE +# if (__arm__ && !__armv7k__) || __i386__ +# define RealBool 0 +# else +# define RealBool 1 +# endif +#else +# define RealBool 1 +#endif + +#if __OBJC__ && !defined(__OBJC_BOOL_IS_BOOL) +# error no __OBJC_BOOL_IS_BOOL +#endif + +#if RealBool != OBJC_BOOL_IS_BOOL +# error wrong OBJC_BOOL_IS_BOOL +#endif + +#if RealBool == OBJC_BOOL_IS_CHAR +# error wrong OBJC_BOOL_IS_CHAR +#endif + +int main() +{ + const char *expected __unused = +#if RealBool + "B" +#else + "c" +#endif + ; +#if __OBJC__ + const char *enc = @encode(BOOL); + testassert(0 == strcmp(enc, expected)); +#endif + succeed(__FILE__); +} diff --git a/test/cacheflush.h b/test/cacheflush.h new file mode 100644 index 0000000..5553dbb --- /dev/null +++ b/test/cacheflush.h @@ -0,0 +1,7 @@ +#include +#include "test.h" + +@interface TestRoot(cat) ++(int)classMethod; +-(int)instanceMethod; +@end diff --git a/test/cacheflush.m b/test/cacheflush.m new file mode 100644 index 0000000..2bc21ae --- /dev/null +++ b/test/cacheflush.m @@ -0,0 +1,61 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/cacheflush0.m -o cacheflush0.dylib -dynamiclib + $C{COMPILE} $DIR/cacheflush2.m -x none cacheflush0.dylib -o cacheflush2.dylib -dynamiclib + $C{COMPILE} $DIR/cacheflush3.m -x none cacheflush0.dylib -o cacheflush3.dylib -dynamiclib + $C{COMPILE} $DIR/cacheflush.m -x none cacheflush0.dylib -o cacheflush.exe +END +*/ + +#include "test.h" +#include +#include + +#include "cacheflush.h" + +@interface Sub : TestRoot @end +@implementation Sub @end + + +int main() +{ + TestRoot *sup = [TestRoot new]; + Sub *sub = [Sub new]; + + // Fill method cache + testassert(1 == [TestRoot classMethod]); + testassert(1 == [sup instanceMethod]); + testassert(1 == [TestRoot classMethod]); + testassert(1 == [sup instanceMethod]); + + testassert(1 == [Sub classMethod]); + testassert(1 == [sub instanceMethod]); + testassert(1 == [Sub classMethod]); + testassert(1 == [sub instanceMethod]); + + // Dynamically load a category + dlopen("cacheflush2.dylib", 0); + + // Make sure old cache results are gone + testassert(2 == [TestRoot classMethod]); + testassert(2 == [sup instanceMethod]); + + testassert(2 == [Sub classMethod]); + testassert(2 == [sub instanceMethod]); + + // Dynamically load another category + dlopen("cacheflush3.dylib", 0); + + // Make sure old cache results are gone + testassert(3 == [TestRoot classMethod]); + testassert(3 == [sup instanceMethod]); + + testassert(3 == [Sub classMethod]); + testassert(3 == [sub instanceMethod]); + + // fixme test subclasses + + // fixme test objc_flush_caches(), class_addMethod(), class_addMethods() + + succeed(__FILE__); +} diff --git a/test/cacheflush0.m b/test/cacheflush0.m new file mode 100644 index 0000000..8536071 --- /dev/null +++ b/test/cacheflush0.m @@ -0,0 +1,7 @@ +#include "cacheflush.h" +#include "testroot.i" + +@implementation TestRoot(cat) ++(int)classMethod { return 1; } +-(int)instanceMethod { return 1; } +@end diff --git a/test/cacheflush2.m b/test/cacheflush2.m new file mode 100644 index 0000000..0337e3f --- /dev/null +++ b/test/cacheflush2.m @@ -0,0 +1,6 @@ +#include "cacheflush.h" + +@implementation TestRoot (Category2) ++(int)classMethod { return 2; } +-(int)instanceMethod { return 2; } +@end diff --git a/test/cacheflush3.m b/test/cacheflush3.m new file mode 100644 index 0000000..c180e98 --- /dev/null +++ b/test/cacheflush3.m @@ -0,0 +1,6 @@ +#include "cacheflush.h" + +@implementation TestRoot (Category3) ++(int)classMethod { return 3; } +-(int)instanceMethod { return 3; } +@end diff --git a/test/category.m b/test/category.m new file mode 100644 index 0000000..80795e2 --- /dev/null +++ b/test/category.m @@ -0,0 +1,229 @@ +// TEST_CFLAGS -Wl,-no_objc_category_merging + +#include "test.h" +#include "testroot.i" +#include +#include + +#if __LP64__ +# define PTR " .quad " +#else +# define PTR " .long " +#endif + +static int state = 0; + +@interface Super : TestRoot @end +@implementation Super +-(void)instancemethod { fail("-instancemethod not overridden by category"); } ++(void)method { fail("+method not overridden by category"); } +@end + +@interface Super (Category) @end +@implementation Super (Category) ++(void)method { + testprintf("in [Super(Category) method]\n"); + testassert(self == [Super class]); + testassert(state == 0); + state = 1; +} +-(void)instancemethod { + testprintf("in [Super(Category) instancemethod]\n"); + testassert(object_getClass(self) == [Super class]); + testassert(state == 1); + state = 2; +} +@end + +@interface Super (PropertyCategory) +@property int i; +@property(class) int i; +@end +@implementation Super (PropertyCategory) +- (int)i { return 0; } +- (void)setI:(int)value { (void)value; } ++ (int)i { return 0; } ++ (void)setI:(int)value { (void)value; } +@end + +// rdar://5086110 memory smasher in category with class method and property +@interface Super (r5086110) +@property int property5086110; +@end +@implementation Super (r5086110) ++(void)method5086110 { + fail("method method5086110 called!"); +} +- (int)property5086110 { fail("property5086110 called!"); return 0; } +- (void)setProperty5086110:(int)value { fail("setProperty5086110 called!"); (void)value; } +@end + +// rdar://25605427 incorrect handling of class properties in 10.11 and earlier +@interface Super25605427 : TestRoot +@property(class, readonly) int i; +@end +@implementation Super25605427 ++(int)i { return 0; } +@end + +@interface Super25605427 (r25605427a) +@property(readonly) int r25605427a1; +@end +@implementation Super25605427 (r25605427a) +-(int)r25605427a1 { return 0; } ++(int)r25605427a2 { return 0; } +@end + +@interface Super25605427 (r25605427b) +@property(readonly) int r25605427b1; +@end +@implementation Super25605427 (r25605427b) +-(int)r25605427b1 { return 0; } ++(int)r25605427b2 { return 0; } +@end + +@interface Super25605427 (r25605427c) +@property(readonly) int r25605427c1; +@end +@implementation Super25605427 (r25605427c) +-(int)r25605427c1 { return 0; } ++(int)r25605427c2 { return 0; } +@end + +@interface Super25605427 (r25605427d) +@property(readonly) int r25605427d1; +@end +@implementation Super25605427 (r25605427d) +-(int)r25605427d1 { return 0; } ++(int)r25605427d2 { return 0; } +@end + + +@interface PropertyClass : Super { + int q; +} +@property(readonly) int q; +@end +@implementation PropertyClass +@synthesize q; +@end + +@interface PropertyClass (PropertyCategory) +@property int q; +@end +@implementation PropertyClass (PropertyCategory) +@dynamic q; +@end + + +// Manually build a category that goes in __objc_catlist2. +#if __has_feature(ptrauth_calls) +#define SIGNED_CATEGORY_IMP "@AUTH(ia,0,addr)" +#else +#define SIGNED_CATEGORY_IMP +#endif +asm( +" .section __DATA,__objc_const \n" +"L_catlist2CategoryName: \n" +" .asciz \"Category_catlist2\" \n" +"L_catlist2MethodString: \n" +" .asciz \"catlist2Method\" \n" +"L_catlist2MethodTypes: \n" +" .asciz \"i16@0:8\" \n" + +" .p2align 3 \n" +"l_OBJC_$_CATEGORY_INSTANCE_METHODS_Super_$_Category_catlist2: \n" +" .long 24 \n" +" .long 1 \n" +" "PTR" L_catlist2MethodString \n" +" "PTR" L_catlist2MethodTypes \n" +" "PTR" _catlist2MethodImplementation"SIGNED_CATEGORY_IMP" \n" + +" .p2align 3 \n" +"l_OBJC_$_CATEGORY_Super_$_Category_catlist2: \n" +" "PTR" L_catlist2CategoryName \n" +" "PTR" _OBJC_CLASS_$_Super \n" +" "PTR" l_OBJC_$_CATEGORY_INSTANCE_METHODS_Super_$_Category_catlist2 \n" +" "PTR" 0 \n" +" "PTR" 0 \n" +" "PTR" 0 \n" +" "PTR" 0 \n" +" .long 64 \n" +" .space 4 \n" + +" .section __DATA,__objc_catlist2 \n" +" .p2align 3 \n" +" "PTR" l_OBJC_$_CATEGORY_Super_$_Category_catlist2 \n" + +" .text \n" +); + +@interface Super (Category_catlist2) +- (int)catlist2Method; +@end + +EXTERN_C int catlist2MethodImplementation(id self __unused, SEL _cmd __unused) { + return 0; +} + + +int main() +{ + { + // rdar://25605427 bugs in 10.11 and earlier when metaclass + // has a property and category has metaclass additions. + // Memory smasher in buildPropertyList (caught by guard malloc) + Class cls = [Super25605427 class]; + // Incorrect attachment of instance properties from category to metacls + testassert(class_getProperty(cls, "r25605427d1")); + testassert(! class_getProperty(object_getClass(cls), "r25605427d1")); + } + + // methods introduced by category + state = 0; + [Super method]; + [[Super new] instancemethod]; + testassert(state == 2); + + // property introduced by category + objc_property_t p = class_getProperty([Super class], "i"); + testassert(p); + testassert(0 == strcmp(property_getName(p), "i")); + testassert(property_getAttributes(p)); + + objc_property_t p2 = class_getProperty(object_getClass([Super class]), "i"); + testassert(p2); + testassert(p != p2); + testassert(0 == strcmp(property_getName(p2), "i")); + testassert(property_getAttributes(p2)); + + // methods introduced by category's property + Method m; + m = class_getInstanceMethod([Super class], @selector(i)); + testassert(m); + m = class_getInstanceMethod([Super class], @selector(setI:)); + testassert(m); + + m = class_getClassMethod([Super class], @selector(i)); + testassert(m); + m = class_getClassMethod([Super class], @selector(setI:)); + testassert(m); + + // class's property shadowed by category's property + objc_property_t *plist = class_copyPropertyList([PropertyClass class], NULL); + testassert(plist); + testassert(plist[0]); + testassert(0 == strcmp(property_getName(plist[0]), "q")); + testassert(0 == strcmp(property_getAttributes(plist[0]), "Ti,D")); + testassert(plist[1]); + testassert(0 == strcmp(property_getName(plist[1]), "q")); + testassert(0 == strcmp(property_getAttributes(plist[1]), "Ti,R,Vq")); + testassert(!plist[2]); + free(plist); + + // method introduced by category in catlist2 + testassert([[Super new] catlist2Method] == 0); + + succeed(__FILE__); +} + diff --git a/test/cdtors.mm b/test/cdtors.mm new file mode 100644 index 0000000..be2a7d1 --- /dev/null +++ b/test/cdtors.mm @@ -0,0 +1,306 @@ +// TEST_CONFIG + +#if USE_FOUNDATION +#include +#define SUPERCLASS NSObject +#define FILENAME "nscdtors.mm" +#else +#define SUPERCLASS TestRoot +#define FILENAME "cdtors.mm" +#endif + +#include "test.h" + +#include +#include "objc/objc-internal.h" +#include "testroot.i" + +static unsigned ctors1 = 0; +static unsigned dtors1 = 0; +static unsigned ctors2 = 0; +static unsigned dtors2 = 0; + +class cxx1 { + unsigned & ctors; + unsigned& dtors; + + public: + cxx1() : ctors(ctors1), dtors(dtors1) { ctors++; } + + ~cxx1() { dtors++; } +}; +class cxx2 { + unsigned& ctors; + unsigned& dtors; + + public: + cxx2() : ctors(ctors2), dtors(dtors2) { ctors++; } + + ~cxx2() { dtors++; } +}; + +/* + Class hierarchy: + TestRoot + CXXBase + NoCXXSub + CXXSub + + This has two cxx-wielding classes, and a class in between without cxx. +*/ + + +@interface CXXBase : SUPERCLASS { + cxx1 baseIvar; +} +@end +@implementation CXXBase @end + +@interface NoCXXSub : CXXBase { + int nocxxIvar; +} +@end +@implementation NoCXXSub @end + +@interface CXXSub : NoCXXSub { + cxx2 subIvar; +} +@end +@implementation CXXSub @end + + +void test_single(void) +{ + // Single allocation + + ctors1 = dtors1 = ctors2 = dtors2 = 0; + testonthread(^{ + id o = [TestRoot new]; + testassert(ctors1 == 0 && dtors1 == 0 && + ctors2 == 0 && dtors2 == 0); + testassert([o class] == [TestRoot class]); + RELEASE_VAR(o); + }); + testcollect(); + testassert(ctors1 == 0 && dtors1 == 0 && + ctors2 == 0 && dtors2 == 0); + + ctors1 = dtors1 = ctors2 = dtors2 = 0; + testonthread(^{ + id o = [CXXBase new]; + testassert(ctors1 == 1 && dtors1 == 0 && + ctors2 == 0 && dtors2 == 0); + testassert([o class] == [CXXBase class]); + RELEASE_VAR(o); + }); + testcollect(); + testassert(ctors1 == 1 && dtors1 == 1 && + ctors2 == 0 && dtors2 == 0); + + ctors1 = dtors1 = ctors2 = dtors2 = 0; + testonthread(^{ + id o = [NoCXXSub new]; + testassert(ctors1 == 1 && dtors1 == 0 && + ctors2 == 0 && dtors2 == 0); + testassert([o class] == [NoCXXSub class]); + RELEASE_VAR(o); + }); + testcollect(); + testassert(ctors1 == 1 && dtors1 == 1 && + ctors2 == 0 && dtors2 == 0); + + ctors1 = dtors1 = ctors2 = dtors2 = 0; + testonthread(^{ + id o = [CXXSub new]; + testassert(ctors1 == 1 && dtors1 == 0 && + ctors2 == 1 && dtors2 == 0); + testassert([o class] == [CXXSub class]); + RELEASE_VAR(o); + }); + testcollect(); + testassert(ctors1 == 1 && dtors1 == 1 && + ctors2 == 1 && dtors2 == 1); +} + +void test_inplace(void) +{ + __unsafe_unretained volatile id o; + char o2[64]; + + id (*objc_constructInstance_fn)(Class, void*) = (id(*)(Class, void*))dlsym(RTLD_DEFAULT, "objc_constructInstance"); + void (*objc_destructInstance_fn)(id) = (void(*)(id))dlsym(RTLD_DEFAULT, "objc_destructInstance"); + + // In-place allocation + + ctors1 = dtors1 = ctors2 = dtors2 = 0; + o = objc_constructInstance_fn([TestRoot class], o2); + testassert(ctors1 == 0 && dtors1 == 0 && + ctors2 == 0 && dtors2 == 0); + testassert([o class] == [TestRoot class]); + objc_destructInstance_fn(o), o = nil; + testcollect(); + testassert(ctors1 == 0 && dtors1 == 0 && + ctors2 == 0 && dtors2 == 0); + + ctors1 = dtors1 = ctors2 = dtors2 = 0; + o = objc_constructInstance_fn([CXXBase class], o2); + testassert(ctors1 == 1 && dtors1 == 0 && + ctors2 == 0 && dtors2 == 0); + testassert([o class] == [CXXBase class]); + objc_destructInstance_fn(o), o = nil; + testcollect(); + testassert(ctors1 == 1 && dtors1 == 1 && + ctors2 == 0 && dtors2 == 0); + + ctors1 = dtors1 = ctors2 = dtors2 = 0; + o = objc_constructInstance_fn([NoCXXSub class], o2); + testassert(ctors1 == 1 && dtors1 == 0 && + ctors2 == 0 && dtors2 == 0); + testassert([o class] == [NoCXXSub class]); + objc_destructInstance_fn(o), o = nil; + testcollect(); + testassert(ctors1 == 1 && dtors1 == 1 && + ctors2 == 0 && dtors2 == 0); + + ctors1 = dtors1 = ctors2 = dtors2 = 0; + o = objc_constructInstance_fn([CXXSub class], o2); + testassert(ctors1 == 1 && dtors1 == 0 && + ctors2 == 1 && dtors2 == 0); + testassert([o class] == [CXXSub class]); + objc_destructInstance_fn(o), o = nil; + testcollect(); + testassert(ctors1 == 1 && dtors1 == 1 && + ctors2 == 1 && dtors2 == 1); +} + + +#if __has_feature(objc_arc) + +void test_batch(void) +{ + // not converted to ARC yet + return; +} + +#else + +// Like class_createInstances(), but refuses to accept zero allocations +static unsigned +reallyCreateInstances(Class cls, size_t extraBytes, id *dst, unsigned want) +{ + unsigned count; + while (0 == (count = class_createInstances(cls, extraBytes, dst, want))) { + testprintf("class_createInstances created nothing; retrying\n"); + RELEASE_VALUE([[TestRoot alloc] init]); + } + return count; +} + +void test_batch(void) +{ + id o2[100]; + unsigned int count, i; + + // Batch allocation + + for (i = 0; i < 100; i++) { + o2[i] = (id)malloc(class_getInstanceSize([TestRoot class])); + } + for (i = 0; i < 100; i++) { + free(o2[i]); + } + + ctors1 = dtors1 = ctors2 = dtors2 = 0; + count = reallyCreateInstances([TestRoot class], 0, o2, 10); + testassert(count > 0); + testassert(ctors1 == 0 && dtors1 == 0 && + ctors2 == 0 && dtors2 == 0); + for (i = 0; i < count; i++) testassert([o2[i] class] == [TestRoot class]); + for (i = 0; i < count; i++) object_dispose(o2[i]), o2[i] = nil; + testcollect(); + testassert(ctors1 == 0 && dtors1 == 0 && + ctors2 == 0 && dtors2 == 0); + + for (i = 0; i < 100; i++) { + // prime batch allocator + free(malloc(class_getInstanceSize([TestRoot class]))); + } + + ctors1 = dtors1 = ctors2 = dtors2 = 0; + count = reallyCreateInstances([CXXBase class], 0, o2, 10); + testassert(count > 0); + testassert(ctors1 == count && dtors1 == 0 && + ctors2 == 0 && dtors2 == 0); + for (i = 0; i < count; i++) testassert([o2[i] class] == [CXXBase class]); + for (i = 0; i < count; i++) object_dispose(o2[i]), o2[i] = nil; + testcollect(); + testassert(ctors1 == count && dtors1 == count && + ctors2 == 0 && dtors2 == 0); + + for (i = 0; i < 100; i++) { + // prime batch allocator + free(malloc(class_getInstanceSize([TestRoot class]))); + } + + ctors1 = dtors1 = ctors2 = dtors2 = 0; + count = reallyCreateInstances([NoCXXSub class], 0, o2, 10); + testassert(count > 0); + testassert(ctors1 == count && dtors1 == 0 && + ctors2 == 0 && dtors2 == 0); + for (i = 0; i < count; i++) testassert([o2[i] class] == [NoCXXSub class]); + for (i = 0; i < count; i++) object_dispose(o2[i]), o2[i] = nil; + testcollect(); + testassert(ctors1 == count && dtors1 == count && + ctors2 == 0 && dtors2 == 0); + + for (i = 0; i < 100; i++) { + // prime batch allocator + free(malloc(class_getInstanceSize([TestRoot class]))); + } + + ctors1 = dtors1 = ctors2 = dtors2 = 0; + count = reallyCreateInstances([CXXSub class], 0, o2, 10); + testassert(count > 0); + testassert(ctors1 == count && dtors1 == 0 && + ctors2 == count && dtors2 == 0); + for (i = 0; i < count; i++) testassert([o2[i] class] == [CXXSub class]); + for (i = 0; i < count; i++) object_dispose(o2[i]), o2[i] = nil; + testcollect(); + testassert(ctors1 == count && dtors1 == count && + ctors2 == count && dtors2 == count); +} + +// not ARC +#endif + + +int main() +{ + for (int i = 0; i < 1000; i++) { + testonthread(^{ test_single(); }); + testonthread(^{ test_inplace(); }); + testonthread(^{ test_batch(); }); + } + + testonthread(^{ test_single(); }); + testonthread(^{ test_inplace(); }); + testonthread(^{ test_batch(); }); + + leak_mark(); + + for (int i = 0; i < 1000; i++) { + testonthread(^{ test_single(); }); + testonthread(^{ test_inplace(); }); + testonthread(^{ test_batch(); }); + } + + leak_check(0); + + // fixme ctor exceptions aren't caught inside .cxx_construct ? + // Single allocation, ctors fail + // In-place allocation, ctors fail + // Batch allocation, ctors fail for every object + // Batch allocation, ctors fail for every other object + + succeed(FILENAME); +} diff --git a/test/classgetclass.m b/test/classgetclass.m new file mode 100644 index 0000000..b73243b --- /dev/null +++ b/test/classgetclass.m @@ -0,0 +1,17 @@ +// TEST_CONFIG + +#include "test.h" +#include +#include +#import + +@interface Foo:NSObject +@end +@implementation Foo +@end + +int main() +{ + testassert(gdb_class_getClass([Foo class]) == [Foo class]); + succeed(__FILE__); +} diff --git a/test/classname.m b/test/classname.m new file mode 100644 index 0000000..8d4b993 --- /dev/null +++ b/test/classname.m @@ -0,0 +1,39 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" +#include +#include + +@interface Fake : TestRoot @end +@implementation Fake @end + +int main() +{ + TestRoot *obj = [TestRoot new]; + Class __unsafe_unretained * buf = (Class *)(__bridge void *)(obj); + *buf = [Fake class]; + + testassert(object_getClass(obj) == [Fake class]); + testassert(object_setClass(obj, [TestRoot class]) == [Fake class]); + testassert(object_getClass(obj) == [TestRoot class]); + testassert(object_setClass(nil, [TestRoot class]) == nil); + + testassert(malloc_size(buf) >= sizeof(id)); + bzero(buf, malloc_size(buf)); + testassert(object_setClass(obj, [TestRoot class]) == nil); + + testassert(object_getClass(obj) == [TestRoot class]); + testassert(object_getClass([TestRoot class]) == object_getClass([TestRoot class])); + testassert(object_getClass(nil) == Nil); + + testassert(0 == strcmp(object_getClassName(obj), "TestRoot")); + testassert(0 == strcmp(object_getClassName([TestRoot class]), "TestRoot")); + testassert(0 == strcmp(object_getClassName(nil), "nil")); + + testassert(0 == strcmp(class_getName([TestRoot class]), "TestRoot")); + testassert(0 == strcmp(class_getName(object_getClass([TestRoot class])), "TestRoot")); + testassert(0 == strcmp(class_getName(nil), "nil")); + + succeed(__FILE__); +} diff --git a/test/classpair.mm b/test/classpair.mm new file mode 100644 index 0000000..1ce76ac --- /dev/null +++ b/test/classpair.mm @@ -0,0 +1,319 @@ +// TEST_CONFIG + +#include "test.h" + +#include "testroot.i" +#include +#include + +@protocol Proto +-(void) instanceMethod; ++(void) classMethod; +@optional +-(void) instanceMethod2; ++(void) classMethod2; +@end + +@protocol Proto2 +-(void) instanceMethod; ++(void) classMethod; +@optional +-(void) instanceMethod2; ++(void) classMethod_that_does_not_exist; +@end + +@protocol Proto3 +-(void) instanceMethod; ++(void) classMethod_that_does_not_exist; +@optional +-(void) instanceMethod2; ++(void) classMethod2; +@end + +static int super_initialize; +static int super_cxxctor; +static int super_cxxdtor; + +struct super_cxx { + int foo; + super_cxx() : foo(0) { + super_cxxctor++; + } + ~super_cxx() { + super_cxxdtor++; + } +}; + +@interface Super : TestRoot +@property int superProp; +@end +@implementation Super { + super_cxx _foo; +} +@dynamic superProp; ++(void)initialize { super_initialize++; } + ++(void) classMethod { fail("+[Super classMethod] called"); } ++(void) classMethod2 { fail("+[Super classMethod2] called"); } +-(void) instanceMethod { fail("-[Super instanceMethod] called"); } +-(void) instanceMethod2 { fail("-[Super instanceMethod2] called"); } +@end + +static int state; + +static void instance_fn(id self, SEL _cmd __attribute__((unused))) +{ + testassert(!class_isMetaClass(object_getClass(self))); + state++; +} + +static void class_fn(id self, SEL _cmd __attribute__((unused))) +{ + testassert(class_isMetaClass(object_getClass(self))); + state++; +} + +static void fail_fn(id self __attribute__((unused)), SEL _cmd) +{ + fail("fail_fn '%s' called", sel_getName(_cmd)); +} + + +static void cycle(void) +{ + Class cls; + BOOL ok; + objc_property_t prop; + char namebuf[256]; + + testassert(!objc_getClass("Sub")); + testassert([Super class]); + + // Test subclass with bells and whistles + + cls = objc_allocateClassPair([Super class], "Sub", 0); + testassert(cls); + + class_addMethod(cls, @selector(instanceMethod), + (IMP)&instance_fn, "v@:"); + class_addMethod(object_getClass(cls), @selector(classMethod), + (IMP)&class_fn, "v@:"); + class_addMethod(object_getClass(cls), @selector(initialize), + (IMP)&class_fn, "v@:"); + class_addMethod(object_getClass(cls), @selector(load), + (IMP)&fail_fn, "v@:"); + + ok = class_addProtocol(cls, @protocol(Proto)); + testassert(ok); + ok = class_addProtocol(cls, @protocol(Proto)); + testassert(!ok); + + char attrname[2]; + char attrvalue[2]; + objc_property_attribute_t attrs[1]; + unsigned int attrcount = sizeof(attrs) / sizeof(attrs[0]); + + attrs[0].name = attrname; + attrs[0].value = attrvalue; + strcpy(attrname, "T"); + strcpy(attrvalue, "x"); + + strcpy(namebuf, "subProp"); + ok = class_addProperty(cls, namebuf, attrs, attrcount); + testassert(ok); + strcpy(namebuf, "subProp"); + ok = class_addProperty(cls, namebuf, attrs, attrcount); + testassert(!ok); + strcpy(attrvalue, "i"); + class_replaceProperty(cls, namebuf, attrs, attrcount); + strcpy(namebuf, "superProp"); + ok = class_addProperty(cls, namebuf, attrs, attrcount); + testassert(!ok); + bzero(namebuf, sizeof(namebuf)); + bzero(attrs, sizeof(attrs)); + bzero(attrname, sizeof(attrname)); + bzero(attrvalue, sizeof(attrvalue)); + +#ifndef __LP64__ +# define size 4 +# define align 2 +#else +#define size 8 +# define align 3 +#endif + + /* + { + int ivar; + id ivarid; + id* ivaridstar; + Block_t ivarblock; + } + */ + ok = class_addIvar(cls, "ivar", 4, 2, "i"); + testassert(ok); + ok = class_addIvar(cls, "ivarid", size, align, "@"); + testassert(ok); + ok = class_addIvar(cls, "ivaridstar", size, align, "^@"); + testassert(ok); + ok = class_addIvar(cls, "ivarblock", size, align, "@?"); + testassert(ok); + + ok = class_addIvar(cls, "ivar", 4, 2, "i"); + testassert(!ok); + ok = class_addIvar(object_getClass(cls), "classvar", 4, 2, "i"); + testassert(!ok); + + objc_registerClassPair(cls); + + // should call cls's +initialize, not super's + // Provoke +initialize using class_getMethodImplementation(class method) + // in order to test getNonMetaClass's slow case + super_initialize = 0; + state = 0; + class_getMethodImplementation(object_getClass(cls), @selector(class)); + testassert(super_initialize == 0); + testassert(state == 1); + + testassert(cls == [cls class]); + testassert(cls == objc_getClass("Sub")); + + testassert(!class_isMetaClass(cls)); + testassert(class_isMetaClass(object_getClass(cls))); + + testassert(class_getSuperclass(cls) == [Super class]); + testassert(class_getSuperclass(object_getClass(cls)) == object_getClass([Super class])); + + testassert(class_getInstanceSize(cls) >= sizeof(Class) + 4 + 3*size); + testassert(class_conformsToProtocol(cls, @protocol(Proto))); + + class_addMethod(cls, @selector(instanceMethod2), + (IMP)&instance_fn, "v@:"); + class_addMethod(object_getClass(cls), @selector(classMethod2), + (IMP)&class_fn, "v@:"); + + ok = class_addIvar(cls, "ivar2", 4, 4, "i"); + testassert(!ok); + ok = class_addIvar(object_getClass(cls), "classvar2", 4, 4, "i"); + testassert(!ok); + + ok = class_addProtocol(cls, @protocol(Proto2)); + testassert(ok); + ok = class_addProtocol(cls, @protocol(Proto2)); + testassert(!ok); + ok = class_addProtocol(cls, @protocol(Proto)); + testassert(!ok); + + attrs[0].name = attrname; + attrs[0].value = attrvalue; + strcpy(attrname, "T"); + strcpy(attrvalue, "i"); + + strcpy(namebuf, "subProp2"); + ok = class_addProperty(cls, namebuf, attrs, attrcount); + testassert(ok); + strcpy(namebuf, "subProp"); + ok = class_addProperty(cls, namebuf, attrs, attrcount); + testassert(!ok); + strcpy(namebuf, "superProp"); + ok = class_addProperty(cls, namebuf, attrs, attrcount); + testassert(!ok); + bzero(namebuf, sizeof(namebuf)); + bzero(attrs, sizeof(attrs)); + bzero(attrname, sizeof(attrname)); + bzero(attrvalue, sizeof(attrvalue)); + + prop = class_getProperty(cls, "subProp"); + testassert(prop); + testassert(0 == strcmp(property_getName(prop), "subProp")); + testassert(0 == strcmp(property_getAttributes(prop), "Ti")); + prop = class_getProperty(cls, "subProp2"); + testassert(prop); + testassert(0 == strcmp(property_getName(prop), "subProp2")); + testassert(0 == strcmp(property_getAttributes(prop), "Ti")); + + // note: adding more methods here causes a false leak check failure + state = 0; + [cls classMethod]; + [cls classMethod2]; + testassert(state == 2); + + // put instance tests on a separate thread so they + // are reliably deallocated before class destruction + testonthread(^{ + super_cxxctor = 0; + super_cxxdtor = 0; + id obj = [cls new]; + testassert(super_cxxctor == 1); + testassert(super_cxxdtor == 0); + state = 0; + [obj instanceMethod]; + [obj instanceMethod2]; + testassert(state == 2); + RELEASE_VAR(obj); + testassert(super_cxxctor == 1); + testassert(super_cxxdtor == 1); + }); + + // Test ivar layouts of sub-subclass + Class cls2 = objc_allocateClassPair(cls, "SubSub", 0); + testassert(cls2); + + /* + { + id ivarid2; + id idarray[16]; + void* ptrarray[16]; + char a; + char b; + char c; + } + */ + ok = class_addIvar(cls2, "ivarid2", size, align, "@"); + testassert(ok); + ok = class_addIvar(cls2, "idarray", 16*sizeof(id), align, "[16@]"); + testassert(ok); + ok = class_addIvar(cls2, "ptrarray", 16*sizeof(void*), align, "[16^]"); + testassert(ok); + ok = class_addIvar(cls2, "a", 1, 0, "c"); + testassert(ok); + ok = class_addIvar(cls2, "b", 1, 0, "c"); + testassert(ok); + ok = class_addIvar(cls2, "c", 1, 0, "c"); + testassert(ok); + + objc_registerClassPair(cls2); + + // 1-byte ivars should be well packed + testassert(ivar_getOffset(class_getInstanceVariable(cls2, "b")) == + ivar_getOffset(class_getInstanceVariable(cls2, "a")) + 1); + testassert(ivar_getOffset(class_getInstanceVariable(cls2, "c")) == + ivar_getOffset(class_getInstanceVariable(cls2, "b")) + 1); + + objc_disposeClassPair(cls2); + objc_disposeClassPair(cls); + + testassert(!objc_getClass("Sub")); + + // fixme test layout setters +} + +int main() +{ + int count = 5000; + + // fixme even with this long warmup we still + // suffer false 4096-byte leaks occasionally. + for (int i = 0; i < 500; i++) { + testonthread(^{ cycle(); }); + } + + leak_mark(); + while (count--) { + testonthread(^{ cycle(); }); + } + leak_check(4096); + + succeed(__FILE__); +} + diff --git a/test/classversion.m b/test/classversion.m new file mode 100644 index 0000000..c34b98e --- /dev/null +++ b/test/classversion.m @@ -0,0 +1,19 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" +#include + +int main() +{ + Class cls = [TestRoot class]; + testassert(class_getVersion(cls) == 0); + testassert(class_getVersion(object_getClass(cls)) > 5); + class_setVersion(cls, 100); + testassert(class_getVersion(cls) == 100); + + testassert(class_getVersion(Nil) == 0); + class_setVersion(Nil, 100); + + succeed(__FILE__); +} diff --git a/test/concurrentcat.m b/test/concurrentcat.m new file mode 100644 index 0000000..f4bb145 --- /dev/null +++ b/test/concurrentcat.m @@ -0,0 +1,127 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/concurrentcat.m -o concurrentcat.exe -framework Foundation + + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc1 -o cc1.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc2 -o cc2.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc3 -o cc3.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc4 -o cc4.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc5 -o cc5.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc6 -o cc6.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc7 -o cc7.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc8 -o cc8.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc9 -o cc9.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc10 -o cc10.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc11 -o cc11.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc12 -o cc12.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc13 -o cc13.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc14 -o cc14.bundle + $C{COMPILE} -bundle -bundle_loader concurrentcat.exe -framework Foundation $DIR/concurrentcat_category.m -DTN=cc15 -o cc15.bundle +END +*/ + +#include "test.h" +#include +#include +#include +#include +#include +#include + +@interface TargetClass : NSObject +@end + +@interface TargetClass(LoadedMethods) +- (void) m0; +- (void) m1; +- (void) m2; +- (void) m3; +- (void) m4; +- (void) m5; +- (void) m6; +- (void) m7; +- (void) m8; +- (void) m9; +- (void) m10; +- (void) m11; +- (void) m12; +- (void) m13; +- (void) m14; +- (void) m15; +@end + +@implementation TargetClass +- (void) m0 { fail("shoulda been loaded!"); } +- (void) m1 { fail("shoulda been loaded!"); } +- (void) m2 { fail("shoulda been loaded!"); } +- (void) m3 { fail("shoulda been loaded!"); } +- (void) m4 { fail("shoulda been loaded!"); } +- (void) m5 { fail("shoulda been loaded!"); } +- (void) m6 { fail("shoulda been loaded!"); } +@end + +void *threadFun(void *aTargetClassName) { + const char *className = (const char *)aTargetClassName; + + PUSH_POOL { + + Class targetSubclass = objc_getClass(className); + testassert(targetSubclass); + + id target = [targetSubclass new]; + testassert(target); + + while(1) { + [target m0]; + RETAIN(target); + [target addObserver: target forKeyPath: @"m3" options: 0 context: NULL]; + [target addObserver: target forKeyPath: @"m4" options: 0 context: NULL]; + [target m1]; + RELEASE_VALUE(target); + [target m2]; + AUTORELEASE(target); + [target m3]; + RETAIN(target); + [target removeObserver: target forKeyPath: @"m4"]; + [target addObserver: target forKeyPath: @"m5" options: 0 context: NULL]; + [target m4]; + RETAIN(target); + [target m5]; + AUTORELEASE(target); + [target m6]; + [target m7]; + [target m8]; + [target m9]; + [target m10]; + [target m11]; + [target m12]; + [target m13]; + [target m14]; + [target m15]; + [target removeObserver: target forKeyPath: @"m3"]; + [target removeObserver: target forKeyPath: @"m5"]; + } + } POP_POOL; + return NULL; +} + +int main() +{ + int i; + + void *dylib; + + for(i=1; i<16; i++) { + pthread_t t; + char dlName[100]; + sprintf(dlName, "cc%d.bundle", i); + dylib = dlopen(dlName, RTLD_LAZY); + char className[100]; + sprintf(className, "cc%d", i); + pthread_create(&t, NULL, threadFun, strdup(className)); + testassert(dylib); + } + sleep(1); + + succeed(__FILE__); +} diff --git a/test/concurrentcat_category.m b/test/concurrentcat_category.m new file mode 100644 index 0000000..c59f663 --- /dev/null +++ b/test/concurrentcat_category.m @@ -0,0 +1,70 @@ +#include +#include +#import + +@interface TargetClass : NSObject +@end + +@interface TargetClass(LoadedMethods) +- (void) m0; +- (void) m1; +- (void) m2; +- (void) m3; +- (void) m4; +- (void) m5; +- (void) m6; +- (void) m7; +- (void) m8; +- (void) m9; +- (void) m10; +- (void) m11; +- (void) m12; +- (void) m13; +- (void) m14; +- (void) m15; +@end + +@interface TN:TargetClass +@end + +@implementation TN +- (void) m1 { [super m1]; } +- (void) m3 { [self m1]; } + +- (void) m2 +{ + [self willChangeValueForKey: @"m4"]; + [self didChangeValueForKey: @"m4"]; +} + +- (void)observeValueForKeyPath:(NSString *) keyPath + ofObject:(id)object + change:(NSDictionary *)change + context:(void *)context +{ + // suppress warning + keyPath = nil; + object = nil; + change = nil; + context = NULL; +} +@end + +@implementation TargetClass(LoadedMethods) +- (void) m0 { ; } +- (void) m1 { ; } +- (void) m2 { ; } +- (void) m3 { ; } +- (void) m4 { ; } +- (void) m5 { ; } +- (void) m6 { ; } +- (void) m7 { ; } +- (void) m8 { ; } +- (void) m9 { ; } +- (void) m10 { ; } +- (void) m11 { ; } +- (void) m12 { ; } +- (void) m13 { ; } +- (void) m14 { ; } +- (void) m15 { ; } +@end diff --git a/test/copyFixupHandler.mm b/test/copyFixupHandler.mm new file mode 100644 index 0000000..c2c5c64 --- /dev/null +++ b/test/copyFixupHandler.mm @@ -0,0 +1,89 @@ +// TEST_CONFIG MEM=mrc + +#define TEST_CALLS_OPERATOR_NEW +#include "test.h" +#include "testroot.i" +#include "swift-class-def.m" + +#include + +#include + +static Class expectedOldClass; + +static std::vector observedNewClasses1; +static void handler1(Class _Nonnull oldClass, Class _Nonnull newClass) { + testprintf("%s(%p, %p)", __func__, oldClass, newClass); + testassert(oldClass == expectedOldClass); + observedNewClasses1.push_back(newClass); +} + +static std::vector observedNewClasses2; +static void handler2(Class _Nonnull oldClass, Class _Nonnull newClass) { + testprintf("%s(%p, %p)", __func__, oldClass, newClass); + testassert(oldClass == expectedOldClass); + observedNewClasses2.push_back(newClass); +} + +static std::vector observedNewClasses3; +static void handler3(Class _Nonnull oldClass, Class _Nonnull newClass) { + testprintf("%s(%p, %p)", __func__, oldClass, newClass); + testassert(oldClass == expectedOldClass); + observedNewClasses3.push_back(newClass); +} + +EXTERN_C Class _objc_realizeClassFromSwift(Class, void *); + +EXTERN_C Class init(Class cls, void *arg) { + (void)arg; + _objc_realizeClassFromSwift(cls, cls); + return cls; +} + +@interface SwiftRoot: TestRoot @end +SWIFT_CLASS(SwiftRoot, TestRoot, init); + +int main() +{ + expectedOldClass = [SwiftRoot class]; + Class A = objc_allocateClassPair([RawSwiftRoot class], "A", 0); + objc_registerClassPair(A); + testassert(observedNewClasses1.size() == 0); + testassert(observedNewClasses2.size() == 0); + testassert(observedNewClasses3.size() == 0); + + _objc_setClassCopyFixupHandler(handler1); + + expectedOldClass = A; + Class B = objc_allocateClassPair(A, "B", 0); + objc_registerClassPair(B); + testassert(observedNewClasses1.size() == 2); + testassert(observedNewClasses2.size() == 0); + testassert(observedNewClasses3.size() == 0); + testassert(observedNewClasses1[0] == B); + + _objc_setClassCopyFixupHandler(handler2); + + expectedOldClass = B; + Class C = objc_allocateClassPair(B, "C", 0); + objc_registerClassPair(C); + testassert(observedNewClasses1.size() == 4); + testassert(observedNewClasses2.size() == 2); + testassert(observedNewClasses3.size() == 0); + testassert(observedNewClasses1[2] == C); + testassert(observedNewClasses2[0] == C); + + _objc_setClassCopyFixupHandler(handler3); + + expectedOldClass = C; + Class D = objc_allocateClassPair(C, "D", 0); + objc_registerClassPair(D); + testassert(observedNewClasses1.size() == 6); + testassert(observedNewClasses2.size() == 4); + testassert(observedNewClasses3.size() == 2); + testassert(observedNewClasses1[4] == D); + testassert(observedNewClasses2[2] == D); + testassert(observedNewClasses3[0] == D); + + succeed(__FILE__); +} diff --git a/test/copyIvarList.m b/test/copyIvarList.m new file mode 100644 index 0000000..b924108 --- /dev/null +++ b/test/copyIvarList.m @@ -0,0 +1,115 @@ +// TEST_CONFIG + +#include "test.h" +#include +#include +#include + +OBJC_ROOT_CLASS +@interface SuperIvars { + id isa; + int ivar1; + int ivar2; +} @end +@implementation SuperIvars @end + +@interface SubIvars : SuperIvars { + int ivar3; + int ivar4; +} @end +@implementation SubIvars @end + +OBJC_ROOT_CLASS +@interface FourIvars { + int ivar1; + int ivar2; + int ivar3; + int ivar4; +} @end +@implementation FourIvars @end + +OBJC_ROOT_CLASS +@interface NoIvars { } @end +@implementation NoIvars @end + +static int isNamed(Ivar iv, const char *name) +{ + return (0 == strcmp(name, ivar_getName(iv))); +} + +int main() +{ + Ivar *ivars; + unsigned int count; + Class cls; + + cls = objc_getClass("SubIvars"); + testassert(cls); + + count = 100; + ivars = class_copyIvarList(cls, &count); + testassert(ivars); + testassert(count == 2); + testassert(isNamed(ivars[0], "ivar3")); + testassert(isNamed(ivars[1], "ivar4")); + // ivars[] should be null-terminated + testassert(ivars[2] == NULL); + free(ivars); + + cls = objc_getClass("SuperIvars"); + testassert(cls); + + count = 100; + ivars = class_copyIvarList(cls, &count); + testassert(ivars); + testassert(count == 3); + testassert(isNamed(ivars[0], "isa")); + testassert(isNamed(ivars[1], "ivar1")); + testassert(isNamed(ivars[2], "ivar2")); + // ivars[] should be null-terminated + testassert(ivars[3] == NULL); + free(ivars); + + // Check null-termination - this ivar list block would be 16 bytes + // if it weren't for the terminator + cls = objc_getClass("FourIvars"); + testassert(cls); + + count = 100; + ivars = class_copyIvarList(cls, &count); + testassert(ivars); + testassert(count == 4); + testassert(malloc_size(ivars) >= 5 * sizeof(Ivar)); + testassert(ivars[3] != NULL); + testassert(ivars[4] == NULL); + free(ivars); + + // Check NULL count parameter + ivars = class_copyIvarList(cls, NULL); + testassert(ivars); + testassert(ivars[4] == NULL); + testassert(ivars[3] != NULL); + free(ivars); + + // Check NULL class parameter + count = 100; + ivars = class_copyIvarList(NULL, &count); + testassert(!ivars); + testassert(count == 0); + + // Check NULL class and count + ivars = class_copyIvarList(NULL, NULL); + testassert(!ivars); + + // Check class with no ivars + cls = objc_getClass("NoIvars"); + testassert(cls); + + count = 100; + ivars = class_copyIvarList(cls, &count); + testassert(!ivars); + testassert(count == 0); + + succeed(__FILE__); + return 0; +} diff --git a/test/copyMethodList.m b/test/copyMethodList.m new file mode 100644 index 0000000..18a0d6c --- /dev/null +++ b/test/copyMethodList.m @@ -0,0 +1,155 @@ +// TEST_CFLAGS -Wl,-no_objc_category_merging + +#include "test.h" +#include "testroot.i" +#include +#include + +@interface SuperMethods : TestRoot { } @end +@implementation SuperMethods ++(BOOL)SuperMethodClass { return NO; } ++(BOOL)SuperMethodClass2 { return NO; } +-(BOOL)SuperMethodInstance { return NO; } +-(BOOL)SuperMethodInstance2 { return NO; } +@end + +@interface SubMethods : SuperMethods { } @end +@implementation SubMethods ++(BOOL)SubMethodClass { return NO; } ++(BOOL)SubMethodClass2 { return NO; } +-(BOOL)SubMethodInstance { return NO; } +-(BOOL)SubMethodInstance2 { return NO; } +@end + +@interface SuperMethods (Category) @end +@implementation SuperMethods (Category) ++(BOOL)SuperMethodClass { return YES; } ++(BOOL)SuperMethodClass2 { return YES; } +-(BOOL)SuperMethodInstance { return YES; } +-(BOOL)SuperMethodInstance2 { return YES; } +@end + +@interface SubMethods (Category) @end +@implementation SubMethods (Category) ++(BOOL)SubMethodClass { return YES; } ++(BOOL)SubMethodClass2 { return YES; } +-(BOOL)SubMethodInstance { return YES; } +-(BOOL)SubMethodInstance2 { return YES; } +@end + + +@interface FourMethods : TestRoot @end +@implementation FourMethods +-(void)one { } +-(void)two { } +-(void)three { } +-(void)four { } +@end + +@interface NoMethods : TestRoot @end +@implementation NoMethods @end + +static void checkReplacement(Method *list, const char *name) +{ + Method first = NULL, second = NULL; + SEL sel = sel_registerName(name); + int i; + + testassert(list); + + // Find the methods. There should be two. + for (i = 0; list[i]; i++) { + if (method_getName(list[i]) == sel) { + if (!first) first = list[i]; + else if (!second) second = list[i]; + else testassert(0); + } + } + + // Call the methods. The first should be the category (returns YES). + BOOL isCat; + isCat = ((BOOL(*)(id, Method))method_invoke)(NULL, first); + testassert(isCat); + isCat = ((BOOL(*)(id, Method))method_invoke)(NULL, second); + testassert(! isCat); +} + +int main() +{ + // Class SubMethods has not yet been touched, so runtime must attach + // the lazy categories + Method *methods; + unsigned int count; + Class cls; + + cls = objc_getClass("SubMethods"); + testassert(cls); + + testprintf("calling class_copyMethodList(SubMethods) (should be unmethodized)\n"); + + count = 100; + methods = class_copyMethodList(cls, &count); + testassert(methods); + testassert(count == 4); + // methods[] should be null-terminated + testassert(methods[4] == NULL); + // Class and category methods may be mixed in the method list thanks + // to linker / shared cache sorting, but a category's replacement should + // always precede the class's implementation. + checkReplacement(methods, "SubMethodInstance"); + checkReplacement(methods, "SubMethodInstance2"); + free(methods); + + testprintf("calling class_copyMethodList(SubMethods(meta)) (should be unmethodized)\n"); + + count = 100; + methods = class_copyMethodList(object_getClass(cls), &count); + testassert(methods); + testassert(count == 4); + // methods[] should be null-terminated + testassert(methods[4] == NULL); + // Class and category methods may be mixed in the method list thanks + // to linker / shared cache sorting, but a category's replacement should + // always precede the class's implementation. + checkReplacement(methods, "SubMethodClass"); + checkReplacement(methods, "SubMethodClass2"); + free(methods); + + // Check null-termination - this method list block would be 16 bytes + // if it weren't for the terminator + count = 100; + cls = objc_getClass("FourMethods"); + methods = class_copyMethodList(cls, &count); + testassert(methods); + testassert(count == 4); + testassert(malloc_size(methods) >= (4+1) * sizeof(Method)); + testassert(methods[3] != NULL); + testassert(methods[4] == NULL); + free(methods); + + // Check NULL count parameter + methods = class_copyMethodList(cls, NULL); + testassert(methods); + testassert(methods[4] == NULL); + testassert(methods[3] != NULL); + free(methods); + + // Check NULL class parameter + count = 100; + methods = class_copyMethodList(NULL, &count); + testassert(!methods); + testassert(count == 0); + + // Check NULL class and count + methods = class_copyMethodList(NULL, NULL); + testassert(!methods); + + // Check class with no methods + count = 100; + cls = objc_getClass("NoMethods"); + methods = class_copyMethodList(cls, &count); + testassert(!methods); + testassert(count == 0); + + succeed(__FILE__); +} diff --git a/test/copyPropertyList.m b/test/copyPropertyList.m new file mode 100644 index 0000000..e3b2632 --- /dev/null +++ b/test/copyPropertyList.m @@ -0,0 +1,167 @@ +// TEST_CONFIG + +#include "test.h" +#include +#include +#include + +OBJC_ROOT_CLASS +@interface SuperProps { id isa; int prop1; int prop2; } +@property int prop1; +@property int prop2; +@end +@implementation SuperProps +@synthesize prop1; +@synthesize prop2; +@end + +@interface SubProps : SuperProps { int prop3; int prop4; } +@property int prop3; +@property int prop4; +@end +@implementation SubProps +@synthesize prop3; +@synthesize prop4; +@end + +OBJC_ROOT_CLASS +@interface FourProps { int prop1; int prop2; int prop3; int prop4; } +@property int prop1; +@property int prop2; +@property int prop3; +@property int prop4; +@end +@implementation FourProps +@synthesize prop1; +@synthesize prop2; +@synthesize prop3; +@synthesize prop4; +@end + +OBJC_ROOT_CLASS +@interface NoProps @end +@implementation NoProps @end + +OBJC_ROOT_CLASS +@interface ClassProps +@property(readonly,class) int prop1; +@property(readonly,class) int prop2; +@property(readonly) int prop2; +@property(readonly) int prop3; +@end +@implementation ClassProps ++(int)prop1 { return 0; } ++(int)prop2 { return 0; } +-(int)prop2 { return 0; } +-(int)prop3 { return 0; } +@end + +static int isNamed(objc_property_t p, const char *name) +{ + return (0 == strcmp(name, property_getName(p))); +} + +int main() +{ + objc_property_t *props; + unsigned int count; + Class cls; + + cls = objc_getClass("SubProps"); + testassert(cls); + + count = 100; + props = class_copyPropertyList(cls, &count); + testassert(props); + testassert(count == 2); + testassert((isNamed(props[0], "prop3") && isNamed(props[1], "prop4")) || + (isNamed(props[1], "prop3") && isNamed(props[0], "prop4"))); + // props[] should be null-terminated + testassert(props[2] == NULL); + free(props); + + cls = objc_getClass("SuperProps"); + testassert(cls); + + count = 100; + props = class_copyPropertyList(cls, &count); + testassert(props); + testassert(count == 2); + testassert((isNamed(props[0], "prop1") && isNamed(props[1], "prop2")) || + (isNamed(props[1], "prop1") && isNamed(props[0], "prop2"))); + // props[] should be null-terminated + testassert(props[2] == NULL); + free(props); + + // Check null-termination - this property list block would be 16 bytes + // if it weren't for the terminator + cls = objc_getClass("FourProps"); + testassert(cls); + + count = 100; + props = class_copyPropertyList(cls, &count); + testassert(props); + testassert(count == 4); + testassert(malloc_size(props) >= 5 * sizeof(objc_property_t)); + testassert(props[3] != NULL); + testassert(props[4] == NULL); + free(props); + + // Check NULL count parameter + props = class_copyPropertyList(cls, NULL); + testassert(props); + testassert(props[4] == NULL); + testassert(props[3] != NULL); + free(props); + + // Check NULL class parameter + count = 100; + props = class_copyPropertyList(NULL, &count); + testassert(!props); + testassert(count == 0); + + // Check NULL class and count + props = class_copyPropertyList(NULL, NULL); + testassert(!props); + + // Check class with no properties + cls = objc_getClass("NoProps"); + testassert(cls); + + count = 100; + props = class_copyPropertyList(cls, &count); + testassert(!props); + testassert(count == 0); + + // Check class properties + + cls = objc_getClass("ClassProps"); + testassert(cls); + + count = 100; + props = class_copyPropertyList(cls, &count); + testassert(props); + testassert(count == 2); + testassert((isNamed(props[0], "prop2") && isNamed(props[1], "prop3")) || + (isNamed(props[1], "prop2") && isNamed(props[0], "prop3"))); + // props[] should be null-terminated + testassert(props[2] == NULL); + free(props); + + cls = object_getClass(objc_getClass("ClassProps")); + testassert(cls); + + count = 100; + props = class_copyPropertyList(cls, &count); + testassert(props); + testassert(count == 2); + testassert((isNamed(props[0], "prop1") && isNamed(props[1], "prop2")) || + (isNamed(props[1], "prop1") && isNamed(props[0], "prop2"))); + // props[] should be null-terminated + testassert(props[2] == NULL); + free(props); + + + succeed(__FILE__); + return 0; +} diff --git a/test/copyProtocolList.m b/test/copyProtocolList.m new file mode 100644 index 0000000..5543e50 --- /dev/null +++ b/test/copyProtocolList.m @@ -0,0 +1,69 @@ +// TEST_CONFIG + +#include "test.h" +#include +#include +#include + +@protocol Proto1 ++(id)proto1ClassMethod; +-(id)proto1InstanceMethod; +@end + +void noNullEntries(Protocol * _Nonnull __unsafe_unretained * _Nullable protolist, + unsigned int count) +{ + for (unsigned int i = 0; i != count; ++i) { + testassert(protolist[i]); + testassert(protocol_getName(protolist[i])); + testprintf("Protocol[%d/%d]: %p %s\n", i, count, protolist[i], protocol_getName(protolist[i])); + } +} + +Protocol* getProtocol(Protocol * _Nonnull __unsafe_unretained * _Nullable protolist, + unsigned int count, const char* name) { + for (unsigned int i = 0; i != count; ++i) { + if (!strcmp(protocol_getName(protolist[i]), name)) + return protolist[i]; + } + return nil; +} + +int main() +{ + Protocol * _Nonnull __unsafe_unretained * _Nullable protolist; + unsigned int count; + + count = 100; + protolist = objc_copyProtocolList(&count); + testassert(protolist); + testassert(count != 0); + testassert(malloc_size(protolist) >= (count * sizeof(Protocol*))); + noNullEntries(protolist, count); + testassert(protolist[count] == nil); + // Check for a shared cache protocol, ie, the one we know comes from libobjc + testassert(getProtocol(protolist, count, "NSObject")); + // Test for a protocol we know isn't in the cache + testassert(getProtocol(protolist, count, "Proto1") == @protocol(Proto1)); + // Test for a protocol we know isn't there + testassert(!getProtocol(protolist, count, "Proto2")); + free(protolist); + + // Now add it + Protocol* newproto = objc_allocateProtocol("Proto2"); + objc_registerProtocol(newproto); + + Protocol * _Nonnull __unsafe_unretained * _Nullable newProtolist; + unsigned int newCount; + + newCount = 100; + newProtolist = objc_copyProtocolList(&newCount); + testassert(newProtolist); + testassert(newCount == (count + 1)); + testassert(getProtocol(newProtolist, newCount, "Proto2")); + free(newProtolist); + + + succeed(__FILE__); + return 0; +} diff --git a/test/createInstance.m b/test/createInstance.m new file mode 100644 index 0000000..c1c156c --- /dev/null +++ b/test/createInstance.m @@ -0,0 +1,66 @@ +// TEST_CONFIG + +#import +#import +#include "test.h" +#include "testroot.i" + +@interface Super : TestRoot @end +@implementation Super @end + +@interface Sub : Super { int array[128]; } @end +@implementation Sub @end + +#if __has_feature(objc_arc) +#define object_dispose(x) do {} while (0) +#endif + +int main() +{ + Super *s; + + s = class_createInstance([Super class], 0); + testassert(s); + testassert(object_getClass(s) == [Super class]); + testassert(malloc_size((__bridge const void *)s) >= class_getInstanceSize([Super class])); + + object_dispose(s); + + s = class_createInstance([Sub class], 0); + testassert(s); + testassert(object_getClass(s) == [Sub class]); + testassert(malloc_size((__bridge const void *)s) >= class_getInstanceSize([Sub class])); + + object_dispose(s); + + s = class_createInstance([Super class], 100); + testassert(s); + testassert(object_getClass(s) == [Super class]); + testassert(malloc_size((__bridge const void *)s) >= class_getInstanceSize([Super class]) + 100); + + object_dispose(s); + + s = class_createInstance([Sub class], 100); + testassert(s); + testassert(object_getClass(s) == [Sub class]); + testassert(malloc_size((__bridge const void *)s) >= class_getInstanceSize([Sub class]) + 100); + + object_dispose(s); + + s = class_createInstance(Nil, 0); + testassert(!s); + + testassert(TestRootAlloc == 0); + +#if __has_feature(objc_arc) + // ARC version didn't use object_dispose() + // and should have called -dealloc on 4 objects + testassert(TestRootDealloc == 4); +#else + // MRC version used object_dispose() + // which doesn't call -dealloc + testassert(TestRootDealloc == 0); +#endif + + succeed(__FILE__); +} diff --git a/test/customrr-cat1.m b/test/customrr-cat1.m new file mode 100644 index 0000000..823238d --- /dev/null +++ b/test/customrr-cat1.m @@ -0,0 +1,7 @@ +@interface InheritingSubCat @end + +@interface InheritingSubCat (NonClobberingCategory) @end + +@implementation InheritingSubCat (NonClobberingCategory) +-(id) unrelatedMethod { return self; } +@end diff --git a/test/customrr-cat2.m b/test/customrr-cat2.m new file mode 100644 index 0000000..55c96df --- /dev/null +++ b/test/customrr-cat2.m @@ -0,0 +1,7 @@ +@interface InheritingSubCat @end + +@interface InheritingSubCat (ClobberingCategory) @end + +@implementation InheritingSubCat (ClobberingCategory) +-(int) retainCount { return 1; } +@end diff --git a/test/customrr-nsobject-awz.m b/test/customrr-nsobject-awz.m new file mode 100644 index 0000000..258ce9e --- /dev/null +++ b/test/customrr-nsobject-awz.m @@ -0,0 +1,16 @@ +/* + +TEST_CONFIG MEM=mrc +TEST_ENV OBJC_PRINT_CUSTOM_RR=YES OBJC_PRINT_CUSTOM_AWZ=YES OBJC_PRINT_CUSTOM_CORE=YES + +TEST_BUILD + $C{COMPILE} $DIR/customrr-nsobject.m -o customrr-nsobject-awz.exe -DSWIZZLE_AWZ=1 -fno-objc-convert-messages-to-runtime-calls +END + +TEST_RUN_OUTPUT +objc\[\d+\]: CUSTOM AWZ: NSObject \(meta\) +OK: customrr-nsobject-awz.exe +END + +*/ + diff --git a/test/customrr-nsobject-core.m b/test/customrr-nsobject-core.m new file mode 100644 index 0000000..f1442f8 --- /dev/null +++ b/test/customrr-nsobject-core.m @@ -0,0 +1,17 @@ +/* + +TEST_CONFIG MEM=mrc +TEST_ENV OBJC_PRINT_CUSTOM_RR=YES OBJC_PRINT_CUSTOM_AWZ=YES OBJC_PRINT_CUSTOM_CORE=YES + +TEST_BUILD + $C{COMPILE} $DIR/customrr-nsobject.m -o customrr-nsobject-core.exe -DSWIZZLE_CORE=1 -fno-objc-convert-messages-to-runtime-calls +END + +TEST_RUN_OUTPUT +objc\[\d+\]: CUSTOM Core: NSObject +objc\[\d+\]: CUSTOM Core: NSObject \(meta\) +OK: customrr-nsobject-core.exe +END + +*/ + diff --git a/test/customrr-nsobject-none.m b/test/customrr-nsobject-none.m new file mode 100644 index 0000000..90be7fe --- /dev/null +++ b/test/customrr-nsobject-none.m @@ -0,0 +1,15 @@ +/* + +TEST_CONFIG MEM=mrc +TEST_ENV OBJC_PRINT_CUSTOM_RR=YES OBJC_PRINT_CUSTOM_AWZ=YES OBJC_PRINT_CUSTOM_CORE=YES + +TEST_BUILD + $C{COMPILE} $DIR/customrr-nsobject.m -o customrr-nsobject-none.exe -fno-objc-convert-messages-to-runtime-calls +END + +TEST_RUN_OUTPUT +OK: customrr-nsobject-none.exe +END + +*/ + diff --git a/test/customrr-nsobject-rr.m b/test/customrr-nsobject-rr.m new file mode 100644 index 0000000..2021591 --- /dev/null +++ b/test/customrr-nsobject-rr.m @@ -0,0 +1,16 @@ +/* + +TEST_CONFIG MEM=mrc +TEST_ENV OBJC_PRINT_CUSTOM_RR=YES OBJC_PRINT_CUSTOM_AWZ=YES OBJC_PRINT_CUSTOM_CORE=YES + +TEST_BUILD + $C{COMPILE} $DIR/customrr-nsobject.m -o customrr-nsobject-rr.exe -DSWIZZLE_RELEASE=1 -fno-objc-convert-messages-to-runtime-calls +END + +TEST_RUN_OUTPUT +objc\[\d+\]: CUSTOM RR: NSObject +OK: customrr-nsobject-rr.exe +END + +*/ + diff --git a/test/customrr-nsobject-rrawz.m b/test/customrr-nsobject-rrawz.m new file mode 100644 index 0000000..5e0fa2a --- /dev/null +++ b/test/customrr-nsobject-rrawz.m @@ -0,0 +1,17 @@ +/* + +TEST_CONFIG MEM=mrc +TEST_ENV OBJC_PRINT_CUSTOM_RR=YES OBJC_PRINT_CUSTOM_AWZ=YES OBJC_PRINT_CUSTOM_CORE=YES + +TEST_BUILD + $C{COMPILE} $DIR/customrr-nsobject.m -o customrr-nsobject-rrawz.exe -DSWIZZLE_RELEASE=1 -DSWIZZLE_AWZ=1 -fno-objc-convert-messages-to-runtime-calls +END + +TEST_RUN_OUTPUT +objc\[\d+\]: CUSTOM AWZ: NSObject \(meta\) +objc\[\d+\]: CUSTOM RR: NSObject +OK: customrr-nsobject-rrawz.exe +END + +*/ + diff --git a/test/customrr-nsobject.m b/test/customrr-nsobject.m new file mode 100644 index 0000000..912f414 --- /dev/null +++ b/test/customrr-nsobject.m @@ -0,0 +1,231 @@ +// This file is used in the customrr-nsobject-*.m tests + +#include "test.h" +#include +#include + +#if __has_feature(ptrauth_calls) +typedef IMP __ptrauth_objc_method_list_imp MethodListIMP; +#else +typedef IMP MethodListIMP; +#endif + +static int Retains; +static int Releases; +static int Autoreleases; +static int PlusInitializes; +static int Allocs; +static int AllocWithZones; +static int Inits; +static int PlusNew; +static int Self; +static int PlusSelf; + +id (*RealRetain)(id self, SEL _cmd); +void (*RealRelease)(id self, SEL _cmd); +id (*RealAutorelease)(id self, SEL _cmd); +id (*RealAlloc)(id self, SEL _cmd); +id (*RealAllocWithZone)(id self, SEL _cmd, void *zone); +id (*RealPlusNew)(id self, SEL _cmd); +id (*RealSelf)(id self); +id (*RealPlusSelf)(id self); + +id HackRetain(id self, SEL _cmd) { Retains++; return RealRetain(self, _cmd); } +void HackRelease(id self, SEL _cmd) { Releases++; return RealRelease(self, _cmd); } +id HackAutorelease(id self, SEL _cmd) { Autoreleases++; return RealAutorelease(self, _cmd); } + +id HackAlloc(Class self, SEL _cmd) { Allocs++; return RealAlloc(self, _cmd); } +id HackAllocWithZone(Class self, SEL _cmd, void *zone) { AllocWithZones++; return RealAllocWithZone(self, _cmd, zone); } + +void HackPlusInitialize(id self __unused, SEL _cmd __unused) { PlusInitializes++; } + +id HackInit(id self, SEL _cmd __unused) { Inits++; return self; } + +id HackPlusNew(id self, SEL _cmd __unused) { PlusNew++; return RealPlusNew(self, _cmd); } +id HackSelf(id self) { Self++; return RealSelf(self); } +id HackPlusSelf(id self) { PlusSelf++; return RealPlusSelf(self); } + + +int main(int argc __unused, char **argv) +{ + Class cls = objc_getClass("NSObject"); + Method meth; + + meth = class_getClassMethod(cls, @selector(initialize)); + method_setImplementation(meth, (IMP)HackPlusInitialize); + + // We either swizzle the method normally (testing that it properly + // disables optimizations), or we hack the implementation into place + // behind objc's back (so we can see whether it got called with the + // optimizations still enabled). + + meth = class_getClassMethod(cls, @selector(allocWithZone:)); + RealAllocWithZone = (typeof(RealAllocWithZone))method_getImplementation(meth); +#if SWIZZLE_AWZ + method_setImplementation(meth, (IMP)HackAllocWithZone); +#else + ((MethodListIMP *)meth)[2] = (IMP)HackAllocWithZone; +#endif + + meth = class_getClassMethod(cls, @selector(new)); + RealPlusNew = (typeof(RealPlusNew))method_getImplementation(meth); +#if SWIZZLE_CORE + method_setImplementation(meth, (IMP)HackPlusNew); +#else + ((MethodListIMP *)meth)[2] = (IMP)HackPlusNew; +#endif + + meth = class_getClassMethod(cls, @selector(self)); + RealPlusSelf = (typeof(RealPlusSelf))method_getImplementation(meth); +#if SWIZZLE_CORE + method_setImplementation(meth, (IMP)HackPlusSelf); +#else + ((MethodListIMP *)meth)[2] = (IMP)HackPlusSelf; +#endif + + meth = class_getInstanceMethod(cls, @selector(self)); + RealSelf = (typeof(RealSelf))method_getImplementation(meth); +#if SWIZZLE_CORE + method_setImplementation(meth, (IMP)HackSelf); +#else + ((MethodListIMP *)meth)[2] = (IMP)HackSelf; +#endif + + meth = class_getInstanceMethod(cls, @selector(release)); + RealRelease = (typeof(RealRelease))method_getImplementation(meth); +#if SWIZZLE_RELEASE + method_setImplementation(meth, (IMP)HackRelease); +#else + ((MethodListIMP *)meth)[2] = (IMP)HackRelease; +#endif + + // These other methods get hacked for counting purposes only + + meth = class_getInstanceMethod(cls, @selector(retain)); + RealRetain = (typeof(RealRetain))method_getImplementation(meth); + ((MethodListIMP *)meth)[2] = (IMP)HackRetain; + + meth = class_getInstanceMethod(cls, @selector(autorelease)); + RealAutorelease = (typeof(RealAutorelease))method_getImplementation(meth); + ((MethodListIMP *)meth)[2] = (IMP)HackAutorelease; + + meth = class_getClassMethod(cls, @selector(alloc)); + RealAlloc = (typeof(RealAlloc))method_getImplementation(meth); + ((MethodListIMP *)meth)[2] = (IMP)HackAlloc; + + meth = class_getInstanceMethod(cls, @selector(init)); + ((MethodListIMP *)meth)[2] = (IMP)HackInit; + + // Verify that the swizzles occurred before +initialize by provoking it now + testassert(PlusInitializes == 0); + [NSObject self]; + testassert(PlusInitializes == 1); + + id obj; + id result; + + Allocs = 0; + AllocWithZones = 0; + Inits = 0; + obj = objc_alloc(cls); +#if SWIZZLE_AWZ + testprintf("swizzled AWZ should be called\n"); + testassert(Allocs == 1); + testassert(AllocWithZones == 1); + testassert(Inits == 0); +#else + testprintf("unswizzled AWZ should be bypassed\n"); + testassert(Allocs == 0); + testassert(AllocWithZones == 0); + testassert(Inits == 0); +#endif + testassert([obj isKindOfClass:[NSObject class]]); + + Allocs = 0; + AllocWithZones = 0; + Inits = 0; + obj = [NSObject alloc]; +#if SWIZZLE_AWZ + testprintf("swizzled AWZ should be called\n"); + testassert(Allocs == 1); + testassert(AllocWithZones == 1); + testassert(Inits == 0); +#else + testprintf("unswizzled AWZ should be bypassed\n"); + testassert(Allocs == 1); + testassert(AllocWithZones == 0); + testassert(Inits == 0); +#endif + testassert([obj isKindOfClass:[NSObject class]]); + + Allocs = 0; + AllocWithZones = 0; + Inits = 0; + obj = objc_alloc_init(cls); +#if SWIZZLE_AWZ + testprintf("swizzled AWZ should be called\n"); + testassert(Allocs == 1); + testassert(AllocWithZones == 1); + testassert(Inits == 1); +#else + testprintf("unswizzled AWZ should be bypassed\n"); + testassert(Allocs == 0); + testassert(AllocWithZones == 0); + testassert(Inits == 1); // swizzled init is still called +#endif + testassert([obj isKindOfClass:[NSObject class]]); + + Retains = 0; + result = objc_retain(obj); +#if SWIZZLE_RELEASE + testprintf("swizzled release should force retain\n"); + testassert(Retains == 1); +#else + testprintf("unswizzled release should bypass retain\n"); + testassert(Retains == 0); +#endif + testassert(result == obj); + + Releases = 0; + Autoreleases = 0; + PUSH_POOL { + result = objc_autorelease(obj); +#if SWIZZLE_RELEASE + testprintf("swizzled release should force autorelease\n"); + testassert(Autoreleases == 1); +#else + testprintf("unswizzled release should bypass autorelease\n"); + testassert(Autoreleases == 0); +#endif + testassert(result == obj); + } POP_POOL + +#if SWIZZLE_RELEASE + testprintf("swizzled release should be called\n"); + testassert(Releases == 1); +#else + testprintf("unswizzled release should be bypassed\n"); + testassert(Releases == 0); +#endif + + PlusNew = 0; + Self = 0; + PlusSelf = 0; + Class nso = objc_opt_self([NSObject class]); + obj = objc_opt_new(nso); + obj = objc_opt_self(obj); +#if SWIZZLE_CORE + testprintf("swizzled Core should be called\n"); + testassert(PlusNew == 1); + testassert(Self == 1); + testassert(PlusSelf == 1); +#else + testprintf("unswizzled CORE should be bypassed\n"); + testassert(PlusNew == 0); + testassert(Self == 0); + testassert(PlusSelf == 0); +#endif + testassert([obj isKindOfClass:nso]); + + succeed(basename(argv[0])); +} diff --git a/test/customrr.m b/test/customrr.m new file mode 100644 index 0000000..4ebcece --- /dev/null +++ b/test/customrr.m @@ -0,0 +1,915 @@ +// These options must match customrr2.m +// TEST_CONFIG MEM=mrc +/* +TEST_BUILD + $C{COMPILE} $DIR/customrr.m -fvisibility=default -o customrr.exe -fno-objc-convert-messages-to-runtime-calls + $C{COMPILE} -bundle -bundle_loader customrr.exe $DIR/customrr-cat1.m -o customrr-cat1.bundle + $C{COMPILE} -bundle -bundle_loader customrr.exe $DIR/customrr-cat2.m -o customrr-cat2.bundle +END +*/ + + +#include "test.h" +#include +#include + +static int Retains; +static int Releases; +static int Autoreleases; +static int RetainCounts; +static int PlusRetains; +static int PlusReleases; +static int PlusAutoreleases; +static int PlusRetainCounts; +static int Allocs; +static int AllocWithZones; + +static int SubRetains; +static int SubReleases; +static int SubAutoreleases; +static int SubRetainCounts; +static int SubPlusRetains; +static int SubPlusReleases; +static int SubPlusAutoreleases; +static int SubPlusRetainCounts; +static int SubAllocs; +static int SubAllocWithZones; + +static int Imps; + +static id imp_fn(id self, SEL _cmd __unused, ...) +{ + Imps++; + return self; +} + +static void zero(void) { + Retains = 0; + Releases = 0; + Autoreleases = 0; + RetainCounts = 0; + PlusRetains = 0; + PlusReleases = 0; + PlusAutoreleases = 0; + PlusRetainCounts = 0; + Allocs = 0; + AllocWithZones = 0; + + SubRetains = 0; + SubReleases = 0; + SubAutoreleases = 0; + SubRetainCounts = 0; + SubPlusRetains = 0; + SubPlusReleases = 0; + SubPlusAutoreleases = 0; + SubPlusRetainCounts = 0; + SubAllocs = 0; + SubAllocWithZones = 0; + + Imps = 0; +} + + +id HackRetain(id self, SEL _cmd __unused) { Retains++; return self; } +void HackRelease(id self __unused, SEL _cmd __unused) { Releases++; } +id HackAutorelease(id self, SEL _cmd __unused) { Autoreleases++; return self; } +NSUInteger HackRetainCount(id self __unused, SEL _cmd __unused) { RetainCounts++; return 1; } +id HackPlusRetain(id self, SEL _cmd __unused) { PlusRetains++; return self; } +void HackPlusRelease(id self __unused, SEL _cmd __unused) { PlusReleases++; } +id HackPlusAutorelease(id self, SEL _cmd __unused) { PlusAutoreleases++; return self; } +NSUInteger HackPlusRetainCount(id self __unused, SEL _cmd __unused) { PlusRetainCounts++; return 1; } +id HackAlloc(Class self, SEL _cmd __unused) { Allocs++; return class_createInstance(self, 0); } +id HackAllocWithZone(Class self, SEL _cmd __unused) { AllocWithZones++; return class_createInstance(self, 0); } + + +@interface OverridingSub : NSObject @end +@implementation OverridingSub + +-(id) retain { SubRetains++; return self; } ++(id) retain { SubPlusRetains++; return self; } +-(oneway void) release { SubReleases++; } ++(oneway void) release { SubPlusReleases++; } +-(id) autorelease { SubAutoreleases++; return self; } ++(id) autorelease { SubPlusAutoreleases++; return self; } +-(NSUInteger) retainCount { SubRetainCounts++; return 1; } ++(NSUInteger) retainCount { SubPlusRetainCounts++; return 1; } + +@end + +@interface OverridingASub : NSObject @end +@implementation OverridingASub ++(id) alloc { SubAllocs++; return class_createInstance(self, 0); } +@end + +@interface OverridingAWZSub : NSObject @end +@implementation OverridingAWZSub ++(id) allocWithZone:(NSZone * __unused)z { SubAllocWithZones++; return class_createInstance(self, 0); } +@end + +@interface OverridingAAWZSub : NSObject @end +@implementation OverridingAAWZSub ++(id) alloc { SubAllocs++; return class_createInstance(self, 0); } ++(id) allocWithZone:(NSZone * __unused)z { SubAllocWithZones++; return class_createInstance(self, 0); } +@end + + +@interface InheritingSub : NSObject @end +@implementation InheritingSub @end + +@interface InheritingSub2 : NSObject @end +@implementation InheritingSub2 @end +@interface InheritingSub2_2 : InheritingSub2 @end +@implementation InheritingSub2_2 @end + +@interface InheritingSub3 : NSObject @end +@implementation InheritingSub3 @end +@interface InheritingSub3_2 : InheritingSub3 @end +@implementation InheritingSub3_2 @end + +@interface InheritingSub4 : NSObject @end +@implementation InheritingSub4 @end +@interface InheritingSub4_2 : InheritingSub4 @end +@implementation InheritingSub4_2 @end + +@interface InheritingSub5 : NSObject @end +@implementation InheritingSub5 @end +@interface InheritingSub5_2 : InheritingSub5 @end +@implementation InheritingSub5_2 @end + +@interface InheritingSub6 : NSObject @end +@implementation InheritingSub6 @end +@interface InheritingSub6_2 : InheritingSub6 @end +@implementation InheritingSub6_2 @end + +@interface InheritingSub7 : NSObject @end +@implementation InheritingSub7 @end +@interface InheritingSub7_2 : InheritingSub7 @end +@implementation InheritingSub7_2 @end + +@interface InheritingSubCat : NSObject @end +@implementation InheritingSubCat @end +@interface InheritingSubCat_2 : InheritingSubCat @end +@implementation InheritingSubCat_2 @end + + +extern uintptr_t OBJC_CLASS_$_UnrealizedSubA1; +@interface UnrealizedSubA1 : NSObject @end +@implementation UnrealizedSubA1 @end +extern uintptr_t OBJC_CLASS_$_UnrealizedSubA2; +@interface UnrealizedSubA2 : NSObject @end +@implementation UnrealizedSubA2 @end +extern uintptr_t OBJC_CLASS_$_UnrealizedSubA3; +@interface UnrealizedSubA3 : NSObject @end +@implementation UnrealizedSubA3 @end + +extern uintptr_t OBJC_CLASS_$_UnrealizedSubB1; +@interface UnrealizedSubB1 : NSObject @end +@implementation UnrealizedSubB1 @end +extern uintptr_t OBJC_CLASS_$_UnrealizedSubB2; +@interface UnrealizedSubB2 : NSObject @end +@implementation UnrealizedSubB2 @end +extern uintptr_t OBJC_CLASS_$_UnrealizedSubB3; +@interface UnrealizedSubB3 : NSObject @end +@implementation UnrealizedSubB3 @end + +extern uintptr_t OBJC_CLASS_$_UnrealizedSubC1; +@interface UnrealizedSubC1 : NSObject @end +@implementation UnrealizedSubC1 @end +extern uintptr_t OBJC_CLASS_$_UnrealizedSubC2; +@interface UnrealizedSubC2 : NSObject @end +@implementation UnrealizedSubC2 @end +extern uintptr_t OBJC_CLASS_$_UnrealizedSubC3; +@interface UnrealizedSubC3 : NSObject @end +@implementation UnrealizedSubC3 @end + + +int main(int argc __unused, char **argv) +{ + objc_autoreleasePoolPush(); + + // Hack NSObject's RR methods. + // Don't use runtime functions to do this - + // we want the runtime to think that these are NSObject's real code + { +#if __has_feature(ptrauth_calls) + typedef IMP __ptrauth_objc_method_list_imp MethodListIMP; +#else + typedef IMP MethodListIMP; +#endif + + Class cls = [NSObject class]; + IMP imp = class_getMethodImplementation(cls, @selector(retain)); + MethodListIMP *m = (MethodListIMP *) + class_getInstanceMethod(cls, @selector(retain)); + testassert(m[2] == imp); // verify Method struct is as we expect + + m = (MethodListIMP *)class_getInstanceMethod(cls, @selector(retain)); + m[2] = (IMP)HackRetain; + m = (MethodListIMP *)class_getInstanceMethod(cls, @selector(release)); + m[2] = (IMP)HackRelease; + m = (MethodListIMP *)class_getInstanceMethod(cls, @selector(autorelease)); + m[2] = (IMP)HackAutorelease; + m = (MethodListIMP *)class_getInstanceMethod(cls, @selector(retainCount)); + m[2] = (IMP)HackRetainCount; + m = (MethodListIMP *)class_getClassMethod(cls, @selector(retain)); + m[2] = (IMP)HackPlusRetain; + m = (MethodListIMP *)class_getClassMethod(cls, @selector(release)); + m[2] = (IMP)HackPlusRelease; + m = (MethodListIMP *)class_getClassMethod(cls, @selector(autorelease)); + m[2] = (IMP)HackPlusAutorelease; + m = (MethodListIMP *)class_getClassMethod(cls, @selector(retainCount)); + m[2] = (IMP)HackPlusRetainCount; + m = (MethodListIMP *)class_getClassMethod(cls, @selector(alloc)); + m[2] = (IMP)HackAlloc; + m = (MethodListIMP *)class_getClassMethod(cls, @selector(allocWithZone:)); + m[2] = (IMP)HackAllocWithZone; + + _objc_flush_caches(cls); + + imp = class_getMethodImplementation(cls, @selector(retain)); + testassert(imp == (IMP)HackRetain); // verify hack worked + } + + Class cls = [NSObject class]; + Class icl = [InheritingSub class]; + Class ocl = [OverridingSub class]; + /* + Class oa1 = [OverridingASub class]; + Class oa2 = [OverridingAWZSub class]; + Class oa3 = [OverridingAAWZSub class]; + */ + NSObject *obj = [NSObject new]; + InheritingSub *inh = [InheritingSub new]; + OverridingSub *ovr = [OverridingSub new]; + + Class ccc; + id ooo; + Class cc2; + id oo2; + + void *dlh; + + +#if __x86_64__ + // vtable dispatch can introduce bypass just like the ARC entrypoints +#else + testprintf("method dispatch does not bypass\n"); + zero(); + + [obj retain]; + testassert(Retains == 1); + [obj release]; + testassert(Releases == 1); + [obj autorelease]; + testassert(Autoreleases == 1); + + [cls retain]; + testassert(PlusRetains == 1); + [cls release]; + testassert(PlusReleases == 1); + [cls autorelease]; + testassert(PlusAutoreleases == 1); + + [inh retain]; + testassert(Retains == 2); + [inh release]; + testassert(Releases == 2); + [inh autorelease]; + testassert(Autoreleases == 2); + + [icl retain]; + testassert(PlusRetains == 2); + [icl release]; + testassert(PlusReleases == 2); + [icl autorelease]; + testassert(PlusAutoreleases == 2); + + [ovr retain]; + testassert(SubRetains == 1); + [ovr release]; + testassert(SubReleases == 1); + [ovr autorelease]; + testassert(SubAutoreleases == 1); + + [ocl retain]; + testassert(SubPlusRetains == 1); + [ocl release]; + testassert(SubPlusReleases == 1); + [ocl autorelease]; + testassert(SubPlusAutoreleases == 1); + + [UnrealizedSubA1 retain]; + testassert(PlusRetains == 3); + [UnrealizedSubA2 release]; + testassert(PlusReleases == 3); + [UnrealizedSubA3 autorelease]; + testassert(PlusAutoreleases == 3); +#endif + + + testprintf("objc_msgSend() does not bypass\n"); + zero(); + + id (*retain_fn)(id, SEL) = (id(*)(id, SEL))objc_msgSend; + void (*release_fn)(id, SEL) = (void(*)(id, SEL))objc_msgSend; + id (*autorelease_fn)(id, SEL) = (id(*)(id, SEL))objc_msgSend; + + retain_fn(obj, @selector(retain)); + testassert(Retains == 1); + release_fn(obj, @selector(release)); + testassert(Releases == 1); + autorelease_fn(obj, @selector(autorelease)); + testassert(Autoreleases == 1); + + retain_fn(cls, @selector(retain)); + testassert(PlusRetains == 1); + release_fn(cls, @selector(release)); + testassert(PlusReleases == 1); + autorelease_fn(cls, @selector(autorelease)); + testassert(PlusAutoreleases == 1); + + retain_fn(inh, @selector(retain)); + testassert(Retains == 2); + release_fn(inh, @selector(release)); + testassert(Releases == 2); + autorelease_fn(inh, @selector(autorelease)); + testassert(Autoreleases == 2); + + retain_fn(icl, @selector(retain)); + testassert(PlusRetains == 2); + release_fn(icl, @selector(release)); + testassert(PlusReleases == 2); + autorelease_fn(icl, @selector(autorelease)); + testassert(PlusAutoreleases == 2); + + retain_fn(ovr, @selector(retain)); + testassert(SubRetains == 1); + release_fn(ovr, @selector(release)); + testassert(SubReleases == 1); + autorelease_fn(ovr, @selector(autorelease)); + testassert(SubAutoreleases == 1); + + retain_fn(ocl, @selector(retain)); + testassert(SubPlusRetains == 1); + release_fn(ocl, @selector(release)); + testassert(SubPlusReleases == 1); + autorelease_fn(ocl, @selector(autorelease)); + testassert(SubPlusAutoreleases == 1); + + retain_fn((Class)&OBJC_CLASS_$_UnrealizedSubB1, @selector(retain)); + testassert(PlusRetains == 3); + release_fn((Class)&OBJC_CLASS_$_UnrealizedSubB2, @selector(release)); + testassert(PlusReleases == 3); + autorelease_fn((Class)&OBJC_CLASS_$_UnrealizedSubB3, @selector(autorelease)); + testassert(PlusAutoreleases == 3); + + + testprintf("arc function bypasses instance but not class or override\n"); + zero(); + + objc_retain(obj); + testassert(Retains == 0); + objc_release(obj); + testassert(Releases == 0); + objc_autorelease(obj); + testassert(Autoreleases == 0); + +#if SUPPORT_NONPOINTER_ISA + objc_retain(cls); + testassert(PlusRetains == 0); + objc_release(cls); + testassert(PlusReleases == 0); + objc_autorelease(cls); + testassert(PlusAutoreleases == 0); +#else + objc_retain(cls); + testassert(PlusRetains == 1); + objc_release(cls); + testassert(PlusReleases == 1); + objc_autorelease(cls); + testassert(PlusAutoreleases == 1); +#endif + + objc_retain(inh); + testassert(Retains == 0); + objc_release(inh); + testassert(Releases == 0); + objc_autorelease(inh); + testassert(Autoreleases == 0); + +#if SUPPORT_NONPOINTER_ISA + objc_retain(icl); + testassert(PlusRetains == 0); + objc_release(icl); + testassert(PlusReleases == 0); + objc_autorelease(icl); + testassert(PlusAutoreleases == 0); +#else + objc_retain(icl); + testassert(PlusRetains == 2); + objc_release(icl); + testassert(PlusReleases == 2); + objc_autorelease(icl); + testassert(PlusAutoreleases == 2); +#endif + + objc_retain(ovr); + testassert(SubRetains == 1); + objc_release(ovr); + testassert(SubReleases == 1); + objc_autorelease(ovr); + testassert(SubAutoreleases == 1); + + objc_retain(ocl); + testassert(SubPlusRetains == 1); + objc_release(ocl); + testassert(SubPlusReleases == 1); + objc_autorelease(ocl); + testassert(SubPlusAutoreleases == 1); + +#if SUPPORT_NONPOINTER_ISA + objc_retain((Class)&OBJC_CLASS_$_UnrealizedSubC1); + testassert(PlusRetains == 1); + objc_release((Class)&OBJC_CLASS_$_UnrealizedSubC2); + testassert(PlusReleases == 1); + objc_autorelease((Class)&OBJC_CLASS_$_UnrealizedSubC3); + testassert(PlusAutoreleases == 1); +#else + objc_retain((Class)&OBJC_CLASS_$_UnrealizedSubC1); + testassert(PlusRetains == 3); + objc_release((Class)&OBJC_CLASS_$_UnrealizedSubC2); + testassert(PlusReleases == 3); + objc_autorelease((Class)&OBJC_CLASS_$_UnrealizedSubC3); + testassert(PlusAutoreleases == 3); +#endif + + testprintf("unrelated addMethod does not clobber\n"); + zero(); + + class_addMethod(cls, @selector(unrelatedMethod), (IMP)imp_fn, ""); + + objc_retain(obj); + testassert(Retains == 0); + objc_release(obj); + testassert(Releases == 0); + objc_autorelease(obj); + testassert(Autoreleases == 0); + + + testprintf("add class method does not clobber\n"); + zero(); + + objc_retain(obj); + testassert(Retains == 0); + objc_release(obj); + testassert(Releases == 0); + objc_autorelease(obj); + testassert(Autoreleases == 0); + + class_addMethod(object_getClass(cls), @selector(retain), (IMP)imp_fn, ""); + + objc_retain(obj); + testassert(Retains == 0); + objc_release(obj); + testassert(Releases == 0); + objc_autorelease(obj); + testassert(Autoreleases == 0); + + + testprintf("addMethod clobbers (InheritingSub2, retain)\n"); + zero(); + + ccc = [InheritingSub2 class]; + ooo = [ccc new]; + cc2 = [InheritingSub2_2 class]; + oo2 = [cc2 new]; + + objc_retain(ooo); + testassert(Retains == 0); + objc_release(ooo); + testassert(Releases == 0); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + + objc_retain(oo2); + testassert(Retains == 0); + objc_release(oo2); + testassert(Releases == 0); + objc_autorelease(oo2); + testassert(Autoreleases == 0); + + class_addMethod(ccc, @selector(retain), (IMP)imp_fn, ""); + + objc_retain(ooo); + testassert(Retains == 0); + testassert(Imps == 1); + objc_release(ooo); + testassert(Releases == 1); + objc_autorelease(ooo); + testassert(Autoreleases == 1); + + objc_retain(oo2); + testassert(Retains == 0); + testassert(Imps == 2); + objc_release(oo2); + testassert(Releases == 2); + objc_autorelease(oo2); + testassert(Autoreleases == 2); + + + testprintf("addMethod clobbers (InheritingSub3, release)\n"); + zero(); + + ccc = [InheritingSub3 class]; + ooo = [ccc new]; + cc2 = [InheritingSub3_2 class]; + oo2 = [cc2 new]; + + objc_retain(ooo); + testassert(Retains == 0); + objc_release(ooo); + testassert(Releases == 0); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + + objc_retain(oo2); + testassert(Retains == 0); + objc_release(oo2); + testassert(Releases == 0); + objc_autorelease(oo2); + testassert(Autoreleases == 0); + + class_addMethod(ccc, @selector(release), (IMP)imp_fn, ""); + + objc_retain(ooo); + testassert(Retains == 1); + objc_release(ooo); + testassert(Releases == 0); + testassert(Imps == 1); + objc_autorelease(ooo); + testassert(Autoreleases == 1); + + objc_retain(oo2); + testassert(Retains == 2); + objc_release(oo2); + testassert(Releases == 0); + testassert(Imps == 2); + objc_autorelease(oo2); + testassert(Autoreleases == 2); + + + testprintf("addMethod clobbers (InheritingSub4, autorelease)\n"); + zero(); + + ccc = [InheritingSub4 class]; + ooo = [ccc new]; + cc2 = [InheritingSub4_2 class]; + oo2 = [cc2 new]; + + objc_retain(ooo); + testassert(Retains == 0); + objc_release(ooo); + testassert(Releases == 0); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + + objc_retain(oo2); + testassert(Retains == 0); + objc_release(oo2); + testassert(Releases == 0); + objc_autorelease(oo2); + testassert(Autoreleases == 0); + + class_addMethod(ccc, @selector(autorelease), (IMP)imp_fn, ""); + + objc_retain(ooo); + testassert(Retains == 1); + objc_release(ooo); + testassert(Releases == 1); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + testassert(Imps == 1); + + objc_retain(oo2); + testassert(Retains == 2); + objc_release(oo2); + testassert(Releases == 2); + objc_autorelease(oo2); + testassert(Autoreleases == 0); + testassert(Imps == 2); + + + testprintf("addMethod clobbers (InheritingSub5, retainCount)\n"); + zero(); + + ccc = [InheritingSub5 class]; + ooo = [ccc new]; + cc2 = [InheritingSub5_2 class]; + oo2 = [cc2 new]; + + objc_retain(ooo); + testassert(Retains == 0); + objc_release(ooo); + testassert(Releases == 0); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + + objc_retain(oo2); + testassert(Retains == 0); + objc_release(oo2); + testassert(Releases == 0); + objc_autorelease(oo2); + testassert(Autoreleases == 0); + + class_addMethod(ccc, @selector(retainCount), (IMP)imp_fn, ""); + + objc_retain(ooo); + testassert(Retains == 1); + objc_release(ooo); + testassert(Releases == 1); + objc_autorelease(ooo); + testassert(Autoreleases == 1); + // no bypassing call for -retainCount + + objc_retain(oo2); + testassert(Retains == 2); + objc_release(oo2); + testassert(Releases == 2); + objc_autorelease(oo2); + testassert(Autoreleases == 2); + // no bypassing call for -retainCount + + + testprintf("setSuperclass to clean super does not clobber (InheritingSub6)\n"); + zero(); + + ccc = [InheritingSub6 class]; + ooo = [ccc new]; + cc2 = [InheritingSub6_2 class]; + oo2 = [cc2 new]; + + objc_retain(ooo); + testassert(Retains == 0); + objc_release(ooo); + testassert(Releases == 0); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + + objc_retain(oo2); + testassert(Retains == 0); + objc_release(oo2); + testassert(Releases == 0); + objc_autorelease(oo2); + testassert(Autoreleases == 0); + + class_setSuperclass(ccc, [InheritingSub class]); + + objc_retain(ooo); + testassert(Retains == 0); + objc_release(ooo); + testassert(Releases == 0); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + + objc_retain(oo2); + testassert(Retains == 0); + objc_release(oo2); + testassert(Releases == 0); + objc_autorelease(oo2); + testassert(Autoreleases == 0); + + + testprintf("setSuperclass to dirty super clobbers (InheritingSub7)\n"); + zero(); + + ccc = [InheritingSub7 class]; + ooo = [ccc new]; + cc2 = [InheritingSub7_2 class]; + oo2 = [cc2 new]; + + objc_retain(ooo); + testassert(Retains == 0); + objc_release(ooo); + testassert(Releases == 0); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + + objc_retain(oo2); + testassert(Retains == 0); + objc_release(oo2); + testassert(Releases == 0); + objc_autorelease(oo2); + testassert(Autoreleases == 0); + + class_setSuperclass(ccc, [OverridingSub class]); + + objc_retain(ooo); + testassert(SubRetains == 1); + objc_release(ooo); + testassert(SubReleases == 1); + objc_autorelease(ooo); + testassert(SubAutoreleases == 1); + + objc_retain(oo2); + testassert(SubRetains == 2); + objc_release(oo2); + testassert(SubReleases == 2); + objc_autorelease(oo2); + testassert(SubAutoreleases == 2); + + + testprintf("category replacement of unrelated method does not clobber (InheritingSubCat)\n"); + zero(); + + ccc = [InheritingSubCat class]; + ooo = [ccc new]; + cc2 = [InheritingSubCat_2 class]; + oo2 = [cc2 new]; + + objc_retain(ooo); + testassert(Retains == 0); + objc_release(ooo); + testassert(Releases == 0); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + + objc_retain(oo2); + testassert(Retains == 0); + objc_release(oo2); + testassert(Releases == 0); + objc_autorelease(oo2); + testassert(Autoreleases == 0); + + dlh = dlopen("customrr-cat1.bundle", RTLD_LAZY); + testassert(dlh); + + objc_retain(ooo); + testassert(Retains == 0); + objc_release(ooo); + testassert(Releases == 0); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + + objc_retain(oo2); + testassert(Retains == 0); + objc_release(oo2); + testassert(Releases == 0); + objc_autorelease(oo2); + testassert(Autoreleases == 0); + + + testprintf("category replacement clobbers (InheritingSubCat)\n"); + zero(); + + ccc = [InheritingSubCat class]; + ooo = [ccc new]; + cc2 = [InheritingSubCat_2 class]; + oo2 = [cc2 new]; + + objc_retain(ooo); + testassert(Retains == 0); + objc_release(ooo); + testassert(Releases == 0); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + + objc_retain(oo2); + testassert(Retains == 0); + objc_release(oo2); + testassert(Releases == 0); + objc_autorelease(oo2); + testassert(Autoreleases == 0); + + dlh = dlopen("customrr-cat2.bundle", RTLD_LAZY); + testassert(dlh); + + objc_retain(ooo); + testassert(Retains == 1); + objc_release(ooo); + testassert(Releases == 1); + objc_autorelease(ooo); + testassert(Autoreleases == 1); + + objc_retain(oo2); + testassert(Retains == 2); + objc_release(oo2); + testassert(Releases == 2); + objc_autorelease(oo2); + testassert(Autoreleases == 2); + + + testprintf("allocateClassPair with clean super does not clobber\n"); + zero(); + + objc_retain(inh); + testassert(Retains == 0); + objc_release(inh); + testassert(Releases == 0); + objc_autorelease(inh); + testassert(Autoreleases == 0); + + ccc = objc_allocateClassPair([InheritingSub class], "CleanClassPair", 0); + objc_registerClassPair(ccc); + ooo = [ccc new]; + + objc_retain(inh); + testassert(Retains == 0); + objc_release(inh); + testassert(Releases == 0); + objc_autorelease(inh); + testassert(Autoreleases == 0); + + objc_retain(ooo); + testassert(Retains == 0); + objc_release(ooo); + testassert(Releases == 0); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + + + testprintf("allocateClassPair with clobbered super clobbers\n"); + zero(); + + ccc = objc_allocateClassPair([OverridingSub class], "DirtyClassPair", 0); + objc_registerClassPair(ccc); + ooo = [ccc new]; + + objc_retain(ooo); + testassert(SubRetains == 1); + objc_release(ooo); + testassert(SubReleases == 1); + objc_autorelease(ooo); + testassert(SubAutoreleases == 1); + + + testprintf("allocateClassPair with clean super and override clobbers\n"); + zero(); + + ccc = objc_allocateClassPair([InheritingSub class], "Dirty2ClassPair", 0); + class_addMethod(ccc, @selector(autorelease), (IMP)imp_fn, ""); + objc_registerClassPair(ccc); + ooo = [ccc new]; + + objc_retain(ooo); + testassert(Retains == 1); + objc_release(ooo); + testassert(Releases == 1); + objc_autorelease(ooo); + testassert(Autoreleases == 0); + testassert(Imps == 1); + + + // method_setImplementation and method_exchangeImplementations only + // clobber when manipulating NSObject. We can only test one at a time. + // To test both, we need two tests: customrr and customrr2. + + // These tests also check recursive clobber. + +#if TEST_EXCHANGEIMPLEMENTATIONS + testprintf("exchangeImplementations clobbers (recursive)\n"); +#else + testprintf("setImplementation clobbers (recursive)\n"); +#endif + zero(); + + objc_retain(obj); + testassert(Retains == 0); + objc_release(obj); + testassert(Releases == 0); + objc_autorelease(obj); + testassert(Autoreleases == 0); + + objc_retain(inh); + testassert(Retains == 0); + objc_release(inh); + testassert(Releases == 0); + objc_autorelease(inh); + testassert(Autoreleases == 0); + + Method meth = class_getInstanceMethod(cls, @selector(retainCount)); + testassert(meth); +#if TEST_EXCHANGEIMPLEMENTATIONS + method_exchangeImplementations(meth, meth); +#else + method_setImplementation(meth, (IMP)imp_fn); +#endif + + objc_retain(obj); + testassert(Retains == 1); + objc_release(obj); + testassert(Releases == 1); + objc_autorelease(obj); + testassert(Autoreleases == 1); + + objc_retain(inh); + testassert(Retains == 2); + objc_release(inh); + testassert(Releases == 2); + objc_autorelease(inh); + testassert(Autoreleases == 2); + + + // do not add more tests here - the recursive test must be LAST + + succeed(basename(argv[0])); +} diff --git a/test/customrr2.m b/test/customrr2.m new file mode 100644 index 0000000..3f28493 --- /dev/null +++ b/test/customrr2.m @@ -0,0 +1,9 @@ +// These options must match customrr.m +// TEST_CONFIG MEM=mrc +/* +TEST_BUILD + $C{COMPILE} $DIR/customrr.m -fvisibility=default -o customrr2.exe -DTEST_EXCHANGEIMPLEMENTATIONS=1 -fno-objc-convert-messages-to-runtime-calls + $C{COMPILE} -bundle -bundle_loader customrr2.exe $DIR/customrr-cat1.m -o customrr-cat1.bundle + $C{COMPILE} -bundle -bundle_loader customrr2.exe $DIR/customrr-cat2.m -o customrr-cat2.bundle +END +*/ diff --git a/test/definitions.c b/test/definitions.c new file mode 100644 index 0000000..570abb0 --- /dev/null +++ b/test/definitions.c @@ -0,0 +1,54 @@ +// TEST_CONFIG + +// DO NOT include anything else here +#include +// DO NOT include anything else here +Class c = Nil; +SEL s; +IMP i; +id o = nil; +BOOL b = YES; +BOOL b2 = NO; +#if !__has_feature(objc_arc) +__strong void *p; +#endif +id __unsafe_unretained u; +#if __has_feature(objc_arc_weak) +id __weak w; +#endif + +void fn(void) __unused; +void fn(void) { + id __autoreleasing a __unused; +} + +// check type inference for blocks returning YES and NO (rdar://10118972) +BOOL (^block1)(void) = ^{ return YES; }; +BOOL (^block2)(void) = ^{ return NO; }; + +#include "test.h" + +int main() +{ + testassert(YES); + testassert(!NO); +#if __cplusplus + testwarn("rdar://12371870 -Wnull-conversion"); + testassert(!(bool)nil); + testassert(!(bool)Nil); +#else + testassert(!nil); + testassert(!Nil); +#endif + +#if __has_feature(objc_bool) + // YES[array] is disallowed for objc just as true[array] is for C++ +#else + // this will fail if YES and NO do not have enough parentheses + int array[2] = { 888, 999 }; + testassert(NO[array] == 888); + testassert(YES[array] == 999); +#endif + + succeed(__FILE__); +} diff --git a/test/designatedinit.m b/test/designatedinit.m new file mode 100644 index 0000000..232625f --- /dev/null +++ b/test/designatedinit.m @@ -0,0 +1,26 @@ +// TEST_CONFIG +/* TEST_BUILD_OUTPUT +.*designatedinit.m:\d+:\d+: warning: designated initializer should only invoke a designated initializer on 'super'.* +.*designatedinit.m:\d+:\d+: note: .* +.*designatedinit.m:\d+:\d+: warning: method override for the designated initializer of the superclass '-init' not found.* +.*NSObject.h:\d+:\d+: note: .* +END */ + +#define NS_ENFORCE_NSOBJECT_DESIGNATED_INITIALIZER 1 +#include "test.h" +#include + +@interface C : NSObject +-(id) initWithInt:(int)i NS_DESIGNATED_INITIALIZER; +@end + +@implementation C +-(id) initWithInt:(int)__unused i { + return [self init]; +} +@end + +int main() +{ + succeed(__FILE__); +} diff --git a/test/duplicateClass.m b/test/duplicateClass.m new file mode 100644 index 0000000..7c44f07 --- /dev/null +++ b/test/duplicateClass.m @@ -0,0 +1,150 @@ +// TEST_CFLAGS -Wno-deprecated-declarations -Wl,-no_objc_category_merging + +#include "test.h" +#include "testroot.i" +#include + +static int state; + +@protocol Proto ++(void)classMethod; +-(void)instanceMethod; +@end + +@interface Super : TestRoot { + int i; +} +@property int i; +@end + +@implementation Super +@synthesize i; + ++(void)classMethod { + state = 1; +} + +-(void)instanceMethod { + state = 3; +} + +@end + + +#if __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation" +#endif + +@implementation Super (Category) + ++(void)classMethod { + state = 2; +} + +-(void)instanceMethod { + state = 4; +} + +@end + +#if __clang__ +#pragma clang diagnostic pop +#endif + + +int main() +{ + Class clone; + Class cls; + Method *m1, *m2; + int i; + + cls = [Super class]; + clone = objc_duplicateClass(cls, "Super_copy", 0); + + testassert(clone != cls); + testassert(object_getClass(clone) == object_getClass(cls)); + testassert(class_getSuperclass(clone) == class_getSuperclass(cls)); + testassert(class_getVersion(clone) == class_getVersion(cls)); + testassert(class_isMetaClass(clone) == class_isMetaClass(cls)); + testassert(class_getIvarLayout(clone) == class_getIvarLayout(cls)); + testassert(class_getWeakIvarLayout(clone) == class_getWeakIvarLayout(cls)); + + // Check method list + + m1 = class_copyMethodList(cls, NULL); + m2 = class_copyMethodList(clone, NULL); + testassert(m1); + testassert(m2); + for (i = 0; m1[i] && m2[i]; i++) { + testassert(m1[i] != m2[i]); // method list must be deep-copied + testassert(method_getName(m1[i]) == method_getName(m2[i])); + testassert(method_getImplementation(m1[i]) == method_getImplementation(m2[i])); + testassert(method_getTypeEncoding(m1[i]) == method_getTypeEncoding(m2[i])); + } + testassert(m1[i] == NULL && m2[i] == NULL); + free(m1); + free(m2); + + // Check ivar list + Ivar *i1 = class_copyIvarList(cls, NULL); + Ivar *i2 = class_copyIvarList(clone, NULL); + testassert(i1); + testassert(i2); + for (i = 0; i1[i] && i2[i]; i++) { + testassert(i1[i] == i2[i]); // ivars are not deep-copied + } + testassert(i1[i] == NULL && i2[i] == NULL); + free(i1); + free(i2); + + // Check protocol list + Protocol * __unsafe_unretained *p1 = class_copyProtocolList(cls, NULL); + Protocol * __unsafe_unretained *p2 = class_copyProtocolList(clone, NULL); + testassert(p1); + testassert(p2); + for (i = 0; p1[i] && p2[i]; i++) { + testassert(p1[i] == p2[i]); // protocols are not deep-copied + } + testassert(p1[i] == NULL && p2[i] == NULL); + free(p1); + free(p2); + + // Check property list + objc_property_t *o1 = class_copyPropertyList(cls, NULL); + objc_property_t *o2 = class_copyPropertyList(clone, NULL); + testassert(o1); + testassert(o2); + for (i = 0; o1[i] && o2[i]; i++) { + testassert(o1[i] == o2[i]); // properties are not deep-copied + } + testassert(o1[i] == NULL && o2[i] == NULL); + free(o1); + free(o2); + + // Check method calls + + state = 0; + [cls classMethod]; + testassert(state == 2); + state = 0; + [clone classMethod]; + testassert(state == 2); + + // #4511660 Make sure category implementation is still the preferred one + id obj; + obj = [cls new]; + state = 0; + [obj instanceMethod]; + testassert(state == 4); + RELEASE_VAR(obj); + + obj = [clone new]; + state = 0; + [obj instanceMethod]; + testassert(state == 4); + RELEASE_VAR(obj); + + succeed(__FILE__); +} diff --git a/test/duplicateProtocols.m b/test/duplicateProtocols.m new file mode 100644 index 0000000..1cb5a76 --- /dev/null +++ b/test/duplicateProtocols.m @@ -0,0 +1,42 @@ + +#include "test.h" + +// This test assumes +// - that on launch we don't have NSCoding, NSSecureCoding, or NSDictionary, ie, libSystem doesn't contain those +// - that after dlopening CF, we get NSDictionary and it conforms to NSSecureCoding which conforms to NSCoding +// - that our NSCoding will be used if we ask either NSSecureCoding or NSDictionary if they conform to our test protocol + +@protocol NewNSCodingSuperProto +@end + +@protocol NSCoding +@end + +int main() +{ + // Before we dlopen, make sure we are using our NSCoding, not the shared cache version + Protocol* codingSuperProto = objc_getProtocol("NewNSCodingSuperProto"); + Protocol* codingProto = objc_getProtocol("NSCoding"); + if (@protocol(NewNSCodingSuperProto) != codingSuperProto) fail("Protocol mismatch"); + if (@protocol(NSCoding) != codingProto) fail("Protocol mismatch"); + if (!protocol_conformsToProtocol(codingProto, codingSuperProto)) fail("Our NSCoding should conform to NewNSCodingSuperProto"); + + // Also make sure we don't yet have an NSSecureCoding or NSDictionary + if (objc_getProtocol("NSSecureCoding")) fail("Test assumes we don't have NSSecureCoding yet"); + if (objc_getClass("NSDictionary")) fail("Test assumes we don't have NSDictionary yet"); + + void *dl = dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY); + if (!dl) fail("couldn't open CoreFoundation"); + + // We should now have NSSecureCoding and NSDictionary + Protocol* secureCodingProto = objc_getProtocol("NSSecureCoding"); + id dictionaryClass = objc_getClass("NSDictionary"); + if (!secureCodingProto) fail("Should have got NSSecureCoding from CoreFoundation"); + if (!dictionaryClass) fail("Should have got NSDictionary from CoreFoundation"); + + // Now make sure that NSDictionary and NSSecureCoding find our new protocols + if (!protocol_conformsToProtocol(secureCodingProto, codingProto)) fail("NSSecureCoding should conform to our NSCoding"); + if (!protocol_conformsToProtocol(secureCodingProto, codingSuperProto)) fail("NSSecureCoding should conform to our NewNSCodingSuperProto"); + if (!class_conformsToProtocol(dictionaryClass, codingProto)) fail("NSDictionary should conform to our NSCoding"); + if (!class_conformsToProtocol(dictionaryClass, codingSuperProto)) fail("NSDictionary should conform to our NewNSCodingSuperProto"); +} diff --git a/test/duplicatedClasses.m b/test/duplicatedClasses.m new file mode 100644 index 0000000..f5acb89 --- /dev/null +++ b/test/duplicatedClasses.m @@ -0,0 +1,26 @@ +// fixme rdar://24624435 duplicate class warning fails with the shared cache +// OBJC_DISABLE_PREOPTIMIZATION=YES works around that problem. + +// TEST_ENV OBJC_DEBUG_DUPLICATE_CLASSES=YES OBJC_DISABLE_PREOPTIMIZATION=YES +// TEST_CRASHES +/* +TEST_RUN_OUTPUT +objc\[\d+\]: Class [^\s]+ is implemented in both .+ \(0x[0-9a-f]+\) and .+ \(0x[0-9a-f]+\)\. One of the two will be used\. Which one is undefined\. +objc\[\d+\]: HALTED +OR +OK: duplicatedClasses.m +END + */ + +#include "test.h" +#include "testroot.i" + +@interface WKWebView : TestRoot @end +@implementation WKWebView @end + +int main() +{ + void *dl = dlopen("/System/Library/Frameworks/WebKit.framework/WebKit", RTLD_LAZY); + if (!dl) fail("couldn't open WebKit"); + fail("should have crashed already"); +} diff --git a/test/evil-class-def.m b/test/evil-class-def.m new file mode 100644 index 0000000..c49bda8 --- /dev/null +++ b/test/evil-class-def.m @@ -0,0 +1,321 @@ +#include + +#if __LP64__ +# define PTR " .quad " +# define PTRSIZE "8" +# define LOGPTRSIZE "3" +#else +# define PTR " .long " +# define PTRSIZE "4" +# define LOGPTRSIZE "2" +#endif + +#if __has_feature(ptrauth_calls) +# define SIGNED_METHOD_LIST_IMP "@AUTH(ia,0,addr) " +#else +# define SIGNED_METHOD_LIST_IMP +#endif + +#define str(x) #x +#define str2(x) str(x) + +__BEGIN_DECLS +// not id to avoid ARC operations because the class doesn't implement RR methods +void* nop(void* self) { return self; } +__END_DECLS + +asm( + ".globl _OBJC_CLASS_$_Super \n" + ".section __DATA,__objc_data \n" + ".align 3 \n" + "_OBJC_CLASS_$_Super: \n" + PTR "_OBJC_METACLASS_$_Super \n" + PTR "0 \n" + PTR "__objc_empty_cache \n" + PTR "0 \n" + PTR "L_ro \n" + // pad to OBJC_MAX_CLASS_SIZE + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + "" + "_OBJC_METACLASS_$_Super: \n" + PTR "_OBJC_METACLASS_$_Super \n" + PTR "_OBJC_CLASS_$_Super \n" + PTR "__objc_empty_cache \n" + PTR "0 \n" + PTR "L_meta_ro \n" + // pad to OBJC_MAX_CLASS_SIZE + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + "" + "L_ro: \n" + ".long 2 \n" + ".long 0 \n" + ".long " PTRSIZE " \n" +#if __LP64__ + ".long 0 \n" +#endif + PTR "0 \n" + PTR "L_super_name \n" +#if EVIL_SUPER + PTR "L_evil_methods \n" +#else + PTR "L_good_methods \n" +#endif + PTR "0 \n" + PTR "L_super_ivars \n" + PTR "0 \n" + PTR "0 \n" + "" + "L_meta_ro: \n" + ".long 3 \n" + ".long 40 \n" + ".long 40 \n" +#if __LP64__ + ".long 0 \n" +#endif + PTR "0 \n" + PTR "L_super_name \n" +#if EVIL_SUPER_META + PTR "L_evil_methods \n" +#else + PTR "L_good_methods \n" +#endif + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + + ".globl _OBJC_CLASS_$_Sub \n" + ".section __DATA,__objc_data \n" + ".align 3 \n" + "_OBJC_CLASS_$_Sub: \n" + PTR "_OBJC_METACLASS_$_Sub \n" + PTR "_OBJC_CLASS_$_Super \n" + PTR "__objc_empty_cache \n" + PTR "0 \n" + PTR "L_sub_ro \n" + // pad to OBJC_MAX_CLASS_SIZE + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + "" + "_OBJC_METACLASS_$_Sub: \n" + PTR "_OBJC_METACLASS_$_Super \n" + PTR "_OBJC_METACLASS_$_Super \n" + PTR "__objc_empty_cache \n" + PTR "0 \n" + PTR "L_sub_meta_ro \n" + // pad to OBJC_MAX_CLASS_SIZE + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + "" + "L_sub_ro: \n" + ".long 2 \n" + ".long 0 \n" + ".long " PTRSIZE " \n" +#if __LP64__ + ".long 0 \n" +#endif + PTR "0 \n" + PTR "L_sub_name \n" +#if EVIL_SUB + PTR "L_evil_methods \n" +#else + PTR "L_good_methods \n" +#endif + PTR "0 \n" + PTR "L_sub_ivars \n" + PTR "0 \n" + PTR "0 \n" + "" + "L_sub_meta_ro: \n" + ".long 3 \n" + ".long 40 \n" + ".long 40 \n" +#if __LP64__ + ".long 0 \n" +#endif + PTR "0 \n" + PTR "L_sub_name \n" +#if EVIL_SUB_META + PTR "L_evil_methods \n" +#else + PTR "L_good_methods \n" +#endif + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + PTR "0 \n" + + "L_evil_methods: \n" + ".long 3*" PTRSIZE " \n" + ".long 1 \n" + PTR "L_load \n" + PTR "L_load \n" + PTR "_abort" SIGNED_METHOD_LIST_IMP "\n" + // assumes that abort is inside the dyld shared cache + + "L_good_methods: \n" + ".long 3*" PTRSIZE " \n" + ".long 2 \n" + PTR "L_load \n" + PTR "L_load \n" + PTR "_nop" SIGNED_METHOD_LIST_IMP "\n" + PTR "L_self \n" + PTR "L_self \n" + PTR "_nop" SIGNED_METHOD_LIST_IMP "\n" + + "L_super_ivars: \n" + ".long 4*" PTRSIZE " \n" + ".long 1 \n" + PTR "L_super_ivar_offset \n" + PTR "L_super_ivar_name \n" + PTR "L_super_ivar_type \n" + ".long " LOGPTRSIZE " \n" + ".long " PTRSIZE " \n" + + "L_sub_ivars: \n" + ".long 4*" PTRSIZE " \n" + ".long 1 \n" + PTR "L_sub_ivar_offset \n" + PTR "L_sub_ivar_name \n" + PTR "L_sub_ivar_type \n" + ".long " LOGPTRSIZE " \n" + ".long " PTRSIZE " \n" + + "L_super_ivar_offset: \n" + ".long 0 \n" + "L_sub_ivar_offset: \n" + ".long " PTRSIZE " \n" + + ".cstring \n" + "L_super_name: .ascii \"Super\\0\" \n" + "L_sub_name: .ascii \"Sub\\0\" \n" + "L_load: .ascii \"load\\0\" \n" + "L_self: .ascii \"self\\0\" \n" + "L_super_ivar_name: .ascii \"super_ivar\\0\" \n" + "L_super_ivar_type: .ascii \"c\\0\" \n" + "L_sub_ivar_name: .ascii \"sub_ivar\\0\" \n" + "L_sub_ivar_type: .ascii \"@\\0\" \n" + + + ".section __DATA,__objc_classlist \n" +#if !OMIT_SUPER + PTR "_OBJC_CLASS_$_Super \n" +#endif +#if !OMIT_SUB + PTR "_OBJC_CLASS_$_Sub \n" +#endif + + ".section __DATA,__objc_nlclslist \n" +#if !OMIT_NL_SUPER + PTR "_OBJC_CLASS_$_Super \n" +#endif +#if !OMIT_NL_SUB + PTR "_OBJC_CLASS_$_Sub \n" +#endif + + ".text \n" +); + +void fn(void) { } diff --git a/test/exc.m b/test/exc.m new file mode 100644 index 0000000..8c7435a --- /dev/null +++ b/test/exc.m @@ -0,0 +1,879 @@ +/* +need exception-safe ARC for exception deallocation tests +TEST_CFLAGS -fobjc-arc-exceptions -framework Foundation +*/ + +#include "test.h" +#include "testroot.i" +#include +#include + +static volatile int state = 0; +static volatile int dealloced = 0; +#define BAD 1000000 + +#if defined(USE_FOUNDATION) + +#include + +@interface Super : NSException @end +@implementation Super ++(id)exception { return AUTORELEASE([[self alloc] initWithName:@"Super" reason:@"reason" userInfo:nil]); } +-(void)check { state++; } ++(void)check { testassert(!"caught class object, not instance"); } +-(void)dealloc { dealloced++; SUPER_DEALLOC(); } +@end + +#define FILENAME "nsexc.m" + +#else + +@interface Super : TestRoot @end +@implementation Super ++(id)exception { return AUTORELEASE([self new]); } +-(void)check { state++; } ++(void)check { testassert(!"caught class object, not instance"); } +-(void)dealloc { dealloced++; SUPER_DEALLOC(); } +@end + +#define FILENAME "exc.m" + +#endif + +@interface Sub : Super @end +@implementation Sub +@end + + +#if TARGET_OS_OSX +void altHandlerFail(id unused __unused, void *context __unused) +{ + fail("altHandlerFail called"); +} + +#define ALT_HANDLER(n) \ + void altHandler##n(id unused __unused, void *context) \ + { \ + testassert(context == (void*)&altHandler##n); \ + testassert(state == n); \ + state++; \ + } + +ALT_HANDLER(1) +ALT_HANDLER(2) +ALT_HANDLER(3) +ALT_HANDLER(4) +ALT_HANDLER(5) +ALT_HANDLER(6) +ALT_HANDLER(7) + + +static void throwWithAltHandler(void) __attribute__((noinline, used)); +static void throwWithAltHandler(void) +{ + @try { + state++; + uintptr_t token = objc_addExceptionHandler(altHandler3, (void*)altHandler3); + // state++ inside alt handler + @throw [Super exception]; + state = BAD; + objc_removeExceptionHandler(token); + } + @catch (Sub *e) { + state = BAD; + } + state = BAD; +} + + +static void throwWithAltHandlerAndRethrow(void) __attribute__((noinline, used)); +static void throwWithAltHandlerAndRethrow(void) +{ + @try { + state++; + uintptr_t token = objc_addExceptionHandler(altHandler3, (void*)altHandler3); + // state++ inside alt handler + @throw [Super exception]; + state = BAD; + objc_removeExceptionHandler(token); + } + @catch (...) { + testassert(state == 4); + state++; + @throw; + } + state = BAD; +} + +#endif + +#if __cplusplus +#include +void terminator() { + succeed(FILENAME); +} +#endif + + +#define TEST(code) \ + do { \ + testonthread(^{ PUSH_POOL { code } POP_POOL; }); \ + testcollect(); \ + } while (0) + + + +int main() +{ + testprintf("try-catch-finally, exception caught exactly\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @catch (Super *e) { + state++; + [e check]; // state++ + } + @finally { + state++; + } + state++; + } + @catch (...) { + state = BAD; + } + }); + testassert(state == 6); + testassert(dealloced == 1); + + + testprintf("try-finally, no exception thrown\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + } + @finally { + state++; + } + state++; + } + @catch (...) { + state = BAD; + } + }); + testassert(state == 4); + testassert(dealloced == 0); + + + testprintf("try-finally, with exception\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @finally { + state++; + } + state = BAD; + } + @catch (id e) { + state++; + [e check]; // state++ + } + }); + testassert(state == 5); + testassert(dealloced == 1); + + + testprintf("try-finally, with autorelease pool pop during unwind\n"); + // Popping an autorelease pool during unwind used to deallocate the + // exception object, but now we retain them while in flight. + + // This use-after-free is undetected without MallocScribble or guardmalloc. + if (!getenv("MallocScribble") && + (!getenv("DYLD_INSERT_LIBRARIES") || + !strstr(getenv("DYLD_INSERT_LIBRARIES"), "libgmalloc"))) + { + testwarn("MallocScribble not set"); + } + + TEST({ + state = 0; + dealloced = 0; + @try { + void *pool2 = objc_autoreleasePoolPush(); + state++; + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @finally { + state++; + objc_autoreleasePoolPop(pool2); + } + state = BAD; + } + @catch (id e) { + state++; + [e check]; // state++ + } + }); + testassert(state == 5); + testassert(dealloced == 1); + + + testprintf("try-catch-finally, no exception\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + } + @catch (...) { + state = BAD; + } + @finally { + state++; + } + state++; + } @catch (...) { + state = BAD; + } + }); + testassert(state == 4); + testassert(dealloced == 0); + + + testprintf("try-catch-finally, exception not caught\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @catch (Sub *e) { + state = BAD; + } + @finally { + state++; + } + state = BAD; + } + @catch (id e) { + state++; + [e check]; // state++ + } + }); + testassert(state == 5); + testassert(dealloced == 1); + + + testprintf("try-catch-finally, exception caught exactly, rethrown\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @catch (Super *e) { + state++; + [e check]; // state++ + @throw; + state = BAD; + } + @finally { + state++; + } + state = BAD; + } + @catch (id e) { + state++; + [e check]; // state++ + } + }); + testassert(state == 7); + testassert(dealloced == 1); + + + testprintf("try-catch, no exception\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + } + @catch (...) { + state = BAD; + } + state++; + } @catch (...) { + state = BAD; + } + }); + testassert(state == 3); + testassert(dealloced == 0); + + + testprintf("try-catch, exception not caught\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @catch (Sub *e) { + state = BAD; + } + state = BAD; + } + @catch (id e) { + state++; + [e check]; // state++ + } + }); + testassert(state == 4); + testassert(dealloced == 1); + + + testprintf("try-catch, exception caught exactly\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @catch (Super *e) { + state++; + [e check]; // state++ + } + state++; + } + @catch (...) { + state = BAD; + } + }); + testassert(state == 5); + testassert(dealloced == 1); + + + testprintf("try-catch, exception caught exactly, rethrown\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @catch (Super *e) { + state++; + [e check]; // state++ + @throw; + state = BAD; + } + state = BAD; + } + @catch (id e) { + state++; + [e check]; // state++ + } + }); + testassert(state == 6); + testassert(dealloced == 1); + + + testprintf("try-catch, exception caught exactly, thrown again explicitly\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @catch (Super *e) { + state++; + [e check]; // state++ + @throw e; + state = BAD; + } + state = BAD; + } + @catch (id e) { + state++; + [e check]; // state++ + } + }); + testassert(state == 6); + testassert(dealloced == 1); + + + testprintf("try-catch, default catch, rethrown\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @catch (...) { + state++; + @throw; + state = BAD; + } + state = BAD; + } + @catch (id e) { + state++; + [e check]; // state++ + } + }); + testassert(state == 5); + testassert(dealloced == 1); + + + testprintf("try-catch, default catch, rethrown and caught inside nested handler\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @catch (...) { + state++; + + @try { + state++; + @throw; + state = BAD; + } @catch (Sub *e) { + state = BAD; + } @catch (Super *e) { + state++; + [e check]; // state++ + } @catch (...) { + state = BAD; + } @finally { + state++; + } + + state++; + } + state++; + } + @catch (...) { + state = BAD; + } + }); + testassert(state == 9); + testassert(dealloced == 1); + + + testprintf("try-catch, default catch, rethrown inside nested handler but not caught\n"); + + TEST({ + state = 0; + dealloced = 0; + @try { + state++; + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @catch (...) { + state++; + + @try { + state++; + @throw; + state = BAD; + } + @catch (Sub *e) { + state = BAD; + } + @finally { + state++; + } + + state = BAD; + } + state = BAD; + } + @catch (id e) { + state++; + [e check]; // state++ + } + }); + testassert(state == 7); + testassert(dealloced == 1); + + +#if __cplusplus + testprintf("C++ try/catch, Objective-C exception superclass\n"); + + TEST({ + state = 0; + dealloced = 0; + try { + state++; + try { + state++; + try { + state++; + @throw [Super exception]; + state = BAD; + } catch (...) { + state++; + throw; + state = BAD; + } + state = BAD; + } catch (void *e) { + state = BAD; + } catch (int e) { + state = BAD; + } catch (Sub *e) { + state = BAD; + } catch (Super *e) { + state++; + [e check]; // state++ + throw; + } catch (...) { + state = BAD; + } + } catch (id e) { + state++; + [e check]; // state++; + } + }); + testassert(state == 8); + testassert(dealloced == 1); + + + testprintf("C++ try/catch, Objective-C exception subclass\n"); + + TEST({ + state = 0; + dealloced = 0; + try { + state++; + try { + state++; + try { + state++; + @throw [Sub exception]; + state = BAD; + } catch (...) { + state++; + throw; + state = BAD; + } + state = BAD; + } catch (void *e) { + state = BAD; + } catch (int e) { + state = BAD; + } catch (Super *e) { + state++; + [e check]; // state++ + throw; + } catch (Sub *e) { + state = BAD; + } catch (...) { + state = BAD; + } + } catch (id e) { + state++; + [e check]; // state++; + } + }); + testassert(state == 8); + testassert(dealloced == 1); + +#endif + + +#if !TARGET_OS_OSX + // alt handlers are for macOS only +#else + { + // alt handlers + // run a lot to catch failed unregistration (runtime complains at 1000) +#define ALT_HANDLER_REPEAT 2000 + + testprintf("alt handler, no exception\n"); + + TEST({ + dealloced = 0; + for (int i = 0; i < ALT_HANDLER_REPEAT; i++) { + state = 0; + @try { + state++; + @try { + uintptr_t token = objc_addExceptionHandler(altHandlerFail, 0); + state++; + objc_removeExceptionHandler(token); + } + @catch (...) { + state = BAD; + } + state++; + } @catch (...) { + state = BAD; + } + testassert(state == 3); + } + }); + testassert(dealloced == 0); + + + testprintf("alt handler, exception thrown through\n"); + + TEST({ + dealloced = 0; + for (int i = 0; i < ALT_HANDLER_REPEAT; i++) { + state = 0; + @try { + state++; + @try { + state++; + uintptr_t token = objc_addExceptionHandler(altHandler2, (void*)altHandler2); + // state++ inside alt handler + @throw [Super exception]; + state = BAD; + objc_removeExceptionHandler(token); + } + @catch (Sub *e) { + state = BAD; + } + state = BAD; + } + @catch (id e) { + testassert(state == 3); + state++; + [e check]; // state++ + } + testassert(state == 5); + } + }); + testassert(dealloced == ALT_HANDLER_REPEAT); + + + testprintf("alt handler, nested\n"); +#if 1 + testwarn("fixme compiler no longer cooperative for local nested?"); + // Nested alt handlers inside the same function require that each + // catch group have its own landing pad descriptor. The compiler is + // probably not doing that anymore. For now we assume that the + // combination of nested exception handlers and alt handlers is + // rare enough that nobody cares. +#else + TEST({ + dealloced = 0; + for (int i = 0; i < ALT_HANDLER_REPEAT; i++) { + state = 0; + @try { + state++; + @try { + state++; + // same-level handlers called in FIFO order (not stack-like) + uintptr_t token = objc_addExceptionHandler(altHandler4, (void*)altHandler4); + // state++ inside alt handler + uintptr_t token2 = objc_addExceptionHandler(altHandler5, (void*)altHandler5); + // state++ inside alt handler + throwWithAltHandler(); // state += 2 inside + state = BAD; + objc_removeExceptionHandler(token); + objc_removeExceptionHandler(token2); + } + @catch (id e) { + testassert(state == 6); + state++; + [e check]; // state++; + } + state++; + } + @catch (...) { + state = BAD; + } + testassert(state == 9); + } + }); + testassert(dealloced == ALT_HANDLER_REPEAT); +#endif + + testprintf("alt handler, nested, rethrows in between\n"); +#if 1 + testwarn("fixme compiler no longer cooperative for local nested?"); + // See above. +#else + TEST({ + dealloced = 0; + for (int i = 0; i < ALT_HANDLER_REPEAT; i++) { + state = 0; + @try { + state++; + @try { + state++; + // same-level handlers called in FIFO order (not stack-like) + uintptr_t token = objc_addExceptionHandler(altHandler5, (void*)altHandler5); + // state++ inside alt handler + uintptr_t token2 = objc_addExceptionHandler(altHandler6, (void*)altHandler6); + // state++ inside alt handler + throwWithAltHandlerAndRethrow(); // state += 3 inside + state = BAD; + objc_removeExceptionHandler(token); + objc_removeExceptionHandler(token2); + } + @catch (...) { + testassert(state == 7); + state++; + @throw; + } + state = BAD; + } + @catch (id e) { + testassert(state == 8); + state++; + [e check]; // state++ + } + testassert(state == 10); + } + }); + testassert(dealloced == ALT_HANDLER_REPEAT); +#endif + + testprintf("alt handler, exception thrown and caught inside\n"); + + TEST({ + dealloced = 0; + for (int i = 0; i < ALT_HANDLER_REPEAT; i++) { + state = 0; + @try { + state++; + uintptr_t token = objc_addExceptionHandler(altHandlerFail, 0); + @try { + state++; + @throw [Super exception]; + state = BAD; + } + @catch (Super *e) { + state++; + [e check]; // state++ + } + state++; + objc_removeExceptionHandler(token); + } + @catch (...) { + state = BAD; + } + testassert(state == 5); + } + }); + testassert(dealloced == ALT_HANDLER_REPEAT); + + +#if defined(USE_FOUNDATION) + testprintf("alt handler, rdar://10055775\n"); + + TEST({ + dealloced = 0; + for (int i = 0; i < ALT_HANDLER_REPEAT; i++) { + state = 0; + @try { + uintptr_t token = objc_addExceptionHandler(altHandler1, (void*)altHandler1); + { + id x = [NSArray array]; + x = [NSArray array]; + } + state++; + // state++ inside alt handler + [Super raise:@"foo" format:@"bar"]; + state = BAD; + objc_removeExceptionHandler(token); + } @catch (id e) { + state++; + testassert(state == 3); + } + testassert(state == 3); + } + }); + testassert(dealloced == ALT_HANDLER_REPEAT); + +// defined(USE_FOUNDATION) +#endif + + } +// alt handlers +#endif + +#if __cplusplus + std::set_terminate(terminator); + objc_terminate(); + fail("should not have returned from objc_terminate()"); +#else + succeed(FILENAME); +#endif +} + diff --git a/test/exchangeImp.m b/test/exchangeImp.m new file mode 100644 index 0000000..da84f94 --- /dev/null +++ b/test/exchangeImp.m @@ -0,0 +1,106 @@ +/* +TEST_BUILD_OUTPUT +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +.*exchangeImp.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'checkExchange')? +END +*/ + +#include "test.h" +#include "testroot.i" +#include + +static int state; + +#define ONE 1 +#define TWO 2 +#define LENGTH 3 +#define COUNT 4 + +@interface Super : TestRoot @end +@implementation Super ++(void) one { state = ONE; } ++(void) two { state = TWO; } ++(void) length { state = LENGTH; } ++(void) count { state = COUNT; } +@end + +#define checkExchange(s1, v1, s2, v2) \ + do { \ + Method m1, m2; \ + \ + testprintf("Check unexchanged version\n"); \ + state = 0; \ + [Super s1]; \ + testassert(state == v1); \ + state = 0; \ + [Super s2]; \ + testassert(state == v2); \ + \ + testprintf("Exchange\n"); \ + m1 = class_getClassMethod([Super class], @selector(s1)); \ + m2 = class_getClassMethod([Super class], @selector(s2)); \ + testassert(m1); \ + testassert(m2); \ + method_exchangeImplementations(m1, m2); \ + \ + testprintf("Check exchanged version\n"); \ + state = 0; \ + [Super s1]; \ + testassert(state == v2); \ + state = 0; \ + [Super s2]; \ + testassert(state == v1); \ + \ + testprintf("NULL should do nothing\n"); \ + method_exchangeImplementations(m1, NULL); \ + method_exchangeImplementations(NULL, m2); \ + method_exchangeImplementations(NULL, NULL); \ + \ + testprintf("Make sure NULL did nothing\n"); \ + state = 0; \ + [Super s1]; \ + testassert(state == v2); \ + state = 0; \ + [Super s2]; \ + testassert(state == v1); \ + \ + testprintf("Put them back\n"); \ + method_exchangeImplementations(m1, m2); \ + \ + testprintf("Check restored version\n"); \ + state = 0; \ + [Super s1]; \ + testassert(state == v1); \ + state = 0; \ + [Super s2]; \ + testassert(state == v2); \ + } while (0) + +int main() +{ + // Check ordinary selectors + checkExchange(one, ONE, two, TWO); + + // Check vtable selectors + checkExchange(length, LENGTH, count, COUNT); + + // Check ordinary<->vtable and vtable<->ordinary + checkExchange(count, COUNT, one, ONE); + checkExchange(two, TWO, length, LENGTH); + + succeed(__FILE__); +} diff --git a/test/foreach.m b/test/foreach.m new file mode 100644 index 0000000..4386db3 --- /dev/null +++ b/test/foreach.m @@ -0,0 +1,227 @@ +// TEST_CFLAGS -framework Foundation + +#include "test.h" +#import + +/* foreach tester */ + +int Errors = 0; + +bool testHandwritten(const char *style, const char *test, const char *message, id collection, NSSet *reference) { + unsigned int counter = 0; + bool result = true; + testprintf("testing: %s %s %s\n", style, test, message); +/* + for (id elem in collection) + if ([reference member:elem]) ++counter; + */ + NSFastEnumerationState state; + id __unsafe_unretained buffer[4]; + state.state = 0; + NSUInteger limit = [collection countByEnumeratingWithState:&state objects:buffer count:4]; + if (limit != 0) { + unsigned long mutationsPtr = *state.mutationsPtr; + do { + unsigned long innerCounter = 0; + do { + if (mutationsPtr != *state.mutationsPtr) objc_enumerationMutation(collection); + id elem = state.itemsPtr[innerCounter++]; + + if ([reference member:elem]) ++counter; + + } while (innerCounter < limit); + } while ((limit = [collection countByEnumeratingWithState:&state objects:buffer count:4])); + } + + + + if (counter == [reference count]) { + testprintf("success: %s %s %s\n", style, test, message); + } + else { + result = false; + printf("** failed: %s %s %s (%d vs %d)\n", style, test, message, counter, (int)[reference count]); + ++Errors; + } + return result; +} + +bool testCompiler(const char *style, const char *test, const char *message, id collection, NSSet *reference) { + unsigned int counter = 0; + bool result = true; + testprintf("testing: %s %s %s\n", style, test, message); + for (id elem in collection) + if ([reference member:elem]) ++counter; + if (counter == [reference count]) { + testprintf("success: %s %s %s\n", style, test, message); + } + else { + result = false; + printf("** failed: %s %s %s (%d vs %d)\n", style, test, message, counter, (int)[reference count]); + ++Errors; + } + return result; +} + +void testContinue(NSArray *array) { + bool broken = false; + testprintf("testing: continue statements\n"); + for (id __unused elem in array) { + if ([array count]) + continue; + broken = true; + } + if (broken) { + printf("** continue statement did not work\n"); + ++Errors; + } +} + + +// array is filled with NSNumbers, in order, from 0 - N +bool testBreak(unsigned int where, NSArray *array) { + PUSH_POOL { + unsigned int counter = 0; + id enumerator = [array objectEnumerator]; + for (id __unused elem in enumerator) { + if (++counter == where) + break; + } + if (counter != where) { + ++Errors; + printf("*** break at %d didn't work (actual was %d)\n", where, counter); + return false; + } + for (id __unused elem in enumerator) + ++counter; + if (counter != [array count]) { + ++Errors; + printf("*** break at %d didn't finish (actual was %d)\n", where, counter); + return false; + } + } POP_POOL; + return true; +} + +bool testBreaks(NSArray *array) { + bool result = true; + testprintf("testing breaks\n"); + unsigned int counter = 0; + for (counter = 1; counter < [array count]; ++counter) { + result = testBreak(counter, array) && result; + } + return result; +} + +bool testCompleteness(const char *test, const char *message, id collection, NSSet *reference) { + bool result = true; + result = result && testHandwritten("handwritten", test, message, collection, reference); + result = result && testCompiler("compiler", test, message, collection, reference); + return result; +} + +bool testEnumerator(const char *test, const char *message, id collection, NSSet *reference) { + bool result = true; + result = result && testHandwritten("handwritten", test, message, [collection objectEnumerator], reference); + result = result && testCompiler("compiler", test, message, [collection objectEnumerator], reference); + return result; +} + +NSMutableSet *ReferenceSet = nil; +NSMutableArray *ReferenceArray = nil; + +void makeReferences(int n) { + if (!ReferenceSet) { + int i; + ReferenceSet = [[NSMutableSet alloc] init]; + ReferenceArray = [[NSMutableArray alloc] init]; + for (i = 0; i < n; ++i) { + NSNumber *number = [[NSNumber alloc] initWithInt:i]; + [ReferenceSet addObject:number]; + [ReferenceArray addObject:number]; + RELEASE_VAR(number); + } + } +} + +void testCollections(const char *test, NSArray *array, NSSet *set) { + PUSH_POOL { + id collection; + collection = [NSMutableArray arrayWithArray:array]; + testCompleteness(test, "mutable array", collection, set); + testEnumerator(test, "mutable array enumerator", collection, set); + collection = [NSArray arrayWithArray:array]; + testCompleteness(test, "immutable array", collection, set); + testEnumerator(test, "immutable array enumerator", collection, set); + collection = set; + testCompleteness(test, "immutable set", collection, set); + testEnumerator(test, "immutable set enumerator", collection, set); + collection = [NSMutableSet setWithArray:array]; + testCompleteness(test, "mutable set", collection, set); + testEnumerator(test, "mutable set enumerator", collection, set); + } POP_POOL; +} + +void testInnerDecl(const char *test, const char *message, id collection) { + unsigned int counter = 0; + for (id __unused x in collection) + ++counter; + if (counter != [collection count]) { + printf("** failed: %s %s\n", test, message); + ++Errors; + } +} + + +void testOuterDecl(const char *test, const char *message, id collection) { + unsigned int counter = 0; + id x; + for (x in collection) + ++counter; + if (counter != [collection count]) { + printf("** failed: %s %s\n", test, message); + ++Errors; + } +} +void testInnerExpression(const char *test, const char *message, id collection) { + unsigned int counter = 0; + for (id __unused x in [collection self]) + ++counter; + if (counter != [collection count]) { + printf("** failed: %s %s\n", test, message); + ++Errors; + } +} +void testOuterExpression(const char *test, const char *message, id collection) { + unsigned int counter = 0; + id x; + for (x in [collection self]) + ++counter; + if (counter != [collection count]) { + printf("** failed: %s %s\n", test, message); + ++Errors; + } +} + +void testExpressions(const char *message, id collection) { + testInnerDecl("inner", message, collection); + testOuterDecl("outer", message, collection); + testInnerExpression("outer expression", message, collection); + testOuterExpression("outer expression", message, collection); +} + + +int main() { + PUSH_POOL { + testCollections("nil", nil, nil); + testCollections("empty", [NSArray array], [NSSet set]); + makeReferences(100); + testCollections("100 item", ReferenceArray, ReferenceSet); + testExpressions("array", ReferenceArray); + testBreaks(ReferenceArray); + testContinue(ReferenceArray); + if (Errors == 0) succeed(__FILE__); + else fail("foreach %d errors detected\n", Errors); + } POP_POOL; + exit(Errors); +} diff --git a/test/fork.m b/test/fork.m new file mode 100644 index 0000000..f3677df --- /dev/null +++ b/test/fork.m @@ -0,0 +1,54 @@ +// TEST_CONFIG + +#include "test.h" + +void *flushthread(void *arg __unused) +{ + while (1) { + _objc_flush_caches(nil); + } +} + +int main() +{ + pthread_t th; + pthread_create(&th, nil, &flushthread, nil); + + alarm(120); + + [NSObject self]; + [NSObject self]; + + int max = is_guardmalloc() ? 10: 100; + + for (int i = 0; i < max; i++) { + pid_t child; + switch ((child = fork())) { + case -1: + abort(); + case 0: + // child + alarm(10); + [NSObject self]; + _exit(0); + default: { + // parent + int result = 0; + while (waitpid(child, &result, 0) < 0) { + if (errno != EINTR) { + fail("waitpid failed (errno %d %s)", + errno, strerror(errno)); + } + } + if (!WIFEXITED(result)) { + fail("child crashed (waitpid result %d)", result); + } + + [NSObject self]; + break; + } + } + } + + succeed(__FILE__ " parent"); +} diff --git a/test/forkInitialize.m b/test/forkInitialize.m new file mode 100644 index 0000000..b9e9a3a --- /dev/null +++ b/test/forkInitialize.m @@ -0,0 +1,159 @@ +/* +TEST_CRASHES +TEST_RUN_OUTPUT +objc\[\d+\]: \+\[BlockingSub initialize\] may have been in progress in another thread when fork\(\) was called\. +objc\[\d+\]: \+\[BlockingSub initialize\] may have been in progress in another thread when fork\(\) was called\. We cannot safely call it or ignore it in the fork\(\) child process\. Crashing instead\. Set a breakpoint on objc_initializeAfterForkError to debug\. +objc\[\d+\]: HALTED +OK: forkInitialize\.m +END +*/ + +#include "test.h" + +static void *retain_fn(void *self, SEL _cmd __unused) { return self; } +static void release_fn(void *self __unused, SEL _cmd __unused) { } + +OBJC_ROOT_CLASS +@interface BlockingRootClass @end +@implementation BlockingRootClass ++(id)self { return self; } ++(void)initialize { + class_addMethod(self, sel_registerName("retain"), (IMP)retain_fn, ""); + class_addMethod(self, sel_registerName("release"), (IMP)release_fn, ""); + + if (self == [BlockingRootClass self]) { + while (1) sleep(1); + } +} +@end + +@interface BlockingRootSub : BlockingRootClass @end +@implementation BlockingRootSub +@end + +OBJC_ROOT_CLASS +@interface BlockingSubRoot @end +@implementation BlockingSubRoot ++(id)self { return self; } ++(void)initialize { + class_addMethod(self, sel_registerName("retain"), (IMP)retain_fn, ""); + class_addMethod(self, sel_registerName("release"), (IMP)release_fn, ""); +} +@end + +@interface BlockingSub : BlockingSubRoot @end +@implementation BlockingSub ++(void)initialize { + class_addMethod(self, sel_registerName("retain"), (IMP)retain_fn, ""); + class_addMethod(self, sel_registerName("release"), (IMP)release_fn, ""); + + while (1) sleep(1); +} +@end + +OBJC_ROOT_CLASS +@interface AnotherRootClass @end + +@interface BoringSub : AnotherRootClass @end +@implementation BoringSub +// can't implement +initialize here +@end + +@implementation AnotherRootClass + +void doFork() +{ + testprintf("FORK\n"); + + pid_t child; + switch((child = fork())) { + case -1: + fail("fork failed"); + case 0: + // child + // This one succeeds even though we're nested inside it's + // superclass's +initialize, because ordinary +initialize nesting + // still works across fork(). + // This falls in the isInitializing() case in _class_initialize. + [BoringSub self]; + +#if !SINGLETHREADED + // This one succeeds even though another thread is in its + // superclass's +initialize, because that superclass is a root class + // so we assume that +initialize is empty and therefore this one + // is safe to call. + // This falls in the reallyInitialize case in _class_initialize. + [BlockingRootSub self]; + + // This one aborts without deadlocking because it was in progress + // when fork() was called. + // This falls in the isInitializing() case in _class_initialize. + [BlockingSub self]; + + fail("should have crashed"); +#endif + break; + default: { + // parent + int result = 0; + while (waitpid(child, &result, 0) < 0) { + if (errno != EINTR) { + fail("waitpid failed (errno %d %s)", + errno, strerror(errno)); + } + } + if (!WIFEXITED(result)) { + fail("child crashed (waitpid result %d)", result); + } + break; + } + } +} + ++(void)initialize { + class_addMethod(self, sel_registerName("retain"), (IMP)retain_fn, ""); + class_addMethod(self, sel_registerName("release"), (IMP)release_fn, ""); + + if (self == [AnotherRootClass self]) { + static bool called = false; + if (!called) { + doFork(); + called = true; + } else { + fail("+[AnotherRootClass initialize] called again"); + } + } +} + ++(id)self { + return self; +} +@end + + +void *blocker(void *arg __unused) +{ + [BlockingSub self]; + return nil; +} + +void *blocker2(void *arg __unused) +{ + [BlockingRootClass self]; + return nil; +} + +int main() +{ +#if !SINGLETHREADED + pthread_t th; + pthread_create(&th, nil, blocker, nil); + pthread_detach(th); + pthread_create(&th, nil, blocker2, nil); + pthread_detach(th); + sleep(1); +#endif + + [AnotherRootClass self]; + succeed(__FILE__); +} diff --git a/test/forkInitializeDisabled.m b/test/forkInitializeDisabled.m new file mode 100644 index 0000000..747d1b8 --- /dev/null +++ b/test/forkInitializeDisabled.m @@ -0,0 +1,21 @@ +/* +TEST_CONFIG OS=macosx MEM=mrc ARCH=x86_64 +(confused by ARC which loads Foundation which provokes more +initialize logs) +(also confused by i386 OS_object +load workaround) + +TEST_ENV OBJC_PRINT_INITIALIZE_METHODS=YES + +TEST_RUN_OUTPUT +objc\[\d+\]: INITIALIZE: disabling \+initialize fork safety enforcement because the app has a __DATA,__objc_fork_ok section +OK: forkInitializeDisabled\.m +END +*/ + +#include "test.h" + +asm(".section __DATA, __objc_fork_ok\n.long 0\n"); + +int main() +{ + succeed(__FILE__); +} diff --git a/test/forkInitializeSingleThreaded.m b/test/forkInitializeSingleThreaded.m new file mode 100644 index 0000000..575c6f3 --- /dev/null +++ b/test/forkInitializeSingleThreaded.m @@ -0,0 +1,8 @@ +/* +TEST_RUN_OUTPUT +OK: forkInitialize\.m +OK: forkInitialize\.m +END +*/ +#define SINGLETHREADED 1 +#include "forkInitialize.m" diff --git a/test/forward.m b/test/forward.m new file mode 100644 index 0000000..517f5e2 --- /dev/null +++ b/test/forward.m @@ -0,0 +1,543 @@ +// TEST_CONFIG MEM=mrc + +#include "test.h" + +#include +#include + +id ID_RESULT = (id)0x12345678; +long long LL_RESULT = __LONG_LONG_MAX__ - 2LL*__INT_MAX__; +double FP_RESULT = __DBL_MIN__ + __DBL_EPSILON__; +long double LFP_RESULT = __LDBL_MIN__ + __LDBL_EPSILON__; +// STRET_RESULT in test.h + + +static int state = 0; +static id receiver; + +OBJC_ROOT_CLASS +@interface Super { id isa; } @end + +@interface Super (Forwarded) ++(id)idret: + (long)i1 :(long)i2 :(long)i3 :(long)i4 :(long)i5 :(long)i6 :(long)i7 :(long)i8 :(long)i9 :(long)i10 :(long)i11 :(long)i12 :(long)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + ++(id)idre2: + (long)i1 :(long)i2 :(long)i3 :(long)i4 :(long)i5 :(long)i6 :(long)i7 :(long)i8 :(long)i9 :(long)i10 :(long)i11 :(long)i12 :(long)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + ++(id)idre3: + (long)i1 :(long)i2 :(long)i3 :(long)i4 :(long)i5 :(long)i6 :(long)i7 :(long)i8 :(long)i9 :(long)i10 :(long)i11 :(long)i12 :(long)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + ++(long long)llret: + (long)i1 :(long)i2 :(long)i3 :(long)i4 :(long)i5 :(long)i6 :(long)i7 :(long)i8 :(long)i9 :(long)i10 :(long)i11 :(long)i12 :(long)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + ++(long long)llre2: + (long)i1 :(long)i2 :(long)i3 :(long)i4 :(long)i5 :(long)i6 :(long)i7 :(long)i8 :(long)i9 :(long)i10 :(long)i11 :(long)i12 :(long)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + ++(long long)llre3: + (long)i1 :(long)i2 :(long)i3 :(long)i4 :(long)i5 :(long)i6 :(long)i7 :(long)i8 :(long)i9 :(long)i10 :(long)i11 :(long)i12 :(long)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + ++(struct stret)stret: + (long)i1 :(long)i2 :(long)i3 :(long)i4 :(long)i5 :(long)i6 :(long)i7 :(long)i8 :(long)i9 :(long)i10 :(long)i11 :(long)i12 :(long)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + ++(struct stret)stre2: + (long)i1 :(long)i2 :(long)i3 :(long)i4 :(long)i5 :(long)i6 :(long)i7 :(long)i8 :(long)i9 :(long)i10 :(long)i11 :(long)i12 :(long)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + ++(struct stret)stre3: + (long)i1 :(long)i2 :(long)i3 :(long)i4 :(long)i5 :(long)i6 :(long)i7 :(long)i8 :(long)i9 :(long)i10 :(long)i11 :(long)i12 :(long)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + ++(double)fpret: + (long)i1 :(long)i2 :(long)i3 :(long)i4 :(long)i5 :(long)i6 :(long)i7 :(long)i8 :(long)i9 :(long)i10 :(long)i11 :(long)i12 :(long)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + ++(double)fpre2: + (long)i1 :(long)i2 :(long)i3 :(long)i4 :(long)i5 :(long)i6 :(long)i7 :(long)i8 :(long)i9 :(long)i10 :(long)i11 :(long)i12 :(long)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + ++(double)fpre3: + (long)i1 :(long)i2 :(long)i3 :(long)i4 :(long)i5 :(long)i6 :(long)i7 :(long)i8 :(long)i9 :(long)i10 :(long)i11 :(long)i12 :(long)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + +@end + + +long long forward_handler(id self, SEL _cmd, long i1, long i2, long i3, long i4, long i5, long i6, long i7, long i8, long i9, long i10, long i11, long i12, long i13, double f1, double f2, double f3, double f4, double f5, double f6, double f7, double f8, double f9, double f10, double f11, double f12, double f13, double f14, double f15) +{ +#if __arm64__ +# if __LP64__ +# define p "x" // true arm64 +# else +# define p "w" // arm64_32 +# endif + void *struct_addr; + __asm__ volatile("mov %"p"0, "p"8" : "=r" (struct_addr) : : p"8"); +#endif + + testassert(self == receiver); + + testassert(i1 == 1); + testassert(i2 == 2); + testassert(i3 == 3); + testassert(i4 == 4); + testassert(i5 == 5); + testassert(i6 == 6); + testassert(i7 == 7); + testassert(i8 == 8); + testassert(i9 == 9); + testassert(i10 == 10); + testassert(i11 == 11); + testassert(i12 == 12); + testassert(i13 == 13); + + testassert(f1 == 1.0); + testassert(f2 == 2.0); + testassert(f3 == 3.0); + testassert(f4 == 4.0); + testassert(f5 == 5.0); + testassert(f6 == 6.0); + testassert(f7 == 7.0); + testassert(f8 == 8.0); + testassert(f9 == 9.0); + testassert(f10 == 10.0); + testassert(f11 == 11.0); + testassert(f12 == 12.0); + testassert(f13 == 13.0); + testassert(f14 == 14.0); + testassert(f15 == 15.0); + + if (_cmd == @selector(idret::::::::::::::::::::::::::::) || + _cmd == @selector(idre2::::::::::::::::::::::::::::) || + _cmd == @selector(idre3::::::::::::::::::::::::::::)) + { + union { + id idval; + long long llval; + } result; + testassert(state == 11); + state = 12; + result.idval = ID_RESULT; + return result.llval; + } + else if (_cmd == @selector(llret::::::::::::::::::::::::::::) || + _cmd == @selector(llre2::::::::::::::::::::::::::::) || + _cmd == @selector(llre3::::::::::::::::::::::::::::)) + { + testassert(state == 13); + state = 14; + return LL_RESULT; + } + else if (_cmd == @selector(fpret::::::::::::::::::::::::::::) || + _cmd == @selector(fpre2::::::::::::::::::::::::::::) || + _cmd == @selector(fpre3::::::::::::::::::::::::::::)) + { + testassert(state == 15); + state = 16; +#if defined(__i386__) + __asm__ volatile("fldl %0" : : "m" (FP_RESULT)); +#elif defined(__x86_64__) + __asm__ volatile("movsd %0, %%xmm0" : : "m" (FP_RESULT)); +#elif defined(__arm64__) + __asm__ volatile("ldr d0, %0" : : "m" (FP_RESULT)); +#elif defined(__arm__) && __ARM_ARCH_7K__ + __asm__ volatile("vld1.64 {d0}, %0" : : "m" (FP_RESULT)); +#elif defined(__arm__) + union { + double fpval; + long long llval; + } result; + result.fpval = FP_RESULT; + return result.llval; +#else +# error unknown architecture +#endif + return 0; + } + else if (_cmd == @selector(stret::::::::::::::::::::::::::::) || + _cmd == @selector(stre2::::::::::::::::::::::::::::) || + _cmd == @selector(stre3::::::::::::::::::::::::::::)) + { +#if __i386__ || __x86_64__ || __arm__ + fail("stret message sent to non-stret forward_handler"); +#elif __arm64_32__ || __arm64__ + testassert(state == 17); + state = 18; + memcpy(struct_addr, &STRET_RESULT, sizeof(STRET_RESULT)); + return 0; +#else +# error unknown architecture +#endif + } + else { + fail("unknown selector %s in forward_handler", sel_getName(_cmd)); + } +} + + +struct stret forward_stret_handler(id self, SEL _cmd, long i1, long i2, long i3, long i4, long i5, long i6, long i7, long i8, long i9, long i10, long i11, long i12, long i13, double f1, double f2, double f3, double f4, double f5, double f6, double f7, double f8, double f9, double f10, double f11, double f12, double f13, double f14, double f15) +{ + testassert(self == receiver); + + testassert(i1 == 1); + testassert(i2 == 2); + testassert(i3 == 3); + testassert(i4 == 4); + testassert(i5 == 5); + testassert(i6 == 6); + testassert(i7 == 7); + testassert(i8 == 8); + testassert(i9 == 9); + testassert(i10 == 10); + testassert(i11 == 11); + testassert(i12 == 12); + testassert(i13 == 13); + + testassert(f1 == 1.0); + testassert(f2 == 2.0); + testassert(f3 == 3.0); + testassert(f4 == 4.0); + testassert(f5 == 5.0); + testassert(f6 == 6.0); + testassert(f7 == 7.0); + testassert(f8 == 8.0); + testassert(f9 == 9.0); + testassert(f10 == 10.0); + testassert(f11 == 11.0); + testassert(f12 == 12.0); + testassert(f13 == 13.0); + testassert(f14 == 14.0); + testassert(f15 == 15.0); + + if (_cmd == @selector(idret::::::::::::::::::::::::::::) || + _cmd == @selector(idre2::::::::::::::::::::::::::::) || + _cmd == @selector(idre3::::::::::::::::::::::::::::) || + _cmd == @selector(llret::::::::::::::::::::::::::::) || + _cmd == @selector(llre2::::::::::::::::::::::::::::) || + _cmd == @selector(llre3::::::::::::::::::::::::::::) || + _cmd == @selector(fpret::::::::::::::::::::::::::::) || + _cmd == @selector(fpre2::::::::::::::::::::::::::::) || + _cmd == @selector(fpre3::::::::::::::::::::::::::::)) + { + fail("non-stret selector %s sent to forward_stret_handler", sel_getName(_cmd)); + } + else if (_cmd == @selector(stret::::::::::::::::::::::::::::) || + _cmd == @selector(stre2::::::::::::::::::::::::::::) || + _cmd == @selector(stre3::::::::::::::::::::::::::::)) + { + testassert(state == 17); + state = 18; + return STRET_RESULT; + } + else { + fail("unknown selector %s in forward_stret_handler", sel_getName(_cmd)); + } + +} + + +@implementation Super ++(void)initialize { } ++(id)class { return self; } +@end + +typedef id (*id_fn_t)(id self, SEL _cmd, long i1, long i2, long i3, long i4, long i5, long i6, long i7, long i8, long i9, long i10, long i11, long i12, long i13, double f1, double f2, double f3, double f4, double f5, double f6, double f7, double f8, double f9, double f10, double f11, double f12, double f13, double f14, double f15); + +typedef long long (*ll_fn_t)(id self, SEL _cmd, long i1, long i2, long i3, long i4, long i5, long i6, long i7, long i8, long i9, long i10, long i11, long i12, long i13, double f1, double f2, double f3, double f4, double f5, double f6, double f7, double f8, double f9, double f10, double f11, double f12, double f13, double f14, double f15); + +typedef double (*fp_fn_t)(id self, SEL _cmd, long i1, long i2, long i3, long i4, long i5, long i6, long i7, long i8, long i9, long i10, long i11, long i12, long i13, double f1, double f2, double f3, double f4, double f5, double f6, double f7, double f8, double f9, double f10, double f11, double f12, double f13, double f14, double f15); + +typedef struct stret (*st_fn_t)(id self, SEL _cmd, long i1, long i2, long i3, long i4, long i5, long i6, long i7, long i8, long i9, long i10, long i11, long i12, long i13, double f1, double f2, double f3, double f4, double f5, double f6, double f7, double f8, double f9, double f10, double f11, double f12, double f13, double f14, double f15); + +#if __x86_64__ +typedef struct stret * (*fake_st_fn_t)(struct stret *, id self, SEL _cmd, long i1, long i2, long i3, long i4, long i5, long i6, long i7, long i8, long i9, long i10, long i11, long i12, long i13, double f1, double f2, double f3, double f4, double f5, double f6, double f7, double f8, double f9, double f10, double f11, double f12, double f13, double f14, double f15); +#endif + +__BEGIN_DECLS +extern void *getSP(void); +__END_DECLS + +#if defined(__x86_64__) + asm(".text \n _getSP: movq %rsp, %rax \n retq \n"); +#elif defined(__i386__) + asm(".text \n _getSP: movl %esp, %eax \n ret \n"); +#elif defined(__arm__) + asm(".text \n .thumb \n .thumb_func _getSP \n " + "_getSP: mov r0, sp \n bx lr \n"); +#elif defined(__arm64__) + asm(".text \n _getSP: mov x0, sp \n ret \n"); +#else +# error unknown architecture +#endif + +int main() +{ + id idval; + long long llval; + struct stret stval; +#if __x86_64__ + struct stret *stptr; +#endif + double fpval; + void *sp1 = (void*)1; + void *sp2 = (void*)2; + + st_fn_t stret_fwd; +#if __arm64__ + stret_fwd = (st_fn_t)_objc_msgForward; +#else + stret_fwd = (st_fn_t)_objc_msgForward_stret; +#endif + + receiver = [Super class]; + + // Test user-defined forward handler + + objc_setForwardHandler((void*)&forward_handler, (void*)&forward_stret_handler); + + state = 11; + sp1 = getSP(); + idval = [Super idre3:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 12); + testassert(idval == ID_RESULT); + + state = 13; + sp1 = getSP(); + llval = [Super llre3:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 14); + testassert(llval == LL_RESULT); + + state = 15; + sp1 = getSP(); + fpval = [Super fpre3:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 16); + testassert(fpval == FP_RESULT); + + state = 17; + sp1 = getSP(); + stval = [Super stre3:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 18); + testassert(stret_equal(stval, STRET_RESULT)); + +#if __x86_64__ + // check stret return register + state = 17; + sp1 = getSP(); + stptr = ((fake_st_fn_t)objc_msgSend_stret)(&stval, [Super class], @selector(stre3::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 18); + testassert(stret_equal(stval, STRET_RESULT)); + testassert(stptr == &stval); +#endif + + + // Test user-defined forward handler, cached + + state = 11; + sp1 = getSP(); + idval = [Super idre3:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 12); + testassert(idval == ID_RESULT); + + state = 13; + sp1 = getSP(); + llval = [Super llre3:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 14); + testassert(llval == LL_RESULT); + + state = 15; + sp1 = getSP(); + fpval = [Super fpre3:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 16); + testassert(fpval == FP_RESULT); + + state = 17; + sp1 = getSP(); + stval = [Super stre3:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 18); + testassert(stret_equal(stval, STRET_RESULT)); + +#if __x86_64__ + // check stret return register + state = 17; + sp1 = getSP(); + stptr = ((fake_st_fn_t)objc_msgSend_stret)(&stval, [Super class], @selector(stre3::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 18); + testassert(stret_equal(stval, STRET_RESULT)); + testassert(stptr == &stval); +#endif + + + // Test user-defined forward handler, uncached but fixed-up + + _objc_flush_caches(nil); + + state = 11; + sp1 = getSP(); + idval = [Super idre3:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 12); + testassert(idval == ID_RESULT); + + state = 13; + sp1 = getSP(); + llval = [Super llre3:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 14); + testassert(llval == LL_RESULT); + + state = 15; + sp1 = getSP(); + fpval = [Super fpre3:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 16); + testassert(fpval == FP_RESULT); + + state = 17; + sp1 = getSP(); + stval = [Super stre3:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 18); + testassert(stret_equal(stval, STRET_RESULT)); + +#if __x86_64__ + // check stret return register + state = 17; + sp1 = getSP(); + stptr = ((fake_st_fn_t)objc_msgSend_stret)(&stval, [Super class], @selector(stre3::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 18); + testassert(stret_equal(stval, STRET_RESULT)); + testassert(stptr == &stval); +#endif + + + + // Test user-defined forward handler, manual forwarding + + state = 11; + sp1 = getSP(); + idval = ((id_fn_t)_objc_msgForward)(receiver, @selector(idre2::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 12); + testassert(idval == ID_RESULT); + + state = 13; + sp1 = getSP(); + llval = ((ll_fn_t)_objc_msgForward)(receiver, @selector(llre2::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 14); + testassert(llval == LL_RESULT); + + state = 15; + sp1 = getSP(); + fpval = ((fp_fn_t)_objc_msgForward)(receiver, @selector(fpre2::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 16); + testassert(fpval == FP_RESULT); + + state = 17; + sp1 = getSP(); + stval = stret_fwd(receiver, @selector(stre2::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 18); + testassert(stret_equal(stval, STRET_RESULT)); + + + // Test user-defined forward handler, manual forwarding, cached + + state = 11; + sp1 = getSP(); + idval = ((id_fn_t)_objc_msgForward)(receiver, @selector(idre2::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 12); + testassert(idval == ID_RESULT); + + state = 13; + sp1 = getSP(); + llval = ((ll_fn_t)_objc_msgForward)(receiver, @selector(llre2::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 14); + testassert(llval == LL_RESULT); + + state = 15; + sp1 = getSP(); + fpval = ((fp_fn_t)_objc_msgForward)(receiver, @selector(fpre2::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 16); + testassert(fpval == FP_RESULT); + + state = 17; + sp1 = getSP(); + stval = stret_fwd(receiver, @selector(stre2::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 18); + testassert(stret_equal(stval, STRET_RESULT)); + + + // Test user-defined forward handler, manual forwarding, uncached but fixed-up + + _objc_flush_caches(nil); + + state = 11; + sp1 = getSP(); + idval = ((id_fn_t)_objc_msgForward)(receiver, @selector(idre2::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 12); + testassert(idval == ID_RESULT); + + state = 13; + sp1 = getSP(); + llval = ((ll_fn_t)_objc_msgForward)(receiver, @selector(llre2::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 14); + testassert(llval == LL_RESULT); + + state = 15; + sp1 = getSP(); + fpval = ((fp_fn_t)_objc_msgForward)(receiver, @selector(fpre2::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 16); + testassert(fpval == FP_RESULT); + + state = 17; + sp1 = getSP(); + stval = stret_fwd(receiver, @selector(stre2::::::::::::::::::::::::::::), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + sp2 = getSP(); + testassert(sp1 == sp2); + testassert(state == 18); + testassert(stret_equal(stval, STRET_RESULT)); + + + succeed(__FILE__); +} diff --git a/test/forwardDefault.m b/test/forwardDefault.m new file mode 100644 index 0000000..0ba1d6c --- /dev/null +++ b/test/forwardDefault.m @@ -0,0 +1,24 @@ +/* +no arc, rdar://11368528 confused by Foundation +TEST_CONFIG MEM=mrc +TEST_CRASHES +TEST_RUN_OUTPUT +objc\[\d+\]: \+\[NSObject fakeorama\]: unrecognized selector sent to instance 0x[0-9a-fA-F]+ \(no message forward handler is installed\) +objc\[\d+\]: HALTED +END +*/ + +#include "test.h" + +#include + +@interface NSObject (Fake) +-(void)fakeorama; +@end + +int main() +{ + [NSObject fakeorama]; + fail("should have crashed"); +} + diff --git a/test/forwardDefaultStret.m b/test/forwardDefaultStret.m new file mode 100644 index 0000000..979f535 --- /dev/null +++ b/test/forwardDefaultStret.m @@ -0,0 +1,24 @@ +/* +no arc, rdar://11368528 confused by Foundation +TEST_CONFIG MEM=mrc +TEST_CRASHES +TEST_RUN_OUTPUT +objc\[\d+\]: \+\[NSObject fakeorama\]: unrecognized selector sent to instance 0x[0-9a-fA-F]+ \(no message forward handler is installed\) +objc\[\d+\]: HALTED +END +*/ + +#include "test.h" + +#include + +@interface NSObject (Fake) +-(struct stret)fakeorama; +@end + +int main() +{ + [NSObject fakeorama]; + fail("should have crashed"); +} + diff --git a/test/future.h b/test/future.h new file mode 100644 index 0000000..a48dc9a --- /dev/null +++ b/test/future.h @@ -0,0 +1,15 @@ +#include "test.h" + +@interface Sub1 : TestRoot ++(int)method; ++(Class)classref; +@end + +@interface Sub2 : TestRoot ++(int)method; ++(Class)classref; +@end + +@interface SubSub1 : Sub1 @end + +@interface SubSub2 : Sub2 @end diff --git a/test/future.m b/test/future.m new file mode 100644 index 0000000..9714f66 --- /dev/null +++ b/test/future.m @@ -0,0 +1,82 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/future0.m -o future0.dylib -dynamiclib + $C{COMPILE} $DIR/future2.m -x none future0.dylib -o future2.dylib -dynamiclib + $C{COMPILE} $DIR/future.m -x none future0.dylib -o future.exe +END +*/ + +#include "test.h" + +#if __has_feature(objc_arc) + +int main() +{ + testwarn("rdar://10041403 future class API is not ARC-compatible"); + succeed(__FILE__); +} + + +#else + +#include +#include +#include +#include +#include "future.h" + +@implementation Sub2 ++(int)method { + return 2; +} ++(Class)classref { + return [Sub2 class]; +} +@end + +@implementation SubSub2 ++(int)method { + return 1 + [super method]; +} +@end + +int main() +{ + Class oldTestRoot; + Class oldSub1; + Class newSub1; + + // objc_getFutureClass with existing class + oldTestRoot = objc_getFutureClass("TestRoot"); + testassert(oldTestRoot == [TestRoot class]); + testassert(! _class_isFutureClass(oldTestRoot)); + + // objc_getFutureClass with missing class + oldSub1 = objc_getFutureClass("Sub1"); + testassert(oldSub1); + testassert(malloc_size((__bridge void*)oldSub1) > 0); + testassert(objc_getClass("Sub1") == Nil); + testassert(_class_isFutureClass(oldSub1)); + testassert(0 == strcmp(class_getName(oldSub1), "Sub1")); + testassert(object_getClass(oldSub1) == Nil); // CF expects this + + // objc_getFutureClass a second time + testassert(oldSub1 == objc_getFutureClass("Sub1")); + + // Load class Sub1 + dlopen("future2.dylib", 0); + + // Verify use of future class + newSub1 = objc_getClass("Sub1"); + testassert(oldSub1 == newSub1); + testassert(newSub1 == [newSub1 classref]); + testassert(newSub1 == class_getSuperclass(objc_getClass("SubSub1"))); + testassert(! _class_isFutureClass(newSub1)); + + testassert(1 == [oldSub1 method]); + testassert(1 == [newSub1 method]); + + succeed(__FILE__); +} + +#endif diff --git a/test/future0.m b/test/future0.m new file mode 100644 index 0000000..4dd3146 --- /dev/null +++ b/test/future0.m @@ -0,0 +1,2 @@ +#include "future.h" +#include "testroot.i" diff --git a/test/future2.m b/test/future2.m new file mode 100644 index 0000000..c5ebb58 --- /dev/null +++ b/test/future2.m @@ -0,0 +1,17 @@ +#include "future.h" + + +@implementation Sub1 ++(Class)classref { + return [Sub1 class]; +} ++(int)method { + return 1; +} +@end + +@implementation SubSub1 ++(int)method { + return 1 + [super method]; +} +@end diff --git a/test/gc-main.m b/test/gc-main.m new file mode 100644 index 0000000..44f7476 --- /dev/null +++ b/test/gc-main.m @@ -0,0 +1,10 @@ +#include "test.h" + +OBJC_ROOT_CLASS +@interface Main @end +@implementation Main @end + +int main(int argc __attribute__((unused)), char **argv) +{ + succeed(basename(argv[0])); +} diff --git a/test/gc.c b/test/gc.c new file mode 100644 index 0000000..dab0f7b --- /dev/null +++ b/test/gc.c @@ -0,0 +1 @@ +int GC(void) { return 42; } diff --git a/test/gc.m b/test/gc.m new file mode 100644 index 0000000..65ba5f9 --- /dev/null +++ b/test/gc.m @@ -0,0 +1,8 @@ +#import + +OBJC_ROOT_CLASS +@interface GC @end +@implementation GC @end + +// silence "no debug symbols in executable" warning +void foo(void) { } diff --git a/test/gcenforcer-app-aso.m b/test/gcenforcer-app-aso.m new file mode 100644 index 0000000..8507a62 --- /dev/null +++ b/test/gcenforcer-app-aso.m @@ -0,0 +1,12 @@ +/* +fixme disabled in BATS because of gcfiles +TEST_CONFIG OS=macosx BATS=0 + +TEST_BUILD + cp $DIR/gcfiles/$C{ARCH}-aso gcenforcer-app-aso.exe +END + +TEST_RUN_OUTPUT +.*No Info\.plist file in application bundle or no NSPrincipalClass in the Info\.plist file, exiting +END +*/ diff --git a/test/gcenforcer-app-gc.m b/test/gcenforcer-app-gc.m new file mode 100644 index 0000000..a8ff65b --- /dev/null +++ b/test/gcenforcer-app-gc.m @@ -0,0 +1,14 @@ +/* +fixme disabled in BATS because of gcfiles +TEST_CONFIG OS=macosx BATS=0 + +TEST_BUILD + cp $DIR/gcfiles/$C{ARCH}-gc gcenforcer-app-gc.exe +END + +TEST_CRASHES +TEST_RUN_OUTPUT +objc\[\d+\]: Objective-C garbage collection is no longer supported\. +objc\[\d+\]: HALTED +END +*/ diff --git a/test/gcenforcer-app-gcaso.m b/test/gcenforcer-app-gcaso.m new file mode 100644 index 0000000..2094937 --- /dev/null +++ b/test/gcenforcer-app-gcaso.m @@ -0,0 +1,14 @@ +/* +fixme disabled in BATS because of gcfiles +TEST_CONFIG OS=macosx BATS=0 + +TEST_BUILD + cp $DIR/gcfiles/$C{ARCH}-gcaso gcenforcer-app-gcaso.exe +END + +TEST_CRASHES +TEST_RUN_OUTPUT +objc\[\d+\]: Objective-C garbage collection is no longer supported\. +objc\[\d+\]: HALTED +END +*/ diff --git a/test/gcenforcer-app-gcaso2.m b/test/gcenforcer-app-gcaso2.m new file mode 100644 index 0000000..8231993 --- /dev/null +++ b/test/gcenforcer-app-gcaso2.m @@ -0,0 +1,14 @@ +/* +fixme disabled in BATS because of gcfiles +TEST_CONFIG OS=macosx BATS=0 + +TEST_BUILD + cp $DIR/gcfiles/$C{ARCH}-gcaso2 gcenforcer-app-gcaso2.exe +END + +TEST_CRASHES +TEST_RUN_OUTPUT +objc\[\d+\]: Objective-C garbage collection is no longer supported\. +objc\[\d+\]: HALTED +END +*/ diff --git a/test/gcenforcer-app-gconly.m b/test/gcenforcer-app-gconly.m new file mode 100644 index 0000000..1b8e6a6 --- /dev/null +++ b/test/gcenforcer-app-gconly.m @@ -0,0 +1,14 @@ +/* +fixme disabled in BATS because of gcfiles +TEST_CONFIG OS=macosx BATS=0 + +TEST_BUILD + cp $DIR/gcfiles/$C{ARCH}-gconly gcenforcer-app-gconly.exe +END + +TEST_CRASHES +TEST_RUN_OUTPUT +objc\[\d+\]: Objective-C garbage collection is no longer supported\. +objc\[\d+\]: HALTED +END +*/ diff --git a/test/gcenforcer-app-nogc.m b/test/gcenforcer-app-nogc.m new file mode 100644 index 0000000..d99db0f --- /dev/null +++ b/test/gcenforcer-app-nogc.m @@ -0,0 +1,12 @@ +/* +fixme disabled in BATS because of gcfiles +TEST_CONFIG OS=macosx BATS=0 + +TEST_BUILD + cp $DIR/gcfiles/$C{ARCH}-nogc gcenforcer-app-nogc.exe +END + +TEST_RUN_OUTPUT +running +END +*/ diff --git a/test/gcenforcer-app-noobjc.m b/test/gcenforcer-app-noobjc.m new file mode 100644 index 0000000..ad746c3 --- /dev/null +++ b/test/gcenforcer-app-noobjc.m @@ -0,0 +1,12 @@ +/* +fixme disabled in BATS because of gcfiles +TEST_CONFIG OS=macosx BATS=0 + +TEST_BUILD + cp $DIR/gcfiles/$C{ARCH}-noobjc gcenforcer-app-noobjc.exe +END + +TEST_RUN_OUTPUT + +END +*/ diff --git a/test/gcenforcer-dylib-nogc.m b/test/gcenforcer-dylib-nogc.m new file mode 100644 index 0000000..b10fbe1 --- /dev/null +++ b/test/gcenforcer-dylib-nogc.m @@ -0,0 +1,11 @@ +// gc-off app loading gc-off dylib: should work + +/* +fixme disabled in BATS because of gcfiles +TEST_CONFIG OS=macosx BATS=0 + +TEST_BUILD + cp $DIR/gcfiles/libnogc.dylib . + $C{COMPILE} $DIR/gc-main.m -x none libnogc.dylib -o gcenforcer-dylib-nogc.exe +END +*/ diff --git a/test/gcenforcer-dylib-noobjc.m b/test/gcenforcer-dylib-noobjc.m new file mode 100644 index 0000000..a06fa54 --- /dev/null +++ b/test/gcenforcer-dylib-noobjc.m @@ -0,0 +1,9 @@ +/* +fixme disabled in BATS because of gcfiles +TEST_CONFIG OS=macosx BATS=0 + +TEST_BUILD + cp $DIR/gcfiles/libnoobjc.dylib . + $C{COMPILE} $DIR/gc-main.m -x none libnoobjc.dylib -o gcenforcer-dylib-noobjc.exe +END +*/ diff --git a/test/gcenforcer-dylib-requiresgc.m b/test/gcenforcer-dylib-requiresgc.m new file mode 100644 index 0000000..69a4d25 --- /dev/null +++ b/test/gcenforcer-dylib-requiresgc.m @@ -0,0 +1,22 @@ +// gc-off app loading gc-required dylib: should crash +// linker sees librequiresgc.fake.dylib, runtime uses librequiresgc.dylib + +/* +fixme disabled in BATS because of gcfiles +TEST_CONFIG OS=macosx BATS=0 +TEST_CRASHES + +TEST_RUN_OUTPUT +dyld: Library not loaded: librequiresgc\.dylib + Referenced from: .*gcenforcer-dylib-requiresgc.exe + Reason: no suitable image found\. Did find: + (.*librequiresgc\.dylib: cannot load '.*librequiresgc\.dylib' because Objective-C garbage collection is not supported(\n)?)+ + librequiresgc.dylib: cannot load 'librequiresgc\.dylib' because Objective-C garbage collection is not supported( + .*librequiresgc\.dylib: cannot load '.*librequiresgc\.dylib' because Objective-C garbage collection is not supported(\n)?)* +END + +TEST_BUILD + cp $DIR/gcfiles/librequiresgc.dylib . + $C{COMPILE} $DIR/gc-main.m -x none $DIR/gcfiles/librequiresgc.fake.dylib -o gcenforcer-dylib-requiresgc.exe +END +*/ diff --git a/test/gcenforcer-dylib-supportsgc.m b/test/gcenforcer-dylib-supportsgc.m new file mode 100644 index 0000000..d8ce9e3 --- /dev/null +++ b/test/gcenforcer-dylib-supportsgc.m @@ -0,0 +1,9 @@ +/* +fixme disabled in BATS because of gcfiles +TEST_CONFIG OS=macosx BATS=0 + +TEST_BUILD + cp $DIR/gcfiles/libsupportsgc.dylib . + $C{COMPILE} $DIR/gc-main.m -x none libsupportsgc.dylib -o gcenforcer-dylib-supportsgc.exe +END +*/ diff --git a/test/gcenforcer-preflight.m b/test/gcenforcer-preflight.m new file mode 100644 index 0000000..828cc33 --- /dev/null +++ b/test/gcenforcer-preflight.m @@ -0,0 +1,88 @@ +#pragma clang diagnostic ignored "-Wcomment" +/* +fixme disabled in BATS because of gcfiles +TEST_CONFIG OS=macosx BATS=0 + +TEST_BUILD + cp $DIR/gcfiles/* . + $C{COMPILE} $DIR/gcenforcer-preflight.m -o gcenforcer-preflight.exe +END +*/ + +#include "test.h" +#include + +void check(int expected, const char *name) +{ + int fd = open(name, O_RDONLY); + testassert(fd >= 0); + + int result = objc_appRequiresGC(fd); + + close(fd); + testprintf("want %2d got %2d for %s\n", expected, result, name); + if (result != expected) { + fail("want %2d got %2d for %s\n", expected, result, name); + } + testassert(result == expected); +} + +int main() +{ + int i; + for (i = 0; i < 1000; i++) { + // dlopen_preflight + + testassert(dlopen_preflight("libsupportsgc.dylib")); + testassert(dlopen_preflight("libnoobjc.dylib")); + testassert(! dlopen_preflight("librequiresgc.dylib")); + testassert(dlopen_preflight("libnogc.dylib")); + + // objc_appRequiresGC + + // noobjc: no ObjC content + // nogc: ordinary not GC + // aso: trivial AppleScriptObjC wrapper that can run without GC + // gc: -fobjc-gc + // gconly: -fobjc-gc-only + // gcaso: non-trivial AppleScriptObjC with too many classrefs + // gcaso2: non-trivial AppleScriptObjC with too many class impls + + check(0, "x86_64-noobjc"); + check(0, "x86_64-nogc"); + check(0, "x86_64-aso"); + check(1, "x86_64-gc"); + check(1, "x86_64-gconly"); + check(1, "x86_64-gcaso"); + check(1, "x86_64-gcaso2"); + + check(0, "i386-noobjc"); + check(0, "i386-nogc"); + check(0, "i386-aso"); + check(1, "i386-gc"); + check(1, "i386-gconly"); + check(1, "i386-gcaso"); + check(1, "i386-gcaso2"); + + // fat files + check(0, "i386-aso--x86_64-aso"); + check(0, "i386-nogc--x86_64-nogc"); + check(1, "i386-gc--x86_64-gc"); + check(1, "i386-gc--x86_64-nogc"); + check(1, "i386-nogc--x86_64-gc"); + + // broken files + check(-1, "x86_64-broken"); + check(-1, "i386-broken"); + check(-1, "i386-broken--x86_64-gc"); + check(-1, "i386-broken--x86_64-nogc"); + check(-1, "i386-gc--x86_64-broken"); + check(-1, "i386-nogc--x86_64-broken"); + + // evil files + // evil1: claims to have 4 billion load commands of size 0 + check(-1, "evil1"); + } + + succeed(__FILE__); +} diff --git a/test/gcfiles/evil1 b/test/gcfiles/evil1 new file mode 100644 index 0000000000000000000000000000000000000000..88bd337c1adf4cf57147abe815ff7740f598744d GIT binary patch literal 441 zcmZXQOGuOf5XV17T)Rl1hl0HfB=cB&uz1j!F8KfjK~Z5*I+;i)Dq0eR1%YmfDEQbt zb@1dt6!awc_z+S|4_ZDJQ8Z8qIxN*eMUA!3tIlD5fBwu|ew$bEgwJu3GK;Sdk{!o+ zxIUL~J#Kov2j}ta@}_sP$u%NYys55Ha#L4_UVkS1LKK0REeIgxTLE!h4DA%VNdsG| z9asE@p_alc@U8QLGm08S3A);4gr>2vOz5q$Qqhut{QH4M!I$>SK4G-)yi%$PW~Gn_ zF()&k3hyNSkLpy`&+Wqj2JYJDqvkVh+1(N0ZR7(DKpV+&qn(F&Y=4nTsh-qA_V?p@ zi6#@7@Q{B`lt-SzPLSpLxrMYkz`DS~;+rV>QhBpML6RVa;=_82latp(xQkG2f`lWS%_}U=k@dMlhCc-m_7S?$D`cy l7;nX(76WE4j&8^}P0t*`S_&9>@?Y?WAVon{CHMjS2}H#c4}C#MeL_6-0i;H15by&K!kn`ojlHqVGcZ@VJ2UsroVjy$ zHkw`EJAeH4*CRrRhoEE7C!t9gJ`veO0`)hbQm*FTF1=Sezk)_26H(=G$#I|IB4wp? zsnX}?m}neI&O06|dr9dTTOMnH#K%uQCqy2u$)iz^uuO{W$RefNZB|>BbiGa$#yCa= z7-`rmz+jw_q{ah`hhYE)2Fr$FwGGE_53XY!7+KiYfkDn5NQl98_D0Lc9E{@@FwVe!D;`HUCkw9eTd0($@)eR7NfwppeuVcT z+5_w&r40@PD%+kjbbN{(F}6Qu@)xzGwDeIJsjLvhNX0O?FOpCS<3EFolq-dIioPkh zyov(jL^dcnhVe(^z`x~6$JlUTgbW++_st%}%YN98MD<0=rs2B!rV--AF_MUZ@h7t* zF+a#4s? zI)TBGJhBj0Kj*%mfo%dR zjy&q?iKtE6*FwC6wQ^ki|BL_s0(Sa>y?qR|gwV=e*E5>hl38_hr>mWI^rmsG<$UOB z#a6AQ=QbjPcF}NLvt_$lK5+SQHwG}X|Ad)uw=JVwbIi84QoT|fjvnje&okdQPa^Rh z=tk@jXW%VZMy`+pe`Yn{^itm#AwI%m@ye+6V%l@2H!P&07WjZi9@htO?;)1C#0VZw zw>oxR_dwln>;r#@-@jHN?!h{^)>idOEtij0D#VNE!9B5seTHDf%VC@QSBYXkF`yVw z3@8Q^1OFQaF6``Gy#8zEJ{Np>_tT$oJlMUlhwIMLOmZhVbAK9+-5dLT7F5Oc+nLKB z>~m0WmTxw_S)S<|d^zaN3S4jUyeIa5>GgVzXIT3QwdHaFCmhQVP2IG^s@2l#gXcJ- z=9yQGVsQBL-)MM#=J}V>U-Ep)^DECm)C=MJCF^`YO5;2<0ga1=cgd?zC5i#XfMP%~ zpcqgLC^Ql=&`_7U)PHTa?VKludWDY}j?!C<$axH>9y;)HZjxKLO3nhJ=xtB*1`1*i9=Gh`c0Xe5aydKo)7Nwx>f08H%b*3~t$ouvO x2(dVr2Yxt$oF8h<(p^{DNZG}j!pw^ys~cR`Az~c8?!2jKCl_9uKRJ(J{|1kHi{$_S literal 0 HcmV?d00001 diff --git a/test/gcfiles/i386-aso--x86_64-aso b/test/gcfiles/i386-aso--x86_64-aso new file mode 100644 index 0000000000000000000000000000000000000000..93b331649cdfb041bf5fa300f34f51b8b1d3aa67 GIT binary patch literal 29060 zcmeHPU2I&%6`mU$Sb+r&S*QzvZj1wI>UyC{qKF^KYh!SNNgS_}Mo=(|<_wL#z z?F%XA$a81T{CxM!cW14+b7#&c-+b_)Qfd>zH3(NJ)r07s30%58+9)gH4 zAPfit!hkR!3JkBv?q3U!Xl1ZxP}y^M?2Q=`YGLY3IYwL{GNZnlB7v~KwG4WM;CnNUpEP~b#W~NAr!q#j6_N)VkBZ192Z>(6xu(Ai`IvR4~&?s;Bq%2v{R`dW*ge?+JXO4UDy1Rb!0+1 zwKsKBtM;-U#v@UUXg%xK>$y|D#ff8d!3Nsjm0B00lAkO2tIsgnk8WV_hTSm?WQFV~ z&e94y4BGm#&DybT9Srgq{FcY}AADr=!Ts)cIi%DC3xUCyJW_Sihark0Q=UYWgwpE@ z5f4CT`7dzUhM3Qu<OdwE*?*i+)XU4L)oU=Ib{Oggy`|-D&o~zf= zrQ&otzfj6UzZHQtBnSHc@OOXj!kJwcKKS(C1HbM^UJru0?phOeIh}d$DYX-QWxM$Q z7xVuD`78^@_RUCnDxF=ZH~iW3cyYRxt1YA-s^w<=rz^Ez*3%=EnM!W(q{~Pj^=tKF zrCd*Ef+AygA%dCO2s2Zymi+8YtypawntpPmGkUC&xzFyC&<$SII>N;bu zmRtOAUzIwK+-P4b(@)L}W?WB|x)U`xCgw5DV2t{))j57egaKhd7!U@80b$^K!@!Z| zg`=lGPhMn)k1ze|FPI)Iy?O!H=6HWsv#bAN9}1RUy%fqpQk_1RJpRNbHd@)iH=C6m zoYNQhaS=BkR&^o2p)Q)8VZ{+(;X2f}h6zb8Lb?TvN-cmPiii zgIR7(D9)HonU7-A0t>ot9@5-dsb}k_vVJ*_Jc@@M)CKL&`!jP5AN%QZg!+NmLi^h7 zO65`IeuUxC+-B~*yurij1OB+r7CRKM=Txbm1!)$m(I9P>B(g!8TN+yXzD)zAhNAnx z+btM0rp}ad^|~%&%P#t4cV2YpU9?|q#ME;6+P&%Yp1t?%-m{y=iU)k$tjeG=Li}Mb|qs0sTf?w0}|3UR!h77vjT>S6ovn{`E|+~S!!wc@=L8myKytyLg_u>EE-AtscFABsH)U6y#nIbiB zV{*o^d#^rQ>PML&Y6Bk!@&AjwPxQ?H|L)^}b`rOMK+b~kmO^S>-wDy9ZVe7ung#=G-(!0{|13$_?&;v zyUI(l5aK;Esl53fuc!N(#4^Fxkb-@0V=?Es?mhGQQ2rWCC0iH}2801&Ko}4PgaKhd z7!U@80bxKG5C*Y}j6n#$JVo}qE5FsN_nU#Zjo59WYIr>K%$Fw(;W+TU9?hKfz$;HR8^RBX8f9oUHSvcz3QEJ?|bi_ zx$n$4dNXr-^TD70JfoD_2OWZrK=1EY>T_jfGO*u+YCSi3y8Ll@;T#+z!^RkF8TSAyf{^?oTYW-tVgsG>5sQwRq8Z`tiwuW%_O#2wP{?f!;MC@>FF?P zSJByzQ3pmI{dHh4Pb0bg3C00*00Y6g?szRHi2c3mh=5_CKLQ3hdn7#!`dY7qQQ-P( zy}|xGt^ngDj9mdnTGDu0xBZK*U(>F?+Qg)OjH|#ngZ``id87cj#$TXXPfspQrUoq7 z=6dw5g}DbfOKTq-CRDbf;IZ*JPDE@vXYyx!t-Z>VFmjes#K`qw@V+qDm-(N;LF;o< z@1KNYa8^TfLLL_%&$V{*At#Aw#qp0j%m_pT!c44#l&7~bGgQhB%yX3@J12G&vl#{F)$Td@8cgFGg`<(ady zAC#xhn%`xqcP3zzFo`^Ju90I(ZDL3%97fTMqAi#~v=i{h(9WTqK+837oI$)7ZlPU9 zdk{JYO$+Bz%{5Dp8T)z<$M~i_1mhJbf6I0|3~bM>+O-X@2LEL!bGT+5KlIb`iAz7< z^lu-S{oxztISf??pN;h`lVuoPe-5rdj=Tr8sFKlsb+XT>m9-iW8_edwz0(;fM$W!hkR!3b*(i&=IT0`?AB^J^qMetSBpQ8wV zK2X~#$c>bA3q|eGD!u#*CCw63-3Zqf9lwS?NJgWEdPq0w+F5s2+L41g`U)YAHY%>K z#`#iVpZfj?8Xo8#&fv`h{=FF6sAIr82-Y^B)*&uMemAjM#-OGXJAO4Wld@UQ$_f1c m1dx*PEmTVn=Az?VigEUFk%jhAx~m@*UhG|$S@QrZIMKgpur(k6 literal 0 HcmV?d00001 diff --git a/test/gcfiles/i386-broken--x86_64-gc b/test/gcfiles/i386-broken--x86_64-gc new file mode 100644 index 0000000000000000000000000000000000000000..b22be31828f6f0905f149b02e06526076f2099e8 GIT binary patch literal 29056 zcmeHPTWnNC7@jRfR;jQcF*Pb4u;mugg`kEPk}XhJv;u9x5EC7GIb~P3+h%tgis7LN z!MIjTP0+*#6CO12ff$~AF!7Q|BKUw0UV;e^Jb)<#6JvZJN~qsAXJ&i$Y+Jx5!2eI@ z{PWM{J2T&$)0~-|f4;x@#aBwHI)r%$bCgOztxw_75JTFiN!tx`8KSBIK|~l32801& zKo}4PgaKhd7!U@80bxKG5C-ls2G0Hc`%OFXH;Vv^(f&W?Ds@zOG0gwfMg*;Qw{CBH zxvg_osB>HXX-yX+vy}eH^6uR3BW*Qo$+%+e!3gDPW|L zek$6I5Fq>b4T9ENTlchvGCaiHc-XPP;!mYNTIa!GK;=-WFm3%f;ZQf+X7cB3t#jS8 zVZ=S9h!Kxq@VRi~%l4;n(R$bB=eNLcvJMf{Q(mB1hwXRuz<;i&3x0nQiIC~)o3h?i zdsz<8BTVCM-3NS&6UC@U8;DWw&5AMDH=OgQ#<02{abWO<xc4$;?*xn#3Xw;ab+TNkQDiBF%Mz$1(2Xt= zdM)fG=-tq3q1gxKvl5?$i_rU_=OgS!2n)+HxG@Xox%e52<7}rFAo2tP-{s_Rv5?GV zdy;*Ja(%ELM_?OHdn*=vxqtoPug~Q#J+SlRciGMaf|~!332U6qG{`D6oAvnrB`mzh zegx*Ry-Hm|lh?+QU5AP#e<0bO?J0B@4kfo0x(ED&gN4_N$!&wf`M&N_b}--4?^2U5 z`Gq1UrYHJ4r3`0a15C((+VL%uV2801&KK8=n4ifg%{ zsl2DDzT9?WVkPp%Uc7Pay%9vpOPb1U*C!rEbp6?K+h64~M@~1s^6mI!Gr#aBn;US= zaKbRz%&E(GMnSGzot&J^@RRn+MZ|V?ZN}GQ&R4y;?qX3DhVyw$B=OA(;kP-zdnsYf z?^1rZ@;j1&^IHnzoEJ5qX2R5FYORc7F8MZsh%g`w2m``^Fdz&F1HynXAPfit!hkR! z4BUSV@OS8s5cs$?B?EIkqigaKhd7!U@80bxKG5C((+VL%uV2L5jh zELtA#d|GvG?pVC+>e8XQq1aGe$Kn(>(gvI0d$9i;o8UVNYFriKi&J_Co7zkLv>F~s zX>Kvq1H}Vfe!dTBkeJ3E>N-7;(f)|vJ6!UykG`7_%Lcl$dDYBCg*ocPQYc<%E{tL2 z0e_F0IQB7M4T5_cu-CzCMSd=ba~p%&8pO@gM0+UC{j4lNe*#F#*&cRF*S1BQwzQbm zn@tuvkFC49vhkw2U#`zJtOWOxt1uu82m``^Fdz&F1HynXAPfit!hkSv*E8@#;Qt@P z+O~0W;kXO`f75pQsHYL8Jp;g$u#2k7fexz*VLIg8g6 z6j!v(VurQhm^jZ_@xY&C~Ei{w|MUUw8K(`ZSZD4m*4}7_n-x#9hO?W+0Q6A zVe$A3QD{dof}G&0wV%<(W!l-*<1u0J=qpX3Z5D_Bk;J1@N4JXn#G67q=B? zhuhuPIr+(d){VL%uV2801&Kp2>ffxYSS<@AwjiS*c9o`;tn zd-hKhPnW-&;1@>v*rpC+wp~A^Ciu-}o&M8DPA81l|4F1xHh+ezQtii{d>^W{YqI$$ zwDtY}t>XKC0kNpdKkixO=MU4^{Dunq`*U7OnZe&bwj{__bKdAwv5`b9(J(JQPVk5v zw)3kut!6gw<>r}@;$jK|!hkR!3V&<(KA@AFcKSQHw%d>-+;zxeFIqV4*UaH+&BtAa}C1y y2|#n%qT(xH*D1pO0?1Yyf^wCg5L*MAFh+LZ3Y?0~-%?rlPW%UEVC;^Z2mS?nVR-8R literal 0 HcmV?d00001 diff --git a/test/gcfiles/i386-broken--x86_64-nogc b/test/gcfiles/i386-broken--x86_64-nogc new file mode 100644 index 0000000000000000000000000000000000000000..a401acce90c1eea84e67f306c0b47ef70137b3ff GIT binary patch literal 29056 zcmeHPU2IfE6rP0wD^%E4Vro>pV2fZ#7yJtkl5By(q7`ThhM36E(pz?QyKQ#2p%@-& z2*$NyYJ$F*@Zf_F#PDWf;!h%ppb0*ipz(nR&_XaV#s{K=`h9a}ws-Hg1$+WLC!Krd z%+GgczPY!#GdpL#yYcy#N~t*r3lQcim4I58!lgcjv{93`6XtS6RXu`;Fdz&F1HynX zAPfit!hkR!3$rN*yf1EL0!wLK@<&RH1rn|AJ*L!FWO+-Js&j>? z>(r<-wJshS=pD@IVrjS+iBXIUFt~`30S4RYBt9>|n2!Wtpt8>Rxgoz`?CO4$fZ-v% z1Pm^mRK#M?)_Q-jRLJHJ*vx1+9q z+Kvz)`}j42)|=aQwS_V~#NBwFS%Z-c);8 z4$mV|jc7gK7mGaye2Wvss6!iwQRmHyG1xbp^QXqJx*u_1@P_2V@dlF;YDac}t+0}Kel&}*RC2j;U9pM?w1`=A#g>_i9)%QCnz3+K7`8H?j=rxzpg2m;^bC% zeTQ;=updTX8%}#G7k{yD-Qlm!OdJ2b&4`j!Qp&gPboW?Z|!%f$(Q{? z5h=ywrcgf6i2n?N!;i(z0zWp@FZ3LeTgaKhd7!U@80bxMxE?+uwwP}J(>=}FeM|?hwjsJvexxTr) ztGTY+etqHzh?JK$m)oyR+>hwGv*q@`%4d$8ZhG~b@yQl`;ZL^Ie|@Zxcthpp*cgbp*fw6DQ=_1{e++Zy zQBJBG5YEGK7ykdI?etMkBTRb+fGJ@o={7!-BF+eI5cOeac{9o9=7X?UoBI+tr>Cae z^o#)bJrHk$DXnHbD+p`Dg>rA=X*#S9=*BlgA zw9R6Mwc(gM>Y~)zE=BmI43{Ws_#3x)?*6pHQiW~sWP~J(}Gv@-RA?US8Bxii~K7q|K^_DBO5Bk5JZxBa)_wE9wB6J4x z5cq#5dcFaYDrX#g|K{H3n>ReQ^|OWlw9Nhax0U?AVu-o}&p23*wvjt>cq?ZdpqS}@ zxZbUtbHI8l5y-;{FBsR=*5=f1jP$V$oyKgxc1lg~o6S1?r;nUY7_a}6NSkc=6j!A>jy?JTR9p9C%TZ|S z`~Pdj_x~5fqAvfqXO*8nOk?vKD(vsic`0QEfB)FhAY09Sqf5m`60t=6g7`SWBXZcz zuimoSWxIK%vqcL7!hkR!3cPNJN^SRFm_wc1OEbwId#UXeI(dH{abMt| z^-}55QfizrCJrsni&MqQSz1T#dPFOc{&@RUrOsl=KCD#6Ok$f+o5s~T+-Ovqbsa|S z3Of5SYQV^$zXlBEX(YQp!8m{pU?5o6+_zSb*Y6nOqx zZ?Hd)YruF3W7mL@mNcH$ZU3_8SGDJ_HZiFm<2o?Tq5pb+9w|Vs@h7O()00b+sR0|d zxgNc1VeSFW(%J`y36-rVcx-%@6A_!vnfw`F>w5V~7+G5>Vr2U;cwd<7%lyybp!J2R zcTdAH@+J(#DLZKy!~9Jg_^*dLa92ZgLLL_%&$V~+@WU$T6ihF%*xFq8WvgH-l&=;E$o5Lpy<%Yv4G8crV;RyNvcA zbPk#p&ZU}bmL4Ns5w9$_N(Q{Yx;#%)9YMxgAhHTb2>E+G~&MnHTafw3;5RVy0Et7RE4Pm z*639RV~`UN_Qo_TAFmW9O@va%F@x*B0#0!Pb)x4d7Z{F+Fdz&F1HynXAPfit!hrgq z^V`+m^E+hX($;6cVEeSS{VR^0+*oI6EYm4H*m)geTkk*k^6PaNozbyQ>F=HAV4k|w zDgD*CdG$vAqo23CR2ZfWn`fi=50h23M_RV(#! z7^J7%>(|u7~800z&i-mHlWraE=7Jjv028TrV~4UH8GR2Si@@FrH(2uhV{SN2&eV#*6nRC zw{`9cHICZ^GYHGQgNN38+V<@UWnvju4nE(DR~g4#T9^Eh5<`JR%X?2LwH-;`LZ#|m zCi?Yi)Tvq*4-NDT=5(<%+=Iv{Mg|z%#K-`H^>h-S8(=(u2wwNS@fgyM10yV9JgtZG2ebJ;oz3?TB2yIO6fn|= zKNYP<2#{_322Sg(t$SKS2_C|(KWtxM^QY1tt@Ge8pmL~Gn6`YJa3~wDGx>A6*17K4 zFyfw4#E3^QI4)fOvi@m2wBEJ(`7KbKtV00hlo$9c!}_~&;6GQ?1;4+DNXT^MO92k5dxp2HerG)B{9bhf& zFsNxuBQVHgqc<}Kc?`bg_8mK4XxqBOeU~XD@J+x-ArpCIUMI_y8by*xUjlClltz~b ze=YPT_`Bh+h0iuHorO3SF2dgre?Hu9xUeuUo$Ir3nhT$?I8Jx|0tB9bY}XFZZuM{Pnr~r3ZF?{4VR6fK&4yGH#9283$Q~YO@^wzl4SF z*bm1v)>o-ZsPfuavg=T>3 z$iem>2B&y|T5a{9gQ18B1HynXAPfit!hkR!45+>3%SWy?PLPRxV{iS8>C@QwuXvUl zn#y~c>dS36CRQSC?8O_$-Wx%nyrikzc75Vu1lOM}xBXQg42PhEv)r7ZKXowHdR=oUeLw-Nm9R4CnJ$NMgK zyp;1+&Lio#zNIkCbx{LKCQNC@*2*Z>l5fL_2m``^Fdz&F1HynXAPfit!hkR!3XHaE*+{HiVf9uEKcztZEy&_2j|am2)?7B##JG{IHiYhsJ+xrt>KZB z<`GjpP(0A(=lc)`iD{gnuG0ex6@M!CAjl>OpwXOfF7LPW%6sHFo?5^$I1GIIf-0JRtoW*Mj zvMcIlGsEgI%pG+>`r2+q_@xZDC~Ei{w|MUT)WcGRZSZElm*4}7_n-x#9+q0X+3zSg zVevSIDAc1EK}zt{+RtcXGVS#0{+O_Mw3Vh%HycBNTJz+%JC6ZTbN>OxSJk+4<1aMs zLiaE6gSwyDFYZP!n!3C`K9(SQ2L>4dTRKZ&@>=Fjj{s{PoL@55{D znruD_-`f8FRS z6Fee^?fU9X>ob|}a_h`Uc2R`^VL%uV2801&Ko}4PgaKhd7!U@80b$@CU|`YmL}x4Z zFcziE1?zfCduvyhZkjp@-%(gMK8-=Z=oxHQ7>SLunT14^Z9wC+wgJp|2lfHXZVUzB za}UDU3Bc#JMa5RY?o)*A1(2*Z1or;5t#}GGa;yR!%Lr~Sji3kJ2fG{8o z2m``^Fdz&F1HynXAPfit!oWSoz`4JFziAi#W)olu>i@?)rH(2uhV{SN2&Z*N>-M&n z+q!mz8pmyd8HDBD!9(laZF_fzGO>&+2cPf7tBhkVtxNt$iJ?HE<-Mnr+Kwb|ky3Ro z6a6|h>Qt?ZhX#5FbGld>?nPu2BLfU>Vq}29dOC^E3osr)1TauoXZ+ldUod)gJ4(Rt z5MKfYH%=;IF{o?3zgQ|{^9O8Vv>wNS@fgyM10yV9JgtZG2ebJ;oz3?TB2yIO6fn|= zKNYP<2#{_322Sg(t-D)82_C|(KWtxM^QY1tt@Ge8pmL~Gn6`YJa3~wDGx>A6*14Y9 zFyfw4#E3^QI4)fOvi@m2wBEJ(`7KbKtV00hlo$9c!}_~&;6GQ?1;4+DNXT^MO92k5dxp2HerG)B{9bhf& zFsNxuBQVHgqc<}Kc?`bg_8mK4XxqBOeU~XD@J+x-ArpCIUMI_y8by*xZ-CbTrO_qA zUkkkneh2)u@Yx2Yvk=F^Mfm&RFM#WS3k&noxjqZ0x$qf_<8Vj-E! z_9pud<@%sM4#zs2_Es$Xa^L#HU!TigdSK_r@3Ni=IJMv*|nm7-^C_h z@(V@86q8#*=|Cg?GjI++7CQ_4*i^gFw%}DQn_JMvY;SED)CmaHWd?g++uzdaN+`7g zIoSTg;1n-VtF0b%FccACKo}4PgaKhd7!U@80kx-m`N*}#2{N&F?5&?MeHt7870+^g zQ+an&UAgVX#7d-%y?Eo;dm{*x8=A^(*C!rEaQ)eG+h64~M@~1s^6mI!GiUgd&GmR@ zxL}xU=F(+6qaatVPEJl{IHkRE5uu&kn=yOL`D%Zzr&v^l;d~woNz7RxoSSprO9}g& zmvY|9c_bazw-koCE~-b#gelF~S{cP!@@+T~VL%uV2801&Ko}4PgaKhd7!U@80bxKG zxc?a7@6aE?@pnJ|Hgqzy2N3+$b^obe#5B%O&(Q-J?T`5Thf6-r(f1Hy*+5S=ubR23Fjt*e3Xcz}i(=S$ zz~7@LjB^awgW%Z)oOLiqk)I2~JjS5524S-`(H;u(JS#KMo&b_^x`)%!wRO>^E-j|^ z=8%QXS?%X?*K44>>}NUXYz*qPrf@_G0mELP{a1g`1n zQ*L=j0PG%!x5?yIv)&aD;L+CK8i^bHYFqzTEgo%jDNYYG*j?MZ2Waa|xz*hPIg8g6 zWLMP9W`@;am^jvsfVQs+u+T9FTn>E??DSfJuJ0&v)@s0 z!s2lZQK&~Tf|TH?wV%<(WZLP~{V`$jXe&*jZZ?JjwdTojcOC;F-{KYhTyTd%6mL}ok9xR%_+}9^B^J+Ywh=Lkw=RN5J&XsWD!i%xOOg8wyhy)7 zr;M7;p5=Z6FStX2R$3|KcarwZy#Q(m{yIdGGmgDap|elDWlHUX|6lJnh@-swb^%in z8iRRo{J#^u-vCjSI}Sei@xtoQf7-C}!!`f>a&mBO7yqvqqVB>w4mP5057kfG{8o2m``^Fdz)f#=xF*`EvTmwM2Ss9`D0T zk3IV*vZu@6O>l;hKDMdTsBPCzsR_>6tkHk^$mxW!`ag-d$>z`SRI2^hlkdZ8?VfBt z3g6oP|5ma6zkpEGvh#;(bk3o|_WoR#Qf9FCk2M6zYVI3dDmIdcCFfG{8o2m``^Fdz&F1HynXAPfit!hkSv4=}KJd7`Todl-vT z=7M#-rM;&L*+oEDCVD~A)_5w&&8-je5pAcIEoiIjr;0c_H&fij*`A+NyW?<}&j0gS&1-Exw literal 0 HcmV?d00001 diff --git a/test/gcfiles/i386-gc--x86_64-nogc b/test/gcfiles/i386-gc--x86_64-nogc new file mode 100644 index 0000000000000000000000000000000000000000..3a237e19cb2e19990cce1a370e23ab68f735fcb2 GIT binary patch literal 29056 zcmeHPTWnNC7@ma!D^%DbF*Pb4uthMW3*N$mBwL`cXocP|#6*Uc9@y3Gw%Of=VtA+_ z7}tua3HoNjgAYCs!<&hTmqZdl6MQm3;{y+%g>71E= zF5j8?W==CRJO6xlhbKo}4PgaKhd z7!U@80bxKG5C((+VL%wT%NRKK_wP6C!ryEHEJgkQn5WcH<;Af6R~z88?r7cC_DWmV z&QRmHO)!J7+`D*a-P5+aCzOe0TsioBFJ5IFb7@`jM@kF@5-smNrqnhhc}tY4bD8MZ zsZpnDT|6|>N)$J$&!$W)t z7~D9ih{d3;^}b@Mkj)>kiP3r-2gXB4I}VJngz>Z<&L7O?`*k+Ie-N3X7^i@dM*OL0 zJwkwN00M{XTykl zN)aO-!Qi-X{mc5N@z8qbrWZCtak35plv7^dvkdF+%7On}Q5XFEMMOfTD{u08Q}ty& zj7Opx(R#oy7JCo)7AJ~PhdL0W&YKluuzxt`PxWDSJL16L3(1A!4Jsv6kL&^JTIb19xbJ@OR z|Djwz^oQYChtuAw#b4}QfB36&`HS;+eEc5knSfJ^78>`Y(-{X@jcT(T|G$KV@7N2+ zG}c$Ci>UJ2ShD+2vE&aV+p~Rz-ol~e)b0{5X#D50P;m2ZUfghV{7upuQs%29P+L-ODErU7%p}Ndq-)s9?T3rdHRv`!5 ze;AzN1!|4egARrwA`A!v!hkR!3cu1`FHw6T}2AA5fUf%3AZa@)0u`w?7!w%qnt`OJ~ijjw()KH1C}{$z7Ko*6C} zCY!l*8P6!lv@SxUzg`&QNSEAGkkUM2 zst1Y(y8V1V;vg}NGt_hRKt}r`{=VUok8||Bgjg}qo6W0cZYs=GCziwGgX)qPb{_Ee zpb6s~1NI!aUE)474YJq@3>Iv~+DtaYpchs0};wn?*hkAB4r~JeR;VJ$=ee z?+Ae11MxPR+-lal0s=hR`dcG$gI{gyzsKUyMwjCBK!e@2y?cPR&Xk+o9gwqlO+j`= z-E3x99frB1E=XV7tq8xA;TA;=f8!RBS0 zj`J;E(a!~UC`9pANAReJ>xXX^F;imk%w`)Aqj>8gc+|sqK&rx<`o9#p&%lfHD|E`J z>FinVH}HZx6lkTDLVhP{&)f^3hTyM5Bst^Q`vf}s)LWs{Uikm@euFs5yKfgT6`?Vh z2gmLeTgaKhd7!U@8f!P??l`daOAGw-HkImzKcbKQ|{ zLT5aVE5LXLzAM0pN)%7ZmUXFSRbf^O z&m(*mY7MZLlomJ)sI)z2-|;DS#Mo%eq%uMfBNfBox=_!T@ect|%Cl3i zOrxS?0tUwU4+15}Fn*R4DI2C&l;wKSb%T2MctUWzikDRgQGLNPRKvr1C&mN-h%u4r zjp0hiT=t{rp2rdZIM<~Z2L3N1aWqWVEv}d$PMnKMh*9Z@5j@N=j4nQ_z~Bw5F$^?C z<7ie|b@QL~*vZFgh_Tueqt@U|!X@1m#|ALCCN_Fta7_g7rPH%>ujWtAs&`Bl4n_-% zYv{zaz`jb332_-N-#>z41eIasM0o=BF_eoa`G3WALI0PiGp3PAA+}K-hMt2)g=>$> z_t`;`#CZOH#N~c8Fdn{_jF#&d^;*fOtko;1KMZ9IKU{eGlixm_nB3}Le{cAU#_NnT z4Hbv)^_A}uzs@$uQ7=v zlbjLjx>z31smO$Q0zLTrKA#BQ2TzA}u01W90nLDBKr^5j&oWw zmp575+{hmhVsm>I*Y?0zdtofu&fngB628qdx37NChS44xYv=#iy$|N(^>+UE_IK;w z4!`-$_TKSSMu@%R1GrX`zA13s=H5u$`gw0}ugY!Tt)F1c6|nCy%bv)udQK6Wn7P6f zwp?{n1iwF=mSy3WBsO}%ewKShO0?u&j(a`sJE;79OQFqu=>TBU(1XOrZ^?I{S~LTi z0nLDBKr^5j&Gy|Fe&46Y=GoTsx|1rSt(A2BpZ-1U6^!(%iXS6?06z~zHOFj(o zK0^6fXtNXh^Sdd(&84AV!N+fpl;C?*VC$=9Kr^5j&Gy|Fe&46Y=GoTsJ3}^;4 z1OFZaM~h@ z+DLBqmRTEkFe{nPC>!od!L%xVj;!dD;*oK`*-v7S7V=4Dkl-){XQL|3RkPgkOyt=Y z3H@TDShK`SJOt<$+oQPbWRWm28k`FZ-V>Y~JOuY(0w)Lfd(O9E;tb(=*f?A86CBP2 zwtp zxELscaVkuqxD$kK1#uyCSV3hn#osx#ym9 z&-uL%?t3}E{`JQ{PYNLpfycm;;M-3K@tKH6BIv&jmU21$ZsxJkJ^6S_0l((W1# zQm$n#u7&ip(ROIL?%^(TmXyA|;jAH?htb$w8lvf%k{#ZmDp zx!1=H_?X7{4fqIC*q@Y5=SsyX%8FB}V^V)Ex8UP6#&7k<5pu{JZ-b>gm0nAS6mj(R z`RJYteGPDylnyWqpltZ=Q2Qt+!ZsW;@zb)D)!Zl_(YO%w5$*HA>q4Jj#(x?IDVOHn zn}?w3b#!1S;z3Ir#;@&wf7O$&UGgvp8fLHJj~~R#dAJ_wRgsi6+w*ekcGpk8k0@f` zddx%TOv64O^KLyOzvHyeMy%|n=i4>wTqW=3+)Zo2 z&DHEHb@vm`TBtXjV$QGBon%RmS{H2B!-!|ihxG8Ko=0d8{Kp|h#0dZCMx$zH3vQ+1 zFXb=K4_hQ{^-WUiyct?261jssu#UzL7zQaN=aRs(k{=e^E^NLU72AB>wMXZlwtD9v zCSnkd4HyR)gkr1bFBg(&9hnd>V+POXr?^HClz6o(2Nx^^Lk0nZfI+|@U=T0}7zBnO zaK5#B;p$$ZP1VKi&mUqxvAy#*j;&atwU!ufW%k<(&~3lJzkPk5+V7QiGSTv>eb~mX zk=ie>e}hg7564?3Mr znMU`2LvJxVhuve<7KLig^Tc8nTRgkq3%BVwc%#JTHQ1wb|4RzDYJZCTEB8`lzUPwI z=lf9%`@M;t9X_?#_~Kpi3$P)BfI+|@U=T0}7z7Lg1_6VBLBJqj5HJWB1Rg&G_&@Yb z@Yi7eUxzOo;)l?8fZ+eG$1iV_kwL&9U=T0}7z7Lg1_6VBLBJqj5HJWB1pZG5JTo0# zeMw~JR-Qfi;6!7r5owIA980l~HYkG6qy8L4@GGv^5pMKYN;Xi`?w2UVo=ZuVWXhVi zp0%AK#y}B?dfqWvD@%LBE;M}`b@Vy9I9bb895KTW3Wvnj2{b%VJQ~4Q2mFq!J`VW) zz?ufsA*)j4w}L)PJXH-b235)MrC^+84Wbn4Q_0EQy5W?TF literal 0 HcmV?d00001 diff --git a/test/gcfiles/i386-gconly b/test/gcfiles/i386-gconly new file mode 100644 index 0000000000000000000000000000000000000000..910274770f9e14fcd5b663d0236e3ec37f77fd7f GIT binary patch literal 12608 zcmeHO&u<$=6rN38u&ANIM92tKwk6@0A}=6>I67(E9}wF7DpW{lYR8*+!CqUtYb6mq zM5>T#0#cB2LPFxii9`PcBsjDePH-rv9*UG!RN}w^p$hYTyPjram)=3|sc+}KnR(y5 z`F0)6+OI$Ub?5Jgl~M&RV?XeBZjZ$GQlNetPCmCBe&Y%^-pxLSuB&1$Qm!>Ch5 z=O9KM7&-LUfx$eDWDh1751|7X2-bDC(RPE_-@lIt7&iJNV34zW(#N2$^>P>mp1;~3 z9L!?}7*Anr2N-Ed<7wUT*F3+bJ%6QzNrMh^9RI$gg=<`Za*X*ZbrUI1TF&A|7`aVn)~g1UV8b~P zRGRKuEBGjMPP96HtrB@Hzqn$0op;?JL{I1(PmKeO_^(3^zGYnlzO}b6>@8WUI9HiyQ0jTi;QlXyQ!Jof>ifwJh9e>j2m``^Fdz&F1HynXpw4xF zz4TjQmrR`By8JVaPg~p9aqZ^DyG!GlZu#!+3mDsa=kDiUti$Mzjd#m`?mi0h)ob1I zAKj~$ekgqK{dRAHC;Z+-4%a$g7+W(Va#2TLA;i&U#q-q!3l$Ei zuaBVNh3;Vs-#p;old+9D27Cv>+6L4*#HGk@BsR+!)J$SCOwFcj*0XW~|33kwWPBUd g(!;svIG19aeOzRreU$F%dxaPK_ht4x#0nPr2ZBa1l>h($ literal 0 HcmV?d00001 diff --git a/test/gcfiles/i386-nogc b/test/gcfiles/i386-nogc new file mode 100644 index 0000000000000000000000000000000000000000..4349810f0d0eaa39d8ac541048bc09bcd48d49f3 GIT binary patch literal 12608 zcmeHO--{bn6uz@fZLra9w~IuSI&F7bl{KRv>SK1h=?`7o{bebF+$NjZO>{B|GZVTC zLKll*T!j_-D)P@#R%Cm($4gTl6`^q~(@it+nqa=Wuh?H^Fjp=a(n_uli} zbHAA^caqbe?*94D9;MVS=n!-SdV9B0A1m9kV7~#?dUo<;>D|)&SvW?9jWO6V?lWAp zUM!toOpPNWFTA!VI z^EezMFT+5bvXhoE%-_U;e=pR5yBwku@}T&5uDzWv=izxIYAjke+%PP!xILVHj0|!h zM#g?9Mzh-X-0gd~b01k?a6ximc!NtxB=D+9zQ=3*~3oX@`O1)hkYQ&8xzH2Fe^hdg(}W{?hB;ezX6; zUqAnN={EBmhN``f$NHl2*#~(ZX)}(0U(&)gE6cNgU~tAZ2Q%6RB!r)Wz*}NbAu2)p>sSn4m9Gw3N`qabshND_P(&UWU0bb z0eh@h24j#D5O&5iEAKBACQXD=M=^u@{}7yF0rf)9Pi`)+6L4*#HGluB{s_#)O2DqOwFWh*0XW~|33kwXnY&h g(u29^IF~}4eOzRreU$F%`-K-f_ht4xzzP=n7j?!l%K!iX literal 0 HcmV?d00001 diff --git a/test/gcfiles/i386-nogc--x86_64-broken b/test/gcfiles/i386-nogc--x86_64-broken new file mode 100644 index 0000000000000000000000000000000000000000..6570d398af036d501a9c0b9d6060fb2afadeab28 GIT binary patch literal 29056 zcmeHPTWDNG7@loYyV__su}DO%r)^SgiP=cKy$GAO(XGasn<9cvljfwmy4el88=8pF zV5qppLIf3j5c(qcP^3>jh^U}q>w|spf|76b0 zKbP;!d^0D^%+5cb{Pp&`N~tw)>*3Zam4J6^3XjGZ;-*bpFVrmvszx{wVL%uV2801& zKo}4PgaKhd7!U@80bxKGxWpLv^!K0svI~E+3DAuCpI@icQRT(3{#SRwY2Dj(Z}+3! z{SSs3$8CaTgymkqL+iorM+QTgSjLru&-db0#xa-HWq-2FP$1Fr-i=D#izIKOQVlK> z{RTDdRIN)>W5b14NGKR50djb7W1GB7;E zmw~~Jld4z@>RKNxm5bT@A)6Sj$4kJt7HKa5BP?M&ttaw_v-uI7&5st4DT;9t7-__x zjMgIr$Tq$Yr}f^h!LCq(hp_7p+ZWjUskTS!JU9%f94{A_EFUKv%7*Jq{+zCLZfGTp zxTh2`;t>pv3)jD_e;N<1AKZQ49w<&a5kNWR1wPBL{;nMO&y{r1A1xsgvQ&AK*ITSF z^I<#^)ri((eyKEc$hSCAj0V(!7!BTv7=@9EoWIzIwe5%lgD)f(jyI^3P(89^tc4v0 zb!}+@26=4pmd7BE!MEJAZ~y(>d-u8TGKB=b2^cA4B9F}LWUErsNHXc0;BA7^;u7Jv zLvMxO3%?yc+rV@d;#fEb{{Z|AaJ_J0VO~1dXW=v#K4o#7?)<9|xE_w*a&n?nOy;t~ z$&snt2=wdVSchkB*;eR3a_4*RG+q7knU9W~XFU^eYQq)A?Q}ZhAUC7hEXV&ZVc|Ot zz%h;WRq7j5xjmK~m@1Y1v1CtnxHwdtO5Rf(8uJeqicgl3_Y@}bBSYnEA>T3TVv`U1 z#S&sl$vvTTpb`HzIENpLod$kvv0Z3e@T!j89cW{AxV{YP1ccf$h2bX-c67NCN^M6D zw*P5xiWjIIRu4KDiij{E3tz~i$>-rjgaKhd7!U@80bxKG5C((+VL%uV2801& z;J?QJe}{ezj=%fyx1kfV2k_tPWNDEwAPfit!hkR!3tK!|-w}j)j6v-U!YrnGLSdd~Wd_<4KvGWka9X;)F51+k!_?j!ve0=P z-PP5D7q#tjZLVV_c$Ykd0bxKG5C((+VL%uV2801&Ko}4Pgn^5mfir>qe++BqX)dap z;J$+4F6{qJ-D#tqhFkIu0F%Qm(rtJ-k2sTfLDYtw`OPAqhY!MHb)HM$nw~yo)jI-U z_dvY6O>VW~T>$|eZT+p0xWTWr^?%&r(MFfz^gx5%wY__Qw$7B*?heRVyw)JQqHZ=b ztPaE6X&0og?N)?e&TxyOj=wpJ=iW~}EH&5$Z{>RlUa@$WTM+7DsmELSj)LPBk7I~J zJ&F;e1W&E~j5a3IPOt5cd5cF|X$p0-F%+nEPma5xXX^F^gjH%w`)Aqj;SWJnCUQAl2Y4{$GmRXW&Kp6*^_w zboL7O8+gGT3bfKnA-|KfXYK`1{j;+>-uiskO|S0!x{*6__)qRQKsM9< zaJheS&jHJAha(Tiy`W!rSe^YQA`A!v!hkR!3}HUb@3>CU$Jv;34?&?Q`oF95}RW)i-{WBfR-g~1DNpw>;stH7z)7W9)z(I xfX{7PFA>aH=|gOJ(K@u^(85u?sRD_#1(3d3yi= literal 0 HcmV?d00001 diff --git a/test/gcfiles/i386-nogc--x86_64-gc b/test/gcfiles/i386-nogc--x86_64-gc new file mode 100644 index 0000000000000000000000000000000000000000..3f3b82c3832ef9857d316ee902c278b6577eb445 GIT binary patch literal 29056 zcmeHPTWnNC7@mb9t5(@!V`@}9V9Ui|x5gVUBwL`eC<<-C5EC6*ddjYDcbna9Yz+yG z4aT*S)C5g@FyY0-2V!{g!5B3WBlsW>UV;e^Jcua-jWIqLHP-K&GqXK=wiodU_5ag3 zGyhz^GxN=yW@dK&`TWoKKU7LBfm;r@RH+2K`%-u`#t=7d;&ww_ji73T6A=c40bxKG z5C((+VL%uV2801&Ko}4Pgn@q<17H01!=HBHZ#Dr|qW(WEQ|gHFVp#vHO>kQ8?%dw> zR9DZgP~*5wFpsd@t9WR=r)%GyP$rgf<>2$Zc$IO?rFGdKEi)8Iw7hqhQrnT_tx&4L zWuo7p#+|BlX>6#!kkh5|NIxQ@7#U!26C(o**3(IRS%7gJB7lLyI^*Yt{i4xp+ffFF zhxjruxN%Yyi$PuM1Eq2?n?Gn1qxCojjN6cQ3>aYv<7qvTe?FTZ(AoT80hyv0CxMYh z{K;rNLV#@J$8cJ2>)g{BO7IYN{bBn8n?Kd|Xq^X#0hPn$;+*B-{4eFuGu6UAsi9f;B3Er?MV7|HpweOTL$I57A^a^ZM`N(t2?JH%Sp zVNlnW7GRLa7H@tG@)&%}J9j+sSl6~4?z>DOfo}pv3Yo|w^Ez3h)Hsq%dNaIcC@n4# z{zmAn@OQ)C2%l|WIty_uT!6nH{*7?E;ljeabgs|BX)b));yB&;HzRNd9KYq{NU502 zW&4u@W4Qt7x5KdxFW!51q37_!AAQht%a3P2J^T~vnSfI_-eBB4PG=lsJ*v%e{QnXb zzGFWe(^y}nzC)Ea#*)2brLsSi+?nk!_7%sHj}-fc{O1eB=Ss;(3M2V}zH+vZZy$8A z$tV3{2{EPQ)=)aoi2neb!;i(z06#X{F0?IpRr{89v@zRXUj}soLT#Bs|FZ|$J6#E- z)*=Vne+Zo71!{xUgARrwA`A!v!hkR!366z8~k$aR|P*og$2Z?E%pH4~8QS3wJTlkKG;}(x& zh(bMz5u^l9t^JHPCeu!@?T=}TM_XwMb+a)PsC7?{yK@*2b@v}&eASFQH~vE7F0`M7 z@{8PuVDTIb_p`$FV*iBOz1ZCdi3lF`Fdn}L?r}M>0HMRjQbJ_+l<5k!PFuO4nfX_V$VijL0nXkrvU>?S<%6Q-}P#<^e literal 0 HcmV?d00001 diff --git a/test/gcfiles/i386-nogc--x86_64-nogc b/test/gcfiles/i386-nogc--x86_64-nogc new file mode 100644 index 0000000000000000000000000000000000000000..48dce6ab2d6a3f3ccae5a3ad72116e0df59fa9c3 GIT binary patch literal 29056 zcmeHPU2IfE7@dV8D^%G6F*Pb)phYm)1%G*vWD67)MW8i(FbC9GciU@#0Z+mlL;Chcn~cF4KY3tCDe1~&TQ}AZ43AW_MzT;I!V}x~c7@wvKI~ z#&Mfq3Sqf-@X)%eZD&^~6U(@A@cCZ6$~flIy5tX)7z!j>-g{W7O-S+v2i?nl~tzz9niPwT<_{%pQiXY+gektvFC0vKt;pNQ5Y z1jsgi38(dj)~?o2f`_o{58D^m{He4@>pVCNs2nI2CM_Q)9Lk34O#Ym%b*_6ljJT&1 zG2#&njtkentbZC0t+%avZaowys}MjrP(b_|Fw}!QWd%BxJJkCa*V9U*^Mj zB&rdu`}|_Dd!KJ{q8N3k12O8nX)*eH2Xp>JA6B;`4h+7KTsYpKQbP5}_OTXr7}T_- z5g6pL(VH5BJOyUkGm@ltz~be22jEzTHy&Hs-*M=fFFtEn@W+|24_#(G6L4zo9OE8!I^!VAQEisv|Cg}v9lPO} z#`-Gt3#z;_mh3!GEct!OE!mzzci}*CW1+jx-``(&wV2%4KbY_BE@k`kEqh&T@iI|3vCNt)v~SyZOr!6mO-6>P+g|K=aoGzt*(Sp%aDWZ zKL}3o0=2^GK?g$-5e9?-VL%uV2801&Kp0Rv%I6PXY8)dIJ4fF70n?|E(I4?F*Ef~B zn(E4JSH~Vf+QJqWHoU2glUeCqJY#+Sbu9dG6gf4sRK&kPp~ z z1NdDYB=3NzKQMez8bIxmKu2mIYOpwXOdyi$@z>iqiuPcGvdq0opoKu6K7p&f+x%*%fuOnPGJp z<_^0ceQmcQ{8ENn6gB*fT0HlD>S3wEHh9zDOYnikyW4_L4@)iH^mh~-vv?ds6zWlo zASHNe?Ps(xnRa@0e~ei?+DcQXn~kABt$A|XoyCBtx&Hv;t7_c2@fR9*q5UM3zs`LK z7SF+OKPy}>_D{Io*Si}b5y7J##^d+EJuWA9oS2IOcuzpIy`II3+XB?X^>H}Pw|GTA z7u=x`#akZ1qaLmwzFEXfh{ZFTZA6UXt%~4L590x;3UA{7Qsh1ZFVe5jDZ{3-r@7z2 z3+_;$l~xM*ouoZ;FMt|=zY3A$jAQR3=nLquujQ>{*QMce72WwC_az_qtH>^Bi%Ko}4PgaKhd7!U@gV_-+Rd_H~nQX)Mvi}&HBN1pl<+0*53 z#yG=BA6?sS)V3=p)EMV%*62Td_+-LZ{U1l%c=N}2;`i#K54{hswR61r2z+b%|7*qe z|7V1vCjWS6m7PCSqjL@ww)f|{lrn|Ae{5lptY*H}p<+XcSfYM*e3al3IcV2cZ(83a zyLG11#TN#I0bxKG5C((+VL%uV2801&Ko}4Pgn`?KfrcfCj#lhpG^ET0>w5i`*3M4d zG;tKZy|8L@5`%!@Q`oF95*uY}3yCV*fW}E}1DNq9>;stH7z)7WeuuFWfX{7e|y@GoBXb>;v7 literal 0 HcmV?d00001 diff --git a/test/gcfiles/i386-noobjc b/test/gcfiles/i386-noobjc new file mode 100644 index 0000000000000000000000000000000000000000..8f01860aba841a005c4accf46f4d7cb404973b38 GIT binary patch literal 4228 zcmeHKK`TU26u!@pLRd*rh7i)sD9K8Z7aAc(Nx03MdGa*#)XaMsyID}|tX(<3SJvGEQfADJOyrz3stzmrHUR8PlhU&*Ug(GM_xkIeu;#i=i2AQI z22~YG>AQ!Ho0pEedw|WKGt_X$Joan2L-kd)YW;fm;=)qeoLx|Fh$G|U4aaafKXaA! z2~ka{;I9>TjN+XAJAo8p@}I%V`etASh#GT=Xus(BUp~KSH_!qk{cGy|Fe&46Y=GoTsxuMDi8 zJ&)aP-WB288sL|olB!njoAV%)*e-nhJ*&Mlf^x0SL~ec*>ca$=IxT` zEoL284DiC3p!5#@7`7P@X83gkez?yWFjF8;<51nd?~}oOl%2Bd`IfYx($NO?4%XCP ADB(pKjom4d zM%9cQ`=TgK!Zy%DZatN3dni2=HixtgrIbQ?Fx|Gh6be1GkV8^ROP~}=VGm2czxgA} zV>@gPg(iF-e)Im!dvD%+W_-}VoB7TCpZ`)Rbqetdh!aXpBl*?`*i0oxbvIFbZC~mT z87M#j3Q&Lo6rcbFC_n)UP=Epy_>2ks{@%y;!yo?Y4Ca0oaW|>d8+ZfN>n;+YgMSd(shT$c_9wb`n>ZcskjKP@}5M~*EIVEh1ssC!sGJVrXJl#C&1Z2n_oxO?q})AA>Qwpt$^ z<0bT!#*&^qM%!ujTh@W&;W4`57?PemhTX1jkA^rhk0JU_Dy1YnX$-j*ONd&R*2>Qn z3v1CmIYea^5$Ol$EZ0KXM&S`;E+Nh!j@P&BM#tGUQ_Vf(uve> z;ZXjV&!8;F_!H7eR6dOSRYVC7g;Cnd!?W?@qxeAKQM8h|-}%SWKYqKEx$ukgubsYg zJOAA|)cNzA!ch>PjLJwQ$nh2trLP=csSgno%ILd21E(y}X?(*>rP3Q&Lo6rcbFC_n)U zP=Epypa2CZKmiI+fC3bt00k&O0SZun0u-PC1t>rP3Q&Lo6rcbFC_n)UP=Epypa2CZ zKmiI+fC3bt00lln0-JAs^y2#~ScfjFa?ghoA5UeSudJ(=b-xmS60VPzysXckLeF4j zKMDxZ`uQl8{<8k}#E=d4MC~7RJ+rivb?UMrP3Q&Lo6rcbFC_n)UP=Epypa2CZKmiI+fC3bt00k&O0SZun0u-PC1t>rP z3Q&Lo6rcbFC_n)UP=Epypa2DwI`^3BCcATp>F(qPL{8}~$59tj@?r4c`}4X}Z(ABK z9k@85ZoQ(C>YZUiGX6Kkn<3(Dq*4AZ@>>%L_17E4YqIV?g^j$E;Co2t!u$`Az8>~P zR?Q)vMwEOR8_Dk@okc1^>YK7b_%qV8Xdv$s_^0F%W&D35#h<+9K+P(n+p~A-o@IFL zuHjj(w-Ec>`9h;_x0*(7Bfpxlzp`{?kk4n#R=u-5C={=jDle=T%QaogZLAb)nak7uPfGo;j#gGwr(RxVwXm&2ltK z)7q-{Ti&2rE0orjGnkJ#KR>8dDy95tZmpa_b7R}II$KW9w0fyvehOcxZ+~vi1}Ebv z{}wZ@Ijw%%a&et3({}oopYOAsed*%krK-`3>*?7)PqLXYG2(gdskj{C<}pbC3{~gA%q$H5}!U(fZ&4h5vja zHoxe-htct)ax{j#=Wsk|-S51NAC20^uW#&#&+ol(eyd?8DaXecA5YxA?Yk9nwcOaA zY?wbG6*rkPE`cA$&$DH_p4P{YHyO59*#q;iN?x}(3gLKmyS{D7sm8~f%mz)P@ud7o z=NZk<>A3#YjgL1Sj`vnDtdeqcO~R@}NjP4!?$!PJ_;@$bU&7TOQBwY-@#H$lHPgDZ zR(`HnSc~rAAu1(g{jG}buKjQ@xemB|qwolFmk?(V$LsM7r{myB1s*-v4eYy!k%GADyD1AO{coZGvI6wN&&h1|p zo}Rz)_Sa_rUb^?kIn>E<)zm|Nd@?Fa9l{ji5~B1Cj{6~U31#%%o`F-ADF2D6+=u}C zgMMkG2mM6taKC!rbD~~Hk8?K}Z(rSWJ*%Bsj{3-P9!HerP3Q&Lo6rcbFC_n)UP=Epypa2CZKmiI+fC3bt z00k&O0SZun0u-PC1t>rP3Q&Lo6!;$tY_7if(TnfnU3{{d_I!A-@l@96%G&uwq!ND` zu3MM9tRJ32&uPCm3Rqx!GwY|LjkJ+<&JxE%eEt0RzT@&Itx^88rP3Q&Lo6rcbFC_n)UP=Epypa2CZKmiI+fC3bt00k&O0SZun z0u-PC1t>rP3Q&Lo6rcbFC_n)UP=Epypa2CZKmiI+fC3bt00k&O0Sf#-37mUOb!WTT z?xpTbb@_Rv&Sk?duv_h}x2Mgz`E^S@|IK$2@4gjGJE7HSb!y9T)R!vD&*rt7nNYW0 z!N3*( zZ+U}itx#H9&R|C7{QRI+sg&}oxwUcz&5do->TEeZ)9R&y8LDkF)&3lv4^GEXT5fgY zn$zmHEf?3yGHs{t8s?5`$an7-{YwT5oVoV@A;#|epd$9q@PMu4u*du>HuqXh?Rx!& NZrGis)r;=S{{kfq;2!`0 literal 0 HcmV?d00001 diff --git a/test/gcfiles/libnoobjc.dylib b/test/gcfiles/libnoobjc.dylib new file mode 100644 index 0000000000000000000000000000000000000000..56167e2988e548060e5b67bab17adf9af211be26 GIT binary patch literal 41640 zcmeI4U1%It7>3W9YW<1W%lZR~#-f(&BsGK}EZxoSk_0!qCD|0ckTE+ui7WdPc4uRH zGYwb~F9mNEEEIdwURMPbyihFmN4ymDrlkm83%v-w@64RAEfQ1EQp5X^Z@%-LIcMga zXL7R#zW0Z%FTOIy+y~zeA2Q|u!t@$0!>R4QH`#ZVeQ=owh=2%)fCz|y2#A0Ph=2%) zfCz}d|BS$;n}2L2Km5%DSUZ2<7jPQz9?k*q``>&6cin37wJI8emQ4~w&c7edLj;=Z z2L476x8wZQ`R9)s^Du5O-eUXArp;Zq-+rgjuDgx)N(Y^{ui*mb&Z3{uzSqvRk?Ws= zyKZT&{A#fK}$^>~L5ht*1@l%LJbl`|Obta?7w z3c8-(O~>mqt6tjMGrPfNI)?u)&hbvO-|~C-OupCX^m~rC-gEeUCZewe5ZE?HLMWl` z_JV%RtwGGe@5UcqR}l~a5fA|p5CIVo0TB=Z5fFj9PT+E^|Ci#~fYtxo#32Ip|7efH zZ@_QY-{ZZ=vPrZd$?sGX@LY0EK`lI*&EpK$0c}9`?M5Gv4@05-5dUa#;HP%^!O_ zw!`L7XwvTw{oe2W-n`%Mz4^@OFaz(+Z|?r$mrAKq(9c3gl$wC~#=9uSQu}Q;;eK;Z z+MpB?AOR8}0TLhq5+DH*AOR8}0TLjAPZ)teeE7lL@WEew26G>Wem$+!t9TE9=YN$( zp>?fvwU#)seCWeU{Anm&9{8fQXYF{Q1Cxxu^;FPb>>)|3VdQIVx?a!jY=sTU9B(2P z(^O*PA}=x`(YoJx(e5<0-Pv@|n9Ok-u}f$d8;^_P#R`LO`wXh{my59@dDLSXF%%9W zTDL8)uFY26bp!usd{z$RkBluZV0;0CC_MBJ=g8)jk{n_a^N;0lciRo8otN_T(IPyS}xb;>bLPh#gf*i9IQYT#GrV*5&2O)1`&w z=$;&+F%N}(7r}BZq;Dkmqw+=QBsAXMvKu|?THo$j?v|Np?!s5+kVTkEu6>B&CuCx% z)OobYTqW<5@MWC0VMoz;2K7~_$bF$oUwMHRp99=`k_XXC#`#t42S2&o{c`We&wb}B zpC5nyN7HEY=Q)N^ppQpBtcZ*^3zb+IU#a(?DP{ECo`Dkw!Jvj6(7>sjs0d=&F2GRnN?hQa^%vCtQe6cQi-5+DH*AOR8}0TLhq z5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH* zAOR8}0TLhq5+DH*AOR8}0TTFx5ZHL_{pa6W#5#0Yk^4|M>A00OzOt@f*8Gb8G+ZAq zby=T3hR9%LKO9K3em=4iFKd5Klzea`>VL0mnWdksQ!I5(nhy`<9WjbTbE{`bV2A-vzhM)f;Xh z$h!Xw3fYt3b=c{!{(ab2!dO(*H1sr7>J=1H--Dfk6_NI)6p(jd$I(Id6Zogpp_2dK zu=vR~2WmSy_v+P&Sx5ZyVW!bYsICU{iV4}gJLmfw(6a&!9r=Z zTzz(>RH^A&VQsNg%U#T7E+$gLqtQ5-SYB84`lnWI!ES7RWWtpD6ZOxd2mC{PC z-7^Q(^<2AdI_}P3ZKD!pX~T8 zo@wSN&c3l==evIXqNrT`OI!!AVL!(GdEw&!0TQ`w!!=T6@-xhe{~PeDeG*672;T z`9Uknv>NW$QPKL~1%>~9A~C;c-@|D9$d7W!K8JD9y5D&b4~^Q!(>D$z=XX1t-%=PP zelkZqpK$w@&uyVlE5wfE!}tEeuzKJm3jUcTOKe{Gi(;*Y)Yu3HG-=55O6Y(Oe zfl=arR6e;5a?P|ZFIS!}Ei6a(@DPn6dB0TA-L)4clk0%Xw=egjmW572+Rj7#6MP-Vg zgG$WDl?M?Z$ z8GW~B;G{+6ADJn{AfSiDWuXUgLho=~z3(|u)X{P7q?7&EcU{kFXXc|ABvg+=W&S%u zrR3gyEcAsZg#<`|1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@1W14c zNPq-LfCNZ@1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@1W14cNPq-LfCTMbJ_k4G9ef}Ax&gR1}uv_h}x2w&% z`Bh6jbM1}Pn>T_fr?fh)PHj4ldZ0Rgxv16Th`RL>J_=qe7uMJHw8rKHmsU!(Ld<_M zt!}=YQfak^G7t$7k$nQ1ek~||pYZ<#e{&?I{&Br@MS4hl28HZ#Ao4xbr(^yPQGX>k zH$Qd-Hid*-0Jo@>`l$K4sMZB(KxO>48>Z+V0D+Cq7GK8G2Z7cLBH)oQu8R9LR$ z(B0TFtJwhJ5dS*1u$6 pfiu_sA0l?&2Mvio!#(zrVUPJMHg{W1?Rx!&ZrGis)r;=S{{U2%Gzo*_IPZE z&7shw-v>SK^FHsq&-1?XnbBbe-gn-=^XuO#rA|S=0v%Oq0_K$uQH+o5w_St#gFR`3 zQb>RVNPq-LfCNZ@1W14cNPq-LfCRo^1paXQlRM#qzdD1tpM`!urPOP94}j-?l|rF) zt$3xDII&#l!%F;VC|(};qP1u3c%cK6jKBV^pugBdl2XIS*V=Twp556B87X&0<2}SKqg`w~E{Yc`41VM@s4P5Hh#kqH9@B`Sa1hbD zZFzNVw(71M_($V2av*nPYCa09-5Sy5PEQh<>Za6J}66mY-;T+E+ zRyvD4IY--R_FL8g$HO_gVGgk;=dj!Lt^E{7<}pOBe@Ti$DxzZczerk^sK9WyJxvuX1cixU!6x5VJf-yA&Q@niJ?*# z&?a-0yidZHasC23hQ@nQUxSL=6RPx;7ijT0z`ZMZ0KH_Kw|?@cPrviKZ~o>N)xzvw zZ>RUB(dN%{9HT%Vi+orS8E+0Mu`<3=A3;Zy(RX_WPFU1we9uhhLjX7`F~Z;=MrM3C zrr!6QDB|cCw^GUe>$|RJwbKhx%p=Gs^PV3D|KmqPUpP`ofCNZ@1W14cNPq-LfCNZ@ z1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@1W14c zNPq-LfCNZ@1W14cNPq-L;0r=vyRn3I=&~aB!En-XD{FjZUA?UN75!znK3?jw zK7SmM!ODI(kZApUWF=nK{+=kg;7HW}Zr3tPKUt?PYqMqD@2@bg=i!S4>ym>yOA;Uf z5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH* zAOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*_!I6tbczob&??cs=2{B`@y5ZVW@QT-O`o1-J@AFmfL z%ewzG3fYt3$FS33{b#VRhOwxsY3OOF)XONOz6U!6D-n zsq*YH$T3pSx zd*-0Bo^97n$K4rJH_B0#rnOn`x4gl6ZLzenki~q=ix&sAN~Kg-&aafS=x%J8R%g@c znN}|y%unIl^zARr+2CXx<=8? zFQwFL{w{q=eGUFR9;&sj6|dCbBqf(<2z}Y-PoClZhjCb~J!{89C6r`7d43m>Ifz7l z;Gs;Z;eH(ztq)#M`0pnY^NaR9jK+`rC`WS7L9P3p7xB=jZ9IMZ`46A(d*S?+!vx|d zb40nt3b!Bn+!ph-eC$Xrte;?oo6H%Pz?b-Wwrtna`uKcfVSkl7U>-Kfc8mKG=Cj-N zElW-{nQtr?bdBN7p25I%L9p&AM0j+mrdOAzoxH zFiQN-$|u)Bu9?=QmGaZY#g*tD9->hs=a(wFyY|9lavgB__T@g*GSEqAydCdwde+sx z-LqW0ufPikyWz*2M@H!{c_&~dV5eY{k{Jf`JB0(1dkA0p4_`(*8t+AY4JsmaQJLab zp%U|X+xOwx&H@^GIcNj zQAXeG88~TC`A4SnF$m}(aT(}AoX|TQSMPgH6m@i*Td8FK^f0mlyctY+M61*4)TZO8`zs4i6||ZhRo7p_N5KoF{QA0{*4Uij(rU4mkNHog z)U}sKR7&lk3`9aiWS@YhUki$x3IEUVH%CX*KVL6imL3wHMj?9~i2M}w>6rf(>aPZe z{1UF3hMo!A#4n?W{NJNK6&@4w_k<5uHKhpUU#OqO$Kj)W21aoL;z#=rOp1>_(fl5Q z4|hL9Ch)_r%*)tDw`XtHJvuksZz*h2Bmx%4NJu=zR+%&p1p0& zn1j{gYPQ`o2bJ|~yKXw}&Y-$cjYRgQwzeMMQ({YrRTiv+qwEAt!#kI0b+v&T8x$PSAz56-;l7R)z nT>F2B*nJ-~B>oI{*-M5!=C9b?Z8f#)^&7fjcbZl&x-b6&uxRCk literal 0 HcmV?d00001 diff --git a/test/gcfiles/libsupportsgc.dylib b/test/gcfiles/libsupportsgc.dylib new file mode 100644 index 0000000000000000000000000000000000000000..8a1efdac309cb57c33805b544ce9ff7e6ab022ee GIT binary patch literal 74696 zcmeI5-)|#V700hP-X&_YY!(CxYAdCysM>ak?Omc(Bx+&DF-cngNE0X8K2*2%c$^Nh zJ;pOm6A8IqXtk}x!~O$E3lH$PeOOAx11}X)1T8EG2_8V|LyLHz0wH)Q(u(hyAIW5r zRNDt6TE0g<_uO;mo^$VfX8bTC-8;Xy_tT##rA|RV107Rp66V|QqL@hRx7~vKojqxT zQb>RVNPq-LfCNZ@1W14cNPq-LfCN5a1b+452ls*pe{}|PKL`C*QmI$)9stk(Dv3ht zTJc&fc4E1}hn4u#P`o_wL~GaFaRUb?9)J65et)rtB&kNBueIU0eXF|_G{keguZlEc~UH0-uF3G~(aaE|8@ zE1kujoTFnm2W|6!y7gxf2 za)ibl6!u*N%e9ccp`1nK%g`xkw7qRLoI$T=_g!bpNHuris|&~?OeNPoMDb%XIa2B( z+GMVh_euCN&L3dM(fA>Z=Mqv1^Ptj!-~jwb5Mzu@s)ZHno#M1(@*1sMeW8nj8r}VfTI#43=U#s#)o6- z1J@2Cj*f9R8SlTo>$qkowGhTUhKw@r`H}xWemwAnBZUM=fCNZ@1W14cNPq-LfCNZ@ z1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@1W14c zNPq-LfCNZ@1W14cNPq-BAp|yFd;ht&maq<8R^&b$Ogd_1jjyb$mo>klKMvN%OI_CI zPax7?*$)R2uAdLB#LL>>6D8*#3H#sgT4w1d>(pg!wygX83Fh@Yd=Y)oy4VsW)p zeYRYz)O0Pswp6TTFK1GhuMC$q@@tFbe5q6|WHZB3zJi9OViu@O!?m`}Sz}l(mb0C{ zF|4j(?#}EB5M|{sgtq0u~@zAI(Jbiol51;Rw!Tgqk1meeY zgt^5Ew^u!Gi}_kUawHejPq4yG=8Q|=N%TD1mg8!De7^CZzsemj51M4V#eE6#S)KZp zDW@9GH=gsmhWW(*sPhcxXLlX%>c;a;2KjFKX_fflH3^yynIK=Y?$*8bc)nYR7g_a< z691#}$#sxxrgdqh@^o==CA^16XcWnLr3&w^y&#!f2VA~=Ig45bIt7ik;~kDO==JQr z>)?F_UO-q4FXjR=N`J{a2{Q>h4I7ut$e-US9FW{2_|kuL9qnj*5cO54h}1=8il2o_ z%*U075Fq3HzC8AuH|~7(=huJqxwHTL`tLSp&?e)mi3dD=BJ`yVG69{3N~}Nbd#EK; zdf@caIB8M&N2c--22gN3!Xa-Q`HiIqMV1N9n%BEEh; zdhEEmzef1zk&l~aFLyLYwB=fGJnR=epJ=~u9eT8lbrK)}5+DH*AOR8}0TLhq5+DH* zAOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8} z0TLhq5+DH*AOR8}0TLhq5+DH*_+KG#{!!JN?&W$HdsFKR&nR_17kq);?DX7SZPbky zO!dq+Ur*e*=}$SK)oFFAW!viW)rF@DT1}0q+b`jx;Du6teO=FJY)){sT&(3I{*y^{ z>v}>Z)gDS;Bt%5^321sXzj!C+{}z60ETR5#qqr$OBtC^g_BasvKI$_O|A(l*>>u(< zxM~J^CTJ7Cf+F6EN6G-uj;FF!$KizwCmli z;bL*MRDHHwtkiTZzqVAYWiMw^m#++$Hu7tW<$S4BEo3vpQoe$QrD7IeXg3Yl+BRp6 zVYyh&cKXJ!x}NRS4cplnu5DDpEKRdjAGF=!dTp_^vXI4$j7yh>wQ99gSkA9hvgn@P zGR$tv?i*%5<7%Q6rQDpT5Uv)2*v!$R4oK3SwIP@pq0FKU7Xf5Ar2)ff?;iM;w|eR?XGD8 zsuT%0xB2MV;=++jLG*y&R3VTs-`kzBch^qNNX(PQGjHC^H}8Ej zJD%P7{;%KuIl!2}Ok1opaCcR=r_W z5^q`H$?>HwJER1jw1;@SVL2VkMWZC%s=#|#0FX|+4`s~1cJ*?NH!Z)>GMm;Io-6Q9 z2msQf62pdp^KR>k-KulDwbl;w_q zWwcJt7%N7k8P-E=oyhePSto=$nuqmvWSJoD$#^L_1aP8$5l%3kuyfvWxVPD?wjJK_ z-7&nB@K3r$dJ-?nBj=8}7hXo-N&iTX;?Z-lU;2)m^I)CWO>^C%sZGY4P2iCp^(O)q z6a}7VIj*&a$0Lb1Bk+&~LVDCcJhRdi#j81{=MiEO@1($!@sb|J14s;Sv@ewxE}So( zS&(~5Hk`MQ<>Wypkvj1vO;mpr``+_Z$TCJ_r;slpm$I4-Ol(8s#2KyS9q6=1rk`M} zgZvPZ>__-AQTymTL5&Og)Bcu}dr^@^nnKdMo~t`{Rj+S4b?79IAdw%}>QDXt>3662 z&MRM?`|{G&o5xU>Mq-B!2I(28Q{C6tgAp(FOYVy_&MFew!gX;3C5`EoP0zQQdfBeJ zrn{-1rAPfr+kMZ|3+-CloDH6Rs?aZ5u4lJfo^FID^bh@SARpmoq3wxlbUKbzskwH? zU#wm(OdcW8DB$U2WE@TV7PMm=$sPC$Lz&Ibf=9cG#*p#2Kg4uAw^!U4`n>H=a;u<+ zHi7#t@=@Hk;Lizd4?fSfyREwEgTl$!BfG$l#>?0()W+kb9bwjpM$6cfXhrMe^K7`@ zpBDNKbR~)b#eiZ!F`yXu|1t1JsrR$-rg5>dw9vcL`@6J#I)g)8X?tEnR^C4S11K>D zKceDKluEsuP(2^X>PoLU{V2w^EGF9;LpJXijG|I>XH&r&K|igvxYoo$++ow&F5u$yt%(RWPmYzD0>J#`b(h%=$WTtbje9ycZso$lms#Xjr1{4E|0mXn~ zKrx^gPz)#r6a$I@#eibqKgPg)M>Cz&GGq7W=yEBz4Chtc599LsbvM<2AjcQ;=L)<~ zHYycX&avsOSFBc@ZQa1vjX<6Hu;nfowU3)xiX(Bs?lrNgWH5o2Z*kLWW;R?QP(ESQNV^DP< z&*B&Q@JcNo>huXCkR|-WKD=6_0jaa-kdqEf@@_&ryRZ`{W{g?ath+Dk`us~T%+1fe G$o>UZ$ed*W literal 0 HcmV?d00001 diff --git a/test/gcfiles/x86_64-broken b/test/gcfiles/x86_64-broken new file mode 100644 index 0000000000000000000000000000000000000000..4346f8ac435152adab696f59ce65004d50094f49 GIT binary patch literal 8576 zcmeHM&x;&I7_Cl>8I#4#Mv;g==!C2$A?&O~!AoFfHxp-d$&Z~?M9|92PVdfWPtVdl zLw3c(K!~t}z+4o(?M3hq#FPJkg2~B)B01!!vH=AV52E1sUUl_sb&s37P#;uRy{fNX zfAy-msqXsruV4S!FNCvKi2fcS#3a()hlRK_cKsld|ircjI!5(|qnB3JMIL16G|C% zd-Z5km5uKm%{QSv5}b6%X+B7%EasC<&tLOG)MWX5&39b;6I1t*jm6igzq>5kUbh+4 z+TIx7K=UQ{CpIm`jRq-~qi)y=R<%<$zK=EEBf6c~v~j*(aJ3aQWGh(dz%k3WrTMsq zXvA5LxFx^_5z8$@(+a9eKI- zXi^5gj5gZ!jxX1`;jVlM4cD(37w5+ux0HVEL40}iXM71CvB}5h!Cco;s&&$8*H%4F zMRvTd=KF0J8?hM==u)QnBF_)K6+9kUz7v`+r+X2b@j*A#?3m_T_G?i@#w_1VhL70f z15=7`=e@Krf8n+Ax%uRs;%4R*nATU59lu9(QPm$q(+o?l5FASmc@cSH6<3H80BgwU zN6onQJ_F{Qx<`e$g8U%TB_z}6%Vf@z+0Jqf{gY?cYIMdEv>(2K%?->MfH8-4?r z_7oE1`1HGbC%^pu%=4d|`um4lozshauPC7o9zakB#Hj=$r(rC8_TzieG~VY565ANF z5I-X9(@vqf5p}(GVWCwIYvD%WJRi-go$$S=aK6(E8nte#6HKcImbHa9yf8vdRG2f` z!AAae5>i%X^gW~hchhO4{@P3-H$7Ox@7he;yHLxhb=dr*OMA^ z-mF;#tO8a6tAJI&Dqt0OkOHq)Hh-#I`z2qwvCnbvDSYFl-{D@_{AQawjLOZkmlZ7k z`Gwf#p6!J@o8>V{LF<0{0$&1XJBR;muCt|K2bS$Y3wY{;AJ zdjB3uYJ)7oUJ&3L4O=+eQ#?&Sn3;2r#y#AN$eH+EN@JP(<_Yx7tKO^?&f_XW+xv^c zS5lJ-%i+x6aFHiZ>ZDxOaoCHK?fz|j zkT`S(9C4UGT$F1#SnjTffe4R0R*VlM+tF&(3mT}?=182W?vd@L^wz!QUf07}d5w%m z+qG68ruegAuh@E4yC(%!A9B>mmij;Ogu+7o14;goARqnFID>Y8(Gg-P#-vd;k1N_o h7>7~HS)cb76YS#Tb39h5V%_!nS2NTC1# literal 0 HcmV?d00001 diff --git a/test/gcfiles/x86_64-gc b/test/gcfiles/x86_64-gc new file mode 100644 index 0000000000000000000000000000000000000000..23246c22b53107dab8a66a5c06b88481390e5dfc GIT binary patch literal 8576 zcmeHM&x;&I7_Cl>8I#4#Mv;g==!C2$A?&O~!AoFfHxp-d$&Z~?M9|92PVdfWPtVfb zLw3c(K!~t}z+4o(?M3hq#FPJkg2~B)B01z3WdnK<52E1sUUl_MO|P4~P#;uRzpuV} z{ne}Lrn~F=zkd5?zYxw|A$og+5R)i(9~I)daB(KY(1IJ%7y$;mPv(n(w%dC#LR0+mTEzQR@ zL?g~}BrVBNO0OwbLJn0~mTz109nqbLv37SguDiQf5K^8iEtQNgtgo?km##l!-I14T zk0xc{%V?ur@Az^p4tM2CX}EsPyf{DRxTW-K597XvAhdiFb6=e39pe-U@DyEZ>O?AF-Jqbc1JPns3>!MG+aZd@~t7Vv`R{ zV|=6M(!%_OH_GSc(`SmCnO9(1Pfd369MPxBe+*4CJadJhHaXNq)TvcmAx;3Sp=KO4 z{Or`wEz4nBo%yz)E3E1TbK^9`eN^Xz2>%YS|) zw)xKX(w)unADg!;*S^ia`%Ql^bql`1)R#C4v2gRbPf(SrgQ@GN`%O9@{4y8}n(X@E z9?sMYvIx6DfNwOs!r_|YS;oQ4n(t_QhkF?{3%^TgJm-7!1bXIGZ+eCEq^i*N;i7QX z^G<$Zf38pRjC}+ix6|f3e1*`&A0jq#jz$#!BunJfOtOEZp z1tyQ>7fbkuF9cd$F_KyN#a} z%A7t&9Oe%fWIWog zwE{84pACD(*7G_%J)!%Mqjt8`|B0s*7U~~J`i}(l@Q=nBv;z#c5K9Rrjk0+h(LTi3 gj8egRzqgoT7dxLjbCoLBT@)K1cq+;c1(Q7f1%l~7zyJUM literal 0 HcmV?d00001 diff --git a/test/gcfiles/x86_64-gcaso b/test/gcfiles/x86_64-gcaso new file mode 100644 index 0000000000000000000000000000000000000000..9a58c23c2353415bf3e89506ea46fe2663d39895 GIT binary patch literal 8920 zcmeHNO>7%Q6rLrN28ifZDX54aIaPuJs^V5EQZAM3I4&-Pq={1m6=-5Ro7iQ&wszNT z!UaneKT*}n9QY9@4shTWAugbTkU%1pkjSwI4jfvPN+5y6A>vT;z1g2P z3OV=uM$zl2biQf9Hy~O_EIafIzI%isE(w&0WVA*@O-U&-^`inA;l>GoX}eD{ia z!m9N0HJpnTXOUN&r5YOQd>;rtib)c}I!9O&{&H@W`I1Xh4MgYrS@0beoe1Mv*j2i5 zJei(PE6otEkvNv|ti&<#%5~!0vAMTeE!J#a_g&0$XFf?o*Mn*ot&ciy&h5fpd?xyn zeUgu`%176kdYyjusXkhMEQ~S z)f_Ll8|Zw;1Yb+^A*|V_uyyS7EZen~a68cXz7c$KzJygi+@qlBinNceTVmAtHZ(rM zDj%43;#(~EgnCUPoE+ffXJ;*k=KxtshB z9(k`)+dKe}K%)D0vf;T&yHZRpuG)*hB#$7G9kcKMVZLRZNMHTp^@XX;uYMSY4z7bi z_XX)GiAe{+T$uOh0NQ^Q)e3Rg+VdqFZoRlc(rbbg|~X>LpLr z8V=U8Qgg)AhP-5gU9|eL>{gcxcMzG&Ys- z4iqG09g*Lm9mT&W&~um)$(v%l|H>o3S3{P6{*~Va@_l)?9xKYccWV>tV-Lh0h&>Q{ zAof7)f!G7F2VxJz9*8{MiFjXAwFf z2FL#f`gpa>tre@(@GYFl7s%kzYN6t=qroiMaoW@9W39(=YNJ{fZp)mNhKBH5AKM&4 z>+K=Eu*F1@k~qPl>mJTH)b%=s@8DM+_%+JXi5^`o*1ea1 VIxnljbyZ^^y3#TVdLlBI*xyyL45|PC literal 0 HcmV?d00001 diff --git a/test/gcfiles/x86_64-gcaso2 b/test/gcfiles/x86_64-gcaso2 new file mode 100644 index 0000000000000000000000000000000000000000..3ac79a2f133918ea56a660a31c210729f126b194 GIT binary patch literal 8640 zcmeHMO^6&t6t2#XS(C*~+$a%$=v}fBf7o8pfS16`P9}EZl1+A2Sp}(`pWdC0Jv~Er z57`xkkr2tS1jd5_ZyrTFcu?>dR4_St_24m)2`G3|R2;uoT{Y9wlU?vA)Pw4(_v+PG zufKlX)6?&VKY#t_0At2p#@c%rV^e6K!f=(D7%}!3nvKT!h1@HJHw(*4WE~Y_RK2f8 zIGfmj^OeHt%BYK2pBc3=sUsA&AB}nv&Ld|tipQ~@!aIzUAwT5;eV-arzmK((D(7L; zDu)A8I=(fDHz_@ot`RN-|es`;h5 zGltidcxr#rlQM#h3C=5F6uAC|bV|o}L*hLs=Sfe>I9|)Y?D|#i`s)oirtvF=5}?E9(&E|kuNF?9RnHVNbzPy8+!Pl(evZhYu+QMRH%}vzG2&(d{VIATYbIkS zpj<;we#9B&-c9Iq4eV(=*U%qAyMUGy@};J7C^@Ib^H{I$U1g*GAv6<>o;y}63@pzr zTh%SE3Z3L}G>YS!YhTTL*gEyWr=VP0uM-0=F40l`rM*w@J=QIf;EXe6CGy|Fe&46Y=GoTsxZw$O}y|uk?yHmK+xn*DZ&V1|JZh!VO*!r`d z=xsEaxsVb|lM}7S82)elb@p^5gt` zaRJpG&tVsX`R-JfDo>(P&dVtFqO#q-g^v|NjPB8~wW!xPl?au!ZjhPE@+Jz-(K_q0 z;MgPCxKLRSH;Rs5g-wdVQ77BO>ox9dI+a%BpoU(eou=z0*Jrcz5n?ZEKPBDOfXs&t zQSlPrEgls*9fsdd@I7PjrQ=0RK|yUNmUFQ_DABFP`k?f9`NqusTd zAP$zQB894wFXfyg2ZZT&f2?c(mT?8((KHe`R466 zZ)R7sJKz2F%Rfhiunr5+J0ygdLfU;;h?~MjNr)$qDo9dZDZN&Hr@VZbwP~}`@IMiG zHgN+fSITd%q+L}1e7a6(jM&5k5_6iAp|crA)^osXbyGcgSfzNTw{}lTF9naD3Ec`CQF+R{IlE_o0o&*Qvk1D%(!j^lEKq zjIXcx;`TkF6v%eSNXxQ1xN zS&pbBx=QIZ<(ki-D$DZiX}(js6EW8AfyQ;Wiv=O&h0;nX;fD2?nddDxQxqavH|cV?VwZlg9g8Lt-0a7UD;Q z{eo4fZUv#!E-ba`e$C%1T;!v9z2kon6fSnUUZWPaI^K+WU|Cyu%kcx$1cik}JJ`to z3R28(S@*zijmC!YMYo!npTRg=bvVPa{SX&w)BU*eXG+OlLOcsE>ab%+^ZJ~|oHt`e z0i%FXz$jo8FbWt29;CpVmF@eL8$ahOw?Qi$E!>HUoe^tTqpI?eS z?%C$gC%qB(YS|u2{{wLOKB`~-#m$)dDWY>!aAukw0*cNtj)ZY zpFEQ5lRRS|frr%}>zvKppT^&r#Kw#QMggOMQNSo*6fg=H1&jhl0i%FXz$jo8_$aaim;qut{ItB+giWJ~>@cwAwj{(;2*NRSWzXuODafZ-8hDZ->twumd*hZu)Z e%2}WH7GrGVY}T7@ln?HEBehf>043+E#H@5Q@W>#DPe5QnwC7{fGlXNOWq)o7k{kN4sk^ zK^%;#B1KgtU*Mc02ZXrs13;n`2d)+3$VV!{1#v(iVV-Ap*4bT?-jU{&W@n#w-g)+& zXJ%KsJKz2F+doHyuqK4)9}+@LquhN^h#SJjnGlboR8ge7R6bvMyRveT-n3b1_@9V8 zTeyIftChD_(;=#VKJ614BR6pvg*8pe(Af&3^9ZN;>|+=V8s>Ef@dNwe1pOqGG6;K( zVC2f?cSZ9}>4+G|9deovk|~S$WXo|k9Uq=7pR4(1bUZP2AK6%b-Nw6Xvg3p;uikOS z_y(FUzCLl%Qru{eaxDn`wzr{!viW_Y`5w~k#7P_H>v`AOUQ@Qc^)3Rle7l;DYludi z<%n9Mqm)iduKOISvMk@8<~yM~5o7HhXk2%9u^^;8TV5?E!mz$l>n>h@iFHR_u05KR zo-2dxPNVC}&CoxPFQ(!8P3Fb? zcx}y>Lq8gEGM~u1f7E<|mDcR2R9Qw!iZnEuR02_tRonkNWi*CD7Y;LNX z%;Vb+ue>@l|HAn%C;u)?eE-X7K39}bM<&YLl# zfKk9GU=%P47zK<1_fz1F>dsHq>%Zo!HxFADK80_-{5!&{JKyZ_4WoK%{<4CVKfe%r zd}n+9&Q9fzo!iyxU+3TXYA`H(2H&voIgUas-+Jm}ROQ;Ra0B(AMd!Vrhr?luUGM#X zGxdTj{GR9G8x5~;xTbiLaj;~~cQn4ky@Z;D-=#F3^SyZrJ@cwJy~27_RcL#EMOa&T zD?fE4Hz0Y&J^&BhAL%-7j=wWwY-*!`QNSo*6fg=H1&jhl0i%FXz$jo8FbWt2{yz## zpUSV4@egCVBrh(!wkQ{u%e9)Ekr!$U*xhj*aoL|694~R_N$r%&1~z-Kv)#XqpB2ii z0Y~fSkC)^oHkQM6F%bTVM@rF#WGC3DIbIV!ZI;BI>LJ-_NoUJh>xB;X%Ijo2*{QcZ zQQ(h_39C~YF*hxJq?6E+w9{Bz7&e-? z>k=3K1pk056W!oOegYd;#y{YKaL)AA&eZC{^}ETP_s)HH&b{x@tgyYsXwpm)4n^e2BT;py@N_4c61|^E#>8RtCYRu7 zDKgd2wnGQ74Dxj*aT&zNT?+ANKb_zx4pm$A7eT$EL4EH42ZMaaj*lA9h^f=xOx0F( zuTiSmZpI+rnd7_W2#9-k8^+n!e`9TPz3^bIrycZ3=;rh~b|lm-I(K*$-*;(Pfx#Gf zn8!P^$2>6mtGf6E_Ci=Y2%&;Kn8Ps4MqW+S&BlyjBC{R%McI-GrcIsN+M zR&n{!$KW9K>dlAuw-G~2A`_P*U+7`RU~{-H-{bd>8|&oZJd1iZC-^-~zc%VWK5T}z zws?OLeFWw1+jp_B-Vz@Cx8@zDKHj_nUIDLwSHLUa74Qmp1-t@Y0k6RSDDY(U?f2zR z&p&gPy5R`oD`ytYH21L5-?{mQd(LTT%rcq&Bxa@%qorPMi)m*}COtOZi4ST +#include + +#define SwiftV1MangledName4 "_TtC6Swiftt13SwiftV1Class4" +__attribute__((objc_runtime_name(SwiftV1MangledName4))) +@interface SwiftV1Class4 : TestRoot @end +@implementation SwiftV1Class4 @end + +int main() +{ + // Class hashes + Class result; + + // Class should not be realized yet + // fixme not true during class hash rearrangement + // result = NXMapGet(gdb_objc_realized_classes, "TestRoot"); + // testassert(!result); + + [TestRoot class]; + // Now class should be realized + + result = (__bridge Class)(NXMapGet(gdb_objc_realized_classes, "TestRoot")); + testassert(result); + testassert(result == [TestRoot class]); + + result = (__bridge Class)(NXMapGet(gdb_objc_realized_classes, "DoesNotExist")); + testassert(!result); + + // Class structure decoding + + uintptr_t *maskp = (uintptr_t *)dlsym(RTLD_DEFAULT, "objc_debug_class_rw_data_mask"); + testassert(maskp); + + // Raw class names + testassert(strcmp(objc_debug_class_getNameRaw([SwiftV1Class4 class]), SwiftV1MangledName4) == 0); + testassert(strcmp(objc_debug_class_getNameRaw([TestRoot class]), "TestRoot") == 0); + + + succeed(__FILE__); +} diff --git a/test/getClassHook.m b/test/getClassHook.m new file mode 100644 index 0000000..1d671d9 --- /dev/null +++ b/test/getClassHook.m @@ -0,0 +1,128 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" + +@interface OrdinaryClass : TestRoot @end +@implementation OrdinaryClass @end + +objc_hook_getClass OnePreviousHook; +static int HookOneCalls = 0; +BOOL GetClassHookOne(const char *name, Class *outClass) +{ + HookOneCalls++; + if (0 == strcmp(name, "TwoClass")) { + fail("other hook should have handled this already"); + } else if (0 == strcmp(name, "OrdinaryClass")) { + fail("runtime should have handled this already"); + } else if (0 == strcmp(name, "OneClass")) { + Class cls = objc_allocateClassPair([OrdinaryClass class], "OneClass", 0); + objc_registerClassPair(cls); + *outClass = cls; + return YES; + } else { + return OnePreviousHook(name, outClass); + } +} + +objc_hook_getClass TwoPreviousHook; +static int HookTwoCalls = 0; +BOOL GetClassHookTwo(const char *name, Class *outClass) +{ + HookTwoCalls++; + if (0 == strcmp(name, "OrdinaryClass")) { + fail("runtime should have handled this already"); + } else if (0 == strcmp(name, "TwoClass")) { + Class cls = objc_allocateClassPair([OrdinaryClass class], "TwoClass", 0); + objc_registerClassPair(cls); + *outClass = cls; + return YES; + } else { + return TwoPreviousHook(name, outClass); + } +} + + +objc_hook_getClass ThreePreviousHook; +static int HookThreeCalls = 0; +#define MAXDEPTH 100 +BOOL GetClassHookThree(const char *name, Class *outClass) +{ + // Re-entrant hook test. + // libobjc must prevent re-entrancy when a getClass + // hook provokes another getClass call. + + static int depth = 0; + static char *names[MAXDEPTH] = {0}; + + if (depth < MAXDEPTH) { + // Re-entrantly call objc_getClass() with a new class name. + if (!names[depth]) asprintf(&names[depth], "Reentrant%d", depth); + const char *reentrantName = names[depth]; + depth++; + (void)objc_getClass(reentrantName); + depth--; + } else if (depth == MAXDEPTH) { + // We now have maxdepth getClass hooks stacked up. + // Call objc_getClass() on all of those names a second time. + // None of those lookups should call this hook again. + HookThreeCalls++; + depth = -1; + for (int i = 0; i < MAXDEPTH; i++) { + testassert(!objc_getClass(names[i])); + } + depth = MAXDEPTH; + } else { + fail("reentrancy protection failed"); + } + + // Chain to the previous hook after all of the reentrancy is unwound. + if (depth == 0) { + return ThreePreviousHook(name, outClass); + } else { + return NO; + } +} + + +void testLookup(const char *name, int expectedHookOneCalls, + int expectedHookTwoCalls, int expectedHookThreeCalls) +{ + HookOneCalls = HookTwoCalls = HookThreeCalls = 0; + Class cls = objc_getClass(name); + testassert(HookOneCalls == expectedHookOneCalls && + HookTwoCalls == expectedHookTwoCalls && + HookThreeCalls == expectedHookThreeCalls); + testassert(cls); + testassert(0 == strcmp(class_getName(cls), name)); + testassert(cls == [cls self]); +} + +int main() +{ + testassert(objc_getClass("OrdinaryClass")); + testassert(!objc_getClass("OneClass")); + testassert(!objc_getClass("TwoClass")); + testassert(!objc_getClass("NoSuchClass")); + + objc_setHook_getClass(GetClassHookOne, &OnePreviousHook); + objc_setHook_getClass(GetClassHookTwo, &TwoPreviousHook); + objc_setHook_getClass(GetClassHookThree, &ThreePreviousHook); + // invocation order: HookThree -> Hook Two -> Hook One + + HookOneCalls = HookTwoCalls = HookThreeCalls = 0; + testassert(!objc_getClass("NoSuchClass")); + testassert(HookOneCalls == 1 && HookTwoCalls == 1 && HookThreeCalls == 1); + + testLookup("OneClass", 1, 1, 1); + testLookup("TwoClass", 0, 1, 1); + testLookup("OrdinaryClass", 0, 0, 0); + + // Check again. No hooks should be needed this time. + + testLookup("OneClass", 0, 0, 0); + testLookup("TwoClass", 0, 0, 0); + testLookup("OrdinaryClass", 0, 0, 0); + + succeed(__FILE__); +} diff --git a/test/getImageNameHook.m b/test/getImageNameHook.m new file mode 100644 index 0000000..87c72fe --- /dev/null +++ b/test/getImageNameHook.m @@ -0,0 +1,78 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" + +@interface One : TestRoot @end +@implementation One @end + +@interface Two : TestRoot @end +@implementation Two @end + +@interface Both : TestRoot @end +@implementation Both @end + +@interface None : TestRoot @end +@implementation None @end + + +objc_hook_getImageName OnePreviousHook; +BOOL GetImageNameHookOne(Class cls, const char **outName) +{ + if (0 == strcmp(class_getName(cls), "One")) { + *outName = "Image One"; + return YES; + } else if (0 == strcmp(class_getName(cls), "Both")) { + *outName = "Image Both via One"; + return YES; + } else { + return OnePreviousHook(cls, outName); + } +} + +objc_hook_getImageName TwoPreviousHook; +BOOL GetImageNameHookTwo(Class cls, const char **outName) +{ + if (0 == strcmp(class_getName(cls), "Two")) { + *outName = "Image Two"; + return YES; + } else if (0 == strcmp(class_getName(cls), "Both")) { + *outName = "Image Both via Two"; + return YES; + } else { + return TwoPreviousHook(cls, outName); + } +} + +int main() +{ + + // before hooks: main executable is the image name for four classes + testassert(strstr(class_getImageName([One class]), "getImageNameHook")); + testassert(strstr(class_getImageName([Two class]), "getImageNameHook")); + testassert(strstr(class_getImageName([Both class]), "getImageNameHook")); + testassert(strstr(class_getImageName([None class]), "getImageNameHook")); + testassert(strstr(class_getImageName([NSObject class]), "libobjc")); + + // install hook One + objc_setHook_getImageName(GetImageNameHookOne, &OnePreviousHook); + + // two classes are in Image One with hook One in place + testassert(strstr(class_getImageName([One class]), "Image One")); + testassert(strstr(class_getImageName([Two class]), "getImageNameHook")); + testassert(strstr(class_getImageName([Both class]), "Image Both via One")); + testassert(strstr(class_getImageName([None class]), "getImageNameHook")); + testassert(strstr(class_getImageName([NSObject class]), "libobjc")); + + // install hook Two which chains to One + objc_setHook_getImageName(GetImageNameHookTwo, &TwoPreviousHook); + + // two classes are in Image Two and one in One with both hooks in place + testassert(strstr(class_getImageName([One class]), "Image One")); + testassert(strstr(class_getImageName([Two class]), "Image Two")); + testassert(strstr(class_getImageName([Both class]), "Image Both via Two")); + testassert(strstr(class_getImageName([None class]), "getImageNameHook")); + testassert(strstr(class_getImageName([NSObject class]), "libobjc")); + + succeed(__FILE__); +} diff --git a/test/getMethod.m b/test/getMethod.m new file mode 100644 index 0000000..3777329 --- /dev/null +++ b/test/getMethod.m @@ -0,0 +1,139 @@ +/* +TEST_BUILD_OUTPUT +.*getMethod.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*getMethod.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*getMethod.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*getMethod.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +END +*/ + +#include "test.h" +#include "testroot.i" +#include +#include + +static int state = 0; + +@interface Super : TestRoot @end +@implementation Super ++(void)classMethod { state = 1; } +-(void)instanceMethod { state = 4; } ++(void)classMethodSuperOnly { state = 3; } +-(void)instanceMethodSuperOnly { state = 6; } +@end + +@interface Sub : Super @end +@implementation Sub ++(void)classMethod { state = 2; } +-(void)instanceMethod { state = 5; } +@end + +typedef void (*imp_t)(id, SEL); + +int main() +{ + Class Super_cls, Sub_cls; + Class buf[10]; + Method m; + SEL sel; + IMP imp; + + id bufobj = (__bridge_transfer id)(void*)buf; + + // don't use [Super class] to check laziness handing + Super_cls = objc_getClass("Super"); + Sub_cls = objc_getClass("Sub"); + + sel = sel_registerName("classMethod"); + m = class_getClassMethod(Super_cls, sel); + testassert(m); + testassert(sel == method_getName(m)); + imp = method_getImplementation(m); + testassert(imp == class_getMethodImplementation(object_getClass(Super_cls), sel)); + testassert(imp == object_getMethodImplementation(Super_cls, sel)); + state = 0; + (*(imp_t)imp)(Super_cls, sel); + testassert(state == 1); + + sel = sel_registerName("classMethod"); + m = class_getClassMethod(Sub_cls, sel); + testassert(m); + testassert(sel == method_getName(m)); + imp = method_getImplementation(m); + testassert(imp == class_getMethodImplementation(object_getClass(Sub_cls), sel)); + testassert(imp == object_getMethodImplementation(Sub_cls, sel)); + state = 0; + (*(imp_t)imp)(Sub_cls, sel); + testassert(state == 2); + + sel = sel_registerName("classMethodSuperOnly"); + m = class_getClassMethod(Sub_cls, sel); + testassert(m); + testassert(sel == method_getName(m)); + imp = method_getImplementation(m); + testassert(imp == class_getMethodImplementation(object_getClass(Sub_cls), sel)); + testassert(imp == object_getMethodImplementation(Sub_cls, sel)); + state = 0; + (*(imp_t)imp)(Sub_cls, sel); + testassert(state == 3); + + sel = sel_registerName("instanceMethod"); + m = class_getInstanceMethod(Super_cls, sel); + testassert(m); + testassert(sel == method_getName(m)); + imp = method_getImplementation(m); + testassert(imp == class_getMethodImplementation(Super_cls, sel)); + buf[0] = Super_cls; + testassert(imp == object_getMethodImplementation(bufobj, sel)); + state = 0; + (*(imp_t)imp)(bufobj, sel); + testassert(state == 4); + + sel = sel_registerName("instanceMethod"); + m = class_getInstanceMethod(Sub_cls, sel); + testassert(m); + testassert(sel == method_getName(m)); + imp = method_getImplementation(m); + testassert(imp == class_getMethodImplementation(Sub_cls, sel)); + buf[0] = Sub_cls; + testassert(imp == object_getMethodImplementation(bufobj, sel)); + state = 0; + (*(imp_t)imp)(bufobj, sel); + testassert(state == 5); + + sel = sel_registerName("instanceMethodSuperOnly"); + m = class_getInstanceMethod(Sub_cls, sel); + testassert(m); + testassert(sel == method_getName(m)); + imp = method_getImplementation(m); + testassert(imp == class_getMethodImplementation(Sub_cls, sel)); + buf[0] = Sub_cls; + testassert(imp == object_getMethodImplementation(bufobj, sel)); + state = 0; + (*(imp_t)imp)(bufobj, sel); + testassert(state == 6); + + // check class_getClassMethod(cls) == class_getInstanceMethod(cls->isa) + sel = sel_registerName("classMethod"); + testassert(class_getClassMethod(Sub_cls, sel) == class_getInstanceMethod(object_getClass(Sub_cls), sel)); + + sel = sel_registerName("nonexistent"); + testassert(! class_getInstanceMethod(Sub_cls, sel)); + testassert(! class_getClassMethod(Sub_cls, sel)); + testassert(class_getMethodImplementation(Sub_cls, sel) == (IMP)&_objc_msgForward); + buf[0] = Sub_cls; + testassert(object_getMethodImplementation(bufobj, sel) == (IMP)&_objc_msgForward); +#if !__arm64__ + testassert(class_getMethodImplementation_stret(Sub_cls, sel) == (IMP)&_objc_msgForward_stret); + testassert(object_getMethodImplementation_stret(bufobj, sel) == (IMP)&_objc_msgForward_stret); +#endif + + testassert(! class_getInstanceMethod(NULL, NULL)); + testassert(! class_getInstanceMethod(NULL, sel)); + testassert(! class_getInstanceMethod(Sub_cls, NULL)); + testassert(! class_getClassMethod(NULL, NULL)); + testassert(! class_getClassMethod(NULL, sel)); + testassert(! class_getClassMethod(Sub_cls, NULL)); + + succeed(__FILE__); +} diff --git a/test/get_task_allow_entitlement.plist b/test/get_task_allow_entitlement.plist new file mode 100644 index 0000000..bd10085 --- /dev/null +++ b/test/get_task_allow_entitlement.plist @@ -0,0 +1,8 @@ + + + + + get-task-allow + + + diff --git a/test/headers.c b/test/headers.c new file mode 100644 index 0000000..9fbc1ac --- /dev/null +++ b/test/headers.c @@ -0,0 +1,19 @@ +/* +TEST_BUILD +$DIR/headers.sh '$C{TESTINCLUDEDIR}' '$C{TESTLOCALINCLUDEDIR}' '$C{COMPILE_C}' '$C{COMPILE_CXX}' '$C{COMPILE_M}' '$C{COMPILE_MM}' '$VERBOSE' +$C{COMPILE_C} $DIR/headers.c -o headers.exe +END + +allow `sh -x` output from headers.sh +TEST_BUILD_OUTPUT +(\+ .*\n)*(\+ .*)?done +END + */ + + +#include "test.h" + +int main() +{ + succeed(__FILE__); +} diff --git a/test/headers.sh b/test/headers.sh new file mode 100644 index 0000000..b6306a4 --- /dev/null +++ b/test/headers.sh @@ -0,0 +1,38 @@ +#!/bin/sh + +# Compile every exported ObjC header as if it were a file in every language. +# This script is executed by test headers.c's TEST_BUILD command. + +TESTINCLUDEDIR=$1; shift +TESTLOCALINCLUDEDIR=$1; shift +COMPILE_C=$1; shift +COMPILE_CXX=$1; shift +COMPILE_M=$1; shift +COMPILE_MM=$1; shift +VERBOSE=$1; shift + +# stop after any command error +set -e + +# echo commands when verbose +if [ "$VERBOSE" != "0" ]; then + set -x +fi + +FILES="$TESTINCLUDEDIR/objc/*.h $TESTLOCALINCLUDEDIR/objc/*.h" +CFLAGS='-fsyntax-only -Wno-unused-function -D_OBJC_PRIVATE_H_' + +$COMPILE_C $CFLAGS $FILES +$COMPILE_CXX $CFLAGS $FILES +$COMPILE_M $CFLAGS $FILES +$COMPILE_MM $CFLAGS $FILES +for STDC in '99' '11' ; do + $COMPILE_C $CFLAGS $FILES -std=c$STDC + $COMPILE_M $CFLAGS $FILES -std=c$STDC +done +for STDCXX in '98' '03' '11' '14' '17' ; do + $COMPILE_CXX $CFLAGS $FILES -std=c++$STDCXX + $COMPILE_MM $CFLAGS $FILES -std=c++$STDCXX +done + +echo done diff --git a/test/imageorder.h b/test/imageorder.h new file mode 100644 index 0000000..654e48e --- /dev/null +++ b/test/imageorder.h @@ -0,0 +1,20 @@ +extern int state; +extern int cstate; + +OBJC_ROOT_CLASS +@interface Super { id isa; } ++(void) method; ++(void) method0; +@end + +@interface Super (cat1) ++(void) method1; +@end + +@interface Super (cat2) ++(void) method2; +@end + +@interface Super (cat3) ++(void) method3; +@end diff --git a/test/imageorder.m b/test/imageorder.m new file mode 100644 index 0000000..ec7b629 --- /dev/null +++ b/test/imageorder.m @@ -0,0 +1,41 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/imageorder1.m -o imageorder1.dylib -dynamiclib + $C{COMPILE} $DIR/imageorder2.m -x none imageorder1.dylib -o imageorder2.dylib -dynamiclib + $C{COMPILE} $DIR/imageorder3.m -x none imageorder2.dylib imageorder1.dylib -o imageorder3.dylib -dynamiclib + $C{COMPILE} $DIR/imageorder.m -x none imageorder3.dylib imageorder2.dylib imageorder1.dylib -o imageorder.exe +END +*/ + +#include "test.h" +#include "imageorder.h" +#include +#include + +int main() +{ + // +load methods and C static initializers + testassert(state == 3); + testassert(cstate == 3); + + Class cls = objc_getClass("Super"); + testassert(cls); + + // make sure all categories arrived + state = -1; + [Super method0]; + testassert(state == 0); + [Super method1]; + testassert(state == 1); + [Super method2]; + testassert(state == 2); + [Super method3]; + testassert(state == 3); + + // make sure imageorder3.dylib is the last category to attach + state = 0; + [Super method]; + testassert(state == 3); + + succeed(__FILE__); +} diff --git a/test/imageorder1.m b/test/imageorder1.m new file mode 100644 index 0000000..2cc1d80 --- /dev/null +++ b/test/imageorder1.m @@ -0,0 +1,52 @@ +#include "test.h" +#include "imageorder.h" + +int state = -1; +int cstate = 0; + +static void c1(void) __attribute__((constructor)); +static void c1(void) +{ + testassert(state == 1); // +load before C/C++ + testassert(cstate == 0); + cstate = 1; +} + + +#if __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation" +#endif + +@implementation Super (cat1) ++(void) method { + fail("+[Super(cat1) method] not replaced!"); +} ++(void) method1 { + state = 1; +} ++(void) load { + testassert(state == 0); + state = 1; +} +@end + +#if __clang__ +#pragma clang diagnostic pop +#endif + + +@implementation Super ++(void) initialize { } ++(void) method { + fail("+[Super method] not replaced!"); +} ++(void) method0 { + state = 0; +} ++(void) load { + testassert(state == -1); + state = 0; +} +@end + diff --git a/test/imageorder2.m b/test/imageorder2.m new file mode 100644 index 0000000..e4d690b --- /dev/null +++ b/test/imageorder2.m @@ -0,0 +1,33 @@ +#include "test.h" +#include "imageorder.h" + +static void c2(void) __attribute__((constructor)); +static void c2(void) +{ + testassert(state == 2); // +load before C/C++ + testassert(cstate == 1); + cstate = 2; +} + + +#if __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation" +#endif + +@implementation Super (cat2) ++(void) method { + fail("+[Super(cat2) method] not replaced!"); +} ++(void) method2 { + state = 2; +} ++(void) load { + testassert(state == 1); + state = 2; +} +@end + +#if __clang__ +#pragma clang diagnostic pop +#endif diff --git a/test/imageorder3.m b/test/imageorder3.m new file mode 100644 index 0000000..217130f --- /dev/null +++ b/test/imageorder3.m @@ -0,0 +1,33 @@ +#include "test.h" +#include "imageorder.h" + +static void c3(void) __attribute__((constructor)); +static void c3(void) +{ + testassert(state == 3); // +load before C/C++ + testassert(cstate == 2); + cstate = 3; +} + + +#if __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation" +#endif + +@implementation Super (cat3) ++(void) method { + state = 3; +} ++(void) method3 { + state = 3; +} ++(void) load { + testassert(state == 2); + state = 3; +} +@end + +#if __clang__ +#pragma clang diagnostic pop +#endif diff --git a/test/imports.c b/test/imports.c new file mode 100644 index 0000000..86588fc --- /dev/null +++ b/test/imports.c @@ -0,0 +1,40 @@ +/* +Disallow some imports into and exports from libobjc.A.dylib. + +To debug, re-run libobjc's link command with + -Xlinker -dead_strip -Xlinker -why_live -Xlinker SYMBOL_NAME_HERE + +Disallowed imports (nm -u): +___cxa_guard_acquire (C++ function-scope static initializer) +___cxa_guard_release (C++ function-scope static initializer) +___cxa_atexit (C++ static destructor) +weak external (any weak externals, including operators new and delete) + +Whitelisted imports: +weak external ____chkstk_darwin (from libSystem) + +Disallowed exports (nm -U): +__Z* (any C++-mangled export) +weak external (any weak externals, including operators new and delete) + +fixme rdar://13354718 should disallow anything from libc++ (i.e. not libc++abi) +*/ + +/* +TEST_BUILD +echo $C{XCRUN} nm -m -arch $C{ARCH} $C{TESTLIB} +$C{XCRUN} nm -u -m -arch $C{ARCH} $C{TESTLIB} | grep -v 'weak external ____chkstk_darwin \(from libSystem\)' | egrep '(weak external| external (___cxa_atexit|___cxa_guard_acquire|___cxa_guard_release))' || true +$C{XCRUN} nm -U -m -arch $C{ARCH} $C{TESTLIB} | egrep '(weak external| external __Z)' || true +$C{COMPILE_C} $DIR/imports.c -o imports.exe +END + +TEST_BUILD_OUTPUT +.*libobjc.A.dylib +END + */ + +#include "test.h" +int main() +{ + succeed(__FILE__); +} diff --git a/test/include-warnings.c b/test/include-warnings.c new file mode 100644 index 0000000..4afdb5f --- /dev/null +++ b/test/include-warnings.c @@ -0,0 +1,21 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/include-warnings.c -o include-warnings.exe -Wsystem-headers -Weverything -Wno-undef -Wno-old-style-cast -Wno-nullability-extension -Wno-c++98-compat 2>&1 | grep -v 'In file' | grep objc || true +END + +TEST_RUN_OUTPUT +OK: includes.c +END +*/ + +// Detect warnings inside any header. +// The build command above filters out warnings inside non-objc headers +// (which are noisy with -Weverything). +// -Wno-undef suppresses warnings about `#if __cplusplus` and the like. +// -Wno-old-style-cast is tough to avoid in mixed C/C++ code. +// -Wno-nullability-extension disables a warning about non-portable +// _Nullable etc which we already handle correctly in objc-abi.h. +// -Wno-c++98-compat disables warnings about things that already +// have guards against C++98. + +#include "includes.c" diff --git a/test/includes-objc2.c b/test/includes-objc2.c new file mode 100644 index 0000000..6fb80ab --- /dev/null +++ b/test/includes-objc2.c @@ -0,0 +1,13 @@ +// TEST_CFLAGS -D__OBJC2__ + +// Verify that all headers can be included in any language, even if +// the client is C code that defined __OBJC2__. + +// This is the definition that Instruments uses in its build. +#if defined(__OBJC2__) +#undef __OBJC2__ +#endif +#define __OBJC2__ 1 + +#define NAME "includes-objc2.c" +#include "includes.c" diff --git a/test/includes.c b/test/includes.c new file mode 100644 index 0000000..1abab68 --- /dev/null +++ b/test/includes.c @@ -0,0 +1,47 @@ +// TEST_CONFIG + +// Verify that all headers can be included in any language. +// See also test/include-warnings.c which checks for warnings in these headers. +// See also test/includes-objc2.c which checks for safety even if +// the client is C code that defined __OBJC2__. + +#ifndef NAME +#define NAME "includes.c" +#endif + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#if TARGET_OS_OSX +#include +#include +#include +#endif + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Weverything" +#include "test.h" +#pragma clang diagnostic pop + +int main() +{ + succeed(NAME); +} diff --git a/test/initialize.m b/test/initialize.m new file mode 100644 index 0000000..d32b4df --- /dev/null +++ b/test/initialize.m @@ -0,0 +1,323 @@ +// TEST_CONFIG + +// initialize.m +// Test basic +initialize behavior +// * +initialize before class method +// * superclass +initialize before subclass +initialize +// * subclass inheritance of superclass implementation +// * messaging during +initialize +// * +initialize provoked by class_getMethodImplementation +// * +initialize not provoked by objc_getClass +#include "test.h" +#include "testroot.i" + +int state = 0; + +@interface Super0 : TestRoot @end +@implementation Super0 ++(void)initialize { + fail("objc_getClass() must not trigger +initialize"); +} +@end + +@interface Super : TestRoot @end +@implementation Super ++(void)initialize { + testprintf("in [Super initialize]\n"); + testassert(state == 0); + state = 1; +} ++(void)method { + fail("[Super method] shouldn't be called"); +} +@end + +@interface Sub : Super @end +@implementation Sub ++(void)initialize { + testprintf("in [Sub initialize]\n"); + testassert(state == 1); + state = 2; +} ++(void)method { + testprintf("in [Sub method]\n"); + testassert(state == 2); + state = 3; +} +@end + + +@interface Super2 : TestRoot @end +@interface Sub2 : Super2 @end + +@implementation Super2 ++(void)initialize { + if (self == objc_getClass("Sub2")) { + testprintf("in [Super2 initialize] of Sub2\n"); + testassert(state == 1); + state = 2; + } else if (self == objc_getClass("Super2")) { + testprintf("in [Super2 initialize] of Super2\n"); + testassert(state == 0); + state = 1; + } else { + fail("in [Super2 initialize] of unknown class"); + } +} ++(void)method { + testprintf("in [Super2 method]\n"); + testassert(state == 2); + state = 3; +} +@end + +@implementation Sub2 +// nothing here +@end + + +@interface Super3 : TestRoot @end +@interface Sub3 : Super3 @end + +@implementation Super3 ++(void)initialize { + if (self == [Sub3 class]) { // this message triggers [Sub3 initialize] + testprintf("in [Super3 initialize] of Sub3\n"); + testassert(state == 0); + state = 1; + } else if (self == [Super3 class]) { + testprintf("in [Super3 initialize] of Super3\n"); + testassert(state == 1); + state = 2; + } else { + fail("in [Super3 initialize] of unknown class"); + } +} ++(void)method { + testprintf("in [Super3 method]\n"); + testassert(state == 2); + state = 3; +} +@end + +@implementation Sub3 +// nothing here +@end + + +@interface Super4 : TestRoot @end +@implementation Super4 +-(void)instanceMethod { + testassert(state == 1); + state = 2; +} ++(void)initialize { + testprintf("in [Super4 initialize]\n"); + testassert(state == 0); + state = 1; + id x = [[self alloc] init]; + [x instanceMethod]; + RELEASE_VALUE(x); +} +@end + + +@interface Super5 : TestRoot @end +@implementation Super5 +-(void)instanceMethod { +} ++(void)classMethod { + testassert(state == 1); + state = 2; +} ++(void)initialize { + testprintf("in [Super5 initialize]\n"); + testassert(state == 0); + state = 1; + class_getMethodImplementation(self, @selector(instanceMethod)); + // this is the "memoized" case for getNonMetaClass + class_getMethodImplementation(object_getClass(self), @selector(classMethod)); + [self classMethod]; +} +@end + + +@interface Super6 : TestRoot @end +@interface Sub6 : Super6 @end +@implementation Super6 ++(void)initialize { + static bool once; + bool wasOnce; + testprintf("in [Super6 initialize] (#%d)\n", 1+(int)once); + if (!once) { + once = true; + wasOnce = true; + testassert(state == 0); + state = 1; + } else { + wasOnce = false; + testassert(state == 2); + state = 3; + } + [Sub6 class]; + if (wasOnce) { + testassert(state == 5); + state = 6; + } else { + testassert(state == 3); + state = 4; + } +} +@end +@implementation Sub6 ++(void)initialize { + testprintf("in [Sub6 initialize]\n"); + testassert(state == 1); + state = 2; + [super initialize]; + testassert(state == 4); + state = 5; +} +@end + + +@interface Super7 : TestRoot @end +@interface Sub7 : Super7 @end +@implementation Super7 ++(void)initialize { + static bool once; + bool wasOnce; + testprintf("in [Super7 initialize] (#%d)\n", 1+(int)once); + if (!once) { + once = true; + wasOnce = true; + testassert(state == 0); + state = 1; + } else { + wasOnce = false; + testassert(state == 2); + state = 3; + } + [Sub7 class]; + if (wasOnce) { + testassert(state == 5); + state = 6; + } else { + testassert(state == 3); + state = 4; + } +} +@end +@implementation Sub7 ++(void)initialize { + testprintf("in [Sub7 initialize]\n"); + testassert(state == 1); + state = 2; + [super initialize]; + testassert(state == 4); + state = 5; +} +@end + + + +@interface SuperThrower : TestRoot @end +@implementation SuperThrower ++(void)initialize { + testprintf("in [SuperThrower initialize]\n"); + testassert(state == 0); + state = 10; + @throw AUTORELEASE([TestRoot new]); + fail("@throw didn't throw"); +} +@end + +@interface SubThrower : SuperThrower @end +@implementation SubThrower ++(void)initialize { + testprintf("in [SubThrower initialize]\n"); + testassert(state == 0); + state = 20; +} +@end + +int main() +{ + Class cls; + + // objc_getClass() must not +initialize anything + state = 0; + objc_getClass("Super0"); + testassert(state == 0); + + // initialize superclass, then subclass + state = 0; + [Sub method]; + testassert(state == 3); + + // check subclass's inheritance of superclass initialize + state = 0; + [Sub2 method]; + testassert(state == 3); + + // check subclass method called from superclass initialize + state = 0; + [Sub3 method]; + testassert(state == 3); + + // check class_getMethodImplementation (instance method) + state = 0; + cls = objc_getClass("Super4"); + testassert(state == 0); + class_getMethodImplementation(cls, @selector(classMethod)); + testassert(state == 2); + + // check class_getMethodImplementation (class method) + // this is the "slow" case for getNonMetaClass + state = 0; + cls = objc_getClass("Super5"); + testassert(state == 0); + class_getMethodImplementation(object_getClass(cls), @selector(instanceMethod)); + testassert(state == 2); + + // check +initialize cycles + // this is the "cls is a subclass" case for getNonMetaClass + state = 0; + [Super6 class]; + testassert(state == 6); + + // check +initialize cycles + // this is the "cls is a subclass" case for getNonMetaClass + state = 0; + [Sub7 class]; + testassert(state == 6); + + // exception from +initialize must be handled cleanly + PUSH_POOL { + alarm(3); + testonthread( ^{ + @try { + state = 0; + [SuperThrower class]; + fail("where's the beef^Wexception?"); + } @catch (...) { + testassert(state == 10); + state = 11; + } + testassert(state == 11); + }); + @try { + state = 0; + [SuperThrower class]; + testassert(state == 0); + [SubThrower class]; + testassert(state == 20); + } @catch (...) { + fail("+initialize called again after exception"); + } + } POP_POOL; + + succeed(__FILE__); + + return 0; +} diff --git a/test/initializeVersusWeak.m b/test/initializeVersusWeak.m new file mode 100644 index 0000000..e9c1580 --- /dev/null +++ b/test/initializeVersusWeak.m @@ -0,0 +1,187 @@ +// TEST_CONFIG MEM=arc +// TEST_CFLAGS -framework Foundation + +// Problem: If weak reference operations provoke +initialize, the runtime +// can deadlock (recursive weak lock, or lock inversion between weak lock +// and +initialize lock). +// Solution: object_setClass() and objc_storeWeak() perform +initialize +// if needed so that no weakly-referenced object can ever have an +// un-+initialized isa. + +#include +#include +#include "test.h" + +#pragma clang diagnostic ignored "-Warc-unsafe-retained-assign" + +// This is StripedMap's pointer hash +#if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR + enum { StripeCount = 8 }; +#else + enum { StripeCount = 64 }; +#endif +uintptr_t stripehash(id obj) { + uintptr_t addr = (uintptr_t)obj; + return ((addr >> 4) ^ (addr >> 9)) % StripeCount; +} + +bool sameAlignment(id o1, id o2) +{ + return stripehash(o1) == stripehash(o2); +} + +// Return a new non-tagged object that uses the same striped weak locks as `obj` +NSObject *newAlignedObject(id obj) +{ + // Use immutable arrays because their contents are stored inline, + // which prevents Guard Malloc from using the same alignment for all of them + NSArray *result = [NSArray new]; + while (!sameAlignment(obj, result)) { + result = [result arrayByAddingObject:result]; + } + return result; +} + + +__weak NSObject *weak1; +__weak NSObject *weak2; +NSObject *strong2; + +@interface A : NSObject @end +@implementation A ++(void)initialize { + weak2 = strong2; // weak store #2 + strong2 = nil; +} +@end + +void testA() +{ + // Weak store #1 provokes +initialize which performs weak store #2. + // Solution: weak store #1 runs +initialize if needed + // without holding locks. + @autoreleasepool { + A *obj = [A new]; + strong2 = newAlignedObject(obj); + [obj addObserver:obj forKeyPath:@"foo" options:0 context:0]; + weak1 = obj; // weak store #1 + [obj removeObserver:obj forKeyPath:@"foo"]; + obj = nil; + } +} + + +__weak NSObject *weak3; +__weak NSObject *weak4; +NSObject *strong4; + +@interface B : NSObject @end +@implementation B ++(void)initialize { + weak4 = strong4; // weak store #4 + strong4 = nil; +} +@end + + +void testB() +{ + // Weak load #3 provokes +initialize which performs weak store #4. + // Solution: object_setClass() runs +initialize if needed + // without holding locks. + @autoreleasepool { + B *obj = [B new]; + strong4 = newAlignedObject(obj); + weak3 = obj; + [obj addObserver:obj forKeyPath:@"foo" options:0 context:0]; + [weak3 self]; // weak load #3 + [obj removeObserver:obj forKeyPath:@"foo"]; + obj = nil; + } +} + + +__weak id weak5; + +@interface C : NSObject @end +@implementation C ++(void)initialize { + weak5 = [self new]; +} +@end + +void testC() +{ + // +initialize performs a weak store of itself. + // Make sure the retry in objc_storeWeak() doesn't spin. + @autoreleasepool { + [C self]; + } +} + + +__weak id weak6; +NSObject *strong6; +semaphore_t Dgo; +semaphore_t Ddone; + +void *Dthread(void *arg __unused) +{ + @autoreleasepool { + semaphore_wait(Dgo); + for (int i = 0; i < 1000; i++) { + id x = weak6; + testassert(x == strong6); + } + return nil; + } +} + +@interface D : NSObject @end +@implementation D ++(void)initialize { + strong6 = [self new]; + weak6 = strong6; + semaphore_signal(Dgo); + for (int i = 0; i < 1000; i++) { + id x = weak6; + testassert(x == strong6); + } +} +@end + +void testD() +{ + // +initialize performs a weak store of itself, then another thread + // tries to load that weak variable before +initialize completes. + // Deadlock occurs if the +initialize thread tries to acquire the + // sidetable lock for another operation and the second thread holds + // the sidetable lock while waiting for +initialize. + + @autoreleasepool { + semaphore_create(mach_task_self(), &Dgo, 0, 0); + semaphore_create(mach_task_self(), &Ddone, 0, 0); + pthread_t th; + pthread_create(&th, nil, Dthread, nil); + [D self]; + pthread_join(th, nil); + } +} + +int main() +{ + if (is_guardmalloc() && getenv("MALLOC_PROTECT_BEFORE")) { + testwarn("fixme malloc guard before breaks this with debug libobjc"); + } + else { + alarm(10); // replace hangs with crashes + + testA(); + testB(); + testC(); + testD(); + } + + succeed(__FILE__); +} + diff --git a/test/instanceSize.m b/test/instanceSize.m new file mode 100644 index 0000000..8034a5c --- /dev/null +++ b/test/instanceSize.m @@ -0,0 +1,59 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" +#include + + +@interface Sub1 : TestRoot { + // id isa; // 0..4 + BOOL b; // 4..5 +} +@end + +@implementation Sub1 @end + +@interface Sub2 : Sub1 { + // id isa // 0..4 0..8 + // BOOL b // 4..5 8..9 + BOOL b2; // 5..6 9..10 + id o; // 8..12 16..24 +} +@end +@implementation Sub2 @end + +@interface Sub3 : Sub1 { + // id isa; // 0..4 0..8 + // BOOL b; // 4..5 8..9 + id o; // 8..12 16..24 + BOOL b2; // 12..13 24..25 +} +@end +@implementation Sub3 @end + +int main() +{ + testassert(sizeof(id) == class_getInstanceSize([TestRoot class])); + testassert(2*sizeof(id) == class_getInstanceSize([Sub1 class])); + testassert(3*sizeof(id) == class_getInstanceSize([Sub2 class])); + testassert(4*sizeof(id) == class_getInstanceSize([Sub3 class])); + +#if !__has_feature(objc_arc) + id o; + + o = [TestRoot new]; + testassert(object_getIndexedIvars(o) == (char *)o + class_getInstanceSize(object_getClass(o))); + RELEASE_VAR(o); + o = [Sub1 new]; + testassert(object_getIndexedIvars(o) == (char *)o + class_getInstanceSize(object_getClass(o))); + RELEASE_VAR(o); + o = [Sub2 new]; + testassert(object_getIndexedIvars(o) == (char *)o + class_getInstanceSize(object_getClass(o))); + RELEASE_VAR(o); + o = [Sub3 new]; + testassert(object_getIndexedIvars(o) == (char *)o + class_getInstanceSize(object_getClass(o))); + RELEASE_VAR(o); +#endif + + succeed(__FILE__); +} diff --git a/test/isaValidation.m b/test/isaValidation.m new file mode 100644 index 0000000..bb26808 --- /dev/null +++ b/test/isaValidation.m @@ -0,0 +1,267 @@ +// TEST_CRASHES +// TEST_CONFIG MEM=mrc +/* +TEST_RUN_OUTPUT +Testing object_getMethodImplementation +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_getInstanceMethod +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_getMethodImplementation +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_respondsToSelector +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_conformsToProtocol +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_copyProtocolList +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_getProperty +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_copyPropertyList +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_addMethod +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_replaceMethod +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_addIvar +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_addProtocol +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_addProperty +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_replaceProperty +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_setIvarLayout +objc\[\d+\]: \*\*\* Can't set ivar layout for already-registered class 'TestRoot' +objc\[\d+\]: \*\*\* Can't set ivar layout for already-registered class 'TestRoot' +objc\[\d+\]: \*\*\* Can't set ivar layout for already-registered class 'NSObject' +objc\[\d+\]: \*\*\* Can't set ivar layout for already-registered class 'NSObject' +objc\[\d+\]: \*\*\* Can't set ivar layout for already-registered class 'AllocatedTestClass2' +objc\[\d+\]: \*\*\* Can't set ivar layout for already-registered class 'AllocatedTestClass2' +objc\[\d+\]: \*\*\* Can't set ivar layout for already-registered class 'TestRoot' +objc\[\d+\]: \*\*\* Can't set ivar layout for already-registered class 'DuplicateClass' +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing class_setWeakIvarLayout +objc\[\d+\]: \*\*\* Can't set weak ivar layout for already-registered class 'TestRoot' +objc\[\d+\]: \*\*\* Can't set weak ivar layout for already-registered class 'TestRoot' +objc\[\d+\]: \*\*\* Can't set weak ivar layout for already-registered class 'NSObject' +objc\[\d+\]: \*\*\* Can't set weak ivar layout for already-registered class 'NSObject' +objc\[\d+\]: \*\*\* Can't set weak ivar layout for already-registered class 'AllocatedTestClass2' +objc\[\d+\]: \*\*\* Can't set weak ivar layout for already-registered class 'AllocatedTestClass2' +objc\[\d+\]: \*\*\* Can't set weak ivar layout for already-registered class 'TestRoot' +objc\[\d+\]: \*\*\* Can't set weak ivar layout for already-registered class 'DuplicateClass' +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing objc_registerClassPair +objc\[\d+\]: objc_registerClassPair: class 'TestRoot' was not allocated with objc_allocateClassPair! +objc\[\d+\]: objc_registerClassPair: class 'NSObject' was not allocated with objc_allocateClassPair! +objc\[\d+\]: objc_registerClassPair: class 'AllocatedTestClass2' was already registered! +objc\[\d+\]: objc_registerClassPair: class 'DuplicateClass' was not allocated with objc_allocateClassPair! +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing objc_duplicateClass +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Testing objc_disposeClassPair +objc\[\d+\]: objc_disposeClassPair: class 'TestRoot' was not allocated with objc_allocateClassPair! +objc\[\d+\]: objc_disposeClassPair: class 'NSObject' was not allocated with objc_allocateClassPair! +objc\[\d+\]: objc_disposeClassPair: class 'DuplicateClass' was not allocated with objc_allocateClassPair! +Completed test on good classes. +objc\[\d+\]: Attempt to use unknown class 0x[0-9a-f]+. +objc\[\d+\]: HALTED +Completed! +END + */ + +#include "test.h" +#include "testroot.i" +#include + +@protocol P +@end + +extern char **environ; + +id dummyIMP(id self, SEL _cmd) { (void)_cmd; return self; } + +char *dupeName(Class cls) { + char *name; + asprintf(&name, "%sDuplicate", class_getName(cls)); + return name; +} + +typedef void (^TestBlock)(Class); +struct TestCase { + const char *name; + TestBlock block; +}; + +#define NAMED_TESTCASE(name, ...) { name, ^(Class cls) { __VA_ARGS__; } } +#define TESTCASE(...) NAMED_TESTCASE(#__VA_ARGS__, __VA_ARGS__) +#define TESTCASE_NOMETA(...) \ + NAMED_TESTCASE( #__VA_ARGS__, if(class_isMetaClass(cls)) return; __VA_ARGS__; ) +#define TESTCASE_OBJ(...) NAMED_TESTCASE( \ + #__VA_ARGS__, \ + if(class_isMetaClass(cls)) return; \ + id obj = [TestRoot alloc]; \ + *(Class *)obj = cls; \ + __VA_ARGS__; \ +) + +struct TestCase TestCases[] = { + TESTCASE_OBJ(object_getMethodImplementation(obj, @selector(init))), + + TESTCASE(class_getInstanceMethod(cls, @selector(init))), + TESTCASE(class_getMethodImplementation(cls, @selector(init))), + TESTCASE(class_respondsToSelector(cls, @selector(init))), + TESTCASE(class_conformsToProtocol(cls, @protocol(P))), + TESTCASE(free(class_copyProtocolList(cls, NULL))), + TESTCASE(class_getProperty(cls, "x")), + TESTCASE(free(class_copyPropertyList(cls, NULL))), + TESTCASE(class_addMethod(cls, @selector(nop), (IMP)dummyIMP, "v@:")), + TESTCASE(class_replaceMethod(cls, @selector(nop), (IMP)dummyIMP, "v@:")), + TESTCASE(class_addIvar(cls, "x", sizeof(int), sizeof(int), @encode(int))), + TESTCASE(class_addProtocol(cls, @protocol(P))), + TESTCASE(class_addProperty(cls, "x", NULL, 0)), + TESTCASE(class_replaceProperty(cls, "x", NULL, 0)), + TESTCASE(class_setIvarLayout(cls, NULL)), + TESTCASE(class_setWeakIvarLayout(cls, NULL)), + TESTCASE_NOMETA(objc_registerClassPair(cls)), + TESTCASE_NOMETA(objc_duplicateClass(cls, dupeName(cls), 0)), + TESTCASE_NOMETA(objc_disposeClassPair(cls)), +}; + +void parent(char *argv0) +{ + int testCount = sizeof(TestCases) / sizeof(*TestCases); + for (int i = 0; i < testCount; i++) { + char *testIndex; + asprintf(&testIndex, "%d", i); + char *argvSpawn[] = { + argv0, + testIndex, + NULL + }; + pid_t pid; + int result = posix_spawn(&pid, argv0, NULL, NULL, argvSpawn, environ); + if (result != 0) { + fprintf(stderr, "Could not spawn child process: (%d) %s\n", + errno, strerror(errno)); + exit(1); + } + + free(testIndex); + + result = waitpid(pid, NULL, 0); + if (result == -1) { + fprintf(stderr, "Error waiting for termination of child process: (%d) %s\n", + errno, strerror(errno)); + exit(1); + } + } + fprintf(stderr, "Completed!\n"); +} + +void child(char *argv1) +{ + long index = strtol(argv1, NULL, 10); + struct TestCase testCase = TestCases[index]; + TestBlock block = testCase.block; + + const char *name = testCase.name; + if (strncmp(name, "free(", 5) == 0) + name += 5; + const char *paren = strchr(name, '('); + long len = paren != NULL ? paren - name : strlen(name); + fprintf(stderr, "Testing %.*s\n", (int)len, name); + + // Make sure plain classes work. + block([TestRoot class]); + block(object_getClass([TestRoot class])); + + // And framework classes. + block([NSObject class]); + block(object_getClass([NSObject class])); + + // Test a constructed, unregistered class. + Class allocatedClass = objc_allocateClassPair([TestRoot class], + "AllocatedTestClass", + 0); + class_getMethodImplementation(allocatedClass, @selector(self)); + block(object_getClass(allocatedClass)); + block(allocatedClass); + + // Test a constructed, registered class. (Do this separately so + // test cases can dispose of the class if needed.) + allocatedClass = objc_allocateClassPair([TestRoot class], + "AllocatedTestClass2", + 0); + objc_registerClassPair(allocatedClass); + block(object_getClass(allocatedClass)); + block(allocatedClass); + + // Test a duplicated class. + + Class duplicatedClass = objc_duplicateClass([TestRoot class], + "DuplicateClass", + 0); + block(object_getClass(duplicatedClass)); + block(duplicatedClass); + + fprintf(stderr, "Completed test on good classes.\n"); + + // Test a fake class. + Class templateClass = objc_allocateClassPair([TestRoot class], + "TemplateClass", + 0); + void *fakeClass = malloc(malloc_size(templateClass)); + memcpy(fakeClass, templateClass, malloc_size(templateClass)); + block((Class)fakeClass); + fail("Should have died on the fake class"); +} + +int main(int argc, char **argv) +{ + // We want to run a bunch of tests, all of which end in _objc_fatal + // (at least if they succeed). Spawn one subprocess per test and + // have the parent process manage it all. The test will begin by + // running parent(), which will repeatedly re-spawn this program to + // call child() with the index of the test to run. + if (argc == 1) { + parent(argv[0]); + } else { + child(argv[1]); + } +} diff --git a/test/ismeta.m b/test/ismeta.m new file mode 100644 index 0000000..d0e580d --- /dev/null +++ b/test/ismeta.m @@ -0,0 +1,13 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" +#include + +int main() +{ + testassert(!class_isMetaClass([TestRoot class])); + testassert(class_isMetaClass(object_getClass([TestRoot class]))); + testassert(!class_isMetaClass(nil)); + succeed(__FILE__); +} diff --git a/test/ivar.m b/test/ivar.m new file mode 100644 index 0000000..871dbf0 --- /dev/null +++ b/test/ivar.m @@ -0,0 +1,133 @@ +/* +TEST_BUILD_OUTPUT +.*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +END +*/ + +#include "test.h" +#include "testroot.i" +#include +#include +#include + +@interface Super : TestRoot { + @public + char superIvar; +} +@end + +@interface Sub : Super { + @public + id subIvar; +} +@end + +@implementation Super @end +@implementation Sub @end + + +int main() +{ + /* + Runtime layout of Sub: + [0] isa + [1] superIvar + [2] subIvar + */ + + Ivar ivar; + Sub *sub = [Sub new]; + sub->subIvar = [Sub class]; + testassert(((Class *)(__bridge void *)sub)[2] == [Sub class]); + + ivar = class_getInstanceVariable([Sub class], "subIvar"); + testassert(ivar); + testassert(2*sizeof(intptr_t) == (size_t)ivar_getOffset(ivar)); + testassert(0 == strcmp(ivar_getName(ivar), "subIvar")); + testassert(0 == strcmp(ivar_getTypeEncoding(ivar), "@")); + + ivar = class_getInstanceVariable([Super class], "superIvar"); + testassert(ivar); + testassert(sizeof(intptr_t) == (size_t)ivar_getOffset(ivar)); + testassert(0 == strcmp(ivar_getName(ivar), "superIvar")); + testassert(0 == strcmp(ivar_getTypeEncoding(ivar), "c")); + testassert(ivar == class_getInstanceVariable([Sub class], "superIvar")); + + ivar = class_getInstanceVariable([Super class], "subIvar"); + testassert(!ivar); + + ivar = class_getInstanceVariable(object_getClass([Sub class]), "subIvar"); + testassert(!ivar); + + ivar = class_getInstanceVariable([Sub class], "subIvar"); + object_setIvar(sub, ivar, sub); + testassert(sub->subIvar == sub); + testassert(sub == object_getIvar(sub, ivar)); + + testassert(NULL == class_getInstanceVariable(NULL, "foo")); + testassert(NULL == class_getInstanceVariable([Sub class], NULL)); + testassert(NULL == class_getInstanceVariable(NULL, NULL)); + + testassert(NULL == object_getIvar(sub, NULL)); + testassert(NULL == object_getIvar(NULL, ivar)); + testassert(NULL == object_getIvar(NULL, NULL)); + + object_setIvar(sub, NULL, NULL); + object_setIvar(NULL, ivar, NULL); + object_setIvar(NULL, NULL, NULL); + +#if !__has_feature(objc_arc) + + uintptr_t value; + + sub->subIvar = (id)10; + value = 0; + object_getInstanceVariable(sub, "subIvar", (void **)&value); + testassert(value == 10); + + object_setInstanceVariable(sub, "subIvar", (id)11); + testassert(sub->subIvar == (id)11); + + ivar = class_getInstanceVariable([Sub class], "subIvar"); + testassert(ivar == object_getInstanceVariable(sub, "subIvar", NULL)); + + testassert(NULL == object_getInstanceVariable(sub, NULL, NULL)); + testassert(NULL == object_getInstanceVariable(NULL, "foo", NULL)); + testassert(NULL == object_getInstanceVariable(NULL, NULL, NULL)); + value = 10; + testassert(NULL == object_getInstanceVariable(sub, NULL, (void **)&value)); + testassert(value == 0); + value = 10; + testassert(NULL == object_getInstanceVariable(NULL, "foo", (void **)&value)); + testassert(value == 0); + value = 10; + testassert(NULL == object_getInstanceVariable(NULL, NULL, (void **)&value)); + testassert(value == 0); + + testassert(NULL == object_setInstanceVariable(sub, NULL, NULL)); + testassert(NULL == object_setInstanceVariable(NULL, "foo", NULL)); + testassert(NULL == object_setInstanceVariable(NULL, NULL, NULL)); +#else + // provoke the same nullability warnings as the real test + objc_getClass(nil); + objc_getClass(nil); + objc_getClass(nil); + objc_getClass(nil); + objc_getClass(nil); + objc_getClass(nil); +#endif + + succeed(__FILE__); + return 0; +} diff --git a/test/ivarSlide.h b/test/ivarSlide.h new file mode 100644 index 0000000..d4e89ca --- /dev/null +++ b/test/ivarSlide.h @@ -0,0 +1,110 @@ +@interface Super : TestRoot { + @public +#if OLD + // nothing +#else + char superIvar; +#endif +} +@end + + +@interface ShrinkingSuper : TestRoot { + @public +#if OLD + id superIvar[5]; + __weak id superIvar2[5]; +#else + // nothing +#endif +} +@end; + + +@interface MoreStrongSuper : TestRoot { + @public +#if OLD + void *superIvar; +#else + id superIvar; +#endif +} +@end; + + +@interface MoreWeakSuper : TestRoot { + @public +#if OLD + id superIvar; +#else + __weak id superIvar; +#endif +} +@end; + +@interface MoreWeak2Super : TestRoot { + @public +#if OLD + void *superIvar; +#else + __weak id superIvar; +#endif +} +@end; + +@interface LessStrongSuper : TestRoot { + @public +#if OLD + id superIvar; +#else + void *superIvar; +#endif +} +@end; + +@interface LessWeakSuper : TestRoot { + @public +#if OLD + __weak id superIvar; +#else + id superIvar; +#endif +} +@end; + +@interface LessWeak2Super : TestRoot { + @public +#if OLD + __weak id superIvar; +#else + void *superIvar; +#endif +} +@end; + +@interface NoGCChangeSuper : TestRoot { + @public + intptr_t d; + char superc1; +#if OLD + // nothing +#else + char superc2; +#endif +} +@end + +@interface RunsOf15 : TestRoot { + @public + id scan1; + intptr_t skip15[15]; + id scan15[15]; + intptr_t skip15_2[15]; + id scan15_2[15]; +#if OLD + // nothing +#else + intptr_t skip1; +#endif +} +@end diff --git a/test/ivarSlide.m b/test/ivarSlide.m new file mode 100644 index 0000000..dcc0bcc --- /dev/null +++ b/test/ivarSlide.m @@ -0,0 +1,499 @@ +/* +TEST_BUILD + $C{COMPILE} -fobjc-weak $DIR/ivarSlide1.m $DIR/ivarSlide.m -o ivarSlide.exe +END +*/ + +#include "test.h" +#include +#include +#include +#include + +// fixme should check ARC layout handling +// current test checks GC layout handling which is dead +#define FIXME_CHECK_ARC_LAYOUTS 0 + +// ARC doesn't like __strong void* or __weak void* +#define gc_weak +#define gc_strong + +#define OLD 1 +#include "ivarSlide.h" + +#define ustrcmp(a, b) strcmp((char *)a, (char *)b) + +#ifdef __cplusplus +class CXX { + public: + static uintptr_t count; + uintptr_t magic; + CXX() : magic(1) { } + ~CXX() { count += magic; } +}; +uintptr_t CXX::count; +#endif + +@interface Bitfields : Super { + uint8_t uint8_ivar; + uint8_t uint8_bitfield1 :7; + uint8_t uint8_bitfield2 :1; + + id id_ivar; + + uintptr_t uintptr_ivar; + uintptr_t /*uintptr_bitfield1*/ :31; // anonymous (rdar://5723893) + uintptr_t uintptr_bitfield2 :1; + + id id_ivar2; +} +@end + +@implementation Bitfields @end + + +@interface Sub : Super { + @public + uintptr_t subIvar; + gc_strong void* subIvar2; + gc_weak void* subIvar3; +#ifdef __cplusplus + CXX cxx; +#else + // same layout as cxx + uintptr_t cxx_magic; +#endif +} +@end + +@implementation Sub @end + + +@interface Sub2 : ShrinkingSuper { + @public + gc_weak void* subIvar; + gc_strong void* subIvar2; +} +@end + +@implementation Sub2 @end + +@interface MoreStrongSub : MoreStrongSuper { id subIvar; } @end +@interface LessStrongSub : LessStrongSuper { id subIvar; } @end +@interface MoreWeakSub : MoreWeakSuper { id subIvar; } @end +@interface MoreWeak2Sub : MoreWeak2Super { id subIvar; } @end +@interface LessWeakSub : LessWeakSuper { id subIvar; } @end +@interface LessWeak2Sub : LessWeak2Super { id subIvar; } @end + +@implementation MoreStrongSub @end +@implementation LessStrongSub @end +@implementation MoreWeakSub @end +@implementation MoreWeak2Sub @end +@implementation LessWeakSub @end +@implementation LessWeak2Sub @end + +@interface NoGCChangeSub : NoGCChangeSuper { + @public + char subc3; +} +@end +@implementation NoGCChangeSub @end + +@interface RunsOf15Sub : RunsOf15 { + @public + char sub; +} +@end +@implementation RunsOf15Sub @end + + +int main(int argc __attribute__((unused)), char **argv) +{ +#if __has_feature(objc_arc) + testwarn("fixme check ARC layouts too"); +#endif + + /* + Bitfield ivars. + rdar://5723893 anonymous bitfield ivars crash when slid + rdar://5724385 bitfield ivar alignment incorrect + + Compile-time layout of Bitfields: + [0 scan] isa + [1 skip] uint8_ivar, uint8_bitfield + [2 scan] id_ivar + [3 skip] uintptr_ivar + [4 skip] uintptr_bitfield + [5 scan] id_ivar2 + + Runtime layout of Bitfields: + [0 scan] isa + [1 skip] superIvar + [2 skip] uint8_ivar, uint8_bitfield + [3 scan] id_ivar + [4 skip] uintptr_ivar + [5 skip] uintptr_bitfield + [6 scan] id_ivar2 + */ + + [Bitfields class]; + + testassert(class_getInstanceSize([Bitfields class]) == 7*sizeof(void*)); + + if (FIXME_CHECK_ARC_LAYOUTS) { + const uint8_t *bitfieldlayout; + bitfieldlayout = class_getIvarLayout([Bitfields class]); + testassert(0 == ustrcmp(bitfieldlayout, "\x01\x21\x21")); + + bitfieldlayout = class_getWeakIvarLayout([Bitfields class]); + testassert(bitfieldlayout == NULL); + } + + /* + Compile-time layout of Sub: + [0 scan] isa + [1 skip] subIvar + [2 scan] subIvar2 + [3 weak] subIvar3 + [6 skip] cxx + + Runtime layout of Sub: + [0 scan] isa + [1 skip] superIvar + [2 skip] subIvar + [3 scan] subIvar2 + [4 weak] subIvar3 + [6 skip] cxx + + Also, superIvar is only one byte, so subIvar's alignment must + be handled correctly. + + fixme test more layouts + */ + + Ivar ivar; + static Sub * volatile sub; + sub = [Sub new]; + sub->subIvar = 10; + uintptr_t *subwords = (uintptr_t *)(__bridge void*)sub; + testassert(subwords[2] == 10); + +#ifdef __cplusplus + testassert(subwords[5] == 1); + testassert(sub->cxx.magic == 1); + sub->cxx.magic++; + testassert(subwords[5] == 2); + testassert(sub->cxx.magic == 2); +# if __has_feature(objc_arc) + sub = nil; +# else + [sub dealloc]; +# endif + testassert(CXX::count == 2); +#endif + + testassert(class_getInstanceSize([Sub class]) == 6*sizeof(void*)); + + ivar = class_getInstanceVariable([Sub class], "subIvar"); + testassert(ivar); + testassert(2*sizeof(void*) == (size_t)ivar_getOffset(ivar)); + testassert(0 == strcmp(ivar_getName(ivar), "subIvar")); + testassert(0 == strcmp(ivar_getTypeEncoding(ivar), +#if __LP64__ + "Q" +#else + "L" +#endif + )); + +#ifdef __cplusplus + ivar = class_getInstanceVariable([Sub class], "cxx"); + testassert(ivar); +#endif + + ivar = class_getInstanceVariable([Super class], "superIvar"); + testassert(ivar); + testassert(sizeof(void*) == (size_t)ivar_getOffset(ivar)); + testassert(0 == strcmp(ivar_getName(ivar), "superIvar")); + testassert(0 == strcmp(ivar_getTypeEncoding(ivar), "c")); + + ivar = class_getInstanceVariable([Super class], "subIvar"); + testassert(!ivar); + + if (FIXME_CHECK_ARC_LAYOUTS) { + const uint8_t *superlayout; + const uint8_t *sublayout; + superlayout = class_getIvarLayout([Super class]); + sublayout = class_getIvarLayout([Sub class]); + testassert(0 == ustrcmp(superlayout, "\x01\x10")); + testassert(0 == ustrcmp(sublayout, "\x01\x21\x20")); + + superlayout = class_getWeakIvarLayout([Super class]); + sublayout = class_getWeakIvarLayout([Sub class]); + testassert(superlayout == NULL); + testassert(0 == ustrcmp(sublayout, "\x41\x10")); + } + + /* + Shrinking superclass. + Subclass ivars do not compact, but the GC layout needs to + update, including the gap that the superclass no longer spans. + + Compile-time layout of Sub2: + [0 scan] isa + [1-5 scan] superIvar + [6-10 weak] superIvar2 + [11 weak] subIvar + [12 scan] subIvar2 + + Runtime layout of Sub2: + [0 scan] isa + [1-10 skip] was superIvar + [11 weak] subIvar + [12 scan] subIvar2 + */ + + Sub2 *sub2 = [Sub2 new]; + uintptr_t *sub2words = (uintptr_t *)(__bridge void*)sub2; + sub2->subIvar = (void *)10; + testassert(sub2words[11] == 10); + + testassert(class_getInstanceSize([Sub2 class]) == 13*sizeof(void*)); + + ivar = class_getInstanceVariable([Sub2 class], "subIvar"); + testassert(ivar); + testassert(11*sizeof(void*) == (size_t)ivar_getOffset(ivar)); + testassert(0 == strcmp(ivar_getName(ivar), "subIvar")); + + ivar = class_getInstanceVariable([ShrinkingSuper class], "superIvar"); + testassert(!ivar); + + if (FIXME_CHECK_ARC_LAYOUTS) { + const uint8_t *superlayout; + const uint8_t *sublayout; + superlayout = class_getIvarLayout([ShrinkingSuper class]); + sublayout = class_getIvarLayout([Sub2 class]); + // only `isa` is left; superIvar[] and superIvar2[] are gone + testassert(superlayout == NULL || 0 == ustrcmp(superlayout, "\x01")); + testassert(0 == ustrcmp(sublayout, "\x01\xb1")); + + superlayout = class_getWeakIvarLayout([ShrinkingSuper class]); + sublayout = class_getWeakIvarLayout([Sub2 class]); + testassert(superlayout == NULL); + testassert(0 == ustrcmp(sublayout, "\xb1\x10")); + } + + /* + Ivars slide but GC layouts stay the same + Here, the last word of the superclass is misaligned, but + its GC layout includes a bit for that whole word. + Additionally, all of the subclass ivars fit into that word too, + both before and after sliding. + The runtime will try to slide the GC layout and must not be + confused (rdar://6851700). Note that the second skip-word may or may + not actually be included, because it crosses the end of the object. + + + Compile-time layout of NoGCChangeSub: + [0 scan] isa + [1 skip] d + [2 skip] superc1, subc3 + + Runtime layout of NoGCChangeSub: + [0 scan] isa + [1 skip] d + [2 skip] superc1, superc2, subc3 + */ + if (FIXME_CHECK_ARC_LAYOUTS) { + Ivar ivar1 = class_getInstanceVariable([NoGCChangeSub class], "superc1"); + testassert(ivar1); + Ivar ivar2 = class_getInstanceVariable([NoGCChangeSub class], "superc2"); + testassert(ivar2); + Ivar ivar3 = class_getInstanceVariable([NoGCChangeSub class], "subc3"); + testassert(ivar3); + testassert(ivar_getOffset(ivar1) != ivar_getOffset(ivar2) && + ivar_getOffset(ivar1) != ivar_getOffset(ivar3) && + ivar_getOffset(ivar2) != ivar_getOffset(ivar3)); + } + + /* Ivar layout includes runs of 15 words. + rdar://6859875 this would generate a truncated GC layout. + */ + if (FIXME_CHECK_ARC_LAYOUTS) { + const uint8_t *layout = + class_getIvarLayout(objc_getClass("RunsOf15Sub")); + testassert(layout); + int totalSkip = 0; + int totalScan = 0; + // should find 30+ each of skip and scan + uint8_t c; + while ((c = *layout++)) { + totalSkip += c>>4; + totalScan += c&0xf; + } + testassert(totalSkip >= 30); + testassert(totalScan >= 30); + } + + + /* + Non-strong -> strong + Classes do not change size, but GC layouts must be updated. + Both new and old ABI detect this case (rdar://5774578) + + Compile-time layout of MoreStrongSub: + [0 scan] isa + [1 skip] superIvar + [2 scan] subIvar + + Runtime layout of MoreStrongSub: + [0 scan] isa + [1 scan] superIvar + [2 scan] subIvar + */ + testassert(class_getInstanceSize([MoreStrongSub class]) == 3*sizeof(void*)); + if (FIXME_CHECK_ARC_LAYOUTS) { + const uint8_t *layout; + layout = class_getIvarLayout([MoreStrongSub class]); + testassert(layout == NULL); + + layout = class_getWeakIvarLayout([MoreStrongSub class]); + testassert(layout == NULL); + } + + + /* + Strong -> weak + Classes do not change size, but GC layouts must be updated. + Old ABI intentionally does not detect this case (rdar://5774578) + + Compile-time layout of MoreWeakSub: + [0 scan] isa + [1 scan] superIvar + [2 scan] subIvar + + Runtime layout of MoreWeakSub: + [0 scan] isa + [1 weak] superIvar + [2 scan] subIvar + */ + testassert(class_getInstanceSize([MoreWeakSub class]) == 3*sizeof(void*)); + if (FIXME_CHECK_ARC_LAYOUTS) { + const uint8_t *layout; + layout = class_getIvarLayout([MoreWeakSub class]); + testassert(0 == ustrcmp(layout, "\x01\x11")); + + layout = class_getWeakIvarLayout([MoreWeakSub class]); + testassert(0 == ustrcmp(layout, "\x11\x10")); + } + + + /* + Non-strong -> weak + Classes do not change size, but GC layouts must be updated. + Old ABI intentionally does not detect this case (rdar://5774578) + + Compile-time layout of MoreWeak2Sub: + [0 scan] isa + [1 skip] superIvar + [2 scan] subIvar + + Runtime layout of MoreWeak2Sub: + [0 scan] isa + [1 weak] superIvar + [2 scan] subIvar + */ + testassert(class_getInstanceSize([MoreWeak2Sub class]) == 3*sizeof(void*)); + if (FIXME_CHECK_ARC_LAYOUTS) { + const uint8_t *layout; + layout = class_getIvarLayout([MoreWeak2Sub class]); + testassert(0 == ustrcmp(layout, "\x01\x11") || + 0 == ustrcmp(layout, "\x01\x10\x01")); + + layout = class_getWeakIvarLayout([MoreWeak2Sub class]); + testassert(0 == ustrcmp(layout, "\x11\x10")); + } + + + /* + Strong -> non-strong + Classes do not change size, but GC layouts must be updated. + Old ABI intentionally does not detect this case (rdar://5774578) + + Compile-time layout of LessStrongSub: + [0 scan] isa + [1 scan] superIvar + [2 scan] subIvar + + Runtime layout of LessStrongSub: + [0 scan] isa + [1 skip] superIvar + [2 scan] subIvar + */ + testassert(class_getInstanceSize([LessStrongSub class]) == 3*sizeof(void*)); + if (FIXME_CHECK_ARC_LAYOUTS) { + const uint8_t *layout; + layout = class_getIvarLayout([LessStrongSub class]); + testassert(0 == ustrcmp(layout, "\x01\x11")); + + layout = class_getWeakIvarLayout([LessStrongSub class]); + testassert(layout == NULL); + } + + + /* + Weak -> strong + Classes do not change size, but GC layouts must be updated. + Both new and old ABI detect this case (rdar://5774578 rdar://6924114) + + Compile-time layout of LessWeakSub: + [0 scan] isa + [1 weak] superIvar + [2 scan] subIvar + + Runtime layout of LessWeakSub: + [0 scan] isa + [1 scan] superIvar + [2 scan] subIvar + */ + testassert(class_getInstanceSize([LessWeakSub class]) == 3*sizeof(void*)); + if (FIXME_CHECK_ARC_LAYOUTS) { + const uint8_t *layout; + layout = class_getIvarLayout([LessWeakSub class]); + testassert(layout == NULL); + + layout = class_getWeakIvarLayout([LessWeakSub class]); + testassert(layout == NULL); + } + + + /* + Weak -> non-strong + Classes do not change size, but GC layouts must be updated. + Old ABI intentionally does not detect this case (rdar://5774578) + + Compile-time layout of LessWeak2Sub: + [0 scan] isa + [1 weak] superIvar + [2 scan] subIvar + + Runtime layout of LessWeak2Sub: + [0 scan] isa + [1 skip] superIvar + [2 scan] subIvar + */ + testassert(class_getInstanceSize([LessWeak2Sub class]) == 3*sizeof(void*)); + if (FIXME_CHECK_ARC_LAYOUTS) { + const uint8_t *layout; + layout = class_getIvarLayout([LessWeak2Sub class]); + testassert(0 == ustrcmp(layout, "\x01\x11") || + 0 == ustrcmp(layout, "\x01\x10\x01")); + + layout = class_getWeakIvarLayout([LessWeak2Sub class]); + testassert(layout == NULL); + } + + + succeed(basename(argv[0])); + return 0; +} diff --git a/test/ivarSlide1.m b/test/ivarSlide1.m new file mode 100644 index 0000000..47b2e63 --- /dev/null +++ b/test/ivarSlide1.m @@ -0,0 +1,21 @@ +#include "test.h" +#include +#include + +#define OLD 0 +#include "ivarSlide.h" + +#include "testroot.i" + +@implementation Super @end + +@implementation ShrinkingSuper @end + +@implementation MoreStrongSuper @end +@implementation LessStrongSuper @end +@implementation MoreWeakSuper @end +@implementation MoreWeak2Super @end +@implementation LessWeakSuper @end +@implementation LessWeak2Super @end +@implementation NoGCChangeSuper @end +@implementation RunsOf15 @end diff --git a/test/literals.m b/test/literals.m new file mode 100644 index 0000000..e43673a --- /dev/null +++ b/test/literals.m @@ -0,0 +1,55 @@ +// TEST_CFLAGS -framework Foundation + +#import +#import +#import +#import +#import +#include "test.h" + +int main() { + PUSH_POOL { + +#if __has_feature(objc_bool) // placeholder until we get a more precise macro. + NSArray *array = @[ @1, @2, @YES, @NO, @"Hello", @"World" ]; + testassert([array count] == 6); + NSDictionary *dict = @{ @"Name" : @"John Q. Public", @"Age" : @42 }; + testassert([dict count] == 2); + NSDictionary *numbers = @{ @"π" : @M_PI, @"e" : @M_E }; + testassert([[numbers objectForKey:@"π"] doubleValue] == M_PI); + testassert([[numbers objectForKey:@"e"] doubleValue] == M_E); + + BOOL yesBool = YES; + BOOL noBool = NO; + array = @[ + @(true), + @(YES), + [NSNumber numberWithBool:YES], + @YES, + @(yesBool), + @((BOOL)YES), + + @(false), + @(NO), + [NSNumber numberWithBool:NO], + @NO, + @(noBool), + @((BOOL)NO), + ]; + NSData * jsonData = [NSJSONSerialization dataWithJSONObject:array options:0 error:nil]; + NSString * string = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; +#if __cplusplus + testassert([string isEqualToString:@"[true,true,true,true,true,true,false,false,false,false,false,false]"]); +#else + // C99 @(true) and @(false) evaluate to @(1) and @(0). + testassert([string isEqualToString:@"[1,true,true,true,true,true,0,false,false,false,false,false]"]); +#endif + +#endif + + } POP_POOL; + + succeed(__FILE__); + + return 0; +} diff --git a/test/load-image-notification-dylib.m b/test/load-image-notification-dylib.m new file mode 100644 index 0000000..1495966 --- /dev/null +++ b/test/load-image-notification-dylib.m @@ -0,0 +1,7 @@ +#import "test.h" + +#import + +@interface CLASSNAME: NSObject @end +@implementation CLASSNAME @end + diff --git a/test/load-image-notification.m b/test/load-image-notification.m new file mode 100644 index 0000000..3b0fa72 --- /dev/null +++ b/test/load-image-notification.m @@ -0,0 +1,76 @@ +/* +TEST_BUILD + $C{COMPILE} -DCLASSNAME=Class1 $DIR/load-image-notification-dylib.m -o load-image-notification1.dylib -dynamiclib + $C{COMPILE} -DCLASSNAME=Class2 $DIR/load-image-notification-dylib.m -o load-image-notification2.dylib -dynamiclib + $C{COMPILE} -DCLASSNAME=Class3 $DIR/load-image-notification-dylib.m -o load-image-notification3.dylib -dynamiclib + $C{COMPILE} -DCLASSNAME=Class4 $DIR/load-image-notification-dylib.m -o load-image-notification4.dylib -dynamiclib + $C{COMPILE} -DCLASSNAME=Class5 $DIR/load-image-notification-dylib.m -o load-image-notification5.dylib -dynamiclib + $C{COMPILE} $DIR/load-image-notification.m -o load-image-notification.exe +END +*/ + +#include "test.h" + +#include + +#define ADD_IMAGE_CALLBACK(n) \ +int called ## n = 0; \ +static void add_image ## n(const struct mach_header * mh __unused) { \ + called ## n++; \ +} + +ADD_IMAGE_CALLBACK(1) +ADD_IMAGE_CALLBACK(2) +ADD_IMAGE_CALLBACK(3) +ADD_IMAGE_CALLBACK(4) +ADD_IMAGE_CALLBACK(5) + +int main() +{ + objc_addLoadImageFunc(add_image1); + testassert(called1 > 0); + int oldcalled = called1; + void *handle = dlopen("load-image-notification1.dylib", RTLD_LAZY); + testassert(handle); + testassert(called1 > oldcalled); + + objc_addLoadImageFunc(add_image2); + testassert(called2 == called1); + oldcalled = called1; + handle = dlopen("load-image-notification2.dylib", RTLD_LAZY); + testassert(handle); + testassert(called1 > oldcalled); + testassert(called2 == called1); + + objc_addLoadImageFunc(add_image3); + testassert(called3 == called1); + oldcalled = called1; + handle = dlopen("load-image-notification3.dylib", RTLD_LAZY); + testassert(handle); + testassert(called1 > oldcalled); + testassert(called2 == called1); + testassert(called3 == called1); + + objc_addLoadImageFunc(add_image4); + testassert(called4 == called1); + oldcalled = called1; + handle = dlopen("load-image-notification4.dylib", RTLD_LAZY); + testassert(handle); + testassert(called1 > oldcalled); + testassert(called2 == called1); + testassert(called3 == called1); + testassert(called4 == called1); + + objc_addLoadImageFunc(add_image5); + testassert(called5 == called1); + oldcalled = called1; + handle = dlopen("load-image-notification5.dylib", RTLD_LAZY); + testassert(handle); + testassert(called1 > oldcalled); + testassert(called2 == called1); + testassert(called3 == called1); + testassert(called4 == called1); + testassert(called5 == called1); + + succeed(__FILE__); +} diff --git a/test/load-noobjc.m b/test/load-noobjc.m new file mode 100644 index 0000000..4dd9f86 --- /dev/null +++ b/test/load-noobjc.m @@ -0,0 +1,38 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/load-noobjc.m -o load-noobjc.exe + $C{COMPILE} $DIR/load-noobjc2.m -o libload-noobjc2.dylib -bundle -bundle_loader load-noobjc.exe + $C{COMPILE} $DIR/load-noobjc3.m -o libload-noobjc3.dylib -bundle -bundle_loader load-noobjc.exe +END +*/ + +#include "test.h" +#include + +int state = 0; +semaphore_t go; + +void *thread(void *arg __unused) +{ + dlopen("libload-noobjc2.dylib", RTLD_LAZY); + fail("dlopen should not have returned"); +} + +int main() +{ + semaphore_create(mach_task_self(), &go, SYNC_POLICY_FIFO, 0); + + pthread_t th; + pthread_create(&th, nil, &thread, nil); + + // Wait for thread to stop in libload-noobjc2's +load method. + semaphore_wait(go); + + // run nooobjc3's constructor function. + // There's no objc code here so it shouldn't require the +load lock. + void *dlh = dlopen("libload-noobjc3.dylib", RTLD_LAZY); + testassert(dlh); + testassert(state == 1); + + succeed(__FILE__); +} diff --git a/test/load-noobjc2.m b/test/load-noobjc2.m new file mode 100644 index 0000000..bcca510 --- /dev/null +++ b/test/load-noobjc2.m @@ -0,0 +1,13 @@ +#include "test.h" + +extern semaphore_t go; + +OBJC_ROOT_CLASS +@interface noobjc @end +@implementation noobjc ++(void)load +{ + semaphore_signal(go); + while (1) sleep(1); +} +@end diff --git a/test/load-noobjc3.m b/test/load-noobjc3.m new file mode 100644 index 0000000..98e4071 --- /dev/null +++ b/test/load-noobjc3.m @@ -0,0 +1,9 @@ +#include "test.h" + +extern int state; + +__attribute__((constructor)) +static void ctor(void) +{ + state = 1; +} diff --git a/test/load-order.m b/test/load-order.m new file mode 100644 index 0000000..a0eb8f4 --- /dev/null +++ b/test/load-order.m @@ -0,0 +1,18 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/load-order3.m -o load-order3.dylib -dynamiclib + $C{COMPILE} $DIR/load-order2.m -o load-order2.dylib -x none load-order3.dylib -dynamiclib + $C{COMPILE} $DIR/load-order1.m -o load-order1.dylib -x none load-order3.dylib load-order2.dylib -dynamiclib + $C{COMPILE} $DIR/load-order.m -o load-order.exe -x none load-order3.dylib load-order2.dylib load-order1.dylib +END +*/ + +#include "test.h" + +extern int state1, state2, state3; + +int main() +{ + testassert(state1 == 1 && state2 == 2 && state3 == 3); + succeed(__FILE__); +} diff --git a/test/load-order1.m b/test/load-order1.m new file mode 100644 index 0000000..cdfa075 --- /dev/null +++ b/test/load-order1.m @@ -0,0 +1,15 @@ +#include "test.h" + +extern int state2, state3; + +int state1 = 0; + +OBJC_ROOT_CLASS +@interface One @end +@implementation One ++(void)load +{ + testassert(state2 == 2 && state3 == 3); + state1 = 1; +} +@end diff --git a/test/load-order2.m b/test/load-order2.m new file mode 100644 index 0000000..7537754 --- /dev/null +++ b/test/load-order2.m @@ -0,0 +1,15 @@ +#include "test.h" + +extern int state3; + +int state2 = 0; + +OBJC_ROOT_CLASS +@interface Two @end +@implementation Two ++(void)load +{ + testassert(state3 == 3); + state2 = 2; +} +@end diff --git a/test/load-order3.m b/test/load-order3.m new file mode 100644 index 0000000..7c34d5a --- /dev/null +++ b/test/load-order3.m @@ -0,0 +1,12 @@ +#include "test.h" + +int state3 = 0; + +OBJC_ROOT_CLASS +@interface Three @end +@implementation Three ++(void)load +{ + state3 = 3; +} +@end diff --git a/test/load-parallel.m b/test/load-parallel.m new file mode 100644 index 0000000..5b5bd94 --- /dev/null +++ b/test/load-parallel.m @@ -0,0 +1,63 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/load-parallel00.m -o load-parallel00.dylib -dynamiclib + $C{COMPILE} $DIR/load-parallel.m -x none load-parallel00.dylib -o load-parallel.exe -DCOUNT=10 + + $C{COMPILE} $DIR/load-parallel0.m -x none load-parallel00.dylib -o load-parallel0.dylib -dynamiclib -DN=0 + $C{COMPILE} $DIR/load-parallel0.m -x none load-parallel00.dylib -o load-parallel1.dylib -dynamiclib -DN=1 + $C{COMPILE} $DIR/load-parallel0.m -x none load-parallel00.dylib -o load-parallel2.dylib -dynamiclib -DN=2 + $C{COMPILE} $DIR/load-parallel0.m -x none load-parallel00.dylib -o load-parallel3.dylib -dynamiclib -DN=3 + $C{COMPILE} $DIR/load-parallel0.m -x none load-parallel00.dylib -o load-parallel4.dylib -dynamiclib -DN=4 + $C{COMPILE} $DIR/load-parallel0.m -x none load-parallel00.dylib -o load-parallel5.dylib -dynamiclib -DN=5 + $C{COMPILE} $DIR/load-parallel0.m -x none load-parallel00.dylib -o load-parallel6.dylib -dynamiclib -DN=6 + $C{COMPILE} $DIR/load-parallel0.m -x none load-parallel00.dylib -o load-parallel7.dylib -dynamiclib -DN=7 + $C{COMPILE} $DIR/load-parallel0.m -x none load-parallel00.dylib -o load-parallel8.dylib -dynamiclib -DN=8 + $C{COMPILE} $DIR/load-parallel0.m -x none load-parallel00.dylib -o load-parallel9.dylib -dynamiclib -DN=9 +END +*/ + +#include "test.h" + +#include +#include + +#ifndef COUNT +#error -DCOUNT=c missing +#endif + +extern atomic_int state; + +void *thread(void *arg) +{ + uintptr_t num = (uintptr_t)arg; + char *buf; + + asprintf(&buf, "load-parallel%lu.dylib", (unsigned long)num); + testprintf("%s\n", buf); + void *dlh = dlopen(buf, RTLD_LAZY); + if (!dlh) { + fail("dlopen failed: %s", dlerror()); + } + free(buf); + + return NULL; +} + +int main() +{ + pthread_t t[COUNT]; + uintptr_t i; + + for (i = 0; i < COUNT; i++) { + pthread_create(&t[i], NULL, thread, (void *)i); + } + + for (i = 0; i < COUNT; i++) { + pthread_join(t[i], NULL); + } + + testprintf("loaded %d/%d\n", (int)state, COUNT*26); + testassert(state == COUNT*26); + + succeed(__FILE__); +} diff --git a/test/load-parallel0.m b/test/load-parallel0.m new file mode 100644 index 0000000..2e135e9 --- /dev/null +++ b/test/load-parallel0.m @@ -0,0 +1,48 @@ +#ifndef N +#error -DN=n missing +#endif + +#import +#include +#include +#include +#include "test.h" +extern atomic_int state; + +#define CLASS0(n,nn) \ + OBJC_ROOT_CLASS \ + @interface C_##n##_##nn @end \ + @implementation C_##n##_##nn \ + +(void)load { \ + atomic_fetch_add_explicit(&state, 1, memory_order_relaxed); \ + usleep(10); } \ + @end + +#define CLASS(n,nn) CLASS0(n,nn) + +CLASS(a,N) +CLASS(b,N) +CLASS(c,N) +CLASS(d,N) +CLASS(e,N) +CLASS(f,N) +CLASS(g,N) +CLASS(h,N) +CLASS(i,N) +CLASS(j,N) +CLASS(k,N) +CLASS(l,N) +CLASS(m,N) +CLASS(n,N) +CLASS(o,N) +CLASS(p,N) +CLASS(q,N) +CLASS(r,N) +CLASS(s,N) +CLASS(t,N) +CLASS(u,N) +CLASS(v,N) +CLASS(w,N) +CLASS(x,N) +CLASS(y,N) +CLASS(z,N) diff --git a/test/load-parallel00.m b/test/load-parallel00.m new file mode 100644 index 0000000..9df43b4 --- /dev/null +++ b/test/load-parallel00.m @@ -0,0 +1,2 @@ +#include "test.h" +atomic_int state; diff --git a/test/load-reentrant.m b/test/load-reentrant.m new file mode 100644 index 0000000..1dac2f2 --- /dev/null +++ b/test/load-reentrant.m @@ -0,0 +1,36 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/load-reentrant.m -o load-reentrant.exe + $C{COMPILE} $DIR/load-reentrant2.m -o libload-reentrant2.dylib -bundle -bundle_loader load-reentrant.exe +END +*/ + +#include "test.h" +#include + +int state1 = 0; +int *state2_p; + +OBJC_ROOT_CLASS +@interface One @end +@implementation One ++(void)load +{ + state1 = 111; + + // Re-entrant +load doesn't get to complete until we do + void *dlh = dlopen("libload-reentrant2.dylib", RTLD_LAZY); + testassert(dlh); + state2_p = (int *)dlsym(dlh, "state2"); + testassert(state2_p); + testassert(*state2_p == 0); + + state1 = 1; +} +@end + +int main() +{ + testassert(state1 == 1 && state2_p && *state2_p == 2); + succeed(__FILE__); +} diff --git a/test/load-reentrant2.m b/test/load-reentrant2.m new file mode 100644 index 0000000..0cc6a40 --- /dev/null +++ b/test/load-reentrant2.m @@ -0,0 +1,23 @@ +#include "test.h" + +int state2 = 0; +extern int state1; + +static void ctor(void) __attribute__((constructor)); +static void ctor(void) +{ + // should be called during One's dlopen(), before Two's +load + testassert(state1 == 111); + testassert(state2 == 0); +} + +OBJC_ROOT_CLASS +@interface Two @end +@implementation Two ++(void) load +{ + // Does not run until One's +load completes + testassert(state1 == 1); + state2 = 2; +} +@end diff --git a/test/load.m b/test/load.m new file mode 100644 index 0000000..9e72870 --- /dev/null +++ b/test/load.m @@ -0,0 +1,99 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" + +int state = 0; +int catstate = 0; +int deallocstate = 0; + +@interface Deallocator : TestRoot @end +@implementation Deallocator +-(void)dealloc { + deallocstate = 1; + SUPER_DEALLOC(); +} +@end + + +@interface Super : TestRoot @end +@implementation Super ++(void)initialize { + if (self == [Super class]) { + testprintf("in +[Super initialize]\n"); + testassert(state == 2); + state = 3; + } else { + testprintf("in +[Super initialize] on behalf of Sub\n"); + testassert(state == 3); + state = 4; + } +} +-(void)load { fail("-[Super load] called!"); } ++(void)load { + testprintf("in +[Super load]\n"); + testassert(state == 0); + state = 1; +} +@end + +@interface Sub : Super { } @end +@implementation Sub ++(void)load { + testprintf("in +[Sub load]\n"); + testassert(state == 1); + state = 2; +} +-(void)load { fail("-[Sub load] called!"); } +@end + +@interface SubNoLoad : Super { } @end +@implementation SubNoLoad @end + +@interface Super (Category) @end +@implementation Super (Category) +-(void)load { fail("-[Super(Category) load called!"); } ++(void)load { + testprintf("in +[Super(Category) load]\n"); + testassert(state >= 1); + catstate++; +} +@end + + +@interface Sub (Category) @end +@implementation Sub (Category) +-(void)load { fail("-[Sub(Category) load called!"); } ++(void)load { + testprintf("in +[Sub(Category) load]\n"); + testassert(state >= 2); + catstate++; + + // test autorelease pool + __autoreleasing id x; + x = AUTORELEASE([Deallocator new]); +} +@end + + +@interface SubNoLoad (Category) @end +@implementation SubNoLoad (Category) +-(void)load { fail("-[SubNoLoad(Category) load called!"); } ++(void)load { + testprintf("in +[SubNoLoad(Category) load]\n"); + testassert(state >= 1); + catstate++; +} +@end + +int main() +{ + testassert(state == 2); + testassert(catstate == 3); + testassert(deallocstate == 1); + [Sub class]; + testassert(state == 4); + testassert(catstate == 3); + + succeed(__FILE__); +} diff --git a/test/methodArgs.m b/test/methodArgs.m new file mode 100644 index 0000000..91af094 --- /dev/null +++ b/test/methodArgs.m @@ -0,0 +1,180 @@ +/* +TEST_CFLAGS -Wno-deprecated-declarations +TEST_BUILD_OUTPUT +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*methodArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +END +*/ + +#include "test.h" +#include "testroot.i" +#include +#include + +@interface Super : TestRoot @end +@implementation Super ++(id)method:(int)__unused arg :(void(^)(void)) __unused arg2 { + return 0; +} +@end + + +int main() +{ + char buf[128]; + char *arg; + struct objc_method_description *desc; + Method m = class_getClassMethod([Super class], sel_registerName("method::")); + testassert(m); + + testassert(method_getNumberOfArguments(m) == 4); + + arg = method_copyArgumentType(m, 0); + testassert(arg); + testassert(0 == strcmp(arg, "@")); + memset(buf, 1, 128); + method_getArgumentType(m, 0, buf, 1+strlen(arg)); + testassert(0 == strcmp(arg, buf)); + testassert(buf[1+strlen(arg)] == 1); + memset(buf, 1, 128); + method_getArgumentType(m, 0, buf, 2); + testassert(0 == strncmp(arg, buf, 2)); + testassert(buf[2] == 1); + free(arg); + + arg = method_copyArgumentType(m, 1); + testassert(arg); + testassert(0 == strcmp(arg, ":")); + memset(buf, 1, 128); + method_getArgumentType(m, 1, buf, 1+strlen(arg)); + testassert(0 == strcmp(arg, buf)); + testassert(buf[1+strlen(arg)] == 1); + memset(buf, 1, 128); + method_getArgumentType(m, 1, buf, 2); + testassert(0 == strncmp(arg, buf, 2)); + testassert(buf[2] == 1); + free(arg); + + arg = method_copyArgumentType(m, 2); + testassert(arg); + testassert(0 == strcmp(arg, "i")); + memset(buf, 1, 128); + method_getArgumentType(m, 2, buf, 1+strlen(arg)); + testassert(0 == strcmp(arg, buf)); + testassert(buf[1+strlen(arg)] == 1); + memset(buf, 1, 128); + method_getArgumentType(m, 2, buf, 2); + testassert(0 == strncmp(arg, buf, 2)); + testassert(buf[2] == 1); + free(arg); + + arg = method_copyArgumentType(m, 3); + testassert(arg); + testassert(0 == strcmp(arg, "@?")); + memset(buf, 1, 128); + method_getArgumentType(m, 3, buf, 1+strlen(arg)); + testassert(0 == strcmp(arg, buf)); + testassert(buf[1+strlen(arg)] == 1); + memset(buf, 1, 128); + method_getArgumentType(m, 3, buf, 2); + testassert(0 == strncmp(arg, buf, 2)); + testassert(buf[2] == 1); + memset(buf, 1, 128); + method_getArgumentType(m, 3, buf, 3); + testassert(0 == strncmp(arg, buf, 3)); + testassert(buf[3] == 1); + free(arg); + + arg = method_copyArgumentType(m, 4); + testassert(!arg); + + arg = method_copyArgumentType(m, -1); + testassert(!arg); + + memset(buf, 1, 128); + method_getArgumentType(m, 4, buf, 127); + testassert(buf[0] == 0); + testassert(buf[1] == 0); + testassert(buf[127] == 1); + + memset(buf, 1, 128); + method_getArgumentType(m, -1, buf, 127); + testassert(buf[0] == 0); + testassert(buf[1] == 0); + testassert(buf[127] == 1); + + arg = method_copyReturnType(m); + testassert(arg); + testassert(0 == strcmp(arg, "@")); + memset(buf, 1, 128); + method_getReturnType(m, buf, 1+strlen(arg)); + testassert(0 == strcmp(arg, buf)); + testassert(buf[1+strlen(arg)] == 1); + memset(buf, 1, 128); + method_getReturnType(m, buf, 2); + testassert(0 == strncmp(arg, buf, 2)); + testassert(buf[2] == 1); + free(arg); + + desc = method_getDescription(m); + testassert(desc); + testassert(desc->name == sel_registerName("method::")); +#if __LP64__ + testassert(0 == strcmp(desc->types, "@28@0:8i16@?20")); +#else + testassert(0 == strcmp(desc->types, "@16@0:4i8@?12")); +#endif + + testassert(0 == method_getNumberOfArguments(NULL)); + testassert(NULL == method_copyArgumentType(NULL, 10)); + testassert(NULL == method_copyReturnType(NULL)); + testassert(NULL == method_getDescription(NULL)); + + memset(buf, 1, 128); + method_getArgumentType(NULL, 1, buf, 127); + testassert(buf[0] == 0); + testassert(buf[1] == 0); + testassert(buf[127] == 1); + + memset(buf, 1, 128); + method_getArgumentType(NULL, 1, buf, 0); + testassert(buf[0] == 1); + testassert(buf[1] == 1); + + method_getArgumentType(m, 1, NULL, 128); + method_getArgumentType(m, 1, NULL, 0); + method_getArgumentType(NULL, 1, NULL, 128); + method_getArgumentType(NULL, 1, NULL, 0); + + memset(buf, 1, 128); + method_getReturnType(NULL, buf, 127); + testassert(buf[0] == 0); + testassert(buf[1] == 0); + testassert(buf[127] == 1); + + memset(buf, 1, 128); + method_getReturnType(NULL, buf, 0); + testassert(buf[0] == 1); + testassert(buf[1] == 1); + + method_getReturnType(m, NULL, 128); + method_getReturnType(m, NULL, 0); + method_getReturnType(NULL, NULL, 128); + method_getReturnType(NULL, NULL, 0); + + succeed(__FILE__); +} diff --git a/test/methodCacheLeaks.m b/test/methodCacheLeaks.m new file mode 100644 index 0000000..968bf5a --- /dev/null +++ b/test/methodCacheLeaks.m @@ -0,0 +1,65 @@ +// TEST_CONFIG MEM=mrc LANGUAGE=objective-c +/* +TEST_RUN_OUTPUT +[\S\s]*0 leaks for 0 total leaked bytes[\S\s]* +END +*/ + +#include "test.h" +#include "testroot.i" + +#include +#include + +void noopIMP(id self __unused, SEL _cmd __unused) {} + +id test(int n, int methodCount) { + char *name; + asprintf(&name, "TestClass%d", n); + Class c = objc_allocateClassPair([TestRoot class], name, 0); + free(name); + + SEL *sels = malloc(methodCount * sizeof(*sels)); + for(int i = 0; i < methodCount; i++) { + asprintf(&name, "selector%d", i); + sels[i] = sel_getUid(name); + free(name); + } + + for(int i = 0; i < methodCount; i++) { + class_addMethod(c, sels[i], (IMP)noopIMP, "v@:"); + } + + objc_registerClassPair(c); + + id obj = [[c alloc] init]; + for (int i = 0; i < methodCount; i++) { + ((void (*)(id, SEL))objc_msgSend)(obj, sels[i]); + } + free(sels); + return obj; +} + +int main() +{ + int classCount = 16; + id *objs = malloc(classCount * sizeof(*objs)); + for (int i = 0; i < classCount; i++) { + objs[i] = test(i, 1 << i); + } + + char *pidstr; + int result = asprintf(&pidstr, "%u", getpid()); + testassert(result); + + extern char **environ; + char *argv[] = { "/usr/bin/leaks", pidstr, NULL }; + pid_t pid; + result = posix_spawn(&pid, "/usr/bin/leaks", NULL, NULL, argv, environ); + if (result) { + perror("posix_spawn"); + exit(1); + } + wait4(pid, NULL, 0, NULL); + printf("objs=%p\n", objs); +} diff --git a/test/methodListSize.m b/test/methodListSize.m new file mode 100644 index 0000000..3821844 --- /dev/null +++ b/test/methodListSize.m @@ -0,0 +1,56 @@ +// TEST_CONFIG +// rdar://8052003 rdar://8077031 + +#include "test.h" + +#include +#include + +// add SELCOUNT methods to each of CLASSCOUNT classes +#define CLASSCOUNT 100 +#define SELCOUNT 200 + +int main() +{ + int i, j; + malloc_statistics_t start, end; + + Class root; + root = objc_allocateClassPair(NULL, "Root", 0); + objc_registerClassPair(root); + + Class classes[CLASSCOUNT]; + for (i = 0; i < CLASSCOUNT; i++) { + char *classname; + asprintf(&classname, "GrP_class_%d", i); + classes[i] = objc_allocateClassPair(root, classname, 0); + objc_registerClassPair(classes[i]); + free(classname); + } + + SEL selectors[SELCOUNT]; + for (i = 0; i < SELCOUNT; i++) { + char *selname; + asprintf(&selname, "GrP_sel_%d", i); + selectors[i] = sel_registerName(selname); + free(selname); + } + + malloc_zone_statistics(NULL, &start); + + for (i = 0; i < CLASSCOUNT; i++) { + for (j = 0; j < SELCOUNT; j++) { + class_addMethod(classes[i], selectors[j], (IMP)main, ""); + } + } + + malloc_zone_statistics(NULL, &end); + + // expected: 3-word method struct plus two other words + ssize_t expected = (sizeof(void*) * (3+2)) * SELCOUNT * CLASSCOUNT; + ssize_t actual = end.size_in_use - start.size_in_use; + testassert(actual < expected * 3); // allow generous fudge factor + + succeed(__FILE__); +} + diff --git a/test/msgSend-performance.m b/test/msgSend-performance.m new file mode 100644 index 0000000..30e1716 --- /dev/null +++ b/test/msgSend-performance.m @@ -0,0 +1,176 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" +#include + +#if defined(__arm__) +// rdar://8331406 +# define ALIGN_() +#else +# define ALIGN_() asm(".align 4"); +#endif + + +@interface Super : TestRoot @end + +@implementation Super + +-(void)voidret_nop +{ + return; +} + +-(void)voidret_nop2 +{ + return; +} + +-(id)idret_nop +{ + return nil; +} + +-(long long)llret_nop +{ + return 0; +} + +-(struct stret)stret_nop +{ + return STRET_RESULT; +} + +-(double)fpret_nop +{ + return 0; +} + +-(long double)lfpret_nop +{ + return 0; +} + +-(vector_ulong2)vecret_nop +{ + return (vector_ulong2){0x1234567890abcdefULL, 0xfedcba0987654321ULL}; +} + +@end + + +@interface Sub : Super @end + +@implementation Sub @end + + +int main() +{ + + // cached message performance + // catches failure to cache or (abi=2) failure to fixup (#5584187) + // fixme unless they all fail + + uint64_t startTime; + uint64_t totalTime; + uint64_t targetTime; + + Sub *sub = [Sub new]; + + // fill cache first + + [sub voidret_nop]; + [sub voidret_nop2]; + [sub llret_nop]; + [sub stret_nop]; + [sub fpret_nop]; + [sub lfpret_nop]; + [sub vecret_nop]; + [sub voidret_nop]; + [sub voidret_nop2]; + [sub llret_nop]; + [sub stret_nop]; + [sub fpret_nop]; + [sub lfpret_nop]; + [sub vecret_nop]; + [sub voidret_nop]; + [sub voidret_nop2]; + [sub llret_nop]; + [sub stret_nop]; + [sub fpret_nop]; + [sub lfpret_nop]; + [sub vecret_nop]; + + // Some of these times have high variance on some compilers. + // The errors we're trying to catch should be catastrophically slow, + // so the margins here are generous to avoid false failures. + + // Use voidret because id return is too slow for perf test with ARC. + + // Pick smallest of voidret_nop and voidret_nop2 time + // in the hopes that one of them didn't collide in the method cache. + + // ALIGN_ matches loop alignment to make -O0 work + +#define COUNT 1000000 + + startTime = mach_absolute_time(); + ALIGN_(); + for (int i = 0; i < COUNT; i++) { + [sub voidret_nop]; + } + totalTime = mach_absolute_time() - startTime; + testprintf("time: voidret %llu\n", totalTime); + targetTime = totalTime; + + startTime = mach_absolute_time(); + ALIGN_(); + for (int i = 0; i < COUNT; i++) { + [sub voidret_nop2]; + } + totalTime = mach_absolute_time() - startTime; + testprintf("time: voidret2 %llu\n", totalTime); + if (totalTime < targetTime) targetTime = totalTime; + + startTime = mach_absolute_time(); + ALIGN_(); + for (int i = 0; i < COUNT; i++) { + [sub llret_nop]; + } + totalTime = mach_absolute_time() - startTime; + timecheck("llret ", totalTime, targetTime * 0.65, targetTime * 2.0); + + startTime = mach_absolute_time(); + ALIGN_(); + for (int i = 0; i < COUNT; i++) { + [sub stret_nop]; + } + totalTime = mach_absolute_time() - startTime; + timecheck("stret ", totalTime, targetTime * 0.65, targetTime * 5.0); + + startTime = mach_absolute_time(); + ALIGN_(); + for (int i = 0; i < COUNT; i++) { + [sub fpret_nop]; + } + totalTime = mach_absolute_time() - startTime; + timecheck("fpret ", totalTime, targetTime * 0.65, targetTime * 4.0); + + startTime = mach_absolute_time(); + ALIGN_(); + for (int i = 0; i < COUNT; i++) { + [sub lfpret_nop]; + } + totalTime = mach_absolute_time() - startTime; + timecheck("lfpret", totalTime, targetTime * 0.65, targetTime * 4.0); + + startTime = mach_absolute_time(); + ALIGN_(); + for (int i = 0; i < COUNT; i++) { + [sub vecret_nop]; + } + totalTime = mach_absolute_time() - startTime; + timecheck("vecret", totalTime, targetTime * 0.65, targetTime * 4.0); + + succeed(__FILE__); +} diff --git a/test/msgSend.m b/test/msgSend.m new file mode 100644 index 0000000..37ba2d8 --- /dev/null +++ b/test/msgSend.m @@ -0,0 +1,2680 @@ +/* +asm-placeholder.exe is used below to disassemble objc_msgSend + +TEST_BUILD + $C{COMPILE} -x assembler $DIR/asm-placeholder.s -o asm-placeholder.exe + $C{COMPILE} $DIR/msgSend.m -o msgSend.exe -Wno-unused-parameter -Wundeclared-selector -D__DARWIN_OPAQUE_ARM_THREAD_STATE64=1 +END +*/ + +#include "test.h" +#include "testroot.i" + +#include +#include +#include +#include +#include +#include +#include +#include + +// rdar://21694990 simd.h should have a vector_equal(a, b) function +static bool vector_equal(vector_ulong2 lhs, vector_ulong2 rhs) { + return vector_all(lhs == rhs); +} + +#if __arm64__ + // no stret dispatchers +# define SUPPORT_STRET 0 +# define objc_msgSend_stret objc_msgSend +# define objc_msgSendSuper2_stret objc_msgSendSuper2 +# define objc_msgSend_stret_debug objc_msgSend_debug +# define objc_msgSendSuper2_stret_debug objc_msgSendSuper2_debug +# define objc_msgLookup_stret objc_msgLookup +# define objc_msgLookupSuper2_stret objc_msgLookupSuper2 +# define method_invoke_stret method_invoke +#else +# define SUPPORT_STRET 1 +#endif + + +#if defined(__arm__) +// rdar://8331406 +# define ALIGN_() +#else +# define ALIGN_() asm(".align 4"); +#endif + +@interface Super : TestRoot @end + +@interface Sub : Super @end + +static int state = 0; + +static id SELF; + +// for typeof() shorthand only +id (*idmsg0)(id, SEL) __attribute__((unused)); +long long (*llmsg0)(id, SEL) __attribute__((unused)); +// struct stret (*stretmsg0)(id, SEL) __attribute__((unused)); +double (*fpmsg0)(id, SEL) __attribute__((unused)); +long double (*lfpmsg0)(id, SEL) __attribute__((unused)); +vector_ulong2 (*vecmsg0)(id, SEL) __attribute__((unused)); + +#define VEC1 ((vector_ulong2){1, 1}) +#define VEC2 ((vector_ulong2){2, 2}) +#define VEC3 ((vector_ulong2){3, 3}) +#define VEC4 ((vector_ulong2){4, 4}) +#define VEC5 ((vector_ulong2){5, 5}) +#define VEC6 ((vector_ulong2){6, 6}) +#define VEC7 ((vector_ulong2){7, 7}) +#define VEC8 ((vector_ulong2){8, 8}) + +#define CHECK_ARGS(sel) \ +do { \ + testassert(self == SELF); \ + testassert(_cmd == sel_registerName(#sel "::::::::::::::::::::::::::::::::::::"));\ + testassert(i1 == 1); \ + testassert(i2 == 2); \ + testassert(i3 == 3); \ + testassert(i4 == 4); \ + testassert(i5 == 5); \ + testassert(i6 == 6); \ + testassert(i7 == 7); \ + testassert(i8 == 8); \ + testassert(i9 == 9); \ + testassert(i10 == 10); \ + testassert(i11 == 11); \ + testassert(i12 == 12); \ + testassert(i13 == 13); \ + testassert(f1 == 1.0); \ + testassert(f2 == 2.0); \ + testassert(f3 == 3.0); \ + testassert(f4 == 4.0); \ + testassert(f5 == 5.0); \ + testassert(f6 == 6.0); \ + testassert(f7 == 7.0); \ + testassert(f8 == 8.0); \ + testassert(f9 == 9.0); \ + testassert(f10 == 10.0); \ + testassert(f11 == 11.0); \ + testassert(f12 == 12.0); \ + testassert(f13 == 13.0); \ + testassert(f14 == 14.0); \ + testassert(f15 == 15.0); \ + testassert(vector_all(v1 == 1)); \ + testassert(vector_all(v2 == 2)); \ + testassert(vector_all(v3 == 3)); \ + testassert(vector_all(v4 == 4)); \ + testassert(vector_all(v5 == 5)); \ + testassert(vector_all(v6 == 6)); \ + testassert(vector_all(v7 == 7)); \ + testassert(vector_all(v8 == 8)); \ +} while (0) + +#define CHECK_ARGS_NOARG(sel) \ +do { \ + testassert(self == SELF); \ + testassert(_cmd == sel_registerName(#sel "_noarg"));\ +} while (0) + +id NIL_RECEIVER; +id ID_RESULT; +long long LL_RESULT = __LONG_LONG_MAX__ - 2LL*__INT_MAX__; +double FP_RESULT = __DBL_MIN__ + __DBL_EPSILON__; +long double LFP_RESULT = __LDBL_MIN__ + __LDBL_EPSILON__; +vector_ulong2 VEC_RESULT = { 0x1234567890abcdefULL, 0xfedcba0987654321ULL }; +// STRET_RESULT in test.h + +static struct stret zero; + +struct stret_i1 { + uintptr_t i1; +}; +struct stret_i2 { + uintptr_t i1; + uintptr_t i2; +}; +struct stret_i3 { + uintptr_t i1; + uintptr_t i2; + uintptr_t i3; +}; +struct stret_i4 { + uintptr_t i1; + uintptr_t i2; + uintptr_t i3; +}; +struct stret_i5 { + uintptr_t i1; + uintptr_t i2; + uintptr_t i3; + uintptr_t i4; + uintptr_t i5; +}; +struct stret_i6 { + uintptr_t i1; + uintptr_t i2; + uintptr_t i3; + uintptr_t i4; + uintptr_t i5; + uintptr_t i6; +}; +struct stret_i7 { + uintptr_t i1; + uintptr_t i2; + uintptr_t i3; + uintptr_t i4; + uintptr_t i5; + uintptr_t i6; + uintptr_t i7; +}; +struct stret_i8 { + uintptr_t i1; + uintptr_t i2; + uintptr_t i3; + uintptr_t i4; + uintptr_t i5; + uintptr_t i8; + uintptr_t i9; +}; +struct stret_i9 { + uintptr_t i1; + uintptr_t i2; + uintptr_t i3; + uintptr_t i4; + uintptr_t i5; + uintptr_t i6; + uintptr_t i7; + uintptr_t i8; + uintptr_t i9; +}; + +struct stret_d1 { + double d1; +}; +struct stret_d2 { + double d1; + double d2; +}; +struct stret_d3 { + double d1; + double d2; + double d3; +}; +struct stret_d4 { + double d1; + double d2; + double d3; +}; +struct stret_d5 { + double d1; + double d2; + double d3; + double d4; + double d5; +}; +struct stret_d6 { + double d1; + double d2; + double d3; + double d4; + double d5; + double d6; +}; +struct stret_d7 { + double d1; + double d2; + double d3; + double d4; + double d5; + double d6; + double d7; +}; +struct stret_d8 { + double d1; + double d2; + double d3; + double d4; + double d5; + double d8; + double d9; +}; +struct stret_d9 { + double d1; + double d2; + double d3; + double d4; + double d5; + double d6; + double d7; + double d8; + double d9; +}; + + +@interface Super (Prototypes) + +// Method prototypes to pacify -Wundeclared-selector. + +-(id)idret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + +-(long long)llret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + +-(struct stret)stret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + +-(double)fpret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + +-(long double)lfpret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + +-(vector_ulong2)vecret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15; + +@end + + +// Zero all volatile registers. +#if __cplusplus +extern "C" +#endif +void stomp(void); + +#if __x86_64__ +asm("\n .text" + "\n .globl _stomp" + "\n _stomp:" + "\n mov $0, %rax" + "\n mov $0, %rcx" + "\n mov $0, %rdx" + "\n mov $0, %rsi" + "\n mov $0, %rdi" + "\n mov $0, %r8" + "\n mov $0, %r9" + "\n mov $0, %r10" + "\n mov $0, %r11" + "\n xorps %xmm0, %xmm0" + "\n xorps %xmm1, %xmm1" + "\n xorps %xmm2, %xmm2" + "\n xorps %xmm3, %xmm3" + "\n xorps %xmm4, %xmm4" + "\n xorps %xmm5, %xmm5" + "\n xorps %xmm6, %xmm6" + "\n xorps %xmm7, %xmm7" + "\n xorps %xmm8, %xmm8" + "\n xorps %xmm9, %xmm9" + "\n xorps %xmm10, %xmm10" + "\n xorps %xmm11, %xmm11" + "\n xorps %xmm12, %xmm12" + "\n xorps %xmm13, %xmm13" + "\n xorps %xmm14, %xmm14" + "\n xorps %xmm15, %xmm15" + "\n ret"); + +#elif __i386__ +asm("\n .text" + "\n .globl _stomp" + "\n _stomp:" + "\n mov $0, %eax" + "\n mov $0, %ecx" + "\n mov $0, %edx" + "\n xorps %xmm0, %xmm0" + "\n xorps %xmm1, %xmm1" + "\n xorps %xmm2, %xmm2" + "\n xorps %xmm3, %xmm3" + "\n xorps %xmm4, %xmm4" + "\n xorps %xmm5, %xmm5" + "\n xorps %xmm6, %xmm6" + "\n xorps %xmm7, %xmm7" + "\n ret"); + +#elif __arm64__ +asm("\n .text" + "\n .globl _stomp" + "\n _stomp:" + "\n mov x0, #0" + "\n mov x1, #0" + "\n mov x2, #0" + "\n mov x3, #0" + "\n mov x4, #0" + "\n mov x5, #0" + "\n mov x6, #0" + "\n mov x7, #0" + "\n mov x8, #0" + "\n mov x9, #0" + "\n mov x10, #0" + "\n mov x11, #0" + "\n mov x12, #0" + "\n mov x13, #0" + "\n mov x14, #0" + "\n mov x15, #0" + "\n mov x16, #0" + "\n mov x17, #0" + "\n movi d0, #0" + "\n movi d1, #0" + "\n movi d2, #0" + "\n movi d3, #0" + "\n movi d4, #0" + "\n movi d5, #0" + "\n movi d6, #0" + "\n movi d7, #0" + "\n ret" + ); + +#elif __arm__ +asm("\n .text" + "\n .globl _stomp" + "\n .thumb_func _stomp" + "\n _stomp:" + "\n mov r0, #0" + "\n mov r1, #0" + "\n mov r2, #0" + "\n mov r3, #0" + "\n mov r9, #0" + "\n mov r12, #0" + "\n vmov.i32 q0, #0" + "\n vmov.i32 q1, #0" + "\n vmov.i32 q2, #0" + "\n vmov.i32 q3, #0" + "\n vmov.i32 q8, #0" + "\n vmov.i32 q9, #0" + "\n vmov.i32 q10, #0" + "\n vmov.i32 q11, #0" + "\n vmov.i32 q12, #0" + "\n vmov.i32 q13, #0" + "\n vmov.i32 q14, #0" + "\n vmov.i32 q15, #0" + "\n bx lr" + ); + +#else +# error unknown architecture +#endif + + +@implementation Super +-(struct stret)stret { return STRET_RESULT; } + +// The IMPL_ methods are not called directly. Instead the non IMPL_ name is +// called. The resolver function installs the real method. This allows +// the resolver function to stomp on registers to help test register +// preservation in the uncached path. + ++(BOOL) resolveInstanceMethod:(SEL)sel +{ + const char *name = sel_getName(sel); + if (! strstr(name, "::::::::")) return false; + + testprintf("resolving %s\n", name); + + stomp(); + char *realName; + asprintf(&realName, "IMPL_%s", name); + SEL realSel = sel_registerName(realName); + free(realName); + + IMP imp = class_getMethodImplementation(self, realSel); + if (imp == &_objc_msgForward) return false; + return class_addMethod(self, sel, imp, ""); +} + +-(id)IMPL_idret: +(vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + CHECK_ARGS(idret); + state = 1; + return ID_RESULT; +} + +-(long long)IMPL_llret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + CHECK_ARGS(llret); + state = 2; + return LL_RESULT; +} + +-(struct stret)IMPL_stret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + CHECK_ARGS(stret); + state = 3; + return STRET_RESULT; +} + +-(double)IMPL_fpret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + CHECK_ARGS(fpret); + state = 4; + return FP_RESULT; +} + +-(long double)IMPL_lfpret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + CHECK_ARGS(lfpret); + state = 5; + return LFP_RESULT; +} + +-(vector_ulong2)IMPL_vecret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + CHECK_ARGS(vecret); + state = 6; + return VEC_RESULT; +} + + +-(id)idret_noarg +{ + CHECK_ARGS_NOARG(idret); + state = 11; + return ID_RESULT; +} + +-(long long)llret_noarg +{ + CHECK_ARGS_NOARG(llret); + state = 12; + return LL_RESULT; +} + +-(struct stret)stret_noarg +{ + CHECK_ARGS_NOARG(stret); + state = 13; + return STRET_RESULT; +} + +-(double)fpret_noarg +{ + CHECK_ARGS_NOARG(fpret); + state = 14; + return FP_RESULT; +} + +-(long double)lfpret_noarg +{ + CHECK_ARGS_NOARG(lfpret); + state = 15; + return LFP_RESULT; +} + +-(vector_ulong2)vecret_noarg +{ + CHECK_ARGS_NOARG(vecret); + state = 16; + return VEC_RESULT; +} + + +-(struct stret)stret_nop +{ + return STRET_RESULT; +} + + +#define STRET_IMP(n) \ ++(struct stret_##n)stret_##n##_zero \ +{ \ + struct stret_##n ret; \ + bzero(&ret, sizeof(ret)); \ + return ret; \ +} \ ++(struct stret_##n)stret_##n##_nonzero \ +{ \ + struct stret_##n ret; \ + memset(&ret, 0xff, sizeof(ret)); \ + return ret; \ +} + +STRET_IMP(i1) +STRET_IMP(i2) +STRET_IMP(i3) +STRET_IMP(i4) +STRET_IMP(i5) +STRET_IMP(i6) +STRET_IMP(i7) +STRET_IMP(i8) +STRET_IMP(i9) + +STRET_IMP(d1) +STRET_IMP(d2) +STRET_IMP(d3) +STRET_IMP(d4) +STRET_IMP(d5) +STRET_IMP(d6) +STRET_IMP(d7) +STRET_IMP(d8) +STRET_IMP(d9) + + ++(id)idret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + fail("+idret called instead of -idret"); + CHECK_ARGS(idret); +} + ++(long long)llret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + fail("+llret called instead of -llret"); + CHECK_ARGS(llret); +} + ++(struct stret)stret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + fail("+stret called instead of -stret"); + CHECK_ARGS(stret); +} + ++(double)fpret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + fail("+fpret called instead of -fpret"); + CHECK_ARGS(fpret); +} + ++(long double)lfpret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + fail("+lfpret called instead of -lfpret"); + CHECK_ARGS(lfpret); +} + ++(id)idret_noarg +{ + fail("+idret_noarg called instead of -idret_noarg"); + CHECK_ARGS_NOARG(idret); +} + ++(long long)llret_noarg +{ + fail("+llret_noarg called instead of -llret_noarg"); + CHECK_ARGS_NOARG(llret); +} + ++(struct stret)stret_noarg +{ + fail("+stret_noarg called instead of -stret_noarg"); + CHECK_ARGS_NOARG(stret); +} + ++(double)fpret_noarg +{ + fail("+fpret_noarg called instead of -fpret_noarg"); + CHECK_ARGS_NOARG(fpret); +} + ++(long double)lfpret_noarg +{ + fail("+lfpret_noarg called instead of -lfpret_noarg"); + CHECK_ARGS_NOARG(lfpret); +} + ++(vector_ulong2)vecret_noarg +{ + fail("+vecret_noarg called instead of -vecret_noarg"); + CHECK_ARGS_NOARG(vecret); +} + +@end + + +@implementation Sub + +-(id)IMPL_idret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + id result; + CHECK_ARGS(idret); + state = 100; + result = [super idret:v1:v2:v3:v4:v5:v6:v7:v8:i1:i2:i3:i4:i5:i6:i7:i8:i9:i10:i11:i12:i13:f1:f2:f3:f4:f5:f6:f7:f8:f9:f10:f11:f12:f13:f14:f15]; + testassert(state == 1); + testassert(result == ID_RESULT); + state = 101; + return result; +} + +-(long long)IMPL_llret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + long long result; + CHECK_ARGS(llret); + state = 100; + result = [super llret:v1:v2:v3:v4:v5:v6:v7:v8:i1:i2:i3:i4:i5:i6:i7:i8:i9:i10:i11:i12:i13:f1:f2:f3:f4:f5:f6:f7:f8:f9:f10:f11:f12:f13:f14:f15]; + testassert(state == 2); + testassert(result == LL_RESULT); + state = 102; + return result; +} + +-(struct stret)IMPL_stret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + struct stret result; + CHECK_ARGS(stret); + state = 100; + result = [super stret:v1:v2:v3:v4:v5:v6:v7:v8:i1:i2:i3:i4:i5:i6:i7:i8:i9:i10:i11:i12:i13:f1:f2:f3:f4:f5:f6:f7:f8:f9:f10:f11:f12:f13:f14:f15]; + testassert(state == 3); + testassert(stret_equal(result, STRET_RESULT)); + state = 103; + return result; +} + +-(double)IMPL_fpret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + double result; + CHECK_ARGS(fpret); + state = 100; + result = [super fpret:v1:v2:v3:v4:v5:v6:v7:v8:i1:i2:i3:i4:i5:i6:i7:i8:i9:i10:i11:i12:i13:f1:f2:f3:f4:f5:f6:f7:f8:f9:f10:f11:f12:f13:f14:f15]; + testassert(state == 4); + testassert(result == FP_RESULT); + state = 104; + return result; +} + +-(long double)IMPL_lfpret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + long double result; + CHECK_ARGS(lfpret); + state = 100; + result = [super lfpret:v1:v2:v3:v4:v5:v6:v7:v8:i1:i2:i3:i4:i5:i6:i7:i8:i9:i10:i11:i12:i13:f1:f2:f3:f4:f5:f6:f7:f8:f9:f10:f11:f12:f13:f14:f15]; + testassert(state == 5); + testassert(result == LFP_RESULT); + state = 105; + return result; +} + +-(vector_ulong2)IMPL_vecret: + (vector_ulong2)v1 :(vector_ulong2)v2 :(vector_ulong2)v3 :(vector_ulong2)v4 :(vector_ulong2)v5 :(vector_ulong2)v6 :(vector_ulong2)v7 :(vector_ulong2)v8 :(int)i1 :(int)i2 :(int)i3 :(int)i4 :(int)i5 :(int)i6 :(int)i7 :(int)i8 :(int)i9 :(int)i10 :(int)i11 :(int)i12 :(int)i13 :(double)f1 :(double)f2 :(double)f3 :(double)f4 :(double)f5 :(double)f6 :(double)f7 :(double)f8 :(double)f9 :(double)f10 :(double)f11 :(double)f12 :(double)f13 :(double)f14 :(double)f15 +{ + vector_ulong2 result; + CHECK_ARGS(vecret); + state = 100; + result = [super vecret:v1:v2:v3:v4:v5:v6:v7:v8:i1:i2:i3:i4:i5:i6:i7:i8:i9:i10:i11:i12:i13:f1:f2:f3:f4:f5:f6:f7:f8:f9:f10:f11:f12:f13:f14:f15]; + testassert(state == 6); + testassert(vector_equal(result, VEC_RESULT)); + state = 106; + return result; +} + + +-(id)idret_noarg +{ + id result; + CHECK_ARGS_NOARG(idret); + state = 100; + result = [super idret_noarg]; + testassert(state == 11); + testassert(result == ID_RESULT); + state = 111; + return result; +} + +-(long long)llret_noarg +{ + long long result; + CHECK_ARGS_NOARG(llret); + state = 100; + result = [super llret_noarg]; + testassert(state == 12); + testassert(result == LL_RESULT); + state = 112; + return result; +} + +-(struct stret)stret_noarg +{ + struct stret result; + CHECK_ARGS_NOARG(stret); + state = 100; + result = [super stret_noarg]; + testassert(state == 13); + testassert(stret_equal(result, STRET_RESULT)); + state = 113; + return result; +} + +-(double)fpret_noarg +{ + double result; + CHECK_ARGS_NOARG(fpret); + state = 100; + result = [super fpret_noarg]; + testassert(state == 14); + testassert(result == FP_RESULT); + state = 114; + return result; +} + +-(long double)lfpret_noarg +{ + long double result; + CHECK_ARGS_NOARG(lfpret); + state = 100; + result = [super lfpret_noarg]; + testassert(state == 15); + testassert(result == LFP_RESULT); + state = 115; + return result; +} + +-(vector_ulong2)vecret_noarg +{ + vector_ulong2 result; + CHECK_ARGS_NOARG(vecret); + state = 100; + result = [super vecret_noarg]; + testassert(state == 16); + testassert(vector_equal(result, VEC_RESULT)); + state = 116; + return result; +} + +@end + + +#if OBJC_HAVE_TAGGED_POINTERS + +@interface TaggedSub : Sub @end + +@implementation TaggedSub : Sub + ++(void)initialize +{ + _objc_registerTaggedPointerClass(OBJC_TAG_1, self); +} + +@end + +@interface ExtTaggedSub : Sub @end + +@implementation ExtTaggedSub : Sub + ++(void)initialize +{ + _objc_registerTaggedPointerClass(OBJC_TAG_First52BitPayload, self); +} + +@end + +#endif + + +// DWARF checking machinery + +#if TARGET_OS_WIN32 +// unimplemented on this platform +#define NO_DWARF_REASON "(windows)" + +#elif TARGET_OS_WATCH +// fixme unimplemented - ucontext not passed to signal handlers +#define NO_DWARF_REASON "(watchOS)" + +#elif __has_feature(objc_arc) +// ARC's extra RR calls hit the traps at the wrong times +#define NO_DWARF_REASON "(ARC)" + +#else + +#define TEST_DWARF 1 + +// Classes with no implementations and no cache contents from elsewhere. +@interface SuperDW : TestRoot @end +@implementation SuperDW @end + +@interface Sub0DW : SuperDW @end +@implementation Sub0DW @end + +@interface SubDW : Sub0DW @end +@implementation SubDW @end + +#include +#include +#include +#include + +bool caught = false; +uintptr_t clobbered; + +__BEGIN_DECLS +extern void callit(void *obj, void *sel, void *fn); +extern struct stret callit_stret(void *obj, void *sel, void *fn); +__END_DECLS + +#if __x86_64__ + +typedef uint8_t insn_t; +typedef insn_t clobbered_insn_t; +#define BREAK_INSN ((insn_t)0x06) // undefined +#define BREAK_SIGNAL SIGILL + +uintptr_t r12 = 0; +uintptr_t r13 = 0; +uintptr_t r14 = 0; +uintptr_t r15 = 0; +uintptr_t rbx = 0; +uintptr_t rbp = 0; +uintptr_t rsp = 0; +uintptr_t rip = 0; + +void handle_exception(x86_thread_state64_t *state) +{ + unw_cursor_t curs; + unw_word_t reg; + int err; + int step; + + err = unw_init_local(&curs, (unw_context_t *)state); + testassert(!err); + + step = unw_step(&curs); + testassert(step > 0); + + err = unw_get_reg(&curs, UNW_X86_64_R12, ®); + testassert(!err); + testassert(reg == r12); + + err = unw_get_reg(&curs, UNW_X86_64_R13, ®); + testassert(!err); + testassert(reg == r13); + + err = unw_get_reg(&curs, UNW_X86_64_R14, ®); + testassert(!err); + testassert(reg == r14); + + err = unw_get_reg(&curs, UNW_X86_64_R15, ®); + testassert(!err); + testassert(reg == r15); + + err = unw_get_reg(&curs, UNW_X86_64_RBX, ®); + testassert(!err); + testassert(reg == rbx); + + err = unw_get_reg(&curs, UNW_X86_64_RBP, ®); + testassert(!err); + testassert(reg == rbp); + + err = unw_get_reg(&curs, UNW_X86_64_RSP, ®); + testassert(!err); + testassert(reg == rsp); + + err = unw_get_reg(&curs, UNW_REG_IP, ®); + testassert(!err); + testassert(reg == rip); + + + // set thread state to unwound state + state->__r12 = r12; + state->__r13 = r13; + state->__r14 = r14; + state->__r15 = r15; + state->__rbx = rbx; + state->__rbp = rbp; + state->__rsp = rsp; + state->__rip = rip; + + caught = true; +} + + +void break_handler(int sig, siginfo_t *info, void *cc) +{ + ucontext_t *uc = (ucontext_t *)cc; + mcontext_t mc = (mcontext_t)uc->uc_mcontext; + + testprintf(" handled\n"); + + testassert(sig == BREAK_SIGNAL); + testassert((uintptr_t)info->si_addr == clobbered); + + handle_exception(&mc->__ss); + // handle_exception changed register state for continuation +} + +__asm__( +"\n .text" +"\n .globl _callit" +"\n _callit:" +// save sp and return address to variables +"\n movq (%rsp), %r10" +"\n movq %r10, _rip(%rip)" +"\n movq %rsp, _rsp(%rip)" +"\n addq $8, _rsp(%rip)" // rewind to pre-call value +// save other non-volatile registers to variables +"\n movq %rbx, _rbx(%rip)" +"\n movq %rbp, _rbp(%rip)" +"\n movq %r12, _r12(%rip)" +"\n movq %r13, _r13(%rip)" +"\n movq %r14, _r14(%rip)" +"\n movq %r15, _r15(%rip)" +"\n jmpq *%rdx" + ); + +__asm__( +"\n .text" +"\n .globl _callit_stret" +"\n _callit_stret:" +// save sp and return address to variables +"\n movq (%rsp), %r10" +"\n movq %r10, _rip(%rip)" +"\n movq %rsp, _rsp(%rip)" +"\n addq $8, _rsp(%rip)" // rewind to pre-call value +// save other non-volatile registers to variables +"\n movq %rbx, _rbx(%rip)" +"\n movq %rbp, _rbp(%rip)" +"\n movq %r12, _r12(%rip)" +"\n movq %r13, _r13(%rip)" +"\n movq %r14, _r14(%rip)" +"\n movq %r15, _r15(%rip)" +"\n jmpq *%rcx" + ); + + +// x86_64 + +#elif __i386__ + +typedef uint8_t insn_t; +typedef insn_t clobbered_insn_t; +#define BREAK_INSN ((insn_t)0xcc) // int3 +#define BREAK_SIGNAL SIGTRAP + +uintptr_t eip = 0; +uintptr_t esp = 0; +uintptr_t ebx = 0; +uintptr_t ebp = 0; +uintptr_t edi = 0; +uintptr_t esi = 0; +uintptr_t espfix = 0; + +void handle_exception(i386_thread_state_t *state) +{ + unw_cursor_t curs; + unw_word_t reg; + int err; + int step; + + err = unw_init_local(&curs, (unw_context_t *)state); + testassert(!err); + + step = unw_step(&curs); + testassert(step > 0); + + err = unw_get_reg(&curs, UNW_REG_IP, ®); + testassert(!err); + testassert(reg == eip); + + err = unw_get_reg(&curs, UNW_X86_ESP, ®); + testassert(!err); + testassert(reg == esp); + + err = unw_get_reg(&curs, UNW_X86_EBX, ®); + testassert(!err); + testassert(reg == ebx); + + err = unw_get_reg(&curs, UNW_X86_EBP, ®); + testassert(!err); + testassert(reg == ebp); + + err = unw_get_reg(&curs, UNW_X86_EDI, ®); + testassert(!err); + testassert(reg == edi); + + err = unw_get_reg(&curs, UNW_X86_ESI, ®); + testassert(!err); + testassert(reg == esi); + + + // set thread state to unwound state + state->__eip = eip; + state->__esp = esp + espfix; + state->__ebx = ebx; + state->__ebp = ebp; + state->__edi = edi; + state->__esi = esi; + + caught = true; +} + + +void break_handler(int sig, siginfo_t *info, void *cc) +{ + ucontext_t *uc = (ucontext_t *)cc; + mcontext_t mc = (mcontext_t)uc->uc_mcontext; + + testprintf(" handled\n"); + + testassert(sig == BREAK_SIGNAL); + testassert((uintptr_t)info->si_addr-1 == clobbered); + + handle_exception(&mc->__ss); + // handle_exception changed register state for continuation +} + +__asm__( +"\n .text" +"\n .globl _callit" +"\n _callit:" +// save sp and return address to variables +"\n call 1f" +"\n 1: popl %edx" +"\n movl (%esp), %eax" +"\n movl %eax, _eip-1b(%edx)" +"\n movl %esp, _esp-1b(%edx)" +"\n addl $4, _esp-1b(%edx)" // rewind to pre-call value +"\n movl $0, _espfix-1b(%edx)" +// save other non-volatile registers to variables +"\n movl %ebx, _ebx-1b(%edx)" +"\n movl %ebp, _ebp-1b(%edx)" +"\n movl %edi, _edi-1b(%edx)" +"\n movl %esi, _esi-1b(%edx)" +"\n jmpl *12(%esp)" + ); + +__asm__( +"\n .text" +"\n .globl _callit_stret" +"\n _callit_stret:" +// save sp and return address to variables +"\n call 1f" +"\n 1: popl %edx" +"\n movl (%esp), %eax" +"\n movl %eax, _eip-1b(%edx)" +"\n movl %esp, _esp-1b(%edx)" +"\n addl $4, _esp-1b(%edx)" // rewind to pre-call value +"\n movl $4, _espfix-1b(%edx)" +// save other non-volatile registers to variables +"\n movl %ebx, _ebx-1b(%edx)" +"\n movl %ebp, _ebp-1b(%edx)" +"\n movl %edi, _edi-1b(%edx)" +"\n movl %esi, _esi-1b(%edx)" +"\n jmpl *16(%esp)" + ); + + +// i386 +#elif __arm64__ + +#include + +typedef uint32_t insn_t; +typedef insn_t clobbered_insn_t; +#define BREAK_INSN ((insn_t)0xd4200020) // brk #1 +#define BREAK_SIGNAL SIGTRAP + +uintptr_t x19 = 0; +uintptr_t x20 = 0; +uintptr_t x21 = 0; +uintptr_t x22 = 0; +uintptr_t x23 = 0; +uintptr_t x24 = 0; +uintptr_t x25 = 0; +uintptr_t x26 = 0; +uintptr_t x27 = 0; +uintptr_t x28 = 0; +uintptr_t fp = 0; +uintptr_t sp = 0; +uintptr_t pc = 0; + +void handle_exception(arm_thread_state64_t *state) +{ + unw_cursor_t curs; + unw_word_t reg; + int err; + int step; + + // libunwind layout differs from mcontext layout + // GPRs are the same but vector registers are not + unw_context_t unwstate; + unw_getcontext(&unwstate); + memcpy(&unwstate, state, sizeof(*state)); + + // libunwind and xnu sign some pointers differently + // xnu: not signed (fixme this may change?) + // libunwind: PC and LR both signed with return address key and SP + void **pcp = &((arm_thread_state64_t *)&unwstate)->__opaque_pc; + *pcp = ptrauth_sign_unauthenticated((void*)__darwin_arm_thread_state64_get_pc(*state), + ptrauth_key_return_address, + (ptrauth_extra_data_t)__darwin_arm_thread_state64_get_sp(*state)); + void **lrp = &((arm_thread_state64_t *)&unwstate)->__opaque_lr; + *lrp = ptrauth_sign_unauthenticated((void*)__darwin_arm_thread_state64_get_lr(*state), + ptrauth_key_return_address, + (ptrauth_extra_data_t)__darwin_arm_thread_state64_get_sp(*state)); + + err = unw_init_local(&curs, &unwstate); + testassert(!err); + + step = unw_step(&curs); + testassert(step > 0); + + err = unw_get_reg(&curs, UNW_ARM64_X19, ®); + testassert(!err); + testassert(reg == x19); + + err = unw_get_reg(&curs, UNW_ARM64_X20, ®); + testassert(!err); + testassert(reg == x20); + + err = unw_get_reg(&curs, UNW_ARM64_X21, ®); + testassert(!err); + testassert(reg == x21); + + err = unw_get_reg(&curs, UNW_ARM64_X22, ®); + testassert(!err); + testassert(reg == x22); + + err = unw_get_reg(&curs, UNW_ARM64_X23, ®); + testassert(!err); + testassert(reg == x23); + + err = unw_get_reg(&curs, UNW_ARM64_X24, ®); + testassert(!err); + testassert(reg == x24); + + err = unw_get_reg(&curs, UNW_ARM64_X25, ®); + testassert(!err); + testassert(reg == x25); + + err = unw_get_reg(&curs, UNW_ARM64_X26, ®); + testassert(!err); + testassert(reg == x26); + + err = unw_get_reg(&curs, UNW_ARM64_X27, ®); + testassert(!err); + testassert(reg == x27); + + err = unw_get_reg(&curs, UNW_ARM64_X28, ®); + testassert(!err); + testassert(reg == x28); + + err = unw_get_reg(&curs, UNW_ARM64_FP, ®); + testassert(!err); + testassert(reg == fp); + + err = unw_get_reg(&curs, UNW_ARM64_SP, ®); + testassert(!err); + testassert(reg == sp); + + err = unw_get_reg(&curs, UNW_REG_IP, ®); + testassert(!err); + // libunwind's return is signed but our value is not + reg = (uintptr_t)ptrauth_strip((void *)reg, ptrauth_key_return_address); + testassert(reg == pc); + + // libunwind restores PC into LR and doesn't track LR + // err = unw_get_reg(&curs, UNW_ARM64_LR, ®); + // testassert(!err); + // testassert(reg == lr); + + // set signal handler's thread state to unwound state + state->__x[19] = x19; + state->__x[20] = x20; + state->__x[21] = x21; + state->__x[22] = x22; + state->__x[23] = x23; + state->__x[24] = x24; + state->__x[25] = x25; + state->__x[26] = x26; + state->__x[27] = x27; + state->__x[28] = x28; + state->__opaque_fp = (void *)fp; + state->__opaque_lr = (void *)pc; // libunwind restores PC into LR + state->__opaque_sp = (void *)sp; + state->__opaque_pc = (void *)pc; + + caught = true; +} + + +void break_handler(int sig, siginfo_t *info, void *cc) +{ + ucontext_t *uc = (ucontext_t *)cc; + struct __darwin_mcontext64 *mc = (struct __darwin_mcontext64 *)uc->uc_mcontext; + + testprintf(" handled\n"); + + testassert(sig == BREAK_SIGNAL); + testassert((uintptr_t)info->si_addr == clobbered); + + handle_exception(&mc->__ss); + // handle_exception changed register state for continuation +} + + +__asm__( +"\n .text" +"\n .globl _callit" +"\n _callit:" +// save sp and return address to variables +"\n mov x16, sp" +"\n adrp x17, _sp@PAGE" +"\n str x16, [x17, _sp@PAGEOFF]" +"\n adrp x17, _pc@PAGE" +"\n str lr, [x17, _pc@PAGEOFF]" +// save other non-volatile registers to variables +"\n adrp x17, _x19@PAGE" +"\n str x19, [x17, _x19@PAGEOFF]" +"\n adrp x17, _x19@PAGE" +"\n str x20, [x17, _x20@PAGEOFF]" +"\n adrp x17, _x19@PAGE" +"\n str x21, [x17, _x21@PAGEOFF]" +"\n adrp x17, _x19@PAGE" +"\n str x22, [x17, _x22@PAGEOFF]" +"\n adrp x17, _x19@PAGE" +"\n str x23, [x17, _x23@PAGEOFF]" +"\n adrp x17, _x19@PAGE" +"\n str x24, [x17, _x24@PAGEOFF]" +"\n adrp x17, _x19@PAGE" +"\n str x25, [x17, _x25@PAGEOFF]" +"\n adrp x17, _x19@PAGE" +"\n str x26, [x17, _x26@PAGEOFF]" +"\n adrp x17, _x19@PAGE" +"\n str x27, [x17, _x27@PAGEOFF]" +"\n adrp x17, _x19@PAGE" +"\n str x28, [x17, _x28@PAGEOFF]" +"\n adrp x17, _x19@PAGE" +"\n str fp, [x17, _fp@PAGEOFF]" +"\n br x2" + ); + + +// arm64 +#elif __arm__ + +#include + +typedef uint16_t insn_t; +typedef struct { + insn_t first; + insn_t second; + bool thirty_two; +} clobbered_insn_t; +#define BREAK_INSN ((insn_t)0xdefe) // trap +#define BREAK_SIGNAL SIGILL +#define BREAK_SIGNAL2 SIGTRAP + +uintptr_t r4 = 0; +uintptr_t r5 = 0; +uintptr_t r6 = 0; +uintptr_t r7 = 0; +uintptr_t r8 = 0; +uintptr_t r10 = 0; +uintptr_t r11 = 0; +uintptr_t sp = 0; +uintptr_t pc = 0; + +void handle_exception(arm_thread_state_t *state) +{ + // No unwind tables on this architecture so no libunwind checks. + // We run the test anyway to verify instruction-level coverage. + + // set thread state to unwound state + state->__r[4] = r4; + state->__r[5] = r5; + state->__r[6] = r6; + state->__r[7] = r7; + state->__r[8] = r8; + state->__r[10] = r10; + state->__r[11] = r11; + state->__sp = sp; + state->__pc = pc; + // clear IT... bits so caller doesn't act on them + state->__cpsr &= ~0x0600fc00; + + caught = true; +} + + +void break_handler(int sig, siginfo_t *info, void *cc) +{ + ucontext_t *uc = (ucontext_t *)cc; + struct __darwin_mcontext32 *mc = (struct __darwin_mcontext32 *)uc->uc_mcontext; + + testprintf(" handled\n"); + + testassert(sig == BREAK_SIGNAL || sig == BREAK_SIGNAL2); + testassert((uintptr_t)info->si_addr == clobbered); + + handle_exception(&mc->__ss); + // handle_exception changed register state for continuation +} + + +__asm__( +"\n .text" +"\n .syntax unified" +"\n .code 16" +"\n .align 5" +"\n .globl _callit" +"\n .thumb_func" +"\n _callit:" +// save sp and return address to variables +"\n movw r12, :lower16:(_sp-1f-4)" +"\n movt r12, :upper16:(_sp-1f-4)" +"\n 1: add r12, pc" +"\n str sp, [r12]" +"\n movw r12, :lower16:(_pc-1f-4)" +"\n movt r12, :upper16:(_pc-1f-4)" +"\n 1: add r12, pc" +"\n str lr, [r12]" +// save other non-volatile registers to variables +"\n movw r12, :lower16:(_r4-1f-4)" +"\n movt r12, :upper16:(_r4-1f-4)" +"\n 1: add r12, pc" +"\n str r4, [r12]" +"\n movw r12, :lower16:(_r5-1f-4)" +"\n movt r12, :upper16:(_r5-1f-4)" +"\n 1: add r12, pc" +"\n str r5, [r12]" +"\n movw r12, :lower16:(_r6-1f-4)" +"\n movt r12, :upper16:(_r6-1f-4)" +"\n 1: add r12, pc" +"\n str r6, [r12]" +"\n movw r12, :lower16:(_r7-1f-4)" +"\n movt r12, :upper16:(_r7-1f-4)" +"\n 1: add r12, pc" +"\n str r7, [r12]" +"\n movw r12, :lower16:(_r8-1f-4)" +"\n movt r12, :upper16:(_r8-1f-4)" +"\n 1: add r12, pc" +"\n str r8, [r12]" +"\n movw r12, :lower16:(_r10-1f-4)" +"\n movt r12, :upper16:(_r10-1f-4)" +"\n 1: add r12, pc" +"\n str r10, [r12]" +"\n movw r12, :lower16:(_r11-1f-4)" +"\n movt r12, :upper16:(_r11-1f-4)" +"\n 1: add r12, pc" +"\n str r11, [r12]" +"\n bx r2" + ); + +__asm__( +"\n .text" +"\n .syntax unified" +"\n .code 16" +"\n .align 5" +"\n .globl _callit_stret" +"\n .thumb_func" +"\n _callit_stret:" +// save sp and return address to variables +"\n movw r12, :lower16:(_sp-1f-4)" +"\n movt r12, :upper16:(_sp-1f-4)" +"\n 1: add r12, pc" +"\n str sp, [r12]" +"\n movw r12, :lower16:(_pc-1f-4)" +"\n movt r12, :upper16:(_pc-1f-4)" +"\n 1: add r12, pc" +"\n str lr, [r12]" +// save other non-volatile registers to variables +"\n movw r12, :lower16:(_r4-1f-4)" +"\n movt r12, :upper16:(_r4-1f-4)" +"\n 1: add r12, pc" +"\n str r4, [r12]" +"\n movw r12, :lower16:(_r5-1f-4)" +"\n movt r12, :upper16:(_r5-1f-4)" +"\n 1: add r12, pc" +"\n str r5, [r12]" +"\n movw r12, :lower16:(_r6-1f-4)" +"\n movt r12, :upper16:(_r6-1f-4)" +"\n 1: add r12, pc" +"\n str r6, [r12]" +"\n movw r12, :lower16:(_r7-1f-4)" +"\n movt r12, :upper16:(_r7-1f-4)" +"\n 1: add r12, pc" +"\n str r7, [r12]" +"\n movw r12, :lower16:(_r8-1f-4)" +"\n movt r12, :upper16:(_r8-1f-4)" +"\n 1: add r12, pc" +"\n str r8, [r12]" +"\n movw r12, :lower16:(_r10-1f-4)" +"\n movt r12, :upper16:(_r10-1f-4)" +"\n 1: add r12, pc" +"\n str r10, [r12]" +"\n movw r12, :lower16:(_r11-1f-4)" +"\n movt r12, :upper16:(_r11-1f-4)" +"\n 1: add r12, pc" +"\n str r11, [r12]" +"\n bx r3" + ); + + +// arm +#else + +#error unknown architecture + +#endif + + +#if __arm__ +uintptr_t fnaddr(void *fn) { return (uintptr_t)fn & ~(uintptr_t)1; } +#else +uintptr_t fnaddr(void *fn) { return (uintptr_t)fn; } +#endif + +void flushICache(uintptr_t addr) { + sys_icache_invalidate((void *)addr, sizeof(insn_t)); +} + +insn_t set(uintptr_t dst, insn_t newvalue) +{ + uintptr_t start = dst & ~(PAGE_MAX_SIZE-1); + int err = mprotect((void*)start, PAGE_MAX_SIZE, PROT_READ|PROT_WRITE); + if (err) fail("mprotect(%p, RW-) failed (%d)", start, errno); + insn_t oldvalue = *(insn_t *)dst; + *(insn_t *)dst = newvalue; + err = mprotect((void*)start, PAGE_MAX_SIZE, PROT_READ|PROT_EXEC); + if (err) fail("mprotect(%p, R-X) failed (%d)", start, errno); + flushICache(dst); + return oldvalue; +} + +clobbered_insn_t clobber(void *fn, uintptr_t offset) +{ + clobbered = fnaddr(fn) + offset; + insn_t oldInsn = set(fnaddr(fn) + offset, BREAK_INSN); +#if __arm__ + // Need to clobber 32-bit Thumb instructions with another 32-bit instruction + // to preserve the behavior of IT... blocks. + clobbered_insn_t result = {oldInsn, 0, false}; + if (((oldInsn & 0xf000) == 0xf000) || + ((oldInsn & 0xf800) == 0xe800)) + { + testprintf("clobbering thumb-32 at offset %zu\n", offset); + // Old insn was 32-bit. Clobber all of it. + // First unclobber. + set(fnaddr(fn) + offset, oldInsn); + // f7f0 a0f0 is a "permanently undefined" Thumb-2 instruction. + // Clobber the first half last so `clobbered` gets the right value. + result.second = set(fnaddr(fn) + offset + 2, 0xa0f0); + result.first = set(fnaddr(fn) + offset, 0xf7f0); + result.thirty_two = true; + } + return result; +#else + return oldInsn; +#endif +} + +void unclobber(void *fn, uintptr_t offset, clobbered_insn_t oldvalue) +{ +#if __arm__ + if (oldvalue.thirty_two) { + set(fnaddr(fn) + offset + 2, oldvalue.second); + } + set(fnaddr(fn) + offset, oldvalue.first); +#else + set(fnaddr(fn) + offset, oldvalue); +#endif +} + + +// terminator for the list of instruction offsets +#define END_OFFSETS ~0UL + +// Disassemble instructions symbol.. offsets) *p-- = END_OFFSETS; +#endif + + return p; +} + + +uintptr_t *getOffsets(const char *symname, uintptr_t *outBase) +{ + // Find the start of our function. + uintptr_t symbol = (uintptr_t)dlsym(RTLD_NEXT, symname); + if (!symbol) return nil; +#if __has_feature(ptrauth_calls) + symbol = (uintptr_t) + ptrauth_strip((void*)symbol, ptrauth_key_function_pointer); +#endif + + if (outBase) *outBase = symbol; + + // Find the end of our function by finding the start + // of the next symbol after our target symbol. + + const int insnIncrement = +#if __arm64__ + 4; +#elif __arm__ + 2; // in case of thumb or thumb-2 +#elif __i386__ || __x86_64__ + 1; +#else +#error unknown architecture +#endif + + uintptr_t symbolEnd; + Dl_info dli; + int ok; + for (symbolEnd = symbol + insnIncrement; + ((ok = dladdr((void*)symbolEnd, &dli))) && dli.dli_saddr == (void*)symbol; + symbolEnd += insnIncrement) + ; + + testprintf("found %s at %p..<%p %d %p %s\n", + symname, (void*)symbol, (void*)symbolEnd, ok, dli.dli_saddr, dli.dli_sname); + + // Record the offset to each non-NOP instruction. + uintptr_t *result = (uintptr_t *)malloc(1000 * sizeof(uintptr_t)); + uintptr_t *end = result + 1000; + uintptr_t *p = result; + + p = disassemble(symbol, symbolEnd, p, end); + + // Also record the offsets in _objc_msgSend_uncached when present + // (which is the slow path and has a frame to unwind) + if (!strstr(symname, "_uncached")) { + const char *uncached_symname = strstr(symname, "stret") + ? "_objc_msgSend_stret_uncached" : "_objc_msgSend_uncached"; + uintptr_t uncached_symbol; + uintptr_t *uncached_offsets = + getOffsets(uncached_symname, &uncached_symbol); + if (uncached_offsets) { + uintptr_t *q = uncached_offsets; + // Skip prologue and epilogue of objc_msgSend_uncached + // because it's imprecisely modeled in compact unwind + int prologueInstructions, epilogueInstructions; +#if __arm64e__ + prologueInstructions = 3; + epilogueInstructions = 2; +#elif __arm64__ || __x86_64__ || __i386__ || __arm__ + prologueInstructions = 2; + epilogueInstructions = 1; +#else +#error unknown architecture +#endif + // skip past prologue + for (int i = 0; i < prologueInstructions; i++) { + testassert(*q != END_OFFSETS); + q++; + } + + // copy instructions + while (*q != END_OFFSETS) *p++ = *q++ + uncached_symbol - symbol; + + // rewind past epilogue + for (int i = 0; i < epilogueInstructions; i++) { + testassert(p > result); + p--; + } + + free(uncached_offsets); + } + } + + // Terminate the list of offsets and return. + testassert(p > result); + testassert(p < end); + *p = END_OFFSETS; + + return result; +} + + +void CALLIT(void *o, void *sel_arg, SEL s, void *f, bool stret) __attribute__((noinline)); +void CALLIT(void *o, void *sel_arg, SEL s, void *f, bool stret) +{ + uintptr_t message_ref[2]; + if (sel_arg != s) { + // fixup dispatch + // copy to a local buffer to keep sel_arg un-fixed-up + memcpy(message_ref, sel_arg, sizeof(message_ref)); + sel_arg = message_ref; + } + if (!stret) callit(o, sel_arg, f); +#if SUPPORT_STRET + else callit_stret(o, sel_arg, f); +#else + else fail("stret?"); +#endif +} + +void test_dw_forward(void) +{ + return; +} + +struct stret test_dw_forward_stret(void) +{ + return zero; +} + +// sub = ordinary receiver object +// tagged = tagged receiver object +// SEL = selector to send +// sub_arg = arg to pass in receiver register (may be objc_super struct) +// tagged_arg = arg to pass in receiver register (may be objc_super struct) +// sel_arg = arg to pass in sel register (may be message_ref) +// uncaughtAllowed is the number of acceptable unreachable instructions +// (for example, the ones that handle the corrupt-cache-error case) +void test_dw(const char *name, id sub, id tagged, id exttagged, bool stret, + int uncaughtAllowed) +{ + + testprintf("DWARF FOR %s%s\n", name, stret ? " (stret)" : ""); + + // We need 2 SELs of each alignment so we can generate hash collisions. + // sel_registerName() never returns those alignments because they + // differ from malloc's alignment. So we create lots of compiled-in + // SELs here and hope something fits. +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wundeclared-selector" + SEL sel = @selector(a); + SEL lotsOfSels[] = { + @selector(a1), @selector(a2), @selector(a3), @selector(a4), + @selector(a5), @selector(a6), @selector(a7), @selector(a8), + @selector(aa), @selector(ab), @selector(ac), @selector(ad), + @selector(ae), @selector(af), @selector(ag), @selector(ah), + @selector(A1), @selector(A2), @selector(A3), @selector(A4), + @selector(A5), @selector(A6), @selector(A7), @selector(A8), + @selector(AA), @selector(Ab), @selector(Ac), @selector(Ad), + @selector(Ae), @selector(Af), @selector(Ag), @selector(Ah), + @selector(bb1), @selector(bb2), @selector(bb3), @selector(bb4), + @selector(bb5), @selector(bb6), @selector(bb7), @selector(bb8), + @selector(bba), @selector(bbb), @selector(bbc), @selector(bbd), + @selector(bbe), @selector(bbf), @selector(bbg), @selector(bbh), + @selector(BB1), @selector(BB2), @selector(BB3), @selector(BB4), + @selector(BB5), @selector(BB6), @selector(BB7), @selector(BB8), + @selector(BBa), @selector(BBb), @selector(BBc), @selector(BBd), + @selector(BBe), @selector(BBf), @selector(BBg), @selector(BBh), + @selector(ccc1), @selector(ccc2), @selector(ccc3), @selector(ccc4), + @selector(ccc5), @selector(ccc6), @selector(ccc7), @selector(ccc8), + @selector(ccca), @selector(cccb), @selector(cccc), @selector(cccd), + @selector(ccce), @selector(cccf), @selector(cccg), @selector(ccch), + @selector(CCC1), @selector(CCC2), @selector(CCC3), @selector(CCC4), + @selector(CCC5), @selector(CCC6), @selector(CCC7), @selector(CCC8), + @selector(CCCa), @selector(CCCb), @selector(CCCc), @selector(CCCd), + @selector(CCCe), @selector(CCCf), @selector(CCCg), @selector(CCCh), + }; +#pragma clang diagnostic pop + + { + IMP imp = stret ? (IMP)test_dw_forward_stret : (IMP)test_dw_forward; + Class cls = object_getClass(sub); + Class tagcls = object_getClass(tagged); + Class exttagcls = object_getClass(exttagged); + class_replaceMethod(cls, sel, imp, ""); + class_replaceMethod(tagcls, sel, imp, ""); + class_replaceMethod(exttagcls, sel, imp, ""); + for (size_t i = 0; i < sizeof(lotsOfSels)/sizeof(lotsOfSels[0]); i++) { + class_replaceMethod(cls, lotsOfSels[i], imp, ""); + class_replaceMethod(tagcls, lotsOfSels[i], imp, ""); + class_replaceMethod(exttagcls, lotsOfSels[i], imp, ""); + } + } + + #define ALIGNCOUNT 16 + SEL sels[ALIGNCOUNT][2] = {{0}}; + for (int align = 0; align < ALIGNCOUNT; align++) { + for (size_t i = 0; i < sizeof(lotsOfSels)/sizeof(lotsOfSels[0]); i++) { + if ((uintptr_t)(void*)lotsOfSels[i] % ALIGNCOUNT == align) { + if (sels[align][0]) { + sels[align][1] = lotsOfSels[i]; + } else { + sels[align][0] = lotsOfSels[i]; + } + } + } + if (!sels[align][0]) fail("no SEL with alignment %d", align); + if (!sels[align][1]) fail("only one SEL with alignment %d", align); + } + + void *fn = dlsym(RTLD_DEFAULT, name); +#if __has_feature(ptrauth_calls) + fn = ptrauth_strip(fn, ptrauth_key_function_pointer); +#endif + testassert(fn); + + // argument substitutions + + void *sub_arg = (__bridge void*)sub; + void *tagged_arg = (__bridge void*)tagged; + void *exttagged_arg = (__bridge void*)exttagged; + void *sel_arg = (void*)sel; + + struct objc_super sup_st = { sub, object_getClass(sub) }; + struct objc_super tagged_sup_st = { tagged, object_getClass(tagged) }; + struct objc_super exttagged_sup_st = { exttagged, object_getClass(exttagged) }; + struct { void *imp; SEL sel; } message_ref = { fn, sel }; + + Class cache_cls = object_getClass(sub); + Class tagged_cache_cls = object_getClass(tagged); + Class exttagged_cache_cls = object_getClass(exttagged); + + if (strstr(name, "Super")) { + // super version - replace receiver with objc_super + // clear caches of superclass + cache_cls = class_getSuperclass(cache_cls); + tagged_cache_cls = class_getSuperclass(tagged_cache_cls); + exttagged_cache_cls = class_getSuperclass(exttagged_cache_cls); + sub_arg = &sup_st; + tagged_arg = &tagged_sup_st; + exttagged_arg = &exttagged_sup_st; + } + + if (strstr(name, "_fixup")) { + // fixup version - replace sel with message_ref + sel_arg = &message_ref; + } + + + uintptr_t *insnOffsets = getOffsets(name, nil); + testassert(insnOffsets); + uintptr_t offset; + int uncaughtCount = 0; + for (int oo = 0; insnOffsets[oo] != ~0UL; oo++) { + offset = insnOffsets[oo]; + testprintf("OFFSET %lu\n", offset); + + clobbered_insn_t saved_insn = clobber(fn, offset); + caught = false; + + // nil + if ((__bridge void*)sub == sub_arg) { + SELF = nil; + testprintf(" nil\n"); + CALLIT(nil, sel_arg, sel, fn, stret); + CALLIT(nil, sel_arg, sel, fn, stret); + } + + // uncached + SELF = sub; + testprintf(" uncached\n"); + _objc_flush_caches(cache_cls); + CALLIT(sub_arg, sel_arg, sel, fn, stret); + _objc_flush_caches(cache_cls); + CALLIT(sub_arg, sel_arg, sel, fn, stret); + + // cached + SELF = sub; + testprintf(" cached\n"); + CALLIT(sub_arg, sel_arg, sel, fn, stret); + CALLIT(sub_arg, sel_arg, sel, fn, stret); + + // uncached,tagged + SELF = tagged; + testprintf(" uncached,tagged\n"); + _objc_flush_caches(tagged_cache_cls); + CALLIT(tagged_arg, sel_arg, sel, fn, stret); + _objc_flush_caches(tagged_cache_cls); + CALLIT(tagged_arg, sel_arg, sel, fn, stret); + _objc_flush_caches(exttagged_cache_cls); + CALLIT(exttagged_arg, sel_arg, sel, fn, stret); + _objc_flush_caches(exttagged_cache_cls); + CALLIT(exttagged_arg, sel_arg, sel, fn, stret); + + // cached,tagged + SELF = tagged; + testprintf(" cached,tagged\n"); + CALLIT(tagged_arg, sel_arg, sel, fn, stret); + CALLIT(tagged_arg, sel_arg, sel, fn, stret); + CALLIT(exttagged_arg, sel_arg, sel, fn, stret); + CALLIT(exttagged_arg, sel_arg, sel, fn, stret); + + // multiple SEL alignments, collisions, wraps + SELF = sub; + for (int a = 0; a < ALIGNCOUNT; a++) { + testprintf(" cached and uncached, SEL alignment %d\n", a); + + // Count both up and down to be independent of + // implementation's cache scan direction + + _objc_flush_caches(cache_cls); + for (int x2 = 0; x2 < 8; x2++) { + for (int s = 0; s < 4; s++) { + int align = (a+s) % ALIGNCOUNT; + CALLIT(sub_arg, sels[align][0], sels[align][0], fn, stret); + CALLIT(sub_arg, sels[align][1], sels[align][1], fn, stret); + } + } + + _objc_flush_caches(cache_cls); + for (int x2 = 0; x2 < 8; x2++) { + for (int s = 0; s < 4; s++) { + int align = abs(a-s) % ALIGNCOUNT; + CALLIT(sub_arg, sels[align][0], sels[align][0], fn, stret); + CALLIT(sub_arg, sels[align][1], sels[align][1], fn, stret); + } + } + } + + unclobber(fn, offset, saved_insn); + + // remember offsets that were caught by none of the above + if (caught) { + insnOffsets[oo] = 0; + } else { + uncaughtCount++; + testprintf("offset %s+%lu not caught (%d/%d)\n", + name, offset, uncaughtCount, uncaughtAllowed); + } + } + + // Complain if too many offsets went uncaught. + // Acceptably-uncaught offsets include the corrupt-cache-error handler. + if (uncaughtCount != uncaughtAllowed) { + for (int oo = 0; insnOffsets[oo] != ~0UL; oo++) { + if (insnOffsets[oo]) { + fprintf(stderr, "BAD: offset %s+%lu not caught\n", + name, insnOffsets[oo]); + } + } + fail("wrong instructions not reached for %s (missed %d, expected %d)", + name, uncaughtCount, uncaughtAllowed); + } + + free(insnOffsets); +} + + +// TEST_DWARF +#endif + + +void test_basic(id receiver) +{ + id idval; + long long llval; + struct stret stretval; + double fpval; + long double lfpval; + vector_ulong2 vecval; + + // message uncached + // message uncached long long + // message uncached stret + // message uncached fpret + // message uncached fpret long double + // message uncached noarg (as above) + // message cached + // message cached long long + // message cached stret + // message cached fpret + // message cached fpret long double + // message cached noarg (as above) + // fixme verify that uncached lookup didn't happen the 2nd time? + SELF = receiver; + _objc_flush_caches(object_getClass(receiver)); + for (int i = 0; i < 5; i++) { + testprintf("idret\n"); + state = 0; + idval = nil; + idval = [receiver idret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 101); + testassert(idval == ID_RESULT); + + testprintf("llret\n"); + llval = 0; + llval = [receiver llret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 102); + testassert(llval == LL_RESULT); + + testprintf("stret\n"); + stretval = zero; + stretval = [receiver stret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 103); + testassert(stret_equal(stretval, STRET_RESULT)); + + testprintf("fpret\n"); + fpval = 0; + fpval = [receiver fpret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 104); + testassert(fpval == FP_RESULT); + + testprintf("lfpret\n"); + lfpval = 0; + lfpval = [receiver lfpret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 105); + testassert(lfpval == LFP_RESULT); + + testprintf("vecret\n"); + vecval = 0; + vecval = [receiver vecret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 106); + testassert(vector_equal(vecval, VEC_RESULT)); + + // explicitly call noarg messenger, even if compiler doesn't emit it + state = 0; + testprintf("idret noarg\n"); + idval = nil; + idval = ((typeof(idmsg0))objc_msgSend_noarg)(receiver, @selector(idret_noarg)); + testassert(state == 111); + testassert(idval == ID_RESULT); + + testprintf("llret noarg\n"); + llval = 0; + llval = ((typeof(llmsg0))objc_msgSend_noarg)(receiver, @selector(llret_noarg)); + testassert(state == 112); + testassert(llval == LL_RESULT); + /* + no objc_msgSend_stret_noarg + stretval = zero; + stretval = ((typeof(stretmsg0))objc_msgSend_stret_noarg)(receiver, @selector(stret_noarg)); + stretval = [receiver stret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 113); + testassert(stret_equal(stretval, STRET_RESULT)); + */ +#if !__i386__ + testprintf("fpret noarg\n"); + fpval = 0; + fpval = ((typeof(fpmsg0))objc_msgSend_noarg)(receiver, @selector(fpret_noarg)); + testassert(state == 114); + testassert(fpval == FP_RESULT); + + testprintf("vecret noarg\n"); + vecval = 0; + vecval = ((typeof(vecmsg0))objc_msgSend_noarg)(receiver, @selector(vecret_noarg)); + testassert(state == 116); + testassert(vector_equal(vecval, VEC_RESULT)); +#endif +#if !__i386__ && !__x86_64__ + testprintf("lfpret noarg\n"); + lfpval = 0; + lfpval = ((typeof(lfpmsg0))objc_msgSend_noarg)(receiver, @selector(lfpret_noarg)); + testassert(state == 115); + testassert(lfpval == LFP_RESULT); +#endif + } + + testprintf("basic done\n"); +} + +int main() +{ + PUSH_POOL { + id idval; + long long llval; + struct stret stretval; + double fpval; + long double lfpval; + vector_ulong2 vecval; + +#if __x86_64__ + struct stret *stretptr; +#endif + + Method idmethod; + Method llmethod; + Method stretmethod; + Method fpmethod; + Method lfpmethod; + Method vecmethod; + + id (*idfn)(id, Method, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double); + long long (*llfn)(id, Method, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double); + struct stret (*stretfn)(id, Method, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double); + double (*fpfn)(id, Method, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double); + long double (*lfpfn)(id, Method, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double); + vector_ulong2 (*vecfn)(id, Method, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double); + + id (*idmsg)(id, SEL, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double) __attribute__((unused)); + id (*idmsgsuper)(struct objc_super *, SEL, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double) __attribute__((unused)); + long long (*llmsg)(id, SEL, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double) __attribute__((unused)); + struct stret (*stretmsg)(id, SEL, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double) __attribute__((unused)); + struct stret (*stretmsgsuper)(struct objc_super *, SEL, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double) __attribute__((unused)); + double (*fpmsg)(id, SEL, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double) __attribute__((unused)); + long double (*lfpmsg)(id, SEL, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double) __attribute__((unused)); + vector_ulong2 (*vecmsg)(id, SEL, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double) __attribute__((unused)); + + // get +initialize out of the way + [Sub class]; +#if OBJC_HAVE_TAGGED_POINTERS + [TaggedSub class]; + [ExtTaggedSub class]; +#endif + + ID_RESULT = [Super new]; + + Sub *sub = [Sub new]; + Super *sup = [Super new]; +#if OBJC_HAVE_TAGGED_POINTERS + TaggedSub *tagged = (__bridge id)_objc_makeTaggedPointer(OBJC_TAG_1, 999); + ExtTaggedSub *exttagged = (__bridge id)_objc_makeTaggedPointer(OBJC_TAG_First52BitPayload, 999); +#endif + + // Basic cached and uncached dispatch. + // Do this first before anything below caches stuff. + testprintf("basic\n"); + test_basic(sub); +#if OBJC_HAVE_TAGGED_POINTERS + testprintf("basic tagged\n"); + test_basic(tagged); + testprintf("basic ext tagged\n"); + test_basic(exttagged); +#endif + + idmethod = class_getInstanceMethod([Super class], @selector(idret::::::::::::::::::::::::::::::::::::)); + testassert(idmethod); + llmethod = class_getInstanceMethod([Super class], @selector(llret::::::::::::::::::::::::::::::::::::)); + testassert(llmethod); + stretmethod = class_getInstanceMethod([Super class], @selector(stret::::::::::::::::::::::::::::::::::::)); + testassert(stretmethod); + fpmethod = class_getInstanceMethod([Super class], @selector(fpret::::::::::::::::::::::::::::::::::::)); + testassert(fpmethod); + lfpmethod = class_getInstanceMethod([Super class], @selector(lfpret::::::::::::::::::::::::::::::::::::)); + testassert(lfpmethod); + vecmethod = class_getInstanceMethod([Super class], @selector(vecret::::::::::::::::::::::::::::::::::::)); + testassert(vecmethod); + + idfn = (id (*)(id, Method, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double)) method_invoke; + llfn = (long long (*)(id, Method, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double)) method_invoke; + stretfn = (struct stret (*)(id, Method, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double)) method_invoke_stret; + fpfn = (double (*)(id, Method, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double)) method_invoke; + lfpfn = (long double (*)(id, Method, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double)) method_invoke; + vecfn = (vector_ulong2 (*)(id, Method, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, vector_ulong2, int, int, int, int, int, int, int, int, int, int, int, int, int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double)) method_invoke; + + // method_invoke + // method_invoke long long + // method_invoke_stret stret + // method_invoke_stret fpret + // method_invoke fpret long double + testprintf("method_invoke\n"); + + SELF = sup; + + state = 0; + idval = nil; + idval = (*idfn)(sup, idmethod, VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 1); + testassert(idval == ID_RESULT); + + llval = 0; + llval = (*llfn)(sup, llmethod, VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 2); + testassert(llval == LL_RESULT); + + stretval = zero; + stretval = (*stretfn)(sup, stretmethod, VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 3); + testassert(stret_equal(stretval, STRET_RESULT)); + + fpval = 0; + fpval = (*fpfn)(sup, fpmethod, VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 4); + testassert(fpval == FP_RESULT); + + lfpval = 0; + lfpval = (*lfpfn)(sup, lfpmethod, VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 5); + testassert(lfpval == LFP_RESULT); + + vecval = 0; + vecval = (*vecfn)(sup, vecmethod, VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 6); + testassert(vector_equal(vecval, VEC_RESULT)); + + + // message to nil + // message to nil long long + // message to nil stret + // message to nil fpret + // message to nil fpret long double + // Use NIL_RECEIVER to avoid compiler optimizations. + testprintf("message to nil\n"); + + state = 0; + idval = ID_RESULT; + idval = [(id)NIL_RECEIVER idret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 0); + testassert(idval == nil); + + state = 0; + llval = LL_RESULT; + llval = [(id)NIL_RECEIVER llret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 0); + testassert(llval == 0LL); + + state = 0; + stretval = zero; + stretval = [(id)NIL_RECEIVER stret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 0); + testassert(0 == memcmp(&stretval, &zero, sizeof(stretval))); + +#if __x86_64__ + // check stret return register + state = 0; + stretval = zero; + stretptr = ((struct stret *(*)(struct stret *, id, SEL))objc_msgSend_stret) + (&stretval, nil, @selector(stret_nop)); + testassert(stretptr == &stretval); + testassert(state == 0); + // no stret result guarantee for hand-written calls +#endif + +#if __i386__ + // check struct-return address stack pop + for (int i = 0; i < 10000000; i++) { + state = 0; + ((struct stret (*)(id, SEL))objc_msgSend_stret) + (nil, @selector(stret_nop)); + } +#endif + + state = 0; + fpval = FP_RESULT; + fpval = [(id)NIL_RECEIVER fpret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 0); + testassert(fpval == 0.0); + + state = 0; + lfpval = LFP_RESULT; + lfpval = [(id)NIL_RECEIVER lfpret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 0); + testassert(lfpval == 0.0); + + state = 0; + vecval = VEC_RESULT; + vecval = [(id)NIL_RECEIVER vecret :VEC1:VEC2:VEC3:VEC4:VEC5:VEC6:VEC7:VEC8:1:2:3:4:5:6:7:8:9:10:11:12:13:1.0:2.0:3.0:4.0:5.0:6.0:7.0:8.0:9.0:10.0:11.0:12.0:13.0:14.0:15.0]; + testassert(state == 0); + testassert(vector_all(vecval == 0)); + + // message to nil, different struct types + // This verifies that ordinary objc_msgSend() erases enough registers + // for structs that return in registers. +#define TEST_NIL_STRUCT(i,n) \ + do { \ + struct stret_##i##n z; \ + bzero(&z, sizeof(z)); \ + [Super stret_i##n##_nonzero]; \ + [Super stret_d##n##_nonzero]; \ + struct stret_##i##n val = [(id)NIL_RECEIVER stret_##i##n##_zero]; \ + testassert(0 == memcmp(&z, &val, sizeof(val))); \ + } while (0) + + TEST_NIL_STRUCT(i,1); + TEST_NIL_STRUCT(i,2); + TEST_NIL_STRUCT(i,3); + TEST_NIL_STRUCT(i,4); + TEST_NIL_STRUCT(i,5); + TEST_NIL_STRUCT(i,6); + TEST_NIL_STRUCT(i,7); + TEST_NIL_STRUCT(i,8); + TEST_NIL_STRUCT(i,9); + +#if __i386__ + testwarn("rdar://16267205 i386 struct{float} and struct{double}"); +#else + TEST_NIL_STRUCT(d,1); +#endif + TEST_NIL_STRUCT(d,2); + TEST_NIL_STRUCT(d,3); + TEST_NIL_STRUCT(d,4); + TEST_NIL_STRUCT(d,5); + TEST_NIL_STRUCT(d,6); + TEST_NIL_STRUCT(d,7); + TEST_NIL_STRUCT(d,8); + TEST_NIL_STRUCT(d,9); + + + // message to nil noarg + // explicitly call noarg messenger, even if compiler doesn't emit it + state = 0; + idval = ID_RESULT; + idval = ((typeof(idmsg0))objc_msgSend_noarg)(nil, @selector(idret_noarg)); + testassert(state == 0); + testassert(idval == nil); + + state = 0; + llval = LL_RESULT; + llval = ((typeof(llmsg0))objc_msgSend_noarg)(nil, @selector(llret_noarg)); + testassert(state == 0); + testassert(llval == 0LL); + + // no stret_noarg messenger + +#if !__i386__ + state = 0; + fpval = FP_RESULT; + fpval = ((typeof(fpmsg0))objc_msgSend_noarg)(nil, @selector(fpret_noarg)); + testassert(state == 0); + testassert(fpval == 0.0); + + state = 0; + vecval = VEC_RESULT; + vecval = ((typeof(vecmsg0))objc_msgSend_noarg)(nil, @selector(vecret_noarg)); + testassert(state == 0); + testassert(vector_all(vecval == 0)); +#endif +#if !__i386__ && !__x86_64__ + state = 0; + lfpval = LFP_RESULT; + lfpval = ((typeof(lfpmsg0))objc_msgSend_noarg)(nil, @selector(lfpret_noarg)); + testassert(state == 0); + testassert(lfpval == 0.0); +#endif + + + // rdar://8271364 objc_msgSendSuper2 must not change objc_super + testprintf("super struct\n"); + struct objc_super sup_st = { + sub, + object_getClass(sub), + }; + + SELF = sub; + + state = 100; + idval = nil; + idval = ((id(*)(struct objc_super *, SEL, vector_ulong2,vector_ulong2,vector_ulong2,vector_ulong2,vector_ulong2,vector_ulong2,vector_ulong2,vector_ulong2, int,int,int,int,int,int,int,int,int,int,int,int,int, double,double,double,double,double,double,double,double,double,double,double,double,double,double,double))objc_msgSendSuper2) (&sup_st, @selector(idret::::::::::::::::::::::::::::::::::::), VEC1,VEC2,VEC3,VEC4,VEC5,VEC6,VEC7,VEC8, 1,2,3,4,5,6,7,8,9,10,11,12,13, 1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0); + testassert(state == 1); + testassert(idval == ID_RESULT); + testassert(sup_st.receiver == sub); + testassert(sup_st.super_class == object_getClass(sub)); + + state = 100; + stretval = zero; + stretval = ((struct stret(*)(struct objc_super *, SEL, vector_ulong2,vector_ulong2,vector_ulong2,vector_ulong2,vector_ulong2,vector_ulong2,vector_ulong2,vector_ulong2, int,int,int,int,int,int,int,int,int,int,int,int,int, double,double,double,double,double,double,double,double,double,double,double,double,double,double,double))objc_msgSendSuper2_stret) (&sup_st, @selector(stret::::::::::::::::::::::::::::::::::::), VEC1,VEC2,VEC3,VEC4,VEC5,VEC6,VEC7,VEC8, 1,2,3,4,5,6,7,8,9,10,11,12,13, 1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0); + testassert(state == 3); + testassert(stret_equal(stretval, STRET_RESULT)); + testassert(sup_st.receiver == sub); + testassert(sup_st.super_class == object_getClass(sub)); + +#if !__arm64__ + // Debug messengers. + testprintf("debug messengers\n"); + + state = 0; + idmsg = (typeof(idmsg))objc_msgSend_debug; + idval = nil; + idval = (*idmsg)(sub, @selector(idret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 101); + testassert(idval == ID_RESULT); + + state = 0; + llmsg = (typeof(llmsg))objc_msgSend_debug; + llval = 0; + llval = (*llmsg)(sub, @selector(llret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 102); + testassert(llval == LL_RESULT); + + state = 0; + stretmsg = (typeof(stretmsg))objc_msgSend_stret_debug; + stretval = zero; + stretval = (*stretmsg)(sub, @selector(stret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 103); + testassert(stret_equal(stretval, STRET_RESULT)); + + state = 100; + sup_st.receiver = sub; + sup_st.super_class = object_getClass(sub); + idmsgsuper = (typeof(idmsgsuper))objc_msgSendSuper2_debug; + idval = nil; + idval = (*idmsgsuper)(&sup_st, @selector(idret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 1); + testassert(idval == ID_RESULT); + + state = 100; + sup_st.receiver = sub; + sup_st.super_class = object_getClass(sub); + stretmsgsuper = (typeof(stretmsgsuper))objc_msgSendSuper2_stret_debug; + stretval = zero; + stretval = (*stretmsgsuper)(&sup_st, @selector(stret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 3); + testassert(stret_equal(stretval, STRET_RESULT)); + +#if __i386__ + state = 0; + fpmsg = (typeof(fpmsg))objc_msgSend_fpret_debug; + fpval = 0; + fpval = (*fpmsg)(sub, @selector(fpret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 104); + testassert(fpval == FP_RESULT); +#endif +#if __x86_64__ + state = 0; + lfpmsg = (typeof(lfpmsg))objc_msgSend_fpret_debug; + lfpval = 0; + lfpval = (*lfpmsg)(sub, @selector(lfpret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + testassert(state == 105); + testassert(lfpval == LFP_RESULT); + + // fixme fp2ret +#endif + + // debug messengers +#endif + + + // objc_msgLookup + +#if 1 + // fixme objc_msgLookup test hack stopped working after a compiler update + +#elif __has_feature(objc_arc) + // ARC interferes with objc_msgLookup test hacks + +#elif __i386__ && TARGET_OS_SIMULATOR + testwarn("fixme msgLookup hack doesn't work"); + +#else + // fixme hack: call the looked-up method +# if __arm64__ +# define CALL_LOOKUP(ret) \ + asm volatile ("blr x17 \n mov %x0, x0" : "=r" (ret)) +# define CALL_LOOKUP_STRET(ret) \ + asm volatile ("mov x8, %x1 \n blr x17 \n" : "=m" (ret) : "r" (&ret)) + +# elif __arm__ +# define CALL_LOOKUP(ret) \ + asm volatile ("blx r12 \n mov %0, r0" : "=r" (ret)) +# define CALL_LOOKUP_STRET(ret) \ + asm volatile ("mov r0, %1 \n blx r12 \n" : "=m" (ret) : "r" (&ret)) + +# elif __x86_64__ +# define CALL_LOOKUP(ret) \ + asm volatile ("call *%%r11 \n mov %%rax, %0" : "=r" (ret)) +# define CALL_LOOKUP_STRET(ret) \ + asm volatile ("mov %1, %%rdi \n call *%%r11 \n" : "=m" (ret) : "r" (&ret)) + +# elif __i386__ +# define CALL_LOOKUP(ret) \ + asm volatile ("call *%%eax \n mov %%eax, %0" : "=r" (ret)) +# define CALL_LOOKUP_STRET(ret) \ + asm volatile ("add $4, %%esp \n mov %1, (%%esp) \n call *%%eax \n sub $4, %%esp \n" : "=m" (ret) : "d" (&ret)) + +# else +# error unknown architecture +# endif + + // msgLookup uncached + // msgLookup uncached super + // msgLookup uncached stret + // msgLookup uncached super stret + // msgLookup uncached fpret + // msgLookup uncached fpret long double + // msgLookup cached + // msgLookup cached stret + // msgLookup cached super + // msgLookup cached super stret + // msgLookup cached fpret + // msgLookup cached fpret long double + // fixme verify that uncached lookup didn't happen the 2nd time? + SELF = sub; + _objc_flush_caches(object_getClass(sub)); + for (int i = 0; i < 5; i++) { + testprintf("objc_msgLookup\n"); + state = 0; + idmsg = (typeof(idmsg))objc_msgLookup; + idval = nil; + (*idmsg)(sub, @selector(idret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + CALL_LOOKUP(idval); + testassert(state == 101); + testassert(idval == ID_RESULT); + + testprintf("objc_msgLookup_stret\n"); + state = 0; + stretmsg = (typeof(stretmsg))objc_msgLookup_stret; + stretval = zero; + (*stretmsg)(sub, @selector(stret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + CALL_LOOKUP_STRET(stretval); + testassert(state == 103); + testassert(stret_equal(stretval, STRET_RESULT)); + + testprintf("objc_msgLookupSuper2\n"); + state = 100; + sup_st.receiver = sub; + sup_st.super_class = object_getClass(sub); + idmsgsuper = (typeof(idmsgsuper))objc_msgLookupSuper2; + idval = nil; + idval = (*idmsgsuper)(&sup_st, @selector(idret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + CALL_LOOKUP(idval); + testassert(state == 1); + testassert(idval == ID_RESULT); + + testprintf("objc_msgLookupSuper2_stret\n"); + state = 100; + sup_st.receiver = sub; + sup_st.super_class = object_getClass(sub); + stretmsgsuper = (typeof(stretmsgsuper))objc_msgLookupSuper2_stret; + stretval = zero; + (*stretmsgsuper)(&sup_st, @selector(stret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + CALL_LOOKUP_STRET(stretval); + testassert(state == 3); + testassert(stret_equal(stretval, STRET_RESULT)); + +#if __i386__ + // fixme fpret, can't test FP stack properly +#endif +#if __x86_64__ + // fixme fpret, can't test FP stack properly + // fixme fp2ret, can't test FP stack properly +#endif + + } + + // msgLookup to nil + // msgLookup to nil stret + // fixme msgLookup to nil long long + // fixme msgLookup to nil fpret + // fixme msgLookup to nil fp2ret + + testprintf("objc_msgLookup to nil\n"); + state = 0; + idmsg = (typeof(idmsg))objc_msgLookup; + idval = nil; + (*idmsg)(NIL_RECEIVER, @selector(idret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + CALL_LOOKUP(idval); + testassert(state == 0); + testassert(idval == nil); + + testprintf("objc_msgLookup_stret to nil\n"); + state = 0; + stretmsg = (typeof(stretmsg))objc_msgLookup_stret; + stretval = zero; + (*stretmsg)(NIL_RECEIVER, @selector(stret::::::::::::::::::::::::::::::::::::), VEC1, VEC2, VEC3, VEC4, VEC5, VEC6, VEC7, VEC8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); + CALL_LOOKUP_STRET(stretval); + testassert(state == 0); + // no stret result guarantee + +#if __i386__ + // fixme fpret, can't test FP stack properly +#endif +#if __x86_64__ + // fixme fpret, can't test FP stack properly + // fixme fp2ret, can't test FP stack properly +#endif + + // objc_msgLookup +#endif + + + +#if !TEST_DWARF + testwarn("no unwind tables in this configuration " NO_DWARF_REASON); +#else + // DWARF unwind tables + testprintf("unwind tables\n"); + + // Clear simulator-related environment variables. + // Disassembly will run llvm-objdump which is not a simulator executable. + unsetenv("DYLD_ROOT_PATH"); + unsetenv("DYLD_FALLBACK_LIBRARY_PATH"); + unsetenv("DYLD_FALLBACK_FRAMEWORK_PATH"); + + // Check mprotect() of objc_msgSend. + // It doesn't work when running on a device with no libobjc root. + // In that case we skip this part of the test without failing. + // fixme make this work + // fixme now it doesn't work even with a libobjc root in place? + int err1 = mprotect((void *)((uintptr_t)&objc_msgSend & ~(PAGE_MAX_SIZE-1)), + PAGE_MAX_SIZE, PROT_READ | PROT_WRITE); + int errno1 = errno; + int err2 = mprotect((void *)((uintptr_t)&objc_msgSend & ~(PAGE_MAX_SIZE-1)), + PAGE_MAX_SIZE, PROT_READ | PROT_EXEC); + int errno2 = errno; + if (err1 || err2) { + testwarn("can't mprotect() objc_msgSend (%d, %d). " + "Skipping unwind table test.", + err1, errno1, err2, errno2); + } + else { + // install exception handler + struct sigaction act; + act.sa_sigaction = break_handler; + act.sa_mask = 0; + act.sa_flags = SA_SIGINFO; + sigaction(BREAK_SIGNAL, &act, nil); +#if defined(BREAK_SIGNAL2) + sigaction(BREAK_SIGNAL2, &act, nil); +#endif + + SubDW *dw = [[SubDW alloc] init]; + + objc_setForwardHandler((void*)test_dw_forward, (void*)test_dw_forward_stret); + +# if __x86_64__ + test_dw("objc_msgSend", dw, tagged, exttagged, false, 0); + test_dw("objc_msgSend_stret", dw, tagged, exttagged, true, 0); + test_dw("objc_msgSend_fpret", dw, tagged, exttagged, false, 0); + test_dw("objc_msgSend_fp2ret", dw, tagged, exttagged, false, 0); + test_dw("objc_msgSendSuper", dw, tagged, exttagged, false, 0); + test_dw("objc_msgSendSuper2", dw, tagged, exttagged, false, 0); + test_dw("objc_msgSendSuper_stret", dw, tagged, exttagged, true, 0); + test_dw("objc_msgSendSuper2_stret", dw, tagged, exttagged, true, 0); +# elif __i386__ + test_dw("objc_msgSend", dw, dw, dw, false, 0); + test_dw("objc_msgSend_stret", dw, dw, dw, true, 0); + test_dw("objc_msgSend_fpret", dw, dw, dw, false, 0); + test_dw("objc_msgSendSuper", dw, dw, dw, false, 0); + test_dw("objc_msgSendSuper2", dw, dw, dw, false, 0); + test_dw("objc_msgSendSuper_stret", dw, dw, dw, true, 0); + test_dw("objc_msgSendSuper2_stret", dw, dw, dw, true, 0); +# elif __arm64__ + test_dw("objc_msgSend", dw, tagged, exttagged, false, 1); + test_dw("objc_msgSendSuper", dw, tagged, exttagged, false, 1); + test_dw("objc_msgSendSuper2", dw, tagged, exttagged, false, 1); +# elif __arm__ + test_dw("objc_msgSend", dw, dw, dw, false, 0); + test_dw("objc_msgSend_stret", dw, dw, dw, true, 0); + test_dw("objc_msgSendSuper", dw, dw, dw, false, 0); + test_dw("objc_msgSendSuper2", dw, dw, dw, false, 0); + test_dw("objc_msgSendSuper_stret", dw, dw, dw, true, 0); + test_dw("objc_msgSendSuper2_stret", dw, dw, dw, true, 0); +# else +# error unknown architecture +# endif + } + + // end DWARF unwind test +#endif + + } POP_POOL; + succeed(__FILE__); +} diff --git a/test/nilAPIArgs.m b/test/nilAPIArgs.m new file mode 100644 index 0000000..29eee12 --- /dev/null +++ b/test/nilAPIArgs.m @@ -0,0 +1,18 @@ +/* +TEST_BUILD_OUTPUT +.*nilAPIArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*nilAPIArgs.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +END +*/ + +#include "test.h" + +#import + +int main() { + // ensure various bits of API don't crash when tossed nil parameters + class_conformsToProtocol(nil, nil); + method_setImplementation(nil, NULL); + + succeed(__FILE__); +} diff --git a/test/nonpointerisa.m b/test/nonpointerisa.m new file mode 100644 index 0000000..dbe222c --- /dev/null +++ b/test/nonpointerisa.m @@ -0,0 +1,262 @@ +// TEST_CFLAGS -framework Foundation +// TEST_CONFIG MEM=mrc + +#include "test.h" +#include + +#include +#include + +#define ISA(x) (*((uintptr_t *)(x))) +#define NONPOINTER(x) (ISA(x) & 1) + +#if SUPPORT_NONPOINTER_ISA +# if __x86_64__ +# define RC_ONE (1ULL<<56) +# elif __arm64__ && __LP64__ +# define RC_ONE (1ULL<<45) +# elif __ARM_ARCH_7K__ >= 2 || (__arm64__ && !__LP64__) +# define RC_ONE (1ULL<<25) +# else +# error unknown architecture +# endif +#endif + + +void check_raw_pointer(id obj, Class cls) +{ + testassert(object_getClass(obj) == cls); + testassert(!NONPOINTER(obj)); + + uintptr_t isa = ISA(obj); + testassert((Class)isa == cls); + testassert((Class)(isa & objc_debug_isa_class_mask) == cls); + testassert((Class)(isa & ~objc_debug_isa_class_mask) == 0); + + CFRetain(obj); + testassert(ISA(obj) == isa); + testassert([obj retainCount] == 2); + [obj retain]; + testassert(ISA(obj) == isa); + testassert([obj retainCount] == 3); + CFRelease(obj); + testassert(ISA(obj) == isa); + testassert([obj retainCount] == 2); + [obj release]; + testassert(ISA(obj) == isa); + testassert([obj retainCount] == 1); +} + + +#if ! SUPPORT_NONPOINTER_ISA + +int main() +{ +#if OBJC_HAVE_NONPOINTER_ISA || OBJC_HAVE_PACKED_NONPOINTER_ISA || OBJC_HAVE_INDEXED_NONPOINTER_ISA +# error wrong +#endif + + testprintf("Isa with index\n"); + id index_o = [NSObject new]; + check_raw_pointer(index_o, [NSObject class]); + + // These variables DO NOT exist without non-pointer isa support. + testassert(!dlsym(RTLD_DEFAULT, "objc_absolute_packed_isa_class_mask")); + testassert(!dlsym(RTLD_DEFAULT, "objc_absolute_indexed_isa_magic_mask")); + testassert(!dlsym(RTLD_DEFAULT, "objc_absolute_indexed_isa_magic_value")); + testassert(!dlsym(RTLD_DEFAULT, "objc_absolute_indexed_isa_index_mask")); + testassert(!dlsym(RTLD_DEFAULT, "objc_absolute_indexed_isa_index_shift")); + + // These variables DO exist even without non-pointer isa support. + testassert(dlsym(RTLD_DEFAULT, "objc_debug_isa_class_mask")); + testassert(dlsym(RTLD_DEFAULT, "objc_debug_isa_magic_mask")); + testassert(dlsym(RTLD_DEFAULT, "objc_debug_isa_magic_value")); + + succeed(__FILE__); +} + +#else +// SUPPORT_NONPOINTER_ISA + +void check_nonpointer(id obj, Class cls) +{ + testassert(object_getClass(obj) == cls); + testassert(NONPOINTER(obj)); + + uintptr_t isa = ISA(obj); + + if (objc_debug_indexed_isa_magic_mask != 0) { + // Indexed isa. + testassert((isa & objc_debug_indexed_isa_magic_mask) == objc_debug_indexed_isa_magic_value); + testassert((isa & ~objc_debug_indexed_isa_index_mask) != 0); + uintptr_t index = (isa & objc_debug_indexed_isa_index_mask) >> objc_debug_indexed_isa_index_shift; + testassert(index < objc_indexed_classes_count); + testassert(objc_indexed_classes[index] == cls); + } else { + // Packed isa. + testassert((Class)(isa & objc_debug_isa_class_mask) == cls); + testassert((Class)(isa & ~objc_debug_isa_class_mask) != 0); + testassert((isa & objc_debug_isa_magic_mask) == objc_debug_isa_magic_value); + } + + CFRetain(obj); + testassert(ISA(obj) == isa + RC_ONE); + testassert([obj retainCount] == 2); + [obj retain]; + testassert(ISA(obj) == isa + RC_ONE*2); + testassert([obj retainCount] == 3); + CFRelease(obj); + testassert(ISA(obj) == isa + RC_ONE); + testassert([obj retainCount] == 2); + [obj release]; + testassert(ISA(obj) == isa); + testassert([obj retainCount] == 1); +} + + +@interface OS_object ++(id)alloc; +@end + +@interface Fake_OS_object : NSObject { + int refcnt; + int xref_cnt; +} +@end + +@implementation Fake_OS_object ++(void)initialize { + static bool initialized; + if (!initialized) { + initialized = true; + testprintf("Nonpointer during +initialize\n"); + testassert(!NONPOINTER(self)); + id o = [Fake_OS_object new]; + check_nonpointer(o, self); + [o release]; + } +} +@end + +@interface Sub_OS_object : OS_object @end + +@implementation Sub_OS_object +@end + + + +int main() +{ + uintptr_t isa; + +#if SUPPORT_PACKED_ISA +# if !OBJC_HAVE_NONPOINTER_ISA || !OBJC_HAVE_PACKED_NONPOINTER_ISA || OBJC_HAVE_INDEXED_NONPOINTER_ISA +# error wrong +# endif + testassert(objc_debug_isa_class_mask == (uintptr_t)&objc_absolute_packed_isa_class_mask); + + // Indexed isa variables DO NOT exist on packed-isa platforms + testassert(!dlsym(RTLD_DEFAULT, "objc_absolute_indexed_isa_magic_mask")); + testassert(!dlsym(RTLD_DEFAULT, "objc_absolute_indexed_isa_magic_value")); + testassert(!dlsym(RTLD_DEFAULT, "objc_absolute_indexed_isa_index_mask")); + testassert(!dlsym(RTLD_DEFAULT, "objc_absolute_indexed_isa_index_shift")); + +#elif SUPPORT_INDEXED_ISA +# if !OBJC_HAVE_NONPOINTER_ISA || OBJC_HAVE_PACKED_NONPOINTER_ISA || !OBJC_HAVE_INDEXED_NONPOINTER_ISA +# error wrong +# endif + testassert(objc_debug_indexed_isa_magic_mask == (uintptr_t)&objc_absolute_indexed_isa_magic_mask); + testassert(objc_debug_indexed_isa_magic_value == (uintptr_t)&objc_absolute_indexed_isa_magic_value); + testassert(objc_debug_indexed_isa_index_mask == (uintptr_t)&objc_absolute_indexed_isa_index_mask); + testassert(objc_debug_indexed_isa_index_shift == (uintptr_t)&objc_absolute_indexed_isa_index_shift); + + // Packed isa variable DOES NOT exist on indexed-isa platforms. + testassert(!dlsym(RTLD_DEFAULT, "objc_absolute_packed_isa_class_mask")); + +#else +# error unknown nonpointer isa format +#endif + + testprintf("Isa with index\n"); + id index_o = [Fake_OS_object new]; + check_nonpointer(index_o, [Fake_OS_object class]); + + testprintf("Weakly referenced\n"); + isa = ISA(index_o); + id weak; + objc_storeWeak(&weak, index_o); + testassert(__builtin_popcountl(isa ^ ISA(index_o)) == 1); + + testprintf("Has associated references\n"); + id assoc = @"thing"; + isa = ISA(index_o); + objc_setAssociatedObject(index_o, assoc, assoc, OBJC_ASSOCIATION_ASSIGN); + testassert(__builtin_popcountl(isa ^ ISA(index_o)) == 1); + + + testprintf("Isa without index\n"); + id raw_o = [OS_object alloc]; + check_raw_pointer(raw_o, [OS_object class]); + + + id buf[4]; + id bufo = (id)buf; + + testprintf("Change isa 0 -> raw pointer\n"); + bzero(buf, sizeof(buf)); + object_setClass(bufo, [OS_object class]); + check_raw_pointer(bufo, [OS_object class]); + + testprintf("Change isa 0 -> nonpointer\n"); + bzero(buf, sizeof(buf)); + object_setClass(bufo, [NSObject class]); + check_nonpointer(bufo, [NSObject class]); + + testprintf("Change isa nonpointer -> nonpointer\n"); + testassert(NONPOINTER(bufo)); + _objc_rootRetain(bufo); + testassert(_objc_rootRetainCount(bufo) == 2); + object_setClass(bufo, [Fake_OS_object class]); + testassert(_objc_rootRetainCount(bufo) == 2); + _objc_rootRelease(bufo); + testassert(_objc_rootRetainCount(bufo) == 1); + check_nonpointer(bufo, [Fake_OS_object class]); + + testprintf("Change isa nonpointer -> raw pointer\n"); + // Retain count must be preserved. + // Use root* to avoid OS_object's overrides. + testassert(NONPOINTER(bufo)); + _objc_rootRetain(bufo); + testassert(_objc_rootRetainCount(bufo) == 2); + object_setClass(bufo, [OS_object class]); + testassert(_objc_rootRetainCount(bufo) == 2); + _objc_rootRelease(bufo); + testassert(_objc_rootRetainCount(bufo) == 1); + check_raw_pointer(bufo, [OS_object class]); + + testprintf("Change isa raw pointer -> nonpointer (doesn't happen)\n"); + testassert(!NONPOINTER(bufo)); + _objc_rootRetain(bufo); + testassert(_objc_rootRetainCount(bufo) == 2); + object_setClass(bufo, [Fake_OS_object class]); + testassert(_objc_rootRetainCount(bufo) == 2); + _objc_rootRelease(bufo); + testassert(_objc_rootRetainCount(bufo) == 1); + check_raw_pointer(bufo, [Fake_OS_object class]); + + testprintf("Change isa raw pointer -> raw pointer\n"); + testassert(!NONPOINTER(bufo)); + _objc_rootRetain(bufo); + testassert(_objc_rootRetainCount(bufo) == 2); + object_setClass(bufo, [Sub_OS_object class]); + testassert(_objc_rootRetainCount(bufo) == 2); + _objc_rootRelease(bufo); + testassert(_objc_rootRetainCount(bufo) == 1); + check_raw_pointer(bufo, [Sub_OS_object class]); + + + succeed(__FILE__); +} + +// SUPPORT_NONPOINTER_ISA +#endif diff --git a/test/nopool.m b/test/nopool.m new file mode 100644 index 0000000..f3493de --- /dev/null +++ b/test/nopool.m @@ -0,0 +1,49 @@ +// TEST_CONFIG MEM=mrc + +#include "test.h" +#include "testroot.i" + +@implementation TestRoot (Loader) ++(void)load +{ + [[TestRoot new] autorelease]; + testassert(TestRootAutorelease == 1); + testassert(TestRootDealloc == 0); +} +@end + +int main() +{ + // +load's autoreleased object should have deallocated + testassert(TestRootDealloc == 1); + + [[TestRoot new] autorelease]; + testassert(TestRootAutorelease == 2); + + + objc_autoreleasePoolPop(objc_autoreleasePoolPush()); + [[TestRoot new] autorelease]; + testassert(TestRootAutorelease == 3); + + + testonthread(^{ + [[TestRoot new] autorelease]; + testassert(TestRootAutorelease == 4); + testassert(TestRootDealloc == 1); + }); + // thread's autoreleased object should have deallocated + testassert(TestRootDealloc == 2); + + + // Test no-pool autorelease after a pool was pushed and popped. + // The simplest POOL_SENTINEL check during pop gets this wrong. + testonthread(^{ + objc_autoreleasePoolPop(objc_autoreleasePoolPush()); + [[TestRoot new] autorelease]; + testassert(TestRootAutorelease == 5); + testassert(TestRootDealloc == 2); + }); + testassert(TestRootDealloc == 3 +); + succeed(__FILE__); +} diff --git a/test/nscdtors.mm b/test/nscdtors.mm new file mode 100644 index 0000000..876f6b0 --- /dev/null +++ b/test/nscdtors.mm @@ -0,0 +1,6 @@ +// TEST_CONFIG +// test cdtors, with NSObject instead of TestRoot as the root class + +#define USE_FOUNDATION 1 +#include "cdtors.mm" + diff --git a/test/nsexc.m b/test/nsexc.m new file mode 100644 index 0000000..cf4ac43 --- /dev/null +++ b/test/nsexc.m @@ -0,0 +1,7 @@ +/* +need exception-safe ARC for exception deallocation tests +TEST_CFLAGS -fobjc-arc-exceptions -framework Foundation +*/ + +#define USE_FOUNDATION 1 +#include "exc.m" diff --git a/test/nsobject.m b/test/nsobject.m new file mode 100644 index 0000000..dd64bd0 --- /dev/null +++ b/test/nsobject.m @@ -0,0 +1,114 @@ +// TEST_CONFIG MEM=mrc + +#include "test.h" + +#import + +@interface Sub : NSObject @end +@implementation Sub ++(id)allocWithZone:(NSZone *)zone { + testprintf("in +[Sub alloc]\n"); + return [super allocWithZone:zone]; + } +-(void)dealloc { + testprintf("in -[Sub dealloc]\n"); + [super dealloc]; +} +@end + + +// These declarations and definitions can be used +// to check the compile-time type of an object. +@interface NSObject (Checker) +// fixme this isn't actually enforced ++(void)NSObjectInstance __attribute__((unavailable)); +@end +@implementation NSObject (Checker) +-(void)NSObjectInstance { } ++(void)NSObjectClass { } +@end +@interface Sub (Checker) +-(void)NSObjectInstance __attribute__((unavailable)); ++(void)NSObjectClass __attribute__((unavailable)); +@end +@implementation Sub (Checker) +-(void)SubInstance { } ++(void)SubClass { } +@end + +int main() +{ + PUSH_POOL { + [[Sub new] autorelease]; + } POP_POOL; + + // Verify that dot syntax on class objects works with some instance methods + // (void)NSObject.self; fixme + (void)NSObject.class; + (void)NSObject.superclass; + (void)NSObject.hash; + (void)NSObject.description; + (void)NSObject.debugDescription; + + // Verify that some methods return the correct type. + Class cls; + NSObject *nsobject = nil; + Sub *subobject = nil; + + cls = [NSObject self]; + cls = [Sub self]; + nsobject = [nsobject self]; + subobject = [subobject self]; + [[NSObject self] NSObjectClass]; + [[nsobject self] NSObjectInstance]; + [[Sub self] SubClass]; + [[subobject self] SubInstance]; + + // fixme + // cls = NSObject.self; + // cls = Sub.self; + // [NSObject.self NSObjectClass]; + // [nsobject.self NSObjectInstance]; + // [Sub.self SubClass]; + // [subobject.self SubInstance]; + + cls = [NSObject class]; + cls = [nsobject class]; + cls = [Sub class]; + cls = [subobject class]; + [[NSObject class] NSObjectClass]; + [[nsobject class] NSObjectClass]; + [[Sub class] SubClass]; + [[subobject class] SubClass]; + + cls = NSObject.class; + cls = nsobject.class; + cls = Sub.class; + cls = subobject.class; + [NSObject.class NSObjectClass]; + [nsobject.class NSObjectClass]; + [Sub.class SubClass]; + [subobject.class SubClass]; + + + cls = [NSObject superclass]; + cls = [nsobject superclass]; + cls = [Sub superclass]; + cls = [subobject superclass]; + [[NSObject superclass] NSObjectClass]; + [[nsobject superclass] NSObjectClass]; + [[Sub superclass] NSObjectClass]; + [[subobject superclass] NSObjectClass]; + + cls = NSObject.superclass; + cls = nsobject.superclass; + cls = Sub.superclass; + cls = subobject.superclass; + [NSObject.superclass NSObjectClass]; + [nsobject.superclass NSObjectClass]; + [Sub.superclass NSObjectClass]; + [subobject.superclass NSObjectClass]; + + + succeed(__FILE__); +} diff --git a/test/nsprotocol.m b/test/nsprotocol.m new file mode 100644 index 0000000..5315371 --- /dev/null +++ b/test/nsprotocol.m @@ -0,0 +1,17 @@ +// TEST_CONFIG + +#include "test.h" +#include + +int main() +{ + // Class Protocol is always a subclass of NSObject + + testassert(objc_getClass("NSObject")); + + Class cls = objc_getClass("Protocol"); + testassert(class_getInstanceMethod(cls, sel_registerName("isProxy"))); + testassert(class_getSuperclass(cls) == objc_getClass("NSObject")); + + succeed(__FILE__); +} diff --git a/test/objectCopy.m b/test/objectCopy.m new file mode 100644 index 0000000..973ac4d --- /dev/null +++ b/test/objectCopy.m @@ -0,0 +1,38 @@ +// TEST_CONFIG MEM=mrc + +#include "test.h" +#include + +@interface Test : NSObject { +@public + char bytes[32-sizeof(void*)]; +} +@end +@implementation Test +@end + + +int main() +{ + Test *o0 = [Test new]; + [o0 retain]; + Test *o1 = class_createInstance([Test class], 32); + [o1 retain]; + id o2 = object_copy(o0, 0); + id o3 = object_copy(o1, 0); + id o4 = object_copy(o1, 32); + + testassert(malloc_size(o0) == 32); + testassert(malloc_size(o1) == 64); + testassert(malloc_size(o2) == 32); + testassert(malloc_size(o3) == 32); + testassert(malloc_size(o4) == 64); + + testassert([o0 retainCount] == 2); + testassert([o1 retainCount] == 2); + testassert([o2 retainCount] == 1); + testassert([o3 retainCount] == 1); + testassert([o4 retainCount] == 1); + + succeed(__FILE__); +} diff --git a/test/property.m b/test/property.m new file mode 100644 index 0000000..0c2de1e --- /dev/null +++ b/test/property.m @@ -0,0 +1,157 @@ +/* +TEST_BUILD_OUTPUT +.*property.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*property.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*property.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*property.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*property.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*property.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +END +*/ + +#include "test.h" +#include "testroot.i" +#include +#include +#include + + +@protocol SuperProto +@property(readonly) char superProtoProp; +@property(class,readonly) char superProtoProp; +@end + +@protocol SubProto +@property(readonly) uintptr_t subProtoProp; +@property(class,readonly) uintptr_t subProtoProp; +@property(readonly) uintptr_t subInstanceOnlyProtoProp; +@property(class,readonly) uintptr_t subClassOnlyProtoProp; +@end + +@interface Super : TestRoot { + @public + char superIvar; +} + +@property(readonly) char superProp; +@property(class,readonly) char superProp; +@end + +@implementation Super +@synthesize superProp = superIvar; ++(char)superProp { return 'a'; } + +-(char)superProtoProp { return 'a'; } ++(char)superProtoProp { return 'a'; } +@end + + +@interface Sub : Super { + @public + uintptr_t subIvar; +} +@property(readonly) uintptr_t subProp; +@property(class,readonly) uintptr_t subProp; +@property(readonly) uintptr_t subInstanceOnlyProp; +@property(class,readonly) uintptr_t subClassOnlyProp; +@end + +@implementation Sub +@synthesize subProp = subIvar; ++(uintptr_t)subProp { return 'a'; } ++(uintptr_t)subClassOnlyProp { return 'a'; } +-(uintptr_t)subInstanceOnlyProp { return 'a'; } + +-(uintptr_t)subProtoProp { return 'a'; } ++(uintptr_t)subProtoProp { return 'a'; } ++(uintptr_t)subClassOnlyProtoProp { return 'a'; } +-(uintptr_t)subInstanceOnlyProtoProp { return 'a'; } +@end + + +void test(Class subcls) +{ + objc_property_t prop; + + Class supercls = class_getSuperclass(subcls); + + prop = class_getProperty(subcls, "subProp"); + testassert(prop); + + prop = class_getProperty(subcls, "subProtoProp"); + testassert(prop); + + prop = class_getProperty(supercls, "superProp"); + testassert(prop); + testassert(prop == class_getProperty(subcls, "superProp")); + + prop = class_getProperty(supercls, "superProtoProp"); + testassert(prop); + // These are distinct because Sub adopts SuperProto itself + // in addition to Super's adoption of SuperProto. + testassert(prop != class_getProperty(subcls, "superProtoProp")); + + prop = class_getProperty(supercls, "subProp"); + testassert(!prop); + + prop = class_getProperty(supercls, "subProtoProp"); + testassert(!prop); + + testassert(nil == class_getProperty(nil, "foo")); + testassert(nil == class_getProperty(subcls, nil)); + testassert(nil == class_getProperty(nil, nil)); +} + + +int main() +{ + Class subcls = [Sub class]; + Class submeta = object_getClass(subcls); + objc_property_t prop; + + // instance properties + test(subcls); + + // class properties + test(submeta); + + // properties must not appear on the wrong side + testassert(nil == class_getProperty(subcls, "subClassOnlyProp")); + testassert(nil == class_getProperty(submeta, "subInstanceOnlyProp")); + testassert(nil == class_getProperty(subcls, "subClassOnlyProtoProp")); + testassert(nil == class_getProperty(submeta, "subInstanceOnlyProtoProp")); + + // properties with the same name on both sides are distinct + testassert(class_getProperty(subcls, "subProp") != class_getProperty(submeta, "subProp")); + testassert(class_getProperty(subcls, "superProp") != class_getProperty(submeta, "superProp")); + testassert(class_getProperty(subcls, "subProtoProp") != class_getProperty(submeta, "subProtoProp")); + testassert(class_getProperty(subcls, "superProtoProp") != class_getProperty(submeta, "superProtoProp")); + + // protocol properties + + prop = protocol_getProperty(@protocol(SubProto), "subProtoProp", YES, YES); + testassert(prop); + + prop = protocol_getProperty(@protocol(SuperProto), "superProtoProp", YES, YES); + testassert(prop == protocol_getProperty(@protocol(SubProto), "superProtoProp", YES, YES)); + + prop = protocol_getProperty(@protocol(SuperProto), "subProtoProp", YES, YES); + testassert(!prop); + + // protocol properties must not appear on the wrong side + testassert(nil == protocol_getProperty(@protocol(SubProto), "subClassOnlyProtoProp", YES, YES)); + testassert(nil == protocol_getProperty(@protocol(SubProto), "subInstanceOnlyProtoProp", YES, NO)); + + // protocol properties with the same name on both sides are distinct + testassert(protocol_getProperty(@protocol(SubProto), "subProtoProp", YES, YES) != protocol_getProperty(@protocol(SubProto), "subProtoProp", YES, NO)); + testassert(protocol_getProperty(@protocol(SubProto), "superProtoProp", YES, YES) != protocol_getProperty(@protocol(SubProto), "superProtoProp", YES, NO)); + + testassert(nil == protocol_getProperty(nil, "foo", YES, YES)); + testassert(nil == protocol_getProperty(@protocol(SuperProto), nil, YES, YES)); + testassert(nil == protocol_getProperty(nil, nil, YES, YES)); + testassert(nil == protocol_getProperty(@protocol(SuperProto), "superProtoProp", NO, YES)); + testassert(nil == protocol_getProperty(@protocol(SuperProto), "superProtoProp", NO, NO)); + + succeed(__FILE__); + return 0; +} diff --git a/test/propertyDesc.m b/test/propertyDesc.m new file mode 100644 index 0000000..87d3e65 --- /dev/null +++ b/test/propertyDesc.m @@ -0,0 +1,334 @@ +/* +TEST_BUILD_OUTPUT +.*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*propertyDesc.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +END +*/ + +#include "test.h" +#include "testroot.i" +#include +#include +#include + +struct objc_property { + const char *name; + const char *attr; +}; + +#define checkattrlist(attrs, attrcount, target) \ + do { \ + if (target > 0) { \ + testassert(attrs); \ + testassert(attrcount == target); \ + testassert(malloc_size(attrs) >= \ + (1+target) * sizeof(objc_property_attribute_t)); \ + testassert(attrs[target].name == NULL); \ + testassert(attrs[target].value == NULL); \ + } else { \ + testassert(!attrs); \ + testassert(attrcount == 0); \ + } \ + } while (0) + +#define checkattr(attrs, i, n, v) \ + do { \ + char *attrsstart = (char *)attrs; \ + char *attrsend = (char *)attrs + malloc_size(attrs); \ + testassert((char*)(attrs+i+1) <= attrsend); \ + testassert(attrs[i].name >= attrsstart); \ + testassert(attrs[i].value >= attrsstart); \ + testassert(attrs[i].name + strlen(attrs[i].name) + 1 <= attrsend); \ + testassert(attrs[i].value + strlen(attrs[i].value) + 1 <= attrsend); \ + if (n) testassert(0 == strcmp(attrs[i].name, n)); \ + else testassert(attrs[i].name == NULL); \ + if (v) testassert(0 == strcmp(attrs[i].value, v)); \ + else testassert(attrs[i].value == NULL); \ + } while (0) + +int main() +{ + char *value; + objc_property_attribute_t *attrs; + unsigned int attrcount; + + // STRING TO ATTRIBUTE LIST (property_copyAttributeList) + + struct objc_property prop; + prop.name = "test"; + + // null property + attrcount = 42; + attrs = property_copyAttributeList(NULL, &attrcount); + testassert(!attrs); + testassert(attrcount == 0); + attrs = property_copyAttributeList(NULL, NULL); + testassert(!attrs); + + // null attributes + attrcount = 42; + prop.attr = NULL; + attrs = property_copyAttributeList(&prop, &attrcount); + checkattrlist(attrs, attrcount, 0); + attrs = property_copyAttributeList(&prop, NULL); + testassert(!attrs); + + // empty attributes + attrcount = 42; + prop.attr = ""; + attrs = property_copyAttributeList(&prop, &attrcount); + checkattrlist(attrs, attrcount, 0); + attrs = property_copyAttributeList(&prop, NULL); + testassert(!attrs); + + // commas only + attrcount = 42; + prop.attr = ",,,"; + attrs = property_copyAttributeList(&prop, &attrcount); + checkattrlist(attrs, attrcount, 0); + attrs = property_copyAttributeList(&prop, NULL); + testassert(!attrs); + + // long and short names, with and without values + attrcount = 42; + prop.attr = "?XX,',\"?!?!\"YY,\"''''\""; + attrs = property_copyAttributeList(&prop, &attrcount); + checkattrlist(attrs, attrcount, 4); + checkattr(attrs, 0, "?", "XX"); + checkattr(attrs, 1, "'", ""); + checkattr(attrs, 2, "?!?!", "YY"); + checkattr(attrs, 3, "''''", ""); + free(attrs); + + // all recognized attributes simultaneously, values with quotes + attrcount = 42; + prop.attr = "T11,V2222,S333333\",G\"44444444,W,P,D,R,N,C,&"; + attrs = property_copyAttributeList(&prop, &attrcount); + checkattrlist(attrs, attrcount, 11); + checkattr(attrs, 0, "T", "11"); + checkattr(attrs, 1, "V", "2222"); + checkattr(attrs, 2, "S", "333333\""); + checkattr(attrs, 3, "G", "\"44444444"); + checkattr(attrs, 4, "W", ""); + checkattr(attrs, 5, "P", ""); + checkattr(attrs, 6, "D", ""); + checkattr(attrs, 7, "R", ""); + checkattr(attrs, 8, "N", ""); + checkattr(attrs, 9, "C", ""); + checkattr(attrs,10, "&", ""); + free(attrs); + + // kitchen sink + attrcount = 42; + prop.attr = "W,T11,P,?XX,D,V2222,R,',N,S333333\",C,\"?!?!\"YY,&,G\"44444444,\"''''\""; + attrs = property_copyAttributeList(&prop, &attrcount); + checkattrlist(attrs, attrcount, 15); + checkattr(attrs, 0, "W", ""); + checkattr(attrs, 1, "T", "11"); + checkattr(attrs, 2, "P", ""); + checkattr(attrs, 3, "?", "XX"); + checkattr(attrs, 4, "D", ""); + checkattr(attrs, 5, "V", "2222"); + checkattr(attrs, 6, "R", ""); + checkattr(attrs, 7, "'", ""); + checkattr(attrs, 8, "N", ""); + checkattr(attrs, 9, "S", "333333\""); + checkattr(attrs,10, "C", ""); + checkattr(attrs,11, "?!?!", "YY"); + checkattr(attrs,12, "&", ""); + checkattr(attrs,13, "G", "\"44444444"); + checkattr(attrs,14, "''''", ""); + free(attrs); + + // SEARCH ATTRIBUTE LIST (property_copyAttributeValue) + + // null property, null name, empty name + value = property_copyAttributeValue(NULL, NULL); + testassert(!value); + value = property_copyAttributeValue(NULL, "foo"); + testassert(!value); + value = property_copyAttributeValue(NULL, ""); + testassert(!value); + value = property_copyAttributeValue(&prop, NULL); + testassert(!value); + value = property_copyAttributeValue(&prop, ""); + testassert(!value); + + // null attributes, empty attributes + prop.attr = NULL; + value = property_copyAttributeValue(&prop, "foo"); + testassert(!value); + prop.attr = ""; + value = property_copyAttributeValue(&prop, "foo"); + testassert(!value); + + // long and short names, with and without values + prop.attr = "?XX,',\"?!?!\"YY,\"''''\""; + value = property_copyAttributeValue(&prop, "missing"); + testassert(!value); + value = property_copyAttributeValue(&prop, "X"); + testassert(!value); + value = property_copyAttributeValue(&prop, "\""); + testassert(!value); + value = property_copyAttributeValue(&prop, "'''"); + testassert(!value); + value = property_copyAttributeValue(&prop, "'''''"); + testassert(!value); + + value = property_copyAttributeValue(&prop, "?"); + testassert(0 == strcmp(value, "XX")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "'"); + testassert(0 == strcmp(value, "")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "?!?!"); + testassert(0 == strcmp(value, "YY")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "''''"); + testassert(0 == strcmp(value, "")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + + // all recognized attributes simultaneously, values with quotes + prop.attr = "T11,V2222,S333333\",G\"44444444,W,P,D,R,N,C,&"; + value = property_copyAttributeValue(&prop, "T"); + testassert(0 == strcmp(value, "11")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "V"); + testassert(0 == strcmp(value, "2222")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "S"); + testassert(0 == strcmp(value, "333333\"")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "G"); + testassert(0 == strcmp(value, "\"44444444")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "W"); + testassert(0 == strcmp(value, "")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "P"); + testassert(0 == strcmp(value, "")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "D"); + testassert(0 == strcmp(value, "")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "R"); + testassert(0 == strcmp(value, "")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "N"); + testassert(0 == strcmp(value, "")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "C"); + testassert(0 == strcmp(value, "")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + value = property_copyAttributeValue(&prop, "&"); + testassert(0 == strcmp(value, "")); + testassert(malloc_size(value) >= 1 + strlen(value)); + free(value); + + // ATTRIBUTE LIST TO STRING (class_addProperty) + + BOOL ok; + objc_property_t prop2; + + // null name + ok = class_addProperty([TestRoot class], NULL, (objc_property_attribute_t *)1, 1); + testassert(!ok); + + // null description + ok = class_addProperty([TestRoot class], "test-null-desc", NULL, 0); + testassert(ok); + prop2 = class_getProperty([TestRoot class], "test-null-desc"); + testassert(prop2); + testassert(0 == strcmp(property_getAttributes(prop2), "")); + + // empty description + ok = class_addProperty([TestRoot class], "test-empty-desc", (objc_property_attribute_t*)1, 0); + testassert(ok); + prop2 = class_getProperty([TestRoot class], "test-empty-desc"); + testassert(prop2); + testassert(0 == strcmp(property_getAttributes(prop2), "")); + + // long and short names, with and without values + objc_property_attribute_t attrs2[] = { + { "!", NULL }, + { "?", "XX" }, + { "'", "" }, + { "?!?!", "YY" }, + { "''''", "" } + }; + ok = class_addProperty([TestRoot class], "test-unrecognized", attrs2, 5); + testassert(ok); + prop2 = class_getProperty([TestRoot class], "test-unrecognized"); + testassert(prop2); + testassert(0 == strcmp(property_getAttributes(prop2), "?XX,',\"?!?!\"YY,\"''''\"")); + + // all recognized attributes simultaneously, values with quotes + objc_property_attribute_t attrs3[] = { + { "&", "" }, + { "C", "" }, + { "N", "" }, + { "R", "" }, + { "D", "" }, + { "P", "" }, + { "W", "" }, + { "G", "\"44444444" }, + { "S", "333333\"" }, + { "V", "2222" }, + { "T", "11" }, + }; + ok = class_addProperty([TestRoot class], "test-recognized", attrs3, 11); + testassert(ok); + prop2 = class_getProperty([TestRoot class], "test-recognized"); + testassert(prop2); + testassert(0 == strcmp(property_getAttributes(prop2), + "&,C,N,R,D,P,W,G\"44444444,S333333\",V2222,T11")); + + // kitchen sink + objc_property_attribute_t attrs4[] = { + { "&", "" }, + { "C", "" }, + { "N", "" }, + { "R", "" }, + { "D", "" }, + { "P", "" }, + { "W", "" }, + { "!", NULL }, + { "G", "\"44444444" }, + { "S", "333333\"" }, + { "V", "2222" }, + { "T", "11" }, + { "?", "XX" }, + { "'", "" }, + { "?!?!", "YY" }, + { "''''", "" } + }; + ok = class_addProperty([TestRoot class], "test-sink", attrs4, 16); + testassert(ok); + prop2 = class_getProperty([TestRoot class], "test-sink"); + testassert(prop2); + testassert(0 == strcmp(property_getAttributes(prop2), + "&,C,N,R,D,P,W,G\"44444444,S333333\",V2222,T11," + "?XX,',\"?!?!\"YY,\"''''\"")); + + succeed(__FILE__); +} diff --git a/test/protocol.m b/test/protocol.m new file mode 100644 index 0000000..1e24ad2 --- /dev/null +++ b/test/protocol.m @@ -0,0 +1,281 @@ +// TEST_CFLAGS -framework Foundation -Wno-deprecated-declarations +// need Foundation to get NSObject compatibility additions for class Protocol +// because ARC calls [protocol retain] +/* +TEST_BUILD_OUTPUT +.*protocol.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +END +*/ + +#include "test.h" +#include "testroot.i" +#include +#include +#include + +@protocol Proto1 ++(id)proto1ClassMethod; +-(id)proto1InstanceMethod; +@end + +@protocol Proto2 ++(id)proto2ClassMethod; +-(id)proto2InstanceMethod; +@end + +@protocol Proto3 ++(id)proto3ClassMethod; +-(id)proto3InstanceMethod; +@end + +@protocol Proto4 +@property int i; +@end + +// Force some of Proto5's selectors out of address order rdar://10582325 +SEL fn(int x) { if (x) return @selector(m12:); else return @selector(m22:); } + +// This declaration order deliberately looks weird because it determines the +// selector address order on some architectures rdar://10582325 +@protocol Proto5 +-(id)m11:(id)a; +-(void)m12:(id)a; +-(int)m13:(id)a; ++(void)m22:(TestRoot*)a; ++(int)m23:(TestRoot*)a; ++(TestRoot*)m21:(TestRoot*)a; +@optional +-(id(^)(id))m31:(id(^)(id))a; +-(void)m32:(id(^)(id))a; +-(int)m33:(id(^)(id))a; ++(void)m42:(TestRoot*(^)(TestRoot*))a; ++(int)m43:(TestRoot*(^)(TestRoot*))a; ++(TestRoot*(^)(TestRoot*))m41:(TestRoot*(^)(TestRoot*))a; +@end + +@protocol Proto6 +@optional ++(TestRoot*(^)(TestRoot*))n41:(TestRoot*(^)(TestRoot*))a; +@end + +@protocol ProtoEmpty +@end + +#define SwiftV1MangledName "_TtP6Module15SwiftV1Protocol_" + +__attribute__((objc_runtime_name(SwiftV1MangledName))) +@protocol SwiftV1Protocol +@end + +@interface Super : TestRoot @end +@implementation Super ++(id)proto1ClassMethod { return self; } +-(id)proto1InstanceMethod { return self; } +@end + +@interface SubNoProtocols : Super @end +@implementation SubNoProtocols @end + +@interface SuperNoProtocols : TestRoot @end +@implementation SuperNoProtocols +@end + +@interface SubProp : Super { int i; } @end +@implementation SubProp +@synthesize i; +@end + + +int main() +{ + Class cls; + Protocol * __unsafe_unretained *list; + Protocol *protocol, *empty; + struct objc_method_description desc2; + objc_property_t *proplist; + unsigned int count; + + protocol = @protocol(Proto3); + empty = @protocol(ProtoEmpty); + testassert(protocol); + testassert(empty); + + testassert(0 == strcmp(protocol_getName(protocol), "Proto3")); + testassert(0 == strcmp(protocol_getName(empty), "ProtoEmpty")); + + testassert(class_conformsToProtocol([Super class], @protocol(Proto1))); + testassert(!class_conformsToProtocol([SubProp class], @protocol(Proto1))); + testassert(class_conformsToProtocol([SubProp class], @protocol(Proto4))); + testassert(!class_conformsToProtocol([SubProp class], @protocol(Proto3))); + testassert(!class_conformsToProtocol([Super class], @protocol(Proto3))); + + testassert(!protocol_conformsToProtocol(@protocol(Proto1), @protocol(Proto2))); + testassert(protocol_conformsToProtocol(@protocol(Proto3), @protocol(Proto2))); + testassert(!protocol_conformsToProtocol(@protocol(Proto2), @protocol(Proto3))); + + testassert(protocol_isEqual(@protocol(Proto1), @protocol(Proto1))); + testassert(! protocol_isEqual(@protocol(Proto1), @protocol(Proto2))); + + desc2 = protocol_getMethodDescription(protocol, @selector(proto3InstanceMethod), YES, YES); + testassert(desc2.name && desc2.types); + testassert(desc2.name == @selector(proto3InstanceMethod)); + desc2 = protocol_getMethodDescription(protocol, @selector(proto3ClassMethod), YES, NO); + testassert(desc2.name && desc2.types); + testassert(desc2.name == @selector(proto3ClassMethod)); + desc2 = protocol_getMethodDescription(protocol, @selector(proto2ClassMethod), YES, NO); + testassert(desc2.name && desc2.types); + testassert(desc2.name == @selector(proto2ClassMethod)); + + desc2 = protocol_getMethodDescription(protocol, @selector(proto3ClassMethod), YES, YES); + testassert(!desc2.name && !desc2.types); + desc2 = protocol_getMethodDescription(protocol, @selector(proto3InstanceMethod), YES, NO); + testassert(!desc2.name && !desc2.types); + desc2 = protocol_getMethodDescription(empty, @selector(proto3ClassMethod), YES, YES); + testassert(!desc2.name && !desc2.types); + desc2 = protocol_getMethodDescription(empty, @selector(proto3InstanceMethod), YES, NO); + testassert(!desc2.name && !desc2.types); + + count = 100; + list = protocol_copyProtocolList(@protocol(Proto2), &count); + testassert(!list); + testassert(count == 0); + count = 100; + list = protocol_copyProtocolList(@protocol(Proto3), &count); + testassert(list); + testassert(count == 1); + testassert(protocol_isEqual(list[0], @protocol(Proto2))); + testassert(!list[1]); + free(list); + + count = 100; + cls = objc_getClass("Super"); + testassert(cls); + list = class_copyProtocolList(cls, &count); + testassert(list); + testassert(list[count] == NULL); + testassert(count == 1); + testassert(0 == strcmp(protocol_getName(list[0]), "Proto1")); + free(list); + + count = 100; + cls = objc_getClass("SuperNoProtocols"); + testassert(cls); + list = class_copyProtocolList(cls, &count); + testassert(!list); + testassert(count == 0); + + count = 100; + cls = objc_getClass("SubNoProtocols"); + testassert(cls); + list = class_copyProtocolList(cls, &count); + testassert(!list); + testassert(count == 0); + + + cls = objc_getClass("SuperNoProtocols"); + testassert(cls); + list = class_copyProtocolList(cls, NULL); + testassert(!list); + + cls = objc_getClass("Super"); + testassert(cls); + list = class_copyProtocolList(cls, NULL); + testassert(list); + free(list); + + count = 100; + list = class_copyProtocolList(NULL, &count); + testassert(!list); + testassert(count == 0); + + + // Check property added by protocol + cls = objc_getClass("SubProp"); + testassert(cls); + + count = 100; + list = class_copyProtocolList(cls, &count); + testassert(list); + testassert(count == 1); + testassert(0 == strcmp(protocol_getName(list[0]), "Proto4")); + testassert(list[1] == NULL); + free(list); + + count = 100; + proplist = class_copyPropertyList(cls, &count); + testassert(proplist); + testassert(count == 1); + testassert(0 == strcmp(property_getName(proplist[0]), "i")); + testassert(proplist[1] == NULL); + free(proplist); + + // Check extended type encodings + testassert(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(DoesNotExist), true, true) == NULL); + testassert(_protocol_getMethodTypeEncoding(NULL, @selector(m11), true, true) == NULL); + testassert(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m11), true, false) == NULL); + testassert(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m11), false, false) == NULL); + testassert(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m11), false, true) == NULL); + testassert(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m21), true, true) == NULL); +#if __LP64__ + const char *types11 = "@24@0:8@\"\"16"; + const char *types12 = "v24@0:8@\"\"16"; + const char *types13 = "i24@0:8@\"\"16"; + const char *types21 = "@\"TestRoot\"24@0:8@\"TestRoot\"16"; + const char *types22 = "v24@0:8@\"TestRoot\"16"; + const char *types23 = "i24@0:8@\"TestRoot\"16"; + const char *types31 = "@?<@@?@>24@0:8@?<@\"\"@?@\"\">16"; + const char *types32 = "v24@0:8@?<@\"\"@?@\"\">16"; + const char *types33 = "i24@0:8@?<@\"\"@?@\"\">16"; + const char *types41 = "@?<@\"TestRoot\"@?@\"TestRoot\">24@0:8@?<@\"TestRoot\"@?@\"TestRoot\">16"; + const char *types42 = "v24@0:8@?<@\"TestRoot\"@?@\"TestRoot\">16"; + const char *types43 = "i24@0:8@?<@\"TestRoot\"@?@\"TestRoot\">16"; +#else + const char *types11 = "@12@0:4@\"\"8"; + const char *types12 = "v12@0:4@\"\"8"; + const char *types13 = "i12@0:4@\"\"8"; + const char *types21 = "@\"TestRoot\"12@0:4@\"TestRoot\"8"; + const char *types22 = "v12@0:4@\"TestRoot\"8"; + const char *types23 = "i12@0:4@\"TestRoot\"8"; + const char *types31 = "@?<@@?@>12@0:4@?<@\"\"@?@\"\">8"; + const char *types32 = "v12@0:4@?<@\"\"@?@\"\">8"; + const char *types33 = "i12@0:4@?<@\"\"@?@\"\">8"; + const char *types41 = "@?<@\"TestRoot\"@?@\"TestRoot\">12@0:4@?<@\"TestRoot\"@?@\"TestRoot\">8"; + const char *types42 = "v12@0:4@?<@\"TestRoot\"@?@\"TestRoot\">8"; + const char *types43 = "i12@0:4@?<@\"TestRoot\"@?@\"TestRoot\">8"; +#endif + + // Make sure some of Proto5's selectors are out of order rdar://10582325 + // These comparisons deliberately look weird because they determine the + // selector order on some architectures. + testassert(sel_registerName("m11:") > sel_registerName("m12:") || + sel_registerName("m21:") > sel_registerName("m22:") || + sel_registerName("m32:") < sel_registerName("m31:") || + sel_registerName("m42:") < sel_registerName("m41:") ); + + if (!_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m11:), true, true)) { + fail("rdar://10492418 extended type encodings not present (is compiler old?)"); + } else { + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m11:), true, true), types11)); + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m12:), true, true), types12)); + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m13:), true, true), types13)); + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m21:), true, false), types21)); + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m22:), true, false), types22)); + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m23:), true, false), types23)); + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m31:), false, true), types31)); + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m32:), false, true), types32)); + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m33:), false, true), types33)); + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m41:), false, false), types41)); + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m42:), false, false), types42)); + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto5), @selector(m43:), false, false), types43)); + + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto6), @selector(n41:), false, false), types41)); + testassert(0 == strcmp(_protocol_getMethodTypeEncoding(@protocol(Proto6), @selector(m41:), false, false), types41)); + } + + testassert(@protocol(SwiftV1Protocol) == objc_getProtocol("Module.SwiftV1Protocol")); + testassert(@protocol(SwiftV1Protocol) == objc_getProtocol(SwiftV1MangledName)); + testassert(0 == strcmp(protocol_getName(@protocol(SwiftV1Protocol)), "Module.SwiftV1Protocol")); + testassert(!objc_getProtocol("SwiftV1Protocol")); + + succeed(__FILE__); +} diff --git a/test/protocol_copyMethodList.m b/test/protocol_copyMethodList.m new file mode 100644 index 0000000..2b5e089 --- /dev/null +++ b/test/protocol_copyMethodList.m @@ -0,0 +1,154 @@ +// TEST_CFLAGS -framework Foundation +// need Foundation to get NSObject compatibility additions for class Protocol +// because ARC calls [protocol retain] +/* +TEST_BUILD_OUTPUT +.*protocol_copyMethodList.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*protocol_copyMethodList.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*protocol_copyMethodList.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*protocol_copyMethodList.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +END +*/ + + +#include "test.h" +#include +#include + +@protocol SuperMethods ++(void)SuperMethodClass; ++(void)SuperMethodClass2; +-(void)SuperMethodInstance; +-(void)SuperMethodInstance2; +@end + +@protocol SubMethods ++(void)SubMethodClass; ++(void)SubMethodClass2; +-(void)SubMethodInstance; +-(void)SubMethodInstance2; +@end + +@protocol SuperOptionalMethods +@optional ++(void)SuperOptMethodClass; ++(void)SuperOptMethodClass2; +-(void)SuperOptMethodInstance; +-(void)SuperOptMethodInstance2; +@end + +@protocol SubOptionalMethods +@optional ++(void)SubOptMethodClass; ++(void)SubOptMethodClass2; +-(void)SubOptMethodInstance; +-(void)SubOptMethodInstance2; +@end + +@protocol NoMethods @end + +static int isNamed(struct objc_method_description m, const char *name) +{ + return (m.name == sel_registerName(name)); +} + +int main() +{ + struct objc_method_description *methods; + unsigned int count; + Protocol *proto; + + proto = @protocol(SubMethods); + testassert(proto); + + // Check required methods + count = 999; + methods = protocol_copyMethodDescriptionList(proto, YES, YES, &count); + testassert(methods); + testassert(count == 2); + testassert((isNamed(methods[0], "SubMethodInstance") && + isNamed(methods[1], "SubMethodInstance2")) + || + (isNamed(methods[1], "SubMethodInstance") && + isNamed(methods[0], "SubMethodInstance2"))); + free(methods); + + count = 999; + methods = protocol_copyMethodDescriptionList(proto, YES, NO, &count); + testassert(methods); + testassert(count == 2); + testassert((isNamed(methods[0], "SubMethodClass") && + isNamed(methods[1], "SubMethodClass2")) + || + (isNamed(methods[1], "SubMethodClass") && + isNamed(methods[0], "SubMethodClass2"))); + free(methods); + + // Check lack of optional methods + count = 999; + methods = protocol_copyMethodDescriptionList(proto, NO, YES, &count); + testassert(!methods); + testassert(count == 0); + count = 999; + methods = protocol_copyMethodDescriptionList(proto, NO, NO, &count); + testassert(!methods); + testassert(count == 0); + + + proto = @protocol(SubOptionalMethods); + testassert(proto); + + // Check optional methods + count = 999; + methods = protocol_copyMethodDescriptionList(proto, NO, YES, &count); + testassert(methods); + testassert(count == 2); + testassert((isNamed(methods[0], "SubOptMethodInstance") && + isNamed(methods[1], "SubOptMethodInstance2")) + || + (isNamed(methods[1], "SubOptMethodInstance") && + isNamed(methods[0], "SubOptMethodInstance2"))); + free(methods); + + count = 999; + methods = protocol_copyMethodDescriptionList(proto, NO, NO, &count); + testassert(methods); + testassert(count == 2); + testassert((isNamed(methods[0], "SubOptMethodClass") && + isNamed(methods[1], "SubOptMethodClass2")) + || + (isNamed(methods[1], "SubOptMethodClass") && + isNamed(methods[0], "SubOptMethodClass2"))); + free(methods); + + // Check lack of required methods + count = 999; + methods = protocol_copyMethodDescriptionList(proto, YES, YES, &count); + testassert(!methods); + testassert(count == 0); + count = 999; + methods = protocol_copyMethodDescriptionList(proto, YES, NO, &count); + testassert(!methods); + testassert(count == 0); + + + // Check NULL protocol parameter + count = 999; + methods = protocol_copyMethodDescriptionList(NULL, YES, YES, &count); + testassert(!methods); + testassert(count == 0); + count = 999; + methods = protocol_copyMethodDescriptionList(NULL, YES, NO, &count); + testassert(!methods); + testassert(count == 0); + count = 999; + methods = protocol_copyMethodDescriptionList(NULL, NO, YES, &count); + testassert(!methods); + testassert(count == 0); + count = 999; + methods = protocol_copyMethodDescriptionList(NULL, NO, NO, &count); + testassert(!methods); + testassert(count == 0); + + succeed(__FILE__); +} diff --git a/test/protocol_copyPropertyList.m b/test/protocol_copyPropertyList.m new file mode 100644 index 0000000..a9ecd22 --- /dev/null +++ b/test/protocol_copyPropertyList.m @@ -0,0 +1,207 @@ +// TEST_CFLAGS -framework Foundation +// need Foundation to get NSObject compatibility additions for class Protocol +// because ARC calls [protocol retain] +/* +TEST_BUILD_OUTPUT +.*protocol_copyPropertyList.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*protocol_copyPropertyList.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*protocol_copyPropertyList.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +.*protocol_copyPropertyList.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +END +*/ + +#include "test.h" +#include +#include +#include + +@protocol SuperProps +@property int prop1; +@property int prop2; +@property(class) int prop1; +@property(class) int prop2; +@end + +@protocol SubProps +@property int prop3; +@property int prop4; +@property(class) int prop3; +@property(class) int prop4; +@end + + +@protocol FourProps +@property int prop1; +@property int prop2; +@property int prop3; +@property int prop4; + +@property(class) int prop1; +@property(class) int prop2; +@property(class) int prop3; +@property(class) int prop4; +@end + +@protocol NoProps @end + +@protocol OneProp +@property int instanceProp; +@property(class) int classProp; +@end + + +static int isNamed(objc_property_t p, const char *name) +{ + return (0 == strcmp(name, property_getName(p))); +} + +void testfn(objc_property_t *(*copyPropertyList_fn)(Protocol*, unsigned int *), + const char *onePropName) +{ + objc_property_t *props; + unsigned int count; + Protocol *proto; + + proto = @protocol(SubProps); + testassert(proto); + + count = 100; + props = copyPropertyList_fn(proto, &count); + testassert(props); + testassert(count == 2); + testassert((isNamed(props[0], "prop4") && isNamed(props[1], "prop3")) || + (isNamed(props[0], "prop3") && isNamed(props[1], "prop4"))); + // props[] should be null-terminated + testassert(props[2] == NULL); + free(props); + + proto = @protocol(SuperProps); + testassert(proto); + + count = 100; + props = copyPropertyList_fn(proto, &count); + testassert(props); + testassert(count == 2); + testassert((isNamed(props[0], "prop1") && isNamed(props[1], "prop2")) || + (isNamed(props[0], "prop2") && isNamed(props[1], "prop1"))); + // props[] should be null-terminated + testassert(props[2] == NULL); + free(props); + + // Check null-termination - this property list block would be 16 bytes + // if it weren't for the terminator + proto = @protocol(FourProps); + testassert(proto); + + count = 100; + props = copyPropertyList_fn(proto, &count); + testassert(props); + testassert(count == 4); + testassert(malloc_size(props) >= 5 * sizeof(objc_property_t)); + testassert(props[3] != NULL); + testassert(props[4] == NULL); + free(props); + + // Check NULL count parameter + props = copyPropertyList_fn(proto, NULL); + testassert(props); + testassert(props[4] == NULL); + testassert(props[3] != NULL); + free(props); + + // Check NULL protocol parameter + count = 100; + props = copyPropertyList_fn(NULL, &count); + testassert(!props); + testassert(count == 0); + + // Check NULL protocol and count + props = copyPropertyList_fn(NULL, NULL); + testassert(!props); + + // Check protocol with no properties + proto = @protocol(NoProps); + testassert(proto); + + count = 100; + props = copyPropertyList_fn(proto, &count); + testassert(!props); + testassert(count == 0); + + // Check instance vs class properties + proto = @protocol(OneProp); + testassert(proto); + + count = 100; + props = copyPropertyList_fn(proto, &count); + testassert(props); + testassert(count == 1); + testassert(0 == strcmp(property_getName(props[0]), onePropName)); + free(props); +} + +objc_property_t *protocol_copyPropertyList2_YES_YES(Protocol *proto, unsigned int *outCount) +{ + return protocol_copyPropertyList2(proto, outCount, YES, YES); +} + +objc_property_t *protocol_copyPropertyList2_YES_NO(Protocol *proto, unsigned int *outCount) +{ + return protocol_copyPropertyList2(proto, outCount, YES, NO); +} + +int main() +{ + // protocol_copyPropertyList(...) is identical to + // protocol_copyPropertyList2(..., YES, YES) + testfn(protocol_copyPropertyList, "instanceProp"); + testfn(protocol_copyPropertyList2_YES_YES, "instanceProp"); + + // protocol_copyPropertyList2(..., YES, NO) is also identical + // with the protocol definitions above, except for protocol OneProp. + testfn(protocol_copyPropertyList2_YES_NO, "classProp"); + + // Check non-functionality of optional properties + + unsigned int count; + objc_property_t *props; + + count = 100; + props = protocol_copyPropertyList2(@protocol(FourProps), &count, NO, YES); + testassert(!props); + testassert(count == 0); + + count = 100; + props = protocol_copyPropertyList2(@protocol(FourProps), &count, NO, NO); + testassert(!props); + testassert(count == 0); + + // Check nil count parameter + props = protocol_copyPropertyList2(@protocol(FourProps), nil, NO, YES); + testassert(!props); + + props = protocol_copyPropertyList2(@protocol(FourProps), nil, NO, NO); + testassert(!props); + + // Check nil protocol parameter + count = 100; + props = protocol_copyPropertyList2(nil, &count, NO, YES); + testassert(!props); + testassert(count == 0); + + count = 100; + props = protocol_copyPropertyList2(nil, &count, NO, NO); + testassert(!props); + testassert(count == 0); + + // Check nil protocol and count + props = protocol_copyPropertyList2(nil, nil, NO, YES); + testassert(!props); + + props = protocol_copyPropertyList2(nil, nil, NO, NO); + testassert(!props); + + + succeed(__FILE__); + return 0; +} diff --git a/test/rawisa.m b/test/rawisa.m new file mode 100644 index 0000000..4e30cb3 --- /dev/null +++ b/test/rawisa.m @@ -0,0 +1,30 @@ +/* +TEST_CFLAGS -Xlinker -sectcreate -Xlinker __DATA -Xlinker __objc_rawisa -Xlinker /dev/null +TEST_ENV OBJC_PRINT_RAW_ISA=YES + +TEST_RUN_OUTPUT +objc\[\d+\]: RAW ISA: disabling non-pointer isa because the app has a __DATA,__objc_rawisa section +(.* RAW ISA: .*\n)* +OK: rawisa.m(\n.* RAW ISA: .*)* +OR +(.* RAW ISA: .*\n)* +no __DATA,__rawisa support +OK: rawisa.m(\n.* RAW ISA: .*)* +END + +"RAW ISA" is allowed after "OK" because of static destructors +that provoke class realization. +*/ + +#include "test.h" + +int main() +{ + fprintf(stderr, "\n"); +#if ! (SUPPORT_NONPOINTER_ISA && TARGET_OS_OSX) + // only 64-bit Mac supports this + fprintf(stderr, "no __DATA,__rawisa support\n"); +#endif + succeed(__FILE__); +} + diff --git a/test/readClassPair.m b/test/readClassPair.m new file mode 100644 index 0000000..80313b2 --- /dev/null +++ b/test/readClassPair.m @@ -0,0 +1,82 @@ +/* +TEST_RUN_OUTPUT +objc\[\d+\]: Class Sub is implemented in both [^\s]+ \(0x[0-9a-f]+\) and [^\s]+ \(0x[0-9a-f]+\)\. One of the two will be used\. Which one is undefined\. +OK: readClassPair.m +END + */ + +#include "test.h" +#include + +// Reuse evil-class-def.m as a non-evil class definition. + +#define EVIL_SUPER 0 +#define EVIL_SUPER_META 0 +#define EVIL_SUB 0 +#define EVIL_SUB_META 0 + +#define OMIT_SUPER 1 +#define OMIT_NL_SUPER 1 +#define OMIT_SUB 1 +#define OMIT_NL_SUB 1 + +#include "evil-class-def.m" + +int main() +{ + // This definition is ABI and is never allowed to change. + testassert(OBJC_MAX_CLASS_SIZE == 32*sizeof(void*)); + + struct objc_image_info ii = { 0, 0 }; + + // Read a root class. + testassert(!objc_getClass("Super")); + + extern intptr_t OBJC_CLASS_$_Super[OBJC_MAX_CLASS_SIZE/sizeof(void*)]; + Class Super = objc_readClassPair((__bridge Class)(void*)&OBJC_CLASS_$_Super, &ii); + testassert(Super); + + testassert(objc_getClass("Super") == Super); + testassert(0 == strcmp(class_getName(Super), "Super")); + testassert(class_getSuperclass(Super) == nil); + testassert(class_getClassMethod(Super, @selector(load))); + testassert(class_getInstanceMethod(Super, @selector(load))); + testassert(class_getInstanceVariable(Super, "super_ivar")); + testassert(class_getInstanceSize(Super) == sizeof(void*)); + [Super load]; + + // Read a non-root class. + testassert(!objc_getClass("Sub")); + + extern intptr_t OBJC_CLASS_$_Sub[OBJC_MAX_CLASS_SIZE/sizeof(void*)]; + // Make a duplicate of class Sub for use later. + intptr_t Sub2_buf[OBJC_MAX_CLASS_SIZE/sizeof(void*)]; + memcpy(Sub2_buf, &OBJC_CLASS_$_Sub, sizeof(Sub2_buf)); + Class Sub = objc_readClassPair((__bridge Class)(void*)&OBJC_CLASS_$_Sub, &ii); + testassert(Sub); + + testassert(0 == strcmp(class_getName(Sub), "Sub")); + testassert(objc_getClass("Sub") == Sub); + testassert(class_getSuperclass(Sub) == Super); + testassert(class_getClassMethod(Sub, @selector(load))); + testassert(class_getInstanceMethod(Sub, @selector(load))); + testassert(class_getInstanceVariable(Sub, "sub_ivar")); + testassert(class_getInstanceSize(Sub) == 2*sizeof(void*)); + [Sub load]; + + // Reading a class whose name already exists succeeds + // with a duplicate warning. + Class Sub2 = objc_readClassPair((__bridge Class)(void*)Sub2_buf, &ii); + testassert(Sub2); + testassert(Sub2 != Sub); + testassert(objc_getClass("Sub") == Sub); // didn't change + testassert(0 == strcmp(class_getName(Sub2), "Sub")); + testassert(class_getSuperclass(Sub2) == Super); + testassert(class_getClassMethod(Sub2, @selector(load))); + testassert(class_getInstanceMethod(Sub2, @selector(load))); + testassert(class_getInstanceVariable(Sub2, "sub_ivar")); + testassert(class_getInstanceSize(Sub2) == 2*sizeof(void*)); + [Sub2 load]; + + succeed(__FILE__); +} diff --git a/test/realizedClassGenerationCount.m b/test/realizedClassGenerationCount.m new file mode 100644 index 0000000..72f0882 --- /dev/null +++ b/test/realizedClassGenerationCount.m @@ -0,0 +1,29 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" + +#include + +extern uintptr_t objc_debug_realized_class_generation_count; + +int main() +{ + testassert(objc_debug_realized_class_generation_count > 0); + uintptr_t prev = objc_debug_realized_class_generation_count; + + void *handle = dlopen("/System/Library/Frameworks/Foundation.framework/Foundation", RTLD_LAZY); + testassert(handle); + Class c = objc_getClass("NSFileManager"); + testassert(c); + testassert(objc_debug_realized_class_generation_count > prev); + + prev = objc_debug_realized_class_generation_count; + c = objc_allocateClassPair([TestRoot class], "Dynamic", 0); + testassert(objc_debug_realized_class_generation_count > prev); + prev = objc_debug_realized_class_generation_count; + objc_registerClassPair(c); + testassert(objc_debug_realized_class_generation_count == prev); + + succeed(__FILE__); +} \ No newline at end of file diff --git a/test/release-workaround.m b/test/release-workaround.m new file mode 100644 index 0000000..5e3bfa3 --- /dev/null +++ b/test/release-workaround.m @@ -0,0 +1,34 @@ +// TEST_CONFIG ARCH=x86_64 MEM=mrc +// TEST_CFLAGS -framework Foundation + +// rdar://20206767 + +#include +#include "test.h" + + +@interface Test : NSObject @end +@implementation Test +@end + + +int main() +{ + id buf[1]; + buf[0] = [Test class]; + id obj = (id)buf; + [obj retain]; + [obj retain]; + + uintptr_t rax; + + [obj release]; + asm("mov %%rax, %0" : "=r" (rax)); + testassert(rax == 0); + + objc_release(obj); + asm("mov %%rax, %0" : "=r" (rax)); + testassert(rax == 0); + + succeed(__FILE__); +} diff --git a/test/resolve.m b/test/resolve.m new file mode 100644 index 0000000..913a686 --- /dev/null +++ b/test/resolve.m @@ -0,0 +1,298 @@ +/* resolve.m + * Test +resolveClassMethod: and +resolveInstanceMethod: + */ + +// TEST_CFLAGS -Wno-deprecated-declarations + +#include "test.h" +#include "testroot.i" +#include +#include +#include + +#if __has_feature(objc_arc) + +int main() +{ + testwarn("rdar://11368528 confused by Foundation"); + succeed(__FILE__); +} + +#else + +static int state = 0; + +@interface Super : TestRoot @end +@interface Sub : Super @end + + +@implementation Super ++(void)initialize { + if (self == [Super class]) { + testassert(state == 1); + state = 2; + } +} +@end + +static id forward_handler(id self, SEL sel) +{ + if (class_isMetaClass(object_getClass(self))) { + // self is a class object + if (sel == @selector(missingClassMethod)) { + testassert(state == 21 || state == 25 || state == 80); + if (state == 21) state = 22; + if (state == 25) state = 26; + if (state == 80) state = 81;; + return nil; + } else if (sel == @selector(lyingClassMethod)) { + testassert(state == 31 || state == 35); + if (state == 31) state = 32; + if (state == 35) state = 36; + return nil; + } + fail("+forward:: shouldn't be called with sel %s", sel_getName(sel)); + return nil; + } + else { + // self is not a class object + if (sel == @selector(missingInstanceMethod)) { + testassert(state == 61 || state == 65); + if (state == 61) state = 62; + if (state == 65) state = 66; + return nil; + } else if (sel == @selector(lyingInstanceMethod)) { + testassert(state == 71 || state == 75); + if (state == 71) state = 72; + if (state == 75) state = 76; + return nil; + } + fail("-forward:: shouldn't be called with sel %s", sel_getName(sel)); + return nil; + } +} + + +static id classMethod_c(id __unused self, SEL __unused sel) +{ + testassert(state == 4 || state == 10); + if (state == 4) state = 5; + if (state == 10) state = 11; + return [Super class]; +} + +static id instanceMethod_c(id __unused self, SEL __unused sel) +{ + testassert(state == 41 || state == 50); + if (state == 41) state = 42; + if (state == 50) state = 51; + return [Sub class]; +} + + +@implementation Sub + ++(void)method2 { } ++(void)method3 { } ++(void)method4 { } ++(void)method5 { } + ++(void)initialize { + if (self == [Sub class]) { + testassert(state == 2); + state = 3; + } +} + ++(BOOL)resolveClassMethod:(SEL)sel +{ + if (sel == @selector(classMethod)) { + testassert(state == 3); + state = 4; + class_addMethod(object_getClass(self), sel, (IMP)&classMethod_c, ""); + return YES; + } else if (sel == @selector(missingClassMethod)) { + testassert(state == 20); + state = 21; + return NO; + } else if (sel == @selector(lyingClassMethod)) { + testassert(state == 30); + state = 31; + return YES; // lie + } else { + fail("+resolveClassMethod: called incorrectly (sel %s)", + sel_getName(sel)); + return NO; + } +} + ++(BOOL)resolveInstanceMethod:(SEL)sel +{ + if (sel == @selector(instanceMethod)) { + testassert(state == 40); + state = 41; + class_addMethod(self, sel, (IMP)instanceMethod_c, ""); + return YES; + } else if (sel == @selector(missingInstanceMethod)) { + testassert(state == 60); + state = 61; + return NO; + } else if (sel == @selector(lyingInstanceMethod)) { + testassert(state == 70); + state = 71; + return YES; // lie + } else { + fail("+resolveInstanceMethod: called incorrectly (sel %s)", + sel_getName(sel)); + return NO; + } +} + +@end + +@interface Super (MissingMethods) ++(id)missingClassMethod; +@end + +@interface Sub (ResolvedMethods) ++(id)classMethod; +-(id)instanceMethod; ++(id)missingClassMethod; +-(id)missingInstanceMethod; ++(id)lyingClassMethod; +-(id)lyingInstanceMethod; +@end + + +int main() +{ + Sub *s; + id ret; + + objc_setForwardHandler((void*)&forward_handler, (void*)&abort); + + // Be ready for ARC to retain the class object and call +initialize early + state = 1; + + Class dup = objc_duplicateClass(objc_getClass("Sub"), "Sub_copy", 0); + + // Resolve a class method + // +initialize should fire first (if it hasn't already) + ret = [Sub classMethod]; + testassert(state == 5); + testassert(ret == [Super class]); + + // Call it again, cached + // Resolver shouldn't be called again. + state = 10; + ret = [Sub classMethod]; + testassert(state == 11); + testassert(ret == [Super class]); + + _objc_flush_caches(object_getClass([Sub class])); + + // Call a method that won't get resolved + state = 20; + ret = [Sub missingClassMethod]; + testassert(state == 22); + testassert(ret == nil); + + // Call it again, cached + // Resolver shouldn't be called again. + state = 25; + ret = [Sub missingClassMethod]; + testassert(state == 26); + testassert(ret == nil); + + _objc_flush_caches(object_getClass([Sub class])); + + // Call a method that won't get resolved but the resolver lies about it + state = 30; + ret = [Sub lyingClassMethod]; + testassert(state == 32); + testassert(ret == nil); + + // Call it again, cached + // Resolver shouldn't be called again. + state = 35; + ret = [Sub lyingClassMethod]; + testassert(state == 36); + testassert(ret == nil); + + _objc_flush_caches(object_getClass([Sub class])); + + + // Resolve an instance method + s = [Sub new]; + state = 40; + ret = [s instanceMethod]; + testassert(state == 42); + testassert(ret == [Sub class]); + + // Call it again, cached + // Resolver shouldn't be called again. + state = 50; + ret = [s instanceMethod]; + testassert(state == 51); + testassert(ret == [Sub class]); + + _objc_flush_caches([Sub class]); + + // Call a method that won't get resolved + state = 60; + ret = [s missingInstanceMethod]; + testassert(state == 62); + testassert(ret == nil); + + // Call it again, cached + // Resolver shouldn't be called again. + state = 65; + ret = [s missingInstanceMethod]; + testassert(state == 66); + testassert(ret == nil); + + _objc_flush_caches([Sub class]); + + // Call a method that won't get resolved but the resolver lies about it + state = 70; + ret = [s lyingInstanceMethod]; + testassert(state == 72); + testassert(ret == nil); + + // Call it again, cached + // Resolver shouldn't be called again. + state = 75; + ret = [s lyingInstanceMethod]; + testassert(state == 76); + testassert(ret == nil); + + _objc_flush_caches([Sub class]); + + // Call a missing method on a class that doesn't support resolving + state = 80; + ret = [Super missingClassMethod]; + testassert(state == 81); + testassert(ret == nil); + RELEASE_VAR(s); + + // Resolve an instance method on a class duplicated before resolving + s = [dup new]; + state = 40; + ret = [s instanceMethod]; + testassert(state == 42); + testassert(ret == [Sub class]); + + // Call it again, cached + // Resolver shouldn't be called again. + state = 50; + ret = [s instanceMethod]; + testassert(state == 51); + testassert(ret == [Sub class]); + RELEASE_VAR(s); + + succeed(__FILE__); + return 0; +} + +#endif + diff --git a/test/restartableRangesSynchronizeStress.m b/test/restartableRangesSynchronizeStress.m new file mode 100644 index 0000000..0efc7f6 --- /dev/null +++ b/test/restartableRangesSynchronizeStress.m @@ -0,0 +1,78 @@ +// TEST_CONFIG OS=macosx,iphoneos,tvos,watchos + +// This test checks that objc_msgSend's recovery path works correctly. +// It continuously runs msgSend on some background threads, then +// triggers the recovery path constantly as a stress test. + +#include "test.h" +#include "testroot.i" +#include + +struct Big { + uintptr_t a, b, c, d, e, f, g; +}; + +@interface C1: TestRoot +@end +@implementation C1 +- (id)idret { return nil; } +- (double)fpret { return 0.0; } +- (long double)lfpret { return 0.0; } +- (struct Big)stret { return (struct Big){}; } +@end + +@interface C2: C1 +@end +@implementation C2 +- (id)idret { return [super idret]; } +- (double)fpret { return [super fpret]; } +- (long double)lfpret { return [super lfpret]; } +- (struct Big)stret { return [super stret]; } +@end + +EXTERN_C kern_return_t task_restartable_ranges_synchronize(task_t task); + +EXTERN_C void sendWithMsgLookup(id self, SEL _cmd); + +#if defined(__arm64__) && !__has_feature(ptrauth_calls) +asm( +"_sendWithMsgLookup: \n" +" stp fp, lr, [sp, #-16]! \n" +" mov fp, sp \n" +" bl _objc_msgLookup \n" +" mov sp, fp \n" +" ldp fp, lr, [sp], #16 \n" +" br x17 \n" +); +#elif defined(__x86_64__) +asm( +"_sendWithMsgLookup: \n" +" pushq %rbp \n" +" movq %rsp, %rbp \n" +" callq _objc_msgLookup \n" +" popq %rbp \n" +" jmpq *%r11 \n" +); +#else +// Just skip it. +void sendWithMsgLookup(id self __unused, SEL _cmd __unused) {} +#endif + +int main() { + id obj = [C2 new]; + for(int i = 0; i < 2; i++) { + dispatch_async(dispatch_get_global_queue(0, 0), ^{ + while(1) { + [obj idret]; + [obj fpret]; + [obj lfpret]; + [obj stret]; + sendWithMsgLookup(obj, @selector(idret)); + } + }); + } + for(int i = 0; i < 1000000; i++) { + task_restartable_ranges_synchronize(mach_task_self());; + } + succeed(__FILE__); +} diff --git a/test/rr-autorelease-fast.m b/test/rr-autorelease-fast.m new file mode 100644 index 0000000..0de2b2b --- /dev/null +++ b/test/rr-autorelease-fast.m @@ -0,0 +1,357 @@ +// TEST_CONFIG MEM=mrc +// TEST_CFLAGS -Os + +#include "test.h" +#include "testroot.i" + +#include +#include +#include + +@interface TestObject : TestRoot @end +@implementation TestObject @end + + +// MAGIC and NOT_MAGIC each call two functions +// with or without the magic instruction sequence, respectively. +// +// tmp = first(obj); +// magic, or not; +// tmp = second(tmp); + +#if __arm__ + +#define NOT_MAGIC(first, second) \ + tmp = first(obj); \ + asm volatile("mov r8, r8"); \ + tmp = second(tmp); + +#define MAGIC(first, second) \ + tmp = first(obj); \ + asm volatile("mov r7, r7"); \ + tmp = second(tmp); + +// arm +#elif __arm64__ + +#define NOT_MAGIC(first, second) \ + tmp = first(obj); \ + asm volatile("mov x28, x28"); \ + tmp = second(tmp); + +#define MAGIC(first, second) \ + tmp = first(obj); \ + asm volatile("mov x29, x29"); \ + tmp = second(tmp); + +// arm64 +#elif __x86_64__ + +#define NOT_MAGIC(first, second) \ + tmp = first(obj); \ + asm volatile("nop"); \ + tmp = second(tmp); + +#define MAGIC(first, second) \ + tmp = first(obj); \ + tmp = second(tmp); + +// x86_64 +#elif __i386__ + +#define NOT_MAGIC(first, second) \ + tmp = first(obj); \ + tmp = second(tmp); + +#define MAGIC(first, second) \ + asm volatile("\n subl $16, %%esp" \ + "\n movl %[obj], (%%esp)" \ + "\n call _" #first \ + "\n" \ + "\n movl %%ebp, %%ebp" \ + "\n" \ + "\n movl %%eax, (%%esp)" \ + "\n call _" #second \ + "\n movl %%eax, %[tmp]" \ + "\n addl $16, %%esp" \ + : [tmp] "=r" (tmp) \ + : [obj] "r" (obj) \ + : "eax", "edx", "ecx", "cc", "memory") + +// i386 +#else + +#error unknown architecture + +#endif + + +int +main() +{ + TestObject *tmp, *obj; + +#ifdef __x86_64__ + // need to get DYLD to resolve the stubs on x86 + PUSH_POOL { + TestObject *warm_up = [[TestObject alloc] init]; + testassert(warm_up); + warm_up = objc_retainAutoreleasedReturnValue(warm_up); + warm_up = objc_unsafeClaimAutoreleasedReturnValue(warm_up); + [warm_up release]; + warm_up = nil; + } POP_POOL; +#endif + + testprintf(" Successful +1 -> +1 handshake\n"); + + PUSH_POOL { + obj = [[TestObject alloc] init]; + testassert(obj); + + TestRootRetain = 0; + TestRootRelease = 0; + TestRootAutorelease = 0; + TestRootDealloc = 0; + + MAGIC(objc_autoreleaseReturnValue, + objc_retainAutoreleasedReturnValue); + + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 0); + testassert(TestRootRelease == 0); + testassert(TestRootAutorelease == 0); + + [tmp release]; + testassert(TestRootDealloc == 1); + testassert(TestRootRetain == 0); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 0); + + } POP_POOL; + + testprintf("Unsuccessful +1 -> +1 handshake\n"); + + PUSH_POOL { + obj = [[TestObject alloc] init]; + testassert(obj); + + TestRootRetain = 0; + TestRootRelease = 0; + TestRootAutorelease = 0; + TestRootDealloc = 0; + + NOT_MAGIC(objc_autoreleaseReturnValue, + objc_retainAutoreleasedReturnValue); + + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 0); + testassert(TestRootAutorelease == 1); + + [tmp release]; + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 1); + + } POP_POOL; + testassert(TestRootDealloc == 1); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 2); + testassert(TestRootAutorelease == 1); + + + testprintf(" Successful +0 -> +1 handshake\n"); + + PUSH_POOL { + obj = [[TestObject alloc] init]; + testassert(obj); + + TestRootRetain = 0; + TestRootRelease = 0; + TestRootAutorelease = 0; + TestRootDealloc = 0; + + MAGIC(objc_retainAutoreleaseReturnValue, + objc_retainAutoreleasedReturnValue); + + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 0); + testassert(TestRootAutorelease == 0); + + [tmp release]; + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 0); + + [tmp release]; + testassert(TestRootDealloc == 1); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 2); + testassert(TestRootAutorelease == 0); + + } POP_POOL; + + testprintf("Unsuccessful +0 -> +1 handshake\n"); + + PUSH_POOL { + obj = [[TestObject alloc] init]; + testassert(obj); + + TestRootRetain = 0; + TestRootRelease = 0; + TestRootAutorelease = 0; + TestRootDealloc = 0; + + NOT_MAGIC(objc_retainAutoreleaseReturnValue, + objc_retainAutoreleasedReturnValue); + + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 2); + testassert(TestRootRelease == 0); + testassert(TestRootAutorelease == 1); + + [tmp release]; + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 2); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 1); + + [tmp release]; + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 2); + testassert(TestRootRelease == 2); + testassert(TestRootAutorelease == 1); + + } POP_POOL; + testassert(TestRootDealloc == 1); + testassert(TestRootRetain == 2); + testassert(TestRootRelease == 3); + testassert(TestRootAutorelease == 1); + + + testprintf(" Successful +1 -> +0 handshake\n"); + + PUSH_POOL { + obj = [[[TestObject alloc] init] retain]; + testassert(obj); + + TestRootRetain = 0; + TestRootRelease = 0; + TestRootAutorelease = 0; + TestRootDealloc = 0; + + MAGIC(objc_autoreleaseReturnValue, + objc_unsafeClaimAutoreleasedReturnValue); + + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 0); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 0); + + [tmp release]; + testassert(TestRootDealloc == 1); + testassert(TestRootRetain == 0); + testassert(TestRootRelease == 2); + testassert(TestRootAutorelease == 0); + + } POP_POOL; + + testprintf("Unsuccessful +1 -> +0 handshake\n"); + + PUSH_POOL { + obj = [[[TestObject alloc] init] retain]; + testassert(obj); + + TestRootRetain = 0; + TestRootRelease = 0; + TestRootAutorelease = 0; + TestRootDealloc = 0; + + NOT_MAGIC(objc_autoreleaseReturnValue, + objc_unsafeClaimAutoreleasedReturnValue); + + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 0); + testassert(TestRootRelease == 0); + testassert(TestRootAutorelease == 1); + + [tmp release]; + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 0); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 1); + + } POP_POOL; + testassert(TestRootDealloc == 1); + testassert(TestRootRetain == 0); + testassert(TestRootRelease == 2); + testassert(TestRootAutorelease == 1); + + + testprintf(" Successful +0 -> +0 handshake\n"); + + PUSH_POOL { + obj = [[TestObject alloc] init]; + testassert(obj); + + TestRootRetain = 0; + TestRootRelease = 0; + TestRootAutorelease = 0; + TestRootDealloc = 0; + + MAGIC(objc_retainAutoreleaseReturnValue, + objc_unsafeClaimAutoreleasedReturnValue); + + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 0); + testassert(TestRootRelease == 0); + testassert(TestRootAutorelease == 0); + + [tmp release]; + testassert(TestRootDealloc == 1); + testassert(TestRootRetain == 0); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 0); + + } POP_POOL; + + testprintf("Unsuccessful +0 -> +0 handshake\n"); + + PUSH_POOL { + obj = [[TestObject alloc] init]; + testassert(obj); + + TestRootRetain = 0; + TestRootRelease = 0; + TestRootAutorelease = 0; + TestRootDealloc = 0; + + NOT_MAGIC(objc_retainAutoreleaseReturnValue, + objc_unsafeClaimAutoreleasedReturnValue); + + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 0); + testassert(TestRootAutorelease == 1); + + [tmp release]; + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 1); + + } POP_POOL; + testassert(TestRootDealloc == 1); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 2); + testassert(TestRootAutorelease == 1); + + succeed(__FILE__); + + return 0; +} + diff --git a/test/rr-autorelease-fastarc.m b/test/rr-autorelease-fastarc.m new file mode 100644 index 0000000..da536f1 --- /dev/null +++ b/test/rr-autorelease-fastarc.m @@ -0,0 +1,204 @@ +// TEST_CFLAGS -Os -framework Foundation +// TEST_DISABLED pending clang support for rdar://20530049 + +#include "test.h" +#include "testroot.i" + +#include +#include +#include + +@interface TestObject : TestRoot @end +@implementation TestObject @end + +@interface Tester : NSObject @end +@implementation Tester { +@public + id ivar; +} + +-(id) return0 { + return ivar; +} +-(id) return1 { + id x = ivar; + [x self]; + return x; +} + +@end + +OBJC_EXPORT +id +objc_retainAutoreleasedReturnValue(id obj); + +// Accept a value returned through a +0 autoreleasing convention for use at +0. +OBJC_EXPORT +id +objc_unsafeClaimAutoreleasedReturnValue(id obj); + + +int +main() +{ + TestObject *obj; + Tester *tt = [Tester new]; + +#ifdef __x86_64__ + // need to get DYLD to resolve the stubs on x86 + PUSH_POOL { + TestObject *warm_up = [[TestObject alloc] init]; + testassert(warm_up); + warm_up = objc_retainAutoreleasedReturnValue(warm_up); + warm_up = objc_unsafeClaimAutoreleasedReturnValue(warm_up); + warm_up = nil; + } POP_POOL; +#endif + + testprintf(" Successful +1 -> +1 handshake\n"); + + PUSH_POOL { + obj = [[TestObject alloc] init]; + testassert(obj); + tt->ivar = obj; + obj = nil; + + TestRootRetain = 0; + TestRootRelease = 0; + TestRootAutorelease = 0; + TestRootDealloc = 0; + + TestObject *tmp = [tt return1]; + + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 0); + testassert(TestRootAutorelease == 0); + + tt->ivar = nil; + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 0); + + tmp = nil; + testassert(TestRootDealloc == 1); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 2); + testassert(TestRootAutorelease == 0); + + } POP_POOL; + + testprintf(" Successful +0 -> +0 handshake\n"); + + PUSH_POOL { + obj = [[TestObject alloc] init]; + testassert(obj); + tt->ivar = obj; + obj = nil; + + TestRootRetain = 0; + TestRootRelease = 0; + TestRootAutorelease = 0; + TestRootDealloc = 0; + + __unsafe_unretained TestObject *tmp = [tt return0]; + + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 0); + testassert(TestRootRelease == 0); + testassert(TestRootAutorelease == 0); + + tmp = nil; + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 0); + testassert(TestRootRelease == 0); + testassert(TestRootAutorelease == 0); + + tt->ivar = nil; + testassert(TestRootDealloc == 1); + testassert(TestRootRetain == 0); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 0); + + } POP_POOL; + + + testprintf(" Successful +1 -> +0 handshake\n"); + + PUSH_POOL { + obj = [[TestObject alloc] init]; + testassert(obj); + tt->ivar = obj; + obj = nil; + + TestRootRetain = 0; + TestRootRelease = 0; + TestRootAutorelease = 0; + TestRootDealloc = 0; + + __unsafe_unretained TestObject *tmp = [tt return1]; + + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 0); + + tmp = nil; + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 0); + + tt->ivar = nil; + testassert(TestRootDealloc == 1); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 2); + testassert(TestRootAutorelease == 0); + + } POP_POOL; + + + testprintf(" Successful +0 -> +1 handshake\n"); + + PUSH_POOL { + obj = [[TestObject alloc] init]; + testassert(obj); + tt->ivar = obj; + obj = nil; + + TestRootRetain = 0; + TestRootRelease = 0; + TestRootAutorelease = 0; + TestRootDealloc = 0; + + TestObject *tmp = [tt return0]; + + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 0); + testassert(TestRootAutorelease == 0); + + tmp = nil; + testassert(TestRootDealloc == 0); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 1); + testassert(TestRootAutorelease == 0); + + tt->ivar = nil; + testassert(TestRootDealloc == 1); + testassert(TestRootRetain == 1); + testassert(TestRootRelease == 2); + testassert(TestRootAutorelease == 0); + + } POP_POOL; + + + + succeed(__FILE__); + + return 0; +} + + +#endif diff --git a/test/rr-autorelease-stacklogging.m b/test/rr-autorelease-stacklogging.m new file mode 100644 index 0000000..6347364 --- /dev/null +++ b/test/rr-autorelease-stacklogging.m @@ -0,0 +1,13 @@ +// Test OBJC_DEBUG_POOL_ALLOCATION (which is also enabled by MallocStackLogging) + +// TEST_ENV OBJC_DEBUG_POOL_ALLOCATION=YES +// TEST_CFLAGS -framework Foundation +// TEST_CONFIG MEM=mrc + +#include "test.h" + +#define FOUNDATION 0 +#define NAME "rr-autorelease-stacklogging" +#define DEBUG_POOL_ALLOCATION 1 + +#include "rr-autorelease2.m" diff --git a/test/rr-autorelease.m b/test/rr-autorelease.m new file mode 100644 index 0000000..01eb89e --- /dev/null +++ b/test/rr-autorelease.m @@ -0,0 +1,9 @@ +// TEST_CFLAGS -framework Foundation +// TEST_CONFIG MEM=mrc + +#include "test.h" + +#define FOUNDATION 0 +#define NAME "rr-autorelease" + +#include "rr-autorelease2.m" diff --git a/test/rr-autorelease2.m b/test/rr-autorelease2.m new file mode 100644 index 0000000..f242082 --- /dev/null +++ b/test/rr-autorelease2.m @@ -0,0 +1,384 @@ +// Define FOUNDATION=1 for NSObject and NSAutoreleasePool +// Define FOUNDATION=0 for _objc_root* and _objc_autoreleasePool* + +#include "test.h" + +#if FOUNDATION +# define RR_PUSH() [[NSAutoreleasePool alloc] init] +# define RR_POP(p) [(id)p release] +# define RR_RETAIN(o) [o retain] +# define RR_RELEASE(o) [o release] +# define RR_AUTORELEASE(o) [o autorelease] +# define RR_RETAINCOUNT(o) [o retainCount] +#else +# define RR_PUSH() _objc_autoreleasePoolPush() +# define RR_POP(p) _objc_autoreleasePoolPop(p) +# define RR_RETAIN(o) _objc_rootRetain((id)o) +# define RR_RELEASE(o) _objc_rootRelease((id)o) +# define RR_AUTORELEASE(o) _objc_rootAutorelease((id)o) +# define RR_RETAINCOUNT(o) _objc_rootRetainCount((id)o) +#endif + +#include +#include + +static int state; +static pthread_attr_t smallstack; + +#define NESTED_COUNT 8 + +@interface Deallocator : NSObject @end +@implementation Deallocator +-(void) dealloc +{ + // testprintf("-[Deallocator %p dealloc]\n", self); + state++; + [super dealloc]; +} +@end + +@interface AutoreleaseDuringDealloc : NSObject @end +@implementation AutoreleaseDuringDealloc +-(void) dealloc +{ + state++; + RR_AUTORELEASE([[Deallocator alloc] init]); + [super dealloc]; +} +@end + +@interface AutoreleasePoolDuringDealloc : NSObject @end +@implementation AutoreleasePoolDuringDealloc +-(void) dealloc +{ + // caller's pool + for (int i = 0; i < NESTED_COUNT; i++) { + RR_AUTORELEASE([[Deallocator alloc] init]); + } + + // local pool, popped + void *pool = RR_PUSH(); + for (int i = 0; i < NESTED_COUNT; i++) { + RR_AUTORELEASE([[Deallocator alloc] init]); + } + RR_POP(pool); + + // caller's pool again + for (int i = 0; i < NESTED_COUNT; i++) { + RR_AUTORELEASE([[Deallocator alloc] init]); + } + +#if FOUNDATION + { + static bool warned; + if (!warned) testwarn("rdar://7138159 NSAutoreleasePool leaks"); + warned = true; + } + state += NESTED_COUNT; +#else + // local pool, not popped + RR_PUSH(); + for (int i = 0; i < NESTED_COUNT; i++) { + RR_AUTORELEASE([[Deallocator alloc] init]); + } +#endif + + [super dealloc]; +} +@end + +void *autorelease_lots_fn(void *singlePool) +{ + // Enough to blow out the stack if AutoreleasePoolPage is recursive. + const int COUNT = 1024*1024; + state = 0; + + int p = 0; + void **pools = (void**)malloc((COUNT+1) * sizeof(void*)); + pools[p++] = RR_PUSH(); + + id obj = RR_AUTORELEASE([[Deallocator alloc] init]); + + // last pool has only 1 autorelease in it + pools[p++] = RR_PUSH(); + + for (int i = 0; i < COUNT; i++) { + if (rand() % 1000 == 0 && !singlePool) { + pools[p++] = RR_PUSH(); + } else { + RR_AUTORELEASE(RR_RETAIN(obj)); + } + } + + testassert(state == 0); + while (--p) { + RR_POP(pools[p]); + } + testassert(state == 0); + testassert(RR_RETAINCOUNT(obj) == 1); + RR_POP(pools[0]); + testassert(state == 1); + free(pools); + + return NULL; +} + +void *nsthread_fn(void *arg __unused) +{ + [NSThread currentThread]; + void *pool = RR_PUSH(); + RR_AUTORELEASE([[Deallocator alloc] init]); + RR_POP(pool); + return NULL; +} + +void cycle(void) +{ + // Normal autorelease. + testprintf("-- Normal autorelease.\n"); + { + void *pool = RR_PUSH(); + state = 0; + RR_AUTORELEASE([[Deallocator alloc] init]); + testassert(state == 0); + RR_POP(pool); + testassert(state == 1); + } + + // Autorelease during dealloc during autoreleasepool-pop. + // That autorelease is handled by the popping pool, not the one above it. + testprintf("-- Autorelease during dealloc during autoreleasepool-pop.\n"); + { + void *pool = RR_PUSH(); + state = 0; + RR_AUTORELEASE([[AutoreleaseDuringDealloc alloc] init]); + testassert(state == 0); + RR_POP(pool); + testassert(state == 2); + } + + // Autorelease pool during dealloc during autoreleasepool-pop. + testprintf("-- Autorelease pool during dealloc during autoreleasepool-pop.\n"); + { + void *pool = RR_PUSH(); + state = 0; + RR_AUTORELEASE([[AutoreleasePoolDuringDealloc alloc] init]); + testassert(state == 0); + RR_POP(pool); + testassert(state == 4 * NESTED_COUNT); + } + + // Top-level thread pool popped normally. + // Check twice - once for empty placeholder, once without. +# if DEBUG_POOL_ALLOCATION || FOUNDATION + // DebugPoolAllocation disables the empty placeholder pool. + // Guard Malloc disables the empty placeholder pool (checked at runtime) + // Foundation makes RR_PUSH return an NSAutoreleasePool not the raw token. +# define CHECK_PLACEHOLDER 0 +# else +# define CHECK_PLACEHOLDER 1 +# endif + testprintf("-- Thread-level pool popped normally.\n"); + { + state = 0; + testonthread(^{ + void *pool = RR_PUSH(); +#if CHECK_PLACEHOLDER + if (!is_guardmalloc()) { + testassert(pool == (void*)1); + } +#endif + RR_AUTORELEASE([[Deallocator alloc] init]); + RR_POP(pool); + pool = RR_PUSH(); +#if CHECK_PLACEHOLDER + if (!is_guardmalloc()) { + testassert(pool != (void*)1); + } +#endif + RR_AUTORELEASE([[Deallocator alloc] init]); + RR_POP(pool); + }); + testassert(state == 2); + } + + + // Autorelease with no pool. + testprintf("-- Autorelease with no pool.\n"); + { + state = 0; + testonthread(^{ + RR_AUTORELEASE([[Deallocator alloc] init]); + }); + testassert(state == 1); + } + + // Autorelease with no pool after popping the top-level pool. + testprintf("-- Autorelease with no pool after popping the last pool.\n"); + { + state = 0; + testonthread(^{ + void *pool = RR_PUSH(); + RR_AUTORELEASE([[Deallocator alloc] init]); + RR_POP(pool); + RR_AUTORELEASE([[Deallocator alloc] init]); + }); + testassert(state == 2); + } + + // Top-level thread pool not popped. + // The runtime should clean it up. +#if FOUNDATION + { + static bool warned; + if (!warned) testwarn("rdar://7138159 NSAutoreleasePool leaks"); + warned = true; + } +#else + testprintf("-- Thread-level pool not popped.\n"); + { + state = 0; + testonthread(^{ + RR_PUSH(); + RR_AUTORELEASE([[Deallocator alloc] init]); + // pool not popped + }); + testassert(state == 1); + } +#endif + + // Intermediate pool not popped. + // Popping the containing pool should clean up the skipped pool first. +#if FOUNDATION + { + static bool warned; + if (!warned) testwarn("rdar://7138159 NSAutoreleasePool leaks"); + warned = true; + } +#else + testprintf("-- Intermediate pool not popped.\n"); + { + void *pool = RR_PUSH(); + void *pool2 = RR_PUSH(); + RR_AUTORELEASE([[Deallocator alloc] init]); + state = 0; + (void)pool2; // pool2 not popped + RR_POP(pool); + testassert(state == 1); + } +#endif +} + + +static void +slow_cycle(void) +{ + // Large autorelease stack. + // Do this only once because it's slow. + testprintf("-- Large autorelease stack.\n"); + { + // limit stack size: autorelease pop should not be recursive + pthread_t th; + pthread_create(&th, &smallstack, &autorelease_lots_fn, NULL); + pthread_join(th, NULL); + } + + // Single large autorelease pool. + // Do this only once because it's slow. + testprintf("-- Large autorelease pool.\n"); + { + // limit stack size: autorelease pop should not be recursive + pthread_t th; + pthread_create(&th, &smallstack, &autorelease_lots_fn, (void*)1); + pthread_join(th, NULL); + } +} + + +int main() +{ + pthread_attr_init(&smallstack); + pthread_attr_setstacksize(&smallstack, 32768); + + // inflate the refcount side table so it doesn't show up in leak checks + { + int count = 10000; + id *objs = (id *)malloc(count*sizeof(id)); + for (int i = 0; i < count; i++) { + objs[i] = RR_RETAIN([NSObject new]); + } + for (int i = 0; i < count; i++) { + RR_RELEASE(objs[i]); + RR_RELEASE(objs[i]); + } + free(objs); + } + +#if FOUNDATION + // inflate NSAutoreleasePool's instance cache + { + int count = 32; + id *objs = (id *)malloc(count * sizeof(id)); + for (int i = 0; i < count; i++) { + objs[i] = [[NSAutoreleasePool alloc] init]; + } + for (int i = 0; i < count; i++) { + [objs[count-i-1] release]; + } + + free(objs); + } +#endif + + // preheat + { + for (int i = 0; i < 100; i++) { + cycle(); + } + + slow_cycle(); + } + + // check for leaks using top-level pools + { + leak_mark(); + + for (int i = 0; i < 1000; i++) { + cycle(); + } + + leak_check(0); + + slow_cycle(); + + leak_check(0); + } + + // check for leaks using pools not at top level + // fixme for FOUNDATION this leak mark/check needs + // to be outside the autorelease pool for some reason + leak_mark(); + void *pool = RR_PUSH(); + { + for (int i = 0; i < 1000; i++) { + cycle(); + } + + slow_cycle(); + } + RR_POP(pool); + leak_check(0); + + // NSThread. + // Can't leak check this because it's too noisy. + testprintf("-- NSThread.\n"); + { + pthread_t th; + pthread_create(&th, &smallstack, &nsthread_fn, 0); + pthread_join(th, NULL); + } + + // NO LEAK CHECK HERE + + succeed(NAME); +} diff --git a/test/rr-nsautorelease.m b/test/rr-nsautorelease.m new file mode 100644 index 0000000..095ec36 --- /dev/null +++ b/test/rr-nsautorelease.m @@ -0,0 +1,7 @@ +// TEST_CFLAGS -framework Foundation +// TEST_CONFIG MEM=mrc + +#define FOUNDATION 1 +#define NAME "rr-nsautorelease" + +#include "rr-autorelease2.m" diff --git a/test/rr-sidetable.m b/test/rr-sidetable.m new file mode 100644 index 0000000..daa4090 --- /dev/null +++ b/test/rr-sidetable.m @@ -0,0 +1,59 @@ +// TEST_CFLAGS -framework Foundation +// TEST_CONFIG MEM=mrc ARCH=x86_64 + +// Stress-test nonpointer isa's side table retain count transfers. + +// x86_64 only. arm64's side table limit is high enough that bugs +// are harder to reproduce. + +#include "test.h" +#import + +#define OBJECTS 1 +#define LOOPS 256 +#define THREADS 16 +#if __x86_64__ +# define RC_HALF (1ULL<<7) +#else +# error sorry +#endif +#define RC_DELTA RC_HALF + +static bool Deallocated = false; +@interface Deallocator : NSObject @end +@implementation Deallocator +-(void)dealloc { + Deallocated = true; + [super dealloc]; +} +@end + +// This is global to avoid extra retains by the dispatch block objects. +static Deallocator *obj; + +int main() { + dispatch_queue_t queue = + dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); + + for (size_t i = 0; i < OBJECTS; i++) { + obj = [Deallocator new]; + + dispatch_apply(THREADS, queue, ^(size_t i __unused) { + for (size_t a = 0; a < LOOPS; a++) { + for (size_t b = 0; b < RC_DELTA; b++) { + [obj retain]; + } + for (size_t b = 0; b < RC_DELTA; b++) { + [obj release]; + } + } + }); + + testassert(!Deallocated); + [obj release]; + testassert(Deallocated); + Deallocated = false; + } + + succeed(__FILE__); +} diff --git a/test/runtime.m b/test/runtime.m new file mode 100644 index 0000000..50bd68c --- /dev/null +++ b/test/runtime.m @@ -0,0 +1,227 @@ +/* +TEST_BUILD_OUTPUT +.*runtime.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*runtime.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +.*runtime.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +END + +TEST_RUN_OUTPUT +objc\[\d+\]: class `SwiftV1Class\' not linked into application +objc\[\d+\]: class `DoesNotExist\' not linked into application +OK: runtime.m +OR +confused by Foundation +OK: runtime.m +END +*/ + + +#include "test.h" +#include "testroot.i" +#include +#include +#include +#include + +#if __has_feature(objc_arc) + +int main() +{ + // provoke the same nullability warnings as the real test + objc_getClass(nil); + objc_getClass(nil); + objc_getClass(nil); + + testwarn("rdar://11368528 confused by Foundation"); + fprintf(stderr, "confused by Foundation\n"); + succeed(__FILE__); +} + +#else + +@interface Sub : TestRoot @end +@implementation Sub @end + +#define SwiftV1MangledName "_TtC6Module12SwiftV1Class" +#define SwiftV1MangledName2 "_TtC2Sw13SwiftV1Class2" +#define SwiftV1MangledName3 "_TtCs13SwiftV1Class3" +#define SwiftV1MangledName4 "_TtC6Swiftt13SwiftV1Class4" + +__attribute__((objc_runtime_name(SwiftV1MangledName))) +@interface SwiftV1Class : TestRoot @end +@implementation SwiftV1Class @end + +__attribute__((objc_runtime_name(SwiftV1MangledName2))) +@interface SwiftV1Class2 : TestRoot @end +@implementation SwiftV1Class2 @end + +__attribute__((objc_runtime_name(SwiftV1MangledName3))) +@interface SwiftV1Class3 : TestRoot @end +@implementation SwiftV1Class3 @end + +__attribute__((objc_runtime_name(SwiftV1MangledName4))) +@interface SwiftV1Class4 : TestRoot @end +@implementation SwiftV1Class4 @end + + +int main() +{ + Class list[100]; + Class *list2; + unsigned int count, count0, count2; + unsigned int i; + int foundTestRoot; + int foundSub; + int foundSwiftV1; + int foundSwiftV1class2; + int foundSwiftV1class3; + int foundSwiftV1class4; + const char **names; + const char **namesFromHeader; + Dl_info info; + + [TestRoot class]; + + // This shouldn't touch any classes. + dladdr(&_mh_execute_header, &info); + names = objc_copyClassNamesForImage(info.dli_fname, &count); + testassert(names); + testassert(count == 6); + testassert(names[count] == NULL); + foundTestRoot = 0; + foundSub = 0; + foundSwiftV1 = 0; + foundSwiftV1class2 = 0; + foundSwiftV1class3 = 0; + foundSwiftV1class4 = 0; + for (i = 0; i < count; i++) { + if (0 == strcmp(names[i], "TestRoot")) foundTestRoot++; + if (0 == strcmp(names[i], "Sub")) foundSub++; + if (0 == strcmp(names[i], "Module.SwiftV1Class")) foundSwiftV1++; + if (0 == strcmp(names[i], "Sw.SwiftV1Class2")) foundSwiftV1class2++; + if (0 == strcmp(names[i], "Swift.SwiftV1Class3")) foundSwiftV1class3++; + if (0 == strcmp(names[i], "Swiftt.SwiftV1Class4")) foundSwiftV1class4++; + } + testassert(foundTestRoot == 1); + testassert(foundSub == 1); + testassert(foundSwiftV1 == 1); + testassert(foundSwiftV1class2 == 1); + testassert(foundSwiftV1class3 == 1); + testassert(foundSwiftV1class4 == 1); + + // Getting the names using the header should give us the same list. + namesFromHeader = objc_copyClassNamesForImage(info.dli_fname, &count0); + testassert(namesFromHeader); + testassert(count == count0); + for (i = 0; i < count; i++) { + testassert(!strcmp(names[i], namesFromHeader[i])); + } + + + // class Sub hasn't been touched - make sure it's in the class list too + count0 = objc_getClassList(NULL, 0); + testassert(count0 >= 2 && count0 < 100); + + list[count0-1] = NULL; + count = objc_getClassList(list, count0-1); + testassert(list[count0-1] == NULL); + testassert(count == count0); + + // So that demangling works, fake what would have happened with Swift + // and force the "Swift" bit on the class + ((uintptr_t *)objc_getClass(SwiftV1MangledName))[4] |= 2; + ((uintptr_t *)objc_getClass(SwiftV1MangledName2))[4] |= 2; + ((uintptr_t *)objc_getClass(SwiftV1MangledName3))[4] |= 2; + ((uintptr_t *)objc_getClass(SwiftV1MangledName4))[4] |= 2; + + count = objc_getClassList(list, count0); + testassert(count == count0); + + for (i = 0; i < count; i++) { + testprintf("%s\n", class_getName(list[i])); + } + + foundTestRoot = 0; + foundSub = 0; + foundSwiftV1 = 0; + foundSwiftV1class2 = 0; + foundSwiftV1class3 = 0; + foundSwiftV1class4 = 0; + for (i = 0; i < count; i++) { + if (0 == strcmp(class_getName(list[i]), "TestRoot")) foundTestRoot++; + if (0 == strcmp(class_getName(list[i]), "Sub")) foundSub++; + if (0 == strcmp(class_getName(list[i]), "Module.SwiftV1Class")) foundSwiftV1++; + if (0 == strcmp(class_getName(list[i]), "Sw.SwiftV1Class2")) foundSwiftV1class2++; + if (0 == strcmp(class_getName(list[i]), "Swift.SwiftV1Class3")) foundSwiftV1class3++; + if (0 == strcmp(class_getName(list[i]), "Swiftt.SwiftV1Class4")) foundSwiftV1class4++; + // list should be non-meta classes only + testassert(!class_isMetaClass(list[i])); + } + testassert(foundTestRoot == 1); + testassert(foundSub == 1); + testassert(foundSwiftV1 == 1); + testassert(foundSwiftV1class2 == 1); + testassert(foundSwiftV1class3 == 1); + testassert(foundSwiftV1class4 == 1); + + // fixme check class handler + testassert(objc_getClass("TestRoot") == [TestRoot class]); + testassert(objc_getClass("Module.SwiftV1Class") == [SwiftV1Class class]); + testassert(objc_getClass(SwiftV1MangledName) == [SwiftV1Class class]); + testassert(objc_getClass("Sw.SwiftV1Class2") == [SwiftV1Class2 class]); + testassert(objc_getClass(SwiftV1MangledName2) == [SwiftV1Class2 class]); + testassert(objc_getClass("Swift.SwiftV1Class3") == [SwiftV1Class3 class]); + testassert(objc_getClass(SwiftV1MangledName3) == [SwiftV1Class3 class]); + testassert(objc_getClass("Swiftt.SwiftV1Class4") == [SwiftV1Class4 class]); + testassert(objc_getClass(SwiftV1MangledName4) == [SwiftV1Class4 class]); + testassert(objc_getClass("SwiftV1Class") == nil); + testassert(objc_getClass("DoesNotExist") == nil); + testassert(objc_getClass(NULL) == nil); + + testassert(objc_getMetaClass("TestRoot") == object_getClass([TestRoot class])); + testassert(objc_getMetaClass("Module.SwiftV1Class") == object_getClass([SwiftV1Class class])); + testassert(objc_getMetaClass(SwiftV1MangledName) == object_getClass([SwiftV1Class class])); + testassert(objc_getMetaClass("SwiftV1Class") == nil); + testassert(objc_getMetaClass("DoesNotExist") == nil); + testassert(objc_getMetaClass(NULL) == nil); + + // fixme check class no handler + testassert(objc_lookUpClass("TestRoot") == [TestRoot class]); + testassert(objc_lookUpClass("Module.SwiftV1Class") == [SwiftV1Class class]); + testassert(objc_lookUpClass(SwiftV1MangledName) == [SwiftV1Class class]); + testassert(objc_lookUpClass("SwiftV1Class") == nil); + testassert(objc_lookUpClass("DoesNotExist") == nil); + testassert(objc_lookUpClass(NULL) == nil); + + testassert(! object_isClass(nil)); + testassert(! object_isClass([TestRoot new])); + testassert(object_isClass([TestRoot class])); + testassert(object_isClass(object_getClass([TestRoot class]))); + testassert(object_isClass([Sub class])); + testassert(object_isClass(object_getClass([Sub class]))); + testassert(object_isClass([SwiftV1Class class])); + testassert(object_isClass(object_getClass([SwiftV1Class class]))); + + list2 = objc_copyClassList(&count2); + testassert(count2 == count); + testassert(list2); + testassert(malloc_size(list2) >= (1+count2) * sizeof(Class)); + for (i = 0; i < count; i++) { + testassert(list[i] == list2[i]); + } + testassert(list2[count] == NULL); + free(list2); + free(objc_copyClassList(NULL)); + + // Make sure metaclasses get demangled too. + testassert(strcmp(class_getName([TestRoot class]), class_getName(object_getClass([TestRoot class]))) == 0); + testassert(strcmp(class_getName([Sub class]), class_getName(object_getClass([Sub class]))) == 0); + testassert(strcmp(class_getName([SwiftV1Class class]), class_getName(object_getClass([SwiftV1Class class]))) == 0); + testassert(strcmp(class_getName([SwiftV1Class2 class]), class_getName(object_getClass([SwiftV1Class2 class]))) == 0); + testassert(strcmp(class_getName([SwiftV1Class3 class]), class_getName(object_getClass([SwiftV1Class3 class]))) == 0); + testassert(strcmp(class_getName([SwiftV1Class4 class]), class_getName(object_getClass([SwiftV1Class4 class]))) == 0); + + succeed(__FILE__); +} + +#endif diff --git a/test/sel.m b/test/sel.m new file mode 100644 index 0000000..a4c4dd3 --- /dev/null +++ b/test/sel.m @@ -0,0 +1,21 @@ +/* +TEST_BUILD_OUTPUT +.*sel.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')? +END +*/ + +#include "test.h" +#include +#include +#include + +int main() +{ + // Make sure @selector values are correctly fixed up + testassert(@selector(foo) == sel_registerName("foo")); + + // sel_getName recognizes the zero SEL + testassert(0 == strcmp("", sel_getName(0))); + + succeed(__FILE__); +} diff --git a/test/setAssociatedObjectHook.m b/test/setAssociatedObjectHook.m new file mode 100644 index 0000000..e244d5c --- /dev/null +++ b/test/setAssociatedObjectHook.m @@ -0,0 +1,47 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" + +id sawObject; +const void *sawKey; +id sawValue; +objc_AssociationPolicy sawPolicy; + +objc_hook_setAssociatedObject originalSetAssociatedObject; + +void hook(id _Nonnull object, const void * _Nonnull key, id _Nullable value, objc_AssociationPolicy policy) { + sawObject = object; + sawKey = key; + sawValue = value; + sawPolicy = policy; + originalSetAssociatedObject(object, key, value, policy); +} + +int main() { + id obj = [TestRoot new]; + id value = [TestRoot new]; + const void *key = "key"; + objc_setAssociatedObject(obj, key, value, OBJC_ASSOCIATION_RETAIN); + testassert(sawObject == nil); + testassert(sawKey == nil); + testassert(sawValue == nil); + testassert(sawPolicy == 0); + + id out = objc_getAssociatedObject(obj, key); + testassert(out == value); + + objc_setHook_setAssociatedObject(hook, &originalSetAssociatedObject); + + key = "key2"; + objc_setAssociatedObject(obj, key, value, OBJC_ASSOCIATION_RETAIN); + testassert(sawObject == obj); + testassert(sawKey == key); + testassert(sawValue == value); + testassert(sawPolicy == OBJC_ASSOCIATION_RETAIN); + + out = objc_getAssociatedObject(obj, key); + testassert(out == value); + + succeed(__FILE__); +} \ No newline at end of file diff --git a/test/setSuper.m b/test/setSuper.m new file mode 100644 index 0000000..5f77840 --- /dev/null +++ b/test/setSuper.m @@ -0,0 +1,44 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" +#include + +@interface Super1 : TestRoot @end +@implementation Super1 ++(int)classMethod { return 1; } +-(int)instanceMethod { return 10000; } +@end + +@interface Super2 : TestRoot @end +@implementation Super2 ++(int)classMethod { return 2; } +-(int)instanceMethod { return 20000; } +@end + +@interface Sub : Super1 @end +@implementation Sub ++(int)classMethod { return [super classMethod] + 100; } +-(int)instanceMethod { + return [super instanceMethod] + 1000000; +} +@end + +int main() +{ + Class cls; + Sub *obj = [Sub new]; + + testassert(101 == [[Sub class] classMethod]); + testassert(1010000 == [obj instanceMethod]); + + cls = class_setSuperclass([Sub class], [Super2 class]); + + testassert(cls == [Super1 class]); + testassert(object_getClass(cls) == object_getClass([Super1 class])); + + testassert(102 == [[Sub class] classMethod]); + testassert(1020000 == [obj instanceMethod]); + + succeed(__FILE__); +} diff --git a/test/subscripting.m b/test/subscripting.m new file mode 100644 index 0000000..ecc2b13 --- /dev/null +++ b/test/subscripting.m @@ -0,0 +1,139 @@ +// TEST_CFLAGS -framework Foundation + +#import +#import +#import +#import +#include "test.h" + +@interface TestIndexed : NSObject { + NSMutableArray *indexedValues; +} +@property(readonly) NSUInteger count; +- (id)objectAtIndexedSubscript:(NSUInteger)index; +- (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index; +@end + +@implementation TestIndexed + +- (id)init { + if ((self = [super init])) { + indexedValues = [NSMutableArray new]; + } + return self; +} + +#if !__has_feature(objc_arc) +- (void)dealloc { + [indexedValues release]; + [super dealloc]; +} +#endif + +- (NSUInteger)count { return [indexedValues count]; } +- (id)objectAtIndexedSubscript:(NSUInteger)index { return [indexedValues objectAtIndex:index]; } +- (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index { + if (index == NSNotFound) + [indexedValues addObject:object]; + else + [indexedValues replaceObjectAtIndex:index withObject:object]; +} + +- (NSString *)description { + return [NSString stringWithFormat:@"indexedValues = %@", indexedValues]; +} + +- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len { + return [indexedValues countByEnumeratingWithState:state objects:buffer count:len]; +} + + +@end + +@interface TestKeyed : NSObject { + NSMutableDictionary *keyedValues; +} +@property(readonly) NSUInteger count; +- (id)objectForKeyedSubscript:(id)key; +- (void)setObject:(id)object forKeyedSubscript:(id)key; +@end + +@implementation TestKeyed + +- (id)init { + if ((self = [super init])) { + keyedValues = [NSMutableDictionary new]; + } + return self; +} + +#if !__has_feature(objc_arc) +- (void)dealloc { + [keyedValues release]; + [super dealloc]; +} +#endif + +- (NSUInteger)count { return [keyedValues count]; } +- (id)objectForKeyedSubscript:(id)key { return [keyedValues objectForKey:key]; } +- (void)setObject:(id)object forKeyedSubscript:(id)key { + [keyedValues setObject:object forKey:key]; +} + +- (NSString *)description { + return [NSString stringWithFormat:@"keyedValues = %@", keyedValues]; +} + +- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len { + return [keyedValues countByEnumeratingWithState:state objects:buffer count:len]; +} + +@end + +int main() { + PUSH_POOL { + +#if __has_feature(objc_bool) // placeholder until we get a more precise macro. + TestIndexed *testIndexed = [TestIndexed new]; + id objects[] = { @1, @2, @3, @4, @5 }; + size_t i, count = sizeof(objects) / sizeof(id); + for (i = 0; i < count; ++i) { + testIndexed[NSNotFound] = objects[i]; + } + for (i = 0; i < count; ++i) { + id object = testIndexed[i]; + testassert(object == objects[i]); + } + if (testverbose()) { + i = 0; + for (id object in testIndexed) { + NSString *message = [NSString stringWithFormat:@"testIndexed[%zu] = %@\n", i++, object]; + testprintf([message UTF8String]); + } + } + + TestKeyed *testKeyed = [TestKeyed new]; + id keys[] = { @"One", @"Two", @"Three", @"Four", @"Five" }; + for (i = 0; i < count; ++i) { + id key = keys[i]; + testKeyed[key] = objects[i]; + } + for (i = 0; i < count; ++i) { + id key = keys[i]; + id object = testKeyed[key]; + testassert(object == objects[i]); + } + if (testverbose()) { + for (id key in testKeyed) { + NSString *message = [NSString stringWithFormat:@"testKeyed[@\"%@\"] = %@\n", key, testKeyed[key]]; + testprintf([message UTF8String]); + } + } +#endif + + } POP_POOL; + + succeed(__FILE__); + + return 0; +} diff --git a/test/super.m b/test/super.m new file mode 100644 index 0000000..ff169f7 --- /dev/null +++ b/test/super.m @@ -0,0 +1,21 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" +#include + +@interface Sub : TestRoot @end +@implementation Sub @end + +int main() +{ + // [super ...] messages are tested in msgSend.m + + testassert(class_getSuperclass([Sub class]) == [TestRoot class]); + testassert(class_getSuperclass(object_getClass([Sub class])) == object_getClass([TestRoot class])); + testassert(class_getSuperclass([TestRoot class]) == Nil); + testassert(class_getSuperclass(object_getClass([TestRoot class])) == [TestRoot class]); + testassert(class_getSuperclass(Nil) == Nil); + + succeed(__FILE__); +} diff --git a/test/swift-class-def.m b/test/swift-class-def.m new file mode 100644 index 0000000..6bc2d05 --- /dev/null +++ b/test/swift-class-def.m @@ -0,0 +1,201 @@ +#include + +#if __LP64__ +# define PTR " .quad " +# define PTRSIZE "8" +# define LOGPTRSIZE "3" +# define ONLY_LP64(x) x +#else +# define PTR " .long " +# define PTRSIZE "4" +# define LOGPTRSIZE "2" +# define ONLY_LP64(x) +#endif + +#if __has_feature(ptrauth_calls) +# define SIGNED_METHOD_LIST_IMP "@AUTH(ia,0,addr) " +# define SIGNED_STUB_INITIALIZER "@AUTH(ia,0xc671,addr) " +#else +# define SIGNED_METHOD_LIST_IMP +# define SIGNED_STUB_INITIALIZER +#endif + +#define str(x) #x +#define str2(x) str(x) + +// Swift metadata initializers. Define these in the test. +EXTERN_C Class initSuper(Class cls, void *arg); +EXTERN_C Class initSub(Class cls, void *arg); + +@interface SwiftSuper : NSObject @end +@interface SwiftSub : SwiftSuper @end + +__BEGIN_DECLS +// not id to avoid ARC operations because the class doesn't implement RR methods +void* nop(void* self) { return self; } +__END_DECLS + +#define SWIFT_CLASS(name, superclass, swiftInit) \ +asm( \ + ".globl _OBJC_CLASS_$_" #name "\n" \ + ".section __DATA,__objc_data \n" \ + ".align 3 \n" \ + "_OBJC_CLASS_$_" #name ": \n" \ + PTR "_OBJC_METACLASS_$_" #name "\n" \ + PTR "_OBJC_CLASS_$_" #superclass "\n" \ + PTR "__objc_empty_cache \n" \ + PTR "0 \n" \ + PTR "L_" #name "_ro + 2 \n" \ + /* Swift class fields. */ \ + ".long 0 \n" /* flags */ \ + ".long 0 \n" /* instanceAddressOffset */ \ + ".long 16 \n" /* instanceSize */ \ + ".short 15 \n" /* instanceAlignMask */ \ + ".short 0 \n" /* reserved */ \ + ".long 256 \n" /* classSize */ \ + ".long 0 \n" /* classAddressOffset */ \ + PTR "0 \n" /* description */ \ + /* pad to OBJC_MAX_CLASS_SIZE */ \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + \ + "_OBJC_METACLASS_$_" #name ": \n" \ + PTR "_OBJC_METACLASS_$_" #superclass "\n" \ + PTR "_OBJC_METACLASS_$_" #superclass "\n" \ + PTR "__objc_empty_cache \n" \ + PTR "0 \n" \ + PTR "L_" #name "_meta_ro \n" \ + /* pad to OBJC_MAX_CLASS_SIZE */ \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + \ + "L_" #name "_ro: \n" \ + ".long (1<<6) \n" \ + ".long 0 \n" \ + ".long " PTRSIZE " \n" \ + ONLY_LP64(".long 0 \n") \ + PTR "0 \n" \ + PTR "L_" #name "_name \n" \ + PTR "L_" #name "_methods \n" \ + PTR "0 \n" \ + PTR "L_" #name "_ivars \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "_" #swiftInit SIGNED_METHOD_LIST_IMP "\n" \ + \ + "L_" #name "_meta_ro: \n" \ + ".long 1 \n" \ + ".long 40 \n" \ + ".long 40 \n" \ + ONLY_LP64(".long 0 \n") \ + PTR "0 \n" \ + PTR "L_" #name "_name \n" \ + PTR "L_" #name "_meta_methods \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + PTR "0 \n" \ + \ + "L_" #name "_methods: \n" \ + "L_" #name "_meta_methods: \n" \ + ".long 3*" PTRSIZE "\n" \ + ".long 1 \n" \ + PTR "L_" #name "_self \n" \ + PTR "L_" #name "_self \n" \ + PTR "_nop" SIGNED_METHOD_LIST_IMP "\n" \ + \ + "L_" #name "_ivars: \n" \ + ".long 4*" PTRSIZE " \n" \ + ".long 1 \n" \ + PTR "L_" #name "_ivar_offset \n" \ + PTR "L_" #name "_ivar_name \n" \ + PTR "L_" #name "_ivar_type \n" \ + ".long " LOGPTRSIZE "\n" \ + ".long " PTRSIZE "\n" \ + \ + "L_" #name "_ivar_offset: \n" \ + ".long 0 \n" \ + \ + ".cstring \n" \ + "L_" #name "_name: .ascii \"" #name "\\0\" \n" \ + "L_" #name "_self: .ascii \"self\\0\" \n" \ + "L_" #name "_ivar_name: " \ + " .ascii \"" #name "_ivar\\0\" \n" \ + "L_" #name "_ivar_type: .ascii \"c\\0\" \n" \ + \ + \ + ".text \n" \ +); \ +extern char OBJC_CLASS_$_ ## name; \ +Class Raw ## name = (Class)&OBJC_CLASS_$_ ## name + +#define SWIFT_STUB_CLASSREF(name) \ +extern char OBJC_CLASS_$_ ## name; \ +static Class name ## Classref = (Class)(&OBJC_CLASS_$_ ## name + 1) + +#define SWIFT_STUB_CLASS(name, initializer) \ +asm( \ + ".globl _OBJC_CLASS_$_" #name "\n" \ + ".section __DATA,__objc_data \n" \ + ".align 3 \n" \ + "_dummy" #name ": \n" \ + PTR "0 \n" \ + ".alt_entry _OBJC_CLASS_$_" #name "\n" \ + "_OBJC_CLASS_$_" #name ": \n" \ + PTR "1 \n" \ + PTR "_" #initializer SIGNED_STUB_INITIALIZER "\n" \ + ".text" \ +); \ +extern char OBJC_CLASS_$_ ## name; \ +Class Raw ## name = (Class)&OBJC_CLASS_$_ ## name; \ +SWIFT_STUB_CLASSREF(name) + + +void fn(void) { } diff --git a/test/swiftMetadataInitializer.m b/test/swiftMetadataInitializer.m new file mode 100644 index 0000000..7900344 --- /dev/null +++ b/test/swiftMetadataInitializer.m @@ -0,0 +1,68 @@ +// TEST_CONFIG MEM=mrc + +#include "test.h" +#include "swift-class-def.m" + + +SWIFT_CLASS(SwiftSuper, NSObject, initSuper); +SWIFT_CLASS(SwiftSub, SwiftSuper, initSub); + +// _objc_swiftMetadataInitializer hooks for the fake Swift classes + +Class initSuper(Class cls __unused, void *arg __unused) +{ + // This test provokes objc's callback out of superclass order. + // SwiftSub's init is first. SwiftSuper's init is never called. + + fail("SwiftSuper's init should not have been called"); +} + +bool isRealized(Class cls) +{ + // check the is-realized bits directly + +#if __LP64__ +# define mask (~(uintptr_t)7) +#else +# define mask (~(uintptr_t)3) +#endif +#define RW_REALIZED (1<<31) + + uintptr_t rw = ((uintptr_t *)cls)[4] & mask; // class_t->data + return ((uint32_t *)rw)[0] & RW_REALIZED; // class_rw_t->flags +} + +static int SubInits = 0; +Class initSub(Class cls, void *arg) +{ + testprintf("initSub callback\n"); + + testassert(SubInits == 0); + SubInits++; + testassert(arg == nil); + testassert(0 == strcmp(class_getName(cls), "SwiftSub")); + testassert(cls == RawSwiftSub); + testassert(!isRealized(RawSwiftSuper)); + testassert(!isRealized(RawSwiftSub)); + + testprintf("initSub beginning _objc_realizeClassFromSwift\n"); + _objc_realizeClassFromSwift(cls, cls); + testprintf("initSub finished _objc_realizeClassFromSwift\n"); + + testassert(isRealized(RawSwiftSuper)); + testassert(isRealized(RawSwiftSub)); + + return cls; +} + + +int main() +{ + testassert(SubInits == 0); + testprintf("calling [SwiftSub class]\n"); + [SwiftSub class]; + testprintf("finished [SwiftSub class]\n"); + testassert(SubInits == 1); + [SwiftSuper class]; + succeed(__FILE__); +} diff --git a/test/swiftMetadataInitializerRealloc-dylib1.m b/test/swiftMetadataInitializerRealloc-dylib1.m new file mode 100644 index 0000000..2fbecd2 --- /dev/null +++ b/test/swiftMetadataInitializerRealloc-dylib1.m @@ -0,0 +1,64 @@ +#include "test.h" +#include "swift-class-def.m" + +SWIFT_CLASS(RealSwiftDylib1A, NSObject, nop); +SWIFT_STUB_CLASS(SwiftDylib1A, initSwiftDylib1A); + +SWIFT_CLASS(RealSwiftDylib1B, NSObject, nop); +SWIFT_STUB_CLASS(SwiftDylib1B, initSwiftDylib1B); + +int Dylib1AInits = 0; + +@interface SwiftDylib1A: NSObject @end +@interface SwiftDylib1B: NSObject @end + +@implementation SwiftDylib1A (Category) +- (const char *)dylib1ACategoryInSameDylib { return "dylib1ACategoryInSameDylib"; } +@end +@implementation SwiftDylib1B (Category) +- (const char *)dylib1BCategoryInSameDylib { return "dylib1BCategoryInSameDylib"; } +@end + +EXTERN_C Class initSwiftDylib1A(Class cls, void *arg) +{ + Dylib1AInits++; + testassert(arg == nil); + testassert(cls == RawSwiftDylib1A); + + if (Dylib1AInits == 1) + _objc_realizeClassFromSwift(RawRealSwiftDylib1A, cls); + + return RawRealSwiftDylib1A; +} + +int Dylib1BInits = 0; + +EXTERN_C Class initSwiftDylib1B(Class cls, void *arg) +{ + Dylib1BInits++; + testassert(arg == nil); + testassert(cls == RawSwiftDylib1B); + + if (Dylib1BInits == 1) + _objc_realizeClassFromSwift(RawRealSwiftDylib1B, cls); + + return RawRealSwiftDylib1B; +} + +EXTERN_C Class objc_loadClassref(_Nullable Class * _Nonnull clsref); + +void Dylib1Test(void) { + testassert((uintptr_t)SwiftDylib1AClassref & 1); + Class SwiftDylib1A = objc_loadClassref(&SwiftDylib1AClassref); + testassert(((uintptr_t)SwiftDylib1AClassref & 1) == 0); + testassert(SwiftDylib1A == [SwiftDylib1A class]); + testassert(SwiftDylib1A == SwiftDylib1AClassref); + testassert(Dylib1AInits == 2); + + testassert((uintptr_t)SwiftDylib1BClassref & 1); + Class SwiftDylib1B = objc_loadClassref(&SwiftDylib1BClassref); + testassert(((uintptr_t)SwiftDylib1BClassref & 1) == 0); + testassert(SwiftDylib1B == [SwiftDylib1B class]); + testassert(SwiftDylib1B == SwiftDylib1BClassref); + testassert(Dylib1BInits == 2); +} diff --git a/test/swiftMetadataInitializerRealloc-dylib2.m b/test/swiftMetadataInitializerRealloc-dylib2.m new file mode 100644 index 0000000..71670c0 --- /dev/null +++ b/test/swiftMetadataInitializerRealloc-dylib2.m @@ -0,0 +1,40 @@ +#include "test.h" +#include "swift-class-def.m" + +@interface SwiftDylib1A: NSObject @end +@interface SwiftDylib1B: NSObject @end + +@interface NSObject (DylibCategories) +- (char *)dylib1ACategoryInSameDylib; +- (char *)dylib1BCategoryInSameDylib; +- (char *)dylib1ACategoryInOtherDylib; +- (char *)dylib1BCategoryInOtherDylib; +- (char *)dylib1ACategoryInApp; +- (char *)dylib1BCategoryInApp; ++ (void)testFromOtherDylib; +@end + +@implementation SwiftDylib1A (Category) +- (const char *)dylib1ACategoryInOtherDylib { return "dylib1ACategoryInOtherDylib"; } +@end +@implementation SwiftDylib1B (Category) +- (const char *)dylib1BCategoryInOtherDylib { return "dylib1BCategoryInOtherDylib"; } +@end + +SWIFT_STUB_CLASSREF(SwiftDylib1A); +SWIFT_STUB_CLASSREF(SwiftDylib1B); + +Class objc_loadClassref(_Nullable Class * _Nonnull clsref); + +@implementation SwiftDylib1A (Test) ++ (void)testFromOtherDylib { + Class SwiftDylib1A = objc_loadClassref(&SwiftDylib1AClassref); + Class SwiftDylib1B = objc_loadClassref(&SwiftDylib1BClassref); + testassert(strcmp([[SwiftDylib1A new] dylib1ACategoryInSameDylib], "dylib1ACategoryInSameDylib") == 0); + testassert(strcmp([[SwiftDylib1B new] dylib1BCategoryInSameDylib], "dylib1BCategoryInSameDylib") == 0); + testassert(strcmp([[SwiftDylib1A new] dylib1ACategoryInApp], "dylib1ACategoryInApp") == 0); + testassert(strcmp([[SwiftDylib1B new] dylib1BCategoryInApp], "dylib1BCategoryInApp") == 0); + testassert(strcmp([[SwiftDylib1A new] dylib1ACategoryInOtherDylib], "dylib1ACategoryInOtherDylib") == 0); + testassert(strcmp([[SwiftDylib1B new] dylib1BCategoryInOtherDylib], "dylib1BCategoryInOtherDylib") == 0); +} +@end diff --git a/test/swiftMetadataInitializerRealloc.m b/test/swiftMetadataInitializerRealloc.m new file mode 100644 index 0000000..c50d1dc --- /dev/null +++ b/test/swiftMetadataInitializerRealloc.m @@ -0,0 +1,176 @@ +/* +TEST_CONFIG MEM=mrc +TEST_BUILD + $C{COMPILE} $DIR/swiftMetadataInitializerRealloc-dylib1.m -o libswiftMetadataInitializerRealloc-dylib1.dylib -dynamiclib -Wno-deprecated-objc-pointer-introspection + $C{COMPILE} $DIR/swiftMetadataInitializerRealloc-dylib2.m -o libswiftMetadataInitializerRealloc-dylib2.dylib -dynamiclib -L. -lswiftMetadataInitializerRealloc-dylib1 + $C{COMPILE} $DIR/swiftMetadataInitializerRealloc.m -o swiftMetadataInitializerRealloc.exe -L. -lswiftMetadataInitializerRealloc-dylib1 -Wno-deprecated-objc-pointer-introspection +END +*/ + +#include "test.h" +#include "swift-class-def.m" + + +// _objc_swiftMetadataInitializer hooks for the classes in swift-class-def.m + +Class initSuper(Class cls __unused, void *arg __unused) +{ + // This test provokes objc's callback out of superclass order. + // SwiftSub's init is first. SwiftSuper's init is never called. + + fail("SwiftSuper's init should not have been called"); +} + +bool isRealized(Class cls) +{ + // check the is-realized bits directly + +#if __LP64__ +# define mask (~(uintptr_t)7) +#else +# define mask (~(uintptr_t)3) +#endif +#define RW_REALIZED (1<<31) + + uintptr_t rw = ((uintptr_t *)cls)[4] & mask; // class_t->data + return ((uint32_t *)rw)[0] & RW_REALIZED; // class_rw_t->flags +} + +SWIFT_CLASS(SwiftSuper, NSObject, initSuper); +SWIFT_CLASS(RealSwiftSub, SwiftSuper, initSub); + +SWIFT_STUB_CLASS(SwiftSub, initSub); + +OBJC_EXPORT _Nullable Class +objc_loadClassref(_Nullable Class * _Nonnull clsref); + +static int SubInits = 0; +Class initSub(Class cls, void *arg) +{ + testprintf("initSub callback\n"); + + testassert(SubInits == 0); + SubInits++; + testassert(arg == nil); + testassert(cls == RawSwiftSub); + testassert(!isRealized(RawSwiftSuper)); + + // Copy the class to the heap to ensure they're registered properly. + // Classes in the data segment are automatically "known" even if not + // added as a known class. Swift dynamically allocates classes from + // a statically allocated space in the dylib, then allocates from + // the heap after it runs out of room there. Code that only works + // when the class is in a dylib can fail a long time down the road + // when something finally exceeds the capacity of that space. + // Example: rdar://problem/50707074 + Class HeapSwiftSub = (Class)malloc(OBJC_MAX_CLASS_SIZE); + memcpy(HeapSwiftSub, RawRealSwiftSub, OBJC_MAX_CLASS_SIZE); + + testprintf("initSub beginning _objc_realizeClassFromSwift\n"); + _objc_realizeClassFromSwift(HeapSwiftSub, cls); + testprintf("initSub finished _objc_realizeClassFromSwift\n"); + + testassert(isRealized(RawSwiftSuper)); + testassert(isRealized(HeapSwiftSub)); + + testprintf("Returning reallocated class %p\n", HeapSwiftSub); + + return HeapSwiftSub; +} + + +@interface SwiftSub (Addition) +- (int)number; +@end +@implementation SwiftSub (Addition) +- (int)number { return 42; } +@end + +@interface NSObject (DylibCategories) +- (const char *)dylib1ACategoryInSameDylib; +- (const char *)dylib1BCategoryInSameDylib; +- (const char *)dylib1ACategoryInOtherDylib; +- (const char *)dylib1BCategoryInOtherDylib; +- (const char *)dylib1ACategoryInApp; +- (const char *)dylib1BCategoryInApp; ++ (const char *)dylib1ACategoryInAppClassMethod; ++ (const char *)dylib1BCategoryInAppClassMethod; ++ (void)testFromOtherDylib; +@end + +extern int Dylib1AInits; +extern int Dylib1BInits; +SWIFT_STUB_CLASSREF(SwiftDylib1A); +SWIFT_STUB_CLASSREF(SwiftDylib1B); +void Dylib1Test(void); + +@interface SwiftDylib1A: NSObject @end +@interface SwiftDylib1B: NSObject @end + +@implementation SwiftDylib1A (Category) +- (const char *)dylib1ACategoryInApp { return "dylib1ACategoryInApp"; } ++ (const char *)dylib1ACategoryInAppClassMethod { return "dylib1ACategoryInAppClassMethod"; } +@end +@implementation SwiftDylib1B (Category) +- (const char *)dylib1BCategoryInApp { return "dylib1BCategoryInApp"; } ++ (const char *)dylib1BCategoryInAppClassMethod { return "dylib1BCategoryInAppClassMethod"; } +@end + + +int main() +{ +#define LOG(fmt, expr) testprintf(#expr " is " #fmt "\n", expr); + LOG(%p, SwiftSubClassref); + Class loadedSwiftSub = objc_loadClassref(&SwiftSubClassref); + LOG(%p, SwiftSubClassref); + LOG(%p, loadedSwiftSub); + LOG(%p, [loadedSwiftSub class]); + LOG(%p, [loadedSwiftSub superclass]); + LOG(%p, [RawSwiftSuper class]); + + id obj = [[loadedSwiftSub alloc] init]; + LOG(%p, obj); + LOG(%d, [obj number]); + + LOG(%p, SwiftDylib1AClassref); + testassert(Dylib1AInits == 0); + testassert((uintptr_t)SwiftDylib1AClassref & 1); + Class SwiftDylib1A = objc_loadClassref(&SwiftDylib1AClassref); + testassert(((uintptr_t)SwiftDylib1AClassref & 1) == 0); + testassert(SwiftDylib1A == [SwiftDylib1A class]); + testassert(SwiftDylib1A == SwiftDylib1AClassref); + testassert(Dylib1AInits == 1); + LOG(%p, SwiftDylib1A); + + LOG(%p, SwiftDylib1BClassref); + testassert(Dylib1BInits == 0); + testassert((uintptr_t)SwiftDylib1BClassref & 1); + Class SwiftDylib1B = objc_loadClassref(&SwiftDylib1BClassref); + testassert(((uintptr_t)SwiftDylib1BClassref & 1) == 0); + testassert(SwiftDylib1B == [SwiftDylib1B class]); + testassert(SwiftDylib1B == SwiftDylib1BClassref); + testassert(Dylib1BInits == 1); + LOG(%p, SwiftDylib1B); + + Dylib1Test(); + + testassert(strcmp([[SwiftDylib1A new] dylib1ACategoryInSameDylib], "dylib1ACategoryInSameDylib") == 0); + testassert(strcmp([[SwiftDylib1B new] dylib1BCategoryInSameDylib], "dylib1BCategoryInSameDylib") == 0); + testassert(strcmp([[SwiftDylib1A new] dylib1ACategoryInApp], "dylib1ACategoryInApp") == 0); + testassert(strcmp([[SwiftDylib1B new] dylib1BCategoryInApp], "dylib1BCategoryInApp") == 0); + + void *handle = dlopen("libswiftMetadataInitializerRealloc-dylib2.dylib", RTLD_LAZY); + testassert(handle); + + testassert(strcmp([[SwiftDylib1A new] dylib1ACategoryInOtherDylib], "dylib1ACategoryInOtherDylib") == 0); + testassert(strcmp([[SwiftDylib1B new] dylib1BCategoryInOtherDylib], "dylib1BCategoryInOtherDylib") == 0); + testassert(strcmp([SwiftDylib1A dylib1ACategoryInAppClassMethod], "dylib1ACategoryInAppClassMethod") == 0); + testassert(strcmp([SwiftDylib1B dylib1BCategoryInAppClassMethod], "dylib1BCategoryInAppClassMethod") == 0); + [SwiftDylib1A testFromOtherDylib]; + + testassert(objc_getClass("RealSwiftSub")); + testassert(objc_getClass("RealSwiftDylib1A")); + testassert(objc_getClass("RealSwiftDylib1B")); + + succeed(__FILE__); +} diff --git a/test/synchronized-counter.m b/test/synchronized-counter.m new file mode 100644 index 0000000..7d3fd2d --- /dev/null +++ b/test/synchronized-counter.m @@ -0,0 +1,88 @@ +// TEST_CONFIG + +#include "test.h" + +#include +#include +#include +#include +#include +#include + +// synchronized stress test +// Single locked counter incremented by many threads. + +#if defined(__arm__) +#define THREADS 16 +#define COUNT 1024*24 +#else +// 64 / 1024*24 test takes about 20s on 4x2.6GHz Mac Pro +#define THREADS 64 +#define COUNT 1024*24 +#endif + +static id lock; +static int count; + +static void *threadfn(void *arg) +{ + int n, d; + int depth = 1 + (int)(intptr_t)arg % 4; + + for (n = 0; n < COUNT; n++) { + // Lock + for (d = 0; d < depth; d++) { + int err = objc_sync_enter(lock); + testassert(err == OBJC_SYNC_SUCCESS); + } + + // Increment + count++; + + // Unlock + for (d = 0; d < depth; d++) { + int err = objc_sync_exit(lock); + testassert(err == OBJC_SYNC_SUCCESS); + } + } + + // Verify lack of objc pthread data (should have used sync fast cache) +#ifdef __PTK_FRAMEWORK_OBJC_KEY0 + testassert(! pthread_getspecific(__PTK_FRAMEWORK_OBJC_KEY0)); +#endif + + return NULL; +} + +int main() +{ + pthread_t threads[THREADS]; + int t; + int err; + + lock = [[NSObject alloc] init]; + + // Verify objc pthread data on this thread (from +initialize) + // Worker threads shouldn't have any because of sync fast cache. +#ifdef __PTK_FRAMEWORK_OBJC_KEY0 + testassert(pthread_getspecific(__PTK_FRAMEWORK_OBJC_KEY0)); +#endif + + // Start the threads + for (t = 0; t < THREADS; t++) { + pthread_create(&threads[t], NULL, &threadfn, (void*)(intptr_t)t); + } + + // Wait for threads to finish + for (t = 0; t < THREADS; t++) { + pthread_join(threads[t], NULL); + } + + // Verify lock: should be available + // Verify count: should be THREADS*COUNT + err = objc_sync_enter(lock); + testassert(err == OBJC_SYNC_SUCCESS); + testassert(count == THREADS*COUNT); + + succeed(__FILE__); +} diff --git a/test/synchronized-grid.m b/test/synchronized-grid.m new file mode 100644 index 0000000..47f7ccc --- /dev/null +++ b/test/synchronized-grid.m @@ -0,0 +1,112 @@ +// TEST_CONFIG + +#include "test.h" + +#include +#include +#include +#include +#include + +// synchronized stress test +// 2-D grid of counters and locks. +// Each thread increments all counters some number of times. +// To increment: +// * thread picks a target [row][col] +// * thread locks all locks [row][0] to [row][col], possibly recursively +// * thread increments counter [row][col] +// * thread unlocks all of the locks + +#if defined(__arm__) +// 16 / 4 / 3 / 1024*8 test takes about 30s on 2nd gen iPod touch +#define THREADS 16 +#define ROWS 4 +#define COLS 3 +#define COUNT 1024*8 +#else +// 64 / 4 / 3 / 1024*8 test takes about 20s on 4x2.6GHz Mac Pro +#define THREADS 64 +#define ROWS 4 +#define COLS 3 +#define COUNT 1024*8 +#endif + +static id locks[ROWS][COLS]; +static int counts[ROWS][COLS]; + + +static void *threadfn(void *arg) +{ + int n, d; + int depth = 1 + (int)(intptr_t)arg % 4; + + for (n = 0; n < COUNT; n++) { + int rrr = rand() % ROWS; + int ccc = rand() % COLS; + int rr, cc; + for (rr = 0; rr < ROWS; rr++) { + int r = (rrr+rr) % ROWS; + for (cc = 0; cc < COLS; cc++) { + int c = (ccc+cc) % COLS; + int l; + + // Lock [r][0..c] + // ... in that order to prevent deadlock + for (l = 0; l <= c; l++) { + for (d = 0; d < depth; d++) { + int err = objc_sync_enter(locks[r][l]); + testassert(err == OBJC_SYNC_SUCCESS); + } + } + + // Increment count [r][c] + counts[r][c]++; + + // Unlock [r][0..c] + // ... in that order to increase contention + for (l = 0; l <= c; l++) { + for (d = 0; d < depth; d++) { + int err = objc_sync_exit(locks[r][l]); + testassert(err == OBJC_SYNC_SUCCESS); + } + } + } + } + } + + return NULL; +} + +int main() +{ + pthread_t threads[THREADS]; + int r, c, t; + + for (r = 0; r < ROWS; r++) { + for (c = 0; c < COLS; c++) { + locks[r][c] = [[NSObject alloc] init]; + } + } + + // Start the threads + for (t = 0; t < THREADS; t++) { + pthread_create(&threads[t], NULL, &threadfn, (void*)(intptr_t)t); + } + + // Wait for threads to finish + for (t = 0; t < THREADS; t++) { + pthread_join(threads[t], NULL); + } + + // Verify locks: all should be available + // Verify counts: all should be THREADS*COUNT + for (r = 0; r < ROWS; r++) { + for (c = 0; c < COLS; c++) { + int err = objc_sync_enter(locks[r][c]); + testassert(err == OBJC_SYNC_SUCCESS); + testassert(counts[r][c] == THREADS*COUNT); + } + } + + succeed(__FILE__); +} diff --git a/test/synchronized.m b/test/synchronized.m new file mode 100644 index 0000000..2de5406 --- /dev/null +++ b/test/synchronized.m @@ -0,0 +1,114 @@ +// TEST_CONFIG + +#include "test.h" + +#include +#include +#include +#include +#include +#include + +// Basic @synchronized tests. + + +#define WAIT_SEC 3 + +static id obj; +static semaphore_t go; +static semaphore_t stop; + +void *thread(void *arg __unused) +{ + int err; + BOOL locked; + + // non-blocking sync_enter + err = objc_sync_enter(obj); + testassert(err == OBJC_SYNC_SUCCESS); + + // recursive try_sync_enter + locked = objc_sync_try_enter(obj); + testassert(locked); + err = objc_sync_exit(obj); + testassert(err == OBJC_SYNC_SUCCESS); + + semaphore_signal(go); + // main thread: sync_exit of object locked on some other thread + semaphore_wait(stop); + + err = objc_sync_exit(obj); + testassert(err == OBJC_SYNC_SUCCESS); + err = objc_sync_enter(obj); + testassert(err == OBJC_SYNC_SUCCESS); + + semaphore_signal(go); + // main thread: blocking sync_enter + testassert(WAIT_SEC/3*3 == WAIT_SEC); + sleep(WAIT_SEC/3); + // recursive enter while someone waits + err = objc_sync_enter(obj); + testassert(err == OBJC_SYNC_SUCCESS); + sleep(WAIT_SEC/3); + // recursive exit while someone waits + err = objc_sync_exit(obj); + testassert(err == OBJC_SYNC_SUCCESS); + sleep(WAIT_SEC/3); + // sync_exit while someone waits + err = objc_sync_exit(obj); + testassert(err == OBJC_SYNC_SUCCESS); + + return NULL; +} + +int main() +{ + pthread_t th; + int err; + struct timeval start, end; + BOOL locked; + + obj = [[NSObject alloc] init]; + + // sync_exit of never-locked object + err = objc_sync_exit(obj); + testassert(err == OBJC_SYNC_NOT_OWNING_THREAD_ERROR); + + semaphore_create(mach_task_self(), &go, 0, 0); + semaphore_create(mach_task_self(), &stop, 0, 0); + pthread_create(&th, NULL, &thread, NULL); + semaphore_wait(go); + + // sync_exit of object locked on some other thread + err = objc_sync_exit(obj); + testassert(err == OBJC_SYNC_NOT_OWNING_THREAD_ERROR); + + semaphore_signal(stop); + semaphore_wait(go); + + // contended try_sync_enter + locked = objc_sync_try_enter(obj); + testassert(!locked); + + // blocking sync_enter + gettimeofday(&start, NULL); + err = objc_sync_enter(obj); + gettimeofday(&end, NULL); + testassert(err == OBJC_SYNC_SUCCESS); + // should have waited more than WAIT_SEC but less than WAIT_SEC+1 + // fixme hack: sleep(1) is ending 500 usec too early on x86_64 buildbot + // (rdar://6456975) + testassert(end.tv_sec*1000000LL+end.tv_usec >= + start.tv_sec*1000000LL+start.tv_usec + WAIT_SEC*1000000LL + - 3*500 /*hack*/); + testassert(end.tv_sec*1000000LL+end.tv_usec < + start.tv_sec*1000000LL+start.tv_usec + (1+WAIT_SEC)*1000000LL); + + err = objc_sync_exit(obj); + testassert(err == OBJC_SYNC_SUCCESS); + + err = objc_sync_exit(obj); + testassert(err == OBJC_SYNC_NOT_OWNING_THREAD_ERROR); + + succeed(__FILE__); +} diff --git a/test/taggedNSPointers.m b/test/taggedNSPointers.m new file mode 100644 index 0000000..86efb90 --- /dev/null +++ b/test/taggedNSPointers.m @@ -0,0 +1,80 @@ +// TEST_CFLAGS -framework Foundation + +#include "test.h" +#include +#import + +#if OBJC_HAVE_TAGGED_POINTERS + +void testTaggedNumber() +{ + NSNumber *taggedNS = [NSNumber numberWithInt: 1234]; + CFNumberRef taggedCF = (__bridge CFNumberRef)taggedNS; + int result; + + testassert( CFGetTypeID(taggedCF) == CFNumberGetTypeID() ); + testassert(_objc_getClassForTag(OBJC_TAG_NSNumber) == [taggedNS class]); + + CFNumberGetValue(taggedCF, kCFNumberIntType, &result); + testassert(result == 1234); + + testassert(_objc_isTaggedPointer(taggedCF)); + testassert(_objc_getTaggedPointerTag(taggedCF) == OBJC_TAG_NSNumber); + testassert(_objc_makeTaggedPointer(_objc_getTaggedPointerTag(taggedCF), _objc_getTaggedPointerValue(taggedCF)) == taggedCF); + + // do some generic object-y things to the taggedPointer instance + CFRetain(taggedCF); + CFRelease(taggedCF); + + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + [dict setObject: taggedNS forKey: @"fred"]; + testassert(taggedNS == [dict objectForKey: @"fred"]); + [dict setObject: @"bob" forKey: taggedNS]; + testassert([@"bob" isEqualToString: [dict objectForKey: taggedNS]]); + + NSNumber *iM88 = [NSNumber numberWithInt:-88]; + NSNumber *i12346 = [NSNumber numberWithInt: 12346]; + NSNumber *i12347 = [NSNumber numberWithInt: 12347]; + + NSArray *anArray = [NSArray arrayWithObjects: iM88, i12346, i12347, nil]; + testassert([anArray count] == 3); + testassert([anArray indexOfObject: i12346] == 1); + + NSSet *aSet = [NSSet setWithObjects: iM88, i12346, i12347, nil]; + testassert([aSet count] == 3); + testassert([aSet containsObject: i12346]); + + [taggedNS performSelector: @selector(intValue)]; + testassert(![taggedNS isProxy]); + testassert([taggedNS isKindOfClass: [NSNumber class]]); + testassert([taggedNS respondsToSelector: @selector(intValue)]); + + (void)[taggedNS description]; +} + +int main() +{ + PUSH_POOL { + testTaggedNumber(); // should be tested by CF... our tests are wrong, wrong, wrong. + } POP_POOL; + + succeed(__FILE__); +} + +// OBJC_HAVE_TAGGED_POINTERS +#else +// not OBJC_HAVE_TAGGED_POINTERS + +// Tagged pointers not supported. Crash if an NSNumber actually +// is a tagged pointer (which means this test is out of date). + +int main() +{ + PUSH_POOL { + testassert(*(void **)(__bridge void *)[NSNumber numberWithInt:1234]); + } POP_POOL; + + succeed(__FILE__); +} + +#endif diff --git a/test/taggedPointers.m b/test/taggedPointers.m new file mode 100644 index 0000000..76f1617 --- /dev/null +++ b/test/taggedPointers.m @@ -0,0 +1,356 @@ +// TEST_CFLAGS -fobjc-weak + +#include "test.h" +#include +#include +#include +#include +#import + +#if OBJC_HAVE_TAGGED_POINTERS + +#if !__x86_64__ && !__arm64__ +#error wrong architecture for tagged pointers +#endif + +static BOOL didIt; + +@interface WeakContainer : NSObject +{ + @public + __weak id weaks[10000]; +} +@end +@implementation WeakContainer +-(void) dealloc { + for (unsigned int i = 0; i < sizeof(weaks)/sizeof(weaks[0]); i++) { + testassert(weaks[i] == nil); + } + SUPER_DEALLOC(); +} +@end + +OBJC_ROOT_CLASS +@interface TaggedBaseClass60 +@end + +@implementation TaggedBaseClass60 +-(id) self { return self; } + ++ (void) initialize { +} + +- (void) instanceMethod { + didIt = YES; +} + +- (uintptr_t) taggedValue { + return _objc_getTaggedPointerValue((__bridge void*)self); +} + +- (struct stret) stret: (struct stret) aStruct { + return aStruct; +} + +- (long double) fpret: (long double) aValue { + return aValue; +} + + +-(void) dealloc { + fail("TaggedBaseClass60 dealloc called!"); +} + +static void * +retain_fn(void *self, SEL _cmd __unused) { + void * (*fn)(void *) = (typeof(fn))_objc_rootRetain; + return fn(self); +} + +static void +release_fn(void *self, SEL _cmd __unused) { + void (*fn)(void *) = (typeof(fn))_objc_rootRelease; + fn(self); +} + +static void * +autorelease_fn(void *self, SEL _cmd __unused) { + void * (*fn)(void *) = (typeof(fn))_objc_rootAutorelease; + return fn(self); +} + +static unsigned long +retaincount_fn(void *self, SEL _cmd __unused) { + unsigned long (*fn)(void *) = (typeof(fn))_objc_rootRetainCount; + return fn(self); +} + ++(void) load { + class_addMethod(self, sel_registerName("retain"), (IMP)retain_fn, ""); + class_addMethod(self, sel_registerName("release"), (IMP)release_fn, ""); + class_addMethod(self, sel_registerName("autorelease"), (IMP)autorelease_fn, ""); + class_addMethod(self, sel_registerName("retainCount"), (IMP)retaincount_fn, ""); +} + +@end + +@interface TaggedSubclass52: TaggedBaseClass60 +@end + +@implementation TaggedSubclass52 + +- (void) instanceMethod { + return [super instanceMethod]; +} + +- (uintptr_t) taggedValue { + return [super taggedValue]; +} + +- (struct stret) stret: (struct stret) aStruct { + return [super stret: aStruct]; +} + +- (long double) fpret: (long double) aValue { + return [super fpret: aValue]; +} +@end + +@interface TaggedNSObjectSubclass : NSObject +@end + +@implementation TaggedNSObjectSubclass + +- (void) instanceMethod { + didIt = YES; +} + +- (uintptr_t) taggedValue { + return _objc_getTaggedPointerValue((__bridge void*)self); +} + +- (struct stret) stret: (struct stret) aStruct { + return aStruct; +} + +- (long double) fpret: (long double) aValue { + return aValue; +} +@end + +void testTaggedPointerValue(Class cls, objc_tag_index_t tag, uintptr_t value) +{ + void *taggedAddress = _objc_makeTaggedPointer(tag, value); + testprintf("obj %p, tag %p, value %p\n", + taggedAddress, (void*)tag, (void*)value); + + bool ext = (tag >= OBJC_TAG_First52BitPayload); + + // _objc_makeTaggedPointer must quietly mask out of range values for now + if (ext) { + value = (value << 12) >> 12; + } else { + value = (value << 4) >> 4; + } + + testassert(_objc_isTaggedPointer(taggedAddress)); + testassert(_objc_getTaggedPointerTag(taggedAddress) == tag); + testassert(_objc_getTaggedPointerValue(taggedAddress) == value); + testassert(objc_debug_taggedpointer_obfuscator != 0); + + if (ext) { + uintptr_t slot = ((uintptr_t)taggedAddress >> objc_debug_taggedpointer_ext_slot_shift) & objc_debug_taggedpointer_ext_slot_mask; + testassert(objc_debug_taggedpointer_ext_classes[slot] == cls); + uintptr_t deobfuscated = (uintptr_t)taggedAddress ^ objc_debug_taggedpointer_obfuscator; + testassert(((deobfuscated << objc_debug_taggedpointer_ext_payload_lshift) >> objc_debug_taggedpointer_ext_payload_rshift) == value); + } + else { + testassert(((uintptr_t)taggedAddress & objc_debug_taggedpointer_mask) == objc_debug_taggedpointer_mask); + uintptr_t slot = ((uintptr_t)taggedAddress >> objc_debug_taggedpointer_slot_shift) & objc_debug_taggedpointer_slot_mask; + testassert(objc_debug_taggedpointer_classes[slot] == cls); + uintptr_t deobfuscated = (uintptr_t)taggedAddress ^ objc_debug_taggedpointer_obfuscator; + testassert(((deobfuscated << objc_debug_taggedpointer_payload_lshift) >> objc_debug_taggedpointer_payload_rshift) == value); + } + + id taggedPointer = (__bridge id)taggedAddress; + testassert(!object_isClass(taggedPointer)); + testassert(object_getClass(taggedPointer) == cls); + testassert([taggedPointer taggedValue] == value); + + didIt = NO; + [taggedPointer instanceMethod]; + testassert(didIt); + + struct stret orig = STRET_RESULT; + testassert(stret_equal(orig, [taggedPointer stret: orig])); + + long double dblvalue = 3.14156789; + testassert(dblvalue == [taggedPointer fpret: dblvalue]); + + objc_setAssociatedObject(taggedPointer, (__bridge void *)taggedPointer, taggedPointer, OBJC_ASSOCIATION_RETAIN); + testassert(objc_getAssociatedObject(taggedPointer, (__bridge void *)taggedPointer) == taggedPointer); + objc_setAssociatedObject(taggedPointer, (__bridge void *)taggedPointer, nil, OBJC_ASSOCIATION_RETAIN); + testassert(objc_getAssociatedObject(taggedPointer, (__bridge void *)taggedPointer) == nil); +} + +void testGenericTaggedPointer(objc_tag_index_t tag, Class cls) +{ + testassert(cls); + testprintf("%s\n", class_getName(cls)); + + testTaggedPointerValue(cls, tag, 0); + testTaggedPointerValue(cls, tag, 1UL << 0); + testTaggedPointerValue(cls, tag, 1UL << 1); + testTaggedPointerValue(cls, tag, 1UL << 50); + testTaggedPointerValue(cls, tag, 1UL << 51); + testTaggedPointerValue(cls, tag, 1UL << 52); + testTaggedPointerValue(cls, tag, 1UL << 58); + testTaggedPointerValue(cls, tag, 1UL << 59); + testTaggedPointerValue(cls, tag, ~0UL >> 4); + testTaggedPointerValue(cls, tag, ~0UL); + + // Tagged pointers should bypass refcount tables and autorelease pools + // and weak reference tables + WeakContainer *w = [WeakContainer new]; + + // force sidetable retain of the WeakContainer before leak checking + objc_retain(w); +#if !__has_feature(objc_arc) + // prime method caches before leak checking + id taggedPointer = (id)_objc_makeTaggedPointer(tag, 1234); + [taggedPointer retain]; + [taggedPointer release]; + [taggedPointer autorelease]; +#endif + // prime is_debug() before leak checking + (void)is_debug(); + + leak_mark(); + testonthread(^(void) { + for (uintptr_t i = 0; i < sizeof(w->weaks)/sizeof(w->weaks[0]); i++) { + id o = (__bridge id)_objc_makeTaggedPointer(tag, i); + testassert(object_getClass(o) == cls); + + id result = WEAK_STORE(w->weaks[i], o); + testassert(result == o); + testassert(w->weaks[i] == o); + + result = WEAK_LOAD(w->weaks[i]); + testassert(result == o); + + uintptr_t rc = _objc_rootRetainCount(o); + testassert(rc != 0); + _objc_rootRelease(o); testassert(_objc_rootRetainCount(o) == rc); + _objc_rootRelease(o); testassert(_objc_rootRetainCount(o) == rc); + _objc_rootRetain(o); testassert(_objc_rootRetainCount(o) == rc); + _objc_rootRetain(o); testassert(_objc_rootRetainCount(o) == rc); + _objc_rootRetain(o); testassert(_objc_rootRetainCount(o) == rc); +#if !__has_feature(objc_arc) + [o release]; testassert(_objc_rootRetainCount(o) == rc); + [o release]; testassert(_objc_rootRetainCount(o) == rc); + [o retain]; testassert(_objc_rootRetainCount(o) == rc); + [o retain]; testassert(_objc_rootRetainCount(o) == rc); + [o retain]; testassert(_objc_rootRetainCount(o) == rc); + objc_release(o); testassert(_objc_rootRetainCount(o) == rc); + objc_release(o); testassert(_objc_rootRetainCount(o) == rc); + objc_retain(o); testassert(_objc_rootRetainCount(o) == rc); + objc_retain(o); testassert(_objc_rootRetainCount(o) == rc); + objc_retain(o); testassert(_objc_rootRetainCount(o) == rc); +#endif + PUSH_POOL { + testassert(_objc_rootRetainCount(o) == rc); + _objc_rootAutorelease(o); + testassert(_objc_rootRetainCount(o) == rc); +#if !__has_feature(objc_arc) + [o autorelease]; + testassert(_objc_rootRetainCount(o) == rc); + objc_autorelease(o); + testassert(_objc_rootRetainCount(o) == rc); + objc_retainAutorelease(o); + testassert(_objc_rootRetainCount(o) == rc); + objc_autoreleaseReturnValue(o); + testassert(_objc_rootRetainCount(o) == rc); + objc_retainAutoreleaseReturnValue(o); + testassert(_objc_rootRetainCount(o) == rc); + objc_retainAutoreleasedReturnValue(o); + testassert(_objc_rootRetainCount(o) == rc); +#endif + } POP_POOL; + testassert(_objc_rootRetainCount(o) == rc); + } + }); + if (is_debug()) { + // libobjc's debug lock checking makes this leak check fail + testwarn("skipping leak check with debug libobjc build"); + } else { + leak_check(0); + } + for (uintptr_t i = 0; i < 10000; i++) { + testassert(w->weaks[i] != NULL); + WEAK_STORE(w->weaks[i], NULL); + testassert(w->weaks[i] == NULL); + testassert(WEAK_LOAD(w->weaks[i]) == NULL); + } + objc_release(w); + RELEASE_VAR(w); +} + +int main() +{ + testassert(objc_debug_taggedpointer_mask != 0); + testassert(_objc_taggedPointersEnabled()); + + PUSH_POOL { + // Avoid CF's tagged pointer tags because of rdar://11368528 + + // Reserved slot should be nil until the + // first extended tag is registered. + // This test no longer works because XPC now uses extended tags. +#define HAVE_XPC_TAGS 1 + + uintptr_t extSlot = (~objc_debug_taggedpointer_obfuscator >> objc_debug_taggedpointer_slot_shift) & objc_debug_taggedpointer_slot_mask; + Class extPlaceholder = objc_getClass("__NSUnrecognizedTaggedPointer"); + testassert(extPlaceholder != nil); + +#if !HAVE_XPC_TAGS + testassert(objc_debug_taggedpointer_classes[extSlot] == nil); +#endif + + _objc_registerTaggedPointerClass(OBJC_TAG_1, + objc_getClass("TaggedBaseClass60")); + testGenericTaggedPointer(OBJC_TAG_1, + objc_getClass("TaggedBaseClass60")); + +#if !HAVE_XPC_TAGS + testassert(objc_debug_taggedpointer_classes[extSlot] == nil); +#endif + + _objc_registerTaggedPointerClass(OBJC_TAG_First52BitPayload, + objc_getClass("TaggedSubclass52")); + testGenericTaggedPointer(OBJC_TAG_First52BitPayload, + objc_getClass("TaggedSubclass52")); + + testassert(objc_debug_taggedpointer_classes[extSlot] == extPlaceholder); + + _objc_registerTaggedPointerClass(OBJC_TAG_NSManagedObjectID, + objc_getClass("TaggedNSObjectSubclass")); + testGenericTaggedPointer(OBJC_TAG_NSManagedObjectID, + objc_getClass("TaggedNSObjectSubclass")); + } POP_POOL; + + succeed(__FILE__); +} + +// OBJC_HAVE_TAGGED_POINTERS +#else +// not OBJC_HAVE_TAGGED_POINTERS + +// Tagged pointers not supported. + +int main() +{ + testassert(objc_debug_taggedpointer_mask == 0); + succeed(__FILE__); +} + +#endif diff --git a/test/taggedPointersAllClasses.m b/test/taggedPointersAllClasses.m new file mode 100644 index 0000000..9c02840 --- /dev/null +++ b/test/taggedPointersAllClasses.m @@ -0,0 +1,86 @@ +// TEST_CONFIG + +#include "test.h" +#include "testroot.i" +#include +#include + +#if OBJC_HAVE_TAGGED_POINTERS + +@interface TagSuperclass: TestRoot + +- (void)test; + +@end + +@implementation TagSuperclass + +- (void)test {} + +@end + +int main() +{ + Class classes[OBJC_TAG_Last52BitPayload + 1] = {}; + + __block uintptr_t expectedPayload; + __block uintptr_t sawPayload; + __block int sawTag; + + for (int i = 0; i <= OBJC_TAG_Last52BitPayload; i++) { + objc_tag_index_t tag = (objc_tag_index_t)i; + if (i > OBJC_TAG_Last60BitPayload && i < OBJC_TAG_First52BitPayload) + continue; + if (_objc_getClassForTag(tag) != nil) + continue; + + char *name; + asprintf(&name, "Tag%d", i); + classes[i] = objc_allocateClassPair([TagSuperclass class], name, 0); + free(name); + + IMP testIMP = imp_implementationWithBlock(^(void *self) { + testassert(i == _objc_getTaggedPointerTag(self)); + testassert(expectedPayload == _objc_getTaggedPointerValue(self)); + sawPayload = _objc_getTaggedPointerValue(self); + sawTag = i; + }); + class_addMethod(classes[i], @selector(test), testIMP, "v@@"); + + objc_registerClassPair(classes[i]); + _objc_registerTaggedPointerClass(tag, classes[i]); + } + + for (int i = 0; i <= OBJC_TAG_Last52BitPayload; i++) { + objc_tag_index_t tag = (objc_tag_index_t)i; + if (classes[i] == nil) + continue; + + for (int byte = 0; byte <= 0xff; byte++) { + uintptr_t payload; + memset(&payload, byte, sizeof(payload)); + + if (i <= OBJC_TAG_Last60BitPayload) + payload >>= _OBJC_TAG_PAYLOAD_RSHIFT; + else + payload >>= _OBJC_TAG_EXT_PAYLOAD_RSHIFT; + + expectedPayload = payload; + id obj = (__bridge id)_objc_makeTaggedPointer(tag, payload); + [obj test]; + testassert(sawPayload == payload); + testassert(sawTag == i); + } + } + + succeed(__FILE__); +} + +#else + +int main() +{ + succeed(__FILE__); +} + +#endif diff --git a/test/taggedPointersDisabled.m b/test/taggedPointersDisabled.m new file mode 100644 index 0000000..958cc7d --- /dev/null +++ b/test/taggedPointersDisabled.m @@ -0,0 +1,39 @@ +/* +TEST_ENV OBJC_DISABLE_TAGGED_POINTERS=YES +TEST_CRASHES + +TEST_BUILD_OUTPUT +.*taggedPointersDisabled.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\] +END + +TEST_RUN_OUTPUT +objc\[\d+\]: tagged pointers are disabled +objc\[\d+\]: HALTED +OR +OK: taggedPointersDisabled.m +END +*/ + +#include "test.h" +#include + +#if !OBJC_HAVE_TAGGED_POINTERS + +int main() +{ + // provoke the same nullability warning as the real test + objc_getClass(nil); + + succeed(__FILE__); +} + +#else + +int main() +{ + testassert(!_objc_taggedPointersEnabled()); + _objc_registerTaggedPointerClass((objc_tag_index_t)0, nil); + fail("should have crashed in _objc_registerTaggedPointerClass()"); +} + +#endif diff --git a/test/taggedPointersTagObfuscationDisabled.m b/test/taggedPointersTagObfuscationDisabled.m new file mode 100644 index 0000000..a3aad8b --- /dev/null +++ b/test/taggedPointersTagObfuscationDisabled.m @@ -0,0 +1,21 @@ +// TEST_ENV OBJC_DISABLE_TAG_OBFUSCATION=YES + +#include "test.h" +#include + +#if !OBJC_HAVE_TAGGED_POINTERS + +int main() +{ + succeed(__FILE__); +} + +#else + +int main() +{ + testassert(_objc_getTaggedPointerTag((void *)1) == 0); + succeed(__FILE__); +} + +#endif diff --git a/test/tbi.c b/test/tbi.c new file mode 100644 index 0000000..93c2022 --- /dev/null +++ b/test/tbi.c @@ -0,0 +1,14 @@ +// TEST_CONFIG OS=iphoneos ARCH=arm64 + +#include "test.h" + +#ifndef __arm64__ +#error wrong architecture for TBI hardware feature +#endif + +volatile int x = 123456; + +int main(void) { + testassert(*(int *)((unsigned long)&x | 0xFF00000000000000ul) == 123456); + succeed(__FILE__); +} diff --git a/test/test.h b/test/test.h new file mode 100644 index 0000000..f4332a1 --- /dev/null +++ b/test/test.h @@ -0,0 +1,469 @@ +// test.h +// Common definitions for trivial test harness + + +#ifndef TEST_H +#define TEST_H + +#include +#include +#include +#include +#include +#include +#include +#include +#if __cplusplus +#include +using namespace std; +#else +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if __has_include() +# include +#endif + +#include "../runtime/isa.h" + +#if __cplusplus +# define EXTERN_C extern "C" +#else +# define EXTERN_C /*empty*/ +#endif + + +// Test output + +static inline void succeed(const char *name) __attribute__((noreturn)); +static inline void succeed(const char *name) +{ + if (name) { + char path[MAXPATHLEN+1]; + strcpy(path, name); + fprintf(stderr, "OK: %s\n", basename(path)); + } else { + fprintf(stderr, "OK\n"); + } + exit(0); +} + +static inline void fail(const char *msg, ...) __attribute__((noreturn)); +static inline void fail(const char *msg, ...) +{ + if (msg) { + char *msg2; + asprintf(&msg2, "BAD: %s\n", msg); + va_list v; + va_start(v, msg); + vfprintf(stderr, msg2, v); + va_end(v); + free(msg2); + } else { + fprintf(stderr, "BAD\n"); + } + exit(1); +} + +#define testassert(cond) \ + ((void) (((cond) != 0) ? (void)0 : __testassert(#cond, __FILE__, __LINE__))) +#define __testassert(cond, file, line) \ + (fail("failed assertion '%s' at %s:%u", cond, __FILE__, __LINE__)) + +/* time-sensitive assertion, disabled under valgrind */ +#define timecheck(name, time, fast, slow) \ + if (getenv("VALGRIND") && 0 != strcmp(getenv("VALGRIND"), "NO")) { \ + /* valgrind; do nothing */ \ + } else if (time > slow) { \ + fprintf(stderr, "SLOW: %s %llu, expected %llu..%llu\n", \ + name, (uint64_t)(time), (uint64_t)(fast), (uint64_t)(slow)); \ + } else if (time < fast) { \ + fprintf(stderr, "FAST: %s %llu, expected %llu..%llu\n", \ + name, (uint64_t)(time), (uint64_t)(fast), (uint64_t)(slow)); \ + } else { \ + testprintf("time: %s %llu, expected %llu..%llu\n", \ + name, (uint64_t)(time), (uint64_t)(fast), (uint64_t)(slow)); \ + } + + +// Return true if testprintf() output is enabled. +static inline bool testverbose(void) +{ + static int verbose = -1; + if (verbose < 0) verbose = atoi(getenv("VERBOSE") ?: "0"); + + // VERBOSE=1 prints test harness info only + // VERBOSE=2 prints test info + return verbose >= 2; +} + +// Print debugging info when VERBOSE=2 is set, +// without disturbing the test's expected output. +static inline void testprintf(const char *msg, ...) +{ + if (msg && testverbose()) { + char *msg2; + asprintf(&msg2, "VERBOSE: %s", msg); + va_list v; + va_start(v, msg); + vfprintf(stderr, msg2, v); + va_end(v); + free(msg2); + } +} + +// complain to output, but don't fail the test +// Use when warning that some test is being temporarily skipped +// because of something like a compiler bug. +static inline void testwarn(const char *msg, ...) +{ + if (msg) { + char *msg2; + asprintf(&msg2, "WARN: %s\n", msg); + va_list v; + va_start(v, msg); + vfprintf(stderr, msg2, v); + va_end(v); + free(msg2); + } +} + +static inline void testnoop() { } + +// Prevent deprecation warnings from some runtime functions. + +static inline void test_objc_flush_caches(Class cls) +{ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + _objc_flush_caches(cls); +#pragma clang diagnostic pop +} +#define _objc_flush_caches(c) test_objc_flush_caches(c) + + +static inline Class test_class_setSuperclass(Class cls, Class supercls) +{ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + return class_setSuperclass(cls, supercls); +#pragma clang diagnostic pop +} +#define class_setSuperclass(c, s) test_class_setSuperclass(c, s) + + +static inline void testcollect() +{ + _objc_flush_caches(nil); +} + + +// Synchronously run test code on another thread. + +// The block object is unsafe_unretained because we must not allow +// ARC to retain them in non-Foundation tests +typedef void(^testblock_t)(void); +static __unsafe_unretained testblock_t testcodehack; +static inline void *_testthread(void *arg __unused) +{ + testcodehack(); + return NULL; +} +static inline void testonthread(__unsafe_unretained testblock_t code) +{ + pthread_t th; + testcodehack = code; // force GC not-thread-local, avoid ARC void* casts + pthread_create(&th, NULL, _testthread, NULL); + pthread_join(th, NULL); +} + +/* Make sure libobjc does not call global operator new. + Any test that DOES need to call global operator new must + `#define TEST_CALLS_OPERATOR_NEW` before including test.h. + */ +#if __cplusplus && !defined(TEST_CALLS_OPERATOR_NEW) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Winline-new-delete" +#import +inline void* operator new(std::size_t) throw (std::bad_alloc) { fail("called global operator new"); } +inline void* operator new[](std::size_t) throw (std::bad_alloc) { fail("called global operator new[]"); } +inline void* operator new(std::size_t, const std::nothrow_t&) throw() { fail("called global operator new(nothrow)"); } +inline void* operator new[](std::size_t, const std::nothrow_t&) throw() { fail("called global operator new[](nothrow)"); } +inline void operator delete(void*) throw() { fail("called global operator delete"); } +inline void operator delete[](void*) throw() { fail("called global operator delete[]"); } +inline void operator delete(void*, const std::nothrow_t&) throw() { fail("called global operator delete(nothrow)"); } +inline void operator delete[](void*, const std::nothrow_t&) throw() { fail("called global operator delete[](nothrow)"); } +#pragma clang diagnostic pop +#endif + + +/* Leak checking + Fails if total malloc memory in use at leak_check(n) + is more than n bytes above that at leak_mark(). +*/ + +static inline void leak_recorder(task_t task __unused, void *ctx, unsigned type __unused, vm_range_t *ranges, unsigned count) +{ + size_t *inuse = (size_t *)ctx; + while (count--) { + *inuse += ranges[count].size; + } +} + +static inline size_t leak_inuse(void) +{ + size_t total = 0; + vm_address_t *zones; + unsigned count; + malloc_get_all_zones(mach_task_self(), NULL, &zones, &count); + for (unsigned i = 0; i < count; i++) { + size_t inuse = 0; + malloc_zone_t *zone = (malloc_zone_t *)zones[i]; + if (!zone->introspect || !zone->introspect->enumerator) continue; + + // skip DispatchContinuations because it sometimes claims to be + // using lots of memory that then goes away later + if (0 == strcmp(zone->zone_name, "DispatchContinuations")) continue; + + zone->introspect->enumerator(mach_task_self(), &inuse, MALLOC_PTR_IN_USE_RANGE_TYPE, (vm_address_t)zone, NULL, leak_recorder); + // fprintf(stderr, "%zu in use for zone %s\n", inuse, zone->zone_name); + total += inuse; + } + + return total; +} + + +static inline void leak_dump_heap(const char *msg) +{ + fprintf(stderr, "%s\n", msg); + + // Make `heap` write to stderr + int outfd = dup(STDOUT_FILENO); + dup2(STDERR_FILENO, STDOUT_FILENO); + pid_t pid = getpid(); + char cmd[256]; + // environment variables reset for iOS simulator use + sprintf(cmd, "DYLD_LIBRARY_PATH= DYLD_ROOT_PATH= /usr/bin/heap -addresses all %d", (int)pid); + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + system(cmd); +#pragma clang diagnostic pop + + dup2(outfd, STDOUT_FILENO); + close(outfd); +} + +static size_t _leak_start; +static inline void leak_mark(void) +{ + testcollect(); + if (getenv("LEAK_HEAP")) { + leak_dump_heap("HEAP AT leak_mark"); + } + _leak_start = leak_inuse(); +} + +#define leak_check(n) \ + do { \ + const char *_check = getenv("LEAK_CHECK"); \ + size_t inuse; \ + if (_check && 0 == strcmp(_check, "NO")) break; \ + testcollect(); \ + if (getenv("LEAK_HEAP")) { \ + leak_dump_heap("HEAP AT leak_check"); \ + } \ + inuse = leak_inuse(); \ + if (inuse > _leak_start + n) { \ + fprintf(stderr, "BAD: %zu bytes leaked at %s:%u " \ + "(try LEAK_HEAP and HANG_ON_LEAK to debug)\n", \ + inuse - _leak_start, __FILE__, __LINE__); \ + if (getenv("HANG_ON_LEAK")) { \ + fprintf(stderr, "Hanging after leaks detected. " \ + "Leaks command:\n"); \ + fprintf(stderr, "leaks %d\n", getpid()); \ + while (1) sleep(1); \ + } \ + } \ + } while (0) + +// true when running under Guard Malloc +static inline bool is_guardmalloc(void) +{ + const char *env = getenv("GUARDMALLOC"); + return (env && 0 == strcmp(env, "1")); +} + +// true when running a debug build of libobjc +static inline bool is_debug(void) +{ + static int debugness = -1; + if (debugness == -1) { + debugness = dlsym(RTLD_DEFAULT, "_objc_isDebugBuild") ? 1 : 0; + } + return (bool)debugness; +} + + +/* Memory management compatibility macros */ + +static id self_fn(id x) __attribute__((used)); +static id self_fn(id x) { return x; } + +#if __has_feature(objc_arc_weak) + // __weak +# define WEAK_STORE(dst, val) (dst = (val)) +# define WEAK_LOAD(src) (src) +#else + // no __weak +# define WEAK_STORE(dst, val) objc_storeWeak((id *)&dst, val) +# define WEAK_LOAD(src) objc_loadWeak((id *)&src) +#endif + +#if __has_feature(objc_arc) + // ARC +# define RELEASE_VAR(x) x = nil +# define SUPER_DEALLOC() +# define RETAIN(x) (self_fn(x)) +# define RELEASE_VALUE(x) ((void)self_fn(x)) +# define AUTORELEASE(x) (self_fn(x)) + +#else + // MRC +# define RELEASE_VAR(x) do { [x release]; x = nil; } while (0) +# define SUPER_DEALLOC() [super dealloc] +# define RETAIN(x) [x retain] +# define RELEASE_VALUE(x) [x release] +# define AUTORELEASE(x) [x autorelease] +#endif + +/* gcc compatibility macros */ +/* @autoreleasepool should generate objc_autoreleasePoolPush/Pop on 10.7/5.0 */ +//#if !defined(__clang__) +# define PUSH_POOL { void *pool = objc_autoreleasePoolPush(); +# define POP_POOL objc_autoreleasePoolPop(pool); } +//#else +//# define PUSH_POOL @autoreleasepool +//# define POP_POOL +//#endif + +#if __OBJC__ + +/* General purpose root class */ + +OBJC_ROOT_CLASS +@interface TestRoot { + @public + Class isa; +} + ++(void) load; ++(void) initialize; + +-(id) self; +-(Class) class; +-(Class) superclass; + ++(id) new; ++(id) alloc; ++(id) allocWithZone:(void*)zone; +-(id) copy; +-(id) mutableCopy; +-(id) init; +-(void) dealloc; +@end +@interface TestRoot (RR) +-(id) retain; +-(oneway void) release; +-(id) autorelease; +-(unsigned long) retainCount; +-(id) copyWithZone:(void *)zone; +-(id) mutableCopyWithZone:(void*)zone; +@end + +// incremented for each call of TestRoot's methods +extern atomic_int TestRootLoad; +extern atomic_int TestRootInitialize; +extern atomic_int TestRootAlloc; +extern atomic_int TestRootAllocWithZone; +extern atomic_int TestRootCopy; +extern atomic_int TestRootCopyWithZone; +extern atomic_int TestRootMutableCopy; +extern atomic_int TestRootMutableCopyWithZone; +extern atomic_int TestRootInit; +extern atomic_int TestRootDealloc; +extern atomic_int TestRootRetain; +extern atomic_int TestRootRelease; +extern atomic_int TestRootAutorelease; +extern atomic_int TestRootRetainCount; +extern atomic_int TestRootTryRetain; +extern atomic_int TestRootIsDeallocating; +extern atomic_int TestRootPlusRetain; +extern atomic_int TestRootPlusRelease; +extern atomic_int TestRootPlusAutorelease; +extern atomic_int TestRootPlusRetainCount; + +#endif + + +// Struct that does not return in registers on any architecture + +struct stret { + int a; + int b; + int c; + int d; + int e; + int f; + int g; + int h; + int i; + int j; +}; + +static inline BOOL stret_equal(struct stret a, struct stret b) +{ + return (a.a == b.a && + a.b == b.b && + a.c == b.c && + a.d == b.d && + a.e == b.e && + a.f == b.f && + a.g == b.g && + a.h == b.h && + a.i == b.i && + a.j == b.j); +} + +static struct stret STRET_RESULT __attribute__((used)) = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + + +#if TARGET_OS_SIMULATOR +// Force cwd to executable's directory during launch. +// sim used to do this but simctl does not. +#include + __attribute__((constructor)) +static void hack_cwd(void) +{ + if (!getenv("HACKED_CWD")) { + chdir(dirname((*_NSGetArgv())[0])); + setenv("HACKED_CWD", "1", 1); + } +} +#endif + +#endif diff --git a/test/test.pl b/test/test.pl new file mode 100644 index 0000000..0d0886b --- /dev/null +++ b/test/test.pl @@ -0,0 +1,1738 @@ +#!/usr/bin/perl + +# test.pl +# Run unit tests. + +use strict; +use File::Basename; + +# We use encode_json() to write BATS plist files. +# JSON::PP does not exist on iOS devices, but we need not write plists there. +# So we simply load JSON:PP if it exists. +if (eval { require JSON::PP; 1; }) { + JSON::PP->import(); +} + + +chdir dirname $0; +chomp (my $DIR = `pwd`); + +if (scalar(@ARGV) == 1) { + my $arg = $ARGV[0]; + if ($arg eq "-h" || $arg eq "-H" || $arg eq "-help" || $arg eq "help") { + print(< + OS=[sdk version][-[-]] + ROOT=/path/to/project.roots/ + + CC= + + LANGUAGE=c,c++,objective-c,objective-c++,swift + MEM=mrc,arc + GUARDMALLOC=0|1|before|after + + BUILD=0|1 (build the tests?) + RUN=0|1 (run the tests?) + VERBOSE=0|1|2 (0=quieter 1=print commands executed 2=full test output) + BATS=0|1 (build for and/or run in BATS?) + +examples: + + test installed library, x86_64 + $0 + + test buildit-built root, i386 and x86_64, MRC and ARC, clang compiler + $0 ARCH=i386,x86_64 ROOT=/tmp/objc4.roots MEM=mrc,arc CC=clang + + test buildit-built root with iOS simulator, deploy to iOS 7, run on iOS 8 + $0 ARCH=x86_64 ROOT=/tmp/objc4.roots OS=iphonesimulator-7.0-8.0 + + test buildit-built root on attached iOS device + $0 ARCH=arm64 ROOT=/tmp/objc4.roots OS=iphoneos +END + exit 0; + } +} + +######################################################################### +## Tests + +# Maps test name => test's filename extension. +# ex: "msgSend" => "m" +# `keys %ALL_TESTS` is also used as the list of all tests found on disk. +my %ALL_TESTS; + +######################################################################### +## Variables for use in complex build and run rules + +# variable # example value + +# things you can multiplex on the command line +# ARCH=i386,x86_64,armv6,armv7 +# OS=macosx,iphoneos,iphonesimulator (plus sdk/deployment/run versions) +# LANGUAGE=c,c++,objective-c,objective-c++,swift +# CC=clang +# MEM=mrc,arc +# GUARDMALLOC=0,1,before,after + +# things you can set once on the command line +# ROOT=/path/to/project.roots +# BUILD=0|1 +# RUN=0|1 +# VERBOSE=0|1|2 +# BATS=0|1 + +# environment variables from the command line +# DSTROOT +# OBJROOT +# (SRCROOT is ignored; test sources are assumed to +# be in the same directory as the test script itself.) +# fixme SYMROOT for dsymutil output? + + +# Some arguments as read from the command line. +my %args; +my $BUILD; +my $RUN; +my $VERBOSE; +my $BATS; + +my $HOST; +my $PORT; + +my @TESTLIBNAMES = ("libobjc.A.dylib", "libobjc-trampolines.dylib"); +my $TESTLIBDIR = "/usr/lib"; + +# Top level directory for intermediate and final build products. +# Intermediate files must be kept separate for XBS BATS builds. +my $OBJROOT = $ENV{OBJROOT} || ""; +my $DSTROOT = $ENV{DSTROOT} || ""; + +# Build product directory inside DSTROOT and OBJROOT. +# Each test config gets its own build directory inside this. +my $BUILDDIR; + +# Local top-level directory. +# This is the default value for $BUILDDIR. +my $LOCALBASE = "/tmp/test-$TESTLIBNAMES[0]-build"; + +# Device-side top-level directory. +# This replaces $DSTROOT$BUILDDIR/ for on-device execution. +my $REMOTEBASE = "/AppleInternal/objctest"; + +# BATS top-level directory. +# This replaces $DSTROOT$BUILDDIR/ for BATS execution. +my $BATSBASE = "/AppleInternal/CoreOS/tests/objc4"; + + +my $crashcatch = <<'END'; +// interpose-able code to catch crashes, print, and exit cleanly +#include +#include +#include + +// from dyld-interposing.h +#define DYLD_INTERPOSE(_replacement,_replacee) __attribute__((used)) static struct{ const void* replacement; const void* replacee; } _interpose_##_replacee __attribute__ ((section ("__DATA,__interpose"))) = { (const void*)(unsigned long)&_replacement, (const void*)(unsigned long)&_replacee }; + +static void catchcrash(int sig) +{ + const char *msg; + switch (sig) { + case SIGILL: msg = "CRASHED: SIGILL"; break; + case SIGBUS: msg = "CRASHED: SIGBUS"; break; + case SIGSYS: msg = "CRASHED: SIGSYS"; break; + case SIGSEGV: msg = "CRASHED: SIGSEGV"; break; + case SIGTRAP: msg = "CRASHED: SIGTRAP"; break; + case SIGABRT: msg = "CRASHED: SIGABRT"; break; + default: msg = "unknown signal"; break; + } + write(STDERR_FILENO, msg, strlen(msg)); + + // avoid backslash-n newline due to escaping differences somewhere + // in BATS versus local execution (perhaps different perl versions?) + char newline = 0xa; + write(STDERR_FILENO, &newline, 1); + + _exit(1); +} + +static void setupcrash(void) __attribute__((constructor)); +static void setupcrash(void) +{ + signal(SIGILL, &catchcrash); + signal(SIGBUS, &catchcrash); + signal(SIGSYS, &catchcrash); + signal(SIGSEGV, &catchcrash); + signal(SIGTRAP, &catchcrash); + signal(SIGABRT, &catchcrash); +} + + +static int hacked = 0; +ssize_t hacked_write(int fildes, const void *buf, size_t nbyte) +{ + if (!hacked) { + setupcrash(); + hacked = 1; + } + return write(fildes, buf, nbyte); +} + +DYLD_INTERPOSE(hacked_write, write); + +END + + +######################################################################### +## Harness + + +# map language to buildable extensions for that language +my %extensions_for_language = ( + "c" => ["c"], + "objective-c" => ["c", "m"], + "c++" => ["c", "cc", "cp", "cpp", "cxx", "c++"], + "objective-c++" => ["c", "m", "cc", "cp", "cpp", "cxx", "c++", "mm"], + "swift" => ["swift"], + + "any" => ["c", "m", "cc", "cp", "cpp", "cxx", "c++", "mm", "swift"], + ); + +# map extension to languages +my %languages_for_extension = ( + "c" => ["c", "objective-c", "c++", "objective-c++"], + "m" => ["objective-c", "objective-c++"], + "mm" => ["objective-c++"], + "cc" => ["c++", "objective-c++"], + "cp" => ["c++", "objective-c++"], + "cpp" => ["c++", "objective-c++"], + "cxx" => ["c++", "objective-c++"], + "c++" => ["c++", "objective-c++"], + "swift" => ["swift"], + ); + +# Run some newline-separated commands like `make` would, stopping if any fail +# run("cmd1 \n cmd2 \n cmd3") +sub make { + my $output = ""; + my @cmds = split("\n", $_[0]); + die if scalar(@cmds) == 0; + $? = 0; + foreach my $cmd (@cmds) { + chomp $cmd; + next if $cmd =~ /^\s*$/; + $cmd .= " 2>&1"; + print "$cmd\n" if $VERBOSE; + $output .= `$cmd`; + last if $?; + } + print "$output\n" if $VERBOSE; + return $output; +} + +sub chdir_verbose { + my $dir = shift || die; + print "cd $dir\n" if $VERBOSE; + chdir $dir || die "couldn't cd $dir"; +} + +sub rm_rf_verbose { + my $dir = shift || die; + print "mkdir -p $dir\n" if $VERBOSE; + `rm -rf '$dir'`; + die "couldn't rm -rf $dir" if $?; +} + +sub mkdir_verbose { + my $dir = shift || die; + print "mkdir -p $dir\n" if $VERBOSE; + `mkdir -p '$dir'`; + die "couldn't mkdir $dir" if $?; +} + + +# xterm colors +my $red = "\e[41;37m"; +my $yellow = "\e[43;30m"; +my $nocolor = "\e[0m"; +if (! -t STDIN) { + # Not isatty. Don't use colors. + $red = ""; + $yellow = ""; + $nocolor = ""; +} + +# print text with a colored prefix on each line +# fixme some callers pass an array of lines and some don't +sub colorprefix { + my $color = shift; + while (defined(my $lines = shift)) { + $lines = "\n" if ($lines eq ""); + for my $line (split(/^/, $lines)) { + chomp $line; + print "$color $nocolor$line\n"; + } + } +} + +# print text colored +# fixme some callers pass an array of lines and some don't +sub colorprint { + my $color = shift; + while (defined(my $lines = shift)) { + $lines = "\n" if ($lines eq ""); + for my $line (split(/^/, $lines)) { + chomp $line; + print "$color$line$nocolor\n"; + } + } +} + +# Return test names from the command line. +# Returns all tests if no tests were named. +sub gettests { + my @tests; + + foreach my $arg (@ARGV) { + push @tests, $arg if ($arg !~ /=/ && $arg !~ /^-/); + } + + opendir(my $dir, $DIR) || die; + while (my $file = readdir($dir)) { + my ($name, $ext) = ($file =~ /^([^.]+)\.([^.]+)$/); + next if ! $languages_for_extension{$ext}; + + open(my $in, "< $file") || die "$file"; + my $contents = join "", <$in>; + if (defined $ALL_TESTS{$name}) { + colorprint $yellow, "SKIP: multiple tests named '$name'; skipping file '$file'."; + } else { + $ALL_TESTS{$name} = $ext if ($contents =~ m#^[/*\s]*TEST_#m); + } + close($in); + } + closedir($dir); + + if (scalar(@tests) == 0) { + @tests = keys %ALL_TESTS; + } + + @tests = sort @tests; + + return @tests; +} + + +# Turn a C compiler name into a C++ compiler name. +sub cplusplus { + my ($c) = @_; + if ($c =~ /cc/) { + $c =~ s/cc/\+\+/; + return $c; + } + return $c . "++"; # e.g. clang => clang++ +} + +# Turn a C compiler name into a Swift compiler name +sub swift { + my ($c) = @_; + $c =~ s#[^/]*$#swift#; + return $c; +} + +# Returns an array of all sdks from `xcodebuild -showsdks` +my @sdks_memo; +sub getsdks { + if (!@sdks_memo) { + @sdks_memo = (`xcodebuild -showsdks` =~ /-sdk (.+)$/mg); + } + return @sdks_memo; +} + +my %sdk_path_memo = {}; +sub getsdkpath { + my ($sdk) = @_; + if (!defined $sdk_path_memo{$sdk}) { + ($sdk_path_memo{$sdk}) = (`xcodebuild -version -sdk '$sdk' Path` =~ /^\s*(.+?)\s*$/); + } + return $sdk_path_memo{$sdk}; +} + +# Extract a version number from a string. +# Ignore trailing "internal". +sub versionsuffix { + my ($str) = @_; + my ($vers) = ($str =~ /([0-9]+\.[0-9]+)(?:\.?internal)?$/); + return $vers; +} +sub majorversionsuffix { + my ($str) = @_; + my ($vers) = ($str =~ /([0-9]+)\.[0-9]+(?:\.?internal)?$/); + return $vers; +} +sub minorversionsuffix { + my ($str) = @_; + my ($vers) = ($str =~ /[0-9]+\.([0-9]+)(?:\.?internal)?$/); + return $vers; +} + +# Compares two SDK names and returns the newer one. +# Assumes the two SDKs are the same OS. +sub newersdk { + my ($lhs, $rhs) = @_; + + # Major version wins. + my $lhsMajor = majorversionsuffix($lhs); + my $rhsMajor = majorversionsuffix($rhs); + if ($lhsMajor > $rhsMajor) { return $lhs; } + if ($lhsMajor < $rhsMajor) { return $rhs; } + + # Minor version wins. + my $lhsMinor = minorversionsuffix($lhs); + my $rhsMinor = minorversionsuffix($rhs); + if ($lhsMinor > $rhsMinor) { return $lhs; } + if ($lhsMinor < $rhsMinor) { return $rhs; } + + # Lexically-last wins (i.e. internal is better than not internal) + if ($lhs gt $rhs) { return $lhs; } + return $rhs; +} + +sub rewind { + seek($_[0], 0, 0); +} + +# parse name=value,value pairs +sub readconditions { + my ($conditionstring) = @_; + + my %results; + my @conditions = ($conditionstring =~ /\w+=(?:[^\s,]+,?)+/g); + for my $condition (@conditions) { + my ($name, $values) = ($condition =~ /(\w+)=(.+)/); + $results{$name} = [split ',', $values]; + } + + return %results; +} + +sub check_output { + my %C = %{shift()}; + my $name = shift; + my @output = @_; + + my %T = %{$C{"TEST_$name"}}; + + # Quietly strip MallocScribble before saving the "original" output + # because it is distracting. + filter_malloc(\@output); + + my @original_output = @output; + + # Run result-checking passes, reducing @output each time + my $xit = 1; + my $bad = ""; + my $warn = ""; + my $runerror = $T{TEST_RUN_OUTPUT}; + filter_hax(\@output); + filter_verbose(\@output); + filter_simulator(\@output); + $warn = filter_warn(\@output); + $bad |= filter_guardmalloc(\@output) if ($C{GUARDMALLOC}); + $bad |= filter_valgrind(\@output) if ($C{VALGRIND}); + $bad = filter_expected(\@output, \%C, $name) if ($bad eq ""); + $bad = filter_bad(\@output) if ($bad eq ""); + + # OK line should be the only one left + $bad = "(output not 'OK: $name')" if ($bad eq "" && (scalar(@output) != 1 || $output[0] !~ /^OK: $name/)); + + if ($bad ne "") { + colorprint $red, "FAIL: /// test '$name' \\\\\\"; + colorprefix $red, @original_output; + colorprint $red, "FAIL: \\\\\\ test '$name' ///"; + colorprint $red, "FAIL: $name: $bad"; + $xit = 0; + } + elsif ($warn ne "") { + colorprint $yellow, "PASS: /// test '$name' \\\\\\"; + colorprefix $yellow, @original_output; + colorprint $yellow, "PASS: \\\\\\ test '$name' ///"; + print "PASS: $name (with warnings)\n"; + } + else { + print "PASS: $name\n"; + } + return $xit; +} + +sub filter_expected +{ + my $outputref = shift; + my %C = %{shift()}; + my $name = shift; + + my %T = %{$C{"TEST_$name"}}; + my $runerror = $T{TEST_RUN_OUTPUT} || return ""; + + my $bad = ""; + + my $output = join("\n", @$outputref) . "\n"; + if ($output !~ /$runerror/) { + $bad = "(run output does not match TEST_RUN_OUTPUT)"; + @$outputref = ("FAIL: $name"); + } else { + @$outputref = ("OK: $name"); # pacify later filter + } + + return $bad; +} + +sub filter_bad +{ + my $outputref = shift; + my $bad = ""; + + my @new_output; + for my $line (@$outputref) { + if ($line =~ /^BAD: (.*)/) { + $bad = "(failed)"; + } else { + push @new_output, $line; + } + } + + @$outputref = @new_output; + return $bad; +} + +sub filter_warn +{ + my $outputref = shift; + my $warn = ""; + + my @new_output; + for my $line (@$outputref) { + if ($line !~ /^WARN: (.*)/) { + push @new_output, $line; + } else { + $warn = "(warned)"; + } + } + + @$outputref = @new_output; + return $warn; +} + +sub filter_verbose +{ + my $outputref = shift; + + my @new_output; + for my $line (@$outputref) { + if ($line !~ /^VERBOSE: (.*)/) { + push @new_output, $line; + } + } + + @$outputref = @new_output; +} + +sub filter_simulator +{ + my $outputref = shift; + + my @new_output; + for my $line (@$outputref) { + if (($line !~ /No simulator devices appear to be running/) && + ($line !~ /CoreSimulator is attempting to unload a stale CoreSimulatorService job/) && + ($line !~ /Failed to locate a valid instance of CoreSimulatorService/)) + { + push @new_output, $line; + } + } + + @$outputref = @new_output; +} + +sub filter_hax +{ + my $outputref = shift; + + my @new_output; + for my $line (@$outputref) { + if ($line !~ /Class OS_tcp_/) { + push @new_output, $line; + } + } + + @$outputref = @new_output; +} + +sub filter_valgrind +{ + my $outputref = shift; + my $errors = 0; + my $leaks = 0; + + my @new_output; + for my $line (@$outputref) { + if ($line =~ /^Approx: do_origins_Dirty\([RW]\): missed \d bytes$/) { + # --track-origins warning (harmless) + next; + } + if ($line =~ /^UNKNOWN __disable_threadsignal is unsupported. This warning will not be repeated.$/) { + # signals unsupported (harmless) + next; + } + if ($line =~ /^UNKNOWN __pthread_sigmask is unsupported. This warning will not be repeated.$/) { + # signals unsupported (harmless) + next; + } + if ($line !~ /^^\.*==\d+==/) { + # not valgrind output + push @new_output, $line; + next; + } + + my ($errcount) = ($line =~ /==\d+== ERROR SUMMARY: (\d+) errors/); + if (defined $errcount && $errcount > 0) { + $errors = 1; + } + + (my $leakcount) = ($line =~ /==\d+==\s+(?:definitely|possibly) lost:\s+([0-9,]+)/); + if (defined $leakcount && $leakcount > 0) { + $leaks = 1; + } + } + + @$outputref = @new_output; + + my $bad = ""; + $bad .= "(valgrind errors)" if ($errors); + $bad .= "(valgrind leaks)" if ($leaks); + return $bad; +} + + + +sub filter_malloc +{ + my $outputref = shift; + my $errors = 0; + + my @new_output; + my $count = 0; + for my $line (@$outputref) { + # Ignore MallocScribble prologue. + # Ignore MallocStackLogging prologue. + if ($line =~ /malloc: enabling scribbling to detect mods to free/ || + $line =~ /Deleted objects will be dirtied by the collector/ || + $line =~ /malloc: stack logs being written into/ || + $line =~ /malloc: stack logs deleted from/ || + $line =~ /malloc: process \d+ no longer exists/ || + $line =~ /malloc: recording malloc and VM allocation stacks/) + { + next; + } + + # not malloc output + push @new_output, $line; + + } + + @$outputref = @new_output; +} + +sub filter_guardmalloc +{ + my $outputref = shift; + my $errors = 0; + + my @new_output; + my $count = 0; + for my $line (@$outputref) { + if ($line !~ /^GuardMalloc\[[^\]]+\]: /) { + # not guardmalloc output + push @new_output, $line; + next; + } + + # Ignore 4 lines of guardmalloc prologue. + # Anything further is a guardmalloc error. + if (++$count > 4) { + $errors = 1; + } + } + + @$outputref = @new_output; + + my $bad = ""; + $bad .= "(guardmalloc errors)" if ($errors); + return $bad; +} + +# TEST_SOMETHING +# text +# text +# END +sub extract_multiline { + my ($flag, $contents, $name) = @_; + if ($contents =~ /$flag\n/) { + my ($output) = ($contents =~ /$flag\n(.*?\n)END[ *\/]*\n/s); + die "$name used $flag without END\n" if !defined($output); + return $output; + } + return undef; +} + + +# TEST_SOMETHING +# text +# OR +# text +# END +sub extract_multiple_multiline { + my ($flag, $contents, $name) = @_; + if ($contents =~ /$flag\n/) { + my ($output) = ($contents =~ /$flag\n(.*?\n)END[ *\/]*\n/s); + die "$name used $flag without END\n" if !defined($output); + + $output =~ s/\nOR\n/\n|/sg; + $output = "^(" . $output . ")\$"; + return $output; + } + return undef; +} + + +sub gather_simple { + my $CREF = shift; + my %C = %{$CREF}; + my $name = shift; + chdir_verbose $DIR; + + my $ext = $ALL_TESTS{$name}; + my $file = "$name.$ext"; + return 0 if !$file; + + # search file for 'TEST_CONFIG' or '#include "test.h"' + # also collect other values: + # TEST_DISABLED disable test with an optional message + # TEST_CRASHES test is expected to crash + # TEST_CONFIG test conditions + # TEST_ENV environment prefix + # TEST_CFLAGS compile flags + # TEST_BUILD build instructions + # TEST_BUILD_OUTPUT expected build stdout/stderr + # TEST_RUN_OUTPUT expected run stdout/stderr + open(my $in, "< $file") || die; + my $contents = join "", <$in>; + + my $test_h = ($contents =~ /^\s*#\s*(include|import)\s*"test\.h"/m); + my ($disabled) = ($contents =~ /\b(TEST_DISABLED\b.*)$/m); + my $crashes = ($contents =~ /\bTEST_CRASHES\b/m); + my ($conditionstring) = ($contents =~ /\bTEST_CONFIG\b(.*)$/m); + my ($envstring) = ($contents =~ /\bTEST_ENV\b(.*)$/m); + my ($cflags) = ($contents =~ /\bTEST_CFLAGS\b(.*)$/m); + my ($buildcmd) = extract_multiline("TEST_BUILD", $contents, $name); + my ($builderror) = extract_multiple_multiline("TEST_BUILD_OUTPUT", $contents, $name); + my ($runerror) = extract_multiple_multiline("TEST_RUN_OUTPUT", $contents, $name); + + return 0 if !$test_h && !$disabled && !$crashes && !defined($conditionstring) && !defined($envstring) && !defined($cflags) && !defined($buildcmd) && !defined($builderror) && !defined($runerror); + + if ($disabled) { + colorprint $yellow, "SKIP: $name (disabled by $disabled)"; + return 0; + } + + # check test conditions + + my $run = 1; + my %conditions = readconditions($conditionstring); + if (! $conditions{LANGUAGE}) { + # implicit language restriction from file extension + $conditions{LANGUAGE} = $languages_for_extension{$ext}; + } + for my $condkey (keys %conditions) { + my @condvalues = @{$conditions{$condkey}}; + + # special case: RUN=0 does not affect build + if ($condkey eq "RUN" && @condvalues == 1 && $condvalues[0] == 0) { + $run = 0; + next; + } + + my $testvalue = $C{$condkey}; + next if !defined($testvalue); + # testvalue is the configuration being run now + # condvalues are the allowed values for this test + + my $ok = 0; + for my $condvalue (@condvalues) { + + # special case: objc and objc++ + if ($condkey eq "LANGUAGE") { + $condvalue = "objective-c" if $condvalue eq "objc"; + $condvalue = "objective-c++" if $condvalue eq "objc++"; + } + + $ok = 1 if ($testvalue eq $condvalue); + + # special case: CC and CXX allow substring matches + if ($condkey eq "CC" || $condkey eq "CXX") { + $ok = 1 if ($testvalue =~ /$condvalue/); + } + + last if $ok; + } + + if (!$ok) { + my $plural = (@condvalues > 1) ? "one of: " : ""; + print "SKIP: $name ($condkey=$testvalue, but test requires $plural", join(' ', @condvalues), ")\n"; + return 0; + } + } + + # save some results for build and run phases + $$CREF{"TEST_$name"} = { + TEST_BUILD => $buildcmd, + TEST_BUILD_OUTPUT => $builderror, + TEST_CRASHES => $crashes, + TEST_RUN_OUTPUT => $runerror, + TEST_CFLAGS => $cflags, + TEST_ENV => $envstring, + TEST_RUN => $run, + DSTDIR => "$C{DSTDIR}/$name.build", + OBJDIR => "$C{OBJDIR}/$name.build", + }; + + return 1; +} + + +# Test description plist to write when building for BATS execution. +my %bats_plist; +$bats_plist{'Project'} = "objc4"; +$bats_plist{'Tests'} = []; # populated by append_bats_test() + +# Saves run instructions for a single test in all configurations as a BATS test. +sub append_bats_test { + my $name = shift; + + my $arch = join(',', @{$args{ARCH}}); + my $os = join(',', @{$args{OSVERSION}}); + my $mem = join(',', @{$args{MEM}}); + my $language = join(',', @{$args{LANGUAGE}}); + + push @{$bats_plist{'Tests'}}, { + "TestName" => "$name", + "Command" => [ + "/usr/bin/perl", + "$BATSBASE/test/test.pl", + $name, + "ARCH=$arch", + "OS=$os", + "MEM=$mem", + "LANGUAGE=$language", + "BUILD=0", + "RUN=1", + "VERBOSE=1", + "BATS=1", + ] + }; +} + + +# Builds a simple test +sub build_simple { + my %C = %{shift()}; + my $name = shift; + my %T = %{$C{"TEST_$name"}}; + + mkdir_verbose $T{DSTDIR}; + chdir_verbose $T{DSTDIR}; + # we don't mkdir $T{OBJDIR} because most tests don't use it + + my $ext = $ALL_TESTS{$name}; + my $file = "$DIR/$name.$ext"; + + if ($T{TEST_CRASHES}) { + `echo '$crashcatch' > crashcatch.c`; + make("$C{COMPILE_C} -dynamiclib -o libcrashcatch.dylib -x c crashcatch.c"); + die "$?" if $?; + } + + my $cmd = $T{TEST_BUILD} ? eval "return \"$T{TEST_BUILD}\"" : "$C{COMPILE} $T{TEST_CFLAGS} $file -o $name.exe"; + + my $output = make($cmd); + + # ignore out-of-date text-based stubs (caused by ditto into SDK) + $output =~ s/ld: warning: text-based stub file.*\n//g; + # rdar://10163155 + $output =~ s/ld: warning: could not create compact unwind for [^\n]+: does not use standard frame\n//g; + # rdar://37937122 + $output =~ s/^warning: Cannot lower [^\n]+\n//g; + $output =~ s/^warning: key: [^\n]+\n//g; + $output =~ s/^warning: discriminator: [^\n]+\n//g; + $output =~ s/^warning: callee: [^\n]+\n//g; + # rdar://38710948 + $output =~ s/ld: warning: ignoring file [^\n]*libclang_rt\.bridgeos\.a[^\n]*\n//g; + # ignore compiler logging of CCC_OVERRIDE_OPTIONS effects + if (defined $ENV{CCC_OVERRIDE_OPTIONS}) { + $output =~ s/### (CCC_OVERRIDE_OPTIONS:|Adding argument|Deleting argument|Replacing) [^\n]*\n//g; + } + + my $ok; + if (my $builderror = $T{TEST_BUILD_OUTPUT}) { + # check for expected output and ignore $? + if ($output =~ /$builderror/) { + $ok = 1; + } elsif (defined $ENV{CCC_OVERRIDE_OPTIONS} && $builderror =~ /warning:/) { + # CCC_OVERRIDE_OPTIONS manipulates compiler diagnostics. + # Don't enforce any TEST_BUILD_OUTPUT that looks for warnings. + colorprint $yellow, "WARN: /// test '$name' \\\\\\"; + colorprefix $yellow, $output; + colorprint $yellow, "WARN: \\\\\\ test '$name' ///"; + colorprint $yellow, "WARN: $name (build output does not match TEST_BUILD_OUTPUT; not fatal because CCC_OVERRIDE_OPTIONS is set)"; + $ok = 1; + } else { + colorprint $red, "FAIL: /// test '$name' \\\\\\"; + colorprefix $red, $output; + colorprint $red, "FAIL: \\\\\\ test '$name' ///"; + colorprint $red, "FAIL: $name (build output does not match TEST_BUILD_OUTPUT)"; + $ok = 0; + } + } elsif ($?) { + colorprint $red, "FAIL: /// test '$name' \\\\\\"; + colorprefix $red, $output; + colorprint $red, "FAIL: \\\\\\ test '$name' ///"; + colorprint $red, "FAIL: $name (build failed)"; + $ok = 0; + } elsif ($output ne "") { + colorprint $red, "FAIL: /// test '$name' \\\\\\"; + colorprefix $red, $output; + colorprint $red, "FAIL: \\\\\\ test '$name' ///"; + colorprint $red, "FAIL: $name (unexpected build output)"; + $ok = 0; + } else { + $ok = 1; + } + + if ($ok) { + foreach my $file (glob("*.exe *.dylib *.bundle")) { + if (!$BATS) { + # not for BATS to save space and build time + # fixme use SYMROOT? + make("xcrun dsymutil $file"); + } + if ($C{OS} eq "macosx" || $C{OS} =~ /simulator/) { + # setting any entitlements disables dyld environment variables + } else { + # get-task-allow entitlement is required + # to enable dyld environment variables + make("xcrun codesign -s - --entitlements $DIR/get_task_allow_entitlement.plist $file"); + die "$?" if $?; + } + } + } + + return $ok; +} + +# Run a simple test (testname.exe, with error checking of stdout and stderr) +sub run_simple { + my %C = %{shift()}; + my $name = shift; + my %T = %{$C{"TEST_$name"}}; + + if (! $T{TEST_RUN}) { + print "PASS: $name (build only)\n"; + return 1; + } + + my $testdir = $T{DSTDIR}; + chdir_verbose $testdir; + + my $env = "$C{ENV} $T{TEST_ENV}"; + + if ($T{TEST_CRASHES}) { + $env .= " OBJC_DEBUG_DONT_CRASH=YES"; + } + + my $output; + + if ($C{ARCH} =~ /^arm/ && `uname -p` !~ /^arm/) { + # run on iOS or watchos or tvos device + # fixme device selection and verification + my $remotedir = "$REMOTEBASE/" . basename($C{DSTDIR}) . "/$name.build"; + + # Add test dir and libobjc's dir to DYLD_LIBRARY_PATH. + # Insert libcrashcatch.dylib if necessary. + $env .= " DYLD_LIBRARY_PATH=$remotedir"; + $env .= ":$REMOTEBASE" if ($C{TESTLIBDIR} ne $TESTLIBDIR); + if ($T{TEST_CRASHES}) { + $env .= " DYLD_INSERT_LIBRARIES=$remotedir/libcrashcatch.dylib"; + } + + my $cmd = "ssh -p $PORT $HOST 'cd $remotedir && env $env ./$name.exe'"; + $output = make("$cmd"); + } + elsif ($C{OS} =~ /simulator/) { + # run locally in a simulator + # fixme selection of simulated OS version + my $simdevice; + if ($C{OS} =~ /iphonesimulator/) { + $simdevice = 'iPhone X'; + } elsif ($C{OS} =~ /watchsimulator/) { + $simdevice = 'Apple Watch Series 4 - 40mm'; + } elsif ($C{OS} =~ /tvsimulator/) { + $simdevice = 'Apple TV 1080p'; + } else { + die "unknown simulator $C{OS}\n"; + } + my $sim = "xcrun -sdk iphonesimulator simctl spawn '$simdevice'"; + # Add test dir and libobjc's dir to DYLD_LIBRARY_PATH. + # Insert libcrashcatch.dylib if necessary. + $env .= " DYLD_LIBRARY_PATH=$testdir"; + $env .= ":" . $C{TESTLIBDIR} if ($C{TESTLIBDIR} ne $TESTLIBDIR); + if ($T{TEST_CRASHES}) { + $env .= " DYLD_INSERT_LIBRARIES=$testdir/libcrashcatch.dylib"; + } + + my $simenv = ""; + foreach my $keyvalue (split(' ', $env)) { + $simenv .= "SIMCTL_CHILD_$keyvalue "; + } + # Use the full path here so hack_cwd in test.h works. + $output = make("env $simenv $sim $testdir/$name.exe"); + } + else { + # run locally + + # Add test dir and libobjc's dir to DYLD_LIBRARY_PATH. + # Insert libcrashcatch.dylib if necessary. + $env .= " DYLD_LIBRARY_PATH=$testdir"; + $env .= ":" . $C{TESTLIBDIR} if ($C{TESTLIBDIR} ne $TESTLIBDIR); + if ($T{TEST_CRASHES}) { + $env .= " DYLD_INSERT_LIBRARIES=$testdir/libcrashcatch.dylib"; + } + + $output = make("sh -c '$env ./$name.exe'"); + } + + return check_output(\%C, $name, split("\n", $output)); +} + + +my %compiler_memo; +sub find_compiler { + my ($cc, $toolchain, $sdk_path) = @_; + + # memoize + my $key = $cc . ':' . $toolchain; + my $result = $compiler_memo{$key}; + return $result if defined $result; + + $result = make("xcrun -toolchain $toolchain -find $cc 2>/dev/null"); + + chomp $result; + $compiler_memo{$key} = $result; + return $result; +} + +sub dirContainsAllTestLibs { + my $dir = shift; + + foreach my $testlib (@TESTLIBNAMES) { + my $found = (-e "$dir/$testlib"); + my $foundstr = ($found ? "found" : "didn't find"); + print "note: $foundstr $testlib in $dir\n" if ($VERBOSE); + return 0 if (!$found); + } + + return 1; +} + +sub make_one_config { + my $configref = shift; + my $root = shift; + my %C = %{$configref}; + + # Aliases + $C{LANGUAGE} = "objective-c" if $C{LANGUAGE} eq "objc"; + $C{LANGUAGE} = "objective-c++" if $C{LANGUAGE} eq "objc++"; + + # Interpret OS version string from command line. + my ($sdk_arg, $deployment_arg, $run_arg, undef) = split('-', $C{OSVERSION}); + delete $C{OSVERSION}; + my ($os_arg) = ($sdk_arg =~ /^([^\.0-9]+)/); + $deployment_arg = "default" if !defined($deployment_arg); + $run_arg = "default" if !defined($run_arg); + + my %allowed_os_args = ( + "macosx" => "macosx", "osx" => "macosx", "macos" => "macosx", + "iphoneos" => "iphoneos", "ios" => "iphoneos", + "iphonesimulator" => "iphonesimulator", "iossimulator" => "iphonesimulator", + "watchos" => "watchos", + "watchsimulator" => "watchsimulator", "watchossimulator" => "watchsimulator", + "appletvos" => "appletvos", "tvos" => "appletvos", + "appletvsimulator" => "appletvsimulator", "tvsimulator" => "appletvsimulator", + "bridgeos" => "bridgeos", + ); + + $C{OS} = $allowed_os_args{$os_arg} || die "unknown OS '$os_arg' (expected " . join(', ', sort keys %allowed_os_args) . ")\n"; + + # set the config name now, after massaging the language and OS versions, + # but before adding other settings + my $configname = config_name(%C); + die if ($configname =~ /'/); + die if ($configname =~ / /); + ($C{NAME} = $configname) =~ s/~/ /g; + (my $configdir = $configname) =~ s#/##g; + $C{DSTDIR} = "$DSTROOT$BUILDDIR/$configdir"; + $C{OBJDIR} = "$OBJROOT$BUILDDIR/$configdir"; + + # Allow tests to see BATS-edness in TEST_CONFIG. + $C{BATS} = $BATS; + + if ($C{OS} eq "iphoneos" || $C{OS} eq "iphonesimulator") { + $C{TOOLCHAIN} = "ios"; + } elsif ($C{OS} eq "watchos" || $C{OS} eq "watchsimulator") { + $C{TOOLCHAIN} = "watchos"; + } elsif ($C{OS} eq "appletvos" || $C{OS} eq "appletvsimulator") { + $C{TOOLCHAIN} = "appletvos"; + } elsif ($C{OS} eq "bridgeos") { + $C{TOOLCHAIN} = "bridgeos"; + } elsif ($C{OS} eq "macosx") { + $C{TOOLCHAIN} = "osx"; + } else { + colorprint $yellow, "WARN: don't know toolchain for OS $C{OS}"; + $C{TOOLCHAIN} = "default"; + } + + if ($BUILD) { + # Look up SDK. + # Try exact match first. + # Then try lexically-last prefix match + # (so "macosx" => "macosx10.7internal") + + $sdk_arg =~ s/$os_arg/$C{OS}/; + + my @sdks = getsdks(); + if ($VERBOSE) { + print "note: Installed SDKs: @sdks\n"; + } + my $exactsdk = undef; + my $prefixsdk = undef; + foreach my $sdk (@sdks) { + $exactsdk = $sdk if ($sdk eq $sdk_arg); + $prefixsdk = newersdk($sdk, $prefixsdk) if ($sdk =~ /^$sdk_arg/); + } + + my $sdk; + if ($exactsdk) { + $sdk = $exactsdk; + } elsif ($prefixsdk) { + $sdk = $prefixsdk; + } else { + die "unknown SDK '$sdk_arg'\nInstalled SDKs: @sdks\n"; + } + + # Set deployment target. + # fixme can't enforce version when run_arg eq "default" + # because we don't know it yet + $deployment_arg = versionsuffix($sdk) if $deployment_arg eq "default"; + if ($run_arg ne "default") { + die "Deployment target '$deployment_arg' is newer than run target '$run_arg'\n" if $deployment_arg > $run_arg; + } + $C{DEPLOYMENT_TARGET} = $deployment_arg; + $C{SDK_PATH} = getsdkpath($sdk); + } else { + # not $BUILD + $C{DEPLOYMENT_TARGET} = "unknown_deployment_target"; + $C{SDK_PATH} = "/unknown/sdk"; + } + + # Set run target. + $C{RUN_TARGET} = $run_arg; + + # Look up test library (possible in root or SDK_PATH) + + my $rootarg = $root; + my $symroot; + my @sympaths = ( (glob "$root/*~sym")[0], + (glob "$root/BuildRecords/*_install/Symbols")[0], + "$root/Symbols" ); + my @dstpaths = ( (glob "$root/*~dst")[0], + (glob "$root/BuildRecords/*_install/Root")[0], + "$root/Root" ); + for(my $i = 0; $i < scalar(@sympaths); $i++) { + if (-e $sympaths[$i] && -e $dstpaths[$i]) { + $symroot = $sympaths[$i]; + $root = $dstpaths[$i]; + last; + } + } + + if ($root ne "") { + # Root specified. Require that it contain our dylibs. + if (dirContainsAllTestLibs("$root$C{SDK_PATH}$TESTLIBDIR")) { + $C{TESTLIBDIR} = "$root$C{SDK_PATH}$TESTLIBDIR"; + } elsif (dirContainsAllTestLibs("$root$TESTLIBDIR")) { + $C{TESTLIBDIR} = "$root$TESTLIBDIR"; + } elsif (dirContainsAllTestLibs($root)) { + $C{TESTLIBDIR} = "$root"; + } else { + die "Didn't find some libs in root '$rootarg' for sdk '$C{SDK_PATH}'\n"; + } + } + else { + # No root specified. Use the SDK or / for our dylibs. + if (dirContainsAllTestLibs("$C{SDK_PATH}$TESTLIBDIR")) { + $C{TESTLIBDIR} = "$C{SDK_PATH}$TESTLIBDIR"; + } else { + # We don't actually check in / because on devices + # there are no dylib files there. + $C{TESTLIBDIR} = $TESTLIBDIR; + } + } + + @{$C{TESTLIBS}} = map { "$C{TESTLIBDIR}/$_" } @TESTLIBNAMES; + # convenience for tests that want libobjc.dylib's path + $C{TESTLIB} = @{$C{TESTLIBS}}[0]; + + foreach my $testlibname (@TESTLIBNAMES) { + if (-e "$symroot/$testlibname.dSYM") { + push(@{$C{TESTDSYMS}}, "$symroot/$testlibname.dSYM"); + } + } + + if ($VERBOSE) { + foreach my $testlib (@{$C{TESTLIBS}}) { + my @uuids = `/usr/bin/dwarfdump -u '$testlib'`; + while (my $uuid = shift @uuids) { + print "note: $uuid"; + } + } + } + + # Look up compilers + my $cc = $C{CC}; + my $cxx = cplusplus($C{CC}); + my $swift = swift($C{CC}); + if (! $BUILD) { + $C{CC} = $cc; + $C{CXX} = $cxx; + $C{SWIFT} = $swift + } else { + $C{CC} = find_compiler($cc, $C{TOOLCHAIN}, $C{SDK_PATH}); + $C{CXX} = find_compiler($cxx, $C{TOOLCHAIN}, $C{SDK_PATH}); + $C{SWIFT} = find_compiler($swift, $C{TOOLCHAIN}, $C{SDK_PATH}); + + die "No C compiler '$cc' ('$C{CC}') in toolchain '$C{TOOLCHAIN}'\n" if !-e $C{CC}; + die "No C++ compiler '$cxx' ('$C{CXX}') in toolchain '$C{TOOLCHAIN}'\n" if !-e $C{CXX}; + die "No Swift compiler '$swift' ('$C{SWIFT}') in toolchain '$C{TOOLCHAIN}'\n" if !-e $C{SWIFT}; + } + + if ($C{ARCH} eq "i386" && $C{OS} eq "macosx") { + # libarclite no longer available on i386 + # fixme need an archived copy for bincompat testing + $C{FORCE_LOAD_ARCLITE} = ""; + } elsif ($C{OS} eq "bridgeos") { + # no libarclite on bridgeOS + $C{FORCE_LOAD_ARCLITE} = ""; + } else { + $C{FORCE_LOAD_ARCLITE} = "-Xlinker -force_load -Xlinker " . dirname($C{CC}) . "/../lib/arc/libarclite_$C{OS}.a"; + } + + # Populate cflags + + my $cflags = "-I$DIR -W -Wall -Wno-objc-weak-compat -Wno-arc-bridge-casts-disallowed-in-nonarc -Wshorten-64-to-32 -Qunused-arguments -fno-caret-diagnostics -Os -arch $C{ARCH} "; + if (!$BATS) { + # save-temps so dsymutil works so debug info works. + # Disabled in BATS to save disk space. + # rdar://45656803 -save-temps causes bad -Wstdlibcxx-not-found warnings + $cflags .= "-g -save-temps -Wno-stdlibcxx-not-found"; + } + my $objcflags = ""; + my $swiftflags = "-g "; + + $cflags .= " -isysroot '$C{SDK_PATH}'"; + $cflags .= " '-Wl,-syslibroot,$C{SDK_PATH}'"; + $swiftflags .= " -sdk '$C{SDK_PATH}'"; + + # Set deployment target cflags + my $target = undef; + die "No deployment target" if $C{DEPLOYMENT_TARGET} eq ""; + if ($C{OS} eq "iphoneos") { + $cflags .= " -mios-version-min=$C{DEPLOYMENT_TARGET}"; + $target = "$C{ARCH}-apple-ios$C{DEPLOYMENT_TARGET}"; + } + elsif ($C{OS} eq "iphonesimulator") { + $cflags .= " -mios-simulator-version-min=$C{DEPLOYMENT_TARGET}"; + $target = "$C{ARCH}-apple-ios$C{DEPLOYMENT_TARGET}"; + } + elsif ($C{OS} eq "watchos") { + $cflags .= " -mwatchos-version-min=$C{DEPLOYMENT_TARGET}"; + $target = "$C{ARCH}-apple-watchos$C{DEPLOYMENT_TARGET}"; + } + elsif ($C{OS} eq "watchsimulator") { + $cflags .= " -mwatchos-simulator-version-min=$C{DEPLOYMENT_TARGET}"; + $target = "$C{ARCH}-apple-watchos$C{DEPLOYMENT_TARGET}"; + } + elsif ($C{OS} eq "appletvos") { + $cflags .= " -mtvos-version-min=$C{DEPLOYMENT_TARGET}"; + $target = "$C{ARCH}-apple-tvos$C{DEPLOYMENT_TARGET}"; + } + elsif ($C{OS} eq "appletvsimulator") { + $cflags .= " -mtvos-simulator-version-min=$C{DEPLOYMENT_TARGET}"; + $target = "$C{ARCH}-apple-tvos$C{DEPLOYMENT_TARGET}"; + } + elsif ($C{OS} eq "bridgeos") { + $cflags .= " -mbridgeos-version-min=$C{DEPLOYMENT_TARGET}"; + $target = "$C{ARCH}-apple-bridgeos$C{DEPLOYMENT_TARGET}"; + } + else { + $cflags .= " -mmacosx-version-min=$C{DEPLOYMENT_TARGET}"; + $target = "$C{ARCH}-apple-macosx$C{DEPLOYMENT_TARGET}"; + } + $swiftflags .= " -target $target"; + + $C{TESTINCLUDEDIR} = "$C{SDK_PATH}/usr/include"; + $C{TESTLOCALINCLUDEDIR} = "$C{SDK_PATH}/usr/local/include"; + if ($root ne "") { + if ($C{SDK_PATH} ne "/") { + $cflags .= " -isystem '$root$C{SDK_PATH}/usr/include'"; + $cflags .= " -isystem '$root$C{SDK_PATH}/usr/local/include'"; + } + + my $library_path = $C{TESTLIBDIR}; + $cflags .= " -L$library_path"; + # fixme Root vs SDKContentRoot + $C{TESTINCLUDEDIR} = "$root/../SDKContentRoot/usr/include"; + $C{TESTLOCALINCLUDEDIR} = "$root/../SDKContentRoot/usr/local/include"; + $cflags .= " -isystem '$C{TESTINCLUDEDIR}'"; + $cflags .= " -isystem '$C{TESTLOCALINCLUDEDIR}'"; + } + + + # Populate objcflags + + $objcflags .= " -lobjc"; + if ($C{MEM} eq "arc") { + $objcflags .= " -fobjc-arc"; + } + elsif ($C{MEM} eq "mrc") { + # nothing + } + else { + die "unrecognized MEM '$C{MEM}'\n"; + } + + # Populate ENV_PREFIX + $C{ENV} = "LANG=C MallocScribble=1"; + $C{ENV} .= " VERBOSE=$VERBOSE" if $VERBOSE; + if ($root ne "") { + die "no spaces allowed in root" if $C{TESTLIBDIR} =~ /\s+/; + } + if ($C{GUARDMALLOC}) { + $C{ENV} .= " GUARDMALLOC=1"; # checked by tests and errcheck.pl + $C{ENV} .= " DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib"; + if ($C{GUARDMALLOC} eq "before") { + $C{ENV} .= " MALLOC_PROTECT_BEFORE=1"; + } elsif ($C{GUARDMALLOC} eq "after") { + # protect after is the default + } else { + die "Unknown guard malloc mode '$C{GUARDMALLOC}'\n"; + } + } + + # Populate compiler commands + $C{XCRUN} = "env LANG=C /usr/bin/xcrun -toolchain '$C{TOOLCHAIN}'"; + + $C{COMPILE_C} = "$C{XCRUN} '$C{CC}' $cflags -x c -std=gnu99"; + $C{COMPILE_CXX} = "$C{XCRUN} '$C{CXX}' $cflags -x c++"; + $C{COMPILE_M} = "$C{XCRUN} '$C{CC}' $cflags $objcflags -x objective-c -std=gnu99"; + $C{COMPILE_MM} = "$C{XCRUN} '$C{CXX}' $cflags $objcflags -x objective-c++"; + $C{COMPILE_SWIFT} = "$C{XCRUN} '$C{SWIFT}' $swiftflags"; + + $C{COMPILE} = $C{COMPILE_C} if $C{LANGUAGE} eq "c"; + $C{COMPILE} = $C{COMPILE_CXX} if $C{LANGUAGE} eq "c++"; + $C{COMPILE} = $C{COMPILE_M} if $C{LANGUAGE} eq "objective-c"; + $C{COMPILE} = $C{COMPILE_MM} if $C{LANGUAGE} eq "objective-c++"; + $C{COMPILE} = $C{COMPILE_SWIFT} if $C{LANGUAGE} eq "swift"; + die "unknown language '$C{LANGUAGE}'\n" if !defined $C{COMPILE}; + + ($C{COMPILE_NOMEM} = $C{COMPILE}) =~ s/ -fobjc-arc\S*//g; + ($C{COMPILE_NOLINK} = $C{COMPILE}) =~ s/ '?-(?:Wl,|l)\S*//g; + ($C{COMPILE_NOLINK_NOMEM} = $C{COMPILE_NOMEM}) =~ s/ '?-(?:Wl,|l)\S*//g; + + + # Reject some self-inconsistent and disallowed configurations + if ($C{MEM} !~ /^(mrc|arc)$/) { + die "unknown MEM=$C{MEM} (expected one of mrc arc)\n"; + } + + if ($C{MEM} eq "arc" && $C{CC} !~ /clang/) { + print "note: skipping configuration $C{NAME}\n"; + print "note: because CC=$C{CC} does not support MEM=$C{MEM}\n"; + return 0; + } + + if ($C{ARCH} eq "i386" && $C{OS} eq "macosx") { + colorprint $yellow, "WARN: skipping configuration $C{NAME}\n"; + colorprint $yellow, "WARN: because 32-bit Mac is dead\n"; + return 0; + } + + # fixme + if ($C{LANGUAGE} eq "swift" && $C{ARCH} =~ /^arm/) { + print "note: skipping configuration $C{NAME}\n"; + print "note: because ARCH=$C{ARCH} does not support LANGUAGE=SWIFT\n"; + return 0; + } + + # fixme unimplemented run targets + if ($C{RUN_TARGET} ne "default" && $C{OS} !~ /simulator/) { + colorprint $yellow, "WARN: skipping configuration $C{NAME}"; + colorprint $yellow, "WARN: because OS=$C{OS} does not yet implement RUN_TARGET=$C{RUN_TARGET}"; + } + + %$configref = %C; +} + +sub make_configs { + my ($root, %args) = @_; + + my @results = ({}); # start with one empty config + + for my $key (keys %args) { + my @newresults; + my @values = @{$args{$key}}; + for my $configref (@results) { + my %config = %{$configref}; + for my $value (@values) { + my %newconfig = %config; + $newconfig{$key} = $value; + push @newresults, \%newconfig; + } + } + @results = @newresults; + } + + my @newresults; + for my $configref(@results) { + if (make_one_config($configref, $root)) { + push @newresults, $configref; + } + } + + return @newresults; +} + +sub config_name { + my %config = @_; + my $name = ""; + for my $key (sort keys %config) { + $name .= '~' if $name ne ""; + $name .= "$key=$config{$key}"; + } + return $name; +} + +sub rsync_ios { + my ($src, $timeout) = @_; + for (my $i = 0; $i < 10; $i++) { + make("$DIR/timeout.pl $timeout rsync -e 'ssh -p $PORT' -av $src $HOST:/$REMOTEBASE/"); + return if $? == 0; + colorprint $yellow, "WARN: RETRY\n" if $VERBOSE; + } + die "Couldn't rsync tests to device. Check: device is connected; tcprelay is running; device trusts your Mac; device is unlocked; filesystem is mounted r/w\n"; +} + +sub build_and_run_one_config { + my %C = %{shift()}; + my @tests = @_; + + # Build and run + my $testcount = 0; + my $failcount = 0; + my $skipconfig = 0; + + my @gathertests; + foreach my $test (@tests) { + if ($VERBOSE) { + print "\nGATHER $test\n"; + } + + if ($ALL_TESTS{$test}) { + gather_simple(\%C, $test) || next; # not pass, not fail + push @gathertests, $test; + } else { + die "No test named '$test'\n"; + } + } + + my @builttests; + if (!$BUILD) { + @builttests = @gathertests; + $testcount = scalar(@gathertests); + } else { + foreach my $test (@gathertests) { + if ($VERBOSE) { + print "\nBUILD $test\n"; + } + + if ($ALL_TESTS{$test}) { + $testcount++; + if (!build_simple(\%C, $test)) { + $failcount++; + } else { + push @builttests, $test; + } + } else { + die "No test named '$test'\n"; + } + } + } + + if (!$RUN || !scalar(@builttests)) { + # nothing to do + } + else { + if ($C{ARCH} =~ /^arm/ && `uname -p` !~ /^arm/) { + # upload timeout - longer for slow watch devices + my $timeout = ($C{OS} =~ /watch/) ? 120 : 20; + + # upload all tests to iOS device + rsync_ios($C{DSTDIR}, $timeout); + + # upload library to iOS device + if ($C{TESTLIBDIR} ne $TESTLIBDIR) { + foreach my $thing (@{$C{TESTLIBS}}, @{$C{TESTDSYMS}}) { + rsync_ios($thing, $timeout); + } + } + } + elsif ($C{OS} =~ /simulator/) { + # run locally in a simulator + } + else { + # run locally + if ($BATS) { + # BATS execution tries to run architectures that + # aren't supported by the device. Skip those configs here. + my $machine = `machine`; + chomp $machine; + # unsupported: + # running arm64e on non-arm64e device + # running arm64 on non-arm64* device + # running armv7k on non-armv7k device + # running arm64_32 on armv7k device + # We don't need to handle all mismatches here, + # only mismatches that arise within a single OS. + $skipconfig = + (($C{ARCH} eq "arm64e" && $machine ne "arm64e") || + ($C{ARCH} eq "arm64" && $machine !~ /^arm64/) || + ($C{ARCH} eq "armv7k" && $machine ne "armv7k") || + ($C{ARCH} eq "arm64_32" && $machine eq "armv7k")); + if ($skipconfig) { + print "note: skipping configuration $C{NAME}\n"; + print "note: because test arch $C{ARCH} is not " . + "supported on device arch $machine\n"; + $testcount = 0; + } + } + } + + if (!$skipconfig) { + foreach my $test (@builttests) { + print "\nRUN $test\n" if ($VERBOSE); + + if ($ALL_TESTS{$test}) { + if (!run_simple(\%C, $test)) { + $failcount++; + } + } else { + die "No test named '$test'\n"; + } + } + } + } + + return ($testcount, $failcount, $skipconfig); +} + + + +# Return value if set by "$argname=value" on the command line +# Return $default if not set. +sub getargs { + my ($argname, $default) = @_; + + foreach my $arg (@ARGV) { + my ($value) = ($arg =~ /^$argname=(.+)$/); + return [split ',', $value] if defined $value; + } + + return [split ',', $default]; +} + +# Return 1 or 0 if set by "$argname=1" or "$argname=0" on the +# command line. Return $default if not set. +sub getbools { + my ($argname, $default) = @_; + + my @values = @{getargs($argname, $default)}; + return [( map { ($_ eq "0") ? 0 : 1 } @values )]; +} + +# Return an integer if set by "$argname=value" on the +# command line. Return $default if not set. +sub getints { + my ($argname, $default) = @_; + + my @values = @{getargs($argname, $default)}; + return [( map { int($_) } @values )]; +} + +sub getarg { + my ($argname, $default) = @_; + my @values = @{getargs($argname, $default)}; + die "Only one value allowed for $argname\n" if @values > 1; + return $values[0]; +} + +sub getbool { + my ($argname, $default) = @_; + my @values = @{getbools($argname, $default)}; + die "Only one value allowed for $argname\n" if @values > 1; + return $values[0]; +} + +sub getint { + my ($argname, $default) = @_; + my @values = @{getints($argname, $default)}; + die "Only one value allowed for $argname\n" if @values > 1; + return $values[0]; +} + + +my $default_arch = "x86_64"; +$args{ARCH} = getargs("ARCH", 0); +$args{ARCH} = getargs("ARCHS", $default_arch) if !@{$args{ARCH}}[0]; + +$args{OSVERSION} = getargs("OS", "macosx-default-default"); + +$args{MEM} = getargs("MEM", "mrc,arc"); +$args{LANGUAGE} = [ map { lc($_) } @{getargs("LANGUAGE", "c,objective-c,c++,objective-c++")} ]; + +$args{CC} = getargs("CC", "clang"); + +$HOST = getarg("HOST", "iphone"); +$PORT = getarg("PORT", "10022"); + +{ + my $guardmalloc = getargs("GUARDMALLOC", 0); + # GUARDMALLOC=1 is the same as GUARDMALLOC=before,after + my @guardmalloc2 = (); + for my $arg (@$guardmalloc) { + if ($arg == 1) { push @guardmalloc2, "before"; + push @guardmalloc2, "after"; } + else { push @guardmalloc2, $arg } + } + $args{GUARDMALLOC} = \@guardmalloc2; +} + +$BUILD = getbool("BUILD", 1); +$RUN = getbool("RUN", 1); +$VERBOSE = getint("VERBOSE", 0); +$BATS = getbool("BATS", 0); +$BUILDDIR = getarg("BUILDDIR", $BATS ? $BATSBASE : $LOCALBASE); + +my $root = getarg("ROOT", ""); +$root =~ s#/*$##; + +my @tests = gettests(); + +if ($BUILD) { + rm_rf_verbose "$DSTROOT$BUILDDIR"; + rm_rf_verbose "$OBJROOT$BUILDDIR"; +} + +print "note: -----\n"; +print "note: testing root '$root'\n"; + +my @configs = make_configs($root, %args); + +print "note: -----\n"; +print "note: testing ", scalar(@configs), " configurations:\n"; +for my $configref (@configs) { + my $configname = $$configref{NAME}; + print "note: configuration $configname\n"; +} + +my $failed = 0; + +my $testconfigs = @configs; +my $failconfigs = 0; +my $skipconfigs = 0; +my $testcount = 0; +my $failcount = 0; +for my $configref (@configs) { + my $configname = $$configref{NAME}; + print "note: -----\n"; + print "note: \nnote: $configname\nnote: \n"; + + (my $t, my $f, my $skipconfig) = + eval { build_and_run_one_config($configref, @tests); }; + $skipconfigs += $skipconfig; + if ($@) { + chomp $@; + colorprint $red, "FAIL: $configname"; + colorprint $red, "FAIL: $@"; + $failconfigs++; + } else { + my $color = ($f ? $red : ""); + print "note:\n"; + colorprint $color, "note: $configname\n"; + colorprint $color, "note: $t tests, $f failures"; + $testcount += $t; + $failcount += $f; + $failconfigs++ if ($f); + } +} + +print "note: -----\n"; +my $color = ($failconfigs ? $red : ""); +colorprint $color, "note: $testconfigs configurations, " . + "$failconfigs with failures, $skipconfigs skipped"; +colorprint $color, "note: $testcount tests, $failcount failures"; + +$failed = ($failconfigs ? 1 : 0); + + +if ($BUILD && $BATS && !$failed) { + # Collect BATS execution instructions for all tests. + # Each BATS "test" is all configurations together of one of our tests. + for my $testname (@tests) { + append_bats_test($testname); + } + + # Write the BATS plist to disk. + my $json = encode_json(\%bats_plist); + my $filename = "$DSTROOT$BATSBASE/objc4.plist"; + print "note: writing BATS config to $filename\n"; + open(my $file, '>', $filename); + print $file $json; + close $file; +} + +exit ($failed ? 1 : 0); diff --git a/test/testroot.i b/test/testroot.i new file mode 100644 index 0000000..99fbec8 --- /dev/null +++ b/test/testroot.i @@ -0,0 +1,220 @@ +// testroot.i +// Implementation of class TestRoot +// Include this file into your main test file to use it. + +#include "test.h" +#include +#include + +atomic_int TestRootLoad; +atomic_int TestRootInitialize; +atomic_int TestRootAlloc; +atomic_int TestRootAllocWithZone; +atomic_int TestRootCopy; +atomic_int TestRootCopyWithZone; +atomic_int TestRootMutableCopy; +atomic_int TestRootMutableCopyWithZone; +atomic_int TestRootInit; +atomic_int TestRootDealloc; +atomic_int TestRootRetain; +atomic_int TestRootRelease; +atomic_int TestRootAutorelease; +atomic_int TestRootRetainCount; +atomic_int TestRootTryRetain; +atomic_int TestRootIsDeallocating; +atomic_int TestRootPlusRetain; +atomic_int TestRootPlusRelease; +atomic_int TestRootPlusAutorelease; +atomic_int TestRootPlusRetainCount; + + +@implementation TestRoot + +// These all use void* pending rdar://9310005. + +static void * +retain_fn(void *self, SEL _cmd __unused) { + atomic_fetch_add_explicit(&TestRootRetain, 1, memory_order_relaxed); + void * (*fn)(void *) = (typeof(fn))_objc_rootRetain; + return fn(self); +} + +static void +release_fn(void *self, SEL _cmd __unused) { + atomic_fetch_add_explicit(&TestRootRelease, 1, memory_order_relaxed); + void (*fn)(void *) = (typeof(fn))_objc_rootRelease; + fn(self); +} + +static void * +autorelease_fn(void *self, SEL _cmd __unused) { + atomic_fetch_add_explicit(&TestRootAutorelease, 1, memory_order_relaxed); + void * (*fn)(void *) = (typeof(fn))_objc_rootAutorelease; + return fn(self); +} + +static unsigned long +retaincount_fn(void *self, SEL _cmd __unused) { + atomic_fetch_add_explicit(&TestRootRetainCount, 1, memory_order_relaxed); + unsigned long (*fn)(void *) = (typeof(fn))_objc_rootRetainCount; + return fn(self); +} + +static void * +copywithzone_fn(void *self, SEL _cmd __unused, void *zone) { + atomic_fetch_add_explicit(&TestRootCopyWithZone, 1, memory_order_relaxed); + void * (*fn)(void *, void *) = + (typeof(fn))dlsym(RTLD_DEFAULT, "object_copy"); + return fn(self, zone); +} + +static void * +plusretain_fn(void *self __unused, SEL _cmd __unused) { + atomic_fetch_add_explicit(&TestRootPlusRetain, 1, memory_order_relaxed); + return self; +} + +static void +plusrelease_fn(void *self __unused, SEL _cmd __unused) { + atomic_fetch_add_explicit(&TestRootPlusRelease, 1, memory_order_relaxed); +} + +static void * +plusautorelease_fn(void *self, SEL _cmd __unused) { + atomic_fetch_add_explicit(&TestRootPlusAutorelease, 1, memory_order_relaxed); + return self; +} + +static unsigned long +plusretaincount_fn(void *self __unused, SEL _cmd __unused) { + atomic_fetch_add_explicit(&TestRootPlusRetainCount, 1, memory_order_relaxed); + return ULONG_MAX; +} + ++(void) load { + atomic_fetch_add_explicit(&TestRootLoad, 1, memory_order_relaxed); + + // install methods that ARC refuses to compile + class_addMethod(self, sel_registerName("retain"), (IMP)retain_fn, ""); + class_addMethod(self, sel_registerName("release"), (IMP)release_fn, ""); + class_addMethod(self, sel_registerName("autorelease"), (IMP)autorelease_fn, ""); + class_addMethod(self, sel_registerName("retainCount"), (IMP)retaincount_fn, ""); + class_addMethod(self, sel_registerName("copyWithZone:"), (IMP)copywithzone_fn, ""); + + class_addMethod(object_getClass(self), sel_registerName("retain"), (IMP)plusretain_fn, ""); + class_addMethod(object_getClass(self), sel_registerName("release"), (IMP)plusrelease_fn, ""); + class_addMethod(object_getClass(self), sel_registerName("autorelease"), (IMP)plusautorelease_fn, ""); + class_addMethod(object_getClass(self), sel_registerName("retainCount"), (IMP)plusretaincount_fn, ""); +} + + ++(void) initialize { + atomic_fetch_add_explicit(&TestRootInitialize, 1, memory_order_relaxed); +} + +-(id) self { + return self; +} + ++(Class) class { + return self; +} + +-(Class) class { + return object_getClass(self); +} + ++(Class) superclass { + return class_getSuperclass(self); +} + +-(Class) superclass { + return class_getSuperclass([self class]); +} + ++(id) new { + return [[self alloc] init]; +} + ++(id) alloc { + atomic_fetch_add_explicit(&TestRootAlloc, 1, memory_order_relaxed); + void * (*fn)(id __unsafe_unretained) = (typeof(fn))_objc_rootAlloc; + return (__bridge_transfer id)(fn(self)); +} + ++(id) allocWithZone:(void *)zone { + atomic_fetch_add_explicit(&TestRootAllocWithZone, 1, memory_order_relaxed); + void * (*fn)(id __unsafe_unretained, void *) = (typeof(fn))_objc_rootAllocWithZone; + return (__bridge_transfer id)(fn(self, zone)); +} + ++(id) copy { + return self; +} + ++(id) copyWithZone:(void *) __unused zone { + return self; +} + +-(id) copy { + atomic_fetch_add_explicit(&TestRootCopy, 1, memory_order_relaxed); + return [self copyWithZone:NULL]; +} + ++(id) mutableCopyWithZone:(void *) __unused zone { + fail("+mutableCopyWithZone: called"); +} + +-(id) mutableCopy { + atomic_fetch_add_explicit(&TestRootMutableCopy, 1, memory_order_relaxed); + return [self mutableCopyWithZone:NULL]; +} + +-(id) mutableCopyWithZone:(void *) __unused zone { + atomic_fetch_add_explicit(&TestRootMutableCopyWithZone, 1, memory_order_relaxed); + void * (*fn)(id __unsafe_unretained) = (typeof(fn))_objc_rootAlloc; + return (__bridge_transfer id)(fn(object_getClass(self))); +} + +-(id) init { + atomic_fetch_add_explicit(&TestRootInit, 1, memory_order_relaxed); + return _objc_rootInit(self); +} + ++(void) dealloc { + fail("+dealloc called"); +} + +-(void) dealloc { + atomic_fetch_add_explicit(&TestRootDealloc, 1, memory_order_relaxed); + _objc_rootDealloc(self); +} + ++(BOOL) _tryRetain { + return YES; +} + +-(BOOL) _tryRetain { + atomic_fetch_add_explicit(&TestRootTryRetain, 1, memory_order_relaxed); + return _objc_rootTryRetain(self); +} + ++(BOOL) _isDeallocating { + return NO; +} + +-(BOOL) _isDeallocating { + atomic_fetch_add_explicit(&TestRootIsDeallocating, 1, memory_order_relaxed); + return _objc_rootIsDeallocating(self); +} + +-(BOOL) allowsWeakReference { + return ! [self _isDeallocating]; +} + +-(BOOL) retainWeakReference { + return [self _tryRetain]; +} + + +@end diff --git a/test/timeout.pl b/test/timeout.pl new file mode 100644 index 0000000..750b21e --- /dev/null +++ b/test/timeout.pl @@ -0,0 +1,9 @@ +#!/usr/bin/perl -w + +use strict; + +my $usage = "timeout \n"; +my $timeout = shift || die $usage; +alarm($timeout); +exec @ARGV; +die "exec failed: @ARGV"; diff --git a/test/unload.h b/test/unload.h new file mode 100644 index 0000000..5287fe9 --- /dev/null +++ b/test/unload.h @@ -0,0 +1,6 @@ +#include "test.h" + +@interface SmallClass : TestRoot @end + +@interface BigClass : TestRoot @end + diff --git a/test/unload.m b/test/unload.m new file mode 100644 index 0000000..ccd99b7 --- /dev/null +++ b/test/unload.m @@ -0,0 +1,180 @@ +// xpc leaks memory in dlopen(). Disable it. +// TEST_ENV XPC_SERVICES_UNAVAILABLE=1 +/* +TEST_BUILD + $C{COMPILE} $DIR/unload4.m -o unload4.dylib -dynamiclib + $C{COMPILE_C} $DIR/unload3.c -o unload3.dylib -dynamiclib + $C{COMPILE} $DIR/unload2.m -o unload2.bundle -bundle $C{FORCE_LOAD_ARCLITE} -Xlinker -undefined -Xlinker dynamic_lookup + $C{COMPILE} $DIR/unload.m -o unload.exe -framework Foundation +END +*/ + +/* +TEST_BUILD_OUTPUT +ld: warning: -undefined dynamic_lookup is deprecated on .* +OR +END + */ + +#include "test.h" +#include +#include +#include + +#include "unload.h" + +#if __has_feature(objc_arc) + +int main() +{ + testwarn("rdar://11368528 confused by Foundation"); + succeed(__FILE__); +} + +#else + +static id forward_handler(void) +{ + return 0; +} + +static BOOL hasName(const char * const *names, const char *query) +{ + const char *name; + while ((name = *names++)) { + if (strstr(name, query)) return YES; + } + + return NO; +} + +void cycle(void) +{ + int i; + char buf[100]; + unsigned int imageCount, imageCount0; + const char **names; + const char *name; + + names = objc_copyImageNames(&imageCount0); + testassert(names); + free(names); + + void *bundle = dlopen("unload2.bundle", RTLD_LAZY); + testassert(bundle); + + names = objc_copyImageNames(&imageCount); + testassert(names); + testassert(imageCount == imageCount0 + 1); + testassert(hasName(names, "unload2.bundle")); + free(names); + + Class small = objc_getClass("SmallClass"); + Class big = objc_getClass("BigClass"); + Class missing = objc_getClass("SubclassOfMissingWeakImport"); + testassert(small); + testassert(big); + testassert(!missing); + + name = class_getImageName(small); + testassert(name); + testassert(strstr(name, "unload2.bundle")); + name = class_getImageName(big); + testassert(name); + testassert(strstr(name, "unload2.bundle")); + + id o1 = [small new]; + id o2 = [big new]; + testassert(o1); + testassert(o2); + + // give BigClass and BigClass->isa large method caches (4692641) + // Flush caches part way through to test large empty caches. + for (i = 0; i < 3000; i++) { + sprintf(buf, "method_%d", i); + SEL sel = sel_registerName(buf); + ((void(*)(id, SEL))objc_msgSend)(o2, sel); + ((void(*)(id, SEL))objc_msgSend)(object_getClass(o2), sel); + } + _objc_flush_caches(object_getClass(o2)); + for (i = 0; i < 17000; i++) { + sprintf(buf, "method_%d", i); + SEL sel = sel_registerName(buf); + ((void(*)(id, SEL))objc_msgSend)(o2, sel); + ((void(*)(id, SEL))objc_msgSend)(object_getClass(o2), sel); + } + + RELEASE_VAR(o1); + RELEASE_VAR(o2); + + testcollect(); + + int err = dlclose(bundle); + testassert(err == 0); + err = dlclose(bundle); + testassert(err == -1); // already closed + + _objc_flush_caches(nil); + + testassert(objc_getClass("SmallClass") == NULL); + testassert(objc_getClass("BigClass") == NULL); + + names = objc_copyImageNames(&imageCount); + testassert(names); + testassert(imageCount == imageCount0); + testassert(! hasName(names, "unload2.bundle")); + free(names); + + // these selectors came from the bundle + testassert(0 == strcmp("unload2_instance_method", sel_getName(sel_registerName("unload2_instance_method")))); + testassert(0 == strcmp("unload2_category_method", sel_getName(sel_registerName("unload2_category_method")))); + + // This protocol came from the bundle. + // It isn't unloaded cleanly (rdar://20664713), but neither + // may it cause the protocol table to crash after unloading. + testassert(objc_getProtocol("SmallProtocol")); +} + + +int main() +{ + objc_setForwardHandler((void*)&forward_handler, (void*)&forward_handler); + +#if defined(__arm__) || defined(__arm64__) + int count = 10; +#else + int count = is_guardmalloc() ? 10 : 100; +#endif + + cycle(); +#if __LP64__ + // fixme heap use goes up 512 bytes after the 2nd cycle only - bad or not? + cycle(); +#endif + + leak_mark(); + while (count--) { + cycle(); + } + leak_check(0); + + // 5359412 Make sure dylibs with nothing other than image_info can close + void *dylib = dlopen("unload3.dylib", RTLD_LAZY); + testassert(dylib); + int err = dlclose(dylib); + testassert(err == 0); + err = dlclose(dylib); + testassert(err == -1); // already closed + + // Make sure dylibs with real objc content cannot close + dylib = dlopen("unload4.dylib", RTLD_LAZY); + testassert(dylib); + err = dlclose(dylib); + testassert(err == 0); + err = dlclose(dylib); + testassert(err == -1); // already closed + + succeed(__FILE__); +} + +#endif diff --git a/test/unload2.m b/test/unload2.m new file mode 100644 index 0000000..b16fd63 --- /dev/null +++ b/test/unload2.m @@ -0,0 +1,42 @@ +#include "unload.h" +#include "testroot.i" +#import + +@implementation SmallClass : TestRoot +-(void)unload2_instance_method { } +@end + + +@implementation BigClass : TestRoot +@end + +OBJC_ROOT_CLASS +@interface UnusedClass { id isa; } @end +@implementation UnusedClass @end + + +@protocol SmallProtocol +-(void)unload2_category_method; +@end + +@interface SmallClass (Category) @end + +@implementation SmallClass (Category) +-(void)unload2_category_method { } +@end + +// This isn't really weak-import: we link with `-undefined dynamic_lookup` +// instead of providing a valid definition at link time. +// But it looks the same to the runtime. +__attribute__((weak_import)) +@interface ClassThatIsWeakImportAndMissing : TestRoot @end + +@interface SubclassOfMissingWeakImport : ClassThatIsWeakImportAndMissing @end +@implementation SubclassOfMissingWeakImport +-(void)unload2_category_method { } +@end + +@interface ClassThatIsWeakImportAndMissing (Category) @end +@implementation ClassThatIsWeakImportAndMissing (Category) +-(void)unload2_category_method { } +@end diff --git a/test/unload3.c b/test/unload3.c new file mode 100644 index 0000000..caf0473 --- /dev/null +++ b/test/unload3.c @@ -0,0 +1,10 @@ +// unload3: contains imageinfo but no other objc metadata +// libobjc must not keep it open + +#include + +int fake[2] __attribute__((section("__DATA,__objc_imageinfo"))) + = { 0, TARGET_OS_SIMULATOR ? (1<<5) : 0 }; + +// silence "no debug symbols in executable" warning +void fn(void) { } diff --git a/test/unload4.m b/test/unload4.m new file mode 100644 index 0000000..9608e74 --- /dev/null +++ b/test/unload4.m @@ -0,0 +1,7 @@ +// unload4: contains some objc metadata other than imageinfo +// libobjc must keep it open + +int fake2 __attribute__((section("__DATA,__objc_foo"))) = 0; + +// getsectiondata() falls over if __TEXT has no contents +const char *unload4 = "unload4"; diff --git a/test/unwind.m b/test/unwind.m new file mode 100644 index 0000000..3759147 --- /dev/null +++ b/test/unwind.m @@ -0,0 +1,81 @@ +// TEST_CONFIG + +#include "test.h" +#include +#include + +static int state; + +@interface Foo : NSObject @end +@interface Bar : NSObject @end + +@interface Foo (Unimplemented) ++(void)method; +@end + +@implementation Bar @end + +@implementation Foo + +-(void)check { state++; } ++(void)check { testassert(!"caught class object, not instance"); } + +static id exc; + +static void handler(id unused, void *ctx) __attribute__((used)); +static void handler(id unused __unused, void *ctx __unused) +{ + testassert(state == 3); state++; +} + ++(BOOL) resolveClassMethod:(SEL)__unused name +{ + testassert(state == 1); state++; +#if TARGET_OS_OSX + objc_addExceptionHandler(&handler, 0); + testassert(state == 2); +#else + state++; // handler would have done this +#endif + state++; + exc = [Foo new]; + @throw exc; +} + + +@end + +int main() +{ + // unwind exception and alt handler through objc_msgSend() + + PUSH_POOL { + + const int count = is_guardmalloc() ? 1000 : 100000; + state = 0; + for (int i = 0; i < count; i++) { + @try { + testassert(state == 0); state++; + [Foo method]; + testassert(0); + } @catch (Bar *e) { + testassert(0); + } @catch (Foo *e) { + testassert(e == exc); + testassert(state == 4); state++; + testassert(state == 5); [e check]; // state++ + RELEASE_VAR(exc); + } @catch (id e) { + testassert(0); + } @catch (...) { + testassert(0); + } @finally { + testassert(state == 6); state++; + } + testassert(state == 7); state = 0; + } + + } POP_POOL; + + succeed(__FILE__); +} diff --git a/test/weak.h b/test/weak.h new file mode 100644 index 0000000..6bae410 --- /dev/null +++ b/test/weak.h @@ -0,0 +1,67 @@ +/* + To test -weak-l or -weak-framework: + * -DWEAK_IMPORT= + * -DWEAK_FRAMEWORK=1 + * -UEMPTY when building the weak-not-missing library + * -DEMPTY= when building the weak-missing library + + To test attribute((weak_import)): + * -DWEAK_IMPORT=__attribute__((weak_import)) + * -UWEAK_FRAMEWORK + * -UEMPTY when building the weak-not-missing library + * -DEMPTY= when building the weak-missing library + +*/ + +#include "test.h" +#include + +extern int state; + +WEAK_IMPORT OBJC_ROOT_CLASS +@interface MissingRoot { + id isa; +} ++(void) initialize; ++(Class) class; ++(id) alloc; +-(id) init; +-(void) dealloc; ++(int) method; +@end + +@interface MissingRoot (RR) +-(id) retain; +-(void) release; +@end + +WEAK_IMPORT +@interface MissingSuper : MissingRoot { + @public + int ivar; +} +@end + +OBJC_ROOT_CLASS +@interface NotMissingRoot { + id isa; +} ++(void) initialize; ++(Class) class; ++(id) alloc; +-(id) init; +-(void) dealloc; ++(int) method; +@end + +@interface NotMissingRoot (RR) +-(id) retain; +-(void) release; +@end + +@interface NotMissingSuper : NotMissingRoot { + @public + int unused[100]; + int ivar; +} +@end diff --git a/test/weak.m b/test/weak.m new file mode 100644 index 0000000..77edef6 --- /dev/null +++ b/test/weak.m @@ -0,0 +1,316 @@ +// See instructions in weak.h + +#include "test.h" +#include "weak.h" + +// Subclass of superclass that isn't there +@interface MyMissingSuper : MissingSuper ++(int) method; +@end +@implementation MyMissingSuper ++(int) method { return 1+[super method]; } ++(void) load { state++; } +@end + +// Subclass of subclass of superclass that isn't there +@interface MyMissingSub : MyMissingSuper ++(int) method; +@end +@implementation MyMissingSub ++(int) method { return 1+[super method]; } ++(void) load { state++; } +@end + +// Subclass of real superclass +@interface MyNotMissingSuper : NotMissingSuper ++(int) method; +@end +@implementation MyNotMissingSuper ++(int) method { return 1+[super method]; } ++(void) load { state++; } +@end + +// Subclass of subclass of superclass that isn't there +@interface MyNotMissingSub : MyNotMissingSuper ++(int) method; +@end +@implementation MyNotMissingSub ++(int) method { return 1+[super method]; } ++(void) load { state++; } +@end + +// Categories on all of the above +@interface MissingRoot (MissingRootExtras) ++(void)load; ++(int) cat_method; +@end +@implementation MissingRoot (MissingRootExtras) ++(void)load { state++; } ++(int) cat_method { return 40; } +@end + +@interface MissingSuper (MissingSuperExtras) ++(void)load; ++(int) cat_method; +@end +@implementation MissingSuper (MissingSuperExtras) ++(void)load { state++; } ++(int) cat_method { return 1+[super cat_method]; } +@end + +@interface MyMissingSuper (MyMissingSuperExtras) ++(void)load; ++(int) cat_method; +@end +@implementation MyMissingSuper (MyMissingSuperExtras) ++(void)load { state++; } ++(int) cat_method { return 1+[super cat_method]; } +@end + +@interface MyMissingSub (MyMissingSubExtras) ++(void)load; ++(int) cat_method; +@end +@implementation MyMissingSub (MyMissingSubExtras) ++(void)load { state++; } ++(int) cat_method { return 1+[super cat_method]; } +@end + + +@interface NotMissingRoot (NotMissingRootExtras) ++(void)load; ++(int) cat_method; +@end +@implementation NotMissingRoot (NotMissingRootExtras) ++(void)load { state++; } ++(int) cat_method { return 30; } +@end + +@interface NotMissingSuper (NotMissingSuperExtras) ++(void)load; ++(int) cat_method; +@end +@implementation NotMissingSuper (NotMissingSuperExtras) ++(void)load { state++; } ++(int) cat_method { return 1+[super cat_method]; } +@end + +@interface MyNotMissingSuper (MyNotMissingSuperExtras) ++(void)load; ++(int) cat_method; +@end +@implementation MyNotMissingSuper (MyNotMissingSuperExtras) ++(void)load { state++; } ++(int) cat_method { return 1+[super cat_method]; } +@end + +@interface MyNotMissingSub (MyNotMissingSubExtras) ++(void)load; ++(int) cat_method; +@end +@implementation MyNotMissingSub (MyNotMissingSubExtras) ++(void)load { state++; } ++(int) cat_method { return 1+[super cat_method]; } +@end + + +#if WEAK_FRAMEWORK +# define TESTIVAR(cond) testassert(cond) +#else +# define TESTIVAR(cond) /* rdar */ +#endif + +static BOOL classInList(__unsafe_unretained Class classes[], const char *name) +{ + for (int i = 0; classes[i] != nil; i++) { + if (0 == strcmp(class_getName(classes[i]), name)) return YES; + } + return NO; +} + +static BOOL classInNameList(const char **names, const char *name) +{ + const char **cp; + for (cp = names; *cp; cp++) { + if (0 == strcmp(*cp, name)) return YES; + } + return NO; +} + +int main(int argc __unused, char **argv) +{ + BOOL weakMissing; + if (strstr(argv[0], "-not-missing.exe")) { + weakMissing = NO; + } else if (strstr(argv[0], "-missing.exe")) { + weakMissing = YES; + } else { + fail("executable name must be weak*-missing.exe or weak*-not-missing.exe"); + } + + // class and category +load methods + if (weakMissing) testassert(state == 8); + else testassert(state == 16); + state = 0; + + // classes + testassert([NotMissingRoot class]); + testassert([NotMissingSuper class]); + testassert([MyNotMissingSuper class]); + testassert([MyNotMissingSub class]); + if (weakMissing) { + testassert([MissingRoot class] == nil); + testassert([MissingSuper class] == nil); + testassert([MyMissingSuper class] == nil); + testassert([MyMissingSub class] == nil); + } else { + testassert([MissingRoot class]); + testassert([MissingSuper class]); + testassert([MyMissingSuper class]); + testassert([MyMissingSub class]); + } + + // objc_getClass + testassert(objc_getClass("NotMissingRoot")); + testassert(objc_getClass("NotMissingSuper")); + testassert(objc_getClass("MyNotMissingSuper")); + testassert(objc_getClass("MyNotMissingSub")); + if (weakMissing) { + testassert(objc_getClass("MissingRoot") == nil); + testassert(objc_getClass("MissingSuper") == nil); + testassert(objc_getClass("MyMissingSuper") == nil); + testassert(objc_getClass("MyMissingSub") == nil); + } else { + testassert(objc_getClass("MissingRoot")); + testassert(objc_getClass("MissingSuper")); + testassert(objc_getClass("MyMissingSuper")); + testassert(objc_getClass("MyMissingSub")); + } + + // class list + union { + Class *c; + void *v; + } classes; + classes.c = objc_copyClassList(NULL); + testassert(classInList(classes.c, "NotMissingRoot")); + testassert(classInList(classes.c, "NotMissingSuper")); + testassert(classInList(classes.c, "MyNotMissingSuper")); + testassert(classInList(classes.c, "MyNotMissingSub")); + if (weakMissing) { + testassert(! classInList(classes.c, "MissingRoot")); + testassert(! classInList(classes.c, "MissingSuper")); + testassert(! classInList(classes.c, "MyMissingSuper")); + testassert(! classInList(classes.c, "MyMissingSub")); + } else { + testassert(classInList(classes.c, "MissingRoot")); + testassert(classInList(classes.c, "MissingSuper")); + testassert(classInList(classes.c, "MyMissingSuper")); + testassert(classInList(classes.c, "MyMissingSub")); + } + free(classes.v); + + // class name list + const char *image = class_getImageName(objc_getClass("NotMissingRoot")); + testassert(image); + const char **names = objc_copyClassNamesForImage(image, NULL); + testassert(names); + testassert(classInNameList(names, "NotMissingRoot")); + testassert(classInNameList(names, "NotMissingSuper")); + if (weakMissing) { + testassert(! classInNameList(names, "MissingRoot")); + testassert(! classInNameList(names, "MissingSuper")); + } else { + testassert(classInNameList(names, "MissingRoot")); + testassert(classInNameList(names, "MissingSuper")); + } + free(names); + + image = class_getImageName(objc_getClass("MyNotMissingSub")); + testassert(image); + names = objc_copyClassNamesForImage(image, NULL); + testassert(names); + testassert(classInNameList(names, "MyNotMissingSuper")); + testassert(classInNameList(names, "MyNotMissingSub")); + if (weakMissing) { + testassert(! classInNameList(names, "MyMissingSuper")); + testassert(! classInNameList(names, "MyMissingSub")); + } else { + testassert(classInNameList(names, "MyMissingSuper")); + testassert(classInNameList(names, "MyMissingSub")); + } + free(names); + + // methods + testassert(20 == [NotMissingRoot method]); + testassert(21 == [NotMissingSuper method]); + testassert(22 == [MyNotMissingSuper method]); + testassert(23 == [MyNotMissingSub method]); + if (weakMissing) { + testassert(0 == [MissingRoot method]); + testassert(0 == [MissingSuper method]); + testassert(0 == [MyMissingSuper method]); + testassert(0 == [MyMissingSub method]); + } else { + testassert(10 == [MissingRoot method]); + testassert(11 == [MissingSuper method]); + testassert(12 == [MyMissingSuper method]); + testassert(13 == [MyMissingSub method]); + } + + // category methods + testassert(30 == [NotMissingRoot cat_method]); + testassert(31 == [NotMissingSuper cat_method]); + testassert(32 == [MyNotMissingSuper cat_method]); + testassert(33 == [MyNotMissingSub cat_method]); + if (weakMissing) { + testassert(0 == [MissingRoot cat_method]); + testassert(0 == [MissingSuper cat_method]); + testassert(0 == [MyMissingSuper cat_method]); + testassert(0 == [MyMissingSub cat_method]); + } else { + testassert(40 == [MissingRoot cat_method]); + testassert(41 == [MissingSuper cat_method]); + testassert(42 == [MyMissingSuper cat_method]); + testassert(43 == [MyMissingSub cat_method]); + } + + // allocations and ivars + id obj; + NotMissingSuper *obj2; + MissingSuper *obj3; + testassert((obj = [[NotMissingRoot alloc] init])); + RELEASE_VAR(obj); + testassert((obj2 = [[NotMissingSuper alloc] init])); + TESTIVAR(obj2->ivar == 200); + RELEASE_VAR(obj2); + testassert((obj2 = [[MyNotMissingSuper alloc] init])); + TESTIVAR(obj2->ivar == 200); + RELEASE_VAR(obj2); + testassert((obj2 = [[MyNotMissingSub alloc] init])); + TESTIVAR(obj2->ivar == 200); + RELEASE_VAR(obj2); + if (weakMissing) { + testassert([[MissingRoot alloc] init] == nil); + testassert([[MissingSuper alloc] init] == nil); + testassert([[MyMissingSuper alloc] init] == nil); + testassert([[MyMissingSub alloc] init] == nil); + } else { + testassert((obj = [[MissingRoot alloc] init])); + RELEASE_VAR(obj); + testassert((obj3 = [[MissingSuper alloc] init])); + TESTIVAR(obj3->ivar == 100); + RELEASE_VAR(obj3); + testassert((obj3 = [[MyMissingSuper alloc] init])); + TESTIVAR(obj3->ivar == 100); + RELEASE_VAR(obj3); + testassert((obj3 = [[MyMissingSub alloc] init])); + TESTIVAR(obj3->ivar == 100); + RELEASE_VAR(obj3); + } + + *strrchr(argv[0], '.') = 0; + succeed(basename(argv[0])); + return 0; +} + diff --git a/test/weak2.m b/test/weak2.m new file mode 100644 index 0000000..394363f --- /dev/null +++ b/test/weak2.m @@ -0,0 +1,82 @@ +// See instructions in weak.h + +#include "test.h" +#include "weak.h" +#include + +int state = 0; + +static void *noop_fn(void *self, SEL _cmd __unused) { + return self; +} +static void *retain_fn(void *self, SEL _cmd __unused) { + void * (*fn)(void *) = (typeof(fn))_objc_rootRetain; + return fn(self); +} +static void release_fn(void *self, SEL _cmd __unused) { + void (*fn)(void *) = (typeof(fn))_objc_rootRelease; + fn(self); +} +static void *autorelease_fn(void *self, SEL _cmd __unused) { + void * (*fn)(void *) = (typeof(fn))_objc_rootAutorelease; + return fn(self); +} + +#if !defined(EMPTY) + +@implementation MissingRoot ++(void) initialize { } ++(Class) class { return self; } ++(id) alloc { return _objc_rootAlloc(self); } ++(id) allocWithZone:(void*)zone { return _objc_rootAllocWithZone(self, (malloc_zone_t *)zone); } +-(id) init { return self; } +-(void) dealloc { _objc_rootDealloc(self); } ++(int) method { return 10; } ++(void) load { + class_addMethod(self, sel_registerName("retain"), (IMP)retain_fn, ""); + class_addMethod(self, sel_registerName("release"), (IMP)release_fn, ""); + class_addMethod(self, sel_registerName("autorelease"), (IMP)autorelease_fn, ""); + + class_addMethod(object_getClass(self), sel_registerName("retain"), (IMP)noop_fn, ""); + class_addMethod(object_getClass(self), sel_registerName("release"), (IMP)noop_fn, ""); + class_addMethod(object_getClass(self), sel_registerName("autorelease"), (IMP)noop_fn, ""); + + state++; +} +@end + +@implementation MissingSuper ++(int) method { return 1+[super method]; } +-(id) init { self = [super init]; ivar = 100; return self; } ++(void) load { state++; } +@end + +#endif + +@implementation NotMissingRoot ++(void) initialize { } ++(Class) class { return self; } ++(id) alloc { return _objc_rootAlloc(self); } ++(id) allocWithZone:(void*)zone { return _objc_rootAllocWithZone(self, (malloc_zone_t *)zone); } +-(id) init { return self; } +-(void) dealloc { _objc_rootDealloc(self); } ++(int) method { return 20; } ++(void) load { + class_addMethod(self, sel_registerName("retain"), (IMP)retain_fn, ""); + class_addMethod(self, sel_registerName("release"), (IMP)release_fn, ""); + class_addMethod(self, sel_registerName("autorelease"), (IMP)autorelease_fn, ""); + + class_addMethod(object_getClass(self), sel_registerName("retain"), (IMP)noop_fn, ""); + class_addMethod(object_getClass(self), sel_registerName("release"), (IMP)noop_fn, ""); + class_addMethod(object_getClass(self), sel_registerName("autorelease"), (IMP)noop_fn, ""); + + state++; +} +@end + +@implementation NotMissingSuper ++(int) method { return 1+[super method]; } +-(id) init { self = [super init]; ivar = 200; return self; } ++(void) load { state++; } +@end + diff --git a/test/weakcopy.m b/test/weakcopy.m new file mode 100644 index 0000000..778e36a --- /dev/null +++ b/test/weakcopy.m @@ -0,0 +1,62 @@ +// TEST_CFLAGS -fobjc-weak + +#include "test.h" + +#include "testroot.i" +#include +#include +#include + +@interface Weak : TestRoot { + @public + __weak id value; +} +@end +@implementation Weak +@end + +Weak *oldObject; +Weak *newObject; + +int main() +{ + testonthread(^{ + TestRoot *value; + + PUSH_POOL { + value = [TestRoot new]; + testassert(value); + oldObject = [Weak new]; + testassert(oldObject); + + oldObject->value = value; + testassert(oldObject->value == value); + + newObject = [oldObject copy]; + testassert(newObject); + testassert(newObject->value == oldObject->value); + + newObject->value = nil; + testassert(newObject->value == nil); + testassert(oldObject->value == value); + } POP_POOL; + + testcollect(); + TestRootDealloc = 0; + RELEASE_VAR(value); + }); + + testcollect(); + testassert(TestRootDealloc); + +#if __has_feature(objc_arc_weak) + testassert(oldObject->value == nil); +#endif + testassert(newObject->value == nil); + + RELEASE_VAR(newObject); + RELEASE_VAR(oldObject); + + succeed(__FILE__); + return 0; +} diff --git a/test/weakframework-missing.m b/test/weakframework-missing.m new file mode 100644 index 0000000..1d92433 --- /dev/null +++ b/test/weakframework-missing.m @@ -0,0 +1,14 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/weak2.m -DWEAK_FRAMEWORK=1 -DWEAK_IMPORT= -UEMPTY -dynamiclib -o libweakframework.dylib + + $C{COMPILE} $DIR/weakframework-missing.m -L. -weak-lweakframework -o weakframework-missing.exe + + $C{COMPILE} $DIR/weak2.m -DWEAK_FRAMEWORK=1 -DWEAK_IMPORT= -DEMPTY= -dynamiclib -o libweakframework.dylib + +END +*/ + +#define WEAK_FRAMEWORK 1 +#define WEAK_IMPORT +#include "weak.m" diff --git a/test/weakframework-not-missing.m b/test/weakframework-not-missing.m new file mode 100644 index 0000000..c348ba9 --- /dev/null +++ b/test/weakframework-not-missing.m @@ -0,0 +1,11 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/weak2.m -DWEAK_FRAMEWORK=1 -DWEAK_IMPORT= -UEMPTY -dynamiclib -o libweakframework.dylib + + $C{COMPILE} $DIR/weakframework-not-missing.m -L. -weak-lweakframework -o weakframework-not-missing.exe +END +*/ + +#define WEAK_FRAMEWORK 1 +#define WEAK_IMPORT +#include "weak.m" diff --git a/test/weakimport-missing.m b/test/weakimport-missing.m new file mode 100644 index 0000000..2fcdf83 --- /dev/null +++ b/test/weakimport-missing.m @@ -0,0 +1,13 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/weak2.m -UWEAK_FRAMEWORK -DWEAK_IMPORT=__attribute__\\(\\(weak_import\\)\\) -UEMPTY -dynamiclib -o libweakimport.dylib + + $C{COMPILE} $DIR/weakimport-missing.m -L. -weak-lweakimport -o weakimport-missing.exe + + $C{COMPILE} $DIR/weak2.m -UWEAK_FRAMEWORK -DWEAK_IMPORT=__attribute__\\(\\(weak_import\\)\\) -DEMPTY= -dynamiclib -o libweakimport.dylib +END +*/ + +// #define WEAK_FRAMEWORK +#define WEAK_IMPORT __attribute__((weak_import)) +#include "weak.m" diff --git a/test/weakimport-not-missing.m b/test/weakimport-not-missing.m new file mode 100644 index 0000000..6f5e18c --- /dev/null +++ b/test/weakimport-not-missing.m @@ -0,0 +1,11 @@ +/* +TEST_BUILD + $C{COMPILE} $DIR/weak2.m -UWEAK_FRAMEWORK -DWEAK_IMPORT=__attribute__\\(\\(weak_import\\)\\) -UEMPTY -dynamiclib -o libweakimport.dylib + + $C{COMPILE} $DIR/weakimport-not-missing.m -L. -weak-lweakimport -o weakimport-not-missing.exe +END +*/ + +// #define WEAK_FRAMEWORK +#define WEAK_IMPORT __attribute__((weak_import)) +#include "weak.m" diff --git a/test/weakrace.m b/test/weakrace.m new file mode 100644 index 0000000..2ff2ea9 --- /dev/null +++ b/test/weakrace.m @@ -0,0 +1,76 @@ +// TEST_CONFIG MEM=mrc + +#include "test.h" +#include + +static semaphore_t go1; +static semaphore_t go2; +static semaphore_t done; + +#define VARCOUNT 100000 +static id obj; +static id vars[VARCOUNT]; + + +void *destroyer(void *arg __unused) +{ + while (1) { + semaphore_wait(go1); + for (int i = 0; i < VARCOUNT; i++) { + objc_destroyWeak(&vars[i]); + } + semaphore_signal(done); + } +} + + +void *deallocator(void *arg __unused) +{ + while (1) { + semaphore_wait(go2); + [obj release]; + semaphore_signal(done); + } +} + + +void cycle(void) +{ + // rdar://12896779 objc_destroyWeak() versus weak clear in dealloc + + // Clean up from previous cycle - objc_destroyWeak() doesn't set var to nil + for (int i = 0; i < VARCOUNT; i++) { + vars[i] = nil; + } + + obj = [NSObject new]; + for (int i = 0; i < VARCOUNT; i++) { + objc_storeWeak(&vars[i], obj); + } + + // let destroyer start before deallocator runs + semaphore_signal(go1); + sched_yield(); + semaphore_signal(go2); + + semaphore_wait(done); + semaphore_wait(done); +} + + +int main() +{ + semaphore_create(mach_task_self(), &go1, 0, 0); + semaphore_create(mach_task_self(), &go2, 0, 0); + semaphore_create(mach_task_self(), &done, 0, 0); + + pthread_t th[2]; + pthread_create(&th[1], NULL, deallocator, NULL); + pthread_create(&th[1], NULL, destroyer, NULL); + + for (int i = 0; i < 100; i++) { + cycle(); + } + + succeed(__FILE__); +} diff --git a/test/willInitializeClassFunc.m b/test/willInitializeClassFunc.m new file mode 100644 index 0000000..4fef00e --- /dev/null +++ b/test/willInitializeClassFunc.m @@ -0,0 +1,109 @@ +// TEST_CONFIG MEM=mrc + +#import "test.h" +#import "testroot.i" + +#import + +#import + +char dummy; + +Class *seenClasses; +size_t seenClassesCount; + +static void clear(void) { + free(seenClasses); + seenClasses = NULL; + seenClassesCount = 0; +} + +static void willInitializeClass(void *context, Class cls) { + testprintf("Will initialize %s\n", class_getName(cls)); + seenClassesCount++; + seenClasses = (Class *)realloc(seenClasses, seenClassesCount * sizeof(*seenClasses)); + seenClasses[seenClassesCount - 1] = cls; + testassert(context == &dummy); +} + +int initializedC; +@interface C: TestRoot @end +@implementation C ++ (void)initialize { + testprintf("C initialize\n"); + initializedC = 1; +} +@end + +int initializedD; +@interface D: TestRoot @end +@implementation D ++ (void)initialize { + testprintf("D initialize\n"); + initializedD = 1; +} +@end + +int initializedE; +@interface E: TestRoot @end +@implementation E ++ (void)initialize { + testprintf("E initialize\n"); + initializedE = 1; +} +@end + +int main() +{ + _objc_addWillInitializeClassFunc(willInitializeClass, &dummy); + + // Merely getting a class should not trigger the callback. + clear(); + size_t oldCount = seenClassesCount; + Class c = objc_getClass("C"); + testassert(seenClassesCount == oldCount); + testassert(initializedC == 0); + + // Sending a message to C should trigger the callback and the superclass's callback. + [c class]; + testassert(seenClassesCount == oldCount + 2); + testassert(seenClasses[seenClassesCount - 2] == [TestRoot class]); + testassert(seenClasses[seenClassesCount - 1] == [C class]); + + // Sending a message to D should trigger the callback only for D, since the + // superclass is already initialized. + oldCount = seenClassesCount; + [D class]; + testassert(seenClassesCount == oldCount + 1); + testassert(seenClasses[seenClassesCount - 1] == [D class]); + + // Registering a second callback should inform us of all three exactly once. + clear(); + _objc_addWillInitializeClassFunc(willInitializeClass, &dummy); + testassert(seenClassesCount == 3); + + int foundRoot = 0; + int foundC = 0; + int foundD = 0; + for (size_t i = 0; i < seenClassesCount; i++) { + if (seenClasses[i] == [TestRoot class]) + foundRoot++; + if (seenClasses[i] == [C class]) + foundC++; + if (seenClasses[i] == [D class]) + foundD++; + } + testassert(foundRoot == 1); + testassert(foundC == 1); + testassert(foundD == 1); + + // Both callbacks should fire when sending a message to E. + clear(); + [E class]; + testassert(initializedE); + testassert(seenClassesCount == 2); + testassert(seenClasses[0] == [E class]); + testassert(seenClasses[1] == [E class]); + + succeed(__FILE__); +} diff --git a/test/zone.m b/test/zone.m new file mode 100644 index 0000000..46ec5ea --- /dev/null +++ b/test/zone.m @@ -0,0 +1,40 @@ +// TEST_CONFIG + +#include "test.h" +#include +#include + +// Look for malloc zone "ObjC" iff OBJC_USE_INTERNAL_ZONE is set. +// This fails if objc tries to allocate before checking its own +// environment variables (rdar://6688423) + +int main() +{ + if (is_guardmalloc()) { + // guard malloc confuses this test + succeed(__FILE__); + } + + kern_return_t kr; + vm_address_t *zones; + unsigned int count, i; + BOOL has_objc = NO, want_objc = NO; + + want_objc = (getenv("OBJC_USE_INTERNAL_ZONE") != NULL) ? YES : NO; + testprintf("want objc %s\n", want_objc ? "YES" : "NO"); + + kr = malloc_get_all_zones(mach_task_self(), NULL, &zones, &count); + testassert(!kr); + for (i = 0; i < count; i++) { + const char *name = malloc_get_zone_name((malloc_zone_t *)zones[i]); + if (name) { + BOOL is_objc = (0 == strcmp(name, "ObjC_Internal")) ? YES : NO; + if (is_objc) has_objc = YES; + testprintf("zone %s\n", name); + } + } + + testassert(want_objc == has_objc); + + succeed(__FILE__); +} diff --git a/version.bat b/version.bat old mode 100755 new mode 100644