|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2021, BELLSOFT. All rights reserved. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 21 | + * or visit www.oracle.com if you need additional information or have any |
| 22 | + * questions. |
| 23 | + */ |
| 24 | + |
| 25 | +/* |
| 26 | + * @test |
| 27 | + * @bug 8266310 |
| 28 | + * @summary Checks if there's no deadlock between the two lock objects - |
| 29 | + * class loading lock and ClassLoader.loadedLibraryNames hashmap. |
| 30 | + * @library /test/lib |
| 31 | + * @build LoadLibraryDeadlock Class1 p.Class2 |
| 32 | + * @run main/othervm/native -Xcheck:jni TestLoadLibraryDeadlock |
| 33 | + */ |
| 34 | + |
| 35 | +import jdk.test.lib.Asserts; |
| 36 | +import jdk.test.lib.JDKToolFinder; |
| 37 | +import jdk.test.lib.process.*; |
| 38 | +import jdk.test.lib.util.FileUtils; |
| 39 | + |
| 40 | +import java.lang.ProcessBuilder; |
| 41 | +import java.lang.Process; |
| 42 | +import java.nio.file.Paths; |
| 43 | +import java.io.*; |
| 44 | +import java.util.*; |
| 45 | +import java.util.concurrent.*; |
| 46 | +import java.util.spi.ToolProvider; |
| 47 | + |
| 48 | +public class TestLoadLibraryDeadlock { |
| 49 | + |
| 50 | + private static final ToolProvider JAR = ToolProvider.findFirst("jar") |
| 51 | + .orElseThrow(() -> new RuntimeException("ToolProvider for jar not found")); |
| 52 | + |
| 53 | + private static final String KEYSTORE = "keystore.jks"; |
| 54 | + private static final String STOREPASS = "changeit"; |
| 55 | + private static final String KEYPASS = "changeit"; |
| 56 | + private static final String ALIAS = "test"; |
| 57 | + private static final String DNAME = "CN=test"; |
| 58 | + private static final String VALIDITY = "366"; |
| 59 | + |
| 60 | + private static String testClassPath = System.getProperty("test.classes"); |
| 61 | + private static String testLibraryPath = System.getProperty("test.nativepath"); |
| 62 | + private static String classPathSeparator = System.getProperty("path.separator"); |
| 63 | + |
| 64 | + private static OutputAnalyzer runCommand(File workingDirectory, String... commands) throws Throwable { |
| 65 | + ProcessBuilder pb = new ProcessBuilder(commands); |
| 66 | + pb.directory(workingDirectory); |
| 67 | + System.out.println("COMMAND: " + String.join(" ", commands)); |
| 68 | + return ProcessTools.executeProcess(pb); |
| 69 | + } |
| 70 | + |
| 71 | + private static OutputAnalyzer runCommandInTestClassPath(String... commands) throws Throwable { |
| 72 | + return runCommand(new File(testClassPath), commands); |
| 73 | + } |
| 74 | + |
| 75 | + private static OutputAnalyzer genKey() throws Throwable { |
| 76 | + FileUtils.deleteFileIfExistsWithRetry( |
| 77 | + Paths.get(testClassPath, KEYSTORE)); |
| 78 | + String keytool = JDKToolFinder.getJDKTool("keytool"); |
| 79 | + return runCommandInTestClassPath(keytool, |
| 80 | + "-storepass", STOREPASS, |
| 81 | + "-keypass", KEYPASS, |
| 82 | + "-keystore", KEYSTORE, |
| 83 | + "-keyalg", "rsa", "-keysize", "2048", |
| 84 | + "-genkeypair", |
| 85 | + "-alias", ALIAS, |
| 86 | + "-dname", DNAME, |
| 87 | + "-validity", VALIDITY |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + private static void createJar(String outputJar, String... classes) throws Throwable { |
| 92 | + List<String> args = new ArrayList<>(); |
| 93 | + Collections.addAll(args, "cvf", Paths.get(testClassPath, outputJar).toString()); |
| 94 | + for (String c : classes) { |
| 95 | + Collections.addAll(args, "-C", testClassPath, c); |
| 96 | + } |
| 97 | + if (JAR.run(System.out, System.err, args.toArray(new String[0])) != 0) { |
| 98 | + throw new RuntimeException("jar operation failed"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static OutputAnalyzer signJar(String jarToSign) throws Throwable { |
| 103 | + String jarsigner = JDKToolFinder.getJDKTool("jarsigner"); |
| 104 | + return runCommandInTestClassPath(jarsigner, |
| 105 | + "-keystore", KEYSTORE, |
| 106 | + "-storepass", STOREPASS, |
| 107 | + jarToSign, ALIAS |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + private static Process runJavaCommand(String... command) throws Throwable { |
| 112 | + String java = JDKToolFinder.getJDKTool("java"); |
| 113 | + List<String> commands = new ArrayList<>(); |
| 114 | + Collections.addAll(commands, java); |
| 115 | + Collections.addAll(commands, command); |
| 116 | + System.out.println("COMMAND: " + String.join(" ", commands)); |
| 117 | + return new ProcessBuilder(commands.toArray(new String[0])) |
| 118 | + .redirectErrorStream(true) |
| 119 | + .directory(new File(testClassPath)) |
| 120 | + .start(); |
| 121 | + } |
| 122 | + |
| 123 | + private static OutputAnalyzer jcmd(long pid, String command) throws Throwable { |
| 124 | + String jcmd = JDKToolFinder.getJDKTool("jcmd"); |
| 125 | + return runCommandInTestClassPath(jcmd, |
| 126 | + String.valueOf(pid), |
| 127 | + command |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + private static String readAvailable(final InputStream is) throws Throwable { |
| 132 | + final List<String> list = Collections.synchronizedList(new ArrayList<String>()); |
| 133 | + ExecutorService executor = Executors.newFixedThreadPool(2); |
| 134 | + Future<String> future = executor.submit(new Callable<String>() { |
| 135 | + public String call() { |
| 136 | + String result = new String(); |
| 137 | + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
| 138 | + try { |
| 139 | + while(true) { |
| 140 | + String s = reader.readLine(); |
| 141 | + if (s.length() > 0) { |
| 142 | + list.add(s); |
| 143 | + result += s + "\n"; |
| 144 | + } |
| 145 | + } |
| 146 | + } catch (IOException ignore) {} |
| 147 | + return result; |
| 148 | + } |
| 149 | + }); |
| 150 | + try { |
| 151 | + return future.get(1000, TimeUnit.MILLISECONDS); |
| 152 | + } catch (Exception ignoreAll) { |
| 153 | + future.cancel(true); |
| 154 | + return String.join("\n", list); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private final static long countLines(OutputAnalyzer output, String string) { |
| 159 | + return output.asLines() |
| 160 | + .stream() |
| 161 | + .filter(s -> s.contains(string)) |
| 162 | + .count(); |
| 163 | + } |
| 164 | + |
| 165 | + private final static void dump(OutputAnalyzer output) { |
| 166 | + output.asLines() |
| 167 | + .stream() |
| 168 | + .forEach(s -> System.out.println(s)); |
| 169 | + } |
| 170 | + |
| 171 | + public static void main(String[] args) throws Throwable { |
| 172 | + genKey() |
| 173 | + .shouldHaveExitValue(0); |
| 174 | + |
| 175 | + FileUtils.deleteFileIfExistsWithRetry( |
| 176 | + Paths.get(testClassPath, "a.jar")); |
| 177 | + FileUtils.deleteFileIfExistsWithRetry( |
| 178 | + Paths.get(testClassPath, "b.jar")); |
| 179 | + FileUtils.deleteFileIfExistsWithRetry( |
| 180 | + Paths.get(testClassPath, "c.jar")); |
| 181 | + |
| 182 | + createJar("a.jar", |
| 183 | + "LoadLibraryDeadlock.class", |
| 184 | + "LoadLibraryDeadlock$1.class", |
| 185 | + "LoadLibraryDeadlock$2.class"); |
| 186 | + |
| 187 | + createJar("b.jar", |
| 188 | + "Class1.class"); |
| 189 | + |
| 190 | + createJar("c.jar", |
| 191 | + "p/Class2.class"); |
| 192 | + |
| 193 | + signJar("c.jar") |
| 194 | + .shouldHaveExitValue(0); |
| 195 | + |
| 196 | + // load trigger class |
| 197 | + Process process = runJavaCommand("-cp", |
| 198 | + "a.jar" + classPathSeparator + |
| 199 | + "b.jar" + classPathSeparator + |
| 200 | + "c.jar", |
| 201 | + "-Djava.library.path=" + testLibraryPath, |
| 202 | + "LoadLibraryDeadlock"); |
| 203 | + |
| 204 | + // wait for a while to grab some output |
| 205 | + process.waitFor(5, TimeUnit.SECONDS); |
| 206 | + |
| 207 | + // dump available output |
| 208 | + String output = readAvailable(process.getInputStream()); |
| 209 | + OutputAnalyzer outputAnalyzer = new OutputAnalyzer(output); |
| 210 | + dump(outputAnalyzer); |
| 211 | + |
| 212 | + // if the process is still running, get the thread dump |
| 213 | + OutputAnalyzer outputAnalyzerJcmd = jcmd(process.pid(), "Thread.print"); |
| 214 | + dump(outputAnalyzerJcmd); |
| 215 | + |
| 216 | + Asserts.assertTrue( |
| 217 | + countLines(outputAnalyzer, "Java-level deadlock") == 0, |
| 218 | + "Found a deadlock."); |
| 219 | + |
| 220 | + // if no deadlock, make sure all components have been loaded |
| 221 | + Asserts.assertTrue( |
| 222 | + countLines(outputAnalyzer, "Class Class1 not found.") == 0, |
| 223 | + "Unable to load class. Class1 not found."); |
| 224 | + |
| 225 | + Asserts.assertTrue( |
| 226 | + countLines(outputAnalyzer, "Class Class2 not found.") == 0, |
| 227 | + "Unable to load class. Class2 not found."); |
| 228 | + |
| 229 | + Asserts.assertTrue( |
| 230 | + countLines(outputAnalyzer, "Native library loaded.") > 0, |
| 231 | + "Unable to load native library."); |
| 232 | + |
| 233 | + Asserts.assertTrue( |
| 234 | + countLines(outputAnalyzer, "Signed jar loaded.") > 0, |
| 235 | + "Unable to load signed jar."); |
| 236 | + |
| 237 | + Asserts.assertTrue( |
| 238 | + countLines(outputAnalyzer, "Signed jar loaded from native library.") > 0, |
| 239 | + "Unable to load signed jar from native library."); |
| 240 | + |
| 241 | + if (!process.waitFor(5, TimeUnit.SECONDS)) { |
| 242 | + // if the process is still frozen, fail the test even though |
| 243 | + // the "deadlock" text hasn't been found |
| 244 | + process.destroyForcibly(); |
| 245 | + Asserts.assertTrue(process.waitFor() == 0, |
| 246 | + "Process frozen."); |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments