Search in sources :

Example 41 with StaticObject

use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.

the class VM method JVM_MonitorWait.

@VmImpl(isJni = true)
@TruffleBoundary
@SuppressFBWarnings(value = { "IMSE" }, justification = "Not dubious, .wait is just forwarded from the guest.")
@SuppressWarnings("try")
public void JVM_MonitorWait(@JavaType(Object.class) StaticObject self, long timeout, @Inject Meta meta, @Inject SubstitutionProfiler profiler) {
    EspressoContext context = getContext();
    StaticObject currentThread = context.getCurrentThread();
    State state = timeout > 0 ? State.TIMED_WAITING : State.WAITING;
    try (Transition transition = Transition.transition(context, state)) {
        if (context.EnableManagement) {
            // Locks bookkeeping.
            meta.HIDDEN_THREAD_BLOCKED_OBJECT.setHiddenObject(currentThread, self);
            Target_java_lang_Thread.incrementThreadCounter(currentThread, meta.HIDDEN_THREAD_WAITED_COUNT);
        }
        final boolean report = context.shouldReportVMEvents();
        if (report) {
            context.reportMonitorWait(self, timeout);
        }
        boolean timedOut = !InterpreterToVM.monitorWait(self.getLock(getContext()), timeout);
        if (report) {
            context.reportMonitorWaited(self, timedOut);
        }
    } catch (GuestInterruptedException e) {
        profiler.profile(0);
        if (getThreadAccess().isInterrupted(currentThread, true)) {
            throw meta.throwExceptionWithMessage(meta.java_lang_InterruptedException, e.getMessage());
        }
        getThreadAccess().fullSafePoint(currentThread);
    } catch (IllegalMonitorStateException e) {
        profiler.profile(1);
        throw meta.throwExceptionWithMessage(meta.java_lang_IllegalMonitorStateException, e.getMessage());
    } catch (IllegalArgumentException e) {
        profiler.profile(2);
        throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, e.getMessage());
    } finally {
        if (context.EnableManagement) {
            meta.HIDDEN_THREAD_BLOCKED_OBJECT.setHiddenObject(currentThread, null);
        }
    }
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) GuestInterruptedException(com.oracle.truffle.espresso.blocking.GuestInterruptedException) State(com.oracle.truffle.espresso.threads.State) Transition(com.oracle.truffle.espresso.threads.Transition) EspressoContext(com.oracle.truffle.espresso.runtime.EspressoContext) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) SuppressFBWarnings(com.oracle.truffle.espresso.impl.SuppressFBWarnings)

Example 42 with StaticObject

use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.

the class VM method JVM_DoPrivileged.

