Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/native_library.h" |
| 6 | |
| 7 | #include <windows.h> |
| 8 | |
Helmut Januschka | 1dce9dc | 2024-06-11 13:05:35 | [diff] [blame] | 9 | #include <string_view> |
| 10 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 11 | #include "base/files/file_util.h" |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 12 | #include "base/metrics/histogram_macros.h" |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 13 | #include "base/path_service.h" |
| 14 | #include "base/scoped_native_library.h" |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 15 | #include "base/strings/strcat.h" |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
[email protected] | f4e91145 | 2014-03-20 06:07:26 | [diff] [blame] | 17 | #include "base/strings/stringprintf.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 19 | #include "base/threading/scoped_blocking_call.h" |
Anthony Vallee-Dubois | 3aafcf2 | 2022-02-10 16:52:04 | [diff] [blame] | 20 | #include "base/threading/scoped_thread_priority.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 21 | |
| 22 | namespace base { |
| 23 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 24 | namespace { |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 25 | |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 26 | NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path, |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 27 | NativeLibraryLoadError* error) { |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 28 | // LoadLibrary() opens the file off disk and acquires the LoaderLock, hence |
| 29 | // must not be called from DllMain. |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 30 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
[email protected] | be13068 | 2010-11-12 21:53:16 | [diff] [blame] | 31 | |
Anthony Vallee-Dubois | 3aafcf2 | 2022-02-10 16:52:04 | [diff] [blame] | 32 | // Mitigate the issues caused by loading DLLs on a background thread |
| 33 | // (see http://crbug/973868 for context). This temporarily boosts this |
| 34 | // thread's priority so that it doesn't get starved by higher priority threads |
| 35 | // while it holds the LoaderLock. |
| 36 | SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY_REPEATEDLY(); |
| 37 | |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame] | 38 | HMODULE module_handle = nullptr; |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 39 | |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame] | 40 | // LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR flag is needed to search the library |
| 41 | // directory as the library may have dependencies on DLLs in this |
| 42 | // directory. |
| 43 | module_handle = ::LoadLibraryExW( |
| 44 | library_path.value().c_str(), nullptr, |
| 45 | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); |
| 46 | // If LoadLibraryExW succeeds, log this metric and return. |
| 47 | if (module_handle) { |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame] | 48 | return module_handle; |
| 49 | } |
| 50 | // GetLastError() needs to be called immediately after |
| 51 | // LoadLibraryExW call. |
| 52 | if (error) { |
| 53 | error->code = ::GetLastError(); |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // If LoadLibraryExW API/flags are unavailable or API call fails, try |
Xi Cheng | 2740c2c | 2018-11-20 22:25:22 | [diff] [blame] | 57 | // LoadLibraryW API. From UMA, this fallback is necessary for many users. |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 58 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 59 | // Switch the current directory to the library directory as the library |
| 60 | // may have dependencies on DLLs in this directory. |
| 61 | bool restore_directory = false; |
[email protected] | 18850528 | 2009-09-16 16:31:28 | [diff] [blame] | 62 | FilePath current_directory; |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 63 | if (GetCurrentDirectory(¤t_directory)) { |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 64 | FilePath plugin_path = library_path.DirName(); |
[email protected] | 18850528 | 2009-09-16 16:31:28 | [diff] [blame] | 65 | if (!plugin_path.empty()) { |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 66 | SetCurrentDirectory(plugin_path); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 67 | restore_directory = true; |
| 68 | } |
| 69 | } |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame] | 70 | module_handle = ::LoadLibraryW(library_path.value().c_str()); |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 71 | |
| 72 | // GetLastError() needs to be called immediately after LoadLibraryW call. |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame] | 73 | if (!module_handle && error) { |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 74 | error->code = ::GetLastError(); |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame] | 75 | } |
[email protected] | f4e91145 | 2014-03-20 06:07:26 | [diff] [blame] | 76 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 77 | if (restore_directory) { |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 78 | SetCurrentDirectory(current_directory); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 79 | } |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 80 | |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame] | 81 | return module_handle; |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 82 | } |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 83 | |
| 84 | NativeLibrary LoadSystemLibraryHelper(const FilePath& library_path, |
| 85 | NativeLibraryLoadError* error) { |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 86 | // GetModuleHandleEx and subsequently LoadLibraryEx acquire the LoaderLock, |
| 87 | // hence must not be called from Dllmain. |
| 88 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 89 | NativeLibrary module; |
| 90 | BOOL module_found = |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 91 | ::GetModuleHandleExW(0, library_path.value().c_str(), &module); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 92 | if (!module_found) { |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame] | 93 | module = ::LoadLibraryExW(library_path.value().c_str(), nullptr, |
| 94 | LOAD_LIBRARY_SEARCH_SYSTEM32); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 95 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 96 | if (!module && error) { |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 97 | error->code = ::GetLastError(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 98 | } |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | return module; |
| 102 | } |
| 103 | |
Lei Zhang | b88b59b9 | 2025-02-05 22:46:47 | [diff] [blame] | 104 | FilePath GetSystemLibraryName(FilePath::StringViewType name) { |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 105 | FilePath library_path; |
| 106 | // Use an absolute path to load the DLL to avoid DLL preloading attacks. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 107 | if (PathService::Get(DIR_SYSTEM, &library_path)) { |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 108 | library_path = library_path.Append(name); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 109 | } |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 110 | return library_path; |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 113 | } // namespace |
| 114 | |
| 115 | std::string NativeLibraryLoadError::ToString() const { |
Bruce Dawson | 1917584 | 2017-08-02 17:00:45 | [diff] [blame] | 116 | return StringPrintf("%lu", code); |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 117 | } |
| 118 | |
rockot | 596a0dd | 2016-08-26 00:57:51 | [diff] [blame] | 119 | NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path, |
| 120 | const NativeLibraryOptions& options, |
| 121 | NativeLibraryLoadError* error) { |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 122 | return LoadNativeLibraryHelper(library_path, error); |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 123 | } |
| 124 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 125 | void UnloadNativeLibrary(NativeLibrary library) { |
| 126 | FreeLibrary(library); |
| 127 | } |
| 128 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 129 | void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
David Benjamin | a942007 | 2023-06-13 14:38:36 | [diff] [blame] | 130 | const char* name) { |
| 131 | return reinterpret_cast<void*>(GetProcAddress(library, name)); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 132 | } |
| 133 | |
Helmut Januschka | 1dce9dc | 2024-06-11 13:05:35 | [diff] [blame] | 134 | std::string GetNativeLibraryName(std::string_view name) { |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 135 | DCHECK(IsStringASCII(name)); |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 136 | return StrCat({name, ".dll"}); |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 137 | } |
| 138 | |
Helmut Januschka | 1dce9dc | 2024-06-11 13:05:35 | [diff] [blame] | 139 | std::string GetLoadableModuleName(std::string_view name) { |
Xiaohan Wang | d807ec3 | 2018-04-03 01:31:44 | [diff] [blame] | 140 | return GetNativeLibraryName(name); |
| 141 | } |
| 142 | |
Lei Zhang | b88b59b9 | 2025-02-05 22:46:47 | [diff] [blame] | 143 | NativeLibrary LoadSystemLibrary(FilePath::StringViewType name, |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 144 | NativeLibraryLoadError* error) { |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 145 | FilePath library_path = GetSystemLibraryName(name); |
| 146 | if (library_path.empty()) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 147 | if (error) { |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 148 | error->code = ERROR_NOT_FOUND; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 149 | } |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 150 | return nullptr; |
| 151 | } |
| 152 | return LoadSystemLibraryHelper(library_path, error); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 153 | } |
| 154 | |
Lei Zhang | b88b59b9 | 2025-02-05 22:46:47 | [diff] [blame] | 155 | NativeLibrary PinSystemLibrary(FilePath::StringViewType name, |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 156 | NativeLibraryLoadError* error) { |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 157 | FilePath library_path = GetSystemLibraryName(name); |
| 158 | if (library_path.empty()) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 159 | if (error) { |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 160 | error->code = ERROR_NOT_FOUND; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 161 | } |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 162 | return nullptr; |
| 163 | } |
| 164 | |
| 165 | // GetModuleHandleEx acquires the LoaderLock, hence must not be called from |
| 166 | // Dllmain. |
| 167 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 168 | ScopedNativeLibrary module; |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 169 | if (::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 170 | library_path.value().c_str(), |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 171 | ScopedNativeLibrary::Receiver(module).get())) { |
| 172 | return module.release(); |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 173 | } |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 174 | |
| 175 | // Load and pin the library since it wasn't already loaded. |
| 176 | module = ScopedNativeLibrary(LoadSystemLibraryHelper(library_path, error)); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 177 | if (!module.is_valid()) { |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 178 | return nullptr; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 179 | } |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 180 | |
| 181 | ScopedNativeLibrary temp; |
| 182 | if (::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 183 | library_path.value().c_str(), |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 184 | ScopedNativeLibrary::Receiver(temp).get())) { |
| 185 | return module.release(); |
| 186 | } |
| 187 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 188 | if (error) { |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 189 | error->code = ::GetLastError(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 190 | } |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 191 | // Return nullptr since we failed to pin the module. |
| 192 | return nullptr; |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 195 | } // namespace base |