1 /*
   2  * Copyright (c) 2014, 2016, 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 package jdk.vm.ci.services;
  24 
  25 import java.lang.reflect.Method;
  26 import java.util.Map;
  27 
  28 /**
  29  * Provides utilities needed by JVMCI clients.
  30  */
  31 public final class Services {
  32 
  33     // This class must be compilable and executable on JDK 8 since it's used in annotation
  34     // processors while building JDK 9 so use of API added in JDK 9 is made via reflection.
  35 
  36     /**
  37      * Guards code that should be run when building a native image but should be excluded from
  38      * (being compiled into) the image. Such code must be directly guarded by an {@code if}
  39      * statement on this field - the guard cannot be behind a method call.
  40      */
  41     public static final boolean IS_BUILDING_NATIVE_IMAGE;
  42 
  43     /**
  44      * Guards code that should only be run in native image. Such code must be directly guarded by an
  45      * {@code if} statement on this field - the guard cannot be behind a method call.
  46      *
  47      * The value of this field seen during analysis and compilation of an SVM image must be
  48      * {@code true}.
  49      */
  50     public static final boolean IS_IN_NATIVE_IMAGE;
  51 
  52     static {
  53         /*
  54          * Prevents javac from constant folding use of this field. It is set to true in the SVM
  55          * image via substitution during image building.
  56          */
  57         IS_IN_NATIVE_IMAGE = false;
  58         IS_BUILDING_NATIVE_IMAGE = false;
  59     }
  60 
  61     private Services() {
  62     }
  63 
  64     @SuppressWarnings("unchecked")
  65     private static Map<String, String> initSavedProperties() throws InternalError {
  66         try {
  67             Class<?> vmClass = Class.forName("jdk.internal.misc.VM");
  68             Method m = vmClass.getMethod("getSavedProperties");
  69             return (Map<String, String>) m.invoke(null);
  70         } catch (Exception e) {
  71             throw new InternalError(e);
  72         }
  73     }
  74 
  75     static final Map<String, String> SAVED_PROPERTIES = initSavedProperties();
  76     static final boolean JVMCI_ENABLED = Boolean.parseBoolean(SAVED_PROPERTIES.get("jdk.internal.vm.ci.enabled"));
  77 
  78     /**
  79      * Checks that JVMCI is enabled in the VM and throws an error if it isn't.
  80      */
  81     static void checkJVMCIEnabled() {
  82         if (!JVMCI_ENABLED) {
  83             throw new Error("The EnableJVMCI VM option must be true (i.e., -XX:+EnableJVMCI) to use JVMCI");
  84         }
  85     }
  86 
  87     /**
  88      * Gets an unmodifiable copy of the system properties saved when {@link System} is initialized.
  89      */
  90     public static Map<String, String> getSavedProperties() {
  91         checkJVMCIEnabled();
  92         SecurityManager sm = System.getSecurityManager();
  93         if (sm != null) {
  94             sm.checkPermission(new JVMCIPermission());
  95         }
  96         return SAVED_PROPERTIES;
  97     }
  98 
  99     /**
 100      * Causes the JVMCI subsystem to be initialized if it isn't already initialized.
 101      */
 102     public static void initializeJVMCI() {
 103         checkJVMCIEnabled();
 104         try {
 105             Class.forName("jdk.vm.ci.runtime.JVMCI");
 106         } catch (ClassNotFoundException e) {
 107             throw new InternalError(e);
 108         }
 109     }
 110 }