Search in sources :

Example 56 with Klass

use of com.oracle.truffle.espresso.impl.Klass in project graal by oracle.

the class EspressoInstrumentableNode method getScope.

@ExportMessage
@TruffleBoundary
@SuppressWarnings("static-method")
public final Object getScope(Frame frame, @SuppressWarnings("unused") boolean nodeEnter) {
    // construct the current scope with valid local variables information
    Method method = getMethod();
    Local[] liveLocals = method.getLocalVariableTable().getLocalsAt(getBci(frame));
    if (liveLocals.length == 0) {
        // class was compiled without a local variable table
        // include "this" in method arguments throughout the method
        boolean hasReceiver = !method.isStatic();
        int localCount = hasReceiver ? 1 : 0;
        localCount += method.getParameterCount();
        liveLocals = new Local[localCount];
        Klass[] parameters = (Klass[]) method.getParameters();
        Utf8ConstantTable utf8Constants = getContext().getLanguage().getUtf8ConstantTable();
        int startslot = 0;
        if (hasReceiver) {
            // include 'this' and method arguments
            liveLocals[0] = new Local(utf8Constants.getOrCreate(Symbol.Name.thiz), utf8Constants.getOrCreate(method.getDeclaringKlass().getType()), 0, 65536, 0);
            startslot++;
        }
        // include method parameters
        for (int i = startslot; i < localCount; i++) {
            Klass param = hasReceiver ? parameters[i - 1] : parameters[i];
            liveLocals[i] = new Local(utf8Constants.getOrCreate(ByteSequence.create("param_" + (i))), utf8Constants.getOrCreate(param.getType()), 0, 65536, i);
        }
    }
    return EspressoScope.createVariables(liveLocals, frame, method.getName());
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) Local(com.oracle.truffle.espresso.classfile.attributes.Local) Method(com.oracle.truffle.espresso.impl.Method) Utf8ConstantTable(com.oracle.truffle.espresso.descriptors.Utf8ConstantTable) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 57 with Klass

use of com.oracle.truffle.espresso.impl.Klass in project graal by oracle.

the class JniEnv method GetStaticFieldID.

/**
 * <h3>jfieldID GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig);
 * </h3>
 * <p>
 * Returns the field ID for a static field of a class. The field is specified by its name and
 * signature. The GetStatic<type>Field and SetStatic<type>Field families of accessor functions
 * use field IDs to retrieve static fields.
 * <p>
 * GetStaticFieldID() causes an uninitialized class to be initialized.
 *
 * @param clazz a Java class object.
 * @param namePtr the static field name in a 0-terminated modified UTF-8 string.
 * @param typePtr the field signature in a 0-terminated modified UTF-8 string.
 * @return a field ID, or NULL if the specified static field cannot be found.
 * @throws NoSuchFieldError if the specified static field cannot be found.
 * @throws ExceptionInInitializerError if the class initializer fails due to an exception.
 * @throws OutOfMemoryError if the system runs out of memory.
 */
@JniImpl
@Handle(Field.class)
public long GetStaticFieldID(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject namePtr, @Pointer TruffleObject typePtr) {
    String name = NativeUtils.interopPointerToString(namePtr);
    String type = NativeUtils.interopPointerToString(typePtr);
    assert name != null && type != null;
    Field field = null;
    Symbol<Name> fieldName = getNames().lookup(name);
    if (fieldName != null) {
        Symbol<Type> fieldType = getTypes().lookup(type);
        if (fieldType != null) {
            Klass klass = clazz.getMirrorKlass();
            klass.safeInitialize();
            // Lookup only if name and type are known symbols.
            field = klass.lookupField(fieldName, fieldType, Klass.LookupMode.STATIC_ONLY);
            assert field == null || field.getType().equals(fieldType);
        }
    }
    if (field == null || !field.isStatic()) {
        Meta meta = getMeta();
        throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchFieldError, name);
    }
    return fieldIds.handlify(field);
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) Meta(com.oracle.truffle.espresso.meta.Meta) NativeType(com.oracle.truffle.espresso.ffi.NativeType) Type(com.oracle.truffle.espresso.descriptors.Symbol.Type) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name)

Example 58 with Klass

use of com.oracle.truffle.espresso.impl.Klass in project graal by oracle.

the class JniEnv method GetStaticMethodID.

/**
 * <h3>jmethodID GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name, const char
 * *sig);</h3>
 * <p>
 * Returns the method ID for a static method of a class. The method is specified by its name and
 * signature.
 * <p>
 * GetStaticMethodID() causes an uninitialized class to be initialized.
 *
 * @param clazz a Java class object.
 * @param namePtr the static method name in a 0-terminated modified UTF-8 string.
 * @param signaturePtr the method signature in a 0-terminated modified UTF-8 string.
 * @return a method ID, or NULL if the operation fails.
 * @throws NoSuchMethodError if the specified static method cannot be found. *
 * @throws ExceptionInInitializerError if the class initializer fails due to an exception.
 * @throws OutOfMemoryError if the system runs out of memory.
 */
