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); var instructionLoc = sequence.unpack().find(address);
if (instructionLoc == null) { if (instructionLoc == null) {
return null; return returnForSequence(sequence);
} }
var location = instructionLoc.location(); var location = instructionLoc.location();
if (location == null) { if (location == null) {
return null; return returnForSequence(sequence);
} }
var result = new DeobfuscatedLocation[location.depth()]; var result = new DeobfuscatedLocation[location.depth()];
var method = sequence.method(); var method = sequence.method();
var i = 0; var i = result.length - 1;
while (true) { while (true) {
result[i++] = new DeobfuscatedLocation(location.file(), method, location.line()); result[i--] = new DeobfuscatedLocation(location.file(), method, location.line());
if (i >= result.length) { if (i < 0) {
break; break;
} }
method = location.inlining().method(); method = location.inlining().method();
@ -73,6 +73,12 @@ public class LineInfo {
return result; return result;
} }
private DeobfuscatedLocation[] returnForSequence(LineInfoSequence sequence) {
return new DeobfuscatedLocation[] {
new DeobfuscatedLocation(null, sequence.method(), -1)
};
}
public LineInfoSequence find(int address) { public LineInfoSequence find(int address) {
var index = CollectionUtil.binarySearch(sequenceList, address, LineInfoSequence::endAddress); var index = CollectionUtil.binarySearch(sequenceList, address, LineInfoSequence::endAddress);
if (index < 0) { if (index < 0) {

View File

@ -364,6 +364,7 @@ public class WasmBinaryRenderer {
dwarfSubprogram.function = function; dwarfSubprogram.function = function;
} }
if (debugLines != null && function.getJavaMethod() != null) { if (debugLines != null && function.getJavaMethod() != null) {
debugLines.advance(offset + sectionOffset);
debugLines.start(function.getJavaMethod()); debugLines.start(function.getJavaMethod());
} }
@ -398,6 +399,7 @@ public class WasmBinaryRenderer {
for (var part : function.getBody()) { for (var part : function.getBody()) {
visitor.preprocess(part); visitor.preprocess(part);
} }
visitor.setPositionToEmit(code.getPosition());
for (var part : function.getBody()) { for (var part : function.getBody()) {
part.acceptVisitor(visitor); part.acceptVisitor(visitor);
} }

View File

@ -118,6 +118,10 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
this.debugLines = debugLines; this.debugLines = debugLines;
} }
public void setPositionToEmit(int positionToEmit) {
this.positionToEmit = positionToEmit;
}
void preprocess(WasmExpression expression) { void preprocess(WasmExpression expression) {
expression.acceptVisitor(new WasmDefaultExpressionVisitor() { expression.acceptVisitor(new WasmDefaultExpressionVisitor() {
@Override @Override

View File

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

View File

@ -33,7 +33,7 @@ public final class Deobfuscator {
var frames = new JSArray<Frame>(); var frames = new JSArray<Frame>();
for (var location : locations) { for (var location : locations) {
var frame = new Frame(location.method.cls().fullName(), location.method.name(), 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); frames.push(frame);
} }
return frames; return frames;

View File

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