@VmImpl(isJni = true)
@SuppressWarnings("unused")
@JavaType(Object.class)
public StaticObject JVM_DoPrivileged(@JavaType(Class.class) StaticObject cls, /* PrivilegedAction or PrivilegedActionException */
@JavaType(Object.class) StaticObject action, @JavaType(AccessControlContext.class) StaticObject context, boolean wrapException, @Inject Meta meta, @Inject SubstitutionProfiler profiler) {
    if (StaticObject.isNull(action)) {
        profiler.profile(0);
        throw meta.throwNullPointerException();
    }
    FrameInstance callerFrame = getCallerFrame(1, false, meta);
    assert callerFrame != null : "No caller ?";
    Klass caller = getMethodFromFrame(callerFrame).getDeclaringKlass();
    StaticObject acc = context;
    if (!StaticObject.isNull(context)) {
        if (!isAuthorized(context, caller)) {
            acc = createDummyACC();
        }
    }
    Method run = action.getKlass().lookupMethod(Name.run, Signature.Object);
    if (run == null || !run.isPublic() || run.isStatic()) {
        profiler.profile(1);
        throw meta.throwException(meta.java_lang_InternalError);
    }
    // Prepare the privileged stack
    PrivilegedStack stack = getPrivilegedStack();
    stack.push(callerFrame, acc, caller);
    // Execute the action.
    StaticObject result;
    try {
        result = (StaticObject) run.invokeDirect(action);
    } catch (EspressoException e) {
        profiler.profile(2);
        if (meta.java_lang_Exception.isAssignableFrom(e.getGuestException().getKlass()) && !meta.java_lang_RuntimeException.isAssignableFrom(e.getGuestException().getKlass())) {
            profiler.profile(3);
            StaticObject wrapper = meta.java_security_PrivilegedActionException.allocateInstance();
            getMeta().java_security_PrivilegedActionException_init_Exception.invokeDirect(wrapper, e.getGuestException());
            throw meta.throwException(wrapper);
        }
        profiler.profile(4);
        throw e;
    } finally {
        stack.pop();
    }
    return result;
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 43 with StaticObject

use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.

the class VM method JVM_AssertionStatusDirectives.

// endregion array
// region assertion
/**
 * Espresso only supports basic -ea and -esa options. Complex per-class/package filters are
 * unsupported.
 */
@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(internalName = "Ljava/lang/AssertionStatusDirectives;")
public StaticObject JVM_AssertionStatusDirectives(@SuppressWarnings("unused") @JavaType(Class.class) StaticObject unused) {
    Meta meta = getMeta();
    StaticObject instance = meta.java_lang_AssertionStatusDirectives.allocateInstance();
    meta.java_lang_AssertionStatusDirectives.lookupMethod(Name._init_, Signature._void).invokeDirect(instance);
    meta.java_lang_AssertionStatusDirectives_classes.set(instance, meta.java_lang_String.allocateReferenceArray(0));
    meta.java_lang_AssertionStatusDirectives_classEnabled.set(instance, meta._boolean.allocatePrimitiveArray(0));
    meta.java_lang_AssertionStatusDirectives_packages.set(instance, meta.java_lang_String.allocateReferenceArray(0));
    meta.java_lang_AssertionStatusDirectives_packageEnabled.set(instance, meta._boolean.allocatePrimitiveArray(0));
    boolean ea = getContext().getEnv().getOptions().get(EspressoOptions.EnableAssertions);
    meta.java_lang_AssertionStatusDirectives_deflt.set(instance, ea);
    return instance;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 44 with StaticObject

use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.

the class StackWalk method fetchFirstBatch.

/**
 * initializes the stack walking, and anchors the Frame Walker instance to a particular frame
 * and fetches the first batch of frames requested by guest.
 *
 * Upon return, unanchors the Frame Walker, and it is then not possible to continue walking for
 * this walker anymore.
 *
 * @return The result of invoking guest
 *         {@code java.lang.StackStreamFactory.AbstractStackWalker#doStackWalk(long, int, int, int,
 *         int)} .
 */
public StaticObject fetchFirstBatch(@JavaType(internalName = "Ljava/lang/StackStreamFactory;") StaticObject stackStream, long mode, int skipframes, int batchSize, int startIndex, @JavaType(Object[].class) StaticObject frames, Meta meta) {
    assert synchronizedConstants(meta);
    FrameWalker fw = new FrameWalker(meta, mode);
    fw.init(skipframes, batchSize, startIndex);
    Integer decodedOrNull = fw.doStackWalk(frames);
    int decoded = decodedOrNull == null ? fw.decoded() : decodedOrNull;
    if (decoded < 1) {
        throw meta.throwException(meta.java_lang_InternalError);
    }
    register(fw);
    Object result = meta.java_lang_AbstractStackWalker_doStackWalk.invokeDirect(stackStream, fw.anchor, skipframes, batchSize, startIndex, startIndex + decoded);
    unAnchor(fw);
    return (StaticObject) result;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject)

Example 45 with StaticObject

use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.

the class VM method DetachCurrentThread.

@VmImpl
@TruffleBoundary
public int DetachCurrentThread(@Inject EspressoContext context) {
    StaticObject currentThread = context.getCurrentThread();
    if (currentThread == null) {
        return JNI_OK;
    }
    getLogger().fine(() -> {
        String guestName = getThreadAccess().getThreadName(currentThread);
        return "DetachCurrentThread: " + guestName;
    });
    // HotSpot will wait forever if the current VM this thread was attached to has exited
    // Should we reproduce this behaviour?
    Method lastJavaMethod = Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Method>() {

        @Override
        public Method visitFrame(FrameInstance frameInstance) {
            Method method = getMethodFromFrame(frameInstance);
            if (method != null && method.getContext() == context) {
                return method;
            }
            return null;
        }
    });
    if (lastJavaMethod != null) {
        // this thread is executing
        getLogger().warning(() -> {
            String guestName = getThreadAccess().getThreadName(currentThread);
            return "DetachCurrentThread called while thread is still executing Java code (" + guestName + ")";
        });
        return JNI_ERR;
    }
    StaticObject pendingException = jniEnv.getPendingException();
    jniEnv.clearPendingException();
    Meta meta = context.getMeta();
    try {
        if (pendingException != null) {
            meta.java_lang_Thread_dispatchUncaughtException.invokeDirect(currentThread, pendingException);
        }
        getThreadAccess().terminate(currentThread);
    } catch (EspressoException e) {
        try {
            StaticObject ex = e.getGuestException();
            String exception = ex.getKlass().getExternalName();
            String threadName = getThreadAccess().getThreadName(currentThread);
            context.getLogger().warning(String.format("Exception: %s thrown while terminating thread \"%s\"", exception, threadName));
            Method printStackTrace = ex.getKlass().lookupMethod(Name.printStackTrace, Signature._void);
            printStackTrace.invokeDirect(ex);
        } catch (EspressoException ee) {
            String exception = ee.getGuestException().getKlass().getExternalName();
            context.getLogger().warning(String.format("Exception: %s thrown while trying to print stack trace", exception));
        } catch (EspressoExitException ee) {
        // ignore
        }
    } catch (EspressoExitException e) {
    // ignore
    } catch (Throwable t) {
        context.getLogger().severe("Host exception thrown while trying to terminate thread");
        t.printStackTrace();
    }
    return JNI_OK;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) EspressoExitException(com.oracle.truffle.espresso.runtime.EspressoExitException) Method(com.oracle.truffle.espresso.impl.Method) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)199 Method (com.oracle.truffle.espresso.impl.Method)57 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)44 Klass (com.oracle.truffle.espresso.impl.Klass)32 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)32 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)30 Meta (com.oracle.truffle.espresso.meta.Meta)29 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)26 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)23 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)17 ArrayList (java.util.ArrayList)13 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)9 Symbol (com.oracle.truffle.espresso.descriptors.Symbol)8 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)7 ExportMessage (com.oracle.truffle.api.library.ExportMessage)7 BytecodeNode (com.oracle.truffle.espresso.nodes.BytecodeNode)7 TruffleSafepoint (com.oracle.truffle.api.TruffleSafepoint)6 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)6 EspressoContext (com.oracle.truffle.espresso.runtime.EspressoContext)6 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)5