1 /*
   2  * Copyright (c) 2011, 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 
  25 /**
  26  * @test
  27  * @bug 7068051
  28  * @summary SIGSEGV in PhaseIdealLoop::build_loop_late_post on T5440
  29  * @library /testlibrary
  30  *
  31  * @run main/othervm -showversion -Xbatch Test7068051
  32  */
  33 
  34 import com.oracle.java.testlibrary.JDKToolLauncher;
  35 import com.oracle.java.testlibrary.OutputAnalyzer;
  36 
  37 import java.io.IOException;
  38 import java.io.InputStream;
  39 import java.nio.file.Files;
  40 import java.nio.file.Path;
  41 import java.nio.file.Paths;
  42 import java.nio.file.StandardCopyOption;
  43 import java.util.ArrayList;
  44 import java.util.Enumeration;
  45 import java.util.List;
  46 import java.util.zip.ZipEntry;
  47 import java.util.zip.ZipFile;
  48 
  49 public class Test7068051 {
  50     private static final String SELF_NAME = Test7068051.class.getSimpleName();
  51     private static final String SELF_FILE_NAME = SELF_NAME + ".java";
  52     private static final String JAR_NAME = "foo.jar";
  53     private static final String TEST_PATH = System.getProperty("test.src");
  54     private static final Path CURRENT_DIR = Paths.get(".");
  55     private static final Path TEST_SOURCE_PATH = Paths.get(TEST_PATH, SELF_FILE_NAME);
  56 
  57     public static void main (String[] args) throws IOException {
  58         createTestJarFile();
  59         System.out.println("Running test...");
  60 
  61         try (ZipFile zf = new ZipFile(JAR_NAME)) {
  62 
  63             Enumeration<? extends ZipEntry> entries = zf.entries();
  64             ArrayList<String> names = new ArrayList<String>();
  65             while (entries.hasMoreElements()) {
  66                 names.add(entries.nextElement().getName());
  67             }
  68 
  69             byte[] bytes = new byte[16];
  70             for (String name : names) {
  71                 ZipEntry e = zf.getEntry(name);
  72 
  73                 if (e.isDirectory()) {
  74                     continue;
  75                 }
  76 
  77                 try (final InputStream is = zf.getInputStream(e)) {
  78                     try {
  79                         while (is.read(bytes) >= 0) {
  80                         }
  81                     } catch (IOException x) {
  82                         System.out.println("..................................");
  83                         System.out.println("          -->  is :" + is);
  84                         System.out.println("          is.hash :" + is.hashCode());
  85                         System.out.println();
  86                         System.out.println("           e.name :" + e.getName());
  87                         System.out.println("           e.hash :" + e.hashCode());
  88                         System.out.println("         e.method :" + e.getMethod());
  89                         System.out.println("           e.size :" + e.getSize());
  90                         System.out.println("          e.csize :" + e.getCompressedSize());
  91                         System.out.println("..................................");
  92 
  93                         throw new AssertionError("IOException was throwing while read the archive. Test failed.", x);
  94                     }
  95                 }
  96             }
  97         }
  98         System.out.println("Test passed.");
  99     }
 100 
 101     private static void createTestJarFile() {
 102         ArrayList<String> jarOptions = new ArrayList<>();
 103 
 104         // jar cf foo.jar *
 105         System.out.println("Creating jar file..");
 106         jarOptions.add("cf");
 107         jarOptions.add(JAR_NAME);
 108         try {
 109             for (int i = 0; i < 100; ++i) {
 110                 Path temp = Files.createTempFile(CURRENT_DIR, SELF_NAME, ".java");
 111                 Files.copy(TEST_SOURCE_PATH, temp, StandardCopyOption.REPLACE_EXISTING);
 112                 jarOptions.add(temp.toString());
 113             }
 114         } catch (IOException ex) {
 115             throw new AssertionError("TESTBUG: Creating temp files failed.", ex);
 116         }
 117         runJar(jarOptions);
 118 
 119         // jar -uf0 foo.jar Test7068051.java
 120         System.out.println("Adding unpacked file...");
 121         jarOptions.clear();
 122         jarOptions.add("-uf0");
 123         jarOptions.add(JAR_NAME);
 124         jarOptions.add(TEST_SOURCE_PATH.toString());
 125         runJar(jarOptions);
 126     }
 127 
 128     private static void runJar(List<String> params) {
 129         JDKToolLauncher jar = JDKToolLauncher.create("jar");
 130         for (String p : params) {
 131             jar.addToolArg(p);
 132         }
 133         ProcessBuilder pb = new ProcessBuilder(jar.getCommand());
 134         try {
 135             OutputAnalyzer output = new OutputAnalyzer(pb.start());
 136             output.shouldHaveExitValue(0);
 137         } catch (IOException ex) {
 138             throw new AssertionError("TESTBUG: jar failed.", ex);
 139         }
 140     }
 141 }