Search in sources :

Example 56 with Meta

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

the class VM method JVM_LoadLibrary.

@VmImpl
@TruffleBoundary
@Pointer
public TruffleObject JVM_LoadLibrary(@Pointer TruffleObject namePtr) {
    String name = NativeUtils.interopPointerToString(namePtr);
    getLogger().fine(String.format("JVM_LoadLibrary: '%s'", name));
    TruffleObject lib = getNativeAccess().loadLibrary(Paths.get(name));
    if (lib == null) {
        Meta meta = getMeta();
        throw meta.throwExceptionWithMessage(meta.java_lang_UnsatisfiedLinkError, name);
    }
    long handle = getLibraryHandle(lib);
    handle2Lib.put(handle, lib);
    getLogger().fine(String.format("JVM_LoadLibrary: Successfully loaded '%s' with handle %x", name, handle));
    return RawPointer.create(handle);
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) Pointer(com.oracle.truffle.espresso.ffi.Pointer) RawPointer(com.oracle.truffle.espresso.ffi.RawPointer)

Example 57 with Meta

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

the class VM method extractNativePackages.

private String[] extractNativePackages(TruffleObject pkgs, int numPackages, SubstitutionProfiler profiler) {
    String[] packages = new String[numPackages];
    try {
        for (int i = 0; i < numPackages; i++) {
            String pkg = NativeUtils.interopPointerToString((TruffleObject) getUncached().execute(getPackageAt, pkgs, i));
            if (!Validation.validBinaryName(pkg)) {
                profiler.profile(7);
                Meta meta = getMeta();
                throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, cat("Invalid package name: ", pkg));
            }
            packages[i] = pkg;
        }
    } catch (UnsupportedMessageException | ArityException | UnsupportedTypeException e) {
        throw EspressoError.shouldNotReachHere();
    }
    return packages;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) ArityException(com.oracle.truffle.api.interop.ArityException) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint)

Example 58 with Meta

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

the class BytecodeNode method putField.

// endregion Misc. checks
// region Field read/write
/**
 * Returns the stack effect (slot delta) that cannot be inferred solely from the bytecode. e.g.
 * GETFIELD always pops the receiver, but the (read) result size (1 or 2) is unknown.
 *
 * <pre>
 *   top += putField(frame, top, resolveField(...)); break; // stack effect that depends on the field
 *   top += Bytecodes.stackEffectOf(curOpcode); // stack effect that depends solely on PUTFIELD.
 *   // at this point `top` must have the correct value.
 *   curBCI = bs.next(curBCI);
 * </pre>
 */
