Skip to content

Commit

Permalink
8317965: TestLoadLibraryDeadlock.java fails with "Unable to load nati…
Browse files Browse the repository at this point in the history
…ve library.: expected true, was false"

8319265: TestLoadLibraryDeadlock.java fails on windows-x64 "Unable to load b.jar"

Backport-of: 5207443b360cfe3ee9c53ece55da3464c13f6a9f
  • Loading branch information
GoeLin committed Apr 3, 2024
1 parent d2c6be9 commit 620250b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, BELLSOFT. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -33,16 +33,19 @@
* triggered from JNI.
*/
import java.lang.*;
import java.net.URISyntaxException;

public class LoadLibraryDeadlock {

public static void main(String[] args) {
System.out.println("LoadLibraryDeadlock test started");
Thread t1 = new Thread() {
public void run() {
try {
// an instance of unsigned class that loads a native library
Class<?> c1 = Class.forName("Class1");
Object o = c1.newInstance();
System.out.println("Class1 loaded from " + getLocation(c1));
} catch (ClassNotFoundException |
InstantiationException |
IllegalAccessException e) {
Expand All @@ -56,7 +59,7 @@ public void run() {
try {
// load a class from a signed jar, which locks the JarFile
Class<?> c2 = Class.forName("p.Class2");
System.out.println("Signed jar loaded.");
System.out.println("Class2 loaded from " + getLocation(c2));
} catch (ClassNotFoundException e) {
System.out.println("Class Class2 not found.");
throw new RuntimeException(e);
Expand All @@ -68,7 +71,19 @@ public void run() {
try {
t1.join();
t2.join();
} catch (InterruptedException ignore) {
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}

private static String getLocation(Class<?> c) {
var pd = c.getProtectionDomain();
var cs = pd != null ? pd.getCodeSource() : null;
try {
// same format as returned by TestLoadLibraryDeadlock::getLocation
return cs != null ? cs.getLocation().toURI().getPath() : null;
} catch (URISyntaxException ex) {
throw new RuntimeException(ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, BELLSOFT. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -38,13 +38,14 @@
import jdk.test.lib.util.FileUtils;

import java.lang.ProcessBuilder;
import java.lang.Process;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.spi.ToolProvider;

import static jdk.test.lib.process.ProcessTools.*;

public class TestLoadLibraryDeadlock {

private static final ToolProvider JAR = ToolProvider.findFirst("jar")
Expand Down Expand Up @@ -108,76 +109,24 @@ private static OutputAnalyzer signJar(String jarToSign) throws Throwable {
);
}

private static Process runJavaCommand(String... command) throws Throwable {
String java = JDKToolFinder.getJDKTool("java");
List<String> commands = new ArrayList<>();
Collections.addAll(commands, java);
Collections.addAll(commands, command);
System.out.println("COMMAND: " + String.join(" ", commands));
return new ProcessBuilder(commands.toArray(new String[0]))
.redirectErrorStream(true)
.directory(new File(testClassPath))
.start();
}

private static OutputAnalyzer jcmd(long pid, String command) throws Throwable {
String jcmd = JDKToolFinder.getJDKTool("jcmd");
return runCommandInTestClassPath(jcmd,
String.valueOf(pid),
command
);
}

private static String readAvailable(final InputStream is) throws Throwable {
final List<String> list = Collections.synchronizedList(new ArrayList<String>());
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<String> future = executor.submit(new Callable<String>() {
public String call() {
String result = new String();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while(true) {
String s = reader.readLine();
if (s.length() > 0) {
list.add(s);
result += s + "\n";
}
}
} catch (IOException ignore) {}
return result;
}
});
try {
return future.get(1000, TimeUnit.MILLISECONDS);
} catch (Exception ignoreAll) {
future.cancel(true);
return String.join("\n", list);
}
}

private final static long countLines(OutputAnalyzer output, String string) {
return output.asLines()
.stream()
.filter(s -> s.contains(string))
.count();
}

private final static void dump(OutputAnalyzer output) {
output.asLines()
.stream()
.forEach(s -> System.out.println(s));
}

public static void main(String[] args) throws Throwable {
genKey()
.shouldHaveExitValue(0);

FileUtils.deleteFileIfExistsWithRetry(
Paths.get(testClassPath, "a.jar"));
FileUtils.deleteFileIfExistsWithRetry(
Paths.get(testClassPath, "b.jar"));
FileUtils.deleteFileIfExistsWithRetry(
Paths.get(testClassPath, "c.jar"));
Path aJar = Path.of(testClassPath, "a.jar");
Path bJar = Path.of(testClassPath, "b.jar");
Path cJar = Path.of(testClassPath, "c.jar");

FileUtils.deleteFileIfExistsWithRetry(aJar);
FileUtils.deleteFileIfExistsWithRetry(bJar);
FileUtils.deleteFileIfExistsWithRetry(cJar);

createJar("a.jar",
"LoadLibraryDeadlock.class",
Expand All @@ -194,24 +143,13 @@ public static void main(String[] args) throws Throwable {
.shouldHaveExitValue(0);

// load trigger class
Process process = runJavaCommand("-cp",
"a.jar" + classPathSeparator +
"b.jar" + classPathSeparator +
"c.jar",
OutputAnalyzer outputAnalyzer = executeCommand(createTestJavaProcessBuilder("-cp",
aJar.toString() + classPathSeparator +
bJar.toString() + classPathSeparator +
cJar.toString(),
"-Djava.library.path=" + testLibraryPath,
"LoadLibraryDeadlock");

// wait for a while to grab some output
process.waitFor(5, TimeUnit.SECONDS);

// dump available output
String output = readAvailable(process.getInputStream());
OutputAnalyzer outputAnalyzer = new OutputAnalyzer(output);
dump(outputAnalyzer);

// if the process is still running, get the thread dump
OutputAnalyzer outputAnalyzerJcmd = jcmd(process.pid(), "Thread.print");
dump(outputAnalyzerJcmd);
"LoadLibraryDeadlock"));
outputAnalyzer.shouldHaveExitValue(0);

Asserts.assertTrue(
countLines(outputAnalyzer, "Java-level deadlock") == 0,
Expand All @@ -231,19 +169,20 @@ public static void main(String[] args) throws Throwable {
"Unable to load native library.");

Asserts.assertTrue(
countLines(outputAnalyzer, "Signed jar loaded.") > 0,
"Unable to load signed jar.");
countLines(outputAnalyzer, "Class1 loaded from " + toLocationString(bJar)) > 0,
"Unable to load " + toLocationString(bJar));

Asserts.assertTrue(
countLines(outputAnalyzer, "Class2 loaded from " + toLocationString(cJar)) > 0,
"Unable to load signed " + toLocationString(cJar));

Asserts.assertTrue(
countLines(outputAnalyzer, "Signed jar loaded from native library.") > 0,
"Unable to load signed jar from native library.");
}

if (!process.waitFor(5, TimeUnit.SECONDS)) {
// if the process is still frozen, fail the test even though
// the "deadlock" text hasn't been found
process.destroyForcibly();
Asserts.assertTrue(process.waitFor() == 0,
"Process frozen.");
}
private static String toLocationString(Path path) {
// same format as returned by LoadLibraryDeadlock::getLocation
return path.toUri().getPath();
}
}

1 comment on commit 620250b

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.