Java Code Examples for org.objectweb.asm.MethodVisitor#visitFrame()

The following examples show how to use org.objectweb.asm.MethodVisitor#visitFrame() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FrameNode.java    From JByteMod-Beta with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Makes the given visitor visit this stack map frame.
 * 
 * @param mv
 *          a method visitor.
 */
@Override
public void accept(final MethodVisitor mv) {
  switch (type) {
  case Opcodes.F_NEW:
  case Opcodes.F_FULL:
    mv.visitFrame(type, local.size(), asArray(local), stack.size(), asArray(stack));
    break;
  case Opcodes.F_APPEND:
    mv.visitFrame(type, local.size(), asArray(local), 0, null);
    break;
  case Opcodes.F_CHOP:
    mv.visitFrame(type, local.size(), null, 0, null);
    break;
  case Opcodes.F_SAME:
    mv.visitFrame(type, 0, null, 0, null);
    break;
  case Opcodes.F_SAME1:
    mv.visitFrame(type, 0, null, 1, asArray(stack));
    break;
  }
}
 
Example 2
Source File: EqualsMethod.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
    methodVisitor.visitJumpInsn(Opcodes.GOTO, endOfBlock);
    methodVisitor.visitLabel(secondValueNull);
    if (implementationContext.getClassFileVersion().isAtLeast(ClassFileVersion.JAVA_V6)) {
        methodVisitor.visitFrame(Opcodes.F_SAME1, EMPTY.length, EMPTY, REFERENCE.length, REFERENCE);
    }
    methodVisitor.visitJumpInsn(Opcodes.IFNULL, endOfBlock);
    methodVisitor.visitLabel(firstValueNull);
    if (implementationContext.getClassFileVersion().isAtLeast(ClassFileVersion.JAVA_V6)) {
        methodVisitor.visitFrame(Opcodes.F_SAME, EMPTY.length, EMPTY, EMPTY.length, EMPTY);
    }
    methodVisitor.visitInsn(Opcodes.ICONST_0);
    methodVisitor.visitInsn(Opcodes.IRETURN);
    methodVisitor.visitLabel(endOfBlock);
    if (implementationContext.getClassFileVersion().isAtLeast(ClassFileVersion.JAVA_V6)) {
        methodVisitor.visitFrame(Opcodes.F_SAME, EMPTY.length, EMPTY, EMPTY.length, EMPTY);
    }
    return new Size(0, 0);
}
 
Example 3
Source File: WeavingClassVisitor.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private static void addLoadClassConditional(MethodVisitor mv, Object[] locals) {
    Label label0 = new Label();
    Label label1 = new Label();
    Label label2 = new Label();
    mv.visitTryCatchBlock(label0, label1, label2, "java/lang/ClassNotFoundException");
    mv.visitVarInsn(ALOAD, 1);
    mv.visitLdcInsn("org.glowroot.agent");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "startsWith", "(Ljava/lang/String;)Z",
            false);
    Label label3 = new Label();
    mv.visitJumpInsn(IFEQ, label3);
    mv.visitLabel(label0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(ACONST_NULL);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName",
            "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;", false);
    mv.visitLabel(label1);
    mv.visitInsn(ARETURN);
    mv.visitLabel(label2);
    mv.visitFrame(F_NEW, locals.length, locals, 1,
            new Object[] {"java/lang/ClassNotFoundException"});
    mv.visitInsn(POP);
    mv.visitLabel(label3);
    mv.visitFrame(F_NEW, locals.length, locals, 0, new Object[0]);
}
 
Example 4
Source File: FrameNode.java    From Cafebabe with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void accept(final MethodVisitor methodVisitor) {
	switch (type) {
	case Opcodes.F_NEW:
	case Opcodes.F_FULL:
		methodVisitor.visitFrame(type, local.size(), asArray(local), stack.size(), asArray(stack));
		break;
	case Opcodes.F_APPEND:
		methodVisitor.visitFrame(type, local.size(), asArray(local), 0, null);
		break;
	case Opcodes.F_CHOP:
		methodVisitor.visitFrame(type, local.size(), null, 0, null);
		break;
	case Opcodes.F_SAME:
		methodVisitor.visitFrame(type, 0, null, 0, null);
		break;
	case Opcodes.F_SAME1:
		methodVisitor.visitFrame(type, 0, null, 1, asArray(stack));
		break;
	default:
		throw new IllegalArgumentException();
	}
}
 
