VirtualBox

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

Last change on this file since 47935 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

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