From 6ed00f1af2916c27660e2a14bc63ace74459a27d Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 28 Oct 2019 13:59:03 +0300 Subject: [PATCH] C/Wasm: fix heap corruption when GC hits on initializing stack trace of exception --- .../java/org/teavm/classlib/java/lang/TThrowable.java | 8 +------- .../lowlevel/ExceptionHandlingShadowStackContributor.java | 8 +++++++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TThrowable.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TThrowable.java index 7c72f80aa..428d2a588 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TThrowable.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TThrowable.java @@ -22,7 +22,6 @@ import org.teavm.classlib.java.util.TArrays; import org.teavm.interop.Remove; import org.teavm.interop.Rename; import org.teavm.interop.Superclass; -import org.teavm.interop.Unmanaged; import org.teavm.runtime.ExceptionHandling; @Superclass("java.lang.Object") @@ -104,16 +103,11 @@ public class TThrowable extends RuntimeException { @Override public Throwable fillInStackTrace() { if (PlatformDetector.isLowLevel()) { - stackTrace = fillInStackTraceLowLevel(); + stackTrace = (TStackTraceElement[]) (Object) ExceptionHandling.fillStackTrace(); } return this; } - @Unmanaged - private static TStackTraceElement[] fillInStackTraceLowLevel() { - return (TStackTraceElement[]) (Object) ExceptionHandling.fillStackTrace(); - } - @Rename("getMessage") public String getMessage0() { return message; diff --git a/core/src/main/java/org/teavm/model/lowlevel/ExceptionHandlingShadowStackContributor.java b/core/src/main/java/org/teavm/model/lowlevel/ExceptionHandlingShadowStackContributor.java index 592d20372..4979946f9 100644 --- a/core/src/main/java/org/teavm/model/lowlevel/ExceptionHandlingShadowStackContributor.java +++ b/core/src/main/java/org/teavm/model/lowlevel/ExceptionHandlingShadowStackContributor.java @@ -67,6 +67,8 @@ import org.teavm.runtime.ExceptionHandling; import org.teavm.runtime.ShadowStack; public class ExceptionHandlingShadowStackContributor { + private static final MethodReference FILL_STACK_TRACE = new MethodReference(ExceptionHandling.class, + "fillStackTrace", StackTraceElement[].class); private Characteristics characteristics; private List callSites; private BasicBlock defaultExceptionHandler; @@ -281,7 +283,11 @@ public class ExceptionHandlingShadowStackContributor { || insn instanceof NullCheckInstruction || insn instanceof BoundCheckInstruction) { return true; } else if (insn instanceof InvokeInstruction) { - return isManagedMethodCall(characteristics, ((InvokeInstruction) insn).getMethod()); + MethodReference method = ((InvokeInstruction) insn).getMethod(); + if (method.equals(FILL_STACK_TRACE)) { + return true; + } + return isManagedMethodCall(characteristics, method); } return false; }