[K/N] Small refactoring (review fixes)

This commit is contained in:
Elena Lepilkina
2021-10-25 12:43:10 +03:00
committed by Space
parent 778bbaf5b0
commit 48e3df5224
9 changed files with 16 additions and 16 deletions
@@ -469,7 +469,7 @@ private class ExportedElement(val kind: ElementKind,
} }
builder.append(" } catch (...) {") builder.append(" } catch (...) {")
builder.append(" SetCurrentFrame(reinterpret_cast<KObjHeader**>(frame));\n") builder.append(" SetCurrentFrame(reinterpret_cast<KObjHeader**>(frame));\n")
builder.append(" HandleCurrentExceptionForCInterop();\n") builder.append(" HandleCurrentExceptionWhenLeavingKotlinCode();\n")
builder.append(" } \n") builder.append(" } \n")
builder.append("}\n") builder.append("}\n")
@@ -936,7 +936,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(); |void HandleCurrentExceptionWhenLeavingKotlinCode();
| |
|KObjHeader* CreateStringFromCString(const char*, KObjHeader**); |KObjHeader* CreateStringFromCString(const char*, KObjHeader**);
|char* CreateCStringFromString(const KObjHeader*); |char* CreateCStringFromString(const KObjHeader*);
@@ -475,10 +475,12 @@ internal abstract class FunctionGenerationContext(
private val entryBb = basicBlockInFunction("entry", startLocation) private val entryBb = basicBlockInFunction("entry", startLocation)
protected val cleanupLandingpad = basicBlockInFunction("cleanup_landingpad", endLocation) protected val cleanupLandingpad = basicBlockInFunction("cleanup_landingpad", endLocation)
protected open val needLeaveFrameInUnwindEpilogue: Boolean // Functions that can be exported and called not only from Kotlin code should have `LeaveFrame`
// because there is no guarantee of catching Kotlin exception in Kotlin code.
protected open val needLeaveFrame: Boolean
get() = irFunction?.annotations?.hasAnnotation(RuntimeNames.exportForCppRuntime) == true get() = irFunction?.annotations?.hasAnnotation(RuntimeNames.exportForCppRuntime) == true
|| forwardingForeignExceptionsTerminatedWith != null
|| irFunction?.origin == CBridgeOrigin.C_TO_KOTLIN_BRIDGE || irFunction?.origin == CBridgeOrigin.C_TO_KOTLIN_BRIDGE
private var setCurrentFrameIsCalled: Boolean = false private var setCurrentFrameIsCalled: Boolean = false
val stackLocalsManager = StackLocalsManagerImpl(this, stackLocalsInitBb) val stackLocalsManager = StackLocalsManagerImpl(this, stackLocalsInitBb)
@@ -1051,7 +1053,6 @@ internal abstract class FunctionGenerationContext(
} }
fun generateFrameCheck() { fun generateFrameCheck() {
assert(slotsPhi != null)
call(context.llvm.checkCurrentFrameFunction, listOf(slotsPhi!!)) call(context.llvm.checkCurrentFrameFunction, listOf(slotsPhi!!))
} }
@@ -1426,7 +1427,7 @@ internal abstract class FunctionGenerationContext(
} }
internal fun epilogue() { internal fun epilogue() {
val needLeaveFrameInUnwindEpilogue = this.needLeaveFrameInUnwindEpilogue val needLeaveFrameInUnwindEpilogue = this.needLeaveFrame
appendingTo(prologueBb) { appendingTo(prologueBb) {
val slots = if (needSlotsPhi || needLeaveFrameInUnwindEpilogue) val slots = if (needSlotsPhi || needLeaveFrameInUnwindEpilogue)
@@ -1715,7 +1716,7 @@ internal abstract class FunctionGenerationContext(
} }
private fun releaseVars() { private fun releaseVars() {
if (needLeaveFrameInUnwindEpilogue || needSlots) { if (needLeaveFrame || needSlots) {
check(!forbidRuntime) { "Attempt to leave a frame where runtime usage is forbidden" } check(!forbidRuntime) { "Attempt to leave a frame where runtime usage is forbidden" }
call(context.llvm.leaveFrameFunction, call(context.llvm.leaveFrameFunction,
listOf(slotsPhi!!, Int32(vars.skipSlots).llvm, Int32(slotCount).llvm)) listOf(slotsPhi!!, Int32(vars.skipSlots).llvm, Int32(slotCount).llvm))
@@ -53,7 +53,9 @@ internal class ObjCExportFunctionGenerationContext(
) : FunctionGenerationContext(builder) { ) : FunctionGenerationContext(builder) {
private val objCExportCodegen = builder.objCExportCodegen private val objCExportCodegen = builder.objCExportCodegen
override val needLeaveFrameInUnwindEpilogue: Boolean // All generated bridges by ObjCExport should have `LeaveFrame`
// because there is no guarantee of catching Kotlin exception in Kotlin code.
override val needLeaveFrame: Boolean
get() = true get() = true
// Note: we could generate single "epilogue" and make all [ret]s just branch to it (like [DefaultFunctionGenerationContext]), // Note: we could generate single "epilogue" and make all [ret]s just branch to it (like [DefaultFunctionGenerationContext]),
@@ -45,13 +45,11 @@ void ThrowException(KRef exception) {
#endif #endif
} }
void HandleCurrentExceptionForCInterop() { void HandleCurrentExceptionWhenLeavingKotlinCode() {
try { try {
std::rethrow_exception(std::current_exception()); std::rethrow_exception(std::current_exception());
} catch (ExceptionObjHolder& e) { } catch (ExceptionObjHolder& e) {
std::terminate(); // Terminate when it's a kotlin exception. std::terminate(); // Terminate when it's a kotlin exception.
} catch (...) {
throw; // Just rethrow if it's unknown.
} }
} }
@@ -28,7 +28,7 @@ void ThrowException(KRef exception);
void SetKonanTerminateHandler(); void SetKonanTerminateHandler();
void HandleCurrentExceptionForCInterop(); void HandleCurrentExceptionWhenLeavingKotlinCode();
RUNTIME_NOTHROW OBJ_GETTER(Kotlin_getExceptionObject, void* holder); RUNTIME_NOTHROW OBJ_GETTER(Kotlin_getExceptionObject, void* holder);
@@ -18,7 +18,6 @@
#define RUNTIME_MEMORY_H #define RUNTIME_MEMORY_H
#include <utility> #include <utility>
#include <functional>
#include "KAssert.h" #include "KAssert.h"
#include "Common.h" #include "Common.h"
@@ -1080,7 +1080,7 @@ JobKind Worker::processQueueElement(bool blocking) {
#if !KONAN_NO_EXCEPTIONS #if !KONAN_NO_EXCEPTIONS
FrameOverlay* currentFrame = getCurrentFrame(); FrameOverlay* currentFrame = getCurrentFrame();
#else #else
RuntimeFail("Exceptions aren't supported!\n"); #error "Exceptions aren't supported!"
#endif #endif
try { try {
#if KONAN_OBJC_INTEROP #if KONAN_OBJC_INTEROP
@@ -45,6 +45,6 @@ FrameOverlay* mm::ShadowStack::getCurrentFrame() noexcept {
return currentFrame_; return currentFrame_;
} }
void mm::ShadowStack::checkCurrentFrame(FrameOverlay* frame) noexcept { ALWAYS_INLINE void mm::ShadowStack::checkCurrentFrame(FrameOverlay* frame) noexcept {
RuntimeAssert(currentFrame_ == frame, "Frame is expected to be %p, but current frame is %p", frame, currentFrame_); RuntimeAssert(currentFrame_ == frame, "Frame is expected to be %p, but current frame is %p", frame, currentFrame_);
} }
@@ -55,7 +55,7 @@ public:
void LeaveFrame(ObjHeader** start, int parameters, int count) noexcept; void LeaveFrame(ObjHeader** start, int parameters, int count) noexcept;
void SetCurrentFrame(ObjHeader** start) noexcept; void SetCurrentFrame(ObjHeader** start) noexcept;
FrameOverlay* getCurrentFrame() noexcept; FrameOverlay* getCurrentFrame() noexcept;
void checkCurrentFrame(FrameOverlay* frame) noexcept; ALWAYS_INLINE void checkCurrentFrame(FrameOverlay* frame) noexcept;
Iterator begin() noexcept { return Iterator(currentFrame_); } Iterator begin() noexcept { return Iterator(currentFrame_); }
Iterator end() noexcept { return Iterator(nullptr); } Iterator end() noexcept { return Iterator(nullptr); }