windows_version.h 8.8 KB

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