1 /*
2  * Copyright (c) 2015, 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  * @test
26  * @bug 8137121 8137230
27  * @summary (fc) Infinite loop FileChannel.truncate
28  * @library /lib/testlibrary
29  * @build jdk.testlibrary.Utils
30  * @run main/othervm LoopingTruncate
31  */
32 
33 import java.nio.ByteBuffer;
34 import java.nio.channels.FileChannel;
35 import java.nio.channels.ClosedByInterruptException;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import static java.nio.file.StandardOpenOption.*;
39 import static jdk.testlibrary.Utils.adjustTimeout;
40 
41 public class LoopingTruncate {
42 
43     // (int)FATEFUL_SIZE == -3 == IOStatus.INTERRUPTED
44     static long FATEFUL_SIZE = 0x1FFFFFFFDL;
45 
46     // At least 20 seconds
47     static long TIMEOUT = adjustTimeout(20_000);
48 
main(String[] args)49     public static void main(String[] args) throws Throwable {
50         Path path = Files.createTempFile("LoopingTruncate.tmp", null);
51         try (FileChannel fc = FileChannel.open(path, CREATE, WRITE)) {
52             fc.position(FATEFUL_SIZE + 1L);
53             fc.write(ByteBuffer.wrap(new byte[] {0}));
54 
55             Thread th = new Thread(() -> {
56                 try {
57                     fc.truncate(FATEFUL_SIZE);
58                 } catch (ClosedByInterruptException ignore) {
59                 } catch (Exception e) {
60                     throw new RuntimeException(e);
61                 }});
62             th.start();
63             th.join(TIMEOUT);
64 
65             if (th.isAlive()) {
66                 System.err.println("=== Stack trace of the guilty thread:");
67                 for (StackTraceElement el : th.getStackTrace()) {
68                     System.err.println("\t" + el);
69                 }
70                 System.err.println("===");
71 
72                 th.interrupt();
73                 th.join();
74                 throw new RuntimeException("Failed to complete on time");
75             }
76         } finally {
77             Files.deleteIfExists(path);
78         }
79     }
80 }
81