Skip to content
This repository was archived by the owner on Aug 10, 2021. It is now read-only.

Commit e6cbbfb

Browse files
committed
Add DESTROY_RUNTIME_ON_SHUTDOWN_CHECKED
1 parent 7a1bdbd commit e6cbbfb

File tree

16 files changed

+229
-44
lines changed

16 files changed

+229
-44
lines changed

backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
250250
put(DISABLE_FAKE_OVERRIDE_VALIDATOR, arguments.disableFakeOverrideValidator)
251251
putIfNotNull(PRE_LINK_CACHES, parsePreLinkCachesValue(configuration, arguments.preLinkCaches))
252252
putIfNotNull(OVERRIDE_KONAN_PROPERTIES, parseOverrideKonanProperties(arguments, configuration))
253+
put(DESTROY_RUNTIME_MODE, when (arguments.destroyRuntimeMode) {
254+
"legacy" -> DestroyRuntimeMode.LEGACY
255+
"on-shutdown" -> DestroyRuntimeMode.ON_SHUTDOWN
256+
"on-shutdown-checked" -> DestroyRuntimeMode.ON_SHUTDOWN_CHECKED
257+
else -> {
258+
configuration.report(ERROR, "Unsupported destroy runtime mode ${arguments.destroyRuntimeMode}")
259+
DestroyRuntimeMode.ON_SHUTDOWN
260+
}
261+
})
253262
}
254263
}
255264
}

backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ class K2NativeCompilerArguments : CommonCompilerArguments() {
285285
)
286286
var overrideKonanProperties: Array<String>? = null
287287

288+
@Argument(value="-Xdestroy-runtime-mode", valueDescription = "<mode>", description = "When to destroy runtime. 'legacy', 'on-shutdown' and 'on-shutdown-checked' are currently supported. NOTE: 'legacy' mode is deprecated and will be removed.")
289+
var destroyRuntimeMode: String? = "on-shutdown"
290+
288291
override fun configureAnalysisFlags(collector: MessageCollector): MutableMap<AnalysisFlag<*>, Any> =
289292
super.configureAnalysisFlags(collector).also {
290293
val useExperimental = it[AnalysisFlags.useExperimental] as List<*>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
3+
* that can be found in the LICENSE file.
4+
*/
5+
package org.jetbrains.kotlin.backend.konan
6+
7+
// Must match DestroyRuntimeMode in Runtime.h
8+
enum class DestroyRuntimeMode(val value: Int) {
9+
LEGACY(0),
10+
ON_SHUTDOWN(1),
11+
ON_SHUTDOWN_CHECKED(2)
12+
}

backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration
4545
?: target.family.isAppleFamily // Default is true for Apple targets.
4646

4747
val memoryModel: MemoryModel get() = configuration.get(KonanConfigKeys.MEMORY_MODEL)!!
48+
val destroyRuntimeMode: DestroyRuntimeMode get() = configuration.get(KonanConfigKeys.DESTROY_RUNTIME_MODE)!!
4849

4950
val needCompilerVerification: Boolean
5051
get() = configuration.get(KonanConfigKeys.VERIFY_COMPILER) ?:
@@ -128,6 +129,10 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration
128129
configuration.report(CompilerMessageSeverity.STRONG_WARNING,
129130
"Experimental memory model requires threads, which are not supported on target ${target.name}. Used strict memory model.")
130131
MemoryModel.STRICT
132+
} else if (destroyRuntimeMode == DestroyRuntimeMode.LEGACY) {
133+
configuration.report(CompilerMessageSeverity.STRONG_WARNING,
134+
"Experimental memory model is incompatible with 'legacy' destroy runtime mode. Used strict memory model.")
135+
MemoryModel.STRICT
131136
} else {
132137
MemoryModel.EXPERIMENTAL
133138
}

backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ class KonanConfigKeys {
144144
= CompilerConfigurationKey.create("perform compiler caches pre-link")
145145
val OVERRIDE_KONAN_PROPERTIES: CompilerConfigurationKey<Map<String, String>>
146146
= CompilerConfigurationKey.create("override konan.properties values")
147+
val DESTROY_RUNTIME_MODE: CompilerConfigurationKey<DestroyRuntimeMode>
148+
= CompilerConfigurationKey.create("when to destroy runtime")
147149
}
148150
}
149151

backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
362362
codegen.objCDataGenerator?.finishModule()
363363

364364
context.coverage.writeRegionInfo()
365-
appendDebugSelector()
365+
appendCompilerGlobals()
366366
appendLlvmUsed("llvm.used", context.llvm.usedFunctions + context.llvm.usedGlobals)
367367
appendLlvmUsed("llvm.compiler.used", context.llvm.compilerUsedGlobals)
368368
if (context.isNativeLibrary) {
@@ -2378,13 +2378,19 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
23782378
LLVMSetSection(llvmUsedGlobal.llvmGlobal, "llvm.metadata")
23792379
}
23802380

