From cfd381f47bbd2e055495f27766f62dffe3095a56 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 13 Oct 2024 20:26:37 +0200 Subject: [PATCH] wasm gc: fix bugs in stack trace deobfuscator --- .../teavm/backend/wasm/debug/info/LineInfo.java | 16 +++++++++++----- .../backend/wasm/render/WasmBinaryRenderer.java | 2 ++ .../wasm/render/WasmBinaryRenderingVisitor.java | 4 ++++ .../src/main/resources/test-server/frame.js | 2 ++ .../tooling/deobfuscate/wasmgc/Deobfuscator.java | 2 +- .../main/resources/teavm-run-test-wasm-gc.html | 3 ++- 6 files changed, 22 insertions(+), 7 deletions(-) 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 9363fe597..32dfc38c9 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 @@ -53,18 +53,18 @@ public class LineInfo { } var instructionLoc = sequence.unpack().find(address); if (instructionLoc == null) { - return null; + return returnForSequence(sequence); } var location = instructionLoc.location(); if (location == null) { - return null; + return returnForSequence(sequence); } var result = new DeobfuscatedLocation[location.depth()]; var method = sequence.method(); - var i = 0; + var i = result.length - 1; while (true) { - result[i++] = new DeobfuscatedLocation(location.file(), method, location.line()); - if (i >= result.length) { + result[i--] = new DeobfuscatedLocation(location.file(), method, location.line()); + if (i < 0) { break; } method = location.inlining().method(); @@ -73,6 +73,12 @@ public class LineInfo { return result; } + private DeobfuscatedLocation[] returnForSequence(LineInfoSequence sequence) { + return new DeobfuscatedLocation[] { + new DeobfuscatedLocation(null, sequence.method(), -1) + }; + } + public LineInfoSequence find(int address) { var index = CollectionUtil.binarySearch(sequenceList, address, LineInfoSequence::endAddress); if (index < 0) { diff --git a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java index 35be5415f..a91e99ee8 100644 --- a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java +++ b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java @@ -364,6 +364,7 @@ public class WasmBinaryRenderer { dwarfSubprogram.function = function; } if (debugLines != null && function.getJavaMethod() != null) { + debugLines.advance(offset + sectionOffset); debugLines.start(function.getJavaMethod()); } @@ -398,6 +399,7 @@ public class WasmBinaryRenderer { for (var part : function.getBody()) { visitor.preprocess(part); } + visitor.setPositionToEmit(code.getPosition()); for (var part : function.getBody()) { part.acceptVisitor(visitor); } 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 302789f1a..c53a0d2b9 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 @@ -118,6 +118,10 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor { this.debugLines = debugLines; } + public void setPositionToEmit(int positionToEmit) { + this.positionToEmit = positionToEmit; + } + void preprocess(WasmExpression expression) { expression.acceptVisitor(new WasmDefaultExpressionVisitor() { @Override diff --git a/tools/browser-runner/src/main/resources/test-server/frame.js b/tools/browser-runner/src/main/resources/test-server/frame.js index 59dcd007f..0c77947da 100644 --- a/tools/browser-runner/src/main/resources/test-server/frame.js +++ b/tools/browser-runner/src/main/resources/test-server/frame.js @@ -16,6 +16,8 @@ "use strict"; +Error.stackTraceLimit = 250; + window.addEventListener("message", event => { let request = event.data; switch (request.type) { diff --git a/tools/deobfuscator-wasm-gc/src/main/java/org/teavm/tooling/deobfuscate/wasmgc/Deobfuscator.java b/tools/deobfuscator-wasm-gc/src/main/java/org/teavm/tooling/deobfuscate/wasmgc/Deobfuscator.java index 7f1a6e12f..5a4f051c6 100644 --- a/tools/deobfuscator-wasm-gc/src/main/java/org/teavm/tooling/deobfuscate/wasmgc/Deobfuscator.java +++ b/tools/deobfuscator-wasm-gc/src/main/java/org/teavm/tooling/deobfuscate/wasmgc/Deobfuscator.java @@ -33,7 +33,7 @@ public final class Deobfuscator { var frames = new JSArray(); for (var location : locations) { var frame = new Frame(location.method.cls().fullName(), location.method.name(), - location.file != null ? location.file.fullName() : null, location.line); + location.file != null ? location.file.name() : null, location.line); frames.push(frame); } return frames; diff --git a/tools/junit/src/main/resources/teavm-run-test-wasm-gc.html b/tools/junit/src/main/resources/teavm-run-test-wasm-gc.html index 7eb2e9072..747934e10 100644 --- a/tools/junit/src/main/resources/teavm-run-test-wasm-gc.html +++ b/tools/junit/src/main/resources/teavm-run-test-wasm-gc.html @@ -24,6 +24,7 @@