ReactOS 0.4.17-dev-573-g8315b8c
RtlInitializeContext.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS API tests
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: Test for RtlInitializeContext
5 * COPYRIGHT: Copyright 2026 Timo Kreuzer <timo.kreuzer@reactos.org>
6 */
7
8#include "precomp.h"
9
11{
14 PULONG_PTR StackPtr = &Stack[ARRAYSIZE(Stack) - 2];
15
16 RtlFillMemory(Stack, sizeof(Stack), 0xCC);
17 RtlFillMemory(&Context, sizeof(CONTEXT), 0xCC);
19 &Context,
20 (PVOID)(ULONG_PTR)0xDEADBEEFDEADBEEFull,
21 (PTHREAD_START_ROUTINE)(ULONG_PTR)0xCAFEF00DCAFEF00Dull,
22 StackPtr);
23
24#ifdef _M_IX86
25 #define INITIAL_FPCSR 0x000027F0
26 ok_eq_hex(Context.ContextFlags, CONTEXT_FULL);
27 ok_eq_hex(Context.Dr0, 0xCCCCCCCC);
28 ok_eq_hex(Context.Dr1, 0xCCCCCCCC);
29 ok_eq_hex(Context.Dr2, 0xCCCCCCCC);
30 ok_eq_hex(Context.Dr3, 0xCCCCCCCC);
31 ok_eq_hex(Context.Dr6, 0xCCCCCCCC);
32 ok_eq_hex(Context.Dr7, 0xCCCCCCCC);
33 ok_eq_hex(Context.FloatSave.ControlWord, 0xCCCCCCCC);
34 ok_eq_hex(Context.FloatSave.StatusWord, 0xCCCCCCCC);
35 ok_eq_hex(Context.FloatSave.TagWord, 0xCCCCCCCC);
36 ok_eq_hex(Context.FloatSave.ErrorOffset, 0xCCCCCCCC);
37 ok_eq_hex(Context.FloatSave.ErrorSelector, 0xCCCCCCCC);
38 ok_eq_hex(Context.FloatSave.DataOffset, 0xCCCCCCCC);
39 ok_eq_hex(Context.FloatSave.DataSelector, 0xCCCCCCCC);
40 for (ULONG i = 0; i < ARRAYSIZE(Context.FloatSave.RegisterArea); i++)
41 {
42 ok_eq_hex(Context.FloatSave.RegisterArea[i], 0xCC);
43 }
44 ok_eq_hex(Context.FloatSave.Cr0NpxState, 0xCCCCCCCC);
45 ok_eq_hex(Context.SegGs, 0x00000000);
46 ok_eq_hex(Context.SegFs, 0x00000038);
47 ok_eq_hex(Context.SegEs, 0x00000020);
48 ok_eq_hex(Context.SegDs, 0x00000020);
49 ok_eq_hex(Context.Edi, 0x00000005);
50 ok_eq_hex(Context.Esi, 0x00000004);
51 ok_eq_hex(Context.Ebx, 0x00000001);
52 ok_eq_hex(Context.Edx, 0x00000003);
53 ok_eq_hex(Context.Ecx, 0x00000002);
54 ok_eq_hex(Context.Eax, 0x00000000);
55 ok_eq_hex(Context.Ebp, 0x00000000);
56 ok_eq_hex(Context.Eip, 0xCAFEF00D);
57 ok_eq_hex(Context.SegCs, 0x00000018);
58 ok_eq_hex(Context.EFlags, 0x00000200);
59 ok_eq_hex(Context.Esp, (ULONG)(StackPtr - 2));
60 ok_eq_hex(Context.SegSs, 0x00000020);
61 for (ULONG i = 0; i < ARRAYSIZE(Context.ExtendedRegisters); i++)
62 {
63 ok_eq_hex(Context.ExtendedRegisters[i], 0xCC);
64 }
65
66 ok_eq_hex(Stack[0], 0xCCCCCCCC);
67 ok_eq_hex(Stack[1], 0xDEADBEEF);
68 ok_eq_hex(Stack[2], 0xCCCCCCCC);
69 ok_eq_hex(Stack[3], 0xCCCCCCCC);
70#elif defined(_M_AMD64)
71 ok_eq_hex64(Context.P1Home, 0xCCCCCCCCCCCCCCCCull);
72 ok_eq_hex64(Context.P2Home, 0xCCCCCCCCCCCCCCCCull);
73 ok_eq_hex64(Context.P3Home, 0xCCCCCCCCCCCCCCCCull);
74 ok_eq_hex64(Context.P4Home, 0xCCCCCCCCCCCCCCCCull);
75 ok_eq_hex64(Context.P5Home, 0xCCCCCCCCCCCCCCCCull);
76 ok_eq_hex64(Context.P6Home, 0xCCCCCCCCCCCCCCCCull);
77 ok_eq_hex(Context.ContextFlags, CONTEXT_FULL);
79 ok_eq_hex(Context.SegCs, 0xCCCC);
80 ok_eq_hex(Context.SegDs, 0xCCCC);
81 ok_eq_hex(Context.SegEs, 0xCCCC);
82 ok_eq_hex(Context.SegFs, 0xCCCC);
83 ok_eq_hex(Context.SegGs, 0xCCCC);
84 ok_eq_hex(Context.SegSs, 0xCCCC);
85 ok_eq_hex(Context.EFlags, 0x200);
86 ok_eq_hex64(Context.Dr0, 0xCCCCCCCCCCCCCCCCull);
87 ok_eq_hex64(Context.Dr1, 0xCCCCCCCCCCCCCCCCull);
88 ok_eq_hex64(Context.Dr2, 0xCCCCCCCCCCCCCCCCull);
89 ok_eq_hex64(Context.Dr3, 0xCCCCCCCCCCCCCCCCull);
90 ok_eq_hex64(Context.Dr6, 0xCCCCCCCCCCCCCCCCull);
91 ok_eq_hex64(Context.Dr7, 0xCCCCCCCCCCCCCCCCull);
92 ok_eq_hex64(Context.Rax, 0x0000000000000000ull);
93 ok_eq_hex64(Context.Rcx, 0xDEADBEEFDEADBEEFull);
94 ok_eq_hex64(Context.Rdx, 0xCCCCCCCCCCCCCCCCull);
95 ok_eq_hex64(Context.Rbx, 0x0000000000000001ull);
96 ok_eq_hex64(Context.Rsp, (ULONG64)StackPtr);
97 ok_eq_hex64(Context.Rbp, 0x0000000000000000ull);
98 ok_eq_hex64(Context.Rsi, 0x0000000000000004ull);
99 ok_eq_hex64(Context.Rdi, 0x0000000000000005ull);
100 ok_eq_hex64(Context.R8, 0x0000000000000008ull);
101 ok_eq_hex64(Context.R9, 0xF0E0D0C0A0908070ull);
102 ok_eq_hex64(Context.R10, 0x000000000000000Aull);
103 ok_eq_hex64(Context.R11, 0x000000000000000Bull);
104 ok_eq_hex64(Context.R12, 0x000000000000000Cull);
105 ok_eq_hex64(Context.R13, 0x000000000000000Dull);
106 ok_eq_hex64(Context.R14, 0x000000000000000Eull);
107 ok_eq_hex64(Context.R15, 0x000000000000000Full);
108 ok_eq_hex64(Context.Rip, 0xCAFEF00DCAFEF00Dull);
109 ok_eq_hex(Context.FltSave.ControlWord, INITIAL_FPCSR);
110 ok_eq_hex(Context.FltSave.StatusWord, 0x0000);
111 ok_eq_hex(Context.FltSave.TagWord, 0x00);
112 ok_eq_hex(Context.FltSave.Reserved1, 0x00);
113 ok_eq_hex(Context.FltSave.ErrorOpcode, 0x0000);
114 ok_eq_hex(Context.FltSave.ErrorOffset, 0x00000000);
115 ok_eq_hex(Context.FltSave.ErrorSelector, 0x0000);
116 ok_eq_hex(Context.FltSave.Reserved2, 0x0000);
117 ok_eq_hex(Context.FltSave.DataOffset, 0x00000000);
118 ok_eq_hex(Context.FltSave.DataSelector, 0x0000);
119 ok_eq_hex(Context.FltSave.Reserved3, 0x0000);
120 ok_eq_hex(Context.FltSave.MxCsr, INITIAL_MXCSR);
121 ok_eq_hex(Context.FltSave.MxCsr_Mask, 0x00000000);
122 for (ULONG i = 0; i < ARRAYSIZE(Context.FltSave.FloatRegisters); i++)
123 {
124 ok_eq_hex64(Context.FltSave.FloatRegisters[i].Low, 0x0000000000000000ull);
125 ok_eq_hex64(Context.FltSave.FloatRegisters[i].High, 0x0000000000000000ull);
126 }
127 PM128A XmmRegisters = &Context.Xmm0;
128 for (ULONG i = 0; i < 16; i++)
129 {
130 ok_eq_hex64(XmmRegisters[i].Low, 0x0000000000000000ull);
131 ok_eq_hex64(XmmRegisters[i].High, 0x0000000000000000ull);
132 }
133 for (ULONG i = 0; i < ARRAYSIZE(Context.VectorRegister); i++)
134 {
135 ok_eq_hex64(Context.VectorRegister[i].Low, 0xCCCCCCCCCCCCCCCCull);
136 ok_eq_hex64(Context.VectorRegister[i].High, 0xCCCCCCCCCCCCCCCCull);
137 }
138 ok_eq_hex64(Context.VectorControl, 0xCCCCCCCCCCCCCCCCull);
139 ok_eq_hex64(Context.DebugControl, 0xCCCCCCCCCCCCCCCCull);
140 ok_eq_hex64(Context.LastBranchToRip, 0xCCCCCCCCCCCCCCCCull);
141 ok_eq_hex64(Context.LastBranchFromRip, 0xCCCCCCCCCCCCCCCCull);
142 ok_eq_hex64(Context.LastExceptionToRip, 0xCCCCCCCCCCCCCCCCull);
143 ok_eq_hex64(Context.LastExceptionFromRip, 0xCCCCCCCCCCCCCCCCull);
144
145 ok_eq_hex64(Stack[0], 0xCCCCCCCCCCCCCCCCull);
146 ok_eq_hex64(Stack[1], 0xCCCCCCCCCCCCCCCCull);
147 ok_eq_hex64(Stack[2], 0xCCCCCCCCCCCCCCCCull);
148 ok_eq_hex64(Stack[3], 0xCCCCCCCCCCCCCCCCull);
149#else
150
151_WARN("Missing CONTEXT validation in RtlInitializeContext test");
152
153#endif
154
155 /* Test NULL Context */
156 StartSeh()
157 {
159 NULL,
160 (PVOID)(ULONG_PTR)0xDEADBEEFDEADBEEFull,
161 (PTHREAD_START_ROUTINE)(ULONG_PTR)0xCAFEF00DCAFEF00Dull,
162 StackPtr);
163 }
165
166 /* Test NULL StartRoutine */
167 StartSeh()
168 {
170 &Context,
171 (PVOID)(ULONG_PTR)0xDEADBEEFDEADBEEFull,
172 NULL,
173 StackPtr);
174 }
176
177 /* Test NULL Stack pointer */
178 StartSeh()
179 {
181 &Context,
182 (PVOID)(ULONG_PTR)0xDEADBEEFDEADBEEFull,
183 (PTHREAD_START_ROUTINE)(ULONG_PTR)0xCAFEF00DCAFEF00Dull,
184 NULL);
185 }
187
188 /* Test invalid Stack pointer */
189 StartSeh()
190 {
192 &Context,
193 (PVOID)(ULONG_PTR)0xDEADBEEFDEADBEEFull,
194 (PTHREAD_START_ROUTINE)(ULONG_PTR)0xCAFEF00DCAFEF00Dull,
195 (PVOID)(ULONG_PTR)0xDEADBEEFDEADBEE0ull);
196 }
198
199 /* Test unaligned stack */
200 StartSeh()
201 {
203 &Context,
204 (PVOID)(ULONG_PTR)0xDEADBEEFDEADBEEFull,
205 (PTHREAD_START_ROUTINE)(ULONG_PTR)0xCAFEF00DCAFEF00Dull,
206 (PUCHAR)StackPtr + 1);
207 }
208#ifdef _M_AMD64
210#else
212#endif
213
214}
#define ok_eq_hex(value, expected)
Definition: apitest.h:134
#define ok_eq_hex64(value, expected)
Definition: apitest.h:146
#define StartSeh()
Definition: apitest.h:93
#define EndSeh(ExpectedStatus)
Definition: apitest.h:99
#define START_TEST(x)
Definition: atltest.h:75
#define _WARN(msg)
Definition: debug.h:135
#define NULL
Definition: types.h:112
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define DECLSPEC_ALIGN(x)
Definition: corecrt.h:141
#define STATUS_ACCESS_VIOLATION
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
unsigned __int64 ULONG64
Definition: imports.h:198
NTSYSAPI VOID NTAPI RtlInitializeContext(_In_ HANDLE ProcessHandle, _Out_ PCONTEXT ThreadContext, _In_opt_ PVOID ThreadStartParam, _In_ PTHREAD_START_ROUTINE ThreadStartAddress, _In_ PVOID StackBase)
Definition: thread.c:27
ULONG(NTAPI * PTHREAD_START_ROUTINE)(PVOID Parameter)
Definition: rtltypes.h:560
#define NtCurrentProcess()
Definition: nt_native.h:1660
#define CONTEXT_FULL
Definition: nt_native.h:1378
#define STATUS_BAD_INITIAL_STACK
Definition: ntstatus.h:339
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:603
#define INITIAL_FPCSR
#define INITIAL_MXCSR
#define STATUS_SUCCESS
Definition: shellext.h:65
_In_ PVOID Context
Definition: storport.h:2269
@ High
Definition: strmini.h:378
@ Low
Definition: strmini.h:380
uint32_t * PULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG_PTR
Definition: typedefs.h:65
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
_In_ WDFREQUEST _In_ PIO_STACK_LOCATION Stack
Definition: wdfrequest.h:639
* PM128A
Definition: ketypes.h:1008