Search in sources :

Example 21 with Method

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

the class VM method JVM_GetCallerClass.

@VmImpl(isJni = true)
@JavaType(Class.class)
public StaticObject JVM_GetCallerClass(int depth, @Inject SubstitutionProfiler profiler) {
    // temporarily for existing code to use until a replacement API is defined.
    if (depth != JVM_CALLER_DEPTH) {
        FrameInstance callerFrame = getCallerFrame(depth, true, getMeta());
        if (callerFrame != null) {
            Method callerMethod = getMethodFromFrame(callerFrame);
            if (callerMethod != null) {
                return callerMethod.getDeclaringKlass().mirror();
            }
        }
        // Not found.
        return StaticObject.NULL;
    }
    // Getting the class of the caller frame.
    // 
    // The call stack at this point looks something like this:
    // 
    // [0] [ @CallerSensitive public sun.reflect.Reflection.getCallerClass ]
    // [1] [ @CallerSensitive API.method ]
    // [.] [ (skipped intermediate frames) ]
    // [n] [ caller ]
    Meta meta = getMeta();
    StaticObject[] exception = new StaticObject[] { null };
    Method callerMethod = Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Method>() {

        private int depth = 0;

        @SuppressWarnings("fallthrough")
        @Override
        public Method visitFrame(FrameInstance frameInstance) {
            Method method = getMethodFromFrame(frameInstance);
            if (method != null) {
                switch(depth) {
                    case 0:
                        // Reflection.getCallerClass.
                        if (method != meta.sun_reflect_Reflection_getCallerClass) {
                            exception[0] = Meta.initExceptionWithMessage(meta.java_lang_InternalError, "JVM_GetCallerClass must only be called from Reflection.getCallerClass");
                            return /* ignore */
                            method;
                        }
                    // fall-through
                    case 1:
                        // Frame 0 and 1 must be caller sensitive.
                        if (!method.isCallerSensitive()) {
                            exception[0] = Meta.initExceptionWithMessage(meta.java_lang_InternalError, "CallerSensitive annotation expected at frame " + depth);
                            return /* ignore */
                            method;
                        }
                        break;
                    default:
                        if (!isIgnoredBySecurityStackWalk(method, meta)) {
                            return method;
                        }
                }
                ++depth;
            }
            return null;
        }
    });
    // InternalError was recorded.
    StaticObject internalError = exception[0];
    if (internalError != null) {
        profiler.profile(0);
        assert InterpreterToVM.instanceOf(internalError, meta.java_lang_InternalError);
        throw meta.throwException(internalError);
    }
    if (callerMethod == null) {
        return StaticObject.NULL;
    }
    return callerMethod.getDeclaringKlass().mirror();
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 22 with Method

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

the class Management method GetThreadInfo.

