Search in sources :

Example 31 with Meta

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

the class VM method JVM_GetClassDeclaredMethods.

// TODO(tg): inject constructor calltarget.
@VmImpl(isJni = true)
@JavaType(java.lang.reflect.Method[].class)
public StaticObject JVM_GetClassDeclaredMethods(@JavaType(Class.class) StaticObject self, boolean publicOnly) {
    Meta meta = getMeta();
    ArrayList<Method> collectedMethods = new ArrayList<>();
    Klass klass = self.getMirrorKlass();
    klass.ensureLinked();
    for (Method m : klass.getDeclaredMethods()) {
        if ((!publicOnly || m.isPublic()) && // Filter out <init> and <clinit> from reflection.
        !Name._init_.equals(m.getName()) && !Name._clinit_.equals(m.getName())) {
            collectedMethods.add(m);
        }
    }
    final Method[] methods = collectedMethods.toArray(Method.EMPTY_ARRAY);
    return meta.java_lang_reflect_Method.allocateReferenceArray(methods.length, new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int i) {
            return methods[i].makeMirror();
        }
    });
}
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) ArrayList(java.util.ArrayList) Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 32 with Meta

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

the class ClassRedefinition method handleRemovedMethod.

/**
 * @param accessingKlass the receiver's klass when the method is not static, resolutionSeed's
 *            declaring klass otherwise
 */
@TruffleBoundary
public Method handleRemovedMethod(Method resolutionSeed, Klass accessingKlass) {
    // wait for potential ongoing redefinition to complete
    check();
    Method replacementMethod = accessingKlass.lookupMethod(resolutionSeed.getName(), resolutionSeed.getRawSignature(), accessingKlass);
    Meta meta = resolutionSeed.getMeta();
    if (replacementMethod == null) {
        throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, meta.toGuestString(resolutionSeed.getDeclaringKlass().getNameAsString() + "." + resolutionSeed.getName() + resolutionSeed.getRawSignature()) + " was removed by class redefinition");
    } else if (resolutionSeed.isStatic() != replacementMethod.isStatic()) {
        String message = resolutionSeed.isStatic() ? "expected static method: " : "expected non-static method:" + replacementMethod.getName();
        throw meta.throwExceptionWithMessage(meta.java_lang_IncompatibleClassChangeError, message);
    } else {
        // Update to the latest version of the replacement method
        return replacementMethod;
    }
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) ParserMethod(com.oracle.truffle.espresso.impl.ParserMethod) Method(com.oracle.truffle.espresso.impl.Method) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 33 with Meta

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

the class StaticObject method toVerboseString.

