LLVM 24.0.0git
WindowsSupport.h
Go to the documentation of this file.
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#ifndef WIN32_LEAN_AND_MEAN
30#define WIN32_LEAN_AND_MEAN
31#endif
32#ifndef NOMINMAX
33#define NOMINMAX
34#endif
35
38#include "llvm/ADT/StringRef.h"
39#include "llvm/ADT/Twine.h"
40#include "llvm/Config/llvm-config.h" // Get build system configuration settings
42#include "llvm/Support/Chrono.h"
46#include <cassert>
47#include <string>
48#include <system_error>
49#include <windows.h>
50
51// Must be included after windows.h
52#include <wincrypt.h>
53
54namespace llvm {
55
56/// Determines if the program is running on Windows 8 or newer. This
57/// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
58/// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
59/// yet have VersionHelpers.h, so we have our own helper.
61
62/// Determines if the program is running on Windows 11 or Windows Server 2022.
64
65/// Returns the Windows version as Major.Minor.0.BuildNumber. Uses
66/// RtlGetVersion or GetVersionEx under the hood depending on what is available.
67/// GetVersionEx is deprecated, but this API exposes the build number which can
68/// be useful for working around certain kernel bugs.
70
71LLVM_ABI bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix);
72
73// Include GetLastError() in a fatal error message.
74[[noreturn]] inline void ReportLastErrorFatal(const char *Msg) {
75 std::string ErrMsg;
76 MakeErrMsg(&ErrMsg, Msg);
78}
79
80template <typename HandleTraits>
81class ScopedHandle {
82 typedef typename HandleTraits::handle_type handle_type;
83 handle_type Handle;
84
85 ScopedHandle(const ScopedHandle &other) = delete;
86 void operator=(const ScopedHandle &other) = delete;
87public:
89 : Handle(HandleTraits::GetInvalid()) {}
90
91 explicit ScopedHandle(handle_type h)
92 : Handle(h) {}
93
95 if (HandleTraits::IsValid(Handle))
96 HandleTraits::Close(Handle);
97 }
98
99 handle_type take() {
100 handle_type t = Handle;
101 Handle = HandleTraits::GetInvalid();
102 return t;
103 }
104
105 ScopedHandle &operator=(handle_type h) {
106 if (HandleTraits::IsValid(Handle))
107 HandleTraits::Close(Handle);
108 Handle = h;
109 return *this;
110 }
111
112 // True if Handle is valid.
113 explicit operator bool() const {
114 return HandleTraits::IsValid(Handle) ? true : false;
115 }
116
117 operator handle_type() const {
118 return Handle;
119 }
120};
121
123 typedef HANDLE handle_type;
124
126 return INVALID_HANDLE_VALUE;
127 }
128
129 static void Close(handle_type h) {
130 ::CloseHandle(h);
131 }
132
133 static bool IsValid(handle_type h) {
134 return h != GetInvalid();
135 }
136};
137
140 return NULL;
141 }
142};
143
145 typedef HCRYPTPROV handle_type;
146
148 return 0;
149 }
150
151 static void Close(handle_type h) {
152 ::CryptReleaseContext(h, 0);
153 }
154
155 static bool IsValid(handle_type h) {
156 return h != GetInvalid();
157 }
158};
159
161 typedef HKEY handle_type;
162
164 return NULL;
165 }
166
167 static void Close(handle_type h) {
168 ::RegCloseKey(h);
169 }
170
171 static bool IsValid(handle_type h) {
172 return h != GetInvalid();
173 }
174};
175
177 static void Close(handle_type h) {
178 ::FindClose(h);
179 }
180};
181
183
190
191template <class T>
192class SmallVectorImpl;
193
194template <class T>
197 str.push_back(0);
198 str.pop_back();
199 return str.data();
200}
201
202namespace sys {
203
204inline std::chrono::nanoseconds toDuration(FILETIME Time) {
205 ULARGE_INTEGER TimeInteger;
206 TimeInteger.LowPart = Time.dwLowDateTime;
207 TimeInteger.HighPart = Time.dwHighDateTime;
208
209 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
210 return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
211}
212
213inline TimePoint<> toTimePoint(FILETIME Time) {
214 ULARGE_INTEGER TimeInteger;
215 TimeInteger.LowPart = Time.dwLowDateTime;
216 TimeInteger.HighPart = Time.dwHighDateTime;
217
218 // Adjust for different epoch
219 TimeInteger.QuadPart -= 11644473600ll * 10000000;
220
221 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
222 return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
223}
224
225inline FILETIME toFILETIME(TimePoint<> TP) {
226 ULARGE_INTEGER TimeInteger;
227 TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
228 TimeInteger.QuadPart += 11644473600ll * 10000000;
229
230 FILETIME Time;
231 Time.dwLowDateTime = TimeInteger.LowPart;
232 Time.dwHighDateTime = TimeInteger.HighPart;
233 return Time;
234}
235
236namespace windows {
237// Returns command line arguments. Unlike arguments given to main(),
238// this function guarantees that the returned arguments are encoded in
239// UTF-8 regardless of the current code page setting.
240LLVM_ABI std::error_code
243
244/// Convert UTF-8 path to a suitable UTF-16 path for use with the Win32 Unicode
245/// File API.
246LLVM_ABI std::error_code widenPath(const Twine &Path8,
248 size_t MaxPathLen = MAX_PATH);
249
250/// Retrieves the handle to a in-memory system module such as ntdll.dll, while
251/// ensuring we're not retrieving a malicious injected module but a module
252/// loaded from the system path.
253LLVM_ABI HMODULE loadSystemModuleSecure(LPCWSTR lpModuleName);
254
255/// Convert a UTF-8 path to a long form UTF-8 path expanding any short 8.3 form
256/// components.
257LLVM_ABI std::error_code makeLongFormPath(const Twine &Path8,
259} // end namespace windows
260} // end namespace sys
261} // end namespace llvm.
262
263#endif
This file defines the BumpPtrAllocator interface.
#define LLVM_ABI
Definition Compiler.h:215
const char * Msg
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
Defines the llvm::VersionTuple class, which represents a version in the form major[....
ScopedHandle(handle_type h)
ScopedHandle & operator=(handle_type h)
handle_type take()
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
Represents a version number in the form major[.minor[.subminor[.build]]].
LLVM_ABI std::error_code makeLongFormPath(const Twine &Path8, llvm::SmallVectorImpl< char > &Result8)
Convert a UTF-8 path to a long form UTF-8 path expanding any short 8.3 form components.
LLVM_ABI std::error_code widenPath(const Twine &Path8, SmallVectorImpl< wchar_t > &Path16, size_t MaxPathLen=MAX_PATH)
Convert UTF-8 path to a suitable UTF-16 path for use with the Win32 Unicode File API.
LLVM_ABI HMODULE loadSystemModuleSecure(LPCWSTR lpModuleName)
Retrieves the handle to a in-memory system module such as ntdll.dll, while ensuring we're not retriev...
LLVM_ABI std::error_code GetCommandLineArguments(SmallVectorImpl< const char * > &Args, BumpPtrAllocator &Alloc)
std::chrono::nanoseconds toDuration(FILETIME Time)
FILETIME toFILETIME(TimePoint<> TP)
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition Chrono.h:34
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition Chrono.h:65
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool RunningWindows8OrGreater()
Determines if the program is running on Windows 8 or newer.
void ReportLastErrorFatal(const char *Msg)
ScopedHandle< CommonHandleTraits > ScopedCommonHandle
ScopedHandle< CryptContextTraits > ScopedCryptContext
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
LLVM_ABI llvm::VersionTuple GetWindowsOSVersion()
Returns the Windows version as Major.Minor.0.BuildNumber.
ScopedHandle< FindHandleTraits > ScopedFindHandle
ScopedHandle< JobHandleTraits > ScopedJobHandle
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
LLVM_ABI bool RunningWindows11OrGreater()
Determines if the program is running on Windows 11 or Windows Server 2022.
LLVM_ABI bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix)
ScopedHandle< RegTraits > ScopedRegHandle
ScopedHandle< FileHandleTraits > ScopedFileHandle
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:390
static handle_type GetInvalid()
static void Close(handle_type h)
static bool IsValid(handle_type h)
static void Close(handle_type h)
static handle_type GetInvalid()
static bool IsValid(handle_type h)
static void Close(handle_type h)
static handle_type GetInvalid()
static handle_type GetInvalid()
static bool IsValid(handle_type h)
static void Close(handle_type h)