@JniImpl
@Handle(Method.class)
public long GetStaticMethodID(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject namePtr, @Pointer TruffleObject signaturePtr) {
    String name = NativeUtils.interopPointerToString(namePtr);
    String signature = NativeUtils.interopPointerToString(signaturePtr);
    assert name != null && signature != null;
    Method method = null;
    Symbol<Name> methodName = getNames().lookup(name);
    if (methodName != null) {
        Symbol<Signature> methodSignature = getSignatures().lookupValidSignature(signature);
        if (methodSignature != null) {
            // Throw a NoSuchMethodError exception if we have an instance of a
            // primitive java.lang.Class
            Klass klass = clazz.getMirrorKlass();
            if (klass.isPrimitive()) {
                Meta meta = getMeta();
                throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, name);
            }
            klass.safeInitialize();
            // Lookup only if name and type are known symbols.
            if (Name._clinit_.equals(methodName)) {
                // Never search superclasses for static initializers.
                method = klass.lookupDeclaredMethod(methodName, methodSignature);
            } else {
                method = klass.lookupMethod(methodName, methodSignature);
            }
        }
    }
    if (method == null || !method.isStatic()) {
        Meta meta = getMeta();
        throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, name);
    }
    return methodIds.handlify(method);
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) NativeSignature(com.oracle.truffle.espresso.ffi.NativeSignature) Signature(com.oracle.truffle.espresso.descriptors.Symbol.Signature) Method(com.oracle.truffle.espresso.impl.Method) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name)

Example 59 with Klass

use of com.oracle.truffle.espresso.impl.Klass in project graal by oracle.

the class MethodVerifier method checkProtectedMember.

// Helper methods
private void checkProtectedMember(Operand stackOp, Symbol<Type> holderType, MemberRefConstant mrc, boolean method) {
    /*
         * 4.10.1.8.
         *
         * If the name of a class is not the name of any superclass, it cannot be a superclass, and
         * so it can safely be ignored.
         */
    if (stackOp.getType() == thisKlass.getType()) {
        return;
    }
    /*
         * If the MemberClassName is the same as the name of a superclass, the class being resolved
         * may indeed be a superclass. In this case, if no superclass named MemberClassName in a
         * different run-time package has a protected member named MemberName with descriptor
         * MemberDescriptor, the protected check does not apply.
         */
    Klass superKlass = thisKlass.getSuperKlass();
    while (superKlass != null) {
        if (superKlass.getType() == holderType) {
            Operand holderOp = kindToOperand(holderType);
            Member<?> member;
            if (method) {
                /* Non-failing method lookup. */
                member = holderOp.getKlass().lookupMethod(mrc.getName(pool), ((MethodRefConstant) mrc).getSignature(pool));
            } else {
                /* Non-failing field lookup. */
                member = holderOp.getKlass().lookupField(mrc.getName(pool), ((FieldRefConstant) mrc).getType(pool));
            }
            /*
                 * If there does exist a protected superclass member in a different run-time
                 * package, then load MemberClassName; if the member in question is not protected,
                 * the check does not apply. (Using a superclass member that is not protected is
                 * trivially correct.)
                 */
            if (member == null || !member.isProtected()) {
                return;
            }
            if (!thisKlass.getRuntimePackage().contentEquals(Types.getRuntimePackage(holderType))) {
                if (method) {
                    if (stackOp.isArrayType() && Type.java_lang_Object.equals(holderType) && Name.clone.equals(member.getName())) {
                        // Special case: Arrays pretend to implement Object.clone().
                        return;
                    }
                }
                verifyGuarantee(stackOp.compliesWith(thisOperand), "Illegal protected field access");
            }
            return;
        }
        superKlass = superKlass.getSuperKlass();
    }
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) FieldRefConstant(com.oracle.truffle.espresso.classfile.constantpool.FieldRefConstant) MethodRefConstant(com.oracle.truffle.espresso.classfile.constantpool.MethodRefConstant)

Example 60 with Klass

use of com.oracle.truffle.espresso.impl.Klass in project graal by oracle.

the class MethodVerifier method validateExceptionHandlers.

private void validateExceptionHandlers() {
    verifyGuarantee(exceptionHandlers.length == 0 || maxStack >= 1, "Method with exception handlers has a zero max stack value.");
    for (ExceptionHandler handler : exceptionHandlers) {
        validateFormatBCI(handler.getHandlerBCI());
        int startBCI = handler.getStartBCI();
        validateFormatBCI(startBCI);
        int endBCI = handler.getEndBCI();
        formatGuarantee(endBCI > startBCI, "End BCI of handler is before start BCI");
        formatGuarantee(endBCI >= 0, "negative branch target: " + endBCI);
        // handler end BCI can be equal to code end.
        formatGuarantee(endBCI <= code.endBCI(), "Control flow falls through code end");
        if (handler.catchTypeCPI() != 0) {
            Klass catchType = pool.resolvedKlassAt(thisKlass, handler.catchTypeCPI());
            verifyGuarantee(getMeta().java_lang_Throwable.isAssignableFrom(catchType), "Illegal exception handler catch type: " + catchType);
        }
        if (endBCI != code.endBCI()) {
            formatGuarantee(bciStates[endBCI] != UNREACHABLE, "Jump to the middle of an instruction: " + endBCI);
        }
    }
}
Also used : ExceptionHandler(com.oracle.truffle.espresso.meta.ExceptionHandler) Klass(com.oracle.truffle.espresso.impl.Klass)

Aggregations

Klass (com.oracle.truffle.espresso.impl.Klass)71 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)54 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)49 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)33 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)24 Meta (com.oracle.truffle.espresso.meta.Meta)21 Method (com.oracle.truffle.espresso.impl.Method)19 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)10 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)10 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)10 ExportMessage (com.oracle.truffle.api.library.ExportMessage)8 Field (com.oracle.truffle.espresso.impl.Field)7 ArrayList (java.util.ArrayList)7 RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)6 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)6 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)5 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)5 NativeType (com.oracle.truffle.espresso.ffi.NativeType)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 EnclosingMethodAttribute (com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute)3