• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
207       return VERSION_WIN10_RS4;
208     }
209   } else if (major > 6) {
210     RTC_NOTREACHED();
211     return VERSION_WIN_LAST;
212   }
213 
214   return VERSION_PRE_XP;
215 }
216 
217 // Returns the the "UBR" value from the registry. Introduced in Windows 10,
218 // this undocumented value appears to be similar to a patch number.
219 // Returns 0 if the value does not exist or it could not be read.
GetUBR()220 int GetUBR() {
221 #if defined(WINUWP)
222   // The registry is not accessible for WinUWP sandboxed store applications.
223   return 0;
224 #else
225   // The values under the CurrentVersion registry hive are mirrored under
226   // the corresponding Wow6432 hive.
227   static constexpr wchar_t kRegKeyWindowsNTCurrentVersion[] =
228       L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
229 
230   RegKey key;
231   if (key.Open(HKEY_LOCAL_MACHINE, kRegKeyWindowsNTCurrentVersion,
232                KEY_QUERY_VALUE) != ERROR_SUCCESS) {
233     return 0;
234   }
235 
236   DWORD ubr = 0;
237   key.ReadValueDW(L"UBR", &ubr);
238 
239   return static_cast<int>(ubr);
240 #endif  // defined(WINUWP)
241 }
242 
243 }  // namespace
244 
245 // static
GetInstance()246 OSInfo* OSInfo::GetInstance() {
247   // Note: we don't use the Singleton class because it depends on AtExitManager,
248   // and it's convenient for other modules to use this class without it. This
249   // pattern is copied from gurl.cc.
250   static OSInfo* info;
251   if (!info) {
252     OSInfo* new_info = new OSInfo();
253     if (InterlockedCompareExchangePointer(reinterpret_cast<PVOID*>(&info),
254                                           new_info, NULL)) {
255       delete new_info;
256     }
257   }
258   return info;
259 }
260 
OSInfo()261 OSInfo::OSInfo()
262     : version_(VERSION_PRE_XP),
263       architecture_(OTHER_ARCHITECTURE),
264       wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) {
265   OSVERSIONINFOEXW version_info = {sizeof version_info};
266   // Applications not manifested for Windows 8.1 or Windows 10 will return the
267   // Windows 8 OS version value (6.2). Once an application is manifested for a
268   // given operating system version, GetVersionEx() will always return the
269   // version that the application is manifested for in future releases.
270   // https://docs.microsoft.com/en-us/windows/desktop/SysInfo/targeting-your-application-at-windows-8-1.
271   // https://www.codeproject.com/Articles/678606/Part-Overcoming-Windows-s-deprecation-of-GetVe.
272   ::GetVersionExW(reinterpret_cast<OSVERSIONINFOW*>(&version_info));
273   version_number_.major = version_info.dwMajorVersion;
274   version_number_.minor = version_info.dwMinorVersion;
275   version_number_.build = version_info.dwBuildNumber;
276   version_number_.patch = GetUBR();
277   version_ = MajorMinorBuildToVersion(
278       version_number_.major, version_number_.minor, version_number_.build);
279   service_pack_.major = version_info.wServicePackMajor;
280   service_pack_.minor = version_info.wServicePackMinor;
281   service_pack_str_ = rtc::ToUtf8(version_info.szCSDVersion);
282 
283   SYSTEM_INFO system_info = {};
284   ::GetNativeSystemInfo(&system_info);
285   switch (system_info.wProcessorArchitecture) {
286     case PROCESSOR_ARCHITECTURE_INTEL:
287       architecture_ = X86_ARCHITECTURE;
288       break;
289     case PROCESSOR_ARCHITECTURE_AMD64:
290       architecture_ = X64_ARCHITECTURE;
291       break;
292     case PROCESSOR_ARCHITECTURE_IA64:
293       architecture_ = IA64_ARCHITECTURE;
294       break;
295   }
296   processors_ = system_info.dwNumberOfProcessors;
297   allocation_granularity_ = system_info.dwAllocationGranularity;
298 
299 #if !defined(WINUWP)
300   GetProductInfoPtr get_product_info;
301   DWORD os_type;
302 
303   if (version_info.dwMajorVersion == 6 || version_info.dwMajorVersion == 10) {
304     // Only present on Vista+.
305     get_product_info = reinterpret_cast<GetProductInfoPtr>(::GetProcAddress(
306         ::GetModuleHandleW(L"kernel32.dll"), "GetProductInfo"));
307 
308     get_product_info(version_info.dwMajorVersion, version_info.dwMinorVersion,
309                      0, 0, &os_type);
310     switch (os_type) {
311       case PRODUCT_CLUSTER_SERVER:
312       case PRODUCT_DATACENTER_SERVER:
313       case PRODUCT_DATACENTER_SERVER_CORE:
314       case PRODUCT_ENTERPRISE_SERVER:
315       case PRODUCT_ENTERPRISE_SERVER_CORE:
316       case PRODUCT_ENTERPRISE_SERVER_IA64:
317       case PRODUCT_SMALLBUSINESS_SERVER:
318       case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
319       case PRODUCT_STANDARD_SERVER:
320       case PRODUCT_STANDARD_SERVER_CORE:
321       case PRODUCT_WEB_SERVER:
322         version_type_ = SUITE_SERVER;
323         break;
324       case PRODUCT_PROFESSIONAL:
325       case PRODUCT_ULTIMATE:
326         version_type_ = SUITE_PROFESSIONAL;
327         break;
328       case PRODUCT_ENTERPRISE:
329       case PRODUCT_ENTERPRISE_E:
330       case PRODUCT_ENTERPRISE_EVALUATION:
331       case PRODUCT_ENTERPRISE_N:
332       case PRODUCT_ENTERPRISE_N_EVALUATION:
333       case PRODUCT_ENTERPRISE_S:
334       case PRODUCT_ENTERPRISE_S_EVALUATION:
335       case PRODUCT_ENTERPRISE_S_N:
336       case PRODUCT_ENTERPRISE_S_N_EVALUATION:
337       case PRODUCT_BUSINESS:
338       case PRODUCT_BUSINESS_N:
339         version_type_ = SUITE_ENTERPRISE;
340         break;
341       case PRODUCT_EDUCATION:
342       case PRODUCT_EDUCATION_N:
343         version_type_ = SUITE_EDUCATION;
344         break;
345       case PRODUCT_HOME_BASIC:
346       case PRODUCT_HOME_PREMIUM:
347       case PRODUCT_STARTER:
348       default:
349         version_type_ = SUITE_HOME;
350         break;
351     }
352   } else if (version_info.dwMajorVersion == 5 &&
353              version_info.dwMinorVersion == 2) {
354     if (version_info.wProductType == VER_NT_WORKSTATION &&
355         system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
356       version_type_ = SUITE_PROFESSIONAL;
357     } else if (version_info.wSuiteMask & VER_SUITE_WH_SERVER) {
358       version_type_ = SUITE_HOME;
359     } else {
360       version_type_ = SUITE_SERVER;
361     }
362   } else if (version_info.dwMajorVersion == 5 &&
363              version_info.dwMinorVersion == 1) {
364     if (version_info.wSuiteMask & VER_SUITE_PERSONAL)
365       version_type_ = SUITE_HOME;
366     else
367       version_type_ = SUITE_PROFESSIONAL;
368   } else {
369     // Windows is pre XP so we don't care but pick a safe default.
370     version_type_ = SUITE_HOME;
371   }
372 #else
373   // WinUWP sandboxed store apps do not have a mechanism to determine
374   // product suite thus the most restricted suite is chosen.
375   version_type_ = SUITE_HOME;
376 #endif  // !defined(WINUWP)
377 }
378 
~OSInfo()379 OSInfo::~OSInfo() {}
380 
processor_model_name()381 std::string OSInfo::processor_model_name() {
382 #if defined(WINUWP)
383   // WinUWP sandboxed store apps do not have the ability to
384   // probe the name of the current processor.
385   return "Unknown Processor (UWP)";
386 #else
387   if (processor_model_name_.empty()) {
388     const wchar_t kProcessorNameString[] =
389         L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
390     RegKey key(HKEY_LOCAL_MACHINE, kProcessorNameString, KEY_READ);
391     std::wstring value;
392     key.ReadValue(L"ProcessorNameString", &value);
393     processor_model_name_ = rtc::ToUtf8(value);
394   }
395   return processor_model_name_;
396 #endif  // defined(WINUWP)
397 }
398 
399 // static
GetWOW64StatusForProcess(HANDLE process_handle)400 OSInfo::WOW64Status OSInfo::GetWOW64StatusForProcess(HANDLE process_handle) {
401   BOOL is_wow64;
402 #if defined(WINUWP)
403   if (!IsWow64Process(process_handle, &is_wow64))
404     return WOW64_UNKNOWN;
405 #else
406   typedef BOOL(WINAPI * IsWow64ProcessFunc)(HANDLE, PBOOL);
407   IsWow64ProcessFunc is_wow64_process = reinterpret_cast<IsWow64ProcessFunc>(
408       GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "IsWow64Process"));
409   if (!is_wow64_process)
410     return WOW64_DISABLED;
411   if (!(*is_wow64_process)(process_handle, &is_wow64))
412     return WOW64_UNKNOWN;
413 #endif  // defined(WINUWP)
414   return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED;
415 }
416 
GetVersion()417 Version GetVersion() {
418   return OSInfo::GetInstance()->version();
419 }
420 
421 }  // namespace rtc_win
422 }  // namespace rtc
423