VirtualBox

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

Last change on this file since 1507 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.3 KB
Line 
1/* $Id: tstLdr.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - Testcase for parts of RTLdr*.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include <iprt/ldr.h>
27#include <iprt/alloc.h>
28#include <iprt/stream.h>
29#include <iprt/assert.h>
30#include <iprt/runtime.h>
31#include <iprt/err.h>
32#include <iprt/string.h>
33
34
35/*******************************************************************************
36* Global Variables *
37*******************************************************************************/
38/** If set, don't bitch when failing to resolve symbols. */
39static bool g_fDontBitchOnResolveFailure = false;
40
41/**
42 * Resolve an external symbol during RTLdrGetBits().
43 *
44 * @returns iprt status code.
45 * @param hLdrMod The loader module handle.
46 * @param pszModule Module name.
47 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
48 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
49 * @param pValue Where to store the symbol value (address).
50 * @param pvUser User argument.
51 */
52static DECLCALLBACK(int) testGetImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
53{
54 /* check the name format and only permit certain names */
55 *pValue = 0xabcdef0f;
56 return VINF_SUCCESS;
57}
58
59
60/**
61 * One test iteration with one file.
62 *
63 * The test is very simple, we load the the file three times
64 * into two different regions. The first two into each of the
65 * regions the for compare usage. The third is loaded into one
66 * and then relocated between the two and other locations a few times.
67 *
68 * @returns number of errors.
69 * @param pszFilename The file to load the mess with.
70 */
71static int testLdrOne(const char *pszFilename)
72{
73 int rcRet = 0;
74 size_t cbImage = 0;
75 struct Load
76 {
77 RTLDRMOD hLdrMod;
78 void *pvBits;
79 RTUINTPTR Addr;
80 char *pszName;
81 } aLoads[6] =
82 {
83 { NULL, NULL, 0xefefef00, "foo" },
84 { NULL, NULL, 0x40404040, "bar" },
85 { NULL, NULL, 0xefefef00, "foobar" },
86 { NULL, NULL, 0xefefef00, "kLdr-foo" },
87 { NULL, NULL, 0x40404040, "kLdr-bar" },
88 { NULL, NULL, 0xefefef00, "kLdr-foobar" }
89 };
90 unsigned i;
91
92 /*
93 * Load them.
94 */
95 for (i = 0; i < ELEMENTS(aLoads); i++)
96 {
97 int rc;
98 if (!strncmp(aLoads[i].pszName, "kLdr-", sizeof("kLdr-") - 1))
99 rc = RTLdrOpenkLdr(pszFilename, &aLoads[i].hLdrMod);
100 else
101 rc = RTLdrOpen(pszFilename, &aLoads[i].hLdrMod);
102 if (RT_FAILURE(rc))
103 {
104 RTPrintf("tstLdr: Failed to open '%s'/%d, rc=%Rrc. aborting test.\n", pszFilename, i, rc);
105 Assert(aLoads[i].hLdrMod == NIL_RTLDRMOD);
106 rcRet++;
107 break;
108 }
109
110 /* size it */
111 size_t cb = RTLdrSize(aLoads[i].hLdrMod);
112 if (cbImage && cb != cbImage)
113 {
114 RTPrintf("tstLdr: Size mismatch '%s'/%d. aborting test.\n", pszFilename, i);
115 rcRet++;
116 break;
117 }
118 cbImage = cb;
119
120 /* Allocate bits. */
121 aLoads[i].pvBits = RTMemAlloc(cb);
122 if (!aLoads[i].pvBits)
123 {
124 RTPrintf("tstLdr: Out of memory '%s'/%d cbImage=%d. aborting test.\n", pszFilename, i, cbImage);
125 rcRet++;
126 break;
127 }
128
129 /* Get the bits. */
130 rc = RTLdrGetBits(aLoads[i].hLdrMod, aLoads[i].pvBits, aLoads[i].Addr, testGetImport, NULL);
131 if (RT_FAILURE(rc))
132 {
133 RTPrintf("tstLdr: Failed to get bits for '%s'/%d, rc=%Rrc. aborting test\n", pszFilename, i, rc);
134 rcRet++;
135 break;
136 }
137 }
138
139 /*
140 * Continue with the relocations and symbol resolving.
141 */
142 if (!rcRet)
143 {
144 static RTUINTPTR aRels[] =
145 {
146 0xefefef00, /* same. */
147 0x40404040, /* the other. */
148 0xefefef00, /* back. */
149 0x40404040, /* the other. */
150 0xefefef00, /* back again. */
151 0x77773420, /* somewhere entirely else. */
152 0xf0000000, /* somewhere entirely else. */
153 0x40404040, /* the other. */
154 0xefefef00 /* back again. */
155 };
156 struct Symbols
157 {
158 /** The symbol offset. -1 indicates the first time. */
159 unsigned off;
160 /** The symbol name. */
161 const char *pszName;
162 } aSyms[] =
163 {
164 { ~0, "Entrypoint" },
165 { ~0, "SomeExportFunction1" },
166 { ~0, "SomeExportFunction2" },
167 { ~0, "SomeExportFunction3" },
168 { ~0, "SomeExportFunction4" },
169 { ~0, "SomeExportFunction5" },
170 { ~0, "SomeExportFunction5" },
171 { ~0, "DISCoreOne" }
172 };
173
174 unsigned iRel = 0;
175 for (;;)
176 {
177 /* Compare all which are at the same address. */
178 for (i = 0; i < ELEMENTS(aLoads) - 1; i++)
179 {
180 for (unsigned j = i + 1; j < ELEMENTS(aLoads); j++)
181 {
182 if (aLoads[j].Addr == aLoads[i].Addr)
183 {
184 if (memcmp(aLoads[j].pvBits, aLoads[i].pvBits, cbImage))
185 {
186 RTPrintf("tstLdr: Mismatch between load %d and %d. ('%s')\n", j, i, pszFilename);
187 const uint8_t *pu8J = (const uint8_t *)aLoads[j].pvBits;
188 const uint8_t *pu8I = (const uint8_t *)aLoads[i].pvBits;
189 for (uint32_t off = 0; off < cbImage; off++, pu8J++, pu8I++)
190 if (*pu8J != *pu8I)
191 RTPrintf(" %08x %02x != %02x\n", off, *pu8J, *pu8I);
192 rcRet++;
193 }
194 }
195 }
196 }
197
198 /* compare symbols. */
199 for (i = 0; i < ELEMENTS(aLoads); i++)
200 {
201 for (unsigned iSym = 0; iSym < ELEMENTS(aSyms); iSym++)
202 {
203 RTUINTPTR Value;
204 int rc = RTLdrGetSymbolEx(aLoads[i].hLdrMod, aLoads[i].pvBits, aLoads[i].Addr, aSyms[iSym].pszName, &Value);
205 if (RT_SUCCESS(rc))
206 {
207 unsigned off = Value - aLoads[i].Addr;
208 if (off < cbImage)
209 {
210 if (aSyms[iSym].off == ~0U)
211 aSyms[iSym].off = off;
212 else if (off != aSyms[iSym].off)
213 {
214 RTPrintf("tstLdr: Mismatching symbol '%s' in '%s'/%d. expected off=%d got %d\n",
215 aSyms[iSym].pszName, pszFilename, i, aSyms[iSym].off, off);
216 rcRet++;
217 }
218 }
219 else
220 {
221 RTPrintf("tstLdr: Invalid value for symbol '%s' in '%s'/%d. off=%#x Value=%#x\n",
222 aSyms[iSym].pszName, pszFilename, i, off, Value);
223 rcRet++;
224 }
225 }
226 else if (!g_fDontBitchOnResolveFailure)
227 {
228 RTPrintf("tstLdr: Failed to resolve symbol '%s' in '%s'/%d.\n", aSyms[iSym].pszName, pszFilename, i);
229 rcRet++;
230 }
231 }
232 }
233
234 if (iRel >= ELEMENTS(aRels))
235 break;
236
237 /* relocate it stuff. */
238 int rc = RTLdrRelocate(aLoads[2].hLdrMod, aLoads[2].pvBits, aRels[iRel], aLoads[2].Addr, testGetImport, NULL);
239 if (RT_FAILURE(rc))
240 {
241 RTPrintf("tstLdr: Relocate of '%s' from %#x to %#x failed, rc=%Rrc. Aborting test.\n",
242 pszFilename, aRels[iRel], aLoads[2].Addr, rc);
243 rcRet++;
244 break;
245 }
246 aLoads[2].Addr = aRels[iRel];
247
248 /* next */
249 iRel++;
250 }
251 }
252
253 /*
254 * Clean up.
255 */
256 for (i = 0; i < ELEMENTS(aLoads); i++)
257 {
258 if (aLoads[i].pvBits)
259 RTMemFree(aLoads[i].pvBits);
260 if (aLoads[i].hLdrMod)
261 {
262 int rc = RTLdrClose(aLoads[i].hLdrMod);
263 if (RT_FAILURE(rc))
264 {
265 RTPrintf("tstLdr: Failed to close '%s' i=%d, rc=%Rrc.\n", pszFilename, i, rc);
266 rcRet++;
267 }
268 }
269 }
270
271 return rcRet;
272}
273
274
275
276int main(int argc, char **argv)
277{
278 RTR3Init();
279
280 int rcRet = 0;
281 if (argc <= 1)
282 {
283 RTPrintf("usage: %s <module> [more modules]\n", argv[0]);
284 return 1;
285 }
286
287 /*
288 * Iterate the files.
289 */
290 for (int argi = 1; argi < argc; argi++)
291 {
292 if (argv[argi][0] == '-' && argv[argi][1] == 'n')
293 g_fDontBitchOnResolveFailure = true;
294 else
295 {
296 RTPrintf("tstLdr: TESTING '%s'...\n", argv[argi]);
297 rcRet += testLdrOne(argv[argi]);
298 }
299 }
300
301 /*
302 * Test result summary.
303 */
304 if (!rcRet)
305 RTPrintf("tstLdr: SUCCESS\n");
306 else
307 RTPrintf("tstLdr: FAILURE - %d errors\n", rcRet);
308 return !!rcRet;
309}
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