Example 5
Source File: FrameNode.java    From JReFrameworker with MIT License 6 votes vote down vote up
@Override
public void accept(final MethodVisitor methodVisitor) {
  switch (type) {
    case Opcodes.F_NEW:
    case Opcodes.F_FULL:
      methodVisitor.visitFrame(type, local.size(), asArray(local), stack.size(), asArray(stack));
      break;
    case Opcodes.F_APPEND:
      methodVisitor.visitFrame(type, local.size(), asArray(local), 0, null);
      break;
    case Opcodes.F_CHOP:
      methodVisitor.visitFrame(type, local.size(), null, 0, null);
      break;
    case Opcodes.F_SAME:
      methodVisitor.visitFrame(type, 0, null, 0, null);
      break;
    case Opcodes.F_SAME1:
      methodVisitor.visitFrame(type, 0, null, 1, asArray(stack));
      break;
    default:
      throw new IllegalArgumentException();
  }
}
 
Example 6
Source File: HashCodeMethod.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
    methodVisitor.visitLabel(label);
    if (implementationContext.getClassFileVersion().isAtLeast(ClassFileVersion.JAVA_V6)) {
        methodVisitor.visitFrame(Opcodes.F_SAME1, EMPTY.length, EMPTY, INTEGER.length, INTEGER);
    }
    return new Size(0, 0);
}
 
Example 7
Source File: AdviceInconsistentStackSizeTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    if (original != null) {
        methodVisitor.visitLdcInsn(original);
    }
    methodVisitor.visitInsn(opcode);
    methodVisitor.visitFrame(Opcodes.F_SAME, 0, new Object[0], 0, new Object[0]);
    if (original != null) {
        methodVisitor.visitLdcInsn(original);
        methodVisitor.visitLdcInsn(original);
    }
    methodVisitor.visitInsn(opcode);
    return new Size(StackSize.of(type).getSize() * 2, instrumentedMethod.getStackSize());
}
 
Example 8
Source File: GenerateMoreHackedConstructorBytecode.java    From glowroot with Apache License 2.0 5 votes vote down vote up
static LazyDefinedClass generateMoreHackedConstructorBytecode() throws Exception {

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        MethodVisitor mv;

        // 1.7+ since testing frames
        cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "MoreHackedConstructorBytecode", null,
                Test.class.getName().replace('.', '/'), new String[] {});

        {
            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
            mv.visitCode();
            Label trueLabel = new Label();
            mv.visitInsn(ICONST_0);
            mv.visitJumpInsn(IFEQ, trueLabel);
            mv.visitInsn(ACONST_NULL);
            mv.visitInsn(ATHROW);
            mv.visitLabel(trueLabel);
            mv.visitFrame(F_SAME, 0, null, 0, null);

            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKESPECIAL, Test.class.getName().replace('.', '/'), "<init>",
                    "()V", false);

            mv.visitInsn(RETURN);

            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
        cw.visitEnd();

        return ImmutableLazyDefinedClass.builder()
                .type(Type.getObjectType("MoreHackedConstructorBytecode"))
                .bytes(cw.toByteArray())
                .build();
    }
 
Example 9
Source File: AdviceInconsistentFrameTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    methodVisitor.visitFrame(Opcodes.F_FULL, 0, new Object[0], 0, new Object[0]);
    methodVisitor.visitLdcInsn(BAR);
    methodVisitor.visitInsn(Opcodes.ARETURN);
    return new Size(1, 2);
}
 
