Search in sources :

Example 11 with Meta

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

the class JniEnv method FindClass.

/**
 * <h3>jclass FindClass(JNIEnv *env, const char *name);</h3>
 *
 * <p>
 * FindClass locates the class loader associated with the current native method; that is, the
 * class loader of the class that declared the native method. If the native method belongs to a
 * system class, no class loader will be involved. Otherwise, the proper class loader will be
 * invoked to load and link the named class. Since Java 2 SDK release 1.2, when FindClass is
 * called through the Invocation Interface, there is no current native method or its associated
 * class loader. In that case, the result of {@link ClassLoader#getSystemClassLoader} is used.
 * This is the class loader the virtual machine creates for applications, and is able to locate
 * classes listed in the java.class.path property. The name argument is a fully-qualified class
 * name or an array type signature .
 * <p>
 * For example, the fully-qualified class name for the {@code java.lang.String} class is:
 *
 * <pre>
 * "java/lang/String"}
 * </pre>
 *
 * <p>
 * The array type signature of the array class {@code java.lang.Object[]} is:
 *
 * <pre>
 * "[Ljava/lang/Object;"
 * </pre>
 *
 * @param namePtr a fully-qualified class name (that is, a package name, delimited by "/",
 *            followed by the class name). If the name begins with "[" (the array signature
 *            character), it returns an array class. The string is encoded in modified UTF-8.
 * @return Returns a class object from a fully-qualified name, or NULL if the class cannot be
 *         found.
 * @throws ClassFormatError if the class data does not specify a valid class.
 * @throws ClassCircularityError if a class or interface would be its own superclass or
 *             superinterface.
 * @throws NoClassDefFoundError if no definition for a requested class or interface can be
 *             found.
 * @throws OutOfMemoryError if the system runs out of memory.
 */
@TruffleBoundary
@JniImpl
@JavaType(Class.class)
public StaticObject FindClass(@Pointer TruffleObject namePtr, @Inject SubstitutionProfiler profiler) {
    String name = NativeUtils.interopPointerToString(namePtr);
    Meta meta = getMeta();
    if (name == null || (name.indexOf('.') > -1)) {
        profiler.profile(7);
        throw meta.throwExceptionWithMessage(meta.java_lang_NoClassDefFoundError, name);
    }
    String internalName = name;
    if (!name.startsWith("[")) {
        // Force 'L' type.
        internalName = "L" + name + ";";
    }
    if (!Validation.validTypeDescriptor(ByteSequence.create(internalName), true)) {
        profiler.profile(6);
        throw meta.throwExceptionWithMessage(meta.java_lang_NoClassDefFoundError, name);
    }
    StaticObject protectionDomain = StaticObject.NULL;
    StaticObject loader = StaticObject.NULL;
    // security stack walk
    StaticObject caller = getVM().JVM_GetCallerClass(0, profiler);
    if (StaticObject.notNull(caller)) {
        Klass callerKlass = caller.getMirrorKlass();
        loader = callerKlass.getDefiningClassLoader();
        if (StaticObject.isNull(loader) && Type.java_lang_ClassLoader$NativeLibrary.equals(callerKlass.getType())) {
            StaticObject result = (StaticObject) getMeta().java_lang_ClassLoader$NativeLibrary_getFromClass.invokeDirect(null);
            loader = result.getMirrorKlass().getDefiningClassLoader();
            protectionDomain = getVM().JVM_GetProtectionDomain(result);
        }
    } else {
        loader = (StaticObject) getMeta().java_lang_ClassLoader_getSystemClassLoader.invokeDirect(null);
    }
    StaticObject guestClass = StaticObject.NULL;
    try {
        String dotName = name.replace('/', '.');
        guestClass = (StaticObject) getMeta().java_lang_Class_forName_String_boolean_ClassLoader.invokeDirect(null, meta.toGuestString(dotName), false, loader);
        EspressoError.guarantee(StaticObject.notNull(guestClass), "Class.forName returned null");
    } catch (EspressoException e) {
        profiler.profile(5);
        if (InterpreterToVM.instanceOf(e.getGuestException(), meta.java_lang_ClassNotFoundException)) {
            profiler.profile(4);
            throw meta.throwExceptionWithMessage(meta.java_lang_NoClassDefFoundError, name);
        }
        throw e;
    }
    meta.HIDDEN_PROTECTION_DOMAIN.setHiddenObject(guestClass, protectionDomain);
    // FindClass should initialize the class.
    guestClass.getMirrorKlass().safeInitialize();
    return guestClass;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) 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) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 12 with Meta

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

