ReactOS 0.4.15-dev-7958-gcd0bb1a
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
13static
14VOID
16
18static
19VOID
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
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
107static
108VOID
110{
113}
114
116static
117VOID
119{
122}
123
125static
126VOID
128{
131}
132
133#ifdef _M_AMD64
135PVOID
136NTAPI
138 _In_ PVOID PcValue,
139 _Out_ PVOID *BaseOfImage);
140
141extern char __ImageBase;
142
144static
145VOID
146TestRtlPcToFileHeader(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
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
182#else
183 ok_eq_pointer(ImageBase, NULL);
184#endif
185 }
187 {
188 ok(FALSE, "Got an exception!\n");
189 }
191}
192#endif // _M_AMD64
193
194START_TEST(RtlStack)
195{
197#ifdef _M_AMD64
198 TestRtlPcToFileHeader();
199#endif // _M_AMD64
200}
static VOID TestStackWalk3(VOID)
Definition: RtlStack.c:109
static PVOID ReturnAddresses[4]
Definition: RtlStack.c:11
static DECLSPEC_NOINLINE VOID TestStackWalk1(VOID)
Definition: RtlStack.c:127
static DECLSPEC_NOINLINE VOID TestStackWalk4(VOID)
Definition: RtlStack.c:20
static DECLSPEC_NOINLINE VOID TestStackWalk2(VOID)
Definition: RtlStack.c:118
#define ok_eq_pointer(value, expected)
Definition: apitest.h:59
#define ok_eq_hex(value, expected)
Definition: apitest.h:77
#define ok_eq_ulong(value, expected)
Definition: apitest.h:63
#define ok(value,...)
Definition: atltest.h:57
#define START_TEST(x)
Definition: atltest.h:75
#define NTSYSAPI
Definition: ntoskrnl.h:12
#define __ImageBase
Definition: crt_handler.c:22
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
ULONG NTAPI RtlWalkFrameChain(OUT PVOID *Callers, IN ULONG Count, IN ULONG Flags)
Definition: libsupp.c:227
PVOID NTAPI RtlPcToFileHeader(IN PVOID PcValue, PVOID *BaseOfImage)
Definition: libsupp.c:656
PPEB Peb
Definition: dllmain.c:27
static int Hash(const char *)
Definition: reader.c:2257
#define ULONG_PTR
Definition: config.h:101
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
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 KeGetCurrentThread
Definition: hal.h:55
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:599
#define _ReturnAddress()
Definition: intrin_arm.h:35
#define NtCurrentTeb
#define KmtStartSeh()
Definition: kmt_test.h:282
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:288
#define _Out_
Definition: ms_sal.h:345
#define _In_
Definition: ms_sal.h:308
#define DECLSPEC_NOINLINE
Definition: ntbasedef.h:225
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:34
PVOID ImageBaseAddress
Definition: ntddk_ex.h:245
Definition: compat.h:836
PPEB ProcessEnvironmentBlock
Definition: ntddk_ex.h:337
#define NTAPI
Definition: typedefs.h:36
uint32_t ULONG_PTR
Definition: typedefs.h:65
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
_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:409
NTSYSAPI USHORT NTAPI RtlCaptureStackBackTrace(_In_ ULONG FramesToSkip, _In_ ULONG FramesToCapture, _Out_writes_to_(FramesToCapture, return) PVOID *BackTrace, _Out_opt_ PULONG BackTraceHash)