Example 10
Source File: AsmClassAccess.java    From jadira with Apache License 2.0 4 votes vote down vote up
private static void enhanceForGetValuePrimitive(ClassVisitor cw, String accessClassNm, String clazzNm, Field[] fields, Type type) {

		String methodName;
		final String typeNm = type.getDescriptor();
		final int instruction;

		switch (type.getSort()) {
		case Type.BOOLEAN:
			methodName = "getBooleanValue";
			instruction = IRETURN;
			break;
		case Type.BYTE:
			methodName = "getByteValue";
			instruction = IRETURN;
			break;
		case Type.CHAR:
			methodName = "getCharValue";
			instruction = IRETURN;
			break;
		case Type.SHORT:
			methodName = "getShortValue";
			instruction = IRETURN;
			break;
		case Type.INT:
			methodName = "getIntValue";
			instruction = IRETURN;
			break;
		case Type.FLOAT:
			methodName = "getFloatValue";
			instruction = FRETURN;
			break;
		case Type.LONG:
			methodName = "getLongValue";
			instruction = LRETURN;
			break;
		case Type.DOUBLE:
			methodName = "getDoubleValue";
			instruction = DRETURN;
			break;
		default:
			methodName = "getValue";
			instruction = ARETURN;
			break;
		}

		MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "(Ljava/lang/Object;Ljava/lang/String;)" + typeNm, null, null);

		mv.visitCode();
		mv.visitVarInsn(ALOAD, 0);
		mv.visitFieldInsn(GETFIELD, accessClassNm, "fieldNames", "[Ljava/lang/String;");
		mv.visitVarInsn(ALOAD, 2);
		mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "binarySearch", "([Ljava/lang/Object;Ljava/lang/Object;)I");
		mv.visitVarInsn(ISTORE, 3);
		mv.visitVarInsn(ILOAD, 3);

		final int maxStack;

		if (fields.length > 0) {
			maxStack = 5;
			Label[] labels = constructLabels(fields);

			Label defaultLabel = new Label();
			mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

			for (int i = 0, n = labels.length; i < n; i++) {
				Field field = fields[i];
				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, clazzNm);
				mv.visitFieldInsn(GETFIELD, clazzNm, field.getName(), typeNm);
				mv.visitInsn(instruction);
			}

			mv.visitLabel(defaultLabel);
			mv.visitFrame(F_SAME, 0, null, 0, null);
		} else {
			maxStack = 6;
		}
		enhanceForThrowingException(mv, IllegalArgumentException.class, "Field was not found", "Ljava/lang/Object;", ALOAD, 2);
		mv.visitMaxs(maxStack, 4);
		mv.visitEnd();
	}
 
Example 11
Source File: FieldAccess.java    From reflectasm with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static private void insertSetObject (ClassWriter cw, String classNameInternal, ArrayList<Field> fields) {
	int maxStack = 6;
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "set", "(Ljava/lang/Object;ILjava/lang/Object;)V", null, null);
	mv.visitCode();
	mv.visitVarInsn(ILOAD, 2);

	if (!fields.isEmpty()) {
		maxStack--;
		Label[] labels = new Label[fields.size()];
		for (int i = 0, n = labels.length; i < n; i++)
			labels[i] = new Label();
		Label defaultLabel = new Label();
		mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

		for (int i = 0, n = labels.length; i < n; i++) {
			Field field = fields.get(i);
			Type fieldType = Type.getType(field.getType());

			mv.visitLabel(labels[i]);
			mv.visitFrame(F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ALOAD, 1);
			mv.visitTypeInsn(CHECKCAST, classNameInternal);
			mv.visitVarInsn(ALOAD, 3);

			switch (fieldType.getSort()) {
			case Type.BOOLEAN:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
				break;
			case Type.BYTE:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Byte");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B");
				break;
			case Type.CHAR:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Character");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C");
				break;
			case Type.SHORT:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Short");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S");
				break;
			case Type.INT:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Integer");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I");
				break;
			case Type.FLOAT:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Float");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F");
				break;
			case Type.LONG:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Long");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J");
				break;
			case Type.DOUBLE:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Double");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D");
				break;
			case Type.ARRAY:
				mv.visitTypeInsn(CHECKCAST, fieldType.getDescriptor());
				break;
			case Type.OBJECT:
				mv.visitTypeInsn(CHECKCAST, fieldType.getInternalName());
				break;
			}

			mv.visitFieldInsn(PUTFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
				fieldType.getDescriptor());
			mv.visitInsn(RETURN);
		}

		mv.visitLabel(defaultLabel);
		mv.visitFrame(F_SAME, 0, null, 0, null);
	}
	mv = insertThrowExceptionForFieldNotFound(mv);
	mv.visitMaxs(maxStack, 4);
	mv.visitEnd();
}
 