private int putField(VirtualFrame frame, int top, Field field, int curBCI, int opcode, int statementIndex) {
    assert opcode == PUTFIELD || opcode == PUTSTATIC;
    CompilerAsserts.partialEvaluationConstant(field);
    /*
         * PUTFIELD: Otherwise, if the resolved field is a static field, putfield throws an
         * IncompatibleClassChangeError.
         *
         * PUTSTATIC: Otherwise, if the resolved field is not a static (class) field or an interface
         * field, putstatic throws an IncompatibleClassChangeError.
         */
    if (field.isStatic() != (opcode == PUTSTATIC)) {
        CompilerDirectives.transferToInterpreter();
        Meta meta = getMeta();
        throw meta.throwExceptionWithMessage(meta.java_lang_IncompatibleClassChangeError, String.format("Expected %s field %s.%s", (opcode == PUTSTATIC) ? "static" : "non-static", field.getDeclaringKlass().getNameAsString(), field.getNameAsString()));
    }
    /*
         * PUTFIELD: Otherwise, if the field is final, it must be declared in the current class, and
         * the instruction must occur in an instance initialization method (<init>) of the current
         * class. Otherwise, an IllegalAccessError is thrown.
         *
         * PUTSTATIC: Otherwise, if the field is final, it must be declared in the current class,
         * and the instruction must occur in the <clinit> method of the current class. Otherwise, an
         * IllegalAccessError is thrown.
         */
    if (field.isFinalFlagSet()) {
        if (field.getDeclaringKlass() != getDeclaringKlass()) {
            CompilerDirectives.transferToInterpreter();
            Meta meta = getMeta();
            throw meta.throwExceptionWithMessage(meta.java_lang_IllegalAccessError, String.format("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class", (opcode == PUTSTATIC) ? "static" : "non-static", field.getDeclaringKlass().getNameAsString(), field.getNameAsString(), getDeclaringKlass().getNameAsString()));
        }
        boolean enforceInitializerCheck = (getContext().SpecCompliancyMode == STRICT) || // HotSpot enforces this only for >= Java 9 (v53) .class files.
        field.getDeclaringKlass().getMajorVersion() >= ClassfileParser.JAVA_9_VERSION;
        if (enforceInitializerCheck && ((opcode == PUTFIELD && !getMethod().isConstructor()) || (opcode == PUTSTATIC && !getMethod().isClassInitializer()))) {
            CompilerDirectives.transferToInterpreter();
            Meta meta = getMeta();
            throw meta.throwExceptionWithMessage(meta.java_lang_IllegalAccessError, String.format("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ", (opcode == PUTSTATIC) ? "static" : "non-static", field.getDeclaringKlass().getNameAsString(), field.getNameAsString(), getMethod().getNameAsString(), (opcode == PUTSTATIC) ? "<clinit>" : "<init>"));
        }
    }
    assert field.isStatic() == (opcode == PUTSTATIC);
    // -receiver
    int slot = top - field.getKind().getSlotCount() - 1;
    StaticObject receiver = field.isStatic() ? field.getDeclaringKlass().tryInitializeAndGetStatics() : // Do not release the object, it might be read again in PutFieldNode
    nullCheck(popObject(frame, slot));
    if (!noForeignObjects.isValid() && opcode == PUTFIELD) {
        if (receiver.isForeignObject()) {
            // Restore the receiver for quickening.
            putObject(frame, slot, receiver);
            return quickenPutField(frame, top, curBCI, opcode, statementIndex, field);
        }
    }
    switch(field.getKind()) {
        case Boolean:
            boolean booleanValue = stackIntToBoolean(popInt(frame, top - 1));
            if (instrumentation != null) {
                instrumentation.notifyFieldModification(frame, statementIndex, field, receiver, booleanValue);
            }
            InterpreterToVM.setFieldBoolean(booleanValue, receiver, field);
            break;
        case Byte:
            byte byteValue = (byte) popInt(frame, top - 1);
            if (instrumentation != null) {
                instrumentation.notifyFieldModification(frame, statementIndex, field, receiver, byteValue);
            }
            InterpreterToVM.setFieldByte(byteValue, receiver, field);
            break;
        case Char:
            char charValue = (char) popInt(frame, top - 1);
            if (instrumentation != null) {
                instrumentation.notifyFieldModification(frame, statementIndex, field, receiver, charValue);
            }
            InterpreterToVM.setFieldChar(charValue, receiver, field);
            break;
        case Short:
            short shortValue = (short) popInt(frame, top - 1);
            if (instrumentation != null) {
                instrumentation.notifyFieldModification(frame, statementIndex, field, receiver, shortValue);
            }
            InterpreterToVM.setFieldShort(shortValue, receiver, field);
            break;
        case Int:
            int intValue = popInt(frame, top - 1);
            if (instrumentation != null) {
                instrumentation.notifyFieldModification(frame, statementIndex, field, receiver, intValue);
            }
            InterpreterToVM.setFieldInt(intValue, receiver, field);
            break;
        case Double:
            double doubleValue = popDouble(frame, top - 1);
            if (instrumentation != null) {
                instrumentation.notifyFieldModification(frame, statementIndex, field, receiver, doubleValue);
            }
            InterpreterToVM.setFieldDouble(doubleValue, receiver, field);
            break;
        case Float:
            float floatValue = popFloat(frame, top - 1);
            if (instrumentation != null) {
                instrumentation.notifyFieldModification(frame, statementIndex, field, receiver, floatValue);
            }
            InterpreterToVM.setFieldFloat(floatValue, receiver, field);
            break;
        case Long:
            long longValue = popLong(frame, top - 1);
            if (instrumentation != null) {
                instrumentation.notifyFieldModification(frame, statementIndex, field, receiver, longValue);
            }
            InterpreterToVM.setFieldLong(longValue, receiver, field);
            break;
        case Object:
            StaticObject value = popObject(frame, top - 1);
            if (instrumentation != null) {
                instrumentation.notifyFieldModification(frame, statementIndex, field, receiver, value);
            }
            InterpreterToVM.setFieldObject(value, receiver, field);
            break;
        default:
            CompilerDirectives.transferToInterpreter();
            throw EspressoError.shouldNotReachHere("unexpected kind");
    }
    return -field.getKind().getSlotCount();
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) TruffleSafepoint(com.oracle.truffle.api.TruffleSafepoint)

