[unsafe] Avoid loading main class during ASM manipulation

This commit is contained in:
Johannes Frohnmeyer 2022-04-03 19:55:40 +02:00
parent 4a65a61b7e
commit 9e52a0d1f6
Signed by: Johannes
GPG Key ID: E76429612C2929F4
4 changed files with 21 additions and 19 deletions

View File

@ -1,9 +1,9 @@
package io.gitlab.jfronny.libjf.unsafe;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.unsafe.asm.AsmConfig;
import io.gitlab.jfronny.libjf.unsafe.asm.AsmTransformer;
import io.gitlab.jfronny.libjf.unsafe.asm.BakedAsmConfig;
import net.fabricmc.loader.impl.util.log.Log;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
@ -37,16 +37,15 @@ public class MixinPlugin implements IMixinConfigPlugin {
AsmTransformer.INSTANCE = (AsmTransformer) unsafe.allocateInstance(AsmTransformer.class);
AsmTransformer.INSTANCE.delegate = (IMixinTransformer) mixinTransformerField.get(delegate);
AsmTransformer.INSTANCE.asmConfigs = new HashSet<>();
DynamicEntry.execute(LibJf.MOD_ID + ":asm", AsmConfig.class, s -> {
LibJf.LOGGER.info("Discovered LibJF asm plugin in " + s.modId());
DynamicEntry.execute("libjf:asm", AsmConfig.class, s -> {
Log.info(JfLanguageAdapter.LOG_CATEGORY, "Discovered LibJF asm plugin in " + s.modId());
AsmTransformer.INSTANCE.asmConfigs.add(new BakedAsmConfig(s.instance(), s.modId()));
});
AsmTransformer.INSTANCE.init();
mixinTransformerField.set(delegate, AsmTransformer.INSTANCE);
} catch (NoSuchFieldException | IllegalAccessException | InstantiationException e) {
LibJf.LOGGER.error("Could not initialize LibJF ASM", e);
Log.error(JfLanguageAdapter.LOG_CATEGORY, "Could not initialize LibJF ASM", e);
}
}
@Override

View File

@ -1,10 +1,11 @@
package io.gitlab.jfronny.libjf.unsafe.asm;
import io.gitlab.jfronny.libjf.Flags;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.unsafe.JfLanguageAdapter;
import io.gitlab.jfronny.libjf.unsafe.asm.patch.Patch;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.MappingResolver;
import net.fabricmc.loader.impl.util.log.Log;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
@ -39,7 +40,7 @@ public class AsmTransformer implements IMixinTransformer {
for (Flags.BooleanFlag flag : flags) {
flagNames.add(flag.source());
}
LibJf.LOGGER.info("Exporting ASM due to: " + String.join(", ", flagNames));
Log.info(JfLanguageAdapter.LOG_CATEGORY, "Exporting ASM due to: " + String.join(", ", flagNames));
}
flags = Flags.getBoolFlags("asm.log");
flags.removeIf(flag -> !flag.value());
@ -49,7 +50,7 @@ public class AsmTransformer implements IMixinTransformer {
for (Flags.BooleanFlag flag : flags) {
flagNames.add(flag.source());
}
LibJf.LOGGER.info("Logging ASM logs due to: " + String.join(", ", flagNames));
Log.info(JfLanguageAdapter.LOG_CATEGORY, "Logging ASM logs due to: " + String.join(", ", flagNames));
}
}
@ -77,7 +78,7 @@ public class AsmTransformer implements IMixinTransformer {
classBytes = delegate.transformClassBytes(name, transformedName, classBytes);
if (classBytes == null || name == null) return classBytes;
if (isClassUnmoddable(name, null)) {
if (debugLogsEnabled()) LibJf.LOGGER.info("Skipping " + name);
if (debugLogsEnabled()) Log.info(JfLanguageAdapter.LOG_CATEGORY, "Skipping " + name);
return classBytes;
}
@ -93,7 +94,7 @@ public class AsmTransformer implements IMixinTransformer {
patch.apply(klazz);
}
catch (Throwable t) {
LibJf.LOGGER.error("Could not apply patch: " + patch.getClass() + " on " + name, t);
Log.error(JfLanguageAdapter.LOG_CATEGORY, "Could not apply patch: " + patch.getClass() + " on " + name, t);
}
}
}
@ -105,7 +106,7 @@ public class AsmTransformer implements IMixinTransformer {
klazz.accept(writer);
}
catch (NullPointerException t) {
LibJf.LOGGER.error("Could not transform " + transformedName, t);
Log.error(JfLanguageAdapter.LOG_CATEGORY, "Could not transform " + transformedName, t);
return null;
}
classBytes = writer.toByteArray();
@ -115,7 +116,7 @@ public class AsmTransformer implements IMixinTransformer {
if (!Files.exists(path)) Files.createDirectories(path.getParent());
Files.write(path, classBytes);
} catch (IOException e) {
LibJf.LOGGER.error("Could not export modified bytecode", e);
Log.error(JfLanguageAdapter.LOG_CATEGORY, "Could not export modified bytecode", e);
}
}
return classBytes;

