wasm gc: fix issues with stack trace deobfuscator

This commit is contained in:
Alexey Andreev 2024-10-14 14:00:16 +02:00
parent cfd381f47b
commit d37ab2a276
5 changed files with 24 additions and 13 deletions

View File

@ -25,4 +25,12 @@ public class DeobfuscatedLocation {
this.method = method; this.method = method;
this.line = line; 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 + ")";
}
} }

View File

@ -61,15 +61,13 @@ public class LineInfo {
} }
var result = new DeobfuscatedLocation[location.depth()]; var result = new DeobfuscatedLocation[location.depth()];
var method = sequence.method(); var method = sequence.method();
var i = result.length - 1; var i = 0;
while (true) { while (i < result.length - 1) {
result[i--] = new DeobfuscatedLocation(location.file(), method, location.line()); var inlining = location.inlining();
if (i < 0) { result[i++] = new DeobfuscatedLocation(location.file(), inlining.method(), location.line());
break;
}
method = location.inlining().method();
location = location.inlining().location(); location = location.inlining().location();
} }
result[i] = new DeobfuscatedLocation(location.file(), method, location.line());
return result; return result;
} }

View File

@ -36,6 +36,8 @@ public class LineInfoCommandExecutor implements LineInfoCommandVisitor {
@Override @Override
public void visit(LineInfoExitCommand command) { public void visit(LineInfoExitCommand command) {
address = command.address(); address = command.address();
file = inliningLocation.location().file();
line = inliningLocation.location().line();
inliningLocation = inliningLocation.location().inlining(); inliningLocation = inliningLocation.location().inlining();
} }

View File

@ -779,6 +779,7 @@ public abstract class BaseWasmGenerationVisitor implements StatementVisitor, Exp
var callSiteId = generateCallSiteId(expr.getLocation()); var callSiteId = generateCallSiteId(expr.getLocation());
if (needsCallSiteId() && isManagedCall(expr.getMethod())) { if (needsCallSiteId() && isManagedCall(expr.getMethod())) {
var invocation = generateInvocation(expr, callSiteId); var invocation = generateInvocation(expr, callSiteId);
invocation.setLocation(expr.getLocation());
var type = mapType(expr.getMethod().getReturnType()); var type = mapType(expr.getMethod().getReturnType());
List<WasmExpression> targetList; List<WasmExpression> targetList;
@ -828,6 +829,7 @@ public abstract class BaseWasmGenerationVisitor implements StatementVisitor, Exp
return block; return block;
} else { } else {
var resultExpr = generateInvocation(expr, null); var resultExpr = generateInvocation(expr, null);
resultExpr.setLocation(expr.getLocation());
return trivialInvocation(resultExpr, resultConsumer, expr.getLocation(), willDrop); return trivialInvocation(resultExpr, resultConsumer, expr.getLocation(), willDrop);
} }
} }

View File

@ -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.WasmThrow;
import org.teavm.backend.wasm.model.expression.WasmTry; import org.teavm.backend.wasm.model.expression.WasmTry;
import org.teavm.backend.wasm.model.expression.WasmUnreachable; import org.teavm.backend.wasm.model.expression.WasmUnreachable;
import org.teavm.model.MethodReference; import org.teavm.model.InliningInfo;
import org.teavm.model.TextLocation; import org.teavm.model.TextLocation;
class WasmBinaryRenderingVisitor implements WasmExpressionVisitor { class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
@ -100,8 +100,8 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
private int addressOffset; private int addressOffset;
private int depth; private int depth;
private Map<WasmBlock, Integer> blockDepths = new HashMap<>(); private Map<WasmBlock, Integer> blockDepths = new HashMap<>();
private List<MethodReference> methodStack = new ArrayList<>(); private List<InliningInfo> methodStack = new ArrayList<>();
private List<MethodReference> currentMethodStack = new ArrayList<>(); private List<InliningInfo> currentMethodStack = new ArrayList<>();
private TextLocation textLocationToEmit; private TextLocation textLocationToEmit;
private boolean deferTextLocationToEmit; private boolean deferTextLocationToEmit;
private TextLocation lastEmittedLocation; private TextLocation lastEmittedLocation;
@ -1441,13 +1441,13 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
var loc = textLocationToEmit; var loc = textLocationToEmit;
var inlining = loc != null ? loc.getInlining() : null; var inlining = loc != null ? loc.getInlining() : null;
while (inlining != null) { while (inlining != null) {
currentMethodStack.add(inlining.getMethod()); currentMethodStack.add(inlining);
inlining = inlining.getParent(); inlining = inlining.getParent();
} }
Collections.reverse(currentMethodStack); Collections.reverse(currentMethodStack);
var commonPart = 0; var commonPart = 0;
while (commonPart < currentMethodStack.size() && commonPart < methodStack.size() while (commonPart < currentMethodStack.size() && commonPart < methodStack.size()
&& currentMethodStack.get(commonPart).equals(methodStack.get(commonPart))) { && currentMethodStack.get(commonPart) == methodStack.get(commonPart)) {
++commonPart; ++commonPart;
} }
while (methodStack.size() > commonPart) { while (methodStack.size() > commonPart) {
@ -1457,7 +1457,8 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
while (commonPart < currentMethodStack.size()) { while (commonPart < currentMethodStack.size()) {
var method = currentMethodStack.get(commonPart++); var method = currentMethodStack.get(commonPart++);
methodStack.add(method); methodStack.add(method);
debugLines.start(method); debugLines.location(method.getFileName(), method.getLine());
debugLines.start(method.getMethod());
} }
currentMethodStack.clear(); currentMethodStack.clear();
if (loc != null) { if (loc != null) {