the class Method method makeMirror.

public StaticObject makeMirror() {
    Meta meta = getMeta();
    Attribute rawRuntimeVisibleAnnotations = getAttribute(Name.RuntimeVisibleAnnotations);
    StaticObject runtimeVisibleAnnotations = rawRuntimeVisibleAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleAnnotations.getData(), meta) : StaticObject.NULL;
    Attribute rawRuntimeVisibleParameterAnnotations = getAttribute(Name.RuntimeVisibleParameterAnnotations);
    StaticObject runtimeVisibleParameterAnnotations = rawRuntimeVisibleParameterAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleParameterAnnotations.getData(), meta) : StaticObject.NULL;
    Attribute rawRuntimeVisibleTypeAnnotations = getAttribute(Name.RuntimeVisibleTypeAnnotations);
    StaticObject runtimeVisibleTypeAnnotations = rawRuntimeVisibleTypeAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleTypeAnnotations.getData(), meta) : StaticObject.NULL;
    Attribute rawAnnotationDefault = getAttribute(Name.AnnotationDefault);
    StaticObject annotationDefault = rawAnnotationDefault != null ? StaticObject.wrap(rawAnnotationDefault.getData(), meta) : StaticObject.NULL;
    final Klass[] rawParameterKlasses = resolveParameterKlasses();
    StaticObject parameterTypes = meta.java_lang_Class.allocateReferenceArray(getParameterCount(), new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int j) {
            return rawParameterKlasses[j].mirror();
        }
    });
    final Klass[] rawCheckedExceptions = getCheckedExceptions();
    StaticObject guestCheckedExceptions = meta.java_lang_Class.allocateReferenceArray(rawCheckedExceptions.length, new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int j) {
            return rawCheckedExceptions[j].mirror();
        }
    });
    SignatureAttribute signatureAttribute = (SignatureAttribute) getAttribute(Name.Signature);
    StaticObject guestGenericSignature = StaticObject.NULL;
    if (signatureAttribute != null) {
        String sig = getConstantPool().symbolAt(signatureAttribute.getSignatureIndex(), "signature").toString();
        guestGenericSignature = meta.toGuestString(sig);
    }
    StaticObject instance = meta.java_lang_reflect_Method.allocateInstance();
    meta.java_lang_reflect_Method_init.invokeDirect(/* this */
    instance, /* declaringClass */
    getDeclaringKlass().mirror(), /* name */
    getContext().getStrings().intern(getName()), /* parameterTypes */
    parameterTypes, /* returnType */
    resolveReturnKlass().mirror(), /* checkedExceptions */
    guestCheckedExceptions, /* modifiers */
    getMethodModifiers(), /* slot */
    getVTableIndex(), /* signature */
    guestGenericSignature, /* annotations */
    runtimeVisibleAnnotations, /* parameterAnnotations */
    runtimeVisibleParameterAnnotations, /* annotationDefault */
    annotationDefault);
    meta.HIDDEN_METHOD_KEY.setHiddenObject(instance, this);
    meta.HIDDEN_METHOD_RUNTIME_VISIBLE_TYPE_ANNOTATIONS.setHiddenObject(instance, runtimeVisibleTypeAnnotations);
    return instance;
}
Also used : SignatureAttribute(com.oracle.truffle.espresso.classfile.attributes.SignatureAttribute) Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) BootstrapMethodsAttribute(com.oracle.truffle.espresso.classfile.attributes.BootstrapMethodsAttribute) SourceFileAttribute(com.oracle.truffle.espresso.classfile.attributes.SourceFileAttribute) LineNumberTableAttribute(com.oracle.truffle.espresso.classfile.attributes.LineNumberTableAttribute) ExceptionsAttribute(com.oracle.truffle.espresso.classfile.attributes.ExceptionsAttribute) CodeAttribute(com.oracle.truffle.espresso.classfile.attributes.CodeAttribute) Attribute(com.oracle.truffle.espresso.runtime.Attribute) SignatureAttribute(com.oracle.truffle.espresso.classfile.attributes.SignatureAttribute)

Example 13 with Meta

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

the class JniEnv method NewObjectVarargs.

