VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstLdr-2.cpp@ 6736

Last change on this file since 6736 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1/* $Id: tstLdr-2.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Testcase for parts of RTLdr*, manual inspection.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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/stream.h>
34#include <iprt/assert.h>
35#include <iprt/runtime.h>
36#include <VBox/dis.h>
37#include <iprt/err.h>
38#include <iprt/string.h>
39
40
41bool MyDisBlock(PDISCPUSTATE pCpu, RTHCUINTPTR pvCodeBlock, int32_t cbMax, RTUINTPTR off)
42{
43 int32_t i = 0;
44 while (i < cbMax)
45 {
46 char szOutput[256];
47 uint32_t cbInstr;
48 if (RT_FAILURE(DISInstr(pCpu, pvCodeBlock + i, off, &cbInstr, szOutput)))
49 return false;
50
51 RTPrintf("%s", szOutput);
52
53 /* next */
54 i += cbInstr;
55 }
56 return true;
57}
58
59
60
61/**
62 * Resolve an external symbol during RTLdrGetBits().
63 *
64 * @returns iprt status code.
65 * @param hLdrMod The loader module handle.
66 * @param pszModule Module name.
67 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
68 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
69 * @param pValue Where to store the symbol value (address).
70 * @param pvUser User argument.
71 */
72static DECLCALLBACK(int) testGetImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
73{
74 /* check the name format and only permit certain names */
75 *pValue = 0xf0f0f0f0;
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 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 RTLDRMOD hLdrMod;
94 int rc = RTLdrOpen(pszFilename, &hLdrMod);
95 if (RT_FAILURE(rc))
96 {
97 RTPrintf("tstLdr: Failed to open '%s', rc=%Vrc. aborting test.\n", pszFilename, rc);
98 Assert(hLdrMod == NIL_RTLDRMOD);
99 return 1;
100 }
101
102 int rcRet = 1;
103 size_t cb = RTLdrSize(hLdrMod);
104 if (cb > 100)
105 {
106 void *pvBits = RTMemAlloc(cb);
107 if (pvBits)
108 {
109 RTUINTPTR Addr = 0xc0000000;
110 rc = RTLdrGetBits(hLdrMod, pvBits, Addr, testGetImport, NULL);
111 if (RT_SUCCESS(rc))
112 {
113 RTUINTPTR Value;
114 rc = RTLdrGetSymbolEx(hLdrMod, pvBits, Addr, "Entrypoint", &Value);
115 if (RT_SUCCESS(rc))
116 {
117 unsigned off = Value - Addr;
118 if (off < cb)
119 {
120 DISCPUSTATE Cpu = {0};
121 Cpu.mode = CPUMODE_32BIT;
122 if (MyDisBlock(&Cpu, (RTUINTPTR)pvBits + off, 200, Addr - (uintptr_t)pvBits))
123 {
124 RTUINTPTR Addr2 = 0xd0000000;
125 rc = RTLdrRelocate(hLdrMod, pvBits, Addr2, Addr, testGetImport, NULL);
126 if (RT_SUCCESS(rc))
127 {
128 if (MyDisBlock(&Cpu, (RTUINTPTR)pvBits + off, 200, Addr2 - (uintptr_t)pvBits))
129 rcRet = 0;
130 else
131 RTPrintf("tstLdr: Disassembly failed!\n");
132 }
133 else
134 RTPrintf("tstLdr: Relocate of '%s' from %#x to %#x failed, rc=%Vrc. Aborting test.\n",
135 pszFilename, Addr2, Addr, rc);
136 }
137 else
138 RTPrintf("tstLdr: Disassembly failed!\n");
139 }
140 else
141 RTPrintf("tstLdr: Invalid value for symbol '%s' in '%s'. off=%#x Value=%#x\n",
142 "Entrypoint", pszFilename, off, Value);
143 }
144 else
145 RTPrintf("tstLdr: Failed to resolve symbol '%s' in '%s', rc=%Vrc.\n", "Entrypoint", pszFilename, rc);
146 }
147 else
148 RTPrintf("tstLdr: Failed to get bits for '%s', rc=%Vrc. aborting test\n", pszFilename, rc);
149 RTMemFree(pvBits);
150 }
151 else
152 RTPrintf("tstLdr: Out of memory '%s' cb=%d. aborting test.\n", pszFilename, cb);
153 }
154 else
155 RTPrintf("tstLdr: Size is odd, '%s'. aborting test.\n", pszFilename);
156
157
158 /* cleanup */
159 rc = RTLdrClose(hLdrMod);
160 if (RT_FAILURE(rc))
161 {
162 RTPrintf("tstLdr: Failed to close '%s', rc=%Vrc.\n", pszFilename, rc);
163 rcRet++;
164 }
165
166 return rcRet;
167}
168
169
170
171int main(int argc, char **argv)
172{
173 RTR3Init();
174
175 int rcRet = 0;
176 if (argc <= 1)
177 {
178 RTPrintf("usage: %s <module> [more modules]\n", argv[0]);
179 return 1;
180 }
181
182 /*
183 * Iterate the files.
184 */
185 for (int argi = 1; argi < argc; argi++)
186 {
187 RTPrintf("tstLdr: TESTING '%s'...\n", argv[argi]);
188 rcRet += testLdrOne(argv[argi]);
189 }
190
191 /*
192 * Test result summary.
193 */
194 if (!rcRet)
195 RTPrintf("tstLdr: SUCCESS\n");
196 else
197 RTPrintf("tstLdr: FAILURE - %d errors\n", rcRet);
198 return !!rcRet;
199}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette