Skip to content

Commit 218d9fe

Browse files
[SYCL] Limit the directories that are searched when loading dependencies of the plugins (#12336)
Currently the default search order is used when loading dependencies of the plugins (these dependencies include the Level Zero loader and the ICD loader for opencl and level zero plugins respectively) and that list includes current directory and some other directories which are not considered safe. This patch limits the list of directories when loading the dependencies of the plugins. See: https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa for reference. --------- Co-authored-by: aelovikov-intel <[email protected]>
1 parent 5245c75 commit 218d9fe

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

sycl/pi_win_proxy_loader/pi_win_proxy_loader.cpp

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -133,31 +133,23 @@ void preloadLibraries() {
133133

134134
MapT &dllMap = getDllMap();
135135

136-
auto ocl_path = LibSYCLDir / __SYCL_OPENCL_PLUGIN_NAME;
137-
dllMap.emplace(ocl_path,
138-
LoadLibraryEx(ocl_path.wstring().c_str(), NULL, NULL));
139-
140-
auto l0_path = LibSYCLDir / __SYCL_LEVEL_ZERO_PLUGIN_NAME;
141-
dllMap.emplace(l0_path, LoadLibraryEx(l0_path.wstring().c_str(), NULL, NULL));
142-
143-
auto cuda_path = LibSYCLDir / __SYCL_CUDA_PLUGIN_NAME;
144-
dllMap.emplace(cuda_path,
145-
LoadLibraryEx(cuda_path.wstring().c_str(), NULL, NULL));
146-
147-
auto esimd_path = LibSYCLDir / __SYCL_ESIMD_EMULATOR_PLUGIN_NAME;
148-
dllMap.emplace(esimd_path,
149-
LoadLibraryEx(esimd_path.wstring().c_str(), NULL, NULL));
150-
151-
auto hip_path = LibSYCLDir / __SYCL_HIP_PLUGIN_NAME;
152-
dllMap.emplace(hip_path,
153-
LoadLibraryEx(hip_path.wstring().c_str(), NULL, NULL));
154-
155-
auto ur_path = LibSYCLDir / __SYCL_UNIFIED_RUNTIME_PLUGIN_NAME;
156-
dllMap.emplace(ur_path, LoadLibraryEx(ur_path.wstring().c_str(), NULL, NULL));
157-
158-
auto nativecpu_path = LibSYCLDir / __SYCL_NATIVE_CPU_PLUGIN_NAME;
159-
dllMap.emplace(nativecpu_path,
160-
LoadLibraryEx(nativecpu_path.wstring().c_str(), NULL, NULL));
136+
// When searching for dependencies of the plugins limit the
137+
// list of directories to %windows%\system32 and the directory that contains
138+
// the loaded DLL (the plugin). This is necessary to avoid loading dlls from
139+
// current directory and some other directories which are considered unsafe.
140+
auto loadPlugin = [&](auto pluginName,
141+
DWORD flags = LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
142+
LOAD_LIBRARY_SEARCH_SYSTEM32) {
143+
auto path = LibSYCLDir / pluginName;
144+
dllMap.emplace(path, LoadLibraryEx(path.wstring().c_str(), NULL, flags));
145+
};
146+
loadPlugin(__SYCL_OPENCL_PLUGIN_NAME);
147+
loadPlugin(__SYCL_LEVEL_ZERO_PLUGIN_NAME);
148+
loadPlugin(__SYCL_CUDA_PLUGIN_NAME);
149+
loadPlugin(__SYCL_ESIMD_EMULATOR_PLUGIN_NAME);
150+
loadPlugin(__SYCL_HIP_PLUGIN_NAME);
151+
loadPlugin(__SYCL_UNIFIED_RUNTIME_PLUGIN_NAME);
152+
loadPlugin(__SYCL_NATIVE_CPU_PLUGIN_NAME);
161153

162154
// Restore system error handling.
163155
(void)SetErrorMode(SavedMode);

0 commit comments

Comments
 (0)