-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Windows: Show more useful DLL load errors to say exactly what DLL is missing #24053
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include <windows.h> | ||
| #include <dbghelp.h> | ||
| #pragma comment(lib, "dbghelp.lib") | ||
| #include <iostream> | ||
| #include <memory> | ||
| #include <string> | ||
| #include "dll_load_error.h" | ||
|
Check warning on line 10 in onnxruntime/core/platform/windows/dll_load_error.cc
|
||
|
|
||
| struct HMODULE_Deleter { | ||
| typedef HMODULE pointer; | ||
| void operator()(HMODULE h) { FreeLibrary(h); } | ||
| }; | ||
|
|
||
| using ModulePtr = std::unique_ptr<HMODULE, HMODULE_Deleter>; | ||
|
|
||
| // If a DLL fails to load, this will try loading the DLL and then its dependencies recursively | ||
| // until it finds a missing file, then will report which file is missing and what the dependency | ||
| // chain is. | ||
| std::wstring DetermineLoadLibraryError(const wchar_t* filename_in, DWORD flags) { | ||
| std::wstring error(L"Error loading"); | ||
|
|
||
| std::wstring filename{filename_in}; | ||
| while (filename.size()) { | ||
| error += std::wstring(L" \"") + filename + L"\""; | ||
|
|
||
| // We use DONT_RESOLVE_DLL_REFERENCES instead of LOAD_LIBRARY_AS_DATAFILE because the latter will not process the import table | ||
| // and will result in the IMAGE_IMPORT_DESCRIPTOR table names being uninitialized. | ||
| ModulePtr hModule = ModulePtr{LoadLibraryExW(filename.c_str(), NULL, flags | DONT_RESOLVE_DLL_REFERENCES)}; | ||
| if (!hModule) { | ||
| error += L" which is missing."; | ||
| return error; | ||
| } | ||
|
|
||
| // Get the address of the Import Directory | ||
| ULONG size{}; | ||
| IMAGE_IMPORT_DESCRIPTOR* import_desc = reinterpret_cast<IMAGE_IMPORT_DESCRIPTOR*>(ImageDirectoryEntryToData(hModule.get(), TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size)); | ||
| if (!import_desc) { | ||
| error += L" No import directory found."; // This is unexpected, and I'm not sure how it could happen but we handle it just in case. | ||
| return error; | ||
| } | ||
|
|
||
| // Iterate through the import descriptors to see which dependent DLL can't load | ||
| filename.clear(); | ||
| flags = 0; // Dependent libraries are relative, and flags like LOAD_WITH_ALTERED_SEARCH_PATH is undefined for those. | ||
| for (; import_desc->Characteristics; import_desc++) { | ||
| const char* dll_name = reinterpret_cast<const char*>(reinterpret_cast<const BYTE*>(hModule.get()) + import_desc->Name); | ||
| // Try to load the dependent DLL, and if it fails, we loop again with this as the DLL and we'll be one step closer to the missing file. | ||
| ModulePtr hDepModule{LoadLibrary(dll_name)}; | ||
| if (!hDepModule) { | ||
| filename = std::wstring(dll_name, dll_name + strlen(dll_name)); | ||
| error += L" which depends on"; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| error += L" But no dependency issue could be determined."; | ||
| return error; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| std::wstring DetermineLoadLibraryError(const wchar_t* filename, DWORD flags); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.