Inbox WinML tests fail because Inbox loads binaries from system32 (#4660)

* make dml and onnxruntime system32 only when winml and onnxruntime is loaded from system32

* use __ImageBase as that will not incur the unsupport store api call into GetModuleHandleEx

* remove accidental comment

Co-authored-by: Sheil Kumar <sheilk@microsoft.com>
This commit is contained in:
Sheil Kumar 2020-07-30 00:09:18 -07:00 committed by GitHub
parent 382f94c95c
commit 0a8bfb10fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View file

@ -19,10 +19,27 @@
namespace winmla = Windows::AI::MachineLearning::Adapter;
#ifdef USE_DML
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
static bool IsCurrentModuleInSystem32() {
std::string current_module_path;
current_module_path.reserve(MAX_PATH);
auto size_module_path = GetModuleFileNameA((HINSTANCE)&__ImageBase, current_module_path.data(), MAX_PATH);
FAIL_FAST_IF(size_module_path == 0);
std::string system32_path;
system32_path.reserve(MAX_PATH);
auto size_system32_path = GetSystemDirectoryA(system32_path.data(), MAX_PATH);
FAIL_FAST_IF(size_system32_path == 0);
return _strnicmp(system32_path.c_str(), current_module_path.c_str(), size_system32_path) == 0;
}
Microsoft::WRL::ComPtr<IDMLDevice> CreateDmlDevice(ID3D12Device* d3d12Device) {
DWORD flags = 0;
#ifdef BUILD_INBOX
flags = LOAD_LIBRARY_SEARCH_SYSTEM32;
flags |= IsCurrentModuleInSystem32() ? LOAD_LIBRARY_SEARCH_SYSTEM32 : 0;
#endif
// Dynamically load DML to avoid WinML taking a static dependency on DirectML.dll

View file

@ -13,10 +13,26 @@ using namespace _winml;
static bool debug_output_ = false;
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
static bool IsCurrentModuleInSystem32() {
std::string current_module_path;
current_module_path.reserve(MAX_PATH);
auto size_module_path = GetModuleFileNameA((HINSTANCE)&__ImageBase, current_module_path.data(), MAX_PATH);
FAIL_FAST_IF(size_module_path == 0);
std::string system32_path;
system32_path.reserve(MAX_PATH);
auto size_system32_path = GetSystemDirectoryA(system32_path.data(), MAX_PATH);
FAIL_FAST_IF(size_system32_path == 0);
return _strnicmp(system32_path.c_str(), current_module_path.c_str(), size_system32_path) == 0;
}
static HRESULT GetOnnxruntimeLibrary(HMODULE& module) {
DWORD flags = 0;
#ifdef BUILD_INBOX
flags = LOAD_LIBRARY_SEARCH_SYSTEM32;
flags |= IsCurrentModuleInSystem32() ? LOAD_LIBRARY_SEARCH_SYSTEM32 : 0;
#endif
auto out_module = LoadLibraryExA("onnxruntime.dll", nullptr, flags);