1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8136421
  27  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  28  * @library / /testlibrary
  29  * @build compiler.jvmci.common.JVMCIHelpers
  30  *     compiler.jvmci.events.JvmciCompleteInitializationTest
  31  * @run main jdk.test.lib.FileInstaller ../common/services/ ./META-INF/services/
  32  * @run main jdk.test.lib.FileInstaller ./JvmciCompleteInitializationTest.config
  33  *     ./META-INF/services/jdk.vm.ci.hotspot.HotSpotVMEventListener
  34  * @run main ClassFileInstaller
  35  *     compiler.jvmci.common.JVMCIHelpers$EmptyHotspotCompiler
  36  *     compiler.jvmci.common.JVMCIHelpers$EmptyCompilerFactory
  37  *     compiler.jvmci.events.JvmciCompleteInitializationTest
  38  *     jdk.test.lib.Asserts
  39  * @run main/othervm -XX:+UnlockExperimentalVMOptions
  40  *     -Xbootclasspath/a:.
  41  *     -XX:+EnableJVMCI
  42  *     -Dcompiler.jvmci.events.JvmciCompleteInitializationTest.positive=true
  43  *     compiler.jvmci.events.JvmciCompleteInitializationTest
  44  * @run main/othervm -XX:+UnlockExperimentalVMOptions
  45  *     -Xbootclasspath/a:.
  46  *     -XX:-EnableJVMCI
  47  *     -Dcompiler.jvmci.events.JvmciCompleteInitializationTest.positive=false
  48  *     compiler.jvmci.events.JvmciCompleteInitializationTest
  49  */
  50 
  51 package compiler.jvmci.events;
  52 
  53 import jdk.test.lib.Asserts;
  54 import jdk.vm.ci.hotspot.HotSpotVMEventListener;
  55 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  56 
  57 public class JvmciCompleteInitializationTest implements HotSpotVMEventListener {
  58     private static final boolean IS_POSITIVE = Boolean.getBoolean(
  59             "compiler.jvmci.events.JvmciCompleteInitializationTest.positive");
  60     private static volatile int completeInitializationCount = 0;
  61     private static volatile String errorMessage = "";
  62 
  63     public static void main(String args[]) {
  64         if (completeInitializationCount != 0) {
  65             throw new Error("Unexpected completeInitialization events"
  66                     + " count at start");
  67         }
  68         initializeRuntime();
  69         int expectedEventCount = IS_POSITIVE ? 1 : 0;
  70         Asserts.assertEQ(completeInitializationCount, expectedEventCount,
  71                 "Unexpected completeInitialization events count"
  72                         + " after JVMCI init");
  73         initializeRuntime();
  74         Asserts.assertEQ(completeInitializationCount, expectedEventCount,
  75                 "Unexpected completeInitialization events count"
  76                         + " after 2nd JVMCI init");
  77         Asserts.assertTrue(errorMessage.isEmpty(), errorMessage);
  78     }
  79 
  80     private static void initializeRuntime() {
  81         Error t = null;
  82         try {
  83             /* in case JVMCI disabled, an InternalError on initialization
  84                and NoClassDefFound on 2nd try */
  85             HotSpotJVMCIRuntime.runtime();
  86         } catch (Error e) {
  87             t = e;
  88         }
  89         if (IS_POSITIVE) {
  90             Asserts.assertNull(t, "Caught unexpected exception");
  91         } else {
  92             Asserts.assertNotNull(t, "Got no expected error");
  93         }
  94     }
  95 
  96     @Override
  97     public void completeInitialization(HotSpotJVMCIRuntime
  98             hotSpotJVMCIRuntime) {
  99         completeInitializationCount++;
 100         if (hotSpotJVMCIRuntime == null) {
 101             errorMessage += " HotSpotJVMCIRuntime is null.";
 102         }
 103     }
 104 }