VirtualBox

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

Last change on this file since 53428 was 52213, checked in by vboxsync, 10 years ago

SUP,IPRT: Implemented forwarder support in RTLdr and cleaned up some the ordinal mess. Resolved imports when doing the process verification/purification runs other than SUPHARDNTVPKIND_CHILD_PURIFICATION. This is necessary since 32-bit windows combine .text with .rdata, and we don't want to overwrite the import table after it has been snapped. Include read-only sections in the verfication runs.

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