Search in sources :

Example 26 with Meta

use of com.oracle.truffle.espresso.meta.Meta in project graal by oracle.

the class VM method JVM_GetArrayElement.

/**
 * Returns the value of the indexed component in the specified array object. The value is
 * automatically wrapped in an object if it has a primitive type.
 *
 * @param array the array
 * @param index the index
 * @throws NullPointerException If the specified object is null
 * @throws IllegalArgumentException If the specified object is not an array
 * @throws ArrayIndexOutOfBoundsException If the specified {@code index} argument is negative,
 *             or if it is greater than or equal to the length of the specified array
 * @return the (possibly wrapped) value of the indexed component in the specified array
 */
@VmImpl(isJni = true)
@JavaType(Object.class)
public StaticObject JVM_GetArrayElement(@JavaType(Object.class) StaticObject array, int index, @Inject SubstitutionProfiler profiler) {
    Meta meta = getMeta();
    if (StaticObject.isNull(array)) {
        profiler.profile(7);
        throw meta.throwNullPointerException();
    }
    if (array.isArray()) {
        profiler.profile(6);
        return getInterpreterToVM().getArrayObject(index, array);
    }
    if (!array.getClass().isArray()) {
        profiler.profile(5);
        throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "Argument is not an array");
    }
    assert array.getClass().isArray() && array.getClass().getComponentType().isPrimitive();
    if (index < 0 || index >= JVM_GetArrayLength(array, profiler)) {
        profiler.profile(4);
        throw meta.throwExceptionWithMessage(meta.java_lang_ArrayIndexOutOfBoundsException, "index");
    }
    Object elem = Array.get(array, index);
    return getMeta().boxPrimitive(elem);
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 27 with Meta

use of com.oracle.truffle.espresso.meta.Meta 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 28 with Meta

use of com.oracle.truffle.espresso.meta.Meta 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 29 with Meta

use of com.oracle.truffle.espresso.meta.Meta in project graal by oracle.

the class VM method JVM_InitStackTraceElementArray.

@VmImpl(isJni = true)
public void JVM_InitStackTraceElementArray(@JavaType(StackTraceElement[].class) StaticObject elements, @JavaType(Throwable.class) StaticObject throwable, @Inject SubstitutionProfiler profiler) {
    Meta meta = getMeta();
    if (StaticObject.isNull(elements) || StaticObject.isNull(throwable)) {
        profiler.profile(0);
        throw meta.throwNullPointerException();
    }
    assert elements.isArray();
    VM.StackTrace stackTrace = (VM.StackTrace) meta.HIDDEN_FRAMES.getHiddenObject(throwable);
    if (stackTrace != null) {
        if (elements.length() != stackTrace.size) {
            profiler.profile(1);
            throw meta.throwException(meta.java_lang_IndexOutOfBoundsException);
        }
        for (int i = 0; i < stackTrace.size; i++) {
            if (StaticObject.isNull(elements.get(i))) {
                profiler.profile(2);
                throw meta.throwNullPointerException();
            }
            fillInElement(elements.get(i), stackTrace.trace[i], getMeta().java_lang_Class_getName);
        }
    }
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint)

Example 30 with Meta

use of com.oracle.truffle.espresso.meta.Meta in project graal by oracle.

the class VM method JVM_FindPrimitiveClass.

@VmImpl(isJni = true)
@JavaType(Class.class)
public StaticObject JVM_FindPrimitiveClass(@Pointer TruffleObject namePtr) {
    Meta meta = getMeta();
    String hostName = NativeUtils.interopPointerToString(namePtr);
    return findPrimitiveClass(meta, hostName);
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Aggregations

Meta (com.oracle.truffle.espresso.meta.Meta)82 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)29 Method (com.oracle.truffle.espresso.impl.Method)27 ExportMessage (com.oracle.truffle.api.library.ExportMessage)24 Klass (com.oracle.truffle.espresso.impl.Klass)21 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)21 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)21 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)20 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)17 ArityException (com.oracle.truffle.api.interop.ArityException)9 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)9 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)9 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)8 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)6 NativeType (com.oracle.truffle.espresso.ffi.NativeType)4 Field (com.oracle.truffle.espresso.impl.Field)4 ArrayList (java.util.ArrayList)4 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)3 RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)3