VirtualBox

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

Last change on this file since 13068 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

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