undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
82
test/jdk/java/io/FileOutputStream/AtomicAppend.java
Normal file
82
test/jdk/java/io/FileOutputStream/AtomicAppend.java
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* 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
|
||||
* @bug 6631352
|
||||
* @summary Check that appends are atomic
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class AtomicAppend {
|
||||
// Before the fix for
|
||||
// 6631352: Implement atomic append mode using FILE_APPEND_DATA (win)
|
||||
// this would fail intermittently on windows
|
||||
void test(String[] args) throws Throwable {
|
||||
final int nThreads = 10;
|
||||
final int writes = 1000;
|
||||
final File file = new File("foo");
|
||||
file.delete();
|
||||
try {
|
||||
final ExecutorService es = Executors.newFixedThreadPool(nThreads);
|
||||
for (int i = 0; i < nThreads; i++)
|
||||
es.execute(new Runnable() { public void run() {
|
||||
try {
|
||||
try (FileOutputStream s = new FileOutputStream(file, true)) {
|
||||
for (int j = 0; j < 1000; j++) {
|
||||
s.write((int) 'x');
|
||||
s.flush();
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) { unexpected(t); }}});
|
||||
es.shutdown();
|
||||
es.awaitTermination(10L, TimeUnit.MINUTES);
|
||||
equal(file.length(), (long) (nThreads * writes));
|
||||
} finally {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
volatile int passed = 0, failed = 0;
|
||||
void pass() {passed++;}
|
||||
void fail() {failed++; Thread.dumpStack();}
|
||||
void fail(String msg) {System.err.println(msg); fail();}
|
||||
void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
void check(boolean cond) {if (cond) pass(); else fail();}
|
||||
void equal(Object x, Object y) {
|
||||
if (x == null ? y == null : x.equals(y)) pass();
|
||||
else fail(x + " not equal to " + y);}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
new AtomicAppend().instanceMain(args);
|
||||
}
|
||||
void instanceMain(String[] args) throws Throwable {
|
||||
try {test(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
}
|
||||
82
test/jdk/java/io/FileOutputStream/FileOpenTest.java
Normal file
82
test/jdk/java/io/FileOutputStream/FileOpenTest.java
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright (c) 2017, 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
|
||||
* @bug 6364894
|
||||
* @requires (os.family == "windows")
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.Asserts
|
||||
* @run main FileOpenTest
|
||||
* @summary Test to ensure that opening of hidden Vs non-hidden,
|
||||
* read/write Vs read-only files for writing works as expected.
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import static jdk.test.lib.Asserts.assertTrue;
|
||||
|
||||
public class FileOpenTest {
|
||||
|
||||
private static File tmpFile;
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
try {
|
||||
tmpFile = File.createTempFile("FileOpenTest", "suffix");
|
||||
|
||||
// Opening Writable Normal File..
|
||||
test(true);
|
||||
|
||||
// Opening Writable Hidden File..
|
||||
Files.setAttribute(tmpFile.toPath(), "dos:hidden", true);
|
||||
test(false);
|
||||
|
||||
// Opening Read-Only Hidden File..
|
||||
Files.setAttribute(tmpFile.toPath(), "dos:hidden", false);
|
||||
tmpFile.setReadOnly();
|
||||
test(false);
|
||||
|
||||
// Opening Read-Only Normal File..
|
||||
Files.setAttribute(tmpFile.toPath(), "dos:hidden", true);
|
||||
test(false);
|
||||
} finally {
|
||||
tmpFile.delete();
|
||||
}
|
||||
}
|
||||
|
||||
private static void test(boolean writable) throws Exception {
|
||||
|
||||
try (FileOutputStream fs = new FileOutputStream(tmpFile)) {
|
||||
fs.write(1);
|
||||
assertTrue(writable, "Able to open READ-ONLY file for WRITING!");
|
||||
assertTrue(tmpFile.canWrite(), "Able to open READ-ONLY file for WRITING!");
|
||||
} catch(IOException e) {
|
||||
assertTrue(!writable, "Unable to open non-READ-ONLY file for WRITING!");
|
||||
System.out.println("Caught the Exception as expected");
|
||||
e.printStackTrace(System.out);
|
||||
}
|
||||
}
|
||||
}
|
||||
58
test/jdk/java/io/FileOutputStream/ManyFiles.java
Normal file
58
test/jdk/java/io/FileOutputStream/ManyFiles.java
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2025, 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
|
||||
* @bug 4189011 5019303
|
||||
* @summary Test opening over 2048 files
|
||||
* @run main/timeout=300 ManyFiles
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class ManyFiles {
|
||||
static int count;
|
||||
static List files = new ArrayList();
|
||||
static List streams = new ArrayList();
|
||||
static int NUM_FILES = 2050;
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
for (int n = 0; n < NUM_FILES; n++) {
|
||||
File f = new File("file" + count++);
|
||||
files.add(f);
|
||||
streams.add(new FileOutputStream(f));
|
||||
}
|
||||
|
||||
Iterator i = streams.iterator();
|
||||
while(i.hasNext()) {
|
||||
FileOutputStream fos = (FileOutputStream)i.next();
|
||||
fos.close();
|
||||
}
|
||||
|
||||
i = files.iterator();
|
||||
while(i.hasNext()) {
|
||||
File f = (File)i.next();
|
||||
f.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
54
test/jdk/java/io/FileOutputStream/OpenNUL.java
Normal file
54
test/jdk/java/io/FileOutputStream/OpenNUL.java
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright (c) 2022, 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
|
||||
* @bug 8285445
|
||||
* @requires (os.family == "windows")
|
||||
* @summary Verify behavior of opening "NUL:" with ADS enabled and disabled.
|
||||
* @run main/othervm OpenNUL
|
||||
* @run main/othervm -Djdk.io.File.enableADS OpenNUL
|
||||
* @run main/othervm -Djdk.io.File.enableADS=FalsE OpenNUL
|
||||
* @run main/othervm -Djdk.io.File.enableADS=true OpenNUL
|
||||
*/
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class OpenNUL {
|
||||
public static void main(String args[]) throws IOException {
|
||||
String enableADS = System.getProperty("jdk.io.File.enableADS", "true");
|
||||
boolean fails = enableADS.equalsIgnoreCase(Boolean.FALSE.toString());
|
||||
|
||||
FileOutputStream fos;
|
||||
try {
|
||||
fos = new FileOutputStream("NUL:");
|
||||
if (fails)
|
||||
throw new RuntimeException("Should have failed");
|
||||
} catch (FileNotFoundException fnfe) {
|
||||
if (!fails)
|
||||
throw new RuntimeException("Should not have failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
126
test/jdk/java/io/FileOutputStream/OpsAfterClose.java
Normal file
126
test/jdk/java/io/FileOutputStream/OpsAfterClose.java
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* Copyright (c) 2007, 2013, 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
|
||||
* @bug 6359397
|
||||
* @summary Test if FileOutputStream methods will check if the stream
|
||||
* has been closed.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public enum OpsAfterClose {
|
||||
|
||||
WRITE { boolean check(FileOutputStream r) {
|
||||
try {
|
||||
r.write(1);
|
||||
} catch (IOException io) {
|
||||
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} },
|
||||
|
||||
WRITE_BUF { boolean check(FileOutputStream r) {
|
||||
try {
|
||||
byte buf[] = new byte[2];
|
||||
r.write(buf);
|
||||
} catch (IOException io) {
|
||||
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} },
|
||||
WRITE_BUF_OFF { boolean check(FileOutputStream r) {
|
||||
try {
|
||||
byte buf[] = new byte[2];
|
||||
int len = 1;
|
||||
r.write(buf, 0, len);
|
||||
} catch (IOException io) {
|
||||
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} },
|
||||
GET_CHANNEL { boolean check(FileOutputStream r) {
|
||||
r.getChannel();
|
||||
return true;
|
||||
} },
|
||||
GET_FD { boolean check(FileOutputStream r) {
|
||||
try {
|
||||
r.getFD();
|
||||
return true;
|
||||
} catch (IOException io) {
|
||||
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
||||
return false;
|
||||
}
|
||||
} },
|
||||
CLOSE { boolean check(FileOutputStream r) {
|
||||
try {
|
||||
r.close();
|
||||
return true; // No Exceptin thrown on Windows
|
||||
} catch (IOException io) {
|
||||
System.out.print("Excep Msg: "+ io.getMessage() + ", ");
|
||||
return true; // Exception thrown on solaris and linux
|
||||
}
|
||||
} };
|
||||
|
||||
abstract boolean check(FileOutputStream r);
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
|
||||
boolean failed = false;
|
||||
|
||||
File f = new File(System.getProperty("test.dir", "."),
|
||||
"f.txt");
|
||||
f.createNewFile();
|
||||
f.deleteOnExit();
|
||||
|
||||
FileOutputStream fis = new FileOutputStream(f);
|
||||
if (testFileOutputStream(fis)) {
|
||||
throw new Exception("Test failed for some of the operation{s}" +
|
||||
" on FileOutputStream, check the messages");
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean testFileOutputStream(FileOutputStream r)
|
||||
throws Exception {
|
||||
r.close();
|
||||
boolean failed = false;
|
||||
boolean result;
|
||||
System.out.println("Testing File:" + r);
|
||||
for (OpsAfterClose op : OpsAfterClose.values()) {
|
||||
result = op.check(r);
|
||||
if (!result) {
|
||||
failed = true;
|
||||
}
|
||||
System.out.println(op + ":" + result);
|
||||
}
|
||||
if (failed) {
|
||||
System.out.println("Test failed for the failed operation{s}" +
|
||||
" above for the FileOutputStream:" + r);
|
||||
}
|
||||
return failed;
|
||||
}
|
||||
}
|
||||
233
test/jdk/java/io/FileOutputStream/UnreferencedFOSClosesFd.java
Normal file
233
test/jdk/java/io/FileOutputStream/UnreferencedFOSClosesFd.java
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
/*
|
||||
* Copyright (c) 2007, 2025, 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 the fd is closed if left unreferenced
|
||||
* @run main/othervm/timeout=480 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 {
|
||||
|
||||
static final String FILE_NAME = "empty.txt";
|
||||
|
||||
/**
|
||||
* Subclass w/ no overrides; not close.
|
||||
* Cleanup should be via the Cleaner.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
public static class StreamOverridesFinalize extends StreamOverrides {
|
||||
|
||||
public StreamOverridesFinalize(String name) throws FileNotFoundException {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"deprecation","removal"})
|
||||
protected void finalize() throws IOException, Throwable {
|
||||
super.finalize();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclass overrides finalize and close.
|
||||
* Cleanup should be via the Cleaner.
|
||||
*/
|
||||
public static class StreamOverridesFinalizeClose extends StreamOverridesClose {
|
||||
|
||||
public StreamOverridesFinalizeClose(String name) throws FileNotFoundException {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"deprecation","removal"})
|
||||
protected void finalize() throws IOException, Throwable {
|
||||
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));
|
||||
|
||||
failCount += test(new StreamOverrides(name));
|
||||
|
||||
failCount += test(new StreamOverridesClose(name));
|
||||
|
||||
failCount += test(new StreamOverridesFinalize(name));
|
||||
|
||||
failCount += test(new StreamOverridesFinalizeClose(name));
|
||||
|
||||
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) 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 cleanupField = FileDescriptor.class.getDeclaredField("cleanup");
|
||||
cleanupField.setAccessible(true);
|
||||
Object cleanup = cleanupField.get(fd);
|
||||
System.out.printf(" cleanup: %s, ffd: %d, cf: %s%n", cleanup, ffd, cleanupField);
|
||||
if (cleanup == null) {
|
||||
throw new RuntimeException("cleanup should not be 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);
|
||||
|
||||
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;
|
||||
System.gc(); // attempt to reclaim them
|
||||
}
|
||||
}
|
||||
Reference.reachabilityFence(fd);
|
||||
Reference.reachabilityFence(fos);
|
||||
Reference.reachabilityFence(cleanup);
|
||||
|
||||
// Confirm the correct number of calls to close depending on the cleanup type
|
||||
if (closeCounter != null && closeCounter.get() > 0) {
|
||||
throw new RuntimeException("Close should not have been called: count: " + closeCounter);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace(System.out);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue