Search in sources :

Example 1 with ThreadsAccess

use of com.oracle.truffle.espresso.threads.ThreadsAccess 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)

Aggregations

EspressoBindings (com.oracle.truffle.espresso.EspressoBindings)1 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)1 Meta (com.oracle.truffle.espresso.meta.Meta)1 DebugCloseable (com.oracle.truffle.espresso.perf.DebugCloseable)1 ThreadsAccess (com.oracle.truffle.espresso.threads.ThreadsAccess)1 InterpreterToVM (com.oracle.truffle.espresso.vm.InterpreterToVM)1