List of usage examples for org.objectweb.asm.tree AbstractInsnNode getOpcode
public int getOpcode()
From source file:com.thomas15v.packetlib.codegenerator.asm.ASMHelper.java
License:MIT License
public static boolean isConstant(AbstractInsnNode insn) { if (insn == null) { return false; }/*from w ww .ja v a 2 s.c om*/ return Ints.contains(ASMHelper.CONSTANTS_ALL, insn.getOpcode()); }
From source file:com.thomas15v.packetlib.codegenerator.asm.ASMHelper.java
License:MIT License
public static Object getConstant(AbstractInsnNode insn) { if (insn == null) { return null; } else if (insn instanceof LdcInsnNode) { return ((LdcInsnNode) insn).cst; } else if (insn instanceof IntInsnNode) { int value = ((IntInsnNode) insn).operand; if (insn.getOpcode() == Opcodes.BIPUSH || insn.getOpcode() == Opcodes.SIPUSH) { return Integer.valueOf(value); }/*from www . j a v a 2 s . c o m*/ throw new IllegalArgumentException( "IntInsnNode with invalid opcode " + insn.getOpcode() + " in getConstant"); } int index = Ints.indexOf(ASMHelper.CONSTANTS_ALL, insn.getOpcode()); return index < 0 ? null : ASMHelper.CONSTANTS_VALUES[index]; }
From source file:com.thomas15v.packetlib.codegenerator.asm.ASMHelper.java
License:MIT License
public static Type getConstantType(AbstractInsnNode insn) { if (insn == null) { return null; } else if (insn instanceof LdcInsnNode) { Object cst = ((LdcInsnNode) insn).cst; if (cst instanceof Integer) { return Type.getType("I"); } else if (cst instanceof Float) { return Type.getType("F"); } else if (cst instanceof Long) { return Type.getType("J"); } else if (cst instanceof Double) { return Type.getType("D"); } else if (cst instanceof String) { return Type.getType("Ljava/lang/String;"); } else if (cst instanceof Type) { return Type.getType("Ljava/lang/Class;"); }//from ww w. j a va 2s. c o m throw new IllegalArgumentException( "LdcInsnNode with invalid payload type " + cst.getClass() + " in getConstant"); } int index = Ints.indexOf(ASMHelper.CONSTANTS_ALL, insn.getOpcode()); return index < 0 ? null : Type.getType(ASMHelper.CONSTANTS_TYPES[index]); }
From source file:com.triage.bytecodemaster.GroovyShardTransformer.java
protected byte[] transformClass(String tapScript, String className, ClassLoader cl, byte[] classFileBuffer) throws Exception { Timer timer = new Timer(); timer.start();//w w w. ja v a 2 s . c om //so that we can reference classes available in the caller when we compile the script //MAKES A KEY ASSUMPTION: that all classloaders we will have a path back to the system //classloader, and thus will have our groovy stuff in them! //if this isnt the case, we may want to have a dual-parent classloader where //we can search both the target classloader and the system CachingGroovyClassLoader groovyClassLoader = new CachingGroovyClassLoader(cl); //load the script in ASM ClassNode shimClassNode = new ClassNode(); String scriptName = className + "-Tap.groovy"; timer.mark("startParse"); Class groovyClass = groovyClassLoader.parseClass(tapScript, scriptName); timer.mark("endParse"); String generatedClassName = groovyClass.getName() + ".class"; byte[] classBytes = groovyClassLoader.getClassBytes(generatedClassName); timer.mark("getClassBytes"); ClassReader shimClassReader = new ClassReader(classBytes); shimClassReader.accept(shimClassNode, 0); timer.mark("readShimClass"); ClassNode targetClassNode = new ClassNode(); ClassReader targetClassReader = new ClassReader(classFileBuffer); targetClassReader.accept(targetClassNode, 0); timer.mark("readTargetClass"); //copy instructions //TODO: this is just a POC-- of course all this hardcoded stuff needs //to be replaced with real code MethodNode targetMethod = null; MethodNode sourceMethod = null; InsnList instructionsToInject = null; if (className.contains("TestFriend")) { targetMethod = findMethod(targetClassNode, "whatDoIThinkAbout"); sourceMethod = findMethod(shimClassNode, "beforeWhatDoIThinkAbout"); instructionsToInject = sourceMethod.instructions; } else if (className.contains("Calculator")) { targetMethod = findMethod(targetClassNode, "add"); sourceMethod = findMethod(shimClassNode, "beforeAdd"); //HACK: in the calculator script, we do not want a premature //return, so lets remove RETURNs from the source. //that DOESNT work in gneeral because sometimes we do want a return //but this will just see if removing the returns works instructionsToInject = sourceMethod.instructions; ListIterator li = instructionsToInject.iterator(); while (li.hasNext()) { AbstractInsnNode node = (AbstractInsnNode) li.next(); if (isReturnOpCode(node.getOpcode())) { li.remove(); } } } timer.mark("gotInstructionsToInject"); System.out.println("Transforming Target Method:"); printMethodNode(targetMethod); System.out.println("Transforming Source Method:"); printMethodNode(sourceMethod); //insert source instructions in target targetMethod.instructions.insert(sourceMethod.instructions); timer.mark("injectedInstructions"); //write a class ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); targetClassNode.accept(cw); timer.mark("finishedWrite"); System.out.println("Successfully transformed class" + className); timer.stop(); System.out.println("Timings:" + timer); return cw.toByteArray(); }
From source file:com.triage.bytecodemaster.TestObjectReferenceSwitches.java
public void testReplacingThisWithOtherVariable() throws Exception { final String FIELDPROXYWORKED = "FIELDPROXYWORKED"; //set up the proxy object. this is the object that will receive //the proxied calls TestSubClass tcc = new TestSubClass(); tcc.setBaseString(FIELDPROXYWORKED); TestBaseClassHolder.setTestBase(tcc); //get the dynamic source that has the donor body in it ClassNode donorSource = loadGroovyTestClassAsBytecode(GROOVY_CLASS_FIELDREF); MethodNode donorMethod = findMethod(donorSource, "before_whatDoIThinkAbout"); System.out.println("Donor"); printMethodNode(donorMethod);//from ww w. ja v a 2 s. co m //alright here's the strategy: (1) inject a new local variable that points // to our remote instance, // (2) inject code that sets this local to the value of a method call, // (3) change references to 'this' ( ALOAD0 or ALOAD 0 ) to ALOAD1 InsnList instructionsToInject = donorMethod.instructions; //make a new local variable LabelNode begin = new LabelNode(); LabelNode end = new LabelNode(); instructionsToInject.insertBefore(instructionsToInject.getFirst(), begin); instructionsToInject.add(end); Type type = Type.getObjectType("com/triage/bytecodemaster/fortesting/TestBaseClass"); int variableIndex = donorMethod.maxLocals; donorMethod.maxLocals += type.getSize(); donorMethod.visitLocalVariable("proxy", type.getDescriptor(), null, begin.getLabel(), end.getLabel(), variableIndex); //set the value of the local variable with a new instruction at the top //fetch a reference to our proxy object MethodInsnNode getTestBase = new MethodInsnNode(Opcodes.INVOKESTATIC, "com/triage/bytecodemaster/fortesting/TestBaseClassHolder", "getTestBase", "()Lcom/triage/bytecodemaster/fortesting/TestBaseClass;"); //insert after begin label instructionsToInject.insert(begin, getTestBase); //store reference VarInsnNode setRef = new VarInsnNode(Opcodes.ASTORE, variableIndex); //insert store after fetch instructionsToInject.insert(getTestBase, setRef); //replace all references to 'this' with the new variable for (int currentIndex = 0; currentIndex < instructionsToInject.size(); currentIndex++) { AbstractInsnNode node = instructionsToInject.get(currentIndex); if (node.getOpcode() == Opcodes.ALOAD) { VarInsnNode vin = (VarInsnNode) node; //'this' is var index 0. ours is var index varindex if (vin.var == 0) { vin.var = variableIndex; } } } System.out.println(">>>>>>>>>Finished Modifying<<<<<<<<"); printMethodNode(donorMethod); String NEWCLASSNAME = "ScriptTestClass"; //write a class Class c = createClassFromClassNode(donorSource, NEWCLASSNAME); Object o = c.newInstance(); Method m = o.getClass().getDeclaredMethod("before_whatDoIThinkAbout", String.class); //should return HAHAHA not baseStringValue String result = (String) m.invoke(o, new Object[] { "AAAA" }); System.out.println("TestDonorClass.whatDoIThinkAbout Result: " + result); assertTrue(result.equals(FIELDPROXYWORKED + "AAAA")); }
From source file:com.triage.bytecodemaster.TestObjectReferenceSwitches.java
@Test public void testInjectingIntoMethodWithLotsOfParameters() throws Exception { final String FIELDPROXYWORKED = "FIELDPROXYWORKED"; //set up the proxy object. this is the object that will receive //the proxied calls TestSubClass tcc = new TestSubClass(); tcc.setBaseString(FIELDPROXYWORKED); TestBaseClassHolder.setTestBase(tcc); //get the dynamic source that has the donor body in it ClassNode donorSource = loadGroovyTestClassAsBytecode(GROOVY_CLASS_FIELDREF2); MethodNode donorMethod = findMethod(donorSource, "before_whatDoIThinkAbout"); System.out.println("Donor Method Before Modifications:"); printMethodNode(donorMethod);//from www . j av a 2 s . c o m String TARGETCLASSNAME = "com.triage.bytecodemaster.fortesting.Concatenator"; ClassNode targetSource = loadLocalClass(TARGETCLASSNAME); ClassNode exampleSource = loadLocalClass("com.triage.bytecodemaster.fortesting.JustLikeGroovyClass"); MethodNode exampleMethod = findMethod(exampleSource, "before_whatDoIThinkAbout"); System.out.println("Example Method-- Should be just like the Donor Source"); printMethodNode(exampleMethod); MethodNode targetMethod = findMethod(targetSource, "concat"); System.out.println("Target Method <Before Mods>"); printMethodNode(targetMethod); //alright here's the strategy: (1) inject a new local variable that points // to our remote instance, // (2) inject code that sets this local to the value of a method call, // (3) change references to 'this' ( ALOAD0 or ALOAD 0 ) to ALOAD1 InsnList instructionsToInject = donorMethod.instructions; InsnList targetInstructions = targetMethod.instructions; //make a new local variable in the donor method. //this variable needs to have a slot high enough that it doesnt //conflict with either the target or the source method //it will hold references to the objects we replace with 'this' LabelNode begin = new LabelNode(); LabelNode end = new LabelNode(); targetInstructions.insertBefore(targetInstructions.getFirst(), begin); targetInstructions.add(end); Type type = Type.getObjectType("com/triage/bytecodemaster/fortesting/TestBaseClass"); int variableIndex = targetMethod.maxLocals; targetMethod.maxLocals += type.getSize(); targetMethod.visitLocalVariable("proxy", type.getDescriptor(), null, begin.getLabel(), end.getLabel(), variableIndex); //set the value of the local variable with a new instruction at the top //fetch a reference to our proxy object MethodInsnNode getTestBase = new MethodInsnNode(Opcodes.INVOKESTATIC, "com/triage/bytecodemaster/fortesting/TestBaseClassHolder", "getTestBase", "()Lcom/triage/bytecodemaster/fortesting/TestBaseClass;"); //insert after begin label targetInstructions.insert(begin, getTestBase); //store reference VarInsnNode setRef = new VarInsnNode(Opcodes.ASTORE, variableIndex); //insert store after fetch targetInstructions.insert(getTestBase, setRef); //replace all references to 'this' in the DONOR method with the new variable //in the TARGET code for (int currentIndex = 0; currentIndex < instructionsToInject.size(); currentIndex++) { AbstractInsnNode node = instructionsToInject.get(currentIndex); if (node.getOpcode() == Opcodes.ALOAD) { VarInsnNode vin = (VarInsnNode) node; //'this' is var index 0. ours is var index varindex if (vin.var == 0) { vin.var = variableIndex; } } //remove return methods. this will prevent a return. it should cause the donor //method to have parameters that overlap with the target, which has more parameters if (node.getOpcode() == Opcodes.RETURN || node.getOpcode() == Opcodes.ARETURN) { instructionsToInject.remove(node); } } System.out.println(">>>>>>>>>Finished Modifying Donor Method <<<<<<<<"); printMethodNode(donorMethod); String NEWCLASSNAME = "ScriptTestClass"; //stash instructions at the beginning of the original method, //but after populating the new variable targetInstructions.insert(setRef, instructionsToInject); System.out.println("Modified Target:"); printMethodNode(targetMethod); //write a class Class c = createClassFromClassNode(targetSource, TARGETCLASSNAME); Object o = c.newInstance(); Method m = o.getClass().getDeclaredMethod("concat", String.class, String.class, String.class, String.class); //should return HAHAHA not baseStringValue String result = (String) m.invoke(o, new Object[] { "A", "B", "C", "D" }); System.out.println("Concatenator.concat Result: " + result); assertTrue(result.equals("ABCD")); }
From source file:com.triage.bytecodemaster.TestObjectReferenceSwitches.java
public void testMergedInReplacingThisWithOtherVariable() throws Exception { final String FIELDPROXYWORKED = "FIELDPROXYWORKED"; //set up the proxy object. this is the object that will receive //the proxied calls TestSubClass tcc = new TestSubClass(); tcc.setBaseString(FIELDPROXYWORKED); TestBaseClassHolder.setTestBase(tcc); //get the dynamic source that has the donor body in it ClassNode donorSource = loadGroovyTestClassAsBytecode(GROOVY_CLASS_FIELDREF); MethodNode donorMethod = findMethod(donorSource, "before_whatDoIThinkAbout"); //load the target class String TARGETCLASSNAME = "com.triage.bytecodemaster.fortesting.TestFriend"; ClassNode targetSource = loadLocalClass(TARGETCLASSNAME); MethodNode targetMethod = findMethod(targetSource, "whatDoIThinkAbout"); System.out.println("Target"); printMethodNode(targetMethod);// w ww . ja v a2 s.c o m System.out.println("Donor"); printMethodNode(donorMethod); //alright here's the strategy: (1) inject a new local variable that points // to our remote instance, // (2) inject code that sets this local to the value of a method call, // (3) change references to 'this' ( ALOAD0 or ALOAD 0 ) to ALOAD1 InsnList instructionsToInject = donorMethod.instructions; //make a new local variable LabelNode begin = new LabelNode(); LabelNode end = new LabelNode(); instructionsToInject.insertBefore(instructionsToInject.getFirst(), begin); instructionsToInject.add(end); Type type = Type.getObjectType("com/triage/bytecodemaster/fortesting/TestBaseClass"); int variableIndex = donorMethod.maxLocals; donorMethod.maxLocals += type.getSize(); donorMethod.visitLocalVariable("proxy", type.getDescriptor(), null, begin.getLabel(), end.getLabel(), variableIndex); //set the value of the local variable with a new instruction at the top //fetch a reference to our proxy object MethodInsnNode getTestBase = new MethodInsnNode(Opcodes.INVOKESTATIC, "com/triage/bytecodemaster/fortesting/TestBaseClassHolder", "getTestBase", "()Lcom/triage/bytecodemaster/fortesting/TestBaseClass;"); //insert after begin label instructionsToInject.insert(begin, getTestBase); //store reference VarInsnNode setRef = new VarInsnNode(Opcodes.ASTORE, variableIndex); //insert store after fetch instructionsToInject.insert(getTestBase, setRef); //replace all references to 'this' with the new variable for (int currentIndex = 0; currentIndex < instructionsToInject.size(); currentIndex++) { AbstractInsnNode node = instructionsToInject.get(currentIndex); if (node.getOpcode() == Opcodes.ALOAD) { VarInsnNode vin = (VarInsnNode) node; //'this' is var index 0. ours is var index varindex if (vin.var == 0) { vin.var = variableIndex; } } } System.out.println(">>>>>>>>>Finished Modifying<<<<<<<<"); printMethodNode(donorMethod); //insert the donorMethod targetMethod.instructions.insert(instructionsToInject); System.out.println(">>>>>>>>>Final Method<<<<<<<<"); printMethodNode(targetMethod); //write a class Class c = createClassFromClassNode(targetSource, TARGETCLASSNAME); Object o = c.newInstance(); Method m = o.getClass().getDeclaredMethod("whatDoIThinkAbout", TestPerson.class); TestPerson testPerson = new TestPerson(); testPerson.setName("AAAA"); //should return HAHAHA not baseStringValue String result = (String) m.invoke(o, new Object[] { testPerson }); System.out.println("TestFriend.whatDoIThinkAbout Result: " + result); assertTrue(result.equals(FIELDPROXYWORKED + "AAAA")); }
From source file:cuchaz.enigma.CompiledSourceTypeLoader.java
License:Open Source License
private void removeRedundantClassCalls(ClassNode node) { // remove <obj>.getClass() calls that are seemingly injected // DUP//w w w .j a v a 2s . co m // INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class; // POP for (MethodNode methodNode : node.methods) { AbstractInsnNode insnNode = methodNode.instructions.getFirst(); while (insnNode != null) { if (insnNode instanceof MethodInsnNode && insnNode.getOpcode() == Opcodes.INVOKEVIRTUAL) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; if (methodInsnNode.name.equals("getClass") && methodInsnNode.owner.equals("java/lang/Object") && methodInsnNode.desc.equals("()Ljava/lang/Class;")) { AbstractInsnNode previous = methodInsnNode.getPrevious(); AbstractInsnNode next = methodInsnNode.getNext(); if (previous.getOpcode() == Opcodes.DUP && next.getOpcode() == Opcodes.POP) { insnNode = previous.getPrevious();//reset the iterator so it gets the new next instruction methodNode.instructions.remove(previous); methodNode.instructions.remove(methodInsnNode); methodNode.instructions.remove(next); } } } insnNode = insnNode.getNext(); } } }
From source file:cuchaz.enigma.TranslatingTypeLoader.java
License:Open Source License
private byte[] loadType(String className) { // NOTE: don't know if class name is obf or deobf ClassEntry classEntry = new ClassEntry(className); ClassEntry obfClassEntry = this.obfuscatingTranslator.getTranslatedClass(classEntry); // is this an inner class referenced directly? (ie trying to load b instead of a$b) if (!obfClassEntry.isInnerClass()) { List<ClassEntry> classChain = this.jarIndex.getObfClassChain(obfClassEntry); if (classChain.size() > 1) { System.err.println(String.format("WARNING: no class %s after inner class reconstruction. Try %s", className, obfClassEntry.buildClassEntry(classChain))); return null; }//from w w w . ja va 2 s. c o m } // is this a class we should even know about? if (!this.jarIndex.containsObfClass(obfClassEntry)) { return null; } // DEBUG //System.out.println(String.format("Looking for %s (obf: %s)", classEntry.getName(), obfClassEntry.getName())); // find the class in the jar ClassNode node = findClassInJar(obfClassEntry); if (node == null) { // couldn't find it return null; } // remove <obj>.getClass() calls that are seemingly injected // DUP // INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class; // POP for (MethodNode methodNode : node.methods) { AbstractInsnNode insnNode = methodNode.instructions.getFirst(); while (insnNode != null) { if (insnNode instanceof MethodInsnNode && insnNode.getOpcode() == Opcodes.INVOKEVIRTUAL) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; if (methodInsnNode.name.equals("getClass") && methodInsnNode.owner.equals("java/lang/Object") && methodInsnNode.desc.equals("()Ljava/lang/Class;")) { AbstractInsnNode previous = methodInsnNode.getPrevious(); AbstractInsnNode next = methodInsnNode.getNext(); if (previous.getOpcode() == Opcodes.DUP && next.getOpcode() == Opcodes.POP) { insnNode = previous.getPrevious();//reset the iterator so it gets the new next instruction methodNode.instructions.remove(previous); methodNode.instructions.remove(methodInsnNode); methodNode.instructions.remove(next); } } } insnNode = insnNode.getNext(); } } ClassWriter writer = new ClassWriter(0); transformInto(node, writer); // we have a transformed class! return writer.toByteArray(); }
From source file:cz.vutbr.fit.xhriba01.bc.BytecodeCodeVisitor.java
License:Open Source License
private String getOpcodeLabel(AbstractInsnNode insn) { return Printer.OPCODES[insn.getOpcode()]; }