1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/*
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
*
* @test
* @modules java.base/java.io:open
* @library /test/lib
* @build jdk.test.lib.util.FileUtils UnreferencedFOSClosesFd
* @bug 6524062
* @summary Test to ensure that FOS.finalize() invokes the close() method as per
* the specification.
* @run main/othervm UnreferencedFOSClosesFd
*/
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.concurrent.atomic.AtomicInteger;
import com.sun.management.UnixOperatingSystemMXBean;
import jdk.test.lib.util.FileUtils;
public class UnreferencedFOSClosesFd {
enum CleanupType {
CLOSE, // Cleanup is handled via calling close
CLEANER} // Cleanup is handled via Cleaner
static final String FILE_NAME = "empty.txt";
/**
* Subclass w/ no overrides; not finalize or close.
* Cleanup should be via the Cleaner (not close).
*/
public static class StreamOverrides extends FileOutputStream {
protected final AtomicInteger closeCounter;
public StreamOverrides(String name) throws FileNotFoundException {
super(name);
closeCounter = new AtomicInteger(0);
}
final AtomicInteger closeCounter() {
return closeCounter;
}
}
/**
* Subclass overrides close.
* Cleanup should be via AltFinalizer calling close().
*/
public static class StreamOverridesClose extends StreamOverrides {
public StreamOverridesClose(String name) throws FileNotFoundException {
super(name);
}
public void close() throws IOException {
closeCounter.incrementAndGet();
super.close();
}
}
/**
* Subclass overrides finalize and close.
* Cleanup should be via the Cleaner (not close).
*/
public static class StreamOverridesFinalize extends StreamOverrides {
public StreamOverridesFinalize(String name) throws FileNotFoundException {
super(name);
}
@SuppressWarnings({"deprecation","removal"})
protected void finalize() throws IOException {
super.finalize();
}
}
/**
* Subclass overrides finalize and close.
* Cleanup should be via AltFinalizer calling close().
*/
public static class StreamOverridesFinalizeClose extends StreamOverridesClose {
public StreamOverridesFinalizeClose(String name) throws FileNotFoundException {
super(name);
}
@SuppressWarnings({"deprecation","removal"})
protected void finalize() throws IOException {
super.finalize();
}
}
/**
* Main runs each test case and reports number of failures.
*/
public static void main(String argv[]) throws Exception {
File inFile = new File(System.getProperty("test.dir", "."), FILE_NAME);
inFile.createNewFile();
inFile.deleteOnExit();
String name = inFile.getPath();
FileUtils.listFileDescriptors(System.out);
long fdCount0 = getFdCount();
int failCount = 0;
failCount += test(new FileOutputStream(name), CleanupType.CLEANER);
failCount += test(new StreamOverrides(name), CleanupType.CLEANER);
failCount += test(new StreamOverridesClose(name), CleanupType.CLOSE);
failCount += test(new StreamOverridesFinalize(name), CleanupType.CLEANER);
failCount += test(new StreamOverridesFinalizeClose(name), CleanupType.CLOSE);
if (failCount > 0) {
throw new AssertionError("Failed test count: " + failCount);
}
// Check the final count of open file descriptors
long fdCount = getFdCount();
if (fdCount != fdCount0) {
System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
System.out.printf("final count of open file descriptors: %d%n", fdCount);
FileUtils.listFileDescriptors(System.out);
}
}
// Get the count of open file descriptors, or -1 if not available
private static long getFdCount() {
OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
return (mxBean instanceof UnixOperatingSystemMXBean)
? ((UnixOperatingSystemMXBean) mxBean).getOpenFileDescriptorCount()
: -1L;
}
private static int test(FileOutputStream fos, CleanupType cleanType) throws Exception {
try {
System.out.printf("%nTesting %s%n", fos.getClass().getName());
// Prepare to wait for FOS to be reclaimed
ReferenceQueue<Object> queue = new ReferenceQueue<>();
HashSet<Reference<?>> pending = new HashSet<>();
WeakReference<FileOutputStream> msWeak = new WeakReference<>(fos, queue);
pending.add(msWeak);
FileDescriptor fd = fos.getFD();
WeakReference<FileDescriptor> fdWeak = new WeakReference<>(fd, queue);
pending.add(fdWeak);
Field fdField = FileDescriptor.class.getDeclaredField("fd");
fdField.setAccessible(true);
int ffd = fdField.getInt(fd);
Field altFinalizerField = FileOutputStream.class.getDeclaredField("altFinalizer");
altFinalizerField.setAccessible(true);
Object altFinalizer = altFinalizerField.get(fos);
if ((altFinalizer != null) ^ (cleanType == CleanupType.CLOSE)) {
throw new RuntimeException("Unexpected AltFinalizer: " + altFinalizer
+ ", for " + cleanType);
}
Field cleanupField = FileDescriptor.class.getDeclaredField("cleanup");
cleanupField.setAccessible(true);
Object cleanup = cleanupField.get(fd);
System.out.printf(" cleanup: %s, alt: %s, ffd: %d, cf: %s%n",
cleanup, altFinalizer, ffd, cleanupField);
if ((cleanup != null) ^ (cleanType == CleanupType.CLEANER)) {
throw new Exception("unexpected cleanup: "
+ cleanup + ", for " + cleanType);
}
if (cleanup != null) {
WeakReference<Object> cleanupWeak = new WeakReference<>(cleanup, queue);
pending.add(cleanupWeak);
System.out.printf(" fdWeak: %s%n msWeak: %s%n cleanupWeak: %s%n",
fdWeak, msWeak, cleanupWeak);
}
if (altFinalizer != null) {
WeakReference<Object> altFinalizerWeak = new WeakReference<>(altFinalizer, queue);
pending.add(altFinalizerWeak);
System.out.printf(" fdWeak: %s%n msWeak: %s%n altFinalizerWeak: %s%n",
fdWeak, msWeak, altFinalizerWeak);
}
AtomicInteger closeCounter = fos instanceof StreamOverrides
? ((StreamOverrides) fos).closeCounter() : null;
Reference<?> r;
while (((r = queue.remove(1000L)) != null)
|| !pending.isEmpty()) {
System.out.printf(" r: %s, pending: %d%n",
r, pending.size());
if (r != null) {
pending.remove(r);
} else {
fos = null;
fd = null;
cleanup = null;
altFinalizer = null;
System.gc(); // attempt to reclaim them
}
}
Reference.reachabilityFence(fd);
Reference.reachabilityFence(fos);
Reference.reachabilityFence(cleanup);
Reference.reachabilityFence(altFinalizer);
// Confirm the correct number of calls to close depending on the cleanup type
switch (cleanType) {
case CLEANER:
if (closeCounter != null && closeCounter.get() > 0) {
throw new RuntimeException("Close should not have been called: count: "
+ closeCounter);
}
break;
case CLOSE:
if (closeCounter == null || closeCounter.get() == 0) {
throw new RuntimeException("Close should have been called: count: 0");
}
break;
}
} catch (Exception ex) {
ex.printStackTrace(System.out);
return 1;
}
return 0;
}
}
|