Skip to content

Commit 7134423

Browse files
authored
[SYCL]Replaced LoadLibraryA() with LoadLibraryEx() (#10914)
Replaced LoadLibraryA() with LoadLibraryEx() and added SetDllDirectoryA() to remove the working directory from the standard search path. LoadLibraryExA() was used because "path" is of type c-string. - LoadLibraryExA() <-- for ANSI (ASCII) - LoadLibraryExW() <-- for Unicode The libloaderapi.h header defines LoadLibraryEx as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw HMODULE LoadLibraryEx( [in] LPCSTR lpLibFileName, //==> library path as a string HANDLE hFile, //==> reserved as NULL for future use [in] DWORD dwFlags //==> dwFlags determine the behavior of LoadLibaryEx(). When it is NULL, it behaves same as LoadLibaryA() ); https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa --------- Signed-off-by: Kim, Euna <[email protected]>
1 parent fbad42a commit 7134423

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

sycl/pi_win_proxy_loader/pi_win_proxy_loader.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,26 @@ void preloadLibraries() {
151151
MapT &dllMap = getDllMap();
152152

153153
std::string ocl_path = LibSYCLDir + __SYCL_OPENCL_PLUGIN_NAME;
154-
dllMap.emplace(ocl_path, LoadLibraryA(ocl_path.c_str()));
154+
dllMap.emplace(ocl_path, LoadLibraryExA(ocl_path.c_str(), NULL, NULL));
155155

156156
std::string l0_path = LibSYCLDir + __SYCL_LEVEL_ZERO_PLUGIN_NAME;
157-
dllMap.emplace(l0_path, LoadLibraryA(l0_path.c_str()));
157+
dllMap.emplace(l0_path, LoadLibraryExA(l0_path.c_str(), NULL, NULL));
158158

159159
std::string cuda_path = LibSYCLDir + __SYCL_CUDA_PLUGIN_NAME;
160-
dllMap.emplace(cuda_path, LoadLibraryA(cuda_path.c_str()));
160+
dllMap.emplace(cuda_path, LoadLibraryExA(cuda_path.c_str(), NULL, NULL));
161161

162162
std::string esimd_path = LibSYCLDir + __SYCL_ESIMD_EMULATOR_PLUGIN_NAME;
163-
dllMap.emplace(esimd_path, LoadLibraryA(esimd_path.c_str()));
163+
dllMap.emplace(esimd_path, LoadLibraryExA(esimd_path.c_str(), NULL, NULL));
164164

165165
std::string hip_path = LibSYCLDir + __SYCL_HIP_PLUGIN_NAME;
166-
dllMap.emplace(hip_path, LoadLibraryA(hip_path.c_str()));
166+
dllMap.emplace(hip_path, LoadLibraryExA(hip_path.c_str(), NULL, NULL));
167167

168168
std::string ur_path = LibSYCLDir + __SYCL_UNIFIED_RUNTIME_PLUGIN_NAME;
169-
dllMap.emplace(ur_path, LoadLibraryA(ur_path.c_str()));
169+
dllMap.emplace(ur_path, LoadLibraryExA(ur_path.c_str(), NULL, NULL));
170170

171171
std::string nativecpu_path = LibSYCLDir + __SYCL_NATIVE_CPU_PLUGIN_NAME;
172-
dllMap.emplace(nativecpu_path, LoadLibraryA(nativecpu_path.c_str()));
172+
dllMap.emplace(nativecpu_path,
173+
LoadLibraryExA(nativecpu_path.c_str(), NULL, NULL));
173174

174175
// Restore system error handling.
175176
(void)SetErrorMode(SavedMode);

sycl/source/detail/windows_pi.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ void *loadOsLibrary(const std::string &LibraryPath) {
3131
if (!SetDllDirectoryA("")) {
3232
assert(false && "Failed to update DLL search path");
3333
}
34-
auto Result = (void *)LoadLibraryA(LibraryPath.c_str());
34+
35+
auto Result = (void *)LoadLibraryExA(LibraryPath.c_str(), NULL, NULL);
3536
(void)SetErrorMode(SavedMode);
3637
if (!SetDllDirectoryA(nullptr)) {
3738
assert(false && "Failed to restore DLL search path");

xpti/include/xpti/xpti_trace_framework.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,19 @@ class PlatformHelper {
228228
xpti_plugin_handle_t loadLibrary(const char *path, std::string &error) {
229229
xpti_plugin_handle_t handle = 0;
230230
#if defined(_WIN32) || defined(_WIN64)
231-
handle = LoadLibraryA(path);
231+
UINT SavedMode = SetErrorMode(SEM_FAILCRITICALERRORS);
232+
// Exclude current directory from DLL search path
233+
if (!SetDllDirectoryA("")) {
234+
assert(false && "Failed to update DLL search path");
235+
}
236+
handle = LoadLibraryExA(path, NULL, NULL);
232237
if (!handle) {
233238
error = getLastError();
234239
}
240+
(void)SetErrorMode(SavedMode);
241+
if (!SetDllDirectoryA(nullptr)) {
242+
assert(false && "Failed to restore DLL search path");
243+
}
235244
#else
236245
handle = dlopen(path, RTLD_LAZY);
237246
if (!handle) {

0 commit comments

Comments
 (0)