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