Skip to content

Commit c37d94c

Browse files
committed
Force wchar_t string; move function to WindowsSupport.h
1 parent 670a81a commit c37d94c

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

llvm/include/llvm/Support/Windows/WindowsSupport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ LLVM_ABI std::error_code widenPath(const Twine &Path8,
245245
SmallVectorImpl<wchar_t> &Path16,
246246
size_t MaxPathLen = MAX_PATH);
247247

248+
/// Retrieves the handle to a in-memory system module such as ntdll.dll, while
249+
/// ensuring we're not retrieving a malicious injected module but a module
250+
/// loaded from the system path.
251+
LLVM_ABI HMODULE loadSystemModuleSecure(LPCWSTR lpModuleName);
248252
} // end namespace windows
249253
} // end namespace sys
250254
} // end namespace llvm.

llvm/lib/Support/Windows/Threading.inc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
106106
Name.clear();
107107
}
108108

109-
static HMODULE LoadSystemModuleSecure(LPCWSTR lpModuleName) {
109+
namespace llvm::sys::windows {
110+
HMODULE loadSystemModuleSecure(LPCWSTR lpModuleName) {
110111
// Ensure we load indeed a module from system32 path.
111112
// As per GetModuleHandle documentation:
112113
// "If lpModuleName does not include a path and there is more than one loaded
@@ -126,26 +127,26 @@ static HMODULE LoadSystemModuleSecure(LPCWSTR lpModuleName) {
126127
} while (Size > Buf.size());
127128

128129
Buf.truncate(Size);
129-
Buf.push_back(TEXT('\\'));
130+
Buf.push_back(L'\\');
130131
Buf.append(lpModuleName, lpModuleName + std::wcslen(lpModuleName));
131132
Buf.push_back(0);
132133

133134
return ::GetModuleHandleW(Buf.data());
134135
}
136+
} // namespace llvm::sys::windows
135137

136138
SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
137-
HMODULE kernelMod = LoadSystemModuleSecure(TEXT("kernel32.dll"));
138-
if (kernelMod) {
139+
HMODULE kernelM = llvm::sys::windows::loadSystemModuleSecure(L"kernel32.dll");
140+
if (kernelM) {
139141
// SetThreadInformation is only available on Windows 8 and later. Since we
140142
// still support compilation on Windows 7, we load the function dynamically.
141143
typedef BOOL(WINAPI * SetThreadInformation_t)(
142144
HANDLE hThread, THREAD_INFORMATION_CLASS ThreadInformationClass,
143145
_In_reads_bytes_(ThreadInformationSize) PVOID ThreadInformation,
144146
ULONG ThreadInformationSize);
145147
static const auto pfnSetThreadInformation =
146-
(SetThreadInformation_t)::GetProcAddress(kernelMod,
148+
(SetThreadInformation_t)::GetProcAddress(kernelM,
147149
"SetThreadInformation");
148-
149150
if (pfnSetThreadInformation) {
150151
auto setThreadInformation = [](ULONG ControlMaskAndStateMask) {
151152
THREAD_POWER_THROTTLING_STATE state{};

0 commit comments

Comments
 (0)