VirtualBox

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

Last change on this file since 5606 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1/* $Id: tstLdr-2.cpp 4071 2007-08-07 17:07:59Z 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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <iprt/ldr.h>
23#include <iprt/alloc.h>
24#include <iprt/stream.h>
25#include <iprt/assert.h>
26#include <iprt/runtime.h>
27#include <VBox/dis.h>
28#include <iprt/err.h>
29#include <iprt/string.h>
30
31
32bool MyDisBlock(PDISCPUSTATE pCpu, RTHCUINTPTR pvCodeBlock, int32_t cbMax, RTUINTPTR off)
33{
34 int32_t i = 0;
35 while (i < cbMax)
36 {
37 char szOutput[256];
38 uint32_t cbInstr;
39 if (!DISInstr(pCpu, pvCodeBlock + i, off, &cbInstr, szOutput))
40 return false;
41
42 RTPrintf("%s", szOutput);
43
44 /* next */
45 i += cbInstr;
46 }
47 return true;
48}
49
50
51
52/**
53 * Resolve an external symbol during RTLdrGetBits().
54 *
55 * @returns iprt status code.
56 * @param hLdrMod The loader module handle.
57 * @param pszModule Module name.
58 * @param pszSymbol Symbol name, NULL if uSymbol should be used.
59 * @param uSymbol Symbol ordinal, ~0 if pszSymbol should be used.
60 * @param pValue Where to store the symbol value (address).
61 * @param pvUser User argument.
62 */
63static DECLCALLBACK(int) testGetImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
64{
65 /* check the name format and only permit certain names */
66 *pValue = 0xf0f0f0f0;
67 return VINF_SUCCESS;
68}
69
70
71/**
72 * One test iteration with one file.
73 *
74 * The test is very simple, we load the the file three times
75 * into two different regions. The first two into each of the
76 * regions the for compare usage. The third is loaded into one
77 * and then relocated between the two and other locations a few times.
78 *
79 * @returns number of errors.
80 * @param pszFilename The file to load the mess with.
81 */
82static int testLdrOne(const char *pszFilename)
83{
84 RTLDRMOD hLdrMod;
85 int rc = RTLdrOpen(pszFilename, &hLdrMod);
86 if (RT_FAILURE(rc))
87 {
88 RTPrintf("tstLdr: Failed to open '%s', rc=%Vrc. aborting test.\n", pszFilename, rc);
89 Assert(hLdrMod == NIL_RTLDRMOD);
90 return 1;
91 }
92
93 int rcRet = 1;
94 size_t cb = RTLdrSize(hLdrMod);
95 if (cb > 100)
96 {
97 void *pvBits = RTMemAlloc(cb);
98 if (pvBits)
99 {
100 RTUINTPTR Addr = 0xc0000000;
101 rc = RTLdrGetBits(hLdrMod, pvBits, Addr, testGetImport, NULL);
102 if (RT_SUCCESS(rc))
103 {
104 RTUINTPTR Value;
105 rc = RTLdrGetSymbolEx(hLdrMod, pvBits, Addr, "Entrypoint", &Value);
106 if (RT_SUCCESS(rc))
107 {
108 unsigned off = Value - Addr;
109 if (off < cb)
110 {
111 DISCPUSTATE Cpu = {0};
112 Cpu.mode = CPUMODE_32BIT;
113 if (MyDisBlock(&Cpu, (RTUINTPTR)pvBits + off, 200, Addr - (uintptr_t)pvBits))
114 {
115 RTUINTPTR Addr2 = 0xd0000000;
116 rc = RTLdrRelocate(hLdrMod, pvBits, Addr2, Addr, testGetImport, NULL);
117 if (RT_SUCCESS(rc))
118 {
119 if (MyDisBlock(&Cpu, (RTUINTPTR)pvBits + off, 200, Addr2 - (uintptr_t)pvBits))
120 rcRet = 0;
121 else
122 RTPrintf("tstLdr: Disassembly failed!\n");
123 }
124 else
125 RTPrintf("tstLdr: Relocate of '%s' from %#x to %#x failed, rc=%Vrc. Aborting test.\n",
126 pszFilename, Addr2, Addr, rc);
127 }
128 else
129 RTPrintf("tstLdr: Disassembly failed!\n");
130 }
131 else
132 RTPrintf("tstLdr: Invalid value for symbol '%s' in '%s'. off=%#x Value=%#x\n",
133 "Entrypoint", pszFilename, off, Value);
134 }
135 else
136 RTPrintf("tstLdr: Failed to resolve symbol '%s' in '%s', rc=%Vrc.\n", "Entrypoint", pszFilename, rc);
137 }
138 else
139 RTPrintf("tstLdr: Failed to get bits for '%s', rc=%Vrc. aborting test\n", pszFilename, rc);
140 RTMemFree(pvBits);
141 }
142 else
143 RTPrintf("tstLdr: Out of memory '%s' cb=%d. aborting test.\n", pszFilename, cb);
144 }
145 else
146 RTPrintf("tstLdr: Size is odd, '%s'. aborting test.\n", pszFilename);
147
148
149 /* cleanup */
150 rc = RTLdrClose(hLdrMod);
151 if (RT_FAILURE(rc))
152 {
153 RTPrintf("tstLdr: Failed to close '%s', rc=%Vrc.\n", pszFilename, rc);
154 rcRet++;
155 }
156
157 return rcRet;
158}
159
160
161
162int main(int argc, char **argv)
163{
164 RTR3Init();
165
166 int rcRet = 0;
167 if (argc <= 1)
168 {
169 RTPrintf("usage: %s <module> [more modules]\n", argv[0]);
170 return 1;
171 }
172
173 /*
174 * Iterate the files.
175 */
176 for (int argi = 1; argi < argc; argi++)
177 {
178 RTPrintf("tstLdr: TESTING '%s'...\n", argv[argi]);
179 rcRet += testLdrOne(argv[argi]);
180 }
181
182 /*
183 * Test result summary.
184 */
185 if (!rcRet)
186 RTPrintf("tstLdr: SUCCESS\n");
187 else
188 RTPrintf("tstLdr: FAILURE - %d errors\n", rcRet);
189 return !!rcRet;
190}
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