@JniImpl
@JavaType(Object.class)
public StaticObject NewObjectVarargs(@JavaType(Class.class) StaticObject clazz, @Handle(Method.class) long methodId, @Pointer TruffleObject varargsPtr) {
    Method method = methodIds.getObject(methodId);
    assert method.isConstructor();
    Klass klass = clazz.getMirrorKlass();
    if (klass.isInterface() || klass.isAbstract()) {
        Meta meta = getMeta();
        throw meta.throwException(meta.java_lang_InstantiationException);
    }
    klass.initialize();
    StaticObject instance;
    if (CompilerDirectives.isPartialEvaluationConstant(klass)) {
        instance = klass.allocateInstance();
    } else {
        instance = allocateBoundary(klass);
    }
    method.invokeDirect(instance, popVarArgs(varargsPtr, method.getParsedSignature()));
    return instance;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 14 with Meta

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

the class JniEnv method RegisterNative.

// endregion DirectBuffers
// region Register/Unregister natives
@JniImpl
@TruffleBoundary
public int RegisterNative(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject methodNamePtr, @Pointer TruffleObject methodSignaturePtr, @Pointer TruffleObject closure) {
    String methodName = NativeUtils.interopPointerToString(methodNamePtr);
    String methodSignature = NativeUtils.interopPointerToString(methodSignaturePtr);
    assert methodName != null && methodSignature != null;
    Symbol<Name> name = getNames().lookup(methodName);
    Symbol<Signature> signature = getSignatures().lookupValidSignature(methodSignature);
    Meta meta = getMeta();
    if (name == null || signature == null) {
        setPendingException(Meta.initException(meta.java_lang_NoSuchMethodError));
        return JNI_ERR;
    }
    Method targetMethod = clazz.getMirrorKlass().lookupDeclaredMethod(name, signature);
    if (targetMethod != null && targetMethod.isNative()) {
        targetMethod.unregisterNative();
        getSubstitutions().removeRuntimeSubstitution(targetMethod);
    } else {
        setPendingException(Meta.initException(meta.java_lang_NoSuchMethodError));
        return JNI_ERR;
    }
    Substitutions.EspressoRootNodeFactory factory = null;
    // Lookup known VM methods to shortcut native boudaries.
    factory = lookupKnownVmMethods(closure, targetMethod);
    if (factory == null) {
        NativeSignature ns = Method.buildJniNativeSignature(targetMethod.getParsedSignature());
        final TruffleObject boundNative = getNativeAccess().bindSymbol(closure, ns);
        factory = createJniRootNodeFactory(() -> new NativeMethodNode(boundNative, targetMethod.getMethodVersion()), targetMethod);
    }
    Symbol<Type> classType = clazz.getMirrorKlass().getType();
    getSubstitutions().registerRuntimeSubstitution(classType, name, signature, factory, true);
    return JNI_OK;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Method(com.oracle.truffle.espresso.impl.Method) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) NativeSignature(com.oracle.truffle.espresso.ffi.NativeSignature) NativeType(com.oracle.truffle.espresso.ffi.NativeType) Type(com.oracle.truffle.espresso.descriptors.Symbol.Type) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) NativeSignature(com.oracle.truffle.espresso.ffi.NativeSignature) Signature(com.oracle.truffle.espresso.descriptors.Symbol.Signature) Substitutions(com.oracle.truffle.espresso.substitutions.Substitutions) NativeMethodNode(com.oracle.truffle.espresso.nodes.NativeMethodNode) IntrinsifiedNativeMethodNode(com.oracle.truffle.espresso.nodes.IntrinsifiedNativeMethodNode) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 15 with Meta

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

the class JniEnv method GetStringUTFRegion.

@JniImpl
@TruffleBoundary
public void GetStringUTFRegion(@JavaType(String.class) StaticObject str, int start, int len, @Pointer TruffleObject bufPtr) {
    Meta meta = getMeta();
    int length = ModifiedUtf8.utfLength(meta.toHostString(str));
    if (start < 0 || start + (long) len > length) {
        throw meta.throwException(meta.java_lang_StringIndexOutOfBoundsException);
    }
    // always
    byte[] bytes = ModifiedUtf8.asUtf(meta.toHostString(str), start, len, true);
    // 0
    // terminated.
    ByteBuffer buf = NativeUtils.directByteBuffer(bufPtr, bytes.length, JavaKind.Byte);
    buf.put(bytes);
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) ByteBuffer(java.nio.ByteBuffer) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

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