1 /*
   2  * Copyright (c) 2015, 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.  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 
  26 package jdk.internal.module;
  27 
  28 import java.io.ByteArrayInputStream;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.UncheckedIOException;
  32 import java.lang.module.ModuleDescriptor;
  33 import java.lang.module.ModuleFinder;
  34 import java.lang.module.ModuleReader;
  35 import java.lang.module.ModuleReference;
  36 import java.net.URI;
  37 import java.net.URLConnection;
  38 import java.nio.ByteBuffer;
  39 import java.util.ArrayDeque;
  40 import java.util.Collections;
  41 import java.util.Deque;
  42 import java.util.HashMap;
  43 import java.util.Iterator;
  44 import java.util.Map;
  45 import java.util.Map.Entry;
  46 import java.util.Objects;
  47 import java.util.Optional;
  48 import java.util.Set;
  49 import java.util.Spliterator;
  50 import java.util.function.Consumer;
  51 import java.util.function.Supplier;
  52 import java.util.stream.Stream;
  53 import java.util.stream.StreamSupport;
  54 
  55 import jdk.internal.jimage.ImageLocation;
  56 import jdk.internal.jimage.ImageReader;
  57 import jdk.internal.jimage.ImageReaderFactory;
  58 import jdk.internal.misc.JavaNetUriAccess;
  59 import jdk.internal.misc.SharedSecrets;
  60 import jdk.internal.module.ModuleHashes.HashSupplier;
  61 import jdk.internal.perf.PerfCounter;
  62 
  63 /**
  64  * A {@code ModuleFinder} that finds modules that are linked into the
  65  * run-time image.
  66  *
  67  * The modules linked into the run-time image are assumed to have the
  68  * Packages attribute.
  69  */
  70 
  71 public class SystemModuleFinder implements ModuleFinder {
  72 
  73     private static final JavaNetUriAccess JNUA = SharedSecrets.getJavaNetUriAccess();
  74 
  75     private static final PerfCounter initTime
  76         = PerfCounter.newPerfCounter("jdk.module.finder.jimage.initTime");
  77     private static final PerfCounter moduleCount
  78         = PerfCounter.newPerfCounter("jdk.module.finder.jimage.modules");
  79     private static final PerfCounter packageCount
  80         = PerfCounter.newPerfCounter("jdk.module.finder.jimage.packages");
  81     private static final PerfCounter exportsCount
  82         = PerfCounter.newPerfCounter("jdk.module.finder.jimage.exports");
  83 
  84     // singleton finder to find modules in the run-time images
  85     private static final SystemModuleFinder INSTANCE;
  86 
  87     public static SystemModuleFinder getInstance() {
  88         return INSTANCE;
  89     }
  90 
  91     /**
  92      * For now, the module references are created eagerly on the assumption
  93      * that service binding will require all modules to be located.
  94      */
  95     static {
  96         long t0 = System.nanoTime();
  97 
  98         INSTANCE = new SystemModuleFinder();
  99 
 100         initTime.addElapsedTimeFrom(t0);
 101     }
 102 
 103     /**
 104      * Holder class for the ImageReader
 105      */
 106     private static class SystemImage {
 107         static final ImageReader READER;
 108         static {
 109             long t0 = System.nanoTime();
 110             READER = ImageReaderFactory.getImageReader();
 111             initTime.addElapsedTimeFrom(t0);
 112         }
 113 
 114         static ImageReader reader() {
 115             return READER;
 116         }
 117     }
 118 
 119     private static boolean isFastPathSupported() {
 120        return SystemModules.MODULE_NAMES.length > 0;
 121     }
 122 
 123     private static String[] moduleNames() {
 124         if (isFastPathSupported())
 125             // module names recorded at link time
 126             return SystemModules.MODULE_NAMES;
 127 
 128         // this happens when java.base is patched with java.base
 129         // from an exploded image
 130         return SystemImage.reader().getModuleNames();
 131     }
 132 
 133     // the set of modules in the run-time image
 134     private final Set<ModuleReference> modules;
 135 
 136     // maps module name to module reference
 137     private final Map<String, ModuleReference> nameToModule;
 138 
 139     // module name to hashes
 140     private final Map<String, byte[]> hashes;
 141 
 142     private SystemModuleFinder() {
 143         String[] names = moduleNames();
 144         int n = names.length;
 145         moduleCount.add(n);
 146 
 147         // fastpath is enabled by default.
 148         // It can be disabled for troubleshooting purpose.
 149         boolean disabled =
 150             System.getProperty("jdk.system.module.finder.disabledFastPath") != null;
 151 
 152         ModuleDescriptor[] descriptors;
 153         ModuleHashes[] recordedHashes;
 154         ModuleResolution[] moduleResolutions;
 155 
 156         // fast loading of ModuleDescriptor of system modules
 157         if (isFastPathSupported() && !disabled) {
 158             descriptors = SystemModules.descriptors();
 159             recordedHashes = SystemModules.hashes();
 160             moduleResolutions = SystemModules.moduleResolutions();
 161         } else {
 162             // if fast loading of ModuleDescriptors is disabled
 163             // fallback to read module-info.class
 164             descriptors = new ModuleDescriptor[n];
 165             recordedHashes = new ModuleHashes[n];
 166             moduleResolutions = new ModuleResolution[n];
 167             ImageReader imageReader = SystemImage.reader();
 168             for (int i = 0; i < names.length; i++) {
 169                 String mn = names[i];
 170                 ImageLocation loc = imageReader.findLocation(mn, "module-info.class");
 171                 ModuleInfo.Attributes attrs =
 172                     ModuleInfo.read(imageReader.getResourceBuffer(loc), null);
 173                 descriptors[i] = attrs.descriptor();
 174                 recordedHashes[i] = attrs.recordedHashes();
 175                 moduleResolutions[i] = attrs.moduleResolution();
 176             }
 177         }
 178 
 179         Map<String, byte[]> hashes = null;
 180         boolean secondSeen = false;
 181         // record the hashes to build HashSupplier
 182         for (ModuleHashes mh : recordedHashes) {
 183             if (mh != null) {
 184                 // if only one module contain ModuleHashes, use it
 185                 if (hashes == null) {
 186                     hashes = mh.hashes();
 187                 } else {
 188                     if (!secondSeen) {
 189                         hashes = new HashMap<>(hashes);
 190                         secondSeen = true;
 191                     }
 192                     hashes.putAll(mh.hashes());
 193                 }
 194             }
 195         }
 196         this.hashes = (hashes == null) ? Map.of() : hashes;
 197 
 198         ModuleReference[] mods = new ModuleReference[n];
 199 
 200         @SuppressWarnings(value = {"rawtypes", "unchecked"})
 201         Entry<String, ModuleReference>[] map
 202             = (Entry<String, ModuleReference>[])new Entry[n];
 203 
 204         for (int i = 0; i < n; i++) {
 205             ModuleDescriptor md = descriptors[i];
 206 
 207             // create the ModuleReference
 208             ModuleReference mref = toModuleReference(md,
 209                                                      recordedHashes[i],
 210                                                      hashSupplier(names[i]),
 211                                                      moduleResolutions[i]);
 212             mods[i] = mref;
 213             map[i] = Map.entry(names[i], mref);
 214 
 215             // counters
 216             packageCount.add(md.packages().size());
 217             exportsCount.add(md.exports().size());
 218         }
 219 
 220         modules = Set.of(mods);
 221         nameToModule = Map.ofEntries(map);
 222     }
 223 
 224     @Override
 225     public Optional<ModuleReference> find(String name) {
 226         Objects.requireNonNull(name);
 227         return Optional.ofNullable(nameToModule.get(name));
 228     }
 229 
 230     @Override
 231     public Set<ModuleReference> findAll() {
 232         return modules;
 233     }
 234 
 235     private ModuleReference toModuleReference(ModuleDescriptor md,
 236                                               ModuleHashes recordedHashes,
 237                                               HashSupplier hasher,
 238                                               ModuleResolution mres) {
 239         String mn = md.name();
 240         URI uri = JNUA.create("jrt", "/".concat(mn));
 241 
 242         Supplier<ModuleReader> readerSupplier = new Supplier<>() {
 243             @Override
 244             public ModuleReader get() {
 245                 return new ImageModuleReader(mn, uri);
 246             }
 247         };
 248 
 249         ModuleReference mref =
 250             new ModuleReferenceImpl(md, uri, readerSupplier, null,
 251                                     recordedHashes, hasher, mres);
 252 
 253         // may need a reference to a patched module if --patch-module specified
 254         mref = ModuleBootstrap.patcher().patchIfNeeded(mref);
 255 
 256         return mref;
 257     }
 258 
 259     private HashSupplier hashSupplier(String name) {
 260         if (!hashes.containsKey(name))
 261             return null;
 262 
 263         return new HashSupplier() {
 264             @Override
 265             public byte[] generate(String algorithm) {
 266                 return hashes.get(name);
 267             }
 268         };
 269     }
 270 
 271     /**
 272      * A ModuleReader for reading resources from a module linked into the
 273      * run-time image.
 274      */
 275     static class ImageModuleReader implements ModuleReader {
 276         private final String module;
 277         private volatile boolean closed;
 278 
 279         /**
 280          * If there is a security manager set then check permission to
 281          * connect to the run-time image.
 282          */
 283         private static void checkPermissionToConnect(URI uri) {
 284             SecurityManager sm = System.getSecurityManager();
 285             if (sm != null) {
 286                 try {
 287                     URLConnection uc = uri.toURL().openConnection();
 288                     sm.checkPermission(uc.getPermission());
 289                 } catch (IOException ioe) {
 290                     throw new UncheckedIOException(ioe);
 291                 }
 292             }
 293         }
 294 
 295         ImageModuleReader(String module, URI uri) {
 296             checkPermissionToConnect(uri);
 297             this.module = module;
 298         }
 299 
 300         /**
 301          * Returns the ImageLocation for the given resource, {@code null}
 302          * if not found.
 303          */
 304         private ImageLocation findImageLocation(String name) throws IOException {
 305             Objects.requireNonNull(name);
 306             if (closed)
 307                 throw new IOException("ModuleReader is closed");
 308             ImageReader imageReader = SystemImage.reader();
 309             if (imageReader != null) {
 310                 return imageReader.findLocation(module, name);
 311             } else {
 312                 // not an images build
 313                 return null;
 314             }
 315         }
 316 
 317         @Override
 318         public Optional<URI> find(String name) throws IOException {
 319             ImageLocation location = findImageLocation(name);
 320             if (location != null) {
 321                 URI u = URI.create("jrt:/" + module + "/" + name);
 322                 return Optional.of(u);
 323             } else {
 324                 return Optional.empty();
 325             }
 326         }
 327 
 328         @Override
 329         public Optional<InputStream> open(String name) throws IOException {
 330             return read(name).map(this::toInputStream);
 331         }
 332 
 333         private InputStream toInputStream(ByteBuffer bb) { // ## -> ByteBuffer?
 334             try {
 335                 int rem = bb.remaining();
 336                 byte[] bytes = new byte[rem];
 337                 bb.get(bytes);
 338                 return new ByteArrayInputStream(bytes);
 339             } finally {
 340                 release(bb);
 341             }
 342         }
 343 
 344         @Override
 345         public Optional<ByteBuffer> read(String name) throws IOException {
 346             ImageLocation location = findImageLocation(name);
 347             if (location != null) {
 348                 return Optional.of(SystemImage.reader().getResourceBuffer(location));
 349             } else {
 350                 return Optional.empty();
 351             }
 352         }
 353 
 354         @Override
 355         public void release(ByteBuffer bb) {
 356             Objects.requireNonNull(bb);
 357             ImageReader.releaseByteBuffer(bb);
 358         }
 359 
 360         @Override
 361         public Stream<String> list() throws IOException {
 362             if (closed)
 363                 throw new IOException("ModuleReader is closed");
 364 
 365             Spliterator<String> s = new ModuleContentSpliterator(module);
 366             return StreamSupport.stream(s, false);
 367         }
 368 
 369         @Override
 370         public void close() {
 371             // nothing else to do
 372             closed = true;
 373         }
 374     }
 375 
 376     /**
 377      * A Spliterator for traversing the resources of a module linked into the
 378      * run-time image.
 379      */
 380     static class ModuleContentSpliterator implements Spliterator<String> {
 381         final String moduleRoot;
 382         final Deque<ImageReader.Node> stack;
 383         Iterator<ImageReader.Node> iterator;
 384 
 385         ModuleContentSpliterator(String module) throws IOException {
 386             moduleRoot = "/modules/" + module;
 387             stack = new ArrayDeque<>();
 388 
 389             // push the root node to the stack to get started
 390             ImageReader.Node dir = SystemImage.reader().findNode(moduleRoot);
 391             if (dir == null || !dir.isDirectory())
 392                 throw new IOException(moduleRoot + " not a directory");
 393             stack.push(dir);
 394             iterator = Collections.emptyIterator();
 395         }
 396 
 397         /**
 398          * Returns the name of the next non-directory node or {@code null} if
 399          * there are no remaining nodes to visit.
 400          */
 401         private String next() throws IOException {
 402             for (;;) {
 403                 while (iterator.hasNext()) {
 404                     ImageReader.Node node = iterator.next();
 405                     String name = node.getName();
 406                     if (node.isDirectory()) {
 407                         // build node
 408                         ImageReader.Node dir = SystemImage.reader().findNode(name);
 409                         assert dir.isDirectory();
 410                         stack.push(dir);
 411                     } else {
 412                         // strip /modules/$MODULE/ prefix
 413                         return name.substring(moduleRoot.length() + 1);
 414                     }
 415                 }
 416 
 417                 if (stack.isEmpty()) {
 418                     return null;
 419                 } else {
 420                     ImageReader.Node dir = stack.poll();
 421                     assert dir.isDirectory();
 422                     iterator = dir.getChildren().iterator();
 423                 }
 424             }
 425         }
 426 
 427         @Override
 428         public boolean tryAdvance(Consumer<? super String> action) {
 429             String next;
 430             try {
 431                 next = next();
 432             } catch (IOException ioe) {
 433                 throw new UncheckedIOException(ioe);
 434             }
 435             if (next != null) {
 436                 action.accept(next);
 437                 return true;
 438             } else {
 439                 return false;
 440             }
 441         }
 442 
 443         @Override
 444         public Spliterator<String> trySplit() {
 445             return null;
 446         }
 447 
 448         @Override
 449         public int characteristics() {
 450             return Spliterator.DISTINCT + Spliterator.NONNULL + Spliterator.IMMUTABLE;
 451         }
 452 
 453         @Override
 454         public long estimateSize() {
 455             return Long.MAX_VALUE;
 456         }
 457     }
 458 }