VirtualBox

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

Last change on this file since 15424 was 14831, checked in by vboxsync, 16 years ago

whole bunch: avoid runtime.h, include individual headers indead.

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