1 | /* $Id: tstLdr-4.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Testcase for RTLdrOpen using ldrLdrObjR0.r0.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek 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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <iprt/ldr.h>
|
---|
23 | #include <iprt/alloc.h>
|
---|
24 | #include <iprt/stream.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <iprt/runtime.h>
|
---|
27 | #include <iprt/err.h>
|
---|
28 | #include <iprt/string.h>
|
---|
29 |
|
---|
30 |
|
---|
31 | extern "C" DECLEXPORT(int) DisasmTest1(void);
|
---|
32 |
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Resolve an external symbol during RTLdrGetBits().
|
---|
36 | *
|
---|
37 | * @returns iprt status code.
|
---|
38 | * @param hLdrMod The loader module handle.
|
---|
39 | * @param pszModule Module name.
|
---|
40 | * @param pszSymbol Symbol name, NULL if uSymbol should be used.
|
---|
41 | * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
|
---|
42 | * @param pValue Where to store the symbol value (address).
|
---|
43 | * @param pvUser User argument.
|
---|
44 | */
|
---|
45 | static DECLCALLBACK(int) testGetImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
|
---|
46 | {
|
---|
47 | if ( !strcmp(pszSymbol, "AssertMsg1") || !strcmp(pszSymbol, "_AssertMsg1"))
|
---|
48 | *pValue = (uintptr_t)AssertMsg1;
|
---|
49 | else if (!strcmp(pszSymbol, "AssertMsg2") || !strcmp(pszSymbol, "_AssertMsg2"))
|
---|
50 | *pValue = (uintptr_t)AssertMsg2;
|
---|
51 | else if (!strcmp(pszSymbol, "RTLogDefaultInstance") || !strcmp(pszSymbol, "_RTLogDefaultInstance"))
|
---|
52 | *pValue = (uintptr_t)RTLogDefaultInstance;
|
---|
53 | else if (!strcmp(pszSymbol, "MyPrintf") || !strcmp(pszSymbol, "_MyPrintf"))
|
---|
54 | *pValue = (uintptr_t)RTPrintf;
|
---|
55 | else
|
---|
56 | {
|
---|
57 | RTPrintf("tstLdr-4: Unexpected import '%s'!\n", pszSymbol);
|
---|
58 | return VERR_SYMBOL_NOT_FOUND;
|
---|
59 | }
|
---|
60 | return VINF_SUCCESS;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * One test iteration with one file.
|
---|
66 | *
|
---|
67 | * The test is very simple, we load the the file three times
|
---|
68 | * into two different regions. The first two into each of the
|
---|
69 | * regions the for compare usage. The third is loaded into one
|
---|
70 | * and then relocated between the two and other locations a few times.
|
---|
71 | *
|
---|
72 | * @returns number of errors.
|
---|
73 | * @param pszFilename The file to load the mess with.
|
---|
74 | */
|
---|
75 | static int testLdrOne(const char *pszFilename)
|
---|
76 | {
|
---|
77 | int cErrors = 0;
|
---|
78 | size_t cbImage = 0;
|
---|
79 | struct Load
|
---|
80 | {
|
---|
81 | RTLDRMOD hLdrMod;
|
---|
82 | void *pvBits;
|
---|
83 | const char *pszName;
|
---|
84 | } aLoads[6] =
|
---|
85 | {
|
---|
86 | { NULL, NULL, "foo" },
|
---|
87 | { NULL, NULL, "bar" },
|
---|
88 | { NULL, NULL, "foobar" },
|
---|
89 | { NULL, NULL, "kLdr-foo" },
|
---|
90 | { NULL, NULL, "kLdr-bar" },
|
---|
91 | { NULL, NULL, "kLdr-foobar" }
|
---|
92 | };
|
---|
93 | unsigned i;
|
---|
94 | int rc;
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Load them.
|
---|
98 | */
|
---|
99 | for (i = 0; i < ELEMENTS(aLoads); i++)
|
---|
100 | {
|
---|
101 | if (!strncmp(aLoads[i].pszName, "kLdr-", sizeof("kLdr-") - 1))
|
---|
102 | rc = RTLdrOpenkLdr(pszFilename, &aLoads[i].hLdrMod);
|
---|
103 | else
|
---|
104 | rc = RTLdrOpen(pszFilename, &aLoads[i].hLdrMod);
|
---|
105 | if (RT_FAILURE(rc))
|
---|
106 | {
|
---|
107 | RTPrintf("tstLdr-4: Failed to open '%s'/%d, rc=%Rrc. aborting test.\n", pszFilename, i, rc);
|
---|
108 | Assert(aLoads[i].hLdrMod == NIL_RTLDRMOD);
|
---|
109 | cErrors++;
|
---|
110 | break;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /* size it */
|
---|
114 | size_t cb = RTLdrSize(aLoads[i].hLdrMod);
|
---|
115 | if (cbImage && cb != cbImage)
|
---|
116 | {
|
---|
117 | RTPrintf("tstLdr-4: Size mismatch '%s'/%d. aborting test.\n", pszFilename, i);
|
---|
118 | cErrors++;
|
---|
119 | break;
|
---|
120 | }
|
---|
121 | cbImage = cb;
|
---|
122 |
|
---|
123 | /* Allocate bits. */
|
---|
124 | aLoads[i].pvBits = RTMemAlloc(cb);
|
---|
125 | if (!aLoads[i].pvBits)
|
---|
126 | {
|
---|
127 | RTPrintf("tstLdr-4: Out of memory '%s'/%d cbImage=%d. aborting test.\n", pszFilename, i, cbImage);
|
---|
128 | cErrors++;
|
---|
129 | break;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* Get the bits. */
|
---|
133 | rc = RTLdrGetBits(aLoads[i].hLdrMod, aLoads[i].pvBits, (uintptr_t)aLoads[i].pvBits, testGetImport, NULL);
|
---|
134 | if (RT_FAILURE(rc))
|
---|
135 | {
|
---|
136 | RTPrintf("tstLdr-4: Failed to get bits for '%s'/%d, rc=%Rrc. aborting test\n", pszFilename, i, rc);
|
---|
137 | cErrors++;
|
---|
138 | break;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * Execute the code.
|
---|
144 | */
|
---|
145 | if (!cErrors)
|
---|
146 | {
|
---|
147 | for (i = 0; i < ELEMENTS(aLoads); i += 1)
|
---|
148 | {
|
---|
149 | /* get the pointer. */
|
---|
150 | RTUINTPTR Value;
|
---|
151 | rc = RTLdrGetSymbolEx(aLoads[i].hLdrMod, aLoads[i].pvBits, (uintptr_t)aLoads[i].pvBits, "DisasmTest1", &Value);
|
---|
152 | if (rc == VERR_SYMBOL_NOT_FOUND)
|
---|
153 | rc = RTLdrGetSymbolEx(aLoads[i].hLdrMod, aLoads[i].pvBits, (uintptr_t)aLoads[i].pvBits, "_DisasmTest1", &Value);
|
---|
154 | if (RT_FAILURE(rc))
|
---|
155 | {
|
---|
156 | RTPrintf("tstLdr-4: Failed to get symbol \"DisasmTest1\" from load #%d: %Rrc\n", i, rc);
|
---|
157 | cErrors++;
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | DECLCALLBACKPTR(int, pfnDisasmTest1)(void) = (DECLCALLBACKPTR(int, )(void))(uintptr_t)Value; /* eeeh. */
|
---|
161 | RTPrintf("tstLdr-4: pfnDisasmTest1=%p / add-symbol-file %s %#x\n", pfnDisasmTest1, pszFilename, aLoads[i].pvBits);
|
---|
162 |
|
---|
163 | /* call the test function. */
|
---|
164 | rc = pfnDisasmTest1();
|
---|
165 | if (rc)
|
---|
166 | {
|
---|
167 | RTPrintf("tstLdr-4: load #%d Test1 -> %#x\n", i, rc);
|
---|
168 | cErrors++;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * Clean up.
|
---|
176 | */
|
---|
177 | for (i = 0; i < ELEMENTS(aLoads); i++)
|
---|
178 | {
|
---|
179 | if (aLoads[i].pvBits)
|
---|
180 | RTMemFree(aLoads[i].pvBits);
|
---|
181 | if (aLoads[i].hLdrMod)
|
---|
182 | {
|
---|
183 | int rc = RTLdrClose(aLoads[i].hLdrMod);
|
---|
184 | if (RT_FAILURE(rc))
|
---|
185 | {
|
---|
186 | RTPrintf("tstLdr-4: Failed to close '%s' i=%d, rc=%Rrc.\n", pszFilename, i, rc);
|
---|
187 | cErrors++;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | return cErrors;
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 |
|
---|
197 | int main(int argc, char **argv)
|
---|
198 | {
|
---|
199 | int cErrors = 0;
|
---|
200 | RTR3Init();
|
---|
201 |
|
---|
202 | /*
|
---|
203 | * Sanity check.
|
---|
204 | */
|
---|
205 | int rc = DisasmTest1();
|
---|
206 | if (rc)
|
---|
207 | {
|
---|
208 | RTPrintf("tstLdr-4: FATAL ERROR - DisasmTest1 is buggy: rc=%#x\n", rc);
|
---|
209 | return 1;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /*
|
---|
213 | * Execute the test.
|
---|
214 | */
|
---|
215 | char szPath[RTPATH_MAX];
|
---|
216 | rc = RTPathProgram(szPath, sizeof(szPath) - sizeof("/tstLdrObjR0.r0"));
|
---|
217 | if (RT_SUCCESS(rc))
|
---|
218 | {
|
---|
219 | strcat(szPath, "/tstLdrObjR0.r0");
|
---|
220 | RTPrintf("tstLdr-4: TESTING '%s'...\n", szPath);
|
---|
221 | cErrors += testLdrOne(szPath);
|
---|
222 | }
|
---|
223 | else
|
---|
224 | {
|
---|
225 | RTPrintf("tstLdr-4: RTPathProgram -> %Rrc\n", rc);
|
---|
226 | cErrors++;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Test result summary.
|
---|
231 | */
|
---|
232 | if (!cErrors)
|
---|
233 | RTPrintf("tstLdr-4: SUCCESS\n");
|
---|
234 | else
|
---|
235 | RTPrintf("tstLdr-4: FAILURE - %d errors\n", cErrors);
|
---|
236 | return !!cErrors;
|
---|
237 | }
|
---|