ReactOS 0.4.17-dev-1196-gb6224c3
RtlRetrieveNtUserPfn.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 RtlRetrieveNtUserPfn
5 * COPYRIGHT: Copyright 2026 Timo Kreuzer <timo.kreuzer@reactos.org>
6 */
7
8#include "precomp.h"
9
10typedef
14 _Out_ const PVOID**,
15 _Out_ const PVOID**,
16 _Out_ const PVOID**);
17
19
21{
22 HINSTANCE hinstNtdll = GetModuleHandleA("ntdll.dll");
23 pRtlRetrieveNtUserPfn = (void*)GetProcAddress(hinstNtdll, "RtlRetrieveNtUserPfn");
25 {
26 win_skip("RtlRetrieveNtUserPfn not supported\n");
27 return;
28 }
29
30 /* Get the start and end of the ".const" section in ntdll, and check that the retrieved pointers are within that range. */
31 PVOID NtDllTextStart = NULL, NtDllTextEnd = NULL;
32 PVOID NtDllRdataStart = NULL, NtDllRdataEnd = NULL;
33 PIMAGE_NT_HEADERS NtHeaders = RtlImageNtHeader(hinstNtdll);
34 PIMAGE_SECTION_HEADER SectionHeaders = IMAGE_FIRST_SECTION(NtHeaders);
35
36 for (int i = 0; i < NtHeaders->FileHeader.NumberOfSections; i++)
37 {
38 PIMAGE_SECTION_HEADER Section = &SectionHeaders[i];
39 if (memcmp(Section->Name, ".text", 5) == 0)
40 {
41 NtDllTextStart = (PBYTE)hinstNtdll + Section->VirtualAddress;
42 NtDllTextEnd = (PBYTE)NtDllTextStart + Section->Misc.VirtualSize;
43 }
44 else if (memcmp(Section->Name, ".rdata", 6) == 0)
45 {
46 NtDllRdataStart = (PBYTE)hinstNtdll + Section->VirtualAddress;
47 NtDllRdataEnd = (PBYTE)NtDllRdataStart + Section->Misc.VirtualSize;
48 }
49 }
50
51 /* Some versions don't have a .rdata section */
52 if (NtDllRdataStart == NULL)
53 {
54 /* Use .text instead */
55 NtDllRdataStart = NtDllTextStart;
56 NtDllRdataEnd = NtDllTextEnd;
57 }
58
59 ok(NtDllTextStart != NULL, "Failed to find .text section in ntdll.dll\n");
60 ok(NtDllRdataStart != NULL, "Failed to find .rdata section in ntdll.dll\n");
61
63 const PVOID *p1 = (PVOID)(ULONG_PTR)0xCCCCCCCCCCCCCCCC;
64 const PVOID *p2 = (PVOID)(ULONG_PTR)0xCCCCCCCCCCCCCCCC;
65 const PVOID *p3 = (PVOID)(ULONG_PTR)0xCCCCCCCCCCCCCCCC;
66
67 /* We expect the user PFNs not to have been set up by now */
68 Status = pRtlRetrieveNtUserPfn(&p1, &p2, &p3);
70
72 {
73 /* Load win32u.dll, which should NOT register the PFNs */
74 HINSTANCE hinstWin32u = GetModuleHandleA("win32u.dll");
75 ok(hinstWin32u == NULL, "win32u.dll should not be loaded yet\n");
76 hinstWin32u = LoadLibraryA("win32u.dll");
77 ok(hinstWin32u != NULL, "Failed to load win32u.dll\n");
78
79 /* Try again (should still fail) */
80 Status = pRtlRetrieveNtUserPfn(&p1, &p2, &p3);
82 }
83
84 /* Load user32, which should register the PFNs */
85 HINSTANCE hinstUser32 = GetModuleHandleA("user32.dll");
86 ok(hinstUser32 == NULL, "user32.dll should not be loaded yet\n");
87 hinstUser32 = LoadLibraryA("user32.dll");
88 ok(hinstUser32 != NULL, "Failed to load user32.dll\n");
89
90 /* Try again */
91 Status = pRtlRetrieveNtUserPfn(&p1, &p2, &p3);
93
94 /* The function retrieves three pointers to internal ntdll pointer arrays.
95 All three arrays are located in the .rdata section of ntdll.dll,
96 so check that the retrieved pointers are within that range. */
97 ok((PVOID)p1 >= NtDllRdataStart && (PVOID)p1 < NtDllRdataEnd,
98 "p1 (%p) is not within .rdata section (%p-%p)\n", p1, NtDllRdataStart, NtDllRdataEnd);
99 ok((PVOID)p2 >= NtDllRdataStart && (PVOID)p2 < NtDllRdataEnd,
100 "p2 (%p) is not within .rdata section (%p-%p)\n", p2, NtDllRdataStart, NtDllRdataEnd);
101 ok((PVOID)p3 >= NtDllRdataStart && (PVOID)p3 < NtDllRdataEnd,
102 "p3 (%p) is not within .rdata section (%p-%p)\n", p3, NtDllRdataStart, NtDllRdataEnd);
103
104 /* The arrays are expected to follow one after the other */
105 ok(p2 > p1, "p2 (%p) is not greater than p1 (%p)\n", p2, p1);
106 ok(p3 > p2, "p3 (%p) is not greater than p2 (%p)\n", p3, p2);
107
108 /* Get the field size */
111 SIZE_T FieldSize = IsWow64 ? sizeof(ULONG64) : sizeof(PVOID);
112
113 SIZE_T Count1 = ((PUCHAR)p2 - (PUCHAR)p1) / FieldSize;
114 SIZE_T Count2 = ((PUCHAR)p3 - (PUCHAR)p2) / FieldSize;
115
116 ok_eq_size(Count1, Count2);
117 ok_eq_size(Count1, GetNTVersion() >= _WIN32_WINNT_WIN10 ? 24 : 23);
118
119 /* Iterate over all function pointers from arrays 1 and 2 in one run */
120 for (SIZE_T i = 0; i < (Count1 + Count2); i++)
121 {
122 PUCHAR pfn = *(PVOID*)((PUCHAR)p1 + i * FieldSize);
123 ok((PVOID)pfn >= NtDllTextStart && (PVOID)pfn < NtDllTextEnd,
124 "p1[%d] (%p) is not within .text section (%p-%p)\n", i, pfn, NtDllTextStart, NtDllTextEnd);
125
126 /* Check the dispatch code */
128 {
129#if defined(_M_IX86)
130 ok_eq_size((ULONG_PTR)pfn & 15, 0); // The functions should be 16-byte aligned
131 ok_eq_int(memcmp(&pfn[0], "\xFF\x25", 2), 0); // jmp dword ptr [x]
132 ok_eq_int(memcmp(&pfn[6], "\x8D\xA4\x24\x00\x00\x00\x00", 7), 0); // lea esp, [esp]
133 ok_eq_int(memcmp(&pfn[13], "\x8D\x49\x00", 3), 0); // lea ecx, [ecx]
134#elif defined(_M_AMD64)
135 ok_eq_size((ULONG_PTR)pfn & 15, 0); // The functions should be 16-byte aligned
136 ok_eq_int(memcmp(&pfn[0], "\xFF\x25", 2), 0); // jmp qword ptr [rip + x]
137 ok_eq_int(memcmp(&pfn[6], "\x66\x66\x0F\x1F\x84\x00\x00\x00\x00\x00", 10), 0); // nop word ptr [rax + rax]
138#endif
139 }
140 else
141 {
142#if defined(_M_IX86)
143 ok_eq_size((ULONG_PTR)pfn & 3, 0); // The functions should be 4-byte aligned
144 ok_eq_int(memcmp(&pfn[0], "\xFF\x25", 2), 0); // jmp dword ptr [x]
145#elif defined(_M_AMD64)
146 ok_eq_size((ULONG_PTR)pfn & 3, 0); // The functions should be 4-byte aligned
147 ok_eq_int(memcmp(&pfn[0], "\x48\xFF\x25", 3), 0); // jmp qword ptr [rip + x]
148#endif
149 }
150 }
151
152 /* The 3rd array has only 11 entries, check those as well. */
153 for (SIZE_T i = 0; i < 11; i++)
154 {
155 PUCHAR pfn = *(PVOID*)((PUCHAR)p3 + i * FieldSize);
156 ok((PVOID)pfn >= NtDllTextStart && (PVOID)pfn < NtDllTextEnd,
157 "p3[%d] (%p) is not within .text section (%p-%p)\n", i, pfn, NtDllTextStart, NtDllTextEnd);
158 }
159
160 StartSeh()
161 Status = pRtlRetrieveNtUserPfn(NULL, &p2, &p3);
163
164 StartSeh()
165 Status = pRtlRetrieveNtUserPfn(&p1, NULL, &p3);
167
168 StartSeh()
169 Status = pRtlRetrieveNtUserPfn(&p1, &p2, NULL);
171}
#define IsWow64()
Definition: NtReadFile.c:37
NTSTATUS WINAPI FN_RtlRetrieveNtUserPfn(_Out_ const PVOID **, _Out_ const PVOID **, _Out_ const PVOID **)
FN_RtlRetrieveNtUserPfn * pRtlRetrieveNtUserPfn
#define GetNTVersion()
Definition: apitest.h:17
#define ok_eq_size(value, expected)
Definition: apitest.h:128
#define ok_eq_int(value, expected)
Definition: apitest.h:119
#define StartSeh()
Definition: apitest.h:95
#define EndSeh(ExpectedStatus)
Definition: apitest.h:101
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
#define ok(value,...)
Definition: atltest.h:57
#define START_TEST(x)
Definition: atltest.h:75
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define GetProcAddress(x, y)
Definition: compat.h:753
#define GetCurrentProcess()
Definition: compat.h:759
#define IsWow64Process
Definition: compat.h:760
#define RtlImageNtHeader
Definition: compat.h:806
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryA(LPCSTR lpLibFileName)
Definition: loader.c:111
_ACRTIMP int __cdecl memcmp(const void *, const void *, size_t)
Definition: string.c:2807
unsigned int BOOL
Definition: ntddk_ex.h:94
#define STATUS_ACCESS_VIOLATION
Status
Definition: gdiplustypes.h:24
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 is_reactos()
Definition: kmt_test.h:534
#define win_skip
Definition: minitest.h:67
unsigned __int64 ULONG64
Definition: imports.h:198
#define _Out_
Definition: no_sal2.h:160
#define IMAGE_FIRST_SECTION(NtHeader)
Definition: ntimage.h:427
BYTE * PBYTE
Definition: pedump.c:66
#define _WIN32_WINNT_WIN10
Definition: sdkddkver.h:32
#define _WIN32_WINNT_WIN7
Definition: sdkddkver.h:28
#define STATUS_SUCCESS
Definition: shellext.h:65
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
union _IMAGE_SECTION_HEADER::@1806 Misc
BYTE Name[IMAGE_SIZEOF_SHORT_NAME]
Definition: pedump.c:281
void * PVOID
Definition: typedefs.h:50
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t ULONG_PTR
Definition: typedefs.h:65
unsigned char * PUCHAR
Definition: typedefs.h:53
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define WINAPI
Definition: msvc.h:6
NTSYSAPI NTSTATUS WINAPI RtlRetrieveNtUserPfn(const void **, const void **, const void **)