@ManagementImpl
public int GetThreadInfo(@JavaType(long[].class) StaticObject ids, int maxDepth, @JavaType(Object[].class) StaticObject infoArray, @Inject SubstitutionProfiler profiler) {
    Meta meta = getMeta();
    if (StaticObject.isNull(ids) || StaticObject.isNull(infoArray)) {
        profiler.profile(0);
        throw meta.throwNullPointerException();
    }
    if (maxDepth < -1) {
        profiler.profile(1);
        throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "Invalid maxDepth");
    }
    validateThreadIdArray(meta, ids, profiler);
    validateThreadInfoArray(meta, infoArray, profiler);
    if (ids.length() != infoArray.length()) {
        profiler.profile(2);
        throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "The length of the given ThreadInfo array does not match the length of the given array of thread IDs");
    }
    Method init = meta.java_lang_management_ThreadInfo.lookupDeclaredMethod(Symbol.Name._init_, getSignatures().makeRaw(/* returns */
    Symbol.Type._void, /* t */
    Symbol.Type.java_lang_Thread, /* state */
    Symbol.Type._int, /* lockObj */
    Symbol.Type.java_lang_Object, /* lockOwner */
    Symbol.Type.java_lang_Thread, /* blockedCount */
    Symbol.Type._long, /* blockedTime */
    Symbol.Type._long, /* waitedCount */
    Symbol.Type._long, /* waitedTime */
    Symbol.Type._long, /* StackTraceElement[] */
    Symbol.Type.java_lang_StackTraceElement_array));
    StaticObject[] activeThreads = getContext().getActiveThreads();
    StaticObject currentThread = getContext().getCurrentThread();
    for (int i = 0; i < ids.length(); ++i) {
        long id = getInterpreterToVM().getArrayLong(i, ids);
        StaticObject thread = StaticObject.NULL;
        for (int j = 0; j < activeThreads.length; ++j) {
            if (getThreadAccess().getThreadId(activeThreads[j]) == id) {
                thread = activeThreads[j];
                break;
            }
        }
        if (StaticObject.isNull(thread)) {
            getInterpreterToVM().setArrayObject(StaticObject.NULL, i, infoArray);
        } else {
            int threadStatus = meta.getThreadAccess().getState(thread);
            StaticObject lockObj = StaticObject.NULL;
            StaticObject lockOwner = StaticObject.NULL;
            int mask = State.BLOCKED.value | State.WAITING.value | State.TIMED_WAITING.value;
            if ((threadStatus & mask) != 0) {
                lockObj = (StaticObject) meta.HIDDEN_THREAD_BLOCKED_OBJECT.getHiddenObject(thread);
                if (lockObj == null) {
                    lockObj = StaticObject.NULL;
                }
                Thread hostOwner = StaticObject.isNull(lockObj) ? null : lockObj.getLock(getContext()).getOwnerThread();
                if (hostOwner != null && hostOwner.isAlive()) {
                    lockOwner = getContext().getGuestThreadFromHost(hostOwner);
                    if (lockOwner == null) {
                        lockOwner = StaticObject.NULL;
                    }
                }
            }
            long blockedCount = Target_java_lang_Thread.getThreadCounter(thread, meta.HIDDEN_THREAD_BLOCKED_COUNT);
            long waitedCount = Target_java_lang_Thread.getThreadCounter(thread, meta.HIDDEN_THREAD_WAITED_COUNT);
            StaticObject stackTrace;
            if (maxDepth != 0 && thread == currentThread) {
                stackTrace = (StaticObject) getMeta().java_lang_Throwable_getStackTrace.invokeDirect(Meta.initException(meta.java_lang_Throwable));
                if (stackTrace.length() > maxDepth && maxDepth != -1) {
                    StaticObject[] unwrapped = stackTrace.unwrap();
                    unwrapped = Arrays.copyOf(unwrapped, maxDepth);
                    stackTrace = StaticObject.wrap(meta.java_lang_StackTraceElement.getArrayClass(), unwrapped);
                }
            } else {
                stackTrace = meta.java_lang_StackTraceElement.allocateReferenceArray(0);
            }
            StaticObject threadInfo = meta.java_lang_management_ThreadInfo.allocateInstance();
            init.invokeDirect(/* this */
            threadInfo, /* t */
            thread, /* state */
            threadStatus, /* lockObj */
            lockObj, /* lockOwner */
            lockOwner, /* blockedCount */
            blockedCount, /* blockedTime */
            -1L, /* waitedCount */
            waitedCount, /* waitedTime */
            -1L, /* StackTraceElement[] */
            stackTrace);
            getInterpreterToVM().setArrayObject(threadInfo, i, infoArray);
        }
    }
    // always 0
    return 0;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) Target_java_lang_Thread(com.oracle.truffle.espresso.substitutions.Target_java_lang_Thread)

Example 23 with Method

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

the class Management method GetMemoryUsage.

@ManagementImpl
@JavaType(Object.class)
public StaticObject GetMemoryUsage(@SuppressWarnings("unused") boolean heap) {
    Method init = getMeta().java_lang_management_MemoryUsage.lookupDeclaredMethod(Symbol.Name._init_, getSignatures().makeRaw(Symbol.Type._void, Symbol.Type._long, Symbol.Type._long, Symbol.Type._long, Symbol.Type._long));
    StaticObject instance = getMeta().java_lang_management_MemoryUsage.allocateInstance();
    init.invokeDirect(instance, 0L, 0L, 0L, 0L);
    return instance;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 24 with Method

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

the class VM method JVM_ClassLoaderDepth.

@VmImpl(isJni = true)
public int JVM_ClassLoaderDepth() {
    PrivilegedStack stack = getPrivilegedStack();
    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 (isTrustedFrame(frameInstance, stack)) {
                    return -1;
                }
                if (!m.isNative()) {
                    ObjectKlass klass = m.getDeclaringKlass();
                    StaticObject loader = klass.getDefiningClassLoader();
                    if (StaticObject.notNull(loader) && !isTrustedLoader(loader)) {
                        return depth;
                    }
                    depth++;
                }
            }
            return null;
        }
    });
    return res == null ? -1 : res;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) FrameInstance(com.oracle.truffle.api.frame.FrameInstance)

Example 25 with Method

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

the class VM method getGuestReflectiveMethodRoot.

// endregion stack inspection
// region annotations
@JavaType(java.lang.reflect.Method.class)
private static StaticObject getGuestReflectiveMethodRoot(@JavaType(java.lang.reflect.Method.class) StaticObject seed, Meta meta) {
    assert InterpreterToVM.instanceOf(seed, meta.java_lang_reflect_Method);
    StaticObject curMethod = seed;
    Method target = null;
    while (target == null) {
        target = (Method) meta.HIDDEN_METHOD_KEY.getHiddenObject(curMethod);
        if (target == null) {
            curMethod = meta.java_lang_reflect_Method_root.getObject(curMethod);
        }
    }
    return curMethod;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) 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