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