Search in sources :

Example 51 with Method

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

the class InterpreterToVM method fillInStackTrace.

/**
 * Preemptively added method to benefit from truffle lazy stack traces when they will be
 * reworked.
 */
@SuppressWarnings("unused")
public static StaticObject fillInStackTrace(@JavaType(Throwable.class) StaticObject throwable, Meta meta) {
    // Inlined calls to help StackOverflows.
    VM.StackTrace frames = (VM.StackTrace) meta.HIDDEN_FRAMES.getHiddenObject(throwable);
    if (frames != null) {
        return throwable;
    }
    EspressoException e = EspressoException.wrap(throwable, meta);
    List<TruffleStackTraceElement> trace = TruffleStackTrace.getStackTrace(e);
    if (trace == null) {
        meta.HIDDEN_FRAMES.setHiddenObject(throwable, VM.StackTrace.EMPTY_STACK_TRACE);
        meta.java_lang_Throwable_backtrace.setObject(throwable, throwable);
        return throwable;
    }
    int bci = -1;
    Method m = null;
    frames = new VM.StackTrace();
    FrameCounter c = new FrameCounter();
    for (TruffleStackTraceElement element : trace) {
        Node location = element.getLocation();
        while (location != null) {
            if (location instanceof QuickNode) {
                bci = ((QuickNode) location).getBci(element.getFrame());
                break;
            }
            location = location.getParent();
        }
        RootCallTarget target = element.getTarget();
        if (target != null) {
            RootNode rootNode = target.getRootNode();
            if (rootNode instanceof EspressoRootNode) {
                m = ((EspressoRootNode) rootNode).getMethod();
                if (c.checkFillIn(m) || c.checkThrowableInit(m)) {
                    bci = UNKNOWN_BCI;
                    continue;
                }
                if (m.isNative()) {
                    bci = NATIVE_BCI;
                }
                frames.add(new VM.StackElement(m, bci));
                bci = UNKNOWN_BCI;
            }
        }
    }
    meta.HIDDEN_FRAMES.setHiddenObject(throwable, frames);
    meta.java_lang_Throwable_backtrace.setObject(throwable, throwable);
    return throwable;
}
Also used : EspressoRootNode(com.oracle.truffle.espresso.nodes.EspressoRootNode) RootNode(com.oracle.truffle.api.nodes.RootNode) TruffleStackTrace(com.oracle.truffle.api.TruffleStackTrace) TruffleStackTraceElement(com.oracle.truffle.api.TruffleStackTraceElement) EspressoRootNode(com.oracle.truffle.espresso.nodes.EspressoRootNode) QuickNode(com.oracle.truffle.espresso.nodes.quick.QuickNode) RootNode(com.oracle.truffle.api.nodes.RootNode) Node(com.oracle.truffle.api.nodes.Node) BytecodeNode(com.oracle.truffle.espresso.nodes.BytecodeNode) Method(com.oracle.truffle.espresso.impl.Method) EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) QuickNode(com.oracle.truffle.espresso.nodes.quick.QuickNode) EspressoRootNode(com.oracle.truffle.espresso.nodes.EspressoRootNode) RootCallTarget(com.oracle.truffle.api.RootCallTarget)

Example 52 with Method

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

the class VM method JVM_ClassDepth.

@VmImpl(isJni = true)
@TruffleBoundary
public int JVM_ClassDepth(@JavaType(String.class) StaticObject name) {
    Symbol<Name> className = getContext().getNames().lookup(getMeta().toHostString(name).replace('.', '/'));
    if (className == null) {
        return -1;
    }
    Integer res = Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Integer>() {

        int depth = 0;

        @Override
        public Integer visitFrame(FrameInstance frameInstance) {
            Method m = getMethodFromFrame(frameInstance);
            if (m != null) {
                if (className.equals(m.getDeclaringKlass().getName())) {
                    return depth;
                }
                depth++;
            }
            return null;
        }
    });
    return res == null ? -1 : res;
}
Also used : Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 53 with Method

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

the class VM method JVM_GetClassDeclaredConstructors.

