Search in sources :

Example 6 with EspressoException

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

the class MethodTypeConstant method signatureToMethodType.

static StaticObject signatureToMethodType(Symbol<Symbol.Type>[] signature, Klass accessingKlass, boolean failWithBME, Meta meta) {
    Symbol<Symbol.Type> rt = Signatures.returnType(signature);
    int pcount = Signatures.parameterCount(signature, false);
    StaticObject[] ptypes = new StaticObject[pcount];
    StaticObject rtype;
    try {
        for (int i = 0; i < pcount; i++) {
            Symbol<Symbol.Type> paramType = Signatures.parameterType(signature, i);
            ptypes[i] = meta.resolveSymbolAndAccessCheck(paramType, accessingKlass).mirror();
        }
    } catch (EspressoException e) {
        if (meta.java_lang_ClassNotFoundException.isAssignableFrom(e.getGuestException().getKlass())) {
            throw meta.throwExceptionWithMessage(meta.java_lang_NoClassDefFoundError, e.getGuestMessage());
        }
        throw e;
    }
    try {
        rtype = meta.resolveSymbolAndAccessCheck(rt, accessingKlass).mirror();
    } catch (EspressoException e) {
        EspressoException rethrow = e;
        if (meta.java_lang_ClassNotFoundException.isAssignableFrom(e.getGuestException().getKlass())) {
            rethrow = EspressoException.wrap(Meta.initExceptionWithMessage(meta.java_lang_NoClassDefFoundError, e.getGuestMessage()), meta);
        }
        if (failWithBME) {
            rethrow = EspressoException.wrap(Meta.initExceptionWithCause(meta.java_lang_BootstrapMethodError, rethrow.getGuestException()), meta);
        }
        throw rethrow;
    }
    return (StaticObject) meta.java_lang_invoke_MethodHandleNatives_findMethodHandleType.invokeDirect(null, rtype, StaticObject.createArray(meta.java_lang_Class_array, ptypes));
}
Also used : EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject)

Example 7 with EspressoException

use of com.oracle.truffle.espresso.runtime.EspressoException 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 8 with EspressoException

use of com.oracle.truffle.espresso.runtime.EspressoException 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)

Example 9 with EspressoException

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

the class ClassRedefinition method redefineClass.

public int redefineClass(ChangePacket packet, List<ObjectKlass> invalidatedClasses, List<ObjectKlass> redefinedClasses) {
    try {
        switch(packet.classChange) {
            case METHOD_BODY_CHANGE:
            case CONSTANT_POOL_CHANGE:
            case CLASS_NAME_CHANGED:
            case ADD_METHOD:
            case REMOVE_METHOD:
            case SCHEMA_CHANGE:
                doRedefineClass(packet, invalidatedClasses, redefinedClasses);
                return 0;
            case CLASS_HIERARCHY_CHANGED:
                context.markChangedHierarchy();
                doRedefineClass(packet, invalidatedClasses, redefinedClasses);
                return 0;
            case NEW_CLASS:
                ClassInfo classInfo = packet.info;
                // if there is a currently loaded class under that name
                // we have to replace that in the class loader registry etc.
                // otherwise, don't eagerly define the new class
                Symbol<Symbol.Type> type = context.getTypes().fromName(classInfo.getName());
                ClassRegistry classRegistry = context.getRegistries().getClassRegistry(classInfo.getClassLoader());
                Klass loadedKlass = classRegistry.findLoadedKlass(type);
                if (loadedKlass != null) {
                    // OK, we have to define the new klass instance and
                    // inject it under the existing JDWP ID
                    classRegistry.onInnerClassRemoved(type);
                    ObjectKlass newKlass = classRegistry.defineKlass(type, classInfo.getBytes());
                    packet.info.setKlass(newKlass);
                }
                return 0;
            default:
                return 0;
        }
    } catch (EspressoException ex) {
        // we get from parsing the class file
        return ErrorCodes.INVALID_CLASS_FORMAT;
    }
}
Also used : ClassRegistry(com.oracle.truffle.espresso.impl.ClassRegistry) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ParserKlass(com.oracle.truffle.espresso.impl.ParserKlass) EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass)

Example 10 with EspressoException

use of com.oracle.truffle.espresso.runtime.EspressoException 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)

Aggregations

EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)14 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)9 Klass (com.oracle.truffle.espresso.impl.Klass)6 Method (com.oracle.truffle.espresso.impl.Method)5 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)5 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)4 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)3 Meta (com.oracle.truffle.espresso.meta.Meta)3 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)3 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)2 RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)2 EspressoExitException (com.oracle.truffle.espresso.runtime.EspressoExitException)2 RootCallTarget (com.oracle.truffle.api.RootCallTarget)1 TruffleSafepoint (com.oracle.truffle.api.TruffleSafepoint)1 TruffleStackTrace (com.oracle.truffle.api.TruffleStackTrace)1 TruffleStackTraceElement (com.oracle.truffle.api.TruffleStackTraceElement)1 AbstractTruffleException (com.oracle.truffle.api.exception.AbstractTruffleException)1 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 ExportMessage (com.oracle.truffle.api.library.ExportMessage)1 ExplodeLoop (com.oracle.truffle.api.nodes.ExplodeLoop)1