Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
[GR-52020] Backport to 23.0: Improve uncaught exception handler and e…
Browse files Browse the repository at this point in the history
…rror handling in CEntryPointSnippets.initializeIsolate().

PullRequest: graal/17124
  • Loading branch information
christianhaeubl committed Mar 5, 2024
2 parents 3016b3d + 4d321e6 commit 9693aa5
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,9 @@ protected void onValueUpdate(EconomicMap<OptionKey<?>, Object> values, String ol
@Option(help = "Specifies the number of entries that diagnostic buffers have.", type = OptionType.Debug)//
public static final HostedOptionKey<Integer> DiagnosticBufferSize = new HostedOptionKey<>(30);

@Option(help = "Determines if implicit exceptions are fatal if they don't have a stack trace.", type = OptionType.Debug)//
public static final RuntimeOptionKey<Boolean> ImplicitExceptionWithoutStacktraceIsFatal = new RuntimeOptionKey<>(false);

@SuppressWarnings("unused")//
@APIOption(name = "configure-reflection-metadata")//
@Option(help = "Enable runtime instantiation of reflection objects for non-invoked methods.", type = OptionType.Expert, deprecated = true)//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ private static int initializeIsolateInterruptibly(CEntryPointCreateIsolateParame
}

private static int initializeIsolateInterruptibly0(CEntryPointCreateIsolateParameters parameters) {
try {
return initializeIsolateInterruptibly1(parameters);
} catch (Throwable t) {
Log.log().string("Uncaught exception while initializing isolate: ").exception(t).newline();
return CEntryPointErrors.ISOLATE_INITIALIZATION_FAILED;
}
}

@NeverInline("GR-24649")
private static int initializeIsolateInterruptibly1(CEntryPointCreateIsolateParameters parameters) {
/*
* The VM operation thread must be started early as no VM operations can be scheduled before
* this thread is fully started. The isolate teardown may also use VM operations.
Expand Down Expand Up @@ -374,10 +384,11 @@ private static int initializeIsolateInterruptibly0(CEntryPointCreateIsolateParam
assert !isolateInitialized;
isolateInitialized = true;

/* Run isolate initialization hooks. */
try {
RuntimeSupport.executeInitializationHooks();
} catch (Throwable t) {
System.err.println("Uncaught exception while running initialization hooks:");
System.err.println("Uncaught exception while running isolate initialization hooks:");
t.printStackTrace();
return CEntryPointErrors.ISOLATE_INITIALIZATION_FAILED;
}
Expand All @@ -390,6 +401,7 @@ private static int initializeIsolateInterruptibly0(CEntryPointCreateIsolateParam
t.printStackTrace();
return CEntryPointErrors.ISOLATE_INITIALIZATION_FAILED;
}

return CEntryPointErrors.NO_ERROR;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public static String getRawMessage(Throwable ex) {
return SubstrateUtil.cast(ex, Target_java_lang_Throwable.class).detailMessage;
}

/**
* Returns the raw cause stored in {@link Throwable} and returned by default from
* {@link Throwable#getCause}. This method ignores possible overrides of
* {@link Throwable#getCause} and is therefore guaranteed to be allocation free.
*/
public static Throwable getRawCause(Throwable ex) {
Throwable cause = SubstrateUtil.cast(ex, Target_java_lang_Throwable.class).cause;
return cause == ex ? null : cause;
}

public static StackTraceElement[] getRawStackTrace(Throwable ex) {
return SubstrateUtil.cast(ex, Target_java_lang_Throwable.class).stackTrace;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ final class Target_java_lang_Throwable {
@Alias @RecomputeFieldValue(kind = Reset)//
private Object backtrace;

@Alias//
Throwable cause;

@Alias @RecomputeFieldValue(kind = Reset)//
StackTraceElement[] stackTrace;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,30 +634,41 @@ public Log exception(Throwable t, int maxFrames) {
return this;
}

/*
* We do not want to call getMessage(), since it can be overridden by subclasses of
* Throwable. So we access the raw detailMessage directly from the field in Throwable. That
* is better than printing nothing.
*/
String detailMessage = JDKUtils.getRawMessage(t);
StackTraceElement[] stackTrace = JDKUtils.getRawStackTrace(t);

string(t.getClass().getName()).string(": ").string(detailMessage);
if (stackTrace != null) {
int i;
for (i = 0; i < stackTrace.length && i < maxFrames; i++) {
StackTraceElement element = stackTrace[i];
if (element != null) {
newline();
string(" at ").string(element.getClassName()).string(".").string(element.getMethodName());
string("(").string(element.getFileName()).string(":").signed(element.getLineNumber()).string(")");
}
Throwable cur = t;
int maxCauses = 25;
for (int i = 0; i < maxCauses && cur != null; i++) {
if (i > 0) {
newline().string("Caused by: ");
}
int remaining = stackTrace.length - i;
if (remaining > 0) {
newline().string(" ... ").unsigned(remaining).string(" more");

/*
* We do not want to call getMessage(), since it can be overridden by subclasses of
* Throwable. So we access the raw detailMessage directly from the field in Throwable.
* That is better than printing nothing.
*/
String detailMessage = JDKUtils.getRawMessage(cur);
StackTraceElement[] stackTrace = JDKUtils.getRawStackTrace(cur);

string(cur.getClass().getName()).string(": ").string(detailMessage);
if (stackTrace != null) {
int j;
for (j = 0; j < stackTrace.length && j < maxFrames; j++) {
StackTraceElement element = stackTrace[j];
if (element != null) {
newline();
string(" at ").string(element.getClassName()).string(".").string(element.getMethodName());
string("(").string(element.getFileName()).string(":").signed(element.getLineNumber()).string(")");
}
}
int remaining = stackTrace.length - j;
if (remaining > 0) {
newline().string(" ... ").unsigned(remaining).string(" more");
}
}

cur = JDKUtils.getRawCause(cur);
}

return this;
}
}
Loading

0 comments on commit 9693aa5

Please sign in to comment.