1 /*
   2  * Copyright (c) 2014, 2017, 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 8038491
  26  * @summary Crash in ZipFile.read() when ZipFileInputStream is shared between threads
  27  * @library /test/lib
  28  * @build jdk.test.lib.Platform
  29  *        jdk.test.lib.Utils
  30  *        jdk.test.lib.Asserts
  31  *        jdk.test.lib.JDKToolFinder
  32  *        jdk.test.lib.JDKToolLauncher
  33  *        jdk.test.lib.process.*
  34  *        jdk.test.lib.util.FileUtils
  35  * @run main MultiThreadedReadTest
  36  * @key randomness
  37  */
  38 
  39 import java.io.File;
  40 import java.io.FileOutputStream;
  41 import java.io.InputStream;
  42 import java.nio.file.Paths;
  43 import java.util.Random;
  44 import java.util.zip.ZipEntry;
  45 import java.util.zip.ZipFile;
  46 import java.util.zip.ZipOutputStream;
  47 import jdk.test.lib.util.FileUtils;
  48 
  49 public class MultiThreadedReadTest extends Thread {
  50 
  51     private static final int NUM_THREADS = 10;
  52     private static final String ZIPFILE_NAME = "large.zip";
  53     private static final String ZIPENTRY_NAME = "random.txt";
  54     private static InputStream is = null;
  55 
  56     public static void main(String args[]) throws Exception {
  57         createZipFile();
  58         try (ZipFile zf = new ZipFile(new File(ZIPFILE_NAME))) {
  59             is = zf.getInputStream(zf.getEntry(ZIPENTRY_NAME));
  60             Thread[] threadArray = new Thread[NUM_THREADS];
  61             for (int i = 0; i < threadArray.length; i++) {
  62                 threadArray[i] = new MultiThreadedReadTest();
  63             }
  64             for (int i = 0; i < threadArray.length; i++) {
  65                 threadArray[i].start();
  66             }
  67             for (int i = 0; i < threadArray.length; i++) {
  68                 threadArray[i].join();
  69             }
  70         } finally {
  71             FileUtils.deleteFileIfExistsWithRetry(Paths.get(ZIPFILE_NAME));
  72         }
  73     }
  74 
  75     private static void createZipFile() throws Exception {
  76         try (ZipOutputStream zos =
  77             new ZipOutputStream(new FileOutputStream(ZIPFILE_NAME))) {
  78 
  79             zos.putNextEntry(new ZipEntry(ZIPENTRY_NAME));
  80             StringBuilder sb = new StringBuilder();
  81             Random rnd = new Random();
  82             for(int i = 0; i < 1000; i++) {
  83                 // append some random string for ZipEntry
  84                 sb.append(Long.toString(rnd.nextLong()));
  85             }
  86             byte[] b = sb.toString().getBytes();
  87             zos.write(b, 0, b.length);
  88         }
  89     }
  90 
  91     @Override
  92     public void run() {
  93         try {
  94             while (is.read() != -1) { }
  95         } catch (Exception e) {
  96             // Swallow any Exceptions (which are expected) - we're only interested in the crash
  97         }
  98     }
  99 }