summaryrefslogtreecommitdiff
path: root/src/utilcode/winfix.cpp
blob: c914fb6edd4909e9de4621c9fba468620e5287dd (plain)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// 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
#define CONTRACT $$$$$$$$READ_COMMENT_IN_WINFIX_CPP$$$$$$$$$$

#undef CONTRACTL
#define CONTRACTL $$$$$$$$READ_COMMENT_IN_WINFIX_CPP$$$$$$$$$$

#ifdef ENABLE_CONTRACTS_IMPL
static BOOL gWinWrapperContractRecursionBreak = FALSE;


class WinWrapperContract
{
    public:
        WinWrapperContract(char *szFunction, char *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
#define WINWRAPPER_NO_CONTRACT(stmt) \
    STATIC_CONTRACT_NOTHROW;      \
    STATIC_CONTRACT_FAULT;        \
    STATIC_CONTRACT_CANNOT_TAKE_LOCK;  \
    WinWrapperContract __wcontract(__FUNCTION__, __FILE__, __LINE__); \
    if (0) {stmt}  \
        
#define STATIC_WINWRAPPER_NO_CONTRACT(stmt) \
    STATIC_CONTRACT_NOTHROW;      \
    STATIC_CONTRACT_CANNOT_TAKE_LOCK;  \
    STATIC_CONTRACT_FAULT;        \
    if (0) {stmt}  \
        
        
#else
#define WINWRAPPER_NO_CONTRACT(stmt)
#define STATIC_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); return 0;);

    BOOL fResult;
    DWORD err;
    {
        size_t commandLineLength = wcslen(lpCommandLine) + 1;
        NewArrayHolder<WCHAR> nonConstCommandLine(new (nothrow) WCHAR[commandLineLength]);
        if (nonConstCommandLine == NULL)
        {
            SetLastError(ERROR_OUTOFMEMORY);
            return 0;
        }
            
        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);
    return fResult;
}

#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
void EnsureCharSetInfoInitialized()
{
    STATIC_CONTRACT_NOTHROW;
    STATIC_CONTRACT_FORBID_FAULT;
    STATIC_CONTRACT_CANNOT_TAKE_LOCK;
    STATIC_CONTRACT_SO_TOLERANT;

    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;

    static int fInteractive = -1;
    if (fInteractive != -1)
        return fInteractive != 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;

    return fInteractive != 0;
}


// Wrapper function around CheckTokenMembership to determine if the token enables the SID "S-1-5-<rid>".
// If hToken is NULL, this function uses the thread's impersonation token. If the thread is not impersonating, the 
// process token is used.
//
// If the function succeeds, it returns ERROR_SUCCESS, else it returns the error code returned by GetLastError()
static DWORD TokenEnablesSID(IN HANDLE hToken OPTIONAL, IN DWORD rid, OUT BOOL& fResult)
{
    DWORD dwError;
    SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
    PSID pSid = NULL;
    HMODULE hAdvApi32 = NULL;
    typedef BOOL (WINAPI *CheckTokenMembership_t)(HANDLE TokenHandle, PSID SidToCheck, PBOOL IsMember);
    CheckTokenMembership_t pfnCheckTokenMembership = NULL;

    hAdvApi32 = WszGetModuleHandle(W("advapi32.dll"));
    if (hAdvApi32 == NULL)
    {
        dwError = ERROR_MOD_NOT_FOUND;
        goto lExit;
    }
    pfnCheckTokenMembership = (CheckTokenMembership_t) GetProcAddress(hAdvApi32, "CheckTokenMembership");
    if (pfnCheckTokenMembership == NULL)
    {
        dwError = GetLastError();
        goto lExit;
    }

    fResult = FALSE;
    if (!AllocateAndInitializeSid(&SIDAuthNT, 1, rid, 0, 0, 0, 0, 0, 0, 0, &pSid))
    {
        dwError = GetLastError();
        goto lExit;
    }
    if (!pfnCheckTokenMembership(hToken, pSid, &fResult))
    {
        dwError = GetLastError();
        goto lExit;
    }
    dwError = ERROR_SUCCESS;

lExit:
    if (pSid) FreeSid(pSid);
    return dwError;
    
}

// Determines if the process is running as Local System or as a service. Note that
// the function attempts to determine the process' identity and not the thread's 
// (if the thread is impersonating).
//
// Parameters:
//    fIsLocalSystemOrService - TRUE if the function succeeds and the process is
//                              running as SYSTEM or as a service
//
// Return value:
//
// If the function succeeds, it returns ERROR_SUCCESS, else it returns the error
// code returned by GetLastError()
//
// Notes:
// 
// This function will generally fail if the calling thread is impersonating at the 
// ANONYMOUS level; see the comments in the function.
//
DWORD RunningAsLocalSystemOrService(OUT BOOL& fIsLocalSystemOrService)
{
    STATIC_CONTRACT_NOTHROW;
    STATIC_CONTRACT_FORBID_FAULT;

    static int fLocalSystemOrService = -1;
    if (fLocalSystemOrService != -1)
    {
        fIsLocalSystemOrService = fLocalSystemOrService != 0;
        return ERROR_SUCCESS;
    }

    DWORD dwError;
    HANDLE hThreadToken = NULL;
    HANDLE hProcessToken = NULL;
    HANDLE hDuplicatedProcessToken = NULL;
    BOOL fLocalSystem = FALSE;
    BOOL fService = FALSE;
    BOOL bReverted = FALSE;

    if (OpenThreadToken(GetCurrentThread(), TOKEN_IMPERSONATE, TRUE, &hThreadToken))
    {
        if (RevertToSelf())
        {
            bReverted = TRUE;
        }
#ifdef _DEBUG
        else
        {
            // For debugging only, continue as the impersonated user; see comment below
            dwError = GetLastError();
        }
#endif // #ifdef _DEBUG
    }
#ifdef _DEBUG
    else
    {
        dwError = GetLastError();
        if (dwError == ERROR_NO_IMPERSONATION_TOKEN || dwError == ERROR_NO_TOKEN)
        {
            // The thread is not impersonating; it's safe to continue
        }
        else
        {
            // The thread could be impersonating, but we won't be able to restore the impersonation
            // token if we RevertToSelf(). Continue as the impersonated user. OpenProcessToken will
            // fail (unless the impersonated user is SYSTEM or the same as the process' user).
            //
            // Note that this case will occur if the impersonation level is ANONYMOUS, the error
            // code will be ERROR_CANT_OPEN_ANONYMOUS. 
        }
    }
#endif // #ifdef _DEBUG

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &hProcessToken))
    {
        dwError = GetLastError();
        goto lExit;
    }
        
    if (!DuplicateToken(hProcessToken, SecurityImpersonation, &hDuplicatedProcessToken))
    {
        dwError = GetLastError();
        goto lExit;
    }

    dwError = TokenEnablesSID(hDuplicatedProcessToken, SECURITY_LOCAL_SYSTEM_RID, fLocalSystem);
    if (dwError != ERROR_SUCCESS)
    {
        goto lExit;
    }
    if (fLocalSystem)
    {
        goto lExit;
    }

    dwError = TokenEnablesSID(hDuplicatedProcessToken, SECURITY_SERVICE_RID, fService);
    