// TODO(tg): inject constructor calltarget.
@VmImpl(isJni = true)
@JavaType(Constructor[].class)
public StaticObject JVM_GetClassDeclaredConstructors(@JavaType(Class.class) StaticObject self, boolean publicOnly) {
    Meta meta = getMeta();
    ArrayList<Method> collectedMethods = new ArrayList<>();
    Klass klass = self.getMirrorKlass();
    klass.ensureLinked();
    for (Method m : klass.getDeclaredConstructors()) {
        if (Name._init_.equals(m.getName()) && (!publicOnly || m.isPublic())) {
            collectedMethods.add(m);
        }
    }
    final Method[] constructors = collectedMethods.toArray(Method.EMPTY_ARRAY);
    EspressoContext context = meta.getContext();
    // TODO(peterssen): Cache guest j.l.reflect.Constructor constructor.
    // Calling the constructor is just for validation, manually setting the fields would be
    // faster.
    Method constructorInit = meta.java_lang_reflect_Constructor.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
    Type.java_lang_Class, /* parameterTypes */
    Type.java_lang_Class_array, /* checkedExceptions */
    Type.java_lang_Class_array, /* modifiers */
    Type._int, /* slot */
    Type._int, /* signature */
    Type.java_lang_String, /* annotations */
    Type._byte_array, /* parameterAnnotations */
    Type._byte_array));
    StaticObject arr = meta.java_lang_reflect_Constructor.allocateReferenceArray(constructors.length, new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int i) {
            final Method m = constructors[i];
            Attribute rawRuntimeVisibleAnnotations = m.getAttribute(Name.RuntimeVisibleAnnotations);
            StaticObject runtimeVisibleAnnotations = rawRuntimeVisibleAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleAnnotations.getData(), meta) : StaticObject.NULL;
            Attribute rawRuntimeVisibleParameterAnnotations = m.getAttribute(Name.RuntimeVisibleParameterAnnotations);
            StaticObject runtimeVisibleParameterAnnotations = rawRuntimeVisibleParameterAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleParameterAnnotations.getData(), meta) : StaticObject.NULL;
            Attribute rawRuntimeVisibleTypeAnnotations = m.getAttribute(Name.RuntimeVisibleTypeAnnotations);
            StaticObject runtimeVisibleTypeAnnotations = rawRuntimeVisibleTypeAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleTypeAnnotations.getData(), meta) : StaticObject.NULL;
            final Klass[] rawParameterKlasses = m.resolveParameterKlasses();
            StaticObject parameterTypes = meta.java_lang_Class.allocateReferenceArray(m.getParameterCount(), new IntFunction<StaticObject>() {

                @Override
                public StaticObject apply(int j) {
                    return rawParameterKlasses[j].mirror();
                }
            });
            final Klass[] rawCheckedExceptions = m.getCheckedExceptions();
            StaticObject checkedExceptions = meta.java_lang_Class.allocateReferenceArray(rawCheckedExceptions.length, new IntFunction<StaticObject>() {

                @Override
                public StaticObject apply(int j) {
                    return rawCheckedExceptions[j].mirror();
                }
            });
            SignatureAttribute signatureAttribute = (SignatureAttribute) m.getAttribute(Name.Signature);
            StaticObject genericSignature = StaticObject.NULL;
            if (signatureAttribute != null) {
                String sig = m.getConstantPool().symbolAt(signatureAttribute.getSignatureIndex(), "signature").toString();
                genericSignature = meta.toGuestString(sig);
            }
            StaticObject instance = meta.java_lang_reflect_Constructor.allocateInstance();
            constructorInit.invokeDirect(/* this */
            instance, /* declaringKlass */
            m.getDeclaringKlass().mirror(), /* parameterTypes */
            parameterTypes, /* checkedExceptions */
            checkedExceptions, /* modifiers */
            m.getMethodModifiers(), // TODO(peterssen): Fill method slot.
            i, /* signature */
            genericSignature, /* annotations */
            runtimeVisibleAnnotations, /* parameterAnnotations */
            runtimeVisibleParameterAnnotations);
            meta.HIDDEN_CONSTRUCTOR_KEY.setHiddenObject(instance, m);
            meta.HIDDEN_CONSTRUCTOR_RUNTIME_VISIBLE_TYPE_ANNOTATIONS.setHiddenObject(instance, runtimeVisibleTypeAnnotations);
            return instance;
        }
    });
    return arr;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) PermittedSubclassesAttribute(com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute) MethodParametersAttribute(com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute) EnclosingMethodAttribute(com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute) 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) ArrayList(java.util.ArrayList) EspressoContext(com.oracle.truffle.espresso.runtime.EspressoContext) Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) SignatureAttribute(com.oracle.truffle.espresso.classfile.attributes.SignatureAttribute) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) IntFunction(java.util.function.IntFunction) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 54 with Method

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

