VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/testcase/tstNtQueryStuff.cpp@ 43394

Last change on this file since 43394 was 43386, checked in by vboxsync, 13 years ago

exploring nt.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: tstNtQueryStuff.cpp 43386 2012-09-21 08:40:12Z vboxsync $ */
2/** @file
3 * SUP Testcase - Exploring some NT Query APIs.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <Windows.h>
32#include <winternl.h>
33
34typedef enum
35{
36 MemoryBasicInformation = 0,
37 MemoryWorkingSetList,
38 MemorySectionName,
39 MemoryBasicVlmInformation
40} MEMORY_INFORMATION_CLASS;
41
42typedef struct
43{
44 UNICODE_STRING SectionFileName;
45 WCHAR NameBuffer[ANYSIZE_ARRAY];
46} MEMORY_SECTION_NAME;
47
48extern "C"
49NTSYSAPI NTSTATUS NTAPI NtQueryVirtualMemory(IN HANDLE hProcess,
50 IN LPCVOID pvWhere,
51 IN MEMORY_INFORMATION_CLASS MemoryInfo,
52 OUT PVOID pvBuf,
53 IN SIZE_T cbBuf,
54 OUT PSIZE_T pcbReturned OPTIONAL);
55
56
57#include <iprt/test.h>
58#include <iprt/string.h>
59
60
61/*******************************************************************************
62* Structures and Typedefs *
63*******************************************************************************/
64typedef struct FLAGDESC
65{
66 ULONG f;
67 const char *psz;
68} FLAGDESC;
69typedef const FLAGDESC *PCFLAGDESC;
70
71
72
73/*******************************************************************************
74* Global Variables *
75*******************************************************************************/
76static RTTEST g_hTest = NIL_RTTEST;
77
78
79static char *stringifyAppend(char *pszBuf, size_t *pcbBuf, const char *pszAppend, bool fWithSpace)
80{
81 size_t cchAppend = strlen(pszAppend);
82 if (cchAppend + 1 + fWithSpace <= *pcbBuf)
83 {
84 if (fWithSpace)
85 {
86 *pszBuf++ = ' ';
87 *pcbBuf += 1;
88 }
89 memcpy(pszBuf, pszAppend, cchAppend + 1);
90 *pcbBuf -= cchAppend;
91 pszBuf += cchAppend;
92 }
93
94 return pszBuf;
95}
96
97
98static char *stringifyAppendUnknownFlags(uint32_t fFlags, char *pszBuf, size_t *pcbBuf, bool fWithSpace)
99{
100 for (unsigned iBit = 0; iBit < 32; iBit++)
101 if (fFlags & RT_BIT_32(iBit))
102 {
103 char szTmp[32]; /* lazy bird */
104 RTStrPrintf(szTmp, sizeof(szTmp), "BIT(%d)", iBit);
105 pszBuf = stringifyAppend(pszBuf, pcbBuf, szTmp, fWithSpace);
106 fWithSpace = true;
107 }
108
109 return pszBuf;
110}
111
112
113static char *stringifyFlags(uint32_t fFlags, char *pszBuf, size_t cbBuf, PCFLAGDESC paFlagDesc, size_t cFlagDesc)
114{
115 char *pszBufStart = pszBuf;
116 if (fFlags)
117 {
118 for (size_t i = 0; i < cFlagDesc; i++)
119 {
120 if (fFlags & paFlagDesc[i].f)
121 {
122 fFlags &= ~paFlagDesc[i].f;
123 pszBuf = stringifyAppend(pszBuf, &cbBuf, paFlagDesc[i].psz, pszBuf != pszBufStart);
124 }
125 }
126
127 if (fFlags)
128 stringifyAppendUnknownFlags(fFlags, pszBuf, &cbBuf, pszBuf != pszBufStart);
129 }
130 else
131 {
132 pszBuf[0] = '0';
133 pszBuf[1] = '\0';
134 }
135 return pszBufStart;
136}
137
138
139static char *stringifyMemType(uint32_t fType, char *pszBuf, size_t cbBuf)
140{
141 static const FLAGDESC s_aMemTypes[] =
142 {
143 { MEM_PRIVATE, "PRIVATE" },
144 { MEM_MAPPED, "MAPPED" },
145 { MEM_IMAGE, "IMAGE" },
146 };
147 return stringifyFlags(fType, pszBuf, cbBuf, s_aMemTypes, RT_ELEMENTS(s_aMemTypes));
148}
149
150
151static char *stringifyMemState(uint32_t fState, char *pszBuf, size_t cbBuf)
152{
153 static const FLAGDESC s_aMemStates[] =
154 {
155 { MEM_FREE, "FREE" },
156 { MEM_COMMIT, "COMMIT" },
157 { MEM_RESERVE, "RESERVE" },
158 { MEM_DECOMMIT, "DECOMMMIT" },
159 };
160 return stringifyFlags(fState, pszBuf, cbBuf, s_aMemStates, RT_ELEMENTS(s_aMemStates));
161}
162
163
164static char *stringifyMemProt(uint32_t fProt, char *pszBuf, size_t cbBuf)
165{
166 static const FLAGDESC s_aProtections[] =
167 {
168 { PAGE_NOACCESS, "NOACCESS" },
169 { PAGE_READONLY, "READONLY" },
170 { PAGE_READWRITE, "READWRITE" },
171 { PAGE_WRITECOPY, "WRITECOPY" },
172 { PAGE_EXECUTE, "EXECUTE" },
173 { PAGE_EXECUTE_READ, "EXECUTE_READ" },
174 { PAGE_EXECUTE_READWRITE, "EXECUTE_READWRITE" },
175 { PAGE_EXECUTE_WRITECOPY, "EXECUTE_WRITECOPY" },
176 { PAGE_GUARD, "GUARD" },
177 { PAGE_NOCACHE, "NOCACHE" },
178 { PAGE_WRITECOMBINE, "WRITECOMBINE" },
179
180 };
181 return stringifyFlags(fProt, pszBuf, cbBuf, s_aProtections, RT_ELEMENTS(s_aProtections));
182}
183
184
185
186static void tstQueryVirtualMemory(void)
187{
188 RTTestISub("NtQueryVirtualMemory");
189
190
191 uintptr_t cbAdvance = 0;
192 uintptr_t uPtrWhere = 0;
193 for (;;)
194 {
195 SIZE_T cbActual = 0;
196 MEMORY_BASIC_INFORMATION MemInfo = { 0, 0, 0, 0, 0, 0, 0 };
197 NTSTATUS ntRc = NtQueryVirtualMemory(GetCurrentProcess(),
198 (void const *)uPtrWhere,
199 MemoryBasicInformation,
200 &MemInfo,
201 sizeof(MemInfo),
202 &cbActual);
203 if (!NT_SUCCESS(ntRc))
204 {
205 RTTestIPrintf(RTTESTLVL_ALWAYS, "%p: ntRc=%#x\n", uPtrWhere, ntRc);
206 break;
207 }
208
209 /* stringify the memory state. */
210 char szMemType[1024];
211 char szMemState[1024];
212 char szMemProt[1024];
213
214 RTTestIPrintf(RTTESTLVL_ALWAYS, "%p-%p %-8s %-8s %-12s\n",
215 MemInfo.BaseAddress, (uintptr_t)MemInfo.BaseAddress + MemInfo.RegionSize - 1,
216 stringifyMemType(MemInfo.Type, szMemType, sizeof(szMemType)),
217 stringifyMemState(MemInfo.State, szMemState, sizeof(szMemState)),
218 stringifyMemProt(MemInfo.Protect, szMemProt, sizeof(szMemProt))
219 );
220
221 if ((uintptr_t)MemInfo.BaseAddress != uPtrWhere)
222 RTTestIPrintf(RTTESTLVL_ALWAYS, " !Warning! Queried %p got BaseAddress=%p!\n",
223 uPtrWhere, MemInfo.BaseAddress);
224
225 cbAdvance = MemInfo.RegionSize;
226 //cbAdvance = 0;
227 if (uPtrWhere + cbAdvance <= uPtrWhere)
228 break;
229 uPtrWhere += MemInfo.RegionSize;
230 }
231}
232
233
234
235//NtQueryInformationProcess
236
237int main()
238{
239 RTEXITCODE rcExit = RTTestInitAndCreate("tstNtQueryStuff", &g_hTest);
240 if (rcExit != RTEXITCODE_SUCCESS)
241 return rcExit;
242 RTTestBanner(g_hTest);
243
244 tstQueryVirtualMemory();
245
246
247 return RTTestSummaryAndDestroy(g_hTest);
248}
249
250
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette