List of usage examples for org.objectweb.asm.tree AbstractInsnNode METHOD_INSN
int METHOD_INSN
To view the source code for org.objectweb.asm.tree AbstractInsnNode METHOD_INSN.
Click Source Link
From source file:blockphysics.asm.BPTransformer.java
License:Open Source License
private byte[] transformEntityFallingSand(byte[] bytes) { /*try//from w w w . ja v a2 s. c om { FileOutputStream fos = new FileOutputStream("d:/EntityFallingSand.orig.class"); fos.write(bytes); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ System.out.print("[BlockPhysics] Patching EntityFallingSand.class ......."); boolean ok = false; boolean ok2 = false; boolean ok3 = false; boolean ok4 = false; boolean ok5 = false; boolean ok6 = false; boolean ok7 = false; boolean ok8 = false; boolean ok9 = false, ok10 = false; ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(bytes); classReader.accept(classNode, 0); MethodNode m; Iterator<MethodNode> methods = classNode.methods.iterator(); while (methods.hasNext()) { m = methods.next(); if (m.name.equals("<init>") && m.desc.equals("(Labv;)V")) { for (int index = m.instructions.size() - 1; index >= 0; index--) { if (m.instructions.get(index).getOpcode() == RETURN) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_1)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "m", "Z")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new LdcInsnNode(new Float("0.996"))); toInject.add(new LdcInsnNode(new Float("0.996"))); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "sq", "a", "(FF)V")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new FieldInsnNode(GETFIELD, "sq", "P", "F")); toInject.add(new InsnNode(FCONST_2)); toInject.add(new InsnNode(FDIV)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "N", "F")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(DCONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "x", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(DCONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "y", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(DCONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "z", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(DCONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationX", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new LdcInsnNode(new Double("-0.024525"))); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationY", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(DCONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationZ", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "slideDir", "B")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_1)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "Z", "Z")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new LdcInsnNode(new Float("0.8"))); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "aa", "F")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_4)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "dead", "B")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "bpdata", "I")); m.instructions.insertBefore(m.instructions.get(index), toInject); ok10 = true; break; } } } else if (m.name.equals("<init>") && m.desc.equals("(Labv;DDDII)V")) { for (int index = 0; index < m.instructions.size(); index++) { if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN && ((MethodInsnNode) m.instructions.get(index)).owner.equals("sq") && ((MethodInsnNode) m.instructions.get(index)).name.equals("a")) { m.instructions.set(m.instructions.get(index - 1), new LdcInsnNode(new Float("0.996"))); m.instructions.set(m.instructions.get(index - 2), new LdcInsnNode(new Float("0.996"))); ok = true; break; } } for (int index = m.instructions.size() - 1; index >= 0; index--) { if (m.instructions.get(index).getOpcode() == RETURN) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(DCONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationX", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new LdcInsnNode(new Double("-0.024525"))); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationY", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(DCONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationZ", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "slideDir", "B")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_1)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "Z", "Z")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new LdcInsnNode(new Float("0.8"))); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "aa", "F")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_4)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "dead", "B")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "bpdata", "I")); m.instructions.insertBefore(m.instructions.get(index), toInject); ok2 = true; break; } } } else if (m.name.equals("K") && m.desc.equals("()Z")) { InsnList toInject = new InsnList(); toInject.add(new InsnNode(ICONST_0)); toInject.add(new InsnNode(IRETURN)); m.instructions.clear(); m.localVariables.clear(); m.instructions.add(toInject); ok9 = true; } else if (m.name.equals("l_") && m.desc.equals("()V")) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new FieldInsnNode(GETFIELD, "sq", "q", "Labv;")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "fallingSandUpdate", "(Labv;Lsq;)V")); toInject.add(new InsnNode(RETURN)); m.instructions.clear(); m.localVariables.clear(); m.instructions.add(toInject); ok3 = true; } else if (m.name.equals("b") && m.desc.equals("(F)V")) { m.instructions.clear(); m.localVariables.clear(); m.instructions.add(new InsnNode(RETURN)); ok4 = true; } else if (m.name.equals("b") && m.desc.equals("(Lbx;)V")) { for (int index = m.instructions.size() - 1; index >= 0; index--) { if (m.instructions.get(index).getOpcode() == RETURN) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 1)); toInject.add(new LdcInsnNode("Acceleration")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_3)); toInject.add(new IntInsnNode(NEWARRAY, T_DOUBLE)); toInject.add(new InsnNode(DUP)); toInject.add(new InsnNode(ICONST_0)); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new FieldInsnNode(GETFIELD, "sq", "accelerationX", "D")); toInject.add(new InsnNode(DASTORE)); toInject.add(new InsnNode(DUP)); toInject.add(new InsnNode(ICONST_1)); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new FieldInsnNode(GETFIELD, "sq", "accelerationY", "D")); toInject.add(new InsnNode(DASTORE)); toInject.add(new InsnNode(DUP)); toInject.add(new InsnNode(ICONST_2)); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new FieldInsnNode(GETFIELD, "sq", "accelerationZ", "D")); toInject.add(new InsnNode(DASTORE)); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "sq", "a", "([D)Lcf;")); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "a", "(Ljava/lang/String;Lck;)V")); toInject.add(new VarInsnNode(ALOAD, 1)); toInject.add(new LdcInsnNode("BPData")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new FieldInsnNode(GETFIELD, "sq", "bpdata", "I")); toInject.add(new InsnNode(I2B)); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "a", "(Ljava/lang/String;B)V")); m.instructions.insertBefore(m.instructions.get(index), toInject); ok5 = true; break; } } } else if (m.name.equals("a") && m.desc.equals("(Lbx;)V")) { while (!(m.instructions.get(0).getType() == AbstractInsnNode.LDC_INSN && ((LdcInsnNode) m.instructions.get(0)).cst.equals("Data"))) { m.instructions.remove(m.instructions.get(0)); ok6 = true; } InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new VarInsnNode(ALOAD, 1)); toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "readFallingSandID", "(Lbx;)I")); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "a", "I")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new VarInsnNode(ALOAD, 1)); m.instructions.insertBefore(m.instructions.get(0), toInject); for (int index = m.instructions.size() - 1; index >= 0; index--) { if (m.instructions.get(index).getOpcode() == RETURN) { toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 1)); toInject.add(new LdcInsnNode("Acceleration")); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "b", "(Ljava/lang/String;)Z")); LabelNode l4 = new LabelNode(); toInject.add(new JumpInsnNode(IFEQ, l4)); toInject.add(new VarInsnNode(ALOAD, 1)); toInject.add(new LdcInsnNode("Acceleration")); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "m", "(Ljava/lang/String;)Lcf;")); toInject.add(new VarInsnNode(ASTORE, 2)); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new VarInsnNode(ALOAD, 2)); toInject.add(new InsnNode(ICONST_0)); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "cf", "b", "(I)Lck;")); toInject.add(new TypeInsnNode(CHECKCAST, "ca")); toInject.add(new FieldInsnNode(GETFIELD, "ca", "a", "D")); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationX", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new VarInsnNode(ALOAD, 2)); toInject.add(new InsnNode(ICONST_1)); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "cf", "b", "(I)Lck;")); toInject.add(new TypeInsnNode(CHECKCAST, "ca")); toInject.add(new FieldInsnNode(GETFIELD, "ca", "a", "D")); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationY", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new VarInsnNode(ALOAD, 2)); toInject.add(new InsnNode(ICONST_2)); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "cf", "b", "(I)Lck;")); toInject.add(new TypeInsnNode(CHECKCAST, "ca")); toInject.add(new FieldInsnNode(GETFIELD, "ca", "a", "D")); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationZ", "D")); LabelNode l5 = new LabelNode(); toInject.add(new JumpInsnNode(GOTO, l5)); toInject.add(l4); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(DCONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationX", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(DCONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationY", "D")); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(DCONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationZ", "D")); toInject.add(l5); toInject.add(new VarInsnNode(ALOAD, 1)); toInject.add(new LdcInsnNode("BPData")); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "b", "(Ljava/lang/String;)Z")); LabelNode l7 = new LabelNode(); toInject.add(new JumpInsnNode(IFEQ, l7)); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new VarInsnNode(ALOAD, 1)); toInject.add(new LdcInsnNode("BPData")); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "c", "(Ljava/lang/String;)B")); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "bpdata", "I")); LabelNode l8 = new LabelNode(); toInject.add(new JumpInsnNode(GOTO, l8)); toInject.add(l7); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "sq", "bpdata", "I")); toInject.add(l8); m.instructions.insertBefore(m.instructions.get(index), toInject); ok7 = true; break; } } } else if (m.name.equals("au") && m.desc.equals("()Z")) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "sq", "ae", "()Z")); toInject.add(new InsnNode(IRETURN)); m.instructions.clear(); m.localVariables.clear(); m.instructions.add(toInject); ok8 = true; } } ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(cw); FieldVisitor fv; fv = cw.visitField(ACC_PUBLIC, "accelerationX", "D", null, null); fv.visitEnd(); fv = cw.visitField(ACC_PUBLIC, "accelerationY", "D", null, null); fv.visitEnd(); fv = cw.visitField(ACC_PUBLIC, "accelerationZ", "D", null, null); fv.visitEnd(); fv = cw.visitField(ACC_PUBLIC, "bpdata", "I", null, null); fv.visitEnd(); fv = cw.visitField(ACC_PUBLIC, "slideDir", "B", null, null); fv.visitEnd(); fv = cw.visitField(ACC_PUBLIC, "media", "I", null, null); fv.visitEnd(); fv = cw.visitField(ACC_PUBLIC, "dead", "B", null, null); fv.visitEnd(); MethodVisitor mv; mv = cw.visitMethod(ACC_PUBLIC, "D", "()Lasu;", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, "sq", "E", "Lasu;"); mv.visitInsn(ARETURN); mv.visitMaxs(1, 1); mv.visitEnd(); mv = cw.visitMethod(ACC_PUBLIC, "al", "()V", null, null); mv.visitCode(); mv.visitInsn(RETURN); mv.visitMaxs(0, 1); mv.visitEnd(); mv = cw.visitMethod(ACC_PUBLIC, "d", "(DDD)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, "sq", "q", "Labv;"); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(DLOAD, 1); mv.visitVarInsn(DLOAD, 3); mv.visitVarInsn(DLOAD, 5); mv.visitMethodInsn(INVOKESTATIC, "blockphysics/BlockPhysics", "moveEntity", "(Labv;Lsq;DDD)V"); mv.visitInsn(RETURN); mv.visitMaxs(8, 7); mv.visitEnd(); if (!ok8) { mv = cw.visitMethod(ACC_PUBLIC, "au", "()Z", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "sq", "ae", "()Z"); mv.visitInsn(IRETURN); mv.visitMaxs(0, 0); mv.visitEnd(); ok8 = true; } cw.visitEnd(); if (ok && ok2 && ok3 && ok4 && ok5 && ok6 && ok7 && ok8 && ok9 && ok10) System.out.println("OK"); else System.out.println("Failed." + ok + ok2 + ok3 + ok4 + ok5 + ok6 + ok7 + ok8 + ok9 + ok10); /*try { FileOutputStream fos = new FileOutputStream("d:/EntityFallingSand.class"); fos.write(cw.toByteArray()); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ return cw.toByteArray(); }
From source file:blockphysics.asm.BPTransformer.java
License:Open Source License
private byte[] transformRenderFallingSand(byte[] bytes) { /*try//w w w. j av a 2 s. c o m { FileOutputStream fos = new FileOutputStream("d:/RenderFallingSand.orig.class"); fos.write(bytes); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ System.out.print("[BlockPhysics] Patching RenderFallingSand.class ......."); boolean ok = false, ok2 = false, ok4 = false; ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(bytes); classReader.accept(classNode, 0); MethodNode m; Iterator<MethodNode> methods = classNode.methods.iterator(); while (methods.hasNext()) { m = methods.next(); if (m.name.equals("a") && m.desc.equals("(Lsq;DDDFF)V")) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 1)); toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BClient", "cancelRender", "(Lsq;)Z")); LabelNode l0 = new LabelNode(); toInject.add(new JumpInsnNode(IFEQ, l0)); toInject.add(new InsnNode(RETURN)); toInject.add(l0); m.instructions.insertBefore(m.instructions.getFirst(), toInject); int index; for (index = 0; index < m.instructions.size(); index++) { if (m.instructions.get(index).getOpcode() == GETFIELD && m.instructions.get(index).getType() == AbstractInsnNode.FIELD_INSN && ((FieldInsnNode) m.instructions.get(index)).owner.equals("sq") && ((FieldInsnNode) m.instructions.get(index)).name.equals("a") && ((FieldInsnNode) m.instructions.get(index)).desc.equals("I")) { index = index + 3; ok = true; break; } } while (!(m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN && ((MethodInsnNode) m.instructions.get(index)).name.equals("glPushMatrix"))) { m.instructions.remove(m.instructions.get(index)); ok2 = true; } for (index = 0; index < m.instructions.size(); index++) { if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN && ((MethodInsnNode) m.instructions.get(index)).owner.equals("bfo") && ((MethodInsnNode) m.instructions.get(index)).name.equals("a") && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(Laqw;Labv;IIII)V")) { m.instructions.set(m.instructions.get(index), new MethodInsnNode(INVOKESTATIC, "blockphysics/BClient", "renderBlockSandFalling", "(Lbfo;Laqw;Labv;IIII)V")); ok4 = true; break; } } } } ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(cw); if (ok && ok2 && ok4) System.out.println("OK"); else System.out.println("Failed." + ok + ok2 + ok4); /*try { FileOutputStream fos = new FileOutputStream("d:/RenderFallingSand.class"); fos.write(cw.toByteArray()); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ return cw.toByteArray(); }
From source file:blockphysics.asm.BPTransformer.java
License:Open Source License
private byte[] transformNetClientHandler(byte[] bytes) { /*try//from w ww. j av a 2 s . co m { FileOutputStream fos = new FileOutputStream("d:/NetClientHandler.orig.class"); fos.write(bytes); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ System.out.print("[BlockPhysics] Patching NetClientHandler.class ........"); boolean ok = false; ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(bytes); classReader.accept(classNode, 0); MethodNode m; Iterator<MethodNode> methods = classNode.methods.iterator(); while (methods.hasNext()) { m = methods.next(); if (m.name.equals("a") && m.desc.equals("(Ldc;)V")) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new FieldInsnNode(GETFIELD, "bct", "i", "Lbda;")); toInject.add(new VarInsnNode(DLOAD, 2)); toInject.add(new VarInsnNode(DLOAD, 4)); toInject.add(new VarInsnNode(DLOAD, 6)); toInject.add(new VarInsnNode(ALOAD, 1)); toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "createFallingsand", "(Labv;DDDLdc;)Lsq;")); toInject.add(new VarInsnNode(ASTORE, 8)); toInject.add(new VarInsnNode(ALOAD, 1)); toInject.add(new InsnNode(ICONST_1)); toInject.add(new FieldInsnNode(PUTFIELD, "dc", "k", "I")); for (int index = 0; index < m.instructions.size(); index++) { if (m.instructions.get(index).getOpcode() == INVOKESPECIAL && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN && ((MethodInsnNode) m.instructions.get(index)).owner.equals("sq") && ((MethodInsnNode) m.instructions.get(index)).name.equals("<init>")) { while (m.instructions.get(index).getType() != AbstractInsnNode.FIELD_INSN || m.instructions.get(index).getOpcode() != PUTFIELD || !((FieldInsnNode) m.instructions.get(index)).owner.equals("dc") || !((FieldInsnNode) m.instructions.get(index)).name.equals("k")) { index++; } while (m.instructions.get(index).getType() != AbstractInsnNode.TYPE_INSN || !((TypeInsnNode) m.instructions.get(index)).desc.equals("sq")) { m.instructions.remove(m.instructions.get(index)); index--; ok = true; } if (ok) { m.instructions.remove(m.instructions.get(index)); m.instructions.insertBefore(m.instructions.get(index), toInject); } break; } } } } ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(cw); if (ok) System.out.println("OK"); else System.out.println("Failed." + ok); /*try { FileOutputStream fos = new FileOutputStream("d:/NetClientHandler.class"); fos.write(cw.toByteArray()); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ return cw.toByteArray(); }
From source file:blockphysics.asm.BPTransformer.java
License:Open Source License
private byte[] transformGuiSelectWorld(byte[] bytes) { /*try//from w ww.ja v a2s.c o m { FileOutputStream fos = new FileOutputStream("d:/GuiSelectWorld.orig.class"); fos.write(bytes); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ System.out.print("[BlockPhysics] Patching GuiSelectWorld.class .........."); boolean ok = false, ok2 = false; ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(bytes); classReader.accept(classNode, 0); MethodNode m; Iterator<MethodNode> methods = classNode.methods.iterator(); while (methods.hasNext()) { m = methods.next(); if (m.name.equals("A_") && m.desc.equals("()V")) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "awe", "d", "Z")); for (int index = m.instructions.size() - 1; index >= 0; index--) { if (m.instructions.get(index).getOpcode() == RETURN) { m.instructions.insertBefore(m.instructions.get(index), toInject); ok = true; break; } } } else if (m.name.equals("e") && m.desc.equals("(I)V")) { for (int index = 0; index < m.instructions.size(); index++) { if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN && ((MethodInsnNode) m.instructions.get(index)).owner.equals("ats") && ((MethodInsnNode) m.instructions.get(index)).name.equals("a") && ((MethodInsnNode) m.instructions.get(index)).desc .equals("(Ljava/lang/String;Ljava/lang/String;Lacc;)V")) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new VarInsnNode(ALOAD, 2)); toInject.add(new VarInsnNode(ALOAD, 3)); toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BClient", "loadWorld", "(Lawb;Ljava/lang/String;Ljava/lang/String;)V")); m.instructions.insert(m.instructions.get(index), toInject); while (m.instructions.get(index).getType() != AbstractInsnNode.JUMP_INSN) { m.instructions.remove(m.instructions.get(index)); index--; } ok2 = true; break; } } } } ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(cw); if (ok && ok2) System.out.println("OK"); else System.out.println("Failed." + ok + ok2); /*try { FileOutputStream fos = new FileOutputStream("d:/GuiSelectWorld.class"); fos.write(cw.toByteArray()); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ return cw.toByteArray(); }
From source file:blockphysics.asm.BPTransformer.java
License:Open Source License
private byte[] transformGuiCreateWorld(byte[] bytes) { /*try/*from w ww . j av a2 s .com*/ { FileOutputStream fos = new FileOutputStream("d:/GuiCreateWorld.orig.class"); fos.write(bytes); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ System.out.print("[BlockPhysics] Patching GuiCreateWorld.class .........."); boolean ok = false, ok2 = false; ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(bytes); classReader.accept(classNode, 0); MethodNode m; Iterator<MethodNode> methods = classNode.methods.iterator(); while (methods.hasNext()) { m = methods.next(); if (m.name.equals("A_") && m.desc.equals("()V")) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "auy", "v", "Z")); for (int index = m.instructions.size() - 1; index >= 0; index--) { if (m.instructions.get(index).getOpcode() == RETURN) { m.instructions.insertBefore(m.instructions.get(index), toInject); ok = true; break; } } } else if (m.name.equals("a") && m.desc.equals("(Lauq;)V")) { for (int index = 0; index < m.instructions.size(); index++) { if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN && ((MethodInsnNode) m.instructions.get(index)).owner.equals("ats") && ((MethodInsnNode) m.instructions.get(index)).name.equals("a") && ((MethodInsnNode) m.instructions.get(index)).desc .equals("(Ljava/lang/String;Ljava/lang/String;Lacc;)V")) { m.instructions.remove(m.instructions.get(index)); InsnList toInject = new InsnList(); toInject.add(new InsnNode(ICONST_1)); toInject.add(new MethodInsnNode(INVOKESPECIAL, "blockphysics/BGui", "<init>", "(Lawb;Ljava/lang/String;Ljava/lang/String;Lacc;Z)V")); toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "ats", "a", "(Lawb;)V")); m.instructions.insertBefore(m.instructions.get(index), toInject); while (m.instructions.get(index).getOpcode() != GETFIELD || m.instructions.get(index).getType() != AbstractInsnNode.FIELD_INSN || !((FieldInsnNode) m.instructions.get(index)).owner.equals("auy") || !((FieldInsnNode) m.instructions.get(index)).name.equals("f") || !((FieldInsnNode) m.instructions.get(index)).desc.equals("Lats;")) { index--; } toInject = new InsnList(); toInject.add(new TypeInsnNode(NEW, "blockphysics/BGui")); toInject.add(new InsnNode(DUP)); toInject.add(new VarInsnNode(ALOAD, 0)); m.instructions.insert(m.instructions.get(index), toInject); ok2 = true; break; } } } } ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(cw); if (ok && ok2) System.out.println("OK"); else System.out.println("Failed." + ok + ok2); /*try { FileOutputStream fos = new FileOutputStream("d:/GuiCreateWorld.class"); fos.write(cw.toByteArray()); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ return cw.toByteArray(); }
From source file:blockphysics.asm.BPTransformer.java
License:Open Source License
private byte[] transformBlockDragonEgg(byte[] bytes) { /*try// w w w.ja v a 2 s . co m { FileOutputStream fos = new FileOutputStream("d:/BlockDragonEgg.orig.class"); fos.write(bytes); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ System.out.print("[BlockPhysics] Patching BlockDragonEgg.class .........."); boolean ok = false, ok2 = false, ok3 = false, ok4 = false; ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(bytes); classReader.accept(classNode, 0); MethodNode m; Iterator<MethodNode> methods = classNode.methods.iterator(); while (methods.hasNext()) { m = methods.next(); if (m.name.equals("a") && m.desc.equals("(Labv;IIII)V")) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 1)); toInject.add(new VarInsnNode(ILOAD, 2)); toInject.add(new VarInsnNode(ILOAD, 3)); toInject.add(new VarInsnNode(ILOAD, 4)); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new FieldInsnNode(GETFIELD, "any", "cF", "I")); toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "onNeighborBlockChange", "(Labv;IIII)V")); toInject.add(new InsnNode(RETURN)); m.instructions.clear(); m.localVariables.clear(); m.instructions.add(toInject); ok = true; } else if (m.name.equals("a") && m.desc.equals("(Labv;IIILjava/util/Random;)V")) { m.instructions.clear(); m.localVariables.clear(); m.instructions.insert(new InsnNode(RETURN)); ok2 = true; } else if (m.name.equals("k") && m.desc.equals("(Labv;III)V")) { m.instructions.clear(); m.localVariables.clear(); m.instructions.insert(new InsnNode(RETURN)); ok3 = true; } else if (m.name.equals("m") && m.desc.equals("(Labv;III)V")) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 1)); toInject.add(new VarInsnNode(ILOAD, 6)); toInject.add(new VarInsnNode(ILOAD, 7)); toInject.add(new VarInsnNode(ILOAD, 8)); toInject.add( new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "notifyMove", "(Labv;III)V")); for (int index = 0; index < m.instructions.size(); index++) { if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN && ((MethodInsnNode) m.instructions.get(index)).owner.equals("abv") && ((MethodInsnNode) m.instructions.get(index)).name.equals("i") && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(III)Z")) { if (m.instructions.get(index + 1).getOpcode() == POP) { m.instructions.insert(m.instructions.get(index + 1), toInject); } else m.instructions.insert(m.instructions.get(index), toInject); ok4 = true; break; } } } } ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(cw); if (ok && ok2 && ok3 & ok4) System.out.println("OK"); else System.out.println("Failed." + ok + ok2 + ok3 + ok4); /*try { FileOutputStream fos = new FileOutputStream("d:/BlockDragonEgg.class"); fos.write(cw.toByteArray()); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ return cw.toByteArray(); }
From source file:cl.inria.stiq.instrumenter.BCIUtils.java
License:Open Source License
private static void printFrames(MethodNode aNode, Frame[] aFrames) { int bcIndex = 1; for (int i = 0; i < aFrames.length; i++) { Frame theFrame = aFrames[i]; AbstractInsnNode theInsn = aNode.instructions.get(i); switch (theInsn.getType()) { case AbstractInsnNode.INSN: case AbstractInsnNode.INT_INSN: case AbstractInsnNode.VAR_INSN: case AbstractInsnNode.TYPE_INSN: case AbstractInsnNode.FIELD_INSN: case AbstractInsnNode.METHOD_INSN: case AbstractInsnNode.JUMP_INSN: case AbstractInsnNode.LDC_INSN: case AbstractInsnNode.IINC_INSN: case AbstractInsnNode.TABLESWITCH_INSN: case AbstractInsnNode.LOOKUPSWITCH_INSN: case AbstractInsnNode.MULTIANEWARRAY_INSN: TraceMethodVisitor theTraceVisitor = new TraceMethodVisitor(); theInsn.accept(theTraceVisitor); StringWriter theWriter = new StringWriter(); theTraceVisitor.print(new PrintWriter(theWriter)); String theTraced = theWriter.toString().replace("\n", ""); System.out.println(bcIndex + "\t" + frameString(theFrame) + " |\t" + theTraced); bcIndex++;//from w w w . j a v a 2s. com break; case AbstractInsnNode.FRAME: case AbstractInsnNode.LINE: case AbstractInsnNode.LABEL: break; } } }
From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java
License:Open Source License
public boolean collectCodeBlocks() { final int numIns = mn.instructions.size(); codeBlocks[0] = FrameInfo.FIRST;//w w w . jav a2 s .com for (int i = 0; i < numIns; i++) { final Frame f = frames[i]; if (f != null) { // reachable ? AbstractInsnNode in = mn.instructions.get(i); if (in.getType() == AbstractInsnNode.METHOD_INSN || in.getType() == AbstractInsnNode.INVOKE_DYNAMIC_INSN) { Boolean susp = true; if (in.getType() == AbstractInsnNode.METHOD_INSN) { final MethodInsnNode min = (MethodInsnNode) in; int opcode = min.getOpcode(); if (isReflectInvocation(min.owner, min.name)) db.log(LogLevel.DEBUG, "Reflective method call at instruction %d is assumed suspendable", i); else if (isMethodHandleInvocation(min.owner, min.name)) db.log(LogLevel.DEBUG, "MethodHandle invocation at instruction %d is assumed suspendable", i); else if (isInvocationHandlerInvocation(min.owner, min.name)) db.log(LogLevel.DEBUG, "InvocationHandler invocation at instruction %d is assumed suspendable", i); else { SuspendableType st = db.isMethodSuspendable(min.owner, min.name, min.desc, opcode); if (st == SuspendableType.NON_SUSPENDABLE) susp = false; else if (st == null) { db.log(LogLevel.WARNING, "Method not found in class - assuming suspendable: %s#%s%s (at%s#%s)", min.owner, min.name, min.desc, className, mn.name); susp = true; } else if (susp) db.log(LogLevel.DEBUG, "Method call at instruction %d to %s#%s%s is suspendable", i, min.owner, min.name, min.desc); if (st == SuspendableType.SUSPENDABLE_SUPER) this.hasSuspendableSuperCalls = true; } } else { // invoke dynamic final InvokeDynamicInsnNode idin = (InvokeDynamicInsnNode) in; if (idin.bsm.getOwner().equals("java/lang/invoke/LambdaMetafactory")) { // lambda db.log(LogLevel.DEBUG, "Lambda at instruction %d", i); susp = false; } else db.log(LogLevel.DEBUG, "InvokeDynamic Method call at instruction %d to is assumed suspendable", i); } if (susp) { FrameInfo fi = addCodeBlock(f, i); splitTryCatch(fi); } else { if (in.getType() == AbstractInsnNode.METHOD_INSN) {// not invokedynamic final MethodInsnNode min = (MethodInsnNode) in; db.log(LogLevel.DEBUG, "Method call at instruction %d to %s#%s%s is not suspendable", i, min.owner, min.name, min.desc); int blockingId = isBlockingCall(min); if (blockingId >= 0 && !isAllowedToBlock(className, mn.name)) { int mask = 1 << blockingId; if (!db.isAllowBlocking()) { throw new UnableToInstrumentException( "blocking call to " + min.owner + "#" + min.name + min.desc, className, mn.name, mn.desc); } else if ((warnedAboutBlocking & mask) == 0) { warnedAboutBlocking |= mask; db.log(LogLevel.WARNING, "Method %s#%s%s contains potentially blocking call to " + min.owner + "#" + min.name + min.desc, className, mn.name, mn.desc); } } } } } } } addCodeBlock(null, numIns); return numCodeBlocks > 1; }
From source file:co.paralleluniverse.fibers.instrument.LabelSuspendableCallSitesClassVisitor.java
License:Open Source License
@Override public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { if ((access & Opcodes.ACC_NATIVE) == 0 && !isYieldMethod(className, name)) { // Bytecode-level AST of a method, being a MethodVisitor itself can be filled through delegation from another visitor // Analyze, fill and enqueue method ASTs final MethodVisitor outMV = super.visitMethod(access, name, desc, signature, exceptions); return new MethodVisitor(ASMAPI, outMV) { private int currLineNumber = -1; @Override/* w w w . j a va2 s .c o m*/ public void visitLineNumber(int i, Label label) { currLineNumber = i; super.visitLineNumber(i, label); } @Override public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean isInterface) { final int type = AbstractInsnNode.METHOD_INSN; if (InstrumentMethod.isSuspendableCall(db, type, opcode, owner, name, desc)) { final Label l = new Label(); super.visitLabel(l); super.visitLineNumber(currLineNumber, l); // Force label } super.visitMethodInsn(opcode, owner, name, desc, isInterface); } @Override public void visitInvokeDynamicInsn(String name, String desc, Handle handle, Object... objects) { final int type = AbstractInsnNode.INVOKE_DYNAMIC_INSN; final int opcode = Opcodes.INVOKEDYNAMIC; if (InstrumentMethod.isSuspendableCall(db, type, opcode, handle.getOwner(), name, desc)) { final Label l = new Label(); super.visitLabel(l); super.visitLineNumber(currLineNumber, l); // Force label } super.visitInvokeDynamicInsn(name, desc, handle, objects); } }; } return super.visitMethod(access, name, desc, signature, exceptions); }
From source file:co.paralleluniverse.fibers.instrument.SuspOffsetsAfterInstrClassVisitor.java
License:Open Source License
@Override public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { if ((access & Opcodes.ACC_NATIVE) == 0 && !isYieldMethod(className, name)) { // Bytecode-level AST of a method, being a MethodVisitor itself can be filled through delegation from another visitor final MethodNode mn = new MethodNode(access, name, desc, signature, exceptions); // Analyze, fill and enqueue method ASTs final MethodVisitor outMV = super.visitMethod(access, name, desc, signature, exceptions); return new MethodVisitor(ASMAPI, outMV) { private Label currLabel = null; private int prevOffset = -1; private boolean instrumented; private boolean optimized = false; private int methodStart = -1, methodEnd = -1; private List<Integer> suspOffsetsAfterInstrL = new ArrayList<>(); private int[] suspCallSites = new int[0]; @Override//from ww w. j ava 2 s .c o m public AnnotationVisitor visitAnnotation(final String adesc, boolean visible) { if (Classes.INSTRUMENTED_DESC.equals(adesc)) { instrumented = true; return new AnnotationVisitor(ASMAPI) { // Only collect info @Override public void visit(String name, Object value) { if (Instrumented.FIELD_NAME_METHOD_START.equals(name)) methodStart = (Integer) value; else if (Instrumented.FIELD_NAME_METHOD_END.equals(name)) methodEnd = (Integer) value; else if (Instrumented.FIELD_NAME_METHOD_OPTIMIZED.equals(name)) optimized = (Boolean) value; else if (Instrumented.FIELD_NAME_SUSPENDABLE_CALL_SITES.equals(name)) suspCallSites = (int[]) value; else //noinspection StatementWithEmptyBody if (Instrumented.FIELD_NAME_SUSPENDABLE_CALL_SITES_OFFSETS_AFTER_INSTR.equals(name)) ; // Ignore, we're filling it else throw new RuntimeException("Unexpected `@Instrumented` field: " + name); } }; } return super.visitAnnotation(adesc, visible); } @Override public void visitLocalVariable(String name, String desc, String sig, Label lStart, Label lEnd, int slot) { super.visitLocalVariable(name, desc, sig, lStart, lEnd, slot); } @Override public void visitLabel(Label label) { if (instrumented) { currLabel = label; } super.visitLabel(label); } @Override public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean isInterface) { if (instrumented) { final int type = AbstractInsnNode.METHOD_INSN; if (InstrumentMethod.isSuspendableCall(db, type, opcode, owner, name, desc) && !Classes.STACK_NAME.equals(owner) && // postRestore currLabel != null && currLabel.info instanceof Integer) addLine(); } super.visitMethodInsn(opcode, owner, name, desc, isInterface); } @Override public void visitInvokeDynamicInsn(String name, String desc, Handle handle, Object... objects) { if (instrumented) { final int type = AbstractInsnNode.INVOKE_DYNAMIC_INSN; final int opcode = Opcodes.INVOKEDYNAMIC; if (InstrumentMethod.isSuspendableCall(db, type, opcode, handle.getOwner(), name, desc) && !Classes.STACK_NAME.equals(handle.getOwner()) && // postRestore currLabel != null && currLabel.info instanceof Integer) addLine(); } super.visitInvokeDynamicInsn(name, desc, handle, objects); } @Override public void visitEnd() { if (instrumented) InstrumentMethod.emitInstrumentedAnn(db, outMV, mn, sourceName, className, optimized, methodStart, methodEnd, suspCallSites, toIntArray(suspOffsetsAfterInstrL)); super.visitEnd(); } private void addLine() { final int currOffset = (Integer) currLabel.info; if (currOffset > prevOffset) { suspOffsetsAfterInstrL.add(currOffset); prevOffset = currOffset; } } }; } return super.visitMethod(access, name, desc, signature, exceptions); }