|
| 1 | +package org.embeddedt.blacksmith.impl.transformers; |
| 2 | + |
| 3 | +import org.objectweb.asm.Opcodes; |
| 4 | +import org.objectweb.asm.tree.ClassNode; |
| 5 | +import org.objectweb.asm.tree.InsnList; |
| 6 | +import org.objectweb.asm.tree.MethodInsnNode; |
| 7 | +import org.objectweb.asm.tree.MethodNode; |
| 8 | +import org.objectweb.asm.tree.VarInsnNode; |
| 9 | + |
| 10 | +import java.lang.instrument.IllegalClassFormatException; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Optional; |
| 14 | + |
| 15 | +public class BootstrapLauncherTransformer implements RuntimeTransformer { |
| 16 | + @Override |
| 17 | + public List<String> getTransformedClasses() { |
| 18 | + return Arrays.asList("cpw/mods/bootstraplauncher/BootstrapLauncher"); |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + public void transformClass(ClassNode data) throws IllegalClassFormatException { |
| 23 | + for (MethodNode method : data.methods) { |
| 24 | + if (method.name.equals("main") && (method.access & Opcodes.ACC_STATIC) != 0) { |
| 25 | + Optional<MethodInsnNode> nameCaptureInLoop = RuntimeTransformer.streamInsnList(method.instructions) |
| 26 | + .filter(n -> n.getOpcode() == Opcodes.INVOKEINTERFACE) |
| 27 | + .map(MethodInsnNode.class::cast) |
| 28 | + .filter(m -> m.name.equals("name") && m.owner.equals("cpw/mods/jarhandling/SecureJar")) |
| 29 | + .findFirst(); |
| 30 | + nameCaptureInLoop.ifPresent(methodInsnNode -> RuntimeTransformer.searchBackward(methodInsnNode, insn -> insn.getOpcode() == Opcodes.ALOAD).ifPresent(aLoad -> { |
| 31 | + RuntimeTransformer.searchForward(methodInsnNode, insn -> { |
| 32 | + if (insn.getOpcode() == Opcodes.INVOKEVIRTUAL) { |
| 33 | + MethodInsnNode node = (MethodInsnNode) insn; |
| 34 | + return node.owner.equals("java/util/ArrayList") && node.name.equals("add"); |
| 35 | + } else { |
| 36 | + return false; |
| 37 | + } |
| 38 | + }).ifPresent(listAdd -> { |
| 39 | + InsnList epilogueClose = new InsnList(); |
| 40 | + epilogueClose.add(new VarInsnNode(Opcodes.ALOAD, ((VarInsnNode) aLoad).var)); |
| 41 | + epilogueClose.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, "cpw/mods/jarhandling/SecureJar", "getRootPath", "()Ljava/nio/file/Path;", true)); |
| 42 | + epilogueClose.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, "java/nio/file/Path", "getFileSystem", "()Ljava/nio/file/FileSystem;", true)); |
| 43 | + epilogueClose.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, "java/io/Closeable", "close", "()V", true)); |
| 44 | + method.instructions.insert(listAdd, epilogueClose); |
| 45 | + }); |
| 46 | + })); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments