ReactOS  0.4.15-dev-2714-g4864c87
RtlStack.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS kernel-mode tests
3  * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4  * PURPOSE: Kernel-Mode Test Suite Runtime library stack trace test
5  * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #define KMT_EMULATE_KERNEL
9 #include <kmt_test.h>
10 
12 
13 static
14 VOID
16 
18 static
19 VOID
21 {
22  PVOID Frames[5];
23  ULONG Ret;
24  ULONG Hash;
25  ULONG ExpectedHash;
26  ULONG i;
27  const ULONG FunctionSizeGuess = 0x1000;
28 
30 
31  Ret = RtlWalkFrameChain(NULL, 5, 0);
32  ok_eq_ulong(Ret, 0);
33 
34  RtlFillMemory(Frames, sizeof(Frames), 0x55);
35  Ret = RtlWalkFrameChain(Frames, 0, 0);
36  ok_eq_ulong(Ret, 0);
37  ok_eq_pointer(Frames[0], (PVOID)(ULONG_PTR)0x5555555555555555);
38 
39  RtlFillMemory(Frames, sizeof(Frames), 0x55);
40  Ret = RtlWalkFrameChain(Frames, 5, 0);
41  ok_eq_ulong(Ret, 5);
42  ok((ULONG_PTR)Frames[0] > (ULONG_PTR)TestStackWalk4, "Frame is %p, function is %p\n", Frames[0], TestStackWalk4);
43  ok((ULONG_PTR)Frames[0] < (ULONG_PTR)TestStackWalk4 + FunctionSizeGuess, "Frame is %p, function is %p\n", Frames[0], TestStackWalk4);
44  ok_eq_pointer(Frames[1], ReturnAddresses[3]);
45  ok_eq_pointer(Frames[2], ReturnAddresses[2]);
46  ok_eq_pointer(Frames[3], ReturnAddresses[1]);
47  ok_eq_pointer(Frames[4], ReturnAddresses[0]);
48 
49  RtlFillMemory(Frames, sizeof(Frames), 0x55);
50  Ret = RtlWalkFrameChain(Frames, 4, 0);
51  ok_eq_ulong(Ret, 4);
52  ok((ULONG_PTR)Frames[0] > (ULONG_PTR)TestStackWalk4, "Frame is %p, function is %p\n", Frames[0], TestStackWalk4);
53  ok((ULONG_PTR)Frames[0] < (ULONG_PTR)TestStackWalk4 + FunctionSizeGuess, "Frame is %p, function is %p\n", Frames[0], TestStackWalk4);
54  ok_eq_pointer(Frames[1], ReturnAddresses[3]);
55  ok_eq_pointer(Frames[2], ReturnAddresses[2]);
56  ok_eq_pointer(Frames[3], ReturnAddresses[1]);
57  ok_eq_pointer(Frames[4], (PVOID)(ULONG_PTR)0x5555555555555555);
58 
59  KmtStartSeh()
62 
63  RtlFillMemory(Frames, sizeof(Frames), 0x55);
64  Hash = 0x55555555;
65  Ret = RtlCaptureStackBackTrace(0, 0, Frames, &Hash);
66  ok_eq_ulong(Ret, 0);
67  ok_eq_hex(Hash, 0x55555555);
68  ok_eq_pointer(Frames[0], (PVOID)(ULONG_PTR)0x5555555555555555);
69 
70  RtlFillMemory(Frames, sizeof(Frames), 0x55);
71  Hash = 0x55555555;
72  Ret = RtlCaptureStackBackTrace(0, 1, Frames, NULL);
73  ok_eq_ulong(Ret, 1);
74  ok((ULONG_PTR)Frames[0] > (ULONG_PTR)TestStackWalk4, "Frame is %p, function is %p\n", Frames[0], TestStackWalk4);
75  ok((ULONG_PTR)Frames[0] < (ULONG_PTR)TestStackWalk4 + FunctionSizeGuess, "Frame is %p, function is %p\n", Frames[0], TestStackWalk4);
76  ok_eq_pointer(Frames[1], (PVOID)(ULONG_PTR)0x5555555555555555);
77 
78  RtlFillMemory(Frames, sizeof(Frames), 0x55);
79  Ret = RtlCaptureStackBackTrace(0, 5, Frames, &Hash);
80  ok_eq_ulong(Ret, 5);
81  ExpectedHash = 0;
82  for (i = 0; i < 5; i++)
83  ExpectedHash += (ULONG)(ULONG_PTR)Frames[i];
84  ok_eq_hex(Hash, ExpectedHash);
85  ok((ULONG_PTR)Frames[0] > (ULONG_PTR)TestStackWalk4, "Frame is %p, function is %p\n", Frames[0], TestStackWalk4);
86  ok((ULONG_PTR)Frames[0] < (ULONG_PTR)TestStackWalk4 + FunctionSizeGuess, "Frame is %p, function is %p\n", Frames[0], TestStackWalk4);
87  ok_eq_pointer(Frames[1], ReturnAddresses[3]);
88  ok_eq_pointer(Frames[2], ReturnAddresses[2]);
89  ok_eq_pointer(Frames[3], ReturnAddresses[1]);
90  ok_eq_pointer(Frames[4], ReturnAddresses[0]);
91 
92  RtlFillMemory(Frames, sizeof(Frames), 0x55);
93  Ret = RtlCaptureStackBackTrace(1, 4, Frames, &Hash);
94  ok_eq_ulong(Ret, 4);
95  ExpectedHash = 0;
96  for (i = 0; i < 4; i++)
97  ExpectedHash += (ULONG)(ULONG_PTR)Frames[i];
98  ok_eq_hex(Hash, ExpectedHash);
99  ok_eq_pointer(Frames[0], ReturnAddresses[3]);
100  ok_eq_pointer(Frames[1], ReturnAddresses[2]);
101  ok_eq_pointer(Frames[2], ReturnAddresses[1]);
102  ok_eq_pointer(Frames[3], ReturnAddresses[0]);
103  ok_eq_pointer(Frames[4], (PVOID)(ULONG_PTR)0x5555555555555555);
104 }
105 
107 static
108 VOID
110 {
112  TestStackWalk4();
113 }
114 
116 static
117 VOID
119 {
121  TestStackWalk3();
122 }
123 
125 static
126 VOID
128 {
130  TestStackWalk2();
131 }
132 
133 #ifdef _M_AMD64
134 NTSYSAPI
135 PVOID
136 NTAPI
138  _In_ PVOID PcValue,
139  _Out_ PVOID *BaseOfImage);
140 
141 extern char __ImageBase;
142 
144 static
145 VOID
146 TestRtlPcToFileHeader(VOID)
147 {
148  PVOID ImageBase, Result;
149  PTEB Teb;
150  PPEB Peb;
151 
152  /* First test a function from this image */
153  Result = RtlPcToFileHeader(&TestRtlPcToFileHeader, &ImageBase);
154  ok_eq_pointer(Result, ImageBase);
155  ok_eq_pointer(ImageBase, &__ImageBase);
156 
157 #ifdef NTOS_MODE_USER
158  Teb = NtCurrentTeb();
159 #else
160  Teb = KeGetCurrentThread()->Teb;
161 #endif
162  ok(Teb != NULL, "Teb is NULL!\n");
163  if (Teb == NULL)
164  {
165  return;
166  }
167 
168  _SEH2_TRY
169  {
171  ok(Peb != NULL, "Peb is NULL!\n");
172  if (Peb == NULL)
173  {
174  return;
175  }
176 
177  /* Test an address somewhere within the main image of the current process */
178  Result = RtlPcToFileHeader((PUCHAR)Peb->ImageBaseAddress + 0x1000, &ImageBase);
179  ok_eq_pointer(Result, ImageBase);
180 #ifdef NTOS_MODE_USER
181  ok_eq_pointer(ImageBase, Peb->ImageBaseAddress);
182 #else
183  ok_eq_pointer(ImageBase, NULL);
184 #endif
185  }
187  {
188  ok(FALSE, "Got an exception!\n");
189  }
190  _SEH2_END
191 }
192 #endif // _M_AMD64
193 
194 START_TEST(RtlStack)
195 {
196  TestStackWalk1();
197 #ifdef _M_AMD64
198  TestRtlPcToFileHeader();
199 #endif // _M_AMD64
200 }
static int Hash(const char *)
Definition: reader.c:2257
static DECLSPEC_NOINLINE VOID TestStackWalk4(VOID)
Definition: RtlStack.c:20
#define ok_eq_ulong(value, expected)
PPEB Peb
Definition: dllmain.c:27
PPEB ProcessEnvironmentBlock
Definition: ntddk_ex.h:337
#define ok_eq_pointer(value, expected)
unsigned char * PUCHAR
Definition: retypes.h:3
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:283
static PVOID ReturnAddresses[4]
Definition: RtlStack.c:11
START_TEST(RtlStack)
Definition: RtlStack.c:194
PVOID ImageBaseAddress
Definition: ntddk_ex.h:245
PVOID NTAPI RtlPcToFileHeader(IN PVOID PcValue, PVOID *BaseOfImage)
Definition: libsupp.c:659
#define NTSYSAPI
Definition: ntoskrnl.h:14
static VOID TestStackWalk3(VOID)
Definition: RtlStack.c:109
_SEH2_TRY
Definition: create.c:4226
uint32_t ULONG_PTR
Definition: typedefs.h:65
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define FALSE
Definition: types.h:117
static DECLSPEC_NOINLINE VOID TestStackWalk2(VOID)
Definition: RtlStack.c:118
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
#define _Out_
Definition: no_sal2.h:160
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:593
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define KmtStartSeh()
Definition: kmt_test.h:277
#define _In_
Definition: no_sal2.h:158
Definition: compat.h:694
_SEH2_END
Definition: create.c:4400
FORCEINLINE struct _TEB * NtCurrentTeb(VOID)
Definition: psfuncs.h:420
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
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
ULONG NTAPI RtlWalkFrameChain(OUT PVOID *Callers, IN ULONG Count, IN ULONG Flags)
Definition: libsupp.c:227
#define ok(value,...)
Definition: atltest.h:57
#define NULL
Definition: types.h:112
static DECLSPEC_NOINLINE VOID TestStackWalk1(VOID)
Definition: RtlStack.c:127
void * _ReturnAddress(void)
NTSYSAPI USHORT NTAPI RtlCaptureStackBackTrace(_In_ ULONG FramesToSkip, _In_ ULONG FramesToCapture, _Out_writes_to_(FramesToCapture, return) PVOID *BackTrace, _Out_opt_ PULONG BackTraceHash)
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define ok_eq_hex(value, expected)
#define __ImageBase
Definition: crt_handler.c:22
#define KeGetCurrentThread
Definition: hal.h:50
#define DECLSPEC_NOINLINE
Definition: ntbasedef.h:225