Skip to content

Commit

Permalink
log stacktraces using logger
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicPlayerA10 committed Sep 10, 2024
1 parent b034ab8 commit 2194a36
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.stream.Stream;

import dev.xdark.ssvm.VirtualMachine;
import dev.xdark.ssvm.execution.VMException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import uwu.narumi.deobfuscator.api.asm.ClassWrapper;
Expand Down Expand Up @@ -36,15 +37,14 @@ public Context(DeobfuscatorOptions options, LibraryClassLoader libraryLoader) {
public SandBox getSandBox() {
if (this.sandBox == null) {
// Lazily load sandbox
VirtualMachine vm = options.virtualMachine() == null ? new VirtualMachine() : options.virtualMachine();
try {
this.sandBox = new SandBox(
this.libraryLoader,
options.virtualMachine() == null ? new VirtualMachine() : options.virtualMachine()
);
} catch (Throwable t) {
LOGGER.error("SSVM bootstrap failed");
LOGGER.debug("Error", t);
if (options.consoleDebug()) t.printStackTrace();
this.sandBox = new SandBox(this.libraryLoader, vm);
} catch (VMException ex) {
LOGGER.error("SSVM bootstrap failed. Make sure that you run this deobfuscator on java 17");
SandBox.logVMException(ex, vm);

throw new RuntimeException(ex);
}
}
return this.sandBox;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public record DeobfuscatorOptions(
int classReaderFlags,
int classWriterFlags,

boolean consoleDebug,
boolean suppressErrors,
boolean printStacktraces,
boolean continueOnError,
boolean verifyBytecode,

VirtualMachine virtualMachine
Expand Down Expand Up @@ -66,8 +66,8 @@ public static class Builder {
private int classReaderFlags = ClassReader.SKIP_FRAMES;
private int classWriterFlags = ClassWriter.COMPUTE_FRAMES;

private boolean consoleDebug = false;
private boolean suppressErrors = false;
private boolean printStacktraces = true;
private boolean continueOnError = false;
private boolean verifyBytecode = false;

private VirtualMachine virtualMachine = null;
Expand Down Expand Up @@ -176,20 +176,20 @@ public DeobfuscatorOptions.Builder classWriterFlags(int classWriterFlags) {
}

/**
* Enables stacktraces logging
* Disables stacktraces logging
*/
@Contract(" -> this")
public DeobfuscatorOptions.Builder consoleDebug() {
this.consoleDebug = true;
public DeobfuscatorOptions.Builder noStacktraces() {
this.printStacktraces = false;
return this;
}

/**
* Continue deobfuscation even if errors occur
*/
@Contract(" -> this")
public DeobfuscatorOptions.Builder suppressErrors() {
this.suppressErrors = true;
public DeobfuscatorOptions.Builder continueOnError() {
this.continueOnError = true;
return this;
}

Expand Down Expand Up @@ -238,8 +238,8 @@ public DeobfuscatorOptions build() {
classReaderFlags,
classWriterFlags,
// Other config
consoleDebug,
suppressErrors,
printStacktraces,
continueOnError,
verifyBytecode,

virtualMachine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
import org.apache.logging.log4j.Logger;
import uwu.narumi.deobfuscator.api.library.LibraryClassLoader;

/**
* A wrapper for {@link VirtualMachine}
*/
public class SandBox {

private static final Logger LOGGER = LogManager.getLogger(SandBox.class);

private final LibraryClassLoader loader;

private final VirtualMachine virtualMachine;
private final MemoryManager memoryManager;
private final SupplyingClassLoaderInstaller.Helper helper;
Expand All @@ -41,7 +42,6 @@ public SandBox(LibraryClassLoader loader) {
}

public SandBox(LibraryClassLoader loader, VirtualMachine virtualMachine) {
this.loader = loader;
this.virtualMachine = virtualMachine;

try {
Expand Down Expand Up @@ -85,19 +85,25 @@ public static String toString(Throwable t) {
return stringWriter.toString();
}

/**
* @see SandBox#logVMException(VMException, VirtualMachine)
*/
public void logVMException(VMException ex) {
logVMException(ex, this.virtualMachine);
}

/**
* Converts {@link VMException} into readable java exception
*/
public void handleVMException(VMException ex) {
public static void logVMException(VMException ex, VirtualMachine vm) {
InstanceValue oop = ex.getOop();
if (oop.getJavaClass() == virtualMachine.getSymbols().java_lang_ExceptionInInitializerError()) {
oop = (InstanceValue) virtualMachine.getOperations().getReference(oop, "exception", "Ljava/lang/Throwable;");
if (oop.getJavaClass() == vm.getSymbols().java_lang_ExceptionInInitializerError()) {
oop = (InstanceValue) vm.getOperations().getReference(oop, "exception", "Ljava/lang/Throwable;");
}

// Print pretty exception
LOGGER.error(oop);
LOGGER.error(virtualMachine.getOperations().toJavaException(oop));
throw ex;
LOGGER.error(vm.getOperations().toJavaException(oop));
}

public VirtualMachine getVirtualMachine() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private static boolean transform(
return false;
} catch (Exception e) {
LOGGER.error("Error occurred when transforming {}", transformer.name(), e);
if (!context.getOptions().suppressErrors()) {
if (!context.getOptions().continueOnError()) {
throw new RuntimeException(e);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import dev.xdark.ssvm.VirtualMachine;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import uwu.narumi.deobfuscator.api.asm.ClassWrapper;
import uwu.narumi.deobfuscator.api.context.Context;
import uwu.narumi.deobfuscator.api.context.DeobfuscatorOptions;
import uwu.narumi.deobfuscator.api.execution.SandBox;
import uwu.narumi.deobfuscator.api.helper.ClassHelper;
import uwu.narumi.deobfuscator.api.helper.FileHelper;
import uwu.narumi.deobfuscator.api.library.Library;
Expand Down Expand Up @@ -117,10 +115,9 @@ private void loadClass(String path, byte[] bytes) {
}
} catch (Exception e) {
LOGGER.error("Could not load class: {}, adding as file", path);
LOGGER.debug("Error", e);
if (this.options.printStacktraces()) LOGGER.error(e);

context.getFiles().putIfAbsent(path, bytes);
if (this.options.consoleDebug()) e.printStackTrace();
}
}

Expand Down Expand Up @@ -191,8 +188,7 @@ private void saveToJar() {
zipOutputStream.write(data);
} catch (Exception e) {
LOGGER.error("Could not save class, saving original class instead of deobfuscated: {}", classWrapper.name());
LOGGER.debug("Error", e);
if (this.options.consoleDebug()) e.printStackTrace();
if (this.options.printStacktraces()) LOGGER.error(e);

try {
// Save original class as a fallback
Expand All @@ -202,9 +198,7 @@ private void saveToJar() {
zipOutputStream.write(data);
} catch (Exception e2) {
LOGGER.error("Could not save original class: {}", classWrapper.name());
LOGGER.debug("Error", e2);

if (this.options.consoleDebug()) e2.printStackTrace();
if (this.options.printStacktraces()) LOGGER.error(e2);
}
}

Expand All @@ -221,9 +215,7 @@ private void saveToJar() {
zipOutputStream.write(data);
} catch (Exception e) {
LOGGER.error("Could not save file: {}", name);
LOGGER.debug("Error", e);

if (this.options.consoleDebug()) e.printStackTrace();
if (this.options.printStacktraces()) LOGGER.error(e);
}

context.getFiles().remove(name);
Expand Down
3 changes: 1 addition & 2 deletions deobfuscator-impl/src/test/java/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public static void main(String[] args) {
// Pick your transformers here
() -> new ComposedGeneralFlowTransformer()
)
.consoleDebug()
.suppressErrors()
.continueOnError()
.classReaderFlags(ClassReader.SKIP_FRAMES)
.classWriterFlags(ClassWriter.COMPUTE_FRAMES)
.build()
Expand Down

0 comments on commit 2194a36

Please sign in to comment.