View File

@ -1,9 +1,10 @@
package io.gitlab.jfronny.libjf.unsafe.asm.patch.modification;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.unsafe.JfLanguageAdapter;
import io.gitlab.jfronny.libjf.unsafe.asm.AsmTransformer;
import io.gitlab.jfronny.libjf.unsafe.asm.patch.MethodPatch;
import io.gitlab.jfronny.libjf.unsafe.asm.patch.Patch;
import net.fabricmc.loader.impl.util.log.Log;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
@ -22,7 +23,7 @@ public class MethodModificationPatch implements Patch {
);
}
for (String s : this.patches.keySet()) {
if (AsmTransformer.INSTANCE.debugLogsEnabled()) LibJf.LOGGER.info("Registered patch for " + s);
if (AsmTransformer.INSTANCE.debugLogsEnabled()) Log.info(JfLanguageAdapter.LOG_CATEGORY, "Registered patch for " + s);
}
}
@ -30,7 +31,7 @@ public class MethodModificationPatch implements Patch {
public void apply(ClassNode klazz) {
for (MethodNode method : klazz.methods) {
if (patches.containsKey(method.name)) {
if (AsmTransformer.INSTANCE.debugLogsEnabled()) LibJf.LOGGER.info("Patching " + method.name);
if (AsmTransformer.INSTANCE.debugLogsEnabled()) Log.info(JfLanguageAdapter.LOG_CATEGORY, "Patching " + method.name);
patches.get(method.name).apply(method, klazz);
}
}

View File

@ -1,8 +1,9 @@
package io.gitlab.jfronny.libjf.unsafe.asm.patch.targeting;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.unsafe.JfLanguageAdapter;
import io.gitlab.jfronny.libjf.unsafe.asm.AsmTransformer;
import io.gitlab.jfronny.libjf.unsafe.asm.patch.Patch;
import net.fabricmc.loader.impl.util.log.Log;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.ClassNode;
@ -22,7 +23,7 @@ public class InterfaceImplTargetPatch implements Patch {
public void apply(ClassNode klazz) {
scanInterfaces(klazz);
if (getUpper(klazz.name).contains(targetInterface)) {
if (AsmTransformer.INSTANCE.debugLogsEnabled()) LibJf.LOGGER.info("Found " + klazz.name + " implementing " + targetInterface);
if (AsmTransformer.INSTANCE.debugLogsEnabled()) Log.info(JfLanguageAdapter.LOG_CATEGORY, "Found " + klazz.name + " implementing " + targetInterface);
methodPatch.apply(klazz);
}
}
@ -78,7 +79,7 @@ public class InterfaceImplTargetPatch implements Patch {
&& !className.startsWith("it/unimi/dsi/fastutil/")
&& !className.startsWith("com/google/")
) {
if (AsmTransformer.INSTANCE.debugLogsEnabled()) LibJf.LOGGER.info("Non-default class not considered for interface scanning: " + className);
if (AsmTransformer.INSTANCE.debugLogsEnabled()) Log.info(JfLanguageAdapter.LOG_CATEGORY, "Non-default class not considered for interface scanning: " + className);
INTERFACES.put(className, Set.of());
return Set.of();
}
@ -86,7 +87,7 @@ public class InterfaceImplTargetPatch implements Patch {
scanInterfaces(Class.forName(className.replace('/', '.')));
s = INTERFACES.get(className);
} catch (ClassNotFoundException e) {
LibJf.LOGGER.error("Could not get base for " + className, e);
Log.error(JfLanguageAdapter.LOG_CATEGORY, "Could not get base for " + className, e);
return Set.of();
}
}