VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstLdr-4.cpp@ 68704

Last change on this file since 68704 was 66368, checked in by vboxsync, 8 years ago

tstLdr-4.cpp: oops, build fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.2 KB
Line 
1/* $Id: tstLdr-4.cpp 66368 2017-03-30 14:10:13Z vboxsync $ */
2/** @file
3 * IPRT - Testcase for RTLdrOpen using ldrLdrObjR0.r0.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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/log.h>
34#include <iprt/stream.h>
35#include <iprt/assert.h>
36#include <iprt/param.h>
37#include <iprt/path.h>
38#include <iprt/initterm.h>
39#include <iprt/err.h>
40#include <iprt/string.h>
41
42
43extern "C" DECLEXPORT(int) DisasmTest1(void);
44
45
46/**
47 * Resolve an external symbol during RTLdrGetBits().
48 *
49 * @returns iprt status code.
50 * @param hLdrMod The loader module handle.
51 * @param pszModule Module name.
52 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
53 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
54 * @param pValue Where to store the symbol value (address).
55 * @param pvUser User argument.
56 */
57static DECLCALLBACK(int) testGetImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
58{
59 RT_NOREF4(hLdrMod, pszModule, uSymbol, pvUser);
60 if ( !strcmp(pszSymbol, "RTAssertMsg1Weak") || !strcmp(pszSymbol, "_RTAssertMsg1Weak"))
61 *pValue = (uintptr_t)RTAssertMsg1Weak;
62 else if (!strcmp(pszSymbol, "RTAssertMsg2Weak") || !strcmp(pszSymbol, "_RTAssertMsg2Weak"))
63 *pValue = (uintptr_t)RTAssertMsg1Weak;
64 else if (!strcmp(pszSymbol, "RTAssertMsg1") || !strcmp(pszSymbol, "_RTAssertMsg1"))
65 *pValue = (uintptr_t)RTAssertMsg1;
66 else if (!strcmp(pszSymbol, "RTAssertMsg2") || !strcmp(pszSymbol, "_RTAssertMsg2"))
67 *pValue = (uintptr_t)RTAssertMsg2;
68 else if (!strcmp(pszSymbol, "RTAssertMsg2V") || !strcmp(pszSymbol, "_RTAssertMsg2V"))
69 *pValue = (uintptr_t)RTAssertMsg2V;
70 else if (!strcmp(pszSymbol, "RTAssertMayPanic") || !strcmp(pszSymbol, "_RTAssertMayPanic"))
71 *pValue = (uintptr_t)RTAssertMayPanic;
72 else if (!strcmp(pszSymbol, "RTLogDefaultInstanceEx") || !strcmp(pszSymbol, "RTLogDefaultInstanceEx"))
73 *pValue = (uintptr_t)RTLogDefaultInstanceEx;
74 else if (!strcmp(pszSymbol, "RTLogLoggerExV") || !strcmp(pszSymbol, "_RTLogLoggerExV"))
75 *pValue = (uintptr_t)RTLogLoggerExV;
76 else if (!strcmp(pszSymbol, "RTLogPrintfV") || !strcmp(pszSymbol, "_RTLogPrintfV"))
77 *pValue = (uintptr_t)RTLogPrintfV;
78 else if (!strcmp(pszSymbol, "RTR0AssertPanicSystem")|| !strcmp(pszSymbol, "_RTR0AssertPanicSystem"))
79 *pValue = (uintptr_t)0;
80 else if (!strcmp(pszSymbol, "MyPrintf") || !strcmp(pszSymbol, "_MyPrintf"))
81 *pValue = (uintptr_t)RTPrintf;
82 else if (!strcmp(pszSymbol, "SomeImportFunction") || !strcmp(pszSymbol, "_SomeImportFunction"))
83 *pValue = (uintptr_t)0;
84 else
85 {
86 RTPrintf("tstLdr-4: Unexpected import '%s'!\n", pszSymbol);
87 return VERR_SYMBOL_NOT_FOUND;
88 }
89 return VINF_SUCCESS;
90}
91
92
93/**
94 * One test iteration with one file.
95 *
96 * The test is very simple, we load the file three times
97 * into two different regions. The first two into each of the
98 * regions the for compare usage. The third is loaded into one
99 * and then relocated between the two and other locations a few times.
100 *
101 * @returns number of errors.
102 * @param pszFilename The file to load the mess with.
103 */
104static int testLdrOne(const char *pszFilename)
105{
106 int cErrors = 0;
107 size_t cbImage = 0;
108 struct Load
109 {
110 RTLDRMOD hLdrMod;
111 void *pvBits;
112 size_t cbBits;
113 const char *pszName;
114 } aLoads[6] =
115 {
116 { NULL, NULL, 0, "foo" },
117 { NULL, NULL, 0, "bar" },
118 { NULL, NULL, 0, "foobar" },
119 { NULL, NULL, 0, "kLdr-foo" },
120 { NULL, NULL, 0, "kLdr-bar" },
121 { NULL, NULL, 0, "kLdr-foobar" }
122 };
123 unsigned i;
124 int rc;
125
126 /*
127 * Load them.
128 */
129 for (i = 0; i < RT_ELEMENTS(aLoads); i++)
130 {
131 if (!strncmp(aLoads[i].pszName, RT_STR_TUPLE("kLdr-")))
132 {
133 rc = RTLdrOpenkLdr(pszFilename, 0, RTLDRARCH_WHATEVER, &aLoads[i].hLdrMod);
134 if (rc == VERR_ELF_EXE_NOT_SUPPORTED)
135 continue;
136 }
137 else
138 rc = RTLdrOpen(pszFilename, 0, RTLDRARCH_WHATEVER, &aLoads[i].hLdrMod);
139 if (RT_FAILURE(rc))
140 {
141 RTPrintf("tstLdr-4: Failed to open '%s'/%d, rc=%Rrc. aborting test.\n", pszFilename, i, rc);
142 Assert(aLoads[i].hLdrMod == NIL_RTLDRMOD);
143 cErrors++;
144 break;
145 }
146
147 /* size it */
148 size_t cb = RTLdrSize(aLoads[i].hLdrMod);
149 if (cbImage && cb != cbImage)
150 {
151 RTPrintf("tstLdr-4: Size mismatch '%s'/%d. aborting test.\n", pszFilename, i);
152 cErrors++;
153 break;
154 }
155 aLoads[i].cbBits = cbImage = cb;
156
157 /* Allocate bits. */
158 aLoads[i].pvBits = RTMemExecAlloc(cb);
159 if (!aLoads[i].pvBits)
160 {
161 RTPrintf("tstLdr-4: Out of memory '%s'/%d cbImage=%d. aborting test.\n", pszFilename, i, cbImage);
162 cErrors++;
163 break;
164 }
165
166 /* Get the bits. */
167 rc = RTLdrGetBits(aLoads[i].hLdrMod, aLoads[i].pvBits, (uintptr_t)aLoads[i].pvBits, testGetImport, NULL);
168 if (RT_FAILURE(rc))
169 {
170 RTPrintf("tstLdr-4: Failed to get bits for '%s'/%d, rc=%Rrc. aborting test\n", pszFilename, i, rc);
171 cErrors++;
172 break;
173 }
174 }
175
176 /*
177 * Execute the code.
178 */
179 if (!cErrors)
180 {
181 for (i = 0; i < RT_ELEMENTS(aLoads); i += 1)
182 {
183 /* VERR_ELF_EXE_NOT_SUPPORTED in the previous loop? */
184 if (!aLoads[i].hLdrMod)
185 continue;
186 /* get the pointer. */
187 RTUINTPTR Value;
188 rc = RTLdrGetSymbolEx(aLoads[i].hLdrMod, aLoads[i].pvBits, (uintptr_t)aLoads[i].pvBits,
189 UINT32_MAX, "DisasmTest1", &Value);
190 if (rc == VERR_SYMBOL_NOT_FOUND)
191 rc = RTLdrGetSymbolEx(aLoads[i].hLdrMod, aLoads[i].pvBits, (uintptr_t)aLoads[i].pvBits,
192 UINT32_MAX, "_DisasmTest1", &Value);
193 if (RT_FAILURE(rc))
194 {
195 RTPrintf("tstLdr-4: Failed to get symbol \"DisasmTest1\" from load #%d: %Rrc\n", i, rc);
196 cErrors++;
197 break;
198 }
199 DECLCALLBACKPTR(int, pfnDisasmTest1)(void) = (DECLCALLBACKPTR(int, RT_NOTHING)(void))(uintptr_t)Value; /* eeeh. */
200 RTPrintf("tstLdr-4: pfnDisasmTest1=%p / add-symbol-file %s %#x\n", pfnDisasmTest1, pszFilename, aLoads[i].pvBits);
201
202 /* call the test function. */
203 rc = pfnDisasmTest1();
204 if (rc)
205 {
206 RTPrintf("tstLdr-4: load #%d Test1 -> %#x\n", i, rc);
207 cErrors++;
208 }
209
210 /* While we're here, check a couple of RTLdrQueryProp calls too */
211 void *pvBits = aLoads[i].pvBits;
212 for (unsigned iBits = 0; iBits < 2; iBits++, pvBits = NULL)
213 {
214 union
215 {
216 char szName[127];
217 } uBuf;
218 rc = RTLdrQueryPropEx(aLoads[i].hLdrMod, RTLDRPROP_INTERNAL_NAME, aLoads[i].pvBits,
219 uBuf.szName, sizeof(uBuf.szName), NULL);
220 if (RT_SUCCESS(rc))
221 RTPrintf("tstLdr-4: internal name #%d: '%s'\n", i, uBuf.szName);
222 else if (rc != VERR_NOT_FOUND && rc != VERR_NOT_SUPPORTED)
223 RTPrintf("tstLdr-4: internal name #%d failed: %Rrc\n", i, rc);
224 }
225 }
226 }
227
228 /*
229 * Clean up.
230 */
231 for (i = 0; i < RT_ELEMENTS(aLoads); i++)
232 {
233 if (aLoads[i].pvBits)
234 RTMemExecFree(aLoads[i].pvBits, aLoads[i].cbBits);
235 if (aLoads[i].hLdrMod)
236 {
237 rc = RTLdrClose(aLoads[i].hLdrMod);
238 if (RT_FAILURE(rc))
239 {
240 RTPrintf("tstLdr-4: Failed to close '%s' i=%d, rc=%Rrc.\n", pszFilename, i, rc);
241 cErrors++;
242 }
243 }
244 }
245
246 return cErrors;
247}
248
249
250
251int main(int argc, char **argv)
252{
253 int cErrors = 0;
254 RTR3InitExe(argc, &argv, 0);
255
256 /*
257 * Sanity check.
258 */
259 int rc = DisasmTest1();
260 if (rc)
261 {
262 RTPrintf("tstLdr-4: FATAL ERROR - DisasmTest1 is buggy: rc=%#x\n", rc);
263 return 1;
264 }
265
266 /*
267 * Execute the test.
268 */
269 char szPath[RTPATH_MAX];
270 rc = RTPathExecDir(szPath, sizeof(szPath) - sizeof("/tstLdrObjR0.r0"));
271 if (RT_SUCCESS(rc))
272 {
273 strcat(szPath, "/tstLdrObjR0.r0");
274 RTPrintf("tstLdr-4: TESTING '%s'...\n", szPath);
275 cErrors += testLdrOne(szPath);
276 }
277 else
278 {
279 RTPrintf("tstLdr-4: RTPathExecDir -> %Rrc\n", rc);
280 cErrors++;
281 }
282
283 /*
284 * Test result summary.
285 */
286 if (!cErrors)
287 RTPrintf("tstLdr-4: SUCCESS\n");
288 else
289 RTPrintf("tstLdr-4: FAILURE - %d errors\n", cErrors);
290 return !!cErrors;
291}
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