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