VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstLdr-2.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: 6.7 KB
Line 
1/* $Id: tstLdr-2.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
2/** @file
3 * IPRT - Testcase for parts of RTLdr*, manual inspection.
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 <VBox/dis.h>
41#include <iprt/err.h>
42#include <iprt/string.h>
43
44
45bool MyDisBlock(PDISCPUSTATE pCpu, RTHCUINTPTR pvCodeBlock, int32_t cbMax, RTUINTPTR off)
46{
47 int32_t i = 0;
48 while (i < cbMax)
49 {
50 char szOutput[256];
51 uint32_t cbInstr;
52 if (RT_FAILURE(DISInstr(pCpu, pvCodeBlock + i, off, &cbInstr, szOutput)))
53 return false;
54
55 RTPrintf("%s", szOutput);
56
57 /* next */
58 i += cbInstr;
59 }
60 return true;
61}
62
63
64
65/**
66 * Resolve an external symbol during RTLdrGetBits().
67 *
68 * @returns iprt status code.
69 * @param hLdrMod The loader module handle.
70 * @param pszModule Module name.
71 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
72 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
73 * @param pValue Where to store the symbol value (address).
74 * @param pvUser User argument.
75 */
76static DECLCALLBACK(int) testGetImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
77{
78 /* check the name format and only permit certain names */
79 *pValue = 0xf0f0f0f0;
80 return VINF_SUCCESS;
81}
82
83
84/**
85 * One test iteration with one file.
86 *
87 * The test is very simple, we load the the file three times
88 * into two different regions. The first two into each of the
89 * regions the for compare usage. The third is loaded into one
90 * and then relocated between the two and other locations a few times.
91 *
92 * @returns number of errors.
93 * @param pszFilename The file to load the mess with.
94 */
95static int testLdrOne(const char *pszFilename)
96{
97 RTLDRMOD hLdrMod;
98 int rc = RTLdrOpen(pszFilename, &hLdrMod);
99 if (RT_FAILURE(rc))
100 {
101 RTPrintf("tstLdr: Failed to open '%s', rc=%Vrc. aborting test.\n", pszFilename, rc);
102 Assert(hLdrMod == NIL_RTLDRMOD);
103 return 1;
104 }
105
106 int rcRet = 1;
107 size_t cb = RTLdrSize(hLdrMod);
108 if (cb > 100)
109 {
110 void *pvBits = RTMemAlloc(cb);
111 if (pvBits)
112 {
113 RTUINTPTR Addr = 0xc0000000;
114 rc = RTLdrGetBits(hLdrMod, pvBits, Addr, testGetImport, NULL);
115 if (RT_SUCCESS(rc))
116 {
117 RTUINTPTR Value;
118 rc = RTLdrGetSymbolEx(hLdrMod, pvBits, Addr, "Entrypoint", &Value);
119 if (RT_SUCCESS(rc))
120 {
121 unsigned off = Value - Addr;
122 if (off < cb)
123 {
124 DISCPUSTATE Cpu;
125
126 memset(&Cpu, 0, sizeof(Cpu));
127 Cpu.mode = CPUMODE_32BIT;
128 if (MyDisBlock(&Cpu, (RTUINTPTR)pvBits + off, 200, Addr - (uintptr_t)pvBits))
129 {
130 RTUINTPTR Addr2 = 0xd0000000;
131 rc = RTLdrRelocate(hLdrMod, pvBits, Addr2, Addr, testGetImport, NULL);
132 if (RT_SUCCESS(rc))
133 {
134 if (MyDisBlock(&Cpu, (RTUINTPTR)pvBits + off, 200, Addr2 - (uintptr_t)pvBits))
135 rcRet = 0;
136 else
137 RTPrintf("tstLdr: Disassembly failed!\n");
138 }
139 else
140 RTPrintf("tstLdr: Relocate of '%s' from %#x to %#x failed, rc=%Vrc. Aborting test.\n",
141 pszFilename, Addr2, Addr, rc);
142 }
143 else
144 RTPrintf("tstLdr: Disassembly failed!\n");
145 }
146 else
147 RTPrintf("tstLdr: Invalid value for symbol '%s' in '%s'. off=%#x Value=%#x\n",
148 "Entrypoint", pszFilename, off, Value);
149 }
150 else
151 RTPrintf("tstLdr: Failed to resolve symbol '%s' in '%s', rc=%Vrc.\n", "Entrypoint", pszFilename, rc);
152 }
153 else
154 RTPrintf("tstLdr: Failed to get bits for '%s', rc=%Vrc. aborting test\n", pszFilename, rc);
155 RTMemFree(pvBits);
156 }
157 else
158 RTPrintf("tstLdr: Out of memory '%s' cb=%d. aborting test.\n", pszFilename, cb);
159 }
160 else
161 RTPrintf("tstLdr: Size is odd, '%s'. aborting test.\n", pszFilename);
162
163
164 /* cleanup */
165 rc = RTLdrClose(hLdrMod);
166 if (RT_FAILURE(rc))
167 {
168 RTPrintf("tstLdr: Failed to close '%s', rc=%Vrc.\n", pszFilename, rc);
169 rcRet++;
170 }
171
172 return rcRet;
173}
174
175
176
177int main(int argc, char **argv)
178{
179 RTR3Init();
180
181 int rcRet = 0;
182 if (argc <= 1)
183 {
184 RTPrintf("usage: %s <module> [more modules]\n", argv[0]);
185 return 1;
186 }
187
188 /*
189 * Iterate the files.
190 */
191 for (int argi = 1; argi < argc; argi++)
192 {
193 RTPrintf("tstLdr: TESTING '%s'...\n", argv[argi]);
194 rcRet += testLdrOne(argv[argi]);
195 }
196
197 /*
198 * Test result summary.
199 */
200 if (!rcRet)
201 RTPrintf("tstLdr: SUCCESS\n");
202 else
203 RTPrintf("tstLdr: FAILURE - %d errors\n", rcRet);
204 return !!rcRet;
205}
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