Installation simplification and ASM upgrade (#82)

* Upstream TerminalTransformer

* TerminalTransformer: support ASM9 opcodes

* Mod*Visitor: accept newer asm (mixin)

* Gradle fix: disable runClient17/runServer17 as the regular tasks already use java 17 in this repo

* Update to asm 9.5 for better Java 21 support

* Installation overhaul: embed the modified libraries in the forgePatches jar to remove a complex installation step of merging libraries folders.
This commit is contained in:
Raven Szewczyk authored and GitHub committed 2023-09-23 13:38:17 +01:00
1 parent 0fd9137540
commit 03c6021fca
9 files changed
+234 -87

No files matched your search

@@ -0,0 +1,149 @@
package cpw.mods.fml.common.asm.transformers;
import net.minecraft.launchwrapper.IClassTransformer;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import cpw.mods.fml.relauncher.FMLRelaunchLog;
import cpw.mods.fml.relauncher.FMLSecurityManager.ExitTrappedException;
public class TerminalTransformer implements IClassTransformer {
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
if (basicClass == null) return null;
ClassReader reader = new ClassReader(basicClass);
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassVisitor visitor = writer;
visitor = new ExitVisitor(visitor);
reader.accept(visitor, 0);
return writer.toByteArray();
}
public static class ExitVisitor extends ClassVisitor {
private String clsName = null;
private static final String callbackOwner = org.objectweb.asm.Type.getInternalName(ExitVisitor.class);
private ExitVisitor(ClassVisitor cv) {
// LWJGL3ify patch: ASM5->ASM9
super(Opcodes.ASM9, cv);
}
@Override
public void visit(int version, int access, String name, String signature, String superName,
String[] interfaces) {
super.visit(version, access, name, signature, superName, interfaces);
this.clsName = name;
}
@Override
public MethodVisitor visitMethod(int mAccess, final String mName, final String mDesc, String mSignature,
String[] mExceptions) {
final boolean warn = !(clsName.equals("net/minecraft/client/Minecraft")
|| clsName.equals("net/minecraft/server/dedicated/DedicatedServer")
|| clsName.equals("cpw/mods/fml/common/FMLCommonHandler")
|| clsName.startsWith("com/jcraft/jogg/")
|| clsName.startsWith("scala/sys/"));
// LWJGL3ify patch: ASM5->ASM9
return new MethodVisitor(Opcodes.ASM9, super.visitMethod(mAccess, mName, mDesc, mSignature, mExceptions)) {
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean isIntf) {
if (opcode == Opcodes.INVOKESTATIC && owner.equals("java/lang/System")
&& name.equals("exit")
&& desc.equals("(I)V")) {
if (warn) {
FMLRelaunchLog.warning("=============================================================");
FMLRelaunchLog.warning(
"MOD HAS DIRECT REFERENCE System.exit() THIS IS NOT ALLOWED REROUTING TO FML!");
FMLRelaunchLog.warning("Offendor: %s.%s%s", ExitVisitor.this.clsName, mName, mDesc);
FMLRelaunchLog.warning("Use FMLCommonHandler.exitJava instead");
FMLRelaunchLog.warning("=============================================================");
}
owner = ExitVisitor.callbackOwner;
name = "systemExitCalled";
} else if (opcode == Opcodes.INVOKEVIRTUAL && owner.equals("java/lang/Runtime")
&& name.equals("exit")
&& desc.equals("(I)V")) {
if (warn) {
FMLRelaunchLog.warning("=============================================================");
FMLRelaunchLog.warning(
"MOD HAS DIRECT REFERENCE Runtime.exit() THIS IS NOT ALLOWED REROUTING TO FML!");
FMLRelaunchLog.warning("Offendor: %s.%s%s", ExitVisitor.this.clsName, mName, mDesc);
FMLRelaunchLog.warning("Use FMLCommonHandler.exitJava instead");
FMLRelaunchLog.warning("=============================================================");
}
opcode = Opcodes.INVOKESTATIC;
owner = ExitVisitor.callbackOwner;
name = "runtimeExitCalled";
desc = "(Ljava/lang/Runtime;I)V";
} else if (opcode == Opcodes.INVOKEVIRTUAL && owner.equals("java/lang/Runtime")
&& name.equals("halt")
&& desc.equals("(I)V")) {
if (warn) {
FMLRelaunchLog
.warning("=============================================================");
FMLRelaunchLog.warning(
"MOD HAS DIRECT REFERENCE Runtime.halt() THIS IS NOT ALLOWED REROUTING TO FML!");
FMLRelaunchLog.warning("Offendor: %s.%s%s", ExitVisitor.this.clsName, mName, mDesc);
FMLRelaunchLog.warning("Use FMLCommonHandler.exitJava instead");
FMLRelaunchLog
.warning("=============================================================");
}
opcode = Opcodes.INVOKESTATIC;
owner = ExitVisitor.callbackOwner;
name = "runtimeHaltCalled";
desc = "(Ljava/lang/Runtime;I)V";
}
super.visitMethodInsn(opcode, owner, name, desc, isIntf);
}
};
}
// Intercept System.exit, and check if the caller is allowed to use it, if not wrap it in a ExitTrappedException
public static void systemExitCalled(int status) {
ExitVisitor.checkAccess();
System.exit(status);
}
// Intercept Runtime.getRuntime().exit, and check if the caller is allowed to use it, if not wrap it in a
// ExitTrappedException
public static void runtimeExitCalled(Runtime runtime, int status) {
ExitVisitor.checkAccess();
runtime.exit(status);
}
// Intercept Runtime.getRuntime().halt, and check if the caller is allowed to use it, if not wrap it in a
// ExitTrappedException
public static void runtimeHaltCalled(Runtime runtime, int status) {
ExitVisitor.checkAccess();
runtime.halt(status);
}
private static void checkAccess() {
StackTraceElement[] cause = Thread.currentThread()
.getStackTrace();
String callingClass = cause.length > 2 ? cause[3].getClassName() : "none";
String callingParent = cause.length > 3 ? cause[4].getClassName() : "none";
// FML is allowed to call system exit and the Minecraft applet (from the quit button), and the dedicated
// server (from itself)
if (!(callingClass.startsWith("cpw.mods.fml.")
|| ("net.minecraft.client.Minecraft".equals(callingClass)
&& "net.minecraft.client.Minecraft".equals(callingParent))
|| ("net.minecraft.server.dedicated.DedicatedServer".equals(callingClass)
&& "net.minecraft.server.MinecraftServer".equals(callingParent)))) {
throw new ExitTrappedException();
}
}
}
}