Search in sources :

Example 66 with StaticObject

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

the class Target_sun_misc_Unsafe method arrayIndexScale.

/**
 * Report the scale factor for addressing elements in the storage allocation of a given array
 * class. However, arrays of "narrow" types will generally not work properly with accessors like
 * {@link #getByte}, so the scale factor for such classes is reported as zero.
 *
 * @see #arrayBaseOffset
 * @see #getInt
 * @see #putInt
 */
@Substitution(hasReceiver = true, nameProvider = SharedUnsafeAppend0.class)
public static int arrayIndexScale(@SuppressWarnings("unused") @JavaType(Unsafe.class) StaticObject self, @JavaType(Class.class) StaticObject clazz, @Inject Meta meta) {
    Unsafe unsafe = UnsafeAccess.getIfAllowed(meta);
    Klass klass = clazz.getMirrorKlass();
    assert klass.isArray();
    if (((ArrayKlass) klass).getComponentType().isPrimitive()) {
        Class<?> hostPrimitive = ((ArrayKlass) klass).getComponentType().getJavaKind().toJavaClass();
        return unsafe.arrayIndexScale(Array.newInstance(hostPrimitive, 0).getClass());
    } else {
        // Just a reference type.
        return unsafe.arrayIndexScale(Object[].class);
    }
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) Unsafe(sun.misc.Unsafe) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject)

Example 67 with StaticObject

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

the class Management method DumpThreads.

@ManagementImpl
@SuppressWarnings("unused")
@JavaType(internalName = "[Ljava/lang/management/ThreadInfo;")
public StaticObject DumpThreads(@JavaType(long[].class) StaticObject ids, boolean lockedMonitors, boolean lockedSynchronizers, @Inject SubstitutionProfiler profiler) {
    StaticObject threadIds = ids;
    if (StaticObject.isNull(threadIds)) {
        StaticObject[] activeThreads = getContext().getActiveThreads();
        threadIds = InterpreterToVM.allocatePrimitiveArray((byte) JavaKind.Long.getBasicType(), activeThreads.length, getMeta());
        for (int j = 0; j < activeThreads.length; ++j) {
            long tid = getThreadAccess().getThreadId(activeThreads[j]);
            getInterpreterToVM().setArrayLong(tid, j, threadIds);
        }
    }
    StaticObject result = getMeta().java_lang_management_ThreadInfo.allocateReferenceArray(threadIds.length());
    if (GetThreadInfo(threadIds, 0, result, profiler) != JNI_OK) {
        return StaticObject.NULL;
    }
    return result;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 68 with StaticObject

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

the class UnsafeAccess method initializeGuestUnsafeConstants.

public static void initializeGuestUnsafeConstants(Meta meta) {
    /*
         * To obtain the unobtainable fields, we would need to have one such method per supported
         * host java version
         */
    StaticObject staticStorage = meta.jdk_internal_misc_UnsafeConstants.tryInitializeAndGetStatics();
    meta.jdk_internal_misc_UnsafeConstants_ADDRESS_SIZE0.set(staticStorage, UNSAFE.addressSize());
    meta.jdk_internal_misc_UnsafeConstants_PAGE_SIZE.set(staticStorage, UNSAFE.pageSize());
    meta.jdk_internal_misc_UnsafeConstants_BIG_ENDIAN.set(staticStorage, ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);
    meta.jdk_internal_misc_UnsafeConstants_UNALIGNED_ACCESS.set(staticStorage, Target_sun_misc_Unsafe.unalignedAccess0(/*- Ignored guest Unsafe */
    StaticObject.NULL));
    meta.jdk_internal_misc_UnsafeConstants_DATA_CACHE_LINE_FLUSH_SIZE.set(staticStorage, /*- Unobtainable */
    0);
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject)

Example 69 with StaticObject

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

the class InnerClassRedefiner method fetchMissingInnerClasses.

private void fetchMissingInnerClasses(HotSwapClassInfo hotswapInfo) throws RedefintionNotSupportedException {
    StaticObject definingLoader = hotswapInfo.getClassLoader();
    ArrayList<Symbol<Symbol.Name>> innerNames = new ArrayList<>(1);
    try {
        searchConstantPoolForInnerClassNames(hotswapInfo, innerNames);
    } catch (ClassFormatError ex) {
        throw new RedefintionNotSupportedException(ErrorCodes.INVALID_CLASS_FORMAT);
    }
    // poke the defining guest classloader for the resources
    for (Symbol<Symbol.Name> innerName : innerNames) {
        if (!hotswapInfo.knowsInnerClass(innerName)) {
            byte[] classBytes = null;
            StaticObject resourceGuestString = context.getMeta().toGuestString(innerName + ".class");
            StaticObject inputStream = (StaticObject) context.getMeta().java_lang_ClassLoader_getResourceAsStream.invokeDirect(definingLoader, resourceGuestString);
            if (StaticObject.notNull(inputStream)) {
                classBytes = readAllBytes(inputStream);
            } else {
            // There is no safe way to retrieve the class bytes using e.g. a scheme using
            // j.l.ClassLoader#loadClass and special marker for the type to have the call
            // end up in defineClass where we could grab the bytes. Guest language
            // classloaders in many cases have caches for the class name that prevents
            // forcefully attempting to load previously not loadable classes.
            // without the class bytes, the matching is less precise and cached un-matched
            // inner class instances that are executed after redefintion will lead to
            // NoSuchMethod errors because they're marked as removed
            }
            if (classBytes != null) {
                hotswapInfo.addInnerClass(ClassInfo.create(innerName, classBytes, definingLoader, context));
            }
        }
    }
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Symbol(com.oracle.truffle.espresso.descriptors.Symbol) ArrayList(java.util.ArrayList)

Example 70 with StaticObject

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

the class InnerClassRedefiner method readAllBytes.

private byte[] readAllBytes(StaticObject inputStream) {
    byte[] buf = new byte[4 * 0x400];
    StaticObject guestBuf = StaticObject.wrap(buf, context.getMeta());
    int readLen;
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        while ((readLen = (int) context.getMeta().java_io_InputStream_read.invokeDirect(inputStream, guestBuf, 0, buf.length)) != -1) {
            byte[] bytes = guestBuf.unwrap();
            outputStream.write(bytes, 0, readLen);
        }
        return outputStream.toByteArray();
    } catch (IOException ex) {
        return new byte[0];
    } finally {
        context.getMeta().java_io_InputStream_close.invokeDirect(inputStream);
    }
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

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