• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_WIN_WINDOWS_VERSION_H_
6 #define BASE_WIN_WINDOWS_VERSION_H_
7 
8 #include <stddef.h>
9 
10 #include <string>
11 
12 #include "base/base_export.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/version.h"
15 
16 using DWORD = unsigned long;  // NOLINT(runtime/int)
17 using HANDLE = void*;
18 struct _OSVERSIONINFOEXW;
19 struct _SYSTEM_INFO;
20 
21 namespace base {
22 namespace test {
23 class ScopedOSInfoOverride;
24 }  // namespace test
25 }  // namespace base
26 
27 namespace base {
28 namespace win {
29 
30 // The running version of Windows.  This is declared outside OSInfo for
31 // syntactic sugar reasons; see the declaration of GetVersion() below.
32 // NOTE: Keep these in order so callers can do things like
33 // "if (base::win::GetVersion() >= base::win::Version::VISTA) ...".
34 enum class Version {
35   PRE_XP = 0,  // Not supported.
36   XP = 1,
37   SERVER_2003 = 2,   // Also includes XP Pro x64 and Server 2003 R2.
38   VISTA = 3,         // Also includes Windows Server 2008.
39   WIN7 = 4,          // Also includes Windows Server 2008 R2.
40   WIN8 = 5,          // Also includes Windows Server 2012.
41   WIN8_1 = 6,        // Also includes Windows Server 2012 R2.
42   WIN10 = 7,         // Threshold 1: Version 1507, Build 10240.
43   WIN10_TH2 = 8,     // Threshold 2: Version 1511, Build 10586.
44   WIN10_RS1 = 9,     // Redstone 1: Version 1607, Build 14393.
45                      // Also includes Windows Server 2016
46   WIN10_RS2 = 10,    // Redstone 2: Version 1703, Build 15063.
47   WIN10_RS3 = 11,    // Redstone 3: Version 1709, Build 16299.
48   WIN10_RS4 = 12,    // Redstone 4: Version 1803, Build 17134.
49   WIN10_RS5 = 13,    // Redstone 5: Version 1809, Build 17763.
50                      // Also includes Windows Server 2019
51   WIN10_19H1 = 14,   // 19H1: Version 1903, Build 18362.
52   WIN10_19H2 = 15,   // 19H2: Version 1909, Build 18363.
53   WIN10_20H1 = 16,   // 20H1: Build 19041.
54   WIN10_20H2 = 17,   // 20H2: Build 19042.
55   WIN10_21H1 = 18,   // 21H1: Build 19043.
56   WIN10_21H2 = 19,   // Win10 21H2: Build 19044.
57   WIN10_22H2 = 20,   // Win10 21H2: Build 19045.
58   SERVER_2022 = 21,  // Server 2022: Build 20348.
59   WIN11 = 22,        // Win11 21H2: Build 22000.
60   WIN11_22H2 = 23,   // Win11 22H2: Build 22621.
61   WIN_LAST,          // Indicates error condition.
62 };
63 
64 // A rough bucketing of the available types of versions of Windows. This is used
65 // to distinguish enterprise enabled versions from home versions and potentially
66 // server versions. Keep these values in the same order, since they are used as
67 // is for metrics histogram ids.
68 enum VersionType {
69   SUITE_HOME = 0,
70   SUITE_PROFESSIONAL,
71   SUITE_SERVER,
72   SUITE_ENTERPRISE,
73   SUITE_EDUCATION,
74   SUITE_EDUCATION_PRO,
75   SUITE_LAST,
76 };
77 
78 // A singleton that can be used to query various pieces of information about the
79 // OS and process state. Note that this doesn't use the base Singleton class, so
80 // it can be used without an AtExitManager.
81 class BASE_EXPORT OSInfo {
82  public:
83   struct VersionNumber {
84     uint32_t major;
85     uint32_t minor;
86     uint32_t build;
87     uint32_t patch;
88   };
89 
90   struct ServicePack {
91     int major;
92     int minor;
93   };
94 
95   // The processor architecture this copy of Windows natively uses.  For
96   // example, given an x64-capable processor, we have three possibilities:
97   //   32-bit Chrome running on 32-bit Windows:           X86_ARCHITECTURE
98   //   32-bit Chrome running on 64-bit Windows via WOW64: X64_ARCHITECTURE
99   //   64-bit Chrome running on 64-bit Windows:           X64_ARCHITECTURE
100   enum WindowsArchitecture {
101     X86_ARCHITECTURE,
102     X64_ARCHITECTURE,
103     IA64_ARCHITECTURE,
104     ARM64_ARCHITECTURE,
105     OTHER_ARCHITECTURE,
106   };
107 
108   static OSInfo* GetInstance();
109 
110   OSInfo(const OSInfo&) = delete;
111   OSInfo& operator=(const OSInfo&) = delete;
112 
113   // Separate from the rest of OSInfo so they can be used during early process
114   // initialization.
115   static WindowsArchitecture GetArchitecture();
116   // This is necessary because GetArchitecture doesn't return correct OS
117   // architectures for x86/x64 binaries running on ARM64 - it says the OS is
118   // x86/x64. This function returns true if the process is an x86 or x64 process
119   // running emulated on ARM64.
120   static bool IsRunningEmulatedOnArm64();
121 
122   // Returns the OS Version as returned from a call to GetVersionEx().
version()123   const Version& version() const { return version_; }
124 
125   // Returns detailed version info containing major, minor, build and patch.
version_number()126   const VersionNumber& version_number() const { return version_number_; }
127 
128   // The Kernel32* set of functions return the OS version as determined by a
129   // call to VerQueryValue() on kernel32.dll. This avoids any running App Compat
130   // shims from manipulating the version reported.
131   static Version Kernel32Version();
132   static VersionNumber Kernel32VersionNumber();
133   static base::Version Kernel32BaseVersion();
134 
135   // These helper functions return information about common scenarios of
136   // interest in regards to WOW emulation.
137   bool IsWowDisabled() const;    // Chrome bitness matches OS bitness.
138   bool IsWowX86OnAMD64() const;  // Chrome x86 on an AMD64 host machine.
139   bool IsWowX86OnARM64() const;  // Chrome x86 on an ARM64 host machine.
140   bool IsWowAMD64OnARM64()
141       const;                     // Chrome AMD64 build on an ARM64 host machine.
142   bool IsWowX86OnOther() const;  // Chrome x86 on some other x64 host machine.
143 
144   // Functions to determine Version Type (e.g. Enterprise/Home) and Service Pack
145   // value. See above for definitions of these values.
version_type()146   const VersionType& version_type() const { return version_type_; }
service_pack()147   const ServicePack& service_pack() const { return service_pack_; }
service_pack_str()148   const std::string& service_pack_str() const { return service_pack_str_; }
149 
150   // Returns the number of processors on the system.
processors()151   const int& processors() const { return processors_; }
152 
153   // Returns the allocation granularity. See
154   // https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info.
allocation_granularity()155   const size_t& allocation_granularity() const {
156     return allocation_granularity_;
157   }
158 
159   // Processor name as read from registry.
160   std::string processor_model_name();
161 
162   // Returns the "ReleaseId" (Windows 10 release number) from the registry.
release_id()163   const std::string& release_id() const { return release_id_; }
164 
165  private:
166   friend class base::test::ScopedOSInfoOverride;
167   FRIEND_TEST_ALL_PREFIXES(OSInfo, MajorMinorBuildToVersion);
168 
169   // This enum contains a variety of 32-bit process types that could be
170   // running with consideration towards WOW64.
171   enum class WowProcessMachine {
172     kDisabled,  // Chrome bitness matches OS bitness.
173     kX86,       // 32-bit (x86) Chrome.
174     kARM32,     // 32-bit (arm32) Chrome.
175     kOther,     // all other 32-bit Chrome.
176     kUnknown,
177   };
178 
179   // This enum contains a variety of 64-bit host machine architectures that
180   // could be running with consideration towards WOW64.
181   enum class WowNativeMachine {
182     kARM64,  // 32-bit Chrome running on ARM64 Windows.
183     kAMD64,  // 32-bit Chrome running on AMD64 Windows.
184     kOther,  // 32-bit Chrome running on all other 64-bit Windows.
185     kUnknown,
186   };
187 
188   // This is separate from GetInstance() so that ScopedOSInfoOverride
189   // can override it in tests.
190   static OSInfo** GetInstanceStorage();
191 
192   OSInfo(const _OSVERSIONINFOEXW& version_info,
193          const _SYSTEM_INFO& system_info,
194          DWORD os_type);
195   ~OSInfo();
196 
197   // Returns a Version value for a given OS version tuple.
198   static Version MajorMinorBuildToVersion(uint32_t major,
199                                           uint32_t minor,
200                                           uint32_t build);
201 
202   // Returns the architecture of the process machine within the WOW emulator.
203   WowProcessMachine GetWowProcessMachineArchitecture(const int process_machine);
204 
205   // Returns the architecture of the native (host) machine using the WOW
206   // emulator.
207   WowNativeMachine GetWowNativeMachineArchitecture(const int native_machine);
208 
209   void InitializeWowStatusValuesFromLegacyApi(HANDLE process_handle);
210 
211   void InitializeWowStatusValuesForProcess(HANDLE process_handle);
212 
213   Version version_;
214   VersionNumber version_number_;
215   VersionType version_type_;
216   ServicePack service_pack_;
217 
218   // Represents the version of the OS associated to a release of
219   // Windows 10. Each version may have different releases (such as patch
220   // updates). This is the identifier of the release.
221   // Example:
222   //    Windows 10 Version 1809 (OS build 17763) has multiple releases
223   //    (i.e. build 17763.1, build 17763.195, build 17763.379, ...).
224   // See https://docs.microsoft.com/en-us/windows/windows-10/release-information
225   // for more information.
226   std::string release_id_;
227 
228   // A string, such as "Service Pack 3", that indicates the latest Service Pack
229   // installed on the system. If no Service Pack has been installed, the string
230   // is empty.
231   std::string service_pack_str_;
232   int processors_;
233   size_t allocation_granularity_;
234   WowProcessMachine wow_process_machine_;
235   WowNativeMachine wow_native_machine_;
236   std::string processor_model_name_;
237 };
238 
239 // Because this is by far the most commonly-requested value from the above
240 // singleton, we add a global-scope accessor here as syntactic sugar.
241 BASE_EXPORT Version GetVersion();
242 
243 }  // namespace win
244 }  // namespace base
245 
246 #endif  // BASE_WIN_WINDOWS_VERSION_H_
247