Skip to content

Commit 4895a15

Browse files
Suchismith RoyTheRealMDoerr
authored andcommitted
8319516: AIX System::loadLibrary needs support to load a shared library from an archive object
Reviewed-by: mdoerr, mchung
1 parent fd331ff commit 4895a15

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2024, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
27+
package jdk.internal.loader;
28+
29+
import java.io.File;
30+
import java.util.ArrayList;
31+
32+
class ClassLoaderHelper {
33+
34+
private ClassLoaderHelper() {}
35+
36+
/**
37+
* Shared objects may be loaded from .a archive object on AIX and .so may not exist.
38+
* This method returns false so that loading of shared library continues if
39+
* libname.so is not present.
40+
*/
41+
static boolean loadLibraryOnlyIfPresent() {
42+
return false;
43+
}
44+
45+
/**
46+
* AIX implementation of JVM_LoadLibrary handles the alternate path name mapping.
47+
* If loading of the given library name with ".so" suffix fails, it will attempt
48+
* to load the library of the same name with ".a" suffix as the alternate name.
49+
* This method simply returns null. It could implement the alternate name
50+
* converting ".so" with ".a" suffix but redundant.
51+
*/
52+
static File mapAlternativeName(File lib) {
53+
return null;
54+
}
55+
56+
/**
57+
* Parse a PATH env variable.
58+
*
59+
* Empty elements will be replaced by dot.
60+
*/
61+
static String[] parsePath(String ldPath) {
62+
char ps = File.pathSeparatorChar;
63+
ArrayList<String> paths = new ArrayList<>();
64+
int pathStart = 0;
65+
int pathEnd;
66+
while ((pathEnd = ldPath.indexOf(ps, pathStart)) >= 0) {
67+
paths.add((pathStart < pathEnd) ?
68+
ldPath.substring(pathStart, pathEnd) : ".");
69+
pathStart = pathEnd + 1;
70+
}
71+
int ldLen = ldPath.length();
72+
paths.add((pathStart < ldLen) ?
73+
ldPath.substring(pathStart, ldLen) : ".");
74+
return paths.toArray(new String[paths.size()]);
75+
}
76+
}
77+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2024, IBM Corporation. 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+
import java.nio.file.Files;
25+
import java.nio.file.Path;
26+
import jdk.test.lib.process.ProcessTools;
27+
28+
/**
29+
* @test
30+
* @bug 8319516
31+
* @summary verify that System.loadLibrary on AIX is able to load libraries from ".a" (archive) file
32+
* @requires os.family == "aix"
33+
* @library /test/lib/
34+
* @build jdk.test.lib.process.ProcessTools
35+
* @run main/othervm LoadAIXLibraryFromArchiveObject
36+
*/
37+
public class LoadAIXLibraryFromArchiveObject {
38+
39+
private static final String TEST_LIBRARY_NAME = "foobar";
40+
// creates a ".a" archive file in a test specific directory and then
41+
// launches a java application passing this directory through "-Djava.library.path".
42+
// the java application then attempts to load the library using System.loadLibrary()
43+
public static void main(String[] args) throws Exception {
44+
String javaHome = System.getProperty("java.home");
45+
Path libj2pcscSo = Path.of(javaHome).resolve("lib", "libj2pcsc.so");
46+
if (!Files.exists(libj2pcscSo)) {
47+
throw new AssertionError(libj2pcscSo + " is missing");
48+
}
49+
String archiveFileName = "lib" + TEST_LIBRARY_NAME + ".a";
50+
// copy over libj2pcsc.so as an archive file to test specific scratch dir
51+
Path testNativeLibDir = Path.of("native").toAbsolutePath();
52+
Files.createDirectories(testNativeLibDir);
53+
Path libFooBarArchive = testNativeLibDir.resolve(archiveFileName);
54+
Files.copy(libj2pcscSo, libFooBarArchive);
55+
// launch a java application which calls System.loadLibrary and is passed
56+
// the directory containing the native library archive file, through
57+
// -Djava.library.path
58+
ProcessBuilder processBuilder = ProcessTools.createTestJavaProcessBuilder(
59+
"-Djava.library.path=" + testNativeLibDir,
60+
LoadAIXLibraryFromArchiveObject.LoadLibraryApp.class.getName());
61+
ProcessTools.executeCommand(processBuilder).shouldHaveExitValue(0);
62+
}
63+
64+
static class LoadLibraryApp {
65+
public static void main(final String[] args) throws Exception {
66+
System.out.println("attempting to load library " + TEST_LIBRARY_NAME);
67+
System.loadLibrary(TEST_LIBRARY_NAME);
68+
System.out.println(TEST_LIBRARY_NAME + " successfully loaded");
69+
}
70+
}
71+
}
72+

0 commit comments

Comments
 (0)