Example 12
Source File: AsmClassAccess.java    From jadira with Apache License 2.0 4 votes vote down vote up
private static void enhanceForGetValueObject(ClassVisitor cw, String accessClassNm, String clazzNm, Field[] fields) {

		MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "getValue", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", null, null);

		mv.visitCode();
		mv.visitVarInsn(ALOAD, 0);
		mv.visitFieldInsn(GETFIELD, accessClassNm, "fieldNames", "[Ljava/lang/String;");
		mv.visitVarInsn(ALOAD, 2);
		mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "binarySearch", "([Ljava/lang/Object;Ljava/lang/Object;)I");
		mv.visitVarInsn(ISTORE, 3);
		mv.visitVarInsn(ILOAD, 3);

		final int maxStack;

		if (fields.length > 0) {
			maxStack = 5;
			Label[] labels = constructLabels(fields);

			Label defaultLabel = new Label();
			mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

			for (int i = 0, n = labels.length; i < n; i++) {
				Field field = fields[i];
				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, clazzNm);
				mv.visitFieldInsn(GETFIELD, clazzNm, field.getName(), Type.getDescriptor(field.getType()));

				Type fieldType = Type.getType(field.getType());
				switch (fieldType.getSort()) {
				case Type.BOOLEAN:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
					break;
				case Type.BYTE:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
					break;
				case Type.CHAR:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
					break;
				case Type.SHORT:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
					break;
				case Type.INT:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
					break;
				case Type.FLOAT:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
					break;
				case Type.LONG:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
					break;
				case Type.DOUBLE:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
					break;
				}

				mv.visitInsn(ARETURN);
			}

			mv.visitLabel(defaultLabel);
			mv.visitFrame(F_SAME, 0, null, 0, null);
		} else {
			maxStack = 6;
		}
		enhanceForThrowingException(mv, IllegalArgumentException.class, "Field was not found", "Ljava/lang/Object;", ALOAD, 2);
		mv.visitMaxs(maxStack, 4);
		mv.visitEnd();
	}
 
Example 13
Source File: FieldAccess.java    From reflectasm with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static private void insertSetPrimitive (ClassWriter cw, String classNameInternal, ArrayList<Field> fields,
	Type primitiveType) {
	int maxStack = 6;
	int maxLocals = 4; // See correction below for LLOAD and DLOAD
	final String setterMethodName;
	final String typeNameInternal = primitiveType.getDescriptor();
	final int loadValueInstruction;
	switch (primitiveType.getSort()) {
	case Type.BOOLEAN:
		setterMethodName = "setBoolean";
		loadValueInstruction = ILOAD;
		break;
	case Type.BYTE:
		setterMethodName = "setByte";
		loadValueInstruction = ILOAD;
		break;
	case Type.CHAR:
		setterMethodName = "setChar";
		loadValueInstruction = ILOAD;
		break;
	case Type.SHORT:
		setterMethodName = "setShort";
		loadValueInstruction = ILOAD;
		break;
	case Type.INT:
		setterMethodName = "setInt";
		loadValueInstruction = ILOAD;
		break;
	case Type.FLOAT:
		setterMethodName = "setFloat";
		loadValueInstruction = FLOAD;
		break;
	case Type.LONG:
		setterMethodName = "setLong";
		loadValueInstruction = LLOAD;
		maxLocals++; // (LLOAD and DLOAD actually load two slots)
		break;
	case Type.DOUBLE:
		setterMethodName = "setDouble";
		loadValueInstruction = DLOAD;
		maxLocals++; // (LLOAD and DLOAD actually load two slots)
		break;
	default:
		setterMethodName = "set";
		loadValueInstruction = ALOAD;
		break;
	}
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, setterMethodName, "(Ljava/lang/Object;I" + typeNameInternal + ")V", null,
		null);
	mv.visitCode();
	mv.visitVarInsn(ILOAD, 2);

	if (!fields.isEmpty()) {
		maxStack--;
		Label[] labels = new Label[fields.size()];
		Label labelForInvalidTypes = new Label();
		boolean hasAnyBadTypeLabel = false;
		for (int i = 0, n = labels.length; i < n; i++) {
			if (Type.getType(fields.get(i).getType()).equals(primitiveType))
				labels[i] = new Label();
			else {
				labels[i] = labelForInvalidTypes;
				hasAnyBadTypeLabel = true;
			}
		}
		Label defaultLabel = new Label();
		mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

		for (int i = 0, n = labels.length; i < n; i++) {
			if (!labels[i].equals(labelForInvalidTypes)) {
				Field field = fields.get(i);
				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, classNameInternal);
				mv.visitVarInsn(loadValueInstruction, 3);
				mv.visitFieldInsn(PUTFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
					typeNameInternal);
				mv.visitInsn(RETURN);
			}
		}
		// Rest of fields: different type
		if (hasAnyBadTypeLabel) {
			mv.visitLabel(labelForInvalidTypes);
			mv.visitFrame(F_SAME, 0, null, 0, null);
			insertThrowExceptionForFieldType(mv, primitiveType.getClassName());
		}
		// Default: field not found
		mv.visitLabel(defaultLabel);
		mv.visitFrame(F_SAME, 0, null, 0, null);
	}
	mv = insertThrowExceptionForFieldNotFound(mv);
	mv.visitMaxs(maxStack, maxLocals);
	mv.visitEnd();
}
 