@TruffleBoundary
public final String toVerboseString() {
    if (this == NULL) {
        return "null";
    }
    if (isForeignObject()) {
        return String.format("foreign object: %s\n%s", getKlass().getTypeAsString(), InteropLibrary.getUncached().toDisplayString(rawForeignObject()));
    }
    if (getKlass() == getKlass().getMeta().java_lang_String) {
        Meta meta = getKlass().getMeta();
        StaticObject value = meta.java_lang_String_value.getObject(this);
        if (value == null || isNull(value)) {
            // Prevents debugger crashes when trying to inspect a string in construction.
            return "<UNINITIALIZED>";
        }
        return Meta.toHostStringStatic(this);
    }
    if (isArray()) {
        return unwrap().toString();
    }
    if (getKlass() == getKlass().getMeta().java_lang_Class) {
        return "mirror: " + getMirrorKlass().toString();
    }
    StringBuilder str = new StringBuilder(getKlass().getType().toString());
    for (Field f : ((ObjectKlass) getKlass()).getFieldTable()) {
        // Also prints hidden fields
        if (!f.isRemoved()) {
            str.append("\n    ").append(f.getName()).append(": ").append(f.get(this).toString());
        }
    }
    return str.toString();
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Field(com.oracle.truffle.espresso.impl.Field) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 34 with Meta

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

the class EspressoContext method spawnVM.

@SuppressWarnings("try")
private void spawnVM() {
    try (DebugCloseable spawn = SPAWN_VM.scope(timers)) {
        long initStartTimeNanos = System.nanoTime();
        this.nativeAccess = spawnNativeAccess();
        initVmProperties();
        // Spawn JNI first, then the VM.
        try (DebugCloseable vmInit = VM_INIT.scope(timers)) {
            // Mokapot is loaded
            this.vm = VM.create(getJNI());
            vm.attachThread(Thread.currentThread());
        }
        if (getJavaVersion().modulesEnabled()) {
            registries.initJavaBaseModule();
            registries.getBootClassRegistry().initUnnamedModule(StaticObject.NULL);
        }
        // TODO: link libjimage
        initializeAgents();
        try (DebugCloseable metaInit = META_INIT.scope(timers)) {
            this.meta = new Meta(this);
        }
        this.metaInitialized = true;
        this.threads = new ThreadsAccess(meta);
        this.blockingSupport = BlockingSupport.create(threads);
        this.interpreterToVM = new InterpreterToVM(this);
        try (DebugCloseable knownClassInit = KNOWN_CLASS_INIT.scope(timers)) {
            initializeKnownClass(Type.java_lang_Object);
            for (Symbol<Type> type : Arrays.asList(Type.java_lang_String, Type.java_lang_System, // JDK-8069005
            Type.java_lang_Class, Type.java_lang_ThreadGroup, Type.java_lang_Thread)) {
                initializeKnownClass(type);
            }
        }
        if (meta.jdk_internal_misc_UnsafeConstants != null) {
            initializeKnownClass(Type.jdk_internal_misc_UnsafeConstants);
            UnsafeAccess.initializeGuestUnsafeConstants(meta);
        }
        // Create main thread as soon as Thread class is initialized.
        threadRegistry.createMainThread(meta);
        try (DebugCloseable knownClassInit = KNOWN_CLASS_INIT.scope(timers)) {
            initializeKnownClass(Type.java_lang_Object);
            for (Symbol<Type> type : Arrays.asList(Type.java_lang_reflect_Method, Type.java_lang_ref_Finalizer)) {
                initializeKnownClass(type);
            }
        }
        referenceDrainer.initReferenceDrain();
        try (DebugCloseable systemInit = SYSTEM_INIT.scope(timers)) {
            // Call guest initialization
            if (getJavaVersion().java8OrEarlier()) {
                meta.java_lang_System_initializeSystemClass.invokeDirect(null);
            } else {
                assert getJavaVersion().java9OrLater();
                meta.java_lang_System_initPhase1.invokeDirect(null);
                int e = (int) meta.java_lang_System_initPhase2.invokeDirect(null, false, false);
                if (e != 0) {
                    throw EspressoError.shouldNotReachHere();
                }
                getVM().getJvmti().postVmStart();
                modulesInitialized = true;
                meta.java_lang_System_initPhase3.invokeDirect(null);
            }
        }
        getVM().getJvmti().postVmInit();
        meta.postSystemInit();
        try (DebugCloseable knownClassInit = KNOWN_CLASS_INIT.scope(timers)) {
            // System exceptions.
            for (Symbol<Type> type : Arrays.asList(Type.java_lang_OutOfMemoryError, Type.java_lang_NullPointerException, Type.java_lang_ClassCastException, Type.java_lang_ArrayStoreException, Type.java_lang_ArithmeticException, Type.java_lang_StackOverflowError, Type.java_lang_IllegalMonitorStateException, Type.java_lang_IllegalArgumentException)) {
                initializeKnownClass(type);
            }
        }
        // Init memoryError instances
        StaticObject stackOverflowErrorInstance = meta.java_lang_StackOverflowError.allocateInstance();
        StaticObject outOfMemoryErrorInstance = meta.java_lang_OutOfMemoryError.allocateInstance();
        // Preemptively set stack trace.
        meta.HIDDEN_FRAMES.setHiddenObject(stackOverflowErrorInstance, VM.StackTrace.EMPTY_STACK_TRACE);
        meta.java_lang_Throwable_backtrace.setObject(stackOverflowErrorInstance, stackOverflowErrorInstance);
        meta.HIDDEN_FRAMES.setHiddenObject(outOfMemoryErrorInstance, VM.StackTrace.EMPTY_STACK_TRACE);
        meta.java_lang_Throwable_backtrace.setObject(outOfMemoryErrorInstance, outOfMemoryErrorInstance);
        this.stackOverflow = EspressoException.wrap(stackOverflowErrorInstance, meta);
        this.outOfMemory = EspressoException.wrap(outOfMemoryErrorInstance, meta);
        meta.java_lang_StackOverflowError.lookupDeclaredMethod(Name._init_, Signature._void_String).invokeDirect(stackOverflowErrorInstance, meta.toGuestString("VM StackOverFlow"));
        meta.java_lang_OutOfMemoryError.lookupDeclaredMethod(Name._init_, Signature._void_String).invokeDirect(outOfMemoryErrorInstance, meta.toGuestString("VM OutOfMemory"));
        // Create application (system) class loader.
        StaticObject systemClassLoader = null;
        try (DebugCloseable systemLoader = SYSTEM_CLASSLOADER.scope(timers)) {
            systemClassLoader = (StaticObject) meta.java_lang_ClassLoader_getSystemClassLoader.invokeDirect(null);
        }
        topBindings = new EspressoBindings(systemClassLoader, getEnv().getOptions().get(EspressoOptions.ExposeNativeJavaVM));
        initDoneTimeNanos = System.nanoTime();
        long elapsedNanos = initDoneTimeNanos - initStartTimeNanos;
        getLogger().log(Level.FINE, "VM booted in {0} ms", TimeUnit.NANOSECONDS.toMillis(elapsedNanos));
    }
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Type(com.oracle.truffle.espresso.descriptors.Symbol.Type) ThreadsAccess(com.oracle.truffle.espresso.threads.ThreadsAccess) InterpreterToVM(com.oracle.truffle.espresso.vm.InterpreterToVM) EspressoBindings(com.oracle.truffle.espresso.EspressoBindings) DebugCloseable(com.oracle.truffle.espresso.perf.DebugCloseable)

Example 35 with Meta

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

the class EspressoReferenceDrainer method initReferenceDrain.

void initReferenceDrain() {
    TruffleLanguage.Env env = getContext().getEnv();
    Meta meta = getMeta();
    if (getContext().multiThreadingEnabled()) {
        if (getJavaVersion().java8OrEarlier()) {
            // Initialize reference queue
            this.hostToGuestReferenceDrainThread = env.createThread(new ReferenceDrain() {

                @SuppressWarnings("rawtypes")
                @Override
                protected void updateReferencePendingList(EspressoReference head, EspressoReference prev, StaticObject lock) {
                    StaticObject obj = meta.java_lang_ref_Reference_pending.getAndSetObject(meta.java_lang_ref_Reference.getStatics(), head.getGuestReference());
                    meta.java_lang_ref_Reference_discovered.set(prev.getGuestReference(), obj);
                    getVM().JVM_MonitorNotify(lock, profiler);
                }
            });
        } else if (getJavaVersion().java9OrLater()) {
            // Initialize reference queue
            this.hostToGuestReferenceDrainThread = env.createThread(new ReferenceDrain() {

                @SuppressWarnings("rawtypes")
                @Override
                protected void updateReferencePendingList(EspressoReference head, EspressoReference prev, StaticObject lock) {
                    EspressoLock pLock = getLock();
                    pLock.lock();
                    try {
                        StaticObject obj = referencePendingList;
                        referencePendingList = head.getGuestReference();
                        meta.java_lang_ref_Reference_discovered.set(prev.getGuestReference(), obj);
                        getVM().JVM_MonitorNotify(lock, profiler);
                        pLock.signalAll();
                    } finally {
                        pLock.unlock();
                    }
                }
            });
        } else {
            throw EspressoError.shouldNotReachHere();
        }
    }
    if (hostToGuestReferenceDrainThread != null) {
        hostToGuestReferenceDrainThread.setName("Reference Drain");
    }
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) EspressoReference(com.oracle.truffle.espresso.ref.EspressoReference) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) EspressoLock(com.oracle.truffle.espresso.blocking.EspressoLock)

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