xref: /aosp_15_r20/external/cronet/base/win/windows_version.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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   WIN11_23H2 = 24,   // Win11 23H2: Build 22631.
62   WIN_LAST,          // Indicates error condition.
63 };
64 
65 // A rough bucketing of the available types of versions of Windows. This is used
66 // to distinguish enterprise enabled versions from home versions and potentially
67 // server versions. Keep these values in the same order, since they are used as
68 // is for metrics histogram ids.
69 enum VersionType {
70   SUITE_HOME = 0,
71   SUITE_PROFESSIONAL,
72   SUITE_SERVER,
73   SUITE_ENTERPRISE,
74   SUITE_EDUCATION,
75   SUITE_EDUCATION_PRO,
76   SUITE_LAST,
77 };
78 
79 // A singleton that can be used to query various pieces of information about the
80 // OS and process state. Note that this doesn't use the base Singleton class, so
81 // it can be used without an AtExitManager.
82 class BASE_EXPORT OSInfo {
83  public:
84   struct VersionNumber {
85     uint32_t major;
86     uint32_t minor;
87     uint32_t build;
88     uint32_t patch;
89   };
90 
91   struct ServicePack {
92     int major;
93     int minor;
94   };
95 
96   // The processor architecture this copy of Windows natively uses.  For
97   // example, given an x64-capable processor, we have three possibilities:
98   //   32-bit Chrome running on 32-bit Windows:           X86_ARCHITECTURE
99   //   32-bit Chrome running on 64-bit Windows via WOW64: X64_ARCHITECTURE
100   //   64-bit Chrome running on 64-bit Windows:           X64_ARCHITECTURE
101   enum WindowsArchitecture {
102     X86_ARCHITECTURE,
103     X64_ARCHITECTURE,
104     IA64_ARCHITECTURE,
105     ARM64_ARCHITECTURE,
106     OTHER_ARCHITECTURE,
107   };
108 
109   static OSInfo* GetInstance();
110 
111   OSInfo(const OSInfo&) = delete;
112   OSInfo& operator=(const OSInfo&) = delete;
113 
114   // Separate from the rest of OSInfo so they can be used during early process
115   // initialization.
116   static WindowsArchitecture GetArchitecture();
117   // This is necessary because GetArchitecture doesn't return correct OS
118   // architectures for x86/x64 binaries running on ARM64 - it says the OS is
119   // x86/x64. This function returns true if the process is an x86 or x64 process
120   // running emulated on ARM64.
121   static bool IsRunningEmulatedOnArm64();
122 
123   // Returns the OS Version as returned from a call to GetVersionEx().
version()124   const Version& version() const { return version_; }
125 
126   // Returns detailed version info containing major, minor, build and patch.
version_number()127   const VersionNumber& version_number() const { return version_number_; }
128 
129   // The Kernel32* set of functions return the OS version as determined by a
130   // call to VerQueryValue() on kernel32.dll. This avoids any running App Compat
131   // shims from manipulating the version reported.
132   static Version Kernel32Version();
133   static VersionNumber Kernel32VersionNumber();
134   static base::Version Kernel32BaseVersion();
135 
136   // These helper functions return information about common scenarios of
137   // interest in regards to WOW emulation.
138   bool IsWowDisabled() const;    // Chrome bitness matches OS bitness.
139   bool IsWowX86OnAMD64() const;  // Chrome x86 on an AMD64 host machine.
140   bool IsWowX86OnARM64() const;  // Chrome x86 on an ARM64 host machine.
141   bool IsWowAMD64OnARM64()
142       const;                     // Chrome AMD64 build on an ARM64 host machine.
143   bool IsWowX86OnOther() const;  // Chrome x86 on some other x64 host machine.
144 
145   // Functions to determine Version Type (e.g. Enterprise/Home) and Service Pack
146   // value. See above for definitions of these values.
version_type()147   const VersionType& version_type() const { return version_type_; }
service_pack()148   const ServicePack& service_pack() const { return service_pack_; }
service_pack_str()149   const std::string& service_pack_str() const { return service_pack_str_; }
150 
151   // Returns the number of processors on the system.
processors()152   const int& processors() const { return processors_; }
153 
154   // Returns the allocation granularity. See
155   // https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info.
allocation_granularity()156   const size_t& allocation_granularity() const {
157     return allocation_granularity_;
158   }
159 
160   // Processor name as read from registry.
161   std::string processor_model_name();
162 
163   // Returns the "ReleaseId" (Windows 10 release number) from the registry.
release_id()164   const std::string& release_id() const { return release_id_; }
165 
166   // It returns true if the Windows SKU is N edition.
167   bool IsWindowsNSku() const;
168 
169  private:
170   friend class base::test::ScopedOSInfoOverride;
171   FRIEND_TEST_ALL_PREFIXES(OSInfo, MajorMinorBuildToVersion);
172 
173   // This enum contains a variety of 32-bit process types that could be
174   // running with consideration towards WOW64.
175   enum class WowProcessMachine {
176     kDisabled,  // Chrome bitness matches OS bitness.
177     kX86,       // 32-bit (x86) Chrome.
178     kARM32,     // 32-bit (arm32) Chrome.
179     kOther,     // all other 32-bit Chrome.
180     kUnknown,
181   };
182 
183   // This enum contains a variety of 64-bit host machine architectures that
184   // could be running with consideration towards WOW64.
185   enum class WowNativeMachine {
186     kARM64,  // 32-bit Chrome running on ARM64 Windows.
187     kAMD64,  // 32-bit Chrome running on AMD64 Windows.
188     kOther,  // 32-bit Chrome running on all other 64-bit Windows.
189     kUnknown,
190   };
191 
192   // This is separate from GetInstance() so that ScopedOSInfoOverride
193   // can override it in tests.
194   static OSInfo** GetInstanceStorage();
195 
196   OSInfo(const _OSVERSIONINFOEXW& version_info,
197          const _SYSTEM_INFO& system_info,
198          DWORD os_type);
199   ~OSInfo();
200 
201   // Returns a Version value for a given OS version tuple.
202   static Version MajorMinorBuildToVersion(uint32_t major,
203                                           uint32_t minor,
204                                           uint32_t build);
205 
206   // Returns the architecture of the process machine within the WOW emulator.
207   WowProcessMachine GetWowProcessMachineArchitecture(const int process_machine);
208 
209   // Returns the architecture of the native (host) machine using the WOW
210   // emulator.
211   WowNativeMachine GetWowNativeMachineArchitecture(const int native_machine);
212 
213   void InitializeWowStatusValuesFromLegacyApi(HANDLE process_handle);
214 
215   void InitializeWowStatusValuesForProcess(HANDLE process_handle);
216 
217   Version version_;
218   VersionNumber version_number_;
219   VersionType version_type_;
220   ServicePack service_pack_;
221 
222   // Represents the version of the OS associated to a release of
223   // Windows 10. Each version may have different releases (such as patch
224   // updates). This is the identifier of the release.
225   // Example:
226   //    Windows 10 Version 1809 (OS build 17763) has multiple releases
227   //    (i.e. build 17763.1, build 17763.195, build 17763.379, ...).
228   // See https://docs.microsoft.com/en-us/windows/windows-10/release-information
229   // for more information.
230   std::string release_id_;
231 
232   // A string, such as "Service Pack 3", that indicates the latest Service Pack
233   // installed on the system. If no Service Pack has been installed, the string
234   // is empty.
235   std::string service_pack_str_;
236   int processors_;
237   size_t allocation_granularity_;
238   WowProcessMachine wow_process_machine_;
239   WowNativeMachine wow_native_machine_;
240   std::string processor_model_name_;
241   DWORD os_type_;
242 };
243 
244 // Because this is by far the most commonly-requested value from the above
245 // singleton, we add a global-scope accessor here as syntactic sugar.
246 BASE_EXPORT Version GetVersion();
247 
248 }  // namespace win
249 }  // namespace base
250 
251 #endif  // BASE_WIN_WINDOWS_VERSION_H_
252