Example 14
Source File: AdviceInconsistentFrameTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    methodVisitor.visitFrame(Opcodes.F_FULL, 1, new Object[]{TypeDescription.OBJECT.getInternalName()}, 0, new Object[0]);
    methodVisitor.visitLdcInsn(BAR);
    methodVisitor.visitInsn(Opcodes.ARETURN);
    return new Size(1, 2);
}
 
Example 15
Source File: AdviceInconsistentFrameTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    methodVisitor.visitFrame(Opcodes.F_FULL, 1, new Object[]{TypeDescription.OBJECT.getInternalName()}, 0, new Object[0]);
    methodVisitor.visitLdcInsn(BAR);
    methodVisitor.visitInsn(Opcodes.ARETURN);
    return new Size(1, 2);
}
 
Example 16
Source File: UnbalancedMonitorsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void visitWrongOrder(ClassWriter cw) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "wrongOrder", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, null);
    Label l3 = new Label();
    mv.visitTryCatchBlock(l2, l3, l2, null);
    Label l4 = new Label();
    Label l5 = new Label();
    Label l6 = new Label();
    mv.visitTryCatchBlock(l4, l5, l6, null);
    Label l7 = new Label();
    mv.visitTryCatchBlock(l2, l7, l6, null);
    Label l8 = new Label();
    mv.visitLabel(l8);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ASTORE, 3);
    mv.visitInsn(MONITORENTER);
    mv.visitLabel(l4);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ASTORE, 4);
    mv.visitInsn(MONITORENTER);
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitInsn(MONITOREXIT);
    mv.visitLabel(l1);
    // Swapped exit order with exit above
    mv.visitVarInsn(ALOAD, 4);
    mv.visitInsn(MONITOREXIT);
    mv.visitLabel(l5);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_FULL, 5, new Object[]{INNER_CLASS_NAME_INTERNAL, "java/lang/Object", "java/lang/Object", "java/lang/Object",
                    "java/lang/Object"}, 1, new Object[]{"java/lang/Throwable"});
    mv.visitVarInsn(ALOAD, 4);
    mv.visitInsn(MONITOREXIT);
    mv.visitLabel(l3);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l6);
    mv.visitFrame(Opcodes.F_FULL, 4, new Object[]{INNER_CLASS_NAME_INTERNAL, "java/lang/Object", "java/lang/Object", "java/lang/Object"}, 1,
                    new Object[]{"java/lang/Throwable"});
    mv.visitVarInsn(ALOAD, 3);
    mv.visitInsn(MONITOREXIT);
    mv.visitLabel(l7);
    mv.visitInsn(ATHROW);
    Label l9 = new Label();
    mv.visitLabel(l9);
    mv.visitMaxs(2, 5);
    mv.visitEnd();
}
 
