Skip to content

[SYCL]Replaced LoadLibraryA() with LoadLibraryEx() #10914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 24, 2023

Conversation

eunakim0103
Copy link
Contributor

@eunakim0103 eunakim0103 commented Aug 21, 2023

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

@eunakim0103 eunakim0103 requested a review from a team as a code owner August 21, 2023 21:00
@eunakim0103 eunakim0103 requested a review from againull August 21, 2023 21:00
@eunakim0103 eunakim0103 marked this pull request as draft August 21, 2023 22:42
@eunakim0103 eunakim0103 marked this pull request as ready for review August 22, 2023 02:03
@stdale-intel stdale-intel changed the title Replaced LoadLibraryA() with LoadLibraryEx() for security reasons [SYCL]Replaced LoadLibraryA() with LoadLibraryEx() Aug 22, 2023
@@ -31,7 +31,8 @@ void *loadOsLibrary(const std::string &LibraryPath) {
if (!SetDllDirectoryA("")) {
assert(false && "Failed to update DLL search path");
}
auto Result = (void *)LoadLibraryA(LibraryPath.c_str());
// auto Result = (void *)LoadLibraryA(LibraryPath.c_str());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove this comment

@@ -151,25 +151,26 @@ void preloadLibraries() {
MapT &dllMap = getDllMap();

std::string ocl_path = LibSYCLDir + __SYCL_OPENCL_PLUGIN_NAME;
dllMap.emplace(ocl_path, LoadLibraryA(ocl_path.c_str()));
dllMap.emplace(ocl_path, LoadLibraryExA(ocl_path.c_str(), NULL, NULL));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind this change, but it seems like if flags are NULL then LoadLibraryExA is doing the same as LoadLibraryA.
See https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa

If no flags are specified, the behavior of this function is identical to that of the LoadLibrary function. This parameter can be one of the following values.

@eunakim0103
Copy link
Contributor Author

My fix passed all tests before and I removed one comment afterwards. The test fail in SYCL E2E on AWS CUDA is not relevant to my code. This PR is ready to merge @intel/llvm-gatekeepers.

@againull againull merged commit 7134423 into intel:sycl Aug 24, 2023
@dm-vodopyanov
Copy link
Contributor

@eunakim0103 post-merge nit comment: avoid using NULL in C++ code, instead use nullptr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants