1 /* 2 * Copyright (c) 2015, 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 6907252 26 * @summary ZipFileInputStream Not Thread-Safe 27 * @library /test/lib 28 * @run main ZipEntryFreeTest 29 */ 30 31 import java.io.*; 32 import java.nio.file.Paths; 33 import java.util.Random; 34 import java.util.Timer; 35 import java.util.TimerTask; 36 import java.util.zip.*; 37 import jdk.test.lib.util.FileUtils; 38 39 public class ZipEntryFreeTest extends Thread { 40 41 private static final int NUM_THREADS = 5; 42 private static final int TEST_ITERATIONS = 5; 43 private static final String ZIPFILE_NAME = "large.zip"; 44 private static final String ZIPENTRY_NAME = "random.txt"; 45 private static InputStream is = null; 46 final Timer timer = new Timer(); 47 48 public static void main(String args[]) throws Exception { 49 createZipFile(); 50 try { 51 for (int i = 0; i < TEST_ITERATIONS; i++) { 52 runTest(); 53 } 54 } finally { 55 FileUtils.deleteFileIfExistsWithRetry(Paths.get(ZIPFILE_NAME)); 56 } 57 } 58 59 private static void runTest() throws Exception { 60 try (ZipFile zf = new ZipFile(new File(ZIPFILE_NAME))) { 61 is = zf.getInputStream(zf.getEntry(ZIPENTRY_NAME + "_0")); 62 Thread[] threadArray = new Thread[NUM_THREADS]; 63 for (int i = 0; i < threadArray.length; i++) { 64 threadArray[i] = new ZipEntryFreeTest(); 65 } 66 for (int i = 0; i < threadArray.length; i++) { 67 threadArray[i].start(); 68 } 69 for (int i = 0; i < threadArray.length; i++) { 70 threadArray[i].join(); 71 } 72 } 73 } 74 75 private static void createZipFile() throws Exception { 76 Random rnd = new Random(1000L); 77 byte[] contents = new byte[2_000_000]; 78 ZipEntry ze = null; 79 80 try (ZipOutputStream zos = 81 new ZipOutputStream(new FileOutputStream(ZIPFILE_NAME))) { 82 // uncompressed mode seemed to tickle the crash 83 zos.setMethod(ZipOutputStream.STORED); 84 for (int ze_count = 0; ze_count < 10; ze_count++) { 85 rnd.nextBytes(contents); 86 ze = createZipEntry(contents, ze_count); 87 zos.putNextEntry(ze); 88 zos.write(contents, 0, contents.length); 89 } 90 zos.flush(); 91 } 92 } 93 94 private static ZipEntry createZipEntry(byte[] b, int i) { 95 ZipEntry ze = new ZipEntry(ZIPENTRY_NAME + "_" + i); 96 ze.setCompressedSize(b.length); 97 ze.setSize(b.length); 98 CRC32 crc = new CRC32(); 99 crc.update(b); 100 ze.setCrc(crc.getValue()); 101 return ze; 102 } 103 104 @Override 105 public void run() { 106 try { 107 int iteration = 0; 108 TimerTask tt = (new TimerTask() { 109 @Override 110 public void run() { 111 try { 112 is.close(); 113 } catch (Exception ex) { 114 ex.printStackTrace(System.out); 115 } 116 } 117 }); 118 timer.schedule(tt, 50); 119 while (is.read() != -1 && iteration++ < 1_000) { } 120 } catch (ZipException ze) { 121 // ZipException now expected instead of ZIP_Read crash 122 System.out.println(ze); 123 } catch (Exception e) { 124 throw new RuntimeException(e); 125 } finally { 126 timer.cancel(); 127 } 128 } 129 }