2381-
private fun appendDebugSelector() {
2382-
if (!context.producedLlvmModuleContainsStdlib) return
2383-
val llvmDebugSelector =
2384-
context.llvm.staticData.placeGlobal("KonanNeedDebugInfo",
2385-
Int32(if (context.shouldContainDebugInfo()) 1 else 0))
2386-
llvmDebugSelector.setConstant(true)
2387-
llvmDebugSelector.setLinkage(LLVMLinkage.LLVMExternalLinkage)
2381+
private fun appendGlobal(name: String, value: ConstValue, isStrongSymbol: Boolean) {
2382+
val constant = context.llvm.staticData.placeGlobal(name, value)
2383+
constant.setConstant(true)
2384+
constant.setLinkage(if (isStrongSymbol) LLVMLinkage.LLVMExternalLinkage else LLVMLinkage.LLVMExternalWeakLinkage)
2385+
}
2386+
2387+
private fun appendCompilerGlobals() {
2388+
if (context.producedLlvmModuleContainsStdlib) {
2389+
appendGlobal("KonanNeedDebugInfo", Int32(if (context.shouldContainDebugInfo()) 1 else 0), true)
2390+
}
2391+
val isStrongSymbol = context.config.produce.isFinalBinary
2392+
println("Setting Kotlin_destroyRuntimeMode strong=$isStrongSymbol value=${context.config.destroyRuntimeMode.value}")
2393+
appendGlobal("Kotlin_destroyRuntimeMode", Int32(context.config.destroyRuntimeMode.value), isStrongSymbol)
23882394
}
23892395

23902396
//-------------------------------------------------------------------------//

backend.native/tests/build.gradle

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4351,9 +4351,20 @@ dynamicTest("interop_cleaners_leak") {
43514351
flags = ['-Xopt-in=kotlin.native.internal.InternalForKotlinNative']
43524352
}
43534353

4354+
dynamicTest("interop_migrating_main_thread_legacy") {
4355+
// TODO: Fix it.
4356+
disabled = (project.testTarget != null && project.testTarget != project.hostName)
4357+
source = "interop/migrating_main_thread/lib.kt"
4358+
flags = ['-Xdestroy-runtime-mode=legacy']
4359+
clangFlags = ['-DIS_LEGACY']
4360+
cSource = "$projectDir/interop/migrating_main_thread/main.cpp"
4361+
clangTool = "clang++"
4362+
}
4363+
43544364
dynamicTest("interop_migrating_main_thread") {
43554365
disabled = (project.target.name != project.hostName)
43564366
source = "interop/migrating_main_thread/lib.kt"
4367+
flags = ['-Xdestroy-runtime-mode=on-shutdown']
43574368
cSource = "$projectDir/interop/migrating_main_thread/main.cpp"
43584369
clangTool = "clang++"
43594370
}
@@ -4364,7 +4375,7 @@ dynamicTest("interop_memory_leaks") {
43644375
source = "interop/memory_leaks/lib.kt"
43654376
cSource = "$projectDir/interop/memory_leaks/main.cpp"
43664377
clangTool = "clang++"
4367-
flags = ['-g']
4378+
flags = ['-g', '-Xdestroy-runtime-mode=legacy'] // Runtime cannot be destroyed with interop with on-shutdown.
43684379
expectedExitStatusChecker = { it != 0 }
43694380
outputChecker = { s -> s.contains("Memory leaks detected, 1 objects leaked!") }
43704381
}

backend.native/tests/interop/migrating_main_thread/main.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,27 @@
88
#include <cassert>
99
#include <thread>
1010

11+
constexpr int kInitialValue = 0;
12+
constexpr int kNewValue = 0;
13+
14+
#if defined(IS_LEGACY)
15+
// Globals were reinitialized.
16+
constexpr int kResultValue = kInitialValue;
17+
#else
18+
// Globals were kept.
19+
constexpr int kResultValue = kNewValue;
20+
#endif
21+
1122
int main() {
1223
std::thread main1([]() {
13-
assert(testlib_symbols()->kotlin.root.readFromA() == 0);
14-
testlib_symbols()->kotlin.root.writeToA(1);
15-
assert(testlib_symbols()->kotlin.root.readFromA() == 1);
24+
assert(testlib_symbols()->kotlin.root.readFromA() == kInitialValue);
25+
testlib_symbols()->kotlin.root.writeToA(kNewValue);
26+
assert(testlib_symbols()->kotlin.root.readFromA() == kNewValue);
1627
});
1728
main1.join();
1829

1930
std::thread main2([]() {
20-
// Globals were reinitialized.
21-
assert(testlib_symbols()->kotlin.root.readFromA() == 0);
31+
assert(testlib_symbols()->kotlin.root.readFromA() == kResultValue);
2232
});
2333
main2.join();
2434

backend.native/tests/interop/objc/smoke.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import kotlin.native.ref.*
1010
import kotlin.test.*
1111

1212
fun main(args: Array<String>) {
13+
// Test relies on full deinitialization at shutdown.
14+
kotlin.native.Platform.forceCheckedShutdown = true
1315
autoreleasepool {
1416
run()
1517
}

build-tools/src/main/kotlin/org/jetbrains/kotlin/KotlinNativeTest.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,12 @@ open class KonanDynamicTest : KonanStandaloneTest() {
418418
@Input
419419
lateinit var cSource: String
420420

421+
@Input
421422
var clangTool = "clang"
422423

424+
@Input
425+
var clangFlags: List<String> = listOf()
426+
423427
// Replace testlib_api.h and all occurrences of the testlib with the actual name of the test
424428
private fun processCSource(): String {
425429
val sourceFile = File(cSource)
@@ -452,7 +456,7 @@ open class KonanDynamicTest : KonanStandaloneTest() {
452456
"-c",
453457
"-o", "$executable.o",
454458
"-I", artifactsDir
455-
)
459+
) + clangFlags
456460
it.standardOutput = log
457461
it.errorOutput = log
458462
it.isIgnoreExitValue = true
@@ -481,4 +485,4 @@ open class KonanDynamicTest : KonanStandaloneTest() {
481485
it.execute()
482486
}
483487
}
484-
}
488+
}

0 commit comments

Comments
 (0)