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"

Reviewed-by: rriggs
  • Loading branch information
Mandy Chung committed Nov 1, 2023
1 parent ee57e73 commit 5207443
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 92 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 @@ -37,12 +37,14 @@
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 +58,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 +70,14 @@ 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;
return cs != null ? cs.getLocation().getPath() : null;
}
}
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,15 @@ 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 " + bJar) > 0,
"Unable to load b.jar.");

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

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.");
}
}
}

3 comments on commit 5207443

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 5207443 Mar 28, 2024

Choose a reason for hiding this comment

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

/backport jdk21u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 5207443 Mar 28, 2024

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch backport-GoeLin-5207443b in my personal fork of openjdk/jdk21u-dev. To create a pull request with this backport targeting openjdk/jdk21u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 5207443b from the openjdk/jdk repository.

The commit being backported was authored by Mandy Chung on 1 Nov 2023 and was reviewed by Roger Riggs.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u-dev:

$ git fetch https://github.com/openjdk-bots/jdk21u-dev.git backport-GoeLin-5207443b:backport-GoeLin-5207443b
$ git checkout backport-GoeLin-5207443b
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u-dev.git backport-GoeLin-5207443b

Please sign in to comment.