1 | /* $Id: tstLdr-2.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Testcase for parts of RTLdr*, manual inspection.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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/stream.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/initterm.h>
|
---|
36 | #include <VBox/dis.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | bool 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 | */
|
---|
72 | static 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 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 | */
|
---|
91 | static int testLdrOne(const char *pszFilename)
|
---|
92 | {
|
---|
93 | RTLDRMOD hLdrMod;
|
---|
94 | int rc = RTLdrOpen(pszFilename, 0, RTLDRARCH_WHATEVER, &hLdrMod);
|
---|
95 | if (RT_FAILURE(rc))
|
---|
96 | {
|
---|
97 | RTPrintf("tstLdr: Failed to open '%s', rc=%Rrc. 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;
|
---|
121 |
|
---|
122 | memset(&Cpu, 0, sizeof(Cpu));
|
---|
123 | Cpu.mode = CPUMODE_32BIT;
|
---|
124 | if (MyDisBlock(&Cpu, (uintptr_t)pvBits + off, 200, Addr - (uintptr_t)pvBits))
|
---|
125 | {
|
---|
126 | RTUINTPTR Addr2 = 0xd0000000;
|
---|
127 | rc = RTLdrRelocate(hLdrMod, pvBits, Addr2, Addr, testGetImport, NULL);
|
---|
128 | if (RT_SUCCESS(rc))
|
---|
129 | {
|
---|
130 | if (MyDisBlock(&Cpu, (uintptr_t)pvBits + off, 200, Addr2 - (uintptr_t)pvBits))
|
---|
131 | rcRet = 0;
|
---|
132 | else
|
---|
133 | RTPrintf("tstLdr: Disassembly failed!\n");
|
---|
134 | }
|
---|
135 | else
|
---|
136 | RTPrintf("tstLdr: Relocate of '%s' from %#x to %#x failed, rc=%Rrc. Aborting test.\n",
|
---|
137 | pszFilename, Addr2, Addr, rc);
|
---|
138 | }
|
---|
139 | else
|
---|
140 | RTPrintf("tstLdr: Disassembly failed!\n");
|
---|
141 | }
|
---|
142 | else
|
---|
143 | RTPrintf("tstLdr: Invalid value for symbol '%s' in '%s'. off=%#x Value=%#x\n",
|
---|
144 | "Entrypoint", pszFilename, off, Value);
|
---|
145 | }
|
---|
146 | else
|
---|
147 | RTPrintf("tstLdr: Failed to resolve symbol '%s' in '%s', rc=%Rrc.\n", "Entrypoint", pszFilename, rc);
|
---|
148 | }
|
---|
149 | else
|
---|
150 | RTPrintf("tstLdr: Failed to get bits for '%s', rc=%Rrc. aborting test\n", pszFilename, rc);
|
---|
151 | RTMemFree(pvBits);
|
---|
152 | }
|
---|
153 | else
|
---|
154 | RTPrintf("tstLdr: Out of memory '%s' cb=%d. aborting test.\n", pszFilename, cb);
|
---|
155 | }
|
---|
156 | else
|
---|
157 | RTPrintf("tstLdr: Size is odd, '%s'. aborting test.\n", pszFilename);
|
---|
158 |
|
---|
159 |
|
---|
160 | /* cleanup */
|
---|
161 | rc = RTLdrClose(hLdrMod);
|
---|
162 | if (RT_FAILURE(rc))
|
---|
163 | {
|
---|
164 | RTPrintf("tstLdr: Failed to close '%s', rc=%Rrc.\n", pszFilename, rc);
|
---|
165 | rcRet++;
|
---|
166 | }
|
---|
167 |
|
---|
168 | return rcRet;
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 |
|
---|
173 | int main(int argc, char **argv)
|
---|
174 | {
|
---|
175 | RTR3Init();
|
---|
176 |
|
---|
177 | int rcRet = 0;
|
---|
178 | if (argc <= 1)
|
---|
179 | {
|
---|
180 | RTPrintf("usage: %s <module> [more modules]\n", argv[0]);
|
---|
181 | return 1;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * Iterate the files.
|
---|
186 | */
|
---|
187 | for (int argi = 1; argi < argc; argi++)
|
---|
188 | {
|
---|
189 | RTPrintf("tstLdr: TESTING '%s'...\n", argv[argi]);
|
---|
190 | rcRet += testLdrOne(argv[argi]);
|
---|
191 | }
|
---|
192 |
|
---|
193 | /*
|
---|
194 | * Test result summary.
|
---|
195 | */
|
---|
196 | if (!rcRet)
|
---|
197 | RTPrintf("tstLdr: SUCCESS\n");
|
---|
198 | else
|
---|
199 | RTPrintf("tstLdr: FAILURE - %d errors\n", rcRet);
|
---|
200 | return !!rcRet;
|
---|
201 | }
|
---|