1 /*
   2  * Copyright (c) 2007, 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 
  24 /**
  25  *
  26  * @test
  27  * @modules java.base/java.io:open
  28  * @bug 6524062
  29  * @summary Test to ensure that FIS.finalize() invokes the close() method as per
  30  * the specification.
  31  * @run main/othervm UnreferencedFISClosesFd
  32  */
  33 import java.io.File;
  34 import java.io.FileDescriptor;
  35 import java.io.FileInputStream;
  36 import java.io.FileNotFoundException;
  37 import java.io.IOException;
  38 import java.lang.management.ManagementFactory;
  39 import java.lang.management.OperatingSystemMXBean;
  40 import java.lang.ref.Reference;
  41 import java.lang.ref.ReferenceQueue;
  42 import java.lang.ref.WeakReference;
  43 import java.lang.reflect.Field;
  44 import java.nio.file.Files;
  45 import java.nio.file.Path;
  46 import java.nio.file.Paths;
  47 import java.util.ArrayDeque;
  48 import java.util.HashSet;
  49 import java.util.List;
  50 import java.util.Optional;
  51 import java.util.concurrent.TimeUnit;
  52 import java.util.concurrent.atomic.AtomicInteger;
  53 
  54 import com.sun.management.UnixOperatingSystemMXBean;
  55 
  56 /**
  57  * Tests for FIS unreferenced.
  58  *  - Not subclassed - cleaner cleanup
  59  *  - Subclassed no finalize or close - cleaner cleanup
  60  *  - Subclassed close overridden - AltFinalizer cleanup
  61  *  - Subclasses finalize overridden - cleaner cleanup
  62  *  - Subclasses finalize and close overridden - AltFinalizer cleanup
  63  */
  64 public class UnreferencedFISClosesFd {
  65 
  66     enum CleanupType {
  67         CLOSE,      // Cleanup is handled via calling close
  68         CLEANER}    // Cleanup is handled via Cleaner
  69 
  70     static final String FILE_NAME = "empty.txt";
  71 
  72     /**
  73      * Subclass w/ no overrides; not finalize or close.
  74      * Cleanup should be via the Cleaner (not close).
  75      */
  76     public static class StreamOverrides extends FileInputStream {
  77 
  78         protected final AtomicInteger closeCounter;
  79 
  80         public StreamOverrides(String name) throws FileNotFoundException {
  81             super(name);
  82             closeCounter = new AtomicInteger(0);
  83         }
  84 
  85         final AtomicInteger closeCounter() {
  86             return closeCounter;
  87         }
  88     }
  89 
  90     /**
  91      * Subclass overrides close.
  92      * Cleanup should be via AltFinalizer calling close().
  93      */
  94     public static class StreamOverridesClose extends StreamOverrides {
  95 
  96         public StreamOverridesClose(String name) throws FileNotFoundException {
  97             super(name);
  98         }
  99 
 100         public void close() throws IOException {
 101             closeCounter.incrementAndGet();
 102             super.close();
 103         }
 104     }
 105 
 106     /**
 107      * Subclass overrides finalize.
 108      * Cleanup should be via the Cleaner (not close).
 109      */
 110     public static class StreamOverridesFinalize extends StreamOverrides {
 111 
 112         public StreamOverridesFinalize(String name) throws FileNotFoundException {
 113             super(name);
 114         }
 115 
 116         @SuppressWarnings({"deprecation","removal"})
 117         protected void finalize() throws IOException {
 118             super.finalize();
 119         }
 120     }
 121 
 122     /**
 123      * Subclass overrides finalize and close.
 124      * Cleanup should be via AltFinalizer calling close().
 125      */
 126     public static class StreamOverridesFinalizeClose extends StreamOverridesClose {
 127 
 128         public StreamOverridesFinalizeClose(String name) throws FileNotFoundException {
 129             super(name);
 130         }
 131 
 132         @SuppressWarnings({"deprecation","removal"})
 133         protected void finalize() throws IOException {
 134             super.finalize();
 135         }
 136     }
 137 
 138     /**
 139      * Main runs each test case and reports number of failures.
 140      */
 141     public static void main(String argv[]) throws Exception {
 142 
 143         File inFile = new File(System.getProperty("test.dir", "."), FILE_NAME);
 144         inFile.createNewFile();
 145         inFile.deleteOnExit();
 146 
 147         String name = inFile.getPath();
 148 
 149         long fdCount0 = getFdCount();
 150         System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
 151 
 152         int failCount = 0;
 153         failCount += test(new FileInputStream(name), CleanupType.CLEANER);
 154 
 155         failCount += test(new StreamOverrides(name), CleanupType.CLEANER);
 156 
 157         failCount += test(new StreamOverridesClose(name), CleanupType.CLOSE);
 158 
 159         failCount += test(new StreamOverridesFinalize(name), CleanupType.CLEANER);
 160 
 161         failCount += test(new StreamOverridesFinalizeClose(name), CleanupType.CLOSE);
 162 
 163         if (failCount > 0) {
 164             throw new AssertionError("Failed test count: " + failCount);
 165         }
 166 
 167         // Check the final count of open file descriptors
 168         long fdCount = getFdCount();
 169         System.out.printf("final count of open file descriptors: %d%n", fdCount);
 170         if (fdCount != fdCount0) {
 171             listProcFD();
 172             throw new AssertionError("raw fd count wrong: expected: " + fdCount0
 173                     + ", actual: " + fdCount);
 174         }
 175     }
 176 
 177     // Get the count of open file descriptors, or -1 if not available
 178     private static long getFdCount() {
 179         OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
 180         return  (mxBean instanceof UnixOperatingSystemMXBean)
 181                 ? ((UnixOperatingSystemMXBean) mxBean).getOpenFileDescriptorCount()
 182                 : -1L;
 183     }
 184 
 185     private static int test(FileInputStream fis, CleanupType cleanType) throws Exception {
 186 
 187         try {
 188             System.out.printf("%nTesting %s%n", fis.getClass().getName());
 189 
 190             // Prepare to wait for FIS to be reclaimed
 191             ReferenceQueue<Object> queue = new ReferenceQueue<>();
 192             HashSet<Reference<?>> pending = new HashSet<>();
 193             WeakReference<FileInputStream> msWeak = new WeakReference<>(fis, queue);
 194             pending.add(msWeak);
 195 
 196             FileDescriptor fd = fis.getFD();
 197             WeakReference<FileDescriptor> fdWeak = new WeakReference<>(fd, queue);
 198             pending.add(fdWeak);
 199 
 200             Field fdField = FileDescriptor.class.getDeclaredField("fd");
 201             fdField.setAccessible(true);
 202             int ffd = fdField.getInt(fd);
 203 
 204             Field altFinalizerField = FileInputStream.class.getDeclaredField("altFinalizer");
 205             altFinalizerField.setAccessible(true);
 206             Object altFinalizer = altFinalizerField.get(fis);
 207             if ((altFinalizer != null) ^ (cleanType == CleanupType.CLOSE)) {
 208                 throw new RuntimeException("Unexpected AltFinalizer: " + altFinalizer
 209                 + ", for " + cleanType);
 210             }
 211 
 212             Field cleanupField = FileDescriptor.class.getDeclaredField("cleanup");
 213             cleanupField.setAccessible(true);
 214             Object cleanup = cleanupField.get(fd);
 215             System.out.printf("  cleanup: %s, alt: %s, ffd: %d, cf: %s%n",
 216                     cleanup, altFinalizer, ffd, cleanupField);
 217             if ((cleanup != null) ^ (cleanType == CleanupType.CLEANER)) {
 218                 throw new Exception("unexpected cleanup: "
 219                 + cleanup + ", for " + cleanType);
 220             }
 221             if (cleanup != null) {
 222                 WeakReference<Object> cleanupWeak = new WeakReference<>(cleanup, queue);
 223                 pending.add(cleanupWeak);
 224                 System.out.printf("    fdWeak: %s%n    msWeak: %s%n    cleanupWeak: %s%n",
 225                         fdWeak, msWeak, cleanupWeak);
 226             }
 227             if (altFinalizer != null) {
 228                 WeakReference<Object> altFinalizerWeak = new WeakReference<>(altFinalizer, queue);
 229                 pending.add(altFinalizerWeak);
 230                 System.out.printf("    fdWeak: %s%n    msWeak: %s%n    altFinalizerWeak: %s%n",
 231                         fdWeak, msWeak, altFinalizerWeak);
 232             }
 233 
 234             AtomicInteger closeCounter = fis instanceof StreamOverrides
 235                     ? ((StreamOverrides)fis).closeCounter() : null;
 236 
 237             Reference<?> r;
 238             while (((r = queue.remove(1000L)) != null)
 239                     || !pending.isEmpty()) {
 240                 System.out.printf("    r: %s, pending: %d%n",
 241                         r, pending.size());
 242                 if (r != null) {
 243                     pending.remove(r);
 244                 } else {
 245                     fis = null;
 246                     fd = null;
 247                     cleanup = null;
 248                     altFinalizer = null;
 249                     System.gc();  // attempt to reclaim them
 250                 }
 251             }
 252             Reference.reachabilityFence(fd);
 253             Reference.reachabilityFence(fis);
 254             Reference.reachabilityFence(cleanup);
 255             Reference.reachabilityFence(altFinalizer);
 256 
 257             // Confirm the correct number of calls to close depending on the cleanup type
 258             switch (cleanType) {
 259                 case CLEANER:
 260                     if (closeCounter != null && closeCounter.get() > 0) {
 261                         throw new RuntimeException("Close should not have been called: count: "
 262                                 + closeCounter);
 263                     }
 264                     break;
 265                 case CLOSE:
 266                     if (closeCounter == null || closeCounter.get() == 0) {
 267                         throw new RuntimeException("Close should have been called: count: 0");
 268                     }
 269                     break;
 270             }
 271         } catch (Exception ex) {
 272             ex.printStackTrace(System.out);
 273             return 1;
 274         }
 275         return 0;
 276     }
 277 
 278     /**
 279      * Method to list the open file descriptors (if supported by the 'lsof' command).
 280      */
 281     static void listProcFD() {
 282         List<String> lsofDirs = List.of("/usr/bin", "/usr/sbin");
 283         Optional<Path> lsof = lsofDirs.stream()
 284                 .map(s -> Paths.get(s, "lsof"))
 285                 .filter(f -> Files.isExecutable(f))
 286                 .findFirst();
 287         lsof.ifPresent(exe -> {
 288             try {
 289                 System.out.printf("Open File Descriptors:%n");
 290                 long pid = ProcessHandle.current().pid();
 291                 ProcessBuilder pb = new ProcessBuilder(exe.toString(), "-p", Integer.toString((int) pid));
 292                 pb.inheritIO();
 293                 Process p = pb.start();
 294                 p.waitFor(10, TimeUnit.SECONDS);
 295             } catch (IOException | InterruptedException ie) {
 296                 ie.printStackTrace();
 297             }
 298         });
 299     }
 300 }