[K/N] Catch exceptions in CAdapters when caches enabled (KT-47828 fixed)

This commit is contained in:
Elena Lepilkina
2021-09-30 10:58:03 +03:00
committed by Space
parent d8b456e8b6
commit c294365068
3 changed files with 14 additions and 8 deletions
@@ -466,7 +466,7 @@ private class ExportedElement(val kind: ElementKind,
"result", cfunction[0], Direction.KOTLIN_TO_C, builder) "result", cfunction[0], Direction.KOTLIN_TO_C, builder)
builder.append(" return $result;\n") builder.append(" return $result;\n")
} }
builder.append(" } catch (ExceptionObjHolder& e) { std::terminate(); } \n") builder.append(" } catch (...) { HandleCurrentExceptionForCInterop(); } \n")
builder.append("}\n") builder.append("}\n")
@@ -927,6 +927,7 @@ internal class CAdapterGenerator(val context: Context) : DeclarationDescriptorVi
|void Kotlin_initRuntimeIfNeeded(); |void Kotlin_initRuntimeIfNeeded();
|void Kotlin_mm_switchThreadStateRunnable() RUNTIME_NOTHROW; |void Kotlin_mm_switchThreadStateRunnable() RUNTIME_NOTHROW;
|void Kotlin_mm_switchThreadStateNative() RUNTIME_NOTHROW; |void Kotlin_mm_switchThreadStateNative() RUNTIME_NOTHROW;
|void HandleCurrentExceptionForCInterop();
| |
|KObjHeader* CreateStringFromCString(const char*, KObjHeader**); |KObjHeader* CreateStringFromCString(const char*, KObjHeader**);
|char* CreateCStringFromString(const KObjHeader*); |char* CreateCStringFromString(const KObjHeader*);
@@ -961,13 +962,6 @@ internal class CAdapterGenerator(val context: Context) : DeclarationDescriptorVi
| KObjHeader** frame() { return reinterpret_cast<KObjHeader**>(&frame_); } | KObjHeader** frame() { return reinterpret_cast<KObjHeader**>(&frame_); }
|}; |};
| |
|class ExceptionObjHolder {
|public:
| virtual ~ExceptionObjHolder() = default;
|
| KObjHeader* GetExceptionObject() noexcept;
|};
|
|class ScopedRunnableState { |class ScopedRunnableState {
|public: |public:
| ScopedRunnableState() noexcept { Kotlin_mm_switchThreadStateRunnable(); } | ScopedRunnableState() noexcept { Kotlin_mm_switchThreadStateRunnable(); }
@@ -45,6 +45,16 @@ void ThrowException(KRef exception) {
#endif #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 { namespace {
class { class {
@@ -28,6 +28,8 @@ void ThrowException(KRef exception);
void SetKonanTerminateHandler(); void SetKonanTerminateHandler();
void HandleCurrentExceptionForCInterop();
RUNTIME_NOTHROW OBJ_GETTER(Kotlin_getExceptionObject, void* holder); RUNTIME_NOTHROW OBJ_GETTER(Kotlin_getExceptionObject, void* holder);
// The functions below are implemented in Kotlin (at package kotlin.native.internal). // The functions below are implemented in Kotlin (at package kotlin.native.internal).