1370b324cSopenharmony_ci// Windows/MemoryLock.cpp
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#include "../../C/CpuArch.h"
6370b324cSopenharmony_ci
7370b324cSopenharmony_ci#include "MemoryLock.h"
8370b324cSopenharmony_ci
9370b324cSopenharmony_cinamespace NWindows {
10370b324cSopenharmony_cinamespace NSecurity {
11370b324cSopenharmony_ci
12370b324cSopenharmony_ci#ifndef UNDER_CE
13370b324cSopenharmony_ci
14370b324cSopenharmony_ci#ifdef _UNICODE
15370b324cSopenharmony_ci#define MY_FUNC_SELECT(f) :: f
16370b324cSopenharmony_ci#else
17370b324cSopenharmony_ci#define MY_FUNC_SELECT(f) my_ ## f
18370b324cSopenharmony_ciextern "C" {
19370b324cSopenharmony_citypedef BOOL (WINAPI * Func_OpenProcessToken)(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle);
20370b324cSopenharmony_citypedef BOOL (WINAPI * Func_LookupPrivilegeValue)(LPCTSTR lpSystemName, LPCTSTR lpName, PLUID lpLuid);
21370b324cSopenharmony_citypedef BOOL (WINAPI * Func_AdjustTokenPrivileges)(HANDLE TokenHandle, BOOL DisableAllPrivileges,
22370b324cSopenharmony_ci    PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength);
23370b324cSopenharmony_ci}
24370b324cSopenharmony_ci
25370b324cSopenharmony_ci#define GET_PROC_ADDR(fff, name)  \
26370b324cSopenharmony_ci  const Func_ ## fff  my_ ## fff = Z7_GET_PROC_ADDRESS( \
27370b324cSopenharmony_ci        Func_ ## fff, hModule, name);
28370b324cSopenharmony_ci#endif
29370b324cSopenharmony_ci
30370b324cSopenharmony_cibool EnablePrivilege(LPCTSTR privilegeName, bool enable)
31370b324cSopenharmony_ci{
32370b324cSopenharmony_ci  bool res = false;
33370b324cSopenharmony_ci
34370b324cSopenharmony_ci  #ifndef _UNICODE
35370b324cSopenharmony_ci
36370b324cSopenharmony_ci  const HMODULE hModule = ::LoadLibrary(TEXT("advapi32.dll"));
37370b324cSopenharmony_ci  if (!hModule)
38370b324cSopenharmony_ci    return false;
39370b324cSopenharmony_ci
40370b324cSopenharmony_ci  GET_PROC_ADDR(
41370b324cSopenharmony_ci     OpenProcessToken,
42370b324cSopenharmony_ci    "OpenProcessToken")
43370b324cSopenharmony_ci  GET_PROC_ADDR(
44370b324cSopenharmony_ci     LookupPrivilegeValue,
45370b324cSopenharmony_ci    "LookupPrivilegeValueA")
46370b324cSopenharmony_ci  GET_PROC_ADDR(
47370b324cSopenharmony_ci     AdjustTokenPrivileges,
48370b324cSopenharmony_ci    "AdjustTokenPrivileges")
49370b324cSopenharmony_ci
50370b324cSopenharmony_ci  if (my_OpenProcessToken &&
51370b324cSopenharmony_ci      my_AdjustTokenPrivileges &&
52370b324cSopenharmony_ci      my_LookupPrivilegeValue)
53370b324cSopenharmony_ci
54370b324cSopenharmony_ci  #endif
55370b324cSopenharmony_ci
56370b324cSopenharmony_ci  {
57370b324cSopenharmony_ci    HANDLE token;
58370b324cSopenharmony_ci    if (MY_FUNC_SELECT(OpenProcessToken)(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
59370b324cSopenharmony_ci    {
60370b324cSopenharmony_ci      TOKEN_PRIVILEGES tp;
61370b324cSopenharmony_ci      if (MY_FUNC_SELECT(LookupPrivilegeValue)(NULL, privilegeName, &(tp.Privileges[0].Luid)))
62370b324cSopenharmony_ci      {
63370b324cSopenharmony_ci        tp.PrivilegeCount = 1;
64370b324cSopenharmony_ci        tp.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
65370b324cSopenharmony_ci        if (MY_FUNC_SELECT(AdjustTokenPrivileges)(token, FALSE, &tp, 0, NULL, NULL))
66370b324cSopenharmony_ci          res = (GetLastError() == ERROR_SUCCESS);
67370b324cSopenharmony_ci      }
68370b324cSopenharmony_ci      ::CloseHandle(token);
69370b324cSopenharmony_ci    }
70370b324cSopenharmony_ci  }
71370b324cSopenharmony_ci
72370b324cSopenharmony_ci  #ifndef _UNICODE
73370b324cSopenharmony_ci
74370b324cSopenharmony_ci  ::FreeLibrary(hModule);
75370b324cSopenharmony_ci
76370b324cSopenharmony_ci  #endif
77370b324cSopenharmony_ci
78370b324cSopenharmony_ci  return res;
79370b324cSopenharmony_ci}
80370b324cSopenharmony_ci
81370b324cSopenharmony_ci
82370b324cSopenharmony_ci
83370b324cSopenharmony_citypedef void (WINAPI * Func_RtlGetVersion) (OSVERSIONINFOEXW *);
84370b324cSopenharmony_ci
85370b324cSopenharmony_ci/*
86370b324cSopenharmony_ci  We suppose that Window 10 works incorrectly with "Large Pages" at:
87370b324cSopenharmony_ci    - Windows 10 1703 (15063) : incorrect allocating after VirtualFree()
88370b324cSopenharmony_ci    - Windows 10 1709 (16299) : incorrect allocating after VirtualFree()
89370b324cSopenharmony_ci    - Windows 10 1809 (17763) : the failures for blocks of 1 GiB and larger,
90370b324cSopenharmony_ci                                if CPU doesn't support 1 GB pages.
91370b324cSopenharmony_ci  Windows 10 1903 (18362) probably works correctly.
92370b324cSopenharmony_ci*/
93370b324cSopenharmony_ci
94370b324cSopenharmony_ciunsigned Get_LargePages_RiskLevel()
95370b324cSopenharmony_ci{
96370b324cSopenharmony_ci  OSVERSIONINFOEXW vi;
97370b324cSopenharmony_ci  const HMODULE ntdll = ::GetModuleHandleW(L"ntdll.dll");
98370b324cSopenharmony_ci  if (!ntdll)
99370b324cSopenharmony_ci    return 0;
100370b324cSopenharmony_ci  const
101370b324cSopenharmony_ci  Func_RtlGetVersion func = Z7_GET_PROC_ADDRESS(
102370b324cSopenharmony_ci  Func_RtlGetVersion, ntdll,
103370b324cSopenharmony_ci      "RtlGetVersion");
104370b324cSopenharmony_ci  if (!func)
105370b324cSopenharmony_ci    return 0;
106370b324cSopenharmony_ci  func(&vi);
107370b324cSopenharmony_ci  if (vi.dwPlatformId != VER_PLATFORM_WIN32_NT)
108370b324cSopenharmony_ci    return 0;
109370b324cSopenharmony_ci  if (vi.dwMajorVersion + vi.dwMinorVersion != 10)
110370b324cSopenharmony_ci    return 0;
111370b324cSopenharmony_ci  if (vi.dwBuildNumber <= 16299)
112370b324cSopenharmony_ci    return 1;
113370b324cSopenharmony_ci
114370b324cSopenharmony_ci  #ifdef MY_CPU_X86_OR_AMD64
115370b324cSopenharmony_ci  if (vi.dwBuildNumber < 18362 && !CPU_IsSupported_PageGB())
116370b324cSopenharmony_ci    return 1;
117370b324cSopenharmony_ci  #endif
118370b324cSopenharmony_ci
119370b324cSopenharmony_ci  return 0;
120370b324cSopenharmony_ci}
121370b324cSopenharmony_ci
122370b324cSopenharmony_ci#endif
123370b324cSopenharmony_ci
124370b324cSopenharmony_ci}}
125