From d37ab2a276401fb4cec0f85c0c963d2d8ea4140f Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 14 Oct 2024 14:00:16 +0200 Subject: [PATCH] wasm gc: fix issues with stack trace deobfuscator --- .../wasm/debug/info/DeobfuscatedLocation.java | 8 ++++++++ .../org/teavm/backend/wasm/debug/info/LineInfo.java | 12 +++++------- .../wasm/debug/info/LineInfoCommandExecutor.java | 2 ++ .../common/methods/BaseWasmGenerationVisitor.java | 2 ++ .../wasm/render/WasmBinaryRenderingVisitor.java | 13 +++++++------ 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/wasm/debug/info/DeobfuscatedLocation.java b/core/src/main/java/org/teavm/backend/wasm/debug/info/DeobfuscatedLocation.java index 550d31455..931b9fd4a 100644 --- a/core/src/main/java/org/teavm/backend/wasm/debug/info/DeobfuscatedLocation.java +++ b/core/src/main/java/org/teavm/backend/wasm/debug/info/DeobfuscatedLocation.java @@ -25,4 +25,12 @@ public class DeobfuscatedLocation { this.method = method; this.line = line; } + + @Override + public String toString() { + var sourceLocation = file == null || line < 0 + ? "Unknow source" + : file.name() + ":" + line; + return " at " + method.cls().name() + "." + method.name() + "(" + sourceLocation + ")"; + } } diff --git a/core/src/main/java/org/teavm/backend/wasm/debug/info/LineInfo.java b/core/src/main/java/org/teavm/backend/wasm/debug/info/LineInfo.java index 32dfc38c9..04ec334e7 100644 --- a/core/src/main/java/org/teavm/backend/wasm/debug/info/LineInfo.java +++ b/core/src/main/java/org/teavm/backend/wasm/debug/info/LineInfo.java @@ -61,15 +61,13 @@ public class LineInfo { } var result = new DeobfuscatedLocation[location.depth()]; var method = sequence.method(); - var i = result.length - 1; - while (true) { - result[i--] = new DeobfuscatedLocation(location.file(), method, location.line()); - if (i < 0) { - break; - } - method = location.inlining().method(); + var i = 0; + while (i < result.length - 1) { + var inlining = location.inlining(); + result[i++] = new DeobfuscatedLocation(location.file(), inlining.method(), location.line()); location = location.inlining().location(); } + result[i] = new DeobfuscatedLocation(location.file(), method, location.line()); return result; } diff --git a/core/src/main/java/org/teavm/backend/wasm/debug/info/LineInfoCommandExecutor.java b/core/src/main/java/org/teavm/backend/wasm/debug/info/LineInfoCommandExecutor.java index 2b5d05866..b18a7b2a6 100644 --- a/core/src/main/java/org/teavm/backend/wasm/debug/info/LineInfoCommandExecutor.java +++ b/core/src/main/java/org/teavm/backend/wasm/debug/info/LineInfoCommandExecutor.java @@ -36,6 +36,8 @@ public class LineInfoCommandExecutor implements LineInfoCommandVisitor { @Override public void visit(LineInfoExitCommand command) { address = command.address(); + file = inliningLocation.location().file(); + line = inliningLocation.location().line(); inliningLocation = inliningLocation.location().inlining(); } diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/common/methods/BaseWasmGenerationVisitor.java b/core/src/main/java/org/teavm/backend/wasm/generate/common/methods/BaseWasmGenerationVisitor.java index 9ef03adc9..07e7e920d 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/common/methods/BaseWasmGenerationVisitor.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/common/methods/BaseWasmGenerationVisitor.java @@ -779,6 +779,7 @@ public abstract class BaseWasmGenerationVisitor implements StatementVisitor, Exp var callSiteId = generateCallSiteId(expr.getLocation()); if (needsCallSiteId() && isManagedCall(expr.getMethod())) { var invocation = generateInvocation(expr, callSiteId); + invocation.setLocation(expr.getLocation()); var type = mapType(expr.getMethod().getReturnType()); List targetList; @@ -828,6 +829,7 @@ public abstract class BaseWasmGenerationVisitor implements StatementVisitor, Exp return block; } else { var resultExpr = generateInvocation(expr, null); + resultExpr.setLocation(expr.getLocation()); return trivialInvocation(resultExpr, resultConsumer, expr.getLocation(), willDrop); } } diff --git a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderingVisitor.java b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderingVisitor.java index c53a0d2b9..1823586c6 100644 --- a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderingVisitor.java +++ b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderingVisitor.java @@ -89,7 +89,7 @@ import org.teavm.backend.wasm.model.expression.WasmTest; import org.teavm.backend.wasm.model.expression.WasmThrow; import org.teavm.backend.wasm.model.expression.WasmTry; import org.teavm.backend.wasm.model.expression.WasmUnreachable; -import org.teavm.model.MethodReference; +import org.teavm.model.InliningInfo; import org.teavm.model.TextLocation; class WasmBinaryRenderingVisitor implements WasmExpressionVisitor { @@ -100,8 +100,8 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor { private int addressOffset; private int depth; private Map blockDepths = new HashMap<>(); - private List methodStack = new ArrayList<>(); - private List currentMethodStack = new ArrayList<>(); + private List methodStack = new ArrayList<>(); + private List currentMethodStack = new ArrayList<>(); private TextLocation textLocationToEmit; private boolean deferTextLocationToEmit; private TextLocation lastEmittedLocation; @@ -1441,13 +1441,13 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor { var loc = textLocationToEmit; var inlining = loc != null ? loc.getInlining() : null; while (inlining != null) { - currentMethodStack.add(inlining.getMethod()); + currentMethodStack.add(inlining); inlining = inlining.getParent(); } Collections.reverse(currentMethodStack); var commonPart = 0; while (commonPart < currentMethodStack.size() && commonPart < methodStack.size() - && currentMethodStack.get(commonPart).equals(methodStack.get(commonPart))) { + && currentMethodStack.get(commonPart) == methodStack.get(commonPart)) { ++commonPart; } while (methodStack.size() > commonPart) { @@ -1457,7 +1457,8 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor { while (commonPart < currentMethodStack.size()) { var method = currentMethodStack.get(commonPart++); methodStack.add(method); - debugLines.start(method); + debugLines.location(method.getFileName(), method.getLine()); + debugLines.start(method.getMethod()); } currentMethodStack.clear(); if (loc != null) {