VirtualBox

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

Last change on this file since 86669 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.8 KB
Line 
1/* $Id: tstLdr-2.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - Testcase for parts of RTLdr*, manual inspection.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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/errcore.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,
74 RTUINTPTR *pValue, void *pvUser)
75{
76 RT_NOREF5(hLdrMod, pszModule, pszSymbol, uSymbol, pvUser);
77 /* check the name format and only permit certain names */
78 *pValue = 0xf0f0f0f0;
79 return VINF_SUCCESS;
80}
81
82
83/**
84 * One test iteration with one file.
85 *
86 * The test is very simple, we load the file three times
87 * into two different regions. The first two into each of the
88 * regions the for compare usage. The third is loaded into one
89 * and then relocated between the two and other locations a few times.
90 *
91 * @returns number of errors.
92 * @param pszFilename The file to load the mess with.
93 */
94static int testLdrOne(const char *pszFilename)
95{
96 RTERRINFOSTATIC ErrInfo;
97 RTLDRMOD hLdrMod;
98 int rc = RTLdrOpenEx(pszFilename, 0, RTLDRARCH_WHATEVER, &hLdrMod, RTErrInfoInitStatic(&ErrInfo));
99 if (RT_FAILURE(rc))
100 {
101 RTPrintf("tstLdr: Failed to open '%s', rc=%Rrc. aborting test.\n", pszFilename, rc);
102 if (ErrInfo.szMsg[0])
103 RTPrintf("tstLdr: %s\n", ErrInfo.szMsg);
104 Assert(hLdrMod == NIL_RTLDRMOD);
105 return 1;
106 }
107
108 int rcRet = 1;
109 size_t cb = RTLdrSize(hLdrMod);
110 if (cb > 100)
111 {
112 void *pvBits = RTMemAlloc(cb);
113 if (pvBits)
114 {
115 RTUINTPTR Addr = 0xc0000000;
116 rc = RTLdrGetBits(hLdrMod, pvBits, Addr, testGetImport, NULL);
117 if (RT_SUCCESS(rc))
118 {
119 RTUINTPTR Value;
120 rc = RTLdrGetSymbolEx(hLdrMod, pvBits, Addr, UINT32_MAX, "Entrypoint", &Value);
121 if (RT_SUCCESS(rc))
122 {
123 unsigned off = Value - Addr;
124 if (off < cb)
125 {
126 if (MyDisBlock((uint8_t *)pvBits + off, Addr - (uintptr_t)pvBits))
127 {
128 RTUINTPTR Addr2 = 0xd0000000;
129 rc = RTLdrRelocate(hLdrMod, pvBits, Addr2, Addr, testGetImport, NULL);
130 if (RT_SUCCESS(rc))
131 {
132 if (MyDisBlock((uint8_t *)pvBits + off, Addr2 - (uintptr_t)pvBits))
133 rcRet = 0;
134 else
135 RTPrintf("tstLdr: Disassembly failed!\n");
136 }
137 else
138 RTPrintf("tstLdr: Relocate of '%s' from %#x to %#x failed, rc=%Rrc. Aborting test.\n",
139 pszFilename, Addr2, Addr, rc);
140 }
141 else
142 RTPrintf("tstLdr: Disassembly failed!\n");
143 }
144 else
145 RTPrintf("tstLdr: Invalid value for symbol '%s' in '%s'. off=%#x Value=%#x\n",
146 "Entrypoint", pszFilename, off, Value);
147 }
148 else
149 RTPrintf("tstLdr: Failed to resolve symbol '%s' in '%s', rc=%Rrc.\n", "Entrypoint", pszFilename, rc);
150 }
151 else
152 RTPrintf("tstLdr: Failed to get bits for '%s', rc=%Rrc. aborting test\n", pszFilename, rc);
153 RTMemFree(pvBits);
154 }
155 else
156 RTPrintf("tstLdr: Out of memory '%s' cb=%d. aborting test.\n", pszFilename, cb);
157 }
158 else
159 RTPrintf("tstLdr: Size is odd, '%s'. aborting test.\n", pszFilename);
160
161
162 /* cleanup */
163 rc = RTLdrClose(hLdrMod);
164 if (RT_FAILURE(rc))
165 {
166 RTPrintf("tstLdr: Failed to close '%s', rc=%Rrc.\n", pszFilename, rc);
167 rcRet++;
168 }
169
170 return rcRet;
171}
172
173
174
175int main(int argc, char **argv)
176{
177 RTR3InitExe(argc, &argv, 0);
178
179 int rcRet = 0;
180 if (argc <= 1)
181 {
182 RTPrintf("usage: %s <module> [more modules]\n", argv[0]);
183 return 1;
184 }
185
186 /*
187 * Iterate the files.
188 */
189 for (int argi = 1; argi < argc; argi++)
190 {
191 RTPrintf("tstLdr: TESTING '%s'...\n", argv[argi]);
192 rcRet += testLdrOne(argv[argi]);
193 }
194
195 /*
196 * Test result summary.
197 */
198 if (!rcRet)
199 RTPrintf("tstLdr: SUCCESS\n");
200 else
201 RTPrintf("tstLdr: FAILURE - %d errors\n", rcRet);
202 return !!rcRet;
203}
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