ReactOS  0.4.13-dev-99-g7e18b6d
pageheap.c
Go to the documentation of this file.
1 /*
2  * PROJECT: Global Flags utility
3  * LICENSE: GPL-2.0 (https://spdx.org/licenses/GPL-2.0)
4  * PURPOSE: Global Flags utility page heap options
5  * COPYRIGHT: Copyright 2017 Pierre Schweitzer (pierre@reactos.org)
6  */
7 
8 #include "gflags.h"
9 
10 static BOOL Set = FALSE;
11 static BOOL Unset = FALSE;
12 static BOOL Full = FALSE;
13 static PWSTR Image = NULL;
14 
15 
17 {
18  LONG Ret;
20  HKEY IFEOKey;
21  WCHAR Buffer[11];
22 
24  {
25  return;
26  }
27 
28  GlobalFlags = ReadSZFlagsFromRegistry(IFEOKey, L"GlobalFlag");
29  if (Set)
30  {
32  }
33  else
34  {
36  }
37 
38  if (GlobalFlags != 0)
39  {
40  wsprintf(Buffer, L"0x%08x", GlobalFlags);
41  Ret = RegSetValueExW(IFEOKey, L"GlobalFlag", 0, REG_SZ, (BYTE*)Buffer, (wcslen(Buffer) + 1) * sizeof(WCHAR));
42  if (Ret != ERROR_SUCCESS)
43  {
44  wprintf(L"MS: RegSetValueEx failed (%d)\n", Ret);
45  }
46  }
47  else
48  {
49  Ret = RegDeleteValueW(IFEOKey, L"GlobalFlag");
50  if (Ret != ERROR_SUCCESS)
51  {
52  wprintf(L"MS: RegDeleteValue failed (%d)\n", Ret);
53  }
54  }
55 
56  if (Unset)
57  {
58  Ret = RegDeleteValueW(IFEOKey, L"PageHeapFlags");
59  if (Ret != ERROR_SUCCESS)
60  {
61  wprintf(L"MS: RegDeleteValue failed (%d)\n", Ret);
62  }
63  }
64  else
65  {
66  DWORD PageHeapFlags;
67 
68  PageHeapFlags = ReadSZFlagsFromRegistry(IFEOKey, L"PageHeapFlags");
69  PageHeapFlags &= ~3;
70 
71  if (Full)
72  {
73  PageHeapFlags |= 1;
74  }
75  PageHeapFlags |= 2;
76 
77  wsprintf(Buffer, L"0x%x", PageHeapFlags);
78  Ret = RegSetValueExW(IFEOKey, L"PageHeapFlags", 0, REG_SZ, (BYTE*)Buffer, (wcslen(Buffer) + 1) * sizeof(WCHAR));
79  if (Ret != ERROR_SUCCESS)
80  {
81  wprintf(L"MS: RegSetValueEx failed (%d)\n", Ret);
82  }
83  }
84 
85  if (Set)
86  {
87  DWORD Type, VerifierFlags, Len;
88 
89  VerifierFlags = 0;
90  Len = VerifierFlags;
91  if (RegQueryValueExW(IFEOKey, L"VerifierFlags", NULL, &Type, (BYTE *)&VerifierFlags, &Len) == ERROR_SUCCESS &&
92  Type == REG_DWORD && Len == sizeof(DWORD))
93  {
94  VerifierFlags &= ~0x8001; /* RTL_VRF_FLG_FAST_FILL_HEAP | RTL_VRF_FLG_FULL_PAGE_HEAP */
95  }
96  else
97  {
98  VerifierFlags = 0;
99  }
100 
101  if (Full)
102  {
103  VerifierFlags |= 1; /* RTL_VRF_FLG_FULL_PAGE_HEAP */
104  }
105  else
106  {
107  VerifierFlags |= 0x8000; /* RTL_VRF_FLG_FAST_FILL_HEAP */
108  }
109 
110  Ret = RegSetValueExW(IFEOKey, L"VerifierFlags", 0, REG_DWORD, (const BYTE *)&VerifierFlags, sizeof(DWORD));
111  if (Ret != ERROR_SUCCESS)
112  {
113  wprintf(L"MS: RegSetValueEx failed (%d)\n", Ret);
114  }
115  }
116 
117  wprintf(L"path: %s\n", ImageExecOptionsString);
118  wprintf(L"\t%s: page heap %s\n", Image, (Set ? L"enabled" : L"disabled"));
119 
121  RegCloseKey(IFEOKey);
122 }
123 
124 static BOOL DisplayImageInfo(HKEY HandleKey, PWSTR SubKey, PBOOL Header)
125 {
126  LONG Ret;
127  BOOL Handled;
129  HKEY HandleSubKey;
130 
131  Ret = RegOpenKeyExW(HandleKey, SubKey, 0, KEY_READ, &HandleSubKey);
132  if (Ret != ERROR_SUCCESS)
133  {
134  wprintf(L"DII: RegOpenKeyEx failed (%d)\n", Ret);
135  return FALSE;
136  }
137 
138  Handled = FALSE;
139  GlobalFlags = ReadSZFlagsFromRegistry(HandleSubKey, L"GlobalFlag");
141  {
142  DWORD PageHeapFlags;
143 
144  if (Image == NULL)
145  {
146  if (!*Header)
147  {
148  wprintf(L"path: %s\n", ImageExecOptionsString);
149  *Header = TRUE;
150  }
151  wprintf(L"\t%s: page heap enabled with flags (", SubKey);
152  }
153  else
154  {
155  wprintf(L"Page heap is enabled for %s with flags (", SubKey);
156  }
157 
158  PageHeapFlags = ReadSZFlagsFromRegistry(HandleSubKey, L"PageHeapFlags");
159  if (PageHeapFlags & 0x1)
160  {
161  wprintf(L"full ");
162  }
163 
164  if (PageHeapFlags & 0x2)
165  {
166  wprintf(L"traces");
167  }
168 
169  wprintf(L")\n");
170 
171  Handled = TRUE;
172  }
173 
174  RegCloseKey(HandleSubKey);
175 
176  return Handled;
177 }
178 
180 {
181  LONG Ret;
182  HKEY HandleKey;
183  DWORD Index, MaxLen, Handled;
184  TCHAR * SubKey;
185  BOOL Header;
186 
187  if (!OpenImageFileExecOptions(KEY_READ, NULL, &HandleKey))
188  {
189  return;
190  }
191 
192  Ret = RegQueryInfoKeyW(HandleKey, NULL, NULL, NULL, NULL, &MaxLen, NULL, NULL, NULL, NULL, NULL, NULL);
193  if (Ret != ERROR_SUCCESS)
194  {
195  wprintf(L"DS: RegQueryInfoKey failed (%d)\n", Ret);
196  RegCloseKey(HandleKey);
197  return;
198  }
199 
200  ++MaxLen; // NULL-char
201  SubKey = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MaxLen * sizeof(WCHAR));
202  if (SubKey == NULL)
203  {
204  wprintf(L"DS: HeapAlloc failed\n");
205  RegCloseKey(HandleKey);
206  return;
207  }
208 
209  Index = 0;
210  Handled = 0;
211  Header = FALSE;
212  do
213  {
214  Ret = RegEnumKeyW(HandleKey, Index, SubKey, MaxLen);
215  if (Ret != ERROR_NO_MORE_ITEMS)
216  {
217  if (Image == NULL || wcscmp(SubKey, Image) == 0)
218  {
219  if (DisplayImageInfo(HandleKey, SubKey, &Header))
220  {
221  ++Handled;
222  }
223  }
224 
225  ++Index;
226  }
227  } while (Ret != ERROR_NO_MORE_ITEMS);
228 
229  if (Handled == 0)
230  {
231  if (Image == NULL)
232  {
233  wprintf(L"No application has page heap enabled.\n");
234  }
235  else
236  {
237  wprintf(L"Page heap is not enabled for %s\n", Image);
238  }
239  }
240 
241  HeapFree(GetProcessHeap(), 0, SubKey);
242  RegCloseKey(HandleKey);
243 }
244 
246 {
247  for (; i < argc; i++)
248  {
249  if (argv[i][0] == L'/')
250  {
251  if (wcscmp(argv[i], L"/enable") == 0)
252  {
253  Set = TRUE;
254  }
255  else if (wcscmp(argv[i], L"/disable") == 0)
256  {
257  Unset = TRUE;
258  }
259  else if (wcscmp(argv[i], L"/full") == 0)
260  {
261  Full = TRUE;
262  }
263  }
264  else if (Image == NULL)
265  {
266  Image = argv[i];
267  }
268  else
269  {
270  wprintf(L"Invalid option: %s\n", argv[i]);
271  return FALSE;
272  }
273  }
274 
275  if (Set && Unset)
276  {
277  wprintf(L"ENABLE and DISABLED cannot be set together\n");
278  return FALSE;
279  }
280 
281  if (Image == NULL && (Set || Unset || Full))
282  {
283  wprintf(L"Can't ENABLE or DISABLE with no image\n");
284  return FALSE;
285  }
286 
287  if (!Set && !Unset && Full)
288  {
289  wprintf(L"Cannot deal with full traces with no other indication\n");
290  return FALSE;
291  }
292 
293  return TRUE;
294 }
295 
297 {
298  if (!Set && !Unset)
299  {
300  DisplayStatus();
301  }
302  else
303  {
304  ModifyStatus();
305  }
306 
307  return 0;
308 }
UNICODE_STRING ImageExecOptionsString
Definition: ldrinit.c:24
static int argc
Definition: ServiceArgs.c:12
#define TRUE
Definition: types.h:120
static VOID ModifyStatus(VOID)
Definition: pageheap.c:16
Type
Definition: Type.h:6
#define ERROR_SUCCESS
Definition: deptool.c:10
#define ERROR_NO_MORE_ITEMS
Definition: compat.h:95
#define KEY_READ
Definition: nt_native.h:1023
uint16_t * PWSTR
Definition: typedefs.h:54
UINT NTAPI GlobalFlags(HGLOBAL hMem)
Definition: heapmem.c:520
static BOOL Unset
Definition: pageheap.c:11
_In_ CLIPOBJ _In_ BRUSHOBJ _In_ LONG x1
Definition: winddi.h:3706
#define wprintf(...)
Definition: whoami.c:18
#define argv
Definition: mplay32.c:18
int32_t INT
Definition: typedefs.h:56
static BOOL Set
Definition: pageheap.c:10
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
Definition: Header.h:8
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
BOOL PageHeap_ParseCmdline(INT i, int argc, LPWSTR argv[])
Definition: pageheap.c:245
smooth NULL
Definition: ftsmooth.c:416
Definition: bufpool.h:45
BOOL * PBOOL
Definition: windef.h:161
BOOL OpenImageFileExecOptions(IN REGSAM SamDesired, IN OPTIONAL PCWSTR ImageName, OUT HKEY *Key)
Definition: gflags.c:16
#define KEY_WRITE
Definition: nt_native.h:1031
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4917
char TCHAR
Definition: xmlstorage.h:189
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4134
_In_ BOOLEAN Handled
Definition: ketypes.h:337
__wchar_t WCHAR
Definition: xmlstorage.h:180
static BOOL Full
Definition: pageheap.c:12
#define FLG_HEAP_PAGE_ALLOCS
Definition: pstypes.h:84
static const UCHAR Index[8]
Definition: usbohci.c:18
DWORD ReadSZFlagsFromRegistry(HKEY SubKey, PWSTR Value)
Definition: gflags.c:47
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
LONG WINAPI RegQueryInfoKeyW(HKEY hKey, LPWSTR lpClass, LPDWORD lpcClass, LPDWORD lpReserved, LPDWORD lpcSubKeys, LPDWORD lpcMaxSubKeyLen, LPDWORD lpcMaxClassLen, LPDWORD lpcValues, LPDWORD lpcMaxValueNameLen, LPDWORD lpcMaxValueLen, LPDWORD lpcbSecurityDescriptor, PFILETIME lpftLastWriteTime)
Definition: reg.c:3704
static const WCHAR L[]
Definition: oid.c:1250
static VOID DisplayStatus(VOID)
Definition: pageheap.c:179
unsigned char BYTE
Definition: mem.h:68
LONG WINAPI RegEnumKeyW(HKEY hKey, DWORD dwIndex, LPWSTR lpName, DWORD cbName)
Definition: reg.c:2430
_Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
LONG WINAPI RegDeleteValueW(HKEY hKey, LPCWSTR lpValueName)
Definition: reg.c:2368
INT PageHeap_Execute()
Definition: pageheap.c:296
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
_In_ CLIPOBJ _In_ BRUSHOBJ _In_ LONG _In_ LONG _In_ LONG x2
Definition: winddi.h:3706
static BOOL DisplayImageInfo(HKEY HandleKey, PWSTR SubKey, PBOOL Header)
Definition: pageheap.c:124
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3381
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define wsprintf
Definition: winuser.h:5731
#define REG_DWORD
Definition: sdbapi.c:596
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:394
#define REG_SZ
Definition: layer.c:22