VirtualBox

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

Last change on this file since 23299 was 19924, checked in by vboxsync, 16 years ago

IPRT,SUP: Renamed RTPathProgram to RTPathExecDir to make it clear what it returns. Renamed hardened version of it as well.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.3 KB
Line 
1/* $Id: tstLdr-4.cpp 19924 2009-05-22 21:52:47Z 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, "RTLogLoggerExV") || !strcmp(pszSymbol, "_RTLogLoggerExV"))
70 *pValue = (uintptr_t)RTLogLoggerExV;
71 else if (!strcmp(pszSymbol, "RTLogPrintfV") || !strcmp(pszSymbol, "_RTLogPrintfV"))
72 *pValue = (uintptr_t)RTLogPrintfV;
73 else if (!strcmp(pszSymbol, "RTR0AssertPanicSystem")|| !strcmp(pszSymbol, "_RTR0AssertPanicSystem"))
74 *pValue = (uintptr_t)0;
75 else if (!strcmp(pszSymbol, "MyPrintf") || !strcmp(pszSymbol, "_MyPrintf"))
76 *pValue = (uintptr_t)RTPrintf;
77 else
78 {
79 RTPrintf("tstLdr-4: Unexpected import '%s'!\n", pszSymbol);
80 return VERR_SYMBOL_NOT_FOUND;
81 }
82 return VINF_SUCCESS;
83}
84
85
86/**
87 * One test iteration with one file.
88 *
89 * The test is very simple, we load the file three times
90 * into two different regions. The first two into each of the
91 * regions the for compare usage. The third is loaded into one
92 * and then relocated between the two and other locations a few times.
93 *
94 * @returns number of errors.
95 * @param pszFilename The file to load the mess with.
96 */
97static int testLdrOne(const char *pszFilename)
98{
99 int cErrors = 0;
100 size_t cbImage = 0;
101 struct Load
102 {
103 RTLDRMOD hLdrMod;
104 void *pvBits;
105 const char *pszName;
106 } aLoads[6] =
107 {
108 { NULL, NULL, "foo" },
109 { NULL, NULL, "bar" },
110 { NULL, NULL, "foobar" },
111 { NULL, NULL, "kLdr-foo" },
112 { NULL, NULL, "kLdr-bar" },
113 { NULL, NULL, "kLdr-foobar" }
114 };
115 unsigned i;
116 int rc;
117
118 /*
119 * Load them.
120 */
121 for (i = 0; i < RT_ELEMENTS(aLoads); i++)
122 {
123 if (!strncmp(aLoads[i].pszName, "kLdr-", sizeof("kLdr-") - 1))
124 rc = RTLdrOpenkLdr(pszFilename, 0, RTLDRARCH_WHATEVER, &aLoads[i].hLdrMod);
125 else
126 rc = RTLdrOpen(pszFilename, 0, RTLDRARCH_WHATEVER, &aLoads[i].hLdrMod);
127 if (RT_FAILURE(rc))
128 {
129 RTPrintf("tstLdr-4: Failed to open '%s'/%d, rc=%Rrc. aborting test.\n", pszFilename, i, rc);
130 Assert(aLoads[i].hLdrMod == NIL_RTLDRMOD);
131 cErrors++;
132 break;
133 }
134
135 /* size it */
136 size_t cb = RTLdrSize(aLoads[i].hLdrMod);
137 if (cbImage && cb != cbImage)
138 {
139 RTPrintf("tstLdr-4: Size mismatch '%s'/%d. aborting test.\n", pszFilename, i);
140 cErrors++;
141 break;
142 }
143 cbImage = cb;
144
145 /* Allocate bits. */
146 aLoads[i].pvBits = RTMemExecAlloc(cb);
147 if (!aLoads[i].pvBits)
148 {
149 RTPrintf("tstLdr-4: Out of memory '%s'/%d cbImage=%d. aborting test.\n", pszFilename, i, cbImage);
150 cErrors++;
151 break;
152 }
153
154 /* Get the bits. */
155 rc = RTLdrGetBits(aLoads[i].hLdrMod, aLoads[i].pvBits, (uintptr_t)aLoads[i].pvBits, testGetImport, NULL);
156 if (RT_FAILURE(rc))
157 {
158 RTPrintf("tstLdr-4: Failed to get bits for '%s'/%d, rc=%Rrc. aborting test\n", pszFilename, i, rc);
159 cErrors++;
160 break;
161 }
162 }
163
164 /*
165 * Execute the code.
166 */
167 if (!cErrors)
168 {
169 for (i = 0; i < RT_ELEMENTS(aLoads); i += 1)
170 {
171 /* get the pointer. */
172 RTUINTPTR Value;
173 rc = RTLdrGetSymbolEx(aLoads[i].hLdrMod, aLoads[i].pvBits, (uintptr_t)aLoads[i].pvBits, "DisasmTest1", &Value);
174 if (rc == VERR_SYMBOL_NOT_FOUND)
175 rc = RTLdrGetSymbolEx(aLoads[i].hLdrMod, aLoads[i].pvBits, (uintptr_t)aLoads[i].pvBits, "_DisasmTest1", &Value);
176 if (RT_FAILURE(rc))
177 {
178 RTPrintf("tstLdr-4: Failed to get symbol \"DisasmTest1\" from load #%d: %Rrc\n", i, rc);
179 cErrors++;
180 break;
181 }
182 DECLCALLBACKPTR(int, pfnDisasmTest1)(void) = (DECLCALLBACKPTR(int, RT_NOTHING)(void))(uintptr_t)Value; /* eeeh. */
183 RTPrintf("tstLdr-4: pfnDisasmTest1=%p / add-symbol-file %s %#x\n", pfnDisasmTest1, pszFilename, aLoads[i].pvBits);
184
185 /* call the test function. */
186 rc = pfnDisasmTest1();
187 if (rc)
188 {
189 RTPrintf("tstLdr-4: load #%d Test1 -> %#x\n", i, rc);
190 cErrors++;
191 }
192 }
193 }
194
195
196 /*
197 * Clean up.
198 */
199 for (i = 0; i < RT_ELEMENTS(aLoads); i++)
200 {
201 if (aLoads[i].pvBits)
202 RTMemExecFree(aLoads[i].pvBits);
203 if (aLoads[i].hLdrMod)
204 {
205 int rc = RTLdrClose(aLoads[i].hLdrMod);
206 if (RT_FAILURE(rc))
207 {
208 RTPrintf("tstLdr-4: Failed to close '%s' i=%d, rc=%Rrc.\n", pszFilename, i, rc);
209 cErrors++;
210 }
211 }
212 }
213
214 return cErrors;
215}
216
217
218
219int main(int argc, char **argv)
220{
221 int cErrors = 0;
222 RTR3Init();
223
224 /*
225 * Sanity check.
226 */
227 int rc = DisasmTest1();
228 if (rc)
229 {
230 RTPrintf("tstLdr-4: FATAL ERROR - DisasmTest1 is buggy: rc=%#x\n", rc);
231 return 1;
232 }
233
234 /*
235 * Execute the test.
236 */
237 char szPath[RTPATH_MAX];
238 rc = RTPathExecDir(szPath, sizeof(szPath) - sizeof("/tstLdrObjR0.r0"));
239 if (RT_SUCCESS(rc))
240 {
241 strcat(szPath, "/tstLdrObjR0.r0");
242 RTPrintf("tstLdr-4: TESTING '%s'...\n", szPath);
243 cErrors += testLdrOne(szPath);
244 }
245 else
246 {
247 RTPrintf("tstLdr-4: RTPathExecDir -> %Rrc\n", rc);
248 cErrors++;
249 }
250
251 /*
252 * Test result summary.
253 */
254 if (!cErrors)
255 RTPrintf("tstLdr-4: SUCCESS\n");
256 else
257 RTPrintf("tstLdr-4: FAILURE - %d errors\n", cErrors);
258 return !!cErrors;
259}
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