1 /*
  2  * Copyright (c) 2015, 2025, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package jdk.internal.loader;
 26 
 27 import java.io.IOException;
 28 import java.io.InputStream;
 29 import java.lang.module.ModuleReference;
 30 import java.net.MalformedURLException;
 31 import java.net.URI;
 32 import java.net.URL;
 33 import java.nio.file.Files;
 34 import java.nio.file.Path;
 35 import java.util.Arrays;
 36 import java.util.Enumeration;
 37 import java.util.concurrent.ConcurrentHashMap;
 38 import java.util.jar.JarInputStream;
 39 import java.util.jar.Manifest;
 40 import java.util.stream.Stream;
 41 
 42 import jdk.internal.access.JavaLangAccess;
 43 import jdk.internal.access.SharedSecrets;
 44 import jdk.internal.module.Modules;
 45 import jdk.internal.module.ServicesCatalog;
 46 import jdk.internal.util.StaticProperty;
 47 
 48 /**
 49  * Find resources and packages in modules defined to the boot class loader or
 50  * resources and packages on the "boot class path" specified via -Xbootclasspath/a.
 51  */
 52 
 53 public class BootLoader {
 54     private BootLoader() { }
 55 
 56     private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
 57 
 58     // The unnamed module for the boot loader
 59     private static final Module UNNAMED_MODULE;
 60     private static final String JAVA_HOME = StaticProperty.javaHome();
 61 
 62     static {
 63         JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
 64         ArchivedClassLoaders archivedClassLoaders = ArchivedClassLoaders.get();
 65         if (archivedClassLoaders != null) {
 66             UNNAMED_MODULE = archivedClassLoaders.unnamedModuleForBootLoader();
 67         } else {
 68             UNNAMED_MODULE = jla.defineUnnamedModule(null);
 69         }
 70         jla.addEnableNativeAccess(UNNAMED_MODULE);
 71         setBootLoaderUnnamedModule0(UNNAMED_MODULE);
 72     }
 73 
 74     // ClassLoaderValue map for the boot class loader
 75     private static final ConcurrentHashMap<?, ?> CLASS_LOADER_VALUE_MAP
 76         = new ConcurrentHashMap<>();
 77 
 78     // Holder has the field(s) that need to be initialized during JVM bootstrap even if
 79     // the outer is aot-initialized.
 80     private static class Holder {
 81         // native libraries loaded by the boot class loader
 82         private static final NativeLibraries NATIVE_LIBS
 83             = NativeLibraries.newInstance(null);
 84     }
 85 
 86     /**
 87      * Returns the unnamed module for the boot loader.
 88      */
 89     public static Module getUnnamedModule() {
 90         return UNNAMED_MODULE;
 91     }
 92 
 93     /**
 94      * Returns the ServiceCatalog for modules defined to the boot class loader.
 95      */
 96     public static ServicesCatalog getServicesCatalog() {
 97         return ServicesCatalog.getServicesCatalog(ClassLoaders.bootLoader());
 98     }
 99 
100     /**
101      * Returns the ClassLoaderValue map for the boot class loader.
102      */
103     public static ConcurrentHashMap<?, ?> getClassLoaderValueMap() {
104         return CLASS_LOADER_VALUE_MAP;
105     }
106 
107     /**
108      * Returns NativeLibraries for the boot class loader.
109      */
110     public static NativeLibraries getNativeLibraries() {
111         return Holder.NATIVE_LIBS;
112     }
113 
114     /**
115      * Returns {@code true} if there is a class path associated with the
116      * BootLoader.
117      */
118     public static boolean hasClassPath() {
119         return ClassLoaders.bootLoader().hasClassPath();
120     }
121 
122     /**
123      * Registers a module with this class loader so that its classes
124      * (and resources) become visible via this class loader.
125      */
126     public static void loadModule(ModuleReference mref) {
127         ClassLoaders.bootLoader().loadModule(mref);
128     }
129 
130     /**
131      * Loads the Class object with the given name defined to the boot loader.
132      */
133     public static Class<?> loadClassOrNull(String name) {
134         return JLA.findBootstrapClassOrNull(name);
135     }
136 
137     /**
138      * Loads the Class object with the given name in the given module
139      * defined to the boot loader. Returns {@code null} if not found.
140      */
141     public static Class<?> loadClass(Module module, String name) {
142         Class<?> c = loadClassOrNull(name);
143         if (c != null && c.getModule() == module) {
144             return c;
145         } else {
146             return null;
147         }
148     }
149 
150     /**
151      * Loads a native library from the system library path.
152      */
153     public static void loadLibrary(String name) {
154         getNativeLibraries().loadLibrary(name);
155     }
156 
157     /**
158      * Returns a URL to a resource in a module defined to the boot loader.
159      */
160     public static URL findResource(String mn, String name) throws IOException {
161         return ClassLoaders.bootLoader().findResource(mn, name);
162     }
163 
164     /**
165      * Returns an input stream to a resource in a module defined to the
166      * boot loader.
167      */
168     public static InputStream findResourceAsStream(String mn, String name)
169         throws IOException
170     {
171         return ClassLoaders.bootLoader().findResourceAsStream(mn, name);
172     }
173 
174     /**
175      * Returns the URL to the given resource in any of the modules
176      * defined to the boot loader and the boot class path.
177      */
178     public static URL findResource(String name) {
179         return ClassLoaders.bootLoader().findResource(name);
180     }
181 
182     /**
183      * Returns an Iterator to iterate over the resources of the given name
184      * in any of the modules defined to the boot loader.
185      */
186     public static Enumeration<URL> findResources(String name) throws IOException {
187         return ClassLoaders.bootLoader().findResources(name);
188     }
189 
190     /**
191      * Define a package for the given class to the boot loader, if not already
192      * defined.
193      */
194     public static Package definePackage(Class<?> c) {
195         return getDefinedPackage(c.getPackageName());
196     }
197 
198     /**
199      * Returns the Package of the given name defined to the boot loader or null
200      * if the package has not been defined.
201      */
202     public static Package getDefinedPackage(String pn) {
203         Package pkg = ClassLoaders.bootLoader().getDefinedPackage(pn);
204         if (pkg == null) {
205             String location = getSystemPackageLocation(pn.replace('.', '/'));
206             if (location != null) {
207                 pkg = PackageHelper.definePackage(pn.intern(), location);
208             }
209         }
210         return pkg;
211     }
212 
213     /**
214      * Returns a stream of the packages defined to the boot loader.
215      */
216     public static Stream<Package> packages() {
217         return Arrays.stream(getSystemPackageNames())
218                      .map(name -> getDefinedPackage(name.replace('/', '.')));
219     }
220 
221     /**
222      * Helper class to define {@code Package} objects for packages in modules
223      * defined to the boot loader.
224      */
225     static class PackageHelper {
226         private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
227 
228         /**
229          * Define the {@code Package} with the given name. The specified
230          * location is a jrt URL to a named module in the run-time image,
231          * a file URL to a module in an exploded run-time image, or a file
232          * path to an entry on the boot class path (java agent Boot-Class-Path
233          * or -Xbootclasspath/a.
234          *
235          * <p> If the given location is a JAR file containing a manifest,
236          * the defined Package contains the versioning information from
237          * the manifest, if present.
238          *
239          * @param name     package name
240          * @param location location where the package is (jrt URL or file URL
241          *                 for a named module in the run-time or exploded image;
242          *                 a file path for a package from -Xbootclasspath/a)
243          */
244         static Package definePackage(String name, String location) {
245             Module module = findModule(location);
246             if (module != null) {
247                 // named module from runtime image or exploded module
248                 if (name.isEmpty())
249                     throw new InternalError("empty package in " + location);
250                 return JLA.definePackage(ClassLoaders.bootLoader(), name, module);
251             }
252 
253             // package in unnamed module (-Xbootclasspath/a)
254             URL url = toFileURL(location);
255             Manifest man = url != null ? getManifest(location) : null;
256 
257             return ClassLoaders.bootLoader().defineOrCheckPackage(name, man, url);
258         }
259 
260         /**
261          * Finds the module at the given location defined to the boot loader.
262          * The module is either in runtime image or exploded image.
263          * Otherwise this method returns null.
264          */
265         private static Module findModule(String location) {
266             String mn = null;
267             if (location.startsWith("jrt:/")) {
268                 // named module in runtime image ("jrt:/".length() == 5)
269                 mn = location.substring(5, location.length());
270             } else if (location.startsWith("file:/")) {
271                 // named module in exploded image
272                 Path path = Path.of(URI.create(location));
273                 Path modulesDir = Path.of(JAVA_HOME, "modules");
274                 if (path.startsWith(modulesDir)) {
275                     mn = path.getFileName().toString();
276                 }
277             }
278 
279             // return the Module object for the module name. The Module may
280             // in the boot layer or a child layer for the case that the module
281             // is loaded into a running VM
282             if (mn != null) {
283                 String name = mn;
284                 return Modules.findLoadedModule(mn)
285                     .orElseThrow(() -> new InternalError(name + " not loaded"));
286             } else {
287                 return null;
288             }
289         }
290 
291         /**
292          * Returns URL if the given location is a regular file path.
293          */
294         private static URL toFileURL(String location) {
295             Path path = Path.of(location);
296             if (Files.isRegularFile(path)) {
297                 try {
298                     return path.toUri().toURL();
299                 } catch (MalformedURLException e) {}
300             }
301             return null;
302         }
303 
304         /**
305          * Returns the Manifest if the given location is a JAR file
306          * containing a manifest.
307          */
308         private static Manifest getManifest(String location) {
309             Path jar = Path.of(location);
310             try (InputStream in = Files.newInputStream(jar);
311                  JarInputStream jis = new JarInputStream(in, false)) {
312                 return jis.getManifest();
313             } catch (IOException e) {
314                 return null;
315             }
316         }
317     }
318 
319     /**
320      * Returns an array of the binary name of the packages defined by
321      * the boot loader, in VM internal form (forward slashes instead of dot).
322      */
323     private static native String[] getSystemPackageNames();
324 
325     /**
326      * Returns the location of the package of the given name, if
327      * defined by the boot loader; otherwise {@code null} is returned.
328      *
329      * The location may be a module from the runtime image or exploded image,
330      * or from the boot class append path (i.e. -Xbootclasspath/a or
331      * BOOT-CLASS-PATH attribute specified in java agent).
332      */
333     private static native String getSystemPackageLocation(String name);
334     private static native void setBootLoaderUnnamedModule0(Module module);
335 }