From 2194a360a66161671c37c2b186c80d4c96668747 Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Tue, 10 Sep 2024 21:43:55 +0200 Subject: [PATCH] log stacktraces using logger --- .../deobfuscator/api/context/Context.java | 16 +++++++------- .../api/context/DeobfuscatorOptions.java | 22 +++++++++---------- .../deobfuscator/api/execution/SandBox.java | 22 ++++++++++++------- .../api/transformer/Transformer.java | 2 +- .../uwu/narumi/deobfuscator/Deobfuscator.java | 16 ++++---------- .../src/test/java/Bootstrap.java | 3 +-- 6 files changed, 39 insertions(+), 42 deletions(-) diff --git a/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/context/Context.java b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/context/Context.java index 3dc5fe8..f47ba99 100644 --- a/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/context/Context.java +++ b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/context/Context.java @@ -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; @@ -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; diff --git a/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/context/DeobfuscatorOptions.java b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/context/DeobfuscatorOptions.java index 70a0fce..39400be 100644 --- a/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/context/DeobfuscatorOptions.java +++ b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/context/DeobfuscatorOptions.java @@ -30,8 +30,8 @@ public record DeobfuscatorOptions( int classReaderFlags, int classWriterFlags, - boolean consoleDebug, - boolean suppressErrors, + boolean printStacktraces, + boolean continueOnError, boolean verifyBytecode, VirtualMachine virtualMachine @@ -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; @@ -176,11 +176,11 @@ 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; } @@ -188,8 +188,8 @@ public DeobfuscatorOptions.Builder consoleDebug() { * Continue deobfuscation even if errors occur */ @Contract(" -> this") - public DeobfuscatorOptions.Builder suppressErrors() { - this.suppressErrors = true; + public DeobfuscatorOptions.Builder continueOnError() { + this.continueOnError = true; return this; } @@ -238,8 +238,8 @@ public DeobfuscatorOptions build() { classReaderFlags, classWriterFlags, // Other config - consoleDebug, - suppressErrors, + printStacktraces, + continueOnError, verifyBytecode, virtualMachine diff --git a/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/execution/SandBox.java b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/execution/SandBox.java index ead6e8b..4faa07f 100644 --- a/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/execution/SandBox.java +++ b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/execution/SandBox.java @@ -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; @@ -41,7 +42,6 @@ public SandBox(LibraryClassLoader loader) { } public SandBox(LibraryClassLoader loader, VirtualMachine virtualMachine) { - this.loader = loader; this.virtualMachine = virtualMachine; try { @@ -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() { diff --git a/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/transformer/Transformer.java b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/transformer/Transformer.java index 1d526fe..a80cbd0 100644 --- a/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/transformer/Transformer.java +++ b/deobfuscator-api/src/main/java/uwu/narumi/deobfuscator/api/transformer/Transformer.java @@ -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; diff --git a/deobfuscator-impl/src/main/java/uwu/narumi/deobfuscator/Deobfuscator.java b/deobfuscator-impl/src/main/java/uwu/narumi/deobfuscator/Deobfuscator.java index f9c644c..4a38fd5 100644 --- a/deobfuscator-impl/src/main/java/uwu/narumi/deobfuscator/Deobfuscator.java +++ b/deobfuscator-impl/src/main/java/uwu/narumi/deobfuscator/Deobfuscator.java @@ -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; @@ -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(); } } @@ -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 @@ -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); } } @@ -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); diff --git a/deobfuscator-impl/src/test/java/Bootstrap.java b/deobfuscator-impl/src/test/java/Bootstrap.java index b26d037..8ad2e9b 100644 --- a/deobfuscator-impl/src/test/java/Bootstrap.java +++ b/deobfuscator-impl/src/test/java/Bootstrap.java @@ -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()