1 /*
2  * Copyright (c) 2014, 2019, 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.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.DataInputStream;
28 import java.io.DataOutputStream;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.Formatter;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.ServiceLoader;
37 import java.util.Set;
38 
39 import jdk.internal.misc.VM;
40 
41 /**
42  * Provides utilities needed by JVMCI clients.
43  */
44 public final class Services {
45 
46     /**
47      * Guards code that should be run when building an JVMCI shared library but should be excluded
48      * from (being compiled into) the library. Such code must be directly guarded by an {@code if}
49      * statement on this field - the guard cannot be behind a method call.
50      */
51     public static final boolean IS_BUILDING_NATIVE_IMAGE = Boolean.parseBoolean(VM.getSavedProperty("jdk.vm.ci.services.aot"));
52 
53     /**
54      * Guards code that should only be run in a JVMCI shared library. Such code must be directly
55      * guarded by an {@code if} statement on this field - the guard cannot be behind a method call.
56      *
57      * The value of this field in a JVMCI shared library runtime must be {@code true}.
58      */
59     public static final boolean IS_IN_NATIVE_IMAGE;
60     static {
61         /*
62          * Prevents javac from constant folding use of this field. It is set to true by the process
63          * that builds the shared library.
64          */
65         IS_IN_NATIVE_IMAGE = false;
66     }
67 
Services()68     private Services() {
69     }
70 
71     /**
72      * In a native image, this field is initialized by {@link #initializeSavedProperties(byte[])}.
73      */
74     private static volatile Map<String, String> savedProperties;
75 
76     static final boolean JVMCI_ENABLED = Boolean.parseBoolean(VM.getSavedProperties().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      */
checkJVMCIEnabled()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      */
getSavedProperties()90     public static Map<String, String> getSavedProperties() {
91         checkJVMCIEnabled();
92         if (IS_IN_NATIVE_IMAGE) {
93             if (savedProperties == null) {
94                 throw new InternalError("Saved properties not initialized");
95             }
96         } else {
97             if (savedProperties == null) {
98                 synchronized (Services.class) {
99                     if (savedProperties == null) {
100                         SecurityManager sm = System.getSecurityManager();
101                         if (sm != null) {
102                             sm.checkPermission(new JVMCIPermission());
103                         }
104                         savedProperties = VM.getSavedProperties();
105                     }
106                 }
107             }
108         }
109         return savedProperties;
110     }
111 
112     /**
113      * Helper method equivalent to {@link #getSavedProperties()}{@code .getOrDefault(name, def)}.
114      */
getSavedProperty(String name, String def)115     public static String getSavedProperty(String name, String def) {
116         return Services.getSavedProperties().getOrDefault(name, def);
117     }
118 
119     /**
120      * Helper method equivalent to {@link #getSavedProperties()}{@code .get(name)}.
121      */
getSavedProperty(String name)122     public static String getSavedProperty(String name) {
123         return Services.getSavedProperties().get(name);
124     }
125 
126     /**
127      * Causes the JVMCI subsystem to be initialized if it isn't already initialized.
128      */
initializeJVMCI()129     public static void initializeJVMCI() {
130         checkJVMCIEnabled();
131         try {
132             Class.forName("jdk.vm.ci.runtime.JVMCI");
133         } catch (ClassNotFoundException e) {
134             throw new InternalError(e);
135         }
136     }
137 
138     private static final Map<Class<?>, List<?>> servicesCache = IS_BUILDING_NATIVE_IMAGE ? new HashMap<>() : null;
139 
140     @SuppressWarnings("unchecked")
load0(Class<S> service)141     private static <S> Iterable<S> load0(Class<S> service) {
142         if (IS_IN_NATIVE_IMAGE || IS_BUILDING_NATIVE_IMAGE) {
143             List<?> list = servicesCache.get(service);
144             if (list != null) {
145                 return (Iterable<S>) list;
146             }
147             if (IS_IN_NATIVE_IMAGE) {
148                 throw new InternalError(String.format("No %s providers found when building native image", service.getName()));
149             }
150         }
151 
152         Iterable<S> providers = ServiceLoader.load(service, ClassLoader.getSystemClassLoader());
153         if (IS_BUILDING_NATIVE_IMAGE) {
154             synchronized (servicesCache) {
155                 ArrayList<S> providersList = new ArrayList<>();
156                 for (S provider : providers) {
157                     providersList.add(provider);
158                 }
159                 servicesCache.put(service, providersList);
160                 providers = providersList;
161             }
162         }
163         return providers;
164     }
165 
166     /**
167      * Opens all JVMCI packages to {@code otherModule}.
168      */
openJVMCITo(Module otherModule)169     static void openJVMCITo(Module otherModule) {
170         Module jvmci = Services.class.getModule();
171         if (jvmci != otherModule) {
172             Set<String> packages = jvmci.getPackages();
173             for (String pkg : packages) {
174                 boolean opened = jvmci.isOpen(pkg, otherModule);
175                 if (!opened) {
176                     jvmci.addOpens(pkg, otherModule);
177                 }
178             }
179         }
180     }
181 
182     /**
183      * Gets an {@link Iterable} of the JVMCI providers available for a given service.
184      *
185      * @throws SecurityException if a security manager is present and it denies <tt>
186      *             {@link RuntimePermission}("jvmci")</tt>
187      */
load(Class<S> service)188     public static <S> Iterable<S> load(Class<S> service) {
189         SecurityManager sm = System.getSecurityManager();
190         if (sm != null) {
191             sm.checkPermission(new JVMCIPermission());
192         }
193         return load0(service);
194     }
195 
196     /**
197      * Gets the JVMCI provider for a given service for which at most one provider must be available.
198      *
199      * @param service the service whose provider is being requested
200      * @param required specifies if an {@link InternalError} should be thrown if no provider of
201      *            {@code service} is available
202      * @throws SecurityException if a security manager is present and it denies <tt>
203      *             {@link RuntimePermission}("jvmci")</tt>
204      */
loadSingle(Class<S> service, boolean required)205     public static <S> S loadSingle(Class<S> service, boolean required) {
206         SecurityManager sm = System.getSecurityManager();
207         if (sm != null) {
208             sm.checkPermission(new JVMCIPermission());
209         }
210         Iterable<S> providers = load0(service);
211 
212         S singleProvider = null;
213         for (S provider : providers) {
214             if (singleProvider != null) {
215                 throw new InternalError(String.format("Multiple %s providers found: %s, %s", service.getName(), singleProvider.getClass().getName(), provider.getClass().getName()));
216             }
217             singleProvider = provider;
218         }
219         if (singleProvider == null && required) {
220             String javaHome = Services.getSavedProperty("java.home");
221             String vmName = Services.getSavedProperty("java.vm.name");
222             Formatter errorMessage = new Formatter();
223             errorMessage.format("The VM does not expose required service %s.%n", service.getName());
224             errorMessage.format("Currently used Java home directory is %s.%n", javaHome);
225             errorMessage.format("Currently used VM configuration is: %s", vmName);
226             throw new UnsupportedOperationException(errorMessage.toString());
227         }
228         return singleProvider;
229     }
230 
231     /**
232      * A Java {@code char} has a maximal UTF8 length of 3.
233      */
234     private static final int MAX_UNICODE_IN_UTF8_LENGTH = 3;
235 
236     /**
237      * {@link DataOutputStream#writeUTF(String)} only supports values whose UTF8 encoding length is
238      * less than 65535.
239      */
240     private static final int MAX_UTF8_PROPERTY_STRING_LENGTH = 65535 / MAX_UNICODE_IN_UTF8_LENGTH;
241 
242     /**
243      * Serializes the {@linkplain #getSavedProperties() saved system properties} to a byte array for
244      * the purpose of {@linkplain #initializeSavedProperties(byte[]) initializing} the initial
245      * properties in the JVMCI shared library.
246      */
247     @VMEntryPoint
serializeSavedProperties()248     private static byte[] serializeSavedProperties() throws IOException {
249         if (IS_IN_NATIVE_IMAGE) {
250             throw new InternalError("Can only serialize saved properties in HotSpot runtime");
251         }
252         return serializeProperties(Services.getSavedProperties());
253     }
254 
serializeProperties(Map<String, String> props)255     private static byte[] serializeProperties(Map<String, String> props) throws IOException {
256         // Compute size of output on the assumption that
257         // all system properties have ASCII names and values
258         int estimate = 4 + 4;
259         int nonUtf8Props = 0;
260         for (Map.Entry<String, String> e : props.entrySet()) {
261             String name = e.getKey();
262             String value = e.getValue();
263             estimate += (2 + (name.length())) + (2 + (value.length()));
264             if (name.length() > MAX_UTF8_PROPERTY_STRING_LENGTH || value.length() > MAX_UTF8_PROPERTY_STRING_LENGTH) {
265                 nonUtf8Props++;
266             }
267         }
268 
269         ByteArrayOutputStream baos = new ByteArrayOutputStream(estimate);
270         DataOutputStream out = new DataOutputStream(baos);
271         out.writeInt(props.size() - nonUtf8Props);
272         out.writeInt(nonUtf8Props);
273         for (Map.Entry<String, String> e : props.entrySet()) {
274             String name = e.getKey();
275             String value = e.getValue();
276             if (name.length() <= MAX_UTF8_PROPERTY_STRING_LENGTH && value.length() <= MAX_UTF8_PROPERTY_STRING_LENGTH) {
277                 out.writeUTF(name);
278                 out.writeUTF(value);
279             }
280         }
281         if (nonUtf8Props != 0) {
282             for (Map.Entry<String, String> e : props.entrySet()) {
283                 String name = e.getKey();
284                 String value = e.getValue();
285                 if (name.length() > MAX_UTF8_PROPERTY_STRING_LENGTH || value.length() > MAX_UTF8_PROPERTY_STRING_LENGTH) {
286                     byte[] utf8Name = name.getBytes("UTF-8");
287                     byte[] utf8Value = value.getBytes("UTF-8");
288                     out.writeInt(utf8Name.length);
289                     out.write(utf8Name);
290                     out.writeInt(utf8Value.length);
291                     out.write(utf8Value);
292                 }
293             }
294         }
295         return baos.toByteArray();
296     }
297 
298     /**
299      * Initialized the {@linkplain #getSavedProperties() saved system properties} in the JVMCI
300      * shared library from the {@linkplain #serializeSavedProperties() serialized saved properties}
301      * in the HotSpot runtime.
302      */
303     @VMEntryPoint
initializeSavedProperties(byte[] serializedProperties)304     private static void initializeSavedProperties(byte[] serializedProperties) throws IOException {
305         if (!IS_IN_NATIVE_IMAGE) {
306             throw new InternalError("Can only initialize saved properties in JVMCI shared library runtime");
307         }
308         savedProperties = Collections.unmodifiableMap(deserializeProperties(serializedProperties));
309     }
310 
deserializeProperties(byte[] serializedProperties)311     private static Map<String, String> deserializeProperties(byte[] serializedProperties) throws IOException {
312         DataInputStream in = new DataInputStream(new ByteArrayInputStream(serializedProperties));
313         int utf8Props = in.readInt();
314         int nonUtf8Props = in.readInt();
315         Map<String, String> props = new HashMap<>(utf8Props + nonUtf8Props);
316         int index = 0;
317         while (in.available() != 0) {
318             if (index < utf8Props) {
319                 String name = in.readUTF();
320                 String value = in.readUTF();
321                 props.put(name, value);
322             } else {
323                 int nameLen = in.readInt();
324                 byte[] nameBytes = new byte[nameLen];
325                 in.read(nameBytes);
326                 int valueLen = in.readInt();
327                 byte[] valueBytes = new byte[valueLen];
328                 in.read(valueBytes);
329                 String name = new String(nameBytes, "UTF-8");
330                 String value = new String(valueBytes, "UTF-8");
331                 props.put(name, value);
332             }
333             index++;
334         }
335         return props;
336     }
337 }
338