File: TestLoadLibraryDeadlock.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (188 lines) | stat: -rw-r--r-- 7,343 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * 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.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8266310
 * @summary Checks if there's no deadlock between the two lock objects -
 *          class loading lock and ClassLoader.loadedLibraryNames hashmap.
 * @library /test/lib
 * @build LoadLibraryDeadlock Class1 p.Class2
 * @run main/othervm/native -Xcheck:jni TestLoadLibraryDeadlock
 */

import jdk.test.lib.Asserts;
import jdk.test.lib.JDKToolFinder;
import jdk.test.lib.process.*;
import jdk.test.lib.util.FileUtils;

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

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

public class TestLoadLibraryDeadlock {

    private static final ToolProvider JAR = ToolProvider.findFirst("jar")
            .orElseThrow(() -> new RuntimeException("ToolProvider for jar not found"));

    private static final String KEYSTORE = "keystore.jks";
    private static final String STOREPASS = "changeit";
    private static final String KEYPASS = "changeit";
    private static final String ALIAS = "test";
    private static final String DNAME = "CN=test";
    private static final String VALIDITY = "366";

    private static String testClassPath = System.getProperty("test.classes");
    private static String testLibraryPath = System.getProperty("test.nativepath");
    private static String classPathSeparator = System.getProperty("path.separator");

    private static OutputAnalyzer runCommand(File workingDirectory, String... commands) throws Throwable {
        ProcessBuilder pb = new ProcessBuilder(commands);
        pb.directory(workingDirectory);
        System.out.println("COMMAND: " + String.join(" ", commands));
        return ProcessTools.executeProcess(pb);
    }

    private static OutputAnalyzer runCommandInTestClassPath(String... commands) throws Throwable {
        return runCommand(new File(testClassPath), commands);
    }

    private static OutputAnalyzer genKey() throws Throwable {
        FileUtils.deleteFileIfExistsWithRetry(
                Paths.get(testClassPath, KEYSTORE));
        String keytool = JDKToolFinder.getJDKTool("keytool");
        return runCommandInTestClassPath(keytool,
                "-storepass", STOREPASS,
                "-keypass", KEYPASS,
                "-keystore", KEYSTORE,
                "-keyalg", "rsa", "-keysize", "2048",
                "-genkeypair",
                "-alias", ALIAS,
                "-dname", DNAME,
                "-validity", VALIDITY
        );
    }

    private static void createJar(String outputJar, String... classes) throws Throwable {
        List<String> args = new ArrayList<>();
        Collections.addAll(args, "cvf", Paths.get(testClassPath, outputJar).toString());
        for (String c : classes) {
            Collections.addAll(args, "-C", testClassPath, c);
        }
        if (JAR.run(System.out, System.err, args.toArray(new String[0])) != 0) {
            throw new RuntimeException("jar operation failed");
        }
    }

    private static OutputAnalyzer signJar(String jarToSign) throws Throwable {
        String jarsigner = JDKToolFinder.getJDKTool("jarsigner");
        return runCommandInTestClassPath(jarsigner,
                "-keystore", KEYSTORE,
                "-storepass", STOREPASS,
                jarToSign, ALIAS
        );
    }

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

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

        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",
                "LoadLibraryDeadlock$1.class",
                "LoadLibraryDeadlock$2.class");

        createJar("b.jar",
                "Class1.class");

        createJar("c.jar",
                "p/Class2.class");

        signJar("c.jar")
                .shouldHaveExitValue(0);

        // load trigger class
        OutputAnalyzer outputAnalyzer = executeCommand(createTestJavaProcessBuilder("-cp",
                aJar.toString() + classPathSeparator +
                bJar.toString() + classPathSeparator +
                cJar.toString(),
                "-Djava.library.path=" + testLibraryPath,
                "LoadLibraryDeadlock"));
        outputAnalyzer.shouldHaveExitValue(0);

        Asserts.assertTrue(
                countLines(outputAnalyzer, "Java-level deadlock") == 0,
                "Found a deadlock.");

        // if no deadlock, make sure all components have been loaded
        Asserts.assertTrue(
                countLines(outputAnalyzer, "Class Class1 not found.") == 0,
                "Unable to load class. Class1 not found.");

        Asserts.assertTrue(
                countLines(outputAnalyzer, "Class Class2 not found.") == 0,
                "Unable to load class. Class2 not found.");

        Asserts.assertTrue(
                countLines(outputAnalyzer, "Native library loaded.") > 0,
                "Unable to load native library.");

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

    private static String toLocationString(Path path) {
        // same format as returned by LoadLibraryDeadlock::getLocation
        return path.toUri().getPath();
    }
}