ReactOS  0.4.14-dev-384-g5b37caa
ObReference.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS kernel-mode tests
3  * LICENSE: GPLv2+ - See COPYING in the top level directory
4  * PURPOSE: Kernel-Mode Test Suite Object Referencing test
5  * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #include <kmt_test.h>
9 #define NDEBUG
10 #include <debug.h>
11 
12 #define CheckObject(Handle, Pointers, Handles) do \
13 { \
14  PUBLIC_OBJECT_BASIC_INFORMATION ObjectInfo; \
15  Status = ZwQueryObject(Handle, ObjectBasicInformation, \
16  &ObjectInfo, sizeof ObjectInfo, NULL); \
17  ok_eq_hex(Status, STATUS_SUCCESS); \
18  ok_eq_ulong(ObjectInfo.PointerCount, Pointers); \
19  ok_eq_ulong(ObjectInfo.HandleCount, Handles); \
20 } while (0)
21 
23 
24 static
25 VOID
29  IN PUNICODE_STRING NameUpper OPTIONAL,
30  IN BOOLEAN CaseSensitive,
31  IN ULONG AdditionalReferences,
32  IN BOOLEAN Permanent)
33 {
35  LONG_PTR Ret;
36  PVOID Object = NULL;
37  PVOID Object2 = NULL;
38  PVOID Object3 = NULL;
39  PVOID Object4 = NULL;
40 
41  CheckObject(Handle, 2LU + AdditionalReferences, 1LU);
42 
45  ok(Object != NULL, "ObReferenceObjectByHandle returned NULL object\n");
46  CheckObject(Handle, 3LU + AdditionalReferences, 1LU);
47 
50  ok(Object != NULL, "ObReferenceObjectByHandle returned NULL object\n");
51  ok_eq_pointer(Object, Object2);
52  CheckObject(Handle, 4LU + AdditionalReferences, 1LU);
53 
54  if (!skip(Object != NULL, "No object to reference!\n"))
55  {
57  ok_eq_longptr(Ret, (LONG_PTR)4 + AdditionalReferences);
58  CheckObject(Handle, 5LU + AdditionalReferences, 1LU);
59 
61  ok_eq_longptr(Ret, (LONG_PTR)5 + AdditionalReferences);
62  CheckObject(Handle, 6LU + AdditionalReferences, 1LU);
63 
66  CheckObject(Handle, 7LU + AdditionalReferences, 1LU);
67 
70  CheckObject(Handle, 8LU + AdditionalReferences, 1LU);
71 
73  ok_eq_longptr(Ret, (LONG_PTR)6 + AdditionalReferences);
74  CheckObject(Handle, 7LU + AdditionalReferences, 1LU);
75 
77  ok_eq_longptr(Ret, (LONG_PTR)5 + AdditionalReferences);
78  CheckObject(Handle, 6LU + AdditionalReferences, 1LU);
79 
81  ok_eq_longptr(Ret, (LONG_PTR)4 + AdditionalReferences);
82  CheckObject(Handle, 5LU + AdditionalReferences, 1LU);
83 
85  ok_eq_longptr(Ret, (LONG_PTR)3 + AdditionalReferences);
86  CheckObject(Handle, 4LU + AdditionalReferences, 1LU);
87  }
88 
89  if (Name && !skip(ObDirectoryObjectType != NULL, "No directory object type\n"))
90  {
93  CheckObject(Handle, 5LU + AdditionalReferences, 1LU);
94  }
95 
96  if (NameUpper && !skip(ObDirectoryObjectType != NULL, "No directory object type\n"))
97  {
100  CheckObject(Handle, 5LU + AdditionalReferences + !CaseSensitive, 1LU);
101  }
102 
103  if (NameUpper && !skip(Object4 != NULL, "No object to dereference\n"))
104  {
105  Ret = ObDereferenceObject(Object4);
106  ok_eq_longptr(Ret, (LONG_PTR)4 + AdditionalReferences);
107  CheckObject(Handle, 5LU + AdditionalReferences, 1LU);
108  }
109  if (Name && !skip(Object3 != NULL, "No object to dereference\n"))
110  {
111  Ret = ObDereferenceObject(Object3);
112  ok_eq_longptr(Ret, (LONG_PTR)3 + AdditionalReferences);
113  CheckObject(Handle, 4LU + AdditionalReferences, 1LU);
114  }
115  if (!skip(Object2 != NULL, "No object to dereference\n"))
116  {
117  Ret = ObDereferenceObject(Object2);
118  ok_eq_longptr(Ret, (LONG_PTR)2 + AdditionalReferences);
119  CheckObject(Handle, 3LU + AdditionalReferences, 1LU);
120  }
121  if (!skip(Object != NULL, "No object to dereference\n"))
122  {
124  ok_eq_longptr(Ret, (LONG_PTR)1 + AdditionalReferences);
125  CheckObject(Handle, 2LU + AdditionalReferences, 1LU);
126  }
127 
128  CheckObject(Handle, 2LU + AdditionalReferences, 1LU);
129 
130  if (Permanent)
131  {
134  CheckObject(Handle, 2LU + AdditionalReferences, 1LU);
135 
138  CheckObject(Handle, 2LU + AdditionalReferences, 1LU);
139  }
140 }
141 
142 START_TEST(ObReference)
143 {
148  UNICODE_STRING NameUpper, *pNameUpper;
149  ULONG i;
150  struct
151  {
152  PWSTR Name;
153  ULONG Flags;
154  ULONG AdditionalReferences;
155  } Tests[] =
156  {
157  { NULL, 0, 0 },
158  { NULL, OBJ_CASE_INSENSITIVE, 0 },
159  { NULL, OBJ_KERNEL_HANDLE, 0 },
160  { NULL, OBJ_PERMANENT, 0 },
165  { L"\\YayDirectory0", 0, 1 },
166  { L"\\YayDirectory1", OBJ_CASE_INSENSITIVE, 1 },
167  { L"\\YayDirectory2", OBJ_KERNEL_HANDLE, 1 },
168  { L"\\YayDirectory3", OBJ_PERMANENT, 1 },
169  { L"\\YayDirectory4", OBJ_CASE_INSENSITIVE | OBJ_PERMANENT, 1 },
170  { L"\\YayDirectory5", OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, 1 },
171  { L"\\YayDirectory6", OBJ_KERNEL_HANDLE | OBJ_PERMANENT, 1 },
172  { L"\\YayDirectory7", OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE | OBJ_PERMANENT, 1 },
173  };
174  HANDLE ObjectTypeHandle;
175 
176  /* ObReferenceObjectByName needs the object type... so get it... */
177  RtlInitUnicodeString(&Name, L"\\ObjectTypes\\Directory");
179  Status = ObOpenObjectByName(&ObjectAttributes, NULL, KernelMode, NULL, 0, NULL, &ObjectTypeHandle);
181  ok(ObjectTypeHandle != NULL, "ObjectTypeHandle = NULL\n");
182  if (!skip(Status == STATUS_SUCCESS && ObjectTypeHandle, "No handle\n"))
183  {
186  ok(ObDirectoryObjectType != NULL, "ObDirectoryObjectType = NULL\n");
187  Status = ZwClose(ObjectTypeHandle);
189  }
190 
191  for (i = 0; i < sizeof Tests / sizeof Tests[0]; ++i)
192  {
193  DPRINT("Run %d\n", i);
194  if (Tests[i].Name)
195  {
197  pName = &Name;
198  Status = RtlUpcaseUnicodeString(&NameUpper, &Name, TRUE);
200  if (skip(Status == STATUS_SUCCESS, "No upper case name\n"))
201  pNameUpper = NULL;
202  else
203  pNameUpper = &NameUpper;
204  }
205  else
206  {
207  pName = NULL;
208  pNameUpper = NULL;
209  }
213  ok(DirectoryHandle != NULL, "DirectoryHandle = NULL\n");
214 
215  if (!skip(Status == STATUS_SUCCESS && DirectoryHandle, "Cannot proceed without an object\n"))
216  {
217  TestReference(DirectoryHandle, pName, pNameUpper, FALSE, Tests[i].AdditionalReferences, (Tests[i].Flags & OBJ_PERMANENT) != 0);
218  /* try again for good measure */
219  TestReference(DirectoryHandle, pName, pNameUpper, FALSE, Tests[i].AdditionalReferences, FALSE);
220 
224  }
225 
226  if (pNameUpper)
227  RtlFreeUnicodeString(pNameUpper);
228  }
229 
230  if (DirectoryHandle)
231  {
234  }
235 
236  /* parameter tests */
237  /* bugcheck at APC_LEVEL
238  Status = ObReferenceObject(NULL);
239  Status = ObReferenceObjectByPointer(NULL, 0, NULL, UserMode);
240  Status = ObReferenceObjectByPointer(NULL, 0, NULL, KernelMode);*/
241 
243  {
246  }
247 }
NTSTATUS NTAPI ObOpenObjectByName(IN POBJECT_ATTRIBUTES ObjectAttributes, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, IN PACCESS_STATE PassedAccessState, IN ACCESS_MASK DesiredAccess, IN OUT PVOID ParseContext, OUT PHANDLE Handle)
Definition: obhandle.c:2529
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
static POBJECT_TYPE ObDirectoryObjectType
Definition: ObReference.c:22
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
NTSTATUS RtlUpcaseUnicodeString(PUNICODE_STRING dst, PUNICODE_STRING src, BOOLEAN Alloc)
Definition: string_lib.cpp:46
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
uint16_t * PWSTR
Definition: typedefs.h:54
#define ok_eq_pointer(value, expected)
LONG NTSTATUS
Definition: precomp.h:26
static HANDLE DirectoryHandle
Definition: ObType.c:48
#define OBJ_PERMANENT
Definition: winternl.h:226
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
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
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:496
struct NameRec_ * Name
Definition: cdprocs.h:464
NTSYSAPI NTSTATUS NTAPI ZwCreateDirectoryObject(_Out_ PHANDLE DirectoryHandle, _In_ ACCESS_MASK DesiredAccess, _In_ POBJECT_ATTRIBUTES ObjectAttributes)
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
NTSTATUS NTAPI ObReferenceObjectByPointer(IN PVOID Object, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode)
Definition: obref.c:383
_In_ HANDLE Handle
Definition: extypes.h:390
static LPSTR pName
Definition: security.c:75
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
static IUnknown Object
Definition: main.c:512
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
START_TEST(ObReference)
Definition: ObReference.c:142
static const WCHAR L[]
Definition: oid.c:1250
#define CheckObject(Handle, Pointers, Handles)
Definition: ObReference.c:12
Status
Definition: gdiplustypes.h:24
#define ok_eq_longptr(value, expected)
Definition: kmt_test.h:248
#define STATUS_OBJECT_NAME_NOT_FOUND
Definition: udferr_usr.h:149
#define DIRECTORY_ALL_ACCESS
Definition: nt_native.h:1259
#define ok(value,...)
Definition: atltest.h:57
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
#define skip(...)
Definition: atltest.h:64
#define ObReferenceObject
Definition: obfuncs.h:204
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define ok_eq_hex(value, expected)
return STATUS_SUCCESS
Definition: btrfs.c:2938
NTSTATUS NTAPI ObReferenceObjectByName(IN PUNICODE_STRING ObjectPath, IN ULONG Attributes, IN PACCESS_STATE PassedAccessState, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, IN OUT PVOID ParseContext, OUT PVOID *ObjectPtr)
Definition: obref.c:411
NTSYSAPI NTSTATUS NTAPI ZwMakeTemporaryObject(_In_ HANDLE Handle)
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
static VOID TestReference(IN HANDLE Handle, IN PUNICODE_STRING Name OPTIONAL, IN PUNICODE_STRING NameUpper OPTIONAL, IN BOOLEAN CaseSensitive, IN ULONG AdditionalReferences, IN BOOLEAN Permanent)
Definition: ObReference.c:26
struct test_data Tests[]
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68