VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstLdr.cpp@ 80721

Last change on this file since 80721 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.5 KB
Line 
1/* $Id: tstLdr.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - Testcase for parts of RTLdr*.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/ldr.h>
32#include <iprt/alloc.h>
33#include <iprt/stream.h>
34#include <iprt/assert.h>
35#include <iprt/initterm.h>
36#include <iprt/errcore.h>
37#include <iprt/string.h>
38
39
40/*********************************************************************************************************************************
41* Global Variables *
42*********************************************************************************************************************************/
43/** If set, don't bitch when failing to resolve symbols. */
44static bool g_fDontBitchOnResolveFailure = false;
45/** Whether it's kernel model code or not.. */
46static bool g_fKernel = true;
47/** Module architecture bit count. */
48static uint32_t g_cBits = HC_ARCH_BITS;
49
50
51/**
52 * Resolve an external symbol during RTLdrGetBits().
53 *
54 * @returns iprt status code.
55 * @param hLdrMod The loader module handle.
56 * @param pszModule Module name.
57 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
58 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
59 * @param pValue Where to store the symbol value (address).
60 * @param pvUser User argument.
61 */
62static DECLCALLBACK(int) testGetImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol,
63 RTUINTPTR *pValue, void *pvUser)
64{
65 /* check the name format and only permit certain names... later, right? */
66 RT_NOREF_PV(hLdrMod); RT_NOREF_PV(pszModule); RT_NOREF_PV(pszSymbol); RT_NOREF_PV(uSymbol); RT_NOREF_PV(pvUser);
67
68 if (g_cBits == 32)
69 *pValue = 0xabcdef0f;
70 else
71 {
72 RTUINTPTR BaseAddr = *(PCRTUINTPTR)pvUser;
73 if (g_fKernel)
74 *pValue = BaseAddr & RT_BIT(31) ? -(int32_t)0x76634935 : 0x7f304938;
75 else
76 *pValue = (int32_t)0x76634935 * ((BaseAddr >> 8) & 7);
77 }
78 return VINF_SUCCESS;
79}
80
81
82/**
83 * One test iteration with one file.
84 *
85 * The test is very simple, we load the file three times
86 * into two different regions. The first two into each of the
87 * regions the for compare usage. The third is loaded into one
88 * and then relocated between the two and other locations a few times.
89 *
90 * @returns number of errors.
91 * @param pszFilename The file to load the mess with.
92 */
93static int testLdrOne(const char *pszFilename)
94{
95 int rcRet = 0;
96 size_t cbImage = 0;
97 struct Load
98 {
99 RTLDRMOD hLdrMod;
100 void *pvBits;
101 RTUINTPTR Addr;
102 const char *pszName;
103 } aLoads[6] =
104 {
105 { NULL, NULL, (RTUINTPTR)0xefefef00, "foo" },
106 { NULL, NULL, (RTUINTPTR)0x40404040, "bar" },
107 { NULL, NULL, (RTUINTPTR)0xefefef00, "foobar" },
108 };
109 unsigned i;
110
111 /*
112 * Load them.
113 */
114 for (i = 0; i < RT_ELEMENTS(aLoads); i++)
115 {
116 /* adjust load address and announce our intentions */
117 if (g_cBits == 32)
118 aLoads[i].Addr &= UINT32_C(0xffffffff);
119 RTPrintf("tstLdr: Loading image at %RTptr\n", aLoads[i].Addr);
120
121 /* open it */
122 int rc = RTLdrOpen(pszFilename, 0, RTLDRARCH_WHATEVER, &aLoads[i].hLdrMod);
123 if (RT_FAILURE(rc))
124 {
125 RTPrintf("tstLdr: Failed to open '%s'/%d, rc=%Rrc. aborting test.\n", pszFilename, i, rc);
126 Assert(aLoads[i].hLdrMod == NIL_RTLDRMOD);
127 rcRet++;
128 break;
129 }
130
131 /* size it */
132 size_t cb = RTLdrSize(aLoads[i].hLdrMod);
133 if (cbImage && cb != cbImage)
134 {
135 RTPrintf("tstLdr: Size mismatch '%s'/%d. aborting test.\n", pszFilename, i);
136 rcRet++;
137 break;
138 }
139 cbImage = cb;
140
141 /* Allocate bits. */
142 aLoads[i].pvBits = RTMemAlloc(cb);
143 if (!aLoads[i].pvBits)
144 {
145 RTPrintf("tstLdr: Out of memory '%s'/%d cbImage=%d. aborting test.\n", pszFilename, i, cbImage);
146 rcRet++;
147 break;
148 }
149
150 /* Get the bits. */
151 rc = RTLdrGetBits(aLoads[i].hLdrMod, aLoads[i].pvBits, aLoads[i].Addr, testGetImport, &aLoads[i].Addr);
152 if (RT_FAILURE(rc))
153 {
154 RTPrintf("tstLdr: Failed to get bits for '%s'/%d, rc=%Rrc. aborting test\n", pszFilename, i, rc);
155 rcRet++;
156 break;
157 }
158 }
159
160 /*
161 * Continue with the relocations and symbol resolving.
162 */
163 if (!rcRet)
164 {
165 static RTUINTPTR aRels[] =
166 {
167 (RTUINTPTR)0xefefef00, /* same. */
168 (RTUINTPTR)0x40404040, /* the other. */
169 (RTUINTPTR)0xefefef00, /* back. */
170 (RTUINTPTR)0x40404040, /* the other. */
171 (RTUINTPTR)0xefefef00, /* back again. */
172 (RTUINTPTR)0x77773420, /* somewhere entirely else. */
173 (RTUINTPTR)0xf0000000, /* somewhere entirely else. */
174 (RTUINTPTR)0x40404040, /* the other. */
175 (RTUINTPTR)0xefefef00 /* back again. */
176 };
177 struct Symbols
178 {
179 /** The symbol offset. -1 indicates the first time. */
180 unsigned off;
181 /** The symbol name. */
182 const char *pszName;
183 } aSyms[] =
184 {
185 { ~0U, "Entrypoint" },
186 { ~0U, "SomeExportFunction1" },
187 { ~0U, "SomeExportFunction2" },
188 { ~0U, "SomeExportFunction3" },
189 { ~0U, "SomeExportFunction4" },
190 { ~0U, "SomeExportFunction5" },
191 { ~0U, "SomeExportFunction5" },
192 { ~0U, "DISCoreOne" }
193 };
194
195 unsigned iRel = 0;
196 for (;;)
197 {
198 /* Compare all which are at the same address. */
199 for (i = 0; i < RT_ELEMENTS(aLoads) - 1; i++)
200 {
201 for (unsigned j = i + 1; j < RT_ELEMENTS(aLoads); j++)
202 {
203 if (aLoads[j].Addr == aLoads[i].Addr)
204 {
205 if (memcmp(aLoads[j].pvBits, aLoads[i].pvBits, cbImage))
206 {
207 RTPrintf("tstLdr: Mismatch between load %d and %d. ('%s')\n", j, i, pszFilename);
208#if 1
209 const uint8_t *pu8J = (const uint8_t *)aLoads[j].pvBits;
210 const uint8_t *pu8I = (const uint8_t *)aLoads[i].pvBits;
211 for (uint32_t off = 0; off < cbImage; off++, pu8J++, pu8I++)
212 if (*pu8J != *pu8I)
213 RTPrintf(" %08x %02x != %02x\n", off, *pu8J, *pu8I);
214#else
215 const uint32_t *pu32J = (const uint32_t *)aLoads[j].pvBits;
216 const uint32_t *pu32I = (const uint32_t *)aLoads[i].pvBits;
217 for (uint32_t off = 0; off < cbImage; off += 4, pu32J++, pu32I++)
218 if (*pu32J != *pu32I)
219 RTPrintf(" %08x %08x != %08x\n", off, *pu32J, *pu32I);
220#endif
221 rcRet++;
222 break;
223 }
224 }
225 }
226 }
227
228 /* compare symbols. */
229 for (i = 0; i < RT_ELEMENTS(aLoads); i++)
230 {
231 for (unsigned iSym = 0; iSym < RT_ELEMENTS(aSyms); iSym++)
232 {
233 RTUINTPTR Value;
234 int rc = RTLdrGetSymbolEx(aLoads[i].hLdrMod, aLoads[i].pvBits, aLoads[i].Addr,
235 UINT32_MAX, aSyms[iSym].pszName, &Value);
236 if (RT_SUCCESS(rc))
237 {
238 unsigned off = Value - aLoads[i].Addr;
239 if (off < cbImage)
240 {
241 if (aSyms[iSym].off == ~0U)
242 aSyms[iSym].off = off;
243 else if (off != aSyms[iSym].off)
244 {
245 RTPrintf("tstLdr: Mismatching symbol '%s' in '%s'/%d. expected off=%d got %d\n",
246 aSyms[iSym].pszName, pszFilename, i, aSyms[iSym].off, off);
247 rcRet++;
248 }
249 }
250 else
251 {
252 RTPrintf("tstLdr: Invalid value for symbol '%s' in '%s'/%d. off=%#x Value=%#x\n",
253 aSyms[iSym].pszName, pszFilename, i, off, Value);
254 rcRet++;
255 }
256 }
257 else if (!g_fDontBitchOnResolveFailure)
258 {
259 RTPrintf("tstLdr: Failed to resolve symbol '%s' in '%s'/%d.\n", aSyms[iSym].pszName, pszFilename, i);
260 rcRet++;
261 }
262 }
263 }
264
265 if (iRel >= RT_ELEMENTS(aRels))
266 break;
267
268 /* adjust load address and announce our intentions */
269 if (g_cBits == 32)
270 aRels[iRel] &= UINT32_C(0xffffffff);
271
272 /* relocate it stuff. */
273 RTPrintf("tstLdr: Relocating image 2 from %RTptr to %RTptr\n", aLoads[2].Addr, aRels[iRel]);
274 int rc = RTLdrRelocate(aLoads[2].hLdrMod, aLoads[2].pvBits, aRels[iRel], aLoads[2].Addr, testGetImport, &aRels[iRel]);
275 if (RT_FAILURE(rc))
276 {
277 RTPrintf("tstLdr: Relocate of '%s' from %#x to %#x failed, rc=%Rrc. Aborting test.\n",
278 pszFilename, aRels[iRel], aLoads[2].Addr, rc);
279 rcRet++;
280 break;
281 }
282 aLoads[2].Addr = aRels[iRel];
283
284 /* next */
285 iRel++;
286 }
287 }
288
289 /*
290 * Clean up.
291 */
292 for (i = 0; i < RT_ELEMENTS(aLoads); i++)
293 {
294 if (aLoads[i].pvBits)
295 RTMemFree(aLoads[i].pvBits);
296 if (aLoads[i].hLdrMod)
297 {
298 int rc = RTLdrClose(aLoads[i].hLdrMod);
299 if (RT_FAILURE(rc))
300 {
301 RTPrintf("tstLdr: Failed to close '%s' i=%d, rc=%Rrc.\n", pszFilename, i, rc);
302 rcRet++;
303 }
304 }
305 }
306
307 return rcRet;
308}
309
310
311
312int main(int argc, char **argv)
313{
314 RTR3InitExe(argc, &argv, 0);
315
316 int rcRet = 0;
317 if (argc <= 1)
318 {
319 RTPrintf("usage: %s [-32|-64] [-kernel] <module> [more options/modules]\n", argv[0]);
320 return 1;
321 }
322
323 /*
324 * Iterate the files.
325 */
326 for (int argi = 1; argi < argc; argi++)
327 {
328 if (!strcmp(argv[argi], "-n"))
329 g_fDontBitchOnResolveFailure = true;
330 else if (!strcmp(argv[argi], "-32"))
331 g_cBits = 32;
332 else if (!strcmp(argv[argi], "-64"))
333 g_cBits = 64;
334 else if (!strcmp(argv[argi], "-kernel"))
335 g_fKernel = true;
336 else
337 {
338 RTPrintf("tstLdr: TESTING '%s'...\n", argv[argi]);
339 rcRet += testLdrOne(argv[argi]);
340 }
341 }
342
343 /*
344 * Test result summary.
345 */
346 if (!rcRet)
347 RTPrintf("tstLdr: SUCCESS\n");
348 else
349 RTPrintf("tstLdr: FAILURE - %d errors\n", rcRet);
350 return !!rcRet;
351}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette