DPC++ Runtime
Runtime libraries for oneAPI DPC++
windows_ur.cpp
Go to the documentation of this file.
1 //==---------------- windows_ur.cpp ----------------------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <sycl/backend.hpp>
10 #include <sycl/detail/defines.hpp>
11 
12 #include <cassert>
13 #include <string>
14 #include <windows.h>
15 #include <winreg.h>
16 
18 #include "ur_win_proxy_loader.hpp"
19 
20 namespace sycl {
21 inline namespace _V1 {
22 namespace detail {
23 
24 void *GetWinProcAddress(void *module, const char *funcName) {
25  return (void *)GetProcAddress((HMODULE)module, funcName);
26 }
27 
28 namespace ur {
29 
30 void *loadOsLibrary(const std::string &LibraryPath) {
31  // Tells the system to not display the critical-error-handler message box.
32  // Instead, the system sends the error to the calling process.
33  // This is crucial for graceful handling of shared libs that can't be
34  // loaded, e.g. due to missing native run-times.
35 
36  UINT SavedMode = SetErrorMode(SEM_FAILCRITICALERRORS);
37  // Exclude current directory from DLL search path
38  if (!SetDllDirectoryA("")) {
39  assert(false && "Failed to update DLL search path");
40  }
41 
42  auto Result = (void *)LoadLibraryExA(LibraryPath.c_str(), NULL, NULL);
43  (void)SetErrorMode(SavedMode);
44  if (!SetDllDirectoryA(nullptr)) {
45  assert(false && "Failed to restore DLL search path");
46  }
47 
48  return Result;
49 }
50 
51 int unloadOsLibrary(void *Library) {
52  return (int)FreeLibrary((HMODULE)Library);
53 }
54 
55 void *getOsLibraryFuncAddress(void *Library, const std::string &FunctionName) {
56  return reinterpret_cast<void *>(
57  GetProcAddress((HMODULE)Library, FunctionName.c_str()));
58 }
59 
60 static std::filesystem::path getCurrentDSODirPath() {
61  wchar_t Path[MAX_PATH];
62  auto Handle =
63  getOSModuleHandle(reinterpret_cast<void *>(&getCurrentDSODirPath));
64  DWORD Ret = GetModuleFileName(
65  reinterpret_cast<HMODULE>(ExeModuleHandle == Handle ? 0 : Handle), Path,
66  MAX_PATH);
67  assert(Ret < MAX_PATH && "Path is longer than MAX_PATH?");
68  assert(Ret > 0 && "GetModuleFileName failed");
69  (void)Ret;
70 
71  BOOL RetCode = PathRemoveFileSpec(Path);
72  assert(RetCode && "PathRemoveFileSpec failed");
73  (void)RetCode;
74 
75  return std::filesystem::path(Path);
76 }
77 
78 void *getURLoaderLibrary() { return getPreloadedURLib(); }
79 
80 } // namespace ur
81 } // namespace detail
82 } // namespace _V1
83 } // namespace sycl
void * getOsLibraryFuncAddress(void *Library, const std::string &FunctionName)
Definition: posix_ur.cpp:34
void * getURLoaderLibrary()
Definition: posix_ur.cpp:38
static std::filesystem::path getCurrentDSODirPath()
Definition: windows_ur.cpp:60
void * loadOsLibrary(const std::string &Library)
Definition: posix_ur.cpp:20
int unloadOsLibrary(void *Library)
Definition: posix_ur.cpp:32
void * GetWinProcAddress(void *module, const char *funcName)
Definition: windows_ur.cpp:24
Definition: access.hpp:18
OSModuleHandle getOSModuleHandle(const void *VirtAddr)
constexpr OSModuleHandle ExeModuleHandle