From bb8498b0c561cec55cdb93fd596320b19861a9a1 Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Thu, 30 Mar 2023 23:11:18 +0300 Subject: [PATCH] [K/N] Move kotlin.native.internal.Debugging into kotlin.native.runtime As a part of efforts to stabilize Native stdlib #KT-55765. --- .../backend.native/tests/build.gradle | 6 +++ .../backend.native/tests/interop/cpp/skia.kt | 2 +- .../leakMemoryWithRunningThread/checked.kt | 4 +- .../leakMemoryWithRunningThread/unchecked.kt | 4 +- .../tests/interop/objc/kt42172/main.kt | 4 +- .../tests/interop/objc/smoke.kt | 2 +- .../tests/interop/objc/smoke_noopgc.kt | 2 +- .../threadStates/callbacksWithExceptions.kt | 4 +- .../interop/threadStates/threadStates.kt | 4 +- .../threadStates/unhandledException.kt | 4 +- .../unhandledExceptionInForeignThread.kt | 4 +- .../unhandledException/unhandled.kt | 3 +- .../tests/runtime/exceptions/custom_hook.kt | 3 +- .../kotlin/native/internal/Debugging.kt | 22 +++----- .../kotlin/kotlin/native/runtime/Debugging.kt | 50 +++++++++++++++++++ 15 files changed, 89 insertions(+), 29 deletions(-) create mode 100644 kotlin-native/runtime/src/main/kotlin/kotlin/native/runtime/Debugging.kt diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 2b24f317358..9cdf510ebdd 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -2749,6 +2749,7 @@ standaloneTest("custom_hook") { } expectedExitStatusChecker = { it != 0 } source = "runtime/exceptions/custom_hook.kt" + flags = ['-opt-in=kotlin.native.internal.InternalForKotlinNative'] } standaloneTest("custom_hook_memory_leak") { @@ -4709,6 +4710,7 @@ interopTest("interop_threadStates") { disabled = (project.testTarget == 'wasm32') || // No interop for wasm yet. !isExperimentalMM // No thread state switching in the legacy MM. source = "interop/threadStates/threadStates.kt" + flags = ['-opt-in=kotlin.native.internal.InternalForKotlinNative'] interop = "threadStates" } @@ -4717,6 +4719,7 @@ interopTest("interop_threadStates_callbacksWithExceptions") { !isExperimentalMM // No thread state switching in the legacy MM. source = "interop/threadStates/callbacksWithExceptions.kt" interop = "threadStates" + flags = ['-opt-in=kotlin.native.internal.InternalForKotlinNative'] } interopTest("interop_threadStates_unhandledException") { @@ -4726,6 +4729,7 @@ interopTest("interop_threadStates_unhandledException") { interop = "threadStates" outputChecker = { it.contains("Error. Runnable state: true") } expectedExitStatusChecker = { it != 0 } + flags = ['-opt-in=kotlin.native.internal.InternalForKotlinNative'] } interopTest("interop_threadStates_unhandledExceptionInForeignThread") { @@ -4735,6 +4739,7 @@ interopTest("interop_threadStates_unhandledExceptionInForeignThread") { interop = "threadStates" outputChecker = { it.contains("Error. Runnable state: true") } expectedExitStatusChecker = { it != 0 } + flags = ['-opt-in=kotlin.native.internal.InternalForKotlinNative'] } interopTest("interop_withSpaces") { @@ -5408,6 +5413,7 @@ dynamicTest("produce_dynamic_unhandledException") { clangTool = "clang++" expectedExitStatusChecker = { it != 0 } outputChecker = { str -> str.startsWith("Kotlin hook: Exception. Runnable state: true") } + flags = ['-opt-in=kotlin.native.internal.InternalForKotlinNative'] } dynamicTest("interop_concurrentRuntime") { diff --git a/kotlin-native/backend.native/tests/interop/cpp/skia.kt b/kotlin-native/backend.native/tests/interop/cpp/skia.kt index 747fad08b97..51f53492ee6 100644 --- a/kotlin-native/backend.native/tests/interop/cpp/skia.kt +++ b/kotlin-native/backend.native/tests/interop/cpp/skia.kt @@ -11,7 +11,7 @@ fun main() { // TODO: the test used to work with forceCheckedShutdown, // but it is broken now. Revisit after it is fixed. - // kotlin.native.internal.Debugging.forceCheckedShutdown = true + // kotlin.native.runtime.Debugging.forceCheckedShutdown = true Platform.isCleanersLeakCheckerActive = true val f = Foo() diff --git a/kotlin-native/backend.native/tests/interop/leakMemoryWithRunningThread/checked.kt b/kotlin-native/backend.native/tests/interop/leakMemoryWithRunningThread/checked.kt index 0bb9ce4f50e..728df757229 100644 --- a/kotlin-native/backend.native/tests/interop/leakMemoryWithRunningThread/checked.kt +++ b/kotlin-native/backend.native/tests/interop/leakMemoryWithRunningThread/checked.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlin.native.runtime.NativeRuntimeApi::class) + import leakMemory.* import kotlin.native.concurrent.* import kotlin.native.Platform @@ -16,7 +18,7 @@ fun ensureInititalized() { fun main() { Platform.isMemoryLeakCheckerActive = true - kotlin.native.internal.Debugging.forceCheckedShutdown = true + kotlin.native.runtime.Debugging.forceCheckedShutdown = true assertTrue(global.value == 0) // Created a thread, made sure Kotlin is initialized there. test_RunInNewThread(staticCFunction(::ensureInititalized)) diff --git a/kotlin-native/backend.native/tests/interop/leakMemoryWithRunningThread/unchecked.kt b/kotlin-native/backend.native/tests/interop/leakMemoryWithRunningThread/unchecked.kt index 923018124d2..a7dfb14e974 100644 --- a/kotlin-native/backend.native/tests/interop/leakMemoryWithRunningThread/unchecked.kt +++ b/kotlin-native/backend.native/tests/interop/leakMemoryWithRunningThread/unchecked.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlin.native.runtime.NativeRuntimeApi::class) + import leakMemory.* import kotlin.native.concurrent.* import kotlin.native.Platform @@ -16,7 +18,7 @@ fun ensureInititalized() { fun main() { Platform.isMemoryLeakCheckerActive = true - kotlin.native.internal.Debugging.forceCheckedShutdown = false + kotlin.native.runtime.Debugging.forceCheckedShutdown = false assertTrue(global.value == 0) // Created a thread, made sure Kotlin is initialized there. test_RunInNewThread(staticCFunction(::ensureInititalized)) diff --git a/kotlin-native/backend.native/tests/interop/objc/kt42172/main.kt b/kotlin-native/backend.native/tests/interop/objc/kt42172/main.kt index 6e18fb7b047..f0f2fc79037 100644 --- a/kotlin-native/backend.native/tests/interop/objc/kt42172/main.kt +++ b/kotlin-native/backend.native/tests/interop/objc/kt42172/main.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlin.native.runtime.NativeRuntimeApi::class) + import objclib.* import kotlin.native.concurrent.* import kotlinx.cinterop.* @@ -19,6 +21,6 @@ fun main() { if (Platform.memoryModel == MemoryModel.EXPERIMENTAL) { // Experimental MM by default doesn't run GC neither on worker termination nor on program exit. // Enforce GC on program exit: - kotlin.native.internal.Debugging.forceCheckedShutdown = true + kotlin.native.runtime.Debugging.forceCheckedShutdown = true } } diff --git a/kotlin-native/backend.native/tests/interop/objc/smoke.kt b/kotlin-native/backend.native/tests/interop/objc/smoke.kt index 0408a0ea2d8..934acff44c4 100644 --- a/kotlin-native/backend.native/tests/interop/objc/smoke.kt +++ b/kotlin-native/backend.native/tests/interop/objc/smoke.kt @@ -11,7 +11,7 @@ import kotlin.test.* fun main(args: Array) { // Test relies on full deinitialization at shutdown. - kotlin.native.internal.Debugging.forceCheckedShutdown = true + kotlin.native.runtime.Debugging.forceCheckedShutdown = true autoreleasepool { run() } diff --git a/kotlin-native/backend.native/tests/interop/objc/smoke_noopgc.kt b/kotlin-native/backend.native/tests/interop/objc/smoke_noopgc.kt index 0408a0ea2d8..934acff44c4 100644 --- a/kotlin-native/backend.native/tests/interop/objc/smoke_noopgc.kt +++ b/kotlin-native/backend.native/tests/interop/objc/smoke_noopgc.kt @@ -11,7 +11,7 @@ import kotlin.test.* fun main(args: Array) { // Test relies on full deinitialization at shutdown. - kotlin.native.internal.Debugging.forceCheckedShutdown = true + kotlin.native.runtime.Debugging.forceCheckedShutdown = true autoreleasepool { run() } diff --git a/kotlin-native/backend.native/tests/interop/threadStates/callbacksWithExceptions.kt b/kotlin-native/backend.native/tests/interop/threadStates/callbacksWithExceptions.kt index 8747a403834..9c64d72707a 100644 --- a/kotlin-native/backend.native/tests/interop/threadStates/callbacksWithExceptions.kt +++ b/kotlin-native/backend.native/tests/interop/threadStates/callbacksWithExceptions.kt @@ -3,7 +3,9 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -import kotlin.native.internal.Debugging +@file:OptIn(kotlin.native.runtime.NativeRuntimeApi::class) + +import kotlin.native.runtime.Debugging import kotlin.test.* import kotlinx.cinterop.staticCFunction import threadStates.* diff --git a/kotlin-native/backend.native/tests/interop/threadStates/threadStates.kt b/kotlin-native/backend.native/tests/interop/threadStates/threadStates.kt index 0961bbf1833..4f29284c09f 100644 --- a/kotlin-native/backend.native/tests/interop/threadStates/threadStates.kt +++ b/kotlin-native/backend.native/tests/interop/threadStates/threadStates.kt @@ -3,7 +3,9 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -import kotlin.native.internal.Debugging +@file:OptIn(kotlin.native.runtime.NativeRuntimeApi::class) + +import kotlin.native.runtime.Debugging import kotlin.test.* import kotlinx.cinterop.* import threadStates.* diff --git a/kotlin-native/backend.native/tests/interop/threadStates/unhandledException.kt b/kotlin-native/backend.native/tests/interop/threadStates/unhandledException.kt index e47bcc8006e..1d6f79b56ae 100644 --- a/kotlin-native/backend.native/tests/interop/threadStates/unhandledException.kt +++ b/kotlin-native/backend.native/tests/interop/threadStates/unhandledException.kt @@ -2,10 +2,10 @@ * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -@file:OptIn(FreezingIsDeprecated::class, ExperimentalStdlibApi::class) +@file:OptIn(FreezingIsDeprecated::class, ExperimentalStdlibApi::class, kotlin.native.runtime.NativeRuntimeApi::class) import kotlin.native.concurrent.* -import kotlin.native.internal.Debugging +import kotlin.native.runtime.Debugging import kotlinx.cinterop.staticCFunction import threadStates.* diff --git a/kotlin-native/backend.native/tests/interop/threadStates/unhandledExceptionInForeignThread.kt b/kotlin-native/backend.native/tests/interop/threadStates/unhandledExceptionInForeignThread.kt index 09c62025064..82a71a7cf71 100644 --- a/kotlin-native/backend.native/tests/interop/threadStates/unhandledExceptionInForeignThread.kt +++ b/kotlin-native/backend.native/tests/interop/threadStates/unhandledExceptionInForeignThread.kt @@ -2,10 +2,10 @@ * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -@file:OptIn(FreezingIsDeprecated::class, ExperimentalStdlibApi::class) +@file:OptIn(FreezingIsDeprecated::class, ExperimentalStdlibApi::class, kotlin.native.runtime.NativeRuntimeApi::class) import kotlin.native.concurrent.* -import kotlin.native.internal.Debugging +import kotlin.native.runtime.Debugging import kotlinx.cinterop.staticCFunction import threadStates.* diff --git a/kotlin-native/backend.native/tests/produce_dynamic/unhandledException/unhandled.kt b/kotlin-native/backend.native/tests/produce_dynamic/unhandledException/unhandled.kt index 9e5d50e8524..cbc90eacfd1 100644 --- a/kotlin-native/backend.native/tests/produce_dynamic/unhandledException/unhandled.kt +++ b/kotlin-native/backend.native/tests/produce_dynamic/unhandledException/unhandled.kt @@ -2,10 +2,11 @@ * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -@file:OptIn(FreezingIsDeprecated::class, ExperimentalStdlibApi::class) +@file:OptIn(FreezingIsDeprecated::class, ExperimentalStdlibApi::class, kotlin.native.runtime.NativeRuntimeApi::class) import kotlin.native.concurrent.* import kotlin.native.internal.* +import kotlin.native.runtime.Debugging fun setHookAndThrow() { val hook = { throwable: Throwable -> diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook.kt b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook.kt index 362f3e6fb28..13f9c166c46 100644 --- a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook.kt +++ b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook.kt @@ -2,12 +2,13 @@ * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ -@file:OptIn(FreezingIsDeprecated::class, ExperimentalStdlibApi::class) +@file:OptIn(FreezingIsDeprecated::class, ExperimentalStdlibApi::class, kotlin.native.runtime.NativeRuntimeApi::class) import kotlin.test.* import kotlin.native.concurrent.* import kotlin.native.internal.* +import kotlin.native.runtime.Debugging fun mainLegacyMM() { val wrong = "wrong" diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Debugging.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Debugging.kt index cbce0d9b0d6..2761f7a1661 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Debugging.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Debugging.kt @@ -7,13 +7,14 @@ package kotlin.native.internal /* * Internal utilities for debugging K/N compiler and runtime. */ -public object Debugging { - public var forceCheckedShutdown: Boolean - get() = Debugging_getForceCheckedShutdown() - set(value) = Debugging_setForceCheckedShutdown(value) - public val isThreadStateRunnable: Boolean - get() = Debugging_isThreadStateRunnable() +@OptIn(kotlin.native.runtime.NativeRuntimeApi::class) +@Deprecated("Use kotlin.native.runtime.Debugging instead.", ReplaceWith("Debugging", "kotlin.native.runtime.Debugging")) +@DeprecatedSinceKotlin(warningSince = "1.9") +public object Debugging { + public var forceCheckedShutdown: Boolean by kotlin.native.runtime.Debugging::forceCheckedShutdown + + public val isThreadStateRunnable: Boolean by kotlin.native.runtime.Debugging::isThreadStateRunnable } @GCUnsafeCall("Kotlin_Debugging_isPermanent") @@ -23,12 +24,3 @@ public external fun Any.isPermanent() : Boolean @GCUnsafeCall("Kotlin_Debugging_isLocal") @InternalForKotlinNative public external fun Any.isLocal() : Boolean - -@GCUnsafeCall("Kotlin_Debugging_getForceCheckedShutdown") -private external fun Debugging_getForceCheckedShutdown(): Boolean - -@GCUnsafeCall("Kotlin_Debugging_setForceCheckedShutdown") -private external fun Debugging_setForceCheckedShutdown(value: Boolean): Unit - -@GCUnsafeCall("Kotlin_Debugging_isThreadStateRunnable") -private external fun Debugging_isThreadStateRunnable(): Boolean diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/runtime/Debugging.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/runtime/Debugging.kt new file mode 100644 index 00000000000..7aefc619328 --- /dev/null +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/runtime/Debugging.kt @@ -0,0 +1,50 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlin.native.runtime + +import kotlin.native.internal.GCUnsafeCall +import kotlin.native.internal.InternalForKotlinNative + +/** + * __Note__: this API is unstable and may change in any release. + * + * A set of utilities for debugging Kotlin/Native runtime. + */ +@NativeRuntimeApi +@SinceKotlin("1.9") +public object Debugging { + /** + * Run full checked deinitialization on shutdown. + * + * Make sure that after exiting `main()` only a single thread with Kotlin runtime remains. + * Run GC collecting everything including globals. + * + * When enabled together with [Platform.isCleanersLeakCheckerActive] additionally checks that no cleaners get executed after `main()` + */ + public var forceCheckedShutdown: Boolean + get() = Debugging_getForceCheckedShutdown() + set(value) = Debugging_setForceCheckedShutdown(value) + + /** + * Whether the current thread's state allows running Kotlin code. + * + * Used by Kotlin/Native internal tests. + * If it returns `false`, it's a bug. + */ + @InternalForKotlinNative + public val isThreadStateRunnable: Boolean + get() = Debugging_isThreadStateRunnable() +} + + +@GCUnsafeCall("Kotlin_Debugging_getForceCheckedShutdown") +private external fun Debugging_getForceCheckedShutdown(): Boolean + +@GCUnsafeCall("Kotlin_Debugging_setForceCheckedShutdown") +private external fun Debugging_setForceCheckedShutdown(value: Boolean): Unit + +@GCUnsafeCall("Kotlin_Debugging_isThreadStateRunnable") +private external fun Debugging_isThreadStateRunnable(): Boolean \ No newline at end of file