Skip to content

Commit

Permalink
wasm gc: fix bugs in stack trace deobfuscator
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Oct 13, 2024
1 parent 4f9208c commit cfd381f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
16 changes: 11 additions & 5 deletions core/src/main/java/org/teavm/backend/wasm/debug/info/LineInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ public DeobfuscatedLocation[] deobfuscateSingle(int address) {
}
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();
Expand All @@ -73,6 +73,12 @@ public DeobfuscatedLocation[] deobfuscateSingle(int address) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ private byte[] renderFunction(WasmModule module, WasmFunction function, int offs
dwarfSubprogram.function = function;
}
if (debugLines != null && function.getJavaMethod() != null) {
debugLines.advance(offset + sectionOffset);
debugLines.start(function.getJavaMethod());
}

Expand Down Expand Up @@ -398,6 +399,7 @@ private byte[] renderFunction(WasmModule module, WasmFunction function, int offs
for (var part : function.getBody()) {
visitor.preprocess(part);
}
visitor.setPositionToEmit(code.getPosition());
for (var part : function.getBody()) {
part.acceptVisitor(visitor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tools/browser-runner/src/main/resources/test-server/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

"use strict";

Error.stackTraceLimit = 250;

window.addEventListener("message", event => {
let request = event.data;
switch (request.type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public JSArrayReader<Frame> deobfuscate(int[] addresses) {
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;
Expand Down
3 changes: 2 additions & 1 deletion tools/junit/src/main/resources/teavm-run-test-wasm-gc.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
<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
},
installImports(o) {
o.teavmTest = {
success() {
var pre = document.createElement("pre");
let pre = document.createElement("pre");
document.body.appendChild(pre);
pre.appendChild(document.createTextNode("OK"));
},
Expand Down

0 comments on commit cfd381f

Please sign in to comment.