wasm gc: fix bugs in stack trace deobfuscator

This commit is contained in:
Alexey Andreev 2024-10-13 20:26:37 +02:00
parent 4f9208c4d4
commit cfd381f47b
6 changed files with 22 additions and 7 deletions

View File

@ -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) {

View File

@ -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);
}

View File

@ -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

View File

@ -16,6 +16,8 @@
"use strict";
Error.stackTraceLimit = 250;
window.addEventListener("message", event => {
let request = event.data;
switch (request.type) {

View File

@ -33,7 +33,7 @@ public final class Deobfuscator {
var frames = new JSArray<Frame>();
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;

View File

@ -24,6 +24,7 @@
<script type="text/javascript" src="${SCRIPT}-runtime.js"></script>
<script type="text/javascript">
let instance;
Error.stackTraceLimit = 250;
TeaVM.wasmGC.load("${SCRIPT}", {
stackDeobfuscator: {
enabled: true
@ -31,7 +32,7 @@
installImports(o) {
o.teavmTest = {
success() {
var pre = document.createElement("pre");
let pre = document.createElement("pre");
document.body.appendChild(pre);
pre.appendChild(document.createTextNode("OK"));
},