Example 59 with Meta

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

the class JniEnv method GetStaticFieldID.

/**
 * <h3>jfieldID GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig);
 * </h3>
 * <p>
 * Returns the field ID for a static field of a class. The field is specified by its name and
 * signature. The GetStatic<type>Field and SetStatic<type>Field families of accessor functions
 * use field IDs to retrieve static fields.
 * <p>
 * GetStaticFieldID() causes an uninitialized class to be initialized.
 *
 * @param clazz a Java class object.
 * @param namePtr the static field name in a 0-terminated modified UTF-8 string.
 * @param typePtr the field signature in a 0-terminated modified UTF-8 string.
 * @return a field ID, or NULL if the specified static field cannot be found.
 * @throws NoSuchFieldError if the specified static field cannot be found.
 * @throws ExceptionInInitializerError if the class initializer fails due to an exception.
 * @throws OutOfMemoryError if the system runs out of memory.
 */
@JniImpl
@Handle(Field.class)
public long GetStaticFieldID(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject namePtr, @Pointer TruffleObject typePtr) {
    String name = NativeUtils.interopPointerToString(namePtr);
    String type = NativeUtils.interopPointerToString(typePtr);
    assert name != null && type != null;
    Field field = null;
    Symbol<Name> fieldName = getNames().lookup(name);
    if (fieldName != null) {
        Symbol<Type> fieldType = getTypes().lookup(type);
        if (fieldType != null) {
            Klass klass = clazz.getMirrorKlass();
            klass.safeInitialize();
            // Lookup only if name and type are known symbols.
            field = klass.lookupField(fieldName, fieldType, Klass.LookupMode.STATIC_ONLY);
            assert field == null || field.getType().equals(fieldType);
        }
    }
    if (field == null || !field.isStatic()) {
        Meta meta = getMeta();
        throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchFieldError, name);
    }
    return fieldIds.handlify(field);
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) Meta(com.oracle.truffle.espresso.meta.Meta) NativeType(com.oracle.truffle.espresso.ffi.NativeType) Type(com.oracle.truffle.espresso.descriptors.Symbol.Type) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name)

Example 60 with Meta

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

the class JniEnv method NewDirectByteBuffer.

// endregion Release*ArrayElements
// region DirectBuffers
/**
 * <h3>jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity);</h3>
 * <p>
 * Allocates and returns a direct java.nio.ByteBuffer referring to the block of memory starting
 * at the memory address address and extending capacity bytes.
 * <p>
 * Native code that calls this function and returns the resulting byte-buffer object to
 * Java-level code should ensure that the buffer refers to a valid region of memory that is
 * accessible for reading and, if appropriate, writing. An attempt to access an invalid memory
 * location from Java code will either return an arbitrary value, have no visible effect, or
 * cause an unspecified exception to be thrown.
 *
 * @param addressPtr the starting address of the memory region (must not be NULL)
 * @param capacity the size in bytes of the memory region (must be positive)
 * @return a local reference to the newly-instantiated java.nio.ByteBuffer object. Returns NULL
 *         if an exception occurs, or if JNI access to direct buffers is not supported by this
 *         virtual machine.
 * @throws OutOfMemoryError if allocation of the ByteBuffer object fails
 */
@JniImpl
@JavaType(internalName = "Ljava/nio/DirectByteBuffer;")
public StaticObject NewDirectByteBuffer(@Pointer TruffleObject addressPtr, long capacity) {
    Meta meta = getMeta();
    StaticObject instance = meta.java_nio_DirectByteBuffer.allocateInstance();
    long address = NativeUtils.interopAsPointer(addressPtr);
    meta.java_nio_DirectByteBuffer_init_long_int.invokeDirect(instance, address, (int) capacity);
    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)

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