ReactOS  0.4.14-dev-52-g6116262
rsym.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE: Test for dbghelp rsym functions
5  * COPYRIGHT: Copyright 2017-2019 Mark Jansen (mark.jansen@reactos.org)
6  *
7  * These tests are based on the PDB tests.
8  */
9 
10 #include <ntstatus.h>
11 #define WIN32_NO_STATUS
12 #include <windows.h>
13 #include <dbghelp.h>
14 #include <cvconst.h> // SymTagXXX
15 #include <stdio.h>
16 
17 #include "wine/test.h"
18 
19 #define ok_ulonglong(expression, result) \
20  do { \
21  ULONG64 _value = (expression); \
22  ULONG64 _result = (result); \
23  ok(_value == (result), "Wrong value for '%s', expected: " #result " (%s), got: %s\n", \
24  #expression, wine_dbgstr_longlong(_result), wine_dbgstr_longlong(_value)); \
25  } while (0)
26 
27 #define ok_ulonglong_(file, line, expression, result) \
28  do { \
29  ULONG64 _value = (expression); \
30  ULONG64 _result = (result); \
31  ok_(file, line)(_value == (result), "Wrong value for '%s', expected: " #result " (%s), got: %s\n", \
32  #expression, wine_dbgstr_longlong(_result), wine_dbgstr_longlong(_value)); \
33  } while (0)
34 
35 #define ok_hex_(file, line, expression, result) \
36  do { \
37  int _value = (expression); \
38  ok_(file, line)(_value == (result), "Wrong value for '%s', expected: " #result " (0x%x), got: 0x%x\n", \
39  #expression, (int)(result), _value); \
40  } while (0)
41 
42 #define ok_str_(file, line, x, y) \
43  ok_(file, line)(strcmp(x, y) == 0, "Wrong string. Expected '%s', got '%s'\n", y, x)
44 
45 
46 // data.c
47 void dump_rsym(const char* filename);
48 int extract_gcc_dll(char szFile[MAX_PATH]);
49 void cleanup_gcc_dll();
50 
51 static HANDLE proc()
52 {
53  return GetCurrentProcess();
54 }
55 
56 static BOOL init_sym_imp(BOOL fInvadeProcess, const char* file, int line)
57 {
58  if (!SymInitialize(proc(), NULL, fInvadeProcess))
59  {
61  ok_(file, line)(0, "Failed to init: 0x%x\n", err);
62  return FALSE;
63  }
64  return TRUE;
65 }
66 
67 static void deinit_sym()
68 {
69  SymCleanup(proc());
70 }
71 
72 #define init_sym(fInvadeProcess) init_sym_imp(fInvadeProcess, __FILE__, __LINE__)
73 
74 #define INIT_PSYM(buff) do { \
75  memset((buff), 0, sizeof((buff))); \
76  ((PSYMBOL_INFO)(buff))->SizeOfStruct = sizeof(SYMBOL_INFO); \
77  ((PSYMBOL_INFO)(buff))->MaxNameLen = MAX_SYM_NAME; \
78 } while (0)
79 
81 {
82  IMAGEHLP_MODULE64 ModuleInfo;
83  BOOL Ret;
84 
85  memset(&ModuleInfo, 0, sizeof(ModuleInfo));
86  ModuleInfo.SizeOfStruct = sizeof(ModuleInfo);
87  Ret = SymGetModuleInfo64(hProc, BaseAddress, &ModuleInfo);
88 
89  return Ret &&
90  ModuleInfo.SymType == SymDia &&
91  ModuleInfo.CVSig == ('R' | ('S' << 8) | ('Y' << 16) | ('M' << 24));
92 }
93 
94 
96 {
97  BOOL Ret;
98  char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
99  PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
100 
101  if (!supports_rsym(hProc, BaseAddress))
102  {
103  skip("dbghelp.dll cannot parse rsym\n");
104  }
105  else
106  {
107  INIT_PSYM(buffer);
108  Ret = SymFromName(hProc, "DllMain", pSymbol);
109  ok_int(Ret, TRUE);
110  ok_ulonglong(pSymbol->ModBase, BaseAddress);
111  ok_hex(pSymbol->Flags, SYMFLAG_FUNCTION);
112  ok_ulonglong(pSymbol->Address, BaseAddress + 0x1000);
113  ok_hex(pSymbol->Tag, SymTagFunction);
114  ok_str(pSymbol->Name, "DllMain");
115 
116  INIT_PSYM(buffer);
117  Ret = SymFromName(hProc, "FfsChkdsk", pSymbol);
118  ok_int(Ret, TRUE);
119  ok_ulonglong(pSymbol->ModBase, BaseAddress);
120  ok_hex(pSymbol->Flags, SYMFLAG_FUNCTION);
121  ok_ulonglong(pSymbol->Address, BaseAddress + 0x103F);
122  ok_hex(pSymbol->Tag, SymTagFunction);
123  ok_str(pSymbol->Name, "FfsChkdsk");
124 
125  INIT_PSYM(buffer);
126  Ret = SymFromName(hProc, "FfsFormat", pSymbol);
127  ok_int(Ret, TRUE);
128  ok_ulonglong(pSymbol->ModBase, BaseAddress);
129  ok_hex(pSymbol->Flags, SYMFLAG_FUNCTION);
130  ok_ulonglong(pSymbol->Address, BaseAddress + 0x100C);
131  ok_hex(pSymbol->Tag, SymTagFunction);
132  ok_str(pSymbol->Name, "FfsFormat");
133  }
134 }
135 
137 {
138  BOOL Ret;
139  char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
140  PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
141 
142  DWORD64 Displacement;
143  DWORD dwErr;
144 
145  if (!supports_rsym(hProc, BaseAddress))
146  {
147  skip("dbghelp.dll cannot parse rsym\n");
148  }
149  else
150  {
151  /* No address found before load address of module */
152  Displacement = 0;
153  INIT_PSYM(buffer);
154  Ret = SymFromAddr(hProc, BaseAddress -1, &Displacement, pSymbol);
155  dwErr = GetLastError();
156  ok_int(Ret, FALSE);
158 
159  /* Right at the start of the module is recognized as the first symbol found */
160  Displacement = 0;
161  INIT_PSYM(buffer);
162  Ret = SymFromAddr(hProc, BaseAddress, &Displacement, pSymbol);
163  /* Our dbghelp.dll does not recognize this yet */
164  todo_if(!Ret)
165  {
166  ok_int(Ret, TRUE);
167  ok_ulonglong(Displacement, 0xffffffffffffffff);
168  ok_ulonglong(pSymbol->ModBase, BaseAddress);
169  ok_hex(pSymbol->Flags, SYMFLAG_FUNCTION);
170  ok_ulonglong(pSymbol->Address, BaseAddress + 0x1000);
171  ok_hex(pSymbol->Tag, SymTagFunction);
172  ok_str(pSymbol->Name, "DllMain");
173  }
174 
175  /* The actual first instruction of the function */
176  Displacement = 0;
177  INIT_PSYM(buffer);
178  Ret = SymFromAddr(hProc, BaseAddress + 0x1000, &Displacement, pSymbol);
179  ok_int(Ret, TRUE);
180  ok_ulonglong(Displacement, 0);
181  ok_ulonglong(pSymbol->ModBase, BaseAddress);
182  ok_hex(pSymbol->Flags, SYMFLAG_FUNCTION);
183  ok_ulonglong(pSymbol->Address, BaseAddress + 0x1000);
184  ok_hex(pSymbol->Tag, SymTagFunction);
185  ok_str(pSymbol->Name, "DllMain");
186 
187  /* The last instruction in the function */
188  Displacement = 0;
189  INIT_PSYM(buffer);
190  Ret = SymFromAddr(hProc, BaseAddress + 0x1009, &Displacement, pSymbol);
191  ok_int(Ret, TRUE);
192  ok_ulonglong(Displacement, 0x9);
193  ok_ulonglong(pSymbol->ModBase, BaseAddress);
194  ok_hex(pSymbol->Flags, SYMFLAG_FUNCTION);
195  ok_ulonglong(pSymbol->Address, BaseAddress + 0x1000);
196  ok_hex(pSymbol->Tag, SymTagFunction);
197  ok_str(pSymbol->Name, "DllMain");
198 
199  /* First byte of the next function */
200  Displacement = 0;
201  INIT_PSYM(buffer);
202  Ret = SymFromAddr(hProc, BaseAddress + 0x103F, &Displacement, pSymbol);
203  ok_int(Ret, TRUE);
204  ok_ulonglong(Displacement, 0);
205  ok_ulonglong(pSymbol->ModBase, BaseAddress);
206  ok_hex(pSymbol->Flags, SYMFLAG_FUNCTION);
207  ok_ulonglong(pSymbol->Address, BaseAddress + 0x103F);
208  ok_hex(pSymbol->Tag, SymTagFunction);
209  ok_str(pSymbol->Name, "FfsChkdsk");
210 
211  /* .idata */
212  Displacement = 0;
213  INIT_PSYM(buffer);
214  Ret = SymFromAddr(hProc, BaseAddress + 0x4000, &Displacement, pSymbol);
215  ok_int(Ret, TRUE);
216  ok_ulonglong(Displacement, 0);
217  ok_ulonglong(pSymbol->ModBase, BaseAddress);
218  ok_hex(pSymbol->Flags, SYMFLAG_EXPORT);
219  ok_ulonglong(pSymbol->Address, BaseAddress + 0x4000);
220  ok_hex(pSymbol->Tag, SymTagPublicSymbol);
221  ok_str(pSymbol->Name, "_head_dll_ntdll_libntdll_a");
222  }
223 }
224 
225 typedef struct _test_context
226 {
228  SIZE_T Index;
229 } test_context;
230 
231 static struct _test_data {
233  ULONG Size;
234  ULONG Tag;
235  const char* Name;
236  int Line;
237 } test_data[] = {
238 
239  /* TODO: Order is based on magic, should find entries based on name, and mark as 'seen' */
240  { 0x107c, 0, SymTagPublicSymbol, "__CTOR_LIST__", __LINE__ },
241  { 0x2074, 0, SymTagPublicSymbol, "__RUNTIME_PSEUDO_RELOC_LIST_END__", __LINE__ },
242  { 0x1000, 12, SymTagPublicSymbol, "EntryPoint", __LINE__ },
243  { 0x100c, 51, SymTagFunction, "FfsFormat", __LINE__ },
244  { 0x4030, 0, SymTagPublicSymbol, "_imp__DbgPrint", __LINE__ },
245  { 0x1084, 0, SymTagPublicSymbol, "__DTOR_LIST__", __LINE__ },
246  { 0x103f, 53, SymTagFunction, "FfsChkdsk", __LINE__ },
247  { 0x2074, 0, SymTagPublicSymbol, "_rt_psrelocs_end", __LINE__ },
248  { 0x103f, 53, SymTagPublicSymbol, "ChkdskEx", __LINE__ },
249  { 0x4048, 0, SymTagPublicSymbol, "_dll_ntdll_libntdll_a_iname", __LINE__ },
250 
251 
252 
253  { 0x2074, 0, SymTagPublicSymbol, "_rt_psrelocs_start", __LINE__ },
254  { 0x1000, 12, SymTagFunction, "DllMain", __LINE__ },
255  { 0x100c, 0, SymTagPublicSymbol, "FormatEx", __LINE__ },
256  { 0x1074, 0, SymTagPublicSymbol, "DbgPrint", __LINE__ },
257  { 0x68900000, 0, SymTagPublicSymbol, "__ImageBase", __LINE__ },
258  { 0x68902074, 0, SymTagPublicSymbol, "__RUNTIME_PSEUDO_RELOC_LIST__", __LINE__ },
259  { 0x4000, 0, SymTagPublicSymbol, "_head_dll_ntdll_libntdll_a", __LINE__ },
260 };
261 
262 static BOOL CALLBACK EnumSymProc(PSYMBOL_INFO pSymInfo, ULONG SymbolSize, PVOID UserContext)
263 {
264  test_context* ctx = UserContext;
265 
266  if (ctx->Index < ARRAYSIZE(test_data))
267  {
268  ok_ulonglong_(__FILE__, test_data[ctx->Index].Line, pSymInfo->ModBase, ctx->BaseAddress);
269  if (test_data[ctx->Index].AddressOffset > 0x100000)
270  ok_ulonglong_(__FILE__, test_data[ctx->Index].Line, pSymInfo->Address, test_data[ctx->Index].AddressOffset);
271  else
272  ok_ulonglong_(__FILE__, test_data[ctx->Index].Line, pSymInfo->Address, ctx->BaseAddress + test_data[ctx->Index].AddressOffset);
273  ok_hex_(__FILE__, test_data[ctx->Index].Line, pSymInfo->Tag, test_data[ctx->Index].Tag);
274  ok_str_(__FILE__, test_data[ctx->Index].Line, pSymInfo->Name, test_data[ctx->Index].Name);
275 
276  ctx->Index++;
277  }
278  else
279  {
280  ok(0, "Out of bounds (%lu), max is: %i!\n", ctx->Index, ARRAYSIZE(test_data));
281  }
282 
283  return TRUE;
284 }
285 
287 {
288  BOOL Ret;
289  test_context ctx;
290 
291  ctx.Index = 0;
292  ctx.BaseAddress = BaseAddress;
293 
294  if (!supports_rsym(hProc, ctx.BaseAddress))
295  {
296  skip("dbghelp.dll cannot parse rsym\n");
297  }
298  else
299  {
300  Ret = SymEnumSymbols(hProc, ctx.BaseAddress, NULL, EnumSymProc, &ctx);
301  ok_int(Ret, TRUE);
303  }
304 }
305 
306 
308 {
309  char szDllName[MAX_PATH];
310 #ifdef _M_IX86
311  HMODULE hMod;
312 #endif
315 
317  Options &= ~(SYMOPT_UNDNAME);
318  //Options |= SYMOPT_DEBUG;
320 
322  {
323  ok(0, "Failed extracting files\n");
324  return;
325  }
326 
327  if (init_sym(FALSE))
328  {
330  BaseAddress = SymLoadModule64(proc(), NULL, szDllName, NULL, 0x600000, 0);
331  dwErr = GetLastError();
332 
333  ok_ulonglong(BaseAddress, 0x600000);
335 
336  if (BaseAddress == 0x600000)
337  {
338  trace("Module loaded by SymLoadModule64\n");
342  }
343 
344  deinit_sym();
345  }
346 
347 #ifdef _M_IX86
348  hMod = LoadLibraryA(szDllName);
349  if (hMod)
350  {
351  BaseAddress = (DWORD64)(DWORD_PTR)hMod;
352  /* Invade process */
353  if (init_sym(TRUE))
354  {
355  trace("Module loaded by LoadLibraryA\n");
359 
360  deinit_sym();
361  }
362 
363  FreeLibrary(hMod);
364  }
365 #endif
366  cleanup_gcc_dll();
367 }
BOOL WINAPI SymCleanup(HANDLE hProcess)
Definition: dbghelp.c:416
ULONG Tag
Definition: compat.h:698
SYM_TYPE SymType
Definition: compat.h:885
CHAR Name[1]
Definition: compat.h:701
#define TRUE
Definition: types.h:120
#define ok_ulonglong(expression, result)
Definition: rsym.c:19
Definition: compat.h:712
#define ERROR_SUCCESS
Definition: deptool.c:10
#define ok_str(x, y)
Definition: atltest.h:127
static BOOL init_sym_imp(BOOL fInvadeProcess, const char *file, int line)
Definition: rsym.c:56
static const WCHAR szDllName[]
Definition: sip.c:61
DWORD WINAPI SymSetOptions(DWORD opts)
Definition: dbghelp.c:443
static void test_SymFromAddr(HANDLE hProc, DWORD64 BaseAddress)
Definition: rsym.c:136
#define MAX_SYM_NAME
Definition: rsym.c:34
#define CALLBACK
Definition: compat.h:27
ULONG Flags
Definition: compat.h:693
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
GLuint buffer
Definition: glext.h:5915
ULONG64 Address
Definition: compat.h:695
#define INIT_PSYM(buff)
Definition: rsym.c:74
const char * filename
Definition: ioapi.h:135
enum OPTION_FLAGS Options
Definition: stats.c:44
#define ok_hex(expression, result)
Definition: atltest.h:94
static void deinit_sym()
Definition: rsym.c:67
struct _SYMBOL_INFO * PSYMBOL_INFO
unsigned int BOOL
Definition: ntddk_ex.h:94
static BOOL supports_rsym(HANDLE hProc, DWORD64 BaseAddress)
Definition: rsym.c:80
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryA(LPCSTR lpLibFileName)
Definition: loader.c:111
int Line
Definition: rsym.c:236
#define ok_ulonglong_(file, line, expression, result)
Definition: rsym.c:27
ULONG64 ModBase
Definition: compat.h:692
smooth NULL
Definition: ftsmooth.c:416
BOOL WINAPI SymInitialize(HANDLE hProcess, PCSTR UserSearchPath, BOOL fInvadeProcess)
Definition: dbghelp.c:393
#define ok_int(expression, result)
Definition: atltest.h:134
Definition: parser.c:48
DWORD SizeOfStruct
Definition: compat.h:879
void cleanup_gcc_dll()
Definition: data.c:120
START_TEST(rsym)
Definition: rsym.c:307
DWORD dwErr
Definition: service.c:36
DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName, PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
Definition: module.c:673
int extract_gcc_dll(char szFile[MAX_PATH])
Definition: data.c:107
char TCHAR
Definition: xmlstorage.h:189
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define FreeLibrary(x)
Definition: compat.h:405
ULONG Tag
Definition: pdb.c:424
#define trace
Definition: atltest.h:70
#define init_sym(fInvadeProcess)
Definition: rsym.c:72
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1138
#define todo_if(is_todo)
Definition: test.h:144
struct _SYMBOL_INFO SYMBOL_INFO
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:409
void dump_rsym(const char *filename)
#define SYMOPT_UNDNAME
Definition: compat.h:637
#define MAX_PATH
Definition: rsym.c:33
struct _test_context test_context
DWORD64 AddressOffset
Definition: pdb.c:422
static BOOL CALLBACK EnumSymProc(PSYMBOL_INFO pSymInfo, ULONG SymbolSize, PVOID UserContext)
Definition: rsym.c:262
#define err(...)
uint32_t DWORD_PTR
Definition: typedefs.h:63
BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address, DWORD64 *Displacement, PSYMBOL_INFO Symbol)
Definition: symbol.c:1271
#define SYMFLAG_FUNCTION
Definition: compat.h:653
ULONG_PTR SIZE_T
Definition: typedefs.h:78
uint64_t DWORD64
Definition: typedefs.h:65
const char * Name
Definition: pdb.c:425
BOOL WINAPI SymEnumSymbols(HANDLE hProcess, ULONG64 BaseOfDll, PCSTR Mask, PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback, PVOID UserContext)
Definition: symbol.c:1155
#define ok(value,...)
Definition: atltest.h:57
#define SYMFLAG_EXPORT
Definition: compat.h:651
#define ok_hex_(file, line, expression, result)
Definition: rsym.c:35
static void test_SymFromName(HANDLE hProc, DWORD64 BaseAddress)
Definition: rsym.c:95
DWORD WINAPI SymGetOptions(void)
Definition: dbghelp.c:458
#define skip(...)
Definition: atltest.h:64
DWORD64 BaseAddress
Definition: pdb.c:417
static HANDLE proc()
Definition: rsym.c:51
#define ERROR_MOD_NOT_FOUND
Definition: compat.h:94
unsigned int ULONG
Definition: retypes.h:1
BOOL WINAPI SymFromName(HANDLE hProcess, PCSTR Name, PSYMBOL_INFO Symbol)
Definition: symbol.c:1400
static void test_SymEnumSymbols(HANDLE hProc, DWORD64 BaseAddress)
Definition: rsym.c:286
#define ok_str_(file, line, x, y)
Definition: rsym.c:42
#define memset(x, y, z)
Definition: compat.h:39
ULONG Size
Definition: pdb.c:423
BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr, PIMAGEHLP_MODULE64 ModuleInfo)
Definition: module.c:1013
#define ok_(x1, x2)
Definition: atltest.h:61
SIZE_T Index
Definition: pdb.c:418
Definition: fci.c:126