lExit:
    if (bReverted)
    {
        if (!SetThreadToken(NULL, hThreadToken))
        {
            DWORD dwLastError = GetLastError();
            _ASSERT("SetThreadToken failed");

            TerminateProcess(GetCurrentProcess(), dwLastError);
        }
    }
    
    if (hThreadToken) CloseHandle(hThreadToken);
    if (hProcessToken) CloseHandle(hProcessToken);
    if (hDuplicatedProcessToken) CloseHandle(hDuplicatedProcessToken);
    
    if (dwError != ERROR_SUCCESS)
    {
        fIsLocalSystemOrService = FALSE; // We don't really know
    }
    else
    {
        fLocalSystemOrService = (fLocalSystem || fService)? 1 : 0;
        fIsLocalSystemOrService = fLocalSystemOrService != 0;
    }
        
    return dwError;
    
}

typedef HRESULT(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;
    }

    return g_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)
{
    return g_pfnSetThreadDescription(hThread, lpThreadDescription);
}

DWORD
WszGetWorkingSet()
{
    WINWRAPPER_NO_CONTRACT(SetLastError(ERROR_OUTOFMEMORY); return 0;);

    DWORD dwMemUsage = 0;

    // Consider also calling GetProcessWorkingSetSize to get the min & max working
    // set size.  I don't know how to get the current working set though...
    PROCESS_MEMORY_COUNTERS pmc;

    HINSTANCE hPSapi;
    typedef BOOL (GET_PROCESS_MEMORY_INFO)(HANDLE, PROCESS_MEMORY_COUNTERS*, DWORD);
    GET_PROCESS_MEMORY_INFO* pGetProcessMemoryInfo;

    hPSapi = WszLoadLibrary(W("psapi.dll"));
    if (hPSapi == NULL) {
        _ASSERTE(0);
        return 0;
    }

    pGetProcessMemoryInfo =
        (GET_PROCESS_MEMORY_INFO*)GetProcAddress(hPSapi, "GetProcessMemoryInfo");
    // 403746: Prefix correctly complained about
    // pGetProcessMemoryInfo != NULL assertion.
    if (pGetProcessMemoryInfo == NULL) {
        _ASSERTE(0);
        FreeLibrary(hPSapi);
        return 0;
    }
    PREFIX_ASSUME(pGetProcessMemoryInfo != NULL);

    BOOL r = pGetProcessMemoryInfo(GetCurrentProcess(), &pmc, (DWORD) sizeof(PROCESS_MEMORY_COUNTERS));
    FreeLibrary(hPSapi);
    _ASSERTE(r);

    dwMemUsage = (DWORD)pmc.WorkingSetSize;

    return dwMemUsage;
}


SIZE_T
WszGetPagefileUsage()
{
    WINWRAPPER_NO_CONTRACT(SetLastError(ERROR_OUTOFMEMORY); return 0;);

    SIZE_T dwPagefileUsage = 0;

    typedef BOOL (WINAPI FnGetProcessMemoryInfo)(HANDLE, PROCESS_MEMORY_COUNTERS *, DWORD);

    HMODULE hPSApi = WszLoadLibrary(W("Psapi.dll"));
    if (hPSApi== NULL)
    {
        return 0;
    }

    FnGetProcessMemoryInfo *pfnGetProcessMemoryInfo = reinterpret_cast<FnGetProcessMemoryInfo *>(
        GetProcAddress(hPSApi, "GetProcessMemoryInfo"));

    if (pfnGetProcessMemoryInfo != NULL)
    {
        PROCESS_MEMORY_COUNTERS pmc;
        ZeroMemory(&pmc, sizeof(pmc));

        if (pfnGetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
        {
            dwPagefileUsage = pmc.PagefileUsage;
        }
    }

    FreeLibrary(hPSApi);
    hPSApi = NULL;

    return dwPagefileUsage;
}

DWORD
WszGetProcessHandleCount()
{
    WINWRAPPER_NO_CONTRACT(SetLastError(ERROR_OUTOFMEMORY); return 0;);

    DWORD dwHandleCount = 0;

    if (!GetProcessHandleCount(GetCurrentProcess(), &dwHandleCount))
    {
        dwHandleCount = 0;
    }

    return dwHandleCount;
}

#endif //!FEATURE_PAL