the class VM method getACCAfter12.

private StaticObject getACCAfter12() {
    ArrayList<StaticObject> domains = new ArrayList<>();
    final boolean[] isPrivileged = new boolean[] { false };
    StaticObject context = Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<StaticObject>() {

        StaticObject prevDomain = StaticObject.NULL;

        @Override
        public StaticObject visitFrame(FrameInstance frameInstance) {
            Method m = getMethodFromFrame(frameInstance);
            if (m != null) {
                StaticObject domain = null;
                StaticObject stackContext = null;
                StaticObject domainKlass = null;
                if (m.getDeclaringKlass() == getMeta().java_security_AccessController && m.getName() == Name.executePrivileged) {
                    isPrivileged[0] = true;
                    Frame frame = frameInstance.getFrame(FrameInstance.FrameAccess.READ_ONLY);
                    // 2nd argument: `AccessControlContext context`
                    stackContext = BytecodeNode.getLocalObject(frame, 1);
                    // 3rd argument: Class<?> caller
                    domainKlass = BytecodeNode.getLocalObject(frame, 2);
                } else {
                    domainKlass = m.getDeclaringKlass().mirror();
                }
                domain = JVM_GetProtectionDomain(domainKlass);
                if (domain != prevDomain && domain != StaticObject.NULL) {
                    domains.add(domain);
                    prevDomain = domain;
                }
                if (isPrivileged[0]) {
                    return stackContext;
                }
            }
            return null;
        }
    });
    return getAccFromContext(domains, isPrivileged[0], context);
}
Also used : Frame(com.oracle.truffle.api.frame.Frame) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ArrayList(java.util.ArrayList) Method(com.oracle.truffle.espresso.impl.Method) FrameInstance(com.oracle.truffle.api.frame.FrameInstance)

Example 55 with Method

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

the class VM method JVM_CurrentLoadedClass.

@VmImpl(isJni = true)
@JavaType(Class.class)
public StaticObject JVM_CurrentLoadedClass() {
    PrivilegedStack stack = getPrivilegedStack();
    StaticObject mirrorKlass = Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<StaticObject>() {

        @Override
        public StaticObject visitFrame(FrameInstance frameInstance) {
            Method m = getMethodFromFrame(frameInstance);
            if (m != null) {
                if (isTrustedFrame(frameInstance, stack)) {
                    return StaticObject.NULL;
                }
                if (!m.isNative()) {
                    ObjectKlass klass = m.getDeclaringKlass();
                    StaticObject loader = klass.getDefiningClassLoader();
                    if (StaticObject.notNull(loader) && !isTrustedLoader(loader)) {
                        return klass.mirror();
                    }
                }
            }
            return null;
        }
    });
    return mirrorKlass == null ? StaticObject.NULL : mirrorKlass;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) Method(com.oracle.truffle.espresso.impl.Method) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Aggregations

Method (com.oracle.truffle.espresso.impl.Method)91 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)57 Meta (com.oracle.truffle.espresso.meta.Meta)27 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)22 Klass (com.oracle.truffle.espresso.impl.Klass)19 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)19 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)16 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)14 ExportMessage (com.oracle.truffle.api.library.ExportMessage)12 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)10 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)9 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)8 ArityException (com.oracle.truffle.api.interop.ArityException)8 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)8 ArrayList (java.util.ArrayList)8 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)7 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)5 Field (com.oracle.truffle.espresso.impl.Field)4 MethodParametersAttribute (com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute)3 Signature (com.oracle.truffle.espresso.descriptors.Symbol.Signature)3