Unverified Commit f0454b49 authored by Pavel's avatar Pavel Committed by GitHub

Fix segfault on exception log (#417)

parent 688c97fc
...@@ -56,6 +56,7 @@ val casualRun = tasks.named<JavaExec>("run") { ...@@ -56,6 +56,7 @@ val casualRun = tasks.named<JavaExec>("run") {
systemProperty("skiko.win.exception.logger.enabled", "true") systemProperty("skiko.win.exception.logger.enabled", "true")
systemProperty("skiko.win.exception.handler.enabled", "true") systemProperty("skiko.win.exception.handler.enabled", "true")
jvmArgs?.add("-ea") jvmArgs?.add("-ea")
// jvmArgs?.add("-Xcheck:jni")
// Use systemProperty("skiko.library.path", "/tmp") to test loader. // Use systemProperty("skiko.library.path", "/tmp") to test loader.
System.getProperties().entries System.getProperties().entries
.associate { .associate {
......
...@@ -99,6 +99,19 @@ namespace java { ...@@ -99,6 +99,19 @@ namespace java {
return false; return false;
} }
} }
namespace System {
jclass cls;
jmethodID getProperty;
void onLoad(JNIEnv* env) {
jclass local = env->FindClass("java/lang/System");
cls = static_cast<jclass>(env->NewGlobalRef(local));
getProperty = env->GetStaticMethodID(cls, "getProperty", "(Ljava/lang/String;)Ljava/lang/String;");
}
void onUnload(JNIEnv* env) {
env->DeleteGlobalRef(cls);
}
}
} }
namespace util { namespace util {
...@@ -127,6 +140,7 @@ namespace java { ...@@ -127,6 +140,7 @@ namespace java {
lang::RuntimeException::onLoad(env); lang::RuntimeException::onLoad(env);
lang::String::onLoad(env); lang::String::onLoad(env);
lang::Throwable::onLoad(env); lang::Throwable::onLoad(env);
lang::System::onLoad(env);
util::Iterator::onLoad(env); util::Iterator::onLoad(env);
} }
...@@ -136,6 +150,7 @@ namespace java { ...@@ -136,6 +150,7 @@ namespace java {
lang::RuntimeException::onUnload(env); lang::RuntimeException::onUnload(env);
lang::Float::onUnload(env); lang::Float::onUnload(env);
lang::Boolean::onUnload(env); lang::Boolean::onUnload(env);
lang::System::onUnload(env);
} }
} }
......
...@@ -61,6 +61,13 @@ namespace java { ...@@ -61,6 +61,13 @@ namespace java {
void onLoad(JNIEnv* env); void onLoad(JNIEnv* env);
bool exceptionThrown(JNIEnv* env); bool exceptionThrown(JNIEnv* env);
} }
namespace System {
extern jclass cls;
extern jmethodID getProperty;
void onLoad(JNIEnv* env);
void onUnload(JNIEnv* env);
}
} }
namespace util { namespace util {
......
#if SK_BUILD_FOR_WIN #if SK_BUILD_FOR_WIN
#include "exceptions_handler.h" #include "exceptions_handler.h"
#include "../common/interop.hh"
static JavaVM *jvm = NULL;
bool isHandleException(JNIEnv *env) bool isHandleException(JNIEnv *env)
{ {
static jclass systemClass = NULL;
if (!systemClass)
{
systemClass = env->FindClass("java/lang/System");
}
static jmethodID getPropertyMethod = NULL;
if (!getPropertyMethod)
{
getPropertyMethod = env->GetStaticMethodID(systemClass, "getProperty", "(Ljava/lang/String;)Ljava/lang/String;");
}
jstring propertyName = env->NewStringUTF("skiko.win.exception.handler.enabled"); jstring propertyName = env->NewStringUTF("skiko.win.exception.handler.enabled");
jstring propertyString = (jstring)env->CallStaticObjectMethod(systemClass, getPropertyMethod, propertyName); jstring propertyString = (jstring)env->CallStaticObjectMethod(java::lang::System::cls, java::lang::System::getProperty, propertyName);
if (propertyString == 0) if (propertyString == 0)
{ {
return false; return false;
...@@ -81,29 +68,13 @@ const char *getDescription(DWORD code) ...@@ -81,29 +68,13 @@ const char *getDescription(DWORD code)
void logJavaException(JNIEnv *env, const char *function, DWORD sehCode) void logJavaException(JNIEnv *env, const char *function, DWORD sehCode)
{ {
char buffer[200];
int result = snprintf(
buffer, 200, "Native exception in [%s]:\nSEH description: %s\n", function, getDescription(sehCode));
if (jvm == NULL)
{
env->GetJavaVM(&jvm);
}
if (isHandleException(env)) if (isHandleException(env))
{ {
static jclass logClass = NULL; char buffer[200];
if (!logClass) int result = snprintf(
{ buffer, sizeof(buffer) - 1, "Native exception in [%s]:\nSEH description: %s\n", function, getDescription(sehCode));
logClass = env->FindClass("org/jetbrains/skiko/RenderExceptionsHandler"); jclass logClass = env->FindClass("org/jetbrains/skiko/RenderExceptionsHandler");
} jmethodID logMethod = env->GetStaticMethodID(logClass, "logAndThrow", "(Ljava/lang/String;)V");
static jmethodID logMethod = NULL;
if (!logMethod)
{
logMethod = env->GetStaticMethodID(logClass, "logAndThrow", "(Ljava/lang/String;)V");
}
env->CallStaticVoidMethod(logClass, logMethod, env->NewStringUTF(buffer)); env->CallStaticVoidMethod(logClass, logMethod, env->NewStringUTF(buffer));
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment