This repository was archived by the owner on Jan 23, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathwinfix.cpp
302 lines (238 loc) · 8.99 KB
/
winfix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//*****************************************************************************
// WinWrap.cpp
//
//
// This file contains wrapper functions for Win32 API's that take strings.
//
// COM+ internally uses UNICODE as the internal state and string format. This
// file will undef the mapping macros so that one cannot mistakingly call a
// method that isn't going to work. Instead, you have to call the correct
// wrapper API.
//
//*****************************************************************************
#include"stdafx.h"// Precompiled header key.
#include"winwrap.h"// Header for macros and functions.
#include"utilcode.h"
#include"holder.h"
#include"ndpversion.h"
#include"pedecoder.h"
// ====== READ BEFORE ADDING CONTRACTS ==================================================
// The functions in this file propagate SetLastError codes to their callers.
// Contracts are not guaranteed to preserve these codes (and no, we're not taking
// the overhead hit to make them do so. Don't bother asking.)
//
// Most of the wrappers have a contract of the form:
//
// NOTHROW;
// INJECT_FAULT(xxx);
//
// For such functions, use the special purpose construct:
//
// WINWRAPPER_NO_CONTRACT(xxx);
//
// For everything else, use STATIC_CONTRACT.
//
#undef CONTRACT
#defineCONTRACT $$$$$$$$READ_COMMENT_IN_WINFIX_CPP$$$$$$$$$$
#undef CONTRACTL
#defineCONTRACTL $$$$$$$$READ_COMMENT_IN_WINFIX_CPP$$$$$$$$$$
#ifdef ENABLE_CONTRACTS_IMPL
static BOOL gWinWrapperContractRecursionBreak = FALSE;
classWinWrapperContract
{
public:
WinWrapperContract(constchar *szFunction, constchar *szFile, int lineNum)
{
CANNOT_HAVE_CONTRACT;
m_pClrDebugState = NULL;
if (gWinWrapperContractRecursionBreak)
{
return;
}
m_pClrDebugState = GetClrDebugState();
// Save old debug state
m_IncomingClrDebugState = *m_pClrDebugState;
m_pClrDebugState->ViolationMaskReset( ThrowsViolation );
if (m_pClrDebugState->IsFaultForbid() && !(m_pClrDebugState->ViolationMask() & (FaultViolation|FaultNotFatal|BadDebugState)))
{
gWinWrapperContractRecursionBreak = TRUE;
CONTRACT_ASSERT("INJECT_FAULT called in a FAULTFORBID region.",
Contract::FAULT_Forbid,
Contract::FAULT_Mask,
szFunction,
szFile,
lineNum
);
}
};
~WinWrapperContract()
{
CANNOT_HAVE_CONTRACT;
//!!!!!! THIS DESTRUCTOR MUST NOT CHANGE THE GETLASTERROR VALUE !!!!!!
// Backout all changes to debug state.
if (m_pClrDebugState != NULL)
{
*m_pClrDebugState = m_IncomingClrDebugState;
}
}
private:
ClrDebugState *m_pClrDebugState;
ClrDebugState m_IncomingClrDebugState;
};
#endif
#ifdef ENABLE_CONTRACTS_IMPL
#defineWINWRAPPER_NO_CONTRACT(stmt) \
STATIC_CONTRACT_NOTHROW; \
STATIC_CONTRACT_FAULT; \
STATIC_CONTRACT_CANNOT_TAKE_LOCK; \
WinWrapperContract __wcontract(__FUNCTION__, __FILE__, __LINE__); \
if (0) {stmt} \
#defineSTATIC_WINWRAPPER_NO_CONTRACT(stmt) \
STATIC_CONTRACT_NOTHROW; \
STATIC_CONTRACT_CANNOT_TAKE_LOCK; \
STATIC_CONTRACT_FAULT; \
if (0) {stmt} \
#else
#defineWINWRAPPER_NO_CONTRACT(stmt)
#defineSTATIC_WINWRAPPER_NO_CONTRACT(stmt)
#endif
ULONG g_dwMaxDBCSCharByteSize = 0;
// The only purpose of this function is to make a local copy of lpCommandLine.
// Because windows implementation of CreateProcessW can actually change lpCommandLine,
// but we'd like to keep it const.
BOOL
WszCreateProcess(
LPCWSTR lpApplicationName,
LPCWSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCWSTR lpCurrentDirectory,
LPSTARTUPINFOW lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
)
{
WINWRAPPER_NO_CONTRACT(SetLastError(ERROR_OUTOFMEMORY); return0;);
BOOL fResult;
DWORD err;
{
size_t commandLineLength = wcslen(lpCommandLine) + 1;
NewArrayHolder<WCHAR> nonConstCommandLine(new (nothrow) WCHAR[commandLineLength]);
if (nonConstCommandLine == NULL)
{
SetLastError(ERROR_OUTOFMEMORY);
return0;
}
memcpy(nonConstCommandLine, lpCommandLine, commandLineLength * sizeof(WCHAR));
fResult = CreateProcessW(lpApplicationName,
nonConstCommandLine,
lpProcessAttributes,
lpThreadAttributes,
bInheritHandles,
dwCreationFlags,
lpEnvironment,
(LPWSTR)lpCurrentDirectory,
lpStartupInfo,
lpProcessInformation);
// At the end of the current scope, the last error code will be overwritten by the destructor of
// NewArrayHolder. So we save the error code here, and restore it after the end of the current scope.
err = GetLastError();
}
SetLastError(err);
returnfResult;
}
#ifndef FEATURE_PAL
#include"psapi.h"
#include"tlhelp32.h"
#include"winnls.h"
//********** Globals. *********************************************************
bool g_fEnsureCharSetInfoInitialized = FALSE; // true if we've detected the platform's character set characteristics
// Detect Unicode support of the operating system, and initialize globals
voidEnsureCharSetInfoInitialized()
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_FORBID_FAULT;
STATIC_CONTRACT_CANNOT_TAKE_LOCK;
if (!g_fEnsureCharSetInfoInitialized)
{
// NOTE: Do not use any of the Wsz* wrapper functions right now. They will have
// problems.
// Per Shupak, you're supposed to get the maximum size of a DBCS char
// dynamically to work properly on all locales (bug 2757).
CPINFO cpInfo;
if (GetCPInfo(CP_ACP, &cpInfo))
g_dwMaxDBCSCharByteSize = cpInfo.MaxCharSize;
else
g_dwMaxDBCSCharByteSize = 2;
VolatileStore(&g_fEnsureCharSetInfoInitialized, true);
}
return;
}
// Running with an interactive workstation.
BOOL RunningInteractive()
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_FORBID_FAULT;
staticintfInteractive = -1;
if (fInteractive != -1)
returnfInteractive != 0;
#if !defined(FEATURE_CORESYSTEM)
HWINSTA hwinsta = NULL;
if ((hwinsta = GetProcessWindowStation() ) != NULL)
{
DWORD lengthNeeded;
USEROBJECTFLAGS flags;
if (GetUserObjectInformationW (hwinsta, UOI_FLAGS, &flags, sizeof(flags), &lengthNeeded))
{
if ((flags.dwFlags & WSF_VISIBLE) == 0)
fInteractive = 0;
}
}
#endif// !FEATURE_CORESYSTEM
if (fInteractive != 0)
fInteractive = 1;
returnfInteractive != 0;
}
typedefHRESULT(WINAPI *pfnSetThreadDescription)(HANDLE hThread, PCWSTR lpThreadDescription);
extern pfnSetThreadDescription g_pfnSetThreadDescription;
// Dummy method if windows version does not support it
HRESULT SetThreadDescriptionDummy(HANDLE hThread, PCWSTR lpThreadDescription)
{
return NOERROR;
}
HRESULT WINAPI InitializeSetThreadDescription(HANDLE hThread, PCWSTR lpThreadDescription)
{
HMODULE hKernel32 = WszLoadLibrary(W("kernel32.dll"));
pfnSetThreadDescription pLocal = NULL;
if (hKernel32 != NULL)
{
// store to thread local variable to prevent data race
pLocal = (pfnSetThreadDescription)GetProcAddress(hKernel32, "SetThreadDescription");
}
if (pLocal == NULL) // method is only available with Windows 10 Creators Update or later
{
g_pfnSetThreadDescription = SetThreadDescriptionDummy;
}
else
{
g_pfnSetThreadDescription = pLocal;
}
returng_pfnSetThreadDescription(hThread, lpThreadDescription);
}
pfnSetThreadDescription g_pfnSetThreadDescription = &InitializeSetThreadDescription;
// Set unmanaged thread name which will show up in ETW and Debuggers which know how to read this data.
HRESULT SetThreadName(HANDLE hThread, PCWSTR lpThreadDescription)
{
returng_pfnSetThreadDescription(hThread, lpThreadDescription);
}
#else//!FEATURE_PAL
HRESULT SetThreadName(HANDLE hThread, PCWSTR lpThreadDescription)
{
returnSetThreadDescription(hThread, lpThreadDescription);
}
#endif//!FEATURE_PAL