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") {
systemProperty("skiko.win.exception.logger.enabled", "true")
systemProperty("skiko.win.exception.handler.enabled", "true")
jvmArgs?.add("-ea")
// jvmArgs?.add("-Xcheck:jni")
// Use systemProperty("skiko.library.path", "/tmp") to test loader.
System.getProperties().entries
.associate {
......
......@@ -99,6 +99,19 @@ namespace java {
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 {
......@@ -127,6 +140,7 @@ namespace java {
lang::RuntimeException::onLoad(env);
lang::String::onLoad(env);
lang::Throwable::onLoad(env);
lang::System::onLoad(env);
util::Iterator::onLoad(env);
}
......@@ -136,6 +150,7 @@ namespace java {
lang::RuntimeException::onUnload(env);
lang::Float::onUnload(env);
lang::Boolean::onUnload(env);
lang::System::onUnload(env);
}
}
......
......@@ -61,6 +61,13 @@ namespace java {
void onLoad(JNIEnv* env);
bool exceptionThrown(JNIEnv* env);
}
namespace System {
extern jclass cls;
extern jmethodID getProperty;
void onLoad(JNIEnv* env);
void onUnload(JNIEnv* env);
}
}
namespace util {
......
#if SK_BUILD_FOR_WIN
#include "exceptions_handler.h"
static JavaVM *jvm = NULL;
#include "../common/interop.hh"
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 propertyString = (jstring)env->CallStaticObjectMethod(systemClass, getPropertyMethod, propertyName);
jstring propertyString = (jstring)env->CallStaticObjectMethod(java::lang::System::cls, java::lang::System::getProperty, propertyName);
if (propertyString == 0)
{
return false;
......@@ -81,29 +68,13 @@ const char *getDescription(DWORD code)
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))
{
static jclass logClass = NULL;
if (!logClass)
{
logClass = env->FindClass("org/jetbrains/skiko/RenderExceptionsHandler");
}
static jmethodID logMethod = NULL;
if (!logMethod)
{
logMethod = env->GetStaticMethodID(logClass, "logAndThrow", "(Ljava/lang/String;)V");
}
char buffer[200];
int result = snprintf(
buffer, sizeof(buffer) - 1, "Native exception in [%s]:\nSEH description: %s\n", function, getDescription(sehCode));
jclass logClass = env->FindClass("org/jetbrains/skiko/RenderExceptionsHandler");
jmethodID logMethod = env->GetStaticMethodID(logClass, "logAndThrow", "(Ljava/lang/String;)V");
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