1 //===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines things specific to Windows implementations. In addition to 10 // providing some helpers for working with win32 APIs, this header wraps 11 // <windows.h> with some portability macros. Always include WindowsSupport.h 12 // instead of including <windows.h> directly. 13 // 14 //===----------------------------------------------------------------------===// 15 16 //===----------------------------------------------------------------------===// 17 //=== WARNING: Implementation here must contain only generic Win32 code that 18 //=== is guaranteed to work on *all* Win32 variants. 19 //===----------------------------------------------------------------------===// 20 21 #ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H 22 #define LLVM_SUPPORT_WINDOWSSUPPORT_H 23 24 // mingw-w64 tends to define it as 0x0502 in its headers. 25 #undef _WIN32_WINNT 26 27 // Require at least Windows 7 API. 28 #define _WIN32_WINNT 0x0601 29 #define WIN32_LEAN_AND_MEAN 30 #ifndef NOMINMAX 31 #define NOMINMAX 32 #endif 33 34 #include "llvm/ADT/SmallVector.h" 35 #include "llvm/ADT/StringExtras.h" 36 #include "llvm/ADT/StringRef.h" 37 #include "llvm/ADT/Twine.h" 38 #include "llvm/Config/llvm-config.h" // Get build system configuration settings 39 #include "llvm/Support/Allocator.h" 40 #include "llvm/Support/Chrono.h" 41 #include "llvm/Support/Compiler.h" 42 #include "llvm/Support/ErrorHandling.h" 43 #include "llvm/Support/VersionTuple.h" 44 #include <cassert> 45 #include <string> 46 #include <system_error> 47 #include <windows.h> 48 49 // Must be included after windows.h 50 #include <wincrypt.h> 51 52 namespace llvm { 53 54 /// Determines if the program is running on Windows 8 or newer. This 55 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended 56 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't 57 /// yet have VersionHelpers.h, so we have our own helper. 58 bool RunningWindows8OrGreater(); 59 60 /// Determines if the program is running on Windows 11 or Windows Server 2022. 61 bool RunningWindows11OrGreater(); 62 63 /// Returns the Windows version as Major.Minor.0.BuildNumber. Uses 64 /// RtlGetVersion or GetVersionEx under the hood depending on what is available. 65 /// GetVersionEx is deprecated, but this API exposes the build number which can 66 /// be useful for working around certain kernel bugs. 67 llvm::VersionTuple GetWindowsOSVersion(); 68 69 bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix); 70 71 // Include GetLastError() in a fatal error message. 72 [[noreturn]] inline void ReportLastErrorFatal(const char *Msg) { 73 std::string ErrMsg; 74 MakeErrMsg(&ErrMsg, Msg); 75 llvm::report_fatal_error(Twine(ErrMsg)); 76 } 77 78 template <typename HandleTraits> 79 class ScopedHandle { 80 typedef typename HandleTraits::handle_type handle_type; 81 handle_type Handle; 82 83 ScopedHandle(const ScopedHandle &other) = delete; 84 void operator=(const ScopedHandle &other) = delete; 85 public: 86 ScopedHandle() 87 : Handle(HandleTraits::GetInvalid()) {} 88 89 explicit ScopedHandle(handle_type h) 90 : Handle(h) {} 91 92 ~ScopedHandle() { 93 if (HandleTraits::IsValid(Handle)) 94 HandleTraits::Close(Handle); 95 } 96 97 handle_type take() { 98 handle_type t = Handle; 99 Handle = HandleTraits::GetInvalid(); 100 return t; 101 } 102 103 ScopedHandle &operator=(handle_type h) { 104 if (HandleTraits::IsValid(Handle)) 105 HandleTraits::Close(Handle); 106 Handle = h; 107 return *this; 108 } 109 110 // True if Handle is valid. 111 explicit operator bool() const { 112 return HandleTraits::IsValid(Handle) ? true : false; 113 } 114 115 operator handle_type() const { 116 return Handle; 117 } 118 }; 119 120 struct CommonHandleTraits { 121 typedef HANDLE handle_type; 122 123 static handle_type GetInvalid() { 124 return INVALID_HANDLE_VALUE; 125 } 126 127 static void Close(handle_type h) { 128 ::CloseHandle(h); 129 } 130 131 static bool IsValid(handle_type h) { 132 return h != GetInvalid(); 133 } 134 }; 135 136 struct JobHandleTraits : CommonHandleTraits { 137 static handle_type GetInvalid() { 138 return NULL; 139 } 140 }; 141 142 struct CryptContextTraits : CommonHandleTraits { 143 typedef HCRYPTPROV handle_type; 144 145 static handle_type GetInvalid() { 146 return 0; 147 } 148 149 static void Close(handle_type h) { 150 ::CryptReleaseContext(h, 0); 151 } 152 153 static bool IsValid(handle_type h) { 154 return h != GetInvalid(); 155 } 156 }; 157 158 struct RegTraits : CommonHandleTraits { 159 typedef HKEY handle_type; 160 161 static handle_type GetInvalid() { 162 return NULL; 163 } 164 165 static void Close(handle_type h) { 166 ::RegCloseKey(h); 167 } 168 169 static bool IsValid(handle_type h) { 170 return h != GetInvalid(); 171 } 172 }; 173 174 struct FindHandleTraits : CommonHandleTraits { 175 static void Close(handle_type h) { 176 ::FindClose(h); 177 } 178 }; 179 180 struct FileHandleTraits : CommonHandleTraits {}; 181 182 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle; 183 typedef ScopedHandle<FileHandleTraits> ScopedFileHandle; 184 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext; 185 typedef ScopedHandle<RegTraits> ScopedRegHandle; 186 typedef ScopedHandle<FindHandleTraits> ScopedFindHandle; 187 typedef ScopedHandle<JobHandleTraits> ScopedJobHandle; 188 189 template <class T> 190 class SmallVectorImpl; 191 192 template <class T> 193 typename SmallVectorImpl<T>::const_pointer 194 c_str(SmallVectorImpl<T> &str) { 195 str.push_back(0); 196 str.pop_back(); 197 return str.data(); 198 } 199 200 namespace sys { 201 202 inline std::chrono::nanoseconds toDuration(FILETIME Time) { 203 ULARGE_INTEGER TimeInteger; 204 TimeInteger.LowPart = Time.dwLowDateTime; 205 TimeInteger.HighPart = Time.dwHighDateTime; 206 207 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond) 208 return std::chrono::nanoseconds(100 * TimeInteger.QuadPart); 209 } 210 211 inline TimePoint<> toTimePoint(FILETIME Time) { 212 ULARGE_INTEGER TimeInteger; 213 TimeInteger.LowPart = Time.dwLowDateTime; 214 TimeInteger.HighPart = Time.dwHighDateTime; 215 216 // Adjust for different epoch 217 TimeInteger.QuadPart -= 11644473600ll * 10000000; 218 219 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond) 220 return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart)); 221 } 222 223 inline FILETIME toFILETIME(TimePoint<> TP) { 224 ULARGE_INTEGER TimeInteger; 225 TimeInteger.QuadPart = TP.time_since_epoch().count() / 100; 226 TimeInteger.QuadPart += 11644473600ll * 10000000; 227 228 FILETIME Time; 229 Time.dwLowDateTime = TimeInteger.LowPart; 230 Time.dwHighDateTime = TimeInteger.HighPart; 231 return Time; 232 } 233 234 namespace windows { 235 // Returns command line arguments. Unlike arguments given to main(), 236 // this function guarantees that the returned arguments are encoded in 237 // UTF-8 regardless of the current code page setting. 238 std::error_code GetCommandLineArguments(SmallVectorImpl<const char *> &Args, 239 BumpPtrAllocator &Alloc); 240 241 /// Convert UTF-8 path to a suitable UTF-16 path for use with the Win32 Unicode 242 /// File API. 243 std::error_code widenPath(const Twine &Path8, SmallVectorImpl<wchar_t> &Path16, 244 size_t MaxPathLen = MAX_PATH); 245 246 } // end namespace windows 247 } // end namespace sys 248 } // end namespace llvm. 249 250 #endif 251