Example 17
Source File: FieldAccess.java    From reflectasm with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static private void insertGetObject (ClassWriter cw, String classNameInternal, ArrayList<Field> fields) {
	int maxStack = 6;
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "get", "(Ljava/lang/Object;I)Ljava/lang/Object;", null, null);
	mv.visitCode();
	mv.visitVarInsn(ILOAD, 2);

	if (!fields.isEmpty()) {
		maxStack--;
		Label[] labels = new Label[fields.size()];
		for (int i = 0, n = labels.length; i < n; i++)
			labels[i] = new Label();
		Label defaultLabel = new Label();
		mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

		for (int i = 0, n = labels.length; i < n; i++) {
			Field field = fields.get(i);

			mv.visitLabel(labels[i]);
			mv.visitFrame(F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ALOAD, 1);
			mv.visitTypeInsn(CHECKCAST, classNameInternal);
			mv.visitFieldInsn(GETFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
				Type.getDescriptor(field.getType()));

			Type fieldType = Type.getType(field.getType());
			switch (fieldType.getSort()) {
			case Type.BOOLEAN:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
				break;
			case Type.BYTE:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
				break;
			case Type.CHAR:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
				break;
			case Type.SHORT:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
				break;
			case Type.INT:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
				break;
			case Type.FLOAT:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
				break;
			case Type.LONG:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
				break;
			case Type.DOUBLE:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
				break;
			}

			mv.visitInsn(ARETURN);
		}

		mv.visitLabel(defaultLabel);
		mv.visitFrame(F_SAME, 0, null, 0, null);
	}
	insertThrowExceptionForFieldNotFound(mv);
	mv.visitMaxs(maxStack, 3);
	mv.visitEnd();
}
 
Example 18
Source File: BytecodeVerificationTest.java    From AVM with MIT License 4 votes vote down vote up
@Test
public void testMaxStackSizeWithLoop() throws Exception {
    int originalHash = 1;

    TestResource original = new TestResource(originalHash);
    ClassRewriter.IMethodReplacer replacer = new ClassRewriter.IMethodReplacer() {
        @Override
        public void populatMethod(MethodVisitor visitor) {
            visitor.visitCode();
            Label start = new Label();
            Object[] newstack = new Object[1];
            newstack[0] = Opcodes.INTEGER;

            visitor.visitLabel(start);
            //visitor.visitFrame(Opcodes.F_FULL, 0, null, 1, newstack);
            visitor.visitFrame(Opcodes.F_FULL, 0, null, 0, null);
            visitor.visitVarInsn(Opcodes.BIPUSH, 0);
            visitor.visitJumpInsn(Opcodes.GOTO, start);
            visitor.visitInsn(Opcodes.IRETURN);
            visitor.visitMaxs(100, 100);
            visitor.visitEnd();
        }};

    String className = original.getClass().getName();
    byte[] raw = Utilities.loadRequiredResourceAsBytes(className.replaceAll("\\.", "/") + ".class");
    byte[] rewrittten = ClassRewriter.rewriteOneMethodInClass(raw, "hashCode", replacer, 0);
    
    Map<String, byte[]> classes = new HashMap<>();
    classes.put(className, rewrittten);
    AvmClassLoader loader = NodeEnvironment.singleton.createInvocationClassLoader(classes);
    Class<?> clazz = loader.loadClass(className);

    try{
        Object target = clazz.getConstructor(int.class).newInstance(Integer.valueOf(originalHash));
        target.hashCode();
        Assert.assertEquals(1,0);
    }catch (Error e){
        Boolean expectedError =  e.getMessage().contains("Inconsistent stackmap frames at branch target") ? true : false;
        Assert.assertEquals(expectedError, true);
    }
}
 
