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