[K/N][Runtime] Switch thread states in termination handlers

This commit is contained in:
Ilya Matveev
2021-07-19 20:14:14 +07:00
committed by Space
parent a1f6c70355
commit 99bd26c2ef
15 changed files with 536 additions and 126 deletions
@@ -2669,7 +2669,7 @@ standaloneTest("kt-37572") {
standaloneTest("custom_hook") {
enabled = (project.testTarget != 'wasm32') // Uses exceptions.
outputChecker = {
it.contains("value 42: Error") && it.contains("Uncaught Kotlin exception: kotlin.Error: an error")
it.contains("value 42: Error. Runnable state: true") && it.contains("Uncaught Kotlin exception: kotlin.Error: an error")
}
expectedExitStatusChecker = { it != 0 }
source = "runtime/exceptions/custom_hook.kt"
@@ -4315,7 +4315,7 @@ Task interopTestMultifile(String name, Closure<KonanInteropTest> configureClosur
}
standaloneTest("interop_objc_allocException") {
disabled = !isAppleTarget(project) || isExperimentalMM // Experimental MM doesn't support thread state switching for terminate handlers yet.
disabled = !isAppleTarget(project)
expectedExitStatus = 0
source = "interop/objc/allocException.kt"
UtilsKt.dependsOnPlatformLibs(it)
@@ -4523,6 +4523,24 @@ interopTest("interop_threadStates_callbacksWithExceptions") {
interop = "threadStates"
}
interopTest("interop_threadStates_unhandledException") {
disabled = (project.testTarget == 'wasm32') || // No interop for wasm yet.
!isExperimentalMM // No thread state switching in the legacy MM.
source = "interop/threadStates/unhandledException.kt"
interop = "threadStates"
outputChecker = { it.contains("Error. Runnable state: true") }
expectedExitStatusChecker = { it != 0 }
}
interopTest("interop_threadStates_unhandledExceptionInForeignThread") {
disabled = (project.testTarget == 'wasm32') || // No interop for wasm yet.
!isExperimentalMM // No thread state switching in the legacy MM.
source = "interop/threadStates/unhandledExceptionInForeignThread.kt"
interop = "threadStates"
outputChecker = { it.contains("Error. Runnable state: true") }
expectedExitStatusChecker = { it != 0 }
}
interopTest("interop_withSpaces") {
disabled = (project.testTarget == 'wasm32') // No interop for wasm yet.
interop ='withSpaces'
@@ -4805,7 +4823,6 @@ if (PlatformInfo.isAppleTarget(project)) {
}
interopTest("interop_objc_foreignExceptionMode_default") {
enabled = !isExperimentalMM // Experimental MM doesn't support thread state switching for terminate handlers yet.
source = "interop/objc/foreignException/objcExceptionMode.kt"
interop = 'foreignExceptionMode_default'
goldValue = "OK: Ends with uncaught exception handler\n"
@@ -4976,6 +4993,15 @@ for (i in 0..2) {
}
}
dynamicTest("produce_dynamic_unhandledException") {
disabled = (project.testTarget == 'wasm32') || // Uses exceptions + dynamic is unsupported for wasm.
(cacheTesting != null) // Disabled due to KT-47828.
source = "produce_dynamic/unhandledException/unhandled.kt"
cSource = "$projectDir/produce_dynamic/unhandledException/main.cpp"
clangTool = "clang++"
expectedExitStatusChecker = { it != 0 }
outputChecker = { str -> str.startsWith("Kotlin hook: Exception. Runnable state: true") }
}
dynamicTest("interop_concurrentRuntime") {
disabled = (project.target.name != project.hostName)
@@ -1,3 +1,4 @@
#include <future>
#include <thread>
#include <stdint.h>
#include <stdlib.h>
@@ -17,4 +18,16 @@ extern "C" void runInNewThread(void(*callback)(void)) {
callback();
});
t.join();
}
extern "C" void runInForeignThread(void(*callback)(void)) {
std::thread t([callback]() {
// This thread is not attached to the Kotlin runtime.
auto future = std::async(std::launch::async, callback);
// The machinery of the direct interop doesn't filter out a Kotlin exception thrown by the callback.
// The get() call will re-throw this exception.
future.get();
});
t.join();
}
@@ -17,4 +17,5 @@ int32_t answer() {
return 42;
}
void runInNewThread(void(*callback)(void));
void runInNewThread(void(*callback)(void));
void runInForeignThread(void(*callback)(void));
@@ -0,0 +1,22 @@
/*
* 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.
*/
import kotlin.native.concurrent.*
import kotlin.native.internal.Debugging
import kotlinx.cinterop.staticCFunction
import threadStates.*
fun main() {
val hook = { throwable: Throwable ->
print("${throwable::class.simpleName}. Runnable state: ${Debugging.isThreadStateRunnable}")
}
if (Platform.memoryModel != MemoryModel.EXPERIMENTAL) {
hook.freeze()
}
setUnhandledExceptionHook(hook)
runInNewThread(staticCFunction<Unit> { throw Error("Error") })
}
@@ -0,0 +1,22 @@
/*
* 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.
*/
import kotlin.native.concurrent.*
import kotlin.native.internal.Debugging
import kotlinx.cinterop.staticCFunction
import threadStates.*
fun main() {
val hook = { throwable: Throwable ->
print("${throwable::class.simpleName}. Runnable state: ${Debugging.isThreadStateRunnable}")
}
if (Platform.memoryModel != MemoryModel.EXPERIMENTAL) {
hook.freeze()
}
setUnhandledExceptionHook(hook)
runInForeignThread(staticCFunction<Unit> { throw Error("Error") })
}
@@ -0,0 +1,19 @@
/*
* 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.
*/
#include "testlib_api.h"
#include <stdio.h>
#include <exception>
#include <iostream>
int main(int argc, char** argv) {
// The reverse interop machinery will catch the exception on the interop border and terminate the program.
try {
testlib_symbols()->kotlin.root.setHookAndThrow();
} catch (...) {
std::cout << "Should not happen" << std::endl;
}
}
@@ -0,0 +1,20 @@
/*
* 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.
*/
import kotlin.native.concurrent.*
import kotlin.native.internal.*
fun setHookAndThrow() {
val hook = { throwable: Throwable ->
print("Kotlin hook: ${throwable::class.simpleName}. Runnable state: ${Debugging.isThreadStateRunnable}")
}
if (Platform.memoryModel != MemoryModel.EXPERIMENTAL) {
hook.freeze()
}
setUnhandledExceptionHook(hook)
throw Exception("Error")
}
@@ -5,6 +5,7 @@
import kotlin.test.*
import kotlin.native.concurrent.*
import kotlin.native.internal.*
fun mainLegacyMM() {
assertFailsWith<InvalidMutabilityException> {
@@ -12,8 +13,8 @@ fun mainLegacyMM() {
}
val x = 42
val old = setUnhandledExceptionHook({
throwable: Throwable -> println("value $x: ${throwable::class.simpleName}")
val old = setUnhandledExceptionHook({ throwable: Throwable ->
println("value $x: ${throwable::class.simpleName}. Runnable state: ${Debugging.isThreadStateRunnable}")
}.freeze())
assertNull(old)
@@ -26,8 +27,8 @@ fun mainExperimentalMM() {
assertNull(unset)
val x = 42
val old = setUnhandledExceptionHook {
throwable: Throwable -> println("value $x: ${throwable::class.simpleName}")
val old = setUnhandledExceptionHook { throwable: Throwable ->
println("value $x: ${throwable::class.simpleName}. Runnable state: ${Debugging.isThreadStateRunnable}")
}
assertNotNull(old)