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 package jdk.internal.module;
  26 
  27 import java.io.Closeable;
  28 import java.io.File;
  29 import java.io.IOError;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.io.UncheckedIOException;
  33 import java.lang.module.ModuleDescriptor;
  34 import java.lang.module.ModuleReader;
  35 import java.lang.module.ModuleReference;
  36 import java.net.MalformedURLException;
  37 import java.net.URI;
  38 import java.net.URL;
  39 import java.nio.ByteBuffer;
  40 import java.nio.file.Files;
  41 import java.nio.file.Path;
  42 import java.nio.file.Paths;
  43 import java.util.ArrayList;
  44 import java.util.Collections;
  45 import java.util.HashMap;
  46 import java.util.HashSet;
  47 import java.util.List;
  48 import java.util.Map;
  49 import java.util.Optional;
  50 import java.util.Set;
  51 import java.util.jar.JarEntry;
  52 import java.util.jar.JarFile;
  53 import java.util.stream.Collectors;
  54 import java.util.stream.Stream;
  55 
  56 import jdk.internal.loader.Resource;
  57 import jdk.internal.misc.JavaLangModuleAccess;
  58 import jdk.internal.misc.SharedSecrets;
  59 import sun.net.www.ParseUtil;
  60 
  61 
  62 /**
  63  * Provides support for patching modules, mostly the boot layer.
  64  */
  65 
  66 public final class ModulePatcher {
  67 
  68     private static final JavaLangModuleAccess JLMA
  69         = SharedSecrets.getJavaLangModuleAccess();
  70 
  71     // module name -> sequence of patches (directories or JAR files)
  72     private final Map<String, List<Path>> map;
  73 
  74     /**
  75      * Initialize the module patcher with the given map. The map key is
  76      * the module name, the value is a list of path strings.
  77      */
  78     public ModulePatcher(Map<String, List<String>> input) {
  79         if (input.isEmpty()) {
  80             this.map = Collections.emptyMap();
  81         } else {
  82             Map<String, List<Path>> map = new HashMap<>();
  83             for (Map.Entry<String, List<String>> e : input.entrySet()) {
  84                 String mn = e.getKey();
  85                 List<Path> paths = e.getValue().stream()
  86                         .map(Paths::get)
  87                         .collect(Collectors.toList());
  88                 map.put(mn, paths);
  89             }
  90             this.map = map;
  91         }
  92     }
  93 
  94     /**
  95      * Returns a module reference that interposes on the given module if
  96      * needed. If there are no patches for the given module then the module
  97      * reference is simply returned. Otherwise the patches for the module
  98      * are scanned (to find any new packages) and a new module reference is
  99      * returned.
 100      *
 101      * @throws UncheckedIOException if an I/O error is detected
 102      */
 103     public ModuleReference patchIfNeeded(ModuleReference mref) {
 104         // if there are no patches for the module then nothing to do
 105         ModuleDescriptor descriptor = mref.descriptor();
 106         String mn = descriptor.name();
 107         List<Path> paths = map.get(mn);
 108         if (paths == null)
 109             return mref;
 110 
 111         // scan the JAR file or directory tree to get the set of packages
 112         Set<String> packages = new HashSet<>();
 113         try {
 114             for (Path file : paths) {
 115                 if (Files.isRegularFile(file)) {
 116 
 117                     // JAR file - do not open as a multi-release JAR as this
 118                     // is not supported by the boot class loader
 119                     try (JarFile jf = new JarFile(file.toFile())) {
 120                         jf.stream()
 121                           .map(e -> toPackageName(file, e))
 122                           .filter(Checks::isJavaIdentifier)
 123                           .forEach(packages::add);
 124                     }
 125 
 126                 } else if (Files.isDirectory(file)) {
 127 
 128                     // exploded directory without following sym links
 129                     Path top = file;
 130                     Files.find(top, Integer.MAX_VALUE,
 131                                ((path, attrs) -> attrs.isRegularFile()))
 132                             .map(path -> toPackageName(top, path))
 133                             .filter(Checks::isJavaIdentifier)
 134                             .forEach(packages::add);
 135 
 136                 }
 137             }
 138 
 139         } catch (IOException ioe) {
 140             throw new UncheckedIOException(ioe);
 141         }
 142 
 143         // if there are new packages then we need a new ModuleDescriptor
 144         Set<String> original = descriptor.packages();
 145         packages.addAll(original);
 146         if (packages.size() > original.size()) {
 147             descriptor = JLMA.newModuleDescriptor(descriptor, packages);
 148         }
 149 
 150         // return a module reference to the patched module
 151         URI location = mref.location().orElse(null);
 152 
 153         ModuleHashes recordedHashes = null;
 154         ModuleResolution mres = null;
 155         if (mref instanceof ModuleReferenceImpl) {
 156             ModuleReferenceImpl impl = (ModuleReferenceImpl)mref;
 157             recordedHashes = impl.recordedHashes();
 158             mres = impl.moduleResolution();
 159         }
 160 
 161         return new ModuleReferenceImpl(descriptor,
 162                                        location,
 163                                        () -> new PatchedModuleReader(paths, mref),
 164                                        this,
 165                                        recordedHashes,
 166                                        null,
 167                                        mres);
 168 
 169     }
 170 
 171     /**
 172      * Returns true is this module patcher has no patches.
 173      */
 174     public boolean isEmpty() {
 175         return map.isEmpty();
 176     }
 177 
 178 
 179     /**
 180      * A ModuleReader that reads resources from a patched module.
 181      *
 182      * This class is public so as to expose the findResource method to the
 183      * built-in class loaders and avoid locating the resource twice during
 184      * class loading (once to locate the resource, the second to gets the
 185      * URL for the CodeSource).
 186      */
 187     public static class PatchedModuleReader implements ModuleReader {
 188         private final List<ResourceFinder> finders;
 189         private final ModuleReference mref;
 190         private final URL delegateCodeSourceURL;
 191         private volatile ModuleReader delegate;
 192 
 193         /**
 194          * Creates the ModuleReader to reads resources a patched module.
 195          */
 196         PatchedModuleReader(List<Path> patches, ModuleReference mref) {
 197             List<ResourceFinder> finders = new ArrayList<>();
 198             boolean initialized = false;
 199             try {
 200                 for (Path file : patches) {
 201                     if (Files.isRegularFile(file)) {
 202                         finders.add(new JarResourceFinder(file));
 203                     } else {
 204                         finders.add(new ExplodedResourceFinder(file));
 205                     }
 206                 }
 207                 initialized = true;
 208             } catch (IOException ioe) {
 209                 throw new UncheckedIOException(ioe);
 210             } finally {
 211                 // close all ResourceFinder in the event of an error
 212                 if (!initialized) closeAll(finders);
 213             }
 214 
 215             this.finders = finders;
 216             this.mref = mref;
 217             this.delegateCodeSourceURL = codeSourceURL(mref);
 218         }
 219 
 220         /**
 221          * Closes all resource finders.
 222          */
 223         private static void closeAll(List<ResourceFinder> finders) {
 224             for (ResourceFinder finder : finders) {
 225                 try { finder.close(); } catch (IOException ioe) { }
 226             }
 227         }
 228 
 229         /**
 230          * Returns the code source URL for the given module.
 231          */
 232         private static URL codeSourceURL(ModuleReference mref) {
 233             try {
 234                 Optional<URI> ouri = mref.location();
 235                 if (ouri.isPresent())
 236                     return ouri.get().toURL();
 237             } catch (MalformedURLException e) { }
 238             return null;
 239         }
 240 
 241         /**
 242          * Returns the ModuleReader to delegate to when the resource is not
 243          * found in a patch location.
 244          */
 245         private ModuleReader delegate() throws IOException {
 246             ModuleReader r = delegate;
 247             if (r == null) {
 248                 synchronized (this) {
 249                     r = delegate;
 250                     if (r == null) {
 251                         delegate = r = mref.open();
 252                     }
 253                 }
 254             }
 255             return r;
 256         }
 257 
 258         /**
 259          * Finds a resources in the patch locations. Returns null if not found.
 260          */
 261         private Resource findResourceInPatch(String name) throws IOException {
 262             for (ResourceFinder finder : finders) {
 263                 Resource r = finder.find(name);
 264                 if (r != null)
 265                     return r;
 266             }
 267             return null;
 268         }
 269 
 270         /**
 271          * Finds a resource of the given name in the patched module.
 272          */
 273         public Resource findResource(String name) throws IOException {
 274 
 275             // patch locations
 276             Resource r = findResourceInPatch(name);
 277             if (r != null)
 278                 return r;
 279 
 280             // original module
 281             ByteBuffer bb = delegate().read(name).orElse(null);
 282             if (bb == null)
 283                 return null;
 284 
 285             return new Resource() {
 286                 private <T> T shouldNotGetHere(Class<T> type) {
 287                     throw new InternalError("should not get here");
 288                 }
 289                 @Override
 290                 public String getName() {
 291                     return shouldNotGetHere(String.class);
 292                 }
 293                 @Override
 294                 public URL getURL() {
 295                     return shouldNotGetHere(URL.class);
 296                 }
 297                 @Override
 298                 public URL getCodeSourceURL() {
 299                     return delegateCodeSourceURL;
 300                 }
 301                 @Override
 302                 public ByteBuffer getByteBuffer() throws IOException {
 303                     return bb;
 304                 }
 305                 @Override
 306                 public InputStream getInputStream() throws IOException {
 307                     return shouldNotGetHere(InputStream.class);
 308                 }
 309                 @Override
 310                 public int getContentLength() throws IOException {
 311                     return shouldNotGetHere(int.class);
 312                 }
 313             };
 314         }
 315 
 316         @Override
 317         public Optional<URI> find(String name) throws IOException {
 318             Resource r = findResourceInPatch(name);
 319             if (r != null) {
 320                 URI uri = URI.create(r.getURL().toString());
 321                 return Optional.of(uri);
 322             } else {
 323                 return delegate().find(name);
 324             }
 325         }
 326 
 327         @Override
 328         public Optional<InputStream> open(String name) throws IOException {
 329             Resource r = findResourceInPatch(name);
 330             if (r != null) {
 331                 return Optional.of(r.getInputStream());
 332             } else {
 333                 return delegate().open(name);
 334             }
 335         }
 336 
 337         @Override
 338         public Optional<ByteBuffer> read(String name) throws IOException {
 339             Resource r = findResourceInPatch(name);
 340             if (r != null) {
 341                 ByteBuffer bb = r.getByteBuffer();
 342                 assert !bb.isDirect();
 343                 return Optional.of(bb);
 344             } else {
 345                 return delegate().read(name);
 346             }
 347         }
 348 
 349         @Override
 350         public void release(ByteBuffer bb) {
 351             if (bb.isDirect()) {
 352                 try {
 353                     delegate().release(bb);
 354                 } catch (IOException ioe) {
 355                     throw new InternalError(ioe);
 356                 }
 357             }
 358         }
 359 
 360         @Override
 361         public Stream<String> list() throws IOException {
 362             Stream<String> s = delegate().list();
 363             for (ResourceFinder finder : finders) {
 364                 s = Stream.concat(s, finder.list());
 365             }
 366             return s.distinct();
 367         }
 368 
 369         @Override
 370         public void close() throws IOException {
 371             closeAll(finders);
 372             delegate().close();
 373         }
 374     }
 375 
 376 
 377     /**
 378      * A resource finder that find resources in a patch location.
 379      */
 380     private static interface ResourceFinder extends Closeable {
 381         Resource find(String name) throws IOException;
 382         Stream<String> list() throws IOException;
 383     }
 384 
 385 
 386     /**
 387      * A ResourceFinder that finds resources in a JAR file.
 388      */
 389     private static class JarResourceFinder implements ResourceFinder {
 390         private final JarFile jf;
 391         private final URL csURL;
 392 
 393         JarResourceFinder(Path path) throws IOException {
 394             this.jf = new JarFile(path.toFile());
 395             this.csURL = path.toUri().toURL();
 396         }
 397 
 398         @Override
 399         public void close() throws IOException {
 400             jf.close();
 401         }
 402 
 403         @Override
 404         public Resource find(String name) throws IOException {
 405             JarEntry entry = jf.getJarEntry(name);
 406             if (entry == null)
 407                 return null;
 408 
 409             return new Resource() {
 410                 @Override
 411                 public String getName() {
 412                     return name;
 413                 }
 414                 @Override
 415                 public URL getURL() {
 416                     String encodedPath = ParseUtil.encodePath(name, false);
 417                     try {
 418                         return new URL("jar:" + csURL + "!/" + encodedPath);
 419                     } catch (MalformedURLException e) {
 420                         return null;
 421                     }
 422                 }
 423                 @Override
 424                 public URL getCodeSourceURL() {
 425                     return csURL;
 426                 }
 427                 @Override
 428                 public ByteBuffer getByteBuffer() throws IOException {
 429                     byte[] bytes = getInputStream().readAllBytes();
 430                     return ByteBuffer.wrap(bytes);
 431                 }
 432                 @Override
 433                 public InputStream getInputStream() throws IOException {
 434                     return jf.getInputStream(entry);
 435                 }
 436                 @Override
 437                 public int getContentLength() throws IOException {
 438                     long size = entry.getSize();
 439                     return (size > Integer.MAX_VALUE) ? -1 : (int) size;
 440                 }
 441             };
 442         }
 443 
 444         @Override
 445         public Stream<String> list() throws IOException {
 446             return jf.stream()
 447                     .filter(e -> !e.isDirectory())
 448                     .map(JarEntry::getName);
 449         }
 450     }
 451 
 452 
 453     /**
 454      * A ResourceFinder that finds resources on the file system.
 455      */
 456     private static class ExplodedResourceFinder implements ResourceFinder {
 457         private final Path dir;
 458 
 459         ExplodedResourceFinder(Path dir) {
 460             this.dir = dir;
 461         }
 462 
 463         @Override
 464         public void close() { }
 465 
 466         @Override
 467         public Resource find(String name) throws IOException {
 468             Path file = Paths.get(name.replace('/', File.separatorChar));
 469             if (file.getRoot() == null) {
 470                 file = dir.resolve(file);
 471             } else {
 472                 // drop the root component so that the resource is
 473                 // located relative to the module directory
 474                 int n = file.getNameCount();
 475                 if (n == 0)
 476                     return null;
 477                 file = dir.resolve(file.subpath(0, n));
 478             }
 479 
 480             if (Files.isRegularFile(file)) {
 481                 return newResource(name, dir, file);
 482             } else {
 483                 return null;
 484             }
 485         }
 486 
 487         private Resource newResource(String name, Path top, Path file) {
 488             return new Resource() {
 489                 @Override
 490                 public String getName() {
 491                     return name;
 492                 }
 493                 @Override
 494                 public URL getURL() {
 495                     try {
 496                         return file.toUri().toURL();
 497                     } catch (IOException | IOError e) {
 498                         return null;
 499                     }
 500                 }
 501                 @Override
 502                 public URL getCodeSourceURL() {
 503                     try {
 504                         return top.toUri().toURL();
 505                     } catch (IOException | IOError e) {
 506                         return null;
 507                     }
 508                 }
 509                 @Override
 510                 public ByteBuffer getByteBuffer() throws IOException {
 511                     return ByteBuffer.wrap(Files.readAllBytes(file));
 512                 }
 513                 @Override
 514                 public InputStream getInputStream() throws IOException {
 515                     return Files.newInputStream(file);
 516                 }
 517                 @Override
 518                 public int getContentLength() throws IOException {
 519                     long size = Files.size(file);
 520                     return (size > Integer.MAX_VALUE) ? -1 : (int)size;
 521                 }
 522             };
 523         }
 524 
 525         @Override
 526         public Stream<String> list() throws IOException {
 527             return Files.find(dir, Integer.MAX_VALUE,
 528                               (path, attrs) -> attrs.isRegularFile())
 529                     .map(f -> dir.relativize(f)
 530                                  .toString()
 531                                  .replace(File.separatorChar, '/'));
 532         }
 533     }
 534 
 535 
 536     /**
 537      * Derives a package name from a file path to a .class file.
 538      */
 539     private static String toPackageName(Path top, Path file) {
 540         Path entry = top.relativize(file);
 541         Path parent = entry.getParent();
 542         if (parent == null) {
 543             return warnIfModuleInfo(top, entry.toString());
 544         } else {
 545             return parent.toString().replace(File.separatorChar, '.');
 546         }
 547     }
 548 
 549     /**
 550      * Derives a package name from the name of an entry in a JAR file.
 551      */
 552     private static String toPackageName(Path file, JarEntry entry) {
 553         String name = entry.getName();
 554         int index = name.lastIndexOf("/");
 555         if (index == -1) {
 556             return warnIfModuleInfo(file, name);
 557         } else {
 558             return name.substring(0, index).replace('/', '.');
 559         }
 560     }
 561 
 562     private static String warnIfModuleInfo(Path file, String e) {
 563         if (e.equals("module-info.class"))
 564             System.err.println("WARNING: " + e + " ignored in patch: " + file);
 565         return "";
 566     }
 567 }