ReactOS 0.4.15-dev-7942-gd23573b
support.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: LGPL-2.1+ (https://spdx.org/licenses/LGPL-2.1+)
4 * PURPOSE: Kernel-Mode Test Suite user-mode support routines
5 * COPYRIGHT: Copyright 2011-2020 Thomas Faber <thomas.faber@reactos.org>
6 * Copyright 2013 Nikolay Borisov <nib9@aber.ac.uk>
7 * Copyright 2018 Serge Gautherie <reactos-git_serge_171003@gautherie.fr>
8 */
9
10#include <kmt_test.h>
11
12#include "kmtest.h"
13#include <kmt_public.h>
14
15#include <assert.h>
16#include <debug.h>
17
18extern HANDLE KmtestHandle;
19
31{
33 /* TODO: RequestPacket? */
34 KMT_CALLBACK_REQUEST_PACKET RequestPacket;
37 HANDLE LocalKmtHandle;
38
40
41 /* concurrent IoCtls on the same (non-overlapped) handle aren't possible,
42 * so open a separate one.
43 * For more info http://www.osronline.com/showthread.cfm?link=230782 */
45 if (LocalKmtHandle == INVALID_HANDLE_VALUE)
47
48 while (1)
49 {
50 if (!DeviceIoControl(LocalKmtHandle, IOCTL_KMTEST_USERMODE_AWAIT_REQ, NULL, 0, &RequestPacket, sizeof(RequestPacket), &BytesReturned, NULL))
52 ASSERT(BytesReturned == sizeof(RequestPacket));
53
54 switch (RequestPacket.OperationClass)
55 {
57 {
58 SIZE_T InfoBufferSize = VirtualQuery(RequestPacket.Parameters, &Response.MemInfo, sizeof(Response.MemInfo));
59 /* FIXME: an error is a valid result. That should go as a response to kernel mode instead of terminating the thread */
60 if (InfoBufferSize == 0)
62
63 if (!DeviceIoControl(LocalKmtHandle, IOCTL_KMTEST_USERMODE_SEND_RESPONSE, &RequestPacket.RequestId, sizeof(RequestPacket.RequestId), &Response, sizeof(Response), &BytesReturned, NULL))
66
67 break;
68 }
69 default:
70 DPRINT1("Unrecognized user-mode callback request\n");
71 break;
72 }
73 }
74
76 if (LocalKmtHandle != INVALID_HANDLE_VALUE)
77 CloseHandle(LocalKmtHandle);
78
79 DPRINT("Callback handler dying! Error code %lu", Error);
80 return Error;
81}
82
83
96 IN PCSTR TestName)
97{
98 HANDLE CallbackThread;
101
102 CallbackThread = CreateThread(NULL, 0, KmtUserCallbackThread, NULL, 0, NULL);
103
105 error(Error);
106
107 if (CallbackThread != NULL)
108 CloseHandle(CallbackThread);
109
110 return Error;
111}
112
114static SC_HANDLE TestServiceHandle;
116
127DWORD
130 IN BOOLEAN RestartIfRunning)
131{
132 WCHAR ServicePath[MAX_PATH];
133
134 StringCbCopyW(ServicePath, sizeof(ServicePath), ServiceName);
135 StringCbCatW(ServicePath, sizeof(ServicePath), L"_drv.sys");
136
139
140 return KmtCreateAndStartService(TestServiceName, ServicePath, NULL, &TestServiceHandle, RestartIfRunning);
141}
142
148VOID
150{
151 DWORD Error;
152
154
155 if (Error)
156 {
157 fprintf(stderr, "Failed to stop %ls service with error 0x%lx\n", TestServiceName, Error);
158 }
159}
160
166VOID
168{
169 DWORD Error;
170
172
173 if (Error)
174 {
175 fprintf(stderr, "Failed to stop %ls service with error 0x%lx\n", TestServiceName, Error);
176 }
177
179
180 if (Error)
181 {
182 fprintf(stderr, "Failed to delete %ls service with error 0x%lx\n", TestServiceName, Error);
183 }
184}
185
191DWORD
193{
195 WCHAR DevicePath[MAX_PATH];
196
197 StringCbCopyW(DevicePath, sizeof(DevicePath), L"\\\\.\\Global\\GLOBALROOT\\Device\\");
198 StringCbCatW(DevicePath, sizeof(DevicePath), TestServiceName);
199
202 error(Error);
203
204 return Error;
205}
206
212DWORD
215 IN BOOLEAN RestartIfRunning)
216{
217 DWORD Error;
218
219 Error = KmtLoadDriver(ServiceName, RestartIfRunning);
220 if (Error)
221 return Error;
222
224 if (Error)
225 return Error;
226
227 return ERROR_SUCCESS;
228}
229
235VOID
237{
239
241 error(Error);
242
243 if (Error)
244 {
245 DPRINT1("CloseHandle failed: 0x%lx\n", Error);
246 }
247}
248
258DWORD
261{
263
264 assert(ControlCode < 0x400);
265
267 return GetLastError();
268
269 return ERROR_SUCCESS;
270}
271
282DWORD
286{
288
289 assert(ControlCode < 0x400);
290
292 return GetLastError();
293
294 return ERROR_SUCCESS;
295}
296
307DWORD
311{
313
314 assert(ControlCode < 0x400);
315
317 return GetLastError();
318
319 return ERROR_SUCCESS;
320}
321
332DWORD
335 IN DWORD Value)
336{
338
339 assert(ControlCode < 0x400);
340
342 return GetLastError();
343
344 return ERROR_SUCCESS;
345}
346
359DWORD
363 IN DWORD InLength,
364 IN OUT PDWORD OutLength)
365{
366 assert(OutLength);
367 assert(Buffer || (!InLength && !*OutLength));
368 assert(ControlCode < 0x400);
369
370 if (!DeviceIoControl(TestDeviceHandle, KMT_MAKE_CODE(ControlCode), Buffer, InLength, Buffer, *OutLength, OutLength, NULL))
371 return GetLastError();
372
373 return ERROR_SUCCESS;
374}
unsigned char BOOLEAN
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define DPRINT1
Definition: precomp.h:8
static WCHAR ServiceName[]
Definition: browser.c:19
BOOL Error
Definition: chkdsk.c:66
Definition: bufpool.h:45
#define ERROR_SUCCESS
Definition: deptool.c:10
BOOL WINAPI DeviceIoControl(IN HANDLE hDevice, IN DWORD dwIoControlCode, IN LPVOID lpInBuffer OPTIONAL, IN DWORD nInBufferSize OPTIONAL, OUT LPVOID lpOutBuffer OPTIONAL, IN DWORD nOutBufferSize OPTIONAL, OUT LPDWORD lpBytesReturned OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: deviceio.c:136
#define NULL
Definition: types.h:112
#define CloseHandle
Definition: compat.h:739
#define OPEN_EXISTING
Definition: compat.h:775
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GENERIC_READ
Definition: compat.h:135
#define MAX_PATH
Definition: compat.h:34
static void cleanup(void)
Definition: main.c:1335
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:137
#define assert(x)
Definition: debug.h:53
unsigned long DWORD
Definition: ntddk_ex.h:95
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define KMTEST_DEVICE_PATH
Definition: kmt_public.h:35
#define IOCTL_KMTEST_USERMODE_AWAIT_REQ
Definition: kmt_public.h:25
#define IOCTL_KMTEST_USERMODE_SEND_RESPONSE
Definition: kmt_public.h:22
#define IOCTL_KMTEST_RUN_TEST
Definition: kmt_public.h:16
#define KMT_MAKE_CODE(ControlCode)
Definition: kmt_test.h:270
@ QueryVirtualMemory
Definition: kmt_test.h:51
#define error_goto(Error, label)
Definition: kmtest.h:21
DWORD KmtDeleteService(IN PCWSTR ServiceName OPTIONAL, IN OUT SC_HANDLE *ServiceHandle)
Definition: service.c:356
DWORD KmtStopService(IN PCWSTR ServiceName OPTIONAL, IN OUT SC_HANDLE *ServiceHandle)
Definition: service.c:315
DWORD KmtCreateAndStartService(IN PCWSTR ServiceName, IN PCWSTR ServicePath, IN PCWSTR DisplayName OPTIONAL, OUT SC_HANDLE *ServiceHandle, IN BOOLEAN RestartIfRunning)
Definition: service.c:262
#define error(str)
Definition: mkdosfs.c:1605
#define ASSERT(a)
Definition: mode.c:44
#define GENERIC_WRITE
Definition: nt_native.h:90
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
#define L(x)
Definition: ntvdm.h:50
DWORD * PDWORD
Definition: pedump.c:68
#define DPRINT
Definition: sndvol32.h:71
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
STRSAFEAPI StringCbCopyW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:166
STRSAFEAPI StringCbCatW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:342
Definition: ncftp.h:89
KMT_CALLBACK_INFORMATION_CLASS OperationClass
Definition: kmt_test.h:64
DWORD KmtRunKernelTest(IN PCSTR TestName)
Definition: support.c:95
DWORD KmtLoadAndOpenDriver(IN PCWSTR ServiceName, IN BOOLEAN RestartIfRunning)
Definition: support.c:213
DWORD WINAPI KmtUserCallbackThread(PVOID Parameter)
Definition: support.c:29
static WCHAR TestServiceName[MAX_PATH]
Definition: support.c:113
DWORD KmtOpenDriver(VOID)
Definition: support.c:192
VOID KmtCloseDriver(VOID)
Definition: support.c:236
DWORD KmtSendBufferToDriver(IN DWORD ControlCode, IN OUT PVOID Buffer OPTIONAL, IN DWORD InLength, IN OUT PDWORD OutLength)
Definition: support.c:360
DWORD KmtSendUlongToDriver(IN DWORD ControlCode, IN DWORD Value)
Definition: support.c:333
static HANDLE TestDeviceHandle
Definition: support.c:115
DWORD KmtSendStringToDriver(IN DWORD ControlCode, IN PCSTR String)
Definition: support.c:283
DWORD KmtSendToDriver(IN DWORD ControlCode)
Definition: support.c:259
HANDLE KmtestHandle
Definition: kmtest.c:32
DWORD KmtLoadDriver(IN PCWSTR ServiceName, IN BOOLEAN RestartIfRunning)
Definition: support.c:128
VOID KmtUnloadDriverKeepService(VOID)
Definition: support.c:149
static SC_HANDLE TestServiceHandle
Definition: support.c:114
DWORD KmtSendWStringToDriver(IN DWORD ControlCode, IN PCWSTR String)
Definition: support.c:308
VOID KmtUnloadDriver(VOID)
Definition: support.c:167
const uint16_t * PCWSTR
Definition: typedefs.h:57
ULONG_PTR SIZE_T
Definition: typedefs.h:80
const char * PCSTR
Definition: typedefs.h:52
#define IN
Definition: typedefs.h:39
#define OUT
Definition: typedefs.h:40
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_ ULONG _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_opt_ PULONG_PTR BytesReturned
Definition: wdfiotarget.h:1052
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PLONGLONG _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_opt_ PULONG_PTR BytesRead
Definition: wdfiotarget.h:870
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
SIZE_T NTAPI VirtualQuery(IN LPCVOID lpAddress, OUT PMEMORY_BASIC_INFORMATION lpBuffer, IN SIZE_T dwLength)
Definition: virtmem.c:211
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define CreateFile
Definition: winbase.h:3749
#define WINAPI
Definition: msvc.h:6
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
_Inout_opt_ PVOID Parameter
Definition: rtltypes.h:323
__wchar_t WCHAR
Definition: xmlstorage.h:180