Search in sources :

Example 1 with CodeAttribute

use of com.oracle.truffle.espresso.classfile.attributes.CodeAttribute in project graal by oracle.

the class Method method redefine.

public SharedRedefinitionContent redefine(ObjectKlass.KlassVersion klassVersion, ParserMethod newMethod, ParserKlass newKlass, Ids<Object> ids) {
    // install the new method version immediately
    LinkedMethod newLinkedMethod = new LinkedMethod(newMethod);
    RuntimeConstantPool runtimePool = new RuntimeConstantPool(getContext(), newKlass.getConstantPool(), getDeclaringKlass().getDefiningClassLoader());
    CodeAttribute newCodeAttribute = (CodeAttribute) newMethod.getAttribute(Name.Code);
    MethodVersion oldVersion = methodVersion;
    methodVersion = oldVersion.replace(klassVersion, runtimePool, newLinkedMethod, newCodeAttribute);
    ids.replaceObject(oldVersion, methodVersion);
    return new SharedRedefinitionContent(newLinkedMethod, runtimePool, newCodeAttribute);
}
Also used : RuntimeConstantPool(com.oracle.truffle.espresso.classfile.RuntimeConstantPool) CodeAttribute(com.oracle.truffle.espresso.classfile.attributes.CodeAttribute)

Example 2 with CodeAttribute

use of com.oracle.truffle.espresso.classfile.attributes.CodeAttribute in project graal by oracle.

the class ClassRedefinition method isObsolete.

private static boolean isObsolete(ParserMethod oldMethod, ParserMethod newMethod, ConstantPool oldPool, ConstantPool newPool) {
    CodeAttribute oldCodeAttribute = (CodeAttribute) oldMethod.getAttribute(Symbol.Name.Code);
    CodeAttribute newCodeAttribute = (CodeAttribute) newMethod.getAttribute(Symbol.Name.Code);
    if (oldCodeAttribute == null) {
        return newCodeAttribute != null;
    } else if (newCodeAttribute == null) {
        return oldCodeAttribute != null;
    }
    BytecodeStream oldCode = new BytecodeStream(oldCodeAttribute.getOriginalCode());
    BytecodeStream newCode = new BytecodeStream(newCodeAttribute.getOriginalCode());
    return !isSame(oldCode, oldPool, newCode, newPool);
}
Also used : CodeAttribute(com.oracle.truffle.espresso.classfile.attributes.CodeAttribute) BytecodeStream(com.oracle.truffle.espresso.bytecode.BytecodeStream)

Example 3 with CodeAttribute

use of com.oracle.truffle.espresso.classfile.attributes.CodeAttribute in project graal by oracle.

the class ClassRedefinition method detectMethodChanges.

private static ClassChange detectMethodChanges(ParserMethod oldMethod, ParserMethod newMethod) {
    // check code attribute
    CodeAttribute oldCodeAttribute = (CodeAttribute) oldMethod.getAttribute(Symbol.Name.Code);
    CodeAttribute newCodeAttribute = (CodeAttribute) newMethod.getAttribute(Symbol.Name.Code);
    if (oldCodeAttribute == null) {
        return newCodeAttribute != null ? ClassChange.METHOD_BODY_CHANGE : ClassChange.NO_CHANGE;
    } else if (newCodeAttribute == null) {
        return oldCodeAttribute != null ? ClassChange.METHOD_BODY_CHANGE : ClassChange.NO_CHANGE;
    }
    if (!Arrays.equals(oldCodeAttribute.getOriginalCode(), newCodeAttribute.getOriginalCode())) {
        return ClassChange.METHOD_BODY_CHANGE;
    }
    // check line number table
    if (checkLineNumberTable(oldCodeAttribute.getLineNumberTableAttribute(), newCodeAttribute.getLineNumberTableAttribute())) {
        return ClassChange.METHOD_BODY_CHANGE;
    }
    // check local variable table
    if (checkLocalVariableTable(oldCodeAttribute.getLocalvariableTable(), newCodeAttribute.getLocalvariableTable())) {
        return ClassChange.METHOD_BODY_CHANGE;
    }
    // check local variable type table
    if (checkLocalVariableTable(oldCodeAttribute.getLocalvariableTypeTable(), newCodeAttribute.getLocalvariableTypeTable())) {
        return ClassChange.METHOD_BODY_CHANGE;
    }
    return ClassChange.NO_CHANGE;
}
Also used : CodeAttribute(com.oracle.truffle.espresso.classfile.attributes.CodeAttribute)

Example 4 with CodeAttribute

use of com.oracle.truffle.espresso.classfile.attributes.CodeAttribute in project graal by oracle.

the class Method method swapMethodVersion.

public void swapMethodVersion(ObjectKlass.KlassVersion klassVersion, Ids<Object> ids) {
    MethodVersion oldVersion = methodVersion;
    CodeAttribute codeAttribute = oldVersion.getCodeAttribute();
    // create a copy of the code attribute using the original
    // code of the old version. An obsolete method could be
    // running quickened bytecode and we can't safely patch
    // the bytecodes back to the original.
    CodeAttribute newCodeAttribute = codeAttribute != null ? new CodeAttribute(codeAttribute) : null;
    methodVersion = oldVersion.replace(klassVersion, oldVersion.pool, oldVersion.linkedMethod, newCodeAttribute);
    ids.replaceObject(oldVersion, methodVersion);
}
Also used : CodeAttribute(com.oracle.truffle.espresso.classfile.attributes.CodeAttribute)

Example 5 with CodeAttribute

use of com.oracle.truffle.espresso.classfile.attributes.CodeAttribute in project graal by oracle.

the class ClassfileParser method parseMethod.

private ParserMethod parseMethod(boolean isInterface) {
    int methodFlags = stream.readU2();
    int nameIndex = stream.readU2();
    int signatureIndex = stream.readU2();
    final Symbol<Name> name;
    final Symbol<Signature> signature;
    int attributeCount;
    Attribute[] methodAttributes;
    int extraFlags = methodFlags;
    boolean isClinit = false;
    boolean isInit = false;
    try (DebugCloseable closeable = METHOD_INIT.scope(context.getTimers())) {
        try (DebugCloseable nameCheck = NAME_CHECK.scope(context.getTimers())) {
            pool.utf8At(nameIndex).validateMethodName(true);
            name = pool.symbolAt(nameIndex, "method name");
            if (name.equals(Name._clinit_)) {
                // ACC_STRICT flag.
                if (majorVersion < JAVA_7_VERSION) {
                    // Backwards compatibility.
                    methodFlags = ACC_STATIC;
                } else if ((methodFlags & ACC_STATIC) == ACC_STATIC) {
                    methodFlags &= (ACC_STRICT | ACC_STATIC);
                } else if (context.getJavaVersion().java9OrLater()) {
                    throw ConstantPool.classFormatError("Method <clinit> is not static.");
                }
                // extraFlags = INITIALIZER | methodFlags;
                isClinit = true;
            } else if (name.equals(Name._init_)) {
                if (isInterface) {
                    throw ConstantPool.classFormatError("Method <init> is not valid in an interface.");
                }
                isInit = true;
            }
        }
        final boolean isStatic = Modifier.isStatic(extraFlags);
        verifyMethodFlags(methodFlags, isInterface, isInit, isClinit, majorVersion);
        /*
             * A method is a class or interface initialization method if all of the following are
             * true:
             *
             * It has the special name <clinit>.
             *
             * It is void (4.3.3). (checked earlier)
             *
             * In a class file whose version number is 51.0 or above, the method has its ACC_STATIC
             * flag set and takes no arguments (4.6).
             */
        try (DebugCloseable signatureCheck = SIGNATURE_CHECK.scope(context.getTimers())) {
            // Checks for void method if init or clinit.
            /*
                 * Obtain slot number for the signature. Forces a validation, but better in startup
                 * than going twice through the sequence, once for validation, once for slots.
                 */
            int slots = pool.utf8At(signatureIndex).validateSignatureGetSlots(isInit || isClinit);
            signature = Signatures.check(pool.symbolAt(signatureIndex, "method descriptor"));
            if (isClinit && majorVersion >= JAVA_7_VERSION) {
                // Checks clinit takes no arguments.
                if (!signature.equals(Signature._void)) {
                    throw ConstantPool.classFormatError("Method <clinit> has invalid signature: " + signature);
                }
            }
            if (slots + (isStatic ? 0 : 1) > 255) {
                throw ConstantPool.classFormatError("Too many arguments in method signature: " + signature);
            }
            if (name.equals(Name.finalize) && signature.equals(Signature._void) && !Modifier.isStatic(methodFlags) && !Type.java_lang_Object.equals(classType)) {
                // This class has a finalizer method implementation (ignore for
                // java.lang.Object).
                classFlags |= ACC_FINALIZER;
            }
        }
        attributeCount = stream.readU2();
        methodAttributes = spawnAttributesArray(attributeCount);
    }
    CodeAttribute codeAttribute = null;
    Attribute checkedExceptions = null;
    Attribute runtimeVisibleAnnotations = null;
    CommonAttributeParser commonAttributeParser = new CommonAttributeParser(InfoType.Method);
    MethodParametersAttribute methodParameters = null;
    for (int i = 0; i < attributeCount; ++i) {
        final int attributeNameIndex = stream.readU2();
        final Symbol<Name> attributeName = pool.symbolAt(attributeNameIndex, "attribute name");
        final int attributeSize = stream.readS4();
        final int startPosition = stream.getPosition();
        if (attributeName.equals(Name.Code)) {
            if (codeAttribute != null) {
                throw ConstantPool.classFormatError("Duplicate Code attribute");
            }
            try (DebugCloseable code = CODE_PARSE.scope(context.getTimers())) {
                methodAttributes[i] = codeAttribute = parseCodeAttribute(attributeName);
            }
        } else if (attributeName.equals(Name.Exceptions)) {
            if (checkedExceptions != null) {
                throw ConstantPool.classFormatError("Duplicate Exceptions attribute");
            }
            methodAttributes[i] = checkedExceptions = parseExceptions(attributeName);
        } else if (attributeName.equals(Name.Synthetic)) {
            methodFlags |= ACC_SYNTHETIC;
            methodAttributes[i] = checkedExceptions = new Attribute(attributeName, null);
        } else if (majorVersion >= JAVA_1_5_VERSION) {
            if (attributeName.equals(Name.RuntimeVisibleAnnotations)) {
                if (runtimeVisibleAnnotations != null) {
                    throw ConstantPool.classFormatError("Duplicate RuntimeVisibleAnnotations attribute");
                }
                // Check if java.lang.invoke.LambdaForm.Compiled is present here.
                byte[] data = stream.readByteArray(attributeSize);
                ClassfileStream subStream = new ClassfileStream(data, this.classfile);
                int count = subStream.readU2();
                for (int j = 0; j < count; j++) {
                    int typeIndex = parseAnnotation(subStream);
                    Utf8Constant constant = pool.utf8At(typeIndex, "annotation type");
                    // Validation of the type is done at runtime by guest java code.
                    Symbol<Type> annotType = constant.value();
                    if (Type.java_lang_invoke_LambdaForm$Compiled.equals(annotType)) {
                        methodFlags |= ACC_LAMBDA_FORM_COMPILED;
                    } else if (Type.java_lang_invoke_LambdaForm$Hidden.equals(annotType) || Type.jdk_internal_vm_annotation_Hidden.equals(annotType)) {
                        methodFlags |= ACC_HIDDEN;
                    } else if (Type.sun_reflect_CallerSensitive.equals(annotType) || Type.jdk_internal_reflect_CallerSensitive.equals(annotType)) {
                        methodFlags |= ACC_CALLER_SENSITIVE;
                    }
                }
                methodAttributes[i] = runtimeVisibleAnnotations = new Attribute(attributeName, data);
            } else if (attributeName.equals(Name.MethodParameters)) {
                if (methodParameters != null) {
                    throw ConstantPool.classFormatError("Duplicate MethodParameters attribute");
                }
                methodAttributes[i] = methodParameters = parseMethodParameters(attributeName);
            } else {
                Attribute attr = commonAttributeParser.parseCommonAttribute(attributeName, attributeSize);
                // stream.skip(attributeSize);
                methodAttributes[i] = attr == null ? new Attribute(attributeName, stream.readByteArray(attributeSize)) : attr;
            }
        } else {
            // stream.skip(attributeSize);
            methodAttributes[i] = new Attribute(attributeName, stream.readByteArray(attributeSize));
        }
        final int distance = stream.getPosition() - startPosition;
        if (attributeSize != distance) {
            final String message = "Invalid attribute_length for " + attributeName + " attribute (reported " + attributeSize + " != parsed " + distance + ")";
            throw ConstantPool.classFormatError(message);
        }
    }
    if (Modifier.isAbstract(methodFlags) || Modifier.isNative(methodFlags)) {
        if (codeAttribute != null) {
            throw ConstantPool.classFormatError("Code attribute supplied for native or abstract method");
        }
    } else {
        if (codeAttribute == null) {
            throw ConstantPool.classFormatError("Missing Code attribute");
        }
    }
    if (classDefinitionInfo.isHidden()) {
        methodFlags |= ACC_HIDDEN;
    }
    return ParserMethod.create(methodFlags, name, signature, methodAttributes);
}
Also used : BootstrapMethodsAttribute(com.oracle.truffle.espresso.classfile.attributes.BootstrapMethodsAttribute) EnclosingMethodAttribute(com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute) StackMapTableAttribute(com.oracle.truffle.espresso.classfile.attributes.StackMapTableAttribute) NestHostAttribute(com.oracle.truffle.espresso.classfile.attributes.NestHostAttribute) SourceFileAttribute(com.oracle.truffle.espresso.classfile.attributes.SourceFileAttribute) SourceDebugExtensionAttribute(com.oracle.truffle.espresso.classfile.attributes.SourceDebugExtensionAttribute) PermittedSubclassesAttribute(com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute) LineNumberTableAttribute(com.oracle.truffle.espresso.classfile.attributes.LineNumberTableAttribute) ExceptionsAttribute(com.oracle.truffle.espresso.classfile.attributes.ExceptionsAttribute) CodeAttribute(com.oracle.truffle.espresso.classfile.attributes.CodeAttribute) NestMembersAttribute(com.oracle.truffle.espresso.classfile.attributes.NestMembersAttribute) ConstantValueAttribute(com.oracle.truffle.espresso.classfile.attributes.ConstantValueAttribute) MethodParametersAttribute(com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute) Attribute(com.oracle.truffle.espresso.runtime.Attribute) InnerClassesAttribute(com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute) RecordAttribute(com.oracle.truffle.espresso.classfile.attributes.RecordAttribute) SignatureAttribute(com.oracle.truffle.espresso.classfile.attributes.SignatureAttribute) Symbol(com.oracle.truffle.espresso.descriptors.Symbol) MethodParametersAttribute(com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) Utf8Constant(com.oracle.truffle.espresso.classfile.constantpool.Utf8Constant) CodeAttribute(com.oracle.truffle.espresso.classfile.attributes.CodeAttribute) Signature(com.oracle.truffle.espresso.descriptors.Symbol.Signature) DebugCloseable(com.oracle.truffle.espresso.perf.DebugCloseable)

