Search in sources :

Example 11 with EspressoException

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

the class VM method JVM_GetPermittedSubclasses.

@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(Class[].class)
public StaticObject JVM_GetPermittedSubclasses(@JavaType(Class.class) StaticObject self) {
    Klass k = self.getMirrorKlass();
    if (!(k instanceof ObjectKlass)) {
        return StaticObject.NULL;
    }
    ObjectKlass klass = (ObjectKlass) k;
    if (!klass.isSealed()) {
        return StaticObject.NULL;
    }
    char[] classes = ((PermittedSubclassesAttribute) klass.getAttribute(PermittedSubclassesAttribute.NAME)).getClasses();
    StaticObject[] permittedSubclasses = new StaticObject[classes.length];
    RuntimeConstantPool pool = klass.getConstantPool();
    int nClasses = 0;
    for (int index : classes) {
        Klass permitted;
        try {
            permitted = pool.resolvedKlassAt(klass, index);
        } catch (EspressoException e) {
            /* Suppress and continue */
            continue;
        }
        if (permitted instanceof ObjectKlass) {
            permittedSubclasses[nClasses++] = permitted.mirror();
        }
    }
    if (nClasses == permittedSubclasses.length) {
        return StaticObject.createArray(getMeta().java_lang_Class_array, permittedSubclasses);
    }
    return getMeta().java_lang_Class.allocateReferenceArray(nClasses, (i) -> permittedSubclasses[i]);
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) RuntimeConstantPool(com.oracle.truffle.espresso.classfile.RuntimeConstantPool) EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) PermittedSubclassesAttribute(com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 12 with EspressoException

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

the class NativeEnv method intrinsicWrapper.

private Callback intrinsicWrapper(CallableFromNative.Factory factory) {
    int extraArg = (factory.prependEnv()) ? 1 : 0;
    return new Callback(factory.parameterCount() + extraArg, new Callback.Function() {

        @CompilerDirectives.CompilationFinal
        private CallableFromNative subst = null;

        @Override
        public Object call(Object... args) {
            boolean isJni = factory.prependEnv();
            try {
                if (subst == null) {
                    CompilerDirectives.transferToInterpreterAndInvalidate();
                    subst = factory.create(getMeta());
                }
                return subst.invoke(NativeEnv.this, args);
            } catch (EspressoException | StackOverflowError | OutOfMemoryError e) {
                if (isJni) {
                    // This will most likely SOE again. Nothing we can do about that
                    // unfortunately.
                    EspressoException wrappedError = (e instanceof EspressoException) ? (EspressoException) e : (e instanceof StackOverflowError) ? getContext().getStackOverflow() : getContext().getOutOfMemory();
                    jni().setPendingException(wrappedError);
                    return defaultValue(factory.returnType());
                }
                throw e;
            }
        }
    });
}
Also used : EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) CallableFromNative(com.oracle.truffle.espresso.substitutions.CallableFromNative) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 13 with EspressoException

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

the class NativeMethodNode method maybeThrowAndClearPendingException.

private void maybeThrowAndClearPendingException(JniEnv jniEnv) {
    EspressoException ex = jniEnv.getPendingEspressoException();
    if (ex != null) {
        enterThrowsException();
        jniEnv.clearPendingException();
        throw ex;
    }
}
Also used : EspressoException(com.oracle.truffle.espresso.runtime.EspressoException)

Example 14 with EspressoException

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

the class Target_sun_reflect_NativeMethodAccessorImpl method callMethodReflectively.

@JavaType(Object.class)
public static StaticObject callMethodReflectively(Meta meta, @JavaType(Object.class) StaticObject receiver, @JavaType(Object[].class) StaticObject args, Method m, Klass klass, @JavaType(Class[].class) StaticObject parameterTypes) {
    // Klass should be initialized if method is static, and could be delayed until method
    // invocation, according to specs. However, JCK tests that it is indeed always initialized
    // before doing anything, even if the method to be invoked is from another class.
    klass.safeInitialize();
    Method reflectedMethod = m;
    if (reflectedMethod.isRemovedByRedefition()) {
        reflectedMethod = m.getContext().getClassRedefinition().handleRemovedMethod(reflectedMethod, reflectedMethod.isStatic() ? reflectedMethod.getDeclaringKlass() : receiver.getKlass());
    }
    // actual method to invoke
    Method method;
    // target klass, receiver's klass for non-static
    Klass targetKlass;
    if (reflectedMethod.isStatic()) {
        // Ignore receiver argument;.
        method = reflectedMethod;
        targetKlass = klass;
    } else {
        if (StaticObject.isNull(receiver)) {
            throw meta.throwNullPointerException();
        }
        // Check class of receiver against class declaring method.
        if (!klass.isAssignableFrom(receiver.getKlass())) {
            throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "object is not an instance of declaring class");
        }
        // target klass is receiver's klass
        targetKlass = receiver.getKlass();
        // no need to resolve if method is private or <init>
        if (reflectedMethod.isPrivate() || Name._init_.equals(reflectedMethod.getName())) {
            method = reflectedMethod;
        } else {
            // resolve based on the receiver
            if (reflectedMethod.getDeclaringKlass().isInterface()) {
                // resolve interface call
                // Match resolution errors with those thrown due to reflection inlining
                // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
                method = reflectedMethod;
                assert targetKlass instanceof ObjectKlass;
                method = ((ObjectKlass) targetKlass).itableLookup(method.getDeclaringKlass(), method.getITableIndex());
                if (method != null) {
                    // Check for abstract methods as well
                    if (!method.hasCode()) {
                        // new default: 65315
                        throw meta.throwExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, Meta.initException(meta.java_lang_AbstractMethodError));
                    }
                }
            } else {
                // if the method can be overridden, we resolve using the vtable index.
                method = reflectedMethod;
                // VTable is live, use it
                method = targetKlass.vtableLookup(method.getVTableIndex());
                if (method != null) {
                    // Check for abstract methods as well
                    if (method.isAbstract()) {
                        // new default: 65315
                        throw meta.throwExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, Meta.initException(meta.java_lang_AbstractMethodError));
                    }
                }
            }
        }
    }
    // an internal vtable bug. If you ever get this please let Karen know.
    if (method == null) {
        throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, "please let Karen know");
    }
    int argsLen = StaticObject.isNull(args) ? 0 : args.length();
    final Symbol<Type>[] signature = method.getParsedSignature();
    // Check number of arguments.
    if (Signatures.parameterCount(signature, false) != argsLen) {
        throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "wrong number of arguments!");
    }
    Object[] adjustedArgs = new Object[argsLen];
    for (int i = 0; i < argsLen; ++i) {
        StaticObject arg = args.get(i);
        StaticObject paramTypeMirror = parameterTypes.get(i);
        Klass paramKlass = paramTypeMirror.getMirrorKlass();
        // Throws guest IllegallArgumentException if the parameter cannot be casted or widened.
        adjustedArgs[i] = checkAndWiden(meta, arg, paramKlass);
    }
    Object result;
    try {
        result = method.invokeDirect(receiver, adjustedArgs);
    } catch (EspressoException e) {
        throw meta.throwExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, e.getGuestException());
    }
    if (reflectedMethod.getReturnKind() == JavaKind.Void) {
        return StaticObject.NULL;
    }
    if (reflectedMethod.getReturnKind().isPrimitive()) {
        return Meta.box(meta, result);
    }
    // Result is not void nor primitive, pass through.
    return (StaticObject) result;
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Symbol(com.oracle.truffle.espresso.descriptors.Symbol) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method)

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