1 /*
   2  * Copyright (c) 2013, 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 /* @test
  25  * @bug 7129312
  26  * @summary BufferedInputStream calculates negative array size with large
  27  *          streams and mark
  28  * @library /lib/testlibrary
  29  * @run main/othervm LargeCopyWithMark
  30  */
  31 
  32 import java.io.BufferedInputStream;
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.io.OutputStream;
  36 import static jdk.testlibrary.ProcessTools.*;
  37 
  38 
  39 public class LargeCopyWithMark {
  40 
  41     public static void main(String[] args) throws Exception {
  42         if (! System.getProperty("os.arch").contains("64")) {
  43             System.out.println("Test runs on 64 bit platforms");
  44             return;
  45         }
  46         ProcessBuilder pb = createJavaProcessBuilder("-Xmx4G",
  47                 "-ea:LargeCopyWithMark$Child",
  48                 "LargeCopyWithMark$Child");
  49         int res = pb.inheritIO().start().waitFor();
  50         if (res != 0) {
  51             throw new AssertionError("Test failed: exit code = " + res);
  52         }
  53     }
  54 
  55     public static class Child {
  56         static final int BUFF_SIZE = 8192;
  57         static final int BIS_BUFF_SIZE = Integer.MAX_VALUE / 2 + 100;
  58         static final long BYTES_TO_COPY = 2L * Integer.MAX_VALUE;
  59 
  60         static {
  61             assert BIS_BUFF_SIZE * 2 < 0 : "doubling must overflow";
  62         }
  63 
  64         public static void main(String[] args) throws Exception {
  65             byte[] buff = new byte[BUFF_SIZE];
  66 
  67             try (InputStream myis = new MyInputStream(BYTES_TO_COPY);
  68                  InputStream bis = new BufferedInputStream(myis, BIS_BUFF_SIZE);
  69                  OutputStream myos = new MyOutputStream()) {
  70 
  71                 // will require a buffer bigger than BIS_BUFF_SIZE
  72                 bis.mark(BIS_BUFF_SIZE + 100);
  73 
  74                 for (;;) {
  75                     int count = bis.read(buff, 0, BUFF_SIZE);
  76                     if (count == -1)
  77                         break;
  78                     myos.write(buff, 0, count);
  79                 }
  80             } catch (java.lang.NegativeArraySizeException e) {
  81                 e.printStackTrace();
  82                 System.exit(11);
  83             } catch (Exception e) {
  84                 e.printStackTrace();
  85             }
  86         }
  87     }
  88 }
  89 
  90 class MyInputStream extends InputStream {
  91     private long bytesLeft;
  92     public MyInputStream(long bytesLeft) {
  93         this.bytesLeft = bytesLeft;
  94     }
  95     @Override public int read() throws IOException {
  96         return 0;
  97     }
  98     @Override public int read(byte[] b) throws IOException {
  99         return read(b, 0, b.length);
 100     }
 101     @Override public int read(byte[] b, int off, int len) throws IOException {
 102         if (bytesLeft <= 0)
 103             return -1;
 104         long result = Math.min(bytesLeft, (long)len);
 105         bytesLeft -= result;
 106         return (int)result;
 107     }
 108     @Override public int available() throws IOException {
 109         return (bytesLeft > 0) ? 1 : 0;
 110     }
 111 }
 112 
 113 class MyOutputStream extends OutputStream {
 114     @Override public void write(int b) throws IOException {}
 115     @Override public void write(byte[] b) throws IOException {}
 116     @Override public void write(byte[] b, int off, int len) throws IOException {}
 117 }