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