source: trunk/kStuff/kLdr/tstkLdrMod.c@ 3579

Last change on this file since 3579 was 3579, checked in by bird, 18 years ago

error code cleanup.

  • Property svn:keywords set to Id
File size: 23.0 KB
Line 
1/* $Id: tstkLdrMod.c 3579 2007-09-02 21:40:41Z bird $ */
2/** @file
3 *
4 * kLdr - Module interpreter testcase.
5 *
6 * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@anduin.net>
7 *
8 *
9 * This file is part of kLdr.
10 *
11 * kLdr is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kLdr 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
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kLdr; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <k/kLdr.h>
32#include <k/kErr.h>
33#include <k/kErrors.h>
34
35#include <stdarg.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40
41/*******************************************************************************
42* Defined Constants And Macros *
43*******************************************************************************/
44/** The default base address used in the tests. */
45#define MY_BASEADDRESS 0x2400000
46
47
48/*******************************************************************************
49* Global Variables *
50*******************************************************************************/
51/** The numbers of errors. */
52static int g_cErrors = 0;
53
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("tstLdrMod: ");
66 va_start(va, pszFormat);
67 vprintf(pszFormat, va);
68 va_end(va);
69 printf("\n");
70 return 1;
71}
72
73
74/** Dummy import resolver callback. */
75static int BasicTestsGetImport(PKLDRMOD pMod, KU32 iImport, KU32 iSymbol, const char *pchSymbol, KSIZE cchSymbol,
76 const char *pszVersion, PKLDRADDR puValue, KU32 *pfKind, void *pvUser)
77{
78 *puValue = 0xdeadface;
79 *pfKind = KLDRSYMKIND_NO_BIT | KLDRSYMKIND_NO_TYPE;
80 return 0;
81}
82
83
84/**
85 * Verbose memcmp().
86 */
87static int TestMemComp(const void *pv1, const void *pv2, KSIZE cb)
88{
89 KSIZE off;
90 const KU8 *pb1 = (const KU8 *)pv1;
91 const KU8 *pb2 = (const KU8 *)pv2;
92 if (!memcmp(pb1, pb2, cb))
93 return 0;
94 printf("Mismatching blocks pv1=%p pv2=%p cb=%#x:\n", pv1, pv2, cb);
95 for (off = 0; off < cb; off++)
96 {
97 if (pb1[off] == pb2[off])
98 continue;
99 printf("%08x %02x != %02x\n", off, pb1[off], pb2[off]);
100 }
101 return memcmp(pb1, pb2, cb); /* lazy */
102}
103
104
105/**
106 * Performs basic relocation tests.
107 */
108static int BasicTestsRelocate(PKLDRMOD pMod, void *pvBits, void *pvBits2)
109{
110 const KSIZE cbImage = (KSIZE)kLdrModSize(pMod);
111 int rc;
112
113 printf("* Relocation test...\n");
114
115 /*
116 * Get the same bits again to check that we get the same result.
117 */
118 memset(pvBits2, 0xfe, cbImage);
119 rc = kLdrModGetBits(pMod, pvBits2, (KUPTR)pvBits, BasicTestsGetImport, NULL);
120 if (rc)
121 return Failure("failed to get image bits, rc=%d (%s) (a)", rc, kErrName(rc));
122 if (TestMemComp(pvBits2, pvBits, cbImage))
123 return Failure("relocation test failed, mismatching bits (a)");
124
125 /*
126 * Short relocation round trip.
127 */
128 rc = kLdrModRelocateBits(pMod, pvBits2, 0x1000, (KUPTR)pvBits, BasicTestsGetImport, NULL);
129 if (rc)
130 return Failure("failed to relocate, rc=%d (%s) (b1)", rc, kErrName(rc));
131 rc = kLdrModRelocateBits(pMod, pvBits2, (KUPTR)pvBits, 0x1000, BasicTestsGetImport, NULL);
132 if (rc)
133 return Failure("failed to relocate, rc=%d (%s) (b2)", rc, kErrName(rc));
134 if (TestMemComp(pvBits2, pvBits, cbImage))
135 return Failure("relocation test failed, mismatching bits (b)");
136
137 /*
138 * Longer trip where we also check the intermediate results.
139 */
140 /* stage one */
141 rc = kLdrModRelocateBits(pMod, pvBits, 0x1000000, (KUPTR)pvBits, BasicTestsGetImport, NULL);
142 if (rc)
143 return Failure("failed to relocate, rc=%d (%s) (c1)", rc, kErrName(rc));
144 memset(pvBits2, 0xfe, cbImage);
145 rc = kLdrModGetBits(pMod, pvBits2, 0x1000000, BasicTestsGetImport, NULL);
146 if (rc)
147 return Failure("failed to get image bits, rc=%d (%s) (c1)", rc, kErrName(rc));
148 if (TestMemComp(pvBits2, pvBits, cbImage))
149 return Failure("relocation test failed, mismatching bits (c1)");
150
151 /* stage two */
152 rc = kLdrModRelocateBits(pMod, pvBits, ~(KUPTR)0x1010000, 0x1000000, BasicTestsGetImport, NULL);
153 if (rc)
154 return Failure("failed to relocate, rc=%d (%s) (c2)", rc, kErrName(rc));
155 memset(pvBits2, 0xef, cbImage);
156 rc = kLdrModGetBits(pMod, pvBits2, ~(KUPTR)0x1010000, BasicTestsGetImport, NULL);
157 if (rc)
158 return Failure("failed to get image bits, rc=%d (%s) (c2)", rc, kErrName(rc));
159 if (TestMemComp(pvBits2, pvBits, cbImage))
160 return Failure("relocation test failed, mismatching bits (c2)");
161
162 /* stage three */
163 rc = kLdrModRelocateBits(pMod, pvBits, MY_BASEADDRESS, ~(KUPTR)0x1010000, BasicTestsGetImport, NULL);
164 if (rc)
165 return Failure("failed to relocate, rc=%d (%s) (c3)", rc, kErrName(rc));
166 memset(pvBits2, 0xef, cbImage);
167 rc = kLdrModGetBits(pMod, pvBits2, MY_BASEADDRESS, BasicTestsGetImport, NULL);
168 if (rc)
169 return Failure("failed to get image bits, rc=%d (%s) (c3)", rc, kErrName(rc));
170 if (TestMemComp(pvBits2, pvBits, cbImage))
171 return Failure("relocation test failed, mismatching bits (c3)");
172
173 /* stage four */
174 rc = kLdrModRelocateBits(pMod, pvBits, ~(KUPTR)0 / 2 - 0x10000, MY_BASEADDRESS, BasicTestsGetImport, NULL);
175 if (rc)
176 return Failure("failed to relocate, rc=%d %(s) (c4)", rc, kErrName(rc));
177 memset(pvBits2, 0xdc, cbImage);
178 rc = kLdrModGetBits(pMod, pvBits2, ~(KUPTR)0 / 2 - 0x10000, BasicTestsGetImport, NULL);
179 if (rc)
180 return Failure("failed to get image bits, rc=%d (%s) (c4)", rc, kErrName(rc));
181 if (TestMemComp(pvBits2, pvBits, cbImage))
182 return Failure("relocation test failed, mismatching bits (c4)");
183
184 /* return */
185 rc = kLdrModRelocateBits(pMod, pvBits, (KUPTR)pvBits, ~(KUPTR)0 / 2 - 0x10000, BasicTestsGetImport, NULL);
186 if (rc)
187 return Failure("failed to relocate, rc=%d (%s) (c5)", rc, kErrName(rc));
188 memset(pvBits2, 0xcd, cbImage);
189 rc = kLdrModGetBits(pMod, pvBits2, (KUPTR)pvBits, BasicTestsGetImport, NULL);
190 if (rc)
191 return Failure("failed to get image bits, rc=%d (%s) (c5)", rc, kErrName(rc));
192 if (TestMemComp(pvBits2, pvBits, cbImage))
193 return Failure("relocation test failed, mismatching bits (c5)");
194
195 return 0;
196}
197
198
199/**
200 * Dump symbols and check that we can query each of them recursivly.
201 */
202static int BasicTestsEnumSymCallback(PKLDRMOD pMod, KU32 iSymbol, const char *pchSymbol, KSIZE cchSymbol,
203 const char *pszVersion, KLDRADDR uValue, KU32 fKind, void *pvUser)
204{
205 KLDRADDR uValue2;
206 KU32 fKind2;
207 int rc;
208
209 /* dump */
210 printf("#0x%08x: %016" PRI_KLDRADDR " %#08x", iSymbol, uValue, fKind);
211 if (pchSymbol)
212 printf(" %.*s", cchSymbol, pchSymbol);
213 printf("\n");
214
215 /* query by ordinal */
216 if (iSymbol != NIL_KLDRMOD_SYM_ORDINAL)
217 {
218 fKind2 = 0;
219 rc = kLdrModQuerySymbol(pMod, pvUser, MY_BASEADDRESS, iSymbol, NULL, 0, NULL, NULL, NULL,
220 &uValue2, &fKind2);
221 if (rc)
222 return Failure("Couldn't find symbol %#x (%.*s) by ordinal. rc=%d (%s)", iSymbol, cchSymbol, pchSymbol, rc, kErrName(rc));
223 if (uValue != uValue2)
224 return Failure("Symbol %#x (%.*s): Value mismatch %016" PRI_KLDRADDR " != %016" PRI_KLDRADDR " (enum!=query/ord) pvBits=%p",
225 iSymbol, cchSymbol, pchSymbol, uValue, uValue2, pvUser);
226 if (fKind != fKind2)
227 return Failure("Symbol %#x (%.*s): Kind mismatch %#x != %#x (enum!=query/ord) pvBits=%p",
228 iSymbol, cchSymbol, pchSymbol, fKind, fKind2, pvUser);
229 }
230
231 /* query by name. */
232 if (pchSymbol)
233 {
234 fKind2 = 0;
235 rc = kLdrModQuerySymbol(pMod, pvUser, MY_BASEADDRESS, NIL_KLDRMOD_SYM_ORDINAL, pchSymbol, cchSymbol, pszVersion,
236 NULL, NULL, &uValue2, &fKind2);
237 if (rc)
238 return Failure("Couldn't find symbol %#x (%.*s) by name. rc=%d (%s)", iSymbol, cchSymbol, pchSymbol, rc, kErrName(rc));
239 if (uValue != uValue2)
240 return Failure("Symbol %#x (%.*s): Value mismatch %016" PRI_KLDRADDR " != %016" PRI_KLDRADDR " (enum!=query/name) pvBits=%p",
241 iSymbol, cchSymbol, pchSymbol, uValue, uValue2, pvUser);
242 if (fKind != fKind2)
243 return Failure("Symbol %#x (%.*s): Kind mismatch %#x != %#x (enum!=query/name) pvBits=%p",
244 iSymbol, cchSymbol, pchSymbol, fKind, fKind2, pvUser);
245 }
246
247 return 0;
248}
249
250
251/**
252 * Dump debugger information and check it for correctness.
253 */
254static int BasicTestEnumDbgInfoCallback(PKLDRMOD pMod, KU32 iDbgInfo, KLDRDBGINFOTYPE enmType,
255 KI16 iMajorVer, KI16 iMinorVer, KLDRFOFF offFile, KLDRADDR LinkAddress,
256 KLDRSIZE cb, const char *pszExtFile, void *pvUser)
257{
258 printf("#0x%08x: enmType=%d %d.%d offFile=0x%" PRI_KLDRADDR " LinkAddress=%" PRI_KLDRADDR " cb=%" PRI_KLDRSIZE " pvUser=%p\n",
259 iDbgInfo, enmType, iMajorVer, iMinorVer, (KLDRADDR)offFile, LinkAddress, cb, pvUser);
260 if (pszExtFile)
261 printf(" pszExtFile=%p '%s'\n", pszExtFile, pszExtFile);
262
263 if (enmType >= KLDRDBGINFOTYPE_END || enmType <= KLDRDBGINFOTYPE_INVALID)
264 return Failure("Bad enmType");
265 if (pvUser != NULL)
266 return Failure("pvUser");
267
268 return 0;
269}
270
271
272/**
273 * Performs the basic module loader test on the specified module and image bits.
274 */
275static int BasicTestsSub2(PKLDRMOD pMod, void *pvBits)
276{
277 KI32 cImports;
278 KI32 i;
279 int rc;
280 KU32 fKind;
281 KLDRADDR Value;
282 KLDRADDR MainEPAddress;
283 KLDRSTACKINFO StackInfo;
284
285 printf("* Testing queries with pvBits=%p...\n", pvBits);
286
287 /*
288 * Get the import modules.
289 */
290 cImports = kLdrModNumberOfImports(pMod, pvBits);
291 printf("cImports=%d\n", cImports);
292 if (cImports < 0)
293 return Failure("failed to query the number of import, cImports=%d", cImports);
294 for (i = 0; i < cImports; i++)
295 {
296 char szImportModule[260];
297 rc = kLdrModGetImport(pMod, pvBits, i, szImportModule, sizeof(szImportModule));
298 if (rc)
299 return Failure("failed to get import module name, rc=%d (%s). (%.260s)", rc, kErrName(rc), szImportModule);
300 printf("import #%d: '%s'\n", i, szImportModule);
301 }
302
303 /*
304 * Query stack info.
305 */
306 StackInfo.Address = ~(KLDRADDR)42;
307 StackInfo.LinkAddress = ~(KLDRADDR)42;
308 StackInfo.cbStack = ~(KLDRSIZE)42;
309 StackInfo.cbStackThread = ~(KLDRSIZE)42;
310 rc = kLdrModGetStackInfo(pMod, pvBits, MY_BASEADDRESS, &StackInfo);
311 if (rc)
312 return Failure("kLdrModGetStackInfo failed with rc=%d (%s)", rc, kErrName(rc));
313 printf("Stack: Address=%016" PRI_KLDRADDR " LinkAddress=%016" PRI_KLDRADDR "\n"
314 " cbStack=%016" PRI_KLDRSIZE " cbStackThread=%016" PRI_KLDRSIZE "\n",
315 StackInfo.Address, StackInfo.LinkAddress, StackInfo.cbStack, StackInfo.cbStackThread);
316 if (StackInfo.Address == ~(KLDRADDR)42)
317 return Failure("Bad StackInfo.Address");
318 if (StackInfo.LinkAddress == ~(KLDRADDR)42)
319 return Failure("Bad StackInfo.LinkAddress");
320 if (StackInfo.cbStack == ~(KLDRSIZE)42)
321 return Failure("Bad StackInfo.cbStack");
322 if (StackInfo.cbStackThread == ~(KLDRSIZE)42)
323 return Failure("Bad StackInfo.cbStackThread");
324
325 /*
326 * Query entrypoint.
327 */
328 MainEPAddress = ~(KLDRADDR)42;
329 rc = kLdrModQueryMainEntrypoint(pMod, pvBits, MY_BASEADDRESS, &MainEPAddress);
330 if (rc)
331 return Failure("kLdrModQueryMainEntrypoint failed with rc=%d (%s)", rc, kErrName(rc));
332 printf("Entrypoint: %016" PRI_KLDRADDR "\n", MainEPAddress);
333 if (MainEPAddress == ~(KLDRADDR)42)
334 return Failure("MainEPAddress wasn't set.");
335 if (MainEPAddress != NIL_KLDRADDR && MainEPAddress < MY_BASEADDRESS)
336 return Failure("Bad MainEPAddress (a).");
337 if (MainEPAddress != NIL_KLDRADDR && MainEPAddress >= MY_BASEADDRESS + kLdrModSize(pMod))
338 return Failure("Bad MainEPAddress (b).");
339
340 /*
341 * Debugger information.
342 */
343 rc = kLdrModHasDbgInfo(pMod, pvBits);
344 if (!rc)
345 printf("Has Debugger Information\n");
346 else if (rc == KLDR_ERR_NO_DEBUG_INFO)
347 printf("NO Debugger Information\n");
348 else
349 return Failure("kLdrModHasDbgInfo failed with rc=%d (%s)", rc, kErrName(rc));
350 rc = kLdrModEnumDbgInfo(pMod, pvBits, BasicTestEnumDbgInfoCallback, NULL);
351 if (rc)
352 return Failure("kLdrModEnumDbgInfo failed with rc=%d (%s)", rc, kErrName(rc));
353
354
355 /*
356 * Negative symbol query tests.
357 */
358 fKind = 0;
359 Value = 0x0badc0de;
360 rc = kLdrModQuerySymbol(pMod, pvBits, MY_BASEADDRESS, NIL_KLDRMOD_SYM_ORDINAL - 20, NULL, 0, NULL, NULL, NULL,
361 &Value, &fKind);
362 if (rc)
363 {
364 if (Value != 0)
365 return Failure("Value wasn't cleared on failure.");
366 }
367
368 fKind = 0;
369 Value = 0x0badc0de;
370 rc = kLdrModQuerySymbol(pMod, pvBits, MY_BASEADDRESS, NIL_KLDRMOD_SYM_ORDINAL, NULL, 0, NULL, NULL, NULL,
371 &Value, &fKind);
372 if (!rc)
373 return Failure("NIL ordinal succeeded!");
374 if (Value != 0)
375 return Failure("Value wasn't cleared on failure.");
376
377 /*
378 * Enumerate and query all symbols.
379 */
380 printf("\n"
381 "Symbols:\n");
382 rc = kLdrModEnumSymbols(pMod, pvBits, MY_BASEADDRESS, 0, BasicTestsEnumSymCallback, pvBits);
383 if (rc)
384 return Failure("kLdrModEnumSymbols failed with rc=%d (%s)", rc, kErrName(rc));
385
386
387/*int kLdrModCanExecuteOn(PKLDRMOD pMod, const void *pvBits, KLDRARCH enmArch, KLDRCPU enmCpu);
388*/
389
390 return 0;
391}
392
393
394/**
395 * Performs the basic module loader test on the specified module
396 */
397static int BasicTestsSub(PKLDRMOD pMod)
398{
399 int rc;
400 KU32 i;
401 void *pvBits;
402 KSIZE cbImage;
403
404 /*
405 * Check/dump the module structure.
406 */
407 printf("pMod=%p u32Magic=%#x cSegments=%d\n", (void *)pMod, pMod->u32Magic, pMod->cSegments);
408 printf("enmType=%d enmFmt=%d enmArch=%d enmCpu=%d enmEndian=%d\n",
409 pMod->enmType, pMod->enmFmt, pMod->enmArch, pMod->enmCpu, pMod->enmEndian);
410 printf("Filename: %s (%d bytes)\n", pMod->pszFilename, pMod->cchFilename);
411 printf(" Name: %s (%d bytes)\n", pMod->pszName, pMod->cchName);
412 printf("\n");
413
414 if (pMod->u32Magic != KLDRMOD_MAGIC)
415 return Failure("Bad u32Magic");
416 if (strlen(pMod->pszFilename) != pMod->cchFilename)
417 return Failure("Bad cchFilename");
418 if (strlen(pMod->pszName) != pMod->cchName)
419 return Failure("Bad cchName");
420 if (pMod->enmFmt >= KLDRFMT_END || pMod->enmFmt <= KLDRFMT_INVALID)
421 return Failure("Bad enmFmt");
422 if (pMod->enmType >= KLDRTYPE_END || pMod->enmType <= KLDRTYPE_INVALID)
423 return Failure("Bad enmType: %d", pMod->enmType);
424 if (pMod->enmArch >= KLDRARCH_END || pMod->enmArch <= KLDRARCH_INVALID)
425 return Failure("Bad enmArch");
426 if (pMod->enmCpu >= KLDRCPU_END || pMod->enmCpu <= KLDRCPU_INVALID)
427 return Failure("Bad enmCpu");
428 if (pMod->enmEndian >= KLDRENDIAN_END || pMod->enmEndian <= KLDRENDIAN_INVALID)
429 return Failure("Bad enmEndian");
430
431 for (i = 0; i < pMod->cSegments; i++)
432 {
433 printf("seg #%d: pvUser=%p enmProt=%d Name: '%.*s' (%d bytes)\n",
434 i, pMod->aSegments[i].pvUser, pMod->aSegments[i].enmProt,
435 pMod->aSegments[i].cchName, pMod->aSegments[i].pchName, pMod->aSegments[i].cchName);
436 printf("LinkAddress: %016" PRI_KLDRADDR " cb: %016" PRI_KLDRSIZE " Alignment=%08" PRI_KLDRADDR " \n",
437 pMod->aSegments[i].LinkAddress, pMod->aSegments[i].cb, pMod->aSegments[i].Alignment);
438 printf(" RVA: %016" PRI_KLDRADDR " cbMapped: %016" PRI_KLDRSIZE " MapAddress=%p\n",
439 pMod->aSegments[i].RVA, (KLDRSIZE)pMod->aSegments[i].cbMapped, (void *)pMod->aSegments[i].MapAddress);
440 printf(" offFile: %016" PRI_KLDRADDR " cbFile: %016" PRI_KLDRSIZE "\n",
441 (KLDRADDR)pMod->aSegments[i].offFile, (KLDRSIZE)pMod->aSegments[i].cbFile);
442 printf("\n");
443
444 if (pMod->aSegments[i].pvUser != NULL)
445 return Failure("Bad pvUser");
446 if (pMod->aSegments[i].enmProt >= KPROT_END || pMod->aSegments[i].enmProt <= KPROT_INVALID)
447 return Failure("Bad enmProt");
448 if (pMod->aSegments[i].MapAddress != 0)
449 return Failure("Bad MapAddress");
450 if (pMod->aSegments[i].cbMapped < pMod->aSegments[i].cb)
451 return Failure("Bad cbMapped (1)");
452 if (pMod->aSegments[i].cbMapped && !pMod->aSegments[i].Alignment)
453 return Failure("Bad cbMapped (2)");
454 if (pMod->aSegments[i].cbMapped > kLdrModSize(pMod))
455 return Failure("Bad cbMapped (3)");
456 if ( pMod->aSegments[i].Alignment
457 && (pMod->aSegments[i].RVA & (pMod->aSegments[i].Alignment - 1)))
458 return Failure("Bad RVA (1)");
459 if (pMod->aSegments[i].RVA != NIL_KLDRADDR && !pMod->aSegments[i].Alignment)
460 return Failure("Bad RVA (2)");
461 if ( pMod->aSegments[i].RVA != NIL_KLDRADDR
462 && pMod->aSegments[i].RVA >= kLdrModSize(pMod))
463 return Failure("Bad RVA (3)");
464 if ( pMod->aSegments[i].RVA != NIL_KLDRADDR
465 && pMod->aSegments[i].RVA + pMod->aSegments[i].cbMapped > kLdrModSize(pMod))
466 return Failure("Bad RVA/cbMapped (4)");
467 if (pMod->aSegments[i].LinkAddress != NIL_KLDRADDR && !pMod->aSegments[i].Alignment)
468 return Failure("Bad LinkAddress");
469 if ( pMod->aSegments[i].LinkAddress != NIL_KLDRADDR
470 && (pMod->aSegments[i].LinkAddress) & (pMod->aSegments[i].Alignment - 1))
471 return Failure("Bad LinkAddress alignment");
472 if (pMod->aSegments[i].offFile != -1 && pMod->aSegments[i].cbFile == -1)
473 return Failure("Bad offFile");
474 if (pMod->aSegments[i].offFile == -1 && pMod->aSegments[i].cbFile != -1)
475 return Failure("Bad cbFile");
476 }
477
478
479 /*
480 * Get image the size and query the image bits.
481 */
482 printf("* Testing user mapping...\n");
483
484 cbImage = (KSIZE)kLdrModSize(pMod);
485 if (cbImage != kLdrModSize(pMod))
486 return Failure("aborting test because the image is too huge!");
487 pvBits = malloc((KSIZE)cbImage);
488 if (!pvBits)
489 return Failure("failed to allocate %d bytes for the image", cbImage);
490
491 rc = kLdrModGetBits(pMod, pvBits, (KUPTR)pvBits, BasicTestsGetImport, NULL);
492 if (rc)
493 return Failure("failed to get image bits, rc=%d (%s)", rc, kErrName(rc));
494
495 /*
496 * Another cleanup nesting.
497 */
498 rc = BasicTestsSub2(pMod, pvBits);
499 if (!rc)
500 {
501 /*
502 * Test relocating the bits in a few different ways before we're done with them.
503 */
504 void *pvBits2 = malloc((KSIZE)cbImage);
505 if (pvBits2)
506 {
507 rc = BasicTestsRelocate(pMod, pvBits, pvBits2);
508 free(pvBits2);
509 }
510 else
511 rc = Failure("failed to allocate %d bytes for the 2nd image", cbImage);
512 }
513
514 free(pvBits);
515 return rc;
516}
517
518
519/**
520 * Tests the mapping related api, after mapping.
521 */
522static int BasicTestsSubMap2(PKLDRMOD pMod)
523{
524 int rc;
525
526 rc = kLdrModFixupMapping(pMod, BasicTestsGetImport, NULL);
527 if (rc)
528 return Failure("kLdrModFixupMapping (a) failed, rc=%d (%s)", rc, kErrName(rc));
529
530 rc = kLdrModReload(pMod);
531 if (rc)
532 return Failure("kLdrModReload (a) failed, rc=%d (%s)", rc, kErrName(rc));
533
534 rc = kLdrModReload(pMod);
535 if (rc)
536 return Failure("kLdrModReload (b) failed, rc=%d (%s)", rc, kErrName(rc));
537
538 rc = kLdrModFixupMapping(pMod, BasicTestsGetImport, NULL);
539 if (rc)
540 return Failure("kLdrModFixupMapping (b) failed, rc=%d (%s)", rc, kErrName(rc));
541
542 rc = kLdrModAllocTLS(pMod);
543 if (rc)
544 return Failure("kLdrModAllocTLS (a) failed, rc=%d (%s)", rc, kErrName(rc));
545 kLdrModFreeTLS(pMod);
546
547 rc = kLdrModAllocTLS(pMod);
548 if (rc)
549 return Failure("kLdrModAllocTLS (b) failed, rc=%d (%s)", rc, kErrName(rc));
550 kLdrModFreeTLS(pMod);
551
552 /*
553 * Repeat the BasicTestsSub2 with pvBits as NULL to test module
554 * interpreters that can utilize the mapping.
555 */
556 rc = BasicTestsSub2(pMod, NULL);
557 if (rc)
558 return Failure("BasicTestsSub2 in Map2 failed, rc=%d (%s)", rc, kErrName(rc));
559 return 0;
560}
561
562
563/**
564 * Tests the mapping related api.
565 */
566static int BasicTestsSubMap(PKLDRMOD pMod)
567{
568 int rc, rc2;
569 printf("* Mapping tests...\n");
570
571 rc = kLdrModMap(pMod);
572 if (rc)
573 return Failure("kLdrModMap failed, rc=%d (%s)", rc, kErrName(rc));
574 rc = BasicTestsSubMap2(pMod);
575 rc2 = kLdrModUnmap(pMod);
576 if (rc2)
577 {
578 Failure("kLdrModUnmap failed, rc=%d (%s)", rc2, kErrName(rc2));
579 rc = rc ? rc : rc2;
580 }
581
582 printf("* Mapping tests done.\n");
583 return rc;
584}
585
586
587/**
588 * Performs basic module loader tests on the specified file.
589 */
590static int BasicTests(const char *pszFilename)
591{
592 PKLDRMOD pMod;
593 int rc, rc2;
594
595 printf("tstLdrMod: Testing '%s'", pszFilename);
596 rc = kLdrModOpen(pszFilename, &pMod);
597 if (!rc)
598 {
599 rc = BasicTestsSub(pMod);
600 if (!rc)
601 rc = BasicTestsSubMap(pMod);
602 if (!rc)
603 rc = BasicTestsSub2(pMod, NULL);
604 rc2 = kLdrModClose(pMod);
605 if (rc2)
606 Failure("failed to close '%s', rc=%d (%s)", pszFilename, rc, kErrName(rc));
607 if (rc2 && !rc)
608 rc = rc2;
609 }
610 else
611 Failure("Failed to open '%s', rc=%d (%s)", pszFilename, rc, kErrName(rc));
612 return rc ? 1 : 0;
613}
614
615
616int main(int argc, char **argv)
617{
618 BasicTests(argv[argc-1]);
619
620 if (!g_cErrors)
621 printf("tstLdrMod: SUCCESS\n");
622 else
623 printf("tstLdrMod: FAILURE - %d errors\n", g_cErrors);
624 return !!g_cErrors;
625}
626
Note: See TracBrowser for help on using the repository browser.