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 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 9 | #include "base/files/file_util.h" |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 10 | #include "base/metrics/histogram_macros.h" |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 11 | #include "base/path_service.h" |
| 12 | #include "base/scoped_native_library.h" |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 13 | #include "base/strings/strcat.h" |
| 14 | #include "base/strings/string_piece.h" |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
[email protected] | f4e91145 | 2014-03-20 06:07:26 | [diff] [blame] | 16 | #include "base/strings/stringprintf.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 18 | #include "base/threading/scoped_blocking_call.h" |
Anthony Vallee-Dubois | 3aafcf2 | 2022-02-10 16:52:04 | [diff] [blame] | 19 | #include "base/threading/scoped_thread_priority.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 20 | |
| 21 | namespace base { |
| 22 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 23 | namespace { |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 24 | |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 25 | // This enum is used to back an UMA histogram, and should therefore be treated |
| 26 | // as append-only. |
| 27 | enum LoadLibraryResult { |
| 28 | // LoadLibraryExW API/flags are available and the call succeeds. |
| 29 | SUCCEED = 0, |
| 30 | // LoadLibraryExW API/flags are availabe to use but the call fails, then |
| 31 | // LoadLibraryW is used and succeeds. |
| 32 | FAIL_AND_SUCCEED, |
| 33 | // LoadLibraryExW API/flags are availabe to use but the call fails, then |
| 34 | // LoadLibraryW is used but fails as well. |
| 35 | FAIL_AND_FAIL, |
| 36 | // LoadLibraryExW API/flags are unavailabe to use, then LoadLibraryW is used |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame^] | 37 | // and succeeds. Pre-Win10-only. |
| 38 | UNAVAILABLE_AND_SUCCEED_OBSOLETE, |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 39 | // LoadLibraryExW API/flags are unavailabe to use, then LoadLibraryW is used |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame^] | 40 | // but fails. Pre-Win10-only. |
| 41 | UNAVAILABLE_AND_FAIL_OBSOLETE, |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 42 | // Add new items before this one, always keep this one at the end. |
| 43 | END |
| 44 | }; |
| 45 | |
| 46 | // A helper method to log library loading result to UMA. |
| 47 | void LogLibrarayLoadResultToUMA(LoadLibraryResult result) { |
| 48 | UMA_HISTOGRAM_ENUMERATION("LibraryLoader.LoadNativeLibraryWindows", result, |
| 49 | LoadLibraryResult::END); |
| 50 | } |
| 51 | |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 52 | // A helper method to encode the library loading result to enum |
| 53 | // LoadLibraryResult. |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame^] | 54 | LoadLibraryResult GetLoadLibraryResult(bool has_load_library_succeeded) { |
| 55 | return has_load_library_succeeded ? LoadLibraryResult::FAIL_AND_SUCCEED |
| 56 | : LoadLibraryResult::FAIL_AND_FAIL; |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 57 | } |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 58 | |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 59 | NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path, |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 60 | NativeLibraryLoadError* error) { |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 61 | // LoadLibrary() opens the file off disk and acquires the LoaderLock, hence |
| 62 | // must not be called from DllMain. |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 63 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
[email protected] | be13068 | 2010-11-12 21:53:16 | [diff] [blame] | 64 | |
Anthony Vallee-Dubois | 3aafcf2 | 2022-02-10 16:52:04 | [diff] [blame] | 65 | // Mitigate the issues caused by loading DLLs on a background thread |
| 66 | // (see http://crbug/973868 for context). This temporarily boosts this |
| 67 | // thread's priority so that it doesn't get starved by higher priority threads |
| 68 | // while it holds the LoaderLock. |
| 69 | SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY_REPEATEDLY(); |
| 70 | |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame^] | 71 | HMODULE module_handle = nullptr; |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 72 | |
| 73 | // This variable records the library loading result. |
| 74 | LoadLibraryResult load_library_result = LoadLibraryResult::SUCCEED; |
| 75 | |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame^] | 76 | // LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR flag is needed to search the library |
| 77 | // directory as the library may have dependencies on DLLs in this |
| 78 | // directory. |
| 79 | module_handle = ::LoadLibraryExW( |
| 80 | library_path.value().c_str(), nullptr, |
| 81 | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); |
| 82 | // If LoadLibraryExW succeeds, log this metric and return. |
| 83 | if (module_handle) { |
| 84 | LogLibrarayLoadResultToUMA(load_library_result); |
| 85 | return module_handle; |
| 86 | } |
| 87 | // GetLastError() needs to be called immediately after |
| 88 | // LoadLibraryExW call. |
| 89 | if (error) { |
| 90 | error->code = ::GetLastError(); |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // If LoadLibraryExW API/flags are unavailable or API call fails, try |
Xi Cheng | 2740c2c | 2018-11-20 22:25:22 | [diff] [blame] | 94 | // LoadLibraryW API. From UMA, this fallback is necessary for many users. |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 95 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 96 | // Switch the current directory to the library directory as the library |
| 97 | // may have dependencies on DLLs in this directory. |
| 98 | bool restore_directory = false; |
[email protected] | 18850528 | 2009-09-16 16:31:28 | [diff] [blame] | 99 | FilePath current_directory; |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 100 | if (GetCurrentDirectory(¤t_directory)) { |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 101 | FilePath plugin_path = library_path.DirName(); |
[email protected] | 18850528 | 2009-09-16 16:31:28 | [diff] [blame] | 102 | if (!plugin_path.empty()) { |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 103 | SetCurrentDirectory(plugin_path); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 104 | restore_directory = true; |
| 105 | } |
| 106 | } |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame^] | 107 | module_handle = ::LoadLibraryW(library_path.value().c_str()); |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 108 | |
| 109 | // GetLastError() needs to be called immediately after LoadLibraryW call. |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame^] | 110 | if (!module_handle && error) { |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 111 | error->code = ::GetLastError(); |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame^] | 112 | } |
[email protected] | f4e91145 | 2014-03-20 06:07:26 | [diff] [blame] | 113 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 114 | if (restore_directory) |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 115 | SetCurrentDirectory(current_directory); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 116 | |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 117 | // Get the library loading result and log it to UMA. |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame^] | 118 | LogLibrarayLoadResultToUMA(GetLoadLibraryResult(!!module_handle)); |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 119 | |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame^] | 120 | return module_handle; |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 121 | } |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 122 | |
| 123 | NativeLibrary LoadSystemLibraryHelper(const FilePath& library_path, |
| 124 | NativeLibraryLoadError* error) { |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 125 | // GetModuleHandleEx and subsequently LoadLibraryEx acquire the LoaderLock, |
| 126 | // hence must not be called from Dllmain. |
| 127 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 128 | NativeLibrary module; |
| 129 | BOOL module_found = |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 130 | ::GetModuleHandleExW(0, library_path.value().c_str(), &module); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 131 | if (!module_found) { |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame^] | 132 | module = ::LoadLibraryExW(library_path.value().c_str(), nullptr, |
| 133 | LOAD_LIBRARY_SEARCH_SYSTEM32); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 134 | |
| 135 | if (!module && error) |
| 136 | error->code = ::GetLastError(); |
| 137 | |
David Bienvenu | e279161 | 2023-02-03 15:56:45 | [diff] [blame^] | 138 | LogLibrarayLoadResultToUMA(GetLoadLibraryResult(!!module)); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return module; |
| 142 | } |
| 143 | |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 144 | FilePath GetSystemLibraryName(FilePath::StringPieceType name) { |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 145 | FilePath library_path; |
| 146 | // Use an absolute path to load the DLL to avoid DLL preloading attacks. |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 147 | if (PathService::Get(DIR_SYSTEM, &library_path)) |
| 148 | library_path = library_path.Append(name); |
| 149 | return library_path; |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 152 | } // namespace |
| 153 | |
| 154 | std::string NativeLibraryLoadError::ToString() const { |
Bruce Dawson | 1917584 | 2017-08-02 17:00:45 | [diff] [blame] | 155 | return StringPrintf("%lu", code); |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 156 | } |
| 157 | |
rockot | 596a0dd | 2016-08-26 00:57:51 | [diff] [blame] | 158 | NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path, |
| 159 | const NativeLibraryOptions& options, |
| 160 | NativeLibraryLoadError* error) { |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 161 | return LoadNativeLibraryHelper(library_path, error); |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 164 | void UnloadNativeLibrary(NativeLibrary library) { |
| 165 | FreeLibrary(library); |
| 166 | } |
| 167 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 168 | void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 169 | StringPiece name) { |
Nico Weber | b6499668a | 2018-03-09 12:55:03 | [diff] [blame] | 170 | return reinterpret_cast<void*>(GetProcAddress(library, name.data())); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 171 | } |
| 172 | |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 173 | std::string GetNativeLibraryName(StringPiece name) { |
| 174 | DCHECK(IsStringASCII(name)); |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 175 | return StrCat({name, ".dll"}); |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 176 | } |
| 177 | |
Xiaohan Wang | d807ec3 | 2018-04-03 01:31:44 | [diff] [blame] | 178 | std::string GetLoadableModuleName(StringPiece name) { |
| 179 | return GetNativeLibraryName(name); |
| 180 | } |
| 181 | |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 182 | NativeLibrary LoadSystemLibrary(FilePath::StringPieceType name, |
| 183 | NativeLibraryLoadError* error) { |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 184 | FilePath library_path = GetSystemLibraryName(name); |
| 185 | if (library_path.empty()) { |
| 186 | if (error) |
| 187 | error->code = ERROR_NOT_FOUND; |
| 188 | return nullptr; |
| 189 | } |
| 190 | return LoadSystemLibraryHelper(library_path, error); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 191 | } |
| 192 | |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 193 | NativeLibrary PinSystemLibrary(FilePath::StringPieceType name, |
| 194 | NativeLibraryLoadError* error) { |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 195 | FilePath library_path = GetSystemLibraryName(name); |
| 196 | if (library_path.empty()) { |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 197 | if (error) |
| 198 | error->code = ERROR_NOT_FOUND; |
| 199 | return nullptr; |
| 200 | } |
| 201 | |
| 202 | // GetModuleHandleEx acquires the LoaderLock, hence must not be called from |
| 203 | // Dllmain. |
| 204 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 205 | ScopedNativeLibrary module; |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 206 | if (::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 207 | library_path.value().c_str(), |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 208 | ScopedNativeLibrary::Receiver(module).get())) { |
| 209 | return module.release(); |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 210 | } |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 211 | |
| 212 | // Load and pin the library since it wasn't already loaded. |
| 213 | module = ScopedNativeLibrary(LoadSystemLibraryHelper(library_path, error)); |
| 214 | if (!module.is_valid()) |
| 215 | return nullptr; |
| 216 | |
| 217 | ScopedNativeLibrary temp; |
| 218 | if (::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 219 | library_path.value().c_str(), |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 220 | ScopedNativeLibrary::Receiver(temp).get())) { |
| 221 | return module.release(); |
| 222 | } |
| 223 | |
| 224 | if (error) |
| 225 | error->code = ::GetLastError(); |
| 226 | // Return nullptr since we failed to pin the module. |
| 227 | return nullptr; |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 230 | } // namespace base |