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