From c2943650686b8c6289152cdcdfd788ce90a4e109 Mon Sep 17 00:00:00 2001 From: Elena Lepilkina Date: Thu, 30 Sep 2021 10:58:03 +0300 Subject: [PATCH] [K/N] Catch exceptions in CAdapters when caches enabled (KT-47828 fixed) --- .../kotlin/backend/konan/CAdapterGenerator.kt | 10 ++-------- kotlin-native/runtime/src/main/cpp/Exceptions.cpp | 10 ++++++++++ kotlin-native/runtime/src/main/cpp/Exceptions.h | 2 ++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CAdapterGenerator.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CAdapterGenerator.kt index 724e322df41..a516f42a3f4 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CAdapterGenerator.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CAdapterGenerator.kt @@ -466,7 +466,7 @@ private class ExportedElement(val kind: ElementKind, "result", cfunction[0], Direction.KOTLIN_TO_C, builder) builder.append(" return $result;\n") } - builder.append(" } catch (ExceptionObjHolder& e) { std::terminate(); } \n") + builder.append(" } catch (...) { HandleCurrentExceptionForCInterop(); } \n") builder.append("}\n") @@ -927,6 +927,7 @@ internal class CAdapterGenerator(val context: Context) : DeclarationDescriptorVi |void Kotlin_initRuntimeIfNeeded(); |void Kotlin_mm_switchThreadStateRunnable() RUNTIME_NOTHROW; |void Kotlin_mm_switchThreadStateNative() RUNTIME_NOTHROW; + |void HandleCurrentExceptionForCInterop(); | |KObjHeader* CreateStringFromCString(const char*, KObjHeader**); |char* CreateCStringFromString(const KObjHeader*); @@ -961,13 +962,6 @@ internal class CAdapterGenerator(val context: Context) : DeclarationDescriptorVi | KObjHeader** frame() { return reinterpret_cast(&frame_); } |}; | - |class ExceptionObjHolder { - |public: - | virtual ~ExceptionObjHolder() = default; - | - | KObjHeader* GetExceptionObject() noexcept; - |}; - | |class ScopedRunnableState { |public: | ScopedRunnableState() noexcept { Kotlin_mm_switchThreadStateRunnable(); } diff --git a/kotlin-native/runtime/src/main/cpp/Exceptions.cpp b/kotlin-native/runtime/src/main/cpp/Exceptions.cpp index e7052d9f45c..b2c5d11a4af 100644 --- a/kotlin-native/runtime/src/main/cpp/Exceptions.cpp +++ b/kotlin-native/runtime/src/main/cpp/Exceptions.cpp @@ -45,6 +45,16 @@ void ThrowException(KRef exception) { #endif } +void HandleCurrentExceptionForCInterop() { + try { + std::rethrow_exception(std::current_exception()); + } catch (ExceptionObjHolder& e) { + std::terminate(); // Terminate when it's a kotlin exception. + } catch (...) { + throw; // Just rethrow if it's unknown. + } +} + namespace { class { diff --git a/kotlin-native/runtime/src/main/cpp/Exceptions.h b/kotlin-native/runtime/src/main/cpp/Exceptions.h index e9ac3c6c31e..9f998fdf1f2 100644 --- a/kotlin-native/runtime/src/main/cpp/Exceptions.h +++ b/kotlin-native/runtime/src/main/cpp/Exceptions.h @@ -28,6 +28,8 @@ void ThrowException(KRef exception); void SetKonanTerminateHandler(); +void HandleCurrentExceptionForCInterop(); + RUNTIME_NOTHROW OBJ_GETTER(Kotlin_getExceptionObject, void* holder); // The functions below are implemented in Kotlin (at package kotlin.native.internal).