Skip to content

Commit 72f3ce1

Browse files
committed
Fix invalid use of dlopen()
When attempting to get a handle for the `libOpenCL.so` library in order to call `dlsym()` invalid arguments were set so `dlopen()` always returned null. This just happened to work due to the value of `RTLD_DEFAULT` being 0 so the `dlsym()` calls succeeded. This patch removes the invalid `dlopen()` usage and explicitly uses `RTLD_DEFAULT` instead of the null `handle`.
1 parent 871061f commit 72f3ce1

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

source/adapters/opencl/adapter.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
ur_adapter_handle_t_::ur_adapter_handle_t_() {
2222
#ifdef _MSC_VER
23+
2324
// Loading OpenCL.dll increments the libraries internal reference count.
2425
auto handle = LoadLibraryA("OpenCL.dll");
2526

@@ -30,17 +31,17 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
3031

3132
// So we can safely decrement it here wihtout actually unloading OpenCL.dll.
3233
FreeLibrary(handle);
33-
#else
34-
// Loading libOpenCL.so to get the library handle but don't dlclose it as
35-
// this causes a segfault when attempting to call any OpenCL entry point.
36-
auto handle = dlopen("libOpenCL.so", RTLD_LOCAL);
3734

35+
#else // _MSC_VER
36+
37+
// Use the default shared object search order (RTLD_DEFAULT) since the
38+
// OpenCL-ICD-Loader has already been loaded into the process.
3839
#define CL_CORE_FUNCTION(FUNC) \
39-
FUNC = reinterpret_cast<decltype(::FUNC) *>(dlsym(handle, #FUNC));
40+
FUNC = reinterpret_cast<decltype(::FUNC) *>(dlsym(RTLD_DEFAULT, #FUNC));
4041
#include "core_functions.def"
4142
#undef CL_CORE_FUNCTION
4243

43-
#endif
44+
#endif // _MSC_VER
4445
}
4546

4647
static ur_adapter_handle_t adapter = nullptr;

0 commit comments

Comments
 (0)