From 0d373e9ee2cb240e46f3358b0d0ffd445be65657 Mon Sep 17 00:00:00 2001 From: FalsePattern Date: Mon, 27 May 2024 12:49:29 +0200 Subject: [PATCH] Turbo ASM --- README.MD | 1 + .../java/com/falsepattern/lib/StableAPI.java | 5 + .../lib/asm/IClassNodeTransformer.java | 7 + .../lib/asm/SmartTransformer.java | 6 +- .../lib/internal/FalsePatternLib.java | 6 +- .../lib/internal/asm/CoreLoadingPlugin.java | 53 ++- .../lib/internal/asm/FPTransformer.java | 30 +- .../transformers/ConfigOrderTransformer.java | 25 +- .../GasStationValidatorTransformer.java | 48 -- ...ormer.java => MixinPluginTransformer.java} | 48 +- ... => TypeDiscovererModuleInfoSilencer.java} | 25 +- .../lib/turboasm/ClassHeaderMetadata.java | 410 ++++++++++++++++++ .../lib/turboasm/ClassNodeHandle.java | 181 ++++++++ .../lib/turboasm/FastClassAccessor.java | 186 ++++++++ .../turboasm/MergeableTurboTransformer.java | 68 +++ .../lib/turboasm/TransformerUtil.java | 53 +++ .../lib/turboasm/TurboClassTransformer.java | 64 +++ 17 files changed, 1093 insertions(+), 123 deletions(-) delete mode 100644 src/main/java/com/falsepattern/lib/internal/asm/transformers/GasStationValidatorTransformer.java rename src/main/java/com/falsepattern/lib/internal/asm/transformers/{IMixinPluginTransformer.java => MixinPluginTransformer.java} (77%) rename src/main/java/com/falsepattern/lib/internal/asm/transformers/{ITypeDiscovererTransformer.java => TypeDiscovererModuleInfoSilencer.java} (72%) create mode 100644 src/main/java/com/falsepattern/lib/turboasm/ClassHeaderMetadata.java create mode 100644 src/main/java/com/falsepattern/lib/turboasm/ClassNodeHandle.java create mode 100644 src/main/java/com/falsepattern/lib/turboasm/FastClassAccessor.java create mode 100644 src/main/java/com/falsepattern/lib/turboasm/MergeableTurboTransformer.java create mode 100644 src/main/java/com/falsepattern/lib/turboasm/TransformerUtil.java create mode 100644 src/main/java/com/falsepattern/lib/turboasm/TurboClassTransformer.java diff --git a/README.MD b/README.MD index 79cc27f..be66d35 100644 --- a/README.MD +++ b/README.MD @@ -12,6 +12,7 @@ A library for 1.7.10 with lots of useful stuff, licensed under the LGPLv3 licens | [optifine](src/main/java/com/falsepattern/lib/optifine) | Tools for messing with OptiFine | | [text](src/main/java/com/falsepattern/lib/text) | Better Chat and GUI text processing | | [toasts](src/main/java/com/falsepattern/lib/toasts) | The toast system from newer versions, with some extras | +| [turboasm](src/main/java/com/falsepattern/lib/turboasm) | A forge-style class transformer port of RFB's transformer API. | | [util](src/main/java/com/falsepattern/lib/util) | Additional utilities that do not fit the above categories, see below for more information | The contents of the [util](src/main/java/com/falsepattern/lib/util) package so far: diff --git a/src/main/java/com/falsepattern/lib/StableAPI.java b/src/main/java/com/falsepattern/lib/StableAPI.java index c2ba7b6..a3a0efd 100644 --- a/src/main/java/com/falsepattern/lib/StableAPI.java +++ b/src/main/java/com/falsepattern/lib/StableAPI.java @@ -48,6 +48,11 @@ * You may set the {@link #since()} attribute to "__INTERNAL__", this will signal that even though the specific class/member * has been marked as stable, it is still for internal use only. This may be done for reference purposes in multi-developer * projects, where you need to communicate intent even in internal code. + *

