Skip to content

Commit 464125c

Browse files
authored
Merge pull request #19921 from JasonFengJ9/aixlib_v0.47
(0.47.0) JDK17+ JVM_LoadLibrary() opens shared library via J9PORT_SLOPEN_DECORATE
2 parents 8d8f4f7 + 54e8b25 commit 464125c

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed

runtime/j9vm/j9scar.tdf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,7 @@ TraceExit=Trc_SC_VirtualThreadStart_Exit Overhead=1 Level=3 Template="thread = %
387387

388388
TraceEntry=Trc_SC_VirtualThreadEnd_Entry Overhead=1 Level=3 Template="thread = %p"
389389
TraceExit=Trc_SC_VirtualThreadEnd_Exit Overhead=1 Level=3 Template="thread = %p"
390+
391+
TraceEvent=Trc_SC_libName_no_prefix NoEnv Overhead=1 Level=3 Test Template="Skip JDK17+ libName(%s) without a lib prefix"
392+
TraceEvent=Trc_SC_libName_no_extension NoEnv Overhead=1 Level=3 Test Template="Skip JDK17+ libName(%s) without a file extension"
393+
TraceEvent=Trc_SC_allocate_memory_failed NoEnv Overhead=1 Level=2 Test Template="j9mem_allocate_memory(%zu) for libNameNotDecorated failed"

runtime/j9vm/jvm.c

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3977,6 +3977,7 @@ JVM_LoadLibrary(const char *libName, jboolean throwOnFailure)
39773977
#endif /* defined(WIN32) */
39783978
Trc_SC_LoadLibrary_Entry(libName);
39793979
{
3980+
UDATA slOpenResult = 0;
39803981
UDATA handle = 0;
39813982
UDATA flags = J9_ARE_ANY_BITS_SET(javaVM->extendedRuntimeFlags, J9_EXTENDED_RUNTIME_LAZY_SYMBOL_RESOLUTION) ? J9PORT_SLOPEN_LAZY : 0;
39823983

@@ -3986,18 +3987,82 @@ JVM_LoadLibrary(const char *libName, jboolean throwOnFailure)
39863987
}
39873988
#endif /* defined(J9VM_ZOS_3164_INTEROPERABILITY) */
39883989

3989-
UDATA slOpenResult = j9sl_open_shared_library((char *)libName, &handle, flags);
3990+
slOpenResult = j9sl_open_shared_library((char *)libName, &handle, flags);
39903991
Trc_SC_LoadLibrary_OpenShared(libName);
39913992

3992-
/* jdk17+ calls JVM_LoadLibrary with decorated library names. If the following is done
3993-
* then it overwrites the real error with a failure to load "liblib<name>.so.so".
3994-
*/
3995-
#if JAVA_SPEC_VERSION < 17
39963993
if (0 != slOpenResult) {
3997-
slOpenResult = j9sl_open_shared_library((char *)libName, &handle, flags | J9PORT_SLOPEN_DECORATE);
3998-
Trc_SC_LoadLibrary_OpenShared_Decorate(libName);
3994+
char *libNameNotDecorated = (char *)libName;
3995+
#if JAVA_SPEC_VERSION >= 17
3996+
/* JDK17+ jdk.internal.loader.NativeLibraries.load() calls JVM_LoadLibrary()
3997+
* with a decorated library name, i.e., a path to the library name returned
3998+
* from Java_java_lang_System_mapLibraryName(nameNoPrefixNoExtension).
3999+
* The library name passed to j9sl_open_shared_library() with the flag
4000+
* J9PORT_SLOPEN_DECORATE must be platform independent, i.e., it must not
4001+
* contain any prefix or file extension.
4002+
*/
4003+
const char *fileExt = strrchr(libName, '.');
4004+
BOOLEAN doOpenLibrary = TRUE;
4005+
char libPath[EsMaxPath];
4006+
libPath[0] = '\0';
4007+
if (NULL == fileExt) {
4008+
/* A decorated library name is expected with a file extension,
4009+
* pass to j9sl_open_shared_library w/o modification.
4010+
*/
4011+
Trc_SC_libName_no_extension(libNameNotDecorated);
4012+
} else {
4013+
const char *fileNameTmp = strrchr(libName, DIR_SEPARATOR);
4014+
const char *fileName = (NULL == fileNameTmp) ? libName : (fileNameTmp + 1);
4015+
#if defined(J9OS_I5) || defined(WIN32)
4016+
/* no library prefix for J9OS_I5 and WIN32 */
4017+
const size_t libStrLength = 0;
4018+
#else /* defined(J9OS_I5) || defined(WIN32) */
4019+
/* strlen("lib") = 3 */
4020+
const size_t libStrLength = 3;
4021+
if (0 != strncmp("lib", fileName, libStrLength)) {
4022+
/* A decorated library name is expected to start with lib prefix for
4023+
* platforms other than WIN32 & J9OS_I5.
4024+
* Pass to j9sl_open_shared_library w/o modification.
4025+
*/
4026+
Trc_SC_libName_no_prefix(fileName);
4027+
} else
4028+
#endif /* defined(J9OS_I5) || defined(WIN32) */
4029+
{
4030+
const char *fileNameNoPrefix = fileName + libStrLength;
4031+
uintptr_t libDirLength = (uintptr_t)fileName - (uintptr_t)libName;
4032+
uintptr_t fileNameNotDecoratedLength = (uintptr_t)fileExt - (uintptr_t)fileNameNoPrefix;
4033+
size_t libPathLength = libDirLength + fileNameNotDecoratedLength + 1;
4034+
if (libPathLength <= EsMaxPath) {
4035+
libNameNotDecorated = libPath;
4036+
} else {
4037+
libNameNotDecorated = (char *)j9mem_allocate_memory(libPathLength, OMRMEM_CATEGORY_VM);
4038+
}
4039+
if (NULL == libNameNotDecorated) {
4040+
doOpenLibrary = FALSE;
4041+
Trc_SC_allocate_memory_failed(libPathLength);
4042+
} else {
4043+
j9str_printf(PORTLIB,
4044+
libNameNotDecorated,
4045+
libPathLength,
4046+
"%.*s%.*s",
4047+
libDirLength,
4048+
libName,
4049+
fileNameNotDecoratedLength,
4050+
fileNameNoPrefix);
4051+
}
4052+
}
4053+
}
4054+
if (doOpenLibrary)
4055+
#endif /* JAVA_SPEC_VERSION >= 17 */
4056+
{
4057+
slOpenResult = j9sl_open_shared_library(libNameNotDecorated, &handle, flags | J9PORT_SLOPEN_DECORATE);
4058+
Trc_SC_LoadLibrary_OpenShared_Decorate(libNameNotDecorated);
4059+
#if JAVA_SPEC_VERSION >= 17
4060+
if ((libName != libNameNotDecorated) && (libPath != libNameNotDecorated)) {
4061+
j9mem_free_memory(libNameNotDecorated);
4062+
}
4063+
#endif /* JAVA_SPEC_VERSION >= 17 */
4064+
}
39994065
}
4000-
#endif /* JAVA_SPEC_VERSION < 17 */
40014066
if (0 == slOpenResult) {
40024067
result = (void *)handle;
40034068
}

0 commit comments

Comments
 (0)