Skip to content

Commit

Permalink
Merge pull request #7093 from JasonFengJ9/slnoload
Browse files Browse the repository at this point in the history
Add a flag OMRPORT_SLOPEN_NO_LOAD to test if a library was loaded
  • Loading branch information
babsingh authored Aug 21, 2023
2 parents ac8ca57 + d943d05 commit 0809f62
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
3 changes: 3 additions & 0 deletions include_core/omrport.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,9 @@ typedef struct J9ProcessorInfos {
#define OMRPORT_SL_ZOS_31BIT_TARGET_MASK 0xFFFFFFFF /* Mask used to convert 31-bit tagged handles/addresses to proper values. */
#endif /* defined(J9ZOS39064) */

/* Supported on Linux/OSX/Unix/Windows platforms. */
#define OMRPORT_SLOPEN_NO_LOAD 32

#define OMRPORT_ARCH_X86 "x86"
#define OMRPORT_ARCH_PPC "ppc" /* in line with IBM JDK 1.22 and above for AIX and Linux/PPC */
#define OMRPORT_ARCH_PPC64 "ppc64"
Expand Down
2 changes: 2 additions & 0 deletions port/common/omrport.tdf
Original file line number Diff line number Diff line change
Expand Up @@ -1624,3 +1624,5 @@ TraceException=Trc_PRT_sysinfo_get_number_CPUs_by_type_read_failed Group=sysinfo

TraceExit-Exception=Trc_PRT_mmap_map_file_unix_filestatfailed_exit Group=mmap Overhead=1 Level=1 NoEnv Template="omrmmap_map_file: Could not get stats about the file"
TraceExit-Exception=Trc_PRT_mmap_map_file_cannotallocatehandle_exit Group=mmap Overhead=1 Level=1 NoEnv Template="omrmmap_map_file: Could not allocate memory for handle"

TraceEvent=Trc_PRT_sl_open_shared_library_noload Group=sl Overhead=1 Level=3 NoEnv Template="omrsl_open_shared_library tests if a library is already loaded, returns handle=%p"
13 changes: 11 additions & 2 deletions port/unix/omrsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ omrsl_close_shared_library(struct OMRPortLibrary *portLibrary, uintptr_t descrip
uintptr_t
omrsl_open_shared_library(struct OMRPortLibrary *portLibrary, char *name, uintptr_t *descriptor, uintptr_t flags)
{
void *handle;
void *handle = NULL;
char *openName = name;
char mangledName[MAX_STRING_LENGTH + 1];
char errBuf[MAX_ERR_BUF_LENGTH];
int lazyOrNow = OMR_ARE_ALL_BITS_SET(flags, OMRPORT_SLOPEN_LAZY) ? RTLD_LAZY : RTLD_NOW;
BOOLEAN decorate = OMR_ARE_ALL_BITS_SET(flags, OMRPORT_SLOPEN_DECORATE);
BOOLEAN openExec = OMR_ARE_ALL_BITS_SET(flags, OMRPORT_SLOPEN_OPEN_EXECUTABLE);
BOOLEAN openNoLoad = OMR_ARE_ALL_BITS_SET(flags, OMRPORT_SLOPEN_NO_LOAD);
uintptr_t pathLength = 0;

Trc_PRT_sl_open_shared_library_Entry(name, flags);
Expand All @@ -159,6 +160,13 @@ omrsl_open_shared_library(struct OMRPortLibrary *portLibrary, char *name, uintpt

Trc_PRT_sl_open_shared_library_Event1(openName);

if (openNoLoad) {
/* One of RTLD_LAZY and RTLD_NOW must be included in the flag. */
handle = dlopen(openExec ? NULL : openName, RTLD_NOLOAD | lazyOrNow);
Trc_PRT_sl_open_shared_library_noload(handle);
goto exitOnSuccess;
}

/* dlopen(2) called with NULL filename opens a handle to current executable. */
handle = dlopen(openExec ? NULL : openName, lazyOrNow);

Expand Down Expand Up @@ -211,7 +219,8 @@ omrsl_open_shared_library(struct OMRPortLibrary *portLibrary, char *name, uintpt
}
}

*descriptor = (uintptr_t) handle;
exitOnSuccess:
*descriptor = (uintptr_t)handle;
Trc_PRT_sl_open_shared_library_Exit1(*descriptor);
return 0;
}
Expand Down
51 changes: 25 additions & 26 deletions port/win32/omrsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,20 @@ EsSharedLibraryLookupName(uintptr_t descriptor, char *name, uintptr_t *func)
uintptr_t
omrsl_open_shared_library(struct OMRPortLibrary *portLibrary, char *name, uintptr_t *descriptor, uintptr_t flags)
{
HINSTANCE dllHandle;
UINT prevMode;
DWORD error;
uintptr_t notFound;
HINSTANCE dllHandle = NULL;
UINT prevMode = 0;
DWORD error = 0;
uintptr_t notFound = 0;
const char *errorMessage = NULL;
char errBuf[512];
char mangledName[EsMaxPath + 1];
char *openName = name;
wchar_t portLibDir[EsMaxPath];
wchar_t unicodeBuffer[UNICODE_BUFFER_SIZE], *unicodeName;
wchar_t unicodeBuffer[UNICODE_BUFFER_SIZE];
wchar_t *unicodeName = NULL;
BOOLEAN decorate = OMR_ARE_ALL_BITS_SET(flags, OMRPORT_SLOPEN_DECORATE);
BOOLEAN openExec = OMR_ARE_ALL_BITS_SET(flags, OMRPORT_SLOPEN_OPEN_EXECUTABLE);
BOOLEAN openNoLoad = OMR_ARE_ALL_BITS_SET(flags, OMRPORT_SLOPEN_NO_LOAD);
uintptr_t pathLength = 0;

Trc_PRT_sl_open_shared_library_Entry(name, flags);
Expand Down Expand Up @@ -166,31 +168,25 @@ omrsl_open_shared_library(struct OMRPortLibrary *portLibrary, char *name, uintpt

Trc_PRT_sl_open_shared_library_Event1(openName);

if (openNoLoad) {
dllHandle = GetModuleHandleW(unicodeName);
Trc_PRT_sl_open_shared_library_noload(dllHandle);
goto exitOnSuccess;
}

/* LoadLibraryExW will try appending .DLL if necessary.
* LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR causes the operating system to search for dependencies in the same folder.
* This behaviour is similar to LOAD_WITH_ALTERED_SEARCH_PATH.
*/
dllHandle = LoadLibraryExW(unicodeName, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
if (dllHandle >= (HINSTANCE)HINSTANCE_ERROR) {
*descriptor = (uintptr_t)dllHandle;
SetErrorMode(prevMode);
if (unicodeBuffer != unicodeName) {
portLibrary->mem_free_memory(portLibrary, unicodeName);
}
Trc_PRT_sl_open_shared_library_Exit1(dllHandle);
return 0;
goto exitOnSuccess;
}

/* LoadLibrary will try appending .DLL if necessary. */
dllHandle = LoadLibraryW(unicodeName);
if (dllHandle >= (HINSTANCE)HINSTANCE_ERROR) {
*descriptor = (uintptr_t)dllHandle;
SetErrorMode(prevMode);
if (unicodeBuffer != unicodeName) {
portLibrary->mem_free_memory(portLibrary, unicodeName);
}
Trc_PRT_sl_open_shared_library_Exit1(dllHandle);
return 0;
goto exitOnSuccess;
}

/* record the error */
Expand All @@ -208,13 +204,7 @@ omrsl_open_shared_library(struct OMRPortLibrary *portLibrary, char *name, uintpt
portLibDir[EsMaxPath - 1] = L'\0';
dllHandle = LoadLibraryExW(portLibDir, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
if (dllHandle >= (HINSTANCE)HINSTANCE_ERROR) {
*descriptor = (uintptr_t)dllHandle;
SetErrorMode(prevMode);
if (unicodeBuffer != unicodeName) {
portLibrary->mem_free_memory(portLibrary, unicodeName);
}
Trc_PRT_sl_open_shared_library_Exit1(dllHandle);
return 0;
goto exitOnSuccess;
}
}
}
Expand Down Expand Up @@ -315,6 +305,15 @@ omrsl_open_shared_library(struct OMRPortLibrary *portLibrary, char *name, uintpt
SetErrorMode(prevMode);
Trc_PRT_sl_open_shared_library_Exit2(notFound ? OMRPORT_SL_NOT_FOUND : OMRPORT_SL_INVALID);
return portLibrary->error_set_last_error_with_message(portLibrary, notFound ? OMRPORT_SL_NOT_FOUND : OMRPORT_SL_INVALID, errBuf);

exitOnSuccess:
*descriptor = (uintptr_t)dllHandle;
SetErrorMode(prevMode);
if (unicodeBuffer != unicodeName) {
portLibrary->mem_free_memory(portLibrary, unicodeName);
}
Trc_PRT_sl_open_shared_library_Exit1(dllHandle);
return 0;
}

/**
Expand Down

0 comments on commit 0809f62

Please sign in to comment.