+ * Similarly, you may also set the {@link #since()} attribute to "__EXPERIMENTAL__", this will signal that the given API + * is highly experimental and may be removed or change at any time, however, using it in third party projects is not an + * error, unlike __INTERNAL__, but if any issues arise from the usage of the API or the API changing, the API developer + * shall not be blamed for it. */ @Documented @Retention(RetentionPolicy.RUNTIME) diff --git a/src/main/java/com/falsepattern/lib/asm/IClassNodeTransformer.java b/src/main/java/com/falsepattern/lib/asm/IClassNodeTransformer.java index fc88a02..bb373c7 100644 --- a/src/main/java/com/falsepattern/lib/asm/IClassNodeTransformer.java +++ b/src/main/java/com/falsepattern/lib/asm/IClassNodeTransformer.java @@ -22,10 +22,17 @@ */ package com.falsepattern.lib.asm; +import com.falsepattern.lib.DeprecationDetails; import com.falsepattern.lib.StableAPI; import org.objectweb.asm.tree.ClassNode; +/** + * See: {@link com.falsepattern.lib.turboasm.TurboClassTransformer}. + * This class will not be removed, for backwards compatibility reasons. + */ @StableAPI(since = "0.10.0") +@Deprecated +@DeprecationDetails(deprecatedSince = "1.2.0") public interface IClassNodeTransformer { @StableAPI.Expose String getName(); diff --git a/src/main/java/com/falsepattern/lib/asm/SmartTransformer.java b/src/main/java/com/falsepattern/lib/asm/SmartTransformer.java index 853ac18..6719377 100644 --- a/src/main/java/com/falsepattern/lib/asm/SmartTransformer.java +++ b/src/main/java/com/falsepattern/lib/asm/SmartTransformer.java @@ -22,6 +22,7 @@ */ package com.falsepattern.lib.asm; +import com.falsepattern.lib.DeprecationDetails; import com.falsepattern.lib.StableAPI; import com.falsepattern.lib.internal.asm.CoreLoadingPlugin; import lombok.val; @@ -36,9 +37,12 @@ import java.util.List; /** - * An ASM transformation dispatcher utility, inspired by mixins. + * See: {@link com.falsepattern.lib.turboasm.MergeableTurboTransformer}. + * This class will not be removed, for backwards compatibility reasons. */ @StableAPI(since = "0.10.0") +@Deprecated +@DeprecationDetails(deprecatedSince = "1.2.0") public interface SmartTransformer extends IClassTransformer { @StableAPI.Expose Logger logger(); diff --git a/src/main/java/com/falsepattern/lib/internal/FalsePatternLib.java b/src/main/java/com/falsepattern/lib/internal/FalsePatternLib.java index 71577ae..56a8cce 100644 --- a/src/main/java/com/falsepattern/lib/internal/FalsePatternLib.java +++ b/src/main/java/com/falsepattern/lib/internal/FalsePatternLib.java @@ -51,6 +51,9 @@ public class FalsePatternLib { @SidedProxy(clientSide = Tags.GROUPNAME + ".internal.proxy.ClientProxy", serverSide = Tags.GROUPNAME + ".internal.proxy.CommonProxy") private static CommonProxy proxy; + static { + CoreLoadingPlugin.mergeTurboTransformers(); + } public FalsePatternLib() { FPLog.LOG.info("Version " + Tags.VERSION + " initialized!"); @@ -74,9 +77,6 @@ public void init(FMLInitializationEvent e) { @Mod.EventHandler public void postInit(FMLPostInitializationEvent e) { - if (Loader.isModLoaded("gasstation")) { - CoreLoadingPlugin.validateGasStation(); - } proxy.postInit(e); } } diff --git a/src/main/java/com/falsepattern/lib/internal/asm/CoreLoadingPlugin.java b/src/main/java/com/falsepattern/lib/internal/asm/CoreLoadingPlugin.java index 90c21db..aa49c66 100644 --- a/src/main/java/com/falsepattern/lib/internal/asm/CoreLoadingPlugin.java +++ b/src/main/java/com/falsepattern/lib/internal/asm/CoreLoadingPlugin.java @@ -26,14 +26,24 @@ import com.falsepattern.lib.internal.Tags; import com.falsepattern.lib.internal.impl.dependencies.DependencyLoaderImpl; import com.falsepattern.lib.mapping.MappingManager; +import com.falsepattern.lib.turboasm.MergeableTurboTransformer; import lombok.Getter; +import lombok.SneakyThrows; import lombok.val; +import net.minecraft.launchwrapper.IClassTransformer; +import net.minecraft.launchwrapper.Launch; +import net.minecraft.launchwrapper.LaunchClassLoader; import cpw.mods.fml.relauncher.IFMLLoadingPlugin; import cpw.mods.fml.relauncher.IFMLLoadingPlugin.MCVersion; import cpw.mods.fml.relauncher.IFMLLoadingPlugin.Name; import cpw.mods.fml.relauncher.IFMLLoadingPlugin.SortingIndex; +import javax.swing.BoxLayout; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.SwingUtilities; +import java.util.List; import java.util.Map; import static com.falsepattern.lib.mixin.MixinInfo.isClassPresentSafe; @@ -44,7 +54,7 @@ @MCVersion("1.7.10") @Name(Tags.MODID) @SortingIndex(500) -@IFMLLoadingPlugin.TransformerExclusions({Tags.GROUPNAME + ".internal.asm", Tags.GROUPNAME + ".asm"}) +@IFMLLoadingPlugin.TransformerExclusions({Tags.GROUPNAME + ".internal.asm", Tags.GROUPNAME + ".asm", Tags.GROUPNAME + ".turboasm"}) public class CoreLoadingPlugin implements IFMLLoadingPlugin { @Getter private static boolean obfuscated; @@ -117,25 +127,6 @@ private static Error skillIssue(String message) { return skillIssue; } - public static void validateGasStation() { - FPLog.LOG.info("Got any gas?"); - //Make sure everything is loaded correctly, crash if gasstation is bugged - // @formatter:off - if (!isClassPresentSafe("com.falsepattern.gasstation.core.GasStationCore") //Validate core class - || !isClassPresentSafe("makamys.mixingasm.api.TransformerInclusions") //Validate the mixingasm compat - || !isClassPresentSafe("ru.timeconqueror.spongemixins.core.SpongeMixinsCore") //Validate the spongemixins compat - || !isClassPresentSafe("io.github.tox1cozz.mixinbooterlegacy.MixinBooterLegacyPlugin") //Validate the MBL compat - || !isClassPresentSafe("org.spongepowered.asm.lib.Opcodes") //Validate correct mixins class - || isClassPresentSafe("org.spongepowered.libraries.org.objectweb.asm.Opcodes") - ) { - FPLog.LOG.fatal("Somebody put diesel in my gas tank!"); - throw new Error("Failed to validate your GasStation mixin plugin installation. " - + "Please make sure you have the latest GasStation installed from the official source: " - + "https://github.com/FalsePattern/GasStation"); - } - // @formatter:on - } - @Override public String[] getASMTransformerClass() { return new String[]{Tags.GROUPNAME + ".internal.asm.FPTransformer"}; @@ -154,10 +145,32 @@ public String getSetupClass() { @Override public void injectData(Map data) { obfuscated = (Boolean) data.get("runtimeDeobfuscationEnabled"); + mergeTurboTransformers(); } @Override public String getAccessTransformerClass() { return null; } + + @SneakyThrows + public static synchronized void mergeTurboTransformers() { + val f = LaunchClassLoader.class.getDeclaredField("transformers"); + f.setAccessible(true); + + val transformers = (List) f.get(Launch.classLoader); + for (int i = 0; i < transformers.size() - 1; i++) { + val a = transformers.get(i); + if (!(a instanceof MergeableTurboTransformer)) + continue; + + val b = transformers.get(i + 1); + if (!(b instanceof MergeableTurboTransformer)) + continue; + + transformers.remove(i + 1); + transformers.set(i, MergeableTurboTransformer.merge((MergeableTurboTransformer) a, (MergeableTurboTransformer) b)); + i--; + } + } } diff --git a/src/main/java/com/falsepattern/lib/internal/asm/FPTransformer.java b/src/main/java/com/falsepattern/lib/internal/asm/FPTransformer.java index 1b8690f..5083918 100644 --- a/src/main/java/com/falsepattern/lib/internal/asm/FPTransformer.java +++ b/src/main/java/com/falsepattern/lib/internal/asm/FPTransformer.java @@ -24,41 +24,25 @@ package com.falsepattern.lib.internal.asm; import com.falsepattern.lib.StableAPI; -import com.falsepattern.lib.asm.IClassNodeTransformer; -import com.falsepattern.lib.asm.SmartTransformer; -import com.falsepattern.lib.internal.Tags; import com.falsepattern.lib.internal.asm.transformers.ConfigOrderTransformer; -import com.falsepattern.lib.internal.asm.transformers.GasStationValidatorTransformer; -import com.falsepattern.lib.internal.asm.transformers.IMixinPluginTransformer; -import com.falsepattern.lib.internal.asm.transformers.ITypeDiscovererTransformer; +import com.falsepattern.lib.internal.asm.transformers.MixinPluginTransformer; +import com.falsepattern.lib.internal.asm.transformers.TypeDiscovererModuleInfoSilencer; import com.falsepattern.lib.internal.impl.optifine.OptiFineTransformerHooksImpl; -import lombok.Getter; +import com.falsepattern.lib.turboasm.MergeableTurboTransformer; import lombok.experimental.Accessors; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import java.util.Arrays; -import java.util.List; @Accessors(fluent = true) @StableAPI(since = "__INTERNAL__") -public class FPTransformer implements SmartTransformer { - public static final Logger LOG = LogManager.getLogger(Tags.MODNAME + " ASM"); - - @Getter - private final List transformers; - - @Getter - private final Logger logger = LOG; - +public class FPTransformer extends MergeableTurboTransformer { static { OptiFineTransformerHooksImpl.init(); } public FPTransformer() { - transformers = Arrays.asList(new IMixinPluginTransformer(), - new ITypeDiscovererTransformer(), - new GasStationValidatorTransformer(), - new ConfigOrderTransformer()); + super(Arrays.asList(new MixinPluginTransformer(), + new TypeDiscovererModuleInfoSilencer(), + new ConfigOrderTransformer())); } } diff --git a/src/main/java/com/falsepattern/lib/internal/asm/transformers/ConfigOrderTransformer.java b/src/main/java/com/falsepattern/lib/internal/asm/transformers/ConfigOrderTransformer.java index 7ad0a8b..068a851 100644 --- a/src/main/java/com/falsepattern/lib/internal/asm/transformers/ConfigOrderTransformer.java +++ b/src/main/java/com/falsepattern/lib/internal/asm/transformers/ConfigOrderTransformer.java @@ -23,26 +23,36 @@ package com.falsepattern.lib.internal.asm.transformers; -import com.falsepattern.lib.asm.IClassNodeTransformer; import com.falsepattern.lib.config.Config; +import com.falsepattern.lib.internal.Tags; import com.falsepattern.lib.internal.impl.config.DeclOrderInternal; +import com.falsepattern.lib.turboasm.ClassNodeHandle; +import com.falsepattern.lib.turboasm.TurboClassTransformer; import lombok.val; +import org.jetbrains.annotations.NotNull; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; -import org.objectweb.asm.tree.ClassNode; -public class ConfigOrderTransformer implements IClassNodeTransformer { +public class ConfigOrderTransformer implements TurboClassTransformer { private static final String DESC_CONFIG = Type.getDescriptor(Config.class); private static final String DESC_CONFIG_IGNORE = Type.getDescriptor(Config.Ignore.class); private static final String DESC_ORDER = Type.getDescriptor(DeclOrderInternal.class); @Override - public String getName() { + public String name() { return "ConfigOrderTransformer"; } @Override - public boolean shouldTransform(ClassNode cn, String transformedName, boolean obfuscated) { + public String owner() { + return Tags.MODNAME; + } + + @Override + public boolean shouldTransformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) { + val cn = classNode.getNode(); + if (cn == null) + return false; if (cn.visibleAnnotations != null) { for (val ann : cn.visibleAnnotations) { if (DESC_CONFIG.equals(ann.desc)) { @@ -54,7 +64,10 @@ public boolean shouldTransform(ClassNode cn, String transformedName, boolean obf } @Override - public void transform(ClassNode cn, String transformedName, boolean obfuscated) { + public void transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) { + val cn = classNode.getNode(); + if (cn == null) + return; int order = 0; outer: for (val field : cn.fields) { diff --git a/src/main/java/com/falsepattern/lib/internal/asm/transformers/GasStationValidatorTransformer.java b/src/main/java/com/falsepattern/lib/internal/asm/transformers/GasStationValidatorTransformer.java deleted file mode 100644 index 51622de..0000000 --- a/src/main/java/com/falsepattern/lib/internal/asm/transformers/GasStationValidatorTransformer.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * This file is part of FalsePatternLib. - * - * Copyright (C) 2022-2024 FalsePattern - * All Rights Reserved - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * FalsePatternLib is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * FalsePatternLib is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with FalsePatternLib. If not, see . - */ - -package com.falsepattern.lib.internal.asm.transformers; - -import com.falsepattern.lib.asm.IClassNodeTransformer; -import com.falsepattern.lib.internal.asm.CoreLoadingPlugin; -import org.objectweb.asm.tree.ClassNode; - -public class GasStationValidatorTransformer implements IClassNodeTransformer { - @Override - public String getName() { - return "GasStationValidatorTransformer"; - } - - @Override - public boolean shouldTransform(ClassNode cn, String transformedName, boolean obfuscated) { - if (transformedName.equals("com.falsepattern.gasstation.GasStation")) { - CoreLoadingPlugin.validateGasStation(); - } - return false; - } - - @Override - public void transform(ClassNode cn, String transformedName, boolean obfuscated) { - - } -} diff --git a/src/main/java/com/falsepattern/lib/internal/asm/transformers/IMixinPluginTransformer.java b/src/main/java/com/falsepattern/lib/internal/asm/transformers/MixinPluginTransformer.java similarity index 77% rename from src/main/java/com/falsepattern/lib/internal/asm/transformers/IMixinPluginTransformer.java rename to src/main/java/com/falsepattern/lib/internal/asm/transformers/MixinPluginTransformer.java index 5cc7425..a6055b7 100644 --- a/src/main/java/com/falsepattern/lib/internal/asm/transformers/IMixinPluginTransformer.java +++ b/src/main/java/com/falsepattern/lib/internal/asm/transformers/MixinPluginTransformer.java @@ -23,17 +23,19 @@ package com.falsepattern.lib.internal.asm.transformers; -import com.falsepattern.lib.asm.IClassNodeTransformer; import com.falsepattern.lib.internal.Tags; import com.falsepattern.lib.internal.asm.FPTransformer; +import com.falsepattern.lib.turboasm.ClassNodeHandle; +import com.falsepattern.lib.turboasm.TurboClassTransformer; import lombok.val; +import org.jetbrains.annotations.NotNull; import org.objectweb.asm.tree.ClassNode; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import java.util.HashMap; import java.util.Map; -public class IMixinPluginTransformer implements IClassNodeTransformer { +public class MixinPluginTransformer implements TurboClassTransformer { private static final String IMIXINPLUGIN = Tags.GROUPNAME + ".mixin.IMixinPlugin"; private static final String IMIXINPLUGIN_INTERNAL = IMIXINPLUGIN.replace('.', '/'); private static final String IMIXINCONFIGPLUGIN = "org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin"; @@ -57,25 +59,42 @@ public class IMixinPluginTransformer implements IClassNodeTransformer { } @Override - public String getName() { - return "IMixinPluginTransformer"; + public String owner() { + return Tags.MODNAME; } @Override - public boolean shouldTransform(ClassNode cn, String transformedName, boolean obfuscated) { - return IMIXINPLUGIN.equals(transformedName) - || IMIXINCONFIGPLUGIN.equals(transformedName) - || cn.interfaces.stream() - .anyMatch((i) -> IMIXINPLUGIN_INTERNAL.equals(i) - || IMIXINCONFIGPLUGIN_INTERNAL.equals(i)); + public String name() { + return "MixinPluginTransformer"; } @Override - public void transform(ClassNode cn, String transformedName, boolean obfuscated) { - if (IMIXINCONFIGPLUGIN.equals(transformedName)) { + public boolean shouldTransformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) { + if (IMIXINPLUGIN.equals(className) || + IMIXINCONFIGPLUGIN.equals(className)) + return true; + if (classNode.isOriginal()) { + val meta = classNode.getOriginalMetadata(); + if (meta != null && meta.interfacesCount == 0) + return false; + } + val cn = classNode.getNode(); + if (cn == null) + return false; + return cn.interfaces.stream() + .anyMatch((i) -> IMIXINPLUGIN_INTERNAL.equals(i) || + IMIXINCONFIGPLUGIN_INTERNAL.equals(i)); + } + + @Override + public void transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) { + val cn = classNode.getNode(); + if (cn == null) + return; + if (IMIXINCONFIGPLUGIN.equals(className)) { extractMixinConfigPluginData(cn); } else { - transformPlugin(cn, transformedName); + transformPlugin(cn); } } @@ -101,8 +120,7 @@ private static void extractMixinConfigPluginData(ClassNode cn) { } } - private static void transformPlugin(ClassNode cn, String transformedName) { - FPTransformer.LOG.info("Transforming " + transformedName + " to fit current mixin environment."); + private static void transformPlugin(ClassNode cn) { if (PREAPPLY_DESC == null) { PREAPPLY_DESC = extractMethodWithReflection("preApply"); } diff --git a/src/main/java/com/falsepattern/lib/internal/asm/transformers/ITypeDiscovererTransformer.java b/src/main/java/com/falsepattern/lib/internal/asm/transformers/TypeDiscovererModuleInfoSilencer.java similarity index 72% rename from src/main/java/com/falsepattern/lib/internal/asm/transformers/ITypeDiscovererTransformer.java rename to src/main/java/com/falsepattern/lib/internal/asm/transformers/TypeDiscovererModuleInfoSilencer.java index f2b04af..21d0482 100644 --- a/src/main/java/com/falsepattern/lib/internal/asm/transformers/ITypeDiscovererTransformer.java +++ b/src/main/java/com/falsepattern/lib/internal/asm/transformers/TypeDiscovererModuleInfoSilencer.java @@ -23,24 +23,35 @@ package com.falsepattern.lib.internal.asm.transformers; -import com.falsepattern.lib.asm.IClassNodeTransformer; +import com.falsepattern.lib.turboasm.ClassNodeHandle; +import com.falsepattern.lib.turboasm.TurboClassTransformer; import lombok.val; +import org.jetbrains.annotations.NotNull; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.LdcInsnNode; -public class ITypeDiscovererTransformer implements IClassNodeTransformer { +public class TypeDiscovererModuleInfoSilencer implements TurboClassTransformer { + + @Override + public String owner() { + return "FalsePatternLib"; + } + @Override - public String getName() { - return "ITypeDiscovererTransformer"; + public String name() { + return "TypeDiscovererModuleInfoSilencer"; } @Override - public boolean shouldTransform(ClassNode cn, String transformedName, boolean obfuscated) { - return "cpw.mods.fml.common.discovery.ITypeDiscoverer".equals(transformedName); + public boolean shouldTransformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) { + return "cpw.mods.fml.common.discovery.ITypeDiscoverer".equals(className); } @Override - public void transform(ClassNode cn, String transformedName, boolean obfuscated) { + public void transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) { + val cn = classNode.getNode(); + if (cn == null) + return; for (val method : cn.methods) { if (!method.name.equals("")) { continue; diff --git a/src/main/java/com/falsepattern/lib/turboasm/ClassHeaderMetadata.java b/src/main/java/com/falsepattern/lib/turboasm/ClassHeaderMetadata.java new file mode 100644 index 0000000..30a4a9a --- /dev/null +++ b/src/main/java/com/falsepattern/lib/turboasm/ClassHeaderMetadata.java @@ -0,0 +1,410 @@ +/* + * This file is part of FalsePatternLib. + * + * Copyright (C) 2022-2024 FalsePattern + * All Rights Reserved + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * FalsePatternLib is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FalsePatternLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with FalsePatternLib. If not, see . + */ + +package com.falsepattern.lib.turboasm; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; + +import com.falsepattern.lib.StableAPI; +import com.falsepattern.lib.StableAPI.Expose; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.objectweb.asm.Opcodes; + +/** + * Utilities for quickly processing class files without fully parsing them. + */ +@StableAPI(since = "__EXPERIMENTAL__") +public final class ClassHeaderMetadata implements FastClassAccessor { + + @Expose + public final int minorVersion; + @Expose + public final int majorVersion; + @Expose + public final int constantPoolEntryCount; + /** Byte offsets of where each constant pool entry starts (index of the tag byte), zero-indexed! */ + @Expose + public final int @NotNull [] constantPoolEntryOffsets; + /** Type of each parsed constant pool entry, zero-indexed! */ + @Expose + public final ConstantPoolEntryTypes @NotNull [] constantPoolEntryTypes; + + @Expose + public final int constantPoolEndOffset; + @Expose + public final int accessFlags; + @Expose + public final int thisClassIndex; + @Expose + public final int superClassIndex; + @Expose + public final int interfacesCount; + @Expose + public final @NotNull String binaryThisName; + @Expose + public final @Nullable String binarySuperName; + + /** + * Attempts to parse a class header. + * @param bytes The class bytes to parse. + */ + @Expose + public ClassHeaderMetadata(byte @NotNull [] bytes) { + if (!isValidClass(bytes)) { + throw new IllegalArgumentException("Invalid class detected"); + } + this.minorVersion = u16(bytes, Offsets.minorVersionU16); + this.majorVersion = u16(bytes, Offsets.majorVersionU16); + this.constantPoolEntryCount = u16(bytes, Offsets.constantPoolCountU16); + this.constantPoolEntryOffsets = new int[constantPoolEntryCount]; + this.constantPoolEntryTypes = new ConstantPoolEntryTypes[constantPoolEntryCount]; + // scan through CP entries + final int cpOff; + { + int off = Offsets.constantPoolStart; + for (int entry = 0; entry < constantPoolEntryCount - 1; entry++) { + constantPoolEntryOffsets[entry] = off; + ConstantPoolEntryTypes type = ConstantPoolEntryTypes.parse(bytes, off); + constantPoolEntryTypes[entry] = type; + if (type == ConstantPoolEntryTypes.Double || type == ConstantPoolEntryTypes.Long) { + // Longs and Doubles take up 2 constant pool indices + entry++; + constantPoolEntryOffsets[entry] = off; + constantPoolEntryTypes[entry] = type; + } + off += type.byteLength(bytes, off); + } + cpOff = off; + this.constantPoolEndOffset = cpOff; + } + this.accessFlags = u16(bytes, cpOff + Offsets.pastCpAccessFlagsU16); + this.thisClassIndex = u16(bytes, cpOff + Offsets.pastCpThisClassU16); + this.superClassIndex = u16(bytes, cpOff + Offsets.pastCpSuperClassU16); + this.interfacesCount = u16(bytes, cpOff + Offsets.pastCpInterfacesCountU16); + // Parse this&super names + if (constantPoolEntryTypes[thisClassIndex - 1] != ConstantPoolEntryTypes.Class) { + throw new IllegalArgumentException("This class index is not a class ref"); + } + final int thisNameIndex = u16(bytes, constantPoolEntryOffsets[thisClassIndex - 1] + 1); + if (constantPoolEntryTypes[thisNameIndex - 1] != ConstantPoolEntryTypes.Utf8) { + throw new IllegalArgumentException("This class index does not point to a UTF8 entry"); + } + this.binaryThisName = modifiedUtf8(bytes, constantPoolEntryOffsets[thisNameIndex - 1] + 1); + if (superClassIndex == 0) { + // Should only be true for this==java/lang/Object + this.binarySuperName = null; + } else { + final int superNameIndex = u16(bytes, constantPoolEntryOffsets[superClassIndex - 1] + 1); + if (constantPoolEntryTypes[superClassIndex - 1] != ConstantPoolEntryTypes.Class) { + throw new IllegalArgumentException("Super class index is not a class ref"); + } + if (constantPoolEntryTypes[superNameIndex - 1] != ConstantPoolEntryTypes.Utf8) { + throw new IllegalArgumentException("Super class index does not point to a UTF8 entry"); + } + this.binarySuperName = modifiedUtf8(bytes, constantPoolEntryOffsets[superNameIndex - 1] + 1); + } + } + + /** Helpers to read big-endian values from class files. */ + public static int u8(byte @NotNull [] arr, int off) { + return ((int) arr[off]) & 0xff; + } + /** Helpers to read big-endian values from class files. */ + public static int u16(byte @NotNull [] arr, int off) { + return (u8(arr, off) << 8) | (u8(arr, off + 1)); + } + + /** + * Reads "modified UTF8" (16-bit length + variable-length data) used by Java to encode String constants in class files. + * @param arr The byte array to read from. + * @param off Offset to the 16-bit length field. + * @return The decoded String. + */ + @Expose + public static @NotNull String modifiedUtf8(byte @NotNull [] arr, int off) { + try (ByteArrayInputStream bais = new ByteArrayInputStream(arr, off, arr.length - off); + DataInputStream dis = new DataInputStream(bais)) { + return dis.readUTF(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * Header offsets from JVMS for Java 21 + * ClassFile { + * u4 magic; + * u2 minor_version; + * u2 major_version; + * u2 constant_pool_count; + * cp_info constant_pool[constant_pool_count-1]; + * u2 access_flags; + * u2 this_class; + * u2 super_class; + * u2 interfaces_count; + * u2 interfaces[interfaces_count]; + * u2 fields_count; + * field_info fields[fields_count]; + * u2 methods_count; + * method_info methods[methods_count]; + * u2 attributes_count; + * attribute_info attributes[attributes_count]; + * } + */ + @StableAPI(since = "__EXPERIMENTAL__") + public static final class Offsets { + private Offsets() {} + /** The magic item supplies the magic number identifying the class file format; it has the value 0xCAFEBABE */ + @Expose + public static final int magicU32 = 0; + /** The values of the minor_version and major_version items are the minor and major version numbers of this class file. Together, a major and a minor version number determine the version of the class file format. If a class file has major version number M and minor version number m, we denote the version of its class file format as M.m */ + @Expose + public static final int minorVersionU16 = magicU32 + 4; + /** The values of the minor_version and major_version items are the minor and major version numbers of this class file. Together, a major and a minor version number determine the version of the class file format. If a class file has major version number M and minor version number m, we denote the version of its class file format as M.m */ + @Expose + public static final int majorVersionU16 = minorVersionU16 + 2; + /** The value of the constant_pool_count item is equal to the number of entries in the constant_pool table plus one. A constant_pool index is considered valid if it is greater than zero and less than constant_pool_count, with the exception for constants of type long and double noted in §4.4.5. */ + @Expose + public static final int constantPoolCountU16 = majorVersionU16 + 2; + /** + * The constant_pool is a table of structures (§4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures. The format of each constant_pool table entry is indicated by its first "tag" byte. + *

+ * The constant_pool table is indexed from 1 to constant_pool_count - 1. + */ + @Expose + public static final int constantPoolStart = constantPoolCountU16 + 2; + /** + * The value of the access_flags item is a mask of flags used to denote access permissions to and properties of this class or interface. The interpretation of each flag, when set, is specified in Table 4.1-B. + */ + @Expose + public static final int pastCpAccessFlagsU16 = 0; + /** + * The value of the this_class item must be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure (§4.4.1) representing the class or interface defined by this class file. + */ + @Expose + public static final int pastCpThisClassU16 = pastCpAccessFlagsU16 + 2; + /** + * For a class, the value of the super_class item either must be zero or must be a valid index into the constant_pool table. If the value of the super_class item is nonzero, the constant_pool entry at that index must be a CONSTANT_Class_info structure representing the direct superclass of the class defined by this class file. Neither the direct superclass nor any of its superclasses may have the ACC_FINAL flag set in the access_flags item of its ClassFile structure.

+ * If the value of the super_class item is zero, then this class file must represent the class Object, the only class or interface without a direct superclass.

+ * For an interface, the value of the super_class item must always be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object. + */ + @Expose + public static final int pastCpSuperClassU16 = pastCpThisClassU16 + 2; + /** The value of the interfaces_count item gives the number of direct superinterfaces of this class or interface type */ + @Expose + public static final int pastCpInterfacesCountU16 = pastCpSuperClassU16 + 2; + } + + @StableAPI(since = "__EXPERIMENTAL__") + public enum ConstantPoolEntryTypes { + @Expose + Utf8(1, 45, -1), + @Expose + Integer(3, 45, 4), + @Expose + Float(4, 45, 4), + @Expose + Long(5, 45, 8), + @Expose + Double(6, 45, 8), + @Expose + Class(7, 49, 2), + @Expose + String(8, 45, 2), + @Expose + FieldRef(9, 45, 4), + @Expose + MethodRef(10, 45, 4), + @Expose + InterfaceMethodRef(11, 45, 4), + @Expose + NameAndType(12, 45, 4), + @Expose + MethodHandle(15, 51, 3), + @Expose + MethodType(16, 51, 2), + @Expose + Dynamic(17, 55, 4), + @Expose + InvokeDynamic(18, 51, 4), + @Expose + Module(19, 53, 2), + @Expose + Package(20, 53, 2), + ; + private static final ConstantPoolEntryTypes[] lookup; + + static { + final ConstantPoolEntryTypes[] values = values(); + final int maxTag = values[values.length - 1].tag; + final ConstantPoolEntryTypes[] lut = new ConstantPoolEntryTypes[maxTag + 1]; + for (ConstantPoolEntryTypes entry : values) { + lut[entry.tag] = entry; + } + lookup = lut; + } + + @Expose + public final int tag, minMajorVersion; + /** Length in bytes of the entry (excluding the 1 byte tag), or -1 if length-encoded */ + @Expose + public final int maybeByteLength; + + ConstantPoolEntryTypes(int tag, int minMajorVersion, int maybeByteLength) { + this.tag = tag; + this.minMajorVersion = minMajorVersion; + this.maybeByteLength = maybeByteLength; + } + + /** + * @param classFile Class bytes to scan for variable length entries + * @param offset The offset where the entry starts + * @return The total length of the entry, including the tag byte + */ + @Expose + public int byteLength(final byte @NotNull [] classFile, final int offset) { + if (this == ConstantPoolEntryTypes.Utf8) { + return 3 + u16(classFile, offset + 1); + } + if (this.maybeByteLength < 0) { + throw new UnsupportedOperationException("Missing byte length implementation for tag " + this); + } + return 1 + maybeByteLength; + } + + @Expose + public static @NotNull ConstantPoolEntryTypes parse(final byte @NotNull [] classFile, final int offset) { + final int tag = u8(classFile, offset); + final ConstantPoolEntryTypes type = tag >= lookup.length ? null : lookup[tag]; + if (type == null) { + throw new UnsupportedOperationException("Unknown constant pool tag " + tag); + } + return type; + } + } + + /** + * Sanity-checks the validity of the class header. + * @param classBytes Class data + * @return If the class passes simple sanity checks. + */ + @Contract("null -> false") + @Expose + public static boolean isValidClass(byte @Nullable [] classBytes) { + if (classBytes == null) { + return false; + } + if (classBytes.length < Offsets.constantPoolStart + Offsets.pastCpSuperClassU16) { + return false; + } + final int magic = + (u8(classBytes, 0) << 24) | (u8(classBytes, 1) << 16) | (u8(classBytes, 2) << 8) | (u8(classBytes, 3)); + return magic == 0xCAFEBABE; + } + + /** + * @param classBytes Class data + * @return The major version number of the class file. See {@link org.objectweb.asm.Opcodes#V1_8} + */ + @Expose + public static int majorVersion(byte @NotNull [] classBytes) { + return u16(classBytes, Offsets.majorVersionU16); + } + + /** + * Searches for a sub"string" (byte array) in a longer byte array. Not efficient for long search strings. + * @param classBytes The long byte string to search in. + * @param substring The short substring to search for. + * @return If the substring was found somewhere in the long string. + */ + @Expose + public static boolean hasSubstring(final byte @Nullable [] classBytes, final byte @NotNull [] substring) { + if (classBytes == null) { + return false; + } + final int classLen = classBytes.length; + final int subLen = substring.length; + if (classLen < subLen) { + return false; + } + outer: + for (int startPos = 0; startPos + subLen - 1 < classLen; startPos++) { + for (int i = 0; i < subLen; i++) { + if (classBytes[startPos + i] != substring[i]) { + continue outer; + } + } + return true; + } + return false; + } + + @Override + public boolean isPublic() { + return (accessFlags & Opcodes.ACC_PUBLIC) != 0; + } + + @Override + public boolean isFinal() { + return (accessFlags & Opcodes.ACC_FINAL) != 0; + } + + @Override + public boolean isInterface() { + return (accessFlags & Opcodes.ACC_INTERFACE) != 0; + } + + @Override + public boolean isAbstract() { + return (accessFlags & Opcodes.ACC_ABSTRACT) != 0; + } + + @Override + public boolean isSynthetic() { + return (accessFlags & Opcodes.ACC_SYNTHETIC) != 0; + } + + @Override + public boolean isAnnotation() { + return (accessFlags & Opcodes.ACC_ANNOTATION) != 0; + } + + @Override + public boolean isEnum() { + return (accessFlags & Opcodes.ACC_ENUM) != 0; + } + + @Override + public @NotNull String binaryThisName() { + return binaryThisName; + } + + @Override + public @Nullable String binarySuperName() { + return binarySuperName; + } +} \ No newline at end of file diff --git a/src/main/java/com/falsepattern/lib/turboasm/ClassNodeHandle.java b/src/main/java/com/falsepattern/lib/turboasm/ClassNodeHandle.java new file mode 100644 index 0000000..2272d83 --- /dev/null +++ b/src/main/java/com/falsepattern/lib/turboasm/ClassNodeHandle.java @@ -0,0 +1,181 @@ +/* + * This file is part of FalsePatternLib. + * + * Copyright (C) 2022-2024 FalsePattern + * All Rights Reserved + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * FalsePatternLib is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FalsePatternLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with FalsePatternLib. If not, see . + */ + +package com.falsepattern.lib.turboasm; + +import com.falsepattern.lib.StableAPI; +import com.falsepattern.lib.StableAPI.Expose; +import org.intellij.lang.annotations.MagicConstant; +import org.jetbrains.annotations.Nullable; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.tree.ClassNode; + +/** A simple handle to a mutable ClassNode and flags for ClassWriter. */ +@StableAPI(since = "__EXPERIMENTAL__") +public final class ClassNodeHandle { + private final byte @Nullable [] originalBytes; + private final @Nullable ClassHeaderMetadata originalMetadata; + private final int readerOptions; + private boolean initialized = false; + private @Nullable ClassNode node = null; + private @Nullable FastClassAccessor accessor = null; + private int writerFlags = 0; + + /** Parse the class data with no reader options (for fastest speed). */ + @Expose + public ClassNodeHandle(byte @Nullable [] classData) { + this(classData, 0); + } + + /** Parse the class data with custom reader options. */ + @Expose + public ClassNodeHandle( + byte @Nullable [] classData, @MagicConstant(flagsFromClass = ClassReader.class) int readerOptions) { + @Nullable ClassHeaderMetadata originalMetadata; + this.originalBytes = classData; + if (classData == null) { + originalMetadata = null; + } else { + try { + originalMetadata = new ClassHeaderMetadata(classData); + } catch (Exception e) { + originalMetadata = null; + } + } + this.originalMetadata = originalMetadata; + this.accessor = originalMetadata; + this.readerOptions = 0; + } + + /** Gets the original pre-transformer-phase bytes of the class. */ + @Expose + public byte @Nullable [] getOriginalBytes() { + return originalBytes; + } + + /** Gets the original pre-transformer-phase header metadata of the class, or null if invalid/not present. */ + @Expose + public @Nullable ClassHeaderMetadata getOriginalMetadata() { + return originalMetadata; + } + + /** Gets the fast class metadata accessor of the class, that can access the current state of various class attributes without (re)parsing. */ + @Expose + public @Nullable FastClassAccessor getFastAccessor() { + return accessor; + } + + /** @return If the class was not yet turned into a ClassNode object, and the original bytes still represent the class. */ + @Expose + public boolean isOriginal() { + return !initialized; + } + + /** If the class currently has any bytes or a node associated with it. */ + @Expose + public boolean isPresent() { + if (initialized) { + return node != null; + } else { + return originalBytes != null; + } + } + + /** Gets the parsed node of the currently processed class. This can cause full class parsing! */ + @Expose + public @Nullable ClassNode getNode() { + ensureInitialized(); + return node; + } + + /** Overwrites the parsed node of the currently processed class. */ + @Expose + public void setNode(@Nullable ClassNode node) { + initialized = true; + this.node = node; + if (node == null) { + this.accessor = null; + } else { + this.accessor = FastClassAccessor.ofAsmNode(node); + } + } + + /** Computes the byte[] array of the transformed class. Returns the original bytes if {@link ClassNodeHandle#getNode()} was never called. */ + @Expose + public byte @Nullable [] computeBytes() { + if (!initialized) { + return originalBytes; + } + if (node == null) { + return null; + } + final ClassWriter writer = new ClassWriter(writerFlags); + node.accept(writer); + return writer.toByteArray(); + } + + /** Gets the ClassWriter flags for the current class. */ + @Expose + public int getWriterFlags() { + return writerFlags; + } + + /** Set the ClassWriter flags for the current class. */ + @Expose + public void setWriterFlags(@MagicConstant(flagsFromClass = ClassWriter.class) int flags) { + this.writerFlags = flags; + } + + /** Combine the currently set writer flags with the given flags using bitwise OR. */ + @Expose + public void orWriterFlags(@MagicConstant(flagsFromClass = ClassWriter.class) int flags) { + this.writerFlags |= flags; + } + + /** Set ClassWriter.COMPUTE_MAXS on the writer flags. */ + @Expose + public void computeMaxs() { + this.writerFlags |= ClassWriter.COMPUTE_MAXS; + } + + /** Set ClassWriter.COMPUTE_FRAMES on the writer flags. */ + @Expose + public void computeFrames() { + this.writerFlags |= ClassWriter.COMPUTE_FRAMES; + } + + private void ensureInitialized() { + if (!initialized) { + if (originalBytes == null) { + node = null; + accessor = null; + } else { + node = new ClassNode(); + new ClassReader(originalBytes).accept(node, readerOptions); + accessor = FastClassAccessor.ofAsmNode(node); + } + initialized = true; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/falsepattern/lib/turboasm/FastClassAccessor.java b/src/main/java/com/falsepattern/lib/turboasm/FastClassAccessor.java new file mode 100644 index 0000000..388a804 --- /dev/null +++ b/src/main/java/com/falsepattern/lib/turboasm/FastClassAccessor.java @@ -0,0 +1,186 @@ +/* + * This file is part of FalsePatternLib. + * + * Copyright (C) 2022-2024 FalsePattern + * All Rights Reserved + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * FalsePatternLib is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FalsePatternLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with FalsePatternLib. If not, see . + */ + +package com.falsepattern.lib.turboasm; + +import java.lang.reflect.Modifier; + +import com.falsepattern.lib.StableAPI; +import com.falsepattern.lib.StableAPI.Expose; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.ClassNode; + +/** An accessor to metadata about a class that is quickly accessible without fully parsing one. */ +@StableAPI(since = "__EXPERIMENTAL__") +public interface FastClassAccessor { + /** Accessible from outside its package */ + @Expose + boolean isPublic(); + /** No subclasses allowed */ + @Expose + boolean isFinal(); + /** Is an interface instead of a class */ + @Expose + boolean isInterface(); + /** Is an abstract class that should not be instantiated */ + @Expose + boolean isAbstract(); + /** Is not present in source code (often used by obfuscated code too) */ + @Expose + boolean isSynthetic(); + /** Is an annotation interface */ + @Expose + boolean isAnnotation(); + /** Is an enum class */ + @Expose + boolean isEnum(); + /** Binary (slash-separated packages) name of the class */ + @NotNull + @Expose + String binaryThisName(); + /** Binary (slash-separated packages) name of the super-class, null for the Object class */ + @Nullable + @Expose + String binarySuperName(); + + @Expose + static OfLoaded ofLoaded(Class loadedClass) { + return new OfLoaded(loadedClass); + } + + @Expose + static OfAsmNode ofAsmNode(ClassNode handle) { + return new OfAsmNode(handle); + } + + @StableAPI(since = "__EXPERIMENTAL__") + final class OfAsmNode implements FastClassAccessor { + public final ClassNode handle; + + public OfAsmNode(ClassNode handle) { + this.handle = handle; + } + + @Override + public boolean isPublic() { + return (handle.access & Opcodes.ACC_PUBLIC) != 0; + } + + @Override + public boolean isFinal() { + return (handle.access & Opcodes.ACC_FINAL) != 0; + } + + @Override + public boolean isInterface() { + return (handle.access & Opcodes.ACC_INTERFACE) != 0; + } + + @Override + public boolean isAbstract() { + return (handle.access & Opcodes.ACC_ABSTRACT) != 0; + } + + @Override + public boolean isSynthetic() { + return (handle.access & Opcodes.ACC_SYNTHETIC) != 0; + } + + @Override + public boolean isAnnotation() { + return (handle.access & Opcodes.ACC_ANNOTATION) != 0; + } + + @Override + public boolean isEnum() { + return (handle.access & Opcodes.ACC_ENUM) != 0; + } + + @Override + public @NotNull String binaryThisName() { + return handle.name; + } + + @Override + public @Nullable String binarySuperName() { + return handle.superName; + } + } + + @StableAPI(since = "__EXPERIMENTAL__") + final class OfLoaded implements FastClassAccessor { + public final Class handle; + + private OfLoaded(Class handle) { + this.handle = handle; + } + + @Override + public boolean isPublic() { + return Modifier.isPublic(handle.getModifiers()); + } + + @Override + public boolean isFinal() { + return Modifier.isFinal(handle.getModifiers()); + } + + @Override + public boolean isInterface() { + return Modifier.isInterface(handle.getModifiers()); + } + + @Override + public boolean isAbstract() { + return Modifier.isAbstract(handle.getModifiers()); + } + + @Override + public boolean isSynthetic() { + return handle.isSynthetic(); + } + + @Override + public boolean isAnnotation() { + return handle.isAnnotation(); + } + + @Override + public boolean isEnum() { + return handle.isEnum(); + } + + @Override + public @NotNull String binaryThisName() { + return handle.getName().replace('.', '/'); + } + + @Override + public @Nullable String binarySuperName() { + final Class superclass = handle.getSuperclass(); + return superclass == null ? null : superclass.getName().replace('.', '/'); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/falsepattern/lib/turboasm/MergeableTurboTransformer.java b/src/main/java/com/falsepattern/lib/turboasm/MergeableTurboTransformer.java new file mode 100644 index 0000000..c6d108c --- /dev/null +++ b/src/main/java/com/falsepattern/lib/turboasm/MergeableTurboTransformer.java @@ -0,0 +1,68 @@ +/* + * This file is part of FalsePatternLib. + * + * Copyright (C) 2022-2024 FalsePattern + * All Rights Reserved + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * FalsePatternLib is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FalsePatternLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with FalsePatternLib. If not, see . + */ +package com.falsepattern.lib.turboasm; + +import com.falsepattern.lib.StableAPI; +import com.falsepattern.lib.internal.asm.CoreLoadingPlugin; +import lombok.RequiredArgsConstructor; +import lombok.val; + +import net.minecraft.launchwrapper.IClassTransformer; + +import java.util.ArrayList; +import java.util.List; + +@StableAPI(since = "__EXPERIMENTAL__") +public class MergeableTurboTransformer implements IClassTransformer { + private final List transformers; + + public MergeableTurboTransformer(List transformers) { + this.transformers = new ArrayList<>(transformers); + } + + @Override + public final byte[] transform(String name, String transformedName, byte[] bytes) { + if (bytes == null) { + return null; + } + if (transformers.isEmpty()) { + return bytes; + } + val handle = new ClassNodeHandle(bytes); + if (TransformerUtil.executeTransformers(transformedName, handle, transformers)) { + return handle.computeBytes(); + } else { + return bytes; + } + } + + public static MergeableTurboTransformer merge(MergeableTurboTransformer a, MergeableTurboTransformer b) { + val arr = new ArrayList<>(a.transformers); + arr.addAll(b.transformers); + return new MergeableTurboTransformer(arr); + } + + public static void mergeAllTurboTransformers() { + CoreLoadingPlugin.mergeTurboTransformers(); + } +} diff --git a/src/main/java/com/falsepattern/lib/turboasm/TransformerUtil.java b/src/main/java/com/falsepattern/lib/turboasm/TransformerUtil.java new file mode 100644 index 0000000..b59a98b --- /dev/null +++ b/src/main/java/com/falsepattern/lib/turboasm/TransformerUtil.java @@ -0,0 +1,53 @@ +/* + * This file is part of FalsePatternLib. + * + * Copyright (C) 2022-2024 FalsePattern + * All Rights Reserved + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * FalsePatternLib is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FalsePatternLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with FalsePatternLib. If not, see . + */ + +package com.falsepattern.lib.turboasm; + +import lombok.val; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.List; + +public class TransformerUtil { + private static final Logger LOG = LogManager.getLogger("ASM"); + public static boolean executeTransformers(String transformedName, ClassNodeHandle handle, List transformers) { + boolean didTransformation = false; + for (val transformer: transformers) { + try { + if (transformer.shouldTransformClass(transformedName, handle)) { + if (!didTransformation) { + LOG.debug("Transforming {}!", transformedName); + } + didTransformation = true; + transformer.transformClass(transformedName, handle); + LOG.debug("Successfully transformed class {} with {}, owner: {}", transformedName, transformer.name(), transformer.owner()); + } + } catch (Exception e) { + LOG.error("Failed to transform class {} with {}, owner: {}", transformedName, transformer.name(), transformer.owner()); + LOG.error("Exception stacktrace:", e); + } + } + return didTransformation; + } +} diff --git a/src/main/java/com/falsepattern/lib/turboasm/TurboClassTransformer.java b/src/main/java/com/falsepattern/lib/turboasm/TurboClassTransformer.java new file mode 100644 index 0000000..0ed58e0 --- /dev/null +++ b/src/main/java/com/falsepattern/lib/turboasm/TurboClassTransformer.java @@ -0,0 +1,64 @@ +/* + * This file is part of FalsePatternLib. + * + * Copyright (C) 2022-2024 FalsePattern + * All Rights Reserved + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * FalsePatternLib is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FalsePatternLib is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with FalsePatternLib. If not, see . + */ + +package com.falsepattern.lib.turboasm; + +import com.falsepattern.lib.StableAPI; +import com.falsepattern.lib.StableAPI.Expose; +import org.jetbrains.annotations.NotNull; + +/** + * A simple transformer that takes in class bytes and outputs different class bytes. + * It should be thread-safe, and not change class names. It should also have a public no-arguments constructor. + */ +@StableAPI(since = "__EXPERIMENTAL__") +public interface TurboClassTransformer { + /** + * @return The user-friendly owner name of this transformer. Usually a mod id. + */ + @Expose + String owner(); + + /** + * @return The user-friendly name of this transformer. Used in logs. + */ + @Expose + String name(); + + /** + * A fast scanning function that is used to determine if class transformations should be skipped altogether (if all transformers return false). + * @param className The name of the transformed class (in the dot-separated format). + * @param classNode The handle to the class data and parsed metadata, try to avoid triggering the lazy ASM parse if possible for performance. + * @return true if the class will be transformed by this class transformer. + */ + @Expose + boolean shouldTransformClass(@NotNull String className, @NotNull ClassNodeHandle classNode); + + /** + * (Optionally) transform a given class. No ClassReader flags are used for maximum efficiency, so stack frames are not expanded. + * @param className The name of the transformed class (in the dot-separated format). + * @param classNode The handle to the lazily ASM-parsed class to modify, and metadata used for class writing. + */ + @Expose + void transformClass(@NotNull String className, @NotNull ClassNodeHandle classNode); +} \ No newline at end of file