diff --git a/compiler/testData/codegen/box/cinterop/objc/illegal_sharing_with_weak.kt b/compiler/testData/codegen/box/cinterop/objc/illegal_sharing_with_weak.kt new file mode 100644 index 00000000000..e7c0c38088c --- /dev/null +++ b/compiler/testData/codegen/box/cinterop/objc/illegal_sharing_with_weak.kt @@ -0,0 +1,61 @@ +// TARGET_BACKEND: NATIVE +// DISABLE_NATIVE: isAppleTarget=false + +// MODULE: cinterop +// FILE: objclib.def +language = Objective-C +headers = objclib.h +headerFilter = objclib.h + +// FILE: objclib.h +#import + +static NSObject* __weak globalObject = nil; + +void setObject(NSObject* obj) { + globalObject = obj; +} + +// Make sure this function persists, because the test expects to find this function in the stack trace. +__attribute__((noinline)) +bool isObjectAliveShouldCrash() { + return globalObject != nil; +} + +// MODULE: main(cinterop) +// FILE: main.kt +@file:OptIn(ObsoleteWorkersApi::class, kotlinx.cinterop.ExperimentalForeignApi::class) +import kotlin.native.concurrent.* +import kotlin.test.* +import kotlinx.cinterop.autoreleasepool +import objclib.* + +val sb = StringBuilder() + +fun box(): String { + autoreleasepool { + run() + } + // Experimental MM supports arbitrary object sharing. + assertEquals("Before\nAfter true\n", sb.toString()) + return "OK" +} + +private class NSObjectImpl : NSObject() { + var x = 111 +} + +fun run() = withWorker { + val obj = NSObjectImpl() + setObject(obj) + + sb.appendLine("Before") + val isAlive = try { + execute(TransferMode.SAFE, {}) { + isObjectAliveShouldCrash() + }.result + } catch (e: Throwable) { + false + } + sb.appendLine("After $isAlive") +} diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 78a3549f6e2..617a9793488 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -1165,10 +1165,6 @@ if (PlatformInfo.isAppleTarget(project)) { it.headers "$projectDir/framework/gh3343/objclib.h" it.linkerOpts "-lobjcgh3343" } - createInterop("objc_illegal_sharing_with_weak") { - it.defFile 'interop/objc/illegal_sharing_with_weak/objclib.def' - it.headers "$projectDir/interop/objc/illegal_sharing_with_weak/objclib.h" - } createInterop("objcKt43517") { it.defFile 'framework/kt43517/kt43517.def' } @@ -1618,13 +1614,6 @@ if (PlatformInfo.isAppleTarget(project)) { } } - interopTest("interop_objc_illegal_sharing_with_weak") { - source = "interop/objc/illegal_sharing_with_weak/main.kt" - interop = 'objc_illegal_sharing_with_weak' - // Experimental MM supports arbitrary object sharing. - outputChecker = { it == "Before\nAfter true\n" } - } - interopTest("interop_objc_kt42172") { enabled = !isNoopGC source = "interop/objc/kt42172/main.kt" diff --git a/kotlin-native/backend.native/tests/interop/objc/illegal_sharing_with_weak/main.kt b/kotlin-native/backend.native/tests/interop/objc/illegal_sharing_with_weak/main.kt deleted file mode 100644 index c0e355cad71..00000000000 --- a/kotlin-native/backend.native/tests/interop/objc/illegal_sharing_with_weak/main.kt +++ /dev/null @@ -1,29 +0,0 @@ -@file:OptIn(ObsoleteWorkersApi::class) -import kotlin.native.concurrent.* -import kotlinx.cinterop.autoreleasepool -import objclib.* - -fun main() { - autoreleasepool { - run() - } -} - -private class NSObjectImpl : NSObject() { - var x = 111 -} - -fun run() = withWorker { - val obj = NSObjectImpl() - setObject(obj) - - println("Before") - val isAlive = try { - execute(TransferMode.SAFE, {}) { - isObjectAliveShouldCrash() - }.result - } catch (e: Throwable) { - false - } - println("After $isAlive") -} diff --git a/kotlin-native/backend.native/tests/interop/objc/illegal_sharing_with_weak/objclib.def b/kotlin-native/backend.native/tests/interop/objc/illegal_sharing_with_weak/objclib.def deleted file mode 100644 index 758f44a49a2..00000000000 --- a/kotlin-native/backend.native/tests/interop/objc/illegal_sharing_with_weak/objclib.def +++ /dev/null @@ -1,2 +0,0 @@ -language = Objective-C -headerFilter = **/objclib.h diff --git a/kotlin-native/backend.native/tests/interop/objc/illegal_sharing_with_weak/objclib.h b/kotlin-native/backend.native/tests/interop/objc/illegal_sharing_with_weak/objclib.h deleted file mode 100644 index b44bffe7dc3..00000000000 --- a/kotlin-native/backend.native/tests/interop/objc/illegal_sharing_with_weak/objclib.h +++ /dev/null @@ -1,13 +0,0 @@ -#import - -static NSObject* __weak globalObject = nil; - -void setObject(NSObject* obj) { - globalObject = obj; -} - -// Make sure this function persists, because the test expects to find this function in the stack trace. -__attribute__((noinline)) -bool isObjectAliveShouldCrash() { - return globalObject != nil; -} diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestGenerated.java index fa3843984cb..286176c3a4d 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestGenerated.java @@ -5068,6 +5068,12 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/objc"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true); } + @Test + @TestMetadata("illegal_sharing_with_weak.kt") + public void testIllegal_sharing_with_weak() throws Exception { + runTest("compiler/testData/codegen/box/cinterop/objc/illegal_sharing_with_weak.kt"); + } + @Test @TestMetadata("kt34467.kt") public void testKt34467() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestNoPLGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestNoPLGenerated.java index f6d122f1b69..4920a63c343 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestNoPLGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestNoPLGenerated.java @@ -5184,6 +5184,12 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/objc"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true); } + @Test + @TestMetadata("illegal_sharing_with_weak.kt") + public void testIllegal_sharing_with_weak() throws Exception { + runTest("compiler/testData/codegen/box/cinterop/objc/illegal_sharing_with_weak.kt"); + } + @Test @TestMetadata("kt34467.kt") public void testKt34467() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestGenerated.java index 3407572bed8..ea4a77afe51 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestGenerated.java @@ -4952,6 +4952,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/objc"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true); } + @Test + @TestMetadata("illegal_sharing_with_weak.kt") + public void testIllegal_sharing_with_weak() throws Exception { + runTest("compiler/testData/codegen/box/cinterop/objc/illegal_sharing_with_weak.kt"); + } + @Test @TestMetadata("kt34467.kt") public void testKt34467() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestNoPLGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestNoPLGenerated.java index e338de760aa..e3e0867c504 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestNoPLGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestNoPLGenerated.java @@ -5069,6 +5069,12 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/cinterop/objc"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true); } + @Test + @TestMetadata("illegal_sharing_with_weak.kt") + public void testIllegal_sharing_with_weak() throws Exception { + runTest("compiler/testData/codegen/box/cinterop/objc/illegal_sharing_with_weak.kt"); + } + @Test @TestMetadata("kt34467.kt") public void testKt34467() throws Exception {