Aggregations

CodeAttribute (com.oracle.truffle.espresso.classfile.attributes.CodeAttribute)6 BootstrapMethodsAttribute (com.oracle.truffle.espresso.classfile.attributes.BootstrapMethodsAttribute)2 ConstantValueAttribute (com.oracle.truffle.espresso.classfile.attributes.ConstantValueAttribute)2 EnclosingMethodAttribute (com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute)2 ExceptionsAttribute (com.oracle.truffle.espresso.classfile.attributes.ExceptionsAttribute)2 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)2 LineNumberTableAttribute (com.oracle.truffle.espresso.classfile.attributes.LineNumberTableAttribute)2 MethodParametersAttribute (com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute)2 NestHostAttribute (com.oracle.truffle.espresso.classfile.attributes.NestHostAttribute)2 NestMembersAttribute (com.oracle.truffle.espresso.classfile.attributes.NestMembersAttribute)2 PermittedSubclassesAttribute (com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute)2 RecordAttribute (com.oracle.truffle.espresso.classfile.attributes.RecordAttribute)2 SignatureAttribute (com.oracle.truffle.espresso.classfile.attributes.SignatureAttribute)2 SourceDebugExtensionAttribute (com.oracle.truffle.espresso.classfile.attributes.SourceDebugExtensionAttribute)2 SourceFileAttribute (com.oracle.truffle.espresso.classfile.attributes.SourceFileAttribute)2 StackMapTableAttribute (com.oracle.truffle.espresso.classfile.attributes.StackMapTableAttribute)2 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)2 DebugCloseable (com.oracle.truffle.espresso.perf.DebugCloseable)2 Attribute (com.oracle.truffle.espresso.runtime.Attribute)2 BytecodeStream (com.oracle.truffle.espresso.bytecode.BytecodeStream)1