Skip to content

Commit

Permalink
js: fix deobfuscator, use new ES2015 module builder
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Mar 13, 2024
1 parent 055d5df commit 32ae1ab
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 70 deletions.
11 changes: 11 additions & 0 deletions jso/impl/src/main/java/org/teavm/jso/impl/JS.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ public static <T extends JSObject> JSArray<T> wrap(T[] array) {
return result;
}

public static <T> JSArray<T> wrap(T[] array) {
if (array == null) {
return null;
}
var result = new JSArray<T>(array.length);
for (int i = 0; i < array.length; ++i) {
result.set(i, array[i]);
}
return result;
}

public static <T extends JSObject> WrapFunction<T[], JSArray<T>> arrayWrapper() {
return JS::wrap;
}
Expand Down
1 change: 1 addition & 0 deletions jso/impl/src/main/java/org/teavm/jso/impl/JSMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ final class JSMethods {
String.class, JSObject.class);

public static final ValueType JS_OBJECT = ValueType.object(JSObject.class.getName());
public static final ValueType OBJECT = ValueType.object("java.lang.Object");
public static final ValueType JS_ARRAY = ValueType.object(JSArray.class.getName());
private static final MethodReference[] INVOKE_METHODS = new MethodReference[13];
private static final MethodReference[] CONSTRUCT_METHODS = new MethodReference[13];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ private ValueType getWrappedType(ValueType type) {
} else if (type instanceof ValueType.Object) {
if (type.isObject(String.class)) {
return type;
} else {
} else if (typeHelper.isJavaScriptClass(((ValueType.Object) type).getClassName())) {
return JSMethods.JS_OBJECT;
} else {
return JSMethods.OBJECT;
}
} else {
return type;
Expand Down
11 changes: 5 additions & 6 deletions tools/browser-runner/src/main/resources/test-server/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

let logging = false;
let deobfuscation = false;
if (typeof deobfuscator !== 'undefined') {
deobfuscator();
}

function tryConnect() {
let ws = new WebSocket("ws://localhost:{{PORT}}/ws");
Expand Down Expand Up @@ -82,7 +79,7 @@ function runSingleTest(test, callback) {
console.log("Running test " + test.name);
}
if (deobfuscation) {
const fileName = test.file + ".teavmdbg";
const fileName = test.file.path + ".teavmdbg";
if (lastDeobfuscatorFile === fileName) {
if (lastDeobfuscatorPromise === null) {
runSingleTestWithDeobfuscator(test, lastDeobfuscator, callback);
Expand All @@ -100,7 +97,7 @@ function runSingleTest(test, callback) {
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
const newDeobfuscator = xhr.status === 200
? deobfuscator.create(xhr.response, "http://localhost:{{PORT}}/" + test.file)
? createDeobfuscator(xhr.response, "http://localhost:{{PORT}}/" + test.file.path)
: null;
if (lastDeobfuscatorFile === fileName) {
lastDeobfuscator = newDeobfuscator;
Expand Down Expand Up @@ -139,7 +136,9 @@ function runSingleTestWithDeobfuscator(test, deobfuscator, callback) {
};
window.addEventListener("message", listener);

iframe.contentWindow.$rt_decodeStack = deobfuscator;
iframe.contentWindow.$rt_decodeStack = deobfuscator != null
? deobfuscator.deobfuscate.bind(deobfuscator)
: null;
iframe.contentWindow.postMessage(test, "*");
};
window.addEventListener("message", handshakeListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
<html>
<head>
<meta charset="utf-8">
<script src="deobfuscator.js"></script>
<script type="module">
import { create } from './deobfuscator.js'
window.createDeobfuscator = create;
</script>
<script src="client.js"></script>
</head>
<body>
Expand Down
4 changes: 3 additions & 1 deletion tools/deobfuscator-js/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ val generateJs by tasks.register<JavaExec>("generateJs") {
"org.teavm.tooling.deobfuscate.js.Deobfuscator",
"\$teavm_deobfuscator",
layout.buildDirectory.dir("teavm").get().asFile.absolutePath,
"deobfuscator.js"
"deobfuscator.js",
"none"
)
}

Expand All @@ -59,6 +60,7 @@ val generateLibJs by tasks.register<JavaExec>("generateLibJs") {
"deobfuscator",
layout.buildDirectory.dir("teavm-lib").get().asFile.absolutePath,
"deobfuscator-lib.js",
"es2015"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.teavm.tooling.deobfuscate.js;

import java.io.File;
import org.teavm.backend.javascript.JSModuleType;
import org.teavm.tooling.ConsoleTeaVMToolLog;
import org.teavm.tooling.TeaVMProblemRenderer;
import org.teavm.tooling.TeaVMTargetType;
Expand All @@ -35,6 +36,7 @@ public static void main(String[] args) throws TeaVMToolException {
tool.setEntryPointName(args[1]);
tool.setTargetDirectory(new File(args[2]));
tool.setTargetFileName(args[3]);
tool.setJsModuleType(JSModuleType.valueOf(args[4].toUpperCase()));
tool.setObfuscated(true);
tool.setOptimizationLevel(TeaVMOptimizationLevel.ADVANCED);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.teavm.debugging.information.GeneratedLocation;
import org.teavm.debugging.information.SourceLocation;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSExport;
import org.teavm.jso.ajax.XMLHttpRequest;
import org.teavm.jso.core.JSArray;
import org.teavm.jso.core.JSObjects;
Expand All @@ -33,6 +35,7 @@
import org.teavm.jso.typedarrays.Int8Array;
import org.teavm.model.MethodReference;

@JSClass
public final class Deobfuscator {
private static final JSRegExp FRAME_PATTERN = new JSRegExp(""
+ "(^ +at ([^(]+) *\\((.+):([0-9]+):([0-9]+)\\) *$)|"
Expand Down Expand Up @@ -60,6 +63,7 @@ private static void loadDeobfuscator(String fileName, String classesFileName) {
xhr.send();
}

@JSExport
public Frame[] deobfuscate(String stack) {
List<Frame> frames = new ArrayList<>();
for (String line : splitLines(stack)) {
Expand Down Expand Up @@ -125,14 +129,16 @@ private static List<Frame> deobfuscateFrames(DebugInformation debugInformation,
decodedFileName = decodedFileName.substring(decodedFileName.lastIndexOf('/') + 1);
}

Frame frame = createEmptyFrame();
frame.setClassName(method.getClassName());
frame.setMethodName(method.getName());
frame.setFileName(decodedFileName);
var javaLineNumber = -1;
if (location != null) {
frame.setLineNumber(location.getLine());
javaLineNumber = location.getLine();
}
result.add(frame);
result.add(new Frame(
method.getClassName(),
method.getName(),
decodedFileName,
javaLineNumber
));
}

if (result.isEmpty()) {
Expand All @@ -143,12 +149,12 @@ private static List<Frame> deobfuscateFrames(DebugInformation debugInformation,
}

private static Frame createDefaultFrame(String fileName, String functionName, int lineNumber) {
Frame frame = createEmptyFrame();
frame.setFileName(fileName);
frame.setMethodName(functionName != null ? functionName : "<unknown function>");
frame.setClassName("<JS>");
frame.setLineNumber(lineNumber);
return frame;
return new Frame(
"<JS>",
functionName != null ? functionName : "<unknown function>",
fileName,
lineNumber
);
}

private static String[] splitLines(String text) {
Expand All @@ -165,9 +171,6 @@ private static String[] splitLines(String text) {
return result.toArray(new String[0]);
}

@JSBody(script = "return {};")
private static native Frame createEmptyFrame();

@JSBody(params = "f", script = "window.$rt_decodeStack = f;")
private static native void setDeobfuscateFunction(DeobfuscateFunction f);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,20 @@
package org.teavm.tooling.deobfuscate.js;

import java.io.IOException;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSExport;
import org.teavm.jso.typedarrays.ArrayBuffer;

public final class DeobfuscatorLib implements DeobfuscatorJs {
public final class DeobfuscatorLib {
private DeobfuscatorLib() {
}

@Override
public DeobfuscateFunction create(ArrayBuffer buffer, String classesFileName) {
@JSExport
public static Deobfuscator create(ArrayBuffer buffer, String classesFileName) {
try {
return new Deobfuscator(buffer, classesFileName)::deobfuscate;
return new Deobfuscator(buffer, classesFileName);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
install(new DeobfuscatorLib());
}

@JSBody(params = "instance", script =
"deobfuscator.create = function(buffer, classesFileName) {"
+ "return instance.create(buffer, classesFileName);"
+ "}"
)
private static native void install(DeobfuscatorJs js);
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,43 @@
*/
package org.teavm.tooling.deobfuscate.js;

import org.teavm.jso.JSObject;
import org.teavm.jso.JSExport;
import org.teavm.jso.JSProperty;

public interface Frame extends JSObject {
public class Frame {
private String className;
private String fileName;
private String methodName;
private int lineNumber;

public Frame(String className, String methodName, String fileName, int lineNumber) {
this.className = className;
this.methodName = methodName;
this.fileName = fileName;
this.lineNumber = lineNumber;
}

@JSExport
@JSProperty
void setClassName(String className);
public String getClassName() {
return className;
}

@JSExport
@JSProperty
void setFileName(String fileName);
public String getFileName() {
return fileName;
}

@JSExport
@JSProperty
void setMethodName(String methodName);
public String getMethodName() {
return methodName;
}

@JSExport
@JSProperty
void setLineNumber(int lineNumber);
public int getLineNumber() {
return lineNumber;
}
}

0 comments on commit 32ae1ab

Please sign in to comment.