Example 19
Source File: FMLPatcher.java    From Launcher with GNU General Public License v3.0 4 votes vote down vote up
private static byte[] gen(final String name, final String exName) { // "cpw/mods/fml/SafeExitJVMLegacy", "exit"

        final ClassWriter classWriter = new ClassWriter(0);
        MethodVisitor methodVisitor;

        classWriter.visit(V1_8, ACC_PUBLIC | ACC_SUPER, name, null, "java/lang/Object", null);

        {
            methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
            methodVisitor.visitCode();
            methodVisitor.visitVarInsn(ALOAD, 0);
            methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
            methodVisitor.visitInsn(RETURN);
            methodVisitor.visitMaxs(1, 1);
            methodVisitor.visitEnd();
        }
        {
            methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, exName, "(I)V", null, null);
            methodVisitor.visitCode();
            final Label label0 = new Label();
            final Label label1 = new Label();
            final Label label2 = new Label();
            methodVisitor.visitTryCatchBlock(label0, label1, label2, "java/lang/Throwable");
            methodVisitor.visitLabel(label0);
            methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/Runtime", "getRuntime", "()Ljava/lang/Runtime;",
                    false);
            methodVisitor.visitVarInsn(ILOAD, 0);
            methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Runtime", "halt", "(I)V", false);
            methodVisitor.visitLabel(label1);
            final Label label3 = new Label();
            methodVisitor.visitJumpInsn(GOTO, label3);
            methodVisitor.visitLabel(label2);
            methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[]{"java/lang/Throwable"});
            methodVisitor.visitVarInsn(ASTORE, 1);
            methodVisitor.visitVarInsn(ILOAD, 0);
            methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/System", "exit", "(I)V", false);
            methodVisitor.visitLabel(label3);
            methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
            methodVisitor.visitInsn(RETURN);
            methodVisitor.visitMaxs(2, 2);
            methodVisitor.visitEnd();
        }
        classWriter.visitEnd();
        return classWriter.toByteArray();
    }
 
Example 20
Source File: FieldAccess.java    From reflectasm with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static private void insertGetPrimitive (ClassWriter cw, String classNameInternal, ArrayList<Field> fields,
	Type primitiveType) {
	int maxStack = 6;
	final String getterMethodName;
	final String typeNameInternal = primitiveType.getDescriptor();
	final int returnValueInstruction;
	switch (primitiveType.getSort()) {
	case Type.BOOLEAN:
		getterMethodName = "getBoolean";
		returnValueInstruction = IRETURN;
		break;
	case Type.BYTE:
		getterMethodName = "getByte";
		returnValueInstruction = IRETURN;
		break;
	case Type.CHAR:
		getterMethodName = "getChar";
		returnValueInstruction = IRETURN;
		break;
	case Type.SHORT:
		getterMethodName = "getShort";
		returnValueInstruction = IRETURN;
		break;
	case Type.INT:
		getterMethodName = "getInt";
		returnValueInstruction = IRETURN;
		break;
	case Type.FLOAT:
		getterMethodName = "getFloat";
		returnValueInstruction = FRETURN;
		break;
	case Type.LONG:
		getterMethodName = "getLong";
		returnValueInstruction = LRETURN;
		break;
	case Type.DOUBLE:
		getterMethodName = "getDouble";
		returnValueInstruction = DRETURN;
		break;
	default:
		getterMethodName = "get";
		returnValueInstruction = ARETURN;
		break;
	}
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, getterMethodName, "(Ljava/lang/Object;I)" + typeNameInternal, null, null);
	mv.visitCode();
	mv.visitVarInsn(ILOAD, 2);

	if (!fields.isEmpty()) {
		maxStack--;
		Label[] labels = new Label[fields.size()];
		Label labelForInvalidTypes = new Label();
		boolean hasAnyBadTypeLabel = false;
		for (int i = 0, n = labels.length; i < n; i++) {
			if (Type.getType(fields.get(i).getType()).equals(primitiveType))
				labels[i] = new Label();
			else {
				labels[i] = labelForInvalidTypes;
				hasAnyBadTypeLabel = true;
			}
		}
		Label defaultLabel = new Label();
		mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

		for (int i = 0, n = labels.length; i < n; i++) {
			Field field = fields.get(i);
			if (!labels[i].equals(labelForInvalidTypes)) {
				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, classNameInternal);
				mv.visitFieldInsn(GETFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
					typeNameInternal);
				mv.visitInsn(returnValueInstruction);
			}
		}
		// Rest of fields: different type
		if (hasAnyBadTypeLabel) {
			mv.visitLabel(labelForInvalidTypes);
			mv.visitFrame(F_SAME, 0, null, 0, null);
			insertThrowExceptionForFieldType(mv, primitiveType.getClassName());
		}
		// Default: field not found
		mv.visitLabel(defaultLabel);
		mv.visitFrame(F_SAME, 0, null, 0, null);
	}
	mv = insertThrowExceptionForFieldNotFound(mv);
	mv.visitMaxs(maxStack, 3);
	mv.visitEnd();
}