1 /*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "rtc_base/win/windows_version.h"
12
13 #include <windows.h>
14
15 #include <memory>
16
17 #include "rtc_base/checks.h"
18 #include "rtc_base/string_utils.h"
19
20 #if !defined(__clang__) && _MSC_FULL_VER < 191125507
21 #error VS 2017 Update 3.2 or higher is required
22 #endif
23
24 #if !defined(WINUWP)
25
26 namespace {
27
28 typedef BOOL(WINAPI* GetProductInfoPtr)(DWORD, DWORD, DWORD, DWORD, PDWORD);
29
30 // Mask to pull WOW64 access flags out of REGSAM access.
31 const REGSAM kWow64AccessMask = KEY_WOW64_32KEY | KEY_WOW64_64KEY;
32
33 // Utility class to read, write and manipulate the Windows Registry.
34 // Registry vocabulary primer: a "key" is like a folder, in which there
35 // are "values", which are <name, data> pairs, with an associated data type.
36 // Based on base::win::RegKey but only implements a small fraction of it.
37 class RegKey {
38 public:
RegKey()39 RegKey() : key_(nullptr), wow64access_(0) {}
40
RegKey(HKEY rootkey,const wchar_t * subkey,REGSAM access)41 RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access)
42 : key_(nullptr), wow64access_(0) {
43 if (rootkey) {
44 if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK))
45 Create(rootkey, subkey, access);
46 else
47 Open(rootkey, subkey, access);
48 } else {
49 RTC_DCHECK(!subkey);
50 wow64access_ = access & kWow64AccessMask;
51 }
52 }
53
~RegKey()54 ~RegKey() { Close(); }
55
Create(HKEY rootkey,const wchar_t * subkey,REGSAM access)56 LONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
57 DWORD disposition_value;
58 return CreateWithDisposition(rootkey, subkey, &disposition_value, access);
59 }
60
CreateWithDisposition(HKEY rootkey,const wchar_t * subkey,DWORD * disposition,REGSAM access)61 LONG CreateWithDisposition(HKEY rootkey,
62 const wchar_t* subkey,
63 DWORD* disposition,
64 REGSAM access) {
65 RTC_DCHECK(rootkey && subkey && access && disposition);
66 HKEY subhkey = NULL;
67 LONG result =
68 ::RegCreateKeyExW(rootkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
69 access, NULL, &subhkey, disposition);
70 if (result == ERROR_SUCCESS) {
71 Close();
72 key_ = subhkey;
73 wow64access_ = access & kWow64AccessMask;
74 }
75
76 return result;
77 }
78
79 // Opens an existing reg key.
Open(HKEY rootkey,const wchar_t * subkey,REGSAM access)80 LONG Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
81 RTC_DCHECK(rootkey && subkey && access);
82 HKEY subhkey = NULL;
83
84 LONG result = ::RegOpenKeyExW(rootkey, subkey, 0, access, &subhkey);
85 if (result == ERROR_SUCCESS) {
86 Close();
87 key_ = subhkey;
88 wow64access_ = access & kWow64AccessMask;
89 }
90
91 return result;
92 }
93
94 // Closes this reg key.
Close()95 void Close() {
96 if (key_) {
97 ::RegCloseKey(key_);
98 key_ = nullptr;
99 }
100 }
101
102 // Reads a REG_DWORD (uint32_t) into `out_value`. If `name` is null or empty,
103 // reads the key's default value, if any.
ReadValueDW(const wchar_t * name,DWORD * out_value) const104 LONG ReadValueDW(const wchar_t* name, DWORD* out_value) const {
105 RTC_DCHECK(out_value);
106 DWORD type = REG_DWORD;
107 DWORD size = sizeof(DWORD);
108 DWORD local_value = 0;
109 LONG result = ReadValue(name, &local_value, &size, &type);
110 if (result == ERROR_SUCCESS) {
111 if ((type == REG_DWORD || type == REG_BINARY) && size == sizeof(DWORD))
112 *out_value = local_value;
113 else
114 result = ERROR_CANTREAD;
115 }
116
117 return result;
118 }
119
120 // Reads a string into `out_value`. If `name` is null or empty, reads
121 // the key's default value, if any.
ReadValue(const wchar_t * name,std::wstring * out_value) const122 LONG ReadValue(const wchar_t* name, std::wstring* out_value) const {
123 RTC_DCHECK(out_value);
124 const size_t kMaxStringLength = 1024; // This is after expansion.
125 // Use the one of the other forms of ReadValue if 1024 is too small for you.
126 wchar_t raw_value[kMaxStringLength];
127 DWORD type = REG_SZ, size = sizeof(raw_value);
128 LONG result = ReadValue(name, raw_value, &size, &type);
129 if (result == ERROR_SUCCESS) {
130 if (type == REG_SZ) {
131 *out_value = raw_value;
132 } else if (type == REG_EXPAND_SZ) {
133 wchar_t expanded[kMaxStringLength];
134 size =
135 ::ExpandEnvironmentStringsW(raw_value, expanded, kMaxStringLength);
136 // Success: returns the number of wchar_t's copied
137 // Fail: buffer too small, returns the size required
138 // Fail: other, returns 0
139 if (size == 0 || size > kMaxStringLength) {
140 result = ERROR_MORE_DATA;
141 } else {
142 *out_value = expanded;
143 }
144 } else {
145 // Not a string. Oops.
146 result = ERROR_CANTREAD;
147 }
148 }
149
150 return result;
151 }
152
ReadValue(const wchar_t * name,void * data,DWORD * dsize,DWORD * dtype) const153 LONG ReadValue(const wchar_t* name,
154 void* data,
155 DWORD* dsize,
156 DWORD* dtype) const {
157 LONG result = RegQueryValueExW(key_, name, 0, dtype,
158 reinterpret_cast<LPBYTE>(data), dsize);
159 return result;
160 }
161
162 private:
163 HKEY key_;
164 REGSAM wow64access_;
165 };
166
167 } // namespace
168
169 #endif // !defined(WINUWP)
170
171 namespace rtc {
172 namespace rtc_win {
173 namespace {
174
175 // Helper to map a major.minor.x.build version (e.g. 6.1) to a Windows release.
MajorMinorBuildToVersion(int major,int minor,int build)176 Version MajorMinorBuildToVersion(int major, int minor, int build) {
177 if ((major == 5) && (minor > 0)) {
178 // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003.
179 return (minor == 1) ? VERSION_XP : VERSION_SERVER_2003;
180 } else if (major == 6) {
181 switch (minor) {
182 case 0:
183 // Treat Windows Server 2008 the same as Windows Vista.
184 return VERSION_VISTA;
185 case 1:
186 // Treat Windows Server 2008 R2 the same as Windows 7.
187 return VERSION_WIN7;
188 case 2:
189 // Treat Windows Server 2012 the same as Windows 8.
190 return VERSION_WIN8;
191 default:
192 RTC_DCHECK_EQ(minor, 3);
193 return VERSION_WIN8_1;
194 }
195 } else if (major == 10) {
196 if (build < 10586) {
197 return VERSION_WIN10;
198 } else if (build < 14393) {
199 return VERSION_WIN10_TH2;
200 } else if (build < 15063) {
201 return VERSION_WIN10_RS1;
202 } else if (build < 16299) {
203 return VERSION_WIN10_RS2;
204 } else if (build < 17134) {
205 return VERSION_WIN10_RS3;
206 } else if (build < 17763) {
207 return VERSION_WIN10_RS4;
208 } else if (build < 18362) {
209 return VERSION_WIN10_RS5;
210 } else if (build < 18363) {
211 return VERSION_WIN10_19H1;
212 } else if (build < 19041) {
213 return VERSION_WIN10_19H2;
214 } else if (build < 19042) {
215 return VERSION_WIN10_20H1;
216 } else if (build < 19043) {
217 return VERSION_WIN10_20H2;
218 } else if (build < 19044) {
219 return VERSION_WIN10_21H1;
220 } else if (build < 20348) {
221 return VERSION_WIN10_21H2;
222 } else if (build < 22000) {
223 return VERSION_SERVER_2022;
224 } else {
225 return VERSION_WIN11;
226 }
227 } else if (major == 11) {
228 return VERSION_WIN11;
229 } else if (major > 6) {
230 RTC_DCHECK_NOTREACHED();
231 return VERSION_WIN_LAST;
232 }
233
234 return VERSION_PRE_XP;
235 }
236
237 // Returns the the "UBR" value from the registry. Introduced in Windows 10,
238 // this undocumented value appears to be similar to a patch number.
239 // Returns 0 if the value does not exist or it could not be read.
GetUBR()240 int GetUBR() {
241 #if defined(WINUWP)
242 // The registry is not accessible for WinUWP sandboxed store applications.
243 return 0;
244 #else
245 // The values under the CurrentVersion registry hive are mirrored under
246 // the corresponding Wow6432 hive.
247 static constexpr wchar_t kRegKeyWindowsNTCurrentVersion[] =
248 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
249
250 RegKey key;
251 if (key.Open(HKEY_LOCAL_MACHINE, kRegKeyWindowsNTCurrentVersion,
252 KEY_QUERY_VALUE) != ERROR_SUCCESS) {
253 return 0;
254 }
255
256 DWORD ubr = 0;
257 key.ReadValueDW(L"UBR", &ubr);
258
259 return static_cast<int>(ubr);
260 #endif // defined(WINUWP)
261 }
262
263 } // namespace
264
265 // static
GetInstance()266 OSInfo* OSInfo::GetInstance() {
267 // Note: we don't use the Singleton class because it depends on AtExitManager,
268 // and it's convenient for other modules to use this class without it. This
269 // pattern is copied from gurl.cc.
270 static OSInfo* info;
271 if (!info) {
272 OSInfo* new_info = new OSInfo();
273 if (InterlockedCompareExchangePointer(reinterpret_cast<PVOID*>(&info),
274 new_info, NULL)) {
275 delete new_info;
276 }
277 }
278 return info;
279 }
280
OSInfo()281 OSInfo::OSInfo()
282 : version_(VERSION_PRE_XP),
283 architecture_(OTHER_ARCHITECTURE),
284 wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) {
285 OSVERSIONINFOEXW version_info = {sizeof version_info};
286 // Applications not manifested for Windows 8.1 or Windows 10 will return the
287 // Windows 8 OS version value (6.2). Once an application is manifested for a
288 // given operating system version, GetVersionEx() will always return the
289 // version that the application is manifested for in future releases.
290 // https://docs.microsoft.com/en-us/windows/desktop/SysInfo/targeting-your-application-at-windows-8-1
291 // https://www.codeproject.com/Articles/678606/Part-Overcoming-Windows-s-deprecation-of-GetVe
292 #pragma warning(push)
293 #pragma warning(disable : 4996)
294 ::GetVersionExW(reinterpret_cast<OSVERSIONINFOW*>(&version_info));
295 #pragma warning(pop)
296 version_number_.major = version_info.dwMajorVersion;
297 version_number_.minor = version_info.dwMinorVersion;
298 version_number_.build = version_info.dwBuildNumber;
299 version_number_.patch = GetUBR();
300 version_ = MajorMinorBuildToVersion(
301 version_number_.major, version_number_.minor, version_number_.build);
302 service_pack_.major = version_info.wServicePackMajor;
303 service_pack_.minor = version_info.wServicePackMinor;
304 service_pack_str_ = rtc::ToUtf8(version_info.szCSDVersion);
305
306 SYSTEM_INFO system_info = {};
307 ::GetNativeSystemInfo(&system_info);
308 switch (system_info.wProcessorArchitecture) {
309 case PROCESSOR_ARCHITECTURE_INTEL:
310 architecture_ = X86_ARCHITECTURE;
311 break;
312 case PROCESSOR_ARCHITECTURE_AMD64:
313 architecture_ = X64_ARCHITECTURE;
314 break;
315 case PROCESSOR_ARCHITECTURE_IA64:
316 architecture_ = IA64_ARCHITECTURE;
317 break;
318 }
319 processors_ = system_info.dwNumberOfProcessors;
320 allocation_granularity_ = system_info.dwAllocationGranularity;
321
322 #if !defined(WINUWP)
323 GetProductInfoPtr get_product_info;
324 DWORD os_type;
325
326 if (version_info.dwMajorVersion == 6 || version_info.dwMajorVersion == 10) {
327 // Only present on Vista+.
328 get_product_info = reinterpret_cast<GetProductInfoPtr>(::GetProcAddress(
329 ::GetModuleHandleW(L"kernel32.dll"), "GetProductInfo"));
330
331 get_product_info(version_info.dwMajorVersion, version_info.dwMinorVersion,
332 0, 0, &os_type);
333 switch (os_type) {
334 case PRODUCT_CLUSTER_SERVER:
335 case PRODUCT_DATACENTER_SERVER:
336 case PRODUCT_DATACENTER_SERVER_CORE:
337 case PRODUCT_ENTERPRISE_SERVER:
338 case PRODUCT_ENTERPRISE_SERVER_CORE:
339 case PRODUCT_ENTERPRISE_SERVER_IA64:
340 case PRODUCT_SMALLBUSINESS_SERVER:
341 case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
342 case PRODUCT_STANDARD_SERVER:
343 case PRODUCT_STANDARD_SERVER_CORE:
344 case PRODUCT_WEB_SERVER:
345 version_type_ = SUITE_SERVER;
346 break;
347 case PRODUCT_PROFESSIONAL:
348 case PRODUCT_ULTIMATE:
349 version_type_ = SUITE_PROFESSIONAL;
350 break;
351 case PRODUCT_ENTERPRISE:
352 case PRODUCT_ENTERPRISE_E:
353 case PRODUCT_ENTERPRISE_EVALUATION:
354 case PRODUCT_ENTERPRISE_N:
355 case PRODUCT_ENTERPRISE_N_EVALUATION:
356 case PRODUCT_ENTERPRISE_S:
357 case PRODUCT_ENTERPRISE_S_EVALUATION:
358 case PRODUCT_ENTERPRISE_S_N:
359 case PRODUCT_ENTERPRISE_S_N_EVALUATION:
360 case PRODUCT_BUSINESS:
361 case PRODUCT_BUSINESS_N:
362 version_type_ = SUITE_ENTERPRISE;
363 break;
364 case PRODUCT_EDUCATION:
365 case PRODUCT_EDUCATION_N:
366 version_type_ = SUITE_EDUCATION;
367 break;
368 case PRODUCT_HOME_BASIC:
369 case PRODUCT_HOME_PREMIUM:
370 case PRODUCT_STARTER:
371 default:
372 version_type_ = SUITE_HOME;
373 break;
374 }
375 } else if (version_info.dwMajorVersion == 5 &&
376 version_info.dwMinorVersion == 2) {
377 if (version_info.wProductType == VER_NT_WORKSTATION &&
378 system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
379 version_type_ = SUITE_PROFESSIONAL;
380 } else if (version_info.wSuiteMask & VER_SUITE_WH_SERVER) {
381 version_type_ = SUITE_HOME;
382 } else {
383 version_type_ = SUITE_SERVER;
384 }
385 } else if (version_info.dwMajorVersion == 5 &&
386 version_info.dwMinorVersion == 1) {
387 if (version_info.wSuiteMask & VER_SUITE_PERSONAL)
388 version_type_ = SUITE_HOME;
389 else
390 version_type_ = SUITE_PROFESSIONAL;
391 } else {
392 // Windows is pre XP so we don't care but pick a safe default.
393 version_type_ = SUITE_HOME;
394 }
395 #else
396 // WinUWP sandboxed store apps do not have a mechanism to determine
397 // product suite thus the most restricted suite is chosen.
398 version_type_ = SUITE_HOME;
399 #endif // !defined(WINUWP)
400 }
401
~OSInfo()402 OSInfo::~OSInfo() {}
403
processor_model_name()404 std::string OSInfo::processor_model_name() {
405 #if defined(WINUWP)
406 // WinUWP sandboxed store apps do not have the ability to
407 // probe the name of the current processor.
408 return "Unknown Processor (UWP)";
409 #else
410 if (processor_model_name_.empty()) {
411 const wchar_t kProcessorNameString[] =
412 L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
413 RegKey key(HKEY_LOCAL_MACHINE, kProcessorNameString, KEY_READ);
414 std::wstring value;
415 key.ReadValue(L"ProcessorNameString", &value);
416 processor_model_name_ = rtc::ToUtf8(value);
417 }
418 return processor_model_name_;
419 #endif // defined(WINUWP)
420 }
421
422 // static
GetWOW64StatusForProcess(HANDLE process_handle)423 OSInfo::WOW64Status OSInfo::GetWOW64StatusForProcess(HANDLE process_handle) {
424 BOOL is_wow64;
425 #if defined(WINUWP)
426 if (!IsWow64Process(process_handle, &is_wow64))
427 return WOW64_UNKNOWN;
428 #else
429 typedef BOOL(WINAPI * IsWow64ProcessFunc)(HANDLE, PBOOL);
430 IsWow64ProcessFunc is_wow64_process = reinterpret_cast<IsWow64ProcessFunc>(
431 GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "IsWow64Process"));
432 if (!is_wow64_process)
433 return WOW64_DISABLED;
434 if (!(*is_wow64_process)(process_handle, &is_wow64))
435 return WOW64_UNKNOWN;
436 #endif // defined(WINUWP)
437 return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED;
438 }
439
GetVersion()440 Version GetVersion() {
441 return OSInfo::GetInstance()->version();
442 }
443
444 } // namespace rtc_win
445 } // namespace rtc
446