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