VirtualBox

source: kStuff/trunk/kLdr/testcase/tst-3-driver.c@ 2

Last change on this file since 2 was 2, checked in by bird, 17 years ago

Imported http://svn.netlabs.org/repos/libc/trunk/kStuff, revision 3612.

  • Property svn:keywords set to Id Revision
File size: 6.3 KB
Line 
1/* $Id: tst-3-driver.c 2 2007-11-16 16:07:14Z bird $ */
2/** @file
3 * kLdr - Dynamic Loader testcase no. 3, Driver.
4 */
5
6/*
7 * Copyright (c) 2006-2007 knut st. osmundsen <[email protected]>
8 *
9 * This file is part of kStuff.
10 *
11 * kStuff is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * kStuff is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with kStuff; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include "tst.h"
31#include <k/kErr.h>
32
33#include <stdarg.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#ifdef _MSC_VER
38# include <malloc.h>
39#endif
40
41
42/*******************************************************************************
43* Defined Constants And Macros *
44*******************************************************************************/
45/** Select the appropriate KLDRSYMKIND bit define. */
46#define MY_KLDRSYMKIND_BITS ( sizeof(void *) == 4 ? KLDRSYMKIND_32BIT : KLDRSYMKIND_64BIT )
47
48
49/*******************************************************************************
50* Global Variables *
51*******************************************************************************/
52/** The numbers of errors. */
53static int g_cErrors = 0;
54
55
56/**
57 * Report failure.
58 */
59static int Failure(const char *pszFormat, ...)
60{
61 va_list va;
62
63 g_cErrors++;
64
65 printf("tst-3-driver: ");
66 va_start(va, pszFormat);
67 vprintf(pszFormat, va);
68 va_end(va);
69 printf("\n");
70 return 1;
71}
72
73
74/**
75 * External symbol used by the testcase module.
76 */
77static int Tst3Ext(int iFortyTwo)
78{
79 if (iFortyTwo != 42)
80 return 256;
81 return 42;
82}
83
84
85/**
86 * Callback for resolving the Tst3Ext import.
87 */
88static int GetImport(PKLDRMOD pMod, KU32 iImport, KU32 iSymbol, const char *pchSymbol, KSIZE cchSymbol,
89 const char *pszVersion, PKLDRADDR puValue, KU32 *pfKind, void *pvUser)
90{
91 if (*pfKind != KLDRSYMKIND_REQ_FLAT)
92 return -1;
93
94 if ( !strncmp(pchSymbol, "Tst3Ext", strlen("Tst3Ext"))
95 || !strncmp(pchSymbol, "_Tst3Ext", strlen("_Tst3Ext")))
96 {
97 *puValue = (KUPTR)&Tst3Ext;
98 *pfKind = KLDRSYMKIND_CODE | (sizeof(pfKind) == 4 ? KLDRSYMKIND_32BIT : KLDRSYMKIND_64BIT);
99 return 0;
100 }
101
102 return -2;
103}
104
105
106/**
107 * Performs the tests on one module.
108 * @returns non sense.
109 */
110int TestModule(const char *pszFile)
111{
112 PKLDRMOD pMod;
113 KLDRSIZE cbImage;
114 void *pvBits;
115 int rc;
116
117 printf("tst-3-driver: testing '%s'...\n", pszFile);
118
119 /* open it. */
120 rc = kLdrModOpen(pszFile, &pMod);
121 if (rc)
122 return Failure("kLdrModOpen(%s,) -> %#d (%s)\n", pszFile, rc, kErrName(rc));
123
124 /* get bits. */
125 cbImage = kLdrModSize(pMod);
126 pvBits = malloc((KSIZE)cbImage + 0xfff);
127 if (pvBits)
128 {
129 void *pvBits2 = (void *)( ((KUPTR)pvBits + 0xfff) & ~(KUPTR)0xfff );
130
131 KLDRADDR BaseAddress = (KUPTR)pvBits2;
132 rc = kLdrModGetBits(pMod, pvBits2, BaseAddress, GetImport, NULL);
133 if (!rc)
134 {
135 KLDRADDR EntryPoint;
136
137 /* call into it */
138 rc = kLdrModQuerySymbol(pMod, pvBits2, BaseAddress, NIL_KLDRMOD_SYM_ORDINAL, "_Tst3", strlen("_Tst3"), NULL, NULL, NULL,
139 &EntryPoint, NULL);
140 if (rc == KLDR_ERR_SYMBOL_NOT_FOUND)
141 rc = kLdrModQuerySymbol(pMod, pvBits2, BaseAddress, NIL_KLDRMOD_SYM_ORDINAL, "Tst3", strlen("Tst3"), NULL, NULL, NULL,
142 &EntryPoint, NULL);
143 if (!rc)
144 {
145 int (*pfnEntryPoint)(int) = (int (*)(int)) ((KUPTR)EntryPoint);
146 rc = pfnEntryPoint(42);
147 if (rc == 42)
148 {
149 /* relocate twice and try again. */
150 rc = kLdrModRelocateBits(pMod, pvBits2, BaseAddress + 0x22000, BaseAddress, GetImport, NULL);
151 if (!rc)
152 {
153 rc = kLdrModRelocateBits(pMod, pvBits2, BaseAddress, BaseAddress + 0x22000, GetImport, NULL);
154 if (!rc)
155 {
156 rc = pfnEntryPoint(42);
157 if (rc == 42)
158 {
159 printf("tst-3-driver: success.\n");
160 }
161 else
162 Failure("pfnEntryPoint(42) -> %d (2nd)\n", rc);
163 }
164 else
165 Failure("kLdrModRelocateBits(,,, + 0x22000,,,) -> %#x (%s)\n", rc, kErrName(rc));
166 }
167 else
168 Failure("kLdrModRelocateBits(,, + 0x22000,,,,) -> %#x (%s)\n", rc, kErrName(rc));
169 }
170 else
171 Failure("pfnEntryPoint(42) -> %d (1st)\n", rc);
172 }
173 else
174 Failure("kLdrModQuerySymbol -> %#x (%s)\n", rc, kErrName(rc));
175 }
176 else
177 Failure("kLdrModGetBits -> %#x (%s)\n", rc, kErrName(rc));
178 free(pvBits);
179 }
180 else
181 Failure("malloc(%lx) -> NULL\n", (long)cbImage);
182
183 /* clean up */
184 rc = kLdrModClose(pMod);
185 if (rc)
186 Failure("kLdrModOpen(%s,) -> %#x (%s)\n", pszFile, rc, kErrName(rc));
187 return 0;
188}
189
190
191int main(int argc, char **argv)
192{
193 int i;
194
195 /*
196 * Test all the given modules (requires arguments).
197 */
198 for (i = 1; i < argc; i++)
199 {
200 TestModule(argv[i]);
201 }
202
203
204 /*
205 * Summary
206 */
207 if (!g_cErrors)
208 printf("tst-3-driver: SUCCESS\n");
209 else
210 printf("tst-3-driver: FAILURE - %d errors\n", g_cErrors);